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