2 * (C) Copyright 2000-2005
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,
33 #include <asm/byteorder.h>
36 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
51 #ifdef CONFIG_STATUS_LED
52 # include <status_led.h>
55 #ifdef CONFIG_IDE_8xx_DIRECT
56 DECLARE_GLOBAL_DATA_PTR;
60 # define EIEIO __asm__ volatile ("eieio")
61 # define SYNC __asm__ volatile ("sync")
63 # define EIEIO /* nothing */
64 # define SYNC /* nothing */
67 #ifdef CONFIG_IDE_8xx_DIRECT
68 /* Timings for IDE Interface
70 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
71 * 70 165 30 PIO-Mode 0, [ns]
73 * 50 125 20 PIO-Mode 1, [ns]
75 * 30 100 15 PIO-Mode 2, [ns]
77 * 30 80 10 PIO-Mode 3, [ns]
79 * 25 70 10 PIO-Mode 4, [ns]
83 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
85 /* Setup Length Hold */
86 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
87 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
88 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
89 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
90 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
93 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
95 #ifndef CONFIG_SYS_PIO_MODE
96 #define CONFIG_SYS_PIO_MODE 0 /* use a relaxed default */
98 static int pio_mode = CONFIG_SYS_PIO_MODE;
100 /* Make clock cycles and always round up */
102 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
104 #endif /* CONFIG_IDE_8xx_DIRECT */
106 /* ------------------------------------------------------------------------- */
108 /* Current I/O Device */
109 static int curr_device = -1;
111 /* Current offset for IDE0 / IDE1 bus access */
112 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
113 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
114 CONFIG_SYS_ATA_IDE0_OFFSET,
116 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
117 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, 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_usage(cmdtp);
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) || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
208 puts ("\nno IDE devices available\n");
211 printf ("\nIDE device %d: ", curr_device);
212 dev_print(&ide_dev_desc[curr_device]);
214 } else if (strncmp(argv[1],"part",4) == 0) {
217 for (ok=0, dev=0; dev<CONFIG_SYS_IDE_MAXDEVICE; ++dev) {
218 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
222 print_part(&ide_dev_desc[dev]);
226 puts ("\nno IDE devices available\n");
231 return cmd_usage(cmdtp);
233 if (strncmp(argv[1],"dev",3) == 0) {
234 int dev = (int)simple_strtoul(argv[2], NULL, 10);
236 printf ("\nIDE device %d: ", dev);
237 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
238 puts ("unknown device\n");
241 dev_print(&ide_dev_desc[dev]);
244 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
250 puts ("... is now current device\n");
253 } else if (strncmp(argv[1],"part",4) == 0) {
254 int dev = (int)simple_strtoul(argv[2], NULL, 10);
256 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
257 print_part(&ide_dev_desc[dev]);
259 printf ("\nIDE device %d not available\n", dev);
264 } else if (strncmp(argv[1],"pio",4) == 0) {
265 int mode = (int)simple_strtoul(argv[2], NULL, 10);
267 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
272 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
273 mode, IDE_MAX_PIO_MODE);
279 return cmd_usage(cmdtp);
281 /* at least 4 args */
283 if (strcmp(argv[1],"read") == 0) {
284 ulong addr = simple_strtoul(argv[2], NULL, 16);
285 ulong cnt = simple_strtoul(argv[4], NULL, 16);
287 #ifdef CONFIG_SYS_64BIT_LBA
288 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
290 printf ("\nIDE read: device %d block # %Ld, count %ld ... ",
291 curr_device, blk, cnt);
293 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
295 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
296 curr_device, blk, cnt);
299 n = ide_dev_desc[curr_device].block_read (curr_device,
302 /* flush cache after read */
303 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
305 printf ("%ld blocks read: %s\n",
306 n, (n==cnt) ? "OK" : "ERROR");
312 } else if (strcmp(argv[1],"write") == 0) {
313 ulong addr = simple_strtoul(argv[2], NULL, 16);
314 ulong cnt = simple_strtoul(argv[4], NULL, 16);
316 #ifdef CONFIG_SYS_64BIT_LBA
317 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
319 printf ("\nIDE write: device %d block # %Ld, count %ld ... ",
320 curr_device, blk, cnt);
322 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
324 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
325 curr_device, blk, cnt);
328 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
330 printf ("%ld blocks written: %s\n",
331 n, (n==cnt) ? "OK" : "ERROR");
337 return cmd_usage(cmdtp);
344 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
346 char *boot_device = NULL;
350 disk_partition_t info;
353 #if defined(CONFIG_FIT)
354 const void *fit_hdr = NULL;
357 show_boot_progress (41);
360 addr = CONFIG_SYS_LOAD_ADDR;
361 boot_device = getenv ("bootdevice");
364 addr = simple_strtoul(argv[1], NULL, 16);
365 boot_device = getenv ("bootdevice");
368 addr = simple_strtoul(argv[1], NULL, 16);
369 boot_device = argv[2];
372 show_boot_progress (-42);
373 return cmd_usage(cmdtp);
375 show_boot_progress (42);
378 puts ("\n** No boot device **\n");
379 show_boot_progress (-43);
382 show_boot_progress (43);
384 dev = simple_strtoul(boot_device, &ep, 16);
386 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
387 printf ("\n** Device %d not available\n", dev);
388 show_boot_progress (-44);
391 show_boot_progress (44);
395 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
396 show_boot_progress (-45);
399 part = simple_strtoul(++ep, NULL, 16);
401 show_boot_progress (45);
402 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
403 show_boot_progress (-46);
406 show_boot_progress (46);
407 if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
408 (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
409 printf ("\n** Invalid partition type \"%.32s\""
410 " (expect \"" BOOT_PART_TYPE "\")\n",
412 show_boot_progress (-47);
415 show_boot_progress (47);
417 printf ("\nLoading from IDE device %d, partition %d: "
418 "Name: %.32s Type: %.32s\n",
419 dev, part, info.name, info.type);
421 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
422 info.start, info.size, info.blksz);
424 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
425 printf ("** Read error on %d:%d\n", dev, part);
426 show_boot_progress (-48);
429 show_boot_progress (48);
431 switch (genimg_get_format ((void *)addr)) {
432 case IMAGE_FORMAT_LEGACY:
433 hdr = (image_header_t *)addr;
435 show_boot_progress (49);
437 if (!image_check_hcrc (hdr)) {
438 puts ("\n** Bad Header Checksum **\n");
439 show_boot_progress (-50);
442 show_boot_progress (50);
444 image_print_contents (hdr);
446 cnt = image_get_image_size (hdr);
448 #if defined(CONFIG_FIT)
449 case IMAGE_FORMAT_FIT:
450 fit_hdr = (const void *)addr;
451 puts ("Fit image detected...\n");
453 cnt = fit_get_size (fit_hdr);
457 show_boot_progress (-49);
458 puts ("** Unknown image type\n");
462 cnt += info.blksz - 1;
466 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
467 (ulong *)(addr+info.blksz)) != cnt) {
468 printf ("** Read error on %d:%d\n", dev, part);
469 show_boot_progress (-51);
472 show_boot_progress (51);
474 #if defined(CONFIG_FIT)
475 /* This cannot be done earlier, we need complete FIT image in RAM first */
476 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
477 if (!fit_check_format (fit_hdr)) {
478 show_boot_progress (-140);
479 puts ("** Bad FIT image format\n");
482 show_boot_progress (141);
483 fit_print_contents (fit_hdr);
487 /* Loading ok, update default load address */
491 /* Check if we should attempt an auto-start */
492 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
494 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
496 local_args[0] = argv[0];
497 local_args[1] = NULL;
499 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
501 do_bootm (cmdtp, 0, 1, local_args);
507 /* ------------------------------------------------------------------------- */
510 __ide_outb(int dev, int port, unsigned char val)
512 debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
513 dev, port, val, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
514 outb(val, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
516 void ide_outb (int dev, int port, unsigned char val)
517 __attribute__((weak, alias("__ide_outb")));
520 __ide_inb(int dev, int port)
523 val = inb((ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
524 debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
525 dev, port, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)), val);
528 unsigned char ide_inb(int dev, int port)
529 __attribute__((weak, alias("__ide_inb")));
531 #ifdef CONFIG_TUNE_PIO
533 __ide_set_piomode(int pio_mode)
537 int inline ide_set_piomode(int pio_mode)
538 __attribute__((weak, alias("__ide_set_piomode")));
544 #ifdef CONFIG_IDE_8xx_DIRECT
545 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
546 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
550 #if defined(CONFIG_SC3)
551 unsigned int ata_reset_time = ATA_RESET_TIME;
553 #ifdef CONFIG_IDE_8xx_PCCARD
554 extern int pcmcia_on (void);
555 extern int ide_devices_found; /* Initialized in check_ide_device() */
556 #endif /* CONFIG_IDE_8xx_PCCARD */
558 #ifdef CONFIG_IDE_PREINIT
559 extern int ide_preinit (void);
562 if (ide_preinit ()) {
563 puts ("ide_preinit failed\n");
566 #endif /* CONFIG_IDE_PREINIT */
568 #ifdef CONFIG_IDE_8xx_PCCARD
569 extern int pcmcia_on (void);
570 extern int ide_devices_found; /* Initialized in check_ide_device() */
574 ide_devices_found = 0;
575 /* initialize the PCMCIA IDE adapter card */
577 if (!ide_devices_found)
579 udelay (1000000); /* 1 s */
580 #endif /* CONFIG_IDE_8xx_PCCARD */
584 #ifdef CONFIG_IDE_8xx_DIRECT
585 /* Initialize PIO timing tables */
586 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
587 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
589 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
591 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
593 debug ( "PIO Mode %d: setup=%2d ns/%d clk"
595 " hold=%2d ns/%d clk\n",
597 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
598 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
599 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
601 #endif /* CONFIG_IDE_8xx_DIRECT */
603 /* Reset the IDE just to be sure.
604 * Light LED's to show
606 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
607 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
609 #ifdef CONFIG_IDE_8xx_DIRECT
610 /* PCMCIA / IDE initialization for common mem space */
611 pcmp->pcmc_pgcrb = 0;
613 /* start in PIO mode 0 - most relaxed timings */
615 set_pcmcia_timing (pio_mode);
616 #endif /* CONFIG_IDE_8xx_DIRECT */
619 * Wait for IDE to get ready.
620 * According to spec, this can take up to 31 seconds!
622 for (bus=0; bus<CONFIG_SYS_IDE_MAXBUS; ++bus) {
623 int dev = bus * (CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS);
625 #ifdef CONFIG_IDE_8xx_PCCARD
626 /* Skip non-ide devices from probing */
627 if ((ide_devices_found & (1 << bus)) == 0) {
628 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
632 printf ("Bus %d: ", bus);
638 udelay (100000); /* 100 ms */
639 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
640 udelay (100000); /* 100 ms */
643 udelay (10000); /* 10 ms */
645 c = ide_inb (dev, ATA_STATUS);
647 #if defined(CONFIG_SC3)
648 if (i > (ata_reset_time * 100)) {
650 if (i > (ATA_RESET_TIME * 100)) {
652 puts ("** Timeout **\n");
653 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
656 if ((i >= 100) && ((i%100)==0)) {
659 } while (c & ATA_STAT_BUSY);
661 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
662 puts ("not available ");
663 debug ("Status = 0x%02X ", c);
664 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
665 } else if ((c & ATA_STAT_READY) == 0) {
666 puts ("not available ");
667 debug ("Status = 0x%02X ", c);
678 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
681 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i) {
682 #ifdef CONFIG_IDE_LED
683 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
685 ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
686 ide_dev_desc[i].if_type=IF_TYPE_IDE;
687 ide_dev_desc[i].dev=i;
688 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
689 ide_dev_desc[i].blksz=0;
690 ide_dev_desc[i].lba=0;
691 ide_dev_desc[i].block_read=ide_read;
692 if (!ide_bus_ok[IDE_BUS(i)])
694 ide_led (led, 1); /* LED on */
695 ide_ident(&ide_dev_desc[i]);
696 ide_led (led, 0); /* LED off */
697 dev_print(&ide_dev_desc[i]);
699 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
700 init_part (&ide_dev_desc[i]); /* initialize partition type */
708 /* ------------------------------------------------------------------------- */
710 block_dev_desc_t * ide_get_dev(int dev)
712 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
716 #ifdef CONFIG_IDE_8xx_DIRECT
719 set_pcmcia_timing (int pmode)
721 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
722 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
725 debug ("Set timing for PIO Mode %d\n", pmode);
727 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
728 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
729 | PCMCIA_SL (pio_config_clk[pmode].t_length)
734 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
735 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
736 #if (CONFIG_SYS_PCMCIA_POR0 != 0)
740 debug ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
742 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
743 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
744 #if (CONFIG_SYS_PCMCIA_POR1 != 0)
748 debug ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
750 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
751 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
752 #if (CONFIG_SYS_PCMCIA_POR2 != 0)
756 debug ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
758 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
759 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
760 #if (CONFIG_SYS_PCMCIA_POR3 != 0)
764 debug ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
768 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
769 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
770 #if (CONFIG_SYS_PCMCIA_POR4 != 0)
774 debug ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
776 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
777 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
778 #if (CONFIG_SYS_PCMCIA_POR5 != 0)
782 debug ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
784 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
785 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
786 #if (CONFIG_SYS_PCMCIA_POR6 != 0)
790 debug ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
792 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
793 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
794 #if (CONFIG_SYS_PCMCIA_POR7 != 0)
798 debug ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
802 #endif /* CONFIG_IDE_8xx_DIRECT */
804 /* ------------------------------------------------------------------------- */
806 /* We only need to swap data if we are running on a big endian cpu. */
807 /* But Au1x00 cpu:s already swaps data in big endian mode! */
808 #if defined(__LITTLE_ENDIAN) || ( defined(CONFIG_AU1X00) && !defined(CONFIG_GTH2) )
809 #define input_swap_data(x,y,z) input_data(x,y,z)
812 input_swap_data(int dev, ulong *sect_buf, int words)
814 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
816 volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
817 volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
818 ushort *dbuf = (ushort *)sect_buf;
821 for (i=0; i<2; i++) {
822 *(((uchar *)(dbuf)) + 1) = *pbuf_even;
823 *(uchar *)dbuf = *pbuf_odd;
828 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
829 ushort *dbuf = (ushort *)sect_buf;
831 debug("in input swap data base for read is %lx\n", (unsigned long) pbuf);
835 *dbuf++ = swab16p((u16*)pbuf);
836 *dbuf++ = swab16p((u16*)pbuf);
837 #elif defined(CONFIG_PCS440EP)
841 *dbuf++ = ld_le16(pbuf);
842 *dbuf++ = ld_le16(pbuf);
847 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
850 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
852 output_data(int dev, ulong *sect_buf, int words)
854 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
856 volatile uchar *pbuf_even;
857 volatile uchar *pbuf_odd;
859 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
860 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
861 dbuf = (uchar *)sect_buf;
864 *pbuf_even = *dbuf++;
868 *pbuf_even = *dbuf++;
874 volatile ushort *pbuf;
876 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
877 dbuf = (ushort *)sect_buf;
879 #if defined(CONFIG_PCS440EP)
880 /* not tested, because CF was write protected */
882 *pbuf = ld_le16(dbuf++);
884 *pbuf = ld_le16(dbuf++);
894 #else /* ! __PPC__ */
896 output_data(int dev, ulong *sect_buf, int words)
898 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
902 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
904 input_data(int dev, ulong *sect_buf, int words)
906 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
908 volatile uchar *pbuf_even;
909 volatile uchar *pbuf_odd;
911 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
912 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
913 dbuf = (uchar *)sect_buf;
915 *dbuf++ = *pbuf_even;
921 *dbuf++ = *pbuf_even;
930 volatile ushort *pbuf;
932 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
933 dbuf = (ushort *)sect_buf;
935 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
938 #if defined(CONFIG_PCS440EP)
940 *dbuf++ = ld_le16(pbuf);
942 *dbuf++ = ld_le16(pbuf);
952 #else /* ! __PPC__ */
954 input_data(int dev, ulong *sect_buf, int words)
956 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
961 /* -------------------------------------------------------------------------
963 static void ide_ident (block_dev_desc_t *dev_desc)
965 ulong iobuf[ATA_SECTORWORDS];
967 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
974 #ifdef CONFIG_TUNE_PIO
979 int mode, cycle_time;
982 device=dev_desc->dev;
983 printf (" Device %d: ", device);
985 ide_led (DEVICE_LED(device), 1); /* LED on */
988 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
989 dev_desc->if_type=IF_TYPE_IDE;
995 /* Warning: This will be tricky to read */
996 while (retries <= 1) {
997 /* check signature */
998 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
999 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
1000 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
1001 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
1002 /* ATAPI Signature found */
1003 dev_desc->if_type=IF_TYPE_ATAPI;
1004 /* Start Ident Command
1006 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
1008 * Wait for completion - ATAPI devices need more time
1011 c = ide_wait (device, ATAPI_TIME_OUT);
1015 /* Start Ident Command
1017 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
1019 /* Wait for completion
1021 c = ide_wait (device, IDE_TIME_OUT);
1023 ide_led (DEVICE_LED(device), 0); /* LED off */
1025 if (((c & ATA_STAT_DRQ) == 0) ||
1026 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
1029 /* Need to soft reset the device in case it's an ATAPI... */
1030 debug ("Retrying...\n");
1031 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1033 ide_outb (device, ATA_COMMAND, 0x08);
1034 udelay (500000); /* 500 ms */
1038 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1047 } /* see above - ugly to read */
1049 if (retries == 2) /* Not found */
1053 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1055 ident_cpy ((unsigned char*)dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1056 ident_cpy ((unsigned char*)dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1057 ident_cpy ((unsigned char*)dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1058 #ifdef __LITTLE_ENDIAN
1060 * firmware revision, model, and serial number have Big Endian Byte
1061 * order in Word. Convert all three to little endian.
1063 * See CF+ and CompactFlash Specification Revision 2.0:
1064 * 6.2.1.6: Identify Drive, Table 39 for more details
1067 strswab (dev_desc->revision);
1068 strswab (dev_desc->vendor);
1069 strswab (dev_desc->product);
1070 #endif /* __LITTLE_ENDIAN */
1072 if ((iop->config & 0x0080)==0x0080)
1073 dev_desc->removable = 1;
1075 dev_desc->removable = 0;
1077 #ifdef CONFIG_TUNE_PIO
1078 /* Mode 0 - 2 only, are directly determined by word 51. */
1079 pio_mode = iop->tPIO;
1081 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
1082 pio_mode = 0; /* Force it to dead slow, and hope for the best... */
1085 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
1086 * shall set bit 1 of word 53 to one and support the fields contained
1087 * in words 64 through 70.
1089 if (iop->field_valid & 0x02) {
1090 /* Mode 3 and above are possible. Check in order from slow
1091 * to fast, so we wind up with the highest mode allowed.
1093 if (iop->eide_pio_modes & 0x01)
1095 if (iop->eide_pio_modes & 0x02)
1097 if (ata_id_is_cfa((u16 *)iop)) {
1098 if ((iop->cf_advanced_caps & 0x07) == 0x01)
1100 if ((iop->cf_advanced_caps & 0x07) == 0x02)
1105 /* System-specific, depends on bus speeds, etc. */
1106 ide_set_piomode(pio_mode);
1107 #endif /* CONFIG_TUNE_PIO */
1111 * Drive PIO mode autoselection
1115 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1116 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1118 debug ("Override tPIO -> 2\n");
1120 if (iop->field_valid & 2) { /* drive implements ATA2? */
1121 debug ("Drive implements ATA2\n");
1122 if (iop->capability & 8) { /* drive supports use_iordy? */
1123 cycle_time = iop->eide_pio_iordy;
1125 cycle_time = iop->eide_pio;
1127 debug ("cycle time = %d\n", cycle_time);
1129 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1130 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1131 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1132 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1134 printf ("PIO mode to use: PIO %d\n", mode);
1138 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1139 atapi_inquiry(dev_desc);
1142 #endif /* CONFIG_ATAPI */
1146 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1147 #else /* ! __BIG_ENDIAN */
1149 * do not swap shorts on little endian
1151 * See CF+ and CompactFlash Specification Revision 2.0:
1152 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1154 dev_desc->lba = iop->lba_capacity;
1155 #endif /* __BIG_ENDIAN */
1158 if (iop->command_set_2 & 0x0400) { /* LBA 48 support */
1159 dev_desc->lba48 = 1;
1160 dev_desc->lba = (unsigned long long)iop->lba48_capacity[0] |
1161 ((unsigned long long)iop->lba48_capacity[1] << 16) |
1162 ((unsigned long long)iop->lba48_capacity[2] << 32) |
1163 ((unsigned long long)iop->lba48_capacity[3] << 48);
1165 dev_desc->lba48 = 0;
1167 #endif /* CONFIG_LBA48 */
1169 dev_desc->type=DEV_TYPE_HARDDISK;
1170 dev_desc->blksz=ATA_BLOCKSIZE;
1171 dev_desc->lun=0; /* just to fill something in... */
1173 #if 0 /* only used to test the powersaving mode,
1174 * if enabled, the drive goes after 5 sec
1175 * in standby mode */
1176 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1177 c = ide_wait (device, IDE_TIME_OUT);
1178 ide_outb (device, ATA_SECT_CNT, 1);
1179 ide_outb (device, ATA_LBA_LOW, 0);
1180 ide_outb (device, ATA_LBA_MID, 0);
1181 ide_outb (device, ATA_LBA_HIGH, 0);
1182 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1183 ide_outb (device, ATA_COMMAND, 0xe3);
1185 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1190 /* ------------------------------------------------------------------------- */
1192 ulong ide_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1196 unsigned char pwrsave=0; /* power save */
1198 unsigned char lba48 = 0;
1200 if (blknr & 0x0000fffff0000000ULL) {
1201 /* more than 28 bits used, use 48bit mode */
1205 debug ("ide_read dev %d start %LX, blocks %lX buffer at %lX\n",
1206 device, blknr, blkcnt, (ulong)buffer);
1208 ide_led (DEVICE_LED(device), 1); /* LED on */
1212 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1213 c = ide_wait (device, IDE_TIME_OUT);
1215 if (c & ATA_STAT_BUSY) {
1216 printf ("IDE read: device %d not ready\n", device);
1220 /* first check if the drive is in Powersaving mode, if yes,
1221 * increase the timeout value */
1222 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1225 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1227 if (c & ATA_STAT_BUSY) {
1228 printf ("IDE read: device %d not ready\n", device);
1231 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1232 printf ("No Powersaving mode %X\n", c);
1234 c = ide_inb(device,ATA_SECT_CNT);
1235 debug ("Powersaving %02X\n",c);
1241 while (blkcnt-- > 0) {
1243 c = ide_wait (device, IDE_TIME_OUT);
1245 if (c & ATA_STAT_BUSY) {
1246 printf ("IDE read: device %d not ready\n", device);
1251 /* write high bits */
1252 ide_outb (device, ATA_SECT_CNT, 0);
1253 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1254 #ifdef CONFIG_SYS_64BIT_LBA
1255 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1256 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1258 ide_outb (device, ATA_LBA_MID, 0);
1259 ide_outb (device, ATA_LBA_HIGH, 0);
1263 ide_outb (device, ATA_SECT_CNT, 1);
1264 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1265 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1266 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1270 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1271 ide_outb (device, ATA_COMMAND, ATA_CMD_READ_EXT);
1276 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1277 ATA_DEVICE(device) |
1278 ((blknr >> 24) & 0xF) );
1279 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1285 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1288 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1291 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1292 #if defined(CONFIG_SYS_64BIT_LBA)
1293 printf ("Error (no IRQ) dev %d blk %Ld: status 0x%02x\n",
1296 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1297 device, (ulong)blknr, c);
1302 input_data (device, buffer, ATA_SECTORWORDS);
1303 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1307 buffer += ATA_BLOCKSIZE;
1310 ide_led (DEVICE_LED(device), 0); /* LED off */
1314 /* ------------------------------------------------------------------------- */
1317 ulong ide_write (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1322 unsigned char lba48 = 0;
1324 if (blknr & 0x0000fffff0000000ULL) {
1325 /* more than 28 bits used, use 48bit mode */
1330 ide_led (DEVICE_LED(device), 1); /* LED on */
1334 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1336 while (blkcnt-- > 0) {
1338 c = ide_wait (device, IDE_TIME_OUT);
1340 if (c & ATA_STAT_BUSY) {
1341 printf ("IDE read: device %d not ready\n", device);
1346 /* write high bits */
1347 ide_outb (device, ATA_SECT_CNT, 0);
1348 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1349 #ifdef CONFIG_SYS_64BIT_LBA
1350 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1351 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1353 ide_outb (device, ATA_LBA_MID, 0);
1354 ide_outb (device, ATA_LBA_HIGH, 0);
1358 ide_outb (device, ATA_SECT_CNT, 1);
1359 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1360 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1361 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1365 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1366 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1371 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1372 ATA_DEVICE(device) |
1373 ((blknr >> 24) & 0xF) );
1374 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1379 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1381 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1382 #if defined(CONFIG_SYS_64BIT_LBA)
1383 printf ("Error (no IRQ) dev %d blk %Ld: status 0x%02x\n",
1386 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1387 device, (ulong)blknr, c);
1392 output_data (device, buffer, ATA_SECTORWORDS);
1393 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1396 buffer += ATA_BLOCKSIZE;
1399 ide_led (DEVICE_LED(device), 0); /* LED off */
1403 /* ------------------------------------------------------------------------- */
1406 * copy src to dest, skipping leading and trailing blanks and null
1407 * terminate the string
1408 * "len" is the size of available memory including the terminating '\0'
1410 static void ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
1412 unsigned char *end, *last;
1415 end = src + len - 1;
1417 /* reserve space for '\0' */
1421 /* skip leading white space */
1422 while ((*src) && (src<end) && (*src==' '))
1425 /* copy string, omitting trailing white space */
1426 while ((*src) && (src<end)) {
1435 /* ------------------------------------------------------------------------- */
1438 * Wait until Busy bit is off, or timeout (in ms)
1439 * Return last status
1441 static uchar ide_wait (int dev, ulong t)
1443 ulong delay = 10 * t; /* poll every 100 us */
1446 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1455 /* ------------------------------------------------------------------------- */
1457 #ifdef CONFIG_IDE_RESET
1458 extern void ide_set_reset(int idereset);
1460 static void ide_reset (void)
1462 #if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
1463 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
1468 for (i=0; i<CONFIG_SYS_IDE_MAXBUS; ++i)
1470 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i)
1471 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1473 ide_set_reset (1); /* assert reset */
1475 /* the reset signal shall be asserted for et least 25 us */
1480 #ifdef CONFIG_SYS_PB_12V_ENABLE
1481 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE); /* 12V Enable output OFF */
1482 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1483 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
1484 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
1486 /* wait 500 ms for the voltage to stabilize
1488 for (i=0; i<500; ++i) {
1492 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE; /* 12V Enable output ON */
1493 #endif /* CONFIG_SYS_PB_12V_ENABLE */
1495 #ifdef CONFIG_SYS_PB_IDE_MOTOR
1496 /* configure IDE Motor voltage monitor pin as input */
1497 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1498 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1499 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1501 /* wait up to 1 s for the motor voltage to stabilize
1503 for (i=0; i<1000; ++i) {
1504 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
1510 if (i == 1000) { /* Timeout */
1511 printf ("\nWarning: 5V for IDE Motor missing\n");
1512 # ifdef CONFIG_STATUS_LED
1513 # ifdef STATUS_LED_YELLOW
1514 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1516 # ifdef STATUS_LED_GREEN
1517 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1519 # endif /* CONFIG_STATUS_LED */
1521 #endif /* CONFIG_SYS_PB_IDE_MOTOR */
1525 /* de-assert RESET signal */
1529 for (i=0; i<250; ++i) {
1534 #endif /* CONFIG_IDE_RESET */
1536 /* ------------------------------------------------------------------------- */
1538 #if defined(CONFIG_IDE_LED) && \
1539 !defined(CONFIG_CPC45) && \
1540 !defined(CONFIG_HMI10) && \
1541 !defined(CONFIG_KUP4K) && \
1542 !defined(CONFIG_KUP4X)
1544 static uchar led_buffer = 0; /* Buffer for current LED status */
1546 static void ide_led (uchar led, uchar status)
1548 uchar *led_port = LED_PORT;
1550 if (status) { /* switch LED on */
1552 } else { /* switch LED off */
1556 *led_port = led_buffer;
1559 #endif /* CONFIG_IDE_LED */
1561 #if defined(CONFIG_OF_IDE_FIXUP)
1562 int ide_device_present(int dev)
1564 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1566 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1569 /* ------------------------------------------------------------------------- */
1572 /****************************************************************************
1576 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
1577 /* since ATAPI may use commands with not 4 bytes alligned length
1578 * we have our own transfer functions, 2 bytes alligned */
1580 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1582 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
1584 volatile uchar *pbuf_even;
1585 volatile uchar *pbuf_odd;
1587 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1588 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1591 *pbuf_even = *dbuf++;
1593 *pbuf_odd = *dbuf++;
1597 volatile ushort *pbuf;
1599 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1600 dbuf = (ushort *)sect_buf;
1602 debug ("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
1612 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1614 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
1616 volatile uchar *pbuf_even;
1617 volatile uchar *pbuf_odd;
1619 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1620 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1623 *dbuf++ = *pbuf_even;
1625 *dbuf++ = *pbuf_odd;
1629 volatile ushort *pbuf;
1631 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1632 dbuf = (ushort *)sect_buf;
1634 debug("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
1643 #else /* ! __PPC__ */
1645 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1647 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1651 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1653 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1656 #endif /* __PPC__ */
1659 * Wait until (Status & mask) == res, or timeout (in ms)
1660 * Return last status
1661 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1662 * and then they set their DRQ Bit
1664 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1666 ulong delay = 10 * t; /* poll every 100 us */
1669 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1670 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1671 /* break if error occurs (doesn't make sense to wait more) */
1672 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1683 * issue an atapi command
1685 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1687 unsigned char c,err,mask,res;
1689 ide_led (DEVICE_LED(device), 1); /* LED on */
1693 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1695 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1696 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1697 if ((c & mask) != res) {
1698 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1702 /* write taskfile */
1703 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1704 ide_outb (device, ATA_SECT_CNT, 0);
1705 ide_outb (device, ATA_SECT_NUM, 0);
1706 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1707 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1708 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1710 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1713 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1715 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1717 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1718 printf ("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1723 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1724 /* ATAPI Command written wait for completition */
1725 udelay (5000); /* device must set bsy */
1727 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1728 /* if no data wait for DRQ = 0 BSY = 0
1729 * if data wait for DRQ = 1 BSY = 0 */
1733 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1734 if ((c & mask) != res ) {
1735 if (c & ATA_STAT_ERR) {
1736 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1737 debug ("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1739 printf ("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1744 n=ide_inb(device, ATA_CYL_HIGH);
1746 n+=ide_inb(device, ATA_CYL_LOW);
1748 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1752 if((n==0)&&(buflen<0)) {
1753 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1758 debug ("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1760 if(n!=0) { /* data transfer */
1761 debug ("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1762 /* we transfer shorts */
1764 /* ok now decide if it is an in or output */
1765 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1766 debug ("Write to device\n");
1767 output_data_shorts(device,(unsigned short *)buffer,n);
1769 debug ("Read from device @ %p shorts %d\n",buffer,n);
1770 input_data_shorts(device,(unsigned short *)buffer,n);
1773 udelay(5000); /* seems that some CD ROMs need this... */
1774 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1776 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1777 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1778 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1779 debug ("atapi_issue 2 returned sense key %X status %X\n",err,c);
1784 ide_led (DEVICE_LED(device), 0); /* LED off */
1789 * sending the command to atapi_issue. If an status other than good
1790 * returns, an request_sense will be issued
1793 #define ATAPI_DRIVE_NOT_READY 100
1794 #define ATAPI_UNIT_ATTN 10
1796 unsigned char atapi_issue_autoreq (int device,
1799 unsigned char *buffer,
1802 unsigned char sense_data[18],sense_ccb[12];
1803 unsigned char res,key,asc,ascq;
1804 int notready,unitattn;
1806 unitattn=ATAPI_UNIT_ATTN;
1807 notready=ATAPI_DRIVE_NOT_READY;
1810 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1812 return (0); /* Ok */
1815 return (0xFF); /* error */
1817 debug ("(auto_req)atapi_issue returned sense key %X\n",res);
1819 memset(sense_ccb,0,sizeof(sense_ccb));
1820 memset(sense_data,0,sizeof(sense_data));
1821 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1822 sense_ccb[4]=18; /* allocation Length */
1824 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1825 key=(sense_data[2]&0xF);
1826 asc=(sense_data[12]);
1827 ascq=(sense_data[13]);
1829 debug ("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1830 debug (" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1837 return 0; /* ok device ready */
1839 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1844 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1847 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1852 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1856 debug ("Media not present\n");
1860 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1862 debug ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1867 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1869 unsigned char ccb[12]; /* Command descriptor block */
1870 unsigned char iobuf[64]; /* temp buf */
1874 device=dev_desc->dev;
1875 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1876 dev_desc->block_read=atapi_read;
1878 memset(ccb,0,sizeof(ccb));
1879 memset(iobuf,0,sizeof(iobuf));
1881 ccb[0]=ATAPI_CMD_INQUIRY;
1882 ccb[4]=40; /* allocation Legnth */
1883 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1885 debug ("ATAPI_CMD_INQUIRY returned %x\n",c);
1889 /* copy device ident strings */
1890 ident_cpy((unsigned char*)dev_desc->vendor,&iobuf[8],8);
1891 ident_cpy((unsigned char*)dev_desc->product,&iobuf[16],16);
1892 ident_cpy((unsigned char*)dev_desc->revision,&iobuf[32],5);
1897 dev_desc->type=iobuf[0] & 0x1f;
1899 if ((iobuf[1]&0x80)==0x80)
1900 dev_desc->removable = 1;
1902 dev_desc->removable = 0;
1904 memset(ccb,0,sizeof(ccb));
1905 memset(iobuf,0,sizeof(iobuf));
1906 ccb[0]=ATAPI_CMD_START_STOP;
1907 ccb[4]=0x03; /* start */
1909 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1911 debug ("ATAPI_CMD_START_STOP returned %x\n",c);
1915 memset(ccb,0,sizeof(ccb));
1916 memset(iobuf,0,sizeof(iobuf));
1917 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1919 debug ("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1923 memset(ccb,0,sizeof(ccb));
1924 memset(iobuf,0,sizeof(iobuf));
1925 ccb[0]=ATAPI_CMD_READ_CAP;
1926 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1927 debug ("ATAPI_CMD_READ_CAP returned %x\n",c);
1931 debug ("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1932 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1933 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1935 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1936 ((unsigned long)iobuf[1]<<16) +
1937 ((unsigned long)iobuf[2]<< 8) +
1938 ((unsigned long)iobuf[3]);
1939 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1940 ((unsigned long)iobuf[5]<<16) +
1941 ((unsigned long)iobuf[6]<< 8) +
1942 ((unsigned long)iobuf[7]);
1944 dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1952 * we transfer only one block per command, since the multiple DRQ per
1953 * command is not yet implemented
1955 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1956 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1957 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1959 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1962 unsigned char ccb[12]; /* Command descriptor block */
1965 debug ("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1966 device, blknr, blkcnt, (ulong)buffer);
1969 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1970 cnt=ATAPI_READ_MAX_BLOCK;
1974 ccb[0]=ATAPI_CMD_READ_12;
1975 ccb[1]=0; /* reserved */
1976 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1977 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1978 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1979 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1980 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1981 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1982 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1983 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1984 ccb[10]=0; /* reserved */
1985 ccb[11]=0; /* reserved */
1987 if (atapi_issue_autoreq(device,ccb,12,
1988 (unsigned char *)buffer,
1989 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1995 buffer+=(cnt*ATAPI_READ_BLOCK_SIZE);
1996 } while (blkcnt > 0);
2000 /* ------------------------------------------------------------------------- */
2002 #endif /* CONFIG_ATAPI */
2007 "reset - reset IDE controller\n"
2008 "ide info - show available IDE devices\n"
2009 "ide device [dev] - show or set current device\n"
2010 "ide part [dev] - print partition table of one or all IDE devices\n"
2011 "ide read addr blk# cnt\n"
2012 "ide write addr blk# cnt - read/write `cnt'"
2013 " blocks starting at block `blk#'\n"
2014 " to/from memory address `addr'"
2018 diskboot, 3, 1, do_diskboot,
2019 "boot from IDE device",