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 /* ------------------------------------------------------------------------- */
85 # if !defined(CONFIG_BMS2003) && \
86 !defined(CONFIG_CPC45) && \
87 !defined(CONFIG_KUP4K) && \
88 !defined(CONFIG_KUP4X)
89 static void ide_led (uchar led, uchar status);
91 extern void ide_led (uchar led, uchar status);
94 #define ide_led(a,b) /* dummy */
97 #ifdef CONFIG_IDE_RESET
98 static void ide_reset (void);
100 #define ide_reset() /* dummy */
103 static void ide_ident (block_dev_desc_t *dev_desc);
104 static uchar ide_wait (int dev, ulong t);
106 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
108 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
110 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
112 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
114 #ifndef CONFIG_SYS_ATA_PORT_ADDR
115 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
119 static void atapi_inquiry(block_dev_desc_t *dev_desc);
120 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
124 /* ------------------------------------------------------------------------- */
126 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
133 return CMD_RET_USAGE;
135 if (strncmp(argv[1], "res", 3) == 0) {
137 #ifdef CONFIG_IDE_8xx_DIRECT
138 " on PCMCIA " PCMCIA_SLOT_MSG
144 } else if (strncmp(argv[1], "inf", 3) == 0) {
149 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
150 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
151 continue; /* list only known devices */
152 printf("IDE device %d: ", i);
153 dev_print(&ide_dev_desc[i]);
157 } else if (strncmp(argv[1], "dev", 3) == 0) {
158 if ((curr_device < 0)
159 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
160 puts("\nno IDE devices available\n");
163 printf("\nIDE device %d: ", curr_device);
164 dev_print(&ide_dev_desc[curr_device]);
166 } else if (strncmp(argv[1], "part", 4) == 0) {
169 for (ok = 0, dev = 0;
170 dev < CONFIG_SYS_IDE_MAXDEVICE;
172 if (ide_dev_desc[dev].part_type !=
177 print_part(&ide_dev_desc[dev]);
181 puts("\nno IDE devices available\n");
186 return CMD_RET_USAGE;
188 if (strncmp(argv[1], "dev", 3) == 0) {
189 int dev = (int) simple_strtoul(argv[2], NULL, 10);
191 printf("\nIDE device %d: ", dev);
192 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
193 puts("unknown device\n");
196 dev_print(&ide_dev_desc[dev]);
197 /*ide_print (dev); */
199 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
204 puts("... is now current device\n");
207 } else if (strncmp(argv[1], "part", 4) == 0) {
208 int dev = (int) simple_strtoul(argv[2], NULL, 10);
210 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
211 print_part(&ide_dev_desc[dev]);
213 printf("\nIDE device %d not available\n",
220 return CMD_RET_USAGE;
222 /* at least 4 args */
224 if (strcmp(argv[1], "read") == 0) {
225 ulong addr = simple_strtoul(argv[2], NULL, 16);
226 ulong cnt = simple_strtoul(argv[4], NULL, 16);
229 #ifdef CONFIG_SYS_64BIT_LBA
230 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
232 printf("\nIDE read: device %d block # %lld, count %ld ... ",
233 curr_device, blk, cnt);
235 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
237 printf("\nIDE read: device %d block # %ld, count %ld ... ",
238 curr_device, blk, cnt);
241 n = ide_dev_desc[curr_device].block_read(curr_device,
244 /* flush cache after read */
246 cnt * ide_dev_desc[curr_device].blksz);
248 printf("%ld blocks read: %s\n",
249 n, (n == cnt) ? "OK" : "ERROR");
254 } else if (strcmp(argv[1], "write") == 0) {
255 ulong addr = simple_strtoul(argv[2], NULL, 16);
256 ulong cnt = simple_strtoul(argv[4], NULL, 16);
259 #ifdef CONFIG_SYS_64BIT_LBA
260 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
262 printf("\nIDE write: device %d block # %lld, count %ld ... ",
263 curr_device, blk, cnt);
265 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
267 printf("\nIDE write: device %d block # %ld, count %ld ... ",
268 curr_device, blk, cnt);
270 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
272 printf("%ld blocks written: %s\n",
273 n, (n == cnt) ? "OK" : "ERROR");
279 return CMD_RET_USAGE;
286 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
288 return common_diskboot(cmdtp, "ide", argc, argv);
291 /* ------------------------------------------------------------------------- */
293 inline void __ide_outb(int dev, int port, unsigned char val)
295 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
297 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
299 #if defined(CONFIG_IDE_AHB)
302 ide_write_register(dev, port, val);
305 outb(val, (ATA_CURR_BASE(dev)));
308 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
312 void ide_outb(int dev, int port, unsigned char val)
313 __attribute__ ((weak, alias("__ide_outb")));
315 inline unsigned char __ide_inb(int dev, int port)
319 #if defined(CONFIG_IDE_AHB)
320 val = ide_read_register(dev, port);
322 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
325 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
327 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
331 unsigned char ide_inb(int dev, int port)
332 __attribute__ ((weak, alias("__ide_inb")));
334 #ifdef CONFIG_TUNE_PIO
335 inline int __ide_set_piomode(int pio_mode)
340 inline int ide_set_piomode(int pio_mode)
341 __attribute__ ((weak, alias("__ide_set_piomode")));
349 #ifdef CONFIG_IDE_8xx_PCCARD
350 extern int ide_devices_found; /* Initialized in check_ide_device() */
351 #endif /* CONFIG_IDE_8xx_PCCARD */
353 #ifdef CONFIG_IDE_PREINIT
357 puts("ide_preinit failed\n");
360 #endif /* CONFIG_IDE_PREINIT */
365 * Reset the IDE just to be sure.
366 * Light LED's to show
368 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
370 /* ATAPI Drives seems to need a proper IDE Reset */
373 #ifdef CONFIG_IDE_INIT_POSTRESET
376 if (ide_init_postreset()) {
377 puts("ide_preinit_postreset failed\n");
380 #endif /* CONFIG_IDE_INIT_POSTRESET */
383 * Wait for IDE to get ready.
384 * According to spec, this can take up to 31 seconds!
386 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
388 bus * (CONFIG_SYS_IDE_MAXDEVICE /
389 CONFIG_SYS_IDE_MAXBUS);
391 #ifdef CONFIG_IDE_8xx_PCCARD
392 /* Skip non-ide devices from probing */
393 if ((ide_devices_found & (1 << bus)) == 0) {
394 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
398 printf("Bus %d: ", bus);
404 udelay(100000); /* 100 ms */
405 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
406 udelay(100000); /* 100 ms */
409 udelay(10000); /* 10 ms */
411 c = ide_inb(dev, ATA_STATUS);
413 if (i > (ATA_RESET_TIME * 100)) {
414 puts("** Timeout **\n");
416 ide_led((LED_IDE1 | LED_IDE2), 0);
419 if ((i >= 100) && ((i % 100) == 0))
422 } while (c & ATA_STAT_BUSY);
424 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
425 puts("not available ");
426 debug("Status = 0x%02X ", c);
427 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
428 } else if ((c & ATA_STAT_READY) == 0) {
429 puts("not available ");
430 debug("Status = 0x%02X ", c);
441 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
444 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
445 #ifdef CONFIG_IDE_LED
446 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
448 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
449 ide_dev_desc[i].if_type = IF_TYPE_IDE;
450 ide_dev_desc[i].dev = i;
451 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
452 ide_dev_desc[i].blksz = 0;
453 ide_dev_desc[i].lba = 0;
454 ide_dev_desc[i].block_read = ide_read;
455 ide_dev_desc[i].block_write = ide_write;
456 if (!ide_bus_ok[IDE_BUS(i)])
458 ide_led(led, 1); /* LED on */
459 ide_ident(&ide_dev_desc[i]);
460 ide_led(led, 0); /* LED off */
461 dev_print(&ide_dev_desc[i]);
463 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
464 /* initialize partition type */
465 init_part(&ide_dev_desc[i]);
473 /* ------------------------------------------------------------------------- */
475 #ifdef CONFIG_PARTITIONS
476 block_dev_desc_t *ide_get_dev(int dev)
478 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
482 /* ------------------------------------------------------------------------- */
484 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
485 __attribute__ ((weak, alias("__ide_input_swap_data")));
487 void ide_input_data(int dev, ulong *sect_buf, int words)
488 __attribute__ ((weak, alias("__ide_input_data")));
490 void ide_output_data(int dev, const ulong *sect_buf, int words)
491 __attribute__ ((weak, alias("__ide_output_data")));
493 /* We only need to swap data if we are running on a big endian cpu. */
494 /* But Au1x00 cpu:s already swaps data in big endian mode! */
495 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
496 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
498 ide_input_data(dev, sect_buf, words);
501 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
503 volatile ushort *pbuf =
504 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
505 ushort *dbuf = (ushort *) sect_buf;
507 debug("in input swap data base for read is %lx\n",
508 (unsigned long) pbuf);
512 *dbuf++ = swab16p((u16 *) pbuf);
513 *dbuf++ = swab16p((u16 *) pbuf);
514 #elif defined(CONFIG_PCS440EP)
518 *dbuf++ = ld_le16(pbuf);
519 *dbuf++ = ld_le16(pbuf);
523 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
526 #if defined(CONFIG_IDE_SWAP_IO)
527 void __ide_output_data(int dev, const ulong *sect_buf, int words)
530 volatile ushort *pbuf;
532 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
533 dbuf = (ushort *) sect_buf;
535 #if defined(CONFIG_PCS440EP)
536 /* not tested, because CF was write protected */
538 *pbuf = ld_le16(dbuf++);
540 *pbuf = ld_le16(dbuf++);
549 #else /* ! CONFIG_IDE_SWAP_IO */
550 void __ide_output_data(int dev, const ulong *sect_buf, int words)
552 #if defined(CONFIG_IDE_AHB)
553 ide_write_data(dev, sect_buf, words);
555 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
558 #endif /* CONFIG_IDE_SWAP_IO */
560 #if defined(CONFIG_IDE_SWAP_IO)
561 void __ide_input_data(int dev, ulong *sect_buf, int words)
564 volatile ushort *pbuf;
566 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
567 dbuf = (ushort *) sect_buf;
569 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
572 #if defined(CONFIG_PCS440EP)
574 *dbuf++ = ld_le16(pbuf);
576 *dbuf++ = ld_le16(pbuf);
585 #else /* ! CONFIG_IDE_SWAP_IO */
586 void __ide_input_data(int dev, ulong *sect_buf, int words)
588 #if defined(CONFIG_IDE_AHB)
589 ide_read_data(dev, sect_buf, words);
591 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
595 #endif /* CONFIG_IDE_SWAP_IO */
597 /* -------------------------------------------------------------------------
599 static void ide_ident(block_dev_desc_t *dev_desc)
608 #ifdef CONFIG_TUNE_PIO
613 int mode, cycle_time;
617 device = dev_desc->dev;
618 printf(" Device %d: ", device);
620 ide_led(DEVICE_LED(device), 1); /* LED on */
623 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
624 dev_desc->if_type = IF_TYPE_IDE;
629 /* Warning: This will be tricky to read */
630 while (retries <= 1) {
631 /* check signature */
632 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
633 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
634 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
635 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
636 /* ATAPI Signature found */
637 dev_desc->if_type = IF_TYPE_ATAPI;
639 * Start Ident Command
641 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
643 * Wait for completion - ATAPI devices need more time
646 c = ide_wait(device, ATAPI_TIME_OUT);
651 * Start Ident Command
653 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
656 * Wait for completion
658 c = ide_wait(device, IDE_TIME_OUT);
660 ide_led(DEVICE_LED(device), 0); /* LED off */
662 if (((c & ATA_STAT_DRQ) == 0) ||
663 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
667 * Need to soft reset the device
668 * in case it's an ATAPI...
670 debug("Retrying...\n");
671 ide_outb(device, ATA_DEV_HD,
672 ATA_LBA | ATA_DEVICE(device));
674 ide_outb(device, ATA_COMMAND, 0x08);
675 udelay(500000); /* 500 ms */
680 ide_outb(device, ATA_DEV_HD,
681 ATA_LBA | ATA_DEVICE(device));
690 } /* see above - ugly to read */
692 if (retries == 2) /* Not found */
696 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
698 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
699 sizeof(dev_desc->revision));
700 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
701 sizeof(dev_desc->vendor));
702 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
703 sizeof(dev_desc->product));
704 #ifdef __LITTLE_ENDIAN
706 * firmware revision, model, and serial number have Big Endian Byte
707 * order in Word. Convert all three to little endian.
709 * See CF+ and CompactFlash Specification Revision 2.0:
710 * 6.2.1.6: Identify Drive, Table 39 for more details
713 strswab(dev_desc->revision);
714 strswab(dev_desc->vendor);
715 strswab(dev_desc->product);
716 #endif /* __LITTLE_ENDIAN */
718 if ((iop.config & 0x0080) == 0x0080)
719 dev_desc->removable = 1;
721 dev_desc->removable = 0;
723 #ifdef CONFIG_TUNE_PIO
724 /* Mode 0 - 2 only, are directly determined by word 51. */
727 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
728 /* Force it to dead slow, and hope for the best... */
732 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
733 * shall set bit 1 of word 53 to one and support the fields contained
734 * in words 64 through 70.
736 if (iop.field_valid & 0x02) {
738 * Mode 3 and above are possible. Check in order from slow
739 * to fast, so we wind up with the highest mode allowed.
741 if (iop.eide_pio_modes & 0x01)
743 if (iop.eide_pio_modes & 0x02)
745 if (ata_id_is_cfa((u16 *)&iop)) {
746 if ((iop.cf_advanced_caps & 0x07) == 0x01)
748 if ((iop.cf_advanced_caps & 0x07) == 0x02)
753 /* System-specific, depends on bus speeds, etc. */
754 ide_set_piomode(pio_mode);
755 #endif /* CONFIG_TUNE_PIO */
759 * Drive PIO mode autoselection
763 printf("tPIO = 0x%02x = %d\n", mode, mode);
764 if (mode > 2) { /* 2 is maximum allowed tPIO value */
766 debug("Override tPIO -> 2\n");
768 if (iop.field_valid & 2) { /* drive implements ATA2? */
769 debug("Drive implements ATA2\n");
770 if (iop.capability & 8) { /* drive supports use_iordy? */
771 cycle_time = iop.eide_pio_iordy;
773 cycle_time = iop.eide_pio;
775 debug("cycle time = %d\n", cycle_time);
777 if (cycle_time > 120)
778 mode = 3; /* 120 ns for PIO mode 4 */
779 if (cycle_time > 180)
780 mode = 2; /* 180 ns for PIO mode 3 */
781 if (cycle_time > 240)
782 mode = 1; /* 240 ns for PIO mode 4 */
783 if (cycle_time > 383)
784 mode = 0; /* 383 ns for PIO mode 4 */
786 printf("PIO mode to use: PIO %d\n", mode);
790 if (dev_desc->if_type == IF_TYPE_ATAPI) {
791 atapi_inquiry(dev_desc);
794 #endif /* CONFIG_ATAPI */
798 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
799 #else /* ! __BIG_ENDIAN */
801 * do not swap shorts on little endian
803 * See CF+ and CompactFlash Specification Revision 2.0:
804 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
806 dev_desc->lba = iop.lba_capacity;
807 #endif /* __BIG_ENDIAN */
810 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
812 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
813 ((unsigned long long) iop.lba48_capacity[1] << 16) |
814 ((unsigned long long) iop.lba48_capacity[2] << 32) |
815 ((unsigned long long) iop.lba48_capacity[3] << 48);
819 #endif /* CONFIG_LBA48 */
821 dev_desc->type = DEV_TYPE_HARDDISK;
822 dev_desc->blksz = ATA_BLOCKSIZE;
823 dev_desc->lun = 0; /* just to fill something in... */
825 #if 0 /* only used to test the powersaving mode,
826 * if enabled, the drive goes after 5 sec
828 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
829 c = ide_wait(device, IDE_TIME_OUT);
830 ide_outb(device, ATA_SECT_CNT, 1);
831 ide_outb(device, ATA_LBA_LOW, 0);
832 ide_outb(device, ATA_LBA_MID, 0);
833 ide_outb(device, ATA_LBA_HIGH, 0);
834 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
835 ide_outb(device, ATA_COMMAND, 0xe3);
837 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
842 /* ------------------------------------------------------------------------- */
844 ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
848 unsigned char pwrsave = 0; /* power save */
851 unsigned char lba48 = 0;
853 if (blknr & 0x0000fffff0000000ULL) {
854 /* more than 28 bits used, use 48bit mode */
858 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
859 device, blknr, blkcnt, (ulong) buffer);
861 ide_led(DEVICE_LED(device), 1); /* LED on */
865 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
866 c = ide_wait(device, IDE_TIME_OUT);
868 if (c & ATA_STAT_BUSY) {
869 printf("IDE read: device %d not ready\n", device);
873 /* first check if the drive is in Powersaving mode, if yes,
874 * increase the timeout value */
875 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
878 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
880 if (c & ATA_STAT_BUSY) {
881 printf("IDE read: device %d not ready\n", device);
884 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
885 printf("No Powersaving mode %X\n", c);
887 c = ide_inb(device, ATA_SECT_CNT);
888 debug("Powersaving %02X\n", c);
894 while (blkcnt-- > 0) {
896 c = ide_wait(device, IDE_TIME_OUT);
898 if (c & ATA_STAT_BUSY) {
899 printf("IDE read: device %d not ready\n", device);
904 /* write high bits */
905 ide_outb(device, ATA_SECT_CNT, 0);
906 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
907 #ifdef CONFIG_SYS_64BIT_LBA
908 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
909 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
911 ide_outb(device, ATA_LBA_MID, 0);
912 ide_outb(device, ATA_LBA_HIGH, 0);
916 ide_outb(device, ATA_SECT_CNT, 1);
917 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
918 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
919 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
923 ide_outb(device, ATA_DEV_HD,
924 ATA_LBA | ATA_DEVICE(device));
925 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
930 ide_outb(device, ATA_DEV_HD, ATA_LBA |
931 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
932 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
938 /* may take up to 4 sec */
939 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
942 /* can't take over 500 ms */
943 c = ide_wait(device, IDE_TIME_OUT);
946 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
948 #if defined(CONFIG_SYS_64BIT_LBA)
949 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
952 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
953 device, (ulong) blknr, c);
958 ide_input_data(device, buffer, ATA_SECTORWORDS);
959 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
963 buffer += ATA_BLOCKSIZE;
966 ide_led(DEVICE_LED(device), 0); /* LED off */
970 /* ------------------------------------------------------------------------- */
973 ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
979 unsigned char lba48 = 0;
981 if (blknr & 0x0000fffff0000000ULL) {
982 /* more than 28 bits used, use 48bit mode */
987 ide_led(DEVICE_LED(device), 1); /* LED on */
991 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
993 while (blkcnt-- > 0) {
995 c = ide_wait(device, IDE_TIME_OUT);
997 if (c & ATA_STAT_BUSY) {
998 printf("IDE read: device %d not ready\n", device);
1003 /* write high bits */
1004 ide_outb(device, ATA_SECT_CNT, 0);
1005 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1006 #ifdef CONFIG_SYS_64BIT_LBA
1007 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1008 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1010 ide_outb(device, ATA_LBA_MID, 0);
1011 ide_outb(device, ATA_LBA_HIGH, 0);
1015 ide_outb(device, ATA_SECT_CNT, 1);
1016 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1017 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1018 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1022 ide_outb(device, ATA_DEV_HD,
1023 ATA_LBA | ATA_DEVICE(device));
1024 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1029 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1030 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1031 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1036 /* can't take over 500 ms */
1037 c = ide_wait(device, IDE_TIME_OUT);
1039 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1041 #if defined(CONFIG_SYS_64BIT_LBA)
1042 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1045 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1046 device, (ulong) blknr, c);
1051 ide_output_data(device, buffer, ATA_SECTORWORDS);
1052 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1055 buffer += ATA_BLOCKSIZE;
1058 ide_led(DEVICE_LED(device), 0); /* LED off */
1062 /* ------------------------------------------------------------------------- */
1065 * copy src to dest, skipping leading and trailing blanks and null
1066 * terminate the string
1067 * "len" is the size of available memory including the terminating '\0'
1069 static void ident_cpy(unsigned char *dst, unsigned char *src,
1072 unsigned char *end, *last;
1075 end = src + len - 1;
1077 /* reserve space for '\0' */
1081 /* skip leading white space */
1082 while ((*src) && (src < end) && (*src == ' '))
1085 /* copy string, omitting trailing white space */
1086 while ((*src) && (src < end)) {
1095 /* ------------------------------------------------------------------------- */
1098 * Wait until Busy bit is off, or timeout (in ms)
1099 * Return last status
1101 static uchar ide_wait(int dev, ulong t)
1103 ulong delay = 10 * t; /* poll every 100 us */
1106 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1114 /* ------------------------------------------------------------------------- */
1116 #ifdef CONFIG_IDE_RESET
1117 extern void ide_set_reset(int idereset);
1119 static void ide_reset(void)
1124 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1126 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1127 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1129 ide_set_reset(1); /* assert reset */
1131 /* the reset signal shall be asserted for et least 25 us */
1136 /* de-assert RESET signal */
1140 for (i = 0; i < 250; ++i)
1144 #endif /* CONFIG_IDE_RESET */
1146 /* ------------------------------------------------------------------------- */
1148 #if defined(CONFIG_IDE_LED) && \
1149 !defined(CONFIG_CPC45) && \
1150 !defined(CONFIG_KUP4K) && \
1151 !defined(CONFIG_KUP4X)
1153 static uchar led_buffer; /* Buffer for current LED status */
1155 static void ide_led(uchar led, uchar status)
1157 uchar *led_port = LED_PORT;
1159 if (status) /* switch LED on */
1161 else /* switch LED off */
1164 *led_port = led_buffer;
1167 #endif /* CONFIG_IDE_LED */
1169 #if defined(CONFIG_OF_IDE_FIXUP)
1170 int ide_device_present(int dev)
1172 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1174 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1177 /* ------------------------------------------------------------------------- */
1180 /****************************************************************************
1184 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1185 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1187 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1188 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1191 #if defined(CONFIG_IDE_SWAP_IO)
1192 /* since ATAPI may use commands with not 4 bytes alligned length
1193 * we have our own transfer functions, 2 bytes alligned */
1194 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1197 volatile ushort *pbuf;
1199 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1200 dbuf = (ushort *) sect_buf;
1202 debug("in output data shorts base for read is %lx\n",
1203 (unsigned long) pbuf);
1211 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1214 volatile ushort *pbuf;
1216 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1217 dbuf = (ushort *) sect_buf;
1219 debug("in input data shorts base for read is %lx\n",
1220 (unsigned long) pbuf);
1228 #else /* ! CONFIG_IDE_SWAP_IO */
1229 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1231 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1234 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1236 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1239 #endif /* CONFIG_IDE_SWAP_IO */
1242 * Wait until (Status & mask) == res, or timeout (in ms)
1243 * Return last status
1244 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1245 * and then they set their DRQ Bit
1247 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1249 ulong delay = 10 * t; /* poll every 100 us */
1252 /* prevents to read the status before valid */
1253 c = ide_inb(dev, ATA_DEV_CTL);
1255 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1256 /* break if error occurs (doesn't make sense to wait more) */
1257 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1267 * issue an atapi command
1269 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1270 unsigned char *buffer, int buflen)
1272 unsigned char c, err, mask, res;
1275 ide_led(DEVICE_LED(device), 1); /* LED on */
1279 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1281 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1282 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1283 if ((c & mask) != res) {
1284 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1289 /* write taskfile */
1290 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1291 ide_outb(device, ATA_SECT_CNT, 0);
1292 ide_outb(device, ATA_SECT_NUM, 0);
1293 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1294 ide_outb(device, ATA_CYL_HIGH,
1295 (unsigned char) ((buflen >> 8) & 0xFF));
1296 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1298 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1301 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1303 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1305 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1306 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1312 /* write command block */
1313 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1315 /* ATAPI Command written wait for completition */
1316 udelay(5000); /* device must set bsy */
1318 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1320 * if no data wait for DRQ = 0 BSY = 0
1321 * if data wait for DRQ = 1 BSY = 0
1326 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1327 if ((c & mask) != res) {
1328 if (c & ATA_STAT_ERR) {
1329 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1330 debug("atapi_issue 1 returned sense key %X status %02X\n",
1333 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1339 n = ide_inb(device, ATA_CYL_HIGH);
1341 n += ide_inb(device, ATA_CYL_LOW);
1343 printf("ERROR, transfer bytes %d requested only %d\n", n,
1348 if ((n == 0) && (buflen < 0)) {
1349 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1354 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1357 if (n != 0) { /* data transfer */
1358 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1359 /* we transfer shorts */
1361 /* ok now decide if it is an in or output */
1362 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1363 debug("Write to device\n");
1364 ide_output_data_shorts(device,
1365 (unsigned short *) buffer, n);
1367 debug("Read from device @ %p shorts %d\n", buffer, n);
1368 ide_input_data_shorts(device,
1369 (unsigned short *) buffer, n);
1372 udelay(5000); /* seems that some CD ROMs need this... */
1373 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1375 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1376 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1377 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1378 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1384 ide_led(DEVICE_LED(device), 0); /* LED off */
1389 * sending the command to atapi_issue. If an status other than good
1390 * returns, an request_sense will be issued
1393 #define ATAPI_DRIVE_NOT_READY 100
1394 #define ATAPI_UNIT_ATTN 10
1396 unsigned char atapi_issue_autoreq(int device,
1399 unsigned char *buffer, int buflen)
1401 unsigned char sense_data[18], sense_ccb[12];
1402 unsigned char res, key, asc, ascq;
1403 int notready, unitattn;
1405 unitattn = ATAPI_UNIT_ATTN;
1406 notready = ATAPI_DRIVE_NOT_READY;
1409 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1414 return 0xFF; /* error */
1416 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1418 memset(sense_ccb, 0, sizeof(sense_ccb));
1419 memset(sense_data, 0, sizeof(sense_data));
1420 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1421 sense_ccb[4] = 18; /* allocation Length */
1423 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1424 key = (sense_data[2] & 0xF);
1425 asc = (sense_data[12]);
1426 ascq = (sense_data[13]);
1428 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1429 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1430 sense_data[0], key, asc, ascq);
1433 return 0; /* ok device ready */
1435 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1436 if (unitattn-- > 0) {
1440 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1443 if ((asc == 0x4) && (ascq == 0x1)) {
1444 /* not ready, but will be ready soon */
1445 if (notready-- > 0) {
1449 printf("Drive not ready, tried %d times\n",
1450 ATAPI_DRIVE_NOT_READY);
1454 debug("Media not present\n");
1458 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1461 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1466 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1468 unsigned char ccb[12]; /* Command descriptor block */
1469 unsigned char iobuf[64]; /* temp buf */
1473 device = dev_desc->dev;
1474 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1475 dev_desc->block_read = atapi_read;
1477 memset(ccb, 0, sizeof(ccb));
1478 memset(iobuf, 0, sizeof(iobuf));
1480 ccb[0] = ATAPI_CMD_INQUIRY;
1481 ccb[4] = 40; /* allocation Legnth */
1482 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1484 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1488 /* copy device ident strings */
1489 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1490 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1491 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1495 dev_desc->blksz = 0;
1496 dev_desc->type = iobuf[0] & 0x1f;
1498 if ((iobuf[1] & 0x80) == 0x80)
1499 dev_desc->removable = 1;
1501 dev_desc->removable = 0;
1503 memset(ccb, 0, sizeof(ccb));
1504 memset(iobuf, 0, sizeof(iobuf));
1505 ccb[0] = ATAPI_CMD_START_STOP;
1506 ccb[4] = 0x03; /* start */
1508 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1510 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1514 memset(ccb, 0, sizeof(ccb));
1515 memset(iobuf, 0, sizeof(iobuf));
1516 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1518 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1522 memset(ccb, 0, sizeof(ccb));
1523 memset(iobuf, 0, sizeof(iobuf));
1524 ccb[0] = ATAPI_CMD_READ_CAP;
1525 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1526 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1530 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1531 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1532 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1534 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1535 ((unsigned long) iobuf[1] << 16) +
1536 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1537 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1538 ((unsigned long) iobuf[5] << 16) +
1539 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1541 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1542 dev_desc->lba48 = 0;
1550 * we transfer only one block per command, since the multiple DRQ per
1551 * command is not yet implemented
1553 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1554 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1555 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1557 ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1560 unsigned char ccb[12]; /* Command descriptor block */
1563 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1564 device, blknr, blkcnt, (ulong) buffer);
1567 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1568 cnt = ATAPI_READ_MAX_BLOCK;
1572 ccb[0] = ATAPI_CMD_READ_12;
1573 ccb[1] = 0; /* reserved */
1574 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1575 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1576 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1577 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1578 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1579 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1580 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1581 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1582 ccb[10] = 0; /* reserved */
1583 ccb[11] = 0; /* reserved */
1585 if (atapi_issue_autoreq(device, ccb, 12,
1586 (unsigned char *) buffer,
1587 cnt * ATAPI_READ_BLOCK_SIZE)
1594 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1595 } while (blkcnt > 0);
1599 /* ------------------------------------------------------------------------- */
1601 #endif /* CONFIG_ATAPI */
1603 U_BOOT_CMD(ide, 5, 1, do_ide,
1605 "reset - reset IDE controller\n"
1606 "ide info - show available IDE devices\n"
1607 "ide device [dev] - show or set current device\n"
1608 "ide part [dev] - print partition table of one or all IDE devices\n"
1609 "ide read addr blk# cnt\n"
1610 "ide write addr blk# cnt - read/write `cnt'"
1611 " blocks starting at block `blk#'\n"
1612 " to/from memory address `addr'");
1614 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1615 "boot from IDE device", "loadAddr dev:part");