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>
48 #ifdef CONFIG_SHOW_BOOT_PROGRESS
49 # include <status_led.h>
50 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
52 # define SHOW_BOOT_PROGRESS(arg)
59 #define PRINTF(fmt,args...) printf (fmt ,##args)
61 #define PRINTF(fmt,args...)
64 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
66 /* Timings for IDE Interface
68 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
69 * 70 165 30 PIO-Mode 0, [ns]
71 * 50 125 20 PIO-Mode 1, [ns]
73 * 30 100 15 PIO-Mode 2, [ns]
75 * 30 80 10 PIO-Mode 3, [ns]
77 * 25 70 10 PIO-Mode 4, [ns]
81 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
83 /* Setup Length Hold */
84 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
85 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
86 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
87 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
88 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
91 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
94 #define CFG_PIO_MODE 0 /* use a relaxed default */
96 static int pio_mode = CFG_PIO_MODE;
98 /* Make clock cycles and always round up */
100 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
102 /* ------------------------------------------------------------------------- */
104 /* Current I/O Device */
105 static int curr_device = -1;
107 /* Current offset for IDE0 / IDE1 bus access */
108 ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
109 #if defined(CFG_ATA_IDE0_OFFSET)
112 #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
117 #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
119 static int ide_bus_ok[CFG_IDE_MAXBUS];
121 static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
122 /* ------------------------------------------------------------------------- */
124 #ifdef CONFIG_IDE_LED
125 static void ide_led (uchar led, uchar status);
127 #define ide_led(a,b) /* dummy */
130 #ifdef CONFIG_IDE_RESET
131 static void ide_reset (void);
133 #define ide_reset() /* dummy */
136 static void ide_ident (block_dev_desc_t *dev_desc);
137 static uchar ide_wait (int dev, ulong t);
139 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
141 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
143 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
145 static void __inline__ outb(int dev, int port, unsigned char val);
146 static unsigned char __inline__ inb(int dev, int port);
147 static void input_swap_data(int dev, ulong *sect_buf, int words);
148 static void input_data(int dev, ulong *sect_buf, int words);
149 static void output_data(int dev, ulong *sect_buf, int words);
150 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
154 static void atapi_inquiry(block_dev_desc_t *dev_desc);
155 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
159 #ifdef CONFIG_IDE_8xx_DIRECT
160 static void set_pcmcia_timing (int pmode);
162 #define set_pcmcia_timing(a) /* dummy */
165 /* ------------------------------------------------------------------------- */
167 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
174 printf ("Usage:\n%s\n", cmdtp->usage);
177 if (strncmp(argv[1],"res",3) == 0) {
179 #ifdef CONFIG_IDE_8xx_DIRECT
180 " on PCMCIA " PCMCIA_SLOT_MSG
186 } else if (strncmp(argv[1],"inf",3) == 0) {
191 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
192 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
193 continue; /* list only known devices */
194 printf ("IDE device %d: ", i);
195 dev_print(&ide_dev_desc[i]);
199 } else if (strncmp(argv[1],"dev",3) == 0) {
200 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
201 puts ("\nno IDE devices available\n");
204 printf ("\nIDE device %d: ", curr_device);
205 dev_print(&ide_dev_desc[curr_device]);
207 } else if (strncmp(argv[1],"part",4) == 0) {
210 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
211 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
215 print_part(&ide_dev_desc[dev]);
219 puts ("\nno IDE devices available\n");
224 printf ("Usage:\n%s\n", cmdtp->usage);
227 if (strncmp(argv[1],"dev",3) == 0) {
228 int dev = (int)simple_strtoul(argv[2], NULL, 10);
230 printf ("\nIDE device %d: ", dev);
231 if (dev >= CFG_IDE_MAXDEVICE) {
232 puts ("unknown device\n");
235 dev_print(&ide_dev_desc[dev]);
238 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
244 puts ("... is now current device\n");
247 } else if (strncmp(argv[1],"part",4) == 0) {
248 int dev = (int)simple_strtoul(argv[2], NULL, 10);
250 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
251 print_part(&ide_dev_desc[dev]);
253 printf ("\nIDE device %d not available\n", dev);
258 } else if (strncmp(argv[1],"pio",4) == 0) {
259 int mode = (int)simple_strtoul(argv[2], NULL, 10);
261 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
266 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
267 mode, IDE_MAX_PIO_MODE);
273 printf ("Usage:\n%s\n", cmdtp->usage);
276 /* at least 4 args */
278 if (strcmp(argv[1],"read") == 0) {
279 ulong addr = simple_strtoul(argv[2], NULL, 16);
280 ulong blk = simple_strtoul(argv[3], NULL, 16);
281 ulong cnt = simple_strtoul(argv[4], NULL, 16);
284 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
285 curr_device, blk, cnt);
287 n = ide_dev_desc[curr_device].block_read (curr_device,
290 /* flush cache after read */
291 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
293 printf ("%ld blocks read: %s\n",
294 n, (n==cnt) ? "OK" : "ERROR");
300 } else if (strcmp(argv[1],"write") == 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 write: device %d block # %ld, count %ld ... ",
307 curr_device, blk, cnt);
309 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
311 printf ("%ld blocks written: %s\n",
312 n, (n==cnt) ? "OK" : "ERROR");
319 printf ("Usage:\n%s\n", cmdtp->usage);
327 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
329 char *boot_device = NULL;
334 disk_partition_t info;
340 addr = CFG_LOAD_ADDR;
341 boot_device = getenv ("bootdevice");
344 addr = simple_strtoul(argv[1], NULL, 16);
345 boot_device = getenv ("bootdevice");
348 addr = simple_strtoul(argv[1], NULL, 16);
349 boot_device = argv[2];
352 printf ("Usage:\n%s\n", cmdtp->usage);
353 SHOW_BOOT_PROGRESS (-1);
358 puts ("\n** No boot device **\n");
359 SHOW_BOOT_PROGRESS (-1);
363 dev = simple_strtoul(boot_device, &ep, 16);
365 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
366 printf ("\n** Device %d not available\n", dev);
367 SHOW_BOOT_PROGRESS (-1);
373 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
374 SHOW_BOOT_PROGRESS (-1);
377 part = simple_strtoul(++ep, NULL, 16);
379 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
380 SHOW_BOOT_PROGRESS (-1);
383 if (strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
384 printf ("\n** Invalid partition type \"%.32s\""
385 " (expect \"" BOOT_PART_TYPE "\")\n",
387 SHOW_BOOT_PROGRESS (-1);
391 printf ("\nLoading from IDE device %d, partition %d: "
392 "Name: %.32s Type: %.32s\n",
393 dev, part, info.name, info.type);
395 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
396 info.start, info.size, info.blksz);
398 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
399 printf ("** Read error on %d:%d\n", dev, part);
400 SHOW_BOOT_PROGRESS (-1);
404 hdr = (image_header_t *)addr;
406 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
408 print_image_hdr (hdr);
410 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
411 cnt += info.blksz - 1;
415 printf("\n** Bad Magic Number **\n");
416 SHOW_BOOT_PROGRESS (-1);
420 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
421 (ulong *)(addr+info.blksz)) != cnt) {
422 printf ("** Read error on %d:%d\n", dev, part);
423 SHOW_BOOT_PROGRESS (-1);
428 /* Loading ok, update default load address */
432 /* Check if we should attempt an auto-start */
433 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
435 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
437 local_args[0] = argv[0];
438 local_args[1] = NULL;
440 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
442 do_bootm (cmdtp, 0, 1, local_args);
448 /* ------------------------------------------------------------------------- */
452 DECLARE_GLOBAL_DATA_PTR;
454 #ifdef CONFIG_IDE_8xx_DIRECT
455 volatile immap_t *immr = (immap_t *)CFG_IMMR;
456 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
461 #ifdef CONFIG_IDE_8xx_PCCARD
462 extern int pcmcia_on (void);
466 /* initialize the PCMCIA IDE adapter card */
469 udelay (1000000); /* 1 s */
470 #endif /* CONFIG_IDE_8xx_PCCARD */
474 /* Initialize PIO timing tables */
475 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
476 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
478 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
480 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
482 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
484 " hold=%2d ns/%d clk\n",
486 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
487 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
488 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
491 /* Reset the IDE just to be sure.
492 * Light LED's to show
494 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
495 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
497 #ifdef CONFIG_IDE_8xx_DIRECT
498 /* PCMCIA / IDE initialization for common mem space */
499 pcmp->pcmc_pgcrb = 0;
502 /* start in PIO mode 0 - most relaxed timings */
504 set_pcmcia_timing (pio_mode);
507 * Wait for IDE to get ready.
508 * According to spec, this can take up to 31 seconds!
510 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
511 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
513 printf ("Bus %d: ", bus);
519 udelay (100000); /* 100 ms */
520 outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
521 udelay (100000); /* 100 ms */
525 udelay (10000); /* 10 ms */
527 c = inb (dev, ATA_STATUS);
529 if (i > (ATA_RESET_TIME * 100)) {
530 puts ("** Timeout **\n");
531 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
534 if ((i >= 100) && ((i%100)==0)) {
537 } while (c & ATA_STAT_BUSY);
539 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
540 puts ("not available ");
541 PRINTF ("Status = 0x%02X ", c);
542 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
543 } else if ((c & ATA_STAT_READY) == 0) {
544 puts ("not available ");
545 PRINTF ("Status = 0x%02X ", c);
555 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
558 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
559 #ifdef CONFIG_IDE_LED
560 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
562 ide_dev_desc[i].if_type=IF_TYPE_IDE;
563 ide_dev_desc[i].dev=i;
564 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
565 ide_dev_desc[i].blksz=0;
566 ide_dev_desc[i].lba=0;
567 ide_dev_desc[i].block_read=ide_read;
568 if (!ide_bus_ok[IDE_BUS(i)])
570 ide_led (led, 1); /* LED on */
571 ide_ident(&ide_dev_desc[i]);
572 ide_led (led, 0); /* LED off */
573 dev_print(&ide_dev_desc[i]);
575 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
576 init_part (&ide_dev_desc[i]); /* initialize partition type */
584 /* ------------------------------------------------------------------------- */
586 block_dev_desc_t * ide_get_dev(int dev)
588 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
592 #ifdef CONFIG_IDE_8xx_DIRECT
595 set_pcmcia_timing (int pmode)
597 volatile immap_t *immr = (immap_t *)CFG_IMMR;
598 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
601 PRINTF ("Set timing for PIO Mode %d\n", pmode);
603 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
604 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
605 | PCMCIA_SL (pio_config_clk[pmode].t_length)
610 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
611 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
612 #if (CFG_PCMCIA_POR0 != 0)
616 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
618 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
619 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
620 #if (CFG_PCMCIA_POR1 != 0)
624 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
626 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
627 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
628 #if (CFG_PCMCIA_POR2 != 0)
632 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
634 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
635 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
636 #if (CFG_PCMCIA_POR3 != 0)
640 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
644 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
645 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
646 #if (CFG_PCMCIA_POR4 != 0)
650 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
652 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
653 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
654 #if (CFG_PCMCIA_POR5 != 0)
658 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
660 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
661 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
662 #if (CFG_PCMCIA_POR6 != 0)
666 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
668 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
669 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
670 #if (CFG_PCMCIA_POR7 != 0)
674 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
678 #endif /* CONFIG_IDE_8xx_DIRECT */
680 /* ------------------------------------------------------------------------- */
682 static void __inline__
683 outb(int dev, int port, unsigned char val)
685 /* Ensure I/O operations complete */
686 __asm__ volatile("eieio");
687 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
689 printf ("OUTB: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
693 static unsigned char __inline__
694 inb(int dev, int port)
697 /* Ensure I/O operations complete */
698 __asm__ volatile("eieio");
699 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
701 printf ("INB: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
706 __inline__ unsigned ld_le16(const volatile unsigned short *addr)
710 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r"(val) : "r"(addr), "m"(*addr));
715 input_swap_data(int dev, ulong *sect_buf, int words)
717 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
718 ushort *dbuf = (ushort *)sect_buf;
721 *dbuf++ = ld_le16(pbuf);
722 *dbuf++ = ld_le16(pbuf);
727 output_data(int dev, ulong *sect_buf, int words)
730 volatile ushort *pbuf;
732 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
733 dbuf = (ushort *)sect_buf;
735 __asm__ volatile ("eieio");
737 __asm__ volatile ("eieio");
743 input_data(int dev, ulong *sect_buf, int words)
746 volatile ushort *pbuf;
748 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
749 dbuf = (ushort *)sect_buf;
751 __asm__ volatile ("eieio");
753 __asm__ volatile ("eieio");
758 /* -------------------------------------------------------------------------
760 static void ide_ident (block_dev_desc_t *dev_desc)
762 ulong iobuf[ATA_SECTORWORDS];
764 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
767 int mode, cycle_time;
770 device=dev_desc->dev;
771 printf (" Device %d: ", device);
773 ide_led (DEVICE_LED(device), 1); /* LED on */
776 outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
777 dev_desc->if_type=IF_TYPE_IDE;
779 /* check signature */
780 if ((inb(device,ATA_SECT_CNT)==0x01) &&
781 (inb(device,ATA_SECT_NUM)==0x01) &&
782 (inb(device,ATA_CYL_LOW)==0x14) &&
783 (inb(device,ATA_CYL_HIGH)==0xEB)) {
784 /* ATAPI Signature found */
785 dev_desc->if_type=IF_TYPE_ATAPI;
786 /* Start Ident Command
788 outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
790 * Wait for completion - ATAPI devices need more time
793 c = ide_wait (device, ATAPI_TIME_OUT);
798 /* Start Ident Command
800 outb (device, ATA_COMMAND, ATA_CMD_IDENT);
802 /* Wait for completion
804 c = ide_wait (device, IDE_TIME_OUT);
806 ide_led (DEVICE_LED(device), 0); /* LED off */
808 if (((c & ATA_STAT_DRQ) == 0) ||
809 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
810 dev_desc->type=DEV_TYPE_UNKNOWN;
814 input_swap_data (device, iobuf, ATA_SECTORWORDS);
816 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
817 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
818 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
820 if ((iop->config & 0x0080)==0x0080)
821 dev_desc->removable = 1;
823 dev_desc->removable = 0;
827 * Drive PIO mode autoselection
831 printf ("tPIO = 0x%02x = %d\n",mode, mode);
832 if (mode > 2) { /* 2 is maximum allowed tPIO value */
834 PRINTF ("Override tPIO -> 2\n");
836 if (iop->field_valid & 2) { /* drive implements ATA2? */
837 PRINTF ("Drive implements ATA2\n");
838 if (iop->capability & 8) { /* drive supports use_iordy? */
839 cycle_time = iop->eide_pio_iordy;
841 cycle_time = iop->eide_pio;
843 PRINTF ("cycle time = %d\n", cycle_time);
845 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
846 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
847 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
848 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
850 printf ("PIO mode to use: PIO %d\n", mode);
854 if (dev_desc->if_type==IF_TYPE_ATAPI) {
855 atapi_inquiry(dev_desc);
858 #endif /* CONFIG_ATAPI */
861 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
863 dev_desc->type=DEV_TYPE_HARDDISK;
864 dev_desc->blksz=ATA_BLOCKSIZE;
865 dev_desc->lun=0; /* just to fill something in... */
867 #if 0 /* only used to test the powersaving mode,
868 * if enabled, the drive goes after 5 sec
870 outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
871 c = ide_wait (device, IDE_TIME_OUT);
872 outb (device, ATA_SECT_CNT, 1);
873 outb (device, ATA_LBA_LOW, 0);
874 outb (device, ATA_LBA_MID, 0);
875 outb (device, ATA_LBA_HIGH, 0);
876 outb (device, ATA_DEV_HD, ATA_LBA |
878 outb (device, ATA_COMMAND, 0xe3);
880 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
885 /* ------------------------------------------------------------------------- */
887 ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
891 unsigned char pwrsave=0; /* power save */
893 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
894 device, blknr, blkcnt, (ulong)buffer);
896 ide_led (DEVICE_LED(device), 1); /* LED on */
900 outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
901 c = ide_wait (device, IDE_TIME_OUT);
903 if (c & ATA_STAT_BUSY) {
904 printf ("IDE read: device %d not ready\n", device);
908 /* first check if the drive is in Powersaving mode, if yes,
909 * increase the timeout value */
910 outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
913 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
915 if (c & ATA_STAT_BUSY) {
916 printf ("IDE read: device %d not ready\n", device);
919 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
920 printf ("No Powersaving mode %X\n", c);
922 c = inb(device,ATA_SECT_CNT);
923 PRINTF("Powersaving %02X\n",c);
929 while (blkcnt-- > 0) {
931 c = ide_wait (device, IDE_TIME_OUT);
933 if (c & ATA_STAT_BUSY) {
934 printf ("IDE read: device %d not ready\n", device);
938 outb (device, ATA_SECT_CNT, 1);
939 outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
940 outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
941 outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
942 outb (device, ATA_DEV_HD, ATA_LBA |
944 ((blknr >> 24) & 0xF) );
945 outb (device, ATA_COMMAND, ATA_CMD_READ);
950 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
953 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
956 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
957 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
962 input_data (device, buffer, ATA_SECTORWORDS);
963 (void) inb (device, ATA_STATUS); /* clear IRQ */
967 buffer += ATA_SECTORWORDS;
970 ide_led (DEVICE_LED(device), 0); /* LED off */
974 /* ------------------------------------------------------------------------- */
977 ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
982 ide_led (DEVICE_LED(device), 1); /* LED on */
986 outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
988 while (blkcnt-- > 0) {
990 c = ide_wait (device, IDE_TIME_OUT);
992 if (c & ATA_STAT_BUSY) {
993 printf ("IDE read: device %d not ready\n", device);
997 outb (device, ATA_SECT_CNT, 1);
998 outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
999 outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1000 outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1001 outb (device, ATA_DEV_HD, ATA_LBA |
1002 ATA_DEVICE(device) |
1003 ((blknr >> 24) & 0xF) );
1004 outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1008 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1010 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1011 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1016 output_data (device, buffer, ATA_SECTORWORDS);
1017 c = inb (device, ATA_STATUS); /* clear IRQ */
1020 buffer += ATA_SECTORWORDS;
1023 ide_led (DEVICE_LED(device), 0); /* LED off */
1027 /* ------------------------------------------------------------------------- */
1030 * copy src to dest, skipping leading and trailing blanks and null
1031 * terminate the string
1033 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1039 if (src[start]!=' ')
1049 for ( ; start<=end; start++) {
1055 /* ------------------------------------------------------------------------- */
1058 * Wait until Busy bit is off, or timeout (in ms)
1059 * Return last status
1061 static uchar ide_wait (int dev, ulong t)
1063 ulong delay = 10 * t; /* poll every 100 us */
1066 while ((c = inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1075 /* ------------------------------------------------------------------------- */
1077 #ifdef CONFIG_IDE_RESET
1078 extern void ide_set_reset(int idereset);
1080 static void ide_reset (void)
1082 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1083 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1088 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1090 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1091 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1093 ide_set_reset (1); /* assert reset */
1097 #ifdef CFG_PB_12V_ENABLE
1098 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1099 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1100 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1101 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1103 /* wait 500 ms for the voltage to stabilize
1105 for (i=0; i<500; ++i) {
1109 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1110 #endif /* CFG_PB_12V_ENABLE */
1112 #ifdef CFG_PB_IDE_MOTOR
1113 /* configure IDE Motor voltage monitor pin as input */
1114 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1115 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1116 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1118 /* wait up to 1 s for the motor voltage to stabilize
1120 for (i=0; i<1000; ++i) {
1121 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1127 if (i == 1000) { /* Timeout */
1128 printf ("\nWarning: 5V for IDE Motor missing\n");
1129 # ifdef CONFIG_STATUS_LED
1130 # ifdef STATUS_LED_YELLOW
1131 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1133 # ifdef STATUS_LED_GREEN
1134 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1136 # endif /* CONFIG_STATUS_LED */
1138 #endif /* CFG_PB_IDE_MOTOR */
1142 /* de-assert RESET signal */
1146 for (i=0; i<250; ++i) {
1151 #endif /* CONFIG_IDE_RESET */
1153 /* ------------------------------------------------------------------------- */
1155 #ifdef CONFIG_IDE_LED
1157 static uchar led_buffer = 0; /* Buffer for current LED status */
1159 static void ide_led (uchar led, uchar status)
1161 uchar *led_port = LED_PORT;
1163 if (status) { /* switch LED on */
1165 } else { /* switch LED off */
1169 *led_port = led_buffer;
1172 #endif /* CONFIG_IDE_LED */
1174 /* ------------------------------------------------------------------------- */
1177 /****************************************************************************
1186 #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1188 #define AT_PRINTF(fmt,args...)
1191 /* since ATAPI may use commands with not 4 bytes alligned length
1192 * we have our own transfer functions, 2 bytes alligned */
1194 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1197 volatile ushort *pbuf;
1199 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1200 dbuf = (ushort *)sect_buf;
1202 __asm__ volatile ("eieio");
1208 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1211 volatile ushort *pbuf;
1213 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1214 dbuf = (ushort *)sect_buf;
1216 __asm__ volatile ("eieio");
1222 * Wait until (Status & mask) == res, or timeout (in ms)
1223 * Return last status
1224 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1225 * and then they set their DRQ Bit
1227 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1229 ulong delay = 10 * t; /* poll every 100 us */
1232 c = inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1233 while (((c = inb(dev, ATA_STATUS)) & mask)
1235 /* break if error occurs (doesn't make sense to wait more) */
1236 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1247 * issue an atapi command
1249 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1251 unsigned char c,err,mask,res;
1253 ide_led (DEVICE_LED(device), 1); /* LED on */
1257 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1259 outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1260 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1261 if ((c & mask) != res) {
1262 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1266 /* write taskfile */
1267 outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1268 outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1269 outb (device, ATA_CYL_HIGH, (unsigned char)((buflen<<8) & 0xFF));
1270 outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1272 outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1275 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1277 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1279 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1280 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1285 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1286 /* ATAPI Command written wait for completition */
1287 udelay (5000); /* device must set bsy */
1289 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1290 /* if no data wait for DRQ = 0 BSY = 0
1291 * if data wait for DRQ = 1 BSY = 0 */
1295 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1296 if ((c & mask) != res ) {
1297 if (c & ATA_STAT_ERR) {
1298 err=(inb(device,ATA_ERROR_REG))>>4;
1299 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1301 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1306 n=inb(device, ATA_CYL_HIGH);
1308 n+=inb(device, ATA_CYL_LOW);
1310 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1314 if((n==0)&&(buflen<0)) {
1315 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1320 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1322 if(n!=0) { /* data transfer */
1323 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1324 /* we transfer shorts */
1326 /* ok now decide if it is an in or output */
1327 if ((inb(device, ATA_SECT_CNT)&0x02)==0) {
1328 AT_PRINTF("Write to device\n");
1329 output_data_shorts(device,(unsigned short *)buffer,n);
1331 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1332 input_data_shorts(device,(unsigned short *)buffer,n);
1335 udelay(5000); /* seems that some CD ROMs need this... */
1336 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1338 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1339 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1340 err=(inb(device,ATA_ERROR_REG) >> 4);
1341 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1346 ide_led (DEVICE_LED(device), 0); /* LED off */
1351 * sending the command to atapi_issue. If an status other than good
1352 * returns, an request_sense will be issued
1355 #define ATAPI_DRIVE_NOT_READY 100
1356 #define ATAPI_UNIT_ATTN 10
1358 unsigned char atapi_issue_autoreq (int device,
1361 unsigned char *buffer,
1364 unsigned char sense_data[18],sense_ccb[12];
1365 unsigned char res,key,asc,ascq;
1366 int notready,unitattn;
1368 unitattn=ATAPI_UNIT_ATTN;
1369 notready=ATAPI_DRIVE_NOT_READY;
1372 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1374 return (0); /* Ok */
1377 return (0xFF); /* error */
1379 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1381 memset(sense_ccb,0,sizeof(sense_ccb));
1382 memset(sense_data,0,sizeof(sense_data));
1383 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1384 sense_ccb[4]=18; /* allocation Legnth */
1386 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1387 key=(sense_data[2]&0xF);
1388 asc=(sense_data[12]);
1389 ascq=(sense_data[13]);
1391 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1392 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1399 return 0; /* ok device ready */
1401 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1406 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1409 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1414 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1418 AT_PRINTF("Media not present\n");
1421 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1423 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1429 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1431 unsigned char ccb[12]; /* Command descriptor block */
1432 unsigned char iobuf[64]; /* temp buf */
1436 device=dev_desc->dev;
1437 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1438 dev_desc->block_read=atapi_read;
1440 memset(ccb,0,sizeof(ccb));
1441 memset(iobuf,0,sizeof(iobuf));
1443 ccb[0]=ATAPI_CMD_INQUIRY;
1444 ccb[4]=40; /* allocation Legnth */
1445 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1447 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1451 /* copy device ident strings */
1452 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1453 ident_cpy(dev_desc->product,&iobuf[16],16);
1454 ident_cpy(dev_desc->revision,&iobuf[32],5);
1459 dev_desc->type=iobuf[0] & 0x1f;
1461 if ((iobuf[1]&0x80)==0x80)
1462 dev_desc->removable = 1;
1464 dev_desc->removable = 0;
1466 memset(ccb,0,sizeof(ccb));
1467 memset(iobuf,0,sizeof(iobuf));
1468 ccb[0]=ATAPI_CMD_START_STOP;
1469 ccb[4]=0x03; /* start */
1471 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1473 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1477 memset(ccb,0,sizeof(ccb));
1478 memset(iobuf,0,sizeof(iobuf));
1479 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1481 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1485 memset(ccb,0,sizeof(ccb));
1486 memset(iobuf,0,sizeof(iobuf));
1487 ccb[0]=ATAPI_CMD_READ_CAP;
1488 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1489 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1493 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1494 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1495 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1497 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1498 ((unsigned long)iobuf[1]<<16) +
1499 ((unsigned long)iobuf[2]<< 8) +
1500 ((unsigned long)iobuf[3]);
1501 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1502 ((unsigned long)iobuf[5]<<16) +
1503 ((unsigned long)iobuf[6]<< 8) +
1504 ((unsigned long)iobuf[7]);
1511 * we transfer only one block per command, since the multiple DRQ per
1512 * command is not yet implemented
1514 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1515 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1516 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1518 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1521 unsigned char ccb[12]; /* Command descriptor block */
1524 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1525 device, blknr, blkcnt, (ulong)buffer);
1528 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1529 cnt=ATAPI_READ_MAX_BLOCK;
1533 ccb[0]=ATAPI_CMD_READ_12;
1534 ccb[1]=0; /* reserved */
1535 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1536 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1537 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1538 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1539 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1540 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1541 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1542 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1543 ccb[10]=0; /* reserved */
1544 ccb[11]=0; /* reserved */
1546 if (atapi_issue_autoreq(device,ccb,12,
1547 (unsigned char *)buffer,
1548 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1554 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1555 } while (blkcnt > 0);
1559 /* ------------------------------------------------------------------------- */
1561 #endif /* CONFIG_ATAPI */
1563 #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */