1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
16 #include <linux/delay.h>
19 # define EIEIO __asm__ volatile ("eieio")
20 # define SYNC __asm__ volatile ("sync")
22 # define EIEIO /* nothing */
23 # define SYNC /* nothing */
26 /* Current offset for IDE0 / IDE1 bus access */
27 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
28 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
29 CONFIG_SYS_ATA_IDE0_OFFSET,
31 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
32 CONFIG_SYS_ATA_IDE1_OFFSET,
36 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
38 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
40 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
42 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
44 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
46 #ifndef CONFIG_SYS_ATA_PORT_ADDR
47 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
50 #ifdef CONFIG_IDE_RESET
51 extern void ide_set_reset(int idereset);
53 static void ide_reset(void)
57 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
59 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
60 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
62 ide_set_reset(1); /* assert reset */
64 /* the reset signal shall be asserted for et least 25 us */
69 /* de-assert RESET signal */
73 for (i = 0; i < 250; ++i)
77 #define ide_reset() /* dummy */
78 #endif /* CONFIG_IDE_RESET */
81 * Wait until Busy bit is off, or timeout (in ms)
84 static uchar ide_wait(int dev, ulong t)
86 ulong delay = 10 * t; /* poll every 100 us */
89 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
98 * copy src to dest, skipping leading and trailing blanks and null
99 * terminate the string
100 * "len" is the size of available memory including the terminating '\0'
102 static void ident_cpy(unsigned char *dst, unsigned char *src,
105 unsigned char *end, *last;
110 /* reserve space for '\0' */
114 /* skip leading white space */
115 while ((*src) && (src < end) && (*src == ' '))
118 /* copy string, omitting trailing white space */
119 while ((*src) && (src < end)) {
129 /****************************************************************************
133 /* since ATAPI may use commands with not 4 bytes alligned length
134 * we have our own transfer functions, 2 bytes alligned */
135 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
137 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
140 dbuf = (ushort *)sect_buf;
142 debug("in output data shorts base for read is %p\n", (void *)paddr);
146 outw(cpu_to_le16(*dbuf++), paddr);
150 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
152 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
155 dbuf = (ushort *)sect_buf;
157 debug("in input data shorts base for read is %p\n", (void *)paddr);
161 *dbuf++ = le16_to_cpu(inw(paddr));
166 * Wait until (Status & mask) == res, or timeout (in ms)
168 * This is used since some ATAPI CD ROMs clears their Busy Bit first
169 * and then they set their DRQ Bit
171 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
173 ulong delay = 10 * t; /* poll every 100 us */
176 /* prevents to read the status before valid */
177 c = ide_inb(dev, ATA_DEV_CTL);
179 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
180 /* break if error occurs (doesn't make sense to wait more) */
181 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
191 * issue an atapi command
193 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
194 unsigned char *buffer, int buflen)
196 unsigned char c, err, mask, res;
201 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
203 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
204 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
205 if ((c & mask) != res) {
206 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
212 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
213 ide_outb(device, ATA_SECT_CNT, 0);
214 ide_outb(device, ATA_SECT_NUM, 0);
215 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
216 ide_outb(device, ATA_CYL_HIGH,
217 (unsigned char) ((buflen >> 8) & 0xFF));
218 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
220 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
223 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
225 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
227 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
228 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
234 /* write command block */
235 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
237 /* ATAPI Command written wait for completition */
238 udelay(5000); /* device must set bsy */
240 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
242 * if no data wait for DRQ = 0 BSY = 0
243 * if data wait for DRQ = 1 BSY = 0
248 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
249 if ((c & mask) != res) {
250 if (c & ATA_STAT_ERR) {
251 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
252 debug("atapi_issue 1 returned sense key %X status %02X\n",
255 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
261 n = ide_inb(device, ATA_CYL_HIGH);
263 n += ide_inb(device, ATA_CYL_LOW);
265 printf("ERROR, transfer bytes %d requested only %d\n", n,
270 if ((n == 0) && (buflen < 0)) {
271 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
276 debug("WARNING, transfer bytes %d not equal with requested %d\n",
279 if (n != 0) { /* data transfer */
280 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
281 /* we transfer shorts */
283 /* ok now decide if it is an in or output */
284 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
285 debug("Write to device\n");
286 ide_output_data_shorts(device, (unsigned short *)buffer,
289 debug("Read from device @ %p shorts %d\n", buffer, n);
290 ide_input_data_shorts(device, (unsigned short *)buffer,
294 udelay(5000); /* seems that some CD ROMs need this... */
295 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
297 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
298 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
299 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
300 debug("atapi_issue 2 returned sense key %X status %X\n", err,
310 * sending the command to atapi_issue. If an status other than good
311 * returns, an request_sense will be issued
314 #define ATAPI_DRIVE_NOT_READY 100
315 #define ATAPI_UNIT_ATTN 10
317 unsigned char atapi_issue_autoreq(int device,
320 unsigned char *buffer, int buflen)
322 unsigned char sense_data[18], sense_ccb[12];
323 unsigned char res, key, asc, ascq;
324 int notready, unitattn;
326 unitattn = ATAPI_UNIT_ATTN;
327 notready = ATAPI_DRIVE_NOT_READY;
330 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
335 return 0xFF; /* error */
337 debug("(auto_req)atapi_issue returned sense key %X\n", res);
339 memset(sense_ccb, 0, sizeof(sense_ccb));
340 memset(sense_data, 0, sizeof(sense_data));
341 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
342 sense_ccb[4] = 18; /* allocation Length */
344 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
345 key = (sense_data[2] & 0xF);
346 asc = (sense_data[12]);
347 ascq = (sense_data[13]);
349 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
350 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
351 sense_data[0], key, asc, ascq);
354 return 0; /* ok device ready */
356 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
357 if (unitattn-- > 0) {
361 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
364 if ((asc == 0x4) && (ascq == 0x1)) {
365 /* not ready, but will be ready soon */
366 if (notready-- > 0) {
370 printf("Drive not ready, tried %d times\n",
371 ATAPI_DRIVE_NOT_READY);
375 debug("Media not present\n");
379 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
382 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
388 * we transfer only one block per command, since the multiple DRQ per
389 * command is not yet implemented
391 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
392 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
393 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
395 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
398 int device = block_dev->devnum;
400 unsigned char ccb[12]; /* Command descriptor block */
403 debug("atapi_read dev %d start " LBAF " blocks " LBAF
404 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
407 if (blkcnt > ATAPI_READ_MAX_BLOCK)
408 cnt = ATAPI_READ_MAX_BLOCK;
412 ccb[0] = ATAPI_CMD_READ_12;
413 ccb[1] = 0; /* reserved */
414 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
415 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
416 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
417 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
418 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
419 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
420 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
421 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
422 ccb[10] = 0; /* reserved */
423 ccb[11] = 0; /* reserved */
425 if (atapi_issue_autoreq(device, ccb, 12,
426 (unsigned char *)buffer,
427 cnt * ATAPI_READ_BLOCK_SIZE)
434 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
435 } while (blkcnt > 0);
439 static void atapi_inquiry(struct blk_desc *dev_desc)
441 unsigned char ccb[12]; /* Command descriptor block */
442 unsigned char iobuf[64]; /* temp buf */
446 device = dev_desc->devnum;
447 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
449 dev_desc->block_read = atapi_read;
452 memset(ccb, 0, sizeof(ccb));
453 memset(iobuf, 0, sizeof(iobuf));
455 ccb[0] = ATAPI_CMD_INQUIRY;
456 ccb[4] = 40; /* allocation Legnth */
457 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
459 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
463 /* copy device ident strings */
464 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
465 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
466 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
471 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
472 dev_desc->type = iobuf[0] & 0x1f;
474 if ((iobuf[1] & 0x80) == 0x80)
475 dev_desc->removable = 1;
477 dev_desc->removable = 0;
479 memset(ccb, 0, sizeof(ccb));
480 memset(iobuf, 0, sizeof(iobuf));
481 ccb[0] = ATAPI_CMD_START_STOP;
482 ccb[4] = 0x03; /* start */
484 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
486 debug("ATAPI_CMD_START_STOP returned %x\n", c);
490 memset(ccb, 0, sizeof(ccb));
491 memset(iobuf, 0, sizeof(iobuf));
492 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
494 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
498 memset(ccb, 0, sizeof(ccb));
499 memset(iobuf, 0, sizeof(iobuf));
500 ccb[0] = ATAPI_CMD_READ_CAP;
501 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
502 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
506 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
507 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
508 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
510 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
511 ((unsigned long) iobuf[1] << 16) +
512 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
513 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
514 ((unsigned long) iobuf[5] << 16) +
515 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
516 dev_desc->log2blksz = LOG2(dev_desc->blksz);
518 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
524 #endif /* CONFIG_ATAPI */
526 static void ide_ident(struct blk_desc *dev_desc)
536 device = dev_desc->devnum;
537 printf(" Device %d: ", device);
541 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
542 dev_desc->if_type = IF_TYPE_IDE;
547 /* Warning: This will be tricky to read */
548 while (retries <= 1) {
549 /* check signature */
550 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
551 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
552 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
553 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
554 /* ATAPI Signature found */
555 dev_desc->if_type = IF_TYPE_ATAPI;
557 * Start Ident Command
559 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
561 * Wait for completion - ATAPI devices need more time
564 c = ide_wait(device, ATAPI_TIME_OUT);
569 * Start Ident Command
571 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
574 * Wait for completion
576 c = ide_wait(device, IDE_TIME_OUT);
579 if (((c & ATA_STAT_DRQ) == 0) ||
580 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
584 * Need to soft reset the device
585 * in case it's an ATAPI...
587 debug("Retrying...\n");
588 ide_outb(device, ATA_DEV_HD,
589 ATA_LBA | ATA_DEVICE(device));
591 ide_outb(device, ATA_COMMAND, 0x08);
592 udelay(500000); /* 500 ms */
597 ide_outb(device, ATA_DEV_HD,
598 ATA_LBA | ATA_DEVICE(device));
607 } /* see above - ugly to read */
609 if (retries == 2) /* Not found */
613 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
615 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
616 sizeof(dev_desc->revision));
617 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
618 sizeof(dev_desc->vendor));
619 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
620 sizeof(dev_desc->product));
622 if ((iop.config & 0x0080) == 0x0080)
623 dev_desc->removable = 1;
625 dev_desc->removable = 0;
628 if (dev_desc->if_type == IF_TYPE_ATAPI) {
629 atapi_inquiry(dev_desc);
632 #endif /* CONFIG_ATAPI */
634 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
635 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
637 ((unsigned long)iop.lba_capacity[0]) |
638 ((unsigned long)iop.lba_capacity[1] << 16);
641 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
643 for (int i = 0; i < 4; i++)
644 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
646 ((unsigned long long)iop.lba48_capacity[0] |
647 ((unsigned long long)iop.lba48_capacity[1] << 16) |
648 ((unsigned long long)iop.lba48_capacity[2] << 32) |
649 ((unsigned long long)iop.lba48_capacity[3] << 48));
653 #endif /* CONFIG_LBA48 */
655 dev_desc->type = DEV_TYPE_HARDDISK;
656 dev_desc->blksz = ATA_BLOCKSIZE;
657 dev_desc->log2blksz = LOG2(dev_desc->blksz);
658 dev_desc->lun = 0; /* just to fill something in... */
660 #if 0 /* only used to test the powersaving mode,
661 * if enabled, the drive goes after 5 sec
663 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
664 c = ide_wait(device, IDE_TIME_OUT);
665 ide_outb(device, ATA_SECT_CNT, 1);
666 ide_outb(device, ATA_LBA_LOW, 0);
667 ide_outb(device, ATA_LBA_MID, 0);
668 ide_outb(device, ATA_LBA_HIGH, 0);
669 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
670 ide_outb(device, ATA_COMMAND, 0xe3);
672 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
676 __weak void ide_outb(int dev, int port, unsigned char val)
678 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
680 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
682 #if defined(CONFIG_IDE_AHB)
685 ide_write_register(dev, port, val);
688 outb(val, (ATA_CURR_BASE(dev)));
691 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
695 __weak unsigned char ide_inb(int dev, int port)
699 #if defined(CONFIG_IDE_AHB)
700 val = ide_read_register(dev, port);
702 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
705 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
707 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
716 #ifdef CONFIG_IDE_PREINIT
720 puts("ide_preinit failed\n");
723 #endif /* CONFIG_IDE_PREINIT */
727 /* ATAPI Drives seems to need a proper IDE Reset */
731 * Wait for IDE to get ready.
732 * According to spec, this can take up to 31 seconds!
734 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
736 bus * (CONFIG_SYS_IDE_MAXDEVICE /
737 CONFIG_SYS_IDE_MAXBUS);
739 printf("Bus %d: ", bus);
745 udelay(100000); /* 100 ms */
746 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
747 udelay(100000); /* 100 ms */
750 udelay(10000); /* 10 ms */
752 c = ide_inb(dev, ATA_STATUS);
754 if (i > (ATA_RESET_TIME * 100)) {
755 puts("** Timeout **\n");
758 if ((i >= 100) && ((i % 100) == 0))
761 } while (c & ATA_STAT_BUSY);
763 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
764 puts("not available ");
765 debug("Status = 0x%02X ", c);
766 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
767 } else if ((c & ATA_STAT_READY) == 0) {
768 puts("not available ");
769 debug("Status = 0x%02X ", c);
780 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
781 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
782 ide_dev_desc[i].if_type = IF_TYPE_IDE;
783 ide_dev_desc[i].devnum = i;
784 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
785 ide_dev_desc[i].blksz = 0;
786 ide_dev_desc[i].log2blksz =
787 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
788 ide_dev_desc[i].lba = 0;
790 ide_dev_desc[i].block_read = ide_read;
791 ide_dev_desc[i].block_write = ide_write;
793 if (!ide_bus_ok[IDE_BUS(i)])
795 ide_ident(&ide_dev_desc[i]);
796 dev_print(&ide_dev_desc[i]);
799 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
800 /* initialize partition type */
801 part_init(&ide_dev_desc[i]);
810 uclass_first_device(UCLASS_IDE, &dev);
814 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
816 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
817 ushort *dbuf = (ushort *)sect_buf;
819 debug("in input swap data base for read is %p\n", (void *)paddr);
823 *dbuf++ = be16_to_cpu(inw(paddr));
825 *dbuf++ = be16_to_cpu(inw(paddr));
829 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
831 #if defined(CONFIG_IDE_AHB)
832 ide_write_data(dev, sect_buf, words);
834 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
837 dbuf = (ushort *)sect_buf;
840 outw(cpu_to_le16(*dbuf++), paddr);
842 outw(cpu_to_le16(*dbuf++), paddr);
844 #endif /* CONFIG_IDE_AHB */
847 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
849 #if defined(CONFIG_IDE_AHB)
850 ide_read_data(dev, sect_buf, words);
852 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
855 dbuf = (ushort *)sect_buf;
857 debug("in input data base for read is %p\n", (void *)paddr);
861 *dbuf++ = le16_to_cpu(inw(paddr));
863 *dbuf++ = le16_to_cpu(inw(paddr));
865 #endif /* CONFIG_IDE_AHB */
869 ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
872 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
877 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
879 int device = block_dev->devnum;
882 unsigned char pwrsave = 0; /* power save */
885 unsigned char lba48 = 0;
887 if (blknr & 0x0000fffff0000000ULL) {
888 /* more than 28 bits used, use 48bit mode */
892 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
893 device, blknr, blkcnt, (ulong) buffer);
897 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
898 c = ide_wait(device, IDE_TIME_OUT);
900 if (c & ATA_STAT_BUSY) {
901 printf("IDE read: device %d not ready\n", device);
905 /* first check if the drive is in Powersaving mode, if yes,
906 * increase the timeout value */
907 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
910 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
912 if (c & ATA_STAT_BUSY) {
913 printf("IDE read: device %d not ready\n", device);
916 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
917 printf("No Powersaving mode %X\n", c);
919 c = ide_inb(device, ATA_SECT_CNT);
920 debug("Powersaving %02X\n", c);
926 while (blkcnt-- > 0) {
927 c = ide_wait(device, IDE_TIME_OUT);
929 if (c & ATA_STAT_BUSY) {
930 printf("IDE read: device %d not ready\n", device);
935 /* write high bits */
936 ide_outb(device, ATA_SECT_CNT, 0);
937 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
938 #ifdef CONFIG_SYS_64BIT_LBA
939 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
940 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
942 ide_outb(device, ATA_LBA_MID, 0);
943 ide_outb(device, ATA_LBA_HIGH, 0);
947 ide_outb(device, ATA_SECT_CNT, 1);
948 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
949 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
950 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
954 ide_outb(device, ATA_DEV_HD,
955 ATA_LBA | ATA_DEVICE(device));
956 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
961 ide_outb(device, ATA_DEV_HD, ATA_LBA |
962 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
963 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
969 /* may take up to 4 sec */
970 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
973 /* can't take over 500 ms */
974 c = ide_wait(device, IDE_TIME_OUT);
977 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
979 printf("Error (no IRQ) dev %d blk " LBAF
980 ": status %#02x\n", device, blknr, c);
984 ide_input_data(device, buffer, ATA_SECTORWORDS);
985 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
989 buffer += ATA_BLOCKSIZE;
996 ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
999 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1004 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
1006 int device = block_dev->devnum;
1011 unsigned char lba48 = 0;
1013 if (blknr & 0x0000fffff0000000ULL) {
1014 /* more than 28 bits used, use 48bit mode */
1021 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1023 while (blkcnt-- > 0) {
1024 c = ide_wait(device, IDE_TIME_OUT);
1026 if (c & ATA_STAT_BUSY) {
1027 printf("IDE read: device %d not ready\n", device);
1032 /* write high bits */
1033 ide_outb(device, ATA_SECT_CNT, 0);
1034 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1035 #ifdef CONFIG_SYS_64BIT_LBA
1036 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1037 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1039 ide_outb(device, ATA_LBA_MID, 0);
1040 ide_outb(device, ATA_LBA_HIGH, 0);
1044 ide_outb(device, ATA_SECT_CNT, 1);
1045 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1046 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1047 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1051 ide_outb(device, ATA_DEV_HD,
1052 ATA_LBA | ATA_DEVICE(device));
1053 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
1058 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1059 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1060 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
1065 /* can't take over 500 ms */
1066 c = ide_wait(device, IDE_TIME_OUT);
1068 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1070 printf("Error (no IRQ) dev %d blk " LBAF
1071 ": status %#02x\n", device, blknr, c);
1075 ide_output_data(device, buffer, ATA_SECTORWORDS);
1076 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1079 buffer += ATA_BLOCKSIZE;
1085 #if defined(CONFIG_OF_IDE_FIXUP)
1086 int ide_device_present(int dev)
1088 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1090 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1095 static int ide_blk_probe(struct udevice *udev)
1097 struct blk_desc *desc = dev_get_uclass_plat(udev);
1099 /* fill in device vendor/product/rev strings */
1100 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1102 desc->vendor[BLK_VEN_SIZE] = '\0';
1103 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1105 desc->product[BLK_PRD_SIZE] = '\0';
1106 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1108 desc->revision[BLK_REV_SIZE] = '\0';
1113 static const struct blk_ops ide_blk_ops = {
1118 U_BOOT_DRIVER(ide_blk) = {
1121 .ops = &ide_blk_ops,
1122 .probe = ide_blk_probe,
1125 static int ide_probe(struct udevice *udev)
1127 struct udevice *blk_dev;
1134 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1135 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1136 sprintf(name, "blk#%d", i);
1138 blksz = ide_dev_desc[i].blksz;
1139 size = blksz * ide_dev_desc[i].lba;
1142 * With CDROM, if there is no CD inserted, blksz will
1143 * be zero, don't bother to create IDE block device.
1147 ret = blk_create_devicef(udev, "ide_blk", name,
1149 blksz, size, &blk_dev);
1158 U_BOOT_DRIVER(ide) = {
1164 struct pci_device_id ide_supported[] = {
1165 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1169 U_BOOT_PCI_DEVICE(ide, ide_supported);
1171 UCLASS_DRIVER(ide) = {
1176 U_BOOT_LEGACY_BLK(ide) = {
1177 .if_typename = "ide",
1178 .if_type = IF_TYPE_IDE,
1179 .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
1180 .desc = ide_dev_desc,