2 * (C) Copyright 2000-2002
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>
34 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
42 #ifdef CONFIG_STATUS_LED
43 # include <status_led.h>
48 /* Macros depend on this variable */
49 static unsigned long mips_io_port_base = 0;
53 #ifdef CONFIG_SHOW_BOOT_PROGRESS
54 # include <status_led.h>
55 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
57 # define SHOW_BOOT_PROGRESS(arg)
65 #define PRINTF(fmt,args...) printf (fmt ,##args)
67 #define PRINTF(fmt,args...)
70 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
72 #ifdef CONFIG_IDE_8xx_DIRECT
73 /* Timings for IDE Interface
75 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
76 * 70 165 30 PIO-Mode 0, [ns]
78 * 50 125 20 PIO-Mode 1, [ns]
80 * 30 100 15 PIO-Mode 2, [ns]
82 * 30 80 10 PIO-Mode 3, [ns]
84 * 25 70 10 PIO-Mode 4, [ns]
88 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
90 /* Setup Length Hold */
91 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
92 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
93 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
94 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
95 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
98 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
101 #define CFG_PIO_MODE 0 /* use a relaxed default */
103 static int pio_mode = CFG_PIO_MODE;
105 /* Make clock cycles and always round up */
107 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
109 #endif /* CONFIG_IDE_8xx_DIRECT */
111 /* ------------------------------------------------------------------------- */
113 /* Current I/O Device */
114 static int curr_device = -1;
116 /* Current offset for IDE0 / IDE1 bus access */
117 ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
118 #if defined(CFG_ATA_IDE0_OFFSET)
121 #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
127 #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
129 #ifndef CONFIG_AMIGAONEG3SE
130 static int ide_bus_ok[CFG_IDE_MAXBUS];
132 static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
135 static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
136 /* ------------------------------------------------------------------------- */
138 #ifdef CONFIG_IDE_LED
140 static void ide_led (uchar led, uchar status);
142 extern void ide_led (uchar led, uchar status);
145 #ifndef CONFIG_AMIGAONEG3SE
146 #define ide_led(a,b) /* dummy */
148 extern void ide_led(uchar led, uchar status);
151 #define CONFIG_IDE_LED 1
152 #define DEVICE_LED(x) 1
156 #ifdef CONFIG_IDE_RESET
157 static void ide_reset (void);
159 #define ide_reset() /* dummy */
162 static void ide_ident (block_dev_desc_t *dev_desc);
163 static uchar ide_wait (int dev, ulong t);
165 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
167 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
169 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
171 static void __inline__ ide_outb(int dev, int port, unsigned char val);
172 static unsigned char __inline__ ide_inb(int dev, int port);
173 static void input_data(int dev, ulong *sect_buf, int words);
174 static void output_data(int dev, ulong *sect_buf, int words);
175 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
179 static void atapi_inquiry(block_dev_desc_t *dev_desc);
180 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
184 #ifdef CONFIG_IDE_8xx_DIRECT
185 static void set_pcmcia_timing (int pmode);
188 /* ------------------------------------------------------------------------- */
190 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
197 printf ("Usage:\n%s\n", cmdtp->usage);
200 if (strncmp(argv[1],"res",3) == 0) {
202 #ifdef CONFIG_IDE_8xx_DIRECT
203 " on PCMCIA " PCMCIA_SLOT_MSG
209 } else if (strncmp(argv[1],"inf",3) == 0) {
214 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
215 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
216 continue; /* list only known devices */
217 printf ("IDE device %d: ", i);
218 dev_print(&ide_dev_desc[i]);
222 } else if (strncmp(argv[1],"dev",3) == 0) {
223 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
224 puts ("\nno IDE devices available\n");
227 printf ("\nIDE device %d: ", curr_device);
228 dev_print(&ide_dev_desc[curr_device]);
230 } else if (strncmp(argv[1],"part",4) == 0) {
233 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
234 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
238 print_part(&ide_dev_desc[dev]);
242 puts ("\nno IDE devices available\n");
247 printf ("Usage:\n%s\n", cmdtp->usage);
250 if (strncmp(argv[1],"dev",3) == 0) {
251 int dev = (int)simple_strtoul(argv[2], NULL, 10);
253 printf ("\nIDE device %d: ", dev);
254 if (dev >= CFG_IDE_MAXDEVICE) {
255 puts ("unknown device\n");
258 dev_print(&ide_dev_desc[dev]);
261 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
267 puts ("... is now current device\n");
270 } else if (strncmp(argv[1],"part",4) == 0) {
271 int dev = (int)simple_strtoul(argv[2], NULL, 10);
273 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
274 print_part(&ide_dev_desc[dev]);
276 printf ("\nIDE device %d not available\n", dev);
281 } else if (strncmp(argv[1],"pio",4) == 0) {
282 int mode = (int)simple_strtoul(argv[2], NULL, 10);
284 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
289 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
290 mode, IDE_MAX_PIO_MODE);
296 printf ("Usage:\n%s\n", cmdtp->usage);
299 /* at least 4 args */
301 if (strcmp(argv[1],"read") == 0) {
302 ulong addr = simple_strtoul(argv[2], NULL, 16);
303 ulong blk = simple_strtoul(argv[3], NULL, 16);
304 ulong cnt = simple_strtoul(argv[4], NULL, 16);
307 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
308 curr_device, blk, cnt);
310 n = ide_dev_desc[curr_device].block_read (curr_device,
313 /* flush cache after read */
314 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
316 printf ("%ld blocks read: %s\n",
317 n, (n==cnt) ? "OK" : "ERROR");
323 } else if (strcmp(argv[1],"write") == 0) {
324 ulong addr = simple_strtoul(argv[2], NULL, 16);
325 ulong blk = simple_strtoul(argv[3], NULL, 16);
326 ulong cnt = simple_strtoul(argv[4], NULL, 16);
329 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
330 curr_device, blk, cnt);
332 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
334 printf ("%ld blocks written: %s\n",
335 n, (n==cnt) ? "OK" : "ERROR");
342 printf ("Usage:\n%s\n", cmdtp->usage);
350 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
352 char *boot_device = NULL;
357 disk_partition_t info;
363 addr = CFG_LOAD_ADDR;
364 boot_device = getenv ("bootdevice");
367 addr = simple_strtoul(argv[1], NULL, 16);
368 boot_device = getenv ("bootdevice");
371 addr = simple_strtoul(argv[1], NULL, 16);
372 boot_device = argv[2];
375 printf ("Usage:\n%s\n", cmdtp->usage);
376 SHOW_BOOT_PROGRESS (-1);
381 puts ("\n** No boot device **\n");
382 SHOW_BOOT_PROGRESS (-1);
386 dev = simple_strtoul(boot_device, &ep, 16);
388 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
389 printf ("\n** Device %d not available\n", dev);
390 SHOW_BOOT_PROGRESS (-1);
396 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
397 SHOW_BOOT_PROGRESS (-1);
400 part = simple_strtoul(++ep, NULL, 16);
402 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
403 SHOW_BOOT_PROGRESS (-1);
406 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
407 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
408 printf ("\n** Invalid partition type \"%.32s\""
409 " (expect \"" BOOT_PART_TYPE "\")\n",
411 SHOW_BOOT_PROGRESS (-1);
415 printf ("\nLoading from IDE device %d, partition %d: "
416 "Name: %.32s Type: %.32s\n",
417 dev, part, info.name, info.type);
419 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
420 info.start, info.size, info.blksz);
422 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
423 printf ("** Read error on %d:%d\n", dev, part);
424 SHOW_BOOT_PROGRESS (-1);
428 hdr = (image_header_t *)addr;
430 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
432 print_image_hdr (hdr);
434 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
435 cnt += info.blksz - 1;
439 printf("\n** Bad Magic Number **\n");
440 SHOW_BOOT_PROGRESS (-1);
444 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
445 (ulong *)(addr+info.blksz)) != cnt) {
446 printf ("** Read error on %d:%d\n", dev, part);
447 SHOW_BOOT_PROGRESS (-1);
452 /* Loading ok, update default load address */
456 /* Check if we should attempt an auto-start */
457 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
459 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
461 local_args[0] = argv[0];
462 local_args[1] = NULL;
464 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
466 do_bootm (cmdtp, 0, 1, local_args);
472 /* ------------------------------------------------------------------------- */
477 #ifdef CONFIG_IDE_8xx_DIRECT
478 DECLARE_GLOBAL_DATA_PTR;
479 volatile immap_t *immr = (immap_t *)CFG_IMMR;
480 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
484 #ifdef CONFIG_AMIGAONEG3SE
485 unsigned int max_bus_scan;
486 unsigned int ata_reset_time;
489 #ifdef CONFIG_IDE_8xx_PCCARD
490 extern int pcmcia_on (void);
491 extern int ide_devices_found; /* Initialized in check_ide_device() */
492 #endif /* CONFIG_IDE_8xx_PCCARD */
494 #ifdef CONFIG_IDE_PREINIT
497 if (ide_preinit ()) {
498 puts ("ide_preinit failed\n");
501 #endif /* CONFIG_IDE_PREINIT */
503 #ifdef CONFIG_IDE_8xx_PCCARD
504 extern int pcmcia_on (void);
505 extern int ide_devices_found; /* Initialized in check_ide_device() */
509 ide_devices_found = 0;
510 /* initialize the PCMCIA IDE adapter card */
512 if (!ide_devices_found)
514 udelay (1000000); /* 1 s */
515 #endif /* CONFIG_IDE_8xx_PCCARD */
519 #ifdef CONFIG_IDE_8xx_DIRECT
520 /* Initialize PIO timing tables */
521 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
522 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
524 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
526 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
528 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
530 " hold=%2d ns/%d clk\n",
532 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
533 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
534 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
536 #endif /* CONFIG_IDE_8xx_DIRECT */
538 /* Reset the IDE just to be sure.
539 * Light LED's to show
541 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
542 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
544 #ifdef CONFIG_IDE_8xx_DIRECT
545 /* PCMCIA / IDE initialization for common mem space */
546 pcmp->pcmc_pgcrb = 0;
548 /* start in PIO mode 0 - most relaxed timings */
550 set_pcmcia_timing (pio_mode);
551 #endif /* CONFIG_IDE_8xx_DIRECT */
554 * Wait for IDE to get ready.
555 * According to spec, this can take up to 31 seconds!
557 #ifndef CONFIG_AMIGAONEG3SE
558 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
559 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
561 s = getenv("ide_maxbus");
563 max_bus_scan = simple_strtol(s, NULL, 10);
565 max_bus_scan = CFG_IDE_MAXBUS;
567 for (bus=0; bus<max_bus_scan; ++bus) {
568 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
571 #ifdef CONFIG_IDE_8xx_PCCARD
572 /* Skip non-ide devices from probing */
573 if ((ide_devices_found & (1 << bus)) == 0) {
574 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
578 printf ("Bus %d: ", bus);
584 udelay (100000); /* 100 ms */
585 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
586 udelay (100000); /* 100 ms */
587 #ifdef CONFIG_AMIGAONEG3SE
588 ata_reset_time = ATA_RESET_TIME;
589 s = getenv("ide_reset_timeout");
590 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
594 udelay (10000); /* 10 ms */
596 c = ide_inb (dev, ATA_STATUS);
598 #ifdef CONFIG_AMIGAONEG3SE
599 if (i > (ata_reset_time * 100)) {
601 if (i > (ATA_RESET_TIME * 100)) {
603 puts ("** Timeout **\n");
604 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
605 #ifdef CONFIG_AMIGAONEG3SE
606 /* If this is the second bus, the first one was OK */
615 if ((i >= 100) && ((i%100)==0)) {
618 } while (c & ATA_STAT_BUSY);
620 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
621 puts ("not available ");
622 PRINTF ("Status = 0x%02X ", c);
623 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
624 } else if ((c & ATA_STAT_READY) == 0) {
625 puts ("not available ");
626 PRINTF ("Status = 0x%02X ", c);
635 #ifdef CONFIG_AMIGAONEG3SE
640 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
643 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
644 #ifdef CONFIG_IDE_LED
645 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
647 ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
648 ide_dev_desc[i].if_type=IF_TYPE_IDE;
649 ide_dev_desc[i].dev=i;
650 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
651 ide_dev_desc[i].blksz=0;
652 ide_dev_desc[i].lba=0;
653 ide_dev_desc[i].block_read=ide_read;
654 if (!ide_bus_ok[IDE_BUS(i)])
656 ide_led (led, 1); /* LED on */
657 ide_ident(&ide_dev_desc[i]);
658 ide_led (led, 0); /* LED off */
659 dev_print(&ide_dev_desc[i]);
661 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
662 init_part (&ide_dev_desc[i]); /* initialize partition type */
670 /* ------------------------------------------------------------------------- */
672 block_dev_desc_t * ide_get_dev(int dev)
674 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
678 #ifdef CONFIG_IDE_8xx_DIRECT
681 set_pcmcia_timing (int pmode)
683 volatile immap_t *immr = (immap_t *)CFG_IMMR;
684 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
687 PRINTF ("Set timing for PIO Mode %d\n", pmode);
689 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
690 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
691 | PCMCIA_SL (pio_config_clk[pmode].t_length)
696 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
697 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
698 #if (CFG_PCMCIA_POR0 != 0)
702 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
704 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
705 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
706 #if (CFG_PCMCIA_POR1 != 0)
710 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
712 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
713 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
714 #if (CFG_PCMCIA_POR2 != 0)
718 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
720 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
721 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
722 #if (CFG_PCMCIA_POR3 != 0)
726 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
730 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
731 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
732 #if (CFG_PCMCIA_POR4 != 0)
736 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
738 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
739 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
740 #if (CFG_PCMCIA_POR5 != 0)
744 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
746 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
747 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
748 #if (CFG_PCMCIA_POR6 != 0)
752 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
754 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
755 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
756 #if (CFG_PCMCIA_POR7 != 0)
760 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
764 #endif /* CONFIG_IDE_8xx_DIRECT */
766 /* ------------------------------------------------------------------------- */
769 static void __inline__
770 ide_outb(int dev, int port, unsigned char val)
772 PRINTF ("ide_outb (dev= %d, port= %d, val= 0x%02x) : @ 0x%08lx\n",
773 dev, port, val, (ATA_CURR_BASE(dev)+port));
775 /* Ensure I/O operations complete */
776 __asm__ volatile("eieio");
777 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
779 #else /* ! __PPC__ */
780 static void __inline__
781 ide_outb(int dev, int port, unsigned char val)
783 outb(val, ATA_CURR_BASE(dev)+port);
789 static unsigned char __inline__
790 ide_inb(int dev, int port)
793 /* Ensure I/O operations complete */
794 __asm__ volatile("eieio");
795 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
796 PRINTF ("ide_inb (dev= %d, port= %d) : @ 0x%08lx -> 0x%02x\n",
797 dev, port, (ATA_CURR_BASE(dev)+port), val);
800 #else /* ! __PPC__ */
801 static unsigned char __inline__
802 ide_inb(int dev, int port)
804 return inb(ATA_CURR_BASE(dev)+port);
809 # ifdef CONFIG_AMIGAONEG3SE
811 output_data_short(int dev, ulong *sect_buf, int words)
814 volatile ushort *pbuf;
816 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
817 dbuf = (ushort *)sect_buf;
819 __asm__ volatile ("eieio");
821 __asm__ volatile ("eieio");
827 # endif /* CONFIG_AMIGAONEG3SE */
830 /* We only need to swap data if we are running on a big endian cpu. */
831 /* But Au1x00 cpu:s already swaps data in big endian mode! */
832 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_AU1X00)
833 #define input_swap_data(x,y,z) input_data(x,y,z)
836 input_swap_data(int dev, ulong *sect_buf, int words)
838 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
839 ushort *dbuf = (ushort *)sect_buf;
842 *dbuf++ = ld_le16(pbuf);
843 *dbuf++ = ld_le16(pbuf);
846 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
851 output_data(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;
859 __asm__ volatile ("eieio");
861 __asm__ volatile ("eieio");
865 #else /* ! __PPC__ */
867 output_data(int dev, ulong *sect_buf, int words)
869 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
875 input_data(int dev, ulong *sect_buf, int words)
878 volatile ushort *pbuf;
880 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
881 dbuf = (ushort *)sect_buf;
883 __asm__ volatile ("eieio");
885 __asm__ volatile ("eieio");
889 #else /* ! __PPC__ */
891 input_data(int dev, ulong *sect_buf, int words)
893 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
898 #ifdef CONFIG_AMIGAONEG3SE
900 input_data_short(int dev, ulong *sect_buf, int words)
903 volatile ushort *pbuf;
905 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
906 dbuf = (ushort *)sect_buf;
908 __asm__ volatile ("eieio");
910 __asm__ volatile ("eieio");
921 /* -------------------------------------------------------------------------
923 static void ide_ident (block_dev_desc_t *dev_desc)
925 ulong iobuf[ATA_SECTORWORDS];
927 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
929 #ifdef CONFIG_AMIGAONEG3SE
937 int mode, cycle_time;
940 device=dev_desc->dev;
941 printf (" Device %d: ", device);
943 #ifdef CONFIG_AMIGAONEG3SE
944 s = getenv("ide_maxbus");
946 max_bus_scan = simple_strtol(s, NULL, 10);
948 max_bus_scan = CFG_IDE_MAXBUS;
950 if (device >= max_bus_scan*2) {
951 dev_desc->type=DEV_TYPE_UNKNOWN;
956 ide_led (DEVICE_LED(device), 1); /* LED on */
959 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
960 dev_desc->if_type=IF_TYPE_IDE;
963 #ifdef CONFIG_AMIGAONEG3SE
967 /* Warning: This will be tricky to read */
970 #endif /* CONFIG_AMIGAONEG3SE */
972 /* check signature */
973 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
974 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
975 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
976 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
977 /* ATAPI Signature found */
978 dev_desc->if_type=IF_TYPE_ATAPI;
979 /* Start Ident Command
981 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
983 * Wait for completion - ATAPI devices need more time
986 c = ide_wait (device, ATAPI_TIME_OUT);
991 /* Start Ident Command
993 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
995 /* Wait for completion
997 c = ide_wait (device, IDE_TIME_OUT);
999 ide_led (DEVICE_LED(device), 0); /* LED off */
1001 if (((c & ATA_STAT_DRQ) == 0) ||
1002 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
1003 #ifdef CONFIG_AMIGAONEG3SE
1011 #endif /* CONFIG_AMIGAONEG3SE */
1014 #ifdef CONFIG_AMIGAONEG3SE
1015 s = getenv("ide_doreset");
1016 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
1017 /* Need to soft reset the device in case it's an ATAPI... */
1018 PRINTF("Retrying...\n");
1019 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1021 ide_outb (device, ATA_COMMAND, 0x08);
1022 udelay (100000); /* 100 ms */
1027 } /* see above - ugly to read */
1028 #endif /* CONFIG_AMIGAONEG3SE */
1030 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1032 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1033 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1034 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1036 if ((iop->config & 0x0080)==0x0080)
1037 dev_desc->removable = 1;
1039 dev_desc->removable = 0;
1043 * Drive PIO mode autoselection
1047 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1048 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1050 PRINTF ("Override tPIO -> 2\n");
1052 if (iop->field_valid & 2) { /* drive implements ATA2? */
1053 PRINTF ("Drive implements ATA2\n");
1054 if (iop->capability & 8) { /* drive supports use_iordy? */
1055 cycle_time = iop->eide_pio_iordy;
1057 cycle_time = iop->eide_pio;
1059 PRINTF ("cycle time = %d\n", cycle_time);
1061 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1062 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1063 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1064 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1066 printf ("PIO mode to use: PIO %d\n", mode);
1070 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1071 atapi_inquiry(dev_desc);
1074 #endif /* CONFIG_ATAPI */
1077 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1079 dev_desc->type=DEV_TYPE_HARDDISK;
1080 dev_desc->blksz=ATA_BLOCKSIZE;
1081 dev_desc->lun=0; /* just to fill something in... */
1083 #if 0 /* only used to test the powersaving mode,
1084 * if enabled, the drive goes after 5 sec
1085 * in standby mode */
1086 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1087 c = ide_wait (device, IDE_TIME_OUT);
1088 ide_outb (device, ATA_SECT_CNT, 1);
1089 ide_outb (device, ATA_LBA_LOW, 0);
1090 ide_outb (device, ATA_LBA_MID, 0);
1091 ide_outb (device, ATA_LBA_HIGH, 0);
1092 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1093 ATA_DEVICE(device));
1094 ide_outb (device, ATA_COMMAND, 0xe3);
1096 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1101 /* ------------------------------------------------------------------------- */
1103 ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1107 unsigned char pwrsave=0; /* power save */
1109 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1110 device, blknr, blkcnt, (ulong)buffer);
1112 ide_led (DEVICE_LED(device), 1); /* LED on */
1116 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1117 c = ide_wait (device, IDE_TIME_OUT);
1119 if (c & ATA_STAT_BUSY) {
1120 printf ("IDE read: device %d not ready\n", device);
1124 /* first check if the drive is in Powersaving mode, if yes,
1125 * increase the timeout value */
1126 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1129 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1131 if (c & ATA_STAT_BUSY) {
1132 printf ("IDE read: device %d not ready\n", device);
1135 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1136 printf ("No Powersaving mode %X\n", c);
1138 c = ide_inb(device,ATA_SECT_CNT);
1139 PRINTF("Powersaving %02X\n",c);
1145 while (blkcnt-- > 0) {
1147 c = ide_wait (device, IDE_TIME_OUT);
1149 if (c & ATA_STAT_BUSY) {
1150 printf ("IDE read: device %d not ready\n", device);
1154 ide_outb (device, ATA_SECT_CNT, 1);
1155 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1156 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1157 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1158 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1159 ATA_DEVICE(device) |
1160 ((blknr >> 24) & 0xF) );
1161 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1166 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1169 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1172 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1173 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1178 input_data (device, buffer, ATA_SECTORWORDS);
1179 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1183 buffer += ATA_SECTORWORDS;
1186 ide_led (DEVICE_LED(device), 0); /* LED off */
1190 /* ------------------------------------------------------------------------- */
1193 ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1198 ide_led (DEVICE_LED(device), 1); /* LED on */
1202 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1204 while (blkcnt-- > 0) {
1206 c = ide_wait (device, IDE_TIME_OUT);
1208 if (c & ATA_STAT_BUSY) {
1209 printf ("IDE read: device %d not ready\n", device);
1213 ide_outb (device, ATA_SECT_CNT, 1);
1214 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1215 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1216 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1217 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1218 ATA_DEVICE(device) |
1219 ((blknr >> 24) & 0xF) );
1220 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1224 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1226 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1227 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1232 output_data (device, buffer, ATA_SECTORWORDS);
1233 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1236 buffer += ATA_SECTORWORDS;
1239 ide_led (DEVICE_LED(device), 0); /* LED off */
1243 /* ------------------------------------------------------------------------- */
1246 * copy src to dest, skipping leading and trailing blanks and null
1247 * terminate the string
1249 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1255 if (src[start]!=' ')
1265 for ( ; start<=end; start++) {
1271 /* ------------------------------------------------------------------------- */
1274 * Wait until Busy bit is off, or timeout (in ms)
1275 * Return last status
1277 static uchar ide_wait (int dev, ulong t)
1279 ulong delay = 10 * t; /* poll every 100 us */
1282 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1291 /* ------------------------------------------------------------------------- */
1293 #ifdef CONFIG_IDE_RESET
1294 extern void ide_set_reset(int idereset);
1296 static void ide_reset (void)
1298 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1299 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1304 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1306 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1307 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1309 ide_set_reset (1); /* assert reset */
1313 #ifdef CFG_PB_12V_ENABLE
1314 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1315 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1316 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1317 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1319 /* wait 500 ms for the voltage to stabilize
1321 for (i=0; i<500; ++i) {
1325 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1326 #endif /* CFG_PB_12V_ENABLE */
1328 #ifdef CFG_PB_IDE_MOTOR
1329 /* configure IDE Motor voltage monitor pin as input */
1330 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1331 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1332 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1334 /* wait up to 1 s for the motor voltage to stabilize
1336 for (i=0; i<1000; ++i) {
1337 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1343 if (i == 1000) { /* Timeout */
1344 printf ("\nWarning: 5V for IDE Motor missing\n");
1345 # ifdef CONFIG_STATUS_LED
1346 # ifdef STATUS_LED_YELLOW
1347 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1349 # ifdef STATUS_LED_GREEN
1350 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1352 # endif /* CONFIG_STATUS_LED */
1354 #endif /* CFG_PB_IDE_MOTOR */
1358 /* de-assert RESET signal */
1362 for (i=0; i<250; ++i) {
1367 #endif /* CONFIG_IDE_RESET */
1369 /* ------------------------------------------------------------------------- */
1371 #if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
1373 static uchar led_buffer = 0; /* Buffer for current LED status */
1375 static void ide_led (uchar led, uchar status)
1377 uchar *led_port = LED_PORT;
1379 if (status) { /* switch LED on */
1381 } else { /* switch LED off */
1385 *led_port = led_buffer;
1388 #endif /* CONFIG_IDE_LED */
1390 /* ------------------------------------------------------------------------- */
1393 /****************************************************************************
1401 #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1403 #define AT_PRINTF(fmt,args...)
1407 /* since ATAPI may use commands with not 4 bytes alligned length
1408 * we have our own transfer functions, 2 bytes alligned */
1410 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1413 volatile ushort *pbuf;
1415 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1416 dbuf = (ushort *)sect_buf;
1418 __asm__ volatile ("eieio");
1424 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1427 volatile ushort *pbuf;
1429 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1430 dbuf = (ushort *)sect_buf;
1432 __asm__ volatile ("eieio");
1437 #else /* ! __PPC__ */
1439 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1441 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1446 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1448 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1451 #endif /* __PPC__ */
1454 * Wait until (Status & mask) == res, or timeout (in ms)
1455 * Return last status
1456 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1457 * and then they set their DRQ Bit
1459 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1461 ulong delay = 10 * t; /* poll every 100 us */
1464 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1465 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1466 /* break if error occurs (doesn't make sense to wait more) */
1467 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1478 * issue an atapi command
1480 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1482 unsigned char c,err,mask,res;
1484 ide_led (DEVICE_LED(device), 1); /* LED on */
1488 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1490 #ifdef CONFIG_AMIGAONEG3SE
1491 # warning THF: Removed LBA mode ???
1493 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1494 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1495 if ((c & mask) != res) {
1496 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1500 /* write taskfile */
1501 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1502 ide_outb (device, ATA_SECT_CNT, 0);
1503 ide_outb (device, ATA_SECT_NUM, 0);
1504 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1505 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1506 #ifdef CONFIG_AMIGAONEG3SE
1507 # warning THF: Removed LBA mode ???
1509 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1511 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1514 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1516 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1518 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1519 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1524 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1525 /* ATAPI Command written wait for completition */
1526 udelay (5000); /* device must set bsy */
1528 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1529 /* if no data wait for DRQ = 0 BSY = 0
1530 * if data wait for DRQ = 1 BSY = 0 */
1534 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1535 if ((c & mask) != res ) {
1536 if (c & ATA_STAT_ERR) {
1537 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1538 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1540 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1545 n=ide_inb(device, ATA_CYL_HIGH);
1547 n+=ide_inb(device, ATA_CYL_LOW);
1549 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1553 if((n==0)&&(buflen<0)) {
1554 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1559 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1561 if(n!=0) { /* data transfer */
1562 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1563 /* we transfer shorts */
1565 /* ok now decide if it is an in or output */
1566 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1567 AT_PRINTF("Write to device\n");
1568 output_data_shorts(device,(unsigned short *)buffer,n);
1570 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1571 input_data_shorts(device,(unsigned short *)buffer,n);
1574 udelay(5000); /* seems that some CD ROMs need this... */
1575 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1577 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1578 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1579 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1580 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1585 ide_led (DEVICE_LED(device), 0); /* LED off */
1590 * sending the command to atapi_issue. If an status other than good
1591 * returns, an request_sense will be issued
1594 #define ATAPI_DRIVE_NOT_READY 100
1595 #define ATAPI_UNIT_ATTN 10
1597 unsigned char atapi_issue_autoreq (int device,
1600 unsigned char *buffer,
1603 unsigned char sense_data[18],sense_ccb[12];
1604 unsigned char res,key,asc,ascq;
1605 int notready,unitattn;
1607 #ifdef CONFIG_AMIGAONEG3SE
1609 unsigned int timeout, retrycnt;
1611 s = getenv("ide_cd_timeout");
1612 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1617 unitattn=ATAPI_UNIT_ATTN;
1618 notready=ATAPI_DRIVE_NOT_READY;
1621 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1623 return (0); /* Ok */
1626 return (0xFF); /* error */
1628 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1630 memset(sense_ccb,0,sizeof(sense_ccb));
1631 memset(sense_data,0,sizeof(sense_data));
1632 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1633 sense_ccb[4]=18; /* allocation Length */
1635 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1636 key=(sense_data[2]&0xF);
1637 asc=(sense_data[12]);
1638 ascq=(sense_data[13]);
1640 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1641 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1648 return 0; /* ok device ready */
1650 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1655 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1658 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1663 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1667 AT_PRINTF("Media not present\n");
1671 #ifdef CONFIG_AMIGAONEG3SE
1672 if ((sense_data[2]&0xF)==0x0B) {
1673 AT_PRINTF("ABORTED COMMAND...retry\n");
1679 if ((sense_data[2]&0xf) == 0x02 &&
1680 sense_data[12] == 0x04 &&
1681 sense_data[13] == 0x01 ) {
1682 AT_PRINTF("Waiting for unit to become active\n");
1688 #endif /* CONFIG_AMIGAONEG3SE */
1690 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1692 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1697 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1699 unsigned char ccb[12]; /* Command descriptor block */
1700 unsigned char iobuf[64]; /* temp buf */
1704 device=dev_desc->dev;
1705 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1706 dev_desc->block_read=atapi_read;
1708 memset(ccb,0,sizeof(ccb));
1709 memset(iobuf,0,sizeof(iobuf));
1711 ccb[0]=ATAPI_CMD_INQUIRY;
1712 ccb[4]=40; /* allocation Legnth */
1713 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1715 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1719 /* copy device ident strings */
1720 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1721 ident_cpy(dev_desc->product,&iobuf[16],16);
1722 ident_cpy(dev_desc->revision,&iobuf[32],5);
1727 dev_desc->type=iobuf[0] & 0x1f;
1729 if ((iobuf[1]&0x80)==0x80)
1730 dev_desc->removable = 1;
1732 dev_desc->removable = 0;
1734 memset(ccb,0,sizeof(ccb));
1735 memset(iobuf,0,sizeof(iobuf));
1736 ccb[0]=ATAPI_CMD_START_STOP;
1737 ccb[4]=0x03; /* start */
1739 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1741 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1745 memset(ccb,0,sizeof(ccb));
1746 memset(iobuf,0,sizeof(iobuf));
1747 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1749 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1753 memset(ccb,0,sizeof(ccb));
1754 memset(iobuf,0,sizeof(iobuf));
1755 ccb[0]=ATAPI_CMD_READ_CAP;
1756 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1757 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1761 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1762 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1763 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1765 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1766 ((unsigned long)iobuf[1]<<16) +
1767 ((unsigned long)iobuf[2]<< 8) +
1768 ((unsigned long)iobuf[3]);
1769 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1770 ((unsigned long)iobuf[5]<<16) +
1771 ((unsigned long)iobuf[6]<< 8) +
1772 ((unsigned long)iobuf[7]);
1779 * we transfer only one block per command, since the multiple DRQ per
1780 * command is not yet implemented
1782 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1783 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1784 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1786 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1789 unsigned char ccb[12]; /* Command descriptor block */
1792 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1793 device, blknr, blkcnt, (ulong)buffer);
1796 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1797 cnt=ATAPI_READ_MAX_BLOCK;
1801 ccb[0]=ATAPI_CMD_READ_12;
1802 ccb[1]=0; /* reserved */
1803 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1804 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1805 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1806 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1807 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1808 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1809 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1810 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1811 ccb[10]=0; /* reserved */
1812 ccb[11]=0; /* reserved */
1814 if (atapi_issue_autoreq(device,ccb,12,
1815 (unsigned char *)buffer,
1816 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1822 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1823 } while (blkcnt > 0);
1827 /* ------------------------------------------------------------------------- */
1829 #endif /* CONFIG_ATAPI */
1833 "ide - IDE sub-system\n",
1834 "reset - reset IDE controller\n"
1835 "ide info - show available IDE devices\n"
1836 "ide device [dev] - show or set current device\n"
1837 "ide part [dev] - print partition table of one or all IDE devices\n"
1838 "ide read addr blk# cnt\n"
1839 "ide write addr blk# cnt - read/write `cnt'"
1840 " blocks starting at block `blk#'\n"
1841 " to/from memory address `addr'\n"
1845 diskboot, 3, 1, do_diskboot,
1846 "diskboot- boot from IDE device\n",
1847 "loadAddr dev:part\n"
1850 #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */