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 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
40 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
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 #ifndef CONFIG_SYS_ATA_PORT_ADDR
49 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
52 #ifdef CONFIG_IDE_RESET
53 extern void ide_set_reset(int idereset);
55 static void ide_reset(void)
59 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
61 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
62 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
64 ide_set_reset(1); /* assert reset */
66 /* the reset signal shall be asserted for et least 25 us */
71 /* de-assert RESET signal */
75 for (i = 0; i < 250; ++i)
79 #define ide_reset() /* dummy */
80 #endif /* CONFIG_IDE_RESET */
83 * Wait until Busy bit is off, or timeout (in ms)
86 static uchar ide_wait(int dev, ulong t)
88 ulong delay = 10 * t; /* poll every 100 us */
91 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
100 * copy src to dest, skipping leading and trailing blanks and null
101 * terminate the string
102 * "len" is the size of available memory including the terminating '\0'
104 static void ident_cpy(unsigned char *dst, unsigned char *src,
107 unsigned char *end, *last;
112 /* reserve space for '\0' */
116 /* skip leading white space */
117 while ((*src) && (src < end) && (*src == ' '))
120 /* copy string, omitting trailing white space */
121 while ((*src) && (src < end)) {
131 /****************************************************************************
135 /* since ATAPI may use commands with not 4 bytes alligned length
136 * we have our own transfer functions, 2 bytes alligned */
137 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
139 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
142 dbuf = (ushort *)sect_buf;
144 debug("in output data shorts base for read is %p\n", (void *)paddr);
148 outw(cpu_to_le16(*dbuf++), paddr);
152 __weak void ide_input_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 debug("in input data shorts base for read is %p\n", (void *)paddr);
163 *dbuf++ = le16_to_cpu(inw(paddr));
168 * Wait until (Status & mask) == res, or timeout (in ms)
170 * This is used since some ATAPI CD ROMs clears their Busy Bit first
171 * and then they set their DRQ Bit
173 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
175 ulong delay = 10 * t; /* poll every 100 us */
178 /* prevents to read the status before valid */
179 c = ide_inb(dev, ATA_DEV_CTL);
181 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
182 /* break if error occurs (doesn't make sense to wait more) */
183 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
193 * issue an atapi command
195 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
196 unsigned char *buffer, int buflen)
198 unsigned char c, err, mask, res;
203 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
205 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
206 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
207 if ((c & mask) != res) {
208 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
214 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
215 ide_outb(device, ATA_SECT_CNT, 0);
216 ide_outb(device, ATA_SECT_NUM, 0);
217 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
218 ide_outb(device, ATA_CYL_HIGH,
219 (unsigned char) ((buflen >> 8) & 0xFF));
220 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
222 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
225 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
227 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
229 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
230 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
236 /* write command block */
237 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
239 /* ATAPI Command written wait for completition */
240 udelay(5000); /* device must set bsy */
242 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
244 * if no data wait for DRQ = 0 BSY = 0
245 * if data wait for DRQ = 1 BSY = 0
250 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
251 if ((c & mask) != res) {
252 if (c & ATA_STAT_ERR) {
253 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
254 debug("atapi_issue 1 returned sense key %X status %02X\n",
257 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
263 n = ide_inb(device, ATA_CYL_HIGH);
265 n += ide_inb(device, ATA_CYL_LOW);
267 printf("ERROR, transfer bytes %d requested only %d\n", n,
272 if ((n == 0) && (buflen < 0)) {
273 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
278 debug("WARNING, transfer bytes %d not equal with requested %d\n",
281 if (n != 0) { /* data transfer */
282 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
283 /* we transfer shorts */
285 /* ok now decide if it is an in or output */
286 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
287 debug("Write to device\n");
288 ide_output_data_shorts(device, (unsigned short *)buffer,
291 debug("Read from device @ %p shorts %d\n", buffer, n);
292 ide_input_data_shorts(device, (unsigned short *)buffer,
296 udelay(5000); /* seems that some CD ROMs need this... */
297 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
299 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
300 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
301 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
302 debug("atapi_issue 2 returned sense key %X status %X\n", err,
312 * sending the command to atapi_issue. If an status other than good
313 * returns, an request_sense will be issued
316 #define ATAPI_DRIVE_NOT_READY 100
317 #define ATAPI_UNIT_ATTN 10
319 unsigned char atapi_issue_autoreq(int device,
322 unsigned char *buffer, int buflen)
324 unsigned char sense_data[18], sense_ccb[12];
325 unsigned char res, key, asc, ascq;
326 int notready, unitattn;
328 unitattn = ATAPI_UNIT_ATTN;
329 notready = ATAPI_DRIVE_NOT_READY;
332 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
337 return 0xFF; /* error */
339 debug("(auto_req)atapi_issue returned sense key %X\n", res);
341 memset(sense_ccb, 0, sizeof(sense_ccb));
342 memset(sense_data, 0, sizeof(sense_data));
343 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
344 sense_ccb[4] = 18; /* allocation Length */
346 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
347 key = (sense_data[2] & 0xF);
348 asc = (sense_data[12]);
349 ascq = (sense_data[13]);
351 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
352 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
353 sense_data[0], key, asc, ascq);
356 return 0; /* ok device ready */
358 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
359 if (unitattn-- > 0) {
363 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
366 if ((asc == 0x4) && (ascq == 0x1)) {
367 /* not ready, but will be ready soon */
368 if (notready-- > 0) {
372 printf("Drive not ready, tried %d times\n",
373 ATAPI_DRIVE_NOT_READY);
377 debug("Media not present\n");
381 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
384 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
390 * we transfer only one block per command, since the multiple DRQ per
391 * command is not yet implemented
393 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
394 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
395 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
397 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
400 int device = block_dev->devnum;
402 unsigned char ccb[12]; /* Command descriptor block */
405 debug("atapi_read dev %d start " LBAF " blocks " LBAF
406 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
409 if (blkcnt > ATAPI_READ_MAX_BLOCK)
410 cnt = ATAPI_READ_MAX_BLOCK;
414 ccb[0] = ATAPI_CMD_READ_12;
415 ccb[1] = 0; /* reserved */
416 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
417 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
418 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
419 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
420 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
421 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
422 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
423 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
424 ccb[10] = 0; /* reserved */
425 ccb[11] = 0; /* reserved */
427 if (atapi_issue_autoreq(device, ccb, 12,
428 (unsigned char *)buffer,
429 cnt * ATAPI_READ_BLOCK_SIZE)
436 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
437 } while (blkcnt > 0);
441 static void atapi_inquiry(struct blk_desc *dev_desc)
443 unsigned char ccb[12]; /* Command descriptor block */
444 unsigned char iobuf[64]; /* temp buf */
448 device = dev_desc->devnum;
449 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
451 dev_desc->block_read = atapi_read;
454 memset(ccb, 0, sizeof(ccb));
455 memset(iobuf, 0, sizeof(iobuf));
457 ccb[0] = ATAPI_CMD_INQUIRY;
458 ccb[4] = 40; /* allocation Legnth */
459 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
461 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
465 /* copy device ident strings */
466 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
467 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
468 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
473 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
474 dev_desc->type = iobuf[0] & 0x1f;
476 if ((iobuf[1] & 0x80) == 0x80)
477 dev_desc->removable = 1;
479 dev_desc->removable = 0;
481 memset(ccb, 0, sizeof(ccb));
482 memset(iobuf, 0, sizeof(iobuf));
483 ccb[0] = ATAPI_CMD_START_STOP;
484 ccb[4] = 0x03; /* start */
486 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
488 debug("ATAPI_CMD_START_STOP returned %x\n", c);
492 memset(ccb, 0, sizeof(ccb));
493 memset(iobuf, 0, sizeof(iobuf));
494 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
496 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
500 memset(ccb, 0, sizeof(ccb));
501 memset(iobuf, 0, sizeof(iobuf));
502 ccb[0] = ATAPI_CMD_READ_CAP;
503 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
504 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
508 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
509 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
510 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
512 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
513 ((unsigned long) iobuf[1] << 16) +
514 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
515 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
516 ((unsigned long) iobuf[5] << 16) +
517 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
518 dev_desc->log2blksz = LOG2(dev_desc->blksz);
520 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
526 #endif /* CONFIG_ATAPI */
528 static void ide_ident(struct blk_desc *dev_desc)
538 device = dev_desc->devnum;
539 printf(" Device %d: ", device);
543 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
544 dev_desc->if_type = IF_TYPE_IDE;
549 /* Warning: This will be tricky to read */
550 while (retries <= 1) {
551 /* check signature */
552 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
553 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
554 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
555 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
556 /* ATAPI Signature found */
557 dev_desc->if_type = IF_TYPE_ATAPI;
559 * Start Ident Command
561 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
563 * Wait for completion - ATAPI devices need more time
566 c = ide_wait(device, ATAPI_TIME_OUT);
571 * Start Ident Command
573 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
576 * Wait for completion
578 c = ide_wait(device, IDE_TIME_OUT);
581 if (((c & ATA_STAT_DRQ) == 0) ||
582 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
586 * Need to soft reset the device
587 * in case it's an ATAPI...
589 debug("Retrying...\n");
590 ide_outb(device, ATA_DEV_HD,
591 ATA_LBA | ATA_DEVICE(device));
593 ide_outb(device, ATA_COMMAND, 0x08);
594 udelay(500000); /* 500 ms */
599 ide_outb(device, ATA_DEV_HD,
600 ATA_LBA | ATA_DEVICE(device));
609 } /* see above - ugly to read */
611 if (retries == 2) /* Not found */
615 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
617 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
618 sizeof(dev_desc->revision));
619 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
620 sizeof(dev_desc->vendor));
621 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
622 sizeof(dev_desc->product));
624 if ((iop.config & 0x0080) == 0x0080)
625 dev_desc->removable = 1;
627 dev_desc->removable = 0;
630 if (dev_desc->if_type == IF_TYPE_ATAPI) {
631 atapi_inquiry(dev_desc);
634 #endif /* CONFIG_ATAPI */
636 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
637 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
639 ((unsigned long)iop.lba_capacity[0]) |
640 ((unsigned long)iop.lba_capacity[1] << 16);
643 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
645 for (int i = 0; i < 4; i++)
646 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
648 ((unsigned long long)iop.lba48_capacity[0] |
649 ((unsigned long long)iop.lba48_capacity[1] << 16) |
650 ((unsigned long long)iop.lba48_capacity[2] << 32) |
651 ((unsigned long long)iop.lba48_capacity[3] << 48));
655 #endif /* CONFIG_LBA48 */
657 dev_desc->type = DEV_TYPE_HARDDISK;
658 dev_desc->blksz = ATA_BLOCKSIZE;
659 dev_desc->log2blksz = LOG2(dev_desc->blksz);
660 dev_desc->lun = 0; /* just to fill something in... */
662 #if 0 /* only used to test the powersaving mode,
663 * if enabled, the drive goes after 5 sec
665 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
666 c = ide_wait(device, IDE_TIME_OUT);
667 ide_outb(device, ATA_SECT_CNT, 1);
668 ide_outb(device, ATA_LBA_LOW, 0);
669 ide_outb(device, ATA_LBA_MID, 0);
670 ide_outb(device, ATA_LBA_HIGH, 0);
671 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
672 ide_outb(device, ATA_COMMAND, 0xe3);
674 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
678 __weak void ide_outb(int dev, int port, unsigned char val)
680 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
682 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
684 #if defined(CONFIG_IDE_AHB)
687 ide_write_register(dev, port, val);
690 outb(val, (ATA_CURR_BASE(dev)));
693 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
697 __weak unsigned char ide_inb(int dev, int port)
701 #if defined(CONFIG_IDE_AHB)
702 val = ide_read_register(dev, port);
704 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
707 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
709 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
718 #ifdef CONFIG_IDE_PREINIT
722 puts("ide_preinit failed\n");
725 #endif /* CONFIG_IDE_PREINIT */
729 /* ATAPI Drives seems to need a proper IDE Reset */
733 * Wait for IDE to get ready.
734 * According to spec, this can take up to 31 seconds!
736 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
738 bus * (CONFIG_SYS_IDE_MAXDEVICE /
739 CONFIG_SYS_IDE_MAXBUS);
741 printf("Bus %d: ", bus);
747 udelay(100000); /* 100 ms */
748 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
749 udelay(100000); /* 100 ms */
752 udelay(10000); /* 10 ms */
754 c = ide_inb(dev, ATA_STATUS);
756 if (i > (ATA_RESET_TIME * 100)) {
757 puts("** Timeout **\n");
760 if ((i >= 100) && ((i % 100) == 0))
763 } while (c & ATA_STAT_BUSY);
765 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
766 puts("not available ");
767 debug("Status = 0x%02X ", c);
768 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
769 } else if ((c & ATA_STAT_READY) == 0) {
770 puts("not available ");
771 debug("Status = 0x%02X ", c);
782 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
783 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
784 ide_dev_desc[i].if_type = IF_TYPE_IDE;
785 ide_dev_desc[i].devnum = i;
786 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
787 ide_dev_desc[i].blksz = 0;
788 ide_dev_desc[i].log2blksz =
789 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
790 ide_dev_desc[i].lba = 0;
792 ide_dev_desc[i].block_read = ide_read;
793 ide_dev_desc[i].block_write = ide_write;
795 if (!ide_bus_ok[IDE_BUS(i)])
797 ide_ident(&ide_dev_desc[i]);
798 dev_print(&ide_dev_desc[i]);
801 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
802 /* initialize partition type */
803 part_init(&ide_dev_desc[i]);
812 uclass_first_device(UCLASS_IDE, &dev);
816 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
818 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
819 ushort *dbuf = (ushort *)sect_buf;
821 debug("in input swap data base for read is %p\n", (void *)paddr);
825 *dbuf++ = be16_to_cpu(inw(paddr));
827 *dbuf++ = be16_to_cpu(inw(paddr));
831 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
833 #if defined(CONFIG_IDE_AHB)
834 ide_write_data(dev, sect_buf, words);
836 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
839 dbuf = (ushort *)sect_buf;
842 outw(cpu_to_le16(*dbuf++), paddr);
844 outw(cpu_to_le16(*dbuf++), paddr);
846 #endif /* CONFIG_IDE_AHB */
849 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
851 #if defined(CONFIG_IDE_AHB)
852 ide_read_data(dev, sect_buf, words);
854 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
857 dbuf = (ushort *)sect_buf;
859 debug("in input data base for read is %p\n", (void *)paddr);
863 *dbuf++ = le16_to_cpu(inw(paddr));
865 *dbuf++ = le16_to_cpu(inw(paddr));
867 #endif /* CONFIG_IDE_AHB */
871 ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
874 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
879 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
881 int device = block_dev->devnum;
884 unsigned char pwrsave = 0; /* power save */
887 unsigned char lba48 = 0;
889 if (blknr & 0x0000fffff0000000ULL) {
890 /* more than 28 bits used, use 48bit mode */
894 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
895 device, blknr, blkcnt, (ulong) buffer);
899 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
900 c = ide_wait(device, IDE_TIME_OUT);
902 if (c & ATA_STAT_BUSY) {
903 printf("IDE read: device %d not ready\n", device);
907 /* first check if the drive is in Powersaving mode, if yes,
908 * increase the timeout value */
909 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
912 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
914 if (c & ATA_STAT_BUSY) {
915 printf("IDE read: device %d not ready\n", device);
918 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
919 printf("No Powersaving mode %X\n", c);
921 c = ide_inb(device, ATA_SECT_CNT);
922 debug("Powersaving %02X\n", c);
928 while (blkcnt-- > 0) {
929 c = ide_wait(device, IDE_TIME_OUT);
931 if (c & ATA_STAT_BUSY) {
932 printf("IDE read: device %d not ready\n", device);
937 /* write high bits */
938 ide_outb(device, ATA_SECT_CNT, 0);
939 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
940 #ifdef CONFIG_SYS_64BIT_LBA
941 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
942 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
944 ide_outb(device, ATA_LBA_MID, 0);
945 ide_outb(device, ATA_LBA_HIGH, 0);
949 ide_outb(device, ATA_SECT_CNT, 1);
950 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
951 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
952 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
956 ide_outb(device, ATA_DEV_HD,
957 ATA_LBA | ATA_DEVICE(device));
958 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
963 ide_outb(device, ATA_DEV_HD, ATA_LBA |
964 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
965 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
971 /* may take up to 4 sec */
972 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
975 /* can't take over 500 ms */
976 c = ide_wait(device, IDE_TIME_OUT);
979 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
981 printf("Error (no IRQ) dev %d blk " LBAF
982 ": status %#02x\n", device, blknr, c);
986 ide_input_data(device, buffer, ATA_SECTORWORDS);
987 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
991 buffer += ATA_BLOCKSIZE;
998 ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
1001 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1006 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
1008 int device = block_dev->devnum;
1013 unsigned char lba48 = 0;
1015 if (blknr & 0x0000fffff0000000ULL) {
1016 /* more than 28 bits used, use 48bit mode */
1023 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1025 while (blkcnt-- > 0) {
1026 c = ide_wait(device, IDE_TIME_OUT);
1028 if (c & ATA_STAT_BUSY) {
1029 printf("IDE read: device %d not ready\n", device);
1034 /* write high bits */
1035 ide_outb(device, ATA_SECT_CNT, 0);
1036 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1037 #ifdef CONFIG_SYS_64BIT_LBA
1038 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1039 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1041 ide_outb(device, ATA_LBA_MID, 0);
1042 ide_outb(device, ATA_LBA_HIGH, 0);
1046 ide_outb(device, ATA_SECT_CNT, 1);
1047 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1048 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1049 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1053 ide_outb(device, ATA_DEV_HD,
1054 ATA_LBA | ATA_DEVICE(device));
1055 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
1060 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1061 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1062 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
1067 /* can't take over 500 ms */
1068 c = ide_wait(device, IDE_TIME_OUT);
1070 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1072 printf("Error (no IRQ) dev %d blk " LBAF
1073 ": status %#02x\n", device, blknr, c);
1077 ide_output_data(device, buffer, ATA_SECTORWORDS);
1078 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1081 buffer += ATA_BLOCKSIZE;
1087 #if defined(CONFIG_OF_IDE_FIXUP)
1088 int ide_device_present(int dev)
1090 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1092 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1097 static int ide_blk_probe(struct udevice *udev)
1099 struct blk_desc *desc = dev_get_uclass_plat(udev);
1101 /* fill in device vendor/product/rev strings */
1102 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1104 desc->vendor[BLK_VEN_SIZE] = '\0';
1105 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1107 desc->product[BLK_PRD_SIZE] = '\0';
1108 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1110 desc->revision[BLK_REV_SIZE] = '\0';
1115 static const struct blk_ops ide_blk_ops = {
1120 U_BOOT_DRIVER(ide_blk) = {
1123 .ops = &ide_blk_ops,
1124 .probe = ide_blk_probe,
1127 static int ide_probe(struct udevice *udev)
1129 struct udevice *blk_dev;
1136 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1137 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1138 sprintf(name, "blk#%d", i);
1140 blksz = ide_dev_desc[i].blksz;
1141 size = blksz * ide_dev_desc[i].lba;
1144 * With CDROM, if there is no CD inserted, blksz will
1145 * be zero, don't bother to create IDE block device.
1149 ret = blk_create_devicef(udev, "ide_blk", name,
1151 blksz, size, &blk_dev);
1160 U_BOOT_DRIVER(ide) = {
1166 struct pci_device_id ide_supported[] = {
1167 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1171 U_BOOT_PCI_DEVICE(ide, ide_supported);
1173 UCLASS_DRIVER(ide) = {
1178 U_BOOT_LEGACY_BLK(ide) = {
1179 .if_typename = "ide",
1180 .if_type = IF_TYPE_IDE,
1181 .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
1182 .desc = ide_dev_desc,