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>
35 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
50 #ifdef CONFIG_STATUS_LED
51 # include <status_led.h>
57 /* Macros depend on this variable */
58 unsigned long mips_io_port_base = 0;
62 #ifdef CONFIG_IDE_8xx_DIRECT
63 DECLARE_GLOBAL_DATA_PTR;
67 # define EIEIO __asm__ volatile ("eieio")
68 # define SYNC __asm__ volatile ("sync")
70 # define EIEIO /* nothing */
71 # define SYNC /* nothing */
74 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
76 #ifdef CONFIG_IDE_8xx_DIRECT
77 /* Timings for IDE Interface
79 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
80 * 70 165 30 PIO-Mode 0, [ns]
82 * 50 125 20 PIO-Mode 1, [ns]
84 * 30 100 15 PIO-Mode 2, [ns]
86 * 30 80 10 PIO-Mode 3, [ns]
88 * 25 70 10 PIO-Mode 4, [ns]
92 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
94 /* Setup Length Hold */
95 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
96 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
97 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
98 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
99 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
102 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
105 #define CFG_PIO_MODE 0 /* use a relaxed default */
107 static int pio_mode = CFG_PIO_MODE;
109 /* Make clock cycles and always round up */
111 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
113 #endif /* CONFIG_IDE_8xx_DIRECT */
115 /* ------------------------------------------------------------------------- */
117 /* Current I/O Device */
118 static int curr_device = -1;
120 /* Current offset for IDE0 / IDE1 bus access */
121 ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
122 #if defined(CFG_ATA_IDE0_OFFSET)
125 #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
131 #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
133 #ifndef CONFIG_AMIGAONEG3SE
134 static int ide_bus_ok[CFG_IDE_MAXBUS];
136 static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
139 block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
140 /* ------------------------------------------------------------------------- */
142 #ifdef CONFIG_IDE_LED
143 #if !defined(CONFIG_KUP4K) && !defined(CONFIG_KUP4X) &&!defined(CONFIG_BMS2003) &&!defined(CONFIG_CPC45)
144 static void ide_led (uchar led, uchar status);
146 extern void ide_led (uchar led, uchar status);
149 #ifndef CONFIG_AMIGAONEG3SE
150 #define ide_led(a,b) /* dummy */
152 extern void ide_led(uchar led, uchar status);
155 #define CONFIG_IDE_LED 1
156 #define DEVICE_LED(x) 1
160 #ifdef CONFIG_IDE_RESET
161 static void ide_reset (void);
163 #define ide_reset() /* dummy */
166 static void ide_ident (block_dev_desc_t *dev_desc);
167 static uchar ide_wait (int dev, ulong t);
169 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
171 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
173 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
175 static void __inline__ ide_outb(int dev, int port, unsigned char val);
176 static unsigned char __inline__ ide_inb(int dev, int port);
177 static void input_data(int dev, ulong *sect_buf, int words);
178 static void output_data(int dev, ulong *sect_buf, int words);
179 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
181 #ifndef CFG_ATA_PORT_ADDR
182 #define CFG_ATA_PORT_ADDR(port) (port)
186 static void atapi_inquiry(block_dev_desc_t *dev_desc);
187 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
191 #ifdef CONFIG_IDE_8xx_DIRECT
192 static void set_pcmcia_timing (int pmode);
195 /* ------------------------------------------------------------------------- */
197 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
204 printf ("Usage:\n%s\n", cmdtp->usage);
207 if (strncmp(argv[1],"res",3) == 0) {
209 #ifdef CONFIG_IDE_8xx_DIRECT
210 " on PCMCIA " PCMCIA_SLOT_MSG
216 } else if (strncmp(argv[1],"inf",3) == 0) {
221 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
222 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
223 continue; /* list only known devices */
224 printf ("IDE device %d: ", i);
225 dev_print(&ide_dev_desc[i]);
229 } else if (strncmp(argv[1],"dev",3) == 0) {
230 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
231 puts ("\nno IDE devices available\n");
234 printf ("\nIDE device %d: ", curr_device);
235 dev_print(&ide_dev_desc[curr_device]);
237 } else if (strncmp(argv[1],"part",4) == 0) {
240 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
241 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
245 print_part(&ide_dev_desc[dev]);
249 puts ("\nno IDE devices available\n");
254 printf ("Usage:\n%s\n", cmdtp->usage);
257 if (strncmp(argv[1],"dev",3) == 0) {
258 int dev = (int)simple_strtoul(argv[2], NULL, 10);
260 printf ("\nIDE device %d: ", dev);
261 if (dev >= CFG_IDE_MAXDEVICE) {
262 puts ("unknown device\n");
265 dev_print(&ide_dev_desc[dev]);
268 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
274 puts ("... is now current device\n");
277 } else if (strncmp(argv[1],"part",4) == 0) {
278 int dev = (int)simple_strtoul(argv[2], NULL, 10);
280 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
281 print_part(&ide_dev_desc[dev]);
283 printf ("\nIDE device %d not available\n", dev);
288 } else if (strncmp(argv[1],"pio",4) == 0) {
289 int mode = (int)simple_strtoul(argv[2], NULL, 10);
291 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
296 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
297 mode, IDE_MAX_PIO_MODE);
303 printf ("Usage:\n%s\n", cmdtp->usage);
306 /* at least 4 args */
308 if (strcmp(argv[1],"read") == 0) {
309 ulong addr = simple_strtoul(argv[2], NULL, 16);
310 ulong cnt = simple_strtoul(argv[4], NULL, 16);
312 #ifdef CFG_64BIT_STRTOUL
313 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
315 printf ("\nIDE read: device %d block # %qd, count %ld ... ",
316 curr_device, blk, cnt);
318 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
320 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
321 curr_device, blk, cnt);
324 n = ide_dev_desc[curr_device].block_read (curr_device,
327 /* flush cache after read */
328 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
330 printf ("%ld blocks read: %s\n",
331 n, (n==cnt) ? "OK" : "ERROR");
337 } else if (strcmp(argv[1],"write") == 0) {
338 ulong addr = simple_strtoul(argv[2], NULL, 16);
339 ulong cnt = simple_strtoul(argv[4], NULL, 16);
341 #ifdef CFG_64BIT_STRTOUL
342 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
344 printf ("\nIDE write: device %d block # %qd, count %ld ... ",
345 curr_device, blk, cnt);
347 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
349 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
350 curr_device, blk, cnt);
353 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
355 printf ("%ld blocks written: %s\n",
356 n, (n==cnt) ? "OK" : "ERROR");
363 printf ("Usage:\n%s\n", cmdtp->usage);
371 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
373 char *boot_device = NULL;
376 ulong addr, cnt, checksum;
377 disk_partition_t info;
381 show_boot_progress (41);
384 addr = CFG_LOAD_ADDR;
385 boot_device = getenv ("bootdevice");
388 addr = simple_strtoul(argv[1], NULL, 16);
389 boot_device = getenv ("bootdevice");
392 addr = simple_strtoul(argv[1], NULL, 16);
393 boot_device = argv[2];
396 printf ("Usage:\n%s\n", cmdtp->usage);
397 show_boot_progress (-42);
400 show_boot_progress (42);
403 puts ("\n** No boot device **\n");
404 show_boot_progress (-43);
407 show_boot_progress (43);
409 dev = simple_strtoul(boot_device, &ep, 16);
411 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
412 printf ("\n** Device %d not available\n", dev);
413 show_boot_progress (-44);
416 show_boot_progress (44);
420 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
421 show_boot_progress (-45);
424 part = simple_strtoul(++ep, NULL, 16);
426 show_boot_progress (45);
427 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
428 show_boot_progress (-46);
431 show_boot_progress (46);
432 if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
433 (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
434 printf ("\n** Invalid partition type \"%.32s\""
435 " (expect \"" BOOT_PART_TYPE "\")\n",
437 show_boot_progress (-47);
440 show_boot_progress (47);
442 printf ("\nLoading from IDE device %d, partition %d: "
443 "Name: %.32s Type: %.32s\n",
444 dev, part, info.name, info.type);
446 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
447 info.start, info.size, info.blksz);
449 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
450 printf ("** Read error on %d:%d\n", dev, part);
451 show_boot_progress (-48);
454 show_boot_progress (48);
456 hdr = (image_header_t *)addr;
458 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
459 printf("\n** Bad Magic Number **\n");
460 show_boot_progress (-49);
463 show_boot_progress (49);
465 checksum = ntohl(hdr->ih_hcrc);
468 if (crc32 (0, (uchar *)hdr, sizeof(image_header_t)) != checksum) {
469 puts ("\n** Bad Header Checksum **\n");
470 show_boot_progress (-50);
473 show_boot_progress (50);
474 hdr->ih_hcrc = htonl(checksum); /* restore checksum for later use */
476 print_image_hdr (hdr);
478 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
479 cnt += info.blksz - 1;
483 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
484 (ulong *)(addr+info.blksz)) != cnt) {
485 printf ("** Read error on %d:%d\n", dev, part);
486 show_boot_progress (-51);
489 show_boot_progress (51);
492 /* Loading ok, update default load address */
496 /* Check if we should attempt an auto-start */
497 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
499 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
501 local_args[0] = argv[0];
502 local_args[1] = NULL;
504 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
506 do_bootm (cmdtp, 0, 1, local_args);
512 /* ------------------------------------------------------------------------- */
517 #ifdef CONFIG_IDE_8xx_DIRECT
518 volatile immap_t *immr = (immap_t *)CFG_IMMR;
519 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
523 #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
524 unsigned int ata_reset_time = ATA_RESET_TIME;
527 #ifdef CONFIG_AMIGAONEG3SE
528 unsigned int max_bus_scan;
530 #ifdef CONFIG_IDE_8xx_PCCARD
531 extern int pcmcia_on (void);
532 extern int ide_devices_found; /* Initialized in check_ide_device() */
533 #endif /* CONFIG_IDE_8xx_PCCARD */
535 #ifdef CONFIG_IDE_PREINIT
536 extern int ide_preinit (void);
539 if (ide_preinit ()) {
540 puts ("ide_preinit failed\n");
543 #endif /* CONFIG_IDE_PREINIT */
545 #ifdef CONFIG_IDE_8xx_PCCARD
546 extern int pcmcia_on (void);
547 extern int ide_devices_found; /* Initialized in check_ide_device() */
551 ide_devices_found = 0;
552 /* initialize the PCMCIA IDE adapter card */
554 if (!ide_devices_found)
556 udelay (1000000); /* 1 s */
557 #endif /* CONFIG_IDE_8xx_PCCARD */
561 #ifdef CONFIG_IDE_8xx_DIRECT
562 /* Initialize PIO timing tables */
563 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
564 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
566 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
568 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
570 debug ( "PIO Mode %d: setup=%2d ns/%d clk"
572 " hold=%2d ns/%d clk\n",
574 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
575 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
576 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
578 #endif /* CONFIG_IDE_8xx_DIRECT */
580 /* Reset the IDE just to be sure.
581 * Light LED's to show
583 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
584 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
586 #ifdef CONFIG_IDE_8xx_DIRECT
587 /* PCMCIA / IDE initialization for common mem space */
588 pcmp->pcmc_pgcrb = 0;
590 /* start in PIO mode 0 - most relaxed timings */
592 set_pcmcia_timing (pio_mode);
593 #endif /* CONFIG_IDE_8xx_DIRECT */
596 * Wait for IDE to get ready.
597 * According to spec, this can take up to 31 seconds!
599 #ifndef CONFIG_AMIGAONEG3SE
600 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
601 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
603 s = getenv("ide_maxbus");
605 max_bus_scan = simple_strtol(s, NULL, 10);
607 max_bus_scan = CFG_IDE_MAXBUS;
609 for (bus=0; bus<max_bus_scan; ++bus) {
610 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
613 #ifdef CONFIG_IDE_8xx_PCCARD
614 /* Skip non-ide devices from probing */
615 if ((ide_devices_found & (1 << bus)) == 0) {
616 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
620 printf ("Bus %d: ", bus);
626 udelay (100000); /* 100 ms */
627 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
628 udelay (100000); /* 100 ms */
629 #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
630 if ((s = getenv("ide_reset_timeout")) != NULL)
631 ata_reset_time = simple_strtol(s, NULL, 10);
635 udelay (10000); /* 10 ms */
637 c = ide_inb (dev, ATA_STATUS);
639 #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
640 if (i > (ata_reset_time * 100)) {
642 if (i > (ATA_RESET_TIME * 100)) {
644 puts ("** Timeout **\n");
645 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
646 #ifdef CONFIG_AMIGAONEG3SE
647 /* If this is the second bus, the first one was OK */
655 if ((i >= 100) && ((i%100)==0)) {
658 } while (c & ATA_STAT_BUSY);
660 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
661 puts ("not available ");
662 debug ("Status = 0x%02X ", c);
663 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
664 } else if ((c & ATA_STAT_READY) == 0) {
665 puts ("not available ");
666 debug ("Status = 0x%02X ", c);
675 #ifdef CONFIG_AMIGAONEG3SE
680 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
683 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
684 #ifdef CONFIG_IDE_LED
685 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
687 ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
688 ide_dev_desc[i].if_type=IF_TYPE_IDE;
689 ide_dev_desc[i].dev=i;
690 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
691 ide_dev_desc[i].blksz=0;
692 ide_dev_desc[i].lba=0;
693 ide_dev_desc[i].block_read=ide_read;
694 if (!ide_bus_ok[IDE_BUS(i)])
696 ide_led (led, 1); /* LED on */
697 ide_ident(&ide_dev_desc[i]);
698 ide_led (led, 0); /* LED off */
699 dev_print(&ide_dev_desc[i]);
701 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
702 init_part (&ide_dev_desc[i]); /* initialize partition type */
710 /* ------------------------------------------------------------------------- */
712 block_dev_desc_t * ide_get_dev(int dev)
714 return (dev < CFG_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
718 #ifdef CONFIG_IDE_8xx_DIRECT
721 set_pcmcia_timing (int pmode)
723 volatile immap_t *immr = (immap_t *)CFG_IMMR;
724 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
727 debug ("Set timing for PIO Mode %d\n", pmode);
729 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
730 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
731 | PCMCIA_SL (pio_config_clk[pmode].t_length)
736 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
737 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
738 #if (CFG_PCMCIA_POR0 != 0)
742 debug ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
744 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
745 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
746 #if (CFG_PCMCIA_POR1 != 0)
750 debug ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
752 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
753 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
754 #if (CFG_PCMCIA_POR2 != 0)
758 debug ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
760 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
761 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
762 #if (CFG_PCMCIA_POR3 != 0)
766 debug ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
770 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
771 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
772 #if (CFG_PCMCIA_POR4 != 0)
776 debug ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
778 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
779 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
780 #if (CFG_PCMCIA_POR5 != 0)
784 debug ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
786 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
787 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
788 #if (CFG_PCMCIA_POR6 != 0)
792 debug ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
794 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
795 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
796 #if (CFG_PCMCIA_POR7 != 0)
800 debug ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
804 #endif /* CONFIG_IDE_8xx_DIRECT */
806 /* ------------------------------------------------------------------------- */
808 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
809 static void __inline__
810 ide_outb(int dev, int port, unsigned char val)
812 debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
813 dev, port, val, (ATA_CURR_BASE(dev)+port));
815 /* Ensure I/O operations complete */
817 *((u16 *)(ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port))) = val;
819 #else /* ! __PPC__ */
820 static void __inline__
821 ide_outb(int dev, int port, unsigned char val)
823 outb(val, ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port));
828 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
829 static unsigned char __inline__
830 ide_inb(int dev, int port)
833 /* Ensure I/O operations complete */
835 val = *((u16 *)(ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)));
836 debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
837 dev, port, (ATA_CURR_BASE(dev)+port), val);
840 #else /* ! __PPC__ */
841 static unsigned char __inline__
842 ide_inb(int dev, int port)
844 return inb(ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port));
849 # ifdef CONFIG_AMIGAONEG3SE
851 output_data_short(int dev, ulong *sect_buf, int words)
854 volatile ushort *pbuf;
856 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
857 dbuf = (ushort *)sect_buf;
867 # endif /* CONFIG_AMIGAONEG3SE */
870 /* We only need to swap data if we are running on a big endian cpu. */
871 /* But Au1x00 cpu:s already swaps data in big endian mode! */
872 #if defined(__LITTLE_ENDIAN) || ( defined(CONFIG_AU1X00) && !defined(CONFIG_GTH2) )
873 #define input_swap_data(x,y,z) input_data(x,y,z)
876 input_swap_data(int dev, ulong *sect_buf, int words)
878 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
880 volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
881 volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
882 ushort *dbuf = (ushort *)sect_buf;
885 for (i=0; i<2; i++) {
886 *(((uchar *)(dbuf)) + 1) = *pbuf_even;
887 *(uchar *)dbuf = *pbuf_odd;
892 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
893 ushort *dbuf = (ushort *)sect_buf;
895 debug("in input swap data base for read is %lx\n", (unsigned long) pbuf);
899 *dbuf++ = swab16p((u16*)pbuf);
900 *dbuf++ = swab16p((u16*)pbuf);
901 #elif defined(CONFIG_PCS440EP)
905 *dbuf++ = ld_le16(pbuf);
906 *dbuf++ = ld_le16(pbuf);
911 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
914 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
916 output_data(int dev, ulong *sect_buf, int words)
918 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
920 volatile uchar *pbuf_even;
921 volatile uchar *pbuf_odd;
923 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
924 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
925 dbuf = (uchar *)sect_buf;
928 *pbuf_even = *dbuf++;
932 *pbuf_even = *dbuf++;
938 volatile ushort *pbuf;
940 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
941 dbuf = (ushort *)sect_buf;
943 #if defined(CONFIG_PCS440EP)
944 /* not tested, because CF was write protected */
946 *pbuf = ld_le16(dbuf++);
948 *pbuf = ld_le16(dbuf++);
958 #else /* ! __PPC__ */
960 output_data(int dev, ulong *sect_buf, int words)
962 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
966 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
968 input_data(int dev, ulong *sect_buf, int words)
970 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
972 volatile uchar *pbuf_even;
973 volatile uchar *pbuf_odd;
975 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
976 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
977 dbuf = (uchar *)sect_buf;
979 *dbuf++ = *pbuf_even;
985 *dbuf++ = *pbuf_even;
994 volatile ushort *pbuf;
996 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
997 dbuf = (ushort *)sect_buf;
999 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
1002 #if defined(CONFIG_PCS440EP)
1004 *dbuf++ = ld_le16(pbuf);
1006 *dbuf++ = ld_le16(pbuf);
1016 #else /* ! __PPC__ */
1018 input_data(int dev, ulong *sect_buf, int words)
1020 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
1023 #endif /* __PPC__ */
1025 #ifdef CONFIG_AMIGAONEG3SE
1027 input_data_short(int dev, ulong *sect_buf, int words)
1030 volatile ushort *pbuf;
1032 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1033 dbuf = (ushort *)sect_buf;
1047 /* -------------------------------------------------------------------------
1049 static void ide_ident (block_dev_desc_t *dev_desc)
1051 ulong iobuf[ATA_SECTORWORDS];
1053 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
1055 #ifdef CONFIG_AMIGAONEG3SE
1065 int mode, cycle_time;
1068 device=dev_desc->dev;
1069 printf (" Device %d: ", device);
1071 #ifdef CONFIG_AMIGAONEG3SE
1072 s = getenv("ide_maxbus");
1074 max_bus_scan = simple_strtol(s, NULL, 10);
1076 max_bus_scan = CFG_IDE_MAXBUS;
1078 if (device >= max_bus_scan*2) {
1079 dev_desc->type=DEV_TYPE_UNKNOWN;
1084 ide_led (DEVICE_LED(device), 1); /* LED on */
1087 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1088 dev_desc->if_type=IF_TYPE_IDE;
1094 /* Warning: This will be tricky to read */
1095 while (retries <= 1) {
1096 /* check signature */
1097 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
1098 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
1099 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
1100 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
1101 /* ATAPI Signature found */
1102 dev_desc->if_type=IF_TYPE_ATAPI;
1103 /* Start Ident Command
1105 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
1107 * Wait for completion - ATAPI devices need more time
1110 c = ide_wait (device, ATAPI_TIME_OUT);
1114 /* Start Ident Command
1116 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
1118 /* Wait for completion
1120 c = ide_wait (device, IDE_TIME_OUT);
1122 ide_led (DEVICE_LED(device), 0); /* LED off */
1124 if (((c & ATA_STAT_DRQ) == 0) ||
1125 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
1127 #ifdef CONFIG_AMIGAONEG3SE
1128 s = getenv("ide_doreset");
1129 if (s && strcmp(s, "on") == 0)
1132 /* Need to soft reset the device in case it's an ATAPI... */
1133 debug ("Retrying...\n");
1134 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1136 ide_outb (device, ATA_COMMAND, 0x08);
1137 udelay (500000); /* 500 ms */
1141 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1150 } /* see above - ugly to read */
1152 if (retries == 2) /* Not found */
1156 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1158 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1159 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1160 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1161 #ifdef __LITTLE_ENDIAN
1163 * firmware revision and model number have Big Endian Byte
1164 * order in Word. Convert both to little endian.
1166 * See CF+ and CompactFlash Specification Revision 2.0:
1167 * 6.2.1.6: Identfy Drive, Table 39 for more details
1170 strswab (dev_desc->revision);
1171 strswab (dev_desc->vendor);
1172 #endif /* __LITTLE_ENDIAN */
1174 if ((iop->config & 0x0080)==0x0080)
1175 dev_desc->removable = 1;
1177 dev_desc->removable = 0;
1181 * Drive PIO mode autoselection
1185 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1186 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1188 debug ("Override tPIO -> 2\n");
1190 if (iop->field_valid & 2) { /* drive implements ATA2? */
1191 debug ("Drive implements ATA2\n");
1192 if (iop->capability & 8) { /* drive supports use_iordy? */
1193 cycle_time = iop->eide_pio_iordy;
1195 cycle_time = iop->eide_pio;
1197 debug ("cycle time = %d\n", cycle_time);
1199 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1200 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1201 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1202 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1204 printf ("PIO mode to use: PIO %d\n", mode);
1208 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1209 atapi_inquiry(dev_desc);
1212 #endif /* CONFIG_ATAPI */
1216 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1217 #else /* ! __BIG_ENDIAN */
1219 * do not swap shorts on little endian
1221 * See CF+ and CompactFlash Specification Revision 2.0:
1222 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1224 dev_desc->lba = iop->lba_capacity;
1225 #endif /* __BIG_ENDIAN */
1228 if (iop->command_set_2 & 0x0400) { /* LBA 48 support */
1229 dev_desc->lba48 = 1;
1230 dev_desc->lba = (unsigned long long)iop->lba48_capacity[0] |
1231 ((unsigned long long)iop->lba48_capacity[1] << 16) |
1232 ((unsigned long long)iop->lba48_capacity[2] << 32) |
1233 ((unsigned long long)iop->lba48_capacity[3] << 48);
1235 dev_desc->lba48 = 0;
1237 #endif /* CONFIG_LBA48 */
1239 dev_desc->type=DEV_TYPE_HARDDISK;
1240 dev_desc->blksz=ATA_BLOCKSIZE;
1241 dev_desc->lun=0; /* just to fill something in... */
1243 #if 0 /* only used to test the powersaving mode,
1244 * if enabled, the drive goes after 5 sec
1245 * in standby mode */
1246 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1247 c = ide_wait (device, IDE_TIME_OUT);
1248 ide_outb (device, ATA_SECT_CNT, 1);
1249 ide_outb (device, ATA_LBA_LOW, 0);
1250 ide_outb (device, ATA_LBA_MID, 0);
1251 ide_outb (device, ATA_LBA_HIGH, 0);
1252 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1253 ide_outb (device, ATA_COMMAND, 0xe3);
1255 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1260 /* ------------------------------------------------------------------------- */
1262 ulong ide_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1266 unsigned char pwrsave=0; /* power save */
1268 unsigned char lba48 = 0;
1270 if (blknr & 0x0000fffff0000000) {
1271 /* more than 28 bits used, use 48bit mode */
1275 debug ("ide_read dev %d start %qX, blocks %lX buffer at %lX\n",
1276 device, blknr, blkcnt, (ulong)buffer);
1278 ide_led (DEVICE_LED(device), 1); /* LED on */
1282 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1283 c = ide_wait (device, IDE_TIME_OUT);
1285 if (c & ATA_STAT_BUSY) {
1286 printf ("IDE read: device %d not ready\n", device);
1290 /* first check if the drive is in Powersaving mode, if yes,
1291 * increase the timeout value */
1292 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1295 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1297 if (c & ATA_STAT_BUSY) {
1298 printf ("IDE read: device %d not ready\n", device);
1301 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1302 printf ("No Powersaving mode %X\n", c);
1304 c = ide_inb(device,ATA_SECT_CNT);
1305 debug ("Powersaving %02X\n",c);
1311 while (blkcnt-- > 0) {
1313 c = ide_wait (device, IDE_TIME_OUT);
1315 if (c & ATA_STAT_BUSY) {
1316 printf ("IDE read: device %d not ready\n", device);
1321 /* write high bits */
1322 ide_outb (device, ATA_SECT_CNT, 0);
1323 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1324 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1325 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1328 ide_outb (device, ATA_SECT_CNT, 1);
1329 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1330 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1331 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1335 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1336 ide_outb (device, ATA_COMMAND, ATA_CMD_READ_EXT);
1341 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1342 ATA_DEVICE(device) |
1343 ((blknr >> 24) & 0xF) );
1344 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1350 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1353 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1356 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1357 #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
1358 printf ("Error (no IRQ) dev %d blk %qd: status 0x%02x\n",
1361 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1362 device, (ulong)blknr, c);
1367 input_data (device, buffer, ATA_SECTORWORDS);
1368 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1372 buffer += ATA_BLOCKSIZE;
1375 ide_led (DEVICE_LED(device), 0); /* LED off */
1379 /* ------------------------------------------------------------------------- */
1382 ulong ide_write (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1387 unsigned char lba48 = 0;
1389 if (blknr & 0x0000fffff0000000) {
1390 /* more than 28 bits used, use 48bit mode */
1395 ide_led (DEVICE_LED(device), 1); /* LED on */
1399 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1401 while (blkcnt-- > 0) {
1403 c = ide_wait (device, IDE_TIME_OUT);
1405 if (c & ATA_STAT_BUSY) {
1406 printf ("IDE read: device %d not ready\n", device);
1411 /* write high bits */
1412 ide_outb (device, ATA_SECT_CNT, 0);
1413 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1414 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1415 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1418 ide_outb (device, ATA_SECT_CNT, 1);
1419 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1420 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1421 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1425 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1426 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1431 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1432 ATA_DEVICE(device) |
1433 ((blknr >> 24) & 0xF) );
1434 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1439 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1441 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1442 #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
1443 printf ("Error (no IRQ) dev %d blk %qd: status 0x%02x\n",
1446 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1447 device, (ulong)blknr, c);
1452 output_data (device, buffer, ATA_SECTORWORDS);
1453 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1456 buffer += ATA_BLOCKSIZE;
1459 ide_led (DEVICE_LED(device), 0); /* LED off */
1463 /* ------------------------------------------------------------------------- */
1466 * copy src to dest, skipping leading and trailing blanks and null
1467 * terminate the string
1468 * "len" is the size of available memory including the terminating '\0'
1470 static void ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
1472 unsigned char *end, *last;
1475 end = src + len - 1;
1477 /* reserve space for '\0' */
1481 /* skip leading white space */
1482 while ((*src) && (src<end) && (*src==' '))
1485 /* copy string, omitting trailing white space */
1486 while ((*src) && (src<end)) {
1495 /* ------------------------------------------------------------------------- */
1498 * Wait until Busy bit is off, or timeout (in ms)
1499 * Return last status
1501 static uchar ide_wait (int dev, ulong t)
1503 ulong delay = 10 * t; /* poll every 100 us */
1506 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1515 /* ------------------------------------------------------------------------- */
1517 #ifdef CONFIG_IDE_RESET
1518 extern void ide_set_reset(int idereset);
1520 static void ide_reset (void)
1522 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1523 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1528 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1530 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1531 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1533 ide_set_reset (1); /* assert reset */
1537 #ifdef CFG_PB_12V_ENABLE
1538 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1539 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1540 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1541 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1543 /* wait 500 ms for the voltage to stabilize
1545 for (i=0; i<500; ++i) {
1549 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1550 #endif /* CFG_PB_12V_ENABLE */
1552 #ifdef CFG_PB_IDE_MOTOR
1553 /* configure IDE Motor voltage monitor pin as input */
1554 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1555 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1556 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1558 /* wait up to 1 s for the motor voltage to stabilize
1560 for (i=0; i<1000; ++i) {
1561 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1567 if (i == 1000) { /* Timeout */
1568 printf ("\nWarning: 5V for IDE Motor missing\n");
1569 # ifdef CONFIG_STATUS_LED
1570 # ifdef STATUS_LED_YELLOW
1571 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1573 # ifdef STATUS_LED_GREEN
1574 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1576 # endif /* CONFIG_STATUS_LED */
1578 #endif /* CFG_PB_IDE_MOTOR */
1582 /* de-assert RESET signal */
1586 for (i=0; i<250; ++i) {
1591 #endif /* CONFIG_IDE_RESET */
1593 /* ------------------------------------------------------------------------- */
1595 #if defined(CONFIG_IDE_LED) && \
1596 !defined(CONFIG_AMIGAONEG3SE)&& \
1597 !defined(CONFIG_CPC45) && \
1598 !defined(CONFIG_HMI10) && \
1599 !defined(CONFIG_KUP4K) && \
1600 !defined(CONFIG_KUP4X)
1602 static uchar led_buffer = 0; /* Buffer for current LED status */
1604 static void ide_led (uchar led, uchar status)
1606 uchar *led_port = LED_PORT;
1608 if (status) { /* switch LED on */
1610 } else { /* switch LED off */
1614 *led_port = led_buffer;
1617 #endif /* CONFIG_IDE_LED */
1619 /* ------------------------------------------------------------------------- */
1622 /****************************************************************************
1626 #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
1627 /* since ATAPI may use commands with not 4 bytes alligned length
1628 * we have our own transfer functions, 2 bytes alligned */
1630 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1632 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
1634 volatile uchar *pbuf_even;
1635 volatile uchar *pbuf_odd;
1637 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1638 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1641 *pbuf_even = *dbuf++;
1643 *pbuf_odd = *dbuf++;
1647 volatile ushort *pbuf;
1649 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1650 dbuf = (ushort *)sect_buf;
1652 debug ("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
1662 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1664 #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
1666 volatile uchar *pbuf_even;
1667 volatile uchar *pbuf_odd;
1669 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1670 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1673 *dbuf++ = *pbuf_even;
1675 *dbuf++ = *pbuf_odd;
1679 volatile ushort *pbuf;
1681 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1682 dbuf = (ushort *)sect_buf;
1684 debug("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
1693 #else /* ! __PPC__ */
1695 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1697 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1701 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1703 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1706 #endif /* __PPC__ */
1709 * Wait until (Status & mask) == res, or timeout (in ms)
1710 * Return last status
1711 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1712 * and then they set their DRQ Bit
1714 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1716 ulong delay = 10 * t; /* poll every 100 us */
1719 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1720 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1721 /* break if error occurs (doesn't make sense to wait more) */
1722 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1733 * issue an atapi command
1735 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1737 unsigned char c,err,mask,res;
1739 ide_led (DEVICE_LED(device), 1); /* LED on */
1743 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1745 #ifdef CONFIG_AMIGAONEG3SE
1746 # warning THF: Removed LBA mode ???
1748 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1749 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1750 if ((c & mask) != res) {
1751 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1755 /* write taskfile */
1756 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1757 ide_outb (device, ATA_SECT_CNT, 0);
1758 ide_outb (device, ATA_SECT_NUM, 0);
1759 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1760 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1761 #ifdef CONFIG_AMIGAONEG3SE
1762 # warning THF: Removed LBA mode ???
1764 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1766 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1769 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1771 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1773 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1774 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1779 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1780 /* ATAPI Command written wait for completition */
1781 udelay (5000); /* device must set bsy */
1783 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1784 /* if no data wait for DRQ = 0 BSY = 0
1785 * if data wait for DRQ = 1 BSY = 0 */
1789 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1790 if ((c & mask) != res ) {
1791 if (c & ATA_STAT_ERR) {
1792 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1793 debug ("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1795 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1800 n=ide_inb(device, ATA_CYL_HIGH);
1802 n+=ide_inb(device, ATA_CYL_LOW);
1804 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1808 if((n==0)&&(buflen<0)) {
1809 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1814 debug ("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1816 if(n!=0) { /* data transfer */
1817 debug ("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1818 /* we transfer shorts */
1820 /* ok now decide if it is an in or output */
1821 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1822 debug ("Write to device\n");
1823 output_data_shorts(device,(unsigned short *)buffer,n);
1825 debug ("Read from device @ %p shorts %d\n",buffer,n);
1826 input_data_shorts(device,(unsigned short *)buffer,n);
1829 udelay(5000); /* seems that some CD ROMs need this... */
1830 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1832 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1833 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1834 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1835 debug ("atapi_issue 2 returned sense key %X status %X\n",err,c);
1840 ide_led (DEVICE_LED(device), 0); /* LED off */
1845 * sending the command to atapi_issue. If an status other than good
1846 * returns, an request_sense will be issued
1849 #define ATAPI_DRIVE_NOT_READY 100
1850 #define ATAPI_UNIT_ATTN 10
1852 unsigned char atapi_issue_autoreq (int device,
1855 unsigned char *buffer,
1858 unsigned char sense_data[18],sense_ccb[12];
1859 unsigned char res,key,asc,ascq;
1860 int notready,unitattn;
1862 #ifdef CONFIG_AMIGAONEG3SE
1864 unsigned int timeout, retrycnt;
1866 s = getenv("ide_cd_timeout");
1867 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1872 unitattn=ATAPI_UNIT_ATTN;
1873 notready=ATAPI_DRIVE_NOT_READY;
1876 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1878 return (0); /* Ok */
1881 return (0xFF); /* error */
1883 debug ("(auto_req)atapi_issue returned sense key %X\n",res);
1885 memset(sense_ccb,0,sizeof(sense_ccb));
1886 memset(sense_data,0,sizeof(sense_data));
1887 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1888 sense_ccb[4]=18; /* allocation Length */
1890 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1891 key=(sense_data[2]&0xF);
1892 asc=(sense_data[12]);
1893 ascq=(sense_data[13]);
1895 debug ("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1896 debug (" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1903 return 0; /* ok device ready */
1905 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1910 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1913 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1918 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1922 debug ("Media not present\n");
1926 #ifdef CONFIG_AMIGAONEG3SE
1927 if ((sense_data[2]&0xF)==0x0B) {
1928 debug ("ABORTED COMMAND...retry\n");
1934 if ((sense_data[2]&0xf) == 0x02 &&
1935 sense_data[12] == 0x04 &&
1936 sense_data[13] == 0x01 ) {
1937 debug ("Waiting for unit to become active\n");
1943 #endif /* CONFIG_AMIGAONEG3SE */
1945 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1947 debug ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1952 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1954 unsigned char ccb[12]; /* Command descriptor block */
1955 unsigned char iobuf[64]; /* temp buf */
1959 device=dev_desc->dev;
1960 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1961 dev_desc->block_read=atapi_read;
1963 memset(ccb,0,sizeof(ccb));
1964 memset(iobuf,0,sizeof(iobuf));
1966 ccb[0]=ATAPI_CMD_INQUIRY;
1967 ccb[4]=40; /* allocation Legnth */
1968 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1970 debug ("ATAPI_CMD_INQUIRY returned %x\n",c);
1974 /* copy device ident strings */
1975 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1976 ident_cpy(dev_desc->product,&iobuf[16],16);
1977 ident_cpy(dev_desc->revision,&iobuf[32],5);
1982 dev_desc->type=iobuf[0] & 0x1f;
1984 if ((iobuf[1]&0x80)==0x80)
1985 dev_desc->removable = 1;
1987 dev_desc->removable = 0;
1989 memset(ccb,0,sizeof(ccb));
1990 memset(iobuf,0,sizeof(iobuf));
1991 ccb[0]=ATAPI_CMD_START_STOP;
1992 ccb[4]=0x03; /* start */
1994 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1996 debug ("ATAPI_CMD_START_STOP returned %x\n",c);
2000 memset(ccb,0,sizeof(ccb));
2001 memset(iobuf,0,sizeof(iobuf));
2002 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
2004 debug ("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
2008 memset(ccb,0,sizeof(ccb));
2009 memset(iobuf,0,sizeof(iobuf));
2010 ccb[0]=ATAPI_CMD_READ_CAP;
2011 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
2012 debug ("ATAPI_CMD_READ_CAP returned %x\n",c);
2016 debug ("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
2017 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
2018 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
2020 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
2021 ((unsigned long)iobuf[1]<<16) +
2022 ((unsigned long)iobuf[2]<< 8) +
2023 ((unsigned long)iobuf[3]);
2024 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
2025 ((unsigned long)iobuf[5]<<16) +
2026 ((unsigned long)iobuf[6]<< 8) +
2027 ((unsigned long)iobuf[7]);
2029 dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
2037 * we transfer only one block per command, since the multiple DRQ per
2038 * command is not yet implemented
2040 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
2041 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
2042 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
2044 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
2047 unsigned char ccb[12]; /* Command descriptor block */
2050 debug ("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
2051 device, blknr, blkcnt, (ulong)buffer);
2054 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
2055 cnt=ATAPI_READ_MAX_BLOCK;
2059 ccb[0]=ATAPI_CMD_READ_12;
2060 ccb[1]=0; /* reserved */
2061 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
2062 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
2063 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
2064 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
2065 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
2066 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
2067 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
2068 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
2069 ccb[10]=0; /* reserved */
2070 ccb[11]=0; /* reserved */
2072 if (atapi_issue_autoreq(device,ccb,12,
2073 (unsigned char *)buffer,
2074 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
2080 buffer+=(cnt*ATAPI_READ_BLOCK_SIZE);
2081 } while (blkcnt > 0);
2085 /* ------------------------------------------------------------------------- */
2087 #endif /* CONFIG_ATAPI */
2091 "ide - IDE sub-system\n",
2092 "reset - reset IDE controller\n"
2093 "ide info - show available IDE devices\n"
2094 "ide device [dev] - show or set current device\n"
2095 "ide part [dev] - print partition table of one or all IDE devices\n"
2096 "ide read addr blk# cnt\n"
2097 "ide write addr blk# cnt - read/write `cnt'"
2098 " blocks starting at block `blk#'\n"
2099 " to/from memory address `addr'\n"
2103 diskboot, 3, 1, do_diskboot,
2104 "diskboot- boot from IDE device\n",
2105 "loadAddr dev:part\n"
2108 #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */