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].log2blksz =
459 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
460 ide_dev_desc[i].lba = 0;
461 ide_dev_desc[i].block_read = ide_read;
462 ide_dev_desc[i].block_write = ide_write;
463 if (!ide_bus_ok[IDE_BUS(i)])
465 ide_led(led, 1); /* LED on */
466 ide_ident(&ide_dev_desc[i]);
467 ide_led(led, 0); /* LED off */
468 dev_print(&ide_dev_desc[i]);
470 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
471 /* initialize partition type */
472 init_part(&ide_dev_desc[i]);
480 /* ------------------------------------------------------------------------- */
482 #ifdef CONFIG_PARTITIONS
483 block_dev_desc_t *ide_get_dev(int dev)
485 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
489 /* ------------------------------------------------------------------------- */
491 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
492 __attribute__ ((weak, alias("__ide_input_swap_data")));
494 void ide_input_data(int dev, ulong *sect_buf, int words)
495 __attribute__ ((weak, alias("__ide_input_data")));
497 void ide_output_data(int dev, const ulong *sect_buf, int words)
498 __attribute__ ((weak, alias("__ide_output_data")));
500 /* We only need to swap data if we are running on a big endian cpu. */
501 #if defined(__LITTLE_ENDIAN)
502 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
504 ide_input_data(dev, sect_buf, words);
507 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
509 volatile ushort *pbuf =
510 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
511 ushort *dbuf = (ushort *) sect_buf;
513 debug("in input swap data base for read is %lx\n",
514 (unsigned long) pbuf);
518 *dbuf++ = swab16p((u16 *) pbuf);
519 *dbuf++ = swab16p((u16 *) pbuf);
521 *dbuf++ = ld_le16(pbuf);
522 *dbuf++ = ld_le16(pbuf);
526 #endif /* __LITTLE_ENDIAN */
529 #if defined(CONFIG_IDE_SWAP_IO)
530 void __ide_output_data(int dev, const ulong *sect_buf, int words)
533 volatile ushort *pbuf;
535 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
536 dbuf = (ushort *) sect_buf;
544 #else /* ! CONFIG_IDE_SWAP_IO */
545 void __ide_output_data(int dev, const ulong *sect_buf, int words)
547 #if defined(CONFIG_IDE_AHB)
548 ide_write_data(dev, sect_buf, words);
550 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
553 #endif /* CONFIG_IDE_SWAP_IO */
555 #if defined(CONFIG_IDE_SWAP_IO)
556 void __ide_input_data(int dev, ulong *sect_buf, int words)
559 volatile ushort *pbuf;
561 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
562 dbuf = (ushort *) sect_buf;
564 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
573 #else /* ! CONFIG_IDE_SWAP_IO */
574 void __ide_input_data(int dev, ulong *sect_buf, int words)
576 #if defined(CONFIG_IDE_AHB)
577 ide_read_data(dev, sect_buf, words);
579 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
583 #endif /* CONFIG_IDE_SWAP_IO */
585 /* -------------------------------------------------------------------------
587 static void ide_ident(block_dev_desc_t *dev_desc)
596 #ifdef CONFIG_TUNE_PIO
601 int mode, cycle_time;
605 device = dev_desc->dev;
606 printf(" Device %d: ", device);
608 ide_led(DEVICE_LED(device), 1); /* LED on */
611 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
612 dev_desc->if_type = IF_TYPE_IDE;
617 /* Warning: This will be tricky to read */
618 while (retries <= 1) {
619 /* check signature */
620 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
621 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
622 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
623 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
624 /* ATAPI Signature found */
625 dev_desc->if_type = IF_TYPE_ATAPI;
627 * Start Ident Command
629 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
631 * Wait for completion - ATAPI devices need more time
634 c = ide_wait(device, ATAPI_TIME_OUT);
639 * Start Ident Command
641 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
644 * Wait for completion
646 c = ide_wait(device, IDE_TIME_OUT);
648 ide_led(DEVICE_LED(device), 0); /* LED off */
650 if (((c & ATA_STAT_DRQ) == 0) ||
651 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
655 * Need to soft reset the device
656 * in case it's an ATAPI...
658 debug("Retrying...\n");
659 ide_outb(device, ATA_DEV_HD,
660 ATA_LBA | ATA_DEVICE(device));
662 ide_outb(device, ATA_COMMAND, 0x08);
663 udelay(500000); /* 500 ms */
668 ide_outb(device, ATA_DEV_HD,
669 ATA_LBA | ATA_DEVICE(device));
678 } /* see above - ugly to read */
680 if (retries == 2) /* Not found */
684 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
686 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
687 sizeof(dev_desc->revision));
688 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
689 sizeof(dev_desc->vendor));
690 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
691 sizeof(dev_desc->product));
692 #ifdef __LITTLE_ENDIAN
694 * firmware revision, model, and serial number have Big Endian Byte
695 * order in Word. Convert all three to little endian.
697 * See CF+ and CompactFlash Specification Revision 2.0:
698 * 6.2.1.6: Identify Drive, Table 39 for more details
701 strswab(dev_desc->revision);
702 strswab(dev_desc->vendor);
703 strswab(dev_desc->product);
704 #endif /* __LITTLE_ENDIAN */
706 if ((iop.config & 0x0080) == 0x0080)
707 dev_desc->removable = 1;
709 dev_desc->removable = 0;
711 #ifdef CONFIG_TUNE_PIO
712 /* Mode 0 - 2 only, are directly determined by word 51. */
715 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
716 /* Force it to dead slow, and hope for the best... */
720 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
721 * shall set bit 1 of word 53 to one and support the fields contained
722 * in words 64 through 70.
724 if (iop.field_valid & 0x02) {
726 * Mode 3 and above are possible. Check in order from slow
727 * to fast, so we wind up with the highest mode allowed.
729 if (iop.eide_pio_modes & 0x01)
731 if (iop.eide_pio_modes & 0x02)
733 if (ata_id_is_cfa((u16 *)&iop)) {
734 if ((iop.cf_advanced_caps & 0x07) == 0x01)
736 if ((iop.cf_advanced_caps & 0x07) == 0x02)
741 /* System-specific, depends on bus speeds, etc. */
742 ide_set_piomode(pio_mode);
743 #endif /* CONFIG_TUNE_PIO */
747 * Drive PIO mode autoselection
751 printf("tPIO = 0x%02x = %d\n", mode, mode);
752 if (mode > 2) { /* 2 is maximum allowed tPIO value */
754 debug("Override tPIO -> 2\n");
756 if (iop.field_valid & 2) { /* drive implements ATA2? */
757 debug("Drive implements ATA2\n");
758 if (iop.capability & 8) { /* drive supports use_iordy? */
759 cycle_time = iop.eide_pio_iordy;
761 cycle_time = iop.eide_pio;
763 debug("cycle time = %d\n", cycle_time);
765 if (cycle_time > 120)
766 mode = 3; /* 120 ns for PIO mode 4 */
767 if (cycle_time > 180)
768 mode = 2; /* 180 ns for PIO mode 3 */
769 if (cycle_time > 240)
770 mode = 1; /* 240 ns for PIO mode 4 */
771 if (cycle_time > 383)
772 mode = 0; /* 383 ns for PIO mode 4 */
774 printf("PIO mode to use: PIO %d\n", mode);
778 if (dev_desc->if_type == IF_TYPE_ATAPI) {
779 atapi_inquiry(dev_desc);
782 #endif /* CONFIG_ATAPI */
786 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
787 #else /* ! __BIG_ENDIAN */
789 * do not swap shorts on little endian
791 * See CF+ and CompactFlash Specification Revision 2.0:
792 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
794 dev_desc->lba = iop.lba_capacity;
795 #endif /* __BIG_ENDIAN */
798 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
800 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
801 ((unsigned long long) iop.lba48_capacity[1] << 16) |
802 ((unsigned long long) iop.lba48_capacity[2] << 32) |
803 ((unsigned long long) iop.lba48_capacity[3] << 48);
807 #endif /* CONFIG_LBA48 */
809 dev_desc->type = DEV_TYPE_HARDDISK;
810 dev_desc->blksz = ATA_BLOCKSIZE;
811 dev_desc->log2blksz = LOG2(dev_desc->blksz);
812 dev_desc->lun = 0; /* just to fill something in... */
814 #if 0 /* only used to test the powersaving mode,
815 * if enabled, the drive goes after 5 sec
817 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
818 c = ide_wait(device, IDE_TIME_OUT);
819 ide_outb(device, ATA_SECT_CNT, 1);
820 ide_outb(device, ATA_LBA_LOW, 0);
821 ide_outb(device, ATA_LBA_MID, 0);
822 ide_outb(device, ATA_LBA_HIGH, 0);
823 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
824 ide_outb(device, ATA_COMMAND, 0xe3);
826 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
831 /* ------------------------------------------------------------------------- */
833 ulong ide_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
837 unsigned char pwrsave = 0; /* power save */
840 unsigned char lba48 = 0;
842 if (blknr & 0x0000fffff0000000ULL) {
843 /* more than 28 bits used, use 48bit mode */
847 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
848 device, blknr, blkcnt, (ulong) buffer);
850 ide_led(DEVICE_LED(device), 1); /* LED on */
854 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
855 c = ide_wait(device, IDE_TIME_OUT);
857 if (c & ATA_STAT_BUSY) {
858 printf("IDE read: device %d not ready\n", device);
862 /* first check if the drive is in Powersaving mode, if yes,
863 * increase the timeout value */
864 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
867 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
869 if (c & ATA_STAT_BUSY) {
870 printf("IDE read: device %d not ready\n", device);
873 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
874 printf("No Powersaving mode %X\n", c);
876 c = ide_inb(device, ATA_SECT_CNT);
877 debug("Powersaving %02X\n", c);
883 while (blkcnt-- > 0) {
885 c = ide_wait(device, IDE_TIME_OUT);
887 if (c & ATA_STAT_BUSY) {
888 printf("IDE read: device %d not ready\n", device);
893 /* write high bits */
894 ide_outb(device, ATA_SECT_CNT, 0);
895 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
896 #ifdef CONFIG_SYS_64BIT_LBA
897 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
898 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
900 ide_outb(device, ATA_LBA_MID, 0);
901 ide_outb(device, ATA_LBA_HIGH, 0);
905 ide_outb(device, ATA_SECT_CNT, 1);
906 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
907 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
908 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
912 ide_outb(device, ATA_DEV_HD,
913 ATA_LBA | ATA_DEVICE(device));
914 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
919 ide_outb(device, ATA_DEV_HD, ATA_LBA |
920 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
921 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
927 /* may take up to 4 sec */
928 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
931 /* can't take over 500 ms */
932 c = ide_wait(device, IDE_TIME_OUT);
935 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
937 printf("Error (no IRQ) dev %d blk " LBAF ": status "
938 "%#02x\n", device, blknr, c);
942 ide_input_data(device, buffer, ATA_SECTORWORDS);
943 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
947 buffer += ATA_BLOCKSIZE;
950 ide_led(DEVICE_LED(device), 0); /* LED off */
954 /* ------------------------------------------------------------------------- */
957 ulong ide_write(int device, lbaint_t blknr, lbaint_t blkcnt, const void *buffer)
963 unsigned char lba48 = 0;
965 if (blknr & 0x0000fffff0000000ULL) {
966 /* more than 28 bits used, use 48bit mode */
971 ide_led(DEVICE_LED(device), 1); /* LED on */
975 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
977 while (blkcnt-- > 0) {
979 c = ide_wait(device, IDE_TIME_OUT);
981 if (c & ATA_STAT_BUSY) {
982 printf("IDE read: device %d not ready\n", device);
987 /* write high bits */
988 ide_outb(device, ATA_SECT_CNT, 0);
989 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
990 #ifdef CONFIG_SYS_64BIT_LBA
991 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
992 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
994 ide_outb(device, ATA_LBA_MID, 0);
995 ide_outb(device, ATA_LBA_HIGH, 0);
999 ide_outb(device, ATA_SECT_CNT, 1);
1000 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1001 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1002 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1006 ide_outb(device, ATA_DEV_HD,
1007 ATA_LBA | ATA_DEVICE(device));
1008 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1013 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1014 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1015 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1020 /* can't take over 500 ms */
1021 c = ide_wait(device, IDE_TIME_OUT);
1023 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1025 printf("Error (no IRQ) dev %d blk " LBAF ": status "
1026 "%#02x\n", device, blknr, c);
1030 ide_output_data(device, buffer, ATA_SECTORWORDS);
1031 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1034 buffer += ATA_BLOCKSIZE;
1037 ide_led(DEVICE_LED(device), 0); /* LED off */
1041 /* ------------------------------------------------------------------------- */
1044 * copy src to dest, skipping leading and trailing blanks and null
1045 * terminate the string
1046 * "len" is the size of available memory including the terminating '\0'
1048 static void ident_cpy(unsigned char *dst, unsigned char *src,
1051 unsigned char *end, *last;
1054 end = src + len - 1;
1056 /* reserve space for '\0' */
1060 /* skip leading white space */
1061 while ((*src) && (src < end) && (*src == ' '))
1064 /* copy string, omitting trailing white space */
1065 while ((*src) && (src < end)) {
1074 /* ------------------------------------------------------------------------- */
1077 * Wait until Busy bit is off, or timeout (in ms)
1078 * Return last status
1080 static uchar ide_wait(int dev, ulong t)
1082 ulong delay = 10 * t; /* poll every 100 us */
1085 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1093 /* ------------------------------------------------------------------------- */
1095 #ifdef CONFIG_IDE_RESET
1096 extern void ide_set_reset(int idereset);
1098 static void ide_reset(void)
1103 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1105 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1106 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1108 ide_set_reset(1); /* assert reset */
1110 /* the reset signal shall be asserted for et least 25 us */
1115 /* de-assert RESET signal */
1119 for (i = 0; i < 250; ++i)
1123 #endif /* CONFIG_IDE_RESET */
1125 /* ------------------------------------------------------------------------- */
1127 #if defined(CONFIG_OF_IDE_FIXUP)
1128 int ide_device_present(int dev)
1130 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1132 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1135 /* ------------------------------------------------------------------------- */
1138 /****************************************************************************
1142 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1143 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1145 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1146 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1149 #if defined(CONFIG_IDE_SWAP_IO)
1150 /* since ATAPI may use commands with not 4 bytes alligned length
1151 * we have our own transfer functions, 2 bytes alligned */
1152 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1155 volatile ushort *pbuf;
1157 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1158 dbuf = (ushort *) sect_buf;
1160 debug("in output data shorts base for read is %lx\n",
1161 (unsigned long) pbuf);
1169 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1172 volatile ushort *pbuf;
1174 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1175 dbuf = (ushort *) sect_buf;
1177 debug("in input data shorts base for read is %lx\n",
1178 (unsigned long) pbuf);
1186 #else /* ! CONFIG_IDE_SWAP_IO */
1187 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1189 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1192 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1194 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1197 #endif /* CONFIG_IDE_SWAP_IO */
1200 * Wait until (Status & mask) == res, or timeout (in ms)
1201 * Return last status
1202 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1203 * and then they set their DRQ Bit
1205 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1207 ulong delay = 10 * t; /* poll every 100 us */
1210 /* prevents to read the status before valid */
1211 c = ide_inb(dev, ATA_DEV_CTL);
1213 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1214 /* break if error occurs (doesn't make sense to wait more) */
1215 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1225 * issue an atapi command
1227 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1228 unsigned char *buffer, int buflen)
1230 unsigned char c, err, mask, res;
1233 ide_led(DEVICE_LED(device), 1); /* LED on */
1237 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1239 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1240 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1241 if ((c & mask) != res) {
1242 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1247 /* write taskfile */
1248 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1249 ide_outb(device, ATA_SECT_CNT, 0);
1250 ide_outb(device, ATA_SECT_NUM, 0);
1251 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1252 ide_outb(device, ATA_CYL_HIGH,
1253 (unsigned char) ((buflen >> 8) & 0xFF));
1254 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1256 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1259 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1261 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1263 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1264 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1270 /* write command block */
1271 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1273 /* ATAPI Command written wait for completition */
1274 udelay(5000); /* device must set bsy */
1276 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1278 * if no data wait for DRQ = 0 BSY = 0
1279 * if data wait for DRQ = 1 BSY = 0
1284 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1285 if ((c & mask) != res) {
1286 if (c & ATA_STAT_ERR) {
1287 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1288 debug("atapi_issue 1 returned sense key %X status %02X\n",
1291 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1297 n = ide_inb(device, ATA_CYL_HIGH);
1299 n += ide_inb(device, ATA_CYL_LOW);
1301 printf("ERROR, transfer bytes %d requested only %d\n", n,
1306 if ((n == 0) && (buflen < 0)) {
1307 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1312 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1315 if (n != 0) { /* data transfer */
1316 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1317 /* we transfer shorts */
1319 /* ok now decide if it is an in or output */
1320 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1321 debug("Write to device\n");
1322 ide_output_data_shorts(device,
1323 (unsigned short *) buffer, n);
1325 debug("Read from device @ %p shorts %d\n", buffer, n);
1326 ide_input_data_shorts(device,
1327 (unsigned short *) buffer, n);
1330 udelay(5000); /* seems that some CD ROMs need this... */
1331 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1333 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1334 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1335 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1336 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1342 ide_led(DEVICE_LED(device), 0); /* LED off */
1347 * sending the command to atapi_issue. If an status other than good
1348 * returns, an request_sense will be issued
1351 #define ATAPI_DRIVE_NOT_READY 100
1352 #define ATAPI_UNIT_ATTN 10
1354 unsigned char atapi_issue_autoreq(int device,
1357 unsigned char *buffer, int buflen)
1359 unsigned char sense_data[18], sense_ccb[12];
1360 unsigned char res, key, asc, ascq;
1361 int notready, unitattn;
1363 unitattn = ATAPI_UNIT_ATTN;
1364 notready = ATAPI_DRIVE_NOT_READY;
1367 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1372 return 0xFF; /* error */
1374 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1376 memset(sense_ccb, 0, sizeof(sense_ccb));
1377 memset(sense_data, 0, sizeof(sense_data));
1378 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1379 sense_ccb[4] = 18; /* allocation Length */
1381 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1382 key = (sense_data[2] & 0xF);
1383 asc = (sense_data[12]);
1384 ascq = (sense_data[13]);
1386 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1387 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1388 sense_data[0], key, asc, ascq);
1391 return 0; /* ok device ready */
1393 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1394 if (unitattn-- > 0) {
1398 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1401 if ((asc == 0x4) && (ascq == 0x1)) {
1402 /* not ready, but will be ready soon */
1403 if (notready-- > 0) {
1407 printf("Drive not ready, tried %d times\n",
1408 ATAPI_DRIVE_NOT_READY);
1412 debug("Media not present\n");
1416 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1419 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1424 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1426 unsigned char ccb[12]; /* Command descriptor block */
1427 unsigned char iobuf[64]; /* temp buf */
1431 device = dev_desc->dev;
1432 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1433 dev_desc->block_read = atapi_read;
1435 memset(ccb, 0, sizeof(ccb));
1436 memset(iobuf, 0, sizeof(iobuf));
1438 ccb[0] = ATAPI_CMD_INQUIRY;
1439 ccb[4] = 40; /* allocation Legnth */
1440 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1442 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1446 /* copy device ident strings */
1447 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1448 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1449 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1453 dev_desc->blksz = 0;
1454 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
1455 dev_desc->type = iobuf[0] & 0x1f;
1457 if ((iobuf[1] & 0x80) == 0x80)
1458 dev_desc->removable = 1;
1460 dev_desc->removable = 0;
1462 memset(ccb, 0, sizeof(ccb));
1463 memset(iobuf, 0, sizeof(iobuf));
1464 ccb[0] = ATAPI_CMD_START_STOP;
1465 ccb[4] = 0x03; /* start */
1467 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1469 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1473 memset(ccb, 0, sizeof(ccb));
1474 memset(iobuf, 0, sizeof(iobuf));
1475 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1477 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1481 memset(ccb, 0, sizeof(ccb));
1482 memset(iobuf, 0, sizeof(iobuf));
1483 ccb[0] = ATAPI_CMD_READ_CAP;
1484 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1485 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1489 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1490 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1491 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1493 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1494 ((unsigned long) iobuf[1] << 16) +
1495 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1496 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1497 ((unsigned long) iobuf[5] << 16) +
1498 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1499 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1501 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1502 dev_desc->lba48 = 0;
1510 * we transfer only one block per command, since the multiple DRQ per
1511 * command is not yet implemented
1513 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1514 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1515 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1517 ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
1520 unsigned char ccb[12]; /* Command descriptor block */
1523 debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
1524 device, blknr, blkcnt, (ulong) buffer);
1527 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1528 cnt = ATAPI_READ_MAX_BLOCK;
1532 ccb[0] = ATAPI_CMD_READ_12;
1533 ccb[1] = 0; /* reserved */
1534 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1535 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1536 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1537 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1538 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1539 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1540 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1541 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1542 ccb[10] = 0; /* reserved */
1543 ccb[11] = 0; /* reserved */
1545 if (atapi_issue_autoreq(device, ccb, 12,
1546 (unsigned char *) buffer,
1547 cnt * ATAPI_READ_BLOCK_SIZE)
1554 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1555 } while (blkcnt > 0);
1559 /* ------------------------------------------------------------------------- */
1561 #endif /* CONFIG_ATAPI */
1563 U_BOOT_CMD(ide, 5, 1, do_ide,
1565 "reset - reset IDE controller\n"
1566 "ide info - show available IDE devices\n"
1567 "ide device [dev] - show or set current device\n"
1568 "ide part [dev] - print partition table of one or all IDE devices\n"
1569 "ide read addr blk# cnt\n"
1570 "ide write addr blk# cnt - read/write `cnt'"
1571 " blocks starting at block `blk#'\n"
1572 " to/from memory address `addr'");
1574 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1575 "boot from IDE device", "loadAddr dev:part");