2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
18 #include <asm/byteorder.h>
21 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
28 #ifdef CONFIG_STATUS_LED
29 # include <status_led.h>
33 # define EIEIO __asm__ volatile ("eieio")
34 # define SYNC __asm__ volatile ("sync")
36 # define EIEIO /* nothing */
37 # define SYNC /* nothing */
40 /* ------------------------------------------------------------------------- */
42 /* Current I/O Device */
43 static int curr_device = -1;
45 /* Current offset for IDE0 / IDE1 bus access */
46 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
47 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
48 CONFIG_SYS_ATA_IDE0_OFFSET,
50 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
51 CONFIG_SYS_ATA_IDE1_OFFSET,
55 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
57 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
58 /* ------------------------------------------------------------------------- */
60 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
62 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
64 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
66 #ifndef CONFIG_SYS_ATA_PORT_ADDR
67 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
70 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
71 # define DEVICE_LED(x) 0
76 #ifdef CONFIG_IDE_RESET
77 extern void ide_set_reset(int idereset);
79 static void ide_reset(void)
84 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
86 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
87 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
89 ide_set_reset(1); /* assert reset */
91 /* the reset signal shall be asserted for et least 25 us */
96 /* de-assert RESET signal */
100 for (i = 0; i < 250; ++i)
104 #define ide_reset() /* dummy */
105 #endif /* CONFIG_IDE_RESET */
108 * Wait until Busy bit is off, or timeout (in ms)
111 static uchar ide_wait(int dev, ulong t)
113 ulong delay = 10 * t; /* poll every 100 us */
116 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
125 * copy src to dest, skipping leading and trailing blanks and null
126 * terminate the string
127 * "len" is the size of available memory including the terminating '\0'
129 static void ident_cpy(unsigned char *dst, unsigned char *src,
132 unsigned char *end, *last;
137 /* reserve space for '\0' */
141 /* skip leading white space */
142 while ((*src) && (src < end) && (*src == ' '))
145 /* copy string, omitting trailing white space */
146 while ((*src) && (src < end)) {
156 /****************************************************************************
160 #if defined(CONFIG_IDE_SWAP_IO)
161 /* since ATAPI may use commands with not 4 bytes alligned length
162 * we have our own transfer functions, 2 bytes alligned */
163 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
166 volatile ushort *pbuf;
168 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
169 dbuf = (ushort *)sect_buf;
171 debug("in output data shorts base for read is %lx\n",
172 (unsigned long) pbuf);
180 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
183 volatile ushort *pbuf;
185 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
186 dbuf = (ushort *)sect_buf;
188 debug("in input data shorts base for read is %lx\n",
189 (unsigned long) pbuf);
197 #else /* ! CONFIG_IDE_SWAP_IO */
198 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
200 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
203 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
205 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
208 #endif /* CONFIG_IDE_SWAP_IO */
211 * Wait until (Status & mask) == res, or timeout (in ms)
213 * This is used since some ATAPI CD ROMs clears their Busy Bit first
214 * and then they set their DRQ Bit
216 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
218 ulong delay = 10 * t; /* poll every 100 us */
221 /* prevents to read the status before valid */
222 c = ide_inb(dev, ATA_DEV_CTL);
224 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
225 /* break if error occurs (doesn't make sense to wait more) */
226 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
236 * issue an atapi command
238 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
239 unsigned char *buffer, int buflen)
241 unsigned char c, err, mask, res;
244 ide_led(DEVICE_LED(device), 1); /* LED on */
248 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
250 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
251 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
252 if ((c & mask) != res) {
253 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
259 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
260 ide_outb(device, ATA_SECT_CNT, 0);
261 ide_outb(device, ATA_SECT_NUM, 0);
262 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
263 ide_outb(device, ATA_CYL_HIGH,
264 (unsigned char) ((buflen >> 8) & 0xFF));
265 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
267 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
270 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
272 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
274 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
275 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
281 /* write command block */
282 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
284 /* ATAPI Command written wait for completition */
285 udelay(5000); /* device must set bsy */
287 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
289 * if no data wait for DRQ = 0 BSY = 0
290 * if data wait for DRQ = 1 BSY = 0
295 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
296 if ((c & mask) != res) {
297 if (c & ATA_STAT_ERR) {
298 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
299 debug("atapi_issue 1 returned sense key %X status %02X\n",
302 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
308 n = ide_inb(device, ATA_CYL_HIGH);
310 n += ide_inb(device, ATA_CYL_LOW);
312 printf("ERROR, transfer bytes %d requested only %d\n", n,
317 if ((n == 0) && (buflen < 0)) {
318 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
323 debug("WARNING, transfer bytes %d not equal with requested %d\n",
326 if (n != 0) { /* data transfer */
327 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
328 /* we transfer shorts */
330 /* ok now decide if it is an in or output */
331 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
332 debug("Write to device\n");
333 ide_output_data_shorts(device, (unsigned short *)buffer,
336 debug("Read from device @ %p shorts %d\n", buffer, n);
337 ide_input_data_shorts(device, (unsigned short *)buffer,
341 udelay(5000); /* seems that some CD ROMs need this... */
342 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
344 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
345 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
346 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
347 debug("atapi_issue 2 returned sense key %X status %X\n", err,
353 ide_led(DEVICE_LED(device), 0); /* LED off */
358 * sending the command to atapi_issue. If an status other than good
359 * returns, an request_sense will be issued
362 #define ATAPI_DRIVE_NOT_READY 100
363 #define ATAPI_UNIT_ATTN 10
365 unsigned char atapi_issue_autoreq(int device,
368 unsigned char *buffer, int buflen)
370 unsigned char sense_data[18], sense_ccb[12];
371 unsigned char res, key, asc, ascq;
372 int notready, unitattn;
374 unitattn = ATAPI_UNIT_ATTN;
375 notready = ATAPI_DRIVE_NOT_READY;
378 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
383 return 0xFF; /* error */
385 debug("(auto_req)atapi_issue returned sense key %X\n", res);
387 memset(sense_ccb, 0, sizeof(sense_ccb));
388 memset(sense_data, 0, sizeof(sense_data));
389 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
390 sense_ccb[4] = 18; /* allocation Length */
392 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
393 key = (sense_data[2] & 0xF);
394 asc = (sense_data[12]);
395 ascq = (sense_data[13]);
397 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
398 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
399 sense_data[0], key, asc, ascq);
402 return 0; /* ok device ready */
404 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
405 if (unitattn-- > 0) {
409 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
412 if ((asc == 0x4) && (ascq == 0x1)) {
413 /* not ready, but will be ready soon */
414 if (notready-- > 0) {
418 printf("Drive not ready, tried %d times\n",
419 ATAPI_DRIVE_NOT_READY);
423 debug("Media not present\n");
427 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
430 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
436 * we transfer only one block per command, since the multiple DRQ per
437 * command is not yet implemented
439 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
440 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
441 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
443 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
446 int device = block_dev->devnum;
448 unsigned char ccb[12]; /* Command descriptor block */
451 debug("atapi_read dev %d start " LBAF " blocks " LBAF
452 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
455 if (blkcnt > ATAPI_READ_MAX_BLOCK)
456 cnt = ATAPI_READ_MAX_BLOCK;
460 ccb[0] = ATAPI_CMD_READ_12;
461 ccb[1] = 0; /* reserved */
462 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
463 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
464 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
465 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
466 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
467 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
468 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
469 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
470 ccb[10] = 0; /* reserved */
471 ccb[11] = 0; /* reserved */
473 if (atapi_issue_autoreq(device, ccb, 12,
474 (unsigned char *)buffer,
475 cnt * ATAPI_READ_BLOCK_SIZE)
482 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
483 } while (blkcnt > 0);
487 static void atapi_inquiry(struct blk_desc *dev_desc)
489 unsigned char ccb[12]; /* Command descriptor block */
490 unsigned char iobuf[64]; /* temp buf */
494 device = dev_desc->devnum;
495 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
496 dev_desc->block_read = atapi_read;
498 memset(ccb, 0, sizeof(ccb));
499 memset(iobuf, 0, sizeof(iobuf));
501 ccb[0] = ATAPI_CMD_INQUIRY;
502 ccb[4] = 40; /* allocation Legnth */
503 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
505 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
509 /* copy device ident strings */
510 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
511 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
512 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
517 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
518 dev_desc->type = iobuf[0] & 0x1f;
520 if ((iobuf[1] & 0x80) == 0x80)
521 dev_desc->removable = 1;
523 dev_desc->removable = 0;
525 memset(ccb, 0, sizeof(ccb));
526 memset(iobuf, 0, sizeof(iobuf));
527 ccb[0] = ATAPI_CMD_START_STOP;
528 ccb[4] = 0x03; /* start */
530 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
532 debug("ATAPI_CMD_START_STOP returned %x\n", c);
536 memset(ccb, 0, sizeof(ccb));
537 memset(iobuf, 0, sizeof(iobuf));
538 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
540 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
544 memset(ccb, 0, sizeof(ccb));
545 memset(iobuf, 0, sizeof(iobuf));
546 ccb[0] = ATAPI_CMD_READ_CAP;
547 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
548 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
552 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
553 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
554 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
556 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
557 ((unsigned long) iobuf[1] << 16) +
558 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
559 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
560 ((unsigned long) iobuf[5] << 16) +
561 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
562 dev_desc->log2blksz = LOG2(dev_desc->blksz);
564 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
570 #endif /* CONFIG_ATAPI */
572 static void ide_ident(struct blk_desc *dev_desc)
582 device = dev_desc->devnum;
583 printf(" Device %d: ", device);
585 ide_led(DEVICE_LED(device), 1); /* LED on */
588 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
589 dev_desc->if_type = IF_TYPE_IDE;
594 /* Warning: This will be tricky to read */
595 while (retries <= 1) {
596 /* check signature */
597 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
598 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
599 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
600 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
601 /* ATAPI Signature found */
602 dev_desc->if_type = IF_TYPE_ATAPI;
604 * Start Ident Command
606 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
608 * Wait for completion - ATAPI devices need more time
611 c = ide_wait(device, ATAPI_TIME_OUT);
616 * Start Ident Command
618 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
621 * Wait for completion
623 c = ide_wait(device, IDE_TIME_OUT);
625 ide_led(DEVICE_LED(device), 0); /* LED off */
627 if (((c & ATA_STAT_DRQ) == 0) ||
628 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
632 * Need to soft reset the device
633 * in case it's an ATAPI...
635 debug("Retrying...\n");
636 ide_outb(device, ATA_DEV_HD,
637 ATA_LBA | ATA_DEVICE(device));
639 ide_outb(device, ATA_COMMAND, 0x08);
640 udelay(500000); /* 500 ms */
645 ide_outb(device, ATA_DEV_HD,
646 ATA_LBA | ATA_DEVICE(device));
655 } /* see above - ugly to read */
657 if (retries == 2) /* Not found */
661 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
663 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
664 sizeof(dev_desc->revision));
665 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
666 sizeof(dev_desc->vendor));
667 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
668 sizeof(dev_desc->product));
669 #ifdef __LITTLE_ENDIAN
671 * firmware revision, model, and serial number have Big Endian Byte
672 * order in Word. Convert all three to little endian.
674 * See CF+ and CompactFlash Specification Revision 2.0:
675 * 6.2.1.6: Identify Drive, Table 39 for more details
678 strswab(dev_desc->revision);
679 strswab(dev_desc->vendor);
680 strswab(dev_desc->product);
681 #endif /* __LITTLE_ENDIAN */
683 if ((iop.config & 0x0080) == 0x0080)
684 dev_desc->removable = 1;
686 dev_desc->removable = 0;
689 if (dev_desc->if_type == IF_TYPE_ATAPI) {
690 atapi_inquiry(dev_desc);
693 #endif /* CONFIG_ATAPI */
697 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
698 #else /* ! __BIG_ENDIAN */
700 * do not swap shorts on little endian
702 * See CF+ and CompactFlash Specification Revision 2.0:
703 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
705 dev_desc->lba = iop.lba_capacity;
706 #endif /* __BIG_ENDIAN */
709 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
711 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
712 ((unsigned long long) iop.lba48_capacity[1] << 16) |
713 ((unsigned long long) iop.lba48_capacity[2] << 32) |
714 ((unsigned long long) iop.lba48_capacity[3] << 48);
718 #endif /* CONFIG_LBA48 */
720 dev_desc->type = DEV_TYPE_HARDDISK;
721 dev_desc->blksz = ATA_BLOCKSIZE;
722 dev_desc->log2blksz = LOG2(dev_desc->blksz);
723 dev_desc->lun = 0; /* just to fill something in... */
725 #if 0 /* only used to test the powersaving mode,
726 * if enabled, the drive goes after 5 sec
728 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
729 c = ide_wait(device, IDE_TIME_OUT);
730 ide_outb(device, ATA_SECT_CNT, 1);
731 ide_outb(device, ATA_LBA_LOW, 0);
732 ide_outb(device, ATA_LBA_MID, 0);
733 ide_outb(device, ATA_LBA_HIGH, 0);
734 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
735 ide_outb(device, ATA_COMMAND, 0xe3);
737 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
741 /* ------------------------------------------------------------------------- */
743 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
750 return CMD_RET_USAGE;
752 if (strncmp(argv[1], "res", 3) == 0) {
754 #ifdef CONFIG_IDE_8xx_DIRECT
755 " on PCMCIA " PCMCIA_SLOT_MSG
761 } else if (strncmp(argv[1], "inf", 3) == 0) {
766 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
767 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
768 continue; /* list only known devices */
769 printf("IDE device %d: ", i);
770 dev_print(&ide_dev_desc[i]);
774 } else if (strncmp(argv[1], "dev", 3) == 0) {
775 if (curr_device < 0 ||
776 curr_device >= CONFIG_SYS_IDE_MAXDEVICE) {
777 puts("\nno IDE devices available\n");
780 printf("\nIDE device %d: ", curr_device);
781 dev_print(&ide_dev_desc[curr_device]);
783 } else if (strncmp(argv[1], "part", 4) == 0) {
786 for (ok = 0, dev = 0;
787 dev < CONFIG_SYS_IDE_MAXDEVICE;
789 if (ide_dev_desc[dev].part_type !=
794 part_print(&ide_dev_desc[dev]);
798 puts("\nno IDE devices available\n");
803 return CMD_RET_USAGE;
805 if (strncmp(argv[1], "dev", 3) == 0) {
806 int dev = (int)simple_strtoul(argv[2], NULL, 10);
808 printf("\nIDE device %d: ", dev);
809 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
810 puts("unknown device\n");
813 dev_print(&ide_dev_desc[dev]);
814 /*ide_print (dev); */
816 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
821 puts("... is now current device\n");
824 } else if (strncmp(argv[1], "part", 4) == 0) {
825 int dev = (int)simple_strtoul(argv[2], NULL, 10);
827 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
828 part_print(&ide_dev_desc[dev]);
830 printf("\nIDE device %d not available\n",
837 return CMD_RET_USAGE;
839 /* at least 4 args */
841 if (strcmp(argv[1], "read") == 0) {
842 ulong addr = simple_strtoul(argv[2], NULL, 16);
843 ulong cnt = simple_strtoul(argv[4], NULL, 16);
844 struct blk_desc *dev_desc;
847 #ifdef CONFIG_SYS_64BIT_LBA
848 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
850 printf("\nIDE read: device %d block # %lld, count %ld...",
851 curr_device, blk, cnt);
853 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
855 printf("\nIDE read: device %d block # %ld, count %ld...",
856 curr_device, blk, cnt);
859 dev_desc = &ide_dev_desc[curr_device];
860 n = blk_dread(dev_desc, blk, cnt, (ulong *)addr);
861 /* flush cache after read */
863 cnt * ide_dev_desc[curr_device].blksz);
865 printf("%ld blocks read: %s\n",
866 n, (n == cnt) ? "OK" : "ERROR");
871 } else if (strcmp(argv[1], "write") == 0) {
872 ulong addr = simple_strtoul(argv[2], NULL, 16);
873 ulong cnt = simple_strtoul(argv[4], NULL, 16);
876 #ifdef CONFIG_SYS_64BIT_LBA
877 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
879 printf("\nIDE write: device %d block # %lld, count %ld...",
880 curr_device, blk, cnt);
882 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
884 printf("\nIDE write: device %d block # %ld, count %ld...",
885 curr_device, blk, cnt);
887 n = ide_write(&ide_dev_desc[curr_device], blk, cnt,
890 printf("%ld blocks written: %s\n", n,
891 n == cnt ? "OK" : "ERROR");
897 return CMD_RET_USAGE;
904 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
906 return common_diskboot(cmdtp, "ide", argc, argv);
909 /* ------------------------------------------------------------------------- */
911 __weak void ide_led(uchar led, uchar status)
913 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
914 static uchar led_buffer; /* Buffer for current LED status */
916 uchar *led_port = LED_PORT;
918 if (status) /* switch LED on */
920 else /* switch LED off */
923 *led_port = led_buffer;
927 /* ------------------------------------------------------------------------- */
929 __weak void ide_outb(int dev, int port, unsigned char val)
931 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
933 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
935 #if defined(CONFIG_IDE_AHB)
938 ide_write_register(dev, port, val);
941 outb(val, (ATA_CURR_BASE(dev)));
944 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
948 __weak unsigned char ide_inb(int dev, int port)
952 #if defined(CONFIG_IDE_AHB)
953 val = ide_read_register(dev, port);
955 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
958 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
960 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
969 #ifdef CONFIG_IDE_8xx_PCCARD
970 extern int ide_devices_found; /* Initialized in check_ide_device() */
971 #endif /* CONFIG_IDE_8xx_PCCARD */
973 #ifdef CONFIG_IDE_PREINIT
977 puts("ide_preinit failed\n");
980 #endif /* CONFIG_IDE_PREINIT */
985 * Reset the IDE just to be sure.
986 * Light LED's to show
988 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
990 /* ATAPI Drives seems to need a proper IDE Reset */
993 #ifdef CONFIG_IDE_INIT_POSTRESET
996 if (ide_init_postreset()) {
997 puts("ide_preinit_postreset failed\n");
1000 #endif /* CONFIG_IDE_INIT_POSTRESET */
1003 * Wait for IDE to get ready.
1004 * According to spec, this can take up to 31 seconds!
1006 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1008 bus * (CONFIG_SYS_IDE_MAXDEVICE /
1009 CONFIG_SYS_IDE_MAXBUS);
1011 #ifdef CONFIG_IDE_8xx_PCCARD
1012 /* Skip non-ide devices from probing */
1013 if ((ide_devices_found & (1 << bus)) == 0) {
1014 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
1018 printf("Bus %d: ", bus);
1020 ide_bus_ok[bus] = 0;
1024 udelay(100000); /* 100 ms */
1025 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
1026 udelay(100000); /* 100 ms */
1029 udelay(10000); /* 10 ms */
1031 c = ide_inb(dev, ATA_STATUS);
1033 if (i > (ATA_RESET_TIME * 100)) {
1034 puts("** Timeout **\n");
1036 ide_led((LED_IDE1 | LED_IDE2), 0);
1039 if ((i >= 100) && ((i % 100) == 0))
1042 } while (c & ATA_STAT_BUSY);
1044 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
1045 puts("not available ");
1046 debug("Status = 0x%02X ", c);
1047 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
1048 } else if ((c & ATA_STAT_READY) == 0) {
1049 puts("not available ");
1050 debug("Status = 0x%02X ", c);
1054 ide_bus_ok[bus] = 1;
1061 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
1064 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
1065 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
1066 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1067 ide_dev_desc[i].if_type = IF_TYPE_IDE;
1068 ide_dev_desc[i].devnum = i;
1069 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
1070 ide_dev_desc[i].blksz = 0;
1071 ide_dev_desc[i].log2blksz =
1072 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
1073 ide_dev_desc[i].lba = 0;
1074 ide_dev_desc[i].block_read = ide_read;
1075 ide_dev_desc[i].block_write = ide_write;
1076 if (!ide_bus_ok[IDE_BUS(i)])
1078 ide_led(led, 1); /* LED on */
1079 ide_ident(&ide_dev_desc[i]);
1080 ide_led(led, 0); /* LED off */
1081 dev_print(&ide_dev_desc[i]);
1083 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
1084 /* initialize partition type */
1085 part_init(&ide_dev_desc[i]);
1086 if (curr_device < 0)
1093 /* ------------------------------------------------------------------------- */
1095 #ifdef CONFIG_PARTITIONS
1096 struct blk_desc *ide_get_dev(int dev)
1098 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
1102 /* ------------------------------------------------------------------------- */
1104 /* We only need to swap data if we are running on a big endian cpu. */
1105 #if defined(__LITTLE_ENDIAN)
1106 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
1108 ide_input_data(dev, sect_buf, words);
1111 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
1113 volatile ushort *pbuf =
1114 (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1115 ushort *dbuf = (ushort *)sect_buf;
1117 debug("in input swap data base for read is %lx\n",
1118 (unsigned long) pbuf);
1122 *dbuf++ = swab16p((u16 *)pbuf);
1123 *dbuf++ = swab16p((u16 *)pbuf);
1125 *dbuf++ = ld_le16(pbuf);
1126 *dbuf++ = ld_le16(pbuf);
1130 #endif /* __LITTLE_ENDIAN */
1133 #if defined(CONFIG_IDE_SWAP_IO)
1134 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
1137 volatile ushort *pbuf;
1139 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1140 dbuf = (ushort *)sect_buf;
1148 #else /* ! CONFIG_IDE_SWAP_IO */
1149 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
1151 #if defined(CONFIG_IDE_AHB)
1152 ide_write_data(dev, sect_buf, words);
1154 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
1157 #endif /* CONFIG_IDE_SWAP_IO */
1159 #if defined(CONFIG_IDE_SWAP_IO)
1160 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
1163 volatile ushort *pbuf;
1165 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1166 dbuf = (ushort *)sect_buf;
1168 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
1177 #else /* ! CONFIG_IDE_SWAP_IO */
1178 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
1180 #if defined(CONFIG_IDE_AHB)
1181 ide_read_data(dev, sect_buf, words);
1183 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
1187 #endif /* CONFIG_IDE_SWAP_IO */
1189 /* ------------------------------------------------------------------------- */
1191 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1194 int device = block_dev->devnum;
1197 unsigned char pwrsave = 0; /* power save */
1200 unsigned char lba48 = 0;
1202 if (blknr & 0x0000fffff0000000ULL) {
1203 /* more than 28 bits used, use 48bit mode */
1207 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
1208 device, blknr, blkcnt, (ulong) buffer);
1210 ide_led(DEVICE_LED(device), 1); /* LED on */
1214 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1215 c = ide_wait(device, IDE_TIME_OUT);
1217 if (c & ATA_STAT_BUSY) {
1218 printf("IDE read: device %d not ready\n", device);
1222 /* first check if the drive is in Powersaving mode, if yes,
1223 * increase the timeout value */
1224 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1227 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
1229 if (c & ATA_STAT_BUSY) {
1230 printf("IDE read: device %d not ready\n", device);
1233 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1234 printf("No Powersaving mode %X\n", c);
1236 c = ide_inb(device, ATA_SECT_CNT);
1237 debug("Powersaving %02X\n", c);
1243 while (blkcnt-- > 0) {
1245 c = ide_wait(device, IDE_TIME_OUT);
1247 if (c & ATA_STAT_BUSY) {
1248 printf("IDE read: device %d not ready\n", device);
1253 /* write high bits */
1254 ide_outb(device, ATA_SECT_CNT, 0);
1255 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1256 #ifdef CONFIG_SYS_64BIT_LBA
1257 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1258 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1260 ide_outb(device, ATA_LBA_MID, 0);
1261 ide_outb(device, ATA_LBA_HIGH, 0);
1265 ide_outb(device, ATA_SECT_CNT, 1);
1266 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1267 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1268 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1272 ide_outb(device, ATA_DEV_HD,
1273 ATA_LBA | ATA_DEVICE(device));
1274 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
1279 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1280 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1281 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
1287 /* may take up to 4 sec */
1288 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1291 /* can't take over 500 ms */
1292 c = ide_wait(device, IDE_TIME_OUT);
1295 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1297 printf("Error (no IRQ) dev %d blk " LBAF
1298 ": status %#02x\n", device, blknr, c);
1302 ide_input_data(device, buffer, ATA_SECTORWORDS);
1303 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
1307 buffer += ATA_BLOCKSIZE;
1310 ide_led(DEVICE_LED(device), 0); /* LED off */
1314 /* ------------------------------------------------------------------------- */
1317 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1320 int device = block_dev->devnum;
1325 unsigned char lba48 = 0;
1327 if (blknr & 0x0000fffff0000000ULL) {
1328 /* more than 28 bits used, use 48bit mode */
1333 ide_led(DEVICE_LED(device), 1); /* LED on */
1337 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1339 while (blkcnt-- > 0) {
1340 c = ide_wait(device, IDE_TIME_OUT);
1342 if (c & ATA_STAT_BUSY) {
1343 printf("IDE read: device %d not ready\n", device);
1348 /* write high bits */
1349 ide_outb(device, ATA_SECT_CNT, 0);
1350 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1351 #ifdef CONFIG_SYS_64BIT_LBA
1352 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1353 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1355 ide_outb(device, ATA_LBA_MID, 0);
1356 ide_outb(device, ATA_LBA_HIGH, 0);
1360 ide_outb(device, ATA_SECT_CNT, 1);
1361 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1362 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1363 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1367 ide_outb(device, ATA_DEV_HD,
1368 ATA_LBA | ATA_DEVICE(device));
1369 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1374 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1375 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1376 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1381 /* can't take over 500 ms */
1382 c = ide_wait(device, IDE_TIME_OUT);
1384 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1386 printf("Error (no IRQ) dev %d blk " LBAF
1387 ": status %#02x\n", device, blknr, c);
1391 ide_output_data(device, buffer, ATA_SECTORWORDS);
1392 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1395 buffer += ATA_BLOCKSIZE;
1398 ide_led(DEVICE_LED(device), 0); /* LED off */
1402 /* ------------------------------------------------------------------------- */
1404 #if defined(CONFIG_OF_IDE_FIXUP)
1405 int ide_device_present(int dev)
1407 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1409 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1412 /* ------------------------------------------------------------------------- */
1414 U_BOOT_CMD(ide, 5, 1, do_ide,
1416 "reset - reset IDE controller\n"
1417 "ide info - show available IDE devices\n"
1418 "ide device [dev] - show or set current device\n"
1419 "ide part [dev] - print partition table of one or all IDE devices\n"
1420 "ide read addr blk# cnt\n"
1421 "ide write addr blk# cnt - read/write `cnt'"
1422 " blocks starting at block `blk#'\n"
1423 " to/from memory address `addr'");
1425 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1426 "boot from IDE device", "loadAddr dev:part");