blob: fa5f68ffeb01c2498433fd759daa8d09e39ab197 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse11f0ef2016-05-01 11:36:10 -06002/*
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glasse11f0ef2016-05-01 11:36:10 -06005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_IDE
8
Simon Glasse11f0ef2016-05-01 11:36:10 -06009#include <common.h>
10#include <ata.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <blk.h>
Simon Glassbe7c6662023-01-17 10:47:46 -070012#include <bootdev.h>
Simon Glassf94bea02016-05-01 11:36:22 -060013#include <dm.h>
Simon Glasse11f0ef2016-05-01 11:36:10 -060014#include <ide.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060016#include <part.h>
Simon Glasse11f0ef2016-05-01 11:36:10 -060017#include <watchdog.h>
18#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glasse11f0ef2016-05-01 11:36:10 -060020
21#ifdef __PPC__
22# define EIEIO __asm__ volatile ("eieio")
23# define SYNC __asm__ volatile ("sync")
24#else
25# define EIEIO /* nothing */
26# define SYNC /* nothing */
27#endif
28
29/* Current offset for IDE0 / IDE1 bus access */
30ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
31#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
32 CONFIG_SYS_ATA_IDE0_OFFSET,
33#endif
34#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
35 CONFIG_SYS_ATA_IDE1_OFFSET,
36#endif
37};
38
Simon Glass122267c2023-04-25 10:54:26 -060039#define ATA_CURR_BASE(dev) (CONFIG_SYS_ATA_BASE_ADDR + \
40 ide_bus_offset[IDE_BUS(dev)])
41
Simon Glasse11f0ef2016-05-01 11:36:10 -060042static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
43
44struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
45
46#define IDE_TIME_OUT 2000 /* 2 sec timeout */
47
48#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
49
50#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
51
Simon Glasse11f0ef2016-05-01 11:36:10 -060052#ifdef CONFIG_IDE_RESET
Simon Glasse11f0ef2016-05-01 11:36:10 -060053static void ide_reset(void)
54{
55 int i;
56
57 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
58 ide_bus_ok[i] = 0;
Simon Glasse11f0ef2016-05-01 11:36:10 -060059
60 ide_set_reset(1); /* assert reset */
61
62 /* the reset signal shall be asserted for et least 25 us */
63 udelay(25);
64
Stefan Roese80877fa2022-09-02 14:10:46 +020065 schedule();
Simon Glasse11f0ef2016-05-01 11:36:10 -060066
67 /* de-assert RESET signal */
68 ide_set_reset(0);
69
Simon Glass837ca302023-04-25 10:54:27 -060070 mdelay(250);
Simon Glasse11f0ef2016-05-01 11:36:10 -060071}
72#else
73#define ide_reset() /* dummy */
74#endif /* CONFIG_IDE_RESET */
75
Simon Glass52bae6d2023-04-25 10:54:33 -060076static void ide_outb(int dev, int port, unsigned char val)
Simon Glasscc22a1c2023-04-25 10:54:32 -060077{
78 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
79 dev, port, val, ATA_CURR_BASE(dev) + port);
80
81 outb(val, ATA_CURR_BASE(dev) + port);
82}
83
Simon Glass52bae6d2023-04-25 10:54:33 -060084static unsigned char ide_inb(int dev, int port)
Simon Glasscc22a1c2023-04-25 10:54:32 -060085{
86 uchar val;
87
88 val = inb(ATA_CURR_BASE(dev) + port);
89
90 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
91 dev, port, ATA_CURR_BASE(dev) + port, val);
92 return val;
93}
94
Simon Glass52bae6d2023-04-25 10:54:33 -060095static void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Simon Glasscc22a1c2023-04-25 10:54:32 -060096{
97 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
98 ushort *dbuf = (ushort *)sect_buf;
99
100 debug("in input swap data base for read is %p\n", (void *)paddr);
101
102 while (words--) {
103 EIEIO;
104 *dbuf++ = be16_to_cpu(inw(paddr));
105 EIEIO;
106 *dbuf++ = be16_to_cpu(inw(paddr));
107 }
108}
109
Simon Glasse11f0ef2016-05-01 11:36:10 -0600110/*
111 * Wait until Busy bit is off, or timeout (in ms)
112 * Return last status
113 */
114static uchar ide_wait(int dev, ulong t)
115{
116 ulong delay = 10 * t; /* poll every 100 us */
117 uchar c;
118
119 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
120 udelay(100);
121 if (delay-- == 0)
122 break;
123 }
124 return c;
125}
126
127/*
128 * copy src to dest, skipping leading and trailing blanks and null
129 * terminate the string
130 * "len" is the size of available memory including the terminating '\0'
131 */
132static void ident_cpy(unsigned char *dst, unsigned char *src,
133 unsigned int len)
134{
135 unsigned char *end, *last;
136
137 last = dst;
138 end = src + len - 1;
139
140 /* reserve space for '\0' */
141 if (len < 2)
142 goto OUT;
143
144 /* skip leading white space */
145 while ((*src) && (src < end) && (*src == ' '))
146 ++src;
147
148 /* copy string, omitting trailing white space */
149 while ((*src) && (src < end)) {
150 *dst++ = *src;
151 if (*src++ != ' ')
152 last = dst;
153 }
154OUT:
155 *last = '\0';
156}
157
158#ifdef CONFIG_ATAPI
159/****************************************************************************
160 * ATAPI Support
161 */
162
Simon Glasse11f0ef2016-05-01 11:36:10 -0600163/* since ATAPI may use commands with not 4 bytes alligned length
164 * we have our own transfer functions, 2 bytes alligned */
Simon Glass52bae6d2023-04-25 10:54:33 -0600165static void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600166{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100167 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600168 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600169
Simon Glasse11f0ef2016-05-01 11:36:10 -0600170 dbuf = (ushort *)sect_buf;
171
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100172 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600173
174 while (shorts--) {
175 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100176 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600177 }
178}
179
Simon Glass52bae6d2023-04-25 10:54:33 -0600180static void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600181{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100182 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600183 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600184
Simon Glasse11f0ef2016-05-01 11:36:10 -0600185 dbuf = (ushort *)sect_buf;
186
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100187 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600188
189 while (shorts--) {
190 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100191 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600192 }
193}
194
Simon Glasse11f0ef2016-05-01 11:36:10 -0600195/*
196 * Wait until (Status & mask) == res, or timeout (in ms)
197 * Return last status
198 * This is used since some ATAPI CD ROMs clears their Busy Bit first
199 * and then they set their DRQ Bit
200 */
201static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
202{
203 ulong delay = 10 * t; /* poll every 100 us */
204 uchar c;
205
206 /* prevents to read the status before valid */
207 c = ide_inb(dev, ATA_DEV_CTL);
208
209 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
210 /* break if error occurs (doesn't make sense to wait more) */
211 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
212 break;
213 udelay(100);
214 if (delay-- == 0)
215 break;
216 }
217 return c;
218}
219
220/*
221 * issue an atapi command
222 */
223unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
224 unsigned char *buffer, int buflen)
225{
226 unsigned char c, err, mask, res;
227 int n;
228
Simon Glasse11f0ef2016-05-01 11:36:10 -0600229 /* Select device
230 */
231 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
232 res = 0;
233 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
234 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
235 if ((c & mask) != res) {
236 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
237 c);
238 err = 0xFF;
239 goto AI_OUT;
240 }
241 /* write taskfile */
242 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
243 ide_outb(device, ATA_SECT_CNT, 0);
244 ide_outb(device, ATA_SECT_NUM, 0);
245 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
246 ide_outb(device, ATA_CYL_HIGH,
247 (unsigned char) ((buflen >> 8) & 0xFF));
248 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
249
Heinrich Schuchardt203d83a2020-02-27 18:28:00 +0100250 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600251 udelay(50);
252
253 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
254 res = ATA_STAT_DRQ;
255 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
256
257 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
258 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
259 device, c);
260 err = 0xFF;
261 goto AI_OUT;
262 }
263
264 /* write command block */
265 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
266
267 /* ATAPI Command written wait for completition */
Simon Glass837ca302023-04-25 10:54:27 -0600268 mdelay(5); /* device must set bsy */
Simon Glasse11f0ef2016-05-01 11:36:10 -0600269
270 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
271 /*
272 * if no data wait for DRQ = 0 BSY = 0
273 * if data wait for DRQ = 1 BSY = 0
274 */
275 res = 0;
276 if (buflen)
277 res = ATA_STAT_DRQ;
278 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
279 if ((c & mask) != res) {
280 if (c & ATA_STAT_ERR) {
281 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
282 debug("atapi_issue 1 returned sense key %X status %02X\n",
283 err, c);
284 } else {
285 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
286 ccb[0], c);
287 err = 0xFF;
288 }
289 goto AI_OUT;
290 }
291 n = ide_inb(device, ATA_CYL_HIGH);
292 n <<= 8;
293 n += ide_inb(device, ATA_CYL_LOW);
294 if (n > buflen) {
295 printf("ERROR, transfer bytes %d requested only %d\n", n,
296 buflen);
297 err = 0xff;
298 goto AI_OUT;
299 }
300 if ((n == 0) && (buflen < 0)) {
301 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
302 err = 0xff;
303 goto AI_OUT;
304 }
305 if (n != buflen) {
306 debug("WARNING, transfer bytes %d not equal with requested %d\n",
307 n, buflen);
308 }
309 if (n != 0) { /* data transfer */
310 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
311 /* we transfer shorts */
312 n >>= 1;
313 /* ok now decide if it is an in or output */
314 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
315 debug("Write to device\n");
316 ide_output_data_shorts(device, (unsigned short *)buffer,
317 n);
318 } else {
319 debug("Read from device @ %p shorts %d\n", buffer, n);
320 ide_input_data_shorts(device, (unsigned short *)buffer,
321 n);
322 }
323 }
Simon Glass837ca302023-04-25 10:54:27 -0600324 mdelay(5); /* seems that some CD ROMs need this... */
Simon Glasse11f0ef2016-05-01 11:36:10 -0600325 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
326 res = 0;
327 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
328 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
329 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
330 debug("atapi_issue 2 returned sense key %X status %X\n", err,
331 c);
332 } else {
333 err = 0;
334 }
335AI_OUT:
Simon Glasse11f0ef2016-05-01 11:36:10 -0600336 return err;
337}
338
339/*
340 * sending the command to atapi_issue. If an status other than good
341 * returns, an request_sense will be issued
342 */
343
344#define ATAPI_DRIVE_NOT_READY 100
345#define ATAPI_UNIT_ATTN 10
346
347unsigned char atapi_issue_autoreq(int device,
348 unsigned char *ccb,
349 int ccblen,
350 unsigned char *buffer, int buflen)
351{
352 unsigned char sense_data[18], sense_ccb[12];
353 unsigned char res, key, asc, ascq;
354 int notready, unitattn;
355
356 unitattn = ATAPI_UNIT_ATTN;
357 notready = ATAPI_DRIVE_NOT_READY;
358
359retry:
360 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
361 if (res == 0)
362 return 0; /* Ok */
363
364 if (res == 0xFF)
365 return 0xFF; /* error */
366
367 debug("(auto_req)atapi_issue returned sense key %X\n", res);
368
369 memset(sense_ccb, 0, sizeof(sense_ccb));
370 memset(sense_data, 0, sizeof(sense_data));
371 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
372 sense_ccb[4] = 18; /* allocation Length */
373
374 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
375 key = (sense_data[2] & 0xF);
376 asc = (sense_data[12]);
377 ascq = (sense_data[13]);
378
379 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
380 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
381 sense_data[0], key, asc, ascq);
382
383 if ((key == 0))
384 return 0; /* ok device ready */
385
386 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
387 if (unitattn-- > 0) {
Simon Glass837ca302023-04-25 10:54:27 -0600388 mdelay(200);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600389 goto retry;
390 }
391 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
392 goto error;
393 }
394 if ((asc == 0x4) && (ascq == 0x1)) {
395 /* not ready, but will be ready soon */
396 if (notready-- > 0) {
Simon Glass837ca302023-04-25 10:54:27 -0600397 mdelay(200);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600398 goto retry;
399 }
400 printf("Drive not ready, tried %d times\n",
401 ATAPI_DRIVE_NOT_READY);
402 goto error;
403 }
404 if (asc == 0x3a) {
405 debug("Media not present\n");
406 goto error;
407 }
408
409 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
410 ascq);
411error:
412 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
413 return 0xFF;
414}
415
416/*
417 * atapi_read:
418 * we transfer only one block per command, since the multiple DRQ per
419 * command is not yet implemented
420 */
421#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
422#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
423#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
424
425ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
426 void *buffer)
427{
428 int device = block_dev->devnum;
429 ulong n = 0;
430 unsigned char ccb[12]; /* Command descriptor block */
431 ulong cnt;
432
433 debug("atapi_read dev %d start " LBAF " blocks " LBAF
434 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
435
436 do {
437 if (blkcnt > ATAPI_READ_MAX_BLOCK)
438 cnt = ATAPI_READ_MAX_BLOCK;
439 else
440 cnt = blkcnt;
441
442 ccb[0] = ATAPI_CMD_READ_12;
443 ccb[1] = 0; /* reserved */
444 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
445 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
446 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
447 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
448 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
449 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
450 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
451 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
452 ccb[10] = 0; /* reserved */
453 ccb[11] = 0; /* reserved */
454
455 if (atapi_issue_autoreq(device, ccb, 12,
456 (unsigned char *)buffer,
457 cnt * ATAPI_READ_BLOCK_SIZE)
458 == 0xFF) {
459 return n;
460 }
461 n += cnt;
462 blkcnt -= cnt;
463 blknr += cnt;
464 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
465 } while (blkcnt > 0);
466 return n;
467}
468
469static void atapi_inquiry(struct blk_desc *dev_desc)
470{
471 unsigned char ccb[12]; /* Command descriptor block */
472 unsigned char iobuf[64]; /* temp buf */
473 unsigned char c;
474 int device;
475
476 device = dev_desc->devnum;
477 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse11f0ef2016-05-01 11:36:10 -0600478
479 memset(ccb, 0, sizeof(ccb));
480 memset(iobuf, 0, sizeof(iobuf));
481
482 ccb[0] = ATAPI_CMD_INQUIRY;
483 ccb[4] = 40; /* allocation Legnth */
484 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
485
486 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
487 if (c != 0)
488 return;
489
490 /* copy device ident strings */
491 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
492 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
493 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
494
495 dev_desc->lun = 0;
496 dev_desc->lba = 0;
497 dev_desc->blksz = 0;
498 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
499 dev_desc->type = iobuf[0] & 0x1f;
500
501 if ((iobuf[1] & 0x80) == 0x80)
502 dev_desc->removable = 1;
503 else
504 dev_desc->removable = 0;
505
506 memset(ccb, 0, sizeof(ccb));
507 memset(iobuf, 0, sizeof(iobuf));
508 ccb[0] = ATAPI_CMD_START_STOP;
509 ccb[4] = 0x03; /* start */
510
511 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
512
513 debug("ATAPI_CMD_START_STOP returned %x\n", c);
514 if (c != 0)
515 return;
516
517 memset(ccb, 0, sizeof(ccb));
518 memset(iobuf, 0, sizeof(iobuf));
519 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
520
521 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
522 if (c != 0)
523 return;
524
525 memset(ccb, 0, sizeof(ccb));
526 memset(iobuf, 0, sizeof(iobuf));
527 ccb[0] = ATAPI_CMD_READ_CAP;
528 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
529 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
530 if (c != 0)
531 return;
532
533 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
534 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
535 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
536
537 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
538 ((unsigned long) iobuf[1] << 16) +
539 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
540 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
541 ((unsigned long) iobuf[5] << 16) +
542 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
543 dev_desc->log2blksz = LOG2(dev_desc->blksz);
544#ifdef CONFIG_LBA48
545 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
546 dev_desc->lba48 = 0;
547#endif
548 return;
549}
550
551#endif /* CONFIG_ATAPI */
552
553static void ide_ident(struct blk_desc *dev_desc)
554{
555 unsigned char c;
556 hd_driveid_t iop;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600557#ifdef CONFIG_ATAPI
Simon Glass0cdb2932022-08-11 19:34:53 -0600558 bool is_atapi = false;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600559 int retries = 0;
560#endif
561 int device;
562
563 device = dev_desc->devnum;
564 printf(" Device %d: ", device);
565
Simon Glasse11f0ef2016-05-01 11:36:10 -0600566 /* Select device
567 */
568 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glassfada3f92022-09-17 09:00:09 -0600569 dev_desc->uclass_id = UCLASS_IDE;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600570#ifdef CONFIG_ATAPI
571
572 retries = 0;
573
574 /* Warning: This will be tricky to read */
575 while (retries <= 1) {
576 /* check signature */
577 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
578 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
579 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
580 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
581 /* ATAPI Signature found */
Simon Glass0cdb2932022-08-11 19:34:53 -0600582 is_atapi = true;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600583 /*
584 * Start Ident Command
585 */
Heinrich Schuchardt203d83a2020-02-27 18:28:00 +0100586 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600587 /*
588 * Wait for completion - ATAPI devices need more time
589 * to become ready
590 */
591 c = ide_wait(device, ATAPI_TIME_OUT);
592 } else
593#endif
594 {
595 /*
596 * Start Ident Command
597 */
Heinrich Schuchardt203d83a2020-02-27 18:28:00 +0100598 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600599
600 /*
601 * Wait for completion
602 */
603 c = ide_wait(device, IDE_TIME_OUT);
604 }
Simon Glasse11f0ef2016-05-01 11:36:10 -0600605
606 if (((c & ATA_STAT_DRQ) == 0) ||
607 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
608#ifdef CONFIG_ATAPI
609 {
610 /*
611 * Need to soft reset the device
612 * in case it's an ATAPI...
613 */
614 debug("Retrying...\n");
615 ide_outb(device, ATA_DEV_HD,
616 ATA_LBA | ATA_DEVICE(device));
Simon Glass837ca302023-04-25 10:54:27 -0600617 mdelay(100);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600618 ide_outb(device, ATA_COMMAND, 0x08);
Simon Glass837ca302023-04-25 10:54:27 -0600619 mdelay(500);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600620 }
621 /*
622 * Select device
623 */
624 ide_outb(device, ATA_DEV_HD,
625 ATA_LBA | ATA_DEVICE(device));
626 retries++;
627#else
628 return;
629#endif
630 }
631#ifdef CONFIG_ATAPI
632 else
633 break;
634 } /* see above - ugly to read */
635
636 if (retries == 2) /* Not found */
637 return;
638#endif
639
640 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
641
642 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
643 sizeof(dev_desc->revision));
644 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
645 sizeof(dev_desc->vendor));
646 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
647 sizeof(dev_desc->product));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600648
649 if ((iop.config & 0x0080) == 0x0080)
650 dev_desc->removable = 1;
651 else
652 dev_desc->removable = 0;
653
654#ifdef CONFIG_ATAPI
Simon Glass0cdb2932022-08-11 19:34:53 -0600655 if (is_atapi) {
Simon Glasse11f0ef2016-05-01 11:36:10 -0600656 atapi_inquiry(dev_desc);
657 return;
658 }
659#endif /* CONFIG_ATAPI */
660
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100661 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
662 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
663 dev_desc->lba =
664 ((unsigned long)iop.lba_capacity[0]) |
665 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600666
667#ifdef CONFIG_LBA48
668 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
669 dev_desc->lba48 = 1;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100670 for (int i = 0; i < 4; i++)
671 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
672 dev_desc->lba =
673 ((unsigned long long)iop.lba48_capacity[0] |
674 ((unsigned long long)iop.lba48_capacity[1] << 16) |
675 ((unsigned long long)iop.lba48_capacity[2] << 32) |
676 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600677 } else {
678 dev_desc->lba48 = 0;
679 }
680#endif /* CONFIG_LBA48 */
681 /* assuming HD */
682 dev_desc->type = DEV_TYPE_HARDDISK;
683 dev_desc->blksz = ATA_BLOCKSIZE;
684 dev_desc->log2blksz = LOG2(dev_desc->blksz);
685 dev_desc->lun = 0; /* just to fill something in... */
686
687#if 0 /* only used to test the powersaving mode,
688 * if enabled, the drive goes after 5 sec
689 * in standby mode */
690 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
691 c = ide_wait(device, IDE_TIME_OUT);
692 ide_outb(device, ATA_SECT_CNT, 1);
693 ide_outb(device, ATA_LBA_LOW, 0);
694 ide_outb(device, ATA_LBA_MID, 0);
695 ide_outb(device, ATA_LBA_HIGH, 0);
696 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
697 ide_outb(device, ATA_COMMAND, 0xe3);
698 udelay(50);
699 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
700#endif
701}
702
Simon Glass175e7662023-04-25 10:54:30 -0600703static void ide_init(void)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600704{
705 unsigned char c;
706 int i, bus;
707
Stefan Roese80877fa2022-09-02 14:10:46 +0200708 schedule();
Simon Glasse11f0ef2016-05-01 11:36:10 -0600709
Simon Glasse11f0ef2016-05-01 11:36:10 -0600710 /* ATAPI Drives seems to need a proper IDE Reset */
711 ide_reset();
712
Simon Glasse11f0ef2016-05-01 11:36:10 -0600713 /*
714 * Wait for IDE to get ready.
715 * According to spec, this can take up to 31 seconds!
716 */
717 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
718 int dev =
719 bus * (CONFIG_SYS_IDE_MAXDEVICE /
720 CONFIG_SYS_IDE_MAXBUS);
721
Simon Glasse11f0ef2016-05-01 11:36:10 -0600722 printf("Bus %d: ", bus);
723
724 ide_bus_ok[bus] = 0;
725
Simon Glass837ca302023-04-25 10:54:27 -0600726 /* Select device */
727 mdelay(100);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600728 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
Simon Glass837ca302023-04-25 10:54:27 -0600729 mdelay(100);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600730 i = 0;
731 do {
Simon Glass837ca302023-04-25 10:54:27 -0600732 mdelay(10);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600733
734 c = ide_inb(dev, ATA_STATUS);
735 i++;
736 if (i > (ATA_RESET_TIME * 100)) {
737 puts("** Timeout **\n");
Simon Glasse11f0ef2016-05-01 11:36:10 -0600738 return;
739 }
740 if ((i >= 100) && ((i % 100) == 0))
741 putc('.');
742
743 } while (c & ATA_STAT_BUSY);
744
745 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
746 puts("not available ");
747 debug("Status = 0x%02X ", c);
748#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
749 } else if ((c & ATA_STAT_READY) == 0) {
750 puts("not available ");
751 debug("Status = 0x%02X ", c);
752#endif
753 } else {
754 puts("OK ");
755 ide_bus_ok[bus] = 1;
756 }
Stefan Roese80877fa2022-09-02 14:10:46 +0200757 schedule();
Simon Glasse11f0ef2016-05-01 11:36:10 -0600758 }
759
760 putc('\n');
761
Simon Glasse11f0ef2016-05-01 11:36:10 -0600762 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse11f0ef2016-05-01 11:36:10 -0600763 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
Simon Glassfada3f92022-09-17 09:00:09 -0600764 ide_dev_desc[i].uclass_id = UCLASS_IDE;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600765 ide_dev_desc[i].devnum = i;
766 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
767 ide_dev_desc[i].blksz = 0;
768 ide_dev_desc[i].log2blksz =
769 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
770 ide_dev_desc[i].lba = 0;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600771 if (!ide_bus_ok[IDE_BUS(i)])
772 continue;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600773 ide_ident(&ide_dev_desc[i]);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600774 dev_print(&ide_dev_desc[i]);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600775 }
Stefan Roese80877fa2022-09-02 14:10:46 +0200776 schedule();
Simon Glasse11f0ef2016-05-01 11:36:10 -0600777}
778
Simon Glass52bae6d2023-04-25 10:54:33 -0600779static void ide_output_data(int dev, const ulong *sect_buf, int words)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600780{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100781 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600782 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600783
Simon Glasse11f0ef2016-05-01 11:36:10 -0600784 dbuf = (ushort *)sect_buf;
785 while (words--) {
786 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100787 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600788 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100789 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600790 }
791}
Simon Glasse11f0ef2016-05-01 11:36:10 -0600792
Simon Glass52bae6d2023-04-25 10:54:33 -0600793static void ide_input_data(int dev, ulong *sect_buf, int words)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600794{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100795 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600796 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600797
Simon Glasse11f0ef2016-05-01 11:36:10 -0600798 dbuf = (ushort *)sect_buf;
799
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100800 debug("in input data base for read is %p\n", (void *)paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600801
802 while (words--) {
803 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100804 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600805 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100806 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600807 }
Simon Glasse11f0ef2016-05-01 11:36:10 -0600808}
809
Simon Glassf94bea02016-05-01 11:36:22 -0600810ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
811 void *buffer)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600812{
Simon Glass71fa5b42020-12-03 16:55:18 -0700813 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600814 int device = block_dev->devnum;
815 ulong n = 0;
816 unsigned char c;
817 unsigned char pwrsave = 0; /* power save */
818
819#ifdef CONFIG_LBA48
820 unsigned char lba48 = 0;
821
822 if (blknr & 0x0000fffff0000000ULL) {
823 /* more than 28 bits used, use 48bit mode */
824 lba48 = 1;
825 }
826#endif
827 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
828 device, blknr, blkcnt, (ulong) buffer);
829
Simon Glasse11f0ef2016-05-01 11:36:10 -0600830 /* Select device
831 */
832 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
833 c = ide_wait(device, IDE_TIME_OUT);
834
835 if (c & ATA_STAT_BUSY) {
836 printf("IDE read: device %d not ready\n", device);
837 goto IDE_READ_E;
838 }
839
840 /* first check if the drive is in Powersaving mode, if yes,
841 * increase the timeout value */
Heinrich Schuchardt203d83a2020-02-27 18:28:00 +0100842 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600843 udelay(50);
844
845 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
846
847 if (c & ATA_STAT_BUSY) {
848 printf("IDE read: device %d not ready\n", device);
849 goto IDE_READ_E;
850 }
851 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
852 printf("No Powersaving mode %X\n", c);
853 } else {
854 c = ide_inb(device, ATA_SECT_CNT);
855 debug("Powersaving %02X\n", c);
856 if (c == 0)
857 pwrsave = 1;
858 }
859
860
861 while (blkcnt-- > 0) {
862 c = ide_wait(device, IDE_TIME_OUT);
863
864 if (c & ATA_STAT_BUSY) {
865 printf("IDE read: device %d not ready\n", device);
866 break;
867 }
868#ifdef CONFIG_LBA48
869 if (lba48) {
870 /* write high bits */
871 ide_outb(device, ATA_SECT_CNT, 0);
872 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
873#ifdef CONFIG_SYS_64BIT_LBA
874 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
875 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
876#else
877 ide_outb(device, ATA_LBA_MID, 0);
878 ide_outb(device, ATA_LBA_HIGH, 0);
879#endif
880 }
881#endif
882 ide_outb(device, ATA_SECT_CNT, 1);
883 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
884 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
885 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
886
887#ifdef CONFIG_LBA48
888 if (lba48) {
889 ide_outb(device, ATA_DEV_HD,
890 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100891 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600892
893 } else
894#endif
895 {
896 ide_outb(device, ATA_DEV_HD, ATA_LBA |
897 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100898 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600899 }
900
901 udelay(50);
902
903 if (pwrsave) {
904 /* may take up to 4 sec */
905 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
906 pwrsave = 0;
907 } else {
908 /* can't take over 500 ms */
909 c = ide_wait(device, IDE_TIME_OUT);
910 }
911
912 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
913 ATA_STAT_DRQ) {
914 printf("Error (no IRQ) dev %d blk " LBAF
915 ": status %#02x\n", device, blknr, c);
916 break;
917 }
918
919 ide_input_data(device, buffer, ATA_SECTORWORDS);
920 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
921
922 ++n;
923 ++blknr;
924 buffer += ATA_BLOCKSIZE;
925 }
926IDE_READ_E:
Simon Glasse11f0ef2016-05-01 11:36:10 -0600927 return n;
928}
929
Simon Glassf94bea02016-05-01 11:36:22 -0600930ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
931 const void *buffer)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600932{
Simon Glass71fa5b42020-12-03 16:55:18 -0700933 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600934 int device = block_dev->devnum;
935 ulong n = 0;
936 unsigned char c;
937
938#ifdef CONFIG_LBA48
939 unsigned char lba48 = 0;
940
941 if (blknr & 0x0000fffff0000000ULL) {
942 /* more than 28 bits used, use 48bit mode */
943 lba48 = 1;
944 }
945#endif
946
Simon Glasse11f0ef2016-05-01 11:36:10 -0600947 /* Select device
948 */
949 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
950
951 while (blkcnt-- > 0) {
952 c = ide_wait(device, IDE_TIME_OUT);
953
954 if (c & ATA_STAT_BUSY) {
955 printf("IDE read: device %d not ready\n", device);
956 goto WR_OUT;
957 }
958#ifdef CONFIG_LBA48
959 if (lba48) {
960 /* write high bits */
961 ide_outb(device, ATA_SECT_CNT, 0);
962 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
963#ifdef CONFIG_SYS_64BIT_LBA
964 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
965 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
966#else
967 ide_outb(device, ATA_LBA_MID, 0);
968 ide_outb(device, ATA_LBA_HIGH, 0);
969#endif
970 }
971#endif
972 ide_outb(device, ATA_SECT_CNT, 1);
973 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
974 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
975 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
976
977#ifdef CONFIG_LBA48
978 if (lba48) {
979 ide_outb(device, ATA_DEV_HD,
980 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100981 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600982
983 } else
984#endif
985 {
986 ide_outb(device, ATA_DEV_HD, ATA_LBA |
987 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100988 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600989 }
990
991 udelay(50);
992
993 /* can't take over 500 ms */
994 c = ide_wait(device, IDE_TIME_OUT);
995
996 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
997 ATA_STAT_DRQ) {
998 printf("Error (no IRQ) dev %d blk " LBAF
999 ": status %#02x\n", device, blknr, c);
1000 goto WR_OUT;
1001 }
1002
1003 ide_output_data(device, buffer, ATA_SECTORWORDS);
1004 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1005 ++n;
1006 ++blknr;
1007 buffer += ATA_BLOCKSIZE;
1008 }
1009WR_OUT:
Simon Glasse11f0ef2016-05-01 11:36:10 -06001010 return n;
1011}
1012
Bin Mengb650afa2017-09-10 05:12:51 -07001013static int ide_blk_probe(struct udevice *udev)
1014{
Simon Glass71fa5b42020-12-03 16:55:18 -07001015 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Mengb650afa2017-09-10 05:12:51 -07001016
1017 /* fill in device vendor/product/rev strings */
1018 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1019 BLK_VEN_SIZE);
1020 desc->vendor[BLK_VEN_SIZE] = '\0';
1021 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1022 BLK_PRD_SIZE);
1023 desc->product[BLK_PRD_SIZE] = '\0';
1024 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1025 BLK_REV_SIZE);
1026 desc->revision[BLK_REV_SIZE] = '\0';
1027
Bin Mengb650afa2017-09-10 05:12:51 -07001028 return 0;
1029}
1030
Simon Glassf94bea02016-05-01 11:36:22 -06001031static const struct blk_ops ide_blk_ops = {
1032 .read = ide_read,
1033 .write = ide_write,
1034};
1035
1036U_BOOT_DRIVER(ide_blk) = {
1037 .name = "ide_blk",
1038 .id = UCLASS_BLK,
1039 .ops = &ide_blk_ops,
Bin Mengb650afa2017-09-10 05:12:51 -07001040 .probe = ide_blk_probe,
1041};
1042
Simon Glassbe7c6662023-01-17 10:47:46 -07001043static int ide_bootdev_bind(struct udevice *dev)
1044{
1045 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
1046
Simon Glass7e1f6a42023-01-17 10:48:08 -07001047 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glassbe7c6662023-01-17 10:47:46 -07001048
1049 return 0;
1050}
1051
1052static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
1053{
Simon Glass175e7662023-04-25 10:54:30 -06001054 struct udevice *dev;
1055
1056 uclass_first_device(UCLASS_IDE, &dev);
Simon Glassbe7c6662023-01-17 10:47:46 -07001057
1058 return 0;
1059}
1060
1061struct bootdev_ops ide_bootdev_ops = {
1062};
1063
1064static const struct udevice_id ide_bootdev_ids[] = {
1065 { .compatible = "u-boot,bootdev-ide" },
1066 { }
1067};
1068
1069U_BOOT_DRIVER(ide_bootdev) = {
1070 .name = "ide_bootdev",
1071 .id = UCLASS_BOOTDEV,
1072 .ops = &ide_bootdev_ops,
1073 .bind = ide_bootdev_bind,
1074 .of_match = ide_bootdev_ids,
1075};
1076
1077BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glass7e1f6a42023-01-17 10:48:08 -07001078 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glassbe7c6662023-01-17 10:47:46 -07001079 .uclass = UCLASS_IDE,
1080 .hunt = ide_bootdev_hunt,
1081 .drv = DM_DRIVER_REF(ide_bootdev),
1082};
1083
Bin Mengb650afa2017-09-10 05:12:51 -07001084static int ide_probe(struct udevice *udev)
1085{
1086 struct udevice *blk_dev;
1087 char name[20];
1088 int blksz;
1089 lbaint_t size;
1090 int i;
1091 int ret;
1092
Simon Glass175e7662023-04-25 10:54:30 -06001093 ide_init();
1094
Bin Mengb650afa2017-09-10 05:12:51 -07001095 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1096 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1097 sprintf(name, "blk#%d", i);
1098
1099 blksz = ide_dev_desc[i].blksz;
1100 size = blksz * ide_dev_desc[i].lba;
Bin Meng5781c462017-09-10 05:12:52 -07001101
1102 /*
1103 * With CDROM, if there is no CD inserted, blksz will
1104 * be zero, don't bother to create IDE block device.
1105 */
1106 if (!blksz)
1107 continue;
Bin Mengb650afa2017-09-10 05:12:51 -07001108 ret = blk_create_devicef(udev, "ide_blk", name,
Simon Glassdbfa32c2022-08-11 19:34:59 -06001109 UCLASS_IDE, i,
Bin Mengb650afa2017-09-10 05:12:51 -07001110 blksz, size, &blk_dev);
1111 if (ret)
1112 return ret;
AKASHI Takahiroba748cf2022-03-08 20:36:44 +09001113
1114 ret = blk_probe_or_unbind(blk_dev);
1115 if (ret)
1116 return ret;
Simon Glassbe7c6662023-01-17 10:47:46 -07001117
1118 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1119 if (ret)
1120 return log_msg_ret("bootdev", ret);
Bin Mengb650afa2017-09-10 05:12:51 -07001121 }
1122 }
1123
1124 return 0;
1125}
1126
1127U_BOOT_DRIVER(ide) = {
1128 .name = "ide",
1129 .id = UCLASS_IDE,
1130 .probe = ide_probe,
1131};
1132
1133struct pci_device_id ide_supported[] = {
1134 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1135 { }
1136};
1137
1138U_BOOT_PCI_DEVICE(ide, ide_supported);
1139
1140UCLASS_DRIVER(ide) = {
1141 .name = "ide",
1142 .id = UCLASS_IDE,
Simon Glassf94bea02016-05-01 11:36:22 -06001143};