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)
52 #ifdef CONFIG_STATUS_LED
53 # include <status_led.h>
57 # define EIEIO __asm__ volatile ("eieio")
58 # define SYNC __asm__ volatile ("sync")
60 # define EIEIO /* nothing */
61 # define SYNC /* nothing */
64 /* ------------------------------------------------------------------------- */
66 /* Current I/O Device */
67 static int curr_device = -1;
69 /* Current offset for IDE0 / IDE1 bus access */
70 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
71 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
72 CONFIG_SYS_ATA_IDE0_OFFSET,
74 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
75 CONFIG_SYS_ATA_IDE1_OFFSET,
79 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
81 block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
82 /* ------------------------------------------------------------------------- */
84 #ifdef CONFIG_IDE_RESET
85 static void ide_reset (void);
87 #define ide_reset() /* dummy */
90 static void ide_ident (block_dev_desc_t *dev_desc);
91 static uchar ide_wait (int dev, ulong t);
93 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
95 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
97 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
99 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
101 #ifndef CONFIG_SYS_ATA_PORT_ADDR
102 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
106 static void atapi_inquiry(block_dev_desc_t *dev_desc);
107 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
111 /* ------------------------------------------------------------------------- */
113 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
120 return CMD_RET_USAGE;
122 if (strncmp(argv[1], "res", 3) == 0) {
124 #ifdef CONFIG_IDE_8xx_DIRECT
125 " on PCMCIA " PCMCIA_SLOT_MSG
131 } else if (strncmp(argv[1], "inf", 3) == 0) {
136 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
137 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
138 continue; /* list only known devices */
139 printf("IDE device %d: ", i);
140 dev_print(&ide_dev_desc[i]);
144 } else if (strncmp(argv[1], "dev", 3) == 0) {
145 if ((curr_device < 0)
146 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
147 puts("\nno IDE devices available\n");
150 printf("\nIDE device %d: ", curr_device);
151 dev_print(&ide_dev_desc[curr_device]);
153 } else if (strncmp(argv[1], "part", 4) == 0) {
156 for (ok = 0, dev = 0;
157 dev < CONFIG_SYS_IDE_MAXDEVICE;
159 if (ide_dev_desc[dev].part_type !=
164 print_part(&ide_dev_desc[dev]);
168 puts("\nno IDE devices available\n");
173 return CMD_RET_USAGE;
175 if (strncmp(argv[1], "dev", 3) == 0) {
176 int dev = (int) simple_strtoul(argv[2], NULL, 10);
178 printf("\nIDE device %d: ", dev);
179 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
180 puts("unknown device\n");
183 dev_print(&ide_dev_desc[dev]);
184 /*ide_print (dev); */
186 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
191 puts("... is now current device\n");
194 } else if (strncmp(argv[1], "part", 4) == 0) {
195 int dev = (int) simple_strtoul(argv[2], NULL, 10);
197 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
198 print_part(&ide_dev_desc[dev]);
200 printf("\nIDE device %d not available\n",
207 return CMD_RET_USAGE;
209 /* at least 4 args */
211 if (strcmp(argv[1], "read") == 0) {
212 ulong addr = simple_strtoul(argv[2], NULL, 16);
213 ulong cnt = simple_strtoul(argv[4], NULL, 16);
216 #ifdef CONFIG_SYS_64BIT_LBA
217 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
219 printf("\nIDE read: device %d block # %lld, count %ld ... ",
220 curr_device, blk, cnt);
222 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
224 printf("\nIDE read: device %d block # %ld, count %ld ... ",
225 curr_device, blk, cnt);
228 n = ide_dev_desc[curr_device].block_read(curr_device,
231 /* flush cache after read */
233 cnt * ide_dev_desc[curr_device].blksz);
235 printf("%ld blocks read: %s\n",
236 n, (n == cnt) ? "OK" : "ERROR");
241 } else if (strcmp(argv[1], "write") == 0) {
242 ulong addr = simple_strtoul(argv[2], NULL, 16);
243 ulong cnt = simple_strtoul(argv[4], NULL, 16);
246 #ifdef CONFIG_SYS_64BIT_LBA
247 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
249 printf("\nIDE write: device %d block # %lld, count %ld ... ",
250 curr_device, blk, cnt);
252 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
254 printf("\nIDE write: device %d block # %ld, count %ld ... ",
255 curr_device, blk, cnt);
257 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
259 printf("%ld blocks written: %s\n",
260 n, (n == cnt) ? "OK" : "ERROR");
266 return CMD_RET_USAGE;
273 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
275 return common_diskboot(cmdtp, "ide", argc, argv);
278 /* ------------------------------------------------------------------------- */
280 void __ide_led(uchar led, uchar status)
282 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
283 static uchar led_buffer; /* Buffer for current LED status */
285 uchar *led_port = LED_PORT;
287 if (status) /* switch LED on */
289 else /* switch LED off */
292 *led_port = led_buffer;
296 void ide_led(uchar led, uchar status)
297 __attribute__ ((weak, alias("__ide_led")));
299 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
300 # define DEVICE_LED(x) 0
305 /* ------------------------------------------------------------------------- */
307 inline void __ide_outb(int dev, int port, unsigned char val)
309 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
311 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
313 #if defined(CONFIG_IDE_AHB)
316 ide_write_register(dev, port, val);
319 outb(val, (ATA_CURR_BASE(dev)));
322 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
326 void ide_outb(int dev, int port, unsigned char val)
327 __attribute__ ((weak, alias("__ide_outb")));
329 inline unsigned char __ide_inb(int dev, int port)
333 #if defined(CONFIG_IDE_AHB)
334 val = ide_read_register(dev, port);
336 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
339 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
341 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
345 unsigned char ide_inb(int dev, int port)
346 __attribute__ ((weak, alias("__ide_inb")));
348 #ifdef CONFIG_TUNE_PIO
349 inline int __ide_set_piomode(int pio_mode)
354 inline int ide_set_piomode(int pio_mode)
355 __attribute__ ((weak, alias("__ide_set_piomode")));
363 #ifdef CONFIG_IDE_8xx_PCCARD
364 extern int ide_devices_found; /* Initialized in check_ide_device() */
365 #endif /* CONFIG_IDE_8xx_PCCARD */
367 #ifdef CONFIG_IDE_PREINIT
371 puts("ide_preinit failed\n");
374 #endif /* CONFIG_IDE_PREINIT */
379 * Reset the IDE just to be sure.
380 * Light LED's to show
382 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
384 /* ATAPI Drives seems to need a proper IDE Reset */
387 #ifdef CONFIG_IDE_INIT_POSTRESET
390 if (ide_init_postreset()) {
391 puts("ide_preinit_postreset failed\n");
394 #endif /* CONFIG_IDE_INIT_POSTRESET */
397 * Wait for IDE to get ready.
398 * According to spec, this can take up to 31 seconds!
400 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
402 bus * (CONFIG_SYS_IDE_MAXDEVICE /
403 CONFIG_SYS_IDE_MAXBUS);
405 #ifdef CONFIG_IDE_8xx_PCCARD
406 /* Skip non-ide devices from probing */
407 if ((ide_devices_found & (1 << bus)) == 0) {
408 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
412 printf("Bus %d: ", bus);
418 udelay(100000); /* 100 ms */
419 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
420 udelay(100000); /* 100 ms */
423 udelay(10000); /* 10 ms */
425 c = ide_inb(dev, ATA_STATUS);
427 if (i > (ATA_RESET_TIME * 100)) {
428 puts("** Timeout **\n");
430 ide_led((LED_IDE1 | LED_IDE2), 0);
433 if ((i >= 100) && ((i % 100) == 0))
436 } while (c & ATA_STAT_BUSY);
438 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
439 puts("not available ");
440 debug("Status = 0x%02X ", c);
441 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
442 } else if ((c & ATA_STAT_READY) == 0) {
443 puts("not available ");
444 debug("Status = 0x%02X ", c);
455 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
458 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
459 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
460 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
461 ide_dev_desc[i].if_type = IF_TYPE_IDE;
462 ide_dev_desc[i].dev = i;
463 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
464 ide_dev_desc[i].blksz = 0;
465 ide_dev_desc[i].lba = 0;
466 ide_dev_desc[i].block_read = ide_read;
467 ide_dev_desc[i].block_write = ide_write;
468 if (!ide_bus_ok[IDE_BUS(i)])
470 ide_led(led, 1); /* LED on */
471 ide_ident(&ide_dev_desc[i]);
472 ide_led(led, 0); /* LED off */
473 dev_print(&ide_dev_desc[i]);
475 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
476 /* initialize partition type */
477 init_part(&ide_dev_desc[i]);
485 /* ------------------------------------------------------------------------- */
487 #ifdef CONFIG_PARTITIONS
488 block_dev_desc_t *ide_get_dev(int dev)
490 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
494 /* ------------------------------------------------------------------------- */
496 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
497 __attribute__ ((weak, alias("__ide_input_swap_data")));
499 void ide_input_data(int dev, ulong *sect_buf, int words)
500 __attribute__ ((weak, alias("__ide_input_data")));
502 void ide_output_data(int dev, const ulong *sect_buf, int words)
503 __attribute__ ((weak, alias("__ide_output_data")));
505 /* We only need to swap data if we are running on a big endian cpu. */
506 #if defined(__LITTLE_ENDIAN)
507 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
509 ide_input_data(dev, sect_buf, words);
512 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
514 volatile ushort *pbuf =
515 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
516 ushort *dbuf = (ushort *) sect_buf;
518 debug("in input swap data base for read is %lx\n",
519 (unsigned long) pbuf);
523 *dbuf++ = swab16p((u16 *) pbuf);
524 *dbuf++ = swab16p((u16 *) pbuf);
525 #elif defined(CONFIG_PCS440EP)
529 *dbuf++ = ld_le16(pbuf);
530 *dbuf++ = ld_le16(pbuf);
534 #endif /* __LITTLE_ENDIAN */
537 #if defined(CONFIG_IDE_SWAP_IO)
538 void __ide_output_data(int dev, const ulong *sect_buf, int words)
541 volatile ushort *pbuf;
543 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
544 dbuf = (ushort *) sect_buf;
546 #if defined(CONFIG_PCS440EP)
547 /* not tested, because CF was write protected */
549 *pbuf = ld_le16(dbuf++);
551 *pbuf = ld_le16(dbuf++);
560 #else /* ! CONFIG_IDE_SWAP_IO */
561 void __ide_output_data(int dev, const ulong *sect_buf, int words)
563 #if defined(CONFIG_IDE_AHB)
564 ide_write_data(dev, sect_buf, words);
566 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
569 #endif /* CONFIG_IDE_SWAP_IO */
571 #if defined(CONFIG_IDE_SWAP_IO)
572 void __ide_input_data(int dev, ulong *sect_buf, int words)
575 volatile ushort *pbuf;
577 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
578 dbuf = (ushort *) sect_buf;
580 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
583 #if defined(CONFIG_PCS440EP)
585 *dbuf++ = ld_le16(pbuf);
587 *dbuf++ = ld_le16(pbuf);
596 #else /* ! CONFIG_IDE_SWAP_IO */
597 void __ide_input_data(int dev, ulong *sect_buf, int words)
599 #if defined(CONFIG_IDE_AHB)
600 ide_read_data(dev, sect_buf, words);
602 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
606 #endif /* CONFIG_IDE_SWAP_IO */
608 /* -------------------------------------------------------------------------
610 static void ide_ident(block_dev_desc_t *dev_desc)
619 #ifdef CONFIG_TUNE_PIO
624 int mode, cycle_time;
628 device = dev_desc->dev;
629 printf(" Device %d: ", device);
631 ide_led(DEVICE_LED(device), 1); /* LED on */
634 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
635 dev_desc->if_type = IF_TYPE_IDE;
640 /* Warning: This will be tricky to read */
641 while (retries <= 1) {
642 /* check signature */
643 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
644 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
645 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
646 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
647 /* ATAPI Signature found */
648 dev_desc->if_type = IF_TYPE_ATAPI;
650 * Start Ident Command
652 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
654 * Wait for completion - ATAPI devices need more time
657 c = ide_wait(device, ATAPI_TIME_OUT);
662 * Start Ident Command
664 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
667 * Wait for completion
669 c = ide_wait(device, IDE_TIME_OUT);
671 ide_led(DEVICE_LED(device), 0); /* LED off */
673 if (((c & ATA_STAT_DRQ) == 0) ||
674 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
678 * Need to soft reset the device
679 * in case it's an ATAPI...
681 debug("Retrying...\n");
682 ide_outb(device, ATA_DEV_HD,
683 ATA_LBA | ATA_DEVICE(device));
685 ide_outb(device, ATA_COMMAND, 0x08);
686 udelay(500000); /* 500 ms */
691 ide_outb(device, ATA_DEV_HD,
692 ATA_LBA | ATA_DEVICE(device));
701 } /* see above - ugly to read */
703 if (retries == 2) /* Not found */
707 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
709 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
710 sizeof(dev_desc->revision));
711 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
712 sizeof(dev_desc->vendor));
713 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
714 sizeof(dev_desc->product));
715 #ifdef __LITTLE_ENDIAN
717 * firmware revision, model, and serial number have Big Endian Byte
718 * order in Word. Convert all three to little endian.
720 * See CF+ and CompactFlash Specification Revision 2.0:
721 * 6.2.1.6: Identify Drive, Table 39 for more details
724 strswab(dev_desc->revision);
725 strswab(dev_desc->vendor);
726 strswab(dev_desc->product);
727 #endif /* __LITTLE_ENDIAN */
729 if ((iop.config & 0x0080) == 0x0080)
730 dev_desc->removable = 1;
732 dev_desc->removable = 0;
734 #ifdef CONFIG_TUNE_PIO
735 /* Mode 0 - 2 only, are directly determined by word 51. */
738 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
739 /* Force it to dead slow, and hope for the best... */
743 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
744 * shall set bit 1 of word 53 to one and support the fields contained
745 * in words 64 through 70.
747 if (iop.field_valid & 0x02) {
749 * Mode 3 and above are possible. Check in order from slow
750 * to fast, so we wind up with the highest mode allowed.
752 if (iop.eide_pio_modes & 0x01)
754 if (iop.eide_pio_modes & 0x02)
756 if (ata_id_is_cfa((u16 *)&iop)) {
757 if ((iop.cf_advanced_caps & 0x07) == 0x01)
759 if ((iop.cf_advanced_caps & 0x07) == 0x02)
764 /* System-specific, depends on bus speeds, etc. */
765 ide_set_piomode(pio_mode);
766 #endif /* CONFIG_TUNE_PIO */
770 * Drive PIO mode autoselection
774 printf("tPIO = 0x%02x = %d\n", mode, mode);
775 if (mode > 2) { /* 2 is maximum allowed tPIO value */
777 debug("Override tPIO -> 2\n");
779 if (iop.field_valid & 2) { /* drive implements ATA2? */
780 debug("Drive implements ATA2\n");
781 if (iop.capability & 8) { /* drive supports use_iordy? */
782 cycle_time = iop.eide_pio_iordy;
784 cycle_time = iop.eide_pio;
786 debug("cycle time = %d\n", cycle_time);
788 if (cycle_time > 120)
789 mode = 3; /* 120 ns for PIO mode 4 */
790 if (cycle_time > 180)
791 mode = 2; /* 180 ns for PIO mode 3 */
792 if (cycle_time > 240)
793 mode = 1; /* 240 ns for PIO mode 4 */
794 if (cycle_time > 383)
795 mode = 0; /* 383 ns for PIO mode 4 */
797 printf("PIO mode to use: PIO %d\n", mode);
801 if (dev_desc->if_type == IF_TYPE_ATAPI) {
802 atapi_inquiry(dev_desc);
805 #endif /* CONFIG_ATAPI */
809 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
810 #else /* ! __BIG_ENDIAN */
812 * do not swap shorts on little endian
814 * See CF+ and CompactFlash Specification Revision 2.0:
815 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
817 dev_desc->lba = iop.lba_capacity;
818 #endif /* __BIG_ENDIAN */
821 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
823 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
824 ((unsigned long long) iop.lba48_capacity[1] << 16) |
825 ((unsigned long long) iop.lba48_capacity[2] << 32) |
826 ((unsigned long long) iop.lba48_capacity[3] << 48);
830 #endif /* CONFIG_LBA48 */
832 dev_desc->type = DEV_TYPE_HARDDISK;
833 dev_desc->blksz = ATA_BLOCKSIZE;
834 dev_desc->lun = 0; /* just to fill something in... */
836 #if 0 /* only used to test the powersaving mode,
837 * if enabled, the drive goes after 5 sec
839 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
840 c = ide_wait(device, IDE_TIME_OUT);
841 ide_outb(device, ATA_SECT_CNT, 1);
842 ide_outb(device, ATA_LBA_LOW, 0);
843 ide_outb(device, ATA_LBA_MID, 0);
844 ide_outb(device, ATA_LBA_HIGH, 0);
845 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
846 ide_outb(device, ATA_COMMAND, 0xe3);
848 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
853 /* ------------------------------------------------------------------------- */
855 ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
859 unsigned char pwrsave = 0; /* power save */
862 unsigned char lba48 = 0;
864 if (blknr & 0x0000fffff0000000ULL) {
865 /* more than 28 bits used, use 48bit mode */
869 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
870 device, blknr, blkcnt, (ulong) buffer);
872 ide_led(DEVICE_LED(device), 1); /* LED on */
876 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
877 c = ide_wait(device, IDE_TIME_OUT);
879 if (c & ATA_STAT_BUSY) {
880 printf("IDE read: device %d not ready\n", device);
884 /* first check if the drive is in Powersaving mode, if yes,
885 * increase the timeout value */
886 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
889 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
891 if (c & ATA_STAT_BUSY) {
892 printf("IDE read: device %d not ready\n", device);
895 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
896 printf("No Powersaving mode %X\n", c);
898 c = ide_inb(device, ATA_SECT_CNT);
899 debug("Powersaving %02X\n", c);
905 while (blkcnt-- > 0) {
907 c = ide_wait(device, IDE_TIME_OUT);
909 if (c & ATA_STAT_BUSY) {
910 printf("IDE read: device %d not ready\n", device);
915 /* write high bits */
916 ide_outb(device, ATA_SECT_CNT, 0);
917 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
918 #ifdef CONFIG_SYS_64BIT_LBA
919 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
920 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
922 ide_outb(device, ATA_LBA_MID, 0);
923 ide_outb(device, ATA_LBA_HIGH, 0);
927 ide_outb(device, ATA_SECT_CNT, 1);
928 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
929 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
930 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
934 ide_outb(device, ATA_DEV_HD,
935 ATA_LBA | ATA_DEVICE(device));
936 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
941 ide_outb(device, ATA_DEV_HD, ATA_LBA |
942 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
943 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
949 /* may take up to 4 sec */
950 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
953 /* can't take over 500 ms */
954 c = ide_wait(device, IDE_TIME_OUT);
957 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
959 #if defined(CONFIG_SYS_64BIT_LBA)
960 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
963 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
964 device, (ulong) blknr, c);
969 ide_input_data(device, buffer, ATA_SECTORWORDS);
970 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
974 buffer += ATA_BLOCKSIZE;
977 ide_led(DEVICE_LED(device), 0); /* LED off */
981 /* ------------------------------------------------------------------------- */
984 ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
990 unsigned char lba48 = 0;
992 if (blknr & 0x0000fffff0000000ULL) {
993 /* more than 28 bits used, use 48bit mode */
998 ide_led(DEVICE_LED(device), 1); /* LED on */
1002 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1004 while (blkcnt-- > 0) {
1006 c = ide_wait(device, IDE_TIME_OUT);
1008 if (c & ATA_STAT_BUSY) {
1009 printf("IDE read: device %d not ready\n", device);
1014 /* write high bits */
1015 ide_outb(device, ATA_SECT_CNT, 0);
1016 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1017 #ifdef CONFIG_SYS_64BIT_LBA
1018 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1019 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1021 ide_outb(device, ATA_LBA_MID, 0);
1022 ide_outb(device, ATA_LBA_HIGH, 0);
1026 ide_outb(device, ATA_SECT_CNT, 1);
1027 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1028 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1029 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1033 ide_outb(device, ATA_DEV_HD,
1034 ATA_LBA | ATA_DEVICE(device));
1035 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1040 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1041 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1042 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1047 /* can't take over 500 ms */
1048 c = ide_wait(device, IDE_TIME_OUT);
1050 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1052 #if defined(CONFIG_SYS_64BIT_LBA)
1053 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1056 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1057 device, (ulong) blknr, c);
1062 ide_output_data(device, buffer, ATA_SECTORWORDS);
1063 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1066 buffer += ATA_BLOCKSIZE;
1069 ide_led(DEVICE_LED(device), 0); /* LED off */
1073 /* ------------------------------------------------------------------------- */
1076 * copy src to dest, skipping leading and trailing blanks and null
1077 * terminate the string
1078 * "len" is the size of available memory including the terminating '\0'
1080 static void ident_cpy(unsigned char *dst, unsigned char *src,
1083 unsigned char *end, *last;
1086 end = src + len - 1;
1088 /* reserve space for '\0' */
1092 /* skip leading white space */
1093 while ((*src) && (src < end) && (*src == ' '))
1096 /* copy string, omitting trailing white space */
1097 while ((*src) && (src < end)) {
1106 /* ------------------------------------------------------------------------- */
1109 * Wait until Busy bit is off, or timeout (in ms)
1110 * Return last status
1112 static uchar ide_wait(int dev, ulong t)
1114 ulong delay = 10 * t; /* poll every 100 us */
1117 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1125 /* ------------------------------------------------------------------------- */
1127 #ifdef CONFIG_IDE_RESET
1128 extern void ide_set_reset(int idereset);
1130 static void ide_reset(void)
1135 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1137 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1138 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1140 ide_set_reset(1); /* assert reset */
1142 /* the reset signal shall be asserted for et least 25 us */
1147 /* de-assert RESET signal */
1151 for (i = 0; i < 250; ++i)
1155 #endif /* CONFIG_IDE_RESET */
1157 /* ------------------------------------------------------------------------- */
1159 #if defined(CONFIG_OF_IDE_FIXUP)
1160 int ide_device_present(int dev)
1162 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1164 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1167 /* ------------------------------------------------------------------------- */
1170 /****************************************************************************
1174 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1175 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1177 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1178 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1181 #if defined(CONFIG_IDE_SWAP_IO)
1182 /* since ATAPI may use commands with not 4 bytes alligned length
1183 * we have our own transfer functions, 2 bytes alligned */
1184 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1187 volatile ushort *pbuf;
1189 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1190 dbuf = (ushort *) sect_buf;
1192 debug("in output data shorts base for read is %lx\n",
1193 (unsigned long) pbuf);
1201 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1204 volatile ushort *pbuf;
1206 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1207 dbuf = (ushort *) sect_buf;
1209 debug("in input data shorts base for read is %lx\n",
1210 (unsigned long) pbuf);
1218 #else /* ! CONFIG_IDE_SWAP_IO */
1219 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1221 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1224 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1226 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1229 #endif /* CONFIG_IDE_SWAP_IO */
1232 * Wait until (Status & mask) == res, or timeout (in ms)
1233 * Return last status
1234 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1235 * and then they set their DRQ Bit
1237 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1239 ulong delay = 10 * t; /* poll every 100 us */
1242 /* prevents to read the status before valid */
1243 c = ide_inb(dev, ATA_DEV_CTL);
1245 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1246 /* break if error occurs (doesn't make sense to wait more) */
1247 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1257 * issue an atapi command
1259 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1260 unsigned char *buffer, int buflen)
1262 unsigned char c, err, mask, res;
1265 ide_led(DEVICE_LED(device), 1); /* LED on */
1269 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1271 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1272 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1273 if ((c & mask) != res) {
1274 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1279 /* write taskfile */
1280 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1281 ide_outb(device, ATA_SECT_CNT, 0);
1282 ide_outb(device, ATA_SECT_NUM, 0);
1283 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1284 ide_outb(device, ATA_CYL_HIGH,
1285 (unsigned char) ((buflen >> 8) & 0xFF));
1286 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1288 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1291 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1293 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1295 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1296 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1302 /* write command block */
1303 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1305 /* ATAPI Command written wait for completition */
1306 udelay(5000); /* device must set bsy */
1308 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1310 * if no data wait for DRQ = 0 BSY = 0
1311 * if data wait for DRQ = 1 BSY = 0
1316 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1317 if ((c & mask) != res) {
1318 if (c & ATA_STAT_ERR) {
1319 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1320 debug("atapi_issue 1 returned sense key %X status %02X\n",
1323 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1329 n = ide_inb(device, ATA_CYL_HIGH);
1331 n += ide_inb(device, ATA_CYL_LOW);
1333 printf("ERROR, transfer bytes %d requested only %d\n", n,
1338 if ((n == 0) && (buflen < 0)) {
1339 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1344 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1347 if (n != 0) { /* data transfer */
1348 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1349 /* we transfer shorts */
1351 /* ok now decide if it is an in or output */
1352 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1353 debug("Write to device\n");
1354 ide_output_data_shorts(device,
1355 (unsigned short *) buffer, n);
1357 debug("Read from device @ %p shorts %d\n", buffer, n);
1358 ide_input_data_shorts(device,
1359 (unsigned short *) buffer, n);
1362 udelay(5000); /* seems that some CD ROMs need this... */
1363 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1365 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1366 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1367 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1368 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1374 ide_led(DEVICE_LED(device), 0); /* LED off */
1379 * sending the command to atapi_issue. If an status other than good
1380 * returns, an request_sense will be issued
1383 #define ATAPI_DRIVE_NOT_READY 100
1384 #define ATAPI_UNIT_ATTN 10
1386 unsigned char atapi_issue_autoreq(int device,
1389 unsigned char *buffer, int buflen)
1391 unsigned char sense_data[18], sense_ccb[12];
1392 unsigned char res, key, asc, ascq;
1393 int notready, unitattn;
1395 unitattn = ATAPI_UNIT_ATTN;
1396 notready = ATAPI_DRIVE_NOT_READY;
1399 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1404 return 0xFF; /* error */
1406 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1408 memset(sense_ccb, 0, sizeof(sense_ccb));
1409 memset(sense_data, 0, sizeof(sense_data));
1410 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1411 sense_ccb[4] = 18; /* allocation Length */
1413 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1414 key = (sense_data[2] & 0xF);
1415 asc = (sense_data[12]);
1416 ascq = (sense_data[13]);
1418 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1419 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1420 sense_data[0], key, asc, ascq);
1423 return 0; /* ok device ready */
1425 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1426 if (unitattn-- > 0) {
1430 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1433 if ((asc == 0x4) && (ascq == 0x1)) {
1434 /* not ready, but will be ready soon */
1435 if (notready-- > 0) {
1439 printf("Drive not ready, tried %d times\n",
1440 ATAPI_DRIVE_NOT_READY);
1444 debug("Media not present\n");
1448 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1451 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1456 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1458 unsigned char ccb[12]; /* Command descriptor block */
1459 unsigned char iobuf[64]; /* temp buf */
1463 device = dev_desc->dev;
1464 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1465 dev_desc->block_read = atapi_read;
1467 memset(ccb, 0, sizeof(ccb));
1468 memset(iobuf, 0, sizeof(iobuf));
1470 ccb[0] = ATAPI_CMD_INQUIRY;
1471 ccb[4] = 40; /* allocation Legnth */
1472 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1474 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1478 /* copy device ident strings */
1479 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1480 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1481 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1485 dev_desc->blksz = 0;
1486 dev_desc->type = iobuf[0] & 0x1f;
1488 if ((iobuf[1] & 0x80) == 0x80)
1489 dev_desc->removable = 1;
1491 dev_desc->removable = 0;
1493 memset(ccb, 0, sizeof(ccb));
1494 memset(iobuf, 0, sizeof(iobuf));
1495 ccb[0] = ATAPI_CMD_START_STOP;
1496 ccb[4] = 0x03; /* start */
1498 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1500 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1504 memset(ccb, 0, sizeof(ccb));
1505 memset(iobuf, 0, sizeof(iobuf));
1506 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1508 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1512 memset(ccb, 0, sizeof(ccb));
1513 memset(iobuf, 0, sizeof(iobuf));
1514 ccb[0] = ATAPI_CMD_READ_CAP;
1515 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1516 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1520 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1521 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1522 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1524 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1525 ((unsigned long) iobuf[1] << 16) +
1526 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1527 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1528 ((unsigned long) iobuf[5] << 16) +
1529 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1531 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1532 dev_desc->lba48 = 0;
1540 * we transfer only one block per command, since the multiple DRQ per
1541 * command is not yet implemented
1543 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1544 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1545 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1547 ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1550 unsigned char ccb[12]; /* Command descriptor block */
1553 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1554 device, blknr, blkcnt, (ulong) buffer);
1557 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1558 cnt = ATAPI_READ_MAX_BLOCK;
1562 ccb[0] = ATAPI_CMD_READ_12;
1563 ccb[1] = 0; /* reserved */
1564 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1565 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1566 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1567 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1568 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1569 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1570 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1571 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1572 ccb[10] = 0; /* reserved */
1573 ccb[11] = 0; /* reserved */
1575 if (atapi_issue_autoreq(device, ccb, 12,
1576 (unsigned char *) buffer,
1577 cnt * ATAPI_READ_BLOCK_SIZE)
1584 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1585 } while (blkcnt > 0);
1589 /* ------------------------------------------------------------------------- */
1591 #endif /* CONFIG_ATAPI */
1593 U_BOOT_CMD(ide, 5, 1, do_ide,
1595 "reset - reset IDE controller\n"
1596 "ide info - show available IDE devices\n"
1597 "ide device [dev] - show or set current device\n"
1598 "ide part [dev] - print partition table of one or all IDE devices\n"
1599 "ide read addr blk# cnt\n"
1600 "ide write addr blk# cnt - read/write `cnt'"
1601 " blocks starting at block `blk#'\n"
1602 " to/from memory address `addr'");
1604 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1605 "boot from IDE device", "loadAddr dev:part");