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
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 #ifdef CONFIG_IDE_RESET
49 extern void ide_set_reset(int idereset);
51 static void ide_reset(void)
55 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
57 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
58 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
60 ide_set_reset(1); /* assert reset */
62 /* the reset signal shall be asserted for et least 25 us */
67 /* de-assert RESET signal */
71 for (i = 0; i < 250; ++i)
75 #define ide_reset() /* dummy */
76 #endif /* CONFIG_IDE_RESET */
79 * Wait until Busy bit is off, or timeout (in ms)
82 static uchar ide_wait(int dev, ulong t)
84 ulong delay = 10 * t; /* poll every 100 us */
87 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
96 * copy src to dest, skipping leading and trailing blanks and null
97 * terminate the string
98 * "len" is the size of available memory including the terminating '\0'
100 static void ident_cpy(unsigned char *dst, unsigned char *src,
103 unsigned char *end, *last;
108 /* reserve space for '\0' */
112 /* skip leading white space */
113 while ((*src) && (src < end) && (*src == ' '))
116 /* copy string, omitting trailing white space */
117 while ((*src) && (src < end)) {
127 /****************************************************************************
131 /* since ATAPI may use commands with not 4 bytes alligned length
132 * we have our own transfer functions, 2 bytes alligned */
133 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
135 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
138 dbuf = (ushort *)sect_buf;
140 debug("in output data shorts base for read is %p\n", (void *)paddr);
144 outw(cpu_to_le16(*dbuf++), paddr);
148 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
150 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
153 dbuf = (ushort *)sect_buf;
155 debug("in input data shorts base for read is %p\n", (void *)paddr);
159 *dbuf++ = le16_to_cpu(inw(paddr));
164 * Wait until (Status & mask) == res, or timeout (in ms)
166 * This is used since some ATAPI CD ROMs clears their Busy Bit first
167 * and then they set their DRQ Bit
169 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
171 ulong delay = 10 * t; /* poll every 100 us */
174 /* prevents to read the status before valid */
175 c = ide_inb(dev, ATA_DEV_CTL);
177 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
178 /* break if error occurs (doesn't make sense to wait more) */
179 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
189 * issue an atapi command
191 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
192 unsigned char *buffer, int buflen)
194 unsigned char c, err, mask, res;
199 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
201 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
202 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
203 if ((c & mask) != res) {
204 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
210 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
211 ide_outb(device, ATA_SECT_CNT, 0);
212 ide_outb(device, ATA_SECT_NUM, 0);
213 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
214 ide_outb(device, ATA_CYL_HIGH,
215 (unsigned char) ((buflen >> 8) & 0xFF));
216 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
218 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
221 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
223 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
225 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
226 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
232 /* write command block */
233 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
235 /* ATAPI Command written wait for completition */
236 udelay(5000); /* device must set bsy */
238 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
240 * if no data wait for DRQ = 0 BSY = 0
241 * if data wait for DRQ = 1 BSY = 0
246 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
247 if ((c & mask) != res) {
248 if (c & ATA_STAT_ERR) {
249 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
250 debug("atapi_issue 1 returned sense key %X status %02X\n",
253 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
259 n = ide_inb(device, ATA_CYL_HIGH);
261 n += ide_inb(device, ATA_CYL_LOW);
263 printf("ERROR, transfer bytes %d requested only %d\n", n,
268 if ((n == 0) && (buflen < 0)) {
269 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
274 debug("WARNING, transfer bytes %d not equal with requested %d\n",
277 if (n != 0) { /* data transfer */
278 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
279 /* we transfer shorts */
281 /* ok now decide if it is an in or output */
282 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
283 debug("Write to device\n");
284 ide_output_data_shorts(device, (unsigned short *)buffer,
287 debug("Read from device @ %p shorts %d\n", buffer, n);
288 ide_input_data_shorts(device, (unsigned short *)buffer,
292 udelay(5000); /* seems that some CD ROMs need this... */
293 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
295 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
296 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
297 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
298 debug("atapi_issue 2 returned sense key %X status %X\n", err,
308 * sending the command to atapi_issue. If an status other than good
309 * returns, an request_sense will be issued
312 #define ATAPI_DRIVE_NOT_READY 100
313 #define ATAPI_UNIT_ATTN 10
315 unsigned char atapi_issue_autoreq(int device,
318 unsigned char *buffer, int buflen)
320 unsigned char sense_data[18], sense_ccb[12];
321 unsigned char res, key, asc, ascq;
322 int notready, unitattn;
324 unitattn = ATAPI_UNIT_ATTN;
325 notready = ATAPI_DRIVE_NOT_READY;
328 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
333 return 0xFF; /* error */
335 debug("(auto_req)atapi_issue returned sense key %X\n", res);
337 memset(sense_ccb, 0, sizeof(sense_ccb));
338 memset(sense_data, 0, sizeof(sense_data));
339 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
340 sense_ccb[4] = 18; /* allocation Length */
342 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
343 key = (sense_data[2] & 0xF);
344 asc = (sense_data[12]);
345 ascq = (sense_data[13]);
347 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
348 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
349 sense_data[0], key, asc, ascq);
352 return 0; /* ok device ready */
354 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
355 if (unitattn-- > 0) {
359 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
362 if ((asc == 0x4) && (ascq == 0x1)) {
363 /* not ready, but will be ready soon */
364 if (notready-- > 0) {
368 printf("Drive not ready, tried %d times\n",
369 ATAPI_DRIVE_NOT_READY);
373 debug("Media not present\n");
377 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
380 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
386 * we transfer only one block per command, since the multiple DRQ per
387 * command is not yet implemented
389 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
390 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
391 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
393 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
396 int device = block_dev->devnum;
398 unsigned char ccb[12]; /* Command descriptor block */
401 debug("atapi_read dev %d start " LBAF " blocks " LBAF
402 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
405 if (blkcnt > ATAPI_READ_MAX_BLOCK)
406 cnt = ATAPI_READ_MAX_BLOCK;
410 ccb[0] = ATAPI_CMD_READ_12;
411 ccb[1] = 0; /* reserved */
412 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
413 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
414 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
415 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
416 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
417 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
418 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
419 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
420 ccb[10] = 0; /* reserved */
421 ccb[11] = 0; /* reserved */
423 if (atapi_issue_autoreq(device, ccb, 12,
424 (unsigned char *)buffer,
425 cnt * ATAPI_READ_BLOCK_SIZE)
432 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
433 } while (blkcnt > 0);
437 static void atapi_inquiry(struct blk_desc *dev_desc)
439 unsigned char ccb[12]; /* Command descriptor block */
440 unsigned char iobuf[64]; /* temp buf */
444 device = dev_desc->devnum;
445 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
447 dev_desc->block_read = atapi_read;
450 memset(ccb, 0, sizeof(ccb));
451 memset(iobuf, 0, sizeof(iobuf));
453 ccb[0] = ATAPI_CMD_INQUIRY;
454 ccb[4] = 40; /* allocation Legnth */
455 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
457 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
461 /* copy device ident strings */
462 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
463 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
464 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
469 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
470 dev_desc->type = iobuf[0] & 0x1f;
472 if ((iobuf[1] & 0x80) == 0x80)
473 dev_desc->removable = 1;
475 dev_desc->removable = 0;
477 memset(ccb, 0, sizeof(ccb));
478 memset(iobuf, 0, sizeof(iobuf));
479 ccb[0] = ATAPI_CMD_START_STOP;
480 ccb[4] = 0x03; /* start */
482 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
484 debug("ATAPI_CMD_START_STOP returned %x\n", c);
488 memset(ccb, 0, sizeof(ccb));
489 memset(iobuf, 0, sizeof(iobuf));
490 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
492 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
496 memset(ccb, 0, sizeof(ccb));
497 memset(iobuf, 0, sizeof(iobuf));
498 ccb[0] = ATAPI_CMD_READ_CAP;
499 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
500 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
504 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
505 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
506 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
508 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
509 ((unsigned long) iobuf[1] << 16) +
510 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
511 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
512 ((unsigned long) iobuf[5] << 16) +
513 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
514 dev_desc->log2blksz = LOG2(dev_desc->blksz);
516 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
522 #endif /* CONFIG_ATAPI */
524 static void ide_ident(struct blk_desc *dev_desc)
534 device = dev_desc->devnum;
535 printf(" Device %d: ", device);
539 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
540 dev_desc->if_type = IF_TYPE_IDE;
545 /* Warning: This will be tricky to read */
546 while (retries <= 1) {
547 /* check signature */
548 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
549 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
550 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
551 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
552 /* ATAPI Signature found */
553 dev_desc->if_type = IF_TYPE_ATAPI;
555 * Start Ident Command
557 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
559 * Wait for completion - ATAPI devices need more time
562 c = ide_wait(device, ATAPI_TIME_OUT);
567 * Start Ident Command
569 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
572 * Wait for completion
574 c = ide_wait(device, IDE_TIME_OUT);
577 if (((c & ATA_STAT_DRQ) == 0) ||
578 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
582 * Need to soft reset the device
583 * in case it's an ATAPI...
585 debug("Retrying...\n");
586 ide_outb(device, ATA_DEV_HD,
587 ATA_LBA | ATA_DEVICE(device));
589 ide_outb(device, ATA_COMMAND, 0x08);
590 udelay(500000); /* 500 ms */
595 ide_outb(device, ATA_DEV_HD,
596 ATA_LBA | ATA_DEVICE(device));
605 } /* see above - ugly to read */
607 if (retries == 2) /* Not found */
611 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
613 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
614 sizeof(dev_desc->revision));
615 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
616 sizeof(dev_desc->vendor));
617 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
618 sizeof(dev_desc->product));
620 if ((iop.config & 0x0080) == 0x0080)
621 dev_desc->removable = 1;
623 dev_desc->removable = 0;
626 if (dev_desc->if_type == IF_TYPE_ATAPI) {
627 atapi_inquiry(dev_desc);
630 #endif /* CONFIG_ATAPI */
632 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
633 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
635 ((unsigned long)iop.lba_capacity[0]) |
636 ((unsigned long)iop.lba_capacity[1] << 16);
639 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
641 for (int i = 0; i < 4; i++)
642 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
644 ((unsigned long long)iop.lba48_capacity[0] |
645 ((unsigned long long)iop.lba48_capacity[1] << 16) |
646 ((unsigned long long)iop.lba48_capacity[2] << 32) |
647 ((unsigned long long)iop.lba48_capacity[3] << 48));
651 #endif /* CONFIG_LBA48 */
653 dev_desc->type = DEV_TYPE_HARDDISK;
654 dev_desc->blksz = ATA_BLOCKSIZE;
655 dev_desc->log2blksz = LOG2(dev_desc->blksz);
656 dev_desc->lun = 0; /* just to fill something in... */
658 #if 0 /* only used to test the powersaving mode,
659 * if enabled, the drive goes after 5 sec
661 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
662 c = ide_wait(device, IDE_TIME_OUT);
663 ide_outb(device, ATA_SECT_CNT, 1);
664 ide_outb(device, ATA_LBA_LOW, 0);
665 ide_outb(device, ATA_LBA_MID, 0);
666 ide_outb(device, ATA_LBA_HIGH, 0);
667 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
668 ide_outb(device, ATA_COMMAND, 0xe3);
670 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
674 __weak void ide_outb(int dev, int port, unsigned char val)
676 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
677 dev, port, val, ATA_CURR_BASE(dev) + port);
679 outb(val, ATA_CURR_BASE(dev) + port);
682 __weak unsigned char ide_inb(int dev, int port)
686 val = inb(ATA_CURR_BASE(dev) + port);
688 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
689 dev, port, ATA_CURR_BASE(dev) + port, val);
700 /* ATAPI Drives seems to need a proper IDE Reset */
704 * Wait for IDE to get ready.
705 * According to spec, this can take up to 31 seconds!
707 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
709 bus * (CONFIG_SYS_IDE_MAXDEVICE /
710 CONFIG_SYS_IDE_MAXBUS);
712 printf("Bus %d: ", bus);
718 udelay(100000); /* 100 ms */
719 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
720 udelay(100000); /* 100 ms */
723 udelay(10000); /* 10 ms */
725 c = ide_inb(dev, ATA_STATUS);
727 if (i > (ATA_RESET_TIME * 100)) {
728 puts("** Timeout **\n");
731 if ((i >= 100) && ((i % 100) == 0))
734 } while (c & ATA_STAT_BUSY);
736 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
737 puts("not available ");
738 debug("Status = 0x%02X ", c);
739 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
740 } else if ((c & ATA_STAT_READY) == 0) {
741 puts("not available ");
742 debug("Status = 0x%02X ", c);
753 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
754 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
755 ide_dev_desc[i].if_type = IF_TYPE_IDE;
756 ide_dev_desc[i].devnum = i;
757 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
758 ide_dev_desc[i].blksz = 0;
759 ide_dev_desc[i].log2blksz =
760 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
761 ide_dev_desc[i].lba = 0;
763 ide_dev_desc[i].block_read = ide_read;
764 ide_dev_desc[i].block_write = ide_write;
766 if (!ide_bus_ok[IDE_BUS(i)])
768 ide_ident(&ide_dev_desc[i]);
769 dev_print(&ide_dev_desc[i]);
772 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
773 /* initialize partition type */
774 part_init(&ide_dev_desc[i]);
783 uclass_first_device(UCLASS_IDE, &dev);
787 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
789 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
790 ushort *dbuf = (ushort *)sect_buf;
792 debug("in input swap data base for read is %p\n", (void *)paddr);
796 *dbuf++ = be16_to_cpu(inw(paddr));
798 *dbuf++ = be16_to_cpu(inw(paddr));
802 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
804 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
807 dbuf = (ushort *)sect_buf;
810 outw(cpu_to_le16(*dbuf++), paddr);
812 outw(cpu_to_le16(*dbuf++), paddr);
816 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
818 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
821 dbuf = (ushort *)sect_buf;
823 debug("in input data base for read is %p\n", (void *)paddr);
827 *dbuf++ = le16_to_cpu(inw(paddr));
829 *dbuf++ = le16_to_cpu(inw(paddr));
834 ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
837 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
842 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
844 int device = block_dev->devnum;
847 unsigned char pwrsave = 0; /* power save */
850 unsigned char lba48 = 0;
852 if (blknr & 0x0000fffff0000000ULL) {
853 /* more than 28 bits used, use 48bit mode */
857 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
858 device, blknr, blkcnt, (ulong) buffer);
862 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
863 c = ide_wait(device, IDE_TIME_OUT);
865 if (c & ATA_STAT_BUSY) {
866 printf("IDE read: device %d not ready\n", device);
870 /* first check if the drive is in Powersaving mode, if yes,
871 * increase the timeout value */
872 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
875 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
877 if (c & ATA_STAT_BUSY) {
878 printf("IDE read: device %d not ready\n", device);
881 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
882 printf("No Powersaving mode %X\n", c);
884 c = ide_inb(device, ATA_SECT_CNT);
885 debug("Powersaving %02X\n", c);
891 while (blkcnt-- > 0) {
892 c = ide_wait(device, IDE_TIME_OUT);
894 if (c & ATA_STAT_BUSY) {
895 printf("IDE read: device %d not ready\n", device);
900 /* write high bits */
901 ide_outb(device, ATA_SECT_CNT, 0);
902 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
903 #ifdef CONFIG_SYS_64BIT_LBA
904 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
905 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
907 ide_outb(device, ATA_LBA_MID, 0);
908 ide_outb(device, ATA_LBA_HIGH, 0);
912 ide_outb(device, ATA_SECT_CNT, 1);
913 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
914 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
915 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
919 ide_outb(device, ATA_DEV_HD,
920 ATA_LBA | ATA_DEVICE(device));
921 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
926 ide_outb(device, ATA_DEV_HD, ATA_LBA |
927 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
928 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
934 /* may take up to 4 sec */
935 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
938 /* can't take over 500 ms */
939 c = ide_wait(device, IDE_TIME_OUT);
942 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
944 printf("Error (no IRQ) dev %d blk " LBAF
945 ": status %#02x\n", device, blknr, c);
949 ide_input_data(device, buffer, ATA_SECTORWORDS);
950 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
954 buffer += ATA_BLOCKSIZE;
961 ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
964 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
969 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
971 int device = block_dev->devnum;
976 unsigned char lba48 = 0;
978 if (blknr & 0x0000fffff0000000ULL) {
979 /* more than 28 bits used, use 48bit mode */
986 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
988 while (blkcnt-- > 0) {
989 c = ide_wait(device, IDE_TIME_OUT);
991 if (c & ATA_STAT_BUSY) {
992 printf("IDE read: device %d not ready\n", device);
997 /* write high bits */
998 ide_outb(device, ATA_SECT_CNT, 0);
999 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1000 #ifdef CONFIG_SYS_64BIT_LBA
1001 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1002 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1004 ide_outb(device, ATA_LBA_MID, 0);
1005 ide_outb(device, ATA_LBA_HIGH, 0);
1009 ide_outb(device, ATA_SECT_CNT, 1);
1010 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1011 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1012 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1016 ide_outb(device, ATA_DEV_HD,
1017 ATA_LBA | ATA_DEVICE(device));
1018 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
1023 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1024 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1025 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
1030 /* can't take over 500 ms */
1031 c = ide_wait(device, IDE_TIME_OUT);
1033 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1035 printf("Error (no IRQ) dev %d blk " LBAF
1036 ": status %#02x\n", device, blknr, c);
1040 ide_output_data(device, buffer, ATA_SECTORWORDS);
1041 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1044 buffer += ATA_BLOCKSIZE;
1050 #if defined(CONFIG_OF_IDE_FIXUP)
1051 int ide_device_present(int dev)
1053 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1055 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1060 static int ide_blk_probe(struct udevice *udev)
1062 struct blk_desc *desc = dev_get_uclass_plat(udev);
1064 /* fill in device vendor/product/rev strings */
1065 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1067 desc->vendor[BLK_VEN_SIZE] = '\0';
1068 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1070 desc->product[BLK_PRD_SIZE] = '\0';
1071 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1073 desc->revision[BLK_REV_SIZE] = '\0';
1078 static const struct blk_ops ide_blk_ops = {
1083 U_BOOT_DRIVER(ide_blk) = {
1086 .ops = &ide_blk_ops,
1087 .probe = ide_blk_probe,
1090 static int ide_probe(struct udevice *udev)
1092 struct udevice *blk_dev;
1099 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1100 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1101 sprintf(name, "blk#%d", i);
1103 blksz = ide_dev_desc[i].blksz;
1104 size = blksz * ide_dev_desc[i].lba;
1107 * With CDROM, if there is no CD inserted, blksz will
1108 * be zero, don't bother to create IDE block device.
1112 ret = blk_create_devicef(udev, "ide_blk", name,
1114 blksz, size, &blk_dev);
1118 ret = blk_probe_or_unbind(blk_dev);
1127 U_BOOT_DRIVER(ide) = {
1133 struct pci_device_id ide_supported[] = {
1134 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1138 U_BOOT_PCI_DEVICE(ide, ide_supported);
1140 UCLASS_DRIVER(ide) = {
1145 U_BOOT_LEGACY_BLK(ide) = {
1146 .if_typename = "ide",
1147 .if_type = IF_TYPE_IDE,
1148 .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
1149 .desc = ide_dev_desc,