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)
44 #ifdef CONFIG_STATUS_LED
45 # include <status_led.h>
51 #ifdef CONFIG_SHOW_BOOT_PROGRESS
52 # include <status_led.h>
53 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
55 # define SHOW_BOOT_PROGRESS(arg)
63 #define PRINTF(fmt,args...) printf (fmt ,##args)
65 #define PRINTF(fmt,args...)
68 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
70 /* Timings for IDE Interface
72 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
73 * 70 165 30 PIO-Mode 0, [ns]
75 * 50 125 20 PIO-Mode 1, [ns]
77 * 30 100 15 PIO-Mode 2, [ns]
79 * 30 80 10 PIO-Mode 3, [ns]
81 * 25 70 10 PIO-Mode 4, [ns]
85 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
87 /* Setup Length Hold */
88 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
89 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
90 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
91 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
92 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
95 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
98 #define CFG_PIO_MODE 0 /* use a relaxed default */
100 static int pio_mode = CFG_PIO_MODE;
102 /* Make clock cycles and always round up */
104 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
106 /* ------------------------------------------------------------------------- */
108 /* Current I/O Device */
109 static int curr_device = -1;
111 /* Current offset for IDE0 / IDE1 bus access */
112 ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
113 #if defined(CFG_ATA_IDE0_OFFSET)
116 #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
122 #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
125 #ifndef CONFIG_AMIGAONEG3SE
126 static int ide_bus_ok[CFG_IDE_MAXBUS];
128 static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
131 static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
132 /* ------------------------------------------------------------------------- */
134 #ifdef CONFIG_IDE_LED
136 static void ide_led (uchar led, uchar status);
138 extern void ide_led (uchar led, uchar status);
141 #ifndef CONFIG_AMIGAONEG3SE
142 #define ide_led(a,b) /* dummy */
144 extern void ide_led(uchar led, uchar status);
147 #define CONFIG_IDE_LED 1
148 #define DEVICE_LED(x) 1
152 #ifdef CONFIG_IDE_RESET
153 static void ide_reset (void);
155 #define ide_reset() /* dummy */
158 static void ide_ident (block_dev_desc_t *dev_desc);
159 static uchar ide_wait (int dev, ulong t);
161 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
163 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
165 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
167 static void __inline__ ide_outb(int dev, int port, unsigned char val);
168 static unsigned char __inline__ ide_inb(int dev, int port);
170 static void input_swap_data(int dev, ulong *sect_buf, int words);
172 static void input_data(int dev, ulong *sect_buf, int words);
173 static void output_data(int dev, ulong *sect_buf, int words);
174 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
178 static void atapi_inquiry(block_dev_desc_t *dev_desc);
179 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
183 #ifdef CONFIG_IDE_8xx_DIRECT
184 static void set_pcmcia_timing (int pmode);
186 #define set_pcmcia_timing(a) /* dummy */
189 /* ------------------------------------------------------------------------- */
191 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
198 printf ("Usage:\n%s\n", cmdtp->usage);
201 if (strncmp(argv[1],"res",3) == 0) {
203 #ifdef CONFIG_IDE_8xx_DIRECT
204 " on PCMCIA " PCMCIA_SLOT_MSG
210 } else if (strncmp(argv[1],"inf",3) == 0) {
215 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
216 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
217 continue; /* list only known devices */
218 printf ("IDE device %d: ", i);
219 dev_print(&ide_dev_desc[i]);
223 } else if (strncmp(argv[1],"dev",3) == 0) {
224 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
225 puts ("\nno IDE devices available\n");
228 printf ("\nIDE device %d: ", curr_device);
229 dev_print(&ide_dev_desc[curr_device]);
231 } else if (strncmp(argv[1],"part",4) == 0) {
234 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
235 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
239 print_part(&ide_dev_desc[dev]);
243 puts ("\nno IDE devices available\n");
248 printf ("Usage:\n%s\n", cmdtp->usage);
251 if (strncmp(argv[1],"dev",3) == 0) {
252 int dev = (int)simple_strtoul(argv[2], NULL, 10);
254 printf ("\nIDE device %d: ", dev);
255 if (dev >= CFG_IDE_MAXDEVICE) {
256 puts ("unknown device\n");
259 dev_print(&ide_dev_desc[dev]);
262 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
268 puts ("... is now current device\n");
271 } else if (strncmp(argv[1],"part",4) == 0) {
272 int dev = (int)simple_strtoul(argv[2], NULL, 10);
274 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
275 print_part(&ide_dev_desc[dev]);
277 printf ("\nIDE device %d not available\n", dev);
282 } else if (strncmp(argv[1],"pio",4) == 0) {
283 int mode = (int)simple_strtoul(argv[2], NULL, 10);
285 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
290 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
291 mode, IDE_MAX_PIO_MODE);
297 printf ("Usage:\n%s\n", cmdtp->usage);
300 /* at least 4 args */
302 if (strcmp(argv[1],"read") == 0) {
303 ulong addr = simple_strtoul(argv[2], NULL, 16);
304 ulong blk = simple_strtoul(argv[3], NULL, 16);
305 ulong cnt = simple_strtoul(argv[4], NULL, 16);
308 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
309 curr_device, blk, cnt);
311 n = ide_dev_desc[curr_device].block_read (curr_device,
314 /* flush cache after read */
315 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
317 printf ("%ld blocks read: %s\n",
318 n, (n==cnt) ? "OK" : "ERROR");
324 } else if (strcmp(argv[1],"write") == 0) {
325 ulong addr = simple_strtoul(argv[2], NULL, 16);
326 ulong blk = simple_strtoul(argv[3], NULL, 16);
327 ulong cnt = simple_strtoul(argv[4], NULL, 16);
330 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
331 curr_device, blk, cnt);
333 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
335 printf ("%ld blocks written: %s\n",
336 n, (n==cnt) ? "OK" : "ERROR");
343 printf ("Usage:\n%s\n", cmdtp->usage);
351 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
353 char *boot_device = NULL;
358 disk_partition_t info;
364 addr = CFG_LOAD_ADDR;
365 boot_device = getenv ("bootdevice");
368 addr = simple_strtoul(argv[1], NULL, 16);
369 boot_device = getenv ("bootdevice");
372 addr = simple_strtoul(argv[1], NULL, 16);
373 boot_device = argv[2];
376 printf ("Usage:\n%s\n", cmdtp->usage);
377 SHOW_BOOT_PROGRESS (-1);
382 puts ("\n** No boot device **\n");
383 SHOW_BOOT_PROGRESS (-1);
387 dev = simple_strtoul(boot_device, &ep, 16);
389 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
390 printf ("\n** Device %d not available\n", dev);
391 SHOW_BOOT_PROGRESS (-1);
397 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
398 SHOW_BOOT_PROGRESS (-1);
401 part = simple_strtoul(++ep, NULL, 16);
403 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
404 SHOW_BOOT_PROGRESS (-1);
407 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
408 (strncmp(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 (-1);
416 printf ("\nLoading from IDE device %d, partition %d: "
417 "Name: %.32s Type: %.32s\n",
418 dev, part, info.name, info.type);
420 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
421 info.start, info.size, info.blksz);
423 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
424 printf ("** Read error on %d:%d\n", dev, part);
425 SHOW_BOOT_PROGRESS (-1);
429 hdr = (image_header_t *)addr;
431 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
433 print_image_hdr (hdr);
435 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
436 cnt += info.blksz - 1;
440 printf("\n** Bad Magic Number **\n");
441 SHOW_BOOT_PROGRESS (-1);
445 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
446 (ulong *)(addr+info.blksz)) != cnt) {
447 printf ("** Read error on %d:%d\n", dev, part);
448 SHOW_BOOT_PROGRESS (-1);
453 /* Loading ok, update default load address */
457 /* Check if we should attempt an auto-start */
458 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
460 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
462 local_args[0] = argv[0];
463 local_args[1] = NULL;
465 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
467 do_bootm (cmdtp, 0, 1, local_args);
473 /* ------------------------------------------------------------------------- */
477 DECLARE_GLOBAL_DATA_PTR;
479 #ifdef CONFIG_IDE_8xx_DIRECT
480 volatile immap_t *immr = (immap_t *)CFG_IMMR;
481 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
485 #ifdef CONFIG_AMIGAONEG3SE
486 unsigned int max_bus_scan;
487 unsigned int ata_reset_time;
491 #ifdef CONFIG_IDE_8xx_PCCARD
492 extern int pcmcia_on (void);
493 extern int ide_devices_found; /* Initialized in check_ide_device() */
497 ide_devices_found = 0;
498 /* initialize the PCMCIA IDE adapter card */
500 if (!ide_devices_found)
502 udelay (1000000); /* 1 s */
503 #endif /* CONFIG_IDE_8xx_PCCARD */
507 /* Initialize PIO timing tables */
508 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
509 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
511 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
513 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
515 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
517 " hold=%2d ns/%d clk\n",
519 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
520 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
521 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
524 /* Reset the IDE just to be sure.
525 * Light LED's to show
527 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
528 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
530 #ifdef CONFIG_IDE_8xx_DIRECT
531 /* PCMCIA / IDE initialization for common mem space */
532 pcmp->pcmc_pgcrb = 0;
535 /* start in PIO mode 0 - most relaxed timings */
537 set_pcmcia_timing (pio_mode);
540 * Wait for IDE to get ready.
541 * According to spec, this can take up to 31 seconds!
543 #ifndef CONFIG_AMIGAONEG3SE
544 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
545 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
547 s = getenv("ide_maxbus");
549 max_bus_scan = simple_strtol(s, NULL, 10);
551 max_bus_scan = CFG_IDE_MAXBUS;
553 for (bus=0; bus<max_bus_scan; ++bus) {
554 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
557 #ifdef CONFIG_IDE_8xx_PCCARD
558 /* Skip non-ide devices from probing */
559 if ((ide_devices_found & (1 << bus)) == 0) {
560 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
564 printf ("Bus %d: ", bus);
570 udelay (100000); /* 100 ms */
571 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
572 udelay (100000); /* 100 ms */
573 #ifdef CONFIG_AMIGAONEG3SE
574 ata_reset_time = ATA_RESET_TIME;
575 s = getenv("ide_reset_timeout");
576 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
580 udelay (10000); /* 10 ms */
582 c = ide_inb (dev, ATA_STATUS);
584 #ifdef CONFIG_AMIGAONEG3SE
585 if (i > (ata_reset_time * 100)) {
587 if (i > (ATA_RESET_TIME * 100)) {
589 puts ("** Timeout **\n");
590 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
591 #ifdef CONFIG_AMIGAONEG3SE
592 /* If this is the second bus, the first one was OK */
601 if ((i >= 100) && ((i%100)==0)) {
604 } while (c & ATA_STAT_BUSY);
606 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
607 puts ("not available ");
608 PRINTF ("Status = 0x%02X ", c);
609 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
610 } else if ((c & ATA_STAT_READY) == 0) {
611 puts ("not available ");
612 PRINTF ("Status = 0x%02X ", c);
621 #ifdef CONFIG_AMIGAONEG3SE
626 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
629 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
630 #ifdef CONFIG_IDE_LED
631 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
633 ide_dev_desc[i].if_type=IF_TYPE_IDE;
634 ide_dev_desc[i].dev=i;
635 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
636 ide_dev_desc[i].blksz=0;
637 ide_dev_desc[i].lba=0;
638 ide_dev_desc[i].block_read=ide_read;
639 if (!ide_bus_ok[IDE_BUS(i)])
641 ide_led (led, 1); /* LED on */
642 ide_ident(&ide_dev_desc[i]);
643 ide_led (led, 0); /* LED off */
644 dev_print(&ide_dev_desc[i]);
646 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
647 init_part (&ide_dev_desc[i]); /* initialize partition type */
655 /* ------------------------------------------------------------------------- */
657 block_dev_desc_t * ide_get_dev(int dev)
659 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
663 #ifdef CONFIG_IDE_8xx_DIRECT
666 set_pcmcia_timing (int pmode)
668 volatile immap_t *immr = (immap_t *)CFG_IMMR;
669 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
672 PRINTF ("Set timing for PIO Mode %d\n", pmode);
674 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
675 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
676 | PCMCIA_SL (pio_config_clk[pmode].t_length)
681 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
682 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
683 #if (CFG_PCMCIA_POR0 != 0)
687 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
689 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
690 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
691 #if (CFG_PCMCIA_POR1 != 0)
695 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
697 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
698 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
699 #if (CFG_PCMCIA_POR2 != 0)
703 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
705 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
706 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
707 #if (CFG_PCMCIA_POR3 != 0)
711 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
715 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
716 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
717 #if (CFG_PCMCIA_POR4 != 0)
721 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
723 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
724 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
725 #if (CFG_PCMCIA_POR5 != 0)
729 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
731 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
732 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
733 #if (CFG_PCMCIA_POR6 != 0)
737 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
739 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
740 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
741 #if (CFG_PCMCIA_POR7 != 0)
745 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
749 #endif /* CONFIG_IDE_8xx_DIRECT */
751 /* ------------------------------------------------------------------------- */
754 static void __inline__
755 ide_outb(int dev, int port, unsigned char val)
757 /* Ensure I/O operations complete */
758 __asm__ volatile("eieio");
759 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
761 printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
764 #else /* ! __PPC__ */
765 static void __inline__
766 ide_outb(int dev, int port, unsigned char val)
774 static unsigned char __inline__
775 ide_inb(int dev, int port)
778 /* Ensure I/O operations complete */
779 __asm__ volatile("eieio");
780 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
782 printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
786 #else /* ! __PPC__ */
787 static unsigned char __inline__
788 ide_inb(int dev, int port)
795 __inline__ unsigned ld_le16(const volatile unsigned short *addr)
799 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r"(val) : "r"(addr), "m"(*addr));
803 #ifdef CONFIG_AMIGAONEG3SE
805 output_data_short(int dev, ulong *sect_buf, int words)
808 volatile ushort *pbuf;
810 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
811 dbuf = (ushort *)sect_buf;
813 __asm__ volatile ("eieio");
815 __asm__ volatile ("eieio");
824 input_swap_data(int dev, ulong *sect_buf, int words)
826 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
827 ushort *dbuf = (ushort *)sect_buf;
830 *dbuf++ = ld_le16(pbuf);
831 *dbuf++ = ld_le16(pbuf);
834 #else /* ! __PPC__ */
835 #define input_swap_data(x,y,z) input_data(x,y,z)
843 output_data(int dev, ulong *sect_buf, int words)
846 volatile ushort *pbuf;
848 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
849 dbuf = (ushort *)sect_buf;
851 __asm__ volatile ("eieio");
853 __asm__ volatile ("eieio");
857 #else /* ! __PPC__ */
859 output_data(int dev, ulong *sect_buf, int words)
861 outsw(ATA_DATA_REG, sect_buf, words<<1);
867 input_data(int dev, ulong *sect_buf, int words)
870 volatile ushort *pbuf;
872 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
873 dbuf = (ushort *)sect_buf;
875 __asm__ volatile ("eieio");
877 __asm__ volatile ("eieio");
881 #else /* ! __PPC__ */
883 input_data(int dev, ulong *sect_buf, int words)
885 insw(ATA_DATA_REG, sect_buf, words << 1);
890 #ifdef CONFIG_AMIGAONEG3SE
892 input_data_short(int dev, ulong *sect_buf, int words)
895 volatile ushort *pbuf;
897 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
898 dbuf = (ushort *)sect_buf;
900 __asm__ volatile ("eieio");
902 __asm__ volatile ("eieio");
913 /* -------------------------------------------------------------------------
915 static void ide_ident (block_dev_desc_t *dev_desc)
917 ulong iobuf[ATA_SECTORWORDS];
919 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
921 #ifdef CONFIG_AMIGAONEG3SE
929 int mode, cycle_time;
932 device=dev_desc->dev;
933 printf (" Device %d: ", device);
935 #ifdef CONFIG_AMIGAONEG3SE
936 s = getenv("ide_maxbus");
938 max_bus_scan = simple_strtol(s, NULL, 10);
940 max_bus_scan = CFG_IDE_MAXBUS;
942 if (device >= max_bus_scan*2) {
943 dev_desc->type=DEV_TYPE_UNKNOWN;
948 ide_led (DEVICE_LED(device), 1); /* LED on */
951 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
952 dev_desc->if_type=IF_TYPE_IDE;
955 #ifdef CONFIG_AMIGAONEG3SE
959 /* Warning: This will be tricky to read */
962 #endif /* CONFIG_AMIGAONEG3SE */
964 /* check signature */
965 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
966 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
967 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
968 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
969 /* ATAPI Signature found */
970 dev_desc->if_type=IF_TYPE_ATAPI;
971 /* Start Ident Command
973 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
975 * Wait for completion - ATAPI devices need more time
978 c = ide_wait (device, ATAPI_TIME_OUT);
983 /* Start Ident Command
985 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
987 /* Wait for completion
989 c = ide_wait (device, IDE_TIME_OUT);
991 ide_led (DEVICE_LED(device), 0); /* LED off */
993 if (((c & ATA_STAT_DRQ) == 0) ||
994 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
995 #ifdef CONFIG_AMIGAONEG3SE
999 dev_desc->type=DEV_TYPE_UNKNOWN;
1003 dev_desc->type=DEV_TYPE_UNKNOWN;
1005 #endif /* CONFIG_AMIGAONEG3SE */
1008 #ifdef CONFIG_AMIGAONEG3SE
1009 s = getenv("ide_doreset");
1010 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
1011 /* Need to soft reset the device in case it's an ATAPI... */
1012 PRINTF("Retrying...\n");
1013 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1015 ide_outb (device, ATA_COMMAND, 0x08);
1016 udelay (100000); /* 100 ms */
1021 } /* see above - ugly to read */
1022 #endif /* CONFIG_AMIGAONEG3SE */
1024 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1026 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1027 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1028 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1030 if ((iop->config & 0x0080)==0x0080)
1031 dev_desc->removable = 1;
1033 dev_desc->removable = 0;
1037 * Drive PIO mode autoselection
1041 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1042 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1044 PRINTF ("Override tPIO -> 2\n");
1046 if (iop->field_valid & 2) { /* drive implements ATA2? */
1047 PRINTF ("Drive implements ATA2\n");
1048 if (iop->capability & 8) { /* drive supports use_iordy? */
1049 cycle_time = iop->eide_pio_iordy;
1051 cycle_time = iop->eide_pio;
1053 PRINTF ("cycle time = %d\n", cycle_time);
1055 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1056 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1057 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1058 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1060 printf ("PIO mode to use: PIO %d\n", mode);
1064 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1065 atapi_inquiry(dev_desc);
1068 #endif /* CONFIG_ATAPI */
1071 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1073 dev_desc->type=DEV_TYPE_HARDDISK;
1074 dev_desc->blksz=ATA_BLOCKSIZE;
1075 dev_desc->lun=0; /* just to fill something in... */
1077 #if 0 /* only used to test the powersaving mode,
1078 * if enabled, the drive goes after 5 sec
1079 * in standby mode */
1080 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1081 c = ide_wait (device, IDE_TIME_OUT);
1082 ide_outb (device, ATA_SECT_CNT, 1);
1083 ide_outb (device, ATA_LBA_LOW, 0);
1084 ide_outb (device, ATA_LBA_MID, 0);
1085 ide_outb (device, ATA_LBA_HIGH, 0);
1086 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1087 ATA_DEVICE(device));
1088 ide_outb (device, ATA_COMMAND, 0xe3);
1090 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1095 /* ------------------------------------------------------------------------- */
1097 ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1101 unsigned char pwrsave=0; /* power save */
1103 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1104 device, blknr, blkcnt, (ulong)buffer);
1106 ide_led (DEVICE_LED(device), 1); /* LED on */
1110 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1111 c = ide_wait (device, IDE_TIME_OUT);
1113 if (c & ATA_STAT_BUSY) {
1114 printf ("IDE read: device %d not ready\n", device);
1118 /* first check if the drive is in Powersaving mode, if yes,
1119 * increase the timeout value */
1120 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1123 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1125 if (c & ATA_STAT_BUSY) {
1126 printf ("IDE read: device %d not ready\n", device);
1129 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1130 printf ("No Powersaving mode %X\n", c);
1132 c = ide_inb(device,ATA_SECT_CNT);
1133 PRINTF("Powersaving %02X\n",c);
1139 while (blkcnt-- > 0) {
1141 c = ide_wait (device, IDE_TIME_OUT);
1143 if (c & ATA_STAT_BUSY) {
1144 printf ("IDE read: device %d not ready\n", device);
1148 ide_outb (device, ATA_SECT_CNT, 1);
1149 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1150 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1151 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1152 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1153 ATA_DEVICE(device) |
1154 ((blknr >> 24) & 0xF) );
1155 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1160 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1163 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1166 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1167 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1172 input_data (device, buffer, ATA_SECTORWORDS);
1173 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1177 buffer += ATA_SECTORWORDS;
1180 ide_led (DEVICE_LED(device), 0); /* LED off */
1184 /* ------------------------------------------------------------------------- */
1187 ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1192 ide_led (DEVICE_LED(device), 1); /* LED on */
1196 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1198 while (blkcnt-- > 0) {
1200 c = ide_wait (device, IDE_TIME_OUT);
1202 if (c & ATA_STAT_BUSY) {
1203 printf ("IDE read: device %d not ready\n", device);
1207 ide_outb (device, ATA_SECT_CNT, 1);
1208 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1209 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1210 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1211 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1212 ATA_DEVICE(device) |
1213 ((blknr >> 24) & 0xF) );
1214 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1218 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1220 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1221 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1226 output_data (device, buffer, ATA_SECTORWORDS);
1227 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1230 buffer += ATA_SECTORWORDS;
1233 ide_led (DEVICE_LED(device), 0); /* LED off */
1237 /* ------------------------------------------------------------------------- */
1240 * copy src to dest, skipping leading and trailing blanks and null
1241 * terminate the string
1243 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1249 if (src[start]!=' ')
1259 for ( ; start<=end; start++) {
1265 /* ------------------------------------------------------------------------- */
1268 * Wait until Busy bit is off, or timeout (in ms)
1269 * Return last status
1271 static uchar ide_wait (int dev, ulong t)
1273 ulong delay = 10 * t; /* poll every 100 us */
1276 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1285 /* ------------------------------------------------------------------------- */
1287 #ifdef CONFIG_IDE_RESET
1288 extern void ide_set_reset(int idereset);
1290 static void ide_reset (void)
1292 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1293 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1298 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1300 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1301 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1303 ide_set_reset (1); /* assert reset */
1307 #ifdef CFG_PB_12V_ENABLE
1308 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1309 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1310 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1311 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1313 /* wait 500 ms for the voltage to stabilize
1315 for (i=0; i<500; ++i) {
1319 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1320 #endif /* CFG_PB_12V_ENABLE */
1322 #ifdef CFG_PB_IDE_MOTOR
1323 /* configure IDE Motor voltage monitor pin as input */
1324 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1325 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1326 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1328 /* wait up to 1 s for the motor voltage to stabilize
1330 for (i=0; i<1000; ++i) {
1331 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1337 if (i == 1000) { /* Timeout */
1338 printf ("\nWarning: 5V for IDE Motor missing\n");
1339 # ifdef CONFIG_STATUS_LED
1340 # ifdef STATUS_LED_YELLOW
1341 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1343 # ifdef STATUS_LED_GREEN
1344 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1346 # endif /* CONFIG_STATUS_LED */
1348 #endif /* CFG_PB_IDE_MOTOR */
1352 /* de-assert RESET signal */
1356 for (i=0; i<250; ++i) {
1361 #endif /* CONFIG_IDE_RESET */
1363 /* ------------------------------------------------------------------------- */
1365 #if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
1367 static uchar led_buffer = 0; /* Buffer for current LED status */
1369 static void ide_led (uchar led, uchar status)
1371 uchar *led_port = LED_PORT;
1373 if (status) { /* switch LED on */
1375 } else { /* switch LED off */
1379 *led_port = led_buffer;
1382 #endif /* CONFIG_IDE_LED */
1384 /* ------------------------------------------------------------------------- */
1387 /****************************************************************************
1396 #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1398 #define AT_PRINTF(fmt,args...)
1402 /* since ATAPI may use commands with not 4 bytes alligned length
1403 * we have our own transfer functions, 2 bytes alligned */
1405 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1408 volatile ushort *pbuf;
1410 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1411 dbuf = (ushort *)sect_buf;
1413 __asm__ volatile ("eieio");
1419 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1422 volatile ushort *pbuf;
1424 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1425 dbuf = (ushort *)sect_buf;
1427 __asm__ volatile ("eieio");
1432 #else /* ! __PPC__ */
1434 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1436 outsw(ATA_DATA_REG, sect_buf, shorts);
1441 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1443 insw(ATA_DATA_REG, sect_buf, shorts);
1446 #endif /* __PPC__ */
1449 * Wait until (Status & mask) == res, or timeout (in ms)
1450 * Return last status
1451 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1452 * and then they set their DRQ Bit
1454 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1456 ulong delay = 10 * t; /* poll every 100 us */
1459 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1460 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1461 /* break if error occurs (doesn't make sense to wait more) */
1462 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1473 * issue an atapi command
1475 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1477 unsigned char c,err,mask,res;
1479 ide_led (DEVICE_LED(device), 1); /* LED on */
1483 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1485 #ifdef CONFIG_AMIGAONEG3SE
1486 # warning THF: Removed LBA mode ???
1488 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1489 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1490 if ((c & mask) != res) {
1491 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1495 /* write taskfile */
1496 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1497 ide_outb (device, ATA_SECT_CNT, 0);
1498 ide_outb (device, ATA_SECT_NUM, 0);
1499 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1500 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1501 #ifdef CONFIG_AMIGAONEG3SE
1502 # warning THF: Removed LBA mode ???
1504 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1506 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1509 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1511 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1513 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1514 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1519 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1520 /* ATAPI Command written wait for completition */
1521 udelay (5000); /* device must set bsy */
1523 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1524 /* if no data wait for DRQ = 0 BSY = 0
1525 * if data wait for DRQ = 1 BSY = 0 */
1529 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1530 if ((c & mask) != res ) {
1531 if (c & ATA_STAT_ERR) {
1532 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1533 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1535 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1540 n=ide_inb(device, ATA_CYL_HIGH);
1542 n+=ide_inb(device, ATA_CYL_LOW);
1544 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1548 if((n==0)&&(buflen<0)) {
1549 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1554 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1556 if(n!=0) { /* data transfer */
1557 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1558 /* we transfer shorts */
1560 /* ok now decide if it is an in or output */
1561 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1562 AT_PRINTF("Write to device\n");
1563 output_data_shorts(device,(unsigned short *)buffer,n);
1565 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1566 input_data_shorts(device,(unsigned short *)buffer,n);
1569 udelay(5000); /* seems that some CD ROMs need this... */
1570 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1572 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1573 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1574 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1575 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1580 ide_led (DEVICE_LED(device), 0); /* LED off */
1585 * sending the command to atapi_issue. If an status other than good
1586 * returns, an request_sense will be issued
1589 #define ATAPI_DRIVE_NOT_READY 100
1590 #define ATAPI_UNIT_ATTN 10
1592 unsigned char atapi_issue_autoreq (int device,
1595 unsigned char *buffer,
1598 unsigned char sense_data[18],sense_ccb[12];
1599 unsigned char res,key,asc,ascq;
1600 int notready,unitattn;
1602 #ifdef CONFIG_AMIGAONEG3SE
1604 unsigned int timeout, retrycnt;
1606 s = getenv("ide_cd_timeout");
1607 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1612 unitattn=ATAPI_UNIT_ATTN;
1613 notready=ATAPI_DRIVE_NOT_READY;
1616 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1618 return (0); /* Ok */
1621 return (0xFF); /* error */
1623 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1625 memset(sense_ccb,0,sizeof(sense_ccb));
1626 memset(sense_data,0,sizeof(sense_data));
1627 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1628 sense_ccb[4]=18; /* allocation Length */
1630 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1631 key=(sense_data[2]&0xF);
1632 asc=(sense_data[12]);
1633 ascq=(sense_data[13]);
1635 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1636 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1643 return 0; /* ok device ready */
1645 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1650 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1653 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1658 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1662 AT_PRINTF("Media not present\n");
1666 #ifdef CONFIG_AMIGAONEG3SE
1667 if ((sense_data[2]&0xF)==0x0B) {
1668 AT_PRINTF("ABORTED COMMAND...retry\n");
1674 if ((sense_data[2]&0xf) == 0x02 &&
1675 sense_data[12] == 0x04 &&
1676 sense_data[13] == 0x01 ) {
1677 AT_PRINTF("Waiting for unit to become active\n");
1683 #endif /* CONFIG_AMIGAONEG3SE */
1685 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1687 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1693 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1695 unsigned char ccb[12]; /* Command descriptor block */
1696 unsigned char iobuf[64]; /* temp buf */
1700 device=dev_desc->dev;
1701 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1702 dev_desc->block_read=atapi_read;
1704 memset(ccb,0,sizeof(ccb));
1705 memset(iobuf,0,sizeof(iobuf));
1707 ccb[0]=ATAPI_CMD_INQUIRY;
1708 ccb[4]=40; /* allocation Legnth */
1709 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1711 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1715 /* copy device ident strings */
1716 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1717 ident_cpy(dev_desc->product,&iobuf[16],16);
1718 ident_cpy(dev_desc->revision,&iobuf[32],5);
1723 dev_desc->type=iobuf[0] & 0x1f;
1725 if ((iobuf[1]&0x80)==0x80)
1726 dev_desc->removable = 1;
1728 dev_desc->removable = 0;
1730 memset(ccb,0,sizeof(ccb));
1731 memset(iobuf,0,sizeof(iobuf));
1732 ccb[0]=ATAPI_CMD_START_STOP;
1733 ccb[4]=0x03; /* start */
1735 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1737 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1741 memset(ccb,0,sizeof(ccb));
1742 memset(iobuf,0,sizeof(iobuf));
1743 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1745 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1749 memset(ccb,0,sizeof(ccb));
1750 memset(iobuf,0,sizeof(iobuf));
1751 ccb[0]=ATAPI_CMD_READ_CAP;
1752 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1753 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1757 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1758 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1759 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1761 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1762 ((unsigned long)iobuf[1]<<16) +
1763 ((unsigned long)iobuf[2]<< 8) +
1764 ((unsigned long)iobuf[3]);
1765 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1766 ((unsigned long)iobuf[5]<<16) +
1767 ((unsigned long)iobuf[6]<< 8) +
1768 ((unsigned long)iobuf[7]);
1775 * we transfer only one block per command, since the multiple DRQ per
1776 * command is not yet implemented
1778 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1779 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1780 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1782 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1785 unsigned char ccb[12]; /* Command descriptor block */
1788 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1789 device, blknr, blkcnt, (ulong)buffer);
1792 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1793 cnt=ATAPI_READ_MAX_BLOCK;
1797 ccb[0]=ATAPI_CMD_READ_12;
1798 ccb[1]=0; /* reserved */
1799 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1800 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1801 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1802 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1803 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1804 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1805 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1806 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1807 ccb[10]=0; /* reserved */
1808 ccb[11]=0; /* reserved */
1810 if (atapi_issue_autoreq(device,ccb,12,
1811 (unsigned char *)buffer,
1812 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1818 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1819 } while (blkcnt > 0);
1823 /* ------------------------------------------------------------------------- */
1825 #endif /* CONFIG_ATAPI */
1827 #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */