]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_ide.c
* Patches by Thomas Lange, 09 Oct 2003:
[karo-tx-uboot.git] / common / cmd_ide.c
1 /*
2  * (C) Copyright 2000-2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  *
23  */
24
25 /*
26  * IDE support
27  */
28 #include <common.h>
29 #include <config.h>
30 #include <watchdog.h>
31 #include <command.h>
32 #include <image.h>
33 #include <asm/byteorder.h>
34 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
35 # include <pcmcia.h>
36 #endif
37 #ifdef CONFIG_8xx
38 # include <mpc8xx.h>
39 #endif
40 #include <ide.h>
41 #include <ata.h>
42 #ifdef CONFIG_STATUS_LED
43 # include <status_led.h>
44 #endif
45 #ifndef __PPC__
46 #include <asm/io.h>
47 #ifdef __MIPS__
48 /* Macros depend on this variable */
49 static unsigned long mips_io_port_base = 0;
50 #endif
51 #endif
52
53 #ifdef CONFIG_SHOW_BOOT_PROGRESS
54 # include <status_led.h>
55 # define SHOW_BOOT_PROGRESS(arg)        show_boot_progress(arg)
56 #else
57 # define SHOW_BOOT_PROGRESS(arg)
58 #endif
59
60
61 #undef  IDE_DEBUG
62
63
64 #ifdef  IDE_DEBUG
65 #define PRINTF(fmt,args...)     printf (fmt ,##args)
66 #else
67 #define PRINTF(fmt,args...)
68 #endif
69
70 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
71
72 #ifdef CONFIG_IDE_8xx_DIRECT
73 /* Timings for IDE Interface
74  *
75  * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
76  * 70      165      30     PIO-Mode 0, [ns]
77  *  4        9       2                 [Cycles]
78  * 50      125      20     PIO-Mode 1, [ns]
79  *  3        7       2                 [Cycles]
80  * 30      100      15     PIO-Mode 2, [ns]
81  *  2        6       1                 [Cycles]
82  * 30       80      10     PIO-Mode 3, [ns]
83  *  2        5       1                 [Cycles]
84  * 25       70      10     PIO-Mode 4, [ns]
85  *  2        4       1                 [Cycles]
86  */
87
88 const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
89 {
90     /*  Setup  Length  Hold  */
91         { 70,   165,    30 },           /* PIO-Mode 0, [ns]     */
92         { 50,   125,    20 },           /* PIO-Mode 1, [ns]     */
93         { 30,   101,    15 },           /* PIO-Mode 2, [ns]     */
94         { 30,    80,    10 },           /* PIO-Mode 3, [ns]     */
95         { 25,    70,    10 },           /* PIO-Mode 4, [ns]     */
96 };
97
98 static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
99
100 #ifndef CFG_PIO_MODE
101 #define CFG_PIO_MODE    0               /* use a relaxed default */
102 #endif
103 static int pio_mode = CFG_PIO_MODE;
104
105 /* Make clock cycles and always round up */
106
107 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
108
109 #endif /* CONFIG_IDE_8xx_DIRECT */
110
111 /* ------------------------------------------------------------------------- */
112
113 /* Current I/O Device   */
114 static int curr_device = -1;
115
116 /* Current offset for IDE0 / IDE1 bus access    */
117 ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
118 #if defined(CFG_ATA_IDE0_OFFSET)
119         CFG_ATA_IDE0_OFFSET,
120 #endif
121 #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
122         CFG_ATA_IDE1_OFFSET,
123 #endif
124 };
125
126
127 #define ATA_CURR_BASE(dev)      (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
128
129 #ifndef CONFIG_AMIGAONEG3SE
130 static int          ide_bus_ok[CFG_IDE_MAXBUS];
131 #else
132 static int          ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
133 #endif
134
135 static  block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
136 /* ------------------------------------------------------------------------- */
137
138 #ifdef CONFIG_IDE_LED
139 #ifndef CONFIG_KUP4K
140 static void  ide_led   (uchar led, uchar status);
141 #else
142 extern void  ide_led   (uchar led, uchar status);
143 #endif
144 #else
145 #ifndef CONFIG_AMIGAONEG3SE
146 #define ide_led(a,b)    /* dummy */
147 #else
148 extern void ide_led(uchar led, uchar status);
149 #define LED_IDE1  1
150 #define LED_IDE2  2
151 #define CONFIG_IDE_LED 1
152 #define DEVICE_LED(x) 1
153 #endif
154 #endif
155
156 #ifdef CONFIG_IDE_RESET
157 static void  ide_reset (void);
158 #else
159 #define ide_reset()     /* dummy */
160 #endif
161
162 static void  ide_ident (block_dev_desc_t *dev_desc);
163 static uchar ide_wait  (int dev, ulong t);
164
165 #define IDE_TIME_OUT    2000    /* 2 sec timeout */
166
167 #define ATAPI_TIME_OUT  7000    /* 7 sec timeout (5 sec seems to work...) */
168
169 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
170
171 static void __inline__ ide_outb(int dev, int port, unsigned char val);
172 static unsigned char __inline__ ide_inb(int dev, int port);
173 #ifdef __PPC__
174 static void input_swap_data(int dev, ulong *sect_buf, int words);
175 #endif
176 static void input_data(int dev, ulong *sect_buf, int words);
177 static void output_data(int dev, ulong *sect_buf, int words);
178 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
179
180
181 #ifdef CONFIG_ATAPI
182 static void     atapi_inquiry(block_dev_desc_t *dev_desc);
183 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
184 #endif
185
186
187 #ifdef CONFIG_IDE_8xx_DIRECT
188 static void set_pcmcia_timing (int pmode);
189 #endif
190
191 /* ------------------------------------------------------------------------- */
192
193 int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
194 {
195     int rcode = 0;
196
197     switch (argc) {
198     case 0:
199     case 1:
200         printf ("Usage:\n%s\n", cmdtp->usage);
201         return 1;
202     case 2:
203         if (strncmp(argv[1],"res",3) == 0) {
204                 puts ("\nReset IDE"
205 #ifdef CONFIG_IDE_8xx_DIRECT
206                         " on PCMCIA " PCMCIA_SLOT_MSG
207 #endif
208                         ": ");
209
210                 ide_init ();
211                 return 0;
212         } else if (strncmp(argv[1],"inf",3) == 0) {
213                 int i;
214
215                 putc ('\n');
216
217                 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
218                         if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
219                                 continue; /* list only known devices */
220                         printf ("IDE device %d: ", i);
221                         dev_print(&ide_dev_desc[i]);
222                 }
223                 return 0;
224
225         } else if (strncmp(argv[1],"dev",3) == 0) {
226                 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
227                         puts ("\nno IDE devices available\n");
228                         return 1;
229                 }
230                 printf ("\nIDE device %d: ", curr_device);
231                 dev_print(&ide_dev_desc[curr_device]);
232                 return 0;
233         } else if (strncmp(argv[1],"part",4) == 0) {
234                 int dev, ok;
235
236                 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
237                         if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
238                                 ++ok;
239                                 if (dev)
240                                         putc ('\n');
241                                 print_part(&ide_dev_desc[dev]);
242                         }
243                 }
244                 if (!ok) {
245                         puts ("\nno IDE devices available\n");
246                         rcode ++;
247                 }
248                 return rcode;
249         }
250         printf ("Usage:\n%s\n", cmdtp->usage);
251         return 1;
252     case 3:
253         if (strncmp(argv[1],"dev",3) == 0) {
254                 int dev = (int)simple_strtoul(argv[2], NULL, 10);
255
256                 printf ("\nIDE device %d: ", dev);
257                 if (dev >= CFG_IDE_MAXDEVICE) {
258                         puts ("unknown device\n");
259                         return 1;
260                 }
261                 dev_print(&ide_dev_desc[dev]);
262                 /*ide_print (dev);*/
263
264                 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
265                         return 1;
266                 }
267
268                 curr_device = dev;
269
270                 puts ("... is now current device\n");
271
272                 return 0;
273         } else if (strncmp(argv[1],"part",4) == 0) {
274                 int dev = (int)simple_strtoul(argv[2], NULL, 10);
275
276                 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
277                                 print_part(&ide_dev_desc[dev]);
278                 } else {
279                         printf ("\nIDE device %d not available\n", dev);
280                         rcode = 1;
281                 }
282                 return rcode;
283 #if 0
284         } else if (strncmp(argv[1],"pio",4) == 0) {
285                 int mode = (int)simple_strtoul(argv[2], NULL, 10);
286
287                 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
288                         puts ("\nSetting ");
289                         pio_mode = mode;
290                         ide_init ();
291                 } else {
292                         printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
293                                 mode, IDE_MAX_PIO_MODE);
294                 }
295                 return;
296 #endif
297         }
298
299         printf ("Usage:\n%s\n", cmdtp->usage);
300         return 1;
301     default:
302         /* at least 4 args */
303
304         if (strcmp(argv[1],"read") == 0) {
305                 ulong addr = simple_strtoul(argv[2], NULL, 16);
306                 ulong blk  = simple_strtoul(argv[3], NULL, 16);
307                 ulong cnt  = simple_strtoul(argv[4], NULL, 16);
308                 ulong n;
309
310                 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
311                         curr_device, blk, cnt);
312
313                 n = ide_dev_desc[curr_device].block_read (curr_device,
314                                                           blk, cnt,
315                                                           (ulong *)addr);
316                 /* flush cache after read */
317                 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
318
319                 printf ("%ld blocks read: %s\n",
320                         n, (n==cnt) ? "OK" : "ERROR");
321                 if (n==cnt) {
322                         return 0;
323                 } else {
324                         return 1;
325                 }
326         } else if (strcmp(argv[1],"write") == 0) {
327                 ulong addr = simple_strtoul(argv[2], NULL, 16);
328                 ulong blk  = simple_strtoul(argv[3], NULL, 16);
329                 ulong cnt  = simple_strtoul(argv[4], NULL, 16);
330                 ulong n;
331
332                 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
333                         curr_device, blk, cnt);
334
335                 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
336
337                 printf ("%ld blocks written: %s\n",
338                         n, (n==cnt) ? "OK" : "ERROR");
339                 if (n==cnt) {
340                         return 0;
341                 } else {
342                         return 1;
343                 }
344         } else {
345                 printf ("Usage:\n%s\n", cmdtp->usage);
346                 rcode = 1;
347         }
348
349         return rcode;
350     }
351 }
352
353 int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
354 {
355         char *boot_device = NULL;
356         char *ep;
357         int dev, part = 0;
358         ulong cnt;
359         ulong addr;
360         disk_partition_t info;
361         image_header_t *hdr;
362         int rcode = 0;
363
364         switch (argc) {
365         case 1:
366                 addr = CFG_LOAD_ADDR;
367                 boot_device = getenv ("bootdevice");
368                 break;
369         case 2:
370                 addr = simple_strtoul(argv[1], NULL, 16);
371                 boot_device = getenv ("bootdevice");
372                 break;
373         case 3:
374                 addr = simple_strtoul(argv[1], NULL, 16);
375                 boot_device = argv[2];
376                 break;
377         default:
378                 printf ("Usage:\n%s\n", cmdtp->usage);
379                 SHOW_BOOT_PROGRESS (-1);
380                 return 1;
381         }
382
383         if (!boot_device) {
384                 puts ("\n** No boot device **\n");
385                 SHOW_BOOT_PROGRESS (-1);
386                 return 1;
387         }
388
389         dev = simple_strtoul(boot_device, &ep, 16);
390
391         if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
392                 printf ("\n** Device %d not available\n", dev);
393                 SHOW_BOOT_PROGRESS (-1);
394                 return 1;
395         }
396
397         if (*ep) {
398                 if (*ep != ':') {
399                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
400                         SHOW_BOOT_PROGRESS (-1);
401                         return 1;
402                 }
403                 part = simple_strtoul(++ep, NULL, 16);
404         }
405         if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
406                 SHOW_BOOT_PROGRESS (-1);
407                 return 1;
408         }
409         if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
410             (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
411                 printf ("\n** Invalid partition type \"%.32s\""
412                         " (expect \"" BOOT_PART_TYPE "\")\n",
413                         info.type);
414                 SHOW_BOOT_PROGRESS (-1);
415                 return 1;
416         }
417
418         printf ("\nLoading from IDE device %d, partition %d: "
419                 "Name: %.32s  Type: %.32s\n",
420                 dev, part, info.name, info.type);
421
422         PRINTF ("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
423                 info.start, info.size, info.blksz);
424
425         if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
426                 printf ("** Read error on %d:%d\n", dev, part);
427                 SHOW_BOOT_PROGRESS (-1);
428                 return 1;
429         }
430
431         hdr = (image_header_t *)addr;
432
433         if (ntohl(hdr->ih_magic) == IH_MAGIC) {
434
435                 print_image_hdr (hdr);
436
437                 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
438                 cnt += info.blksz - 1;
439                 cnt /= info.blksz;
440                 cnt -= 1;
441         } else {
442                 printf("\n** Bad Magic Number **\n");
443                 SHOW_BOOT_PROGRESS (-1);
444                 return 1;
445         }
446
447         if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
448                       (ulong *)(addr+info.blksz)) != cnt) {
449                 printf ("** Read error on %d:%d\n", dev, part);
450                 SHOW_BOOT_PROGRESS (-1);
451                 return 1;
452         }
453
454
455         /* Loading ok, update default load address */
456
457         load_addr = addr;
458
459         /* Check if we should attempt an auto-start */
460         if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
461                 char *local_args[2];
462                 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
463
464                 local_args[0] = argv[0];
465                 local_args[1] = NULL;
466
467                 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
468
469                 do_bootm (cmdtp, 0, 1, local_args);
470                 rcode = 1;
471         }
472         return rcode;
473 }
474
475 /* ------------------------------------------------------------------------- */
476
477 void ide_init (void)
478 {
479
480 #ifdef CONFIG_IDE_8xx_DIRECT
481         DECLARE_GLOBAL_DATA_PTR;
482         volatile immap_t *immr = (immap_t *)CFG_IMMR;
483         volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
484 #endif
485         unsigned char c;
486         int i, bus;
487 #ifdef CONFIG_AMIGAONEG3SE
488         unsigned int max_bus_scan;
489         unsigned int ata_reset_time;
490         char *s;
491 #endif
492
493 #ifdef CONFIG_IDE_8xx_PCCARD
494         extern int pcmcia_on (void);
495         extern int ide_devices_found; /* Initialized in check_ide_device() */
496
497         WATCHDOG_RESET();
498
499         ide_devices_found = 0;
500         /* initialize the PCMCIA IDE adapter card */
501         pcmcia_on();
502         if (!ide_devices_found)
503                 return;
504         udelay (1000000);       /* 1 s */
505 #endif  /* CONFIG_IDE_8xx_PCCARD */
506
507         WATCHDOG_RESET();
508
509 #ifdef CONFIG_IDE_8xx_DIRECT
510         /* Initialize PIO timing tables */
511         for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
512             pio_config_clk[i].t_setup  = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
513                                                             gd->bus_clk);
514             pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
515                                                             gd->bus_clk);
516             pio_config_clk[i].t_hold   = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
517                                                             gd->bus_clk);
518             PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
519                     "  len=%3d ns/%d clk"
520                     "  hold=%2d ns/%d clk\n",
521                     i,
522                     pio_config_ns[i].t_setup,  pio_config_clk[i].t_setup,
523                     pio_config_ns[i].t_length, pio_config_clk[i].t_length,
524                     pio_config_ns[i].t_hold,   pio_config_clk[i].t_hold);
525         }
526 #endif /* CONFIG_IDE_8xx_DIRECT */
527
528         /* Reset the IDE just to be sure.
529          * Light LED's to show
530          */
531         ide_led ((LED_IDE1 | LED_IDE2), 1);             /* LED's on     */
532         ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
533
534 #ifdef CONFIG_IDE_8xx_DIRECT
535         /* PCMCIA / IDE initialization for common mem space */
536         pcmp->pcmc_pgcrb = 0;
537
538         /* start in PIO mode 0 - most relaxed timings */
539         pio_mode = 0;
540         set_pcmcia_timing (pio_mode);
541 #endif /* CONFIG_IDE_8xx_DIRECT */
542
543         /*
544          * Wait for IDE to get ready.
545          * According to spec, this can take up to 31 seconds!
546          */
547 #ifndef CONFIG_AMIGAONEG3SE
548         for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
549                 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
550 #else
551         s = getenv("ide_maxbus");
552         if (s)
553             max_bus_scan = simple_strtol(s, NULL, 10);
554         else
555             max_bus_scan = CFG_IDE_MAXBUS;
556
557         for (bus=0; bus<max_bus_scan; ++bus) {
558                 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
559 #endif
560
561 #ifdef CONFIG_IDE_8xx_PCCARD
562                 /* Skip non-ide devices from probing */
563                 if ((ide_devices_found & (1 << bus)) == 0) {
564                         ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
565                         continue;
566                 }
567 #endif
568                 printf ("Bus %d: ", bus);
569
570                 ide_bus_ok[bus] = 0;
571
572                 /* Select device
573                  */
574                 udelay (100000);                /* 100 ms */
575                 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
576                 udelay (100000);                /* 100 ms */
577 #ifdef CONFIG_AMIGAONEG3SE
578                 ata_reset_time = ATA_RESET_TIME;
579                 s = getenv("ide_reset_timeout");
580                 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
581 #endif
582                 i = 0;
583                 do {
584                         udelay (10000);         /* 10 ms */
585
586                         c = ide_inb (dev, ATA_STATUS);
587                         i++;
588 #ifdef CONFIG_AMIGAONEG3SE
589                         if (i > (ata_reset_time * 100)) {
590 #else
591                         if (i > (ATA_RESET_TIME * 100)) {
592 #endif
593                                 puts ("** Timeout **\n");
594                                 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
595 #ifdef CONFIG_AMIGAONEG3SE
596                                 /* If this is the second bus, the first one was OK */
597                                 if (bus != 0)
598                                 {
599                                     ide_bus_ok[bus] = 0;
600                                     goto skip_bus;
601                                 }
602 #endif
603                                 return;
604                         }
605                         if ((i >= 100) && ((i%100)==0)) {
606                                 putc ('.');
607                         }
608                 } while (c & ATA_STAT_BUSY);
609
610                 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
611                         puts ("not available  ");
612                         PRINTF ("Status = 0x%02X ", c);
613 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
614                 } else  if ((c & ATA_STAT_READY) == 0) {
615                         puts ("not available  ");
616                         PRINTF ("Status = 0x%02X ", c);
617 #endif
618                 } else {
619                         puts ("OK ");
620                         ide_bus_ok[bus] = 1;
621                 }
622                 WATCHDOG_RESET();
623         }
624
625 #ifdef CONFIG_AMIGAONEG3SE
626       skip_bus:
627 #endif
628         putc ('\n');
629
630         ide_led ((LED_IDE1 | LED_IDE2), 0);     /* LED's off    */
631
632         curr_device = -1;
633         for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
634 #ifdef CONFIG_IDE_LED
635                 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
636 #endif
637                 ide_dev_desc[i].if_type=IF_TYPE_IDE;
638                 ide_dev_desc[i].dev=i;
639                 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
640                 ide_dev_desc[i].blksz=0;
641                 ide_dev_desc[i].lba=0;
642                 ide_dev_desc[i].block_read=ide_read;
643                 if (!ide_bus_ok[IDE_BUS(i)])
644                         continue;
645                 ide_led (led, 1);               /* LED on       */
646                 ide_ident(&ide_dev_desc[i]);
647                 ide_led (led, 0);               /* LED off      */
648                 dev_print(&ide_dev_desc[i]);
649 /*              ide_print (i); */
650                 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
651                         init_part (&ide_dev_desc[i]);                   /* initialize partition type */
652                         if (curr_device < 0)
653                                 curr_device = i;
654                 }
655         }
656         WATCHDOG_RESET();
657 }
658
659 /* ------------------------------------------------------------------------- */
660
661 block_dev_desc_t * ide_get_dev(int dev)
662 {
663         return ((block_dev_desc_t *)&ide_dev_desc[dev]);
664 }
665
666
667 #ifdef CONFIG_IDE_8xx_DIRECT
668
669 static void
670 set_pcmcia_timing (int pmode)
671 {
672         volatile immap_t *immr = (immap_t *)CFG_IMMR;
673         volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
674         ulong timings;
675
676         PRINTF ("Set timing for PIO Mode %d\n", pmode);
677
678         timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
679                 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
680                 | PCMCIA_SL (pio_config_clk[pmode].t_length)
681                 ;
682
683         /* IDE 0
684          */
685         pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
686         pcmp->pcmc_por0 = CFG_PCMCIA_POR0
687 #if (CFG_PCMCIA_POR0 != 0)
688                         | timings
689 #endif
690                         ;
691         PRINTF ("PBR0: %08x  POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
692
693         pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
694         pcmp->pcmc_por1 = CFG_PCMCIA_POR1
695 #if (CFG_PCMCIA_POR1 != 0)
696                         | timings
697 #endif
698                         ;
699         PRINTF ("PBR1: %08x  POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
700
701         pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
702         pcmp->pcmc_por2 = CFG_PCMCIA_POR2
703 #if (CFG_PCMCIA_POR2 != 0)
704                         | timings
705 #endif
706                         ;
707         PRINTF ("PBR2: %08x  POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
708
709         pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
710         pcmp->pcmc_por3 = CFG_PCMCIA_POR3
711 #if (CFG_PCMCIA_POR3 != 0)
712                         | timings
713 #endif
714                         ;
715         PRINTF ("PBR3: %08x  POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
716
717         /* IDE 1
718          */
719         pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
720         pcmp->pcmc_por4 = CFG_PCMCIA_POR4
721 #if (CFG_PCMCIA_POR4 != 0)
722                         | timings
723 #endif
724                         ;
725         PRINTF ("PBR4: %08x  POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
726
727         pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
728         pcmp->pcmc_por5 = CFG_PCMCIA_POR5
729 #if (CFG_PCMCIA_POR5 != 0)
730                         | timings
731 #endif
732                         ;
733         PRINTF ("PBR5: %08x  POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
734
735         pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
736         pcmp->pcmc_por6 = CFG_PCMCIA_POR6
737 #if (CFG_PCMCIA_POR6 != 0)
738                         | timings
739 #endif
740                         ;
741         PRINTF ("PBR6: %08x  POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
742
743         pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
744         pcmp->pcmc_por7 = CFG_PCMCIA_POR7
745 #if (CFG_PCMCIA_POR7 != 0)
746                         | timings
747 #endif
748                         ;
749         PRINTF ("PBR7: %08x  POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
750
751 }
752
753 #endif  /* CONFIG_IDE_8xx_DIRECT */
754
755 /* ------------------------------------------------------------------------- */
756
757 #ifdef __PPC__
758 static void __inline__
759 ide_outb(int dev, int port, unsigned char val)
760 {
761         /* Ensure I/O operations complete */
762         __asm__ volatile("eieio");
763         *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
764 #if 0
765         printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
766 #endif
767 }
768 #else   /* ! __PPC__ */
769 static void __inline__
770 ide_outb(int dev, int port, unsigned char val)
771 {
772         outb(val, ATA_CURR_BASE(dev)+port);
773 }
774 #endif  /* __PPC__ */
775
776
777 #ifdef __PPC__
778 static unsigned char __inline__
779 ide_inb(int dev, int port)
780 {
781         uchar val;
782         /* Ensure I/O operations complete */
783         __asm__ volatile("eieio");
784         val = *((uchar *)(ATA_CURR_BASE(dev)+port));
785 #if 0
786         printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
787 #endif
788         return (val);
789 }
790 #else   /* ! __PPC__ */
791 static unsigned char __inline__
792 ide_inb(int dev, int port)
793 {
794   return inb(ATA_CURR_BASE(dev)+port);
795 }
796 #endif  /* __PPC__ */
797
798 #ifdef __PPC__
799 # ifdef CONFIG_AMIGAONEG3SE
800 static void
801 output_data_short(int dev, ulong *sect_buf, int words)
802 {
803         ushort  *dbuf;
804         volatile ushort *pbuf;
805
806         pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
807         dbuf = (ushort *)sect_buf;
808         while (words--) {
809                 __asm__ volatile ("eieio");
810                 *pbuf = *dbuf++;
811                 __asm__ volatile ("eieio");
812         }
813
814         if (words&1)
815             *pbuf = 0;
816 }
817 # endif /* CONFIG_AMIGAONEG3SE */
818
819 static void
820 input_swap_data(int dev, ulong *sect_buf, int words)
821 {
822         volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
823         ushort  *dbuf = (ushort *)sect_buf;
824
825         while (words--) {
826                 *dbuf++ = ld_le16(pbuf);
827                 *dbuf++ = ld_le16(pbuf);
828         }
829 }
830 #else   /* ! __PPC__ */
831 #define input_swap_data(x,y,z) input_data(x,y,z)
832 #endif  /* __PPC__ */
833
834
835 #ifdef __PPC__
836 static void
837 output_data(int dev, ulong *sect_buf, int words)
838 {
839         ushort  *dbuf;
840         volatile ushort *pbuf;
841
842         pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
843         dbuf = (ushort *)sect_buf;
844         while (words--) {
845                 __asm__ volatile ("eieio");
846                 *pbuf = *dbuf++;
847                 __asm__ volatile ("eieio");
848                 *pbuf = *dbuf++;
849         }
850 }
851 #else   /* ! __PPC__ */
852 static void
853 output_data(int dev, ulong *sect_buf, int words)
854 {
855         outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
856 }
857 #endif  /* __PPC__ */
858
859 #ifdef __PPC__
860 static void
861 input_data(int dev, ulong *sect_buf, int words)
862 {
863         ushort  *dbuf;
864         volatile ushort *pbuf;
865
866         pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
867         dbuf = (ushort *)sect_buf;
868         while (words--) {
869                 __asm__ volatile ("eieio");
870                 *dbuf++ = *pbuf;
871                 __asm__ volatile ("eieio");
872                 *dbuf++ = *pbuf;
873         }
874 }
875 #else   /* ! __PPC__ */
876 static void
877 input_data(int dev, ulong *sect_buf, int words)
878 {
879         insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
880 }
881
882 #endif  /* __PPC__ */
883
884 #ifdef CONFIG_AMIGAONEG3SE
885 static void
886 input_data_short(int dev, ulong *sect_buf, int words)
887 {
888         ushort  *dbuf;
889         volatile ushort *pbuf;
890
891         pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
892         dbuf = (ushort *)sect_buf;
893         while (words--) {
894                 __asm__ volatile ("eieio");
895                 *dbuf++ = *pbuf;
896                 __asm__ volatile ("eieio");
897         }
898
899         if (words&1)
900         {
901             ushort dummy;
902             dummy = *pbuf;
903         }
904 }
905 #endif
906
907 /* -------------------------------------------------------------------------
908  */
909 static void ide_ident (block_dev_desc_t *dev_desc)
910 {
911         ulong iobuf[ATA_SECTORWORDS];
912         unsigned char c;
913         hd_driveid_t *iop = (hd_driveid_t *)iobuf;
914
915 #ifdef CONFIG_AMIGAONEG3SE
916         int max_bus_scan;
917         int retries = 0;
918         char *s;
919         int do_retry = 0;
920 #endif
921
922 #if 0
923         int mode, cycle_time;
924 #endif
925         int device;
926         device=dev_desc->dev;
927         printf ("  Device %d: ", device);
928
929 #ifdef CONFIG_AMIGAONEG3SE
930         s = getenv("ide_maxbus");
931         if (s) {
932                 max_bus_scan = simple_strtol(s, NULL, 10);
933         } else {
934                 max_bus_scan = CFG_IDE_MAXBUS;
935         }
936         if (device >= max_bus_scan*2) {
937                 dev_desc->type=DEV_TYPE_UNKNOWN;
938                 return;
939         }
940 #endif
941
942         ide_led (DEVICE_LED(device), 1);        /* LED on       */
943         /* Select device
944          */
945         ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
946         dev_desc->if_type=IF_TYPE_IDE;
947 #ifdef CONFIG_ATAPI
948
949 #ifdef CONFIG_AMIGAONEG3SE
950     do_retry = 0;
951     retries = 0;
952
953     /* Warning: This will be tricky to read */
954     while (retries <= 1)
955     {
956 #endif  /* CONFIG_AMIGAONEG3SE */
957
958         /* check signature */
959         if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
960                  (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
961                  (ide_inb(device,ATA_CYL_LOW)  == 0x14) &&
962                  (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
963                 /* ATAPI Signature found */
964                 dev_desc->if_type=IF_TYPE_ATAPI;
965                 /* Start Ident Command
966                  */
967                 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
968                 /*
969                  * Wait for completion - ATAPI devices need more time
970                  * to become ready
971                  */
972                 c = ide_wait (device, ATAPI_TIME_OUT);
973         }
974         else
975 #endif
976         {
977                 /* Start Ident Command
978                  */
979                 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
980
981                 /* Wait for completion
982                  */
983                 c = ide_wait (device, IDE_TIME_OUT);
984         }
985         ide_led (DEVICE_LED(device), 0);        /* LED off      */
986
987         if (((c & ATA_STAT_DRQ) == 0) ||
988             ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
989 #ifdef CONFIG_AMIGAONEG3SE
990                 if (retries == 0) {
991                         do_retry = 1;
992                 } else {
993                         dev_desc->type=DEV_TYPE_UNKNOWN;
994                         return;
995                 }
996 #else
997                 dev_desc->type=DEV_TYPE_UNKNOWN;
998                 return;
999 #endif  /* CONFIG_AMIGAONEG3SE */
1000         }
1001
1002 #ifdef CONFIG_AMIGAONEG3SE
1003         s = getenv("ide_doreset");
1004         if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
1005                 /* Need to soft reset the device in case it's an ATAPI...  */
1006                 PRINTF("Retrying...\n");
1007                 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1008                 udelay(100000);
1009                 ide_outb (device, ATA_COMMAND, 0x08);
1010                 udelay (100000);        /* 100 ms */
1011                 retries++;
1012         } else {
1013                 retries = 100;
1014         }
1015     }   /* see above - ugly to read */
1016 #endif  /* CONFIG_AMIGAONEG3SE */
1017
1018         input_swap_data (device, iobuf, ATA_SECTORWORDS);
1019
1020         ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1021         ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1022         ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1023
1024         if ((iop->config & 0x0080)==0x0080)
1025                 dev_desc->removable = 1;
1026         else
1027                 dev_desc->removable = 0;
1028
1029 #if 0
1030         /*
1031          * Drive PIO mode autoselection
1032          */
1033         mode = iop->tPIO;
1034
1035         printf ("tPIO = 0x%02x = %d\n",mode, mode);
1036         if (mode > 2) {         /* 2 is maximum allowed tPIO value */
1037                 mode = 2;
1038                 PRINTF ("Override tPIO -> 2\n");
1039         }
1040         if (iop->field_valid & 2) {     /* drive implements ATA2? */
1041                 PRINTF ("Drive implements ATA2\n");
1042                 if (iop->capability & 8) {      /* drive supports use_iordy? */
1043                         cycle_time = iop->eide_pio_iordy;
1044                 } else {
1045                         cycle_time = iop->eide_pio;
1046                 }
1047                 PRINTF ("cycle time = %d\n", cycle_time);
1048                 mode = 4;
1049                 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1050                 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1051                 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1052                 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1053         }
1054         printf ("PIO mode to use: PIO %d\n", mode);
1055 #endif /* 0 */
1056
1057 #ifdef CONFIG_ATAPI
1058         if (dev_desc->if_type==IF_TYPE_ATAPI) {
1059                 atapi_inquiry(dev_desc);
1060                 return;
1061         }
1062 #endif /* CONFIG_ATAPI */
1063
1064         /* swap shorts */
1065         dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1066         /* assuming HD */
1067         dev_desc->type=DEV_TYPE_HARDDISK;
1068         dev_desc->blksz=ATA_BLOCKSIZE;
1069         dev_desc->lun=0; /* just to fill something in... */
1070
1071 #if 0   /* only used to test the powersaving mode,
1072          * if enabled, the drive goes after 5 sec
1073          * in standby mode */
1074         ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1075         c = ide_wait (device, IDE_TIME_OUT);
1076         ide_outb (device, ATA_SECT_CNT, 1);
1077         ide_outb (device, ATA_LBA_LOW,  0);
1078         ide_outb (device, ATA_LBA_MID,  0);
1079         ide_outb (device, ATA_LBA_HIGH, 0);
1080         ide_outb (device, ATA_DEV_HD,   ATA_LBA         |
1081                                     ATA_DEVICE(device));
1082         ide_outb (device, ATA_COMMAND,  0xe3);
1083         udelay (50);
1084         c = ide_wait (device, IDE_TIME_OUT);    /* can't take over 500 ms */
1085 #endif
1086 }
1087
1088
1089 /* ------------------------------------------------------------------------- */
1090
1091 ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1092 {
1093         ulong n = 0;
1094         unsigned char c;
1095         unsigned char pwrsave=0; /* power save */
1096
1097         PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1098                 device, blknr, blkcnt, (ulong)buffer);
1099
1100         ide_led (DEVICE_LED(device), 1);        /* LED on       */
1101
1102         /* Select device
1103          */
1104         ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1105         c = ide_wait (device, IDE_TIME_OUT);
1106
1107         if (c & ATA_STAT_BUSY) {
1108                 printf ("IDE read: device %d not ready\n", device);
1109                 goto IDE_READ_E;
1110         }
1111
1112         /* first check if the drive is in Powersaving mode, if yes,
1113          * increase the timeout value */
1114         ide_outb (device, ATA_COMMAND,  ATA_CMD_CHK_PWR);
1115         udelay (50);
1116
1117         c = ide_wait (device, IDE_TIME_OUT);    /* can't take over 500 ms */
1118
1119         if (c & ATA_STAT_BUSY) {
1120                 printf ("IDE read: device %d not ready\n", device);
1121                 goto IDE_READ_E;
1122         }
1123         if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1124                 printf ("No Powersaving mode %X\n", c);
1125         } else {
1126                 c = ide_inb(device,ATA_SECT_CNT);
1127                 PRINTF("Powersaving %02X\n",c);
1128                 if(c==0)
1129                         pwrsave=1;
1130         }
1131
1132
1133         while (blkcnt-- > 0) {
1134
1135                 c = ide_wait (device, IDE_TIME_OUT);
1136
1137                 if (c & ATA_STAT_BUSY) {
1138                         printf ("IDE read: device %d not ready\n", device);
1139                         break;
1140                 }
1141
1142                 ide_outb (device, ATA_SECT_CNT, 1);
1143                 ide_outb (device, ATA_LBA_LOW,  (blknr >>  0) & 0xFF);
1144                 ide_outb (device, ATA_LBA_MID,  (blknr >>  8) & 0xFF);
1145                 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1146                 ide_outb (device, ATA_DEV_HD,   ATA_LBA         |
1147                                             ATA_DEVICE(device)  |
1148                                             ((blknr >> 24) & 0xF) );
1149                 ide_outb (device, ATA_COMMAND,  ATA_CMD_READ);
1150
1151                 udelay (50);
1152
1153                 if(pwrsave) {
1154                         c = ide_wait (device, IDE_SPIN_UP_TIME_OUT);    /* may take up to 4 sec */
1155                         pwrsave=0;
1156                 } else {
1157                         c = ide_wait (device, IDE_TIME_OUT);    /* can't take over 500 ms */
1158                 }
1159
1160                 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1161                         printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1162                                 device, blknr, c);
1163                         break;
1164                 }
1165
1166                 input_data (device, buffer, ATA_SECTORWORDS);
1167                 (void) ide_inb (device, ATA_STATUS);    /* clear IRQ */
1168
1169                 ++n;
1170                 ++blknr;
1171                 buffer += ATA_SECTORWORDS;
1172         }
1173 IDE_READ_E:
1174         ide_led (DEVICE_LED(device), 0);        /* LED off      */
1175         return (n);
1176 }
1177
1178 /* ------------------------------------------------------------------------- */
1179
1180
1181 ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1182 {
1183         ulong n = 0;
1184         unsigned char c;
1185
1186         ide_led (DEVICE_LED(device), 1);        /* LED on       */
1187
1188         /* Select device
1189          */
1190         ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1191
1192         while (blkcnt-- > 0) {
1193
1194                 c = ide_wait (device, IDE_TIME_OUT);
1195
1196                 if (c & ATA_STAT_BUSY) {
1197                         printf ("IDE read: device %d not ready\n", device);
1198                         goto WR_OUT;
1199                 }
1200
1201                 ide_outb (device, ATA_SECT_CNT, 1);
1202                 ide_outb (device, ATA_LBA_LOW,  (blknr >>  0) & 0xFF);
1203                 ide_outb (device, ATA_LBA_MID,  (blknr >>  8) & 0xFF);
1204                 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1205                 ide_outb (device, ATA_DEV_HD,   ATA_LBA         |
1206                                             ATA_DEVICE(device)  |
1207                                             ((blknr >> 24) & 0xF) );
1208                 ide_outb (device, ATA_COMMAND,  ATA_CMD_WRITE);
1209
1210                 udelay (50);
1211
1212                 c = ide_wait (device, IDE_TIME_OUT);    /* can't take over 500 ms */
1213
1214                 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1215                         printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1216                                 device, blknr, c);
1217                         goto WR_OUT;
1218                 }
1219
1220                 output_data (device, buffer, ATA_SECTORWORDS);
1221                 c = ide_inb (device, ATA_STATUS);       /* clear IRQ */
1222                 ++n;
1223                 ++blknr;
1224                 buffer += ATA_SECTORWORDS;
1225         }
1226 WR_OUT:
1227         ide_led (DEVICE_LED(device), 0);        /* LED off      */
1228         return (n);
1229 }
1230
1231 /* ------------------------------------------------------------------------- */
1232
1233 /*
1234  * copy src to dest, skipping leading and trailing blanks and null
1235  * terminate the string
1236  */
1237 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1238 {
1239         int start,end;
1240
1241         start=0;
1242         while (start<len) {
1243                 if (src[start]!=' ')
1244                         break;
1245                 start++;
1246         }
1247         end=len-1;
1248         while (end>start) {
1249                 if (src[end]!=' ')
1250                         break;
1251                 end--;
1252         }
1253         for ( ; start<=end; start++) {
1254                 *dest++=src[start];
1255         }
1256         *dest='\0';
1257 }
1258
1259 /* ------------------------------------------------------------------------- */
1260
1261 /*
1262  * Wait until Busy bit is off, or timeout (in ms)
1263  * Return last status
1264  */
1265 static uchar ide_wait (int dev, ulong t)
1266 {
1267         ulong delay = 10 * t;           /* poll every 100 us */
1268         uchar c;
1269
1270         while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1271                 udelay (100);
1272                 if (delay-- == 0) {
1273                         break;
1274                 }
1275         }
1276         return (c);
1277 }
1278
1279 /* ------------------------------------------------------------------------- */
1280
1281 #ifdef CONFIG_IDE_RESET
1282 extern void ide_set_reset(int idereset);
1283
1284 static void ide_reset (void)
1285 {
1286 #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1287         volatile immap_t *immr = (immap_t *)CFG_IMMR;
1288 #endif
1289         int i;
1290
1291         curr_device = -1;
1292         for (i=0; i<CFG_IDE_MAXBUS; ++i)
1293                 ide_bus_ok[i] = 0;
1294         for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1295                 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1296
1297         ide_set_reset (1); /* assert reset */
1298
1299         WATCHDOG_RESET();
1300
1301 #ifdef CFG_PB_12V_ENABLE
1302         immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE);  /* 12V Enable output OFF */
1303         immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1304         immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1305         immr->im_cpm.cp_pbdir |=   CFG_PB_12V_ENABLE;
1306
1307         /* wait 500 ms for the voltage to stabilize
1308          */
1309         for (i=0; i<500; ++i) {
1310                 udelay (1000);
1311         }
1312
1313         immr->im_cpm.cp_pbdat |=   CFG_PB_12V_ENABLE;   /* 12V Enable output ON */
1314 #endif  /* CFG_PB_12V_ENABLE */
1315
1316 #ifdef CFG_PB_IDE_MOTOR
1317         /* configure IDE Motor voltage monitor pin as input */
1318         immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1319         immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1320         immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1321
1322         /* wait up to 1 s for the motor voltage to stabilize
1323          */
1324         for (i=0; i<1000; ++i) {
1325                 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1326                         break;
1327                 }
1328                 udelay (1000);
1329         }
1330
1331         if (i == 1000) {        /* Timeout */
1332                 printf ("\nWarning: 5V for IDE Motor missing\n");
1333 # ifdef CONFIG_STATUS_LED
1334 #  ifdef STATUS_LED_YELLOW
1335                 status_led_set  (STATUS_LED_YELLOW, STATUS_LED_ON );
1336 #  endif
1337 #  ifdef STATUS_LED_GREEN
1338                 status_led_set  (STATUS_LED_GREEN,  STATUS_LED_OFF);
1339 #  endif
1340 # endif /* CONFIG_STATUS_LED */
1341         }
1342 #endif  /* CFG_PB_IDE_MOTOR */
1343
1344         WATCHDOG_RESET();
1345
1346         /* de-assert RESET signal */
1347         ide_set_reset(0);
1348
1349         /* wait 250 ms */
1350         for (i=0; i<250; ++i) {
1351                 udelay (1000);
1352         }
1353 }
1354
1355 #endif  /* CONFIG_IDE_RESET */
1356
1357 /* ------------------------------------------------------------------------- */
1358
1359 #if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
1360
1361 static  uchar   led_buffer = 0;         /* Buffer for current LED status        */
1362
1363 static void ide_led (uchar led, uchar status)
1364 {
1365         uchar *led_port = LED_PORT;
1366
1367         if (status)     {               /* switch LED on        */
1368                 led_buffer |=  led;
1369         } else {                        /* switch LED off       */
1370                 led_buffer &= ~led;
1371         }
1372
1373         *led_port = led_buffer;
1374 }
1375
1376 #endif  /* CONFIG_IDE_LED */
1377
1378 /* ------------------------------------------------------------------------- */
1379
1380 #ifdef CONFIG_ATAPI
1381 /****************************************************************************
1382  * ATAPI Support
1383  */
1384
1385
1386 #undef  ATAPI_DEBUG
1387
1388 #ifdef  ATAPI_DEBUG
1389 #define AT_PRINTF(fmt,args...)  printf (fmt ,##args)
1390 #else
1391 #define AT_PRINTF(fmt,args...)
1392 #endif
1393
1394 #ifdef __PPC__
1395 /* since ATAPI may use commands with not 4 bytes alligned length
1396  * we have our own transfer functions, 2 bytes alligned */
1397 static void
1398 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1399 {
1400         ushort  *dbuf;
1401         volatile ushort *pbuf;
1402
1403         pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1404         dbuf = (ushort *)sect_buf;
1405         while (shorts--) {
1406                 __asm__ volatile ("eieio");
1407                 *pbuf = *dbuf++;
1408         }
1409 }
1410
1411 static void
1412 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1413 {
1414         ushort  *dbuf;
1415         volatile ushort *pbuf;
1416
1417         pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1418         dbuf = (ushort *)sect_buf;
1419         while (shorts--) {
1420                 __asm__ volatile ("eieio");
1421                 *dbuf++ = *pbuf;
1422         }
1423 }
1424
1425 #else   /* ! __PPC__ */
1426 static void
1427 output_data_shorts(int dev, ushort *sect_buf, int shorts)
1428 {
1429         outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1430 }
1431
1432
1433 static void
1434 input_data_shorts(int dev, ushort *sect_buf, int shorts)
1435 {
1436         insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
1437 }
1438
1439 #endif  /* __PPC__ */
1440
1441 /*
1442  * Wait until (Status & mask) == res, or timeout (in ms)
1443  * Return last status
1444  * This is used since some ATAPI CD ROMs clears their Busy Bit first
1445  * and then they set their DRQ Bit
1446  */
1447 static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1448 {
1449         ulong delay = 10 * t;           /* poll every 100 us */
1450         uchar c;
1451
1452         c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1453         while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1454                 /* break if error occurs (doesn't make sense to wait more) */
1455                 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1456                         break;
1457                 udelay (100);
1458                 if (delay-- == 0) {
1459                         break;
1460                 }
1461         }
1462         return (c);
1463 }
1464
1465 /*
1466  * issue an atapi command
1467  */
1468 unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1469 {
1470         unsigned char c,err,mask,res;
1471         int n;
1472         ide_led (DEVICE_LED(device), 1);        /* LED on       */
1473
1474         /* Select device
1475          */
1476         mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1477         res = 0;
1478 #ifdef  CONFIG_AMIGAONEG3SE
1479 # warning THF: Removed LBA mode ???
1480 #endif
1481         ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1482         c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1483         if ((c & mask) != res) {
1484                 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1485                 err=0xFF;
1486                 goto AI_OUT;
1487         }
1488         /* write taskfile */
1489         ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1490         ide_outb (device, ATA_SECT_CNT, 0);
1491         ide_outb (device, ATA_SECT_NUM, 0);
1492         ide_outb (device, ATA_CYL_LOW,  (unsigned char)(buflen & 0xFF));
1493         ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1494 #ifdef  CONFIG_AMIGAONEG3SE
1495 # warning THF: Removed LBA mode ???
1496 #endif
1497         ide_outb (device, ATA_DEV_HD,   ATA_LBA | ATA_DEVICE(device));
1498
1499         ide_outb (device, ATA_COMMAND,  ATAPI_CMD_PACKET);
1500         udelay (50);
1501
1502         mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1503         res = ATA_STAT_DRQ;
1504         c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1505
1506         if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1507                 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1508                 err=0xFF;
1509                 goto AI_OUT;
1510         }
1511
1512         output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1513         /* ATAPI Command written wait for completition */
1514         udelay (5000); /* device must set bsy */
1515
1516         mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1517         /* if no data wait for DRQ = 0 BSY = 0
1518          * if data wait for DRQ = 1 BSY = 0 */
1519         res=0;
1520         if(buflen)
1521                 res = ATA_STAT_DRQ;
1522         c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1523         if ((c & mask) != res ) {
1524                 if (c & ATA_STAT_ERR) {
1525                         err=(ide_inb(device,ATA_ERROR_REG))>>4;
1526                         AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1527                 } else {
1528                         printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x)  status 0x%02x\n", ccb[0],c);
1529                         err=0xFF;
1530                 }
1531                 goto AI_OUT;
1532         }
1533         n=ide_inb(device, ATA_CYL_HIGH);
1534         n<<=8;
1535         n+=ide_inb(device, ATA_CYL_LOW);
1536         if(n>buflen) {
1537                 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1538                 err=0xff;
1539                 goto AI_OUT;
1540         }
1541         if((n==0)&&(buflen<0)) {
1542                 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1543                 err=0xff;
1544                 goto AI_OUT;
1545         }
1546         if(n!=buflen) {
1547                 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1548         }
1549         if(n!=0) { /* data transfer */
1550                 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1551                  /* we transfer shorts */
1552                 n>>=1;
1553                 /* ok now decide if it is an in or output */
1554                 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
1555                         AT_PRINTF("Write to device\n");
1556                         output_data_shorts(device,(unsigned short *)buffer,n);
1557                 } else {
1558                         AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1559                         input_data_shorts(device,(unsigned short *)buffer,n);
1560                 }
1561         }
1562         udelay(5000); /* seems that some CD ROMs need this... */
1563         mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1564         res=0;
1565         c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1566         if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1567                 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
1568                 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1569         } else {
1570                 err = 0;
1571         }
1572 AI_OUT:
1573         ide_led (DEVICE_LED(device), 0);        /* LED off      */
1574         return (err);
1575 }
1576
1577 /*
1578  * sending the command to atapi_issue. If an status other than good
1579  * returns, an request_sense will be issued
1580  */
1581
1582 #define ATAPI_DRIVE_NOT_READY   100
1583 #define ATAPI_UNIT_ATTN         10
1584
1585 unsigned char atapi_issue_autoreq (int device,
1586                                    unsigned char* ccb,
1587                                    int ccblen,
1588                                    unsigned char *buffer,
1589                                    int buflen)
1590 {
1591         unsigned char sense_data[18],sense_ccb[12];
1592         unsigned char res,key,asc,ascq;
1593         int notready,unitattn;
1594
1595 #ifdef CONFIG_AMIGAONEG3SE
1596         char *s;
1597         unsigned int timeout, retrycnt;
1598
1599         s = getenv("ide_cd_timeout");
1600         timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1601
1602         retrycnt = 0;
1603 #endif
1604
1605         unitattn=ATAPI_UNIT_ATTN;
1606         notready=ATAPI_DRIVE_NOT_READY;
1607
1608 retry:
1609         res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1610         if (res==0)
1611                 return (0); /* Ok */
1612
1613         if (res==0xFF)
1614                 return (0xFF); /* error */
1615
1616         AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1617
1618         memset(sense_ccb,0,sizeof(sense_ccb));
1619         memset(sense_data,0,sizeof(sense_data));
1620         sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1621         sense_ccb[4]=18; /* allocation Length */
1622
1623         res=atapi_issue(device,sense_ccb,12,sense_data,18);
1624         key=(sense_data[2]&0xF);
1625         asc=(sense_data[12]);
1626         ascq=(sense_data[13]);
1627
1628         AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1629         AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1630                 sense_data[0],
1631                 key,
1632                 asc,
1633                 ascq);
1634
1635         if((key==0))
1636                 return 0; /* ok device ready */
1637
1638         if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1639                 if(unitattn-->0) {
1640                         udelay(200*1000);
1641                         goto retry;
1642                 }
1643                 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1644                 goto error;
1645         }
1646         if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1647                 if (notready-->0) {
1648                         udelay(200*1000);
1649                         goto retry;
1650                 }
1651                 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1652                 goto error;
1653         }
1654         if(asc==0x3a) {
1655                 AT_PRINTF("Media not present\n");
1656                 goto error;
1657         }
1658
1659 #ifdef CONFIG_AMIGAONEG3SE
1660         if ((sense_data[2]&0xF)==0x0B) {
1661                 AT_PRINTF("ABORTED COMMAND...retry\n");
1662                 if (retrycnt++ < 4)
1663                         goto retry;
1664                 return (0xFF);
1665         }
1666
1667         if ((sense_data[2]&0xf) == 0x02 &&
1668             sense_data[12] == 0x04      &&
1669             sense_data[13] == 0x01      ) {
1670                 AT_PRINTF("Waiting for unit to become active\n");
1671                 udelay(timeout);
1672                 if (retrycnt++ < 4)
1673                         goto retry;
1674                 return 0xFF;
1675         }
1676 #endif  /* CONFIG_AMIGAONEG3SE */
1677
1678         printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1679 error:
1680         AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1681         return (0xFF);
1682 }
1683
1684
1685 static void     atapi_inquiry(block_dev_desc_t * dev_desc)
1686 {
1687         unsigned char ccb[12]; /* Command descriptor block */
1688         unsigned char iobuf[64]; /* temp buf */
1689         unsigned char c;
1690         int device;
1691
1692         device=dev_desc->dev;
1693         dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1694         dev_desc->block_read=atapi_read;
1695
1696         memset(ccb,0,sizeof(ccb));
1697         memset(iobuf,0,sizeof(iobuf));
1698
1699         ccb[0]=ATAPI_CMD_INQUIRY;
1700         ccb[4]=40; /* allocation Legnth */
1701         c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1702
1703         AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1704         if (c!=0)
1705                 return;
1706
1707         /* copy device ident strings */
1708         ident_cpy(dev_desc->vendor,&iobuf[8],8);
1709         ident_cpy(dev_desc->product,&iobuf[16],16);
1710         ident_cpy(dev_desc->revision,&iobuf[32],5);
1711
1712         dev_desc->lun=0;
1713         dev_desc->lba=0;
1714         dev_desc->blksz=0;
1715         dev_desc->type=iobuf[0] & 0x1f;
1716
1717         if ((iobuf[1]&0x80)==0x80)
1718                 dev_desc->removable = 1;
1719         else
1720                 dev_desc->removable = 0;
1721
1722         memset(ccb,0,sizeof(ccb));
1723         memset(iobuf,0,sizeof(iobuf));
1724         ccb[0]=ATAPI_CMD_START_STOP;
1725         ccb[4]=0x03; /* start */
1726
1727         c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1728
1729         AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1730         if (c!=0)
1731                 return;
1732
1733         memset(ccb,0,sizeof(ccb));
1734         memset(iobuf,0,sizeof(iobuf));
1735         c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1736
1737         AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1738         if (c!=0)
1739                 return;
1740
1741         memset(ccb,0,sizeof(ccb));
1742         memset(iobuf,0,sizeof(iobuf));
1743         ccb[0]=ATAPI_CMD_READ_CAP;
1744         c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1745         AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1746         if (c!=0)
1747                 return;
1748
1749         AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1750                 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1751                 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1752
1753         dev_desc->lba  =((unsigned long)iobuf[0]<<24) +
1754                         ((unsigned long)iobuf[1]<<16) +
1755                         ((unsigned long)iobuf[2]<< 8) +
1756                         ((unsigned long)iobuf[3]);
1757         dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1758                         ((unsigned long)iobuf[5]<<16) +
1759                         ((unsigned long)iobuf[6]<< 8) +
1760                         ((unsigned long)iobuf[7]);
1761         return;
1762 }
1763
1764
1765 /*
1766  * atapi_read:
1767  * we transfer only one block per command, since the multiple DRQ per
1768  * command is not yet implemented
1769  */
1770 #define ATAPI_READ_MAX_BYTES    2048    /* we read max 2kbytes */
1771 #define ATAPI_READ_BLOCK_SIZE   2048    /* assuming CD part */
1772 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1773
1774 ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1775 {
1776         ulong n = 0;
1777         unsigned char ccb[12]; /* Command descriptor block */
1778         ulong cnt;
1779
1780         AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1781                 device, blknr, blkcnt, (ulong)buffer);
1782
1783         do {
1784                 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1785                         cnt=ATAPI_READ_MAX_BLOCK;
1786                 } else {
1787                         cnt=blkcnt;
1788                 }
1789                 ccb[0]=ATAPI_CMD_READ_12;
1790                 ccb[1]=0; /* reserved */
1791                 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1792                 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /*  */
1793                 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1794                 ccb[5]=(unsigned char)  blknr      & 0xFF; /* LSB Block */
1795                 ccb[6]=(unsigned char) (cnt  >>24) & 0xFF; /* MSB Block count */
1796                 ccb[7]=(unsigned char) (cnt  >>16) & 0xFF;
1797                 ccb[8]=(unsigned char) (cnt  >> 8) & 0xFF;
1798                 ccb[9]=(unsigned char)  cnt        & 0xFF; /* LSB Block */
1799                 ccb[10]=0; /* reserved */
1800                 ccb[11]=0; /* reserved */
1801
1802                 if (atapi_issue_autoreq(device,ccb,12,
1803                                         (unsigned char *)buffer,
1804                                         cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1805                         return (n);
1806                 }
1807                 n+=cnt;
1808                 blkcnt-=cnt;
1809                 blknr+=cnt;
1810                 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1811         } while (blkcnt > 0);
1812         return (n);
1813 }
1814
1815 /* ------------------------------------------------------------------------- */
1816
1817 #endif /* CONFIG_ATAPI */
1818
1819 U_BOOT_CMD(
1820         ide,  5,  1,  do_ide,
1821         "ide     - IDE sub-system\n",
1822         "reset - reset IDE controller\n"
1823         "ide info  - show available IDE devices\n"
1824         "ide device [dev] - show or set current device\n"
1825         "ide part [dev] - print partition table of one or all IDE devices\n"
1826         "ide read  addr blk# cnt\n"
1827         "ide write addr blk# cnt - read/write `cnt'"
1828         " blocks starting at block `blk#'\n"
1829         "    to/from memory address `addr'\n"
1830 );
1831
1832 U_BOOT_CMD(
1833         diskboot,       3,      1,      do_diskboot,
1834         "diskboot- boot from IDE device\n",
1835         "loadAddr dev:part\n"
1836 );
1837
1838 #endif  /* CONFIG_COMMANDS & CFG_CMD_IDE */