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