]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_ide.c
split CPC45 board-specific IDE functions from cmd_ide.c
[karo-tx-uboot.git] / common / cmd_ide.c
1 /*
2  * (C) Copyright 2000-2011
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 __PPC__
57 # define EIEIO          __asm__ volatile ("eieio")
58 # define SYNC           __asm__ volatile ("sync")
59 #else
60 # define EIEIO          /* nothing */
61 # define SYNC           /* nothing */
62 #endif
63
64 /* ------------------------------------------------------------------------- */
65
66 /* Current I/O Device   */
67 static int curr_device = -1;
68
69 /* Current offset for IDE0 / IDE1 bus access    */
70 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
71 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
72         CONFIG_SYS_ATA_IDE0_OFFSET,
73 #endif
74 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
75         CONFIG_SYS_ATA_IDE1_OFFSET,
76 #endif
77 };
78
79 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
80
81 block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
82 /* ------------------------------------------------------------------------- */
83
84 #ifdef CONFIG_IDE_LED
85 # if !defined(CONFIG_BMS2003)   && \
86      !defined(CONFIG_CPC45)     && \
87      !defined(CONFIG_KUP4K) && \
88      !defined(CONFIG_KUP4X)
89 static void  ide_led   (uchar led, uchar status);
90 #else
91 extern void  ide_led   (uchar led, uchar status);
92 #endif
93 #else
94 #define ide_led(a,b)    /* dummy */
95 #endif
96
97 #ifdef CONFIG_IDE_RESET
98 static void  ide_reset (void);
99 #else
100 #define ide_reset()     /* dummy */
101 #endif
102
103 static void  ide_ident (block_dev_desc_t *dev_desc);
104 static uchar ide_wait  (int dev, ulong t);
105
106 #define IDE_TIME_OUT    2000    /* 2 sec timeout */
107
108 #define ATAPI_TIME_OUT  7000    /* 7 sec timeout (5 sec seems to work...) */
109
110 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
111
112 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
113
114 #ifndef CONFIG_SYS_ATA_PORT_ADDR
115 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
116 #endif
117
118 #ifdef CONFIG_ATAPI
119 static void     atapi_inquiry(block_dev_desc_t *dev_desc);
120 ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
121 #endif
122
123
124 /* ------------------------------------------------------------------------- */
125
126 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
127 {
128         int rcode = 0;
129
130         switch (argc) {
131         case 0:
132         case 1:
133                 return CMD_RET_USAGE;
134         case 2:
135                 if (strncmp(argv[1], "res", 3) == 0) {
136                         puts("\nReset IDE"
137 #ifdef CONFIG_IDE_8xx_DIRECT
138                              " on PCMCIA " PCMCIA_SLOT_MSG
139 #endif
140                              ": ");
141
142                         ide_init();
143                         return 0;
144                 } else if (strncmp(argv[1], "inf", 3) == 0) {
145                         int i;
146
147                         putc('\n');
148
149                         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
150                                 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
151                                         continue;  /* list only known devices */
152                                 printf("IDE device %d: ", i);
153                                 dev_print(&ide_dev_desc[i]);
154                         }
155                         return 0;
156
157                 } else if (strncmp(argv[1], "dev", 3) == 0) {
158                         if ((curr_device < 0)
159                             || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
160                                 puts("\nno IDE devices available\n");
161                                 return 1;
162                         }
163                         printf("\nIDE device %d: ", curr_device);
164                         dev_print(&ide_dev_desc[curr_device]);
165                         return 0;
166                 } else if (strncmp(argv[1], "part", 4) == 0) {
167                         int dev, ok;
168
169                         for (ok = 0, dev = 0;
170                              dev < CONFIG_SYS_IDE_MAXDEVICE;
171                              ++dev) {
172                                 if (ide_dev_desc[dev].part_type !=
173                                     PART_TYPE_UNKNOWN) {
174                                         ++ok;
175                                         if (dev)
176                                                 putc('\n');
177                                         print_part(&ide_dev_desc[dev]);
178                                 }
179                         }
180                         if (!ok) {
181                                 puts("\nno IDE devices available\n");
182                                 rcode++;
183                         }
184                         return rcode;
185                 }
186                 return CMD_RET_USAGE;
187         case 3:
188                 if (strncmp(argv[1], "dev", 3) == 0) {
189                         int dev = (int) simple_strtoul(argv[2], NULL, 10);
190
191                         printf("\nIDE device %d: ", dev);
192                         if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
193                                 puts("unknown device\n");
194                                 return 1;
195                         }
196                         dev_print(&ide_dev_desc[dev]);
197                         /*ide_print (dev); */
198
199                         if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
200                                 return 1;
201
202                         curr_device = dev;
203
204                         puts("... is now current device\n");
205
206                         return 0;
207                 } else if (strncmp(argv[1], "part", 4) == 0) {
208                         int dev = (int) simple_strtoul(argv[2], NULL, 10);
209
210                         if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
211                                 print_part(&ide_dev_desc[dev]);
212                         } else {
213                                 printf("\nIDE device %d not available\n",
214                                        dev);
215                                 rcode = 1;
216                         }
217                         return rcode;
218                 }
219
220                 return CMD_RET_USAGE;
221         default:
222                 /* at least 4 args */
223
224                 if (strcmp(argv[1], "read") == 0) {
225                         ulong addr = simple_strtoul(argv[2], NULL, 16);
226                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
227                         ulong n;
228
229 #ifdef CONFIG_SYS_64BIT_LBA
230                         lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
231
232                         printf("\nIDE read: device %d block # %lld, count %ld ... ",
233                                 curr_device, blk, cnt);
234 #else
235                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
236
237                         printf("\nIDE read: device %d block # %ld, count %ld ... ",
238                                 curr_device, blk, cnt);
239 #endif
240
241                         n = ide_dev_desc[curr_device].block_read(curr_device,
242                                                                  blk, cnt,
243                                                                  (ulong *)addr);
244                         /* flush cache after read */
245                         flush_cache(addr,
246                                     cnt * ide_dev_desc[curr_device].blksz);
247
248                         printf("%ld blocks read: %s\n",
249                                n, (n == cnt) ? "OK" : "ERROR");
250                         if (n == cnt)
251                                 return 0;
252                         else
253                                 return 1;
254                 } else if (strcmp(argv[1], "write") == 0) {
255                         ulong addr = simple_strtoul(argv[2], NULL, 16);
256                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
257                         ulong n;
258
259 #ifdef CONFIG_SYS_64BIT_LBA
260                         lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
261
262                         printf("\nIDE write: device %d block # %lld, count %ld ... ",
263                                 curr_device, blk, cnt);
264 #else
265                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
266
267                         printf("\nIDE write: device %d block # %ld, count %ld ... ",
268                                 curr_device, blk, cnt);
269 #endif
270                         n = ide_write(curr_device, blk, cnt, (ulong *) addr);
271
272                         printf("%ld blocks written: %s\n",
273                                 n, (n == cnt) ? "OK" : "ERROR");
274                         if (n == cnt)
275                                 return 0;
276                         else
277                                 return 1;
278                 } else {
279                         return CMD_RET_USAGE;
280                 }
281
282                 return rcode;
283         }
284 }
285
286 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
287 {
288         return common_diskboot(cmdtp, "ide", argc, argv);
289 }
290
291 /* ------------------------------------------------------------------------- */
292
293 inline void __ide_outb(int dev, int port, unsigned char val)
294 {
295         debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
296               dev, port, val,
297               (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
298
299 #if defined(CONFIG_IDE_AHB)
300         if (port) {
301                 /* write command */
302                 ide_write_register(dev, port, val);
303         } else {
304                 /* write data */
305                 outb(val, (ATA_CURR_BASE(dev)));
306         }
307 #else
308         outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
309 #endif
310 }
311
312 void ide_outb(int dev, int port, unsigned char val)
313         __attribute__ ((weak, alias("__ide_outb")));
314
315 inline unsigned char __ide_inb(int dev, int port)
316 {
317         uchar val;
318
319 #if defined(CONFIG_IDE_AHB)
320         val = ide_read_register(dev, port);
321 #else
322         val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
323 #endif
324
325         debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
326               dev, port,
327               (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
328         return val;
329 }
330
331 unsigned char ide_inb(int dev, int port)
332         __attribute__ ((weak, alias("__ide_inb")));
333
334 #ifdef CONFIG_TUNE_PIO
335 inline int __ide_set_piomode(int pio_mode)
336 {
337         return 0;
338 }
339
340 inline int ide_set_piomode(int pio_mode)
341         __attribute__ ((weak, alias("__ide_set_piomode")));
342 #endif
343
344 void ide_init(void)
345 {
346         unsigned char c;
347         int i, bus;
348
349 #ifdef CONFIG_IDE_8xx_PCCARD
350         extern int ide_devices_found;   /* Initialized in check_ide_device() */
351 #endif /* CONFIG_IDE_8xx_PCCARD */
352
353 #ifdef CONFIG_IDE_PREINIT
354         WATCHDOG_RESET();
355
356         if (ide_preinit()) {
357                 puts("ide_preinit failed\n");
358                 return;
359         }
360 #endif /* CONFIG_IDE_PREINIT */
361
362         WATCHDOG_RESET();
363
364         /*
365          * Reset the IDE just to be sure.
366          * Light LED's to show
367          */
368         ide_led((LED_IDE1 | LED_IDE2), 1);      /* LED's on     */
369
370         /* ATAPI Drives seems to need a proper IDE Reset */
371         ide_reset();
372
373 #ifdef CONFIG_IDE_INIT_POSTRESET
374         WATCHDOG_RESET();
375
376         if (ide_init_postreset()) {
377                 puts("ide_preinit_postreset failed\n");
378                 return;
379         }
380 #endif /* CONFIG_IDE_INIT_POSTRESET */
381
382         /*
383          * Wait for IDE to get ready.
384          * According to spec, this can take up to 31 seconds!
385          */
386         for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
387                 int dev =
388                         bus * (CONFIG_SYS_IDE_MAXDEVICE /
389                                CONFIG_SYS_IDE_MAXBUS);
390
391 #ifdef CONFIG_IDE_8xx_PCCARD
392                 /* Skip non-ide devices from probing */
393                 if ((ide_devices_found & (1 << bus)) == 0) {
394                         ide_led((LED_IDE1 | LED_IDE2), 0);      /* LED's off */
395                         continue;
396                 }
397 #endif
398                 printf("Bus %d: ", bus);
399
400                 ide_bus_ok[bus] = 0;
401
402                 /* Select device
403                  */
404                 udelay(100000); /* 100 ms */
405                 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
406                 udelay(100000); /* 100 ms */
407                 i = 0;
408                 do {
409                         udelay(10000);  /* 10 ms */
410
411                         c = ide_inb(dev, ATA_STATUS);
412                         i++;
413                         if (i > (ATA_RESET_TIME * 100)) {
414                                 puts("** Timeout **\n");
415                                 /* LED's off */
416                                 ide_led((LED_IDE1 | LED_IDE2), 0);
417                                 return;
418                         }
419                         if ((i >= 100) && ((i % 100) == 0))
420                                 putc('.');
421
422                 } while (c & ATA_STAT_BUSY);
423
424                 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
425                         puts("not available  ");
426                         debug("Status = 0x%02X ", c);
427 #ifndef CONFIG_ATAPI            /* ATAPI Devices do not set DRDY */
428                 } else if ((c & ATA_STAT_READY) == 0) {
429                         puts("not available  ");
430                         debug("Status = 0x%02X ", c);
431 #endif
432                 } else {
433                         puts("OK ");
434                         ide_bus_ok[bus] = 1;
435                 }
436                 WATCHDOG_RESET();
437         }
438
439         putc('\n');
440
441         ide_led((LED_IDE1 | LED_IDE2), 0);      /* LED's off    */
442
443         curr_device = -1;
444         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
445 #ifdef CONFIG_IDE_LED
446                 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
447 #endif
448                 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
449                 ide_dev_desc[i].if_type = IF_TYPE_IDE;
450                 ide_dev_desc[i].dev = i;
451                 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
452                 ide_dev_desc[i].blksz = 0;
453                 ide_dev_desc[i].lba = 0;
454                 ide_dev_desc[i].block_read = ide_read;
455                 ide_dev_desc[i].block_write = ide_write;
456                 if (!ide_bus_ok[IDE_BUS(i)])
457                         continue;
458                 ide_led(led, 1);        /* LED on       */
459                 ide_ident(&ide_dev_desc[i]);
460                 ide_led(led, 0);        /* LED off      */
461                 dev_print(&ide_dev_desc[i]);
462
463                 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
464                         /* initialize partition type */
465                         init_part(&ide_dev_desc[i]);
466                         if (curr_device < 0)
467                                 curr_device = i;
468                 }
469         }
470         WATCHDOG_RESET();
471 }
472
473 /* ------------------------------------------------------------------------- */
474
475 #ifdef CONFIG_PARTITIONS
476 block_dev_desc_t *ide_get_dev(int dev)
477 {
478         return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
479 }
480 #endif
481
482 /* ------------------------------------------------------------------------- */
483
484 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
485         __attribute__ ((weak, alias("__ide_input_swap_data")));
486
487 void ide_input_data(int dev, ulong *sect_buf, int words)
488         __attribute__ ((weak, alias("__ide_input_data")));
489
490 void ide_output_data(int dev, const ulong *sect_buf, int words)
491         __attribute__ ((weak, alias("__ide_output_data")));
492
493 /* We only need to swap data if we are running on a big endian cpu. */
494 /* But Au1x00 cpu:s already swaps data in big endian mode! */
495 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
496 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
497 {
498         ide_input_data(dev, sect_buf, words);
499 }
500 #else
501 void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
502 {
503         volatile ushort *pbuf =
504                 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
505         ushort *dbuf = (ushort *) sect_buf;
506
507         debug("in input swap data base for read is %lx\n",
508               (unsigned long) pbuf);
509
510         while (words--) {
511 #ifdef __MIPS__
512                 *dbuf++ = swab16p((u16 *) pbuf);
513                 *dbuf++ = swab16p((u16 *) pbuf);
514 #elif defined(CONFIG_PCS440EP)
515                 *dbuf++ = *pbuf;
516                 *dbuf++ = *pbuf;
517 #else
518                 *dbuf++ = ld_le16(pbuf);
519                 *dbuf++ = ld_le16(pbuf);
520 #endif /* !MIPS */
521         }
522 }
523 #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
524
525
526 #if defined(CONFIG_IDE_SWAP_IO)
527 void __ide_output_data(int dev, const ulong *sect_buf, int words)
528 {
529         ushort *dbuf;
530         volatile ushort *pbuf;
531
532         pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
533         dbuf = (ushort *) sect_buf;
534         while (words--) {
535 #if defined(CONFIG_PCS440EP)
536                 /* not tested, because CF was write protected */
537                 EIEIO;
538                 *pbuf = ld_le16(dbuf++);
539                 EIEIO;
540                 *pbuf = ld_le16(dbuf++);
541 #else
542                 EIEIO;
543                 *pbuf = *dbuf++;
544                 EIEIO;
545                 *pbuf = *dbuf++;
546 #endif
547         }
548 }
549 #else  /* ! CONFIG_IDE_SWAP_IO */
550 void __ide_output_data(int dev, const ulong *sect_buf, int words)
551 {
552 #if defined(CONFIG_IDE_AHB)
553         ide_write_data(dev, sect_buf, words);
554 #else
555         outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
556 #endif
557 }
558 #endif /* CONFIG_IDE_SWAP_IO */
559
560 #if defined(CONFIG_IDE_SWAP_IO)
561 void __ide_input_data(int dev, ulong *sect_buf, int words)
562 {
563         ushort *dbuf;
564         volatile ushort *pbuf;
565
566         pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
567         dbuf = (ushort *) sect_buf;
568
569         debug("in input data base for read is %lx\n", (unsigned long) pbuf);
570
571         while (words--) {
572 #if defined(CONFIG_PCS440EP)
573                 EIEIO;
574                 *dbuf++ = ld_le16(pbuf);
575                 EIEIO;
576                 *dbuf++ = ld_le16(pbuf);
577 #else
578                 EIEIO;
579                 *dbuf++ = *pbuf;
580                 EIEIO;
581                 *dbuf++ = *pbuf;
582 #endif
583         }
584 }
585 #else  /* ! CONFIG_IDE_SWAP_IO */
586 void __ide_input_data(int dev, ulong *sect_buf, int words)
587 {
588 #if defined(CONFIG_IDE_AHB)
589         ide_read_data(dev, sect_buf, words);
590 #else
591         insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
592 #endif
593 }
594
595 #endif /* CONFIG_IDE_SWAP_IO */
596
597 /* -------------------------------------------------------------------------
598  */
599 static void ide_ident(block_dev_desc_t *dev_desc)
600 {
601         unsigned char c;
602         hd_driveid_t iop;
603
604 #ifdef CONFIG_ATAPI
605         int retries = 0;
606 #endif
607
608 #ifdef CONFIG_TUNE_PIO
609         int pio_mode;
610 #endif
611
612 #if 0
613         int mode, cycle_time;
614 #endif
615         int device;
616
617         device = dev_desc->dev;
618         printf("  Device %d: ", device);
619
620         ide_led(DEVICE_LED(device), 1); /* LED on       */
621         /* Select device
622          */
623         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
624         dev_desc->if_type = IF_TYPE_IDE;
625 #ifdef CONFIG_ATAPI
626
627         retries = 0;
628
629         /* Warning: This will be tricky to read */
630         while (retries <= 1) {
631                 /* check signature */
632                 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
633                     (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
634                     (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
635                     (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
636                         /* ATAPI Signature found */
637                         dev_desc->if_type = IF_TYPE_ATAPI;
638                         /*
639                          * Start Ident Command
640                          */
641                         ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
642                         /*
643                          * Wait for completion - ATAPI devices need more time
644                          * to become ready
645                          */
646                         c = ide_wait(device, ATAPI_TIME_OUT);
647                 } else
648 #endif
649                 {
650                         /*
651                          * Start Ident Command
652                          */
653                         ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
654
655                         /*
656                          * Wait for completion
657                          */
658                         c = ide_wait(device, IDE_TIME_OUT);
659                 }
660                 ide_led(DEVICE_LED(device), 0); /* LED off      */
661
662                 if (((c & ATA_STAT_DRQ) == 0) ||
663                     ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
664 #ifdef CONFIG_ATAPI
665                         {
666                                 /*
667                                  * Need to soft reset the device
668                                  * in case it's an ATAPI...
669                                  */
670                                 debug("Retrying...\n");
671                                 ide_outb(device, ATA_DEV_HD,
672                                          ATA_LBA | ATA_DEVICE(device));
673                                 udelay(100000);
674                                 ide_outb(device, ATA_COMMAND, 0x08);
675                                 udelay(500000); /* 500 ms */
676                         }
677                         /*
678                          * Select device
679                          */
680                         ide_outb(device, ATA_DEV_HD,
681                                  ATA_LBA | ATA_DEVICE(device));
682                         retries++;
683 #else
684                         return;
685 #endif
686                 }
687 #ifdef CONFIG_ATAPI
688                 else
689                         break;
690         }                       /* see above - ugly to read */
691
692         if (retries == 2)       /* Not found */
693                 return;
694 #endif
695
696         ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
697
698         ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
699                   sizeof(dev_desc->revision));
700         ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
701                   sizeof(dev_desc->vendor));
702         ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
703                   sizeof(dev_desc->product));
704 #ifdef __LITTLE_ENDIAN
705         /*
706          * firmware revision, model, and serial number have Big Endian Byte
707          * order in Word. Convert all three to little endian.
708          *
709          * See CF+ and CompactFlash Specification Revision 2.0:
710          * 6.2.1.6: Identify Drive, Table 39 for more details
711          */
712
713         strswab(dev_desc->revision);
714         strswab(dev_desc->vendor);
715         strswab(dev_desc->product);
716 #endif /* __LITTLE_ENDIAN */
717
718         if ((iop.config & 0x0080) == 0x0080)
719                 dev_desc->removable = 1;
720         else
721                 dev_desc->removable = 0;
722
723 #ifdef CONFIG_TUNE_PIO
724         /* Mode 0 - 2 only, are directly determined by word 51. */
725         pio_mode = iop.tPIO;
726         if (pio_mode > 2) {
727                 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
728                 /* Force it to dead slow, and hope for the best... */
729                 pio_mode = 0;
730         }
731
732         /* Any CompactFlash Storage Card that supports PIO mode 3 or above
733          * shall set bit 1 of word 53 to one and support the fields contained
734          * in words 64 through 70.
735          */
736         if (iop.field_valid & 0x02) {
737                 /*
738                  * Mode 3 and above are possible.  Check in order from slow
739                  * to fast, so we wind up with the highest mode allowed.
740                  */
741                 if (iop.eide_pio_modes & 0x01)
742                         pio_mode = 3;
743                 if (iop.eide_pio_modes & 0x02)
744                         pio_mode = 4;
745                 if (ata_id_is_cfa((u16 *)&iop)) {
746                         if ((iop.cf_advanced_caps & 0x07) == 0x01)
747                                 pio_mode = 5;
748                         if ((iop.cf_advanced_caps & 0x07) == 0x02)
749                                 pio_mode = 6;
750                 }
751         }
752
753         /* System-specific, depends on bus speeds, etc. */
754         ide_set_piomode(pio_mode);
755 #endif /* CONFIG_TUNE_PIO */
756
757 #if 0
758         /*
759          * Drive PIO mode autoselection
760          */
761         mode = iop.tPIO;
762
763         printf("tPIO = 0x%02x = %d\n", mode, mode);
764         if (mode > 2) {         /* 2 is maximum allowed tPIO value */
765                 mode = 2;
766                 debug("Override tPIO -> 2\n");
767         }
768         if (iop.field_valid & 2) {      /* drive implements ATA2? */
769                 debug("Drive implements ATA2\n");
770                 if (iop.capability & 8) {       /* drive supports use_iordy? */
771                         cycle_time = iop.eide_pio_iordy;
772                 } else {
773                         cycle_time = iop.eide_pio;
774                 }
775                 debug("cycle time = %d\n", cycle_time);
776                 mode = 4;
777                 if (cycle_time > 120)
778                         mode = 3;       /* 120 ns for PIO mode 4 */
779                 if (cycle_time > 180)
780                         mode = 2;       /* 180 ns for PIO mode 3 */
781                 if (cycle_time > 240)
782                         mode = 1;       /* 240 ns for PIO mode 4 */
783                 if (cycle_time > 383)
784                         mode = 0;       /* 383 ns for PIO mode 4 */
785         }
786         printf("PIO mode to use: PIO %d\n", mode);
787 #endif /* 0 */
788
789 #ifdef CONFIG_ATAPI
790         if (dev_desc->if_type == IF_TYPE_ATAPI) {
791                 atapi_inquiry(dev_desc);
792                 return;
793         }
794 #endif /* CONFIG_ATAPI */
795
796 #ifdef __BIG_ENDIAN
797         /* swap shorts */
798         dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
799 #else  /* ! __BIG_ENDIAN */
800         /*
801          * do not swap shorts on little endian
802          *
803          * See CF+ and CompactFlash Specification Revision 2.0:
804          * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
805          */
806         dev_desc->lba = iop.lba_capacity;
807 #endif /* __BIG_ENDIAN */
808
809 #ifdef CONFIG_LBA48
810         if (iop.command_set_2 & 0x0400) {       /* LBA 48 support */
811                 dev_desc->lba48 = 1;
812                 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
813                         ((unsigned long long) iop.lba48_capacity[1] << 16) |
814                         ((unsigned long long) iop.lba48_capacity[2] << 32) |
815                         ((unsigned long long) iop.lba48_capacity[3] << 48);
816         } else {
817                 dev_desc->lba48 = 0;
818         }
819 #endif /* CONFIG_LBA48 */
820         /* assuming HD */
821         dev_desc->type = DEV_TYPE_HARDDISK;
822         dev_desc->blksz = ATA_BLOCKSIZE;
823         dev_desc->lun = 0;      /* just to fill something in... */
824
825 #if 0                           /* only used to test the powersaving mode,
826                                  * if enabled, the drive goes after 5 sec
827                                  * in standby mode */
828         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
829         c = ide_wait(device, IDE_TIME_OUT);
830         ide_outb(device, ATA_SECT_CNT, 1);
831         ide_outb(device, ATA_LBA_LOW, 0);
832         ide_outb(device, ATA_LBA_MID, 0);
833         ide_outb(device, ATA_LBA_HIGH, 0);
834         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
835         ide_outb(device, ATA_COMMAND, 0xe3);
836         udelay(50);
837         c = ide_wait(device, IDE_TIME_OUT);     /* can't take over 500 ms */
838 #endif
839 }
840
841
842 /* ------------------------------------------------------------------------- */
843
844 ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
845 {
846         ulong n = 0;
847         unsigned char c;
848         unsigned char pwrsave = 0;      /* power save */
849
850 #ifdef CONFIG_LBA48
851         unsigned char lba48 = 0;
852
853         if (blknr & 0x0000fffff0000000ULL) {
854                 /* more than 28 bits used, use 48bit mode */
855                 lba48 = 1;
856         }
857 #endif
858         debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
859               device, blknr, blkcnt, (ulong) buffer);
860
861         ide_led(DEVICE_LED(device), 1); /* LED on       */
862
863         /* Select device
864          */
865         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
866         c = ide_wait(device, IDE_TIME_OUT);
867
868         if (c & ATA_STAT_BUSY) {
869                 printf("IDE read: device %d not ready\n", device);
870                 goto IDE_READ_E;
871         }
872
873         /* first check if the drive is in Powersaving mode, if yes,
874          * increase the timeout value */
875         ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
876         udelay(50);
877
878         c = ide_wait(device, IDE_TIME_OUT);     /* can't take over 500 ms */
879
880         if (c & ATA_STAT_BUSY) {
881                 printf("IDE read: device %d not ready\n", device);
882                 goto IDE_READ_E;
883         }
884         if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
885                 printf("No Powersaving mode %X\n", c);
886         } else {
887                 c = ide_inb(device, ATA_SECT_CNT);
888                 debug("Powersaving %02X\n", c);
889                 if (c == 0)
890                         pwrsave = 1;
891         }
892
893
894         while (blkcnt-- > 0) {
895
896                 c = ide_wait(device, IDE_TIME_OUT);
897
898                 if (c & ATA_STAT_BUSY) {
899                         printf("IDE read: device %d not ready\n", device);
900                         break;
901                 }
902 #ifdef CONFIG_LBA48
903                 if (lba48) {
904                         /* write high bits */
905                         ide_outb(device, ATA_SECT_CNT, 0);
906                         ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
907 #ifdef CONFIG_SYS_64BIT_LBA
908                         ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
909                         ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
910 #else
911                         ide_outb(device, ATA_LBA_MID, 0);
912                         ide_outb(device, ATA_LBA_HIGH, 0);
913 #endif
914                 }
915 #endif
916                 ide_outb(device, ATA_SECT_CNT, 1);
917                 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
918                 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
919                 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
920
921 #ifdef CONFIG_LBA48
922                 if (lba48) {
923                         ide_outb(device, ATA_DEV_HD,
924                                  ATA_LBA | ATA_DEVICE(device));
925                         ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
926
927                 } else
928 #endif
929                 {
930                         ide_outb(device, ATA_DEV_HD, ATA_LBA |
931                                  ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
932                         ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
933                 }
934
935                 udelay(50);
936
937                 if (pwrsave) {
938                         /* may take up to 4 sec */
939                         c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
940                         pwrsave = 0;
941                 } else {
942                         /* can't take over 500 ms */
943                         c = ide_wait(device, IDE_TIME_OUT);
944                 }
945
946                 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
947                     ATA_STAT_DRQ) {
948 #if defined(CONFIG_SYS_64BIT_LBA)
949                         printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
950                                 device, blknr, c);
951 #else
952                         printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
953                                 device, (ulong) blknr, c);
954 #endif
955                         break;
956                 }
957
958                 ide_input_data(device, buffer, ATA_SECTORWORDS);
959                 (void) ide_inb(device, ATA_STATUS);     /* clear IRQ */
960
961                 ++n;
962                 ++blknr;
963                 buffer += ATA_BLOCKSIZE;
964         }
965 IDE_READ_E:
966         ide_led(DEVICE_LED(device), 0); /* LED off      */
967         return (n);
968 }
969
970 /* ------------------------------------------------------------------------- */
971
972
973 ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
974 {
975         ulong n = 0;
976         unsigned char c;
977
978 #ifdef CONFIG_LBA48
979         unsigned char lba48 = 0;
980
981         if (blknr & 0x0000fffff0000000ULL) {
982                 /* more than 28 bits used, use 48bit mode */
983                 lba48 = 1;
984         }
985 #endif
986
987         ide_led(DEVICE_LED(device), 1); /* LED on       */
988
989         /* Select device
990          */
991         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
992
993         while (blkcnt-- > 0) {
994
995                 c = ide_wait(device, IDE_TIME_OUT);
996
997                 if (c & ATA_STAT_BUSY) {
998                         printf("IDE read: device %d not ready\n", device);
999                         goto WR_OUT;
1000                 }
1001 #ifdef CONFIG_LBA48
1002                 if (lba48) {
1003                         /* write high bits */
1004                         ide_outb(device, ATA_SECT_CNT, 0);
1005                         ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1006 #ifdef CONFIG_SYS_64BIT_LBA
1007                         ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1008                         ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1009 #else
1010                         ide_outb(device, ATA_LBA_MID, 0);
1011                         ide_outb(device, ATA_LBA_HIGH, 0);
1012 #endif
1013                 }
1014 #endif
1015                 ide_outb(device, ATA_SECT_CNT, 1);
1016                 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1017                 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1018                 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1019
1020 #ifdef CONFIG_LBA48
1021                 if (lba48) {
1022                         ide_outb(device, ATA_DEV_HD,
1023                                  ATA_LBA | ATA_DEVICE(device));
1024                         ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1025
1026                 } else
1027 #endif
1028                 {
1029                         ide_outb(device, ATA_DEV_HD, ATA_LBA |
1030                                  ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1031                         ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1032                 }
1033
1034                 udelay(50);
1035
1036                 /* can't take over 500 ms */
1037                 c = ide_wait(device, IDE_TIME_OUT);
1038
1039                 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1040                     ATA_STAT_DRQ) {
1041 #if defined(CONFIG_SYS_64BIT_LBA)
1042                         printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
1043                                 device, blknr, c);
1044 #else
1045                         printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1046                                 device, (ulong) blknr, c);
1047 #endif
1048                         goto WR_OUT;
1049                 }
1050
1051                 ide_output_data(device, buffer, ATA_SECTORWORDS);
1052                 c = ide_inb(device, ATA_STATUS);        /* clear IRQ */
1053                 ++n;
1054                 ++blknr;
1055                 buffer += ATA_BLOCKSIZE;
1056         }
1057 WR_OUT:
1058         ide_led(DEVICE_LED(device), 0); /* LED off      */
1059         return (n);
1060 }
1061
1062 /* ------------------------------------------------------------------------- */
1063
1064 /*
1065  * copy src to dest, skipping leading and trailing blanks and null
1066  * terminate the string
1067  * "len" is the size of available memory including the terminating '\0'
1068  */
1069 static void ident_cpy(unsigned char *dst, unsigned char *src,
1070                       unsigned int len)
1071 {
1072         unsigned char *end, *last;
1073
1074         last = dst;
1075         end = src + len - 1;
1076
1077         /* reserve space for '\0' */
1078         if (len < 2)
1079                 goto OUT;
1080
1081         /* skip leading white space */
1082         while ((*src) && (src < end) && (*src == ' '))
1083                 ++src;
1084
1085         /* copy string, omitting trailing white space */
1086         while ((*src) && (src < end)) {
1087                 *dst++ = *src;
1088                 if (*src++ != ' ')
1089                         last = dst;
1090         }
1091 OUT:
1092         *last = '\0';
1093 }
1094
1095 /* ------------------------------------------------------------------------- */
1096
1097 /*
1098  * Wait until Busy bit is off, or timeout (in ms)
1099  * Return last status
1100  */
1101 static uchar ide_wait(int dev, ulong t)
1102 {
1103         ulong delay = 10 * t;   /* poll every 100 us */
1104         uchar c;
1105
1106         while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1107                 udelay(100);
1108                 if (delay-- == 0)
1109                         break;
1110         }
1111         return (c);
1112 }
1113
1114 /* ------------------------------------------------------------------------- */
1115
1116 #ifdef CONFIG_IDE_RESET
1117 extern void ide_set_reset(int idereset);
1118
1119 static void ide_reset(void)
1120 {
1121         int i;
1122
1123         curr_device = -1;
1124         for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
1125                 ide_bus_ok[i] = 0;
1126         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
1127                 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1128
1129         ide_set_reset(1);       /* assert reset */
1130
1131         /* the reset signal shall be asserted for et least 25 us */
1132         udelay(25);
1133
1134         WATCHDOG_RESET();
1135
1136         /* de-assert RESET signal */
1137         ide_set_reset(0);
1138
1139         /* wait 250 ms */
1140         for (i = 0; i < 250; ++i)
1141                 udelay(1000);
1142 }
1143
1144 #endif /* CONFIG_IDE_RESET */
1145
1146 /* ------------------------------------------------------------------------- */
1147
1148 #if defined(CONFIG_IDE_LED)     && \
1149    !defined(CONFIG_CPC45)       && \
1150    !defined(CONFIG_KUP4K)       && \
1151    !defined(CONFIG_KUP4X)
1152
1153 static uchar led_buffer;        /* Buffer for current LED status        */
1154
1155 static void ide_led(uchar led, uchar status)
1156 {
1157         uchar *led_port = LED_PORT;
1158
1159         if (status)             /* switch LED on        */
1160                 led_buffer |= led;
1161         else                    /* switch LED off       */
1162                 led_buffer &= ~led;
1163
1164         *led_port = led_buffer;
1165 }
1166
1167 #endif /* CONFIG_IDE_LED */
1168
1169 #if defined(CONFIG_OF_IDE_FIXUP)
1170 int ide_device_present(int dev)
1171 {
1172         if (dev >= CONFIG_SYS_IDE_MAXBUS)
1173                 return 0;
1174         return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1175 }
1176 #endif
1177 /* ------------------------------------------------------------------------- */
1178
1179 #ifdef CONFIG_ATAPI
1180 /****************************************************************************
1181  * ATAPI Support
1182  */
1183
1184 void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1185         __attribute__ ((weak, alias("__ide_input_data_shorts")));
1186
1187 void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1188         __attribute__ ((weak, alias("__ide_output_data_shorts")));
1189
1190
1191 #if defined(CONFIG_IDE_SWAP_IO)
1192 /* since ATAPI may use commands with not 4 bytes alligned length
1193  * we have our own transfer functions, 2 bytes alligned */
1194 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1195 {
1196         ushort *dbuf;
1197         volatile ushort *pbuf;
1198
1199         pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1200         dbuf = (ushort *) sect_buf;
1201
1202         debug("in output data shorts base for read is %lx\n",
1203               (unsigned long) pbuf);
1204
1205         while (shorts--) {
1206                 EIEIO;
1207                 *pbuf = *dbuf++;
1208         }
1209 }
1210
1211 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1212 {
1213         ushort *dbuf;
1214         volatile ushort *pbuf;
1215
1216         pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1217         dbuf = (ushort *) sect_buf;
1218
1219         debug("in input data shorts base for read is %lx\n",
1220               (unsigned long) pbuf);
1221
1222         while (shorts--) {
1223                 EIEIO;
1224                 *dbuf++ = *pbuf;
1225         }
1226 }
1227
1228 #else  /* ! CONFIG_IDE_SWAP_IO */
1229 void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1230 {
1231         outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1232 }
1233
1234 void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1235 {
1236         insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1237 }
1238
1239 #endif /* CONFIG_IDE_SWAP_IO */
1240
1241 /*
1242  * Wait until (Status & mask) == res, or timeout (in ms)
1243  * Return last status
1244  * This is used since some ATAPI CD ROMs clears their Busy Bit first
1245  * and then they set their DRQ Bit
1246  */
1247 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1248 {
1249         ulong delay = 10 * t;   /* poll every 100 us */
1250         uchar c;
1251
1252         /* prevents to read the status before valid */
1253         c = ide_inb(dev, ATA_DEV_CTL);
1254
1255         while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1256                 /* break if error occurs (doesn't make sense to wait more) */
1257                 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1258                         break;
1259                 udelay(100);
1260                 if (delay-- == 0)
1261                         break;
1262         }
1263         return (c);
1264 }
1265
1266 /*
1267  * issue an atapi command
1268  */
1269 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1270                           unsigned char *buffer, int buflen)
1271 {
1272         unsigned char c, err, mask, res;
1273         int n;
1274
1275         ide_led(DEVICE_LED(device), 1); /* LED on       */
1276
1277         /* Select device
1278          */
1279         mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1280         res = 0;
1281         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1282         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1283         if ((c & mask) != res) {
1284                 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1285                        c);
1286                 err = 0xFF;
1287                 goto AI_OUT;
1288         }
1289         /* write taskfile */
1290         ide_outb(device, ATA_ERROR_REG, 0);     /* no DMA, no overlaped */
1291         ide_outb(device, ATA_SECT_CNT, 0);
1292         ide_outb(device, ATA_SECT_NUM, 0);
1293         ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1294         ide_outb(device, ATA_CYL_HIGH,
1295                  (unsigned char) ((buflen >> 8) & 0xFF));
1296         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1297
1298         ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1299         udelay(50);
1300
1301         mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1302         res = ATA_STAT_DRQ;
1303         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1304
1305         if ((c & mask) != res) {        /* DRQ must be 1, BSY 0 */
1306                 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1307                         device, c);
1308                 err = 0xFF;
1309                 goto AI_OUT;
1310         }
1311
1312         /* write command block */
1313         ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1314
1315         /* ATAPI Command written wait for completition */
1316         udelay(5000);           /* device must set bsy */
1317
1318         mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1319         /*
1320          * if no data wait for DRQ = 0 BSY = 0
1321          * if data wait for DRQ = 1 BSY = 0
1322          */
1323         res = 0;
1324         if (buflen)
1325                 res = ATA_STAT_DRQ;
1326         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1327         if ((c & mask) != res) {
1328                 if (c & ATA_STAT_ERR) {
1329                         err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1330                         debug("atapi_issue 1 returned sense key %X status %02X\n",
1331                                 err, c);
1332                 } else {
1333                         printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x)  status 0x%02x\n",
1334                                 ccb[0], c);
1335                         err = 0xFF;
1336                 }
1337                 goto AI_OUT;
1338         }
1339         n = ide_inb(device, ATA_CYL_HIGH);
1340         n <<= 8;
1341         n += ide_inb(device, ATA_CYL_LOW);
1342         if (n > buflen) {
1343                 printf("ERROR, transfer bytes %d requested only %d\n", n,
1344                        buflen);
1345                 err = 0xff;
1346                 goto AI_OUT;
1347         }
1348         if ((n == 0) && (buflen < 0)) {
1349                 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1350                 err = 0xff;
1351                 goto AI_OUT;
1352         }
1353         if (n != buflen) {
1354                 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1355                         n, buflen);
1356         }
1357         if (n != 0) {           /* data transfer */
1358                 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1359                 /* we transfer shorts */
1360                 n >>= 1;
1361                 /* ok now decide if it is an in or output */
1362                 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1363                         debug("Write to device\n");
1364                         ide_output_data_shorts(device,
1365                                 (unsigned short *) buffer, n);
1366                 } else {
1367                         debug("Read from device @ %p shorts %d\n", buffer, n);
1368                         ide_input_data_shorts(device,
1369                                 (unsigned short *) buffer, n);
1370                 }
1371         }
1372         udelay(5000);           /* seems that some CD ROMs need this... */
1373         mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1374         res = 0;
1375         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1376         if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1377                 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1378                 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1379                       c);
1380         } else {
1381                 err = 0;
1382         }
1383 AI_OUT:
1384         ide_led(DEVICE_LED(device), 0); /* LED off      */
1385         return (err);
1386 }
1387
1388 /*
1389  * sending the command to atapi_issue. If an status other than good
1390  * returns, an request_sense will be issued
1391  */
1392
1393 #define ATAPI_DRIVE_NOT_READY   100
1394 #define ATAPI_UNIT_ATTN         10
1395
1396 unsigned char atapi_issue_autoreq(int device,
1397                                   unsigned char *ccb,
1398                                   int ccblen,
1399                                   unsigned char *buffer, int buflen)
1400 {
1401         unsigned char sense_data[18], sense_ccb[12];
1402         unsigned char res, key, asc, ascq;
1403         int notready, unitattn;
1404
1405         unitattn = ATAPI_UNIT_ATTN;
1406         notready = ATAPI_DRIVE_NOT_READY;
1407
1408 retry:
1409         res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1410         if (res == 0)
1411                 return 0;       /* Ok */
1412
1413         if (res == 0xFF)
1414                 return 0xFF;    /* error */
1415
1416         debug("(auto_req)atapi_issue returned sense key %X\n", res);
1417
1418         memset(sense_ccb, 0, sizeof(sense_ccb));
1419         memset(sense_data, 0, sizeof(sense_data));
1420         sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1421         sense_ccb[4] = 18;      /* allocation Length */
1422
1423         res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1424         key = (sense_data[2] & 0xF);
1425         asc = (sense_data[12]);
1426         ascq = (sense_data[13]);
1427
1428         debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1429         debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1430               sense_data[0], key, asc, ascq);
1431
1432         if ((key == 0))
1433                 return 0;       /* ok device ready */
1434
1435         if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1436                 if (unitattn-- > 0) {
1437                         udelay(200 * 1000);
1438                         goto retry;
1439                 }
1440                 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1441                 goto error;
1442         }
1443         if ((asc == 0x4) && (ascq == 0x1)) {
1444                 /* not ready, but will be ready soon */
1445                 if (notready-- > 0) {
1446                         udelay(200 * 1000);
1447                         goto retry;
1448                 }
1449                 printf("Drive not ready, tried %d times\n",
1450                        ATAPI_DRIVE_NOT_READY);
1451                 goto error;
1452         }
1453         if (asc == 0x3a) {
1454                 debug("Media not present\n");
1455                 goto error;
1456         }
1457
1458         printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1459                ascq);
1460 error:
1461         debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1462         return (0xFF);
1463 }
1464
1465
1466 static void atapi_inquiry(block_dev_desc_t *dev_desc)
1467 {
1468         unsigned char ccb[12];  /* Command descriptor block */
1469         unsigned char iobuf[64];        /* temp buf */
1470         unsigned char c;
1471         int device;
1472
1473         device = dev_desc->dev;
1474         dev_desc->type = DEV_TYPE_UNKNOWN;      /* not yet valid */
1475         dev_desc->block_read = atapi_read;
1476
1477         memset(ccb, 0, sizeof(ccb));
1478         memset(iobuf, 0, sizeof(iobuf));
1479
1480         ccb[0] = ATAPI_CMD_INQUIRY;
1481         ccb[4] = 40;            /* allocation Legnth */
1482         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1483
1484         debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1485         if (c != 0)
1486                 return;
1487
1488         /* copy device ident strings */
1489         ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1490         ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1491         ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1492
1493         dev_desc->lun = 0;
1494         dev_desc->lba = 0;
1495         dev_desc->blksz = 0;
1496         dev_desc->type = iobuf[0] & 0x1f;
1497
1498         if ((iobuf[1] & 0x80) == 0x80)
1499                 dev_desc->removable = 1;
1500         else
1501                 dev_desc->removable = 0;
1502
1503         memset(ccb, 0, sizeof(ccb));
1504         memset(iobuf, 0, sizeof(iobuf));
1505         ccb[0] = ATAPI_CMD_START_STOP;
1506         ccb[4] = 0x03;          /* start */
1507
1508         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1509
1510         debug("ATAPI_CMD_START_STOP returned %x\n", c);
1511         if (c != 0)
1512                 return;
1513
1514         memset(ccb, 0, sizeof(ccb));
1515         memset(iobuf, 0, sizeof(iobuf));
1516         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1517
1518         debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1519         if (c != 0)
1520                 return;
1521
1522         memset(ccb, 0, sizeof(ccb));
1523         memset(iobuf, 0, sizeof(iobuf));
1524         ccb[0] = ATAPI_CMD_READ_CAP;
1525         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1526         debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1527         if (c != 0)
1528                 return;
1529
1530         debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1531               iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1532               iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1533
1534         dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1535                 ((unsigned long) iobuf[1] << 16) +
1536                 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1537         dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1538                 ((unsigned long) iobuf[5] << 16) +
1539                 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1540 #ifdef CONFIG_LBA48
1541         /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1542         dev_desc->lba48 = 0;
1543 #endif
1544         return;
1545 }
1546
1547
1548 /*
1549  * atapi_read:
1550  * we transfer only one block per command, since the multiple DRQ per
1551  * command is not yet implemented
1552  */
1553 #define ATAPI_READ_MAX_BYTES    2048    /* we read max 2kbytes */
1554 #define ATAPI_READ_BLOCK_SIZE   2048    /* assuming CD part */
1555 #define ATAPI_READ_MAX_BLOCK    (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1556
1557 ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
1558 {
1559         ulong n = 0;
1560         unsigned char ccb[12];  /* Command descriptor block */
1561         ulong cnt;
1562
1563         debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1564               device, blknr, blkcnt, (ulong) buffer);
1565
1566         do {
1567                 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1568                         cnt = ATAPI_READ_MAX_BLOCK;
1569                 else
1570                         cnt = blkcnt;
1571
1572                 ccb[0] = ATAPI_CMD_READ_12;
1573                 ccb[1] = 0;     /* reserved */
1574                 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF;  /* MSB Block */
1575                 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF;  /*  */
1576                 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1577                 ccb[5] = (unsigned char) blknr & 0xFF;  /* LSB Block */
1578                 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1579                 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1580                 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1581                 ccb[9] = (unsigned char) cnt & 0xFF;    /* LSB Block */
1582                 ccb[10] = 0;    /* reserved */
1583                 ccb[11] = 0;    /* reserved */
1584
1585                 if (atapi_issue_autoreq(device, ccb, 12,
1586                                         (unsigned char *) buffer,
1587                                         cnt * ATAPI_READ_BLOCK_SIZE)
1588                     == 0xFF) {
1589                         return (n);
1590                 }
1591                 n += cnt;
1592                 blkcnt -= cnt;
1593                 blknr += cnt;
1594                 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1595         } while (blkcnt > 0);
1596         return (n);
1597 }
1598
1599 /* ------------------------------------------------------------------------- */
1600
1601 #endif /* CONFIG_ATAPI */
1602
1603 U_BOOT_CMD(ide, 5, 1, do_ide,
1604            "IDE sub-system",
1605            "reset - reset IDE controller\n"
1606            "ide info  - show available IDE devices\n"
1607            "ide device [dev] - show or set current device\n"
1608            "ide part [dev] - print partition table of one or all IDE devices\n"
1609            "ide read  addr blk# cnt\n"
1610            "ide write addr blk# cnt - read/write `cnt'"
1611            " blocks starting at block `blk#'\n"
1612            "    to/from memory address `addr'");
1613
1614 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1615            "boot from IDE device", "loadAddr dev:part");