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>
49 #ifdef CONFIG_SHOW_BOOT_PROGRESS
50 # include <status_led.h>
51 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
53 # define SHOW_BOOT_PROGRESS(arg)
61 #define PRINTF(fmt,args...) printf (fmt ,##args)
63 #define PRINTF(fmt,args...)
66 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
68 /* Timings for IDE Interface
70 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
71 * 70 165 30 PIO-Mode 0, [ns]
73 * 50 125 20 PIO-Mode 1, [ns]
75 * 30 100 15 PIO-Mode 2, [ns]
77 * 30 80 10 PIO-Mode 3, [ns]
79 * 25 70 10 PIO-Mode 4, [ns]
83 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
85 /* Setup Length Hold */
86 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
87 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
88 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
89 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
90 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
93 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
96 #define CFG_PIO_MODE 0 /* use a relaxed default */
98 static int pio_mode = CFG_PIO_MODE;
100 /* Make clock cycles and always round up */
102 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
104 /* ------------------------------------------------------------------------- */
106 /* Current I/O Device */
107 static int curr_device = -1;
109 /* Current offset for IDE0 / IDE1 bus access */
110 ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
111 #if defined(CFG_ATA_IDE0_OFFSET)
114 #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
120 #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
123 #ifndef CONFIG_AMIGAONEG3SE
124 static int ide_bus_ok[CFG_IDE_MAXBUS];
126 static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
129 static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
130 /* ------------------------------------------------------------------------- */
132 #ifdef CONFIG_IDE_LED
134 static void ide_led (uchar led, uchar status);
136 extern void ide_led (uchar led, uchar status);
139 #ifndef CONFIG_AMIGAONEG3SE
140 #define ide_led(a,b) /* dummy */
142 extern void ide_led(uchar led, uchar status);
145 #define CONFIG_IDE_LED 1
146 #define DEVICE_LED(x) 1
150 #ifdef CONFIG_IDE_RESET
151 static void ide_reset (void);
153 #define ide_reset() /* dummy */
156 static void ide_ident (block_dev_desc_t *dev_desc);
157 static uchar ide_wait (int dev, ulong t);
159 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
161 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
163 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
165 static void __inline__ ide_outb(int dev, int port, unsigned char val);
166 static unsigned char __inline__ ide_inb(int dev, int port);
168 static void input_swap_data(int dev, ulong *sect_buf, int words);
170 static void input_data(int dev, ulong *sect_buf, int words);
171 static void output_data(int dev, ulong *sect_buf, int words);
172 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
176 static void atapi_inquiry(block_dev_desc_t *dev_desc);
177 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
181 #ifdef CONFIG_IDE_8xx_DIRECT
182 static void set_pcmcia_timing (int pmode);
184 #define set_pcmcia_timing(a) /* dummy */
187 /* ------------------------------------------------------------------------- */
189 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
196 printf ("Usage:\n%s\n", cmdtp->usage);
199 if (strncmp(argv[1],"res",3) == 0) {
201 #ifdef CONFIG_IDE_8xx_DIRECT
202 " on PCMCIA " PCMCIA_SLOT_MSG
208 } else if (strncmp(argv[1],"inf",3) == 0) {
213 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
214 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
215 continue; /* list only known devices */
216 printf ("IDE device %d: ", i);
217 dev_print(&ide_dev_desc[i]);
221 } else if (strncmp(argv[1],"dev",3) == 0) {
222 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
223 puts ("\nno IDE devices available\n");
226 printf ("\nIDE device %d: ", curr_device);
227 dev_print(&ide_dev_desc[curr_device]);
229 } else if (strncmp(argv[1],"part",4) == 0) {
232 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
233 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
237 print_part(&ide_dev_desc[dev]);
241 puts ("\nno IDE devices available\n");
246 printf ("Usage:\n%s\n", cmdtp->usage);
249 if (strncmp(argv[1],"dev",3) == 0) {
250 int dev = (int)simple_strtoul(argv[2], NULL, 10);
252 printf ("\nIDE device %d: ", dev);
253 if (dev >= CFG_IDE_MAXDEVICE) {
254 puts ("unknown device\n");
257 dev_print(&ide_dev_desc[dev]);
260 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
266 puts ("... is now current device\n");
269 } else if (strncmp(argv[1],"part",4) == 0) {
270 int dev = (int)simple_strtoul(argv[2], NULL, 10);
272 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
273 print_part(&ide_dev_desc[dev]);
275 printf ("\nIDE device %d not available\n", dev);
280 } else if (strncmp(argv[1],"pio",4) == 0) {
281 int mode = (int)simple_strtoul(argv[2], NULL, 10);
283 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
288 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
289 mode, IDE_MAX_PIO_MODE);
295 printf ("Usage:\n%s\n", cmdtp->usage);
298 /* at least 4 args */
300 if (strcmp(argv[1],"read") == 0) {
301 ulong addr = simple_strtoul(argv[2], NULL, 16);
302 ulong blk = simple_strtoul(argv[3], NULL, 16);
303 ulong cnt = simple_strtoul(argv[4], NULL, 16);
306 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
307 curr_device, blk, cnt);
309 n = ide_dev_desc[curr_device].block_read (curr_device,
312 /* flush cache after read */
313 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
315 printf ("%ld blocks read: %s\n",
316 n, (n==cnt) ? "OK" : "ERROR");
322 } else if (strcmp(argv[1],"write") == 0) {
323 ulong addr = simple_strtoul(argv[2], NULL, 16);
324 ulong blk = simple_strtoul(argv[3], NULL, 16);
325 ulong cnt = simple_strtoul(argv[4], NULL, 16);
328 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
329 curr_device, blk, cnt);
331 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
333 printf ("%ld blocks written: %s\n",
334 n, (n==cnt) ? "OK" : "ERROR");
341 printf ("Usage:\n%s\n", cmdtp->usage);
349 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
351 char *boot_device = NULL;
356 disk_partition_t info;
362 addr = CFG_LOAD_ADDR;
363 boot_device = getenv ("bootdevice");
366 addr = simple_strtoul(argv[1], NULL, 16);
367 boot_device = getenv ("bootdevice");
370 addr = simple_strtoul(argv[1], NULL, 16);
371 boot_device = argv[2];
374 printf ("Usage:\n%s\n", cmdtp->usage);
375 SHOW_BOOT_PROGRESS (-1);
380 puts ("\n** No boot device **\n");
381 SHOW_BOOT_PROGRESS (-1);
385 dev = simple_strtoul(boot_device, &ep, 16);
387 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
388 printf ("\n** Device %d not available\n", dev);
389 SHOW_BOOT_PROGRESS (-1);
395 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
396 SHOW_BOOT_PROGRESS (-1);
399 part = simple_strtoul(++ep, NULL, 16);
401 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
402 SHOW_BOOT_PROGRESS (-1);
405 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
406 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
407 printf ("\n** Invalid partition type \"%.32s\""
408 " (expect \"" BOOT_PART_TYPE "\")\n",
410 SHOW_BOOT_PROGRESS (-1);
414 printf ("\nLoading from IDE device %d, partition %d: "
415 "Name: %.32s Type: %.32s\n",
416 dev, part, info.name, info.type);
418 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
419 info.start, info.size, info.blksz);
421 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
422 printf ("** Read error on %d:%d\n", dev, part);
423 SHOW_BOOT_PROGRESS (-1);
427 hdr = (image_header_t *)addr;
429 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
431 print_image_hdr (hdr);
433 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
434 cnt += info.blksz - 1;
438 printf("\n** Bad Magic Number **\n");
439 SHOW_BOOT_PROGRESS (-1);
443 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
444 (ulong *)(addr+info.blksz)) != cnt) {
445 printf ("** Read error on %d:%d\n", dev, part);
446 SHOW_BOOT_PROGRESS (-1);
451 /* Loading ok, update default load address */
455 /* Check if we should attempt an auto-start */
456 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
458 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
460 local_args[0] = argv[0];
461 local_args[1] = NULL;
463 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
465 do_bootm (cmdtp, 0, 1, local_args);
471 /* ------------------------------------------------------------------------- */
475 DECLARE_GLOBAL_DATA_PTR;
477 #ifdef CONFIG_IDE_8xx_DIRECT
478 volatile immap_t *immr = (immap_t *)CFG_IMMR;
479 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
483 #ifdef CONFIG_AMIGAONEG3SE
484 unsigned int max_bus_scan;
485 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() */
495 ide_devices_found = 0;
496 /* initialize the PCMCIA IDE adapter card */
498 if (!ide_devices_found)
500 udelay (1000000); /* 1 s */
501 #endif /* CONFIG_IDE_8xx_PCCARD */
505 /* Initialize PIO timing tables */
506 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
507 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
509 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
511 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
513 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
515 " hold=%2d ns/%d clk\n",
517 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
518 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
519 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
522 /* Reset the IDE just to be sure.
523 * Light LED's to show
525 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
526 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
528 #ifdef CONFIG_IDE_8xx_DIRECT
529 /* PCMCIA / IDE initialization for common mem space */
530 pcmp->pcmc_pgcrb = 0;
533 /* start in PIO mode 0 - most relaxed timings */
535 set_pcmcia_timing (pio_mode);
538 * Wait for IDE to get ready.
539 * According to spec, this can take up to 31 seconds!
541 #ifndef CONFIG_AMIGAONEG3SE
542 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
543 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
545 s = getenv("ide_maxbus");
547 max_bus_scan = simple_strtol(s, NULL, 10);
549 max_bus_scan = CFG_IDE_MAXBUS;
551 for (bus=0; bus<max_bus_scan; ++bus) {
552 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
555 #ifdef CONFIG_IDE_8xx_PCCARD
556 /* Skip non-ide devices from probing */
557 if ((ide_devices_found & (1 << bus)) == 0) {
558 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
562 printf ("Bus %d: ", bus);
568 udelay (100000); /* 100 ms */
569 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
570 udelay (100000); /* 100 ms */
571 #ifdef CONFIG_AMIGAONEG3SE
572 ata_reset_time = ATA_RESET_TIME;
573 s = getenv("ide_reset_timeout");
574 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
578 udelay (10000); /* 10 ms */
580 c = ide_inb (dev, ATA_STATUS);
582 #ifdef CONFIG_AMIGAONEG3SE
583 if (i > (ata_reset_time * 100)) {
585 if (i > (ATA_RESET_TIME * 100)) {
587 puts ("** Timeout **\n");
588 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
589 #ifdef CONFIG_AMIGAONEG3SE
590 /* If this is the second bus, the first one was OK */
599 if ((i >= 100) && ((i%100)==0)) {
602 } while (c & ATA_STAT_BUSY);
604 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
605 puts ("not available ");
606 PRINTF ("Status = 0x%02X ", c);
607 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
608 } else if ((c & ATA_STAT_READY) == 0) {
609 puts ("not available ");
610 PRINTF ("Status = 0x%02X ", c);
619 #ifdef CONFIG_AMIGAONEG3SE
624 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
627 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
628 #ifdef CONFIG_IDE_LED
629 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
631 ide_dev_desc[i].if_type=IF_TYPE_IDE;
632 ide_dev_desc[i].dev=i;
633 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
634 ide_dev_desc[i].blksz=0;
635 ide_dev_desc[i].lba=0;
636 ide_dev_desc[i].block_read=ide_read;
637 if (!ide_bus_ok[IDE_BUS(i)])
639 ide_led (led, 1); /* LED on */
640 ide_ident(&ide_dev_desc[i]);
641 ide_led (led, 0); /* LED off */
642 dev_print(&ide_dev_desc[i]);
644 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
645 init_part (&ide_dev_desc[i]); /* initialize partition type */
653 /* ------------------------------------------------------------------------- */
655 block_dev_desc_t * ide_get_dev(int dev)
657 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
661 #ifdef CONFIG_IDE_8xx_DIRECT
664 set_pcmcia_timing (int pmode)
666 volatile immap_t *immr = (immap_t *)CFG_IMMR;
667 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
670 PRINTF ("Set timing for PIO Mode %d\n", pmode);
672 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
673 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
674 | PCMCIA_SL (pio_config_clk[pmode].t_length)
679 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
680 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
681 #if (CFG_PCMCIA_POR0 != 0)
685 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
687 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
688 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
689 #if (CFG_PCMCIA_POR1 != 0)
693 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
695 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
696 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
697 #if (CFG_PCMCIA_POR2 != 0)
701 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
703 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
704 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
705 #if (CFG_PCMCIA_POR3 != 0)
709 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
713 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
714 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
715 #if (CFG_PCMCIA_POR4 != 0)
719 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
721 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
722 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
723 #if (CFG_PCMCIA_POR5 != 0)
727 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
729 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
730 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
731 #if (CFG_PCMCIA_POR6 != 0)
735 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
737 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
738 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
739 #if (CFG_PCMCIA_POR7 != 0)
743 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
747 #endif /* CONFIG_IDE_8xx_DIRECT */
749 /* ------------------------------------------------------------------------- */
752 static void __inline__
753 ide_outb(int dev, int port, unsigned char val)
755 /* Ensure I/O operations complete */
756 __asm__ volatile("eieio");
757 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
759 printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
762 #else /* ! __PPC__ */
763 static void __inline__
764 ide_outb(int dev, int port, unsigned char val)
772 static unsigned char __inline__
773 ide_inb(int dev, int port)
776 /* Ensure I/O operations complete */
777 __asm__ volatile("eieio");
778 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
780 printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
784 #else /* ! __PPC__ */
785 static unsigned char __inline__
786 ide_inb(int dev, int port)
793 # ifdef CONFIG_AMIGAONEG3SE
795 output_data_short(int dev, ulong *sect_buf, int words)
798 volatile ushort *pbuf;
800 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
801 dbuf = (ushort *)sect_buf;
803 __asm__ volatile ("eieio");
805 __asm__ volatile ("eieio");
811 # endif /* CONFIG_AMIGAONEG3SE */
814 input_swap_data(int dev, ulong *sect_buf, int words)
816 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
817 ushort *dbuf = (ushort *)sect_buf;
820 *dbuf++ = ld_le16(pbuf);
821 *dbuf++ = ld_le16(pbuf);
824 #else /* ! __PPC__ */
825 #define input_swap_data(x,y,z) input_data(x,y,z)
831 output_data(int dev, ulong *sect_buf, int words)
834 volatile ushort *pbuf;
836 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
837 dbuf = (ushort *)sect_buf;
839 __asm__ volatile ("eieio");
841 __asm__ volatile ("eieio");
845 #else /* ! __PPC__ */
847 output_data(int dev, ulong *sect_buf, int words)
849 outsw(ATA_DATA_REG, sect_buf, words<<1);
855 input_data(int dev, ulong *sect_buf, int words)
858 volatile ushort *pbuf;
860 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
861 dbuf = (ushort *)sect_buf;
863 __asm__ volatile ("eieio");
865 __asm__ volatile ("eieio");
869 #else /* ! __PPC__ */
871 input_data(int dev, ulong *sect_buf, int words)
873 insw(ATA_DATA_REG, sect_buf, words << 1);
878 #ifdef CONFIG_AMIGAONEG3SE
880 input_data_short(int dev, ulong *sect_buf, int words)
883 volatile ushort *pbuf;
885 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
886 dbuf = (ushort *)sect_buf;
888 __asm__ volatile ("eieio");
890 __asm__ volatile ("eieio");
901 /* -------------------------------------------------------------------------
903 static void ide_ident (block_dev_desc_t *dev_desc)
905 ulong iobuf[ATA_SECTORWORDS];
907 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
909 #ifdef CONFIG_AMIGAONEG3SE
917 int mode, cycle_time;
920 device=dev_desc->dev;
921 printf (" Device %d: ", device);
923 #ifdef CONFIG_AMIGAONEG3SE
924 s = getenv("ide_maxbus");
926 max_bus_scan = simple_strtol(s, NULL, 10);
928 max_bus_scan = CFG_IDE_MAXBUS;
930 if (device >= max_bus_scan*2) {
931 dev_desc->type=DEV_TYPE_UNKNOWN;
936 ide_led (DEVICE_LED(device), 1); /* LED on */
939 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
940 dev_desc->if_type=IF_TYPE_IDE;
943 #ifdef CONFIG_AMIGAONEG3SE
947 /* Warning: This will be tricky to read */
950 #endif /* CONFIG_AMIGAONEG3SE */
952 /* check signature */
953 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
954 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
955 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
956 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
957 /* ATAPI Signature found */
958 dev_desc->if_type=IF_TYPE_ATAPI;
959 /* Start Ident Command
961 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
963 * Wait for completion - ATAPI devices need more time
966 c = ide_wait (device, ATAPI_TIME_OUT);
971 /* Start Ident Command
973 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
975 /* Wait for completion
977 c = ide_wait (device, IDE_TIME_OUT);
979 ide_led (DEVICE_LED(device), 0); /* LED off */
981 if (((c & ATA_STAT_DRQ) == 0) ||
982 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
983 #ifdef CONFIG_AMIGAONEG3SE
987 dev_desc->type=DEV_TYPE_UNKNOWN;
991 dev_desc->type=DEV_TYPE_UNKNOWN;
993 #endif /* CONFIG_AMIGAONEG3SE */
996 #ifdef CONFIG_AMIGAONEG3SE
997 s = getenv("ide_doreset");
998 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
999 /* Need to soft reset the device in case it's an ATAPI... */
1000 PRINTF("Retrying...\n");
1001 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1003 ide_outb (device, ATA_COMMAND, 0x08);
1004 udelay (100000); /* 100 ms */
1009 } /* see above - ugly to read */
1010 #endif /* CONFIG_AMIGAONEG3SE */
1012 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1014 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1015 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1016 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1018 if ((iop->config & 0x0080)==0x0080)
1019 dev_desc->removable = 1;
1021 dev_desc->removable = 0;
1025 * Drive PIO mode autoselection
1029 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1030 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1032 PRINTF ("Override tPIO -> 2\n");
1034 if (iop->field_valid & 2) { /* drive implements ATA2? */
1035 PRINTF ("Drive implements ATA2\n");
1036 if (iop->capability & 8) { /* drive supports use_iordy? */
1037 cycle_time = iop->eide_pio_iordy;
1039 cycle_time = iop->eide_pio;
1041 PRINTF ("cycle time = %d\n", cycle_time);
1043 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1044 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1045 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1046 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1048 printf ("PIO mode to use: PIO %d\n", mode);
1052 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1053 atapi_inquiry(dev_desc);
1056 #endif /* CONFIG_ATAPI */
1059 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1061 dev_desc->type=DEV_TYPE_HARDDISK;
1062 dev_desc->blksz=ATA_BLOCKSIZE;
1063 dev_desc->lun=0; /* just to fill something in... */
1065 #if 0 /* only used to test the powersaving mode,
1066 * if enabled, the drive goes after 5 sec
1067 * in standby mode */
1068 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1069 c = ide_wait (device, IDE_TIME_OUT);
1070 ide_outb (device, ATA_SECT_CNT, 1);
1071 ide_outb (device, ATA_LBA_LOW, 0);
1072 ide_outb (device, ATA_LBA_MID, 0);
1073 ide_outb (device, ATA_LBA_HIGH, 0);
1074 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1075 ATA_DEVICE(device));
1076 ide_outb (device, ATA_COMMAND, 0xe3);
1078 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1083 /* ------------------------------------------------------------------------- */
1085 ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1089 unsigned char pwrsave=0; /* power save */
1091 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1092 device, blknr, blkcnt, (ulong)buffer);
1094 ide_led (DEVICE_LED(device), 1); /* LED on */
1098 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1099 c = ide_wait (device, IDE_TIME_OUT);
1101 if (c & ATA_STAT_BUSY) {
1102 printf ("IDE read: device %d not ready\n", device);
1106 /* first check if the drive is in Powersaving mode, if yes,
1107 * increase the timeout value */
1108 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1111 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1113 if (c & ATA_STAT_BUSY) {
1114 printf ("IDE read: device %d not ready\n", device);
1117 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1118 printf ("No Powersaving mode %X\n", c);
1120 c = ide_inb(device,ATA_SECT_CNT);
1121 PRINTF("Powersaving %02X\n",c);
1127 while (blkcnt-- > 0) {
1129 c = ide_wait (device, IDE_TIME_OUT);
1131 if (c & ATA_STAT_BUSY) {
1132 printf ("IDE read: device %d not ready\n", device);
1136 ide_outb (device, ATA_SECT_CNT, 1);
1137 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1138 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1139 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1140 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1141 ATA_DEVICE(device) |
1142 ((blknr >> 24) & 0xF) );
1143 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1148 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1151 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1154 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1155 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1160 input_data (device, buffer, ATA_SECTORWORDS);
1161 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1165 buffer += ATA_SECTORWORDS;
1168 ide_led (DEVICE_LED(device), 0); /* LED off */
1172 /* ------------------------------------------------------------------------- */
1175 ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1180 ide_led (DEVICE_LED(device), 1); /* LED on */
1184 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1186 while (blkcnt-- > 0) {
1188 c = ide_wait (device, IDE_TIME_OUT);
1190 if (c & ATA_STAT_BUSY) {
1191 printf ("IDE read: device %d not ready\n", device);
1195 ide_outb (device, ATA_SECT_CNT, 1);
1196 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1197 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1198 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1199 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1200 ATA_DEVICE(device) |
1201 ((blknr >> 24) & 0xF) );
1202 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1206 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1208 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1209 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1214 output_data (device, buffer, ATA_SECTORWORDS);
1215 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1218 buffer += ATA_SECTORWORDS;
1221 ide_led (DEVICE_LED(device), 0); /* LED off */
1225 /* ------------------------------------------------------------------------- */
1228 * copy src to dest, skipping leading and trailing blanks and null
1229 * terminate the string
1231 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1237 if (src[start]!=' ')
1247 for ( ; start<=end; start++) {
1253 /* ------------------------------------------------------------------------- */
1256 * Wait until Busy bit is off, or timeout (in ms)
1257 * Return last status
1259 static uchar ide_wait (int dev, ulong t)
1261 ulong delay = 10 * t; /* poll every 100 us */
1264 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1273 /* ------------------------------------------------------------------------- */
1275 #ifdef CONFIG_IDE_RESET
1276 extern void ide_set_reset(int idereset);
1278 static void ide_reset (void)
1280 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1281 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1286 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1288 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1289 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1291 ide_set_reset (1); /* assert reset */
1295 #ifdef CFG_PB_12V_ENABLE
1296 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1297 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1298 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1299 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1301 /* wait 500 ms for the voltage to stabilize
1303 for (i=0; i<500; ++i) {
1307 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1308 #endif /* CFG_PB_12V_ENABLE */
1310 #ifdef CFG_PB_IDE_MOTOR
1311 /* configure IDE Motor voltage monitor pin as input */
1312 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1313 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1314 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1316 /* wait up to 1 s for the motor voltage to stabilize
1318 for (i=0; i<1000; ++i) {
1319 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1325 if (i == 1000) { /* Timeout */
1326 printf ("\nWarning: 5V for IDE Motor missing\n");
1327 # ifdef CONFIG_STATUS_LED
1328 # ifdef STATUS_LED_YELLOW
1329 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1331 # ifdef STATUS_LED_GREEN
1332 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1334 # endif /* CONFIG_STATUS_LED */
1336 #endif /* CFG_PB_IDE_MOTOR */
1340 /* de-assert RESET signal */
1344 for (i=0; i<250; ++i) {
1349 #endif /* CONFIG_IDE_RESET */
1351 /* ------------------------------------------------------------------------- */
1353 #if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
1355 static uchar led_buffer = 0; /* Buffer for current LED status */
1357 static void ide_led (uchar led, uchar status)
1359 uchar *led_port = LED_PORT;
1361 if (status) { /* switch LED on */
1363 } else { /* switch LED off */
1367 *led_port = led_buffer;
1370 #endif /* CONFIG_IDE_LED */
1372 /* ------------------------------------------------------------------------- */
1375 /****************************************************************************
1383 #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1385 #define AT_PRINTF(fmt,args...)
1389 /* since ATAPI may use commands with not 4 bytes alligned length
1390 * we have our own transfer functions, 2 bytes alligned */
1392 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1395 volatile ushort *pbuf;
1397 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1398 dbuf = (ushort *)sect_buf;
1400 __asm__ volatile ("eieio");
1406 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1409 volatile ushort *pbuf;
1411 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1412 dbuf = (ushort *)sect_buf;
1414 __asm__ volatile ("eieio");
1419 #else /* ! __PPC__ */
1421 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1423 outsw(ATA_DATA_REG, sect_buf, shorts);
1428 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1430 insw(ATA_DATA_REG, sect_buf, shorts);
1433 #endif /* __PPC__ */
1436 * Wait until (Status & mask) == res, or timeout (in ms)
1437 * Return last status
1438 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1439 * and then they set their DRQ Bit
1441 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1443 ulong delay = 10 * t; /* poll every 100 us */
1446 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1447 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1448 /* break if error occurs (doesn't make sense to wait more) */
1449 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1460 * issue an atapi command
1462 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1464 unsigned char c,err,mask,res;
1466 ide_led (DEVICE_LED(device), 1); /* LED on */
1470 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1472 #ifdef CONFIG_AMIGAONEG3SE
1473 # warning THF: Removed LBA mode ???
1475 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1476 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1477 if ((c & mask) != res) {
1478 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1482 /* write taskfile */
1483 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1484 ide_outb (device, ATA_SECT_CNT, 0);
1485 ide_outb (device, ATA_SECT_NUM, 0);
1486 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1487 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1488 #ifdef CONFIG_AMIGAONEG3SE
1489 # warning THF: Removed LBA mode ???
1491 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1493 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1496 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1498 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1500 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1501 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1506 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1507 /* ATAPI Command written wait for completition */
1508 udelay (5000); /* device must set bsy */
1510 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1511 /* if no data wait for DRQ = 0 BSY = 0
1512 * if data wait for DRQ = 1 BSY = 0 */
1516 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1517 if ((c & mask) != res ) {
1518 if (c & ATA_STAT_ERR) {
1519 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1520 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1522 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1527 n=ide_inb(device, ATA_CYL_HIGH);
1529 n+=ide_inb(device, ATA_CYL_LOW);
1531 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1535 if((n==0)&&(buflen<0)) {
1536 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1541 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1543 if(n!=0) { /* data transfer */
1544 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1545 /* we transfer shorts */
1547 /* ok now decide if it is an in or output */
1548 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1549 AT_PRINTF("Write to device\n");
1550 output_data_shorts(device,(unsigned short *)buffer,n);
1552 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1553 input_data_shorts(device,(unsigned short *)buffer,n);
1556 udelay(5000); /* seems that some CD ROMs need this... */
1557 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1559 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1560 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1561 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1562 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1567 ide_led (DEVICE_LED(device), 0); /* LED off */
1572 * sending the command to atapi_issue. If an status other than good
1573 * returns, an request_sense will be issued
1576 #define ATAPI_DRIVE_NOT_READY 100
1577 #define ATAPI_UNIT_ATTN 10
1579 unsigned char atapi_issue_autoreq (int device,
1582 unsigned char *buffer,
1585 unsigned char sense_data[18],sense_ccb[12];
1586 unsigned char res,key,asc,ascq;
1587 int notready,unitattn;
1589 #ifdef CONFIG_AMIGAONEG3SE
1591 unsigned int timeout, retrycnt;
1593 s = getenv("ide_cd_timeout");
1594 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1599 unitattn=ATAPI_UNIT_ATTN;
1600 notready=ATAPI_DRIVE_NOT_READY;
1603 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1605 return (0); /* Ok */
1608 return (0xFF); /* error */
1610 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1612 memset(sense_ccb,0,sizeof(sense_ccb));
1613 memset(sense_data,0,sizeof(sense_data));
1614 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1615 sense_ccb[4]=18; /* allocation Length */
1617 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1618 key=(sense_data[2]&0xF);
1619 asc=(sense_data[12]);
1620 ascq=(sense_data[13]);
1622 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1623 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1630 return 0; /* ok device ready */
1632 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1637 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1640 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1645 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1649 AT_PRINTF("Media not present\n");
1653 #ifdef CONFIG_AMIGAONEG3SE
1654 if ((sense_data[2]&0xF)==0x0B) {
1655 AT_PRINTF("ABORTED COMMAND...retry\n");
1661 if ((sense_data[2]&0xf) == 0x02 &&
1662 sense_data[12] == 0x04 &&
1663 sense_data[13] == 0x01 ) {
1664 AT_PRINTF("Waiting for unit to become active\n");
1670 #endif /* CONFIG_AMIGAONEG3SE */
1672 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1674 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1679 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1681 unsigned char ccb[12]; /* Command descriptor block */
1682 unsigned char iobuf[64]; /* temp buf */
1686 device=dev_desc->dev;
1687 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1688 dev_desc->block_read=atapi_read;
1690 memset(ccb,0,sizeof(ccb));
1691 memset(iobuf,0,sizeof(iobuf));
1693 ccb[0]=ATAPI_CMD_INQUIRY;
1694 ccb[4]=40; /* allocation Legnth */
1695 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1697 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1701 /* copy device ident strings */
1702 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1703 ident_cpy(dev_desc->product,&iobuf[16],16);
1704 ident_cpy(dev_desc->revision,&iobuf[32],5);
1709 dev_desc->type=iobuf[0] & 0x1f;
1711 if ((iobuf[1]&0x80)==0x80)
1712 dev_desc->removable = 1;
1714 dev_desc->removable = 0;
1716 memset(ccb,0,sizeof(ccb));
1717 memset(iobuf,0,sizeof(iobuf));
1718 ccb[0]=ATAPI_CMD_START_STOP;
1719 ccb[4]=0x03; /* start */
1721 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1723 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1727 memset(ccb,0,sizeof(ccb));
1728 memset(iobuf,0,sizeof(iobuf));
1729 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1731 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1735 memset(ccb,0,sizeof(ccb));
1736 memset(iobuf,0,sizeof(iobuf));
1737 ccb[0]=ATAPI_CMD_READ_CAP;
1738 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1739 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1743 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1744 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1745 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1747 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1748 ((unsigned long)iobuf[1]<<16) +
1749 ((unsigned long)iobuf[2]<< 8) +
1750 ((unsigned long)iobuf[3]);
1751 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1752 ((unsigned long)iobuf[5]<<16) +
1753 ((unsigned long)iobuf[6]<< 8) +
1754 ((unsigned long)iobuf[7]);
1761 * we transfer only one block per command, since the multiple DRQ per
1762 * command is not yet implemented
1764 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1765 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1766 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1768 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1771 unsigned char ccb[12]; /* Command descriptor block */
1774 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1775 device, blknr, blkcnt, (ulong)buffer);
1778 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1779 cnt=ATAPI_READ_MAX_BLOCK;
1783 ccb[0]=ATAPI_CMD_READ_12;
1784 ccb[1]=0; /* reserved */
1785 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1786 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1787 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1788 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1789 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1790 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1791 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1792 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1793 ccb[10]=0; /* reserved */
1794 ccb[11]=0; /* reserved */
1796 if (atapi_issue_autoreq(device,ccb,12,
1797 (unsigned char *)buffer,
1798 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1804 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1805 } while (blkcnt > 0);
1809 /* ------------------------------------------------------------------------- */
1811 #endif /* CONFIG_ATAPI */
1815 "ide - IDE sub-system\n",
1816 "reset - reset IDE controller\n"
1817 "ide info - show available IDE devices\n"
1818 "ide device [dev] - show or set current device\n"
1819 "ide part [dev] - print partition table of one or all IDE devices\n"
1820 "ide read addr blk# cnt\n"
1821 "ide write addr blk# cnt - read/write `cnt'"
1822 " blocks starting at block `blk#'\n"
1823 " to/from memory address `addr'\n"
1827 diskboot, 3, 1, do_diskboot,
1828 "diskboot- boot from IDE device\n",
1829 "loadAddr dev:part\n"
1832 #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */