2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
15 # define EIEIO __asm__ volatile ("eieio")
16 # define SYNC __asm__ volatile ("sync")
18 # define EIEIO /* nothing */
19 # define SYNC /* nothing */
22 /* Current offset for IDE0 / IDE1 bus access */
23 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
24 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
25 CONFIG_SYS_ATA_IDE0_OFFSET,
27 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
28 CONFIG_SYS_ATA_IDE1_OFFSET,
32 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
34 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
36 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
38 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
40 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
42 #ifndef CONFIG_SYS_ATA_PORT_ADDR
43 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
46 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
47 # define DEVICE_LED(x) 0
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 #if defined(CONFIG_IDE_SWAP_IO)
136 /* since ATAPI may use commands with not 4 bytes alligned length
137 * we have our own transfer functions, 2 bytes alligned */
138 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
141 volatile ushort *pbuf;
143 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
144 dbuf = (ushort *)sect_buf;
146 debug("in output data shorts base for read is %lx\n",
147 (unsigned long) pbuf);
155 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
158 volatile ushort *pbuf;
160 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
161 dbuf = (ushort *)sect_buf;
163 debug("in input data shorts base for read is %lx\n",
164 (unsigned long) pbuf);
172 #else /* ! CONFIG_IDE_SWAP_IO */
173 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
175 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
178 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
180 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
183 #endif /* CONFIG_IDE_SWAP_IO */
186 * Wait until (Status & mask) == res, or timeout (in ms)
188 * This is used since some ATAPI CD ROMs clears their Busy Bit first
189 * and then they set their DRQ Bit
191 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
193 ulong delay = 10 * t; /* poll every 100 us */
196 /* prevents to read the status before valid */
197 c = ide_inb(dev, ATA_DEV_CTL);
199 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
200 /* break if error occurs (doesn't make sense to wait more) */
201 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
211 * issue an atapi command
213 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
214 unsigned char *buffer, int buflen)
216 unsigned char c, err, mask, res;
219 ide_led(DEVICE_LED(device), 1); /* LED on */
223 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
225 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
226 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
227 if ((c & mask) != res) {
228 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
234 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
235 ide_outb(device, ATA_SECT_CNT, 0);
236 ide_outb(device, ATA_SECT_NUM, 0);
237 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
238 ide_outb(device, ATA_CYL_HIGH,
239 (unsigned char) ((buflen >> 8) & 0xFF));
240 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
242 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
245 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
247 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
249 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
250 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
256 /* write command block */
257 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
259 /* ATAPI Command written wait for completition */
260 udelay(5000); /* device must set bsy */
262 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
264 * if no data wait for DRQ = 0 BSY = 0
265 * if data wait for DRQ = 1 BSY = 0
270 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
271 if ((c & mask) != res) {
272 if (c & ATA_STAT_ERR) {
273 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
274 debug("atapi_issue 1 returned sense key %X status %02X\n",
277 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
283 n = ide_inb(device, ATA_CYL_HIGH);
285 n += ide_inb(device, ATA_CYL_LOW);
287 printf("ERROR, transfer bytes %d requested only %d\n", n,
292 if ((n == 0) && (buflen < 0)) {
293 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
298 debug("WARNING, transfer bytes %d not equal with requested %d\n",
301 if (n != 0) { /* data transfer */
302 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
303 /* we transfer shorts */
305 /* ok now decide if it is an in or output */
306 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
307 debug("Write to device\n");
308 ide_output_data_shorts(device, (unsigned short *)buffer,
311 debug("Read from device @ %p shorts %d\n", buffer, n);
312 ide_input_data_shorts(device, (unsigned short *)buffer,
316 udelay(5000); /* seems that some CD ROMs need this... */
317 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
319 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
320 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
321 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
322 debug("atapi_issue 2 returned sense key %X status %X\n", err,
328 ide_led(DEVICE_LED(device), 0); /* LED off */
333 * sending the command to atapi_issue. If an status other than good
334 * returns, an request_sense will be issued
337 #define ATAPI_DRIVE_NOT_READY 100
338 #define ATAPI_UNIT_ATTN 10
340 unsigned char atapi_issue_autoreq(int device,
343 unsigned char *buffer, int buflen)
345 unsigned char sense_data[18], sense_ccb[12];
346 unsigned char res, key, asc, ascq;
347 int notready, unitattn;
349 unitattn = ATAPI_UNIT_ATTN;
350 notready = ATAPI_DRIVE_NOT_READY;
353 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
358 return 0xFF; /* error */
360 debug("(auto_req)atapi_issue returned sense key %X\n", res);
362 memset(sense_ccb, 0, sizeof(sense_ccb));
363 memset(sense_data, 0, sizeof(sense_data));
364 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
365 sense_ccb[4] = 18; /* allocation Length */
367 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
368 key = (sense_data[2] & 0xF);
369 asc = (sense_data[12]);
370 ascq = (sense_data[13]);
372 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
373 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
374 sense_data[0], key, asc, ascq);
377 return 0; /* ok device ready */
379 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
380 if (unitattn-- > 0) {
384 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
387 if ((asc == 0x4) && (ascq == 0x1)) {
388 /* not ready, but will be ready soon */
389 if (notready-- > 0) {
393 printf("Drive not ready, tried %d times\n",
394 ATAPI_DRIVE_NOT_READY);
398 debug("Media not present\n");
402 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
405 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
411 * we transfer only one block per command, since the multiple DRQ per
412 * command is not yet implemented
414 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
415 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
416 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
418 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
421 int device = block_dev->devnum;
423 unsigned char ccb[12]; /* Command descriptor block */
426 debug("atapi_read dev %d start " LBAF " blocks " LBAF
427 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
430 if (blkcnt > ATAPI_READ_MAX_BLOCK)
431 cnt = ATAPI_READ_MAX_BLOCK;
435 ccb[0] = ATAPI_CMD_READ_12;
436 ccb[1] = 0; /* reserved */
437 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
438 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
439 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
440 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
441 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
442 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
443 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
444 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
445 ccb[10] = 0; /* reserved */
446 ccb[11] = 0; /* reserved */
448 if (atapi_issue_autoreq(device, ccb, 12,
449 (unsigned char *)buffer,
450 cnt * ATAPI_READ_BLOCK_SIZE)
457 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
458 } while (blkcnt > 0);
462 static void atapi_inquiry(struct blk_desc *dev_desc)
464 unsigned char ccb[12]; /* Command descriptor block */
465 unsigned char iobuf[64]; /* temp buf */
469 device = dev_desc->devnum;
470 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
471 dev_desc->block_read = atapi_read;
473 memset(ccb, 0, sizeof(ccb));
474 memset(iobuf, 0, sizeof(iobuf));
476 ccb[0] = ATAPI_CMD_INQUIRY;
477 ccb[4] = 40; /* allocation Legnth */
478 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
480 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
484 /* copy device ident strings */
485 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
486 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
487 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
492 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
493 dev_desc->type = iobuf[0] & 0x1f;
495 if ((iobuf[1] & 0x80) == 0x80)
496 dev_desc->removable = 1;
498 dev_desc->removable = 0;
500 memset(ccb, 0, sizeof(ccb));
501 memset(iobuf, 0, sizeof(iobuf));
502 ccb[0] = ATAPI_CMD_START_STOP;
503 ccb[4] = 0x03; /* start */
505 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
507 debug("ATAPI_CMD_START_STOP returned %x\n", c);
511 memset(ccb, 0, sizeof(ccb));
512 memset(iobuf, 0, sizeof(iobuf));
513 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
515 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
519 memset(ccb, 0, sizeof(ccb));
520 memset(iobuf, 0, sizeof(iobuf));
521 ccb[0] = ATAPI_CMD_READ_CAP;
522 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
523 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
527 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
528 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
529 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
531 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
532 ((unsigned long) iobuf[1] << 16) +
533 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
534 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
535 ((unsigned long) iobuf[5] << 16) +
536 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
537 dev_desc->log2blksz = LOG2(dev_desc->blksz);
539 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
545 #endif /* CONFIG_ATAPI */
547 static void ide_ident(struct blk_desc *dev_desc)
557 device = dev_desc->devnum;
558 printf(" Device %d: ", device);
560 ide_led(DEVICE_LED(device), 1); /* LED on */
563 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
564 dev_desc->if_type = IF_TYPE_IDE;
569 /* Warning: This will be tricky to read */
570 while (retries <= 1) {
571 /* check signature */
572 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
573 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
574 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
575 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
576 /* ATAPI Signature found */
577 dev_desc->if_type = IF_TYPE_ATAPI;
579 * Start Ident Command
581 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
583 * Wait for completion - ATAPI devices need more time
586 c = ide_wait(device, ATAPI_TIME_OUT);
591 * Start Ident Command
593 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
596 * Wait for completion
598 c = ide_wait(device, IDE_TIME_OUT);
600 ide_led(DEVICE_LED(device), 0); /* LED off */
602 if (((c & ATA_STAT_DRQ) == 0) ||
603 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
607 * Need to soft reset the device
608 * in case it's an ATAPI...
610 debug("Retrying...\n");
611 ide_outb(device, ATA_DEV_HD,
612 ATA_LBA | ATA_DEVICE(device));
614 ide_outb(device, ATA_COMMAND, 0x08);
615 udelay(500000); /* 500 ms */
620 ide_outb(device, ATA_DEV_HD,
621 ATA_LBA | ATA_DEVICE(device));
630 } /* see above - ugly to read */
632 if (retries == 2) /* Not found */
636 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
638 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
639 sizeof(dev_desc->revision));
640 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
641 sizeof(dev_desc->vendor));
642 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
643 sizeof(dev_desc->product));
644 #ifdef __LITTLE_ENDIAN
646 * firmware revision, model, and serial number have Big Endian Byte
647 * order in Word. Convert all three to little endian.
649 * See CF+ and CompactFlash Specification Revision 2.0:
650 * 6.2.1.6: Identify Drive, Table 39 for more details
653 strswab(dev_desc->revision);
654 strswab(dev_desc->vendor);
655 strswab(dev_desc->product);
656 #endif /* __LITTLE_ENDIAN */
658 if ((iop.config & 0x0080) == 0x0080)
659 dev_desc->removable = 1;
661 dev_desc->removable = 0;
664 if (dev_desc->if_type == IF_TYPE_ATAPI) {
665 atapi_inquiry(dev_desc);
668 #endif /* CONFIG_ATAPI */
672 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
673 #else /* ! __BIG_ENDIAN */
675 * do not swap shorts on little endian
677 * See CF+ and CompactFlash Specification Revision 2.0:
678 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
680 dev_desc->lba = iop.lba_capacity;
681 #endif /* __BIG_ENDIAN */
684 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
686 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
687 ((unsigned long long) iop.lba48_capacity[1] << 16) |
688 ((unsigned long long) iop.lba48_capacity[2] << 32) |
689 ((unsigned long long) iop.lba48_capacity[3] << 48);
693 #endif /* CONFIG_LBA48 */
695 dev_desc->type = DEV_TYPE_HARDDISK;
696 dev_desc->blksz = ATA_BLOCKSIZE;
697 dev_desc->log2blksz = LOG2(dev_desc->blksz);
698 dev_desc->lun = 0; /* just to fill something in... */
700 #if 0 /* only used to test the powersaving mode,
701 * if enabled, the drive goes after 5 sec
703 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
704 c = ide_wait(device, IDE_TIME_OUT);
705 ide_outb(device, ATA_SECT_CNT, 1);
706 ide_outb(device, ATA_LBA_LOW, 0);
707 ide_outb(device, ATA_LBA_MID, 0);
708 ide_outb(device, ATA_LBA_HIGH, 0);
709 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
710 ide_outb(device, ATA_COMMAND, 0xe3);
712 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
716 __weak void ide_led(uchar led, uchar status)
718 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
719 static uchar led_buffer; /* Buffer for current LED status */
721 uchar *led_port = LED_PORT;
723 if (status) /* switch LED on */
725 else /* switch LED off */
728 *led_port = led_buffer;
732 __weak void ide_outb(int dev, int port, unsigned char val)
734 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
736 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
738 #if defined(CONFIG_IDE_AHB)
741 ide_write_register(dev, port, val);
744 outb(val, (ATA_CURR_BASE(dev)));
747 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
751 __weak unsigned char ide_inb(int dev, int port)
755 #if defined(CONFIG_IDE_AHB)
756 val = ide_read_register(dev, port);
758 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
761 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
763 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
772 #ifdef CONFIG_IDE_8xx_PCCARD
773 extern int ide_devices_found; /* Initialized in check_ide_device() */
774 #endif /* CONFIG_IDE_8xx_PCCARD */
776 #ifdef CONFIG_IDE_PREINIT
780 puts("ide_preinit failed\n");
783 #endif /* CONFIG_IDE_PREINIT */
788 * Reset the IDE just to be sure.
789 * Light LED's to show
791 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
793 /* ATAPI Drives seems to need a proper IDE Reset */
796 #ifdef CONFIG_IDE_INIT_POSTRESET
799 if (ide_init_postreset()) {
800 puts("ide_preinit_postreset failed\n");
803 #endif /* CONFIG_IDE_INIT_POSTRESET */
806 * Wait for IDE to get ready.
807 * According to spec, this can take up to 31 seconds!
809 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
811 bus * (CONFIG_SYS_IDE_MAXDEVICE /
812 CONFIG_SYS_IDE_MAXBUS);
814 #ifdef CONFIG_IDE_8xx_PCCARD
815 /* Skip non-ide devices from probing */
816 if ((ide_devices_found & (1 << bus)) == 0) {
817 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
821 printf("Bus %d: ", bus);
827 udelay(100000); /* 100 ms */
828 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
829 udelay(100000); /* 100 ms */
832 udelay(10000); /* 10 ms */
834 c = ide_inb(dev, ATA_STATUS);
836 if (i > (ATA_RESET_TIME * 100)) {
837 puts("** Timeout **\n");
839 ide_led((LED_IDE1 | LED_IDE2), 0);
842 if ((i >= 100) && ((i % 100) == 0))
845 } while (c & ATA_STAT_BUSY);
847 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
848 puts("not available ");
849 debug("Status = 0x%02X ", c);
850 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
851 } else if ((c & ATA_STAT_READY) == 0) {
852 puts("not available ");
853 debug("Status = 0x%02X ", c);
864 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
866 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
867 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
868 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
869 ide_dev_desc[i].if_type = IF_TYPE_IDE;
870 ide_dev_desc[i].devnum = i;
871 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
872 ide_dev_desc[i].blksz = 0;
873 ide_dev_desc[i].log2blksz =
874 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
875 ide_dev_desc[i].lba = 0;
876 ide_dev_desc[i].block_read = ide_read;
877 ide_dev_desc[i].block_write = ide_write;
878 if (!ide_bus_ok[IDE_BUS(i)])
880 ide_led(led, 1); /* LED on */
881 ide_ident(&ide_dev_desc[i]);
882 ide_led(led, 0); /* LED off */
883 dev_print(&ide_dev_desc[i]);
885 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
886 /* initialize partition type */
887 part_init(&ide_dev_desc[i]);
893 #ifdef CONFIG_PARTITIONS
894 struct blk_desc *ide_get_dev(int dev)
896 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
900 /* We only need to swap data if we are running on a big endian cpu. */
901 #if defined(__LITTLE_ENDIAN)
902 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
904 ide_input_data(dev, sect_buf, words);
907 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
909 volatile ushort *pbuf =
910 (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
911 ushort *dbuf = (ushort *)sect_buf;
913 debug("in input swap data base for read is %lx\n",
914 (unsigned long) pbuf);
918 *dbuf++ = swab16p((u16 *)pbuf);
919 *dbuf++ = swab16p((u16 *)pbuf);
921 *dbuf++ = ld_le16(pbuf);
922 *dbuf++ = ld_le16(pbuf);
926 #endif /* __LITTLE_ENDIAN */
929 #if defined(CONFIG_IDE_SWAP_IO)
930 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
933 volatile ushort *pbuf;
935 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
936 dbuf = (ushort *)sect_buf;
944 #else /* ! CONFIG_IDE_SWAP_IO */
945 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
947 #if defined(CONFIG_IDE_AHB)
948 ide_write_data(dev, sect_buf, words);
950 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
953 #endif /* CONFIG_IDE_SWAP_IO */
955 #if defined(CONFIG_IDE_SWAP_IO)
956 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
959 volatile ushort *pbuf;
961 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
962 dbuf = (ushort *)sect_buf;
964 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
973 #else /* ! CONFIG_IDE_SWAP_IO */
974 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
976 #if defined(CONFIG_IDE_AHB)
977 ide_read_data(dev, sect_buf, words);
979 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
983 #endif /* CONFIG_IDE_SWAP_IO */
985 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
988 int device = block_dev->devnum;
991 unsigned char pwrsave = 0; /* power save */
994 unsigned char lba48 = 0;
996 if (blknr & 0x0000fffff0000000ULL) {
997 /* more than 28 bits used, use 48bit mode */
1001 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
1002 device, blknr, blkcnt, (ulong) buffer);
1004 ide_led(DEVICE_LED(device), 1); /* LED on */
1008 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1009 c = ide_wait(device, IDE_TIME_OUT);
1011 if (c & ATA_STAT_BUSY) {
1012 printf("IDE read: device %d not ready\n", device);
1016 /* first check if the drive is in Powersaving mode, if yes,
1017 * increase the timeout value */
1018 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1021 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
1023 if (c & ATA_STAT_BUSY) {
1024 printf("IDE read: device %d not ready\n", device);
1027 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1028 printf("No Powersaving mode %X\n", c);
1030 c = ide_inb(device, ATA_SECT_CNT);
1031 debug("Powersaving %02X\n", c);
1037 while (blkcnt-- > 0) {
1038 c = ide_wait(device, IDE_TIME_OUT);
1040 if (c & ATA_STAT_BUSY) {
1041 printf("IDE read: device %d not ready\n", device);
1046 /* write high bits */
1047 ide_outb(device, ATA_SECT_CNT, 0);
1048 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1049 #ifdef CONFIG_SYS_64BIT_LBA
1050 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1051 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1053 ide_outb(device, ATA_LBA_MID, 0);
1054 ide_outb(device, ATA_LBA_HIGH, 0);
1058 ide_outb(device, ATA_SECT_CNT, 1);
1059 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1060 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1061 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1065 ide_outb(device, ATA_DEV_HD,
1066 ATA_LBA | ATA_DEVICE(device));
1067 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
1072 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1073 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1074 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
1080 /* may take up to 4 sec */
1081 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1084 /* can't take over 500 ms */
1085 c = ide_wait(device, IDE_TIME_OUT);
1088 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1090 printf("Error (no IRQ) dev %d blk " LBAF
1091 ": status %#02x\n", device, blknr, c);
1095 ide_input_data(device, buffer, ATA_SECTORWORDS);
1096 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
1100 buffer += ATA_BLOCKSIZE;
1103 ide_led(DEVICE_LED(device), 0); /* LED off */
1107 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1110 int device = block_dev->devnum;
1115 unsigned char lba48 = 0;
1117 if (blknr & 0x0000fffff0000000ULL) {
1118 /* more than 28 bits used, use 48bit mode */
1123 ide_led(DEVICE_LED(device), 1); /* LED on */
1127 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1129 while (blkcnt-- > 0) {
1130 c = ide_wait(device, IDE_TIME_OUT);
1132 if (c & ATA_STAT_BUSY) {
1133 printf("IDE read: device %d not ready\n", device);
1138 /* write high bits */
1139 ide_outb(device, ATA_SECT_CNT, 0);
1140 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1141 #ifdef CONFIG_SYS_64BIT_LBA
1142 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1143 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1145 ide_outb(device, ATA_LBA_MID, 0);
1146 ide_outb(device, ATA_LBA_HIGH, 0);
1150 ide_outb(device, ATA_SECT_CNT, 1);
1151 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1152 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1153 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1157 ide_outb(device, ATA_DEV_HD,
1158 ATA_LBA | ATA_DEVICE(device));
1159 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1164 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1165 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1166 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1171 /* can't take over 500 ms */
1172 c = ide_wait(device, IDE_TIME_OUT);
1174 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1176 printf("Error (no IRQ) dev %d blk " LBAF
1177 ": status %#02x\n", device, blknr, c);
1181 ide_output_data(device, buffer, ATA_SECTORWORDS);
1182 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1185 buffer += ATA_BLOCKSIZE;
1188 ide_led(DEVICE_LED(device), 0); /* LED off */
1192 #if defined(CONFIG_OF_IDE_FIXUP)
1193 int ide_device_present(int dev)
1195 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1197 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1201 U_BOOT_LEGACY_BLK(ide) = {
1202 .if_typename = "ide",
1203 .if_type = IF_TYPE_IDE,
1204 .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
1205 .desc = ide_dev_desc,