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