2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #include <asm/byteorder.h>
37 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
44 #ifdef CONFIG_STATUS_LED
45 # include <status_led.h>
49 # define EIEIO __asm__ volatile ("eieio")
50 # define SYNC __asm__ volatile ("sync")
52 # define EIEIO /* nothing */
53 # define SYNC /* nothing */
56 /* ------------------------------------------------------------------------- */
58 /* Current I/O Device */
59 static int curr_device = -1;
61 /* Current offset for IDE0 / IDE1 bus access */
62 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
63 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
64 CONFIG_SYS_ATA_IDE0_OFFSET,
66 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
67 CONFIG_SYS_ATA_IDE1_OFFSET,
71 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
73 block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
74 /* ------------------------------------------------------------------------- */
76 #ifdef CONFIG_IDE_RESET
77 static void ide_reset (void);
79 #define ide_reset() /* dummy */
82 static void ide_ident (block_dev_desc_t *dev_desc);
83 static uchar ide_wait (int dev, ulong t);
85 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
87 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
89 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
91 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
93 #ifndef CONFIG_SYS_ATA_PORT_ADDR
94 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
98 static void atapi_inquiry(block_dev_desc_t *dev_desc);
99 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
103 /* ------------------------------------------------------------------------- */
105 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
112 return CMD_RET_USAGE;
114 if (strncmp(argv[1], "res", 3) == 0) {
116 #ifdef CONFIG_IDE_8xx_DIRECT
117 " on PCMCIA " PCMCIA_SLOT_MSG
123 } else if (strncmp(argv[1], "inf", 3) == 0) {
128 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
129 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
130 continue; /* list only known devices */
131 printf("IDE device %d: ", i);
132 dev_print(&ide_dev_desc[i]);
136 } else if (strncmp(argv[1], "dev", 3) == 0) {
137 if ((curr_device < 0)
138 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
139 puts("\nno IDE devices available\n");
142 printf("\nIDE device %d: ", curr_device);
143 dev_print(&ide_dev_desc[curr_device]);
145 } else if (strncmp(argv[1], "part", 4) == 0) {
148 for (ok = 0, dev = 0;
149 dev < CONFIG_SYS_IDE_MAXDEVICE;
151 if (ide_dev_desc[dev].part_type !=
156 print_part(&ide_dev_desc[dev]);
160 puts("\nno IDE devices available\n");
165 return CMD_RET_USAGE;
167 if (strncmp(argv[1], "dev", 3) == 0) {
168 int dev = (int) simple_strtoul(argv[2], NULL, 10);
170 printf("\nIDE device %d: ", dev);
171 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
172 puts("unknown device\n");
175 dev_print(&ide_dev_desc[dev]);
176 /*ide_print (dev); */
178 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
183 puts("... is now current device\n");
186 } else if (strncmp(argv[1], "part", 4) == 0) {
187 int dev = (int) simple_strtoul(argv[2], NULL, 10);
189 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
190 print_part(&ide_dev_desc[dev]);
192 printf("\nIDE device %d not available\n",
199 return CMD_RET_USAGE;
201 /* at least 4 args */
203 if (strcmp(argv[1], "read") == 0) {
204 ulong addr = simple_strtoul(argv[2], NULL, 16);
205 ulong cnt = simple_strtoul(argv[4], NULL, 16);
208 #ifdef CONFIG_SYS_64BIT_LBA
209 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
211 printf("\nIDE read: device %d block # %lld, count %ld ... ",
212 curr_device, blk, cnt);
214 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
216 printf("\nIDE read: device %d block # %ld, count %ld ... ",
217 curr_device, blk, cnt);
220 n = ide_dev_desc[curr_device].block_read(curr_device,
223 /* flush cache after read */
225 cnt * ide_dev_desc[curr_device].blksz);
227 printf("%ld blocks read: %s\n",
228 n, (n == cnt) ? "OK" : "ERROR");
233 } else if (strcmp(argv[1], "write") == 0) {
234 ulong addr = simple_strtoul(argv[2], NULL, 16);
235 ulong cnt = simple_strtoul(argv[4], NULL, 16);
238 #ifdef CONFIG_SYS_64BIT_LBA
239 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
241 printf("\nIDE write: device %d block # %lld, count %ld ... ",
242 curr_device, blk, cnt);
244 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
246 printf("\nIDE write: device %d block # %ld, count %ld ... ",
247 curr_device, blk, cnt);
249 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
251 printf("%ld blocks written: %s\n",
252 n, (n == cnt) ? "OK" : "ERROR");
258 return CMD_RET_USAGE;
265 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
267 return common_diskboot(cmdtp, "ide", argc, argv);
270 /* ------------------------------------------------------------------------- */
272 void __ide_led(uchar led, uchar status)
274 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
275 static uchar led_buffer; /* Buffer for current LED status */
277 uchar *led_port = LED_PORT;
279 if (status) /* switch LED on */
281 else /* switch LED off */
284 *led_port = led_buffer;
288 void ide_led(uchar led, uchar status)
289 __attribute__ ((weak, alias("__ide_led")));
291 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
292 # define DEVICE_LED(x) 0
297 /* ------------------------------------------------------------------------- */
299 inline void __ide_outb(int dev, int port, unsigned char val)
301 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
303 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
305 #if defined(CONFIG_IDE_AHB)
308 ide_write_register(dev, port, val);
311 outb(val, (ATA_CURR_BASE(dev)));
314 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
318 void ide_outb(int dev, int port, unsigned char val)
319 __attribute__ ((weak, alias("__ide_outb")));
321 inline unsigned char __ide_inb(int dev, int port)
325 #if defined(CONFIG_IDE_AHB)
326 val = ide_read_register(dev, port);
328 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
331 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
333 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
337 unsigned char ide_inb(int dev, int port)
338 __attribute__ ((weak, alias("__ide_inb")));
340 #ifdef CONFIG_TUNE_PIO
341 inline int __ide_set_piomode(int pio_mode)
346 inline int ide_set_piomode(int pio_mode)
347 __attribute__ ((weak, alias("__ide_set_piomode")));
355 #ifdef CONFIG_IDE_8xx_PCCARD
356 extern int ide_devices_found; /* Initialized in check_ide_device() */
357 #endif /* CONFIG_IDE_8xx_PCCARD */
359 #ifdef CONFIG_IDE_PREINIT
363 puts("ide_preinit failed\n");
366 #endif /* CONFIG_IDE_PREINIT */
371 * Reset the IDE just to be sure.
372 * Light LED's to show
374 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
376 /* ATAPI Drives seems to need a proper IDE Reset */
379 #ifdef CONFIG_IDE_INIT_POSTRESET
382 if (ide_init_postreset()) {
383 puts("ide_preinit_postreset failed\n");
386 #endif /* CONFIG_IDE_INIT_POSTRESET */
389 * Wait for IDE to get ready.
390 * According to spec, this can take up to 31 seconds!
392 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
394 bus * (CONFIG_SYS_IDE_MAXDEVICE /
395 CONFIG_SYS_IDE_MAXBUS);
397 #ifdef CONFIG_IDE_8xx_PCCARD
398 /* Skip non-ide devices from probing */
399 if ((ide_devices_found & (1 << bus)) == 0) {
400 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
404 printf("Bus %d: ", bus);
410 udelay(100000); /* 100 ms */
411 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
412 udelay(100000); /* 100 ms */
415 udelay(10000); /* 10 ms */
417 c = ide_inb(dev, ATA_STATUS);
419 if (i > (ATA_RESET_TIME * 100)) {
420 puts("** Timeout **\n");
422 ide_led((LED_IDE1 | LED_IDE2), 0);
425 if ((i >= 100) && ((i % 100) == 0))
428 } while (c & ATA_STAT_BUSY);
430 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
431 puts("not available ");
432 debug("Status = 0x%02X ", c);
433 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
434 } else if ((c & ATA_STAT_READY) == 0) {
435 puts("not available ");
436 debug("Status = 0x%02X ", c);
447 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
450 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
451 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
452 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
453 ide_dev_desc[i].if_type = IF_TYPE_IDE;
454 ide_dev_desc[i].dev = i;
455 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
456 ide_dev_desc[i].blksz = 0;
457 ide_dev_desc[i].lba = 0;
458 ide_dev_desc[i].block_read = ide_read;
459 ide_dev_desc[i].block_write = ide_write;
460 if (!ide_bus_ok[IDE_BUS(i)])
462 ide_led(led, 1); /* LED on */
463 ide_ident(&ide_dev_desc[i]);
464 ide_led(led, 0); /* LED off */
465 dev_print(&ide_dev_desc[i]);
467 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
468 /* initialize partition type */
469 init_part(&ide_dev_desc[i]);
477 /* ------------------------------------------------------------------------- */
479 #ifdef CONFIG_PARTITIONS
480 block_dev_desc_t *ide_get_dev(int dev)
482 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
486 /* ------------------------------------------------------------------------- */
488 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
489 __attribute__ ((weak, alias("__ide_input_swap_data")));
491 void ide_input_data(int dev, ulong *sect_buf, int words)
492 __attribute__ ((weak, alias("__ide_input_data")));
494 void ide_output_data(int dev, const ulong *sect_buf, int words)
495 __attribute__ ((weak, alias("__ide_output_data")));
497 /* We only need to swap data if we are running on a big endian cpu. */
498 #if defined(__LITTLE_ENDIAN)
499 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
501 ide_input_data(dev, sect_buf, words);
504 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
506 volatile ushort *pbuf =
507 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
508 ushort *dbuf = (ushort *) sect_buf;
510 debug("in input swap data base for read is %lx\n",
511 (unsigned long) pbuf);
515 *dbuf++ = swab16p((u16 *) pbuf);
516 *dbuf++ = swab16p((u16 *) pbuf);
518 *dbuf++ = ld_le16(pbuf);
519 *dbuf++ = ld_le16(pbuf);
523 #endif /* __LITTLE_ENDIAN */
526 #if defined(CONFIG_IDE_SWAP_IO)
527 void __ide_output_data(int dev, const ulong *sect_buf, int words)
530 volatile ushort *pbuf;
532 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
533 dbuf = (ushort *) sect_buf;
541 #else /* ! CONFIG_IDE_SWAP_IO */
542 void __ide_output_data(int dev, const ulong *sect_buf, int words)
544 #if defined(CONFIG_IDE_AHB)
545 ide_write_data(dev, sect_buf, words);
547 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
550 #endif /* CONFIG_IDE_SWAP_IO */
552 #if defined(CONFIG_IDE_SWAP_IO)
553 void __ide_input_data(int dev, ulong *sect_buf, int words)
556 volatile ushort *pbuf;
558 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
559 dbuf = (ushort *) sect_buf;
561 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
570 #else /* ! CONFIG_IDE_SWAP_IO */
571 void __ide_input_data(int dev, ulong *sect_buf, int words)
573 #if defined(CONFIG_IDE_AHB)
574 ide_read_data(dev, sect_buf, words);
576 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
580 #endif /* CONFIG_IDE_SWAP_IO */
582 /* -------------------------------------------------------------------------
584 static void ide_ident(block_dev_desc_t *dev_desc)
593 #ifdef CONFIG_TUNE_PIO
598 int mode, cycle_time;
602 device = dev_desc->dev;
603 printf(" Device %d: ", device);
605 ide_led(DEVICE_LED(device), 1); /* LED on */
608 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
609 dev_desc->if_type = IF_TYPE_IDE;
614 /* Warning: This will be tricky to read */
615 while (retries <= 1) {
616 /* check signature */
617 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
618 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
619 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
620 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
621 /* ATAPI Signature found */
622 dev_desc->if_type = IF_TYPE_ATAPI;
624 * Start Ident Command
626 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
628 * Wait for completion - ATAPI devices need more time
631 c = ide_wait(device, ATAPI_TIME_OUT);
636 * Start Ident Command
638 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
641 * Wait for completion
643 c = ide_wait(device, IDE_TIME_OUT);
645 ide_led(DEVICE_LED(device), 0); /* LED off */
647 if (((c & ATA_STAT_DRQ) == 0) ||
648 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
652 * Need to soft reset the device
653 * in case it's an ATAPI...
655 debug("Retrying...\n");
656 ide_outb(device, ATA_DEV_HD,
657 ATA_LBA | ATA_DEVICE(device));
659 ide_outb(device, ATA_COMMAND, 0x08);
660 udelay(500000); /* 500 ms */
665 ide_outb(device, ATA_DEV_HD,
666 ATA_LBA | ATA_DEVICE(device));
675 } /* see above - ugly to read */
677 if (retries == 2) /* Not found */
681 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
683 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
684 sizeof(dev_desc->revision));
685 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
686 sizeof(dev_desc->vendor));
687 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
688 sizeof(dev_desc->product));
689 #ifdef __LITTLE_ENDIAN
691 * firmware revision, model, and serial number have Big Endian Byte
692 * order in Word. Convert all three to little endian.
694 * See CF+ and CompactFlash Specification Revision 2.0:
695 * 6.2.1.6: Identify Drive, Table 39 for more details
698 strswab(dev_desc->revision);
699 strswab(dev_desc->vendor);
700 strswab(dev_desc->product);
701 #endif /* __LITTLE_ENDIAN */
703 if ((iop.config & 0x0080) == 0x0080)
704 dev_desc->removable = 1;
706 dev_desc->removable = 0;
708 #ifdef CONFIG_TUNE_PIO
709 /* Mode 0 - 2 only, are directly determined by word 51. */
712 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
713 /* Force it to dead slow, and hope for the best... */
717 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
718 * shall set bit 1 of word 53 to one and support the fields contained
719 * in words 64 through 70.
721 if (iop.field_valid & 0x02) {
723 * Mode 3 and above are possible. Check in order from slow
724 * to fast, so we wind up with the highest mode allowed.
726 if (iop.eide_pio_modes & 0x01)
728 if (iop.eide_pio_modes & 0x02)
730 if (ata_id_is_cfa((u16 *)&iop)) {
731 if ((iop.cf_advanced_caps & 0x07) == 0x01)
733 if ((iop.cf_advanced_caps & 0x07) == 0x02)
738 /* System-specific, depends on bus speeds, etc. */
739 ide_set_piomode(pio_mode);
740 #endif /* CONFIG_TUNE_PIO */
744 * Drive PIO mode autoselection
748 printf("tPIO = 0x%02x = %d\n", mode, mode);
749 if (mode > 2) { /* 2 is maximum allowed tPIO value */
751 debug("Override tPIO -> 2\n");
753 if (iop.field_valid & 2) { /* drive implements ATA2? */
754 debug("Drive implements ATA2\n");
755 if (iop.capability & 8) { /* drive supports use_iordy? */
756 cycle_time = iop.eide_pio_iordy;
758 cycle_time = iop.eide_pio;
760 debug("cycle time = %d\n", cycle_time);
762 if (cycle_time > 120)
763 mode = 3; /* 120 ns for PIO mode 4 */
764 if (cycle_time > 180)
765 mode = 2; /* 180 ns for PIO mode 3 */
766 if (cycle_time > 240)
767 mode = 1; /* 240 ns for PIO mode 4 */
768 if (cycle_time > 383)
769 mode = 0; /* 383 ns for PIO mode 4 */
771 printf("PIO mode to use: PIO %d\n", mode);
775 if (dev_desc->if_type == IF_TYPE_ATAPI) {
776 atapi_inquiry(dev_desc);
779 #endif /* CONFIG_ATAPI */
783 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
784 #else /* ! __BIG_ENDIAN */
786 * do not swap shorts on little endian
788 * See CF+ and CompactFlash Specification Revision 2.0:
789 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
791 dev_desc->lba = iop.lba_capacity;
792 #endif /* __BIG_ENDIAN */
795 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
797 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
798 ((unsigned long long) iop.lba48_capacity[1] << 16) |
799 ((unsigned long long) iop.lba48_capacity[2] << 32) |
800 ((unsigned long long) iop.lba48_capacity[3] << 48);
804 #endif /* CONFIG_LBA48 */
806 dev_desc->type = DEV_TYPE_HARDDISK;
807 dev_desc->blksz = ATA_BLOCKSIZE;
808 dev_desc->lun = 0; /* just to fill something in... */
810 #if 0 /* only used to test the powersaving mode,
811 * if enabled, the drive goes after 5 sec
813 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
814 c = ide_wait(device, IDE_TIME_OUT);
815 ide_outb(device, ATA_SECT_CNT, 1);
816 ide_outb(device, ATA_LBA_LOW, 0);
817 ide_outb(device, ATA_LBA_MID, 0);
818 ide_outb(device, ATA_LBA_HIGH, 0);
819 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
820 ide_outb(device, ATA_COMMAND, 0xe3);
822 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
827 /* ------------------------------------------------------------------------- */
829 ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
833 unsigned char pwrsave = 0; /* power save */
836 unsigned char lba48 = 0;
838 if (blknr & 0x0000fffff0000000ULL) {
839 /* more than 28 bits used, use 48bit mode */
843 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
844 device, blknr, blkcnt, (ulong) buffer);
846 ide_led(DEVICE_LED(device), 1); /* LED on */
850 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
851 c = ide_wait(device, IDE_TIME_OUT);
853 if (c & ATA_STAT_BUSY) {
854 printf("IDE read: device %d not ready\n", device);
858 /* first check if the drive is in Powersaving mode, if yes,
859 * increase the timeout value */
860 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
863 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
865 if (c & ATA_STAT_BUSY) {
866 printf("IDE read: device %d not ready\n", device);
869 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
870 printf("No Powersaving mode %X\n", c);
872 c = ide_inb(device, ATA_SECT_CNT);
873 debug("Powersaving %02X\n", c);
879 while (blkcnt-- > 0) {
881 c = ide_wait(device, IDE_TIME_OUT);
883 if (c & ATA_STAT_BUSY) {
884 printf("IDE read: device %d not ready\n", device);
889 /* write high bits */
890 ide_outb(device, ATA_SECT_CNT, 0);
891 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
892 #ifdef CONFIG_SYS_64BIT_LBA
893 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
894 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
896 ide_outb(device, ATA_LBA_MID, 0);
897 ide_outb(device, ATA_LBA_HIGH, 0);
901 ide_outb(device, ATA_SECT_CNT, 1);
902 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
903 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
904 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
908 ide_outb(device, ATA_DEV_HD,
909 ATA_LBA | ATA_DEVICE(device));
910 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
915 ide_outb(device, ATA_DEV_HD, ATA_LBA |
916 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
917 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
923 /* may take up to 4 sec */
924 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
927 /* can't take over 500 ms */
928 c = ide_wait(device, IDE_TIME_OUT);
931 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
933 #if defined(CONFIG_SYS_64BIT_LBA)
934 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
937 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
938 device, (ulong) blknr, c);
943 ide_input_data(device, buffer, ATA_SECTORWORDS);
944 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
948 buffer += ATA_BLOCKSIZE;
951 ide_led(DEVICE_LED(device), 0); /* LED off */
955 /* ------------------------------------------------------------------------- */
958 ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
964 unsigned char lba48 = 0;
966 if (blknr & 0x0000fffff0000000ULL) {
967 /* more than 28 bits used, use 48bit mode */
972 ide_led(DEVICE_LED(device), 1); /* LED on */
976 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
978 while (blkcnt-- > 0) {
980 c = ide_wait(device, IDE_TIME_OUT);
982 if (c & ATA_STAT_BUSY) {
983 printf("IDE read: device %d not ready\n", device);
988 /* write high bits */
989 ide_outb(device, ATA_SECT_CNT, 0);
990 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
991 #ifdef CONFIG_SYS_64BIT_LBA
992 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
993 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
995 ide_outb(device, ATA_LBA_MID, 0);
996 ide_outb(device, ATA_LBA_HIGH, 0);
1000 ide_outb(device, ATA_SECT_CNT, 1);
1001 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1002 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1003 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1007 ide_outb(device, ATA_DEV_HD,
1008 ATA_LBA | ATA_DEVICE(device));
1009 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1014 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1015 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1016 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1021 /* can't take over 500 ms */
1022 c = ide_wait(device, IDE_TIME_OUT);
1024 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1026 #if defined(CONFIG_SYS_64BIT_LBA)
1027 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1030 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1031 device, (ulong) blknr, c);
1036 ide_output_data(device, buffer, ATA_SECTORWORDS);
1037 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1040 buffer += ATA_BLOCKSIZE;
1043 ide_led(DEVICE_LED(device), 0); /* LED off */
1047 /* ------------------------------------------------------------------------- */
1050 * copy src to dest, skipping leading and trailing blanks and null
1051 * terminate the string
1052 * "len" is the size of available memory including the terminating '\0'
1054 static void ident_cpy(unsigned char *dst, unsigned char *src,
1057 unsigned char *end, *last;
1060 end = src + len - 1;
1062 /* reserve space for '\0' */
1066 /* skip leading white space */
1067 while ((*src) && (src < end) && (*src == ' '))
1070 /* copy string, omitting trailing white space */
1071 while ((*src) && (src < end)) {
1080 /* ------------------------------------------------------------------------- */
1083 * Wait until Busy bit is off, or timeout (in ms)
1084 * Return last status
1086 static uchar ide_wait(int dev, ulong t)
1088 ulong delay = 10 * t; /* poll every 100 us */
1091 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1099 /* ------------------------------------------------------------------------- */
1101 #ifdef CONFIG_IDE_RESET
1102 extern void ide_set_reset(int idereset);
1104 static void ide_reset(void)
1109 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1111 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1112 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1114 ide_set_reset(1); /* assert reset */
1116 /* the reset signal shall be asserted for et least 25 us */
1121 /* de-assert RESET signal */
1125 for (i = 0; i < 250; ++i)
1129 #endif /* CONFIG_IDE_RESET */
1131 /* ------------------------------------------------------------------------- */
1133 #if defined(CONFIG_OF_IDE_FIXUP)
1134 int ide_device_present(int dev)
1136 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1138 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1141 /* ------------------------------------------------------------------------- */
1144 /****************************************************************************
1148 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1149 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1151 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1152 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1155 #if defined(CONFIG_IDE_SWAP_IO)
1156 /* since ATAPI may use commands with not 4 bytes alligned length
1157 * we have our own transfer functions, 2 bytes alligned */
1158 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1161 volatile ushort *pbuf;
1163 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1164 dbuf = (ushort *) sect_buf;
1166 debug("in output data shorts base for read is %lx\n",
1167 (unsigned long) pbuf);
1175 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1178 volatile ushort *pbuf;
1180 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1181 dbuf = (ushort *) sect_buf;
1183 debug("in input data shorts base for read is %lx\n",
1184 (unsigned long) pbuf);
1192 #else /* ! CONFIG_IDE_SWAP_IO */
1193 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1195 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1198 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1200 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1203 #endif /* CONFIG_IDE_SWAP_IO */
1206 * Wait until (Status & mask) == res, or timeout (in ms)
1207 * Return last status
1208 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1209 * and then they set their DRQ Bit
1211 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1213 ulong delay = 10 * t; /* poll every 100 us */
1216 /* prevents to read the status before valid */
1217 c = ide_inb(dev, ATA_DEV_CTL);
1219 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1220 /* break if error occurs (doesn't make sense to wait more) */
1221 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1231 * issue an atapi command
1233 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1234 unsigned char *buffer, int buflen)
1236 unsigned char c, err, mask, res;
1239 ide_led(DEVICE_LED(device), 1); /* LED on */
1243 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1245 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1246 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1247 if ((c & mask) != res) {
1248 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1253 /* write taskfile */
1254 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1255 ide_outb(device, ATA_SECT_CNT, 0);
1256 ide_outb(device, ATA_SECT_NUM, 0);
1257 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1258 ide_outb(device, ATA_CYL_HIGH,
1259 (unsigned char) ((buflen >> 8) & 0xFF));
1260 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1262 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1265 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1267 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1269 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1270 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1276 /* write command block */
1277 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1279 /* ATAPI Command written wait for completition */
1280 udelay(5000); /* device must set bsy */
1282 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1284 * if no data wait for DRQ = 0 BSY = 0
1285 * if data wait for DRQ = 1 BSY = 0
1290 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1291 if ((c & mask) != res) {
1292 if (c & ATA_STAT_ERR) {
1293 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1294 debug("atapi_issue 1 returned sense key %X status %02X\n",
1297 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1303 n = ide_inb(device, ATA_CYL_HIGH);
1305 n += ide_inb(device, ATA_CYL_LOW);
1307 printf("ERROR, transfer bytes %d requested only %d\n", n,
1312 if ((n == 0) && (buflen < 0)) {
1313 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1318 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1321 if (n != 0) { /* data transfer */
1322 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1323 /* we transfer shorts */
1325 /* ok now decide if it is an in or output */
1326 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1327 debug("Write to device\n");
1328 ide_output_data_shorts(device,
1329 (unsigned short *) buffer, n);
1331 debug("Read from device @ %p shorts %d\n", buffer, n);
1332 ide_input_data_shorts(device,
1333 (unsigned short *) buffer, n);
1336 udelay(5000); /* seems that some CD ROMs need this... */
1337 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1339 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1340 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1341 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1342 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1348 ide_led(DEVICE_LED(device), 0); /* LED off */
1353 * sending the command to atapi_issue. If an status other than good
1354 * returns, an request_sense will be issued
1357 #define ATAPI_DRIVE_NOT_READY 100
1358 #define ATAPI_UNIT_ATTN 10
1360 unsigned char atapi_issue_autoreq(int device,
1363 unsigned char *buffer, int buflen)
1365 unsigned char sense_data[18], sense_ccb[12];
1366 unsigned char res, key, asc, ascq;
1367 int notready, unitattn;
1369 unitattn = ATAPI_UNIT_ATTN;
1370 notready = ATAPI_DRIVE_NOT_READY;
1373 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1378 return 0xFF; /* error */
1380 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1382 memset(sense_ccb, 0, sizeof(sense_ccb));
1383 memset(sense_data, 0, sizeof(sense_data));
1384 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1385 sense_ccb[4] = 18; /* allocation Length */
1387 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1388 key = (sense_data[2] & 0xF);
1389 asc = (sense_data[12]);
1390 ascq = (sense_data[13]);
1392 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1393 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1394 sense_data[0], key, asc, ascq);
1397 return 0; /* ok device ready */
1399 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1400 if (unitattn-- > 0) {
1404 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1407 if ((asc == 0x4) && (ascq == 0x1)) {
1408 /* not ready, but will be ready soon */
1409 if (notready-- > 0) {
1413 printf("Drive not ready, tried %d times\n",
1414 ATAPI_DRIVE_NOT_READY);
1418 debug("Media not present\n");
1422 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1425 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1430 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1432 unsigned char ccb[12]; /* Command descriptor block */
1433 unsigned char iobuf[64]; /* temp buf */
1437 device = dev_desc->dev;
1438 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1439 dev_desc->block_read = atapi_read;
1441 memset(ccb, 0, sizeof(ccb));
1442 memset(iobuf, 0, sizeof(iobuf));
1444 ccb[0] = ATAPI_CMD_INQUIRY;
1445 ccb[4] = 40; /* allocation Legnth */
1446 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1448 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1452 /* copy device ident strings */
1453 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1454 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1455 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1459 dev_desc->blksz = 0;
1460 dev_desc->type = iobuf[0] & 0x1f;
1462 if ((iobuf[1] & 0x80) == 0x80)
1463 dev_desc->removable = 1;
1465 dev_desc->removable = 0;
1467 memset(ccb, 0, sizeof(ccb));
1468 memset(iobuf, 0, sizeof(iobuf));
1469 ccb[0] = ATAPI_CMD_START_STOP;
1470 ccb[4] = 0x03; /* start */
1472 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1474 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1478 memset(ccb, 0, sizeof(ccb));
1479 memset(iobuf, 0, sizeof(iobuf));
1480 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1482 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1486 memset(ccb, 0, sizeof(ccb));
1487 memset(iobuf, 0, sizeof(iobuf));
1488 ccb[0] = ATAPI_CMD_READ_CAP;
1489 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1490 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1494 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1495 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1496 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1498 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1499 ((unsigned long) iobuf[1] << 16) +
1500 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1501 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1502 ((unsigned long) iobuf[5] << 16) +
1503 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1505 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1506 dev_desc->lba48 = 0;
1514 * we transfer only one block per command, since the multiple DRQ per
1515 * command is not yet implemented
1517 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1518 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1519 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1521 ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1524 unsigned char ccb[12]; /* Command descriptor block */
1527 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1528 device, blknr, blkcnt, (ulong) buffer);
1531 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1532 cnt = ATAPI_READ_MAX_BLOCK;
1536 ccb[0] = ATAPI_CMD_READ_12;
1537 ccb[1] = 0; /* reserved */
1538 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1539 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1540 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1541 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1542 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1543 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1544 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1545 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1546 ccb[10] = 0; /* reserved */
1547 ccb[11] = 0; /* reserved */
1549 if (atapi_issue_autoreq(device, ccb, 12,
1550 (unsigned char *) buffer,
1551 cnt * ATAPI_READ_BLOCK_SIZE)
1558 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1559 } while (blkcnt > 0);
1563 /* ------------------------------------------------------------------------- */
1565 #endif /* CONFIG_ATAPI */
1567 U_BOOT_CMD(ide, 5, 1, do_ide,
1569 "reset - reset IDE controller\n"
1570 "ide info - show available IDE devices\n"
1571 "ide device [dev] - show or set current device\n"
1572 "ide part [dev] - print partition table of one or all IDE devices\n"
1573 "ide read addr blk# cnt\n"
1574 "ide write addr blk# cnt - read/write `cnt'"
1575 " blocks starting at block `blk#'\n"
1576 " to/from memory address `addr'");
1578 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1579 "boot from IDE device", "loadAddr dev:part");