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)
62 #define PRINTF(fmt,args...) printf (fmt ,##args)
64 #define PRINTF(fmt,args...)
67 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
69 /* Timings for IDE Interface
71 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
72 * 70 165 30 PIO-Mode 0, [ns]
74 * 50 125 20 PIO-Mode 1, [ns]
76 * 30 100 15 PIO-Mode 2, [ns]
78 * 30 80 10 PIO-Mode 3, [ns]
80 * 25 70 10 PIO-Mode 4, [ns]
84 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
86 /* Setup Length Hold */
87 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
88 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
89 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
90 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
91 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
94 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
97 #define CFG_PIO_MODE 0 /* use a relaxed default */
99 static int pio_mode = CFG_PIO_MODE;
101 /* Make clock cycles and always round up */
103 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
105 /* ------------------------------------------------------------------------- */
107 /* Current I/O Device */
108 static int curr_device = -1;
110 /* Current offset for IDE0 / IDE1 bus access */
111 ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
112 #if defined(CFG_ATA_IDE0_OFFSET)
115 #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
121 #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
124 static int ide_bus_ok[CFG_IDE_MAXBUS];
126 static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
127 /* ------------------------------------------------------------------------- */
129 #ifdef CONFIG_IDE_LED
130 static void ide_led (uchar led, uchar status);
132 #define ide_led(a,b) /* dummy */
135 #ifdef CONFIG_IDE_RESET
136 static void ide_reset (void);
138 #define ide_reset() /* dummy */
141 static void ide_ident (block_dev_desc_t *dev_desc);
142 static uchar ide_wait (int dev, ulong t);
144 #define IDE_TIME_OUT 2000 /* 2 sec timeout */
146 #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
148 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
150 static void __inline__ ide_outb(int dev, int port, unsigned char val);
151 static unsigned char __inline__ ide_inb(int dev, int port);
153 static void input_swap_data(int dev, ulong *sect_buf, int words);
155 static void input_data(int dev, ulong *sect_buf, int words);
156 static void output_data(int dev, ulong *sect_buf, int words);
157 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
161 static void atapi_inquiry(block_dev_desc_t *dev_desc);
162 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
166 #ifdef CONFIG_IDE_8xx_DIRECT
167 static void set_pcmcia_timing (int pmode);
169 #define set_pcmcia_timing(a) /* dummy */
172 /* ------------------------------------------------------------------------- */
174 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
181 printf ("Usage:\n%s\n", cmdtp->usage);
184 if (strncmp(argv[1],"res",3) == 0) {
186 #ifdef CONFIG_IDE_8xx_DIRECT
187 " on PCMCIA " PCMCIA_SLOT_MSG
193 } else if (strncmp(argv[1],"inf",3) == 0) {
198 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
199 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
200 continue; /* list only known devices */
201 printf ("IDE device %d: ", i);
202 dev_print(&ide_dev_desc[i]);
206 } else if (strncmp(argv[1],"dev",3) == 0) {
207 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
208 puts ("\nno IDE devices available\n");
211 printf ("\nIDE device %d: ", curr_device);
212 dev_print(&ide_dev_desc[curr_device]);
214 } else if (strncmp(argv[1],"part",4) == 0) {
217 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
218 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
222 print_part(&ide_dev_desc[dev]);
226 puts ("\nno IDE devices available\n");
231 printf ("Usage:\n%s\n", cmdtp->usage);
234 if (strncmp(argv[1],"dev",3) == 0) {
235 int dev = (int)simple_strtoul(argv[2], NULL, 10);
237 printf ("\nIDE device %d: ", dev);
238 if (dev >= CFG_IDE_MAXDEVICE) {
239 puts ("unknown device\n");
242 dev_print(&ide_dev_desc[dev]);
245 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
251 puts ("... is now current device\n");
254 } else if (strncmp(argv[1],"part",4) == 0) {
255 int dev = (int)simple_strtoul(argv[2], NULL, 10);
257 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
258 print_part(&ide_dev_desc[dev]);
260 printf ("\nIDE device %d not available\n", dev);
265 } else if (strncmp(argv[1],"pio",4) == 0) {
266 int mode = (int)simple_strtoul(argv[2], NULL, 10);
268 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
273 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
274 mode, IDE_MAX_PIO_MODE);
280 printf ("Usage:\n%s\n", cmdtp->usage);
283 /* at least 4 args */
285 if (strcmp(argv[1],"read") == 0) {
286 ulong addr = simple_strtoul(argv[2], NULL, 16);
287 ulong blk = simple_strtoul(argv[3], NULL, 16);
288 ulong cnt = simple_strtoul(argv[4], NULL, 16);
291 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
292 curr_device, blk, cnt);
294 n = ide_dev_desc[curr_device].block_read (curr_device,
297 /* flush cache after read */
298 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
300 printf ("%ld blocks read: %s\n",
301 n, (n==cnt) ? "OK" : "ERROR");
307 } else if (strcmp(argv[1],"write") == 0) {
308 ulong addr = simple_strtoul(argv[2], NULL, 16);
309 ulong blk = simple_strtoul(argv[3], NULL, 16);
310 ulong cnt = simple_strtoul(argv[4], NULL, 16);
313 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
314 curr_device, blk, cnt);
316 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
318 printf ("%ld blocks written: %s\n",
319 n, (n==cnt) ? "OK" : "ERROR");
326 printf ("Usage:\n%s\n", cmdtp->usage);
334 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
336 char *boot_device = NULL;
341 disk_partition_t info;
347 addr = CFG_LOAD_ADDR;
348 boot_device = getenv ("bootdevice");
351 addr = simple_strtoul(argv[1], NULL, 16);
352 boot_device = getenv ("bootdevice");
355 addr = simple_strtoul(argv[1], NULL, 16);
356 boot_device = argv[2];
359 printf ("Usage:\n%s\n", cmdtp->usage);
360 SHOW_BOOT_PROGRESS (-1);
365 puts ("\n** No boot device **\n");
366 SHOW_BOOT_PROGRESS (-1);
370 dev = simple_strtoul(boot_device, &ep, 16);
372 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
373 printf ("\n** Device %d not available\n", dev);
374 SHOW_BOOT_PROGRESS (-1);
380 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
381 SHOW_BOOT_PROGRESS (-1);
384 part = simple_strtoul(++ep, NULL, 16);
386 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
387 SHOW_BOOT_PROGRESS (-1);
390 if (strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
391 printf ("\n** Invalid partition type \"%.32s\""
392 " (expect \"" BOOT_PART_TYPE "\")\n",
394 SHOW_BOOT_PROGRESS (-1);
398 printf ("\nLoading from IDE device %d, partition %d: "
399 "Name: %.32s Type: %.32s\n",
400 dev, part, info.name, info.type);
402 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
403 info.start, info.size, info.blksz);
405 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
406 printf ("** Read error on %d:%d\n", dev, part);
407 SHOW_BOOT_PROGRESS (-1);
411 hdr = (image_header_t *)addr;
413 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
415 print_image_hdr (hdr);
417 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
418 cnt += info.blksz - 1;
422 printf("\n** Bad Magic Number **\n");
423 SHOW_BOOT_PROGRESS (-1);
427 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
428 (ulong *)(addr+info.blksz)) != cnt) {
429 printf ("** Read error on %d:%d\n", dev, part);
430 SHOW_BOOT_PROGRESS (-1);
435 /* Loading ok, update default load address */
439 /* Check if we should attempt an auto-start */
440 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
442 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
444 local_args[0] = argv[0];
445 local_args[1] = NULL;
447 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
449 do_bootm (cmdtp, 0, 1, local_args);
455 /* ------------------------------------------------------------------------- */
459 DECLARE_GLOBAL_DATA_PTR;
461 #ifdef CONFIG_IDE_8xx_DIRECT
462 volatile immap_t *immr = (immap_t *)CFG_IMMR;
463 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
468 #ifdef CONFIG_IDE_8xx_PCCARD
469 extern int pcmcia_on (void);
473 /* initialize the PCMCIA IDE adapter card */
476 udelay (1000000); /* 1 s */
477 #endif /* CONFIG_IDE_8xx_PCCARD */
481 /* Initialize PIO timing tables */
482 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
483 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
485 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
487 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
489 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
491 " hold=%2d ns/%d clk\n",
493 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
494 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
495 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
498 /* Reset the IDE just to be sure.
499 * Light LED's to show
501 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
502 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
504 #ifdef CONFIG_IDE_8xx_DIRECT
505 /* PCMCIA / IDE initialization for common mem space */
506 pcmp->pcmc_pgcrb = 0;
509 /* start in PIO mode 0 - most relaxed timings */
511 set_pcmcia_timing (pio_mode);
514 * Wait for IDE to get ready.
515 * According to spec, this can take up to 31 seconds!
517 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
518 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
520 printf ("Bus %d: ", bus);
526 udelay (100000); /* 100 ms */
527 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
528 udelay (100000); /* 100 ms */
532 udelay (10000); /* 10 ms */
534 c = ide_inb (dev, ATA_STATUS);
536 if (i > (ATA_RESET_TIME * 100)) {
537 puts ("** Timeout **\n");
538 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
541 if ((i >= 100) && ((i%100)==0)) {
544 } while (c & ATA_STAT_BUSY);
546 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
547 puts ("not available ");
548 PRINTF ("Status = 0x%02X ", c);
549 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
550 } else if ((c & ATA_STAT_READY) == 0) {
551 puts ("not available ");
552 PRINTF ("Status = 0x%02X ", c);
562 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
565 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
566 #ifdef CONFIG_IDE_LED
567 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
569 ide_dev_desc[i].if_type=IF_TYPE_IDE;
570 ide_dev_desc[i].dev=i;
571 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
572 ide_dev_desc[i].blksz=0;
573 ide_dev_desc[i].lba=0;
574 ide_dev_desc[i].block_read=ide_read;
575 if (!ide_bus_ok[IDE_BUS(i)])
577 ide_led (led, 1); /* LED on */
578 ide_ident(&ide_dev_desc[i]);
579 ide_led (led, 0); /* LED off */
580 dev_print(&ide_dev_desc[i]);
582 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
583 init_part (&ide_dev_desc[i]); /* initialize partition type */
591 /* ------------------------------------------------------------------------- */
593 block_dev_desc_t * ide_get_dev(int dev)
595 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
599 #ifdef CONFIG_IDE_8xx_DIRECT
602 set_pcmcia_timing (int pmode)
604 volatile immap_t *immr = (immap_t *)CFG_IMMR;
605 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
608 PRINTF ("Set timing for PIO Mode %d\n", pmode);
610 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
611 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
612 | PCMCIA_SL (pio_config_clk[pmode].t_length)
617 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
618 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
619 #if (CFG_PCMCIA_POR0 != 0)
623 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
625 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
626 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
627 #if (CFG_PCMCIA_POR1 != 0)
631 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
633 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
634 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
635 #if (CFG_PCMCIA_POR2 != 0)
639 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
641 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
642 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
643 #if (CFG_PCMCIA_POR3 != 0)
647 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
651 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
652 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
653 #if (CFG_PCMCIA_POR4 != 0)
657 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
659 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
660 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
661 #if (CFG_PCMCIA_POR5 != 0)
665 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
667 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
668 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
669 #if (CFG_PCMCIA_POR6 != 0)
673 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
675 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
676 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
677 #if (CFG_PCMCIA_POR7 != 0)
681 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
685 #endif /* CONFIG_IDE_8xx_DIRECT */
687 /* ------------------------------------------------------------------------- */
690 static void __inline__
691 ide_outb(int dev, int port, unsigned char val)
693 /* Ensure I/O operations complete */
694 __asm__ volatile("eieio");
695 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
697 printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
700 #else /* ! __PPC__ */
701 static void __inline__
702 ide_outb(int dev, int port, unsigned char val)
710 static unsigned char __inline__
711 ide_inb(int dev, int port)
714 /* Ensure I/O operations complete */
715 __asm__ volatile("eieio");
716 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
718 printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
722 #else /* ! __PPC__ */
723 static unsigned char __inline__
724 ide_inb(int dev, int port)
731 __inline__ unsigned ld_le16(const volatile unsigned short *addr)
735 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r"(val) : "r"(addr), "m"(*addr));
740 input_swap_data(int dev, ulong *sect_buf, int words)
742 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
743 ushort *dbuf = (ushort *)sect_buf;
746 *dbuf++ = ld_le16(pbuf);
747 *dbuf++ = ld_le16(pbuf);
750 #else /* ! __PPC__ */
751 #define input_swap_data(x,y,z) input_data(x,y,z)
759 output_data(int dev, ulong *sect_buf, int words)
762 volatile ushort *pbuf;
764 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
765 dbuf = (ushort *)sect_buf;
767 __asm__ volatile ("eieio");
769 __asm__ volatile ("eieio");
773 #else /* ! __PPC__ */
775 output_data(int dev, ulong *sect_buf, int words)
777 outsw(ATA_DATA_REG, sect_buf, words<<1);
783 input_data(int dev, ulong *sect_buf, int words)
786 volatile ushort *pbuf;
788 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
789 dbuf = (ushort *)sect_buf;
791 __asm__ volatile ("eieio");
793 __asm__ volatile ("eieio");
797 #else /* ! __PPC__ */
799 input_data(int dev, ulong *sect_buf, int words)
801 insw(ATA_DATA_REG, sect_buf, words << 1);
806 /* -------------------------------------------------------------------------
808 static void ide_ident (block_dev_desc_t *dev_desc)
810 ulong iobuf[ATA_SECTORWORDS];
812 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
815 int mode, cycle_time;
818 device=dev_desc->dev;
819 printf (" Device %d: ", device);
821 ide_led (DEVICE_LED(device), 1); /* LED on */
824 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
825 dev_desc->if_type=IF_TYPE_IDE;
827 /* check signature */
828 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
829 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
830 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
831 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
832 /* ATAPI Signature found */
833 dev_desc->if_type=IF_TYPE_ATAPI;
834 /* Start Ident Command
836 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
838 * Wait for completion - ATAPI devices need more time
841 c = ide_wait (device, ATAPI_TIME_OUT);
846 /* Start Ident Command
848 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
850 /* Wait for completion
852 c = ide_wait (device, IDE_TIME_OUT);
854 ide_led (DEVICE_LED(device), 0); /* LED off */
856 if (((c & ATA_STAT_DRQ) == 0) ||
857 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
858 dev_desc->type=DEV_TYPE_UNKNOWN;
862 input_swap_data (device, iobuf, ATA_SECTORWORDS);
864 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
865 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
866 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
868 if ((iop->config & 0x0080)==0x0080)
869 dev_desc->removable = 1;
871 dev_desc->removable = 0;
875 * Drive PIO mode autoselection
879 printf ("tPIO = 0x%02x = %d\n",mode, mode);
880 if (mode > 2) { /* 2 is maximum allowed tPIO value */
882 PRINTF ("Override tPIO -> 2\n");
884 if (iop->field_valid & 2) { /* drive implements ATA2? */
885 PRINTF ("Drive implements ATA2\n");
886 if (iop->capability & 8) { /* drive supports use_iordy? */
887 cycle_time = iop->eide_pio_iordy;
889 cycle_time = iop->eide_pio;
891 PRINTF ("cycle time = %d\n", cycle_time);
893 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
894 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
895 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
896 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
898 printf ("PIO mode to use: PIO %d\n", mode);
902 if (dev_desc->if_type==IF_TYPE_ATAPI) {
903 atapi_inquiry(dev_desc);
906 #endif /* CONFIG_ATAPI */
909 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
911 dev_desc->type=DEV_TYPE_HARDDISK;
912 dev_desc->blksz=ATA_BLOCKSIZE;
913 dev_desc->lun=0; /* just to fill something in... */
915 #if 0 /* only used to test the powersaving mode,
916 * if enabled, the drive goes after 5 sec
918 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
919 c = ide_wait (device, IDE_TIME_OUT);
920 ide_outb (device, ATA_SECT_CNT, 1);
921 ide_outb (device, ATA_LBA_LOW, 0);
922 ide_outb (device, ATA_LBA_MID, 0);
923 ide_outb (device, ATA_LBA_HIGH, 0);
924 ide_outb (device, ATA_DEV_HD, ATA_LBA |
926 ide_outb (device, ATA_COMMAND, 0xe3);
928 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
933 /* ------------------------------------------------------------------------- */
935 ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
939 unsigned char pwrsave=0; /* power save */
941 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
942 device, blknr, blkcnt, (ulong)buffer);
944 ide_led (DEVICE_LED(device), 1); /* LED on */
948 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
949 c = ide_wait (device, IDE_TIME_OUT);
951 if (c & ATA_STAT_BUSY) {
952 printf ("IDE read: device %d not ready\n", device);
956 /* first check if the drive is in Powersaving mode, if yes,
957 * increase the timeout value */
958 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
961 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
963 if (c & ATA_STAT_BUSY) {
964 printf ("IDE read: device %d not ready\n", device);
967 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
968 printf ("No Powersaving mode %X\n", c);
970 c = ide_inb(device,ATA_SECT_CNT);
971 PRINTF("Powersaving %02X\n",c);
977 while (blkcnt-- > 0) {
979 c = ide_wait (device, IDE_TIME_OUT);
981 if (c & ATA_STAT_BUSY) {
982 printf ("IDE read: device %d not ready\n", device);
986 ide_outb (device, ATA_SECT_CNT, 1);
987 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
988 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
989 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
990 ide_outb (device, ATA_DEV_HD, ATA_LBA |
992 ((blknr >> 24) & 0xF) );
993 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
998 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1001 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1004 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1005 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1010 input_data (device, buffer, ATA_SECTORWORDS);
1011 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
1015 buffer += ATA_SECTORWORDS;
1018 ide_led (DEVICE_LED(device), 0); /* LED off */
1022 /* ------------------------------------------------------------------------- */
1025 ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1030 ide_led (DEVICE_LED(device), 1); /* LED on */
1034 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1036 while (blkcnt-- > 0) {
1038 c = ide_wait (device, IDE_TIME_OUT);
1040 if (c & ATA_STAT_BUSY) {
1041 printf ("IDE read: device %d not ready\n", device);
1045 ide_outb (device, ATA_SECT_CNT, 1);
1046 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1047 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1048 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1049 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1050 ATA_DEVICE(device) |
1051 ((blknr >> 24) & 0xF) );
1052 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1056 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1058 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1059 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1064 output_data (device, buffer, ATA_SECTORWORDS);
1065 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
1068 buffer += ATA_SECTORWORDS;
1071 ide_led (DEVICE_LED(device), 0); /* LED off */
1075 /* ------------------------------------------------------------------------- */
1078 * copy src to dest, skipping leading and trailing blanks and null
1079 * terminate the string
1081 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1087 if (src[start]!=' ')
1097 for ( ; start<=end; start++) {
1103 /* ------------------------------------------------------------------------- */
1106 * Wait until Busy bit is off, or timeout (in ms)
1107 * Return last status
1109 static uchar ide_wait (int dev, ulong t)
1111 ulong delay = 10 * t; /* poll every 100 us */
1114 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1123 /* ------------------------------------------------------------------------- */
1125 #ifdef CONFIG_IDE_RESET
1126 extern void ide_set_reset(int idereset);
1128 static void ide_reset (void)
1130 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1131 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1136 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1138 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1139 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1141 ide_set_reset (1); /* assert reset */
1145 #ifdef CFG_PB_12V_ENABLE
1146 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1147 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1148 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1149 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1151 /* wait 500 ms for the voltage to stabilize
1153 for (i=0; i<500; ++i) {
1157 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1158 #endif /* CFG_PB_12V_ENABLE */
1160 #ifdef CFG_PB_IDE_MOTOR
1161 /* configure IDE Motor voltage monitor pin as input */
1162 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1163 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1164 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1166 /* wait up to 1 s for the motor voltage to stabilize
1168 for (i=0; i<1000; ++i) {
1169 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1175 if (i == 1000) { /* Timeout */
1176 printf ("\nWarning: 5V for IDE Motor missing\n");
1177 # ifdef CONFIG_STATUS_LED
1178 # ifdef STATUS_LED_YELLOW
1179 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1181 # ifdef STATUS_LED_GREEN
1182 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1184 # endif /* CONFIG_STATUS_LED */
1186 #endif /* CFG_PB_IDE_MOTOR */
1190 /* de-assert RESET signal */
1194 for (i=0; i<250; ++i) {
1199 #endif /* CONFIG_IDE_RESET */
1201 /* ------------------------------------------------------------------------- */
1203 #ifdef CONFIG_IDE_LED
1205 static uchar led_buffer = 0; /* Buffer for current LED status */
1207 static void ide_led (uchar led, uchar status)
1209 uchar *led_port = LED_PORT;
1211 if (status) { /* switch LED on */
1213 } else { /* switch LED off */
1217 *led_port = led_buffer;
1220 #endif /* CONFIG_IDE_LED */
1222 /* ------------------------------------------------------------------------- */
1225 /****************************************************************************
1234 #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1236 #define AT_PRINTF(fmt,args...)
1240 /* since ATAPI may use commands with not 4 bytes alligned length
1241 * we have our own transfer functions, 2 bytes alligned */
1243 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1246 volatile ushort *pbuf;
1248 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1249 dbuf = (ushort *)sect_buf;
1251 __asm__ volatile ("eieio");
1257 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1260 volatile ushort *pbuf;
1262 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1263 dbuf = (ushort *)sect_buf;
1265 __asm__ volatile ("eieio");
1270 #else /* ! __PPC__ */
1272 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1274 outsw(ATA_DATA_REG, sect_buf, shorts);
1279 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1281 insw(ATA_DATA_REG, sect_buf, shorts);
1284 #endif /* __PPC__ */
1287 * Wait until (Status & mask) == res, or timeout (in ms)
1288 * Return last status
1289 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1290 * and then they set their DRQ Bit
1292 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1294 ulong delay = 10 * t; /* poll every 100 us */
1297 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1298 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1299 /* break if error occurs (doesn't make sense to wait more) */
1300 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1311 * issue an atapi command
1313 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1315 unsigned char c,err,mask,res;
1317 ide_led (DEVICE_LED(device), 1); /* LED on */
1321 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1323 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1324 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1325 if ((c & mask) != res) {
1326 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1330 /* write taskfile */
1331 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1332 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
1333 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen<<8) & 0xFF));
1334 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1336 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
1339 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1341 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1343 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1344 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1349 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1350 /* ATAPI Command written wait for completition */
1351 udelay (5000); /* device must set bsy */
1353 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1354 /* if no data wait for DRQ = 0 BSY = 0
1355 * if data wait for DRQ = 1 BSY = 0 */
1359 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1360 if ((c & mask) != res ) {
1361 if (c & ATA_STAT_ERR) {
1362 err=(ide_inb(device,ATA_ERROR_REG))>>4;
1363 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1365 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1370 n=ide_inb(device, ATA_CYL_HIGH);
1372 n+=ide_inb(device, ATA_CYL_LOW);
1374 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1378 if((n==0)&&(buflen<0)) {
1379 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1384 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1386 if(n!=0) { /* data transfer */
1387 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1388 /* we transfer shorts */
1390 /* ok now decide if it is an in or output */
1391 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1392 AT_PRINTF("Write to device\n");
1393 output_data_shorts(device,(unsigned short *)buffer,n);
1395 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1396 input_data_shorts(device,(unsigned short *)buffer,n);
1399 udelay(5000); /* seems that some CD ROMs need this... */
1400 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1402 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1403 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1404 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1405 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1410 ide_led (DEVICE_LED(device), 0); /* LED off */
1415 * sending the command to atapi_issue. If an status other than good
1416 * returns, an request_sense will be issued
1419 #define ATAPI_DRIVE_NOT_READY 100
1420 #define ATAPI_UNIT_ATTN 10
1422 unsigned char atapi_issue_autoreq (int device,
1425 unsigned char *buffer,
1428 unsigned char sense_data[18],sense_ccb[12];
1429 unsigned char res,key,asc,ascq;
1430 int notready,unitattn;
1432 unitattn=ATAPI_UNIT_ATTN;
1433 notready=ATAPI_DRIVE_NOT_READY;
1436 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1438 return (0); /* Ok */
1441 return (0xFF); /* error */
1443 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1445 memset(sense_ccb,0,sizeof(sense_ccb));
1446 memset(sense_data,0,sizeof(sense_data));
1447 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1448 sense_ccb[4]=18; /* allocation Legnth */
1450 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1451 key=(sense_data[2]&0xF);
1452 asc=(sense_data[12]);
1453 ascq=(sense_data[13]);
1455 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1456 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1463 return 0; /* ok device ready */
1465 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1470 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1473 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1478 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1482 AT_PRINTF("Media not present\n");
1485 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1487 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1493 static void atapi_inquiry(block_dev_desc_t * dev_desc)
1495 unsigned char ccb[12]; /* Command descriptor block */
1496 unsigned char iobuf[64]; /* temp buf */
1500 device=dev_desc->dev;
1501 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1502 dev_desc->block_read=atapi_read;
1504 memset(ccb,0,sizeof(ccb));
1505 memset(iobuf,0,sizeof(iobuf));
1507 ccb[0]=ATAPI_CMD_INQUIRY;
1508 ccb[4]=40; /* allocation Legnth */
1509 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1511 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1515 /* copy device ident strings */
1516 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1517 ident_cpy(dev_desc->product,&iobuf[16],16);
1518 ident_cpy(dev_desc->revision,&iobuf[32],5);
1523 dev_desc->type=iobuf[0] & 0x1f;
1525 if ((iobuf[1]&0x80)==0x80)
1526 dev_desc->removable = 1;
1528 dev_desc->removable = 0;
1530 memset(ccb,0,sizeof(ccb));
1531 memset(iobuf,0,sizeof(iobuf));
1532 ccb[0]=ATAPI_CMD_START_STOP;
1533 ccb[4]=0x03; /* start */
1535 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1537 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1541 memset(ccb,0,sizeof(ccb));
1542 memset(iobuf,0,sizeof(iobuf));
1543 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1545 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1549 memset(ccb,0,sizeof(ccb));
1550 memset(iobuf,0,sizeof(iobuf));
1551 ccb[0]=ATAPI_CMD_READ_CAP;
1552 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1553 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1557 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1558 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1559 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1561 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1562 ((unsigned long)iobuf[1]<<16) +
1563 ((unsigned long)iobuf[2]<< 8) +
1564 ((unsigned long)iobuf[3]);
1565 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1566 ((unsigned long)iobuf[5]<<16) +
1567 ((unsigned long)iobuf[6]<< 8) +
1568 ((unsigned long)iobuf[7]);
1575 * we transfer only one block per command, since the multiple DRQ per
1576 * command is not yet implemented
1578 #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1579 #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1580 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1582 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1585 unsigned char ccb[12]; /* Command descriptor block */
1588 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1589 device, blknr, blkcnt, (ulong)buffer);
1592 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1593 cnt=ATAPI_READ_MAX_BLOCK;
1597 ccb[0]=ATAPI_CMD_READ_12;
1598 ccb[1]=0; /* reserved */
1599 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1600 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1601 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1602 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1603 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1604 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1605 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1606 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1607 ccb[10]=0; /* reserved */
1608 ccb[11]=0; /* reserved */
1610 if (atapi_issue_autoreq(device,ccb,12,
1611 (unsigned char *)buffer,
1612 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1618 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1619 } while (blkcnt > 0);
1623 /* ------------------------------------------------------------------------- */
1625 #endif /* CONFIG_ATAPI */
1627 #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */