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 static ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt,
104 /* ------------------------------------------------------------------------- */
106 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
113 return CMD_RET_USAGE;
115 if (strncmp(argv[1], "res", 3) == 0) {
117 #ifdef CONFIG_IDE_8xx_DIRECT
118 " on PCMCIA " PCMCIA_SLOT_MSG
124 } else if (strncmp(argv[1], "inf", 3) == 0) {
129 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
130 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
131 continue; /* list only known devices */
132 printf("IDE device %d: ", i);
133 dev_print(&ide_dev_desc[i]);
137 } else if (strncmp(argv[1], "dev", 3) == 0) {
138 if ((curr_device < 0)
139 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
140 puts("\nno IDE devices available\n");
143 printf("\nIDE device %d: ", curr_device);
144 dev_print(&ide_dev_desc[curr_device]);
146 } else if (strncmp(argv[1], "part", 4) == 0) {
149 for (ok = 0, dev = 0;
150 dev < CONFIG_SYS_IDE_MAXDEVICE;
152 if (ide_dev_desc[dev].part_type !=
157 print_part(&ide_dev_desc[dev]);
161 puts("\nno IDE devices available\n");
166 return CMD_RET_USAGE;
168 if (strncmp(argv[1], "dev", 3) == 0) {
169 int dev = (int) simple_strtoul(argv[2], NULL, 10);
171 printf("\nIDE device %d: ", dev);
172 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
173 puts("unknown device\n");
176 dev_print(&ide_dev_desc[dev]);
177 /*ide_print (dev); */
179 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
184 puts("... is now current device\n");
187 } else if (strncmp(argv[1], "part", 4) == 0) {
188 int dev = (int) simple_strtoul(argv[2], NULL, 10);
190 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
191 print_part(&ide_dev_desc[dev]);
193 printf("\nIDE device %d not available\n",
200 return CMD_RET_USAGE;
202 /* at least 4 args */
204 if (strcmp(argv[1], "read") == 0) {
205 ulong addr = simple_strtoul(argv[2], NULL, 16);
206 ulong cnt = simple_strtoul(argv[4], NULL, 16);
209 #ifdef CONFIG_SYS_64BIT_LBA
210 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
212 printf("\nIDE read: device %d block # %lld, count %ld ... ",
213 curr_device, blk, cnt);
215 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
217 printf("\nIDE read: device %d block # %ld, count %ld ... ",
218 curr_device, blk, cnt);
221 n = ide_dev_desc[curr_device].block_read(curr_device,
224 /* flush cache after read */
226 cnt * ide_dev_desc[curr_device].blksz);
228 printf("%ld blocks read: %s\n",
229 n, (n == cnt) ? "OK" : "ERROR");
234 } else if (strcmp(argv[1], "write") == 0) {
235 ulong addr = simple_strtoul(argv[2], NULL, 16);
236 ulong cnt = simple_strtoul(argv[4], NULL, 16);
239 #ifdef CONFIG_SYS_64BIT_LBA
240 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
242 printf("\nIDE write: device %d block # %lld, count %ld ... ",
243 curr_device, blk, cnt);
245 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
247 printf("\nIDE write: device %d block # %ld, count %ld ... ",
248 curr_device, blk, cnt);
250 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
252 printf("%ld blocks written: %s\n",
253 n, (n == cnt) ? "OK" : "ERROR");
259 return CMD_RET_USAGE;
266 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
268 return common_diskboot(cmdtp, "ide", argc, argv);
271 /* ------------------------------------------------------------------------- */
273 void __ide_led(uchar led, uchar status)
275 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
276 static uchar led_buffer; /* Buffer for current LED status */
278 uchar *led_port = LED_PORT;
280 if (status) /* switch LED on */
282 else /* switch LED off */
285 *led_port = led_buffer;
289 void ide_led(uchar led, uchar status)
290 __attribute__ ((weak, alias("__ide_led")));
292 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
293 # define DEVICE_LED(x) 0
298 /* ------------------------------------------------------------------------- */
300 inline void __ide_outb(int dev, int port, unsigned char val)
302 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
304 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
306 #if defined(CONFIG_IDE_AHB)
309 ide_write_register(dev, port, val);
312 outb(val, (ATA_CURR_BASE(dev)));
315 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
319 void ide_outb(int dev, int port, unsigned char val)
320 __attribute__ ((weak, alias("__ide_outb")));
322 inline unsigned char __ide_inb(int dev, int port)
326 #if defined(CONFIG_IDE_AHB)
327 val = ide_read_register(dev, port);
329 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
332 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
334 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
338 unsigned char ide_inb(int dev, int port)
339 __attribute__ ((weak, alias("__ide_inb")));
341 #ifdef CONFIG_TUNE_PIO
342 inline int __ide_set_piomode(int pio_mode)
347 inline int ide_set_piomode(int pio_mode)
348 __attribute__ ((weak, alias("__ide_set_piomode")));
356 #ifdef CONFIG_IDE_8xx_PCCARD
357 extern int ide_devices_found; /* Initialized in check_ide_device() */
358 #endif /* CONFIG_IDE_8xx_PCCARD */
360 #ifdef CONFIG_IDE_PREINIT
364 puts("ide_preinit failed\n");
367 #endif /* CONFIG_IDE_PREINIT */
372 * Reset the IDE just to be sure.
373 * Light LED's to show
375 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
377 /* ATAPI Drives seems to need a proper IDE Reset */
380 #ifdef CONFIG_IDE_INIT_POSTRESET
383 if (ide_init_postreset()) {
384 puts("ide_preinit_postreset failed\n");
387 #endif /* CONFIG_IDE_INIT_POSTRESET */
390 * Wait for IDE to get ready.
391 * According to spec, this can take up to 31 seconds!
393 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
395 bus * (CONFIG_SYS_IDE_MAXDEVICE /
396 CONFIG_SYS_IDE_MAXBUS);
398 #ifdef CONFIG_IDE_8xx_PCCARD
399 /* Skip non-ide devices from probing */
400 if ((ide_devices_found & (1 << bus)) == 0) {
401 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
405 printf("Bus %d: ", bus);
411 udelay(100000); /* 100 ms */
412 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
413 udelay(100000); /* 100 ms */
416 udelay(10000); /* 10 ms */
418 c = ide_inb(dev, ATA_STATUS);
420 if (i > (ATA_RESET_TIME * 100)) {
421 puts("** Timeout **\n");
423 ide_led((LED_IDE1 | LED_IDE2), 0);
426 if ((i >= 100) && ((i % 100) == 0))
429 } while (c & ATA_STAT_BUSY);
431 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
432 puts("not available ");
433 debug("Status = 0x%02X ", c);
434 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
435 } else if ((c & ATA_STAT_READY) == 0) {
436 puts("not available ");
437 debug("Status = 0x%02X ", c);
448 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
451 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
452 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
453 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
454 ide_dev_desc[i].if_type = IF_TYPE_IDE;
455 ide_dev_desc[i].dev = i;
456 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
457 ide_dev_desc[i].blksz = 0;
458 ide_dev_desc[i].lba = 0;
459 ide_dev_desc[i].block_read = ide_read;
460 ide_dev_desc[i].block_write = ide_write;
461 if (!ide_bus_ok[IDE_BUS(i)])
463 ide_led(led, 1); /* LED on */
464 ide_ident(&ide_dev_desc[i]);
465 ide_led(led, 0); /* LED off */
466 dev_print(&ide_dev_desc[i]);
468 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
469 /* initialize partition type */
470 init_part(&ide_dev_desc[i]);
478 /* ------------------------------------------------------------------------- */
480 #ifdef CONFIG_PARTITIONS
481 block_dev_desc_t *ide_get_dev(int dev)
483 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
487 /* ------------------------------------------------------------------------- */
489 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
490 __attribute__ ((weak, alias("__ide_input_swap_data")));
492 void ide_input_data(int dev, ulong *sect_buf, int words)
493 __attribute__ ((weak, alias("__ide_input_data")));
495 void ide_output_data(int dev, const ulong *sect_buf, int words)
496 __attribute__ ((weak, alias("__ide_output_data")));
498 /* We only need to swap data if we are running on a big endian cpu. */
499 #if defined(__LITTLE_ENDIAN)
500 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
502 ide_input_data(dev, sect_buf, words);
505 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
507 volatile ushort *pbuf =
508 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
509 ushort *dbuf = (ushort *) sect_buf;
511 debug("in input swap data base for read is %lx\n",
512 (unsigned long) pbuf);
516 *dbuf++ = swab16p((u16 *) pbuf);
517 *dbuf++ = swab16p((u16 *) pbuf);
519 *dbuf++ = ld_le16(pbuf);
520 *dbuf++ = ld_le16(pbuf);
524 #endif /* __LITTLE_ENDIAN */
527 #if defined(CONFIG_IDE_SWAP_IO)
528 void __ide_output_data(int dev, const ulong *sect_buf, int words)
531 volatile ushort *pbuf;
533 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
534 dbuf = (ushort *) sect_buf;
542 #else /* ! CONFIG_IDE_SWAP_IO */
543 void __ide_output_data(int dev, const ulong *sect_buf, int words)
545 #if defined(CONFIG_IDE_AHB)
546 ide_write_data(dev, sect_buf, words);
548 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
551 #endif /* CONFIG_IDE_SWAP_IO */
553 #if defined(CONFIG_IDE_SWAP_IO)
554 void __ide_input_data(int dev, ulong *sect_buf, int words)
557 volatile ushort *pbuf;
559 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
560 dbuf = (ushort *) sect_buf;
562 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
571 #else /* ! CONFIG_IDE_SWAP_IO */
572 void __ide_input_data(int dev, ulong *sect_buf, int words)
574 #if defined(CONFIG_IDE_AHB)
575 ide_read_data(dev, sect_buf, words);
577 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
581 #endif /* CONFIG_IDE_SWAP_IO */
583 /* -------------------------------------------------------------------------
585 static void ide_ident(block_dev_desc_t *dev_desc)
594 #ifdef CONFIG_TUNE_PIO
599 int mode, cycle_time;
603 device = dev_desc->dev;
604 printf(" Device %d: ", device);
606 ide_led(DEVICE_LED(device), 1); /* LED on */
609 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
610 dev_desc->if_type = IF_TYPE_IDE;
615 /* Warning: This will be tricky to read */
616 while (retries <= 1) {
617 /* check signature */
618 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
619 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
620 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
621 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
622 /* ATAPI Signature found */
623 dev_desc->if_type = IF_TYPE_ATAPI;
625 * Start Ident Command
627 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
629 * Wait for completion - ATAPI devices need more time
632 c = ide_wait(device, ATAPI_TIME_OUT);
637 * Start Ident Command
639 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
642 * Wait for completion
644 c = ide_wait(device, IDE_TIME_OUT);
646 ide_led(DEVICE_LED(device), 0); /* LED off */
648 if (((c & ATA_STAT_DRQ) == 0) ||
649 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
653 * Need to soft reset the device
654 * in case it's an ATAPI...
656 debug("Retrying...\n");
657 ide_outb(device, ATA_DEV_HD,
658 ATA_LBA | ATA_DEVICE(device));
660 ide_outb(device, ATA_COMMAND, 0x08);
661 udelay(500000); /* 500 ms */
666 ide_outb(device, ATA_DEV_HD,
667 ATA_LBA | ATA_DEVICE(device));
676 } /* see above - ugly to read */
678 if (retries == 2) /* Not found */
682 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
684 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
685 sizeof(dev_desc->revision));
686 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
687 sizeof(dev_desc->vendor));
688 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
689 sizeof(dev_desc->product));
690 #ifdef __LITTLE_ENDIAN
692 * firmware revision, model, and serial number have Big Endian Byte
693 * order in Word. Convert all three to little endian.
695 * See CF+ and CompactFlash Specification Revision 2.0:
696 * 6.2.1.6: Identify Drive, Table 39 for more details
699 strswab(dev_desc->revision);
700 strswab(dev_desc->vendor);
701 strswab(dev_desc->product);
702 #endif /* __LITTLE_ENDIAN */
704 if ((iop.config & 0x0080) == 0x0080)
705 dev_desc->removable = 1;
707 dev_desc->removable = 0;
709 #ifdef CONFIG_TUNE_PIO
710 /* Mode 0 - 2 only, are directly determined by word 51. */
713 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
714 /* Force it to dead slow, and hope for the best... */
718 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
719 * shall set bit 1 of word 53 to one and support the fields contained
720 * in words 64 through 70.
722 if (iop.field_valid & 0x02) {
724 * Mode 3 and above are possible. Check in order from slow
725 * to fast, so we wind up with the highest mode allowed.
727 if (iop.eide_pio_modes & 0x01)
729 if (iop.eide_pio_modes & 0x02)
731 if (ata_id_is_cfa((u16 *)&iop)) {
732 if ((iop.cf_advanced_caps & 0x07) == 0x01)
734 if ((iop.cf_advanced_caps & 0x07) == 0x02)
739 /* System-specific, depends on bus speeds, etc. */
740 ide_set_piomode(pio_mode);
741 #endif /* CONFIG_TUNE_PIO */
745 * Drive PIO mode autoselection
749 printf("tPIO = 0x%02x = %d\n", mode, mode);
750 if (mode > 2) { /* 2 is maximum allowed tPIO value */
752 debug("Override tPIO -> 2\n");
754 if (iop.field_valid & 2) { /* drive implements ATA2? */
755 debug("Drive implements ATA2\n");
756 if (iop.capability & 8) { /* drive supports use_iordy? */
757 cycle_time = iop.eide_pio_iordy;
759 cycle_time = iop.eide_pio;
761 debug("cycle time = %d\n", cycle_time);
763 if (cycle_time > 120)
764 mode = 3; /* 120 ns for PIO mode 4 */
765 if (cycle_time > 180)
766 mode = 2; /* 180 ns for PIO mode 3 */
767 if (cycle_time > 240)
768 mode = 1; /* 240 ns for PIO mode 4 */
769 if (cycle_time > 383)
770 mode = 0; /* 383 ns for PIO mode 4 */
772 printf("PIO mode to use: PIO %d\n", mode);
776 if (dev_desc->if_type == IF_TYPE_ATAPI) {
777 atapi_inquiry(dev_desc);
780 #endif /* CONFIG_ATAPI */
784 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
785 #else /* ! __BIG_ENDIAN */
787 * do not swap shorts on little endian
789 * See CF+ and CompactFlash Specification Revision 2.0:
790 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
792 dev_desc->lba = iop.lba_capacity;
793 #endif /* __BIG_ENDIAN */
796 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
798 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
799 ((unsigned long long) iop.lba48_capacity[1] << 16) |
800 ((unsigned long long) iop.lba48_capacity[2] << 32) |
801 ((unsigned long long) iop.lba48_capacity[3] << 48);
805 #endif /* CONFIG_LBA48 */
807 dev_desc->type = DEV_TYPE_HARDDISK;
808 dev_desc->blksz = ATA_BLOCKSIZE;
809 dev_desc->lun = 0; /* just to fill something in... */
811 #if 0 /* only used to test the powersaving mode,
812 * if enabled, the drive goes after 5 sec
814 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
815 c = ide_wait(device, IDE_TIME_OUT);
816 ide_outb(device, ATA_SECT_CNT, 1);
817 ide_outb(device, ATA_LBA_LOW, 0);
818 ide_outb(device, ATA_LBA_MID, 0);
819 ide_outb(device, ATA_LBA_HIGH, 0);
820 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
821 ide_outb(device, ATA_COMMAND, 0xe3);
823 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
828 /* ------------------------------------------------------------------------- */
830 ulong ide_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
834 unsigned char pwrsave = 0; /* power save */
837 unsigned char lba48 = 0;
839 if (blknr & 0x0000fffff0000000ULL) {
840 /* more than 28 bits used, use 48bit mode */
844 debug("ide_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
845 device, blknr, blkcnt, (ulong) buffer);
847 ide_led(DEVICE_LED(device), 1); /* LED on */
851 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
852 c = ide_wait(device, IDE_TIME_OUT);
854 if (c & ATA_STAT_BUSY) {
855 printf("IDE read: device %d not ready\n", device);
859 /* first check if the drive is in Powersaving mode, if yes,
860 * increase the timeout value */
861 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
864 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
866 if (c & ATA_STAT_BUSY) {
867 printf("IDE read: device %d not ready\n", device);
870 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
871 printf("No Powersaving mode %X\n", c);
873 c = ide_inb(device, ATA_SECT_CNT);
874 debug("Powersaving %02X\n", c);
880 while (blkcnt-- > 0) {
882 c = ide_wait(device, IDE_TIME_OUT);
884 if (c & ATA_STAT_BUSY) {
885 printf("IDE read: device %d not ready\n", device);
890 /* write high bits */
891 ide_outb(device, ATA_SECT_CNT, 0);
892 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
893 #ifdef CONFIG_SYS_64BIT_LBA
894 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
895 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
897 ide_outb(device, ATA_LBA_MID, 0);
898 ide_outb(device, ATA_LBA_HIGH, 0);
902 ide_outb(device, ATA_SECT_CNT, 1);
903 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
904 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
905 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
909 ide_outb(device, ATA_DEV_HD,
910 ATA_LBA | ATA_DEVICE(device));
911 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
916 ide_outb(device, ATA_DEV_HD, ATA_LBA |
917 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
918 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
924 /* may take up to 4 sec */
925 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
928 /* can't take over 500 ms */
929 c = ide_wait(device, IDE_TIME_OUT);
932 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
934 printf("Error (no IRQ) dev %d blk %ld: status %#02x\n",
939 ide_input_data(device, buffer, ATA_SECTORWORDS);
940 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
944 buffer += ATA_BLOCKSIZE;
947 ide_led(DEVICE_LED(device), 0); /* LED off */
951 /* ------------------------------------------------------------------------- */
954 ulong ide_write(int device, ulong blknr, lbaint_t blkcnt, const void *buffer)
960 unsigned char lba48 = 0;
962 if (blknr & 0x0000fffff0000000ULL) {
963 /* more than 28 bits used, use 48bit mode */
968 ide_led(DEVICE_LED(device), 1); /* LED on */
972 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
974 while (blkcnt-- > 0) {
976 c = ide_wait(device, IDE_TIME_OUT);
978 if (c & ATA_STAT_BUSY) {
979 printf("IDE read: device %d not ready\n", device);
984 /* write high bits */
985 ide_outb(device, ATA_SECT_CNT, 0);
986 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
987 #ifdef CONFIG_SYS_64BIT_LBA
988 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
989 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
991 ide_outb(device, ATA_LBA_MID, 0);
992 ide_outb(device, ATA_LBA_HIGH, 0);
996 ide_outb(device, ATA_SECT_CNT, 1);
997 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
998 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
999 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1003 ide_outb(device, ATA_DEV_HD,
1004 ATA_LBA | ATA_DEVICE(device));
1005 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1010 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1011 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1012 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1017 /* can't take over 500 ms */
1018 c = ide_wait(device, IDE_TIME_OUT);
1020 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1022 printf("Error (no IRQ) dev %d blk %ld: status %#02x\n",
1027 ide_output_data(device, buffer, ATA_SECTORWORDS);
1028 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1031 buffer += ATA_BLOCKSIZE;
1034 ide_led(DEVICE_LED(device), 0); /* LED off */
1038 /* ------------------------------------------------------------------------- */
1041 * copy src to dest, skipping leading and trailing blanks and null
1042 * terminate the string
1043 * "len" is the size of available memory including the terminating '\0'
1045 static void ident_cpy(unsigned char *dst, unsigned char *src,
1048 unsigned char *end, *last;
1051 end = src + len - 1;
1053 /* reserve space for '\0' */
1057 /* skip leading white space */
1058 while ((*src) && (src < end) && (*src == ' '))
1061 /* copy string, omitting trailing white space */
1062 while ((*src) && (src < end)) {
1071 /* ------------------------------------------------------------------------- */
1074 * Wait until Busy bit is off, or timeout (in ms)
1075 * Return last status
1077 static uchar ide_wait(int dev, ulong t)
1079 ulong delay = 10 * t; /* poll every 100 us */
1082 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1090 /* ------------------------------------------------------------------------- */
1092 #ifdef CONFIG_IDE_RESET
1093 extern void ide_set_reset(int idereset);
1095 static void ide_reset(void)
1100 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1102 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1103 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1105 ide_set_reset(1); /* assert reset */
1107 /* the reset signal shall be asserted for et least 25 us */
1112 /* de-assert RESET signal */
1116 for (i = 0; i < 250; ++i)
1120 #endif /* CONFIG_IDE_RESET */
1122 /* ------------------------------------------------------------------------- */
1124 #if defined(CONFIG_OF_IDE_FIXUP)
1125 int ide_device_present(int dev)
1127 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1129 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1132 /* ------------------------------------------------------------------------- */
1135 /****************************************************************************
1139 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1140 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1142 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1143 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1146 #if defined(CONFIG_IDE_SWAP_IO)
1147 /* since ATAPI may use commands with not 4 bytes alligned length
1148 * we have our own transfer functions, 2 bytes alligned */
1149 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1152 volatile ushort *pbuf;
1154 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1155 dbuf = (ushort *) sect_buf;
1157 debug("in output data shorts base for read is %lx\n",
1158 (unsigned long) pbuf);
1166 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1169 volatile ushort *pbuf;
1171 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1172 dbuf = (ushort *) sect_buf;
1174 debug("in input data shorts base for read is %lx\n",
1175 (unsigned long) pbuf);
1183 #else /* ! CONFIG_IDE_SWAP_IO */
1184 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1186 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1189 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1191 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1194 #endif /* CONFIG_IDE_SWAP_IO */
1197 * Wait until (Status & mask) == res, or timeout (in ms)
1198 * Return last status
1199 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1200 * and then they set their DRQ Bit
1202 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1204 ulong delay = 10 * t; /* poll every 100 us */
1207 /* prevents to read the status before valid */
1208 c = ide_inb(dev, ATA_DEV_CTL);
1210 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1211 /* break if error occurs (doesn't make sense to wait more) */
1212 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1222 * issue an atapi command
1224 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1225 unsigned char *buffer, int buflen)
1227 unsigned char c, err, mask, res;
1230 ide_led(DEVICE_LED(device), 1); /* LED on */
1234 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1236 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1237 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1238 if ((c & mask) != res) {
1239 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1244 /* write taskfile */
1245 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1246 ide_outb(device, ATA_SECT_CNT, 0);
1247 ide_outb(device, ATA_SECT_NUM, 0);
1248 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1249 ide_outb(device, ATA_CYL_HIGH,
1250 (unsigned char) ((buflen >> 8) & 0xFF));
1251 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1253 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1256 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1258 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1260 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1261 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1267 /* write command block */
1268 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1270 /* ATAPI Command written wait for completition */
1271 udelay(5000); /* device must set bsy */
1273 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1275 * if no data wait for DRQ = 0 BSY = 0
1276 * if data wait for DRQ = 1 BSY = 0
1281 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1282 if ((c & mask) != res) {
1283 if (c & ATA_STAT_ERR) {
1284 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1285 debug("atapi_issue 1 returned sense key %X status %02X\n",
1288 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1294 n = ide_inb(device, ATA_CYL_HIGH);
1296 n += ide_inb(device, ATA_CYL_LOW);
1298 printf("ERROR, transfer bytes %d requested only %d\n", n,
1303 if ((n == 0) && (buflen < 0)) {
1304 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1309 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1312 if (n != 0) { /* data transfer */
1313 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1314 /* we transfer shorts */
1316 /* ok now decide if it is an in or output */
1317 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1318 debug("Write to device\n");
1319 ide_output_data_shorts(device,
1320 (unsigned short *) buffer, n);
1322 debug("Read from device @ %p shorts %d\n", buffer, n);
1323 ide_input_data_shorts(device,
1324 (unsigned short *) buffer, n);
1327 udelay(5000); /* seems that some CD ROMs need this... */
1328 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1330 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1331 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1332 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1333 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1339 ide_led(DEVICE_LED(device), 0); /* LED off */
1344 * sending the command to atapi_issue. If an status other than good
1345 * returns, an request_sense will be issued
1348 #define ATAPI_DRIVE_NOT_READY 100
1349 #define ATAPI_UNIT_ATTN 10
1351 unsigned char atapi_issue_autoreq(int device,
1354 unsigned char *buffer, int buflen)
1356 unsigned char sense_data[18], sense_ccb[12];
1357 unsigned char res, key, asc, ascq;
1358 int notready, unitattn;
1360 unitattn = ATAPI_UNIT_ATTN;
1361 notready = ATAPI_DRIVE_NOT_READY;
1364 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1369 return 0xFF; /* error */
1371 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1373 memset(sense_ccb, 0, sizeof(sense_ccb));
1374 memset(sense_data, 0, sizeof(sense_data));
1375 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1376 sense_ccb[4] = 18; /* allocation Length */
1378 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1379 key = (sense_data[2] & 0xF);
1380 asc = (sense_data[12]);
1381 ascq = (sense_data[13]);
1383 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1384 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1385 sense_data[0], key, asc, ascq);
1388 return 0; /* ok device ready */
1390 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1391 if (unitattn-- > 0) {
1395 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1398 if ((asc == 0x4) && (ascq == 0x1)) {
1399 /* not ready, but will be ready soon */
1400 if (notready-- > 0) {
1404 printf("Drive not ready, tried %d times\n",
1405 ATAPI_DRIVE_NOT_READY);
1409 debug("Media not present\n");
1413 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1416 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1421 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1423 unsigned char ccb[12]; /* Command descriptor block */
1424 unsigned char iobuf[64]; /* temp buf */
1428 device = dev_desc->dev;
1429 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1430 dev_desc->block_read = atapi_read;
1432 memset(ccb, 0, sizeof(ccb));
1433 memset(iobuf, 0, sizeof(iobuf));
1435 ccb[0] = ATAPI_CMD_INQUIRY;
1436 ccb[4] = 40; /* allocation Legnth */
1437 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1439 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1443 /* copy device ident strings */
1444 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1445 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1446 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1450 dev_desc->blksz = 0;
1451 dev_desc->type = iobuf[0] & 0x1f;
1453 if ((iobuf[1] & 0x80) == 0x80)
1454 dev_desc->removable = 1;
1456 dev_desc->removable = 0;
1458 memset(ccb, 0, sizeof(ccb));
1459 memset(iobuf, 0, sizeof(iobuf));
1460 ccb[0] = ATAPI_CMD_START_STOP;
1461 ccb[4] = 0x03; /* start */
1463 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1465 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1469 memset(ccb, 0, sizeof(ccb));
1470 memset(iobuf, 0, sizeof(iobuf));
1471 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1473 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1477 memset(ccb, 0, sizeof(ccb));
1478 memset(iobuf, 0, sizeof(iobuf));
1479 ccb[0] = ATAPI_CMD_READ_CAP;
1480 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1481 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1485 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1486 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1487 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1489 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1490 ((unsigned long) iobuf[1] << 16) +
1491 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1492 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1493 ((unsigned long) iobuf[5] << 16) +
1494 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1496 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1497 dev_desc->lba48 = 0;
1505 * we transfer only one block per command, since the multiple DRQ per
1506 * command is not yet implemented
1508 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1509 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1510 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1512 ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
1515 unsigned char ccb[12]; /* Command descriptor block */
1518 debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
1519 device, blknr, blkcnt, (ulong) buffer);
1522 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1523 cnt = ATAPI_READ_MAX_BLOCK;
1527 ccb[0] = ATAPI_CMD_READ_12;
1528 ccb[1] = 0; /* reserved */
1529 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1530 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1531 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1532 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1533 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1534 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1535 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1536 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1537 ccb[10] = 0; /* reserved */
1538 ccb[11] = 0; /* reserved */
1540 if (atapi_issue_autoreq(device, ccb, 12,
1541 (unsigned char *) buffer,
1542 cnt * ATAPI_READ_BLOCK_SIZE)
1549 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1550 } while (blkcnt > 0);
1554 /* ------------------------------------------------------------------------- */
1556 #endif /* CONFIG_ATAPI */
1558 U_BOOT_CMD(ide, 5, 1, do_ide,
1560 "reset - reset IDE controller\n"
1561 "ide info - show available IDE devices\n"
1562 "ide device [dev] - show or set current device\n"
1563 "ide part [dev] - print partition table of one or all IDE devices\n"
1564 "ide read addr blk# cnt\n"
1565 "ide write addr blk# cnt - read/write `cnt'"
1566 " blocks starting at block `blk#'\n"
1567 " to/from memory address `addr'");
1569 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1570 "boot from IDE device", "loadAddr dev:part");