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