2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #include <asm/byteorder.h>
37 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
52 #ifdef CONFIG_STATUS_LED
53 # include <status_led.h>
56 #ifdef CONFIG_IDE_8xx_DIRECT
57 DECLARE_GLOBAL_DATA_PTR;
61 # define EIEIO __asm__ volatile ("eieio")
62 # define SYNC __asm__ volatile ("sync")
64 # define EIEIO /* nothing */
65 # define SYNC /* nothing */
68 #ifdef CONFIG_IDE_8xx_DIRECT
69 /* Timings for IDE Interface
71 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
72 * 70 165 30 PIO-Mode 0, [ns]
74 * 50 125 20 PIO-Mode 1, [ns]
76 * 30 100 15 PIO-Mode 2, [ns]
78 * 30 80 10 PIO-Mode 3, [ns]
80 * 25 70 10 PIO-Mode 4, [ns]
84 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
86 /* Setup Length Hold */
87 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
88 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
89 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
90 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
91 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
94 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
96 #ifndef CONFIG_SYS_PIO_MODE
97 #define CONFIG_SYS_PIO_MODE 0 /* use a relaxed default */
99 static int pio_mode = CONFIG_SYS_PIO_MODE;
101 /* Make clock cycles and always round up */
103 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
105 #endif /* CONFIG_IDE_8xx_DIRECT */
107 /* ------------------------------------------------------------------------- */
109 /* Current I/O Device */
110 static int curr_device = -1;
112 /* Current offset for IDE0 / IDE1 bus access */
113 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
114 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
115 CONFIG_SYS_ATA_IDE0_OFFSET,
117 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
118 CONFIG_SYS_ATA_IDE1_OFFSET,
122 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
124 block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
125 /* ------------------------------------------------------------------------- */
127 #ifdef CONFIG_IDE_LED
128 # if !defined(CONFIG_BMS2003) && \
129 !defined(CONFIG_CPC45) && \
130 !defined(CONFIG_KUP4K) && \
131 !defined(CONFIG_KUP4X)
132 static void ide_led (uchar led, uchar status);
134 extern void ide_led (uchar led, uchar status);
137 #define ide_led(a,b) /* dummy */
140 #ifdef CONFIG_IDE_RESET
141 static void ide_reset (void);
143 #define ide_reset() /* dummy */
146 static void ide_ident (block_dev_desc_t *dev_desc);
147 static uchar ide_wait (int dev, ulong t);
149 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
151 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
153 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
155 static void input_data(int dev, ulong *sect_buf, int words);
156 static void output_data(int dev, const ulong *sect_buf, int words);
157 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
159 #ifndef CONFIG_SYS_ATA_PORT_ADDR
160 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
164 static void atapi_inquiry(block_dev_desc_t *dev_desc);
165 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
169 #ifdef CONFIG_IDE_8xx_DIRECT
170 static void set_pcmcia_timing (int pmode);
173 /* ------------------------------------------------------------------------- */
175 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
182 return CMD_RET_USAGE;
184 if (strncmp(argv[1], "res", 3) == 0) {
186 #ifdef CONFIG_IDE_8xx_DIRECT
187 " on PCMCIA " PCMCIA_SLOT_MSG
193 } else if (strncmp(argv[1], "inf", 3) == 0) {
198 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
199 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
200 continue; /* list only known devices */
201 printf("IDE device %d: ", i);
202 dev_print(&ide_dev_desc[i]);
206 } else if (strncmp(argv[1], "dev", 3) == 0) {
207 if ((curr_device < 0)
208 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
209 puts("\nno IDE devices available\n");
212 printf("\nIDE device %d: ", curr_device);
213 dev_print(&ide_dev_desc[curr_device]);
215 } else if (strncmp(argv[1], "part", 4) == 0) {
218 for (ok = 0, dev = 0;
219 dev < CONFIG_SYS_IDE_MAXDEVICE;
221 if (ide_dev_desc[dev].part_type !=
226 print_part(&ide_dev_desc[dev]);
230 puts("\nno IDE devices available\n");
235 return CMD_RET_USAGE;
237 if (strncmp(argv[1], "dev", 3) == 0) {
238 int dev = (int) simple_strtoul(argv[2], NULL, 10);
240 printf("\nIDE device %d: ", dev);
241 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
242 puts("unknown device\n");
245 dev_print(&ide_dev_desc[dev]);
246 /*ide_print (dev); */
248 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
253 puts("... is now current device\n");
256 } else if (strncmp(argv[1], "part", 4) == 0) {
257 int dev = (int) simple_strtoul(argv[2], NULL, 10);
259 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
260 print_part(&ide_dev_desc[dev]);
262 printf("\nIDE device %d not available\n",
269 return CMD_RET_USAGE;
271 /* at least 4 args */
273 if (strcmp(argv[1], "read") == 0) {
274 ulong addr = simple_strtoul(argv[2], NULL, 16);
275 ulong cnt = simple_strtoul(argv[4], NULL, 16);
278 #ifdef CONFIG_SYS_64BIT_LBA
279 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
281 printf("\nIDE read: device %d block # %lld, count %ld ... ",
282 curr_device, blk, cnt);
284 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
286 printf("\nIDE read: device %d block # %ld, count %ld ... ",
287 curr_device, blk, cnt);
290 n = ide_dev_desc[curr_device].block_read(curr_device,
293 /* flush cache after read */
295 cnt * ide_dev_desc[curr_device].blksz);
297 printf("%ld blocks read: %s\n",
298 n, (n == cnt) ? "OK" : "ERROR");
303 } else if (strcmp(argv[1], "write") == 0) {
304 ulong addr = simple_strtoul(argv[2], NULL, 16);
305 ulong cnt = simple_strtoul(argv[4], NULL, 16);
308 #ifdef CONFIG_SYS_64BIT_LBA
309 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
311 printf("\nIDE write: device %d block # %lld, count %ld ... ",
312 curr_device, blk, cnt);
314 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
316 printf("\nIDE write: device %d block # %ld, count %ld ... ",
317 curr_device, blk, cnt);
319 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
321 printf("%ld blocks written: %s\n",
322 n, (n == cnt) ? "OK" : "ERROR");
328 return CMD_RET_USAGE;
335 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
337 return common_diskboot(cmdtp, "ide", argc, argv);
340 /* ------------------------------------------------------------------------- */
342 inline void __ide_outb(int dev, int port, unsigned char val)
344 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
346 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
348 #if defined(CONFIG_IDE_AHB)
351 ide_write_register(dev, port, val);
354 outb(val, (ATA_CURR_BASE(dev)));
357 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
361 void ide_outb(int dev, int port, unsigned char val)
362 __attribute__ ((weak, alias("__ide_outb")));
364 inline unsigned char __ide_inb(int dev, int port)
368 #if defined(CONFIG_IDE_AHB)
369 val = ide_read_register(dev, port);
371 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
374 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
376 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
380 unsigned char ide_inb(int dev, int port)
381 __attribute__ ((weak, alias("__ide_inb")));
383 #ifdef CONFIG_TUNE_PIO
384 inline int __ide_set_piomode(int pio_mode)
389 inline int ide_set_piomode(int pio_mode)
390 __attribute__ ((weak, alias("__ide_set_piomode")));
396 #ifdef CONFIG_IDE_8xx_DIRECT
397 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
398 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
403 #ifdef CONFIG_IDE_8xx_PCCARD
404 extern int pcmcia_on(void);
405 extern int ide_devices_found; /* Initialized in check_ide_device() */
406 #endif /* CONFIG_IDE_8xx_PCCARD */
408 #ifdef CONFIG_IDE_PREINIT
409 extern int ide_preinit(void);
414 puts("ide_preinit failed\n");
417 #endif /* CONFIG_IDE_PREINIT */
419 #ifdef CONFIG_IDE_8xx_PCCARD
420 extern int pcmcia_on(void);
421 extern int ide_devices_found; /* Initialized in check_ide_device() */
425 ide_devices_found = 0;
426 /* initialize the PCMCIA IDE adapter card */
428 if (!ide_devices_found)
430 udelay(1000000); /* 1 s */
431 #endif /* CONFIG_IDE_8xx_PCCARD */
435 #ifdef CONFIG_IDE_8xx_DIRECT
436 /* Initialize PIO timing tables */
437 for (i = 0; i <= IDE_MAX_PIO_MODE; ++i) {
438 pio_config_clk[i].t_setup =
439 PCMCIA_MK_CLKS(pio_config_ns[i].t_setup, gd->bus_clk);
440 pio_config_clk[i].t_length =
441 PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
443 pio_config_clk[i].t_hold =
444 PCMCIA_MK_CLKS(pio_config_ns[i].t_hold, gd->bus_clk);
445 debug("PIO Mode %d: setup=%2d ns/%d clk" " len=%3d ns/%d clk"
446 " hold=%2d ns/%d clk\n", i, pio_config_ns[i].t_setup,
447 pio_config_clk[i].t_setup, pio_config_ns[i].t_length,
448 pio_config_clk[i].t_length, pio_config_ns[i].t_hold,
449 pio_config_clk[i].t_hold);
451 #endif /* CONFIG_IDE_8xx_DIRECT */
454 * Reset the IDE just to be sure.
455 * Light LED's to show
457 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
459 /* ATAPI Drives seems to need a proper IDE Reset */
462 #ifdef CONFIG_IDE_8xx_DIRECT
463 /* PCMCIA / IDE initialization for common mem space */
464 pcmp->pcmc_pgcrb = 0;
466 /* start in PIO mode 0 - most relaxed timings */
468 set_pcmcia_timing(pio_mode);
469 #endif /* CONFIG_IDE_8xx_DIRECT */
472 * Wait for IDE to get ready.
473 * According to spec, this can take up to 31 seconds!
475 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
477 bus * (CONFIG_SYS_IDE_MAXDEVICE /
478 CONFIG_SYS_IDE_MAXBUS);
480 #ifdef CONFIG_IDE_8xx_PCCARD
481 /* Skip non-ide devices from probing */
482 if ((ide_devices_found & (1 << bus)) == 0) {
483 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
487 printf("Bus %d: ", bus);
493 udelay(100000); /* 100 ms */
494 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
495 udelay(100000); /* 100 ms */
498 udelay(10000); /* 10 ms */
500 c = ide_inb(dev, ATA_STATUS);
502 if (i > (ATA_RESET_TIME * 100)) {
503 puts("** Timeout **\n");
505 ide_led((LED_IDE1 | LED_IDE2), 0);
508 if ((i >= 100) && ((i % 100) == 0))
511 } while (c & ATA_STAT_BUSY);
513 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
514 puts("not available ");
515 debug("Status = 0x%02X ", c);
516 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
517 } else if ((c & ATA_STAT_READY) == 0) {
518 puts("not available ");
519 debug("Status = 0x%02X ", c);
530 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
533 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
534 #ifdef CONFIG_IDE_LED
535 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
537 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
538 ide_dev_desc[i].if_type = IF_TYPE_IDE;
539 ide_dev_desc[i].dev = i;
540 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
541 ide_dev_desc[i].blksz = 0;
542 ide_dev_desc[i].lba = 0;
543 ide_dev_desc[i].block_read = ide_read;
544 ide_dev_desc[i].block_write = ide_write;
545 if (!ide_bus_ok[IDE_BUS(i)])
547 ide_led(led, 1); /* LED on */
548 ide_ident(&ide_dev_desc[i]);
549 ide_led(led, 0); /* LED off */
550 dev_print(&ide_dev_desc[i]);
552 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
553 /* initialize partition type */
554 init_part(&ide_dev_desc[i]);
562 /* ------------------------------------------------------------------------- */
564 #ifdef CONFIG_PARTITIONS
565 block_dev_desc_t *ide_get_dev(int dev)
567 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
572 #ifdef CONFIG_IDE_8xx_DIRECT
574 static void set_pcmcia_timing(int pmode)
576 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
577 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
580 debug("Set timing for PIO Mode %d\n", pmode);
582 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
583 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
584 | PCMCIA_SL(pio_config_clk[pmode].t_length);
589 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
590 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
591 #if (CONFIG_SYS_PCMCIA_POR0 != 0)
595 debug("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
597 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
598 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
599 #if (CONFIG_SYS_PCMCIA_POR1 != 0)
603 debug("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
605 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
606 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
607 #if (CONFIG_SYS_PCMCIA_POR2 != 0)
611 debug("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
613 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
614 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
615 #if (CONFIG_SYS_PCMCIA_POR3 != 0)
619 debug("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
624 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
625 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
626 #if (CONFIG_SYS_PCMCIA_POR4 != 0)
630 debug("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
632 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
633 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
634 #if (CONFIG_SYS_PCMCIA_POR5 != 0)
638 debug("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
640 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
641 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
642 #if (CONFIG_SYS_PCMCIA_POR6 != 0)
646 debug("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
648 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
649 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
650 #if (CONFIG_SYS_PCMCIA_POR7 != 0)
654 debug("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
658 #endif /* CONFIG_IDE_8xx_DIRECT */
660 /* ------------------------------------------------------------------------- */
662 /* We only need to swap data if we are running on a big endian cpu. */
663 /* But Au1x00 cpu:s already swaps data in big endian mode! */
664 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
665 #define input_swap_data(x,y,z) input_data(x,y,z)
667 static void input_swap_data(int dev, ulong *sect_buf, int words)
669 #if defined(CONFIG_CPC45)
671 volatile uchar *pbuf_even =
672 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
673 volatile uchar *pbuf_odd =
674 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
675 ushort *dbuf = (ushort *) sect_buf;
678 for (i = 0; i < 2; i++) {
679 *(((uchar *) (dbuf)) + 1) = *pbuf_even;
680 *(uchar *) dbuf = *pbuf_odd;
685 volatile ushort *pbuf =
686 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
687 ushort *dbuf = (ushort *) sect_buf;
689 debug("in input swap data base for read is %lx\n",
690 (unsigned long) pbuf);
694 *dbuf++ = swab16p((u16 *) pbuf);
695 *dbuf++ = swab16p((u16 *) pbuf);
696 #elif defined(CONFIG_PCS440EP)
700 *dbuf++ = ld_le16(pbuf);
701 *dbuf++ = ld_le16(pbuf);
706 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
709 #if defined(CONFIG_IDE_SWAP_IO)
710 static void output_data(int dev, const ulong *sect_buf, int words)
712 #if defined(CONFIG_CPC45)
714 volatile uchar *pbuf_even;
715 volatile uchar *pbuf_odd;
717 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
718 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
719 dbuf = (uchar *) sect_buf;
722 *pbuf_even = *dbuf++;
726 *pbuf_even = *dbuf++;
732 volatile ushort *pbuf;
734 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
735 dbuf = (ushort *) sect_buf;
737 #if defined(CONFIG_PCS440EP)
738 /* not tested, because CF was write protected */
740 *pbuf = ld_le16(dbuf++);
742 *pbuf = ld_le16(dbuf++);
752 #else /* ! CONFIG_IDE_SWAP_IO */
753 static void output_data(int dev, const ulong *sect_buf, int words)
755 #if defined(CONFIG_IDE_AHB)
756 ide_write_data(dev, sect_buf, words);
758 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
761 #endif /* CONFIG_IDE_SWAP_IO */
763 #if defined(CONFIG_IDE_SWAP_IO)
764 static void input_data(int dev, ulong *sect_buf, int words)
766 #if defined(CONFIG_CPC45)
768 volatile uchar *pbuf_even;
769 volatile uchar *pbuf_odd;
771 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
772 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
773 dbuf = (uchar *) sect_buf;
775 *dbuf++ = *pbuf_even;
781 *dbuf++ = *pbuf_even;
790 volatile ushort *pbuf;
792 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
793 dbuf = (ushort *) sect_buf;
795 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
798 #if defined(CONFIG_PCS440EP)
800 *dbuf++ = ld_le16(pbuf);
802 *dbuf++ = ld_le16(pbuf);
812 #else /* ! CONFIG_IDE_SWAP_IO */
813 static void input_data(int dev, ulong *sect_buf, int words)
815 #if defined(CONFIG_IDE_AHB)
816 ide_read_data(dev, sect_buf, words);
818 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
822 #endif /* CONFIG_IDE_SWAP_IO */
824 /* -------------------------------------------------------------------------
826 static void ide_ident(block_dev_desc_t *dev_desc)
835 #ifdef CONFIG_TUNE_PIO
840 int mode, cycle_time;
844 device = dev_desc->dev;
845 printf(" Device %d: ", device);
847 ide_led(DEVICE_LED(device), 1); /* LED on */
850 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
851 dev_desc->if_type = IF_TYPE_IDE;
856 /* Warning: This will be tricky to read */
857 while (retries <= 1) {
858 /* check signature */
859 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
860 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
861 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
862 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
863 /* ATAPI Signature found */
864 dev_desc->if_type = IF_TYPE_ATAPI;
866 * Start Ident Command
868 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
870 * Wait for completion - ATAPI devices need more time
873 c = ide_wait(device, ATAPI_TIME_OUT);
878 * Start Ident Command
880 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
883 * Wait for completion
885 c = ide_wait(device, IDE_TIME_OUT);
887 ide_led(DEVICE_LED(device), 0); /* LED off */
889 if (((c & ATA_STAT_DRQ) == 0) ||
890 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
894 * Need to soft reset the device
895 * in case it's an ATAPI...
897 debug("Retrying...\n");
898 ide_outb(device, ATA_DEV_HD,
899 ATA_LBA | ATA_DEVICE(device));
901 ide_outb(device, ATA_COMMAND, 0x08);
902 udelay(500000); /* 500 ms */
907 ide_outb(device, ATA_DEV_HD,
908 ATA_LBA | ATA_DEVICE(device));
917 } /* see above - ugly to read */
919 if (retries == 2) /* Not found */
923 input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
925 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
926 sizeof(dev_desc->revision));
927 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
928 sizeof(dev_desc->vendor));
929 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
930 sizeof(dev_desc->product));
931 #ifdef __LITTLE_ENDIAN
933 * firmware revision, model, and serial number have Big Endian Byte
934 * order in Word. Convert all three to little endian.
936 * See CF+ and CompactFlash Specification Revision 2.0:
937 * 6.2.1.6: Identify Drive, Table 39 for more details
940 strswab(dev_desc->revision);
941 strswab(dev_desc->vendor);
942 strswab(dev_desc->product);
943 #endif /* __LITTLE_ENDIAN */
945 if ((iop.config & 0x0080) == 0x0080)
946 dev_desc->removable = 1;
948 dev_desc->removable = 0;
950 #ifdef CONFIG_TUNE_PIO
951 /* Mode 0 - 2 only, are directly determined by word 51. */
954 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
955 /* Force it to dead slow, and hope for the best... */
959 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
960 * shall set bit 1 of word 53 to one and support the fields contained
961 * in words 64 through 70.
963 if (iop.field_valid & 0x02) {
965 * Mode 3 and above are possible. Check in order from slow
966 * to fast, so we wind up with the highest mode allowed.
968 if (iop.eide_pio_modes & 0x01)
970 if (iop.eide_pio_modes & 0x02)
972 if (ata_id_is_cfa((u16 *)&iop)) {
973 if ((iop.cf_advanced_caps & 0x07) == 0x01)
975 if ((iop.cf_advanced_caps & 0x07) == 0x02)
980 /* System-specific, depends on bus speeds, etc. */
981 ide_set_piomode(pio_mode);
982 #endif /* CONFIG_TUNE_PIO */
986 * Drive PIO mode autoselection
990 printf("tPIO = 0x%02x = %d\n", mode, mode);
991 if (mode > 2) { /* 2 is maximum allowed tPIO value */
993 debug("Override tPIO -> 2\n");
995 if (iop.field_valid & 2) { /* drive implements ATA2? */
996 debug("Drive implements ATA2\n");
997 if (iop.capability & 8) { /* drive supports use_iordy? */
998 cycle_time = iop.eide_pio_iordy;
1000 cycle_time = iop.eide_pio;
1002 debug("cycle time = %d\n", cycle_time);
1004 if (cycle_time > 120)
1005 mode = 3; /* 120 ns for PIO mode 4 */
1006 if (cycle_time > 180)
1007 mode = 2; /* 180 ns for PIO mode 3 */
1008 if (cycle_time > 240)
1009 mode = 1; /* 240 ns for PIO mode 4 */
1010 if (cycle_time > 383)
1011 mode = 0; /* 383 ns for PIO mode 4 */
1013 printf("PIO mode to use: PIO %d\n", mode);
1017 if (dev_desc->if_type == IF_TYPE_ATAPI) {
1018 atapi_inquiry(dev_desc);
1021 #endif /* CONFIG_ATAPI */
1025 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
1026 #else /* ! __BIG_ENDIAN */
1028 * do not swap shorts on little endian
1030 * See CF+ and CompactFlash Specification Revision 2.0:
1031 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1033 dev_desc->lba = iop.lba_capacity;
1034 #endif /* __BIG_ENDIAN */
1037 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
1038 dev_desc->lba48 = 1;
1039 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
1040 ((unsigned long long) iop.lba48_capacity[1] << 16) |
1041 ((unsigned long long) iop.lba48_capacity[2] << 32) |
1042 ((unsigned long long) iop.lba48_capacity[3] << 48);
1044 dev_desc->lba48 = 0;
1046 #endif /* CONFIG_LBA48 */
1048 dev_desc->type = DEV_TYPE_HARDDISK;
1049 dev_desc->blksz = ATA_BLOCKSIZE;
1050 dev_desc->lun = 0; /* just to fill something in... */
1052 #if 0 /* only used to test the powersaving mode,
1053 * if enabled, the drive goes after 5 sec
1054 * in standby mode */
1055 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1056 c = ide_wait(device, IDE_TIME_OUT);
1057 ide_outb(device, ATA_SECT_CNT, 1);
1058 ide_outb(device, ATA_LBA_LOW, 0);
1059 ide_outb(device, ATA_LBA_MID, 0);
1060 ide_outb(device, ATA_LBA_HIGH, 0);
1061 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1062 ide_outb(device, ATA_COMMAND, 0xe3);
1064 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
1069 /* ------------------------------------------------------------------------- */
1071 ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1075 unsigned char pwrsave = 0; /* power save */
1078 unsigned char lba48 = 0;
1080 if (blknr & 0x0000fffff0000000ULL) {
1081 /* more than 28 bits used, use 48bit mode */
1085 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1086 device, blknr, blkcnt, (ulong) buffer);
1088 ide_led(DEVICE_LED(device), 1); /* LED on */
1092 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1093 c = ide_wait(device, IDE_TIME_OUT);
1095 if (c & ATA_STAT_BUSY) {
1096 printf("IDE read: device %d not ready\n", device);
1100 /* first check if the drive is in Powersaving mode, if yes,
1101 * increase the timeout value */
1102 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1105 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
1107 if (c & ATA_STAT_BUSY) {
1108 printf("IDE read: device %d not ready\n", device);
1111 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1112 printf("No Powersaving mode %X\n", c);
1114 c = ide_inb(device, ATA_SECT_CNT);
1115 debug("Powersaving %02X\n", c);
1121 while (blkcnt-- > 0) {
1123 c = ide_wait(device, IDE_TIME_OUT);
1125 if (c & ATA_STAT_BUSY) {
1126 printf("IDE read: device %d not ready\n", device);
1131 /* write high bits */
1132 ide_outb(device, ATA_SECT_CNT, 0);
1133 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1134 #ifdef CONFIG_SYS_64BIT_LBA
1135 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1136 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1138 ide_outb(device, ATA_LBA_MID, 0);
1139 ide_outb(device, ATA_LBA_HIGH, 0);
1143 ide_outb(device, ATA_SECT_CNT, 1);
1144 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1145 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1146 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1150 ide_outb(device, ATA_DEV_HD,
1151 ATA_LBA | ATA_DEVICE(device));
1152 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
1157 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1158 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1159 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
1165 /* may take up to 4 sec */
1166 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1169 /* can't take over 500 ms */
1170 c = ide_wait(device, IDE_TIME_OUT);
1173 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1175 #if defined(CONFIG_SYS_64BIT_LBA)
1176 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1179 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1180 device, (ulong) blknr, c);
1185 input_data(device, buffer, ATA_SECTORWORDS);
1186 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
1190 buffer += ATA_BLOCKSIZE;
1193 ide_led(DEVICE_LED(device), 0); /* LED off */
1197 /* ------------------------------------------------------------------------- */
1200 ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
1206 unsigned char lba48 = 0;
1208 if (blknr & 0x0000fffff0000000ULL) {
1209 /* more than 28 bits used, use 48bit mode */
1214 ide_led(DEVICE_LED(device), 1); /* LED on */
1218 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1220 while (blkcnt-- > 0) {
1222 c = ide_wait(device, IDE_TIME_OUT);
1224 if (c & ATA_STAT_BUSY) {
1225 printf("IDE read: device %d not ready\n", device);
1230 /* write high bits */
1231 ide_outb(device, ATA_SECT_CNT, 0);
1232 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1233 #ifdef CONFIG_SYS_64BIT_LBA
1234 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1235 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1237 ide_outb(device, ATA_LBA_MID, 0);
1238 ide_outb(device, ATA_LBA_HIGH, 0);
1242 ide_outb(device, ATA_SECT_CNT, 1);
1243 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1244 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1245 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1249 ide_outb(device, ATA_DEV_HD,
1250 ATA_LBA | ATA_DEVICE(device));
1251 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1256 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1257 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1258 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1263 /* can't take over 500 ms */
1264 c = ide_wait(device, IDE_TIME_OUT);
1266 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1268 #if defined(CONFIG_SYS_64BIT_LBA)
1269 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1272 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1273 device, (ulong) blknr, c);
1278 output_data(device, buffer, ATA_SECTORWORDS);
1279 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1282 buffer += ATA_BLOCKSIZE;
1285 ide_led(DEVICE_LED(device), 0); /* LED off */
1289 /* ------------------------------------------------------------------------- */
1292 * copy src to dest, skipping leading and trailing blanks and null
1293 * terminate the string
1294 * "len" is the size of available memory including the terminating '\0'
1296 static void ident_cpy(unsigned char *dst, unsigned char *src,
1299 unsigned char *end, *last;
1302 end = src + len - 1;
1304 /* reserve space for '\0' */
1308 /* skip leading white space */
1309 while ((*src) && (src < end) && (*src == ' '))
1312 /* copy string, omitting trailing white space */
1313 while ((*src) && (src < end)) {
1322 /* ------------------------------------------------------------------------- */
1325 * Wait until Busy bit is off, or timeout (in ms)
1326 * Return last status
1328 static uchar ide_wait(int dev, ulong t)
1330 ulong delay = 10 * t; /* poll every 100 us */
1333 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1341 /* ------------------------------------------------------------------------- */
1343 #ifdef CONFIG_IDE_RESET
1344 extern void ide_set_reset(int idereset);
1346 static void ide_reset(void)
1348 #if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
1349 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
1354 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1356 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1357 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1359 ide_set_reset(1); /* assert reset */
1361 /* the reset signal shall be asserted for et least 25 us */
1366 #ifdef CONFIG_SYS_PB_12V_ENABLE
1367 /* 12V Enable output OFF */
1368 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE);
1370 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1371 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
1372 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
1374 /* wait 500 ms for the voltage to stabilize */
1375 for (i = 0; i < 500; ++i)
1378 /* 12V Enable output ON */
1379 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE;
1380 #endif /* CONFIG_SYS_PB_12V_ENABLE */
1382 #ifdef CONFIG_SYS_PB_IDE_MOTOR
1383 /* configure IDE Motor voltage monitor pin as input */
1384 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1385 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1386 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1388 /* wait up to 1 s for the motor voltage to stabilize */
1389 for (i = 0; i < 1000; ++i) {
1390 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
1396 if (i == 1000) { /* Timeout */
1397 printf("\nWarning: 5V for IDE Motor missing\n");
1398 #ifdef CONFIG_STATUS_LED
1399 #ifdef STATUS_LED_YELLOW
1400 status_led_set(STATUS_LED_YELLOW, STATUS_LED_ON);
1402 #ifdef STATUS_LED_GREEN
1403 status_led_set(STATUS_LED_GREEN, STATUS_LED_OFF);
1405 #endif /* CONFIG_STATUS_LED */
1407 #endif /* CONFIG_SYS_PB_IDE_MOTOR */
1411 /* de-assert RESET signal */
1415 for (i = 0; i < 250; ++i)
1419 #endif /* CONFIG_IDE_RESET */
1421 /* ------------------------------------------------------------------------- */
1423 #if defined(CONFIG_IDE_LED) && \
1424 !defined(CONFIG_CPC45) && \
1425 !defined(CONFIG_KUP4K) && \
1426 !defined(CONFIG_KUP4X)
1428 static uchar led_buffer; /* Buffer for current LED status */
1430 static void ide_led(uchar led, uchar status)
1432 uchar *led_port = LED_PORT;
1434 if (status) /* switch LED on */
1436 else /* switch LED off */
1439 *led_port = led_buffer;
1442 #endif /* CONFIG_IDE_LED */
1444 #if defined(CONFIG_OF_IDE_FIXUP)
1445 int ide_device_present(int dev)
1447 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1449 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1452 /* ------------------------------------------------------------------------- */
1455 /****************************************************************************
1459 #if defined(CONFIG_IDE_SWAP_IO)
1460 /* since ATAPI may use commands with not 4 bytes alligned length
1461 * we have our own transfer functions, 2 bytes alligned */
1462 static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
1464 #if defined(CONFIG_CPC45)
1466 volatile uchar *pbuf_even;
1467 volatile uchar *pbuf_odd;
1469 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1470 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
1473 *pbuf_even = *dbuf++;
1475 *pbuf_odd = *dbuf++;
1479 volatile ushort *pbuf;
1481 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1482 dbuf = (ushort *) sect_buf;
1484 debug("in output data shorts base for read is %lx\n",
1485 (unsigned long) pbuf);
1494 static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
1496 #if defined(CONFIG_CPC45)
1498 volatile uchar *pbuf_even;
1499 volatile uchar *pbuf_odd;
1501 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1502 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
1505 *dbuf++ = *pbuf_even;
1507 *dbuf++ = *pbuf_odd;
1511 volatile ushort *pbuf;
1513 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1514 dbuf = (ushort *) sect_buf;
1516 debug("in input data shorts base for read is %lx\n",
1517 (unsigned long) pbuf);
1526 #else /* ! CONFIG_IDE_SWAP_IO */
1527 static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
1529 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1532 static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
1534 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1537 #endif /* CONFIG_IDE_SWAP_IO */
1540 * Wait until (Status & mask) == res, or timeout (in ms)
1541 * Return last status
1542 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1543 * and then they set their DRQ Bit
1545 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1547 ulong delay = 10 * t; /* poll every 100 us */
1550 /* prevents to read the status before valid */
1551 c = ide_inb(dev, ATA_DEV_CTL);
1553 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1554 /* break if error occurs (doesn't make sense to wait more) */
1555 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1565 * issue an atapi command
1567 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1568 unsigned char *buffer, int buflen)
1570 unsigned char c, err, mask, res;
1573 ide_led(DEVICE_LED(device), 1); /* LED on */
1577 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1579 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1580 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1581 if ((c & mask) != res) {
1582 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1587 /* write taskfile */
1588 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1589 ide_outb(device, ATA_SECT_CNT, 0);
1590 ide_outb(device, ATA_SECT_NUM, 0);
1591 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1592 ide_outb(device, ATA_CYL_HIGH,
1593 (unsigned char) ((buflen >> 8) & 0xFF));
1594 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1596 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1599 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1601 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1603 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1604 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1610 /* write command block */
1611 output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1613 /* ATAPI Command written wait for completition */
1614 udelay(5000); /* device must set bsy */
1616 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1618 * if no data wait for DRQ = 0 BSY = 0
1619 * if data wait for DRQ = 1 BSY = 0
1624 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1625 if ((c & mask) != res) {
1626 if (c & ATA_STAT_ERR) {
1627 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1628 debug("atapi_issue 1 returned sense key %X status %02X\n",
1631 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1637 n = ide_inb(device, ATA_CYL_HIGH);
1639 n += ide_inb(device, ATA_CYL_LOW);
1641 printf("ERROR, transfer bytes %d requested only %d\n", n,
1646 if ((n == 0) && (buflen < 0)) {
1647 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1652 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1655 if (n != 0) { /* data transfer */
1656 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1657 /* we transfer shorts */
1659 /* ok now decide if it is an in or output */
1660 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1661 debug("Write to device\n");
1662 output_data_shorts(device, (unsigned short *) buffer,
1665 debug("Read from device @ %p shorts %d\n", buffer, n);
1666 input_data_shorts(device, (unsigned short *) buffer,
1670 udelay(5000); /* seems that some CD ROMs need this... */
1671 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1673 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1674 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1675 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1676 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1682 ide_led(DEVICE_LED(device), 0); /* LED off */
1687 * sending the command to atapi_issue. If an status other than good
1688 * returns, an request_sense will be issued
1691 #define ATAPI_DRIVE_NOT_READY 100
1692 #define ATAPI_UNIT_ATTN 10
1694 unsigned char atapi_issue_autoreq(int device,
1697 unsigned char *buffer, int buflen)
1699 unsigned char sense_data[18], sense_ccb[12];
1700 unsigned char res, key, asc, ascq;
1701 int notready, unitattn;
1703 unitattn = ATAPI_UNIT_ATTN;
1704 notready = ATAPI_DRIVE_NOT_READY;
1707 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1712 return 0xFF; /* error */
1714 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1716 memset(sense_ccb, 0, sizeof(sense_ccb));
1717 memset(sense_data, 0, sizeof(sense_data));
1718 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1719 sense_ccb[4] = 18; /* allocation Length */
1721 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1722 key = (sense_data[2] & 0xF);
1723 asc = (sense_data[12]);
1724 ascq = (sense_data[13]);
1726 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1727 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1728 sense_data[0], key, asc, ascq);
1731 return 0; /* ok device ready */
1733 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1734 if (unitattn-- > 0) {
1738 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1741 if ((asc == 0x4) && (ascq == 0x1)) {
1742 /* not ready, but will be ready soon */
1743 if (notready-- > 0) {
1747 printf("Drive not ready, tried %d times\n",
1748 ATAPI_DRIVE_NOT_READY);
1752 debug("Media not present\n");
1756 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1759 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1764 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1766 unsigned char ccb[12]; /* Command descriptor block */
1767 unsigned char iobuf[64]; /* temp buf */
1771 device = dev_desc->dev;
1772 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1773 dev_desc->block_read = atapi_read;
1775 memset(ccb, 0, sizeof(ccb));
1776 memset(iobuf, 0, sizeof(iobuf));
1778 ccb[0] = ATAPI_CMD_INQUIRY;
1779 ccb[4] = 40; /* allocation Legnth */
1780 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1782 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1786 /* copy device ident strings */
1787 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1788 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1789 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1793 dev_desc->blksz = 0;
1794 dev_desc->type = iobuf[0] & 0x1f;
1796 if ((iobuf[1] & 0x80) == 0x80)
1797 dev_desc->removable = 1;
1799 dev_desc->removable = 0;
1801 memset(ccb, 0, sizeof(ccb));
1802 memset(iobuf, 0, sizeof(iobuf));
1803 ccb[0] = ATAPI_CMD_START_STOP;
1804 ccb[4] = 0x03; /* start */
1806 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1808 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1812 memset(ccb, 0, sizeof(ccb));
1813 memset(iobuf, 0, sizeof(iobuf));
1814 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1816 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1820 memset(ccb, 0, sizeof(ccb));
1821 memset(iobuf, 0, sizeof(iobuf));
1822 ccb[0] = ATAPI_CMD_READ_CAP;
1823 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1824 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1828 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1829 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1830 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1832 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1833 ((unsigned long) iobuf[1] << 16) +
1834 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1835 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1836 ((unsigned long) iobuf[5] << 16) +
1837 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1839 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1840 dev_desc->lba48 = 0;
1848 * we transfer only one block per command, since the multiple DRQ per
1849 * command is not yet implemented
1851 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1852 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1853 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1855 ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1858 unsigned char ccb[12]; /* Command descriptor block */
1861 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1862 device, blknr, blkcnt, (ulong) buffer);
1865 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1866 cnt = ATAPI_READ_MAX_BLOCK;
1870 ccb[0] = ATAPI_CMD_READ_12;
1871 ccb[1] = 0; /* reserved */
1872 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1873 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1874 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1875 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1876 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1877 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1878 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1879 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1880 ccb[10] = 0; /* reserved */
1881 ccb[11] = 0; /* reserved */
1883 if (atapi_issue_autoreq(device, ccb, 12,
1884 (unsigned char *) buffer,
1885 cnt * ATAPI_READ_BLOCK_SIZE)
1892 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1893 } while (blkcnt > 0);
1897 /* ------------------------------------------------------------------------- */
1899 #endif /* CONFIG_ATAPI */
1901 U_BOOT_CMD(ide, 5, 1, do_ide,
1903 "reset - reset IDE controller\n"
1904 "ide info - show available IDE devices\n"
1905 "ide device [dev] - show or set current device\n"
1906 "ide part [dev] - print partition table of one or all IDE devices\n"
1907 "ide read addr blk# cnt\n"
1908 "ide write addr blk# cnt - read/write `cnt'"
1909 " blocks starting at block `blk#'\n"
1910 " to/from memory address `addr'");
1912 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1913 "boot from IDE device", "loadAddr dev:part");