1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 #define LOG_CATEGORY UCLASS_IDE
19 #include <linux/delay.h>
22 # define EIEIO __asm__ volatile ("eieio")
23 # define SYNC __asm__ volatile ("sync")
25 # define EIEIO /* nothing */
26 # define SYNC /* nothing */
29 /* Current offset for IDE0 / IDE1 bus access */
30 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
31 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
32 CONFIG_SYS_ATA_IDE0_OFFSET,
34 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
35 CONFIG_SYS_ATA_IDE1_OFFSET,
39 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
41 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
43 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
45 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
47 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
49 #ifdef CONFIG_IDE_RESET
50 extern void ide_set_reset(int idereset);
52 static void ide_reset(void)
56 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
58 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
59 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
61 ide_set_reset(1); /* assert reset */
63 /* the reset signal shall be asserted for et least 25 us */
68 /* de-assert RESET signal */
72 for (i = 0; i < 250; ++i)
76 #define ide_reset() /* dummy */
77 #endif /* CONFIG_IDE_RESET */
80 * Wait until Busy bit is off, or timeout (in ms)
83 static uchar ide_wait(int dev, ulong t)
85 ulong delay = 10 * t; /* poll every 100 us */
88 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
97 * copy src to dest, skipping leading and trailing blanks and null
98 * terminate the string
99 * "len" is the size of available memory including the terminating '\0'
101 static void ident_cpy(unsigned char *dst, unsigned char *src,
104 unsigned char *end, *last;
109 /* reserve space for '\0' */
113 /* skip leading white space */
114 while ((*src) && (src < end) && (*src == ' '))
117 /* copy string, omitting trailing white space */
118 while ((*src) && (src < end)) {
128 /****************************************************************************
132 /* since ATAPI may use commands with not 4 bytes alligned length
133 * we have our own transfer functions, 2 bytes alligned */
134 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
136 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
139 dbuf = (ushort *)sect_buf;
141 debug("in output data shorts base for read is %p\n", (void *)paddr);
145 outw(cpu_to_le16(*dbuf++), paddr);
149 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
151 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
154 dbuf = (ushort *)sect_buf;
156 debug("in input data shorts base for read is %p\n", (void *)paddr);
160 *dbuf++ = le16_to_cpu(inw(paddr));
165 * Wait until (Status & mask) == res, or timeout (in ms)
167 * This is used since some ATAPI CD ROMs clears their Busy Bit first
168 * and then they set their DRQ Bit
170 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
172 ulong delay = 10 * t; /* poll every 100 us */
175 /* prevents to read the status before valid */
176 c = ide_inb(dev, ATA_DEV_CTL);
178 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
179 /* break if error occurs (doesn't make sense to wait more) */
180 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
190 * issue an atapi command
192 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
193 unsigned char *buffer, int buflen)
195 unsigned char c, err, mask, res;
200 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
202 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
203 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
204 if ((c & mask) != res) {
205 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
211 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
212 ide_outb(device, ATA_SECT_CNT, 0);
213 ide_outb(device, ATA_SECT_NUM, 0);
214 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
215 ide_outb(device, ATA_CYL_HIGH,
216 (unsigned char) ((buflen >> 8) & 0xFF));
217 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
219 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
222 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
224 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
226 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
227 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
233 /* write command block */
234 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
236 /* ATAPI Command written wait for completition */
237 udelay(5000); /* device must set bsy */
239 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
241 * if no data wait for DRQ = 0 BSY = 0
242 * if data wait for DRQ = 1 BSY = 0
247 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
248 if ((c & mask) != res) {
249 if (c & ATA_STAT_ERR) {
250 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
251 debug("atapi_issue 1 returned sense key %X status %02X\n",
254 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
260 n = ide_inb(device, ATA_CYL_HIGH);
262 n += ide_inb(device, ATA_CYL_LOW);
264 printf("ERROR, transfer bytes %d requested only %d\n", n,
269 if ((n == 0) && (buflen < 0)) {
270 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
275 debug("WARNING, transfer bytes %d not equal with requested %d\n",
278 if (n != 0) { /* data transfer */
279 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
280 /* we transfer shorts */
282 /* ok now decide if it is an in or output */
283 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
284 debug("Write to device\n");
285 ide_output_data_shorts(device, (unsigned short *)buffer,
288 debug("Read from device @ %p shorts %d\n", buffer, n);
289 ide_input_data_shorts(device, (unsigned short *)buffer,
293 udelay(5000); /* seems that some CD ROMs need this... */
294 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
296 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
297 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
298 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
299 debug("atapi_issue 2 returned sense key %X status %X\n", err,
309 * sending the command to atapi_issue. If an status other than good
310 * returns, an request_sense will be issued
313 #define ATAPI_DRIVE_NOT_READY 100
314 #define ATAPI_UNIT_ATTN 10
316 unsigned char atapi_issue_autoreq(int device,
319 unsigned char *buffer, int buflen)
321 unsigned char sense_data[18], sense_ccb[12];
322 unsigned char res, key, asc, ascq;
323 int notready, unitattn;
325 unitattn = ATAPI_UNIT_ATTN;
326 notready = ATAPI_DRIVE_NOT_READY;
329 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
334 return 0xFF; /* error */
336 debug("(auto_req)atapi_issue returned sense key %X\n", res);
338 memset(sense_ccb, 0, sizeof(sense_ccb));
339 memset(sense_data, 0, sizeof(sense_data));
340 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
341 sense_ccb[4] = 18; /* allocation Length */
343 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
344 key = (sense_data[2] & 0xF);
345 asc = (sense_data[12]);
346 ascq = (sense_data[13]);
348 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
349 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
350 sense_data[0], key, asc, ascq);
353 return 0; /* ok device ready */
355 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
356 if (unitattn-- > 0) {
360 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
363 if ((asc == 0x4) && (ascq == 0x1)) {
364 /* not ready, but will be ready soon */
365 if (notready-- > 0) {
369 printf("Drive not ready, tried %d times\n",
370 ATAPI_DRIVE_NOT_READY);
374 debug("Media not present\n");
378 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
381 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
387 * we transfer only one block per command, since the multiple DRQ per
388 * command is not yet implemented
390 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
391 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
392 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
394 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
397 int device = block_dev->devnum;
399 unsigned char ccb[12]; /* Command descriptor block */
402 debug("atapi_read dev %d start " LBAF " blocks " LBAF
403 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
406 if (blkcnt > ATAPI_READ_MAX_BLOCK)
407 cnt = ATAPI_READ_MAX_BLOCK;
411 ccb[0] = ATAPI_CMD_READ_12;
412 ccb[1] = 0; /* reserved */
413 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
414 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
415 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
416 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
417 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
418 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
419 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
420 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
421 ccb[10] = 0; /* reserved */
422 ccb[11] = 0; /* reserved */
424 if (atapi_issue_autoreq(device, ccb, 12,
425 (unsigned char *)buffer,
426 cnt * ATAPI_READ_BLOCK_SIZE)
433 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
434 } while (blkcnt > 0);
438 static void atapi_inquiry(struct blk_desc *dev_desc)
440 unsigned char ccb[12]; /* Command descriptor block */
441 unsigned char iobuf[64]; /* temp buf */
445 device = dev_desc->devnum;
446 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
448 memset(ccb, 0, sizeof(ccb));
449 memset(iobuf, 0, sizeof(iobuf));
451 ccb[0] = ATAPI_CMD_INQUIRY;
452 ccb[4] = 40; /* allocation Legnth */
453 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
455 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
459 /* copy device ident strings */
460 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
461 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
462 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
467 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
468 dev_desc->type = iobuf[0] & 0x1f;
470 if ((iobuf[1] & 0x80) == 0x80)
471 dev_desc->removable = 1;
473 dev_desc->removable = 0;
475 memset(ccb, 0, sizeof(ccb));
476 memset(iobuf, 0, sizeof(iobuf));
477 ccb[0] = ATAPI_CMD_START_STOP;
478 ccb[4] = 0x03; /* start */
480 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
482 debug("ATAPI_CMD_START_STOP returned %x\n", c);
486 memset(ccb, 0, sizeof(ccb));
487 memset(iobuf, 0, sizeof(iobuf));
488 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
490 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
494 memset(ccb, 0, sizeof(ccb));
495 memset(iobuf, 0, sizeof(iobuf));
496 ccb[0] = ATAPI_CMD_READ_CAP;
497 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
498 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
502 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
503 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
504 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
506 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
507 ((unsigned long) iobuf[1] << 16) +
508 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
509 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
510 ((unsigned long) iobuf[5] << 16) +
511 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
512 dev_desc->log2blksz = LOG2(dev_desc->blksz);
514 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
520 #endif /* CONFIG_ATAPI */
522 static void ide_ident(struct blk_desc *dev_desc)
527 bool is_atapi = false;
532 device = dev_desc->devnum;
533 printf(" Device %d: ", device);
537 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
538 dev_desc->uclass_id = UCLASS_IDE;
543 /* Warning: This will be tricky to read */
544 while (retries <= 1) {
545 /* check signature */
546 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
547 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
548 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
549 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
550 /* ATAPI Signature found */
553 * Start Ident Command
555 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
557 * Wait for completion - ATAPI devices need more time
560 c = ide_wait(device, ATAPI_TIME_OUT);
565 * Start Ident Command
567 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
570 * Wait for completion
572 c = ide_wait(device, IDE_TIME_OUT);
575 if (((c & ATA_STAT_DRQ) == 0) ||
576 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
580 * Need to soft reset the device
581 * in case it's an ATAPI...
583 debug("Retrying...\n");
584 ide_outb(device, ATA_DEV_HD,
585 ATA_LBA | ATA_DEVICE(device));
587 ide_outb(device, ATA_COMMAND, 0x08);
588 udelay(500000); /* 500 ms */
593 ide_outb(device, ATA_DEV_HD,
594 ATA_LBA | ATA_DEVICE(device));
603 } /* see above - ugly to read */
605 if (retries == 2) /* Not found */
609 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
611 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
612 sizeof(dev_desc->revision));
613 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
614 sizeof(dev_desc->vendor));
615 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
616 sizeof(dev_desc->product));
618 if ((iop.config & 0x0080) == 0x0080)
619 dev_desc->removable = 1;
621 dev_desc->removable = 0;
625 atapi_inquiry(dev_desc);
628 #endif /* CONFIG_ATAPI */
630 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
631 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
633 ((unsigned long)iop.lba_capacity[0]) |
634 ((unsigned long)iop.lba_capacity[1] << 16);
637 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
639 for (int i = 0; i < 4; i++)
640 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
642 ((unsigned long long)iop.lba48_capacity[0] |
643 ((unsigned long long)iop.lba48_capacity[1] << 16) |
644 ((unsigned long long)iop.lba48_capacity[2] << 32) |
645 ((unsigned long long)iop.lba48_capacity[3] << 48));
649 #endif /* CONFIG_LBA48 */
651 dev_desc->type = DEV_TYPE_HARDDISK;
652 dev_desc->blksz = ATA_BLOCKSIZE;
653 dev_desc->log2blksz = LOG2(dev_desc->blksz);
654 dev_desc->lun = 0; /* just to fill something in... */
656 #if 0 /* only used to test the powersaving mode,
657 * if enabled, the drive goes after 5 sec
659 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
660 c = ide_wait(device, IDE_TIME_OUT);
661 ide_outb(device, ATA_SECT_CNT, 1);
662 ide_outb(device, ATA_LBA_LOW, 0);
663 ide_outb(device, ATA_LBA_MID, 0);
664 ide_outb(device, ATA_LBA_HIGH, 0);
665 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
666 ide_outb(device, ATA_COMMAND, 0xe3);
668 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
672 __weak void ide_outb(int dev, int port, unsigned char val)
674 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
675 dev, port, val, ATA_CURR_BASE(dev) + port);
677 outb(val, ATA_CURR_BASE(dev) + port);
680 __weak unsigned char ide_inb(int dev, int port)
684 val = inb(ATA_CURR_BASE(dev) + port);
686 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
687 dev, port, ATA_CURR_BASE(dev) + port, val);
699 /* ATAPI Drives seems to need a proper IDE Reset */
703 * Wait for IDE to get ready.
704 * According to spec, this can take up to 31 seconds!
706 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
708 bus * (CONFIG_SYS_IDE_MAXDEVICE /
709 CONFIG_SYS_IDE_MAXBUS);
711 printf("Bus %d: ", bus);
717 udelay(100000); /* 100 ms */
718 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
719 udelay(100000); /* 100 ms */
722 udelay(10000); /* 10 ms */
724 c = ide_inb(dev, ATA_STATUS);
726 if (i > (ATA_RESET_TIME * 100)) {
727 puts("** Timeout **\n");
730 if ((i >= 100) && ((i % 100) == 0))
733 } while (c & ATA_STAT_BUSY);
735 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
736 puts("not available ");
737 debug("Status = 0x%02X ", c);
738 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
739 } else if ((c & ATA_STAT_READY) == 0) {
740 puts("not available ");
741 debug("Status = 0x%02X ", c);
752 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
753 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
754 ide_dev_desc[i].uclass_id = UCLASS_IDE;
755 ide_dev_desc[i].devnum = i;
756 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
757 ide_dev_desc[i].blksz = 0;
758 ide_dev_desc[i].log2blksz =
759 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
760 ide_dev_desc[i].lba = 0;
761 if (!ide_bus_ok[IDE_BUS(i)])
763 ide_ident(&ide_dev_desc[i]);
764 dev_print(&ide_dev_desc[i]);
768 uclass_first_device(UCLASS_IDE, &dev);
771 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
773 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
774 ushort *dbuf = (ushort *)sect_buf;
776 debug("in input swap data base for read is %p\n", (void *)paddr);
780 *dbuf++ = be16_to_cpu(inw(paddr));
782 *dbuf++ = be16_to_cpu(inw(paddr));
786 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
788 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
791 dbuf = (ushort *)sect_buf;
794 outw(cpu_to_le16(*dbuf++), paddr);
796 outw(cpu_to_le16(*dbuf++), paddr);
800 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
802 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
805 dbuf = (ushort *)sect_buf;
807 debug("in input data base for read is %p\n", (void *)paddr);
811 *dbuf++ = le16_to_cpu(inw(paddr));
813 *dbuf++ = le16_to_cpu(inw(paddr));
817 ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
820 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
821 int device = block_dev->devnum;
824 unsigned char pwrsave = 0; /* power save */
827 unsigned char lba48 = 0;
829 if (blknr & 0x0000fffff0000000ULL) {
830 /* more than 28 bits used, use 48bit mode */
834 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
835 device, blknr, blkcnt, (ulong) buffer);
839 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
840 c = ide_wait(device, IDE_TIME_OUT);
842 if (c & ATA_STAT_BUSY) {
843 printf("IDE read: device %d not ready\n", device);
847 /* first check if the drive is in Powersaving mode, if yes,
848 * increase the timeout value */
849 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
852 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
854 if (c & ATA_STAT_BUSY) {
855 printf("IDE read: device %d not ready\n", device);
858 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
859 printf("No Powersaving mode %X\n", c);
861 c = ide_inb(device, ATA_SECT_CNT);
862 debug("Powersaving %02X\n", c);
868 while (blkcnt-- > 0) {
869 c = ide_wait(device, IDE_TIME_OUT);
871 if (c & ATA_STAT_BUSY) {
872 printf("IDE read: device %d not ready\n", device);
877 /* write high bits */
878 ide_outb(device, ATA_SECT_CNT, 0);
879 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
880 #ifdef CONFIG_SYS_64BIT_LBA
881 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
882 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
884 ide_outb(device, ATA_LBA_MID, 0);
885 ide_outb(device, ATA_LBA_HIGH, 0);
889 ide_outb(device, ATA_SECT_CNT, 1);
890 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
891 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
892 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
896 ide_outb(device, ATA_DEV_HD,
897 ATA_LBA | ATA_DEVICE(device));
898 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
903 ide_outb(device, ATA_DEV_HD, ATA_LBA |
904 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
905 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
911 /* may take up to 4 sec */
912 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
915 /* can't take over 500 ms */
916 c = ide_wait(device, IDE_TIME_OUT);
919 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
921 printf("Error (no IRQ) dev %d blk " LBAF
922 ": status %#02x\n", device, blknr, c);
926 ide_input_data(device, buffer, ATA_SECTORWORDS);
927 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
931 buffer += ATA_BLOCKSIZE;
937 ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
940 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
941 int device = block_dev->devnum;
946 unsigned char lba48 = 0;
948 if (blknr & 0x0000fffff0000000ULL) {
949 /* more than 28 bits used, use 48bit mode */
956 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
958 while (blkcnt-- > 0) {
959 c = ide_wait(device, IDE_TIME_OUT);
961 if (c & ATA_STAT_BUSY) {
962 printf("IDE read: device %d not ready\n", device);
967 /* write high bits */
968 ide_outb(device, ATA_SECT_CNT, 0);
969 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
970 #ifdef CONFIG_SYS_64BIT_LBA
971 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
972 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
974 ide_outb(device, ATA_LBA_MID, 0);
975 ide_outb(device, ATA_LBA_HIGH, 0);
979 ide_outb(device, ATA_SECT_CNT, 1);
980 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
981 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
982 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
986 ide_outb(device, ATA_DEV_HD,
987 ATA_LBA | ATA_DEVICE(device));
988 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
993 ide_outb(device, ATA_DEV_HD, ATA_LBA |
994 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
995 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
1000 /* can't take over 500 ms */
1001 c = ide_wait(device, IDE_TIME_OUT);
1003 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1005 printf("Error (no IRQ) dev %d blk " LBAF
1006 ": status %#02x\n", device, blknr, c);
1010 ide_output_data(device, buffer, ATA_SECTORWORDS);
1011 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1014 buffer += ATA_BLOCKSIZE;
1020 #if defined(CONFIG_OF_IDE_FIXUP)
1021 int ide_device_present(int dev)
1023 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1025 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1029 static int ide_blk_probe(struct udevice *udev)
1031 struct blk_desc *desc = dev_get_uclass_plat(udev);
1033 /* fill in device vendor/product/rev strings */
1034 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1036 desc->vendor[BLK_VEN_SIZE] = '\0';
1037 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1039 desc->product[BLK_PRD_SIZE] = '\0';
1040 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1042 desc->revision[BLK_REV_SIZE] = '\0';
1047 static const struct blk_ops ide_blk_ops = {
1052 U_BOOT_DRIVER(ide_blk) = {
1055 .ops = &ide_blk_ops,
1056 .probe = ide_blk_probe,
1059 static int ide_bootdev_bind(struct udevice *dev)
1061 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
1063 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
1068 static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
1075 struct bootdev_ops ide_bootdev_ops = {
1078 static const struct udevice_id ide_bootdev_ids[] = {
1079 { .compatible = "u-boot,bootdev-ide" },
1083 U_BOOT_DRIVER(ide_bootdev) = {
1084 .name = "ide_bootdev",
1085 .id = UCLASS_BOOTDEV,
1086 .ops = &ide_bootdev_ops,
1087 .bind = ide_bootdev_bind,
1088 .of_match = ide_bootdev_ids,
1091 BOOTDEV_HUNTER(ide_bootdev_hunter) = {
1092 .prio = BOOTDEVP_5_SCAN_SLOW,
1093 .uclass = UCLASS_IDE,
1094 .hunt = ide_bootdev_hunt,
1095 .drv = DM_DRIVER_REF(ide_bootdev),
1098 static int ide_probe(struct udevice *udev)
1100 struct udevice *blk_dev;
1107 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1108 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1109 sprintf(name, "blk#%d", i);
1111 blksz = ide_dev_desc[i].blksz;
1112 size = blksz * ide_dev_desc[i].lba;
1115 * With CDROM, if there is no CD inserted, blksz will
1116 * be zero, don't bother to create IDE block device.
1120 ret = blk_create_devicef(udev, "ide_blk", name,
1122 blksz, size, &blk_dev);
1126 ret = blk_probe_or_unbind(blk_dev);
1130 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1132 return log_msg_ret("bootdev", ret);
1139 U_BOOT_DRIVER(ide) = {
1145 struct pci_device_id ide_supported[] = {
1146 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1150 U_BOOT_PCI_DEVICE(ide, ide_supported);
1152 UCLASS_DRIVER(ide) = {