2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
18 #include <asm/byteorder.h>
21 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
28 #ifdef CONFIG_STATUS_LED
29 # include <status_led.h>
33 # define EIEIO __asm__ volatile ("eieio")
34 # define SYNC __asm__ volatile ("sync")
36 # define EIEIO /* nothing */
37 # define SYNC /* nothing */
40 /* ------------------------------------------------------------------------- */
42 /* Current I/O Device */
43 static int curr_device = -1;
45 /* Current offset for IDE0 / IDE1 bus access */
46 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
47 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
48 CONFIG_SYS_ATA_IDE0_OFFSET,
50 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
51 CONFIG_SYS_ATA_IDE1_OFFSET,
55 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
57 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
58 /* ------------------------------------------------------------------------- */
60 #ifdef CONFIG_IDE_RESET
61 static void ide_reset(void);
63 #define ide_reset() /* dummy */
66 static void ide_ident(struct blk_desc *dev_desc);
67 static uchar ide_wait(int dev, ulong t);
69 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
71 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
73 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
75 static void ident_cpy(unsigned char *dest, unsigned char *src,
78 #ifndef CONFIG_SYS_ATA_PORT_ADDR
79 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
83 static void atapi_inquiry(struct blk_desc *dev_desc);
84 static ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr,
85 lbaint_t blkcnt, void *buffer);
89 /* ------------------------------------------------------------------------- */
91 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
100 if (strncmp(argv[1], "res", 3) == 0) {
102 #ifdef CONFIG_IDE_8xx_DIRECT
103 " on PCMCIA " PCMCIA_SLOT_MSG
109 } else if (strncmp(argv[1], "inf", 3) == 0) {
114 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
115 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
116 continue; /* list only known devices */
117 printf("IDE device %d: ", i);
118 dev_print(&ide_dev_desc[i]);
122 } else if (strncmp(argv[1], "dev", 3) == 0) {
123 if ((curr_device < 0)
124 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
125 puts("\nno IDE devices available\n");
128 printf("\nIDE device %d: ", curr_device);
129 dev_print(&ide_dev_desc[curr_device]);
131 } else if (strncmp(argv[1], "part", 4) == 0) {
134 for (ok = 0, dev = 0;
135 dev < CONFIG_SYS_IDE_MAXDEVICE;
137 if (ide_dev_desc[dev].part_type !=
142 part_print(&ide_dev_desc[dev]);
146 puts("\nno IDE devices available\n");
151 return CMD_RET_USAGE;
153 if (strncmp(argv[1], "dev", 3) == 0) {
154 int dev = (int)simple_strtoul(argv[2], NULL, 10);
156 printf("\nIDE device %d: ", dev);
157 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
158 puts("unknown device\n");
161 dev_print(&ide_dev_desc[dev]);
162 /*ide_print (dev); */
164 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
169 puts("... is now current device\n");
172 } else if (strncmp(argv[1], "part", 4) == 0) {
173 int dev = (int)simple_strtoul(argv[2], NULL, 10);
175 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
176 part_print(&ide_dev_desc[dev]);
178 printf("\nIDE device %d not available\n",
185 return CMD_RET_USAGE;
187 /* at least 4 args */
189 if (strcmp(argv[1], "read") == 0) {
190 ulong addr = simple_strtoul(argv[2], NULL, 16);
191 ulong cnt = simple_strtoul(argv[4], NULL, 16);
192 struct blk_desc *dev_desc;
195 #ifdef CONFIG_SYS_64BIT_LBA
196 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
198 printf("\nIDE read: device %d block # %lld, count %ld...",
199 curr_device, blk, cnt);
201 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
203 printf("\nIDE read: device %d block # %ld, count %ld...",
204 curr_device, blk, cnt);
207 dev_desc = &ide_dev_desc[curr_device];
208 n = blk_dread(dev_desc, blk, cnt, (ulong *)addr);
209 /* flush cache after read */
211 cnt * ide_dev_desc[curr_device].blksz);
213 printf("%ld blocks read: %s\n",
214 n, (n == cnt) ? "OK" : "ERROR");
219 } else if (strcmp(argv[1], "write") == 0) {
220 ulong addr = simple_strtoul(argv[2], NULL, 16);
221 ulong cnt = simple_strtoul(argv[4], NULL, 16);
224 #ifdef CONFIG_SYS_64BIT_LBA
225 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
227 printf("\nIDE write: device %d block # %lld, count %ld...",
228 curr_device, blk, cnt);
230 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
232 printf("\nIDE write: device %d block # %ld, count %ld...",
233 curr_device, blk, cnt);
235 n = ide_write(&ide_dev_desc[curr_device], blk, cnt,
238 printf("%ld blocks written: %s\n",
239 n, (n == cnt) ? "OK" : "ERROR");
245 return CMD_RET_USAGE;
252 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
254 return common_diskboot(cmdtp, "ide", argc, argv);
257 /* ------------------------------------------------------------------------- */
259 __weak void ide_led(uchar led, uchar status)
261 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
262 static uchar led_buffer; /* Buffer for current LED status */
264 uchar *led_port = LED_PORT;
266 if (status) /* switch LED on */
268 else /* switch LED off */
271 *led_port = led_buffer;
275 #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
276 # define DEVICE_LED(x) 0
281 /* ------------------------------------------------------------------------- */
283 __weak 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 __weak unsigned char ide_inb(int dev, int port)
306 #if defined(CONFIG_IDE_AHB)
307 val = ide_read_register(dev, port);
309 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
312 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
314 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
323 #ifdef CONFIG_IDE_8xx_PCCARD
324 extern int ide_devices_found; /* Initialized in check_ide_device() */
325 #endif /* CONFIG_IDE_8xx_PCCARD */
327 #ifdef CONFIG_IDE_PREINIT
331 puts("ide_preinit failed\n");
334 #endif /* CONFIG_IDE_PREINIT */
339 * Reset the IDE just to be sure.
340 * Light LED's to show
342 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
344 /* ATAPI Drives seems to need a proper IDE Reset */
347 #ifdef CONFIG_IDE_INIT_POSTRESET
350 if (ide_init_postreset()) {
351 puts("ide_preinit_postreset failed\n");
354 #endif /* CONFIG_IDE_INIT_POSTRESET */
357 * Wait for IDE to get ready.
358 * According to spec, this can take up to 31 seconds!
360 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
362 bus * (CONFIG_SYS_IDE_MAXDEVICE /
363 CONFIG_SYS_IDE_MAXBUS);
365 #ifdef CONFIG_IDE_8xx_PCCARD
366 /* Skip non-ide devices from probing */
367 if ((ide_devices_found & (1 << bus)) == 0) {
368 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
372 printf("Bus %d: ", bus);
378 udelay(100000); /* 100 ms */
379 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
380 udelay(100000); /* 100 ms */
383 udelay(10000); /* 10 ms */
385 c = ide_inb(dev, ATA_STATUS);
387 if (i > (ATA_RESET_TIME * 100)) {
388 puts("** Timeout **\n");
390 ide_led((LED_IDE1 | LED_IDE2), 0);
393 if ((i >= 100) && ((i % 100) == 0))
396 } while (c & ATA_STAT_BUSY);
398 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
399 puts("not available ");
400 debug("Status = 0x%02X ", c);
401 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
402 } else if ((c & ATA_STAT_READY) == 0) {
403 puts("not available ");
404 debug("Status = 0x%02X ", c);
415 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
418 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
419 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
420 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
421 ide_dev_desc[i].if_type = IF_TYPE_IDE;
422 ide_dev_desc[i].devnum = i;
423 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
424 ide_dev_desc[i].blksz = 0;
425 ide_dev_desc[i].log2blksz =
426 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
427 ide_dev_desc[i].lba = 0;
428 ide_dev_desc[i].block_read = ide_read;
429 ide_dev_desc[i].block_write = ide_write;
430 if (!ide_bus_ok[IDE_BUS(i)])
432 ide_led(led, 1); /* LED on */
433 ide_ident(&ide_dev_desc[i]);
434 ide_led(led, 0); /* LED off */
435 dev_print(&ide_dev_desc[i]);
437 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
438 /* initialize partition type */
439 part_init(&ide_dev_desc[i]);
447 /* ------------------------------------------------------------------------- */
449 #ifdef CONFIG_PARTITIONS
450 struct blk_desc *ide_get_dev(int dev)
452 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
456 /* ------------------------------------------------------------------------- */
458 /* We only need to swap data if we are running on a big endian cpu. */
459 #if defined(__LITTLE_ENDIAN)
460 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
462 ide_input_data(dev, sect_buf, words);
465 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
467 volatile ushort *pbuf =
468 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
469 ushort *dbuf = (ushort *) sect_buf;
471 debug("in input swap data base for read is %lx\n",
472 (unsigned long) pbuf);
476 *dbuf++ = swab16p((u16 *) pbuf);
477 *dbuf++ = swab16p((u16 *) pbuf);
479 *dbuf++ = ld_le16(pbuf);
480 *dbuf++ = ld_le16(pbuf);
484 #endif /* __LITTLE_ENDIAN */
487 #if defined(CONFIG_IDE_SWAP_IO)
488 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
491 volatile ushort *pbuf;
493 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
494 dbuf = (ushort *) sect_buf;
502 #else /* ! CONFIG_IDE_SWAP_IO */
503 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
505 #if defined(CONFIG_IDE_AHB)
506 ide_write_data(dev, sect_buf, words);
508 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
511 #endif /* CONFIG_IDE_SWAP_IO */
513 #if defined(CONFIG_IDE_SWAP_IO)
514 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
517 volatile ushort *pbuf;
519 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
520 dbuf = (ushort *) sect_buf;
522 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
531 #else /* ! CONFIG_IDE_SWAP_IO */
532 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
534 #if defined(CONFIG_IDE_AHB)
535 ide_read_data(dev, sect_buf, words);
537 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
541 #endif /* CONFIG_IDE_SWAP_IO */
543 /* -------------------------------------------------------------------------
545 static void ide_ident(struct blk_desc *dev_desc)
555 device = dev_desc->devnum;
556 printf(" Device %d: ", device);
558 ide_led(DEVICE_LED(device), 1); /* LED on */
561 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
562 dev_desc->if_type = IF_TYPE_IDE;
567 /* Warning: This will be tricky to read */
568 while (retries <= 1) {
569 /* check signature */
570 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
571 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
572 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
573 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
574 /* ATAPI Signature found */
575 dev_desc->if_type = IF_TYPE_ATAPI;
577 * Start Ident Command
579 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
581 * Wait for completion - ATAPI devices need more time
584 c = ide_wait(device, ATAPI_TIME_OUT);
589 * Start Ident Command
591 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
594 * Wait for completion
596 c = ide_wait(device, IDE_TIME_OUT);
598 ide_led(DEVICE_LED(device), 0); /* LED off */
600 if (((c & ATA_STAT_DRQ) == 0) ||
601 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
605 * Need to soft reset the device
606 * in case it's an ATAPI...
608 debug("Retrying...\n");
609 ide_outb(device, ATA_DEV_HD,
610 ATA_LBA | ATA_DEVICE(device));
612 ide_outb(device, ATA_COMMAND, 0x08);
613 udelay(500000); /* 500 ms */
618 ide_outb(device, ATA_DEV_HD,
619 ATA_LBA | ATA_DEVICE(device));
628 } /* see above - ugly to read */
630 if (retries == 2) /* Not found */
634 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
636 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
637 sizeof(dev_desc->revision));
638 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
639 sizeof(dev_desc->vendor));
640 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
641 sizeof(dev_desc->product));
642 #ifdef __LITTLE_ENDIAN
644 * firmware revision, model, and serial number have Big Endian Byte
645 * order in Word. Convert all three to little endian.
647 * See CF+ and CompactFlash Specification Revision 2.0:
648 * 6.2.1.6: Identify Drive, Table 39 for more details
651 strswab(dev_desc->revision);
652 strswab(dev_desc->vendor);
653 strswab(dev_desc->product);
654 #endif /* __LITTLE_ENDIAN */
656 if ((iop.config & 0x0080) == 0x0080)
657 dev_desc->removable = 1;
659 dev_desc->removable = 0;
662 if (dev_desc->if_type == IF_TYPE_ATAPI) {
663 atapi_inquiry(dev_desc);
666 #endif /* CONFIG_ATAPI */
670 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
671 #else /* ! __BIG_ENDIAN */
673 * do not swap shorts on little endian
675 * See CF+ and CompactFlash Specification Revision 2.0:
676 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
678 dev_desc->lba = iop.lba_capacity;
679 #endif /* __BIG_ENDIAN */
682 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
684 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
685 ((unsigned long long) iop.lba48_capacity[1] << 16) |
686 ((unsigned long long) iop.lba48_capacity[2] << 32) |
687 ((unsigned long long) iop.lba48_capacity[3] << 48);
691 #endif /* CONFIG_LBA48 */
693 dev_desc->type = DEV_TYPE_HARDDISK;
694 dev_desc->blksz = ATA_BLOCKSIZE;
695 dev_desc->log2blksz = LOG2(dev_desc->blksz);
696 dev_desc->lun = 0; /* just to fill something in... */
698 #if 0 /* only used to test the powersaving mode,
699 * if enabled, the drive goes after 5 sec
701 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
702 c = ide_wait(device, IDE_TIME_OUT);
703 ide_outb(device, ATA_SECT_CNT, 1);
704 ide_outb(device, ATA_LBA_LOW, 0);
705 ide_outb(device, ATA_LBA_MID, 0);
706 ide_outb(device, ATA_LBA_HIGH, 0);
707 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
708 ide_outb(device, ATA_COMMAND, 0xe3);
710 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
715 /* ------------------------------------------------------------------------- */
717 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
720 int device = block_dev->devnum;
723 unsigned char pwrsave = 0; /* power save */
726 unsigned char lba48 = 0;
728 if (blknr & 0x0000fffff0000000ULL) {
729 /* more than 28 bits used, use 48bit mode */
733 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
734 device, blknr, blkcnt, (ulong) buffer);
736 ide_led(DEVICE_LED(device), 1); /* LED on */
740 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
741 c = ide_wait(device, IDE_TIME_OUT);
743 if (c & ATA_STAT_BUSY) {
744 printf("IDE read: device %d not ready\n", device);
748 /* first check if the drive is in Powersaving mode, if yes,
749 * increase the timeout value */
750 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
753 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
755 if (c & ATA_STAT_BUSY) {
756 printf("IDE read: device %d not ready\n", device);
759 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
760 printf("No Powersaving mode %X\n", c);
762 c = ide_inb(device, ATA_SECT_CNT);
763 debug("Powersaving %02X\n", c);
769 while (blkcnt-- > 0) {
771 c = ide_wait(device, IDE_TIME_OUT);
773 if (c & ATA_STAT_BUSY) {
774 printf("IDE read: device %d not ready\n", device);
779 /* write high bits */
780 ide_outb(device, ATA_SECT_CNT, 0);
781 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
782 #ifdef CONFIG_SYS_64BIT_LBA
783 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
784 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
786 ide_outb(device, ATA_LBA_MID, 0);
787 ide_outb(device, ATA_LBA_HIGH, 0);
791 ide_outb(device, ATA_SECT_CNT, 1);
792 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
793 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
794 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
798 ide_outb(device, ATA_DEV_HD,
799 ATA_LBA | ATA_DEVICE(device));
800 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
805 ide_outb(device, ATA_DEV_HD, ATA_LBA |
806 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
807 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
813 /* may take up to 4 sec */
814 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
817 /* can't take over 500 ms */
818 c = ide_wait(device, IDE_TIME_OUT);
821 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
823 printf("Error (no IRQ) dev %d blk " LBAF ": status "
824 "%#02x\n", device, blknr, c);
828 ide_input_data(device, buffer, ATA_SECTORWORDS);
829 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
833 buffer += ATA_BLOCKSIZE;
836 ide_led(DEVICE_LED(device), 0); /* LED off */
840 /* ------------------------------------------------------------------------- */
843 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
846 int device = block_dev->devnum;
851 unsigned char lba48 = 0;
853 if (blknr & 0x0000fffff0000000ULL) {
854 /* more than 28 bits used, use 48bit mode */
859 ide_led(DEVICE_LED(device), 1); /* LED on */
863 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
865 while (blkcnt-- > 0) {
867 c = ide_wait(device, IDE_TIME_OUT);
869 if (c & ATA_STAT_BUSY) {
870 printf("IDE read: device %d not ready\n", device);
875 /* write high bits */
876 ide_outb(device, ATA_SECT_CNT, 0);
877 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
878 #ifdef CONFIG_SYS_64BIT_LBA
879 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
880 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
882 ide_outb(device, ATA_LBA_MID, 0);
883 ide_outb(device, ATA_LBA_HIGH, 0);
887 ide_outb(device, ATA_SECT_CNT, 1);
888 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
889 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
890 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
894 ide_outb(device, ATA_DEV_HD,
895 ATA_LBA | ATA_DEVICE(device));
896 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
901 ide_outb(device, ATA_DEV_HD, ATA_LBA |
902 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
903 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
908 /* can't take over 500 ms */
909 c = ide_wait(device, IDE_TIME_OUT);
911 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
913 printf("Error (no IRQ) dev %d blk " LBAF ": status "
914 "%#02x\n", device, blknr, c);
918 ide_output_data(device, buffer, ATA_SECTORWORDS);
919 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
922 buffer += ATA_BLOCKSIZE;
925 ide_led(DEVICE_LED(device), 0); /* LED off */
929 /* ------------------------------------------------------------------------- */
932 * copy src to dest, skipping leading and trailing blanks and null
933 * terminate the string
934 * "len" is the size of available memory including the terminating '\0'
936 static void ident_cpy(unsigned char *dst, unsigned char *src,
939 unsigned char *end, *last;
944 /* reserve space for '\0' */
948 /* skip leading white space */
949 while ((*src) && (src < end) && (*src == ' '))
952 /* copy string, omitting trailing white space */
953 while ((*src) && (src < end)) {
962 /* ------------------------------------------------------------------------- */
965 * Wait until Busy bit is off, or timeout (in ms)
968 static uchar ide_wait(int dev, ulong t)
970 ulong delay = 10 * t; /* poll every 100 us */
973 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
981 /* ------------------------------------------------------------------------- */
983 #ifdef CONFIG_IDE_RESET
984 extern void ide_set_reset(int idereset);
986 static void ide_reset(void)
991 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
993 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
994 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
996 ide_set_reset(1); /* assert reset */
998 /* the reset signal shall be asserted for et least 25 us */
1003 /* de-assert RESET signal */
1007 for (i = 0; i < 250; ++i)
1011 #endif /* CONFIG_IDE_RESET */
1013 /* ------------------------------------------------------------------------- */
1015 #if defined(CONFIG_OF_IDE_FIXUP)
1016 int ide_device_present(int dev)
1018 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1020 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1023 /* ------------------------------------------------------------------------- */
1026 /****************************************************************************
1030 #if defined(CONFIG_IDE_SWAP_IO)
1031 /* since ATAPI may use commands with not 4 bytes alligned length
1032 * we have our own transfer functions, 2 bytes alligned */
1033 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1036 volatile ushort *pbuf;
1038 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1039 dbuf = (ushort *) sect_buf;
1041 debug("in output data shorts base for read is %lx\n",
1042 (unsigned long) pbuf);
1050 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1053 volatile ushort *pbuf;
1055 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1056 dbuf = (ushort *) sect_buf;
1058 debug("in input data shorts base for read is %lx\n",
1059 (unsigned long) pbuf);
1067 #else /* ! CONFIG_IDE_SWAP_IO */
1068 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1070 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1073 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1075 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1078 #endif /* CONFIG_IDE_SWAP_IO */
1081 * Wait until (Status & mask) == res, or timeout (in ms)
1082 * Return last status
1083 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1084 * and then they set their DRQ Bit
1086 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1088 ulong delay = 10 * t; /* poll every 100 us */
1091 /* prevents to read the status before valid */
1092 c = ide_inb(dev, ATA_DEV_CTL);
1094 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1095 /* break if error occurs (doesn't make sense to wait more) */
1096 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1106 * issue an atapi command
1108 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1109 unsigned char *buffer, int buflen)
1111 unsigned char c, err, mask, res;
1114 ide_led(DEVICE_LED(device), 1); /* LED on */
1118 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1120 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1121 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1122 if ((c & mask) != res) {
1123 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1128 /* write taskfile */
1129 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1130 ide_outb(device, ATA_SECT_CNT, 0);
1131 ide_outb(device, ATA_SECT_NUM, 0);
1132 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1133 ide_outb(device, ATA_CYL_HIGH,
1134 (unsigned char) ((buflen >> 8) & 0xFF));
1135 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1137 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1140 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1142 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1144 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1145 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1151 /* write command block */
1152 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1154 /* ATAPI Command written wait for completition */
1155 udelay(5000); /* device must set bsy */
1157 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1159 * if no data wait for DRQ = 0 BSY = 0
1160 * if data wait for DRQ = 1 BSY = 0
1165 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1166 if ((c & mask) != res) {
1167 if (c & ATA_STAT_ERR) {
1168 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1169 debug("atapi_issue 1 returned sense key %X status %02X\n",
1172 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1178 n = ide_inb(device, ATA_CYL_HIGH);
1180 n += ide_inb(device, ATA_CYL_LOW);
1182 printf("ERROR, transfer bytes %d requested only %d\n", n,
1187 if ((n == 0) && (buflen < 0)) {
1188 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1193 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1196 if (n != 0) { /* data transfer */
1197 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1198 /* we transfer shorts */
1200 /* ok now decide if it is an in or output */
1201 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1202 debug("Write to device\n");
1203 ide_output_data_shorts(device,
1204 (unsigned short *) buffer, n);
1206 debug("Read from device @ %p shorts %d\n", buffer, n);
1207 ide_input_data_shorts(device,
1208 (unsigned short *) buffer, n);
1211 udelay(5000); /* seems that some CD ROMs need this... */
1212 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1214 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1215 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1216 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1217 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1223 ide_led(DEVICE_LED(device), 0); /* LED off */
1228 * sending the command to atapi_issue. If an status other than good
1229 * returns, an request_sense will be issued
1232 #define ATAPI_DRIVE_NOT_READY 100
1233 #define ATAPI_UNIT_ATTN 10
1235 unsigned char atapi_issue_autoreq(int device,
1238 unsigned char *buffer, int buflen)
1240 unsigned char sense_data[18], sense_ccb[12];
1241 unsigned char res, key, asc, ascq;
1242 int notready, unitattn;
1244 unitattn = ATAPI_UNIT_ATTN;
1245 notready = ATAPI_DRIVE_NOT_READY;
1248 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1253 return 0xFF; /* error */
1255 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1257 memset(sense_ccb, 0, sizeof(sense_ccb));
1258 memset(sense_data, 0, sizeof(sense_data));
1259 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1260 sense_ccb[4] = 18; /* allocation Length */
1262 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1263 key = (sense_data[2] & 0xF);
1264 asc = (sense_data[12]);
1265 ascq = (sense_data[13]);
1267 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1268 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1269 sense_data[0], key, asc, ascq);
1272 return 0; /* ok device ready */
1274 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1275 if (unitattn-- > 0) {
1279 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1282 if ((asc == 0x4) && (ascq == 0x1)) {
1283 /* not ready, but will be ready soon */
1284 if (notready-- > 0) {
1288 printf("Drive not ready, tried %d times\n",
1289 ATAPI_DRIVE_NOT_READY);
1293 debug("Media not present\n");
1297 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1300 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1305 static void atapi_inquiry(struct blk_desc *dev_desc)
1307 unsigned char ccb[12]; /* Command descriptor block */
1308 unsigned char iobuf[64]; /* temp buf */
1312 device = dev_desc->devnum;
1313 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1314 dev_desc->block_read = atapi_read;
1316 memset(ccb, 0, sizeof(ccb));
1317 memset(iobuf, 0, sizeof(iobuf));
1319 ccb[0] = ATAPI_CMD_INQUIRY;
1320 ccb[4] = 40; /* allocation Legnth */
1321 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1323 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1327 /* copy device ident strings */
1328 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1329 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1330 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1334 dev_desc->blksz = 0;
1335 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
1336 dev_desc->type = iobuf[0] & 0x1f;
1338 if ((iobuf[1] & 0x80) == 0x80)
1339 dev_desc->removable = 1;
1341 dev_desc->removable = 0;
1343 memset(ccb, 0, sizeof(ccb));
1344 memset(iobuf, 0, sizeof(iobuf));
1345 ccb[0] = ATAPI_CMD_START_STOP;
1346 ccb[4] = 0x03; /* start */
1348 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1350 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1354 memset(ccb, 0, sizeof(ccb));
1355 memset(iobuf, 0, sizeof(iobuf));
1356 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1358 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1362 memset(ccb, 0, sizeof(ccb));
1363 memset(iobuf, 0, sizeof(iobuf));
1364 ccb[0] = ATAPI_CMD_READ_CAP;
1365 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1366 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1370 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1371 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1372 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1374 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1375 ((unsigned long) iobuf[1] << 16) +
1376 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1377 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1378 ((unsigned long) iobuf[5] << 16) +
1379 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1380 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1382 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1383 dev_desc->lba48 = 0;
1391 * we transfer only one block per command, since the multiple DRQ per
1392 * command is not yet implemented
1394 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1395 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1396 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1398 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1401 int device = block_dev->devnum;
1403 unsigned char ccb[12]; /* Command descriptor block */
1406 debug("atapi_read dev %d start " LBAF " blocks " LBAF
1407 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
1410 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1411 cnt = ATAPI_READ_MAX_BLOCK;
1415 ccb[0] = ATAPI_CMD_READ_12;
1416 ccb[1] = 0; /* reserved */
1417 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1418 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1419 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1420 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1421 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1422 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1423 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1424 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1425 ccb[10] = 0; /* reserved */
1426 ccb[11] = 0; /* reserved */
1428 if (atapi_issue_autoreq(device, ccb, 12,
1429 (unsigned char *) buffer,
1430 cnt * ATAPI_READ_BLOCK_SIZE)
1437 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1438 } while (blkcnt > 0);
1442 /* ------------------------------------------------------------------------- */
1444 #endif /* CONFIG_ATAPI */
1446 U_BOOT_CMD(ide, 5, 1, do_ide,
1448 "reset - reset IDE controller\n"
1449 "ide info - show available IDE devices\n"
1450 "ide device [dev] - show or set current device\n"
1451 "ide part [dev] - print partition table of one or all IDE devices\n"
1452 "ide read addr blk# cnt\n"
1453 "ide write addr blk# cnt - read/write `cnt'"
1454 " blocks starting at block `blk#'\n"
1455 " to/from memory address `addr'");
1457 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1458 "boot from IDE device", "loadAddr dev:part");