2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
17 #include <asm/byteorder.h>
20 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
27 #ifdef CONFIG_STATUS_LED
28 # include <status_led.h>
32 # define EIEIO __asm__ volatile ("eieio")
33 # define SYNC __asm__ volatile ("sync")
35 # define EIEIO /* nothing */
36 # define SYNC /* nothing */
39 /* ------------------------------------------------------------------------- */
41 /* Current I/O Device */
42 static int curr_device = -1;
44 /* Current offset for IDE0 / IDE1 bus access */
45 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
46 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
47 CONFIG_SYS_ATA_IDE0_OFFSET,
49 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
50 CONFIG_SYS_ATA_IDE1_OFFSET,
54 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
56 block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
57 /* ------------------------------------------------------------------------- */
59 #ifdef CONFIG_IDE_RESET
60 static void ide_reset (void);
62 #define ide_reset() /* dummy */
65 static void ide_ident (block_dev_desc_t *dev_desc);
66 static uchar ide_wait (int dev, ulong t);
68 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
70 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
72 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
74 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
76 #ifndef CONFIG_SYS_ATA_PORT_ADDR
77 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
81 static void atapi_inquiry(block_dev_desc_t *dev_desc);
82 static ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt,
87 /* ------------------------------------------------------------------------- */
89 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
98 if (strncmp(argv[1], "res", 3) == 0) {
100 #ifdef CONFIG_IDE_8xx_DIRECT
101 " on PCMCIA " PCMCIA_SLOT_MSG
107 } else if (strncmp(argv[1], "inf", 3) == 0) {
112 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
113 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
114 continue; /* list only known devices */
115 printf("IDE device %d: ", i);
116 dev_print(&ide_dev_desc[i]);
120 } else if (strncmp(argv[1], "dev", 3) == 0) {
121 if ((curr_device < 0)
122 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
123 puts("\nno IDE devices available\n");
126 printf("\nIDE device %d: ", curr_device);
127 dev_print(&ide_dev_desc[curr_device]);
129 } else if (strncmp(argv[1], "part", 4) == 0) {
132 for (ok = 0, dev = 0;
133 dev < CONFIG_SYS_IDE_MAXDEVICE;
135 if (ide_dev_desc[dev].part_type !=
140 print_part(&ide_dev_desc[dev]);
144 puts("\nno IDE devices available\n");
149 return CMD_RET_USAGE;
151 if (strncmp(argv[1], "dev", 3) == 0) {
152 int dev = (int) simple_strtoul(argv[2], NULL, 10);
154 printf("\nIDE device %d: ", dev);
155 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
156 puts("unknown device\n");
159 dev_print(&ide_dev_desc[dev]);
160 /*ide_print (dev); */
162 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
167 puts("... is now current device\n");
170 } else if (strncmp(argv[1], "part", 4) == 0) {
171 int dev = (int) simple_strtoul(argv[2], NULL, 10);
173 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
174 print_part(&ide_dev_desc[dev]);
176 printf("\nIDE device %d not available\n",
183 return CMD_RET_USAGE;
185 /* at least 4 args */
187 if (strcmp(argv[1], "read") == 0) {
188 ulong addr = simple_strtoul(argv[2], NULL, 16);
189 ulong cnt = simple_strtoul(argv[4], NULL, 16);
192 #ifdef CONFIG_SYS_64BIT_LBA
193 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
195 printf("\nIDE read: device %d block # %lld, count %ld ... ",
196 curr_device, blk, cnt);
198 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
200 printf("\nIDE read: device %d block # %ld, count %ld ... ",
201 curr_device, blk, cnt);
204 n = ide_dev_desc[curr_device].block_read(curr_device,
207 /* flush cache after read */
209 cnt * ide_dev_desc[curr_device].blksz);
211 printf("%ld blocks read: %s\n",
212 n, (n == cnt) ? "OK" : "ERROR");
217 } else if (strcmp(argv[1], "write") == 0) {
218 ulong addr = simple_strtoul(argv[2], NULL, 16);
219 ulong cnt = simple_strtoul(argv[4], NULL, 16);
222 #ifdef CONFIG_SYS_64BIT_LBA
223 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
225 printf("\nIDE write: device %d block # %lld, count %ld ... ",
226 curr_device, blk, cnt);
228 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
230 printf("\nIDE write: device %d block # %ld, count %ld ... ",
231 curr_device, blk, cnt);
233 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
235 printf("%ld blocks written: %s\n",
236 n, (n == cnt) ? "OK" : "ERROR");
242 return CMD_RET_USAGE;
249 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
251 return common_diskboot(cmdtp, "ide", argc, argv);
254 /* ------------------------------------------------------------------------- */
256 void __ide_led(uchar led, uchar status)
258 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
259 static uchar led_buffer; /* Buffer for current LED status */
261 uchar *led_port = LED_PORT;
263 if (status) /* switch LED on */
265 else /* switch LED off */
268 *led_port = led_buffer;
272 void ide_led(uchar led, uchar status)
273 __attribute__ ((weak, alias("__ide_led")));
275 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
276 # define DEVICE_LED(x) 0
281 /* ------------------------------------------------------------------------- */
283 inline void __ide_outb(int dev, int port, unsigned char val)
285 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
287 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
289 #if defined(CONFIG_IDE_AHB)
292 ide_write_register(dev, port, val);
295 outb(val, (ATA_CURR_BASE(dev)));
298 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
302 void ide_outb(int dev, int port, unsigned char val)
303 __attribute__ ((weak, alias("__ide_outb")));
305 inline unsigned char __ide_inb(int dev, int port)
309 #if defined(CONFIG_IDE_AHB)
310 val = ide_read_register(dev, port);
312 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
315 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
317 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
321 unsigned char ide_inb(int dev, int port)
322 __attribute__ ((weak, alias("__ide_inb")));
324 #ifdef CONFIG_TUNE_PIO
325 inline int __ide_set_piomode(int pio_mode)
330 inline int ide_set_piomode(int pio_mode)
331 __attribute__ ((weak, alias("__ide_set_piomode")));
339 #ifdef CONFIG_IDE_8xx_PCCARD
340 extern int ide_devices_found; /* Initialized in check_ide_device() */
341 #endif /* CONFIG_IDE_8xx_PCCARD */
343 #ifdef CONFIG_IDE_PREINIT
347 puts("ide_preinit failed\n");
350 #endif /* CONFIG_IDE_PREINIT */
355 * Reset the IDE just to be sure.
356 * Light LED's to show
358 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
360 /* ATAPI Drives seems to need a proper IDE Reset */
363 #ifdef CONFIG_IDE_INIT_POSTRESET
366 if (ide_init_postreset()) {
367 puts("ide_preinit_postreset failed\n");
370 #endif /* CONFIG_IDE_INIT_POSTRESET */
373 * Wait for IDE to get ready.
374 * According to spec, this can take up to 31 seconds!
376 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
378 bus * (CONFIG_SYS_IDE_MAXDEVICE /
379 CONFIG_SYS_IDE_MAXBUS);
381 #ifdef CONFIG_IDE_8xx_PCCARD
382 /* Skip non-ide devices from probing */
383 if ((ide_devices_found & (1 << bus)) == 0) {
384 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
388 printf("Bus %d: ", bus);
394 udelay(100000); /* 100 ms */
395 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
396 udelay(100000); /* 100 ms */
399 udelay(10000); /* 10 ms */
401 c = ide_inb(dev, ATA_STATUS);
403 if (i > (ATA_RESET_TIME * 100)) {
404 puts("** Timeout **\n");
406 ide_led((LED_IDE1 | LED_IDE2), 0);
409 if ((i >= 100) && ((i % 100) == 0))
412 } while (c & ATA_STAT_BUSY);
414 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
415 puts("not available ");
416 debug("Status = 0x%02X ", c);
417 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
418 } else if ((c & ATA_STAT_READY) == 0) {
419 puts("not available ");
420 debug("Status = 0x%02X ", c);
431 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
434 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
435 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
436 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
437 ide_dev_desc[i].if_type = IF_TYPE_IDE;
438 ide_dev_desc[i].dev = i;
439 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
440 ide_dev_desc[i].blksz = 0;
441 ide_dev_desc[i].log2blksz =
442 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
443 ide_dev_desc[i].lba = 0;
444 ide_dev_desc[i].block_read = ide_read;
445 ide_dev_desc[i].block_write = ide_write;
446 if (!ide_bus_ok[IDE_BUS(i)])
448 ide_led(led, 1); /* LED on */
449 ide_ident(&ide_dev_desc[i]);
450 ide_led(led, 0); /* LED off */
451 dev_print(&ide_dev_desc[i]);
453 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
454 /* initialize partition type */
455 init_part(&ide_dev_desc[i]);
463 /* ------------------------------------------------------------------------- */
465 #ifdef CONFIG_PARTITIONS
466 block_dev_desc_t *ide_get_dev(int dev)
468 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
472 /* ------------------------------------------------------------------------- */
474 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
475 __attribute__ ((weak, alias("__ide_input_swap_data")));
477 void ide_input_data(int dev, ulong *sect_buf, int words)
478 __attribute__ ((weak, alias("__ide_input_data")));
480 void ide_output_data(int dev, const ulong *sect_buf, int words)
481 __attribute__ ((weak, alias("__ide_output_data")));
483 /* We only need to swap data if we are running on a big endian cpu. */
484 #if defined(__LITTLE_ENDIAN)
485 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
487 ide_input_data(dev, sect_buf, words);
490 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
492 volatile ushort *pbuf =
493 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
494 ushort *dbuf = (ushort *) sect_buf;
496 debug("in input swap data base for read is %lx\n",
497 (unsigned long) pbuf);
501 *dbuf++ = swab16p((u16 *) pbuf);
502 *dbuf++ = swab16p((u16 *) pbuf);
504 *dbuf++ = ld_le16(pbuf);
505 *dbuf++ = ld_le16(pbuf);
509 #endif /* __LITTLE_ENDIAN */
512 #if defined(CONFIG_IDE_SWAP_IO)
513 void __ide_output_data(int dev, const ulong *sect_buf, int words)
516 volatile ushort *pbuf;
518 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
519 dbuf = (ushort *) sect_buf;
527 #else /* ! CONFIG_IDE_SWAP_IO */
528 void __ide_output_data(int dev, const ulong *sect_buf, int words)
530 #if defined(CONFIG_IDE_AHB)
531 ide_write_data(dev, sect_buf, words);
533 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
536 #endif /* CONFIG_IDE_SWAP_IO */
538 #if defined(CONFIG_IDE_SWAP_IO)
539 void __ide_input_data(int dev, ulong *sect_buf, int words)
542 volatile ushort *pbuf;
544 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
545 dbuf = (ushort *) sect_buf;
547 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
556 #else /* ! CONFIG_IDE_SWAP_IO */
557 void __ide_input_data(int dev, ulong *sect_buf, int words)
559 #if defined(CONFIG_IDE_AHB)
560 ide_read_data(dev, sect_buf, words);
562 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
566 #endif /* CONFIG_IDE_SWAP_IO */
568 /* -------------------------------------------------------------------------
570 static void ide_ident(block_dev_desc_t *dev_desc)
579 #ifdef CONFIG_TUNE_PIO
584 int mode, cycle_time;
588 device = dev_desc->dev;
589 printf(" Device %d: ", device);
591 ide_led(DEVICE_LED(device), 1); /* LED on */
594 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
595 dev_desc->if_type = IF_TYPE_IDE;
600 /* Warning: This will be tricky to read */
601 while (retries <= 1) {
602 /* check signature */
603 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
604 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
605 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
606 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
607 /* ATAPI Signature found */
608 dev_desc->if_type = IF_TYPE_ATAPI;
610 * Start Ident Command
612 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
614 * Wait for completion - ATAPI devices need more time
617 c = ide_wait(device, ATAPI_TIME_OUT);
622 * Start Ident Command
624 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
627 * Wait for completion
629 c = ide_wait(device, IDE_TIME_OUT);
631 ide_led(DEVICE_LED(device), 0); /* LED off */
633 if (((c & ATA_STAT_DRQ) == 0) ||
634 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
638 * Need to soft reset the device
639 * in case it's an ATAPI...
641 debug("Retrying...\n");
642 ide_outb(device, ATA_DEV_HD,
643 ATA_LBA | ATA_DEVICE(device));
645 ide_outb(device, ATA_COMMAND, 0x08);
646 udelay(500000); /* 500 ms */
651 ide_outb(device, ATA_DEV_HD,
652 ATA_LBA | ATA_DEVICE(device));
661 } /* see above - ugly to read */
663 if (retries == 2) /* Not found */
667 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
669 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
670 sizeof(dev_desc->revision));
671 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
672 sizeof(dev_desc->vendor));
673 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
674 sizeof(dev_desc->product));
675 #ifdef __LITTLE_ENDIAN
677 * firmware revision, model, and serial number have Big Endian Byte
678 * order in Word. Convert all three to little endian.
680 * See CF+ and CompactFlash Specification Revision 2.0:
681 * 6.2.1.6: Identify Drive, Table 39 for more details
684 strswab(dev_desc->revision);
685 strswab(dev_desc->vendor);
686 strswab(dev_desc->product);
687 #endif /* __LITTLE_ENDIAN */
689 if ((iop.config & 0x0080) == 0x0080)
690 dev_desc->removable = 1;
692 dev_desc->removable = 0;
694 #ifdef CONFIG_TUNE_PIO
695 /* Mode 0 - 2 only, are directly determined by word 51. */
698 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
699 /* Force it to dead slow, and hope for the best... */
703 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
704 * shall set bit 1 of word 53 to one and support the fields contained
705 * in words 64 through 70.
707 if (iop.field_valid & 0x02) {
709 * Mode 3 and above are possible. Check in order from slow
710 * to fast, so we wind up with the highest mode allowed.
712 if (iop.eide_pio_modes & 0x01)
714 if (iop.eide_pio_modes & 0x02)
716 if (ata_id_is_cfa((u16 *)&iop)) {
717 if ((iop.cf_advanced_caps & 0x07) == 0x01)
719 if ((iop.cf_advanced_caps & 0x07) == 0x02)
724 /* System-specific, depends on bus speeds, etc. */
725 ide_set_piomode(pio_mode);
726 #endif /* CONFIG_TUNE_PIO */
730 * Drive PIO mode autoselection
734 printf("tPIO = 0x%02x = %d\n", mode, mode);
735 if (mode > 2) { /* 2 is maximum allowed tPIO value */
737 debug("Override tPIO -> 2\n");
739 if (iop.field_valid & 2) { /* drive implements ATA2? */
740 debug("Drive implements ATA2\n");
741 if (iop.capability & 8) { /* drive supports use_iordy? */
742 cycle_time = iop.eide_pio_iordy;
744 cycle_time = iop.eide_pio;
746 debug("cycle time = %d\n", cycle_time);
748 if (cycle_time > 120)
749 mode = 3; /* 120 ns for PIO mode 4 */
750 if (cycle_time > 180)
751 mode = 2; /* 180 ns for PIO mode 3 */
752 if (cycle_time > 240)
753 mode = 1; /* 240 ns for PIO mode 4 */
754 if (cycle_time > 383)
755 mode = 0; /* 383 ns for PIO mode 4 */
757 printf("PIO mode to use: PIO %d\n", mode);
761 if (dev_desc->if_type == IF_TYPE_ATAPI) {
762 atapi_inquiry(dev_desc);
765 #endif /* CONFIG_ATAPI */
769 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
770 #else /* ! __BIG_ENDIAN */
772 * do not swap shorts on little endian
774 * See CF+ and CompactFlash Specification Revision 2.0:
775 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
777 dev_desc->lba = iop.lba_capacity;
778 #endif /* __BIG_ENDIAN */
781 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
783 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
784 ((unsigned long long) iop.lba48_capacity[1] << 16) |
785 ((unsigned long long) iop.lba48_capacity[2] << 32) |
786 ((unsigned long long) iop.lba48_capacity[3] << 48);
790 #endif /* CONFIG_LBA48 */
792 dev_desc->type = DEV_TYPE_HARDDISK;
793 dev_desc->blksz = ATA_BLOCKSIZE;
794 dev_desc->log2blksz = LOG2(dev_desc->blksz);
795 dev_desc->lun = 0; /* just to fill something in... */
797 #if 0 /* only used to test the powersaving mode,
798 * if enabled, the drive goes after 5 sec
800 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
801 c = ide_wait(device, IDE_TIME_OUT);
802 ide_outb(device, ATA_SECT_CNT, 1);
803 ide_outb(device, ATA_LBA_LOW, 0);
804 ide_outb(device, ATA_LBA_MID, 0);
805 ide_outb(device, ATA_LBA_HIGH, 0);
806 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
807 ide_outb(device, ATA_COMMAND, 0xe3);
809 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
814 /* ------------------------------------------------------------------------- */
816 ulong ide_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
820 unsigned char pwrsave = 0; /* power save */
823 unsigned char lba48 = 0;
825 if (blknr & 0x0000fffff0000000ULL) {
826 /* more than 28 bits used, use 48bit mode */
830 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
831 device, blknr, blkcnt, (ulong) buffer);
833 ide_led(DEVICE_LED(device), 1); /* LED on */
837 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
838 c = ide_wait(device, IDE_TIME_OUT);
840 if (c & ATA_STAT_BUSY) {
841 printf("IDE read: device %d not ready\n", device);
845 /* first check if the drive is in Powersaving mode, if yes,
846 * increase the timeout value */
847 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
850 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
852 if (c & ATA_STAT_BUSY) {
853 printf("IDE read: device %d not ready\n", device);
856 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
857 printf("No Powersaving mode %X\n", c);
859 c = ide_inb(device, ATA_SECT_CNT);
860 debug("Powersaving %02X\n", c);
866 while (blkcnt-- > 0) {
868 c = ide_wait(device, IDE_TIME_OUT);
870 if (c & ATA_STAT_BUSY) {
871 printf("IDE read: device %d not ready\n", device);
876 /* write high bits */
877 ide_outb(device, ATA_SECT_CNT, 0);
878 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
879 #ifdef CONFIG_SYS_64BIT_LBA
880 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
881 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
883 ide_outb(device, ATA_LBA_MID, 0);
884 ide_outb(device, ATA_LBA_HIGH, 0);
888 ide_outb(device, ATA_SECT_CNT, 1);
889 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
890 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
891 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
895 ide_outb(device, ATA_DEV_HD,
896 ATA_LBA | ATA_DEVICE(device));
897 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
902 ide_outb(device, ATA_DEV_HD, ATA_LBA |
903 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
904 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
910 /* may take up to 4 sec */
911 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
914 /* can't take over 500 ms */
915 c = ide_wait(device, IDE_TIME_OUT);
918 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
920 printf("Error (no IRQ) dev %d blk " LBAF ": status "
921 "%#02x\n", device, blknr, c);
925 ide_input_data(device, buffer, ATA_SECTORWORDS);
926 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
930 buffer += ATA_BLOCKSIZE;
933 ide_led(DEVICE_LED(device), 0); /* LED off */
937 /* ------------------------------------------------------------------------- */
940 ulong ide_write(int device, lbaint_t blknr, lbaint_t blkcnt, const void *buffer)
946 unsigned char lba48 = 0;
948 if (blknr & 0x0000fffff0000000ULL) {
949 /* more than 28 bits used, use 48bit mode */
954 ide_led(DEVICE_LED(device), 1); /* LED on */
958 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
960 while (blkcnt-- > 0) {
962 c = ide_wait(device, IDE_TIME_OUT);
964 if (c & ATA_STAT_BUSY) {
965 printf("IDE read: device %d not ready\n", device);
970 /* write high bits */
971 ide_outb(device, ATA_SECT_CNT, 0);
972 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
973 #ifdef CONFIG_SYS_64BIT_LBA
974 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
975 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
977 ide_outb(device, ATA_LBA_MID, 0);
978 ide_outb(device, ATA_LBA_HIGH, 0);
982 ide_outb(device, ATA_SECT_CNT, 1);
983 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
984 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
985 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
989 ide_outb(device, ATA_DEV_HD,
990 ATA_LBA | ATA_DEVICE(device));
991 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
996 ide_outb(device, ATA_DEV_HD, ATA_LBA |
997 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
998 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1003 /* can't take over 500 ms */
1004 c = ide_wait(device, IDE_TIME_OUT);
1006 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1008 printf("Error (no IRQ) dev %d blk " LBAF ": status "
1009 "%#02x\n", device, blknr, c);
1013 ide_output_data(device, buffer, ATA_SECTORWORDS);
1014 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1017 buffer += ATA_BLOCKSIZE;
1020 ide_led(DEVICE_LED(device), 0); /* LED off */
1024 /* ------------------------------------------------------------------------- */
1027 * copy src to dest, skipping leading and trailing blanks and null
1028 * terminate the string
1029 * "len" is the size of available memory including the terminating '\0'
1031 static void ident_cpy(unsigned char *dst, unsigned char *src,
1034 unsigned char *end, *last;
1037 end = src + len - 1;
1039 /* reserve space for '\0' */
1043 /* skip leading white space */
1044 while ((*src) && (src < end) && (*src == ' '))
1047 /* copy string, omitting trailing white space */
1048 while ((*src) && (src < end)) {
1057 /* ------------------------------------------------------------------------- */
1060 * Wait until Busy bit is off, or timeout (in ms)
1061 * Return last status
1063 static uchar ide_wait(int dev, ulong t)
1065 ulong delay = 10 * t; /* poll every 100 us */
1068 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1076 /* ------------------------------------------------------------------------- */
1078 #ifdef CONFIG_IDE_RESET
1079 extern void ide_set_reset(int idereset);
1081 static void ide_reset(void)
1086 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1088 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1089 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1091 ide_set_reset(1); /* assert reset */
1093 /* the reset signal shall be asserted for et least 25 us */
1098 /* de-assert RESET signal */
1102 for (i = 0; i < 250; ++i)
1106 #endif /* CONFIG_IDE_RESET */
1108 /* ------------------------------------------------------------------------- */
1110 #if defined(CONFIG_OF_IDE_FIXUP)
1111 int ide_device_present(int dev)
1113 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1115 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1118 /* ------------------------------------------------------------------------- */
1121 /****************************************************************************
1125 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1126 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1128 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1129 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1132 #if defined(CONFIG_IDE_SWAP_IO)
1133 /* since ATAPI may use commands with not 4 bytes alligned length
1134 * we have our own transfer functions, 2 bytes alligned */
1135 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1138 volatile ushort *pbuf;
1140 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1141 dbuf = (ushort *) sect_buf;
1143 debug("in output data shorts base for read is %lx\n",
1144 (unsigned long) pbuf);
1152 void __ide_input_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 input data shorts base for read is %lx\n",
1161 (unsigned long) pbuf);
1169 #else /* ! CONFIG_IDE_SWAP_IO */
1170 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1172 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1175 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1177 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1180 #endif /* CONFIG_IDE_SWAP_IO */
1183 * Wait until (Status & mask) == res, or timeout (in ms)
1184 * Return last status
1185 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1186 * and then they set their DRQ Bit
1188 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1190 ulong delay = 10 * t; /* poll every 100 us */
1193 /* prevents to read the status before valid */
1194 c = ide_inb(dev, ATA_DEV_CTL);
1196 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1197 /* break if error occurs (doesn't make sense to wait more) */
1198 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1208 * issue an atapi command
1210 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1211 unsigned char *buffer, int buflen)
1213 unsigned char c, err, mask, res;
1216 ide_led(DEVICE_LED(device), 1); /* LED on */
1220 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1222 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1223 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1224 if ((c & mask) != res) {
1225 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1230 /* write taskfile */
1231 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1232 ide_outb(device, ATA_SECT_CNT, 0);
1233 ide_outb(device, ATA_SECT_NUM, 0);
1234 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1235 ide_outb(device, ATA_CYL_HIGH,
1236 (unsigned char) ((buflen >> 8) & 0xFF));
1237 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1239 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1242 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1244 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1246 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1247 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1253 /* write command block */
1254 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1256 /* ATAPI Command written wait for completition */
1257 udelay(5000); /* device must set bsy */
1259 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1261 * if no data wait for DRQ = 0 BSY = 0
1262 * if data wait for DRQ = 1 BSY = 0
1267 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1268 if ((c & mask) != res) {
1269 if (c & ATA_STAT_ERR) {
1270 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1271 debug("atapi_issue 1 returned sense key %X status %02X\n",
1274 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1280 n = ide_inb(device, ATA_CYL_HIGH);
1282 n += ide_inb(device, ATA_CYL_LOW);
1284 printf("ERROR, transfer bytes %d requested only %d\n", n,
1289 if ((n == 0) && (buflen < 0)) {
1290 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1295 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1298 if (n != 0) { /* data transfer */
1299 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1300 /* we transfer shorts */
1302 /* ok now decide if it is an in or output */
1303 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1304 debug("Write to device\n");
1305 ide_output_data_shorts(device,
1306 (unsigned short *) buffer, n);
1308 debug("Read from device @ %p shorts %d\n", buffer, n);
1309 ide_input_data_shorts(device,
1310 (unsigned short *) buffer, n);
1313 udelay(5000); /* seems that some CD ROMs need this... */
1314 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1316 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1317 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1318 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1319 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1325 ide_led(DEVICE_LED(device), 0); /* LED off */
1330 * sending the command to atapi_issue. If an status other than good
1331 * returns, an request_sense will be issued
1334 #define ATAPI_DRIVE_NOT_READY 100
1335 #define ATAPI_UNIT_ATTN 10
1337 unsigned char atapi_issue_autoreq(int device,
1340 unsigned char *buffer, int buflen)
1342 unsigned char sense_data[18], sense_ccb[12];
1343 unsigned char res, key, asc, ascq;
1344 int notready, unitattn;
1346 unitattn = ATAPI_UNIT_ATTN;
1347 notready = ATAPI_DRIVE_NOT_READY;
1350 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1355 return 0xFF; /* error */
1357 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1359 memset(sense_ccb, 0, sizeof(sense_ccb));
1360 memset(sense_data, 0, sizeof(sense_data));
1361 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1362 sense_ccb[4] = 18; /* allocation Length */
1364 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1365 key = (sense_data[2] & 0xF);
1366 asc = (sense_data[12]);
1367 ascq = (sense_data[13]);
1369 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1370 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1371 sense_data[0], key, asc, ascq);
1374 return 0; /* ok device ready */
1376 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1377 if (unitattn-- > 0) {
1381 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1384 if ((asc == 0x4) && (ascq == 0x1)) {
1385 /* not ready, but will be ready soon */
1386 if (notready-- > 0) {
1390 printf("Drive not ready, tried %d times\n",
1391 ATAPI_DRIVE_NOT_READY);
1395 debug("Media not present\n");
1399 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1402 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1407 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1409 unsigned char ccb[12]; /* Command descriptor block */
1410 unsigned char iobuf[64]; /* temp buf */
1414 device = dev_desc->dev;
1415 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1416 dev_desc->block_read = atapi_read;
1418 memset(ccb, 0, sizeof(ccb));
1419 memset(iobuf, 0, sizeof(iobuf));
1421 ccb[0] = ATAPI_CMD_INQUIRY;
1422 ccb[4] = 40; /* allocation Legnth */
1423 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1425 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1429 /* copy device ident strings */
1430 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1431 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1432 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1436 dev_desc->blksz = 0;
1437 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
1438 dev_desc->type = iobuf[0] & 0x1f;
1440 if ((iobuf[1] & 0x80) == 0x80)
1441 dev_desc->removable = 1;
1443 dev_desc->removable = 0;
1445 memset(ccb, 0, sizeof(ccb));
1446 memset(iobuf, 0, sizeof(iobuf));
1447 ccb[0] = ATAPI_CMD_START_STOP;
1448 ccb[4] = 0x03; /* start */
1450 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1452 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1456 memset(ccb, 0, sizeof(ccb));
1457 memset(iobuf, 0, sizeof(iobuf));
1458 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1460 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1464 memset(ccb, 0, sizeof(ccb));
1465 memset(iobuf, 0, sizeof(iobuf));
1466 ccb[0] = ATAPI_CMD_READ_CAP;
1467 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1468 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1472 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1473 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1474 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1476 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1477 ((unsigned long) iobuf[1] << 16) +
1478 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1479 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1480 ((unsigned long) iobuf[5] << 16) +
1481 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1482 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1484 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1485 dev_desc->lba48 = 0;
1493 * we transfer only one block per command, since the multiple DRQ per
1494 * command is not yet implemented
1496 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1497 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1498 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1500 ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
1503 unsigned char ccb[12]; /* Command descriptor block */
1506 debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
1507 device, blknr, blkcnt, (ulong) buffer);
1510 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1511 cnt = ATAPI_READ_MAX_BLOCK;
1515 ccb[0] = ATAPI_CMD_READ_12;
1516 ccb[1] = 0; /* reserved */
1517 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1518 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1519 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1520 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1521 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1522 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1523 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1524 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1525 ccb[10] = 0; /* reserved */
1526 ccb[11] = 0; /* reserved */
1528 if (atapi_issue_autoreq(device, ccb, 12,
1529 (unsigned char *) buffer,
1530 cnt * ATAPI_READ_BLOCK_SIZE)
1537 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1538 } while (blkcnt > 0);
1542 /* ------------------------------------------------------------------------- */
1544 #endif /* CONFIG_ATAPI */
1546 U_BOOT_CMD(ide, 5, 1, do_ide,
1548 "reset - reset IDE controller\n"
1549 "ide info - show available IDE devices\n"
1550 "ide device [dev] - show or set current device\n"
1551 "ide part [dev] - print partition table of one or all IDE devices\n"
1552 "ide read addr blk# cnt\n"
1553 "ide write addr blk# cnt - read/write `cnt'"
1554 " blocks starting at block `blk#'\n"
1555 " to/from memory address `addr'");
1557 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1558 "boot from IDE device", "loadAddr dev:part");