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