blob: 58f1ef8f1777333cc805d175141c019e27518930 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse9be1ee2016-05-01 11:36:10 -06002/*
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glasse9be1ee2016-05-01 11:36:10 -06005 */
6
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_IDE
8
Simon Glasse9be1ee2016-05-01 11:36:10 -06009#include <common.h>
10#include <ata.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <blk.h>
Simon Glass0d77f8f2023-01-17 10:47:46 -070012#include <bootdev.h>
Simon Glass145df842016-05-01 11:36:22 -060013#include <dm.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060014#include <ide.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060016#include <part.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060017#include <watchdog.h>
18#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glasse9be1ee2016-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 Glass14a4f522023-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 Glasse9be1ee2016-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 Glasse9be1ee2016-05-01 11:36:10 -060052#ifdef CONFIG_IDE_RESET
53extern void ide_set_reset(int idereset);
54
55static void ide_reset(void)
56{
57 int i;
58
59 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
60 ide_bus_ok[i] = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -060061
62 ide_set_reset(1); /* assert reset */
63
64 /* the reset signal shall be asserted for et least 25 us */
65 udelay(25);
66
Stefan Roese29caf932022-09-02 14:10:46 +020067 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -060068
69 /* de-assert RESET signal */
70 ide_set_reset(0);
71
Simon Glass4d89f4b2023-04-25 10:54:27 -060072 mdelay(250);
Simon Glasse9be1ee2016-05-01 11:36:10 -060073}
74#else
75#define ide_reset() /* dummy */
76#endif /* CONFIG_IDE_RESET */
77
Simon Glassbc65bff2023-04-25 10:54:32 -060078__weak void ide_outb(int dev, int port, unsigned char val)
79{
80 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
81 dev, port, val, ATA_CURR_BASE(dev) + port);
82
83 outb(val, ATA_CURR_BASE(dev) + port);
84}
85
86__weak unsigned char ide_inb(int dev, int port)
87{
88 uchar val;
89
90 val = inb(ATA_CURR_BASE(dev) + port);
91
92 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
93 dev, port, ATA_CURR_BASE(dev) + port, val);
94 return val;
95}
96
97__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
98{
99 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
100 ushort *dbuf = (ushort *)sect_buf;
101
102 debug("in input swap data base for read is %p\n", (void *)paddr);
103
104 while (words--) {
105 EIEIO;
106 *dbuf++ = be16_to_cpu(inw(paddr));
107 EIEIO;
108 *dbuf++ = be16_to_cpu(inw(paddr));
109 }
110}
111
Simon Glasse9be1ee2016-05-01 11:36:10 -0600112/*
113 * Wait until Busy bit is off, or timeout (in ms)
114 * Return last status
115 */
116static uchar ide_wait(int dev, ulong t)
117{
118 ulong delay = 10 * t; /* poll every 100 us */
119 uchar c;
120
121 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
122 udelay(100);
123 if (delay-- == 0)
124 break;
125 }
126 return c;
127}
128
129/*
130 * copy src to dest, skipping leading and trailing blanks and null
131 * terminate the string
132 * "len" is the size of available memory including the terminating '\0'
133 */
134static void ident_cpy(unsigned char *dst, unsigned char *src,
135 unsigned int len)
136{
137 unsigned char *end, *last;
138
139 last = dst;
140 end = src + len - 1;
141
142 /* reserve space for '\0' */
143 if (len < 2)
144 goto OUT;
145
146 /* skip leading white space */
147 while ((*src) && (src < end) && (*src == ' '))
148 ++src;
149
150 /* copy string, omitting trailing white space */
151 while ((*src) && (src < end)) {
152 *dst++ = *src;
153 if (*src++ != ' ')
154 last = dst;
155 }
156OUT:
157 *last = '\0';
158}
159
160#ifdef CONFIG_ATAPI
161/****************************************************************************
162 * ATAPI Support
163 */
164
Simon Glasse9be1ee2016-05-01 11:36:10 -0600165/* since ATAPI may use commands with not 4 bytes alligned length
166 * we have our own transfer functions, 2 bytes alligned */
167__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
168{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100169 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600170 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600171
Simon Glasse9be1ee2016-05-01 11:36:10 -0600172 dbuf = (ushort *)sect_buf;
173
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100174 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600175
176 while (shorts--) {
177 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100178 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600179 }
180}
181
182__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
183{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100184 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600185 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600186
Simon Glasse9be1ee2016-05-01 11:36:10 -0600187 dbuf = (ushort *)sect_buf;
188
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100189 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600190
191 while (shorts--) {
192 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100193 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600194 }
195}
196
Simon Glasse9be1ee2016-05-01 11:36:10 -0600197/*
198 * Wait until (Status & mask) == res, or timeout (in ms)
199 * Return last status
200 * This is used since some ATAPI CD ROMs clears their Busy Bit first
201 * and then they set their DRQ Bit
202 */
203static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
204{
205 ulong delay = 10 * t; /* poll every 100 us */
206 uchar c;
207
208 /* prevents to read the status before valid */
209 c = ide_inb(dev, ATA_DEV_CTL);
210
211 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
212 /* break if error occurs (doesn't make sense to wait more) */
213 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
214 break;
215 udelay(100);
216 if (delay-- == 0)
217 break;
218 }
219 return c;
220}
221
222/*
223 * issue an atapi command
224 */
225unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
226 unsigned char *buffer, int buflen)
227{
228 unsigned char c, err, mask, res;
229 int n;
230
Simon Glasse9be1ee2016-05-01 11:36:10 -0600231 /* Select device
232 */
233 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
234 res = 0;
235 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
236 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
237 if ((c & mask) != res) {
238 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
239 c);
240 err = 0xFF;
241 goto AI_OUT;
242 }
243 /* write taskfile */
244 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
245 ide_outb(device, ATA_SECT_CNT, 0);
246 ide_outb(device, ATA_SECT_NUM, 0);
247 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
248 ide_outb(device, ATA_CYL_HIGH,
249 (unsigned char) ((buflen >> 8) & 0xFF));
250 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
251
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100252 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600253 udelay(50);
254
255 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
256 res = ATA_STAT_DRQ;
257 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
258
259 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
260 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
261 device, c);
262 err = 0xFF;
263 goto AI_OUT;
264 }
265
266 /* write command block */
267 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
268
269 /* ATAPI Command written wait for completition */
Simon Glass4d89f4b2023-04-25 10:54:27 -0600270 mdelay(5); /* device must set bsy */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600271
272 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
273 /*
274 * if no data wait for DRQ = 0 BSY = 0
275 * if data wait for DRQ = 1 BSY = 0
276 */
277 res = 0;
278 if (buflen)
279 res = ATA_STAT_DRQ;
280 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
281 if ((c & mask) != res) {
282 if (c & ATA_STAT_ERR) {
283 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
284 debug("atapi_issue 1 returned sense key %X status %02X\n",
285 err, c);
286 } else {
287 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
288 ccb[0], c);
289 err = 0xFF;
290 }
291 goto AI_OUT;
292 }
293 n = ide_inb(device, ATA_CYL_HIGH);
294 n <<= 8;
295 n += ide_inb(device, ATA_CYL_LOW);
296 if (n > buflen) {
297 printf("ERROR, transfer bytes %d requested only %d\n", n,
298 buflen);
299 err = 0xff;
300 goto AI_OUT;
301 }
302 if ((n == 0) && (buflen < 0)) {
303 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
304 err = 0xff;
305 goto AI_OUT;
306 }
307 if (n != buflen) {
308 debug("WARNING, transfer bytes %d not equal with requested %d\n",
309 n, buflen);
310 }
311 if (n != 0) { /* data transfer */
312 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
313 /* we transfer shorts */
314 n >>= 1;
315 /* ok now decide if it is an in or output */
316 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
317 debug("Write to device\n");
318 ide_output_data_shorts(device, (unsigned short *)buffer,
319 n);
320 } else {
321 debug("Read from device @ %p shorts %d\n", buffer, n);
322 ide_input_data_shorts(device, (unsigned short *)buffer,
323 n);
324 }
325 }
Simon Glass4d89f4b2023-04-25 10:54:27 -0600326 mdelay(5); /* seems that some CD ROMs need this... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600327 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
328 res = 0;
329 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
330 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
331 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
332 debug("atapi_issue 2 returned sense key %X status %X\n", err,
333 c);
334 } else {
335 err = 0;
336 }
337AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600338 return err;
339}
340
341/*
342 * sending the command to atapi_issue. If an status other than good
343 * returns, an request_sense will be issued
344 */
345
346#define ATAPI_DRIVE_NOT_READY 100
347#define ATAPI_UNIT_ATTN 10
348
349unsigned char atapi_issue_autoreq(int device,
350 unsigned char *ccb,
351 int ccblen,
352 unsigned char *buffer, int buflen)
353{
354 unsigned char sense_data[18], sense_ccb[12];
355 unsigned char res, key, asc, ascq;
356 int notready, unitattn;
357
358 unitattn = ATAPI_UNIT_ATTN;
359 notready = ATAPI_DRIVE_NOT_READY;
360
361retry:
362 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
363 if (res == 0)
364 return 0; /* Ok */
365
366 if (res == 0xFF)
367 return 0xFF; /* error */
368
369 debug("(auto_req)atapi_issue returned sense key %X\n", res);
370
371 memset(sense_ccb, 0, sizeof(sense_ccb));
372 memset(sense_data, 0, sizeof(sense_data));
373 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
374 sense_ccb[4] = 18; /* allocation Length */
375
376 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
377 key = (sense_data[2] & 0xF);
378 asc = (sense_data[12]);
379 ascq = (sense_data[13]);
380
381 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
382 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
383 sense_data[0], key, asc, ascq);
384
385 if ((key == 0))
386 return 0; /* ok device ready */
387
388 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
389 if (unitattn-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600390 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600391 goto retry;
392 }
393 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
394 goto error;
395 }
396 if ((asc == 0x4) && (ascq == 0x1)) {
397 /* not ready, but will be ready soon */
398 if (notready-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600399 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600400 goto retry;
401 }
402 printf("Drive not ready, tried %d times\n",
403 ATAPI_DRIVE_NOT_READY);
404 goto error;
405 }
406 if (asc == 0x3a) {
407 debug("Media not present\n");
408 goto error;
409 }
410
411 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
412 ascq);
413error:
414 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
415 return 0xFF;
416}
417
418/*
419 * atapi_read:
420 * we transfer only one block per command, since the multiple DRQ per
421 * command is not yet implemented
422 */
423#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
424#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
425#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
426
427ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
428 void *buffer)
429{
430 int device = block_dev->devnum;
431 ulong n = 0;
432 unsigned char ccb[12]; /* Command descriptor block */
433 ulong cnt;
434
435 debug("atapi_read dev %d start " LBAF " blocks " LBAF
436 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
437
438 do {
439 if (blkcnt > ATAPI_READ_MAX_BLOCK)
440 cnt = ATAPI_READ_MAX_BLOCK;
441 else
442 cnt = blkcnt;
443
444 ccb[0] = ATAPI_CMD_READ_12;
445 ccb[1] = 0; /* reserved */
446 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
447 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
448 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
449 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
450 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
451 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
452 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
453 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
454 ccb[10] = 0; /* reserved */
455 ccb[11] = 0; /* reserved */
456
457 if (atapi_issue_autoreq(device, ccb, 12,
458 (unsigned char *)buffer,
459 cnt * ATAPI_READ_BLOCK_SIZE)
460 == 0xFF) {
461 return n;
462 }
463 n += cnt;
464 blkcnt -= cnt;
465 blknr += cnt;
466 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
467 } while (blkcnt > 0);
468 return n;
469}
470
471static void atapi_inquiry(struct blk_desc *dev_desc)
472{
473 unsigned char ccb[12]; /* Command descriptor block */
474 unsigned char iobuf[64]; /* temp buf */
475 unsigned char c;
476 int device;
477
478 device = dev_desc->devnum;
479 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600480
481 memset(ccb, 0, sizeof(ccb));
482 memset(iobuf, 0, sizeof(iobuf));
483
484 ccb[0] = ATAPI_CMD_INQUIRY;
485 ccb[4] = 40; /* allocation Legnth */
486 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
487
488 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
489 if (c != 0)
490 return;
491
492 /* copy device ident strings */
493 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
494 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
495 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
496
497 dev_desc->lun = 0;
498 dev_desc->lba = 0;
499 dev_desc->blksz = 0;
500 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
501 dev_desc->type = iobuf[0] & 0x1f;
502
503 if ((iobuf[1] & 0x80) == 0x80)
504 dev_desc->removable = 1;
505 else
506 dev_desc->removable = 0;
507
508 memset(ccb, 0, sizeof(ccb));
509 memset(iobuf, 0, sizeof(iobuf));
510 ccb[0] = ATAPI_CMD_START_STOP;
511 ccb[4] = 0x03; /* start */
512
513 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
514
515 debug("ATAPI_CMD_START_STOP returned %x\n", c);
516 if (c != 0)
517 return;
518
519 memset(ccb, 0, sizeof(ccb));
520 memset(iobuf, 0, sizeof(iobuf));
521 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
522
523 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
524 if (c != 0)
525 return;
526
527 memset(ccb, 0, sizeof(ccb));
528 memset(iobuf, 0, sizeof(iobuf));
529 ccb[0] = ATAPI_CMD_READ_CAP;
530 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
531 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
532 if (c != 0)
533 return;
534
535 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
536 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
537 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
538
539 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
540 ((unsigned long) iobuf[1] << 16) +
541 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
542 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
543 ((unsigned long) iobuf[5] << 16) +
544 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
545 dev_desc->log2blksz = LOG2(dev_desc->blksz);
546#ifdef CONFIG_LBA48
547 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
548 dev_desc->lba48 = 0;
549#endif
550 return;
551}
552
553#endif /* CONFIG_ATAPI */
554
555static void ide_ident(struct blk_desc *dev_desc)
556{
557 unsigned char c;
558 hd_driveid_t iop;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600559#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600560 bool is_atapi = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600561 int retries = 0;
562#endif
563 int device;
564
565 device = dev_desc->devnum;
566 printf(" Device %d: ", device);
567
Simon Glasse9be1ee2016-05-01 11:36:10 -0600568 /* Select device
569 */
570 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glass8149b152022-09-17 09:00:09 -0600571 dev_desc->uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600572#ifdef CONFIG_ATAPI
573
574 retries = 0;
575
576 /* Warning: This will be tricky to read */
577 while (retries <= 1) {
578 /* check signature */
579 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
580 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
581 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
582 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
583 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600584 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600585 /*
586 * Start Ident Command
587 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100588 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600589 /*
590 * Wait for completion - ATAPI devices need more time
591 * to become ready
592 */
593 c = ide_wait(device, ATAPI_TIME_OUT);
594 } else
595#endif
596 {
597 /*
598 * Start Ident Command
599 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100600 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600601
602 /*
603 * Wait for completion
604 */
605 c = ide_wait(device, IDE_TIME_OUT);
606 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600607
608 if (((c & ATA_STAT_DRQ) == 0) ||
609 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
610#ifdef CONFIG_ATAPI
611 {
612 /*
613 * Need to soft reset the device
614 * in case it's an ATAPI...
615 */
616 debug("Retrying...\n");
617 ide_outb(device, ATA_DEV_HD,
618 ATA_LBA | ATA_DEVICE(device));
Simon Glass4d89f4b2023-04-25 10:54:27 -0600619 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600620 ide_outb(device, ATA_COMMAND, 0x08);
Simon Glass4d89f4b2023-04-25 10:54:27 -0600621 mdelay(500);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600622 }
623 /*
624 * Select device
625 */
626 ide_outb(device, ATA_DEV_HD,
627 ATA_LBA | ATA_DEVICE(device));
628 retries++;
629#else
630 return;
631#endif
632 }
633#ifdef CONFIG_ATAPI
634 else
635 break;
636 } /* see above - ugly to read */
637
638 if (retries == 2) /* Not found */
639 return;
640#endif
641
642 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
643
644 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
645 sizeof(dev_desc->revision));
646 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
647 sizeof(dev_desc->vendor));
648 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
649 sizeof(dev_desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600650
651 if ((iop.config & 0x0080) == 0x0080)
652 dev_desc->removable = 1;
653 else
654 dev_desc->removable = 0;
655
656#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600657 if (is_atapi) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600658 atapi_inquiry(dev_desc);
659 return;
660 }
661#endif /* CONFIG_ATAPI */
662
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100663 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
664 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
665 dev_desc->lba =
666 ((unsigned long)iop.lba_capacity[0]) |
667 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600668
669#ifdef CONFIG_LBA48
670 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
671 dev_desc->lba48 = 1;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100672 for (int i = 0; i < 4; i++)
673 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
674 dev_desc->lba =
675 ((unsigned long long)iop.lba48_capacity[0] |
676 ((unsigned long long)iop.lba48_capacity[1] << 16) |
677 ((unsigned long long)iop.lba48_capacity[2] << 32) |
678 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600679 } else {
680 dev_desc->lba48 = 0;
681 }
682#endif /* CONFIG_LBA48 */
683 /* assuming HD */
684 dev_desc->type = DEV_TYPE_HARDDISK;
685 dev_desc->blksz = ATA_BLOCKSIZE;
686 dev_desc->log2blksz = LOG2(dev_desc->blksz);
687 dev_desc->lun = 0; /* just to fill something in... */
688
689#if 0 /* only used to test the powersaving mode,
690 * if enabled, the drive goes after 5 sec
691 * in standby mode */
692 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
693 c = ide_wait(device, IDE_TIME_OUT);
694 ide_outb(device, ATA_SECT_CNT, 1);
695 ide_outb(device, ATA_LBA_LOW, 0);
696 ide_outb(device, ATA_LBA_MID, 0);
697 ide_outb(device, ATA_LBA_HIGH, 0);
698 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
699 ide_outb(device, ATA_COMMAND, 0xe3);
700 udelay(50);
701 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
702#endif
703}
704
Simon Glass80778f52023-04-25 10:54:30 -0600705static void ide_init(void)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600706{
707 unsigned char c;
708 int i, bus;
709
Stefan Roese29caf932022-09-02 14:10:46 +0200710 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600711
Simon Glasse9be1ee2016-05-01 11:36:10 -0600712 /* ATAPI Drives seems to need a proper IDE Reset */
713 ide_reset();
714
Simon Glasse9be1ee2016-05-01 11:36:10 -0600715 /*
716 * Wait for IDE to get ready.
717 * According to spec, this can take up to 31 seconds!
718 */
719 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
720 int dev =
721 bus * (CONFIG_SYS_IDE_MAXDEVICE /
722 CONFIG_SYS_IDE_MAXBUS);
723
Simon Glasse9be1ee2016-05-01 11:36:10 -0600724 printf("Bus %d: ", bus);
725
726 ide_bus_ok[bus] = 0;
727
Simon Glass4d89f4b2023-04-25 10:54:27 -0600728 /* Select device */
729 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600730 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
Simon Glass4d89f4b2023-04-25 10:54:27 -0600731 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600732 i = 0;
733 do {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600734 mdelay(10);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600735
736 c = ide_inb(dev, ATA_STATUS);
737 i++;
738 if (i > (ATA_RESET_TIME * 100)) {
739 puts("** Timeout **\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600740 return;
741 }
742 if ((i >= 100) && ((i % 100) == 0))
743 putc('.');
744
745 } while (c & ATA_STAT_BUSY);
746
747 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
748 puts("not available ");
749 debug("Status = 0x%02X ", c);
750#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
751 } else if ((c & ATA_STAT_READY) == 0) {
752 puts("not available ");
753 debug("Status = 0x%02X ", c);
754#endif
755 } else {
756 puts("OK ");
757 ide_bus_ok[bus] = 1;
758 }
Stefan Roese29caf932022-09-02 14:10:46 +0200759 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600760 }
761
762 putc('\n');
763
Simon Glasse9be1ee2016-05-01 11:36:10 -0600764 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600765 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
Simon Glass8149b152022-09-17 09:00:09 -0600766 ide_dev_desc[i].uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600767 ide_dev_desc[i].devnum = i;
768 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
769 ide_dev_desc[i].blksz = 0;
770 ide_dev_desc[i].log2blksz =
771 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
772 ide_dev_desc[i].lba = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600773 if (!ide_bus_ok[IDE_BUS(i)])
774 continue;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600775 ide_ident(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600776 dev_print(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600777 }
Stefan Roese29caf932022-09-02 14:10:46 +0200778 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600779}
780
Simon Glasse9be1ee2016-05-01 11:36:10 -0600781__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
782{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100783 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600784 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600785
Simon Glasse9be1ee2016-05-01 11:36:10 -0600786 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600787 while (words--) {
788 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100789 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600790 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100791 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600792 }
793}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100794
Simon Glasse9be1ee2016-05-01 11:36:10 -0600795__weak void ide_input_data(int dev, ulong *sect_buf, int words)
796{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100797 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
798 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600799
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100800 dbuf = (ushort *)sect_buf;
801
802 debug("in input data base for read is %p\n", (void *)paddr);
803
804 while (words--) {
805 EIEIO;
806 *dbuf++ = le16_to_cpu(inw(paddr));
807 EIEIO;
808 *dbuf++ = le16_to_cpu(inw(paddr));
809 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100810}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600811
Simon Glass145df842016-05-01 11:36:22 -0600812ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
813 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600814{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700815 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600816 int device = block_dev->devnum;
817 ulong n = 0;
818 unsigned char c;
819 unsigned char pwrsave = 0; /* power save */
820
821#ifdef CONFIG_LBA48
822 unsigned char lba48 = 0;
823
824 if (blknr & 0x0000fffff0000000ULL) {
825 /* more than 28 bits used, use 48bit mode */
826 lba48 = 1;
827 }
828#endif
829 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
830 device, blknr, blkcnt, (ulong) buffer);
831
Simon Glasse9be1ee2016-05-01 11:36:10 -0600832 /* Select device
833 */
834 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
835 c = ide_wait(device, IDE_TIME_OUT);
836
837 if (c & ATA_STAT_BUSY) {
838 printf("IDE read: device %d not ready\n", device);
839 goto IDE_READ_E;
840 }
841
842 /* first check if the drive is in Powersaving mode, if yes,
843 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100844 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600845 udelay(50);
846
847 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
848
849 if (c & ATA_STAT_BUSY) {
850 printf("IDE read: device %d not ready\n", device);
851 goto IDE_READ_E;
852 }
853 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
854 printf("No Powersaving mode %X\n", c);
855 } else {
856 c = ide_inb(device, ATA_SECT_CNT);
857 debug("Powersaving %02X\n", c);
858 if (c == 0)
859 pwrsave = 1;
860 }
861
862
863 while (blkcnt-- > 0) {
864 c = ide_wait(device, IDE_TIME_OUT);
865
866 if (c & ATA_STAT_BUSY) {
867 printf("IDE read: device %d not ready\n", device);
868 break;
869 }
870#ifdef CONFIG_LBA48
871 if (lba48) {
872 /* write high bits */
873 ide_outb(device, ATA_SECT_CNT, 0);
874 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
875#ifdef CONFIG_SYS_64BIT_LBA
876 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
877 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
878#else
879 ide_outb(device, ATA_LBA_MID, 0);
880 ide_outb(device, ATA_LBA_HIGH, 0);
881#endif
882 }
883#endif
884 ide_outb(device, ATA_SECT_CNT, 1);
885 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
886 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
887 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
888
889#ifdef CONFIG_LBA48
890 if (lba48) {
891 ide_outb(device, ATA_DEV_HD,
892 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100893 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600894
895 } else
896#endif
897 {
898 ide_outb(device, ATA_DEV_HD, ATA_LBA |
899 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100900 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600901 }
902
903 udelay(50);
904
905 if (pwrsave) {
906 /* may take up to 4 sec */
907 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
908 pwrsave = 0;
909 } else {
910 /* can't take over 500 ms */
911 c = ide_wait(device, IDE_TIME_OUT);
912 }
913
914 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
915 ATA_STAT_DRQ) {
916 printf("Error (no IRQ) dev %d blk " LBAF
917 ": status %#02x\n", device, blknr, c);
918 break;
919 }
920
921 ide_input_data(device, buffer, ATA_SECTORWORDS);
922 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
923
924 ++n;
925 ++blknr;
926 buffer += ATA_BLOCKSIZE;
927 }
928IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600929 return n;
930}
931
Simon Glass145df842016-05-01 11:36:22 -0600932ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
933 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600934{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700935 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600936 int device = block_dev->devnum;
937 ulong n = 0;
938 unsigned char c;
939
940#ifdef CONFIG_LBA48
941 unsigned char lba48 = 0;
942
943 if (blknr & 0x0000fffff0000000ULL) {
944 /* more than 28 bits used, use 48bit mode */
945 lba48 = 1;
946 }
947#endif
948
Simon Glasse9be1ee2016-05-01 11:36:10 -0600949 /* Select device
950 */
951 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
952
953 while (blkcnt-- > 0) {
954 c = ide_wait(device, IDE_TIME_OUT);
955
956 if (c & ATA_STAT_BUSY) {
957 printf("IDE read: device %d not ready\n", device);
958 goto WR_OUT;
959 }
960#ifdef CONFIG_LBA48
961 if (lba48) {
962 /* write high bits */
963 ide_outb(device, ATA_SECT_CNT, 0);
964 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
965#ifdef CONFIG_SYS_64BIT_LBA
966 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
967 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
968#else
969 ide_outb(device, ATA_LBA_MID, 0);
970 ide_outb(device, ATA_LBA_HIGH, 0);
971#endif
972 }
973#endif
974 ide_outb(device, ATA_SECT_CNT, 1);
975 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
976 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
977 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
978
979#ifdef CONFIG_LBA48
980 if (lba48) {
981 ide_outb(device, ATA_DEV_HD,
982 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100983 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600984
985 } else
986#endif
987 {
988 ide_outb(device, ATA_DEV_HD, ATA_LBA |
989 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100990 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600991 }
992
993 udelay(50);
994
995 /* can't take over 500 ms */
996 c = ide_wait(device, IDE_TIME_OUT);
997
998 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
999 ATA_STAT_DRQ) {
1000 printf("Error (no IRQ) dev %d blk " LBAF
1001 ": status %#02x\n", device, blknr, c);
1002 goto WR_OUT;
1003 }
1004
1005 ide_output_data(device, buffer, ATA_SECTORWORDS);
1006 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1007 ++n;
1008 ++blknr;
1009 buffer += ATA_BLOCKSIZE;
1010 }
1011WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -06001012 return n;
1013}
1014
Bin Meng68e6f222017-09-10 05:12:51 -07001015static int ide_blk_probe(struct udevice *udev)
1016{
Simon Glasscaa4daa2020-12-03 16:55:18 -07001017 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Meng68e6f222017-09-10 05:12:51 -07001018
1019 /* fill in device vendor/product/rev strings */
1020 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1021 BLK_VEN_SIZE);
1022 desc->vendor[BLK_VEN_SIZE] = '\0';
1023 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1024 BLK_PRD_SIZE);
1025 desc->product[BLK_PRD_SIZE] = '\0';
1026 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1027 BLK_REV_SIZE);
1028 desc->revision[BLK_REV_SIZE] = '\0';
1029
Bin Meng68e6f222017-09-10 05:12:51 -07001030 return 0;
1031}
1032
Simon Glass145df842016-05-01 11:36:22 -06001033static const struct blk_ops ide_blk_ops = {
1034 .read = ide_read,
1035 .write = ide_write,
1036};
1037
1038U_BOOT_DRIVER(ide_blk) = {
1039 .name = "ide_blk",
1040 .id = UCLASS_BLK,
1041 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -07001042 .probe = ide_blk_probe,
1043};
1044
Simon Glass0d77f8f2023-01-17 10:47:46 -07001045static int ide_bootdev_bind(struct udevice *dev)
1046{
1047 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
1048
Simon Glasseacc2612023-01-17 10:48:08 -07001049 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001050
1051 return 0;
1052}
1053
1054static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
1055{
Simon Glass80778f52023-04-25 10:54:30 -06001056 struct udevice *dev;
1057
1058 uclass_first_device(UCLASS_IDE, &dev);
Simon Glass0d77f8f2023-01-17 10:47:46 -07001059
1060 return 0;
1061}
1062
1063struct bootdev_ops ide_bootdev_ops = {
1064};
1065
1066static const struct udevice_id ide_bootdev_ids[] = {
1067 { .compatible = "u-boot,bootdev-ide" },
1068 { }
1069};
1070
1071U_BOOT_DRIVER(ide_bootdev) = {
1072 .name = "ide_bootdev",
1073 .id = UCLASS_BOOTDEV,
1074 .ops = &ide_bootdev_ops,
1075 .bind = ide_bootdev_bind,
1076 .of_match = ide_bootdev_ids,
1077};
1078
1079BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -07001080 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -07001081 .uclass = UCLASS_IDE,
1082 .hunt = ide_bootdev_hunt,
1083 .drv = DM_DRIVER_REF(ide_bootdev),
1084};
1085
Bin Meng68e6f222017-09-10 05:12:51 -07001086static int ide_probe(struct udevice *udev)
1087{
1088 struct udevice *blk_dev;
1089 char name[20];
1090 int blksz;
1091 lbaint_t size;
1092 int i;
1093 int ret;
1094
Simon Glass80778f52023-04-25 10:54:30 -06001095 ide_init();
1096
Bin Meng68e6f222017-09-10 05:12:51 -07001097 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1098 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1099 sprintf(name, "blk#%d", i);
1100
1101 blksz = ide_dev_desc[i].blksz;
1102 size = blksz * ide_dev_desc[i].lba;
Bin Meng1f4adab2017-09-10 05:12:52 -07001103
1104 /*
1105 * With CDROM, if there is no CD inserted, blksz will
1106 * be zero, don't bother to create IDE block device.
1107 */
1108 if (!blksz)
1109 continue;
Bin Meng68e6f222017-09-10 05:12:51 -07001110 ret = blk_create_devicef(udev, "ide_blk", name,
Simon Glasse33a5c62022-08-11 19:34:59 -06001111 UCLASS_IDE, i,
Bin Meng68e6f222017-09-10 05:12:51 -07001112 blksz, size, &blk_dev);
1113 if (ret)
1114 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001115
1116 ret = blk_probe_or_unbind(blk_dev);
1117 if (ret)
1118 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001119
1120 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1121 if (ret)
1122 return log_msg_ret("bootdev", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001123 }
1124 }
1125
1126 return 0;
1127}
1128
1129U_BOOT_DRIVER(ide) = {
1130 .name = "ide",
1131 .id = UCLASS_IDE,
1132 .probe = ide_probe,
1133};
1134
1135struct pci_device_id ide_supported[] = {
1136 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1137 { }
1138};
1139
1140U_BOOT_PCI_DEVICE(ide, ide_supported);
1141
1142UCLASS_DRIVER(ide) = {
1143 .name = "ide",
1144 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001145};