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
135 static void ide_led (uchar led, uchar status);
137 #ifndef CONFIG_AMIGAONEG3SE
138 #define ide_led(a,b) /* dummy */
140 extern void ide_led(uchar led, uchar status);
143 #define CONFIG_IDE_LED 1
144 #define DEVICE_LED(x) 1
148 #ifdef CONFIG_IDE_RESET
149 static void ide_reset (void);
151 #define ide_reset() /* dummy */
154 static void ide_ident (block_dev_desc_t *dev_desc);
155 static uchar ide_wait (int dev, ulong t);
157 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
159 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
161 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
163 static void __inline__ ide_outb(int dev, int port, unsigned char val);
164 static unsigned char __inline__ ide_inb(int dev, int port);
166 static void input_swap_data(int dev, ulong *sect_buf, int words);
168 static void input_data(int dev, ulong *sect_buf, int words);
169 static void output_data(int dev, ulong *sect_buf, int words);
170 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
174 static void atapi_inquiry(block_dev_desc_t *dev_desc);
175 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
179 #ifdef CONFIG_IDE_8xx_DIRECT
180 static void set_pcmcia_timing (int pmode);
182 #define set_pcmcia_timing(a) /* dummy */
185 /* ------------------------------------------------------------------------- */
187 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
194 printf ("Usage:\n%s\n", cmdtp->usage);
197 if (strncmp(argv[1],"res",3) == 0) {
199 #ifdef CONFIG_IDE_8xx_DIRECT
200 " on PCMCIA " PCMCIA_SLOT_MSG
206 } else if (strncmp(argv[1],"inf",3) == 0) {
211 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
212 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
213 continue; /* list only known devices */
214 printf ("IDE device %d: ", i);
215 dev_print(&ide_dev_desc[i]);
219 } else if (strncmp(argv[1],"dev",3) == 0) {
220 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
221 puts ("\nno IDE devices available\n");
224 printf ("\nIDE device %d: ", curr_device);
225 dev_print(&ide_dev_desc[curr_device]);
227 } else if (strncmp(argv[1],"part",4) == 0) {
230 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
231 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
235 print_part(&ide_dev_desc[dev]);
239 puts ("\nno IDE devices available\n");
244 printf ("Usage:\n%s\n", cmdtp->usage);
247 if (strncmp(argv[1],"dev",3) == 0) {
248 int dev = (int)simple_strtoul(argv[2], NULL, 10);
250 printf ("\nIDE device %d: ", dev);
251 if (dev >= CFG_IDE_MAXDEVICE) {
252 puts ("unknown device\n");
255 dev_print(&ide_dev_desc[dev]);
258 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
264 puts ("... is now current device\n");
267 } else if (strncmp(argv[1],"part",4) == 0) {
268 int dev = (int)simple_strtoul(argv[2], NULL, 10);
270 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
271 print_part(&ide_dev_desc[dev]);
273 printf ("\nIDE device %d not available\n", dev);
278 } else if (strncmp(argv[1],"pio",4) == 0) {
279 int mode = (int)simple_strtoul(argv[2], NULL, 10);
281 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
286 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
287 mode, IDE_MAX_PIO_MODE);
293 printf ("Usage:\n%s\n", cmdtp->usage);
296 /* at least 4 args */
298 if (strcmp(argv[1],"read") == 0) {
299 ulong addr = simple_strtoul(argv[2], NULL, 16);
300 ulong blk = simple_strtoul(argv[3], NULL, 16);
301 ulong cnt = simple_strtoul(argv[4], NULL, 16);
304 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
305 curr_device, blk, cnt);
307 n = ide_dev_desc[curr_device].block_read (curr_device,
310 /* flush cache after read */
311 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
313 printf ("%ld blocks read: %s\n",
314 n, (n==cnt) ? "OK" : "ERROR");
320 } else if (strcmp(argv[1],"write") == 0) {
321 ulong addr = simple_strtoul(argv[2], NULL, 16);
322 ulong blk = simple_strtoul(argv[3], NULL, 16);
323 ulong cnt = simple_strtoul(argv[4], NULL, 16);
326 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
327 curr_device, blk, cnt);
329 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
331 printf ("%ld blocks written: %s\n",
332 n, (n==cnt) ? "OK" : "ERROR");
339 printf ("Usage:\n%s\n", cmdtp->usage);
347 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
349 char *boot_device = NULL;
354 disk_partition_t info;
360 addr = CFG_LOAD_ADDR;
361 boot_device = getenv ("bootdevice");
364 addr = simple_strtoul(argv[1], NULL, 16);
365 boot_device = getenv ("bootdevice");
368 addr = simple_strtoul(argv[1], NULL, 16);
369 boot_device = argv[2];
372 printf ("Usage:\n%s\n", cmdtp->usage);
373 SHOW_BOOT_PROGRESS (-1);
378 puts ("\n** No boot device **\n");
379 SHOW_BOOT_PROGRESS (-1);
383 dev = simple_strtoul(boot_device, &ep, 16);
385 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
386 printf ("\n** Device %d not available\n", dev);
387 SHOW_BOOT_PROGRESS (-1);
393 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
394 SHOW_BOOT_PROGRESS (-1);
397 part = simple_strtoul(++ep, NULL, 16);
399 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
400 SHOW_BOOT_PROGRESS (-1);
403 if (strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
404 printf ("\n** Invalid partition type \"%.32s\""
405 " (expect \"" BOOT_PART_TYPE "\")\n",
407 SHOW_BOOT_PROGRESS (-1);
411 printf ("\nLoading from IDE device %d, partition %d: "
412 "Name: %.32s Type: %.32s\n",
413 dev, part, info.name, info.type);
415 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
416 info.start, info.size, info.blksz);
418 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
419 printf ("** Read error on %d:%d\n", dev, part);
420 SHOW_BOOT_PROGRESS (-1);
424 hdr = (image_header_t *)addr;
426 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
428 print_image_hdr (hdr);
430 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
431 cnt += info.blksz - 1;
435 printf("\n** Bad Magic Number **\n");
436 SHOW_BOOT_PROGRESS (-1);
440 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
441 (ulong *)(addr+info.blksz)) != cnt) {
442 printf ("** Read error on %d:%d\n", dev, part);
443 SHOW_BOOT_PROGRESS (-1);
448 /* Loading ok, update default load address */
452 /* Check if we should attempt an auto-start */
453 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
455 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
457 local_args[0] = argv[0];
458 local_args[1] = NULL;
460 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
462 do_bootm (cmdtp, 0, 1, local_args);
468 /* ------------------------------------------------------------------------- */
472 DECLARE_GLOBAL_DATA_PTR;
474 #ifdef CONFIG_IDE_8xx_DIRECT
475 volatile immap_t *immr = (immap_t *)CFG_IMMR;
476 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
480 #ifdef CONFIG_AMIGAONEG3SE
481 unsigned int max_bus_scan;
482 unsigned int ata_reset_time;
486 #ifdef CONFIG_IDE_8xx_PCCARD
487 extern int pcmcia_on (void);
491 /* initialize the PCMCIA IDE adapter card */
494 udelay (1000000); /* 1 s */
495 #endif /* CONFIG_IDE_8xx_PCCARD */
499 /* Initialize PIO timing tables */
500 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
501 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
503 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
505 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
507 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
509 " hold=%2d ns/%d clk\n",
511 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
512 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
513 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
516 /* Reset the IDE just to be sure.
517 * Light LED's to show
519 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
520 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
522 #ifdef CONFIG_IDE_8xx_DIRECT
523 /* PCMCIA / IDE initialization for common mem space */
524 pcmp->pcmc_pgcrb = 0;
527 /* start in PIO mode 0 - most relaxed timings */
529 set_pcmcia_timing (pio_mode);
532 * Wait for IDE to get ready.
533 * According to spec, this can take up to 31 seconds!
535 #ifndef CONFIG_AMIGAONEG3SE
536 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
537 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
539 s = getenv("ide_maxbus");
541 max_bus_scan = simple_strtol(s, NULL, 10);
543 max_bus_scan = CFG_IDE_MAXBUS;
545 for (bus=0; bus<max_bus_scan; ++bus) {
546 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
549 printf ("Bus %d: ", bus);
555 udelay (100000); /* 100 ms */
556 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
557 udelay (100000); /* 100 ms */
558 #ifdef CONFIG_AMIGAONEG3SE
559 ata_reset_time = ATA_RESET_TIME;
560 s = getenv("ide_reset_timeout");
561 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
565 udelay (10000); /* 10 ms */
567 c = ide_inb (dev, ATA_STATUS);
569 #ifdef CONFIG_AMIGAONEG3SE
570 if (i > (ata_reset_time * 100)) {
572 if (i > (ATA_RESET_TIME * 100)) {
574 puts ("** Timeout **\n");
575 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
576 #ifdef CONFIG_AMIGAONEG3SE
577 /* If this is the second bus, the first one was OK */
586 if ((i >= 100) && ((i%100)==0)) {
589 } while (c & ATA_STAT_BUSY);
591 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
592 puts ("not available ");
593 PRINTF ("Status = 0x%02X ", c);
594 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
595 } else if ((c & ATA_STAT_READY) == 0) {
596 puts ("not available ");
597 PRINTF ("Status = 0x%02X ", c);
606 #ifdef CONFIG_AMIGAONEG3SE
611 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
614 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
615 #ifdef CONFIG_IDE_LED
616 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
618 ide_dev_desc[i].if_type=IF_TYPE_IDE;
619 ide_dev_desc[i].dev=i;
620 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
621 ide_dev_desc[i].blksz=0;
622 ide_dev_desc[i].lba=0;
623 ide_dev_desc[i].block_read=ide_read;
624 if (!ide_bus_ok[IDE_BUS(i)])
626 ide_led (led, 1); /* LED on */
627 ide_ident(&ide_dev_desc[i]);
628 ide_led (led, 0); /* LED off */
629 dev_print(&ide_dev_desc[i]);
631 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
632 init_part (&ide_dev_desc[i]); /* initialize partition type */
640 /* ------------------------------------------------------------------------- */
642 block_dev_desc_t * ide_get_dev(int dev)
644 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
648 #ifdef CONFIG_IDE_8xx_DIRECT
651 set_pcmcia_timing (int pmode)
653 volatile immap_t *immr = (immap_t *)CFG_IMMR;
654 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
657 PRINTF ("Set timing for PIO Mode %d\n", pmode);
659 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
660 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
661 | PCMCIA_SL (pio_config_clk[pmode].t_length)
666 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
667 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
668 #if (CFG_PCMCIA_POR0 != 0)
672 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
674 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
675 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
676 #if (CFG_PCMCIA_POR1 != 0)
680 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
682 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
683 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
684 #if (CFG_PCMCIA_POR2 != 0)
688 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
690 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
691 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
692 #if (CFG_PCMCIA_POR3 != 0)
696 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
700 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
701 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
702 #if (CFG_PCMCIA_POR4 != 0)
706 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
708 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
709 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
710 #if (CFG_PCMCIA_POR5 != 0)
714 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
716 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
717 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
718 #if (CFG_PCMCIA_POR6 != 0)
722 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
724 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
725 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
726 #if (CFG_PCMCIA_POR7 != 0)
730 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
734 #endif /* CONFIG_IDE_8xx_DIRECT */
736 /* ------------------------------------------------------------------------- */
739 static void __inline__
740 ide_outb(int dev, int port, unsigned char val)
742 /* Ensure I/O operations complete */
743 __asm__ volatile("eieio");
744 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
746 printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
749 #else /* ! __PPC__ */
750 static void __inline__
751 ide_outb(int dev, int port, unsigned char val)
759 static unsigned char __inline__
760 ide_inb(int dev, int port)
763 /* Ensure I/O operations complete */
764 __asm__ volatile("eieio");
765 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
767 printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
771 #else /* ! __PPC__ */
772 static unsigned char __inline__
773 ide_inb(int dev, int port)
780 __inline__ unsigned ld_le16(const volatile unsigned short *addr)
784 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r"(val) : "r"(addr), "m"(*addr));
788 #ifdef CONFIG_AMIGAONEG3SE
790 output_data_short(int dev, ulong *sect_buf, int words)
793 volatile ushort *pbuf;
795 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
796 dbuf = (ushort *)sect_buf;
798 __asm__ volatile ("eieio");
800 __asm__ volatile ("eieio");
809 input_swap_data(int dev, ulong *sect_buf, int words)
811 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
812 ushort *dbuf = (ushort *)sect_buf;
815 *dbuf++ = ld_le16(pbuf);
816 *dbuf++ = ld_le16(pbuf);
819 #else /* ! __PPC__ */
820 #define input_swap_data(x,y,z) input_data(x,y,z)
828 output_data(int dev, ulong *sect_buf, int words)
831 volatile ushort *pbuf;
833 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
834 dbuf = (ushort *)sect_buf;
836 __asm__ volatile ("eieio");
838 __asm__ volatile ("eieio");
842 #else /* ! __PPC__ */
844 output_data(int dev, ulong *sect_buf, int words)
846 outsw(ATA_DATA_REG, sect_buf, words<<1);
852 input_data(int dev, ulong *sect_buf, int words)
855 volatile ushort *pbuf;
857 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
858 dbuf = (ushort *)sect_buf;
860 __asm__ volatile ("eieio");
862 __asm__ volatile ("eieio");
866 #else /* ! __PPC__ */
868 input_data(int dev, ulong *sect_buf, int words)
870 insw(ATA_DATA_REG, sect_buf, words << 1);
875 #ifdef CONFIG_AMIGAONEG3SE
877 input_data_short(int dev, ulong *sect_buf, int words)
880 volatile ushort *pbuf;
882 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
883 dbuf = (ushort *)sect_buf;
885 __asm__ volatile ("eieio");
887 __asm__ volatile ("eieio");
898 /* -------------------------------------------------------------------------
900 static void ide_ident (block_dev_desc_t *dev_desc)
902 ulong iobuf[ATA_SECTORWORDS];
904 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
906 #ifdef CONFIG_AMIGAONEG3SE
914 int mode, cycle_time;
917 device=dev_desc->dev;
918 printf (" Device %d: ", device);
920 #ifdef CONFIG_AMIGAONEG3SE
921 s = getenv("ide_maxbus");
923 max_bus_scan = simple_strtol(s, NULL, 10);
925 max_bus_scan = CFG_IDE_MAXBUS;
927 if (device >= max_bus_scan*2) {
928 dev_desc->type=DEV_TYPE_UNKNOWN;
933 ide_led (DEVICE_LED(device), 1); /* LED on */
936 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
937 dev_desc->if_type=IF_TYPE_IDE;
940 #ifdef CONFIG_AMIGAONEG3SE
944 /* Warning: This will be tricky to read */
947 #endif /* CONFIG_AMIGAONEG3SE */
949 /* check signature */
950 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
951 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
952 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
953 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
954 /* ATAPI Signature found */
955 dev_desc->if_type=IF_TYPE_ATAPI;
956 /* Start Ident Command
958 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
960 * Wait for completion - ATAPI devices need more time
963 c = ide_wait (device, ATAPI_TIME_OUT);
968 /* Start Ident Command
970 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
972 /* Wait for completion
974 c = ide_wait (device, IDE_TIME_OUT);
976 ide_led (DEVICE_LED(device), 0); /* LED off */
978 if (((c & ATA_STAT_DRQ) == 0) ||
979 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
980 #ifdef CONFIG_AMIGAONEG3SE
984 dev_desc->type=DEV_TYPE_UNKNOWN;
988 dev_desc->type=DEV_TYPE_UNKNOWN;
990 #endif /* CONFIG_AMIGAONEG3SE */
993 #ifdef CONFIG_AMIGAONEG3SE
994 s = getenv("ide_doreset");
995 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
996 /* Need to soft reset the device in case it's an ATAPI... */
997 PRINTF("Retrying...\n");
998 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1000 ide_outb (device, ATA_COMMAND, 0x08);
1001 udelay (100000); /* 100 ms */
1006 } /* see above - ugly to read */
1007 #endif /* CONFIG_AMIGAONEG3SE */
1009 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1011 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1012 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1013 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1015 if ((iop->config & 0x0080)==0x0080)
1016 dev_desc->removable = 1;
1018 dev_desc->removable = 0;
1022 * Drive PIO mode autoselection
1026 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1027 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1029 PRINTF ("Override tPIO -> 2\n");
1031 if (iop->field_valid & 2) { /* drive implements ATA2? */
1032 PRINTF ("Drive implements ATA2\n");
1033 if (iop->capability & 8) { /* drive supports use_iordy? */
1034 cycle_time = iop->eide_pio_iordy;
1036 cycle_time = iop->eide_pio;
1038 PRINTF ("cycle time = %d\n", cycle_time);
1040 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1041 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1042 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1043 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1045 printf ("PIO mode to use: PIO %d\n", mode);
1049 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1050 atapi_inquiry(dev_desc);
1053 #endif /* CONFIG_ATAPI */
1056 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1058 dev_desc->type=DEV_TYPE_HARDDISK;
1059 dev_desc->blksz=ATA_BLOCKSIZE;
1060 dev_desc->lun=0; /* just to fill something in... */
1062 #if 0 /* only used to test the powersaving mode,
1063 * if enabled, the drive goes after 5 sec
1064 * in standby mode */
1065 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1066 c = ide_wait (device, IDE_TIME_OUT);
1067 ide_outb (device, ATA_SECT_CNT, 1);
1068 ide_outb (device, ATA_LBA_LOW, 0);
1069 ide_outb (device, ATA_LBA_MID, 0);
1070 ide_outb (device, ATA_LBA_HIGH, 0);
1071 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1072 ATA_DEVICE(device));
1073 ide_outb (device, ATA_COMMAND, 0xe3);
1075 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1080 /* ------------------------------------------------------------------------- */
1082 ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1086 unsigned char pwrsave=0; /* power save */
1088 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1089 device, blknr, blkcnt, (ulong)buffer);
1091 ide_led (DEVICE_LED(device), 1); /* LED on */
1095 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1096 c = ide_wait (device, IDE_TIME_OUT);
1098 if (c & ATA_STAT_BUSY) {
1099 printf ("IDE read: device %d not ready\n", device);
1103 /* first check if the drive is in Powersaving mode, if yes,
1104 * increase the timeout value */
1105 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1108 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1110 if (c & ATA_STAT_BUSY) {
1111 printf ("IDE read: device %d not ready\n", device);
1114 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1115 printf ("No Powersaving mode %X\n", c);
1117 c = ide_inb(device,ATA_SECT_CNT);
1118 PRINTF("Powersaving %02X\n",c);
1124 while (blkcnt-- > 0) {
1126 c = ide_wait (device, IDE_TIME_OUT);
1128 if (c & ATA_STAT_BUSY) {
1129 printf ("IDE read: device %d not ready\n", device);
1133 ide_outb (device, ATA_SECT_CNT, 1);
1134 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1135 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1136 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1137 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1138 ATA_DEVICE(device) |
1139 ((blknr >> 24) & 0xF) );
1140 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1145 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1148 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1151 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1152 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1157 input_data (device, buffer, ATA_SECTORWORDS);
1158 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1162 buffer += ATA_SECTORWORDS;
1165 ide_led (DEVICE_LED(device), 0); /* LED off */
1169 /* ------------------------------------------------------------------------- */
1172 ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1177 ide_led (DEVICE_LED(device), 1); /* LED on */
1181 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1183 while (blkcnt-- > 0) {
1185 c = ide_wait (device, IDE_TIME_OUT);
1187 if (c & ATA_STAT_BUSY) {
1188 printf ("IDE read: device %d not ready\n", device);
1192 ide_outb (device, ATA_SECT_CNT, 1);
1193 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1194 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1195 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1196 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1197 ATA_DEVICE(device) |
1198 ((blknr >> 24) & 0xF) );
1199 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1203 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1205 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1206 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1211 output_data (device, buffer, ATA_SECTORWORDS);
1212 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1215 buffer += ATA_SECTORWORDS;
1218 ide_led (DEVICE_LED(device), 0); /* LED off */
1222 /* ------------------------------------------------------------------------- */
1225 * copy src to dest, skipping leading and trailing blanks and null
1226 * terminate the string
1228 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1234 if (src[start]!=' ')
1244 for ( ; start<=end; start++) {
1250 /* ------------------------------------------------------------------------- */
1253 * Wait until Busy bit is off, or timeout (in ms)
1254 * Return last status
1256 static uchar ide_wait (int dev, ulong t)
1258 ulong delay = 10 * t; /* poll every 100 us */
1261 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1270 /* ------------------------------------------------------------------------- */
1272 #ifdef CONFIG_IDE_RESET
1273 extern void ide_set_reset(int idereset);
1275 static void ide_reset (void)
1277 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1278 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1283 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1285 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1286 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1288 ide_set_reset (1); /* assert reset */
1292 #ifdef CFG_PB_12V_ENABLE
1293 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1294 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1295 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1296 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1298 /* wait 500 ms for the voltage to stabilize
1300 for (i=0; i<500; ++i) {
1304 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1305 #endif /* CFG_PB_12V_ENABLE */
1307 #ifdef CFG_PB_IDE_MOTOR
1308 /* configure IDE Motor voltage monitor pin as input */
1309 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1310 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1311 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1313 /* wait up to 1 s for the motor voltage to stabilize
1315 for (i=0; i<1000; ++i) {
1316 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1322 if (i == 1000) { /* Timeout */
1323 printf ("\nWarning: 5V for IDE Motor missing\n");
1324 # ifdef CONFIG_STATUS_LED
1325 # ifdef STATUS_LED_YELLOW
1326 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1328 # ifdef STATUS_LED_GREEN
1329 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1331 # endif /* CONFIG_STATUS_LED */
1333 #endif /* CFG_PB_IDE_MOTOR */
1337 /* de-assert RESET signal */
1341 for (i=0; i<250; ++i) {
1346 #endif /* CONFIG_IDE_RESET */
1348 /* ------------------------------------------------------------------------- */
1350 #if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE)
1352 static uchar led_buffer = 0; /* Buffer for current LED status */
1354 static void ide_led (uchar led, uchar status)
1356 uchar *led_port = LED_PORT;
1358 if (status) { /* switch LED on */
1360 } else { /* switch LED off */
1364 *led_port = led_buffer;
1367 #endif /* CONFIG_IDE_LED */
1369 /* ------------------------------------------------------------------------- */
1372 /****************************************************************************
1381 #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1383 #define AT_PRINTF(fmt,args...)
1387 /* since ATAPI may use commands with not 4 bytes alligned length
1388 * we have our own transfer functions, 2 bytes alligned */
1390 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1393 volatile ushort *pbuf;
1395 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1396 dbuf = (ushort *)sect_buf;
1398 __asm__ volatile ("eieio");
1404 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1407 volatile ushort *pbuf;
1409 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1410 dbuf = (ushort *)sect_buf;
1412 __asm__ volatile ("eieio");
1417 #else /* ! __PPC__ */
1419 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1421 outsw(ATA_DATA_REG, sect_buf, shorts);
1426 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1428 insw(ATA_DATA_REG, sect_buf, shorts);
1431 #endif /* __PPC__ */
1434 * Wait until (Status & mask) == res, or timeout (in ms)
1435 * Return last status
1436 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1437 * and then they set their DRQ Bit
1439 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1441 ulong delay = 10 * t; /* poll every 100 us */
1444 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1445 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1446 /* break if error occurs (doesn't make sense to wait more) */
1447 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1458 * issue an atapi command
1460 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1462 unsigned char c,err,mask,res;
1464 ide_led (DEVICE_LED(device), 1); /* LED on */
1468 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1470 #ifdef CONFIG_AMIGAONEG3SE
1471 # warning THF: Removed LBA mode ???
1473 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1474 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1475 if ((c & mask) != res) {
1476 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1480 /* write taskfile */
1481 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1482 ide_outb (device, ATA_SECT_CNT, 0);
1483 ide_outb (device, ATA_SECT_NUM, 0);
1484 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1485 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1486 #ifdef CONFIG_AMIGAONEG3SE
1487 # warning THF: Removed LBA mode ???
1489 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1491 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1494 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1496 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1498 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1499 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1504 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1505 /* ATAPI Command written wait for completition */
1506 udelay (5000); /* device must set bsy */
1508 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1509 /* if no data wait for DRQ = 0 BSY = 0
1510 * if data wait for DRQ = 1 BSY = 0 */
1514 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1515 if ((c & mask) != res ) {
1516 if (c & ATA_STAT_ERR) {
1517 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1518 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1520 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1525 n=ide_inb(device, ATA_CYL_HIGH);
1527 n+=ide_inb(device, ATA_CYL_LOW);
1529 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1533 if((n==0)&&(buflen<0)) {
1534 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1539 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1541 if(n!=0) { /* data transfer */
1542 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1543 /* we transfer shorts */
1545 /* ok now decide if it is an in or output */
1546 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1547 AT_PRINTF("Write to device\n");
1548 output_data_shorts(device,(unsigned short *)buffer,n);
1550 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1551 input_data_shorts(device,(unsigned short *)buffer,n);
1554 udelay(5000); /* seems that some CD ROMs need this... */
1555 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1557 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1558 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1559 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1560 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1565 ide_led (DEVICE_LED(device), 0); /* LED off */
1570 * sending the command to atapi_issue. If an status other than good
1571 * returns, an request_sense will be issued
1574 #define ATAPI_DRIVE_NOT_READY 100
1575 #define ATAPI_UNIT_ATTN 10
1577 unsigned char atapi_issue_autoreq (int device,
1580 unsigned char *buffer,
1583 unsigned char sense_data[18],sense_ccb[12];
1584 unsigned char res,key,asc,ascq;
1585 int notready,unitattn;
1587 #ifdef CONFIG_AMIGAONEG3SE
1589 unsigned int timeout, retrycnt;
1591 s = getenv("ide_cd_timeout");
1592 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1597 unitattn=ATAPI_UNIT_ATTN;
1598 notready=ATAPI_DRIVE_NOT_READY;
1601 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1603 return (0); /* Ok */
1606 return (0xFF); /* error */
1608 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1610 memset(sense_ccb,0,sizeof(sense_ccb));
1611 memset(sense_data,0,sizeof(sense_data));
1612 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1613 sense_ccb[4]=18; /* allocation Length */
1615 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1616 key=(sense_data[2]&0xF);
1617 asc=(sense_data[12]);
1618 ascq=(sense_data[13]);
1620 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1621 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1628 return 0; /* ok device ready */
1630 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1635 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1638 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1643 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1647 AT_PRINTF("Media not present\n");
1651 #ifdef CONFIG_AMIGAONEG3SE
1652 if ((sense_data[2]&0xF)==0x0B) {
1653 AT_PRINTF("ABORTED COMMAND...retry\n");
1659 if ((sense_data[2]&0xf) == 0x02 &&
1660 sense_data[12] == 0x04 &&
1661 sense_data[13] == 0x01 ) {
1662 AT_PRINTF("Waiting for unit to become active\n");
1668 #endif /* CONFIG_AMIGAONEG3SE */
1670 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1672 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1678 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1680 unsigned char ccb[12]; /* Command descriptor block */
1681 unsigned char iobuf[64]; /* temp buf */
1685 device=dev_desc->dev;
1686 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1687 dev_desc->block_read=atapi_read;
1689 memset(ccb,0,sizeof(ccb));
1690 memset(iobuf,0,sizeof(iobuf));
1692 ccb[0]=ATAPI_CMD_INQUIRY;
1693 ccb[4]=40; /* allocation Legnth */
1694 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1696 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1700 /* copy device ident strings */
1701 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1702 ident_cpy(dev_desc->product,&iobuf[16],16);
1703 ident_cpy(dev_desc->revision,&iobuf[32],5);
1708 dev_desc->type=iobuf[0] & 0x1f;
1710 if ((iobuf[1]&0x80)==0x80)
1711 dev_desc->removable = 1;
1713 dev_desc->removable = 0;
1715 memset(ccb,0,sizeof(ccb));
1716 memset(iobuf,0,sizeof(iobuf));
1717 ccb[0]=ATAPI_CMD_START_STOP;
1718 ccb[4]=0x03; /* start */
1720 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1722 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1726 memset(ccb,0,sizeof(ccb));
1727 memset(iobuf,0,sizeof(iobuf));
1728 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1730 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1734 memset(ccb,0,sizeof(ccb));
1735 memset(iobuf,0,sizeof(iobuf));
1736 ccb[0]=ATAPI_CMD_READ_CAP;
1737 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1738 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1742 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1743 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1744 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1746 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1747 ((unsigned long)iobuf[1]<<16) +
1748 ((unsigned long)iobuf[2]<< 8) +
1749 ((unsigned long)iobuf[3]);
1750 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1751 ((unsigned long)iobuf[5]<<16) +
1752 ((unsigned long)iobuf[6]<< 8) +
1753 ((unsigned long)iobuf[7]);
1760 * we transfer only one block per command, since the multiple DRQ per
1761 * command is not yet implemented
1763 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1764 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1765 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1767 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1770 unsigned char ccb[12]; /* Command descriptor block */
1773 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1774 device, blknr, blkcnt, (ulong)buffer);
1777 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1778 cnt=ATAPI_READ_MAX_BLOCK;
1782 ccb[0]=ATAPI_CMD_READ_12;
1783 ccb[1]=0; /* reserved */
1784 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1785 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1786 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1787 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1788 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1789 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1790 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1791 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1792 ccb[10]=0; /* reserved */
1793 ccb[11]=0; /* reserved */
1795 if (atapi_issue_autoreq(device,ccb,12,
1796 (unsigned char *)buffer,
1797 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1803 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1804 } while (blkcnt > 0);
1808 /* ------------------------------------------------------------------------- */
1810 #endif /* CONFIG_ATAPI */
1812 #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */