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 #define ATA_CURR_BASE(dev) (CONFIG_SYS_ATA_BASE_ADDR + \
40 ide_bus_offset[IDE_BUS(dev)])
42 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
44 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
46 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
48 static void ide_reset(void)
50 if (IS_ENABLED(CONFIG_IDE_RESET)) {
54 /* the reset signal shall be asserted for et least 25 us */
59 /* de-assert RESET signal */
66 static void ide_outb(int dev, int port, u8 val)
68 log_debug("(dev= %d, port= %#x, val= 0x%02x) : @ 0x%08lx\n",
69 dev, port, val, ATA_CURR_BASE(dev) + port);
71 outb(val, ATA_CURR_BASE(dev) + port);
74 static u8 ide_inb(int dev, int port)
78 val = inb(ATA_CURR_BASE(dev) + port);
80 log_debug("(dev= %d, port= %#x) : @ 0x%08lx -> 0x%02x\n",
81 dev, port, ATA_CURR_BASE(dev) + port, val);
85 static void ide_input_swap_data(int dev, ulong *sect_buf, int words)
87 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
88 ushort *dbuf = (ushort *)sect_buf;
90 log_debug("in input swap data base for read is %p\n", (void *)paddr);
94 *dbuf++ = be16_to_cpu(inw(paddr));
96 *dbuf++ = be16_to_cpu(inw(paddr));
101 * Wait until Busy bit is off, or timeout (in ms)
104 static uchar ide_wait(int dev, ulong t)
106 ulong delay = 10 * t; /* poll every 100 us */
109 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
118 * copy src to dest, skipping leading and trailing blanks and null
119 * terminate the string
120 * "len" is the size of available memory including the terminating '\0'
122 static void ident_cpy(u8 *dst, u8 *src, uint len)
129 /* reserve space for '\0' */
133 /* skip leading white space */
134 while ((*src) && (src < end) && (*src == ' '))
137 /* copy string, omitting trailing white space */
138 while ((*src) && (src < end)) {
147 /****************************************************************************
151 /* since ATAPI may use commands with not 4 bytes alligned length
152 * we have our own transfer functions, 2 bytes alligned */
153 static void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
155 uintptr_t paddr = ATA_CURR_BASE(dev) + ATA_DATA_REG;
158 dbuf = (ushort *)sect_buf;
160 log_debug("in output data shorts base for read is %p\n", (void *)paddr);
164 outw(cpu_to_le16(*dbuf++), paddr);
168 static void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
170 uintptr_t paddr = ATA_CURR_BASE(dev) + ATA_DATA_REG;
173 dbuf = (ushort *)sect_buf;
175 log_debug("in input data shorts base for read is %p\n", (void *)paddr);
179 *dbuf++ = le16_to_cpu(inw(paddr));
184 * Wait until (Status & mask) == res, or timeout (in ms)
186 * This is used since some ATAPI CD ROMs clears their Busy Bit first
187 * and then they set their DRQ Bit
189 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
191 ulong delay = 10 * t; /* poll every 100 us */
194 /* prevents to read the status before valid */
195 c = ide_inb(dev, ATA_DEV_CTL);
197 while (c = ide_inb(dev, ATA_STATUS) & mask, c != res) {
198 /* break if error occurs (doesn't make sense to wait more) */
199 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
209 * issue an atapi command
211 static u8 atapi_issue(int device, u8 *ccb, int ccblen, u8 *buffer, int buflen)
213 u8 c, err, mask, res;
218 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
220 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
221 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
222 if ((c & mask) != res) {
223 printf("ATAPI_ISSUE: device %d not ready status %x\n", device,
229 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
230 ide_outb(device, ATA_SECT_CNT, 0);
231 ide_outb(device, ATA_SECT_NUM, 0);
232 ide_outb(device, ATA_CYL_LOW, (u8)(buflen & 0xff));
233 ide_outb(device, ATA_CYL_HIGH, (u8)((buflen >> 8) & 0xff));
234 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
236 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
239 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
241 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
243 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
244 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status %#02x\n",
250 /* write command block */
251 ide_output_data_shorts(device, (ushort *)ccb, ccblen / 2);
253 /* ATAPI Command written wait for completition */
254 mdelay(5); /* device must set bsy */
256 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
258 * if no data wait for DRQ = 0 BSY = 0
259 * if data wait for DRQ = 1 BSY = 0
264 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
265 if ((c & mask) != res) {
266 if (c & ATA_STAT_ERR) {
267 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
268 log_debug("1 returned sense key %x status %02x\n",
271 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status %#02x\n",
277 n = ide_inb(device, ATA_CYL_HIGH);
279 n += ide_inb(device, ATA_CYL_LOW);
281 printf("ERROR, transfer bytes %d requested only %d\n", n,
286 if (!n && buflen < 0) {
287 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
292 log_debug("WARNING, transfer bytes %d not equal with requested %d\n",
295 if (n) { /* data transfer */
296 log_debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
297 /* we transfer shorts */
299 /* ok now decide if it is an in or output */
300 if (!(ide_inb(device, ATA_SECT_CNT) & 0x02)) {
301 log_debug("Write to device\n");
302 ide_output_data_shorts(device, (ushort *)buffer, n);
304 log_debug("Read from device @ %p shorts %d\n", buffer,
306 ide_input_data_shorts(device, (ushort *)buffer, n);
309 mdelay(5); /* seems that some CD ROMs need this... */
310 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
312 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
313 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
314 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
315 log_debug("2 returned sense key %x status %x\n", err, c);
324 * sending the command to atapi_issue. If an status other than good
325 * returns, an request_sense will be issued
328 #define ATAPI_DRIVE_NOT_READY 100
329 #define ATAPI_UNIT_ATTN 10
331 static u8 atapi_issue_autoreq(int device, u8 *ccb, int ccblen, u8 *buffer,
334 u8 sense_data[18], sense_ccb[12];
335 u8 res, key, asc, ascq;
336 int notready, unitattn;
338 unitattn = ATAPI_UNIT_ATTN;
339 notready = ATAPI_DRIVE_NOT_READY;
342 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
347 return 0xff; /* error */
349 log_debug("(auto_req)atapi_issue returned sense key %x\n", res);
351 memset(sense_ccb, 0, sizeof(sense_ccb));
352 memset(sense_data, 0, sizeof(sense_data));
353 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
354 sense_ccb[4] = 18; /* allocation Length */
356 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
357 key = (sense_data[2] & 0xf);
358 asc = (sense_data[12]);
359 ascq = (sense_data[13]);
361 log_debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
362 log_debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
363 sense_data[0], key, asc, ascq);
366 return 0; /* ok device ready */
368 if (key == 6 || asc == 0x29 || asc == 0x28) { /* Unit Attention */
369 if (unitattn-- > 0) {
373 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
376 if (asc == 0x4 && ascq == 0x1) {
377 /* not ready, but will be ready soon */
378 if (notready-- > 0) {
382 printf("Drive not ready, tried %d times\n",
383 ATAPI_DRIVE_NOT_READY);
387 log_debug("Media not present\n");
391 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
394 log_debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
400 * we transfer only one block per command, since the multiple DRQ per
401 * command is not yet implemented
403 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
404 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
405 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
407 static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
410 struct blk_desc *desc = dev_get_uclass_plat(dev);
411 int device = desc->devnum;
413 u8 ccb[12]; /* Command descriptor block */
416 log_debug("%d start " LBAF " blocks " LBAF " buffer at %lx\n", device,
417 blknr, blkcnt, (ulong)buffer);
420 if (blkcnt > ATAPI_READ_MAX_BLOCK)
421 cnt = ATAPI_READ_MAX_BLOCK;
425 ccb[0] = ATAPI_CMD_READ_12;
426 ccb[1] = 0; /* reserved */
427 ccb[2] = (u8)(blknr >> 24) & 0xff; /* MSB Block */
428 ccb[3] = (u8)(blknr >> 16) & 0xff; /* */
429 ccb[4] = (u8)(blknr >> 8) & 0xff;
430 ccb[5] = (u8)blknr & 0xff; /* LSB Block */
431 ccb[6] = (u8)(cnt >> 24) & 0xff; /* MSB Block cnt */
432 ccb[7] = (u8)(cnt >> 16) & 0xff;
433 ccb[8] = (u8)(cnt >> 8) & 0xff;
434 ccb[9] = (u8)cnt & 0xff; /* LSB Block */
435 ccb[10] = 0; /* reserved */
436 ccb[11] = 0; /* reserved */
438 if (atapi_issue_autoreq(device, ccb, 12,
440 cnt * ATAPI_READ_BLOCK_SIZE) == 0xff)
445 buffer += cnt * ATAPI_READ_BLOCK_SIZE;
446 } while (blkcnt > 0);
450 static void atapi_inquiry(struct blk_desc *desc)
452 u8 ccb[12]; /* Command descriptor block */
453 u8 iobuf[64]; /* temp buf */
457 device = desc->devnum;
458 desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
460 memset(ccb, 0, sizeof(ccb));
461 memset(iobuf, 0, sizeof(iobuf));
463 ccb[0] = ATAPI_CMD_INQUIRY;
464 ccb[4] = 40; /* allocation Legnth */
465 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 40);
467 log_debug("ATAPI_CMD_INQUIRY returned %x\n", c);
471 /* copy device ident strings */
472 ident_cpy((u8 *)desc->vendor, &iobuf[8], 8);
473 ident_cpy((u8 *)desc->product, &iobuf[16], 16);
474 ident_cpy((u8 *)desc->revision, &iobuf[32], 5);
479 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
480 desc->type = iobuf[0] & 0x1f;
487 memset(ccb, 0, sizeof(ccb));
488 memset(iobuf, 0, sizeof(iobuf));
489 ccb[0] = ATAPI_CMD_START_STOP;
490 ccb[4] = 0x03; /* start */
492 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 0);
494 log_debug("ATAPI_CMD_START_STOP returned %x\n", c);
498 memset(ccb, 0, sizeof(ccb));
499 memset(iobuf, 0, sizeof(iobuf));
500 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 0);
502 log_debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
506 memset(ccb, 0, sizeof(ccb));
507 memset(iobuf, 0, sizeof(iobuf));
508 ccb[0] = ATAPI_CMD_READ_CAP;
509 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 8);
510 log_debug("ATAPI_CMD_READ_CAP returned %x\n", c);
514 log_debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
515 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
516 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
518 desc->lba = (ulong)iobuf[0] << 24 | (ulong)iobuf[1] << 16 |
519 (ulong)iobuf[2] << 8 | (ulong)iobuf[3];
520 desc->blksz = (ulong)iobuf[4] << 24 | (ulong)iobuf[5] << 16 |
521 (ulong)iobuf[6] << 8 | (ulong)iobuf[7];
522 desc->log2blksz = LOG2(desc->blksz);
524 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
529 * ide_ident() - Identify an IDE device
531 * @device: Device number to use
532 * @desc: Block descriptor to fill in
533 * Returns: 0 if OK, -ENOENT if no device is found
535 static int ide_ident(int device, struct blk_desc *desc)
538 bool is_atapi = false;
542 memset(desc, '\0', sizeof(*desc));
543 desc->devnum = device;
544 desc->type = DEV_TYPE_UNKNOWN;
545 desc->uclass_id = UCLASS_IDE;
546 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
547 printf(" Device %d: ", device);
551 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
552 if (IS_ENABLED(CONFIG_ATAPI))
556 /* check signature */
557 if (IS_ENABLED(CONFIG_ATAPI) &&
558 ide_inb(device, ATA_SECT_CNT) == 0x01 &&
559 ide_inb(device, ATA_SECT_NUM) == 0x01 &&
560 ide_inb(device, ATA_CYL_LOW) == 0x14 &&
561 ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
562 /* ATAPI Signature found */
565 * Start Ident Command
567 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
569 * Wait for completion - ATAPI devices need more time
572 c = ide_wait(device, ATAPI_TIME_OUT);
575 * Start Ident Command
577 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
580 * Wait for completion
582 c = ide_wait(device, IDE_TIME_OUT);
585 if ((c & ATA_STAT_DRQ) &&
586 !(c & (ATA_STAT_FAULT | ATA_STAT_ERR))) {
588 } else if (IS_ENABLED(CONFIG_ATAPI)) {
590 * Need to soft reset the device
591 * in case it's an ATAPI...
593 log_debug("Retrying...\n");
594 ide_outb(device, ATA_DEV_HD,
595 ATA_LBA | ATA_DEVICE(device));
597 ide_outb(device, ATA_COMMAND, 0x08);
600 ide_outb(device, ATA_DEV_HD,
601 ATA_LBA | ATA_DEVICE(device));
606 if (!tries) /* Not found */
609 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
611 ident_cpy((u8 *)desc->revision, iop.fw_rev, sizeof(desc->revision));
612 ident_cpy((u8 *)desc->vendor, iop.model, sizeof(desc->vendor));
613 ident_cpy((u8 *)desc->product, iop.serial_no, sizeof(desc->product));
615 if (iop.config & 0x0080)
620 if (IS_ENABLED(CONFIG_ATAPI) && is_atapi) {
626 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
627 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
628 desc->lba = (ulong)iop.lba_capacity[0] |
629 (ulong)iop.lba_capacity[1] << 16;
631 if (IS_ENABLED(CONFIG_LBA48) && (iop.command_set_2 & 0x0400)) {
634 for (int i = 0; i < 4; i++)
635 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
636 desc->lba = (unsigned long long)iop.lba48_capacity[0] |
637 (unsigned long long)iop.lba48_capacity[1] << 16 |
638 (unsigned long long)iop.lba48_capacity[2] << 32 |
639 (unsigned long long)iop.lba48_capacity[3] << 48;
645 desc->type = DEV_TYPE_HARDDISK;
646 desc->blksz = ATA_BLOCKSIZE;
647 desc->log2blksz = LOG2(desc->blksz);
648 desc->lun = 0; /* just to fill something in... */
650 #if 0 /* only used to test the powersaving mode,
651 * if enabled, the drive goes after 5 sec
653 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
654 c = ide_wait(device, IDE_TIME_OUT);
655 ide_outb(device, ATA_SECT_CNT, 1);
656 ide_outb(device, ATA_LBA_LOW, 0);
657 ide_outb(device, ATA_LBA_MID, 0);
658 ide_outb(device, ATA_LBA_HIGH, 0);
659 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
660 ide_outb(device, ATA_COMMAND, 0xe3);
662 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
669 * ide_init_one() - Init one IDE device
672 * Return: 0 iuf OK, -EIO if not available, -ETIMEDOUT if timed out
674 static int ide_init_one(int bus)
676 int dev = bus * CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS;
680 printf("Bus %d: ", bus);
684 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
690 c = ide_inb(dev, ATA_STATUS);
692 if (i > (ATA_RESET_TIME * 100)) {
693 puts("** Timeout **\n");
696 if (i >= 100 && !(i % 100))
698 } while (c & ATA_STAT_BUSY);
700 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
701 puts("not available ");
702 log_debug("Status = %#02X ", c);
704 } else if (IS_ENABLED(CONFIG_ATAPI) && !(c & ATA_STAT_READY)) {
705 /* ATAPI Devices do not set DRDY */
706 puts("not available ");
707 log_debug("Status = %#02X ", c);
715 static void ide_output_data(int dev, const ulong *sect_buf, int words)
717 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
720 dbuf = (ushort *)sect_buf;
723 outw(cpu_to_le16(*dbuf++), paddr);
725 outw(cpu_to_le16(*dbuf++), paddr);
729 static void ide_input_data(int dev, ulong *sect_buf, int words)
731 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
734 dbuf = (ushort *)sect_buf;
736 log_debug("in input data base for read is %p\n", (void *)paddr);
740 *dbuf++ = le16_to_cpu(inw(paddr));
742 *dbuf++ = le16_to_cpu(inw(paddr));
746 static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
749 struct blk_desc *desc = dev_get_uclass_plat(dev);
750 int device = desc->devnum;
753 u8 pwrsave = 0; /* power save */
756 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
757 /* more than 28 bits used, use 48bit mode */
761 log_debug("dev %d start " LBAF ", blocks " LBAF " buffer at %lx\n",
762 device, blknr, blkcnt, (ulong)buffer);
766 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
767 c = ide_wait(device, IDE_TIME_OUT);
769 if (c & ATA_STAT_BUSY) {
770 printf("IDE read: device %d not ready\n", device);
774 /* first check if the drive is in Powersaving mode, if yes,
775 * increase the timeout value */
776 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
779 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
781 if (c & ATA_STAT_BUSY) {
782 printf("IDE read: device %d not ready\n", device);
785 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
786 printf("No Powersaving mode %x\n", c);
788 c = ide_inb(device, ATA_SECT_CNT);
789 log_debug("Powersaving %02X\n", c);
795 while (blkcnt-- > 0) {
796 c = ide_wait(device, IDE_TIME_OUT);
798 if (c & ATA_STAT_BUSY) {
799 printf("IDE read: device %d not ready\n", device);
802 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
803 /* write high bits */
804 ide_outb(device, ATA_SECT_CNT, 0);
805 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
806 #ifdef CONFIG_SYS_64BIT_LBA
807 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
808 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
810 ide_outb(device, ATA_LBA_MID, 0);
811 ide_outb(device, ATA_LBA_HIGH, 0);
814 ide_outb(device, ATA_SECT_CNT, 1);
815 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
816 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
817 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
819 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
820 ide_outb(device, ATA_DEV_HD,
821 ATA_LBA | ATA_DEVICE(device));
822 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
825 ide_outb(device, ATA_DEV_HD, ATA_LBA |
826 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
827 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
833 /* may take up to 4 sec */
834 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
837 /* can't take over 500 ms */
838 c = ide_wait(device, IDE_TIME_OUT);
841 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
843 printf("Error (no IRQ) dev %d blk " LBAF
844 ": status %#02x\n", device, blknr, c);
848 ide_input_data(device, buffer, ATA_SECTORWORDS);
849 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
853 buffer += ATA_BLOCKSIZE;
859 static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
862 struct blk_desc *desc = dev_get_uclass_plat(dev);
863 int device = desc->devnum;
868 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
869 /* more than 28 bits used, use 48bit mode */
875 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
877 while (blkcnt-- > 0) {
878 c = ide_wait(device, IDE_TIME_OUT);
880 if (c & ATA_STAT_BUSY) {
881 printf("IDE read: device %d not ready\n", device);
884 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
885 /* write high bits */
886 ide_outb(device, ATA_SECT_CNT, 0);
887 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
888 #ifdef CONFIG_SYS_64BIT_LBA
889 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
890 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
892 ide_outb(device, ATA_LBA_MID, 0);
893 ide_outb(device, ATA_LBA_HIGH, 0);
896 ide_outb(device, ATA_SECT_CNT, 1);
897 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
898 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
899 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
901 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
902 ide_outb(device, ATA_DEV_HD,
903 ATA_LBA | ATA_DEVICE(device));
904 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
907 ide_outb(device, ATA_DEV_HD, ATA_LBA |
908 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
909 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
914 /* can't take over 500 ms */
915 c = ide_wait(device, IDE_TIME_OUT);
917 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
919 printf("Error (no IRQ) dev %d blk " LBAF
920 ": status %#02x\n", device, blknr, c);
924 ide_output_data(device, buffer, ATA_SECTORWORDS);
925 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
928 buffer += ATA_BLOCKSIZE;
934 ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
937 struct blk_desc *desc = dev_get_uclass_plat(dev);
939 if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
940 return atapi_read(dev, blknr, blkcnt, buffer);
942 return ide_read(dev, blknr, blkcnt, buffer);
945 static const struct blk_ops ide_blk_ops = {
946 .read = ide_or_atapi_read,
950 U_BOOT_DRIVER(ide_blk) = {
956 static int ide_bootdev_bind(struct udevice *dev)
958 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
960 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
965 static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
969 uclass_first_device(UCLASS_IDE, &dev);
974 struct bootdev_ops ide_bootdev_ops = {
977 static const struct udevice_id ide_bootdev_ids[] = {
978 { .compatible = "u-boot,bootdev-ide" },
982 U_BOOT_DRIVER(ide_bootdev) = {
983 .name = "ide_bootdev",
984 .id = UCLASS_BOOTDEV,
985 .ops = &ide_bootdev_ops,
986 .bind = ide_bootdev_bind,
987 .of_match = ide_bootdev_ids,
990 BOOTDEV_HUNTER(ide_bootdev_hunter) = {
991 .prio = BOOTDEVP_5_SCAN_SLOW,
992 .uclass = UCLASS_IDE,
993 .hunt = ide_bootdev_hunt,
994 .drv = DM_DRIVER_REF(ide_bootdev),
997 static int ide_probe(struct udevice *udev)
999 bool bus_ok[CONFIG_SYS_IDE_MAXBUS];
1004 /* ATAPI Drives seems to need a proper IDE Reset */
1008 * Wait for IDE to get ready.
1009 * According to spec, this can take up to 31 seconds!
1011 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1012 bus_ok[bus] = !ide_init_one(bus);
1020 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1021 struct blk_desc *desc, pdesc;
1022 struct udevice *blk;
1026 if (!bus_ok[IDE_BUS(i)])
1029 ret = ide_ident(i, &pdesc);
1035 sprintf(name, "blk#%d", i);
1038 * With CDROM, if there is no CD inserted, blksz will
1039 * be zero, don't bother to create IDE block device.
1043 ret = blk_create_devicef(udev, "ide_blk", name, UCLASS_IDE, i,
1044 pdesc.blksz, pdesc.lba, &blk);
1048 ret = blk_probe_or_unbind(blk);
1052 /* fill in device vendor/product/rev strings */
1053 desc = dev_get_uclass_plat(blk);
1054 strlcpy(desc->vendor, pdesc.vendor, BLK_VEN_SIZE);
1055 strlcpy(desc->product, pdesc.product, BLK_PRD_SIZE);
1056 strlcpy(desc->revision, pdesc.revision, BLK_REV_SIZE);
1057 desc->removable = pdesc.removable;
1058 desc->atapi = pdesc.atapi;
1059 desc->lba48 = pdesc.lba48;
1060 desc->type = pdesc.type;
1062 ret = bootdev_setup_for_sibling_blk(blk, "ide_bootdev");
1064 return log_msg_ret("bd", ret);
1070 U_BOOT_DRIVER(ide) = {
1076 struct pci_device_id ide_supported[] = {
1077 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1081 U_BOOT_PCI_DEVICE(ide, ide_supported);
1083 UCLASS_DRIVER(ide) = {