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")));
329 #ifdef CONFIG_IDE_8xx_PCCARD
330 extern int ide_devices_found; /* Initialized in check_ide_device() */
331 #endif /* CONFIG_IDE_8xx_PCCARD */
333 #ifdef CONFIG_IDE_PREINIT
337 puts("ide_preinit failed\n");
340 #endif /* CONFIG_IDE_PREINIT */
345 * Reset the IDE just to be sure.
346 * Light LED's to show
348 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
350 /* ATAPI Drives seems to need a proper IDE Reset */
353 #ifdef CONFIG_IDE_INIT_POSTRESET
356 if (ide_init_postreset()) {
357 puts("ide_preinit_postreset failed\n");
360 #endif /* CONFIG_IDE_INIT_POSTRESET */
363 * Wait for IDE to get ready.
364 * According to spec, this can take up to 31 seconds!
366 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
368 bus * (CONFIG_SYS_IDE_MAXDEVICE /
369 CONFIG_SYS_IDE_MAXBUS);
371 #ifdef CONFIG_IDE_8xx_PCCARD
372 /* Skip non-ide devices from probing */
373 if ((ide_devices_found & (1 << bus)) == 0) {
374 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
378 printf("Bus %d: ", bus);
384 udelay(100000); /* 100 ms */
385 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
386 udelay(100000); /* 100 ms */
389 udelay(10000); /* 10 ms */
391 c = ide_inb(dev, ATA_STATUS);
393 if (i > (ATA_RESET_TIME * 100)) {
394 puts("** Timeout **\n");
396 ide_led((LED_IDE1 | LED_IDE2), 0);
399 if ((i >= 100) && ((i % 100) == 0))
402 } while (c & ATA_STAT_BUSY);
404 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
405 puts("not available ");
406 debug("Status = 0x%02X ", c);
407 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
408 } else if ((c & ATA_STAT_READY) == 0) {
409 puts("not available ");
410 debug("Status = 0x%02X ", c);
421 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
424 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
425 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
426 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
427 ide_dev_desc[i].if_type = IF_TYPE_IDE;
428 ide_dev_desc[i].dev = i;
429 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
430 ide_dev_desc[i].blksz = 0;
431 ide_dev_desc[i].log2blksz =
432 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
433 ide_dev_desc[i].lba = 0;
434 ide_dev_desc[i].block_read = ide_read;
435 ide_dev_desc[i].block_write = ide_write;
436 if (!ide_bus_ok[IDE_BUS(i)])
438 ide_led(led, 1); /* LED on */
439 ide_ident(&ide_dev_desc[i]);
440 ide_led(led, 0); /* LED off */
441 dev_print(&ide_dev_desc[i]);
443 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
444 /* initialize partition type */
445 init_part(&ide_dev_desc[i]);
453 /* ------------------------------------------------------------------------- */
455 #ifdef CONFIG_PARTITIONS
456 block_dev_desc_t *ide_get_dev(int dev)
458 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
462 /* ------------------------------------------------------------------------- */
464 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
465 __attribute__ ((weak, alias("__ide_input_swap_data")));
467 void ide_input_data(int dev, ulong *sect_buf, int words)
468 __attribute__ ((weak, alias("__ide_input_data")));
470 void ide_output_data(int dev, const ulong *sect_buf, int words)
471 __attribute__ ((weak, alias("__ide_output_data")));
473 /* We only need to swap data if we are running on a big endian cpu. */
474 #if defined(__LITTLE_ENDIAN)
475 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
477 ide_input_data(dev, sect_buf, words);
480 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
482 volatile ushort *pbuf =
483 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
484 ushort *dbuf = (ushort *) sect_buf;
486 debug("in input swap data base for read is %lx\n",
487 (unsigned long) pbuf);
491 *dbuf++ = swab16p((u16 *) pbuf);
492 *dbuf++ = swab16p((u16 *) pbuf);
494 *dbuf++ = ld_le16(pbuf);
495 *dbuf++ = ld_le16(pbuf);
499 #endif /* __LITTLE_ENDIAN */
502 #if defined(CONFIG_IDE_SWAP_IO)
503 void __ide_output_data(int dev, const ulong *sect_buf, int words)
506 volatile ushort *pbuf;
508 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
509 dbuf = (ushort *) sect_buf;
517 #else /* ! CONFIG_IDE_SWAP_IO */
518 void __ide_output_data(int dev, const ulong *sect_buf, int words)
520 #if defined(CONFIG_IDE_AHB)
521 ide_write_data(dev, sect_buf, words);
523 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
526 #endif /* CONFIG_IDE_SWAP_IO */
528 #if defined(CONFIG_IDE_SWAP_IO)
529 void __ide_input_data(int dev, ulong *sect_buf, int words)
532 volatile ushort *pbuf;
534 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
535 dbuf = (ushort *) sect_buf;
537 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
546 #else /* ! CONFIG_IDE_SWAP_IO */
547 void __ide_input_data(int dev, ulong *sect_buf, int words)
549 #if defined(CONFIG_IDE_AHB)
550 ide_read_data(dev, sect_buf, words);
552 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
556 #endif /* CONFIG_IDE_SWAP_IO */
558 /* -------------------------------------------------------------------------
560 static void ide_ident(block_dev_desc_t *dev_desc)
570 device = dev_desc->dev;
571 printf(" Device %d: ", device);
573 ide_led(DEVICE_LED(device), 1); /* LED on */
576 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
577 dev_desc->if_type = IF_TYPE_IDE;
582 /* Warning: This will be tricky to read */
583 while (retries <= 1) {
584 /* check signature */
585 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
586 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
587 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
588 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
589 /* ATAPI Signature found */
590 dev_desc->if_type = IF_TYPE_ATAPI;
592 * Start Ident Command
594 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
596 * Wait for completion - ATAPI devices need more time
599 c = ide_wait(device, ATAPI_TIME_OUT);
604 * Start Ident Command
606 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
609 * Wait for completion
611 c = ide_wait(device, IDE_TIME_OUT);
613 ide_led(DEVICE_LED(device), 0); /* LED off */
615 if (((c & ATA_STAT_DRQ) == 0) ||
616 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
620 * Need to soft reset the device
621 * in case it's an ATAPI...
623 debug("Retrying...\n");
624 ide_outb(device, ATA_DEV_HD,
625 ATA_LBA | ATA_DEVICE(device));
627 ide_outb(device, ATA_COMMAND, 0x08);
628 udelay(500000); /* 500 ms */
633 ide_outb(device, ATA_DEV_HD,
634 ATA_LBA | ATA_DEVICE(device));
643 } /* see above - ugly to read */
645 if (retries == 2) /* Not found */
649 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
651 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
652 sizeof(dev_desc->revision));
653 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
654 sizeof(dev_desc->vendor));
655 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
656 sizeof(dev_desc->product));
657 #ifdef __LITTLE_ENDIAN
659 * firmware revision, model, and serial number have Big Endian Byte
660 * order in Word. Convert all three to little endian.
662 * See CF+ and CompactFlash Specification Revision 2.0:
663 * 6.2.1.6: Identify Drive, Table 39 for more details
666 strswab(dev_desc->revision);
667 strswab(dev_desc->vendor);
668 strswab(dev_desc->product);
669 #endif /* __LITTLE_ENDIAN */
671 if ((iop.config & 0x0080) == 0x0080)
672 dev_desc->removable = 1;
674 dev_desc->removable = 0;
677 if (dev_desc->if_type == IF_TYPE_ATAPI) {
678 atapi_inquiry(dev_desc);
681 #endif /* CONFIG_ATAPI */
685 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
686 #else /* ! __BIG_ENDIAN */
688 * do not swap shorts on little endian
690 * See CF+ and CompactFlash Specification Revision 2.0:
691 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
693 dev_desc->lba = iop.lba_capacity;
694 #endif /* __BIG_ENDIAN */
697 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
699 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
700 ((unsigned long long) iop.lba48_capacity[1] << 16) |
701 ((unsigned long long) iop.lba48_capacity[2] << 32) |
702 ((unsigned long long) iop.lba48_capacity[3] << 48);
706 #endif /* CONFIG_LBA48 */
708 dev_desc->type = DEV_TYPE_HARDDISK;
709 dev_desc->blksz = ATA_BLOCKSIZE;
710 dev_desc->log2blksz = LOG2(dev_desc->blksz);
711 dev_desc->lun = 0; /* just to fill something in... */
713 #if 0 /* only used to test the powersaving mode,
714 * if enabled, the drive goes after 5 sec
716 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
717 c = ide_wait(device, IDE_TIME_OUT);
718 ide_outb(device, ATA_SECT_CNT, 1);
719 ide_outb(device, ATA_LBA_LOW, 0);
720 ide_outb(device, ATA_LBA_MID, 0);
721 ide_outb(device, ATA_LBA_HIGH, 0);
722 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
723 ide_outb(device, ATA_COMMAND, 0xe3);
725 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
730 /* ------------------------------------------------------------------------- */
732 ulong ide_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
736 unsigned char pwrsave = 0; /* power save */
739 unsigned char lba48 = 0;
741 if (blknr & 0x0000fffff0000000ULL) {
742 /* more than 28 bits used, use 48bit mode */
746 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
747 device, blknr, blkcnt, (ulong) buffer);
749 ide_led(DEVICE_LED(device), 1); /* LED on */
753 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
754 c = ide_wait(device, IDE_TIME_OUT);
756 if (c & ATA_STAT_BUSY) {
757 printf("IDE read: device %d not ready\n", device);
761 /* first check if the drive is in Powersaving mode, if yes,
762 * increase the timeout value */
763 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
766 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
768 if (c & ATA_STAT_BUSY) {
769 printf("IDE read: device %d not ready\n", device);
772 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
773 printf("No Powersaving mode %X\n", c);
775 c = ide_inb(device, ATA_SECT_CNT);
776 debug("Powersaving %02X\n", c);
782 while (blkcnt-- > 0) {
784 c = ide_wait(device, IDE_TIME_OUT);
786 if (c & ATA_STAT_BUSY) {
787 printf("IDE read: device %d not ready\n", device);
792 /* write high bits */
793 ide_outb(device, ATA_SECT_CNT, 0);
794 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
795 #ifdef CONFIG_SYS_64BIT_LBA
796 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
797 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
799 ide_outb(device, ATA_LBA_MID, 0);
800 ide_outb(device, ATA_LBA_HIGH, 0);
804 ide_outb(device, ATA_SECT_CNT, 1);
805 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
806 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
807 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
811 ide_outb(device, ATA_DEV_HD,
812 ATA_LBA | ATA_DEVICE(device));
813 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
818 ide_outb(device, ATA_DEV_HD, ATA_LBA |
819 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
820 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
826 /* may take up to 4 sec */
827 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
830 /* can't take over 500 ms */
831 c = ide_wait(device, IDE_TIME_OUT);
834 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
836 printf("Error (no IRQ) dev %d blk " LBAF ": status "
837 "%#02x\n", device, blknr, c);
841 ide_input_data(device, buffer, ATA_SECTORWORDS);
842 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
846 buffer += ATA_BLOCKSIZE;
849 ide_led(DEVICE_LED(device), 0); /* LED off */
853 /* ------------------------------------------------------------------------- */
856 ulong ide_write(int device, lbaint_t blknr, lbaint_t blkcnt, const void *buffer)
862 unsigned char lba48 = 0;
864 if (blknr & 0x0000fffff0000000ULL) {
865 /* more than 28 bits used, use 48bit mode */
870 ide_led(DEVICE_LED(device), 1); /* LED on */
874 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
876 while (blkcnt-- > 0) {
878 c = ide_wait(device, IDE_TIME_OUT);
880 if (c & ATA_STAT_BUSY) {
881 printf("IDE read: device %d not ready\n", device);
886 /* write high bits */
887 ide_outb(device, ATA_SECT_CNT, 0);
888 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
889 #ifdef CONFIG_SYS_64BIT_LBA
890 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
891 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
893 ide_outb(device, ATA_LBA_MID, 0);
894 ide_outb(device, ATA_LBA_HIGH, 0);
898 ide_outb(device, ATA_SECT_CNT, 1);
899 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
900 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
901 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
905 ide_outb(device, ATA_DEV_HD,
906 ATA_LBA | ATA_DEVICE(device));
907 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
912 ide_outb(device, ATA_DEV_HD, ATA_LBA |
913 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
914 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
919 /* can't take over 500 ms */
920 c = ide_wait(device, IDE_TIME_OUT);
922 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
924 printf("Error (no IRQ) dev %d blk " LBAF ": status "
925 "%#02x\n", device, blknr, c);
929 ide_output_data(device, buffer, ATA_SECTORWORDS);
930 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
933 buffer += ATA_BLOCKSIZE;
936 ide_led(DEVICE_LED(device), 0); /* LED off */
940 /* ------------------------------------------------------------------------- */
943 * copy src to dest, skipping leading and trailing blanks and null
944 * terminate the string
945 * "len" is the size of available memory including the terminating '\0'
947 static void ident_cpy(unsigned char *dst, unsigned char *src,
950 unsigned char *end, *last;
955 /* reserve space for '\0' */
959 /* skip leading white space */
960 while ((*src) && (src < end) && (*src == ' '))
963 /* copy string, omitting trailing white space */
964 while ((*src) && (src < end)) {
973 /* ------------------------------------------------------------------------- */
976 * Wait until Busy bit is off, or timeout (in ms)
979 static uchar ide_wait(int dev, ulong t)
981 ulong delay = 10 * t; /* poll every 100 us */
984 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
992 /* ------------------------------------------------------------------------- */
994 #ifdef CONFIG_IDE_RESET
995 extern void ide_set_reset(int idereset);
997 static void ide_reset(void)
1002 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1004 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1005 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1007 ide_set_reset(1); /* assert reset */
1009 /* the reset signal shall be asserted for et least 25 us */
1014 /* de-assert RESET signal */
1018 for (i = 0; i < 250; ++i)
1022 #endif /* CONFIG_IDE_RESET */
1024 /* ------------------------------------------------------------------------- */
1026 #if defined(CONFIG_OF_IDE_FIXUP)
1027 int ide_device_present(int dev)
1029 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1031 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1034 /* ------------------------------------------------------------------------- */
1037 /****************************************************************************
1041 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1042 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1044 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1045 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1048 #if defined(CONFIG_IDE_SWAP_IO)
1049 /* since ATAPI may use commands with not 4 bytes alligned length
1050 * we have our own transfer functions, 2 bytes alligned */
1051 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1054 volatile ushort *pbuf;
1056 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1057 dbuf = (ushort *) sect_buf;
1059 debug("in output data shorts base for read is %lx\n",
1060 (unsigned long) pbuf);
1068 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1071 volatile ushort *pbuf;
1073 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1074 dbuf = (ushort *) sect_buf;
1076 debug("in input data shorts base for read is %lx\n",
1077 (unsigned long) pbuf);
1085 #else /* ! CONFIG_IDE_SWAP_IO */
1086 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1088 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1091 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1093 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1096 #endif /* CONFIG_IDE_SWAP_IO */
1099 * Wait until (Status & mask) == res, or timeout (in ms)
1100 * Return last status
1101 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1102 * and then they set their DRQ Bit
1104 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1106 ulong delay = 10 * t; /* poll every 100 us */
1109 /* prevents to read the status before valid */
1110 c = ide_inb(dev, ATA_DEV_CTL);
1112 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1113 /* break if error occurs (doesn't make sense to wait more) */
1114 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1124 * issue an atapi command
1126 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1127 unsigned char *buffer, int buflen)
1129 unsigned char c, err, mask, res;
1132 ide_led(DEVICE_LED(device), 1); /* LED on */
1136 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1138 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1139 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1140 if ((c & mask) != res) {
1141 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1146 /* write taskfile */
1147 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1148 ide_outb(device, ATA_SECT_CNT, 0);
1149 ide_outb(device, ATA_SECT_NUM, 0);
1150 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1151 ide_outb(device, ATA_CYL_HIGH,
1152 (unsigned char) ((buflen >> 8) & 0xFF));
1153 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1155 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1158 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1160 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1162 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1163 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1169 /* write command block */
1170 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1172 /* ATAPI Command written wait for completition */
1173 udelay(5000); /* device must set bsy */
1175 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1177 * if no data wait for DRQ = 0 BSY = 0
1178 * if data wait for DRQ = 1 BSY = 0
1183 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1184 if ((c & mask) != res) {
1185 if (c & ATA_STAT_ERR) {
1186 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1187 debug("atapi_issue 1 returned sense key %X status %02X\n",
1190 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1196 n = ide_inb(device, ATA_CYL_HIGH);
1198 n += ide_inb(device, ATA_CYL_LOW);
1200 printf("ERROR, transfer bytes %d requested only %d\n", n,
1205 if ((n == 0) && (buflen < 0)) {
1206 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1211 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1214 if (n != 0) { /* data transfer */
1215 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1216 /* we transfer shorts */
1218 /* ok now decide if it is an in or output */
1219 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1220 debug("Write to device\n");
1221 ide_output_data_shorts(device,
1222 (unsigned short *) buffer, n);
1224 debug("Read from device @ %p shorts %d\n", buffer, n);
1225 ide_input_data_shorts(device,
1226 (unsigned short *) buffer, n);
1229 udelay(5000); /* seems that some CD ROMs need this... */
1230 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1232 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1233 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1234 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1235 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1241 ide_led(DEVICE_LED(device), 0); /* LED off */
1246 * sending the command to atapi_issue. If an status other than good
1247 * returns, an request_sense will be issued
1250 #define ATAPI_DRIVE_NOT_READY 100
1251 #define ATAPI_UNIT_ATTN 10
1253 unsigned char atapi_issue_autoreq(int device,
1256 unsigned char *buffer, int buflen)
1258 unsigned char sense_data[18], sense_ccb[12];
1259 unsigned char res, key, asc, ascq;
1260 int notready, unitattn;
1262 unitattn = ATAPI_UNIT_ATTN;
1263 notready = ATAPI_DRIVE_NOT_READY;
1266 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1271 return 0xFF; /* error */
1273 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1275 memset(sense_ccb, 0, sizeof(sense_ccb));
1276 memset(sense_data, 0, sizeof(sense_data));
1277 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1278 sense_ccb[4] = 18; /* allocation Length */
1280 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1281 key = (sense_data[2] & 0xF);
1282 asc = (sense_data[12]);
1283 ascq = (sense_data[13]);
1285 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1286 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1287 sense_data[0], key, asc, ascq);
1290 return 0; /* ok device ready */
1292 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1293 if (unitattn-- > 0) {
1297 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1300 if ((asc == 0x4) && (ascq == 0x1)) {
1301 /* not ready, but will be ready soon */
1302 if (notready-- > 0) {
1306 printf("Drive not ready, tried %d times\n",
1307 ATAPI_DRIVE_NOT_READY);
1311 debug("Media not present\n");
1315 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1318 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1323 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1325 unsigned char ccb[12]; /* Command descriptor block */
1326 unsigned char iobuf[64]; /* temp buf */
1330 device = dev_desc->dev;
1331 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1332 dev_desc->block_read = atapi_read;
1334 memset(ccb, 0, sizeof(ccb));
1335 memset(iobuf, 0, sizeof(iobuf));
1337 ccb[0] = ATAPI_CMD_INQUIRY;
1338 ccb[4] = 40; /* allocation Legnth */
1339 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1341 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1345 /* copy device ident strings */
1346 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1347 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1348 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1352 dev_desc->blksz = 0;
1353 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
1354 dev_desc->type = iobuf[0] & 0x1f;
1356 if ((iobuf[1] & 0x80) == 0x80)
1357 dev_desc->removable = 1;
1359 dev_desc->removable = 0;
1361 memset(ccb, 0, sizeof(ccb));
1362 memset(iobuf, 0, sizeof(iobuf));
1363 ccb[0] = ATAPI_CMD_START_STOP;
1364 ccb[4] = 0x03; /* start */
1366 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1368 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1372 memset(ccb, 0, sizeof(ccb));
1373 memset(iobuf, 0, sizeof(iobuf));
1374 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1376 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1380 memset(ccb, 0, sizeof(ccb));
1381 memset(iobuf, 0, sizeof(iobuf));
1382 ccb[0] = ATAPI_CMD_READ_CAP;
1383 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1384 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1388 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1389 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1390 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1392 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1393 ((unsigned long) iobuf[1] << 16) +
1394 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1395 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1396 ((unsigned long) iobuf[5] << 16) +
1397 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1398 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1400 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1401 dev_desc->lba48 = 0;
1409 * we transfer only one block per command, since the multiple DRQ per
1410 * command is not yet implemented
1412 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1413 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1414 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1416 ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
1419 unsigned char ccb[12]; /* Command descriptor block */
1422 debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
1423 device, blknr, blkcnt, (ulong) buffer);
1426 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1427 cnt = ATAPI_READ_MAX_BLOCK;
1431 ccb[0] = ATAPI_CMD_READ_12;
1432 ccb[1] = 0; /* reserved */
1433 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1434 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1435 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1436 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1437 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1438 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1439 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1440 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1441 ccb[10] = 0; /* reserved */
1442 ccb[11] = 0; /* reserved */
1444 if (atapi_issue_autoreq(device, ccb, 12,
1445 (unsigned char *) buffer,
1446 cnt * ATAPI_READ_BLOCK_SIZE)
1453 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1454 } while (blkcnt > 0);
1458 /* ------------------------------------------------------------------------- */
1460 #endif /* CONFIG_ATAPI */
1462 U_BOOT_CMD(ide, 5, 1, do_ide,
1464 "reset - reset IDE controller\n"
1465 "ide info - show available IDE devices\n"
1466 "ide device [dev] - show or set current device\n"
1467 "ide part [dev] - print partition table of one or all IDE devices\n"
1468 "ide read addr blk# cnt\n"
1469 "ide write addr blk# cnt - read/write `cnt'"
1470 " blocks starting at block `blk#'\n"
1471 " to/from memory address `addr'");
1473 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1474 "boot from IDE device", "loadAddr dev:part");