1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2011
7 #define LOG_CATEGORY UCLASS_IDE
18 #include <linux/delay.h>
21 # define EIEIO __asm__ volatile ("eieio")
22 # define SYNC __asm__ volatile ("sync")
24 # define EIEIO /* nothing */
25 # define SYNC /* nothing */
28 /* Current offset for IDE0 / IDE1 bus access */
29 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
30 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
31 CONFIG_SYS_ATA_IDE0_OFFSET,
33 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
34 CONFIG_SYS_ATA_IDE1_OFFSET,
38 #define ATA_CURR_BASE(dev) (CONFIG_SYS_ATA_BASE_ADDR + \
39 ide_bus_offset[IDE_BUS(dev)])
41 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
43 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
45 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
47 static void ide_reset(void)
49 if (IS_ENABLED(CONFIG_IDE_RESET)) {
53 /* the reset signal shall be asserted for et least 25 us */
58 /* de-assert RESET signal */
65 static void ide_outb(int dev, int port, u8 val)
67 log_debug("(dev= %d, port= %#x, val= 0x%02x) : @ 0x%08lx\n",
68 dev, port, val, ATA_CURR_BASE(dev) + port);
70 outb(val, ATA_CURR_BASE(dev) + port);
73 static u8 ide_inb(int dev, int port)
77 val = inb(ATA_CURR_BASE(dev) + port);
79 log_debug("(dev= %d, port= %#x) : @ 0x%08lx -> 0x%02x\n",
80 dev, port, ATA_CURR_BASE(dev) + port, val);
84 static void ide_input_swap_data(int dev, ulong *sect_buf, int words)
86 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
87 ushort *dbuf = (ushort *)sect_buf;
89 log_debug("in input swap data base for read is %p\n", (void *)paddr);
93 *dbuf++ = be16_to_cpu(inw(paddr));
95 *dbuf++ = be16_to_cpu(inw(paddr));
100 * Wait until Busy bit is off, or timeout (in ms)
103 static uchar ide_wait(int dev, ulong t)
105 ulong delay = 10 * t; /* poll every 100 us */
108 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
117 * copy src to dest, skipping leading and trailing blanks and null
118 * terminate the string
119 * "len" is the size of available memory including the terminating '\0'
121 static void ident_cpy(u8 *dst, u8 *src, uint len)
128 /* reserve space for '\0' */
132 /* skip leading white space */
133 while ((*src) && (src < end) && (*src == ' '))
136 /* copy string, omitting trailing white space */
137 while ((*src) && (src < end)) {
146 /****************************************************************************
150 /* since ATAPI may use commands with not 4 bytes alligned length
151 * we have our own transfer functions, 2 bytes alligned */
152 static void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
154 uintptr_t paddr = ATA_CURR_BASE(dev) + ATA_DATA_REG;
157 dbuf = (ushort *)sect_buf;
159 log_debug("in output data shorts base for read is %p\n", (void *)paddr);
163 outw(cpu_to_le16(*dbuf++), paddr);
167 static void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
169 uintptr_t paddr = ATA_CURR_BASE(dev) + ATA_DATA_REG;
172 dbuf = (ushort *)sect_buf;
174 log_debug("in input data shorts base for read is %p\n", (void *)paddr);
178 *dbuf++ = le16_to_cpu(inw(paddr));
183 * Wait until (Status & mask) == res, or timeout (in ms)
185 * This is used since some ATAPI CD ROMs clears their Busy Bit first
186 * and then they set their DRQ Bit
188 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
190 ulong delay = 10 * t; /* poll every 100 us */
193 /* prevents to read the status before valid */
194 c = ide_inb(dev, ATA_DEV_CTL);
196 while (c = ide_inb(dev, ATA_STATUS) & mask, c != res) {
197 /* break if error occurs (doesn't make sense to wait more) */
198 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
208 * issue an atapi command
210 static u8 atapi_issue(int device, u8 *ccb, int ccblen, u8 *buffer, int buflen)
212 u8 c, err, mask, res;
217 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
219 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
220 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
221 if ((c & mask) != res) {
222 printf("ATAPI_ISSUE: device %d not ready status %x\n", device,
228 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
229 ide_outb(device, ATA_SECT_CNT, 0);
230 ide_outb(device, ATA_SECT_NUM, 0);
231 ide_outb(device, ATA_CYL_LOW, (u8)(buflen & 0xff));
232 ide_outb(device, ATA_CYL_HIGH, (u8)((buflen >> 8) & 0xff));
233 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
235 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
238 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
240 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
242 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
243 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status %#02x\n",
249 /* write command block */
250 ide_output_data_shorts(device, (ushort *)ccb, ccblen / 2);
252 /* ATAPI Command written wait for completition */
253 mdelay(5); /* device must set bsy */
255 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
257 * if no data wait for DRQ = 0 BSY = 0
258 * if data wait for DRQ = 1 BSY = 0
263 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
264 if ((c & mask) != res) {
265 if (c & ATA_STAT_ERR) {
266 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
267 log_debug("1 returned sense key %x status %02x\n",
270 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status %#02x\n",
276 n = ide_inb(device, ATA_CYL_HIGH);
278 n += ide_inb(device, ATA_CYL_LOW);
280 printf("ERROR, transfer bytes %d requested only %d\n", n,
285 if (!n && buflen < 0) {
286 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
291 log_debug("WARNING, transfer bytes %d not equal with requested %d\n",
294 if (n) { /* data transfer */
295 log_debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
296 /* we transfer shorts */
298 /* ok now decide if it is an in or output */
299 if (!(ide_inb(device, ATA_SECT_CNT) & 0x02)) {
300 log_debug("Write to device\n");
301 ide_output_data_shorts(device, (ushort *)buffer, n);
303 log_debug("Read from device @ %p shorts %d\n", buffer,
305 ide_input_data_shorts(device, (ushort *)buffer, n);
308 mdelay(5); /* seems that some CD ROMs need this... */
309 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
311 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
312 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
313 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
314 log_debug("2 returned sense key %x status %x\n", err, c);
323 * sending the command to atapi_issue. If an status other than good
324 * returns, an request_sense will be issued
327 #define ATAPI_DRIVE_NOT_READY 100
328 #define ATAPI_UNIT_ATTN 10
330 static u8 atapi_issue_autoreq(int device, u8 *ccb, int ccblen, u8 *buffer,
333 u8 sense_data[18], sense_ccb[12];
334 u8 res, key, asc, ascq;
335 int notready, unitattn;
337 unitattn = ATAPI_UNIT_ATTN;
338 notready = ATAPI_DRIVE_NOT_READY;
341 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
346 return 0xff; /* error */
348 log_debug("(auto_req)atapi_issue returned sense key %x\n", res);
350 memset(sense_ccb, 0, sizeof(sense_ccb));
351 memset(sense_data, 0, sizeof(sense_data));
352 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
353 sense_ccb[4] = 18; /* allocation Length */
355 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
356 key = (sense_data[2] & 0xf);
357 asc = (sense_data[12]);
358 ascq = (sense_data[13]);
360 log_debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
361 log_debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
362 sense_data[0], key, asc, ascq);
365 return 0; /* ok device ready */
367 if (key == 6 || asc == 0x29 || asc == 0x28) { /* Unit Attention */
368 if (unitattn-- > 0) {
372 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
375 if (asc == 0x4 && ascq == 0x1) {
376 /* not ready, but will be ready soon */
377 if (notready-- > 0) {
381 printf("Drive not ready, tried %d times\n",
382 ATAPI_DRIVE_NOT_READY);
386 log_debug("Media not present\n");
390 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
393 log_debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
399 * we transfer only one block per command, since the multiple DRQ per
400 * command is not yet implemented
402 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
403 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
404 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
406 static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
409 struct blk_desc *desc = dev_get_uclass_plat(dev);
410 int device = desc->devnum;
412 u8 ccb[12]; /* Command descriptor block */
415 log_debug("%d start " LBAF " blocks " LBAF " buffer at %lx\n", device,
416 blknr, blkcnt, (ulong)buffer);
419 if (blkcnt > ATAPI_READ_MAX_BLOCK)
420 cnt = ATAPI_READ_MAX_BLOCK;
424 ccb[0] = ATAPI_CMD_READ_12;
425 ccb[1] = 0; /* reserved */
426 ccb[2] = (u8)(blknr >> 24) & 0xff; /* MSB Block */
427 ccb[3] = (u8)(blknr >> 16) & 0xff; /* */
428 ccb[4] = (u8)(blknr >> 8) & 0xff;
429 ccb[5] = (u8)blknr & 0xff; /* LSB Block */
430 ccb[6] = (u8)(cnt >> 24) & 0xff; /* MSB Block cnt */
431 ccb[7] = (u8)(cnt >> 16) & 0xff;
432 ccb[8] = (u8)(cnt >> 8) & 0xff;
433 ccb[9] = (u8)cnt & 0xff; /* LSB Block */
434 ccb[10] = 0; /* reserved */
435 ccb[11] = 0; /* reserved */
437 if (atapi_issue_autoreq(device, ccb, 12,
439 cnt * ATAPI_READ_BLOCK_SIZE) == 0xff)
444 buffer += cnt * ATAPI_READ_BLOCK_SIZE;
445 } while (blkcnt > 0);
449 static void atapi_inquiry(struct blk_desc *desc)
451 u8 ccb[12]; /* Command descriptor block */
452 u8 iobuf[64]; /* temp buf */
456 device = desc->devnum;
457 desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
459 memset(ccb, 0, sizeof(ccb));
460 memset(iobuf, 0, sizeof(iobuf));
462 ccb[0] = ATAPI_CMD_INQUIRY;
463 ccb[4] = 40; /* allocation Legnth */
464 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 40);
466 log_debug("ATAPI_CMD_INQUIRY returned %x\n", c);
470 /* copy device ident strings */
471 ident_cpy((u8 *)desc->vendor, &iobuf[8], 8);
472 ident_cpy((u8 *)desc->product, &iobuf[16], 16);
473 ident_cpy((u8 *)desc->revision, &iobuf[32], 5);
478 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
479 desc->type = iobuf[0] & 0x1f;
486 memset(ccb, 0, sizeof(ccb));
487 memset(iobuf, 0, sizeof(iobuf));
488 ccb[0] = ATAPI_CMD_START_STOP;
489 ccb[4] = 0x03; /* start */
491 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 0);
493 log_debug("ATAPI_CMD_START_STOP returned %x\n", c);
497 memset(ccb, 0, sizeof(ccb));
498 memset(iobuf, 0, sizeof(iobuf));
499 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 0);
501 log_debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
505 memset(ccb, 0, sizeof(ccb));
506 memset(iobuf, 0, sizeof(iobuf));
507 ccb[0] = ATAPI_CMD_READ_CAP;
508 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 8);
509 log_debug("ATAPI_CMD_READ_CAP returned %x\n", c);
513 log_debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
514 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
515 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
517 desc->lba = (ulong)iobuf[0] << 24 | (ulong)iobuf[1] << 16 |
518 (ulong)iobuf[2] << 8 | (ulong)iobuf[3];
519 desc->blksz = (ulong)iobuf[4] << 24 | (ulong)iobuf[5] << 16 |
520 (ulong)iobuf[6] << 8 | (ulong)iobuf[7];
521 desc->log2blksz = LOG2(desc->blksz);
523 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
528 * ide_ident() - Identify an IDE device
530 * @device: Device number to use
531 * @desc: Block descriptor to fill in
532 * Returns: 0 if OK, -ENOENT if no device is found
534 static int ide_ident(int device, struct blk_desc *desc)
537 bool is_atapi = false;
541 memset(desc, '\0', sizeof(*desc));
542 desc->devnum = device;
543 desc->type = DEV_TYPE_UNKNOWN;
544 desc->uclass_id = UCLASS_IDE;
545 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
546 printf(" Device %d: ", device);
550 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
551 if (IS_ENABLED(CONFIG_ATAPI))
555 /* check signature */
556 if (IS_ENABLED(CONFIG_ATAPI) &&
557 ide_inb(device, ATA_SECT_CNT) == 0x01 &&
558 ide_inb(device, ATA_SECT_NUM) == 0x01 &&
559 ide_inb(device, ATA_CYL_LOW) == 0x14 &&
560 ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
561 /* ATAPI Signature found */
564 * Start Ident Command
566 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
568 * Wait for completion - ATAPI devices need more time
571 c = ide_wait(device, ATAPI_TIME_OUT);
574 * Start Ident Command
576 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
579 * Wait for completion
581 c = ide_wait(device, IDE_TIME_OUT);
584 if ((c & ATA_STAT_DRQ) &&
585 !(c & (ATA_STAT_FAULT | ATA_STAT_ERR))) {
587 } else if (IS_ENABLED(CONFIG_ATAPI)) {
589 * Need to soft reset the device
590 * in case it's an ATAPI...
592 log_debug("Retrying...\n");
593 ide_outb(device, ATA_DEV_HD,
594 ATA_LBA | ATA_DEVICE(device));
596 ide_outb(device, ATA_COMMAND, 0x08);
599 ide_outb(device, ATA_DEV_HD,
600 ATA_LBA | ATA_DEVICE(device));
605 if (!tries) /* Not found */
608 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
610 ident_cpy((u8 *)desc->revision, iop.fw_rev, sizeof(desc->revision));
611 ident_cpy((u8 *)desc->vendor, iop.model, sizeof(desc->vendor));
612 ident_cpy((u8 *)desc->product, iop.serial_no, sizeof(desc->product));
614 if (iop.config & 0x0080)
619 if (IS_ENABLED(CONFIG_ATAPI) && is_atapi) {
625 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
626 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
627 desc->lba = (ulong)iop.lba_capacity[0] |
628 (ulong)iop.lba_capacity[1] << 16;
630 if (IS_ENABLED(CONFIG_LBA48) && (iop.command_set_2 & 0x0400)) {
633 for (int i = 0; i < 4; i++)
634 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
635 desc->lba = (unsigned long long)iop.lba48_capacity[0] |
636 (unsigned long long)iop.lba48_capacity[1] << 16 |
637 (unsigned long long)iop.lba48_capacity[2] << 32 |
638 (unsigned long long)iop.lba48_capacity[3] << 48;
644 desc->type = DEV_TYPE_HARDDISK;
645 desc->blksz = ATA_BLOCKSIZE;
646 desc->log2blksz = LOG2(desc->blksz);
647 desc->lun = 0; /* just to fill something in... */
649 #if 0 /* only used to test the powersaving mode,
650 * if enabled, the drive goes after 5 sec
652 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
653 c = ide_wait(device, IDE_TIME_OUT);
654 ide_outb(device, ATA_SECT_CNT, 1);
655 ide_outb(device, ATA_LBA_LOW, 0);
656 ide_outb(device, ATA_LBA_MID, 0);
657 ide_outb(device, ATA_LBA_HIGH, 0);
658 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
659 ide_outb(device, ATA_COMMAND, 0xe3);
661 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
668 * ide_init_one() - Init one IDE device
671 * Return: 0 iuf OK, -EIO if not available, -ETIMEDOUT if timed out
673 static int ide_init_one(int bus)
675 int dev = bus * CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS;
679 printf("Bus %d: ", bus);
683 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
689 c = ide_inb(dev, ATA_STATUS);
691 if (i > (ATA_RESET_TIME * 100)) {
692 puts("** Timeout **\n");
695 if (i >= 100 && !(i % 100))
697 } while (c & ATA_STAT_BUSY);
699 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
700 puts("not available ");
701 log_debug("Status = %#02X ", c);
703 } else if (IS_ENABLED(CONFIG_ATAPI) && !(c & ATA_STAT_READY)) {
704 /* ATAPI Devices do not set DRDY */
705 puts("not available ");
706 log_debug("Status = %#02X ", c);
714 static void ide_output_data(int dev, const ulong *sect_buf, int words)
716 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
719 dbuf = (ushort *)sect_buf;
722 outw(cpu_to_le16(*dbuf++), paddr);
724 outw(cpu_to_le16(*dbuf++), paddr);
728 static void ide_input_data(int dev, ulong *sect_buf, int words)
730 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
733 dbuf = (ushort *)sect_buf;
735 log_debug("in input data base for read is %p\n", (void *)paddr);
739 *dbuf++ = le16_to_cpu(inw(paddr));
741 *dbuf++ = le16_to_cpu(inw(paddr));
745 static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
748 struct blk_desc *desc = dev_get_uclass_plat(dev);
749 int device = desc->devnum;
752 u8 pwrsave = 0; /* power save */
755 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
756 /* more than 28 bits used, use 48bit mode */
760 log_debug("dev %d start " LBAF ", blocks " LBAF " buffer at %lx\n",
761 device, blknr, blkcnt, (ulong)buffer);
765 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
766 c = ide_wait(device, IDE_TIME_OUT);
768 if (c & ATA_STAT_BUSY) {
769 printf("IDE read: device %d not ready\n", device);
773 /* first check if the drive is in Powersaving mode, if yes,
774 * increase the timeout value */
775 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
778 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
780 if (c & ATA_STAT_BUSY) {
781 printf("IDE read: device %d not ready\n", device);
784 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
785 printf("No Powersaving mode %x\n", c);
787 c = ide_inb(device, ATA_SECT_CNT);
788 log_debug("Powersaving %02X\n", c);
794 while (blkcnt-- > 0) {
795 c = ide_wait(device, IDE_TIME_OUT);
797 if (c & ATA_STAT_BUSY) {
798 printf("IDE read: device %d not ready\n", device);
801 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
802 /* write high bits */
803 ide_outb(device, ATA_SECT_CNT, 0);
804 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
805 #ifdef CONFIG_SYS_64BIT_LBA
806 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
807 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
809 ide_outb(device, ATA_LBA_MID, 0);
810 ide_outb(device, ATA_LBA_HIGH, 0);
813 ide_outb(device, ATA_SECT_CNT, 1);
814 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
815 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
816 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
818 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
819 ide_outb(device, ATA_DEV_HD,
820 ATA_LBA | ATA_DEVICE(device));
821 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
824 ide_outb(device, ATA_DEV_HD, ATA_LBA |
825 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
826 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
832 /* may take up to 4 sec */
833 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
836 /* can't take over 500 ms */
837 c = ide_wait(device, IDE_TIME_OUT);
840 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
842 printf("Error (no IRQ) dev %d blk " LBAF
843 ": status %#02x\n", device, blknr, c);
847 ide_input_data(device, buffer, ATA_SECTORWORDS);
848 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
852 buffer += ATA_BLOCKSIZE;
858 static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
861 struct blk_desc *desc = dev_get_uclass_plat(dev);
862 int device = desc->devnum;
867 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
868 /* more than 28 bits used, use 48bit mode */
874 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
876 while (blkcnt-- > 0) {
877 c = ide_wait(device, IDE_TIME_OUT);
879 if (c & ATA_STAT_BUSY) {
880 printf("IDE read: device %d not ready\n", device);
883 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
884 /* write high bits */
885 ide_outb(device, ATA_SECT_CNT, 0);
886 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
887 #ifdef CONFIG_SYS_64BIT_LBA
888 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
889 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
891 ide_outb(device, ATA_LBA_MID, 0);
892 ide_outb(device, ATA_LBA_HIGH, 0);
895 ide_outb(device, ATA_SECT_CNT, 1);
896 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
897 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
898 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
900 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
901 ide_outb(device, ATA_DEV_HD,
902 ATA_LBA | ATA_DEVICE(device));
903 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
906 ide_outb(device, ATA_DEV_HD, ATA_LBA |
907 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
908 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
913 /* can't take over 500 ms */
914 c = ide_wait(device, IDE_TIME_OUT);
916 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
918 printf("Error (no IRQ) dev %d blk " LBAF
919 ": status %#02x\n", device, blknr, c);
923 ide_output_data(device, buffer, ATA_SECTORWORDS);
924 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
927 buffer += ATA_BLOCKSIZE;
933 ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
936 struct blk_desc *desc = dev_get_uclass_plat(dev);
938 if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
939 return atapi_read(dev, blknr, blkcnt, buffer);
941 return ide_read(dev, blknr, blkcnt, buffer);
944 static const struct blk_ops ide_blk_ops = {
945 .read = ide_or_atapi_read,
949 U_BOOT_DRIVER(ide_blk) = {
955 static int ide_bootdev_bind(struct udevice *dev)
957 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
959 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
964 static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
968 uclass_first_device(UCLASS_IDE, &dev);
973 struct bootdev_ops ide_bootdev_ops = {
976 static const struct udevice_id ide_bootdev_ids[] = {
977 { .compatible = "u-boot,bootdev-ide" },
981 U_BOOT_DRIVER(ide_bootdev) = {
982 .name = "ide_bootdev",
983 .id = UCLASS_BOOTDEV,
984 .ops = &ide_bootdev_ops,
985 .bind = ide_bootdev_bind,
986 .of_match = ide_bootdev_ids,
989 BOOTDEV_HUNTER(ide_bootdev_hunter) = {
990 .prio = BOOTDEVP_5_SCAN_SLOW,
991 .uclass = UCLASS_IDE,
992 .hunt = ide_bootdev_hunt,
993 .drv = DM_DRIVER_REF(ide_bootdev),
996 static int ide_probe(struct udevice *udev)
998 bool bus_ok[CONFIG_SYS_IDE_MAXBUS];
1003 /* ATAPI Drives seems to need a proper IDE Reset */
1007 * Wait for IDE to get ready.
1008 * According to spec, this can take up to 31 seconds!
1010 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1011 bus_ok[bus] = !ide_init_one(bus);
1019 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1020 struct blk_desc *desc, pdesc;
1021 struct udevice *blk;
1025 if (!bus_ok[IDE_BUS(i)])
1028 ret = ide_ident(i, &pdesc);
1034 sprintf(name, "blk#%d", i);
1037 * With CDROM, if there is no CD inserted, blksz will
1038 * be zero, don't bother to create IDE block device.
1042 ret = blk_create_devicef(udev, "ide_blk", name, UCLASS_IDE, i,
1043 pdesc.blksz, pdesc.lba, &blk);
1047 ret = blk_probe_or_unbind(blk);
1051 /* fill in device vendor/product/rev strings */
1052 desc = dev_get_uclass_plat(blk);
1053 strlcpy(desc->vendor, pdesc.vendor, BLK_VEN_SIZE);
1054 strlcpy(desc->product, pdesc.product, BLK_PRD_SIZE);
1055 strlcpy(desc->revision, pdesc.revision, BLK_REV_SIZE);
1056 desc->removable = pdesc.removable;
1057 desc->atapi = pdesc.atapi;
1058 desc->lba48 = pdesc.lba48;
1059 desc->type = pdesc.type;
1061 ret = bootdev_setup_for_sibling_blk(blk, "ide_bootdev");
1063 return log_msg_ret("bd", ret);
1069 U_BOOT_DRIVER(ide) = {
1075 struct pci_device_id ide_supported[] = {
1076 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1080 U_BOOT_PCI_DEVICE(ide, ide_supported);
1082 UCLASS_DRIVER(ide) = {