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