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,
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,
123 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
125 block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
126 /* ------------------------------------------------------------------------- */
128 #ifdef CONFIG_IDE_LED
129 # if !defined(CONFIG_BMS2003) && \
130 !defined(CONFIG_CPC45) && \
131 !defined(CONFIG_KUP4K) && \
132 !defined(CONFIG_KUP4X)
133 static void ide_led (uchar led, uchar status);
135 extern void ide_led (uchar led, uchar status);
138 #define ide_led(a,b) /* dummy */
141 #ifdef CONFIG_IDE_RESET
142 static void ide_reset (void);
144 #define ide_reset() /* dummy */
147 static void ide_ident (block_dev_desc_t *dev_desc);
148 static uchar ide_wait (int dev, ulong t);
150 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
152 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
154 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
156 static void input_data(int dev, ulong *sect_buf, int words);
157 static void output_data(int dev, const ulong *sect_buf, int words);
158 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
160 #ifndef CONFIG_SYS_ATA_PORT_ADDR
161 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
165 static void atapi_inquiry(block_dev_desc_t *dev_desc);
166 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
170 #ifdef CONFIG_IDE_8xx_DIRECT
171 static void set_pcmcia_timing (int pmode);
174 /* ------------------------------------------------------------------------- */
176 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
183 return cmd_usage(cmdtp);
185 if (strncmp(argv[1],"res",3) == 0) {
187 #ifdef CONFIG_IDE_8xx_DIRECT
188 " on PCMCIA " PCMCIA_SLOT_MSG
194 } else if (strncmp(argv[1],"inf",3) == 0) {
199 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i) {
200 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
201 continue; /* list only known devices */
202 printf ("IDE device %d: ", i);
203 dev_print(&ide_dev_desc[i]);
207 } else if (strncmp(argv[1],"dev",3) == 0) {
208 if ((curr_device < 0) || (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; dev<CONFIG_SYS_IDE_MAXDEVICE; ++dev) {
219 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
223 print_part(&ide_dev_desc[dev]);
227 puts ("\nno IDE devices available\n");
232 return cmd_usage(cmdtp);
234 if (strncmp(argv[1],"dev",3) == 0) {
235 int dev = (int)simple_strtoul(argv[2], NULL, 10);
237 printf ("\nIDE device %d: ", dev);
238 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
239 puts ("unknown device\n");
242 dev_print(&ide_dev_desc[dev]);
245 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
251 puts ("... is now current device\n");
254 } else if (strncmp(argv[1],"part",4) == 0) {
255 int dev = (int)simple_strtoul(argv[2], NULL, 10);
257 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
258 print_part(&ide_dev_desc[dev]);
260 printf ("\nIDE device %d not available\n", dev);
265 } else if (strncmp(argv[1],"pio",4) == 0) {
266 int mode = (int)simple_strtoul(argv[2], NULL, 10);
268 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
273 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
274 mode, IDE_MAX_PIO_MODE);
280 return cmd_usage(cmdtp);
282 /* at least 4 args */
284 if (strcmp(argv[1],"read") == 0) {
285 ulong addr = simple_strtoul(argv[2], NULL, 16);
286 ulong cnt = simple_strtoul(argv[4], NULL, 16);
288 #ifdef CONFIG_SYS_64BIT_LBA
289 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
291 printf ("\nIDE read: device %d block # %Ld, count %ld ... ",
292 curr_device, blk, cnt);
294 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
296 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
297 curr_device, blk, cnt);
300 n = ide_dev_desc[curr_device].block_read (curr_device,
303 /* flush cache after read */
304 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
306 printf ("%ld blocks read: %s\n",
307 n, (n==cnt) ? "OK" : "ERROR");
313 } else if (strcmp(argv[1],"write") == 0) {
314 ulong addr = simple_strtoul(argv[2], NULL, 16);
315 ulong cnt = simple_strtoul(argv[4], NULL, 16);
317 #ifdef CONFIG_SYS_64BIT_LBA
318 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
320 printf ("\nIDE write: device %d block # %Ld, count %ld ... ",
321 curr_device, blk, cnt);
323 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
325 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
326 curr_device, blk, cnt);
329 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
331 printf ("%ld blocks written: %s\n",
332 n, (n==cnt) ? "OK" : "ERROR");
338 return cmd_usage(cmdtp);
345 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
347 char *boot_device = NULL;
351 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 return bootm_maybe_autostart(cmdtp, argv[0]);
494 /* ------------------------------------------------------------------------- */
497 __ide_outb(int dev, int port, unsigned char val)
499 debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
500 dev, port, val, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
502 #if defined(CONFIG_IDE_AHB)
505 ide_write_register(dev, port, val);
508 outb(val, (ATA_CURR_BASE(dev)));
511 outb(val, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
515 void ide_outb (int dev, int port, unsigned char val)
516 __attribute__((weak, alias("__ide_outb")));
519 __ide_inb(int dev, int port)
523 #if defined(CONFIG_IDE_AHB)
524 val = ide_read_register(dev, port);
526 val = inb((ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
529 debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
530 dev, port, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)), val);
533 unsigned char ide_inb(int dev, int port)
534 __attribute__((weak, alias("__ide_inb")));
536 #ifdef CONFIG_TUNE_PIO
538 __ide_set_piomode(int pio_mode)
542 int inline ide_set_piomode(int pio_mode)
543 __attribute__((weak, alias("__ide_set_piomode")));
549 #ifdef CONFIG_IDE_8xx_DIRECT
550 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
551 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
555 #if defined(CONFIG_SC3)
556 unsigned int ata_reset_time = ATA_RESET_TIME;
558 #ifdef CONFIG_IDE_8xx_PCCARD
559 extern int pcmcia_on (void);
560 extern int ide_devices_found; /* Initialized in check_ide_device() */
561 #endif /* CONFIG_IDE_8xx_PCCARD */
563 #ifdef CONFIG_IDE_PREINIT
564 extern int ide_preinit (void);
567 if (ide_preinit ()) {
568 puts ("ide_preinit failed\n");
571 #endif /* CONFIG_IDE_PREINIT */
573 #ifdef CONFIG_IDE_8xx_PCCARD
574 extern int pcmcia_on (void);
575 extern int ide_devices_found; /* Initialized in check_ide_device() */
579 ide_devices_found = 0;
580 /* initialize the PCMCIA IDE adapter card */
582 if (!ide_devices_found)
584 udelay (1000000); /* 1 s */
585 #endif /* CONFIG_IDE_8xx_PCCARD */
589 #ifdef CONFIG_IDE_8xx_DIRECT
590 /* Initialize PIO timing tables */
591 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
592 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
594 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
596 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
598 debug ( "PIO Mode %d: setup=%2d ns/%d clk"
600 " hold=%2d ns/%d clk\n",
602 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
603 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
604 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
606 #endif /* CONFIG_IDE_8xx_DIRECT */
608 /* Reset the IDE just to be sure.
609 * Light LED's to show
611 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
612 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
614 #ifdef CONFIG_IDE_8xx_DIRECT
615 /* PCMCIA / IDE initialization for common mem space */
616 pcmp->pcmc_pgcrb = 0;
618 /* start in PIO mode 0 - most relaxed timings */
620 set_pcmcia_timing (pio_mode);
621 #endif /* CONFIG_IDE_8xx_DIRECT */
624 * Wait for IDE to get ready.
625 * According to spec, this can take up to 31 seconds!
627 for (bus=0; bus<CONFIG_SYS_IDE_MAXBUS; ++bus) {
628 int dev = bus * (CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS);
630 #ifdef CONFIG_IDE_8xx_PCCARD
631 /* Skip non-ide devices from probing */
632 if ((ide_devices_found & (1 << bus)) == 0) {
633 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
637 printf ("Bus %d: ", bus);
643 udelay (100000); /* 100 ms */
644 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
645 udelay (100000); /* 100 ms */
648 udelay (10000); /* 10 ms */
650 c = ide_inb (dev, ATA_STATUS);
652 #if defined(CONFIG_SC3)
653 if (i > (ata_reset_time * 100)) {
655 if (i > (ATA_RESET_TIME * 100)) {
657 puts ("** Timeout **\n");
658 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
661 if ((i >= 100) && ((i%100)==0)) {
664 } while (c & ATA_STAT_BUSY);
666 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
667 puts ("not available ");
668 debug ("Status = 0x%02X ", c);
669 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
670 } else if ((c & ATA_STAT_READY) == 0) {
671 puts ("not available ");
672 debug ("Status = 0x%02X ", c);
683 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
686 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i) {
687 #ifdef CONFIG_IDE_LED
688 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
690 ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
691 ide_dev_desc[i].if_type=IF_TYPE_IDE;
692 ide_dev_desc[i].dev=i;
693 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
694 ide_dev_desc[i].blksz=0;
695 ide_dev_desc[i].lba=0;
696 ide_dev_desc[i].block_read=ide_read;
697 ide_dev_desc[i].block_write = ide_write;
698 if (!ide_bus_ok[IDE_BUS(i)])
700 ide_led (led, 1); /* LED on */
701 ide_ident(&ide_dev_desc[i]);
702 ide_led (led, 0); /* LED off */
703 dev_print(&ide_dev_desc[i]);
705 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
706 init_part (&ide_dev_desc[i]); /* initialize partition type */
714 /* ------------------------------------------------------------------------- */
716 #ifdef CONFIG_PARTITIONS
717 block_dev_desc_t * ide_get_dev(int dev)
719 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
724 #ifdef CONFIG_IDE_8xx_DIRECT
727 set_pcmcia_timing (int pmode)
729 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
730 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
733 debug ("Set timing for PIO Mode %d\n", pmode);
735 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
736 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
737 | PCMCIA_SL (pio_config_clk[pmode].t_length)
742 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
743 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
744 #if (CONFIG_SYS_PCMCIA_POR0 != 0)
748 debug ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
750 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
751 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
752 #if (CONFIG_SYS_PCMCIA_POR1 != 0)
756 debug ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
758 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
759 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
760 #if (CONFIG_SYS_PCMCIA_POR2 != 0)
764 debug ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
766 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
767 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
768 #if (CONFIG_SYS_PCMCIA_POR3 != 0)
772 debug ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
776 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
777 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
778 #if (CONFIG_SYS_PCMCIA_POR4 != 0)
782 debug ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
784 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
785 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
786 #if (CONFIG_SYS_PCMCIA_POR5 != 0)
790 debug ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
792 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
793 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
794 #if (CONFIG_SYS_PCMCIA_POR6 != 0)
798 debug ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
800 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
801 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
802 #if (CONFIG_SYS_PCMCIA_POR7 != 0)
806 debug ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
810 #endif /* CONFIG_IDE_8xx_DIRECT */
812 /* ------------------------------------------------------------------------- */
814 /* We only need to swap data if we are running on a big endian cpu. */
815 /* But Au1x00 cpu:s already swaps data in big endian mode! */
816 #if defined(__LITTLE_ENDIAN) || \
817 (defined(CONFIG_SOC_AU1X00) && !defined(CONFIG_GTH2))
818 #define input_swap_data(x,y,z) input_data(x,y,z)
821 input_swap_data(int dev, ulong *sect_buf, int words)
823 #if defined(CONFIG_CPC45)
825 volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
826 volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
827 ushort *dbuf = (ushort *)sect_buf;
830 for (i=0; i<2; i++) {
831 *(((uchar *)(dbuf)) + 1) = *pbuf_even;
832 *(uchar *)dbuf = *pbuf_odd;
837 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
838 ushort *dbuf = (ushort *)sect_buf;
840 debug("in input swap data base for read is %lx\n", (unsigned long) pbuf);
844 *dbuf++ = swab16p((u16*)pbuf);
845 *dbuf++ = swab16p((u16*)pbuf);
846 #elif defined(CONFIG_PCS440EP)
850 *dbuf++ = ld_le16(pbuf);
851 *dbuf++ = ld_le16(pbuf);
856 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
859 #if defined(CONFIG_IDE_SWAP_IO)
861 output_data(int dev, const ulong *sect_buf, int words)
863 #if defined(CONFIG_CPC45)
865 volatile uchar *pbuf_even;
866 volatile uchar *pbuf_odd;
868 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
869 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
870 dbuf = (uchar *)sect_buf;
873 *pbuf_even = *dbuf++;
877 *pbuf_even = *dbuf++;
883 volatile ushort *pbuf;
885 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
886 dbuf = (ushort *)sect_buf;
888 #if defined(CONFIG_PCS440EP)
889 /* not tested, because CF was write protected */
891 *pbuf = ld_le16(dbuf++);
893 *pbuf = ld_le16(dbuf++);
903 #else /* ! CONFIG_IDE_SWAP_IO */
905 output_data(int dev, const ulong *sect_buf, int words)
907 #if defined(CONFIG_IDE_AHB)
908 ide_write_data(dev, sect_buf, words);
910 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
913 #endif /* CONFIG_IDE_SWAP_IO */
915 #if defined(CONFIG_IDE_SWAP_IO)
917 input_data(int dev, ulong *sect_buf, int words)
919 #if defined(CONFIG_CPC45)
921 volatile uchar *pbuf_even;
922 volatile uchar *pbuf_odd;
924 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
925 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
926 dbuf = (uchar *)sect_buf;
928 *dbuf++ = *pbuf_even;
934 *dbuf++ = *pbuf_even;
943 volatile ushort *pbuf;
945 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
946 dbuf = (ushort *)sect_buf;
948 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
951 #if defined(CONFIG_PCS440EP)
953 *dbuf++ = ld_le16(pbuf);
955 *dbuf++ = ld_le16(pbuf);
965 #else /* ! CONFIG_IDE_SWAP_IO */
967 input_data(int dev, ulong *sect_buf, int words)
969 #if defined(CONFIG_IDE_AHB)
970 ide_read_data(dev, sect_buf, words);
972 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
976 #endif /* CONFIG_IDE_SWAP_IO */
978 /* -------------------------------------------------------------------------
980 static void ide_ident (block_dev_desc_t *dev_desc)
990 #ifdef CONFIG_TUNE_PIO
995 int mode, cycle_time;
998 device=dev_desc->dev;
999 printf (" Device %d: ", device);
1001 ide_led (DEVICE_LED(device), 1); /* LED on */
1004 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1005 dev_desc->if_type=IF_TYPE_IDE;
1011 /* Warning: This will be tricky to read */
1012 while (retries <= 1) {
1013 /* check signature */
1014 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
1015 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
1016 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
1017 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
1018 /* ATAPI Signature found */
1019 dev_desc->if_type=IF_TYPE_ATAPI;
1020 /* Start Ident Command
1022 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
1024 * Wait for completion - ATAPI devices need more time
1027 c = ide_wait (device, ATAPI_TIME_OUT);
1031 /* Start Ident Command
1033 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
1035 /* Wait for completion
1037 c = ide_wait (device, IDE_TIME_OUT);
1039 ide_led (DEVICE_LED(device), 0); /* LED off */
1041 if (((c & ATA_STAT_DRQ) == 0) ||
1042 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
1045 /* Need to soft reset the device in case it's an ATAPI... */
1046 debug ("Retrying...\n");
1047 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1049 ide_outb (device, ATA_COMMAND, 0x08);
1050 udelay (500000); /* 500 ms */
1054 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1063 } /* see above - ugly to read */
1065 if (retries == 2) /* Not found */
1069 input_swap_data (device, (ulong *)&iop, ATA_SECTORWORDS);
1071 ident_cpy ((unsigned char*)dev_desc->revision, iop.fw_rev, sizeof(dev_desc->revision));
1072 ident_cpy ((unsigned char*)dev_desc->vendor, iop.model, sizeof(dev_desc->vendor));
1073 ident_cpy ((unsigned char*)dev_desc->product, iop.serial_no, sizeof(dev_desc->product));
1074 #ifdef __LITTLE_ENDIAN
1076 * firmware revision, model, and serial number have Big Endian Byte
1077 * order in Word. Convert all three to little endian.
1079 * See CF+ and CompactFlash Specification Revision 2.0:
1080 * 6.2.1.6: Identify Drive, Table 39 for more details
1083 strswab (dev_desc->revision);
1084 strswab (dev_desc->vendor);
1085 strswab (dev_desc->product);
1086 #endif /* __LITTLE_ENDIAN */
1088 if ((iop.config & 0x0080) == 0x0080)
1089 dev_desc->removable = 1;
1091 dev_desc->removable = 0;
1093 #ifdef CONFIG_TUNE_PIO
1094 /* Mode 0 - 2 only, are directly determined by word 51. */
1095 pio_mode = iop.tPIO;
1097 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
1098 pio_mode = 0; /* Force it to dead slow, and hope for the best... */
1101 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
1102 * shall set bit 1 of word 53 to one and support the fields contained
1103 * in words 64 through 70.
1105 if (iop.field_valid & 0x02) {
1106 /* Mode 3 and above are possible. Check in order from slow
1107 * to fast, so we wind up with the highest mode allowed.
1109 if (iop.eide_pio_modes & 0x01)
1111 if (iop.eide_pio_modes & 0x02)
1113 if (ata_id_is_cfa((u16 *)&iop)) {
1114 if ((iop.cf_advanced_caps & 0x07) == 0x01)
1116 if ((iop.cf_advanced_caps & 0x07) == 0x02)
1121 /* System-specific, depends on bus speeds, etc. */
1122 ide_set_piomode(pio_mode);
1123 #endif /* CONFIG_TUNE_PIO */
1127 * Drive PIO mode autoselection
1131 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1132 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1134 debug ("Override tPIO -> 2\n");
1136 if (iop.field_valid & 2) { /* drive implements ATA2? */
1137 debug ("Drive implements ATA2\n");
1138 if (iop.capability & 8) { /* drive supports use_iordy? */
1139 cycle_time = iop.eide_pio_iordy;
1141 cycle_time = iop.eide_pio;
1143 debug ("cycle time = %d\n", cycle_time);
1145 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1146 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1147 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1148 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1150 printf ("PIO mode to use: PIO %d\n", mode);
1154 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1155 atapi_inquiry(dev_desc);
1158 #endif /* CONFIG_ATAPI */
1162 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
1163 #else /* ! __BIG_ENDIAN */
1165 * do not swap shorts on little endian
1167 * See CF+ and CompactFlash Specification Revision 2.0:
1168 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1170 dev_desc->lba = iop.lba_capacity;
1171 #endif /* __BIG_ENDIAN */
1174 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
1175 dev_desc->lba48 = 1;
1176 dev_desc->lba = (unsigned long long)iop.lba48_capacity[0] |
1177 ((unsigned long long)iop.lba48_capacity[1] << 16) |
1178 ((unsigned long long)iop.lba48_capacity[2] << 32) |
1179 ((unsigned long long)iop.lba48_capacity[3] << 48);
1181 dev_desc->lba48 = 0;
1183 #endif /* CONFIG_LBA48 */
1185 dev_desc->type=DEV_TYPE_HARDDISK;
1186 dev_desc->blksz=ATA_BLOCKSIZE;
1187 dev_desc->lun=0; /* just to fill something in... */
1189 #if 0 /* only used to test the powersaving mode,
1190 * if enabled, the drive goes after 5 sec
1191 * in standby mode */
1192 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1193 c = ide_wait (device, IDE_TIME_OUT);
1194 ide_outb (device, ATA_SECT_CNT, 1);
1195 ide_outb (device, ATA_LBA_LOW, 0);
1196 ide_outb (device, ATA_LBA_MID, 0);
1197 ide_outb (device, ATA_LBA_HIGH, 0);
1198 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1199 ide_outb (device, ATA_COMMAND, 0xe3);
1201 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1206 /* ------------------------------------------------------------------------- */
1208 ulong ide_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1212 unsigned char pwrsave=0; /* power save */
1214 unsigned char lba48 = 0;
1216 if (blknr & 0x0000fffff0000000ULL) {
1217 /* more than 28 bits used, use 48bit mode */
1221 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1222 device, blknr, blkcnt, (ulong)buffer);
1224 ide_led (DEVICE_LED(device), 1); /* LED on */
1228 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1229 c = ide_wait (device, IDE_TIME_OUT);
1231 if (c & ATA_STAT_BUSY) {
1232 printf ("IDE read: device %d not ready\n", device);
1236 /* first check if the drive is in Powersaving mode, if yes,
1237 * increase the timeout value */
1238 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1241 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1243 if (c & ATA_STAT_BUSY) {
1244 printf ("IDE read: device %d not ready\n", device);
1247 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1248 printf ("No Powersaving mode %X\n", c);
1250 c = ide_inb(device,ATA_SECT_CNT);
1251 debug ("Powersaving %02X\n",c);
1257 while (blkcnt-- > 0) {
1259 c = ide_wait (device, IDE_TIME_OUT);
1261 if (c & ATA_STAT_BUSY) {
1262 printf ("IDE read: device %d not ready\n", device);
1267 /* write high bits */
1268 ide_outb (device, ATA_SECT_CNT, 0);
1269 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1270 #ifdef CONFIG_SYS_64BIT_LBA
1271 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1272 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1274 ide_outb (device, ATA_LBA_MID, 0);
1275 ide_outb (device, ATA_LBA_HIGH, 0);
1279 ide_outb (device, ATA_SECT_CNT, 1);
1280 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1281 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1282 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1286 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1287 ide_outb (device, ATA_COMMAND, ATA_CMD_READ_EXT);
1292 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1293 ATA_DEVICE(device) |
1294 ((blknr >> 24) & 0xF) );
1295 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1301 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1304 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1307 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1308 #if defined(CONFIG_SYS_64BIT_LBA)
1309 printf ("Error (no IRQ) dev %d blk %Ld: status 0x%02x\n",
1312 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1313 device, (ulong)blknr, c);
1318 input_data (device, buffer, ATA_SECTORWORDS);
1319 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1323 buffer += ATA_BLOCKSIZE;
1326 ide_led (DEVICE_LED(device), 0); /* LED off */
1330 /* ------------------------------------------------------------------------- */
1333 ulong ide_write (int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
1338 unsigned char lba48 = 0;
1340 if (blknr & 0x0000fffff0000000ULL) {
1341 /* more than 28 bits used, use 48bit mode */
1346 ide_led (DEVICE_LED(device), 1); /* LED on */
1350 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1352 while (blkcnt-- > 0) {
1354 c = ide_wait (device, IDE_TIME_OUT);
1356 if (c & ATA_STAT_BUSY) {
1357 printf ("IDE read: device %d not ready\n", device);
1362 /* write high bits */
1363 ide_outb (device, ATA_SECT_CNT, 0);
1364 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1365 #ifdef CONFIG_SYS_64BIT_LBA
1366 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1367 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1369 ide_outb (device, ATA_LBA_MID, 0);
1370 ide_outb (device, ATA_LBA_HIGH, 0);
1374 ide_outb (device, ATA_SECT_CNT, 1);
1375 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1376 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1377 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1381 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1382 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1387 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1388 ATA_DEVICE(device) |
1389 ((blknr >> 24) & 0xF) );
1390 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1395 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1397 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1398 #if defined(CONFIG_SYS_64BIT_LBA)
1399 printf ("Error (no IRQ) dev %d blk %Ld: status 0x%02x\n",
1402 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1403 device, (ulong)blknr, c);
1408 output_data (device, buffer, ATA_SECTORWORDS);
1409 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1412 buffer += ATA_BLOCKSIZE;
1415 ide_led (DEVICE_LED(device), 0); /* LED off */
1419 /* ------------------------------------------------------------------------- */
1422 * copy src to dest, skipping leading and trailing blanks and null
1423 * terminate the string
1424 * "len" is the size of available memory including the terminating '\0'
1426 static void ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
1428 unsigned char *end, *last;
1431 end = src + len - 1;
1433 /* reserve space for '\0' */
1437 /* skip leading white space */
1438 while ((*src) && (src<end) && (*src==' '))
1441 /* copy string, omitting trailing white space */
1442 while ((*src) && (src<end)) {
1451 /* ------------------------------------------------------------------------- */
1454 * Wait until Busy bit is off, or timeout (in ms)
1455 * Return last status
1457 static uchar ide_wait (int dev, ulong t)
1459 ulong delay = 10 * t; /* poll every 100 us */
1462 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1471 /* ------------------------------------------------------------------------- */
1473 #ifdef CONFIG_IDE_RESET
1474 extern void ide_set_reset(int idereset);
1476 static void ide_reset (void)
1478 #if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
1479 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
1484 for (i=0; i<CONFIG_SYS_IDE_MAXBUS; ++i)
1486 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i)
1487 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1489 ide_set_reset (1); /* assert reset */
1491 /* the reset signal shall be asserted for et least 25 us */
1496 #ifdef CONFIG_SYS_PB_12V_ENABLE
1497 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE); /* 12V Enable output OFF */
1498 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1499 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
1500 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
1502 /* wait 500 ms for the voltage to stabilize
1504 for (i=0; i<500; ++i) {
1508 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE; /* 12V Enable output ON */
1509 #endif /* CONFIG_SYS_PB_12V_ENABLE */
1511 #ifdef CONFIG_SYS_PB_IDE_MOTOR
1512 /* configure IDE Motor voltage monitor pin as input */
1513 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1514 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1515 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1517 /* wait up to 1 s for the motor voltage to stabilize
1519 for (i=0; i<1000; ++i) {
1520 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
1526 if (i == 1000) { /* Timeout */
1527 printf ("\nWarning: 5V for IDE Motor missing\n");
1528 # ifdef CONFIG_STATUS_LED
1529 # ifdef STATUS_LED_YELLOW
1530 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1532 # ifdef STATUS_LED_GREEN
1533 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1535 # endif /* CONFIG_STATUS_LED */
1537 #endif /* CONFIG_SYS_PB_IDE_MOTOR */
1541 /* de-assert RESET signal */
1545 for (i=0; i<250; ++i) {
1550 #endif /* CONFIG_IDE_RESET */
1552 /* ------------------------------------------------------------------------- */
1554 #if defined(CONFIG_IDE_LED) && \
1555 !defined(CONFIG_CPC45) && \
1556 !defined(CONFIG_KUP4K) && \
1557 !defined(CONFIG_KUP4X)
1559 static uchar led_buffer = 0; /* Buffer for current LED status */
1561 static void ide_led (uchar led, uchar status)
1563 uchar *led_port = LED_PORT;
1565 if (status) { /* switch LED on */
1567 } else { /* switch LED off */
1571 *led_port = led_buffer;
1574 #endif /* CONFIG_IDE_LED */
1576 #if defined(CONFIG_OF_IDE_FIXUP)
1577 int ide_device_present(int dev)
1579 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1581 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1584 /* ------------------------------------------------------------------------- */
1587 /****************************************************************************
1591 #if defined(CONFIG_IDE_SWAP_IO)
1592 /* since ATAPI may use commands with not 4 bytes alligned length
1593 * we have our own transfer functions, 2 bytes alligned */
1595 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1597 #if defined(CONFIG_CPC45)
1599 volatile uchar *pbuf_even;
1600 volatile uchar *pbuf_odd;
1602 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1603 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1606 *pbuf_even = *dbuf++;
1608 *pbuf_odd = *dbuf++;
1612 volatile ushort *pbuf;
1614 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1615 dbuf = (ushort *)sect_buf;
1617 debug ("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
1627 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1629 #if defined(CONFIG_CPC45)
1631 volatile uchar *pbuf_even;
1632 volatile uchar *pbuf_odd;
1634 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1635 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1638 *dbuf++ = *pbuf_even;
1640 *dbuf++ = *pbuf_odd;
1644 volatile ushort *pbuf;
1646 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1647 dbuf = (ushort *)sect_buf;
1649 debug("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
1658 #else /* ! CONFIG_IDE_SWAP_IO */
1660 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1662 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1666 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1668 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1671 #endif /* CONFIG_IDE_SWAP_IO */
1674 * Wait until (Status & mask) == res, or timeout (in ms)
1675 * Return last status
1676 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1677 * and then they set their DRQ Bit
1679 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1681 ulong delay = 10 * t; /* poll every 100 us */
1684 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1685 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1686 /* break if error occurs (doesn't make sense to wait more) */
1687 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1698 * issue an atapi command
1700 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1702 unsigned char c,err,mask,res;
1704 ide_led (DEVICE_LED(device), 1); /* LED on */
1708 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1710 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1711 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1712 if ((c & mask) != res) {
1713 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1717 /* write taskfile */
1718 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1719 ide_outb (device, ATA_SECT_CNT, 0);
1720 ide_outb (device, ATA_SECT_NUM, 0);
1721 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1722 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1723 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1725 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1728 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1730 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1732 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1733 printf ("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1738 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1739 /* ATAPI Command written wait for completition */
1740 udelay (5000); /* device must set bsy */
1742 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1743 /* if no data wait for DRQ = 0 BSY = 0
1744 * if data wait for DRQ = 1 BSY = 0 */
1748 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1749 if ((c & mask) != res ) {
1750 if (c & ATA_STAT_ERR) {
1751 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1752 debug ("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1754 printf ("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1759 n=ide_inb(device, ATA_CYL_HIGH);
1761 n+=ide_inb(device, ATA_CYL_LOW);
1763 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1767 if((n==0)&&(buflen<0)) {
1768 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1773 debug ("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1775 if(n!=0) { /* data transfer */
1776 debug ("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1777 /* we transfer shorts */
1779 /* ok now decide if it is an in or output */
1780 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1781 debug ("Write to device\n");
1782 output_data_shorts(device,(unsigned short *)buffer,n);
1784 debug ("Read from device @ %p shorts %d\n",buffer,n);
1785 input_data_shorts(device,(unsigned short *)buffer,n);
1788 udelay(5000); /* seems that some CD ROMs need this... */
1789 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1791 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1792 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1793 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1794 debug ("atapi_issue 2 returned sense key %X status %X\n",err,c);
1799 ide_led (DEVICE_LED(device), 0); /* LED off */
1804 * sending the command to atapi_issue. If an status other than good
1805 * returns, an request_sense will be issued
1808 #define ATAPI_DRIVE_NOT_READY 100
1809 #define ATAPI_UNIT_ATTN 10
1811 unsigned char atapi_issue_autoreq (int device,
1814 unsigned char *buffer,
1817 unsigned char sense_data[18],sense_ccb[12];
1818 unsigned char res,key,asc,ascq;
1819 int notready,unitattn;
1821 unitattn=ATAPI_UNIT_ATTN;
1822 notready=ATAPI_DRIVE_NOT_READY;
1825 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1827 return (0); /* Ok */
1830 return (0xFF); /* error */
1832 debug ("(auto_req)atapi_issue returned sense key %X\n",res);
1834 memset(sense_ccb,0,sizeof(sense_ccb));
1835 memset(sense_data,0,sizeof(sense_data));
1836 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1837 sense_ccb[4]=18; /* allocation Length */
1839 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1840 key=(sense_data[2]&0xF);
1841 asc=(sense_data[12]);
1842 ascq=(sense_data[13]);
1844 debug ("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1845 debug (" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1852 return 0; /* ok device ready */
1854 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1859 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1862 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1867 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1871 debug ("Media not present\n");
1875 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1877 debug ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1882 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1884 unsigned char ccb[12]; /* Command descriptor block */
1885 unsigned char iobuf[64]; /* temp buf */
1889 device=dev_desc->dev;
1890 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1891 dev_desc->block_read=atapi_read;
1893 memset(ccb,0,sizeof(ccb));
1894 memset(iobuf,0,sizeof(iobuf));
1896 ccb[0]=ATAPI_CMD_INQUIRY;
1897 ccb[4]=40; /* allocation Legnth */
1898 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1900 debug ("ATAPI_CMD_INQUIRY returned %x\n",c);
1904 /* copy device ident strings */
1905 ident_cpy((unsigned char*)dev_desc->vendor,&iobuf[8],8);
1906 ident_cpy((unsigned char*)dev_desc->product,&iobuf[16],16);
1907 ident_cpy((unsigned char*)dev_desc->revision,&iobuf[32],5);
1912 dev_desc->type=iobuf[0] & 0x1f;
1914 if ((iobuf[1]&0x80)==0x80)
1915 dev_desc->removable = 1;
1917 dev_desc->removable = 0;
1919 memset(ccb,0,sizeof(ccb));
1920 memset(iobuf,0,sizeof(iobuf));
1921 ccb[0]=ATAPI_CMD_START_STOP;
1922 ccb[4]=0x03; /* start */
1924 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1926 debug ("ATAPI_CMD_START_STOP returned %x\n",c);
1930 memset(ccb,0,sizeof(ccb));
1931 memset(iobuf,0,sizeof(iobuf));
1932 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1934 debug ("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1938 memset(ccb,0,sizeof(ccb));
1939 memset(iobuf,0,sizeof(iobuf));
1940 ccb[0]=ATAPI_CMD_READ_CAP;
1941 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1942 debug ("ATAPI_CMD_READ_CAP returned %x\n",c);
1946 debug ("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1947 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1948 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1950 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1951 ((unsigned long)iobuf[1]<<16) +
1952 ((unsigned long)iobuf[2]<< 8) +
1953 ((unsigned long)iobuf[3]);
1954 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1955 ((unsigned long)iobuf[5]<<16) +
1956 ((unsigned long)iobuf[6]<< 8) +
1957 ((unsigned long)iobuf[7]);
1959 dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1967 * we transfer only one block per command, since the multiple DRQ per
1968 * command is not yet implemented
1970 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1971 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1972 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1974 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1977 unsigned char ccb[12]; /* Command descriptor block */
1980 debug ("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1981 device, blknr, blkcnt, (ulong)buffer);
1984 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1985 cnt=ATAPI_READ_MAX_BLOCK;
1989 ccb[0]=ATAPI_CMD_READ_12;
1990 ccb[1]=0; /* reserved */
1991 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1992 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1993 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1994 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1995 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1996 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1997 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1998 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1999 ccb[10]=0; /* reserved */
2000 ccb[11]=0; /* reserved */
2002 if (atapi_issue_autoreq(device,ccb,12,
2003 (unsigned char *)buffer,
2004 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
2010 buffer+=(cnt*ATAPI_READ_BLOCK_SIZE);
2011 } while (blkcnt > 0);
2015 /* ------------------------------------------------------------------------- */
2017 #endif /* CONFIG_ATAPI */
2022 "reset - reset IDE controller\n"
2023 "ide info - show available IDE devices\n"
2024 "ide device [dev] - show or set current device\n"
2025 "ide part [dev] - print partition table of one or all IDE devices\n"
2026 "ide read addr blk# cnt\n"
2027 "ide write addr blk# cnt - read/write `cnt'"
2028 " blocks starting at block `blk#'\n"
2029 " to/from memory address `addr'"
2033 diskboot, 3, 1, do_diskboot,
2034 "boot from IDE device",