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 #if defined(CONFIG_SC3)
404 unsigned int ata_reset_time = ATA_RESET_TIME;
406 #ifdef CONFIG_IDE_8xx_PCCARD
407 extern int pcmcia_on(void);
408 extern int ide_devices_found; /* Initialized in check_ide_device() */
409 #endif /* CONFIG_IDE_8xx_PCCARD */
411 #ifdef CONFIG_IDE_PREINIT
412 extern int ide_preinit(void);
417 puts("ide_preinit failed\n");
420 #endif /* CONFIG_IDE_PREINIT */
422 #ifdef CONFIG_IDE_8xx_PCCARD
423 extern int pcmcia_on(void);
424 extern int ide_devices_found; /* Initialized in check_ide_device() */
428 ide_devices_found = 0;
429 /* initialize the PCMCIA IDE adapter card */
431 if (!ide_devices_found)
433 udelay(1000000); /* 1 s */
434 #endif /* CONFIG_IDE_8xx_PCCARD */
438 #ifdef CONFIG_IDE_8xx_DIRECT
439 /* Initialize PIO timing tables */
440 for (i = 0; i <= IDE_MAX_PIO_MODE; ++i) {
441 pio_config_clk[i].t_setup =
442 PCMCIA_MK_CLKS(pio_config_ns[i].t_setup, gd->bus_clk);
443 pio_config_clk[i].t_length =
444 PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
446 pio_config_clk[i].t_hold =
447 PCMCIA_MK_CLKS(pio_config_ns[i].t_hold, gd->bus_clk);
448 debug("PIO Mode %d: setup=%2d ns/%d clk" " len=%3d ns/%d clk"
449 " hold=%2d ns/%d clk\n", i, pio_config_ns[i].t_setup,
450 pio_config_clk[i].t_setup, pio_config_ns[i].t_length,
451 pio_config_clk[i].t_length, pio_config_ns[i].t_hold,
452 pio_config_clk[i].t_hold);
454 #endif /* CONFIG_IDE_8xx_DIRECT */
457 * Reset the IDE just to be sure.
458 * Light LED's to show
460 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
462 /* ATAPI Drives seems to need a proper IDE Reset */
465 #ifdef CONFIG_IDE_8xx_DIRECT
466 /* PCMCIA / IDE initialization for common mem space */
467 pcmp->pcmc_pgcrb = 0;
469 /* start in PIO mode 0 - most relaxed timings */
471 set_pcmcia_timing(pio_mode);
472 #endif /* CONFIG_IDE_8xx_DIRECT */
475 * Wait for IDE to get ready.
476 * According to spec, this can take up to 31 seconds!
478 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
480 bus * (CONFIG_SYS_IDE_MAXDEVICE /
481 CONFIG_SYS_IDE_MAXBUS);
483 #ifdef CONFIG_IDE_8xx_PCCARD
484 /* Skip non-ide devices from probing */
485 if ((ide_devices_found & (1 << bus)) == 0) {
486 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
490 printf("Bus %d: ", bus);
496 udelay(100000); /* 100 ms */
497 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
498 udelay(100000); /* 100 ms */
501 udelay(10000); /* 10 ms */
503 c = ide_inb(dev, ATA_STATUS);
505 #if defined(CONFIG_SC3)
506 if (i > (ata_reset_time * 100)) {
508 if (i > (ATA_RESET_TIME * 100)) {
510 puts("** Timeout **\n");
512 ide_led((LED_IDE1 | LED_IDE2), 0);
515 if ((i >= 100) && ((i % 100) == 0))
518 } while (c & ATA_STAT_BUSY);
520 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
521 puts("not available ");
522 debug("Status = 0x%02X ", c);
523 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
524 } else if ((c & ATA_STAT_READY) == 0) {
525 puts("not available ");
526 debug("Status = 0x%02X ", c);
537 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
540 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
541 #ifdef CONFIG_IDE_LED
542 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
544 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
545 ide_dev_desc[i].if_type = IF_TYPE_IDE;
546 ide_dev_desc[i].dev = i;
547 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
548 ide_dev_desc[i].blksz = 0;
549 ide_dev_desc[i].lba = 0;
550 ide_dev_desc[i].block_read = ide_read;
551 ide_dev_desc[i].block_write = ide_write;
552 if (!ide_bus_ok[IDE_BUS(i)])
554 ide_led(led, 1); /* LED on */
555 ide_ident(&ide_dev_desc[i]);
556 ide_led(led, 0); /* LED off */
557 dev_print(&ide_dev_desc[i]);
559 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
560 /* initialize partition type */
561 init_part(&ide_dev_desc[i]);
569 /* ------------------------------------------------------------------------- */
571 #ifdef CONFIG_PARTITIONS
572 block_dev_desc_t *ide_get_dev(int dev)
574 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
579 #ifdef CONFIG_IDE_8xx_DIRECT
581 static void set_pcmcia_timing(int pmode)
583 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
584 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
587 debug("Set timing for PIO Mode %d\n", pmode);
589 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
590 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
591 | PCMCIA_SL(pio_config_clk[pmode].t_length);
596 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
597 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
598 #if (CONFIG_SYS_PCMCIA_POR0 != 0)
602 debug("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
604 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
605 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
606 #if (CONFIG_SYS_PCMCIA_POR1 != 0)
610 debug("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
612 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
613 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
614 #if (CONFIG_SYS_PCMCIA_POR2 != 0)
618 debug("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
620 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
621 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
622 #if (CONFIG_SYS_PCMCIA_POR3 != 0)
626 debug("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
631 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
632 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
633 #if (CONFIG_SYS_PCMCIA_POR4 != 0)
637 debug("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
639 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
640 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
641 #if (CONFIG_SYS_PCMCIA_POR5 != 0)
645 debug("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
647 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
648 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
649 #if (CONFIG_SYS_PCMCIA_POR6 != 0)
653 debug("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
655 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
656 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
657 #if (CONFIG_SYS_PCMCIA_POR7 != 0)
661 debug("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
665 #endif /* CONFIG_IDE_8xx_DIRECT */
667 /* ------------------------------------------------------------------------- */
669 /* We only need to swap data if we are running on a big endian cpu. */
670 /* But Au1x00 cpu:s already swaps data in big endian mode! */
671 #if defined(__LITTLE_ENDIAN) || \
672 (defined(CONFIG_SOC_AU1X00) && !defined(CONFIG_GTH2))
673 #define input_swap_data(x,y,z) input_data(x,y,z)
675 static void input_swap_data(int dev, ulong *sect_buf, int words)
677 #if defined(CONFIG_CPC45)
679 volatile uchar *pbuf_even =
680 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
681 volatile uchar *pbuf_odd =
682 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
683 ushort *dbuf = (ushort *) sect_buf;
686 for (i = 0; i < 2; i++) {
687 *(((uchar *) (dbuf)) + 1) = *pbuf_even;
688 *(uchar *) dbuf = *pbuf_odd;
693 volatile ushort *pbuf =
694 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
695 ushort *dbuf = (ushort *) sect_buf;
697 debug("in input swap data base for read is %lx\n",
698 (unsigned long) pbuf);
702 *dbuf++ = swab16p((u16 *) pbuf);
703 *dbuf++ = swab16p((u16 *) pbuf);
704 #elif defined(CONFIG_PCS440EP)
708 *dbuf++ = ld_le16(pbuf);
709 *dbuf++ = ld_le16(pbuf);
714 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
717 #if defined(CONFIG_IDE_SWAP_IO)
718 static void output_data(int dev, const ulong *sect_buf, int words)
720 #if defined(CONFIG_CPC45)
722 volatile uchar *pbuf_even;
723 volatile uchar *pbuf_odd;
725 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
726 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
727 dbuf = (uchar *) sect_buf;
730 *pbuf_even = *dbuf++;
734 *pbuf_even = *dbuf++;
740 volatile ushort *pbuf;
742 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
743 dbuf = (ushort *) sect_buf;
745 #if defined(CONFIG_PCS440EP)
746 /* not tested, because CF was write protected */
748 *pbuf = ld_le16(dbuf++);
750 *pbuf = ld_le16(dbuf++);
760 #else /* ! CONFIG_IDE_SWAP_IO */
761 static void output_data(int dev, const ulong *sect_buf, int words)
763 #if defined(CONFIG_IDE_AHB)
764 ide_write_data(dev, sect_buf, words);
766 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
769 #endif /* CONFIG_IDE_SWAP_IO */
771 #if defined(CONFIG_IDE_SWAP_IO)
772 static void input_data(int dev, ulong *sect_buf, int words)
774 #if defined(CONFIG_CPC45)
776 volatile uchar *pbuf_even;
777 volatile uchar *pbuf_odd;
779 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
780 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
781 dbuf = (uchar *) sect_buf;
783 *dbuf++ = *pbuf_even;
789 *dbuf++ = *pbuf_even;
798 volatile ushort *pbuf;
800 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
801 dbuf = (ushort *) sect_buf;
803 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
806 #if defined(CONFIG_PCS440EP)
808 *dbuf++ = ld_le16(pbuf);
810 *dbuf++ = ld_le16(pbuf);
820 #else /* ! CONFIG_IDE_SWAP_IO */
821 static void input_data(int dev, ulong *sect_buf, int words)
823 #if defined(CONFIG_IDE_AHB)
824 ide_read_data(dev, sect_buf, words);
826 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
830 #endif /* CONFIG_IDE_SWAP_IO */
832 /* -------------------------------------------------------------------------
834 static void ide_ident(block_dev_desc_t *dev_desc)
843 #ifdef CONFIG_TUNE_PIO
848 int mode, cycle_time;
852 device = dev_desc->dev;
853 printf(" Device %d: ", device);
855 ide_led(DEVICE_LED(device), 1); /* LED on */
858 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
859 dev_desc->if_type = IF_TYPE_IDE;
864 /* Warning: This will be tricky to read */
865 while (retries <= 1) {
866 /* check signature */
867 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
868 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
869 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
870 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
871 /* ATAPI Signature found */
872 dev_desc->if_type = IF_TYPE_ATAPI;
874 * Start Ident Command
876 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
878 * Wait for completion - ATAPI devices need more time
881 c = ide_wait(device, ATAPI_TIME_OUT);
886 * Start Ident Command
888 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
891 * Wait for completion
893 c = ide_wait(device, IDE_TIME_OUT);
895 ide_led(DEVICE_LED(device), 0); /* LED off */
897 if (((c & ATA_STAT_DRQ) == 0) ||
898 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
902 * Need to soft reset the device
903 * in case it's an ATAPI...
905 debug("Retrying...\n");
906 ide_outb(device, ATA_DEV_HD,
907 ATA_LBA | ATA_DEVICE(device));
909 ide_outb(device, ATA_COMMAND, 0x08);
910 udelay(500000); /* 500 ms */
915 ide_outb(device, ATA_DEV_HD,
916 ATA_LBA | ATA_DEVICE(device));
925 } /* see above - ugly to read */
927 if (retries == 2) /* Not found */
931 input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
933 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
934 sizeof(dev_desc->revision));
935 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
936 sizeof(dev_desc->vendor));
937 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
938 sizeof(dev_desc->product));
939 #ifdef __LITTLE_ENDIAN
941 * firmware revision, model, and serial number have Big Endian Byte
942 * order in Word. Convert all three to little endian.
944 * See CF+ and CompactFlash Specification Revision 2.0:
945 * 6.2.1.6: Identify Drive, Table 39 for more details
948 strswab(dev_desc->revision);
949 strswab(dev_desc->vendor);
950 strswab(dev_desc->product);
951 #endif /* __LITTLE_ENDIAN */
953 if ((iop.config & 0x0080) == 0x0080)
954 dev_desc->removable = 1;
956 dev_desc->removable = 0;
958 #ifdef CONFIG_TUNE_PIO
959 /* Mode 0 - 2 only, are directly determined by word 51. */
962 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
963 /* Force it to dead slow, and hope for the best... */
967 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
968 * shall set bit 1 of word 53 to one and support the fields contained
969 * in words 64 through 70.
971 if (iop.field_valid & 0x02) {
973 * Mode 3 and above are possible. Check in order from slow
974 * to fast, so we wind up with the highest mode allowed.
976 if (iop.eide_pio_modes & 0x01)
978 if (iop.eide_pio_modes & 0x02)
980 if (ata_id_is_cfa((u16 *)&iop)) {
981 if ((iop.cf_advanced_caps & 0x07) == 0x01)
983 if ((iop.cf_advanced_caps & 0x07) == 0x02)
988 /* System-specific, depends on bus speeds, etc. */
989 ide_set_piomode(pio_mode);
990 #endif /* CONFIG_TUNE_PIO */
994 * Drive PIO mode autoselection
998 printf("tPIO = 0x%02x = %d\n", mode, mode);
999 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1001 debug("Override tPIO -> 2\n");
1003 if (iop.field_valid & 2) { /* drive implements ATA2? */
1004 debug("Drive implements ATA2\n");
1005 if (iop.capability & 8) { /* drive supports use_iordy? */
1006 cycle_time = iop.eide_pio_iordy;
1008 cycle_time = iop.eide_pio;
1010 debug("cycle time = %d\n", cycle_time);
1012 if (cycle_time > 120)
1013 mode = 3; /* 120 ns for PIO mode 4 */
1014 if (cycle_time > 180)
1015 mode = 2; /* 180 ns for PIO mode 3 */
1016 if (cycle_time > 240)
1017 mode = 1; /* 240 ns for PIO mode 4 */
1018 if (cycle_time > 383)
1019 mode = 0; /* 383 ns for PIO mode 4 */
1021 printf("PIO mode to use: PIO %d\n", mode);
1025 if (dev_desc->if_type == IF_TYPE_ATAPI) {
1026 atapi_inquiry(dev_desc);
1029 #endif /* CONFIG_ATAPI */
1033 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
1034 #else /* ! __BIG_ENDIAN */
1036 * do not swap shorts on little endian
1038 * See CF+ and CompactFlash Specification Revision 2.0:
1039 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1041 dev_desc->lba = iop.lba_capacity;
1042 #endif /* __BIG_ENDIAN */
1045 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
1046 dev_desc->lba48 = 1;
1047 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
1048 ((unsigned long long) iop.lba48_capacity[1] << 16) |
1049 ((unsigned long long) iop.lba48_capacity[2] << 32) |
1050 ((unsigned long long) iop.lba48_capacity[3] << 48);
1052 dev_desc->lba48 = 0;
1054 #endif /* CONFIG_LBA48 */
1056 dev_desc->type = DEV_TYPE_HARDDISK;
1057 dev_desc->blksz = ATA_BLOCKSIZE;
1058 dev_desc->lun = 0; /* just to fill something in... */
1060 #if 0 /* only used to test the powersaving mode,
1061 * if enabled, the drive goes after 5 sec
1062 * in standby mode */
1063 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1064 c = ide_wait(device, IDE_TIME_OUT);
1065 ide_outb(device, ATA_SECT_CNT, 1);
1066 ide_outb(device, ATA_LBA_LOW, 0);
1067 ide_outb(device, ATA_LBA_MID, 0);
1068 ide_outb(device, ATA_LBA_HIGH, 0);
1069 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1070 ide_outb(device, ATA_COMMAND, 0xe3);
1072 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
1077 /* ------------------------------------------------------------------------- */
1079 ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1083 unsigned char pwrsave = 0; /* power save */
1086 unsigned char lba48 = 0;
1088 if (blknr & 0x0000fffff0000000ULL) {
1089 /* more than 28 bits used, use 48bit mode */
1093 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1094 device, blknr, blkcnt, (ulong) buffer);
1096 ide_led(DEVICE_LED(device), 1); /* LED on */
1100 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1101 c = ide_wait(device, IDE_TIME_OUT);
1103 if (c & ATA_STAT_BUSY) {
1104 printf("IDE read: device %d not ready\n", device);
1108 /* first check if the drive is in Powersaving mode, if yes,
1109 * increase the timeout value */
1110 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1113 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
1115 if (c & ATA_STAT_BUSY) {
1116 printf("IDE read: device %d not ready\n", device);
1119 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1120 printf("No Powersaving mode %X\n", c);
1122 c = ide_inb(device, ATA_SECT_CNT);
1123 debug("Powersaving %02X\n", c);
1129 while (blkcnt-- > 0) {
1131 c = ide_wait(device, IDE_TIME_OUT);
1133 if (c & ATA_STAT_BUSY) {
1134 printf("IDE read: device %d not ready\n", device);
1139 /* write high bits */
1140 ide_outb(device, ATA_SECT_CNT, 0);
1141 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1142 #ifdef CONFIG_SYS_64BIT_LBA
1143 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1144 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1146 ide_outb(device, ATA_LBA_MID, 0);
1147 ide_outb(device, ATA_LBA_HIGH, 0);
1151 ide_outb(device, ATA_SECT_CNT, 1);
1152 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1153 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1154 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1158 ide_outb(device, ATA_DEV_HD,
1159 ATA_LBA | ATA_DEVICE(device));
1160 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
1165 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1166 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1167 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
1173 /* may take up to 4 sec */
1174 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1177 /* can't take over 500 ms */
1178 c = ide_wait(device, IDE_TIME_OUT);
1181 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1183 #if defined(CONFIG_SYS_64BIT_LBA)
1184 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1187 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1188 device, (ulong) blknr, c);
1193 input_data(device, buffer, ATA_SECTORWORDS);
1194 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
1198 buffer += ATA_BLOCKSIZE;
1201 ide_led(DEVICE_LED(device), 0); /* LED off */
1205 /* ------------------------------------------------------------------------- */
1208 ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
1214 unsigned char lba48 = 0;
1216 if (blknr & 0x0000fffff0000000ULL) {
1217 /* more than 28 bits used, use 48bit mode */
1222 ide_led(DEVICE_LED(device), 1); /* LED on */
1226 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1228 while (blkcnt-- > 0) {
1230 c = ide_wait(device, IDE_TIME_OUT);
1232 if (c & ATA_STAT_BUSY) {
1233 printf("IDE read: device %d not ready\n", device);
1238 /* write high bits */
1239 ide_outb(device, ATA_SECT_CNT, 0);
1240 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1241 #ifdef CONFIG_SYS_64BIT_LBA
1242 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1243 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1245 ide_outb(device, ATA_LBA_MID, 0);
1246 ide_outb(device, ATA_LBA_HIGH, 0);
1250 ide_outb(device, ATA_SECT_CNT, 1);
1251 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1252 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1253 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1257 ide_outb(device, ATA_DEV_HD,
1258 ATA_LBA | ATA_DEVICE(device));
1259 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1264 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1265 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1266 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1271 /* can't take over 500 ms */
1272 c = ide_wait(device, IDE_TIME_OUT);
1274 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1276 #if defined(CONFIG_SYS_64BIT_LBA)
1277 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1280 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1281 device, (ulong) blknr, c);
1286 output_data(device, buffer, ATA_SECTORWORDS);
1287 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1290 buffer += ATA_BLOCKSIZE;
1293 ide_led(DEVICE_LED(device), 0); /* LED off */
1297 /* ------------------------------------------------------------------------- */
1300 * copy src to dest, skipping leading and trailing blanks and null
1301 * terminate the string
1302 * "len" is the size of available memory including the terminating '\0'
1304 static void ident_cpy(unsigned char *dst, unsigned char *src,
1307 unsigned char *end, *last;
1310 end = src + len - 1;
1312 /* reserve space for '\0' */
1316 /* skip leading white space */
1317 while ((*src) && (src < end) && (*src == ' '))
1320 /* copy string, omitting trailing white space */
1321 while ((*src) && (src < end)) {
1330 /* ------------------------------------------------------------------------- */
1333 * Wait until Busy bit is off, or timeout (in ms)
1334 * Return last status
1336 static uchar ide_wait(int dev, ulong t)
1338 ulong delay = 10 * t; /* poll every 100 us */
1341 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1349 /* ------------------------------------------------------------------------- */
1351 #ifdef CONFIG_IDE_RESET
1352 extern void ide_set_reset(int idereset);
1354 static void ide_reset(void)
1356 #if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
1357 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
1362 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1364 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1365 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1367 ide_set_reset(1); /* assert reset */
1369 /* the reset signal shall be asserted for et least 25 us */
1374 #ifdef CONFIG_SYS_PB_12V_ENABLE
1375 /* 12V Enable output OFF */
1376 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE);
1378 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1379 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
1380 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
1382 /* wait 500 ms for the voltage to stabilize */
1383 for (i = 0; i < 500; ++i)
1386 /* 12V Enable output ON */
1387 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE;
1388 #endif /* CONFIG_SYS_PB_12V_ENABLE */
1390 #ifdef CONFIG_SYS_PB_IDE_MOTOR
1391 /* configure IDE Motor voltage monitor pin as input */
1392 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1393 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1394 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1396 /* wait up to 1 s for the motor voltage to stabilize */
1397 for (i = 0; i < 1000; ++i) {
1398 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
1404 if (i == 1000) { /* Timeout */
1405 printf("\nWarning: 5V for IDE Motor missing\n");
1406 #ifdef CONFIG_STATUS_LED
1407 #ifdef STATUS_LED_YELLOW
1408 status_led_set(STATUS_LED_YELLOW, STATUS_LED_ON);
1410 #ifdef STATUS_LED_GREEN
1411 status_led_set(STATUS_LED_GREEN, STATUS_LED_OFF);
1413 #endif /* CONFIG_STATUS_LED */
1415 #endif /* CONFIG_SYS_PB_IDE_MOTOR */
1419 /* de-assert RESET signal */
1423 for (i = 0; i < 250; ++i)
1427 #endif /* CONFIG_IDE_RESET */
1429 /* ------------------------------------------------------------------------- */
1431 #if defined(CONFIG_IDE_LED) && \
1432 !defined(CONFIG_CPC45) && \
1433 !defined(CONFIG_KUP4K) && \
1434 !defined(CONFIG_KUP4X)
1436 static uchar led_buffer; /* Buffer for current LED status */
1438 static void ide_led(uchar led, uchar status)
1440 uchar *led_port = LED_PORT;
1442 if (status) /* switch LED on */
1444 else /* switch LED off */
1447 *led_port = led_buffer;
1450 #endif /* CONFIG_IDE_LED */
1452 #if defined(CONFIG_OF_IDE_FIXUP)
1453 int ide_device_present(int dev)
1455 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1457 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1460 /* ------------------------------------------------------------------------- */
1463 /****************************************************************************
1467 #if defined(CONFIG_IDE_SWAP_IO)
1468 /* since ATAPI may use commands with not 4 bytes alligned length
1469 * we have our own transfer functions, 2 bytes alligned */
1470 static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
1472 #if defined(CONFIG_CPC45)
1474 volatile uchar *pbuf_even;
1475 volatile uchar *pbuf_odd;
1477 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1478 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
1481 *pbuf_even = *dbuf++;
1483 *pbuf_odd = *dbuf++;
1487 volatile ushort *pbuf;
1489 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1490 dbuf = (ushort *) sect_buf;
1492 debug("in output data shorts base for read is %lx\n",
1493 (unsigned long) pbuf);
1502 static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
1504 #if defined(CONFIG_CPC45)
1506 volatile uchar *pbuf_even;
1507 volatile uchar *pbuf_odd;
1509 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1510 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
1513 *dbuf++ = *pbuf_even;
1515 *dbuf++ = *pbuf_odd;
1519 volatile ushort *pbuf;
1521 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1522 dbuf = (ushort *) sect_buf;
1524 debug("in input data shorts base for read is %lx\n",
1525 (unsigned long) pbuf);
1534 #else /* ! CONFIG_IDE_SWAP_IO */
1535 static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
1537 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1540 static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
1542 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1545 #endif /* CONFIG_IDE_SWAP_IO */
1548 * Wait until (Status & mask) == res, or timeout (in ms)
1549 * Return last status
1550 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1551 * and then they set their DRQ Bit
1553 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1555 ulong delay = 10 * t; /* poll every 100 us */
1558 /* prevents to read the status before valid */
1559 c = ide_inb(dev, ATA_DEV_CTL);
1561 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1562 /* break if error occurs (doesn't make sense to wait more) */
1563 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1573 * issue an atapi command
1575 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1576 unsigned char *buffer, int buflen)
1578 unsigned char c, err, mask, res;
1581 ide_led(DEVICE_LED(device), 1); /* LED on */
1585 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1587 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1588 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1589 if ((c & mask) != res) {
1590 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1595 /* write taskfile */
1596 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1597 ide_outb(device, ATA_SECT_CNT, 0);
1598 ide_outb(device, ATA_SECT_NUM, 0);
1599 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1600 ide_outb(device, ATA_CYL_HIGH,
1601 (unsigned char) ((buflen >> 8) & 0xFF));
1602 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1604 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1607 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1609 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1611 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1612 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1618 /* write command block */
1619 output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1621 /* ATAPI Command written wait for completition */
1622 udelay(5000); /* device must set bsy */
1624 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1626 * if no data wait for DRQ = 0 BSY = 0
1627 * if data wait for DRQ = 1 BSY = 0
1632 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1633 if ((c & mask) != res) {
1634 if (c & ATA_STAT_ERR) {
1635 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1636 debug("atapi_issue 1 returned sense key %X status %02X\n",
1639 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1645 n = ide_inb(device, ATA_CYL_HIGH);
1647 n += ide_inb(device, ATA_CYL_LOW);
1649 printf("ERROR, transfer bytes %d requested only %d\n", n,
1654 if ((n == 0) && (buflen < 0)) {
1655 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1660 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1663 if (n != 0) { /* data transfer */
1664 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1665 /* we transfer shorts */
1667 /* ok now decide if it is an in or output */
1668 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1669 debug("Write to device\n");
1670 output_data_shorts(device, (unsigned short *) buffer,
1673 debug("Read from device @ %p shorts %d\n", buffer, n);
1674 input_data_shorts(device, (unsigned short *) buffer,
1678 udelay(5000); /* seems that some CD ROMs need this... */
1679 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1681 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1682 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1683 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1684 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1690 ide_led(DEVICE_LED(device), 0); /* LED off */
1695 * sending the command to atapi_issue. If an status other than good
1696 * returns, an request_sense will be issued
1699 #define ATAPI_DRIVE_NOT_READY 100
1700 #define ATAPI_UNIT_ATTN 10
1702 unsigned char atapi_issue_autoreq(int device,
1705 unsigned char *buffer, int buflen)
1707 unsigned char sense_data[18], sense_ccb[12];
1708 unsigned char res, key, asc, ascq;
1709 int notready, unitattn;
1711 unitattn = ATAPI_UNIT_ATTN;
1712 notready = ATAPI_DRIVE_NOT_READY;
1715 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1720 return 0xFF; /* error */
1722 debug("(auto_req)atapi_issue returned sense key %X\n", res);
1724 memset(sense_ccb, 0, sizeof(sense_ccb));
1725 memset(sense_data, 0, sizeof(sense_data));
1726 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1727 sense_ccb[4] = 18; /* allocation Length */
1729 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1730 key = (sense_data[2] & 0xF);
1731 asc = (sense_data[12]);
1732 ascq = (sense_data[13]);
1734 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1735 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1736 sense_data[0], key, asc, ascq);
1739 return 0; /* ok device ready */
1741 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1742 if (unitattn-- > 0) {
1746 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1749 if ((asc == 0x4) && (ascq == 0x1)) {
1750 /* not ready, but will be ready soon */
1751 if (notready-- > 0) {
1755 printf("Drive not ready, tried %d times\n",
1756 ATAPI_DRIVE_NOT_READY);
1760 debug("Media not present\n");
1764 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1767 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1772 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1774 unsigned char ccb[12]; /* Command descriptor block */
1775 unsigned char iobuf[64]; /* temp buf */
1779 device = dev_desc->dev;
1780 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1781 dev_desc->block_read = atapi_read;
1783 memset(ccb, 0, sizeof(ccb));
1784 memset(iobuf, 0, sizeof(iobuf));
1786 ccb[0] = ATAPI_CMD_INQUIRY;
1787 ccb[4] = 40; /* allocation Legnth */
1788 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1790 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1794 /* copy device ident strings */
1795 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1796 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1797 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1801 dev_desc->blksz = 0;
1802 dev_desc->type = iobuf[0] & 0x1f;
1804 if ((iobuf[1] & 0x80) == 0x80)
1805 dev_desc->removable = 1;
1807 dev_desc->removable = 0;
1809 memset(ccb, 0, sizeof(ccb));
1810 memset(iobuf, 0, sizeof(iobuf));
1811 ccb[0] = ATAPI_CMD_START_STOP;
1812 ccb[4] = 0x03; /* start */
1814 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1816 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1820 memset(ccb, 0, sizeof(ccb));
1821 memset(iobuf, 0, sizeof(iobuf));
1822 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1824 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1828 memset(ccb, 0, sizeof(ccb));
1829 memset(iobuf, 0, sizeof(iobuf));
1830 ccb[0] = ATAPI_CMD_READ_CAP;
1831 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1832 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1836 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1837 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1838 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1840 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1841 ((unsigned long) iobuf[1] << 16) +
1842 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1843 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1844 ((unsigned long) iobuf[5] << 16) +
1845 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1847 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1848 dev_desc->lba48 = 0;
1856 * we transfer only one block per command, since the multiple DRQ per
1857 * command is not yet implemented
1859 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1860 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1861 #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1863 ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1866 unsigned char ccb[12]; /* Command descriptor block */
1869 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1870 device, blknr, blkcnt, (ulong) buffer);
1873 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1874 cnt = ATAPI_READ_MAX_BLOCK;
1878 ccb[0] = ATAPI_CMD_READ_12;
1879 ccb[1] = 0; /* reserved */
1880 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1881 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1882 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1883 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1884 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1885 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1886 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1887 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1888 ccb[10] = 0; /* reserved */
1889 ccb[11] = 0; /* reserved */
1891 if (atapi_issue_autoreq(device, ccb, 12,
1892 (unsigned char *) buffer,
1893 cnt * ATAPI_READ_BLOCK_SIZE)
1900 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1901 } while (blkcnt > 0);
1905 /* ------------------------------------------------------------------------- */
1907 #endif /* CONFIG_ATAPI */
1909 U_BOOT_CMD(ide, 5, 1, do_ide,
1911 "reset - reset IDE controller\n"
1912 "ide info - show available IDE devices\n"
1913 "ide device [dev] - show or set current device\n"
1914 "ide part [dev] - print partition table of one or all IDE devices\n"
1915 "ide read addr blk# cnt\n"
1916 "ide write addr blk# cnt - read/write `cnt'"
1917 " blocks starting at block `blk#'\n"
1918 " to/from memory address `addr'");
1920 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1921 "boot from IDE device", "loadAddr dev:part");