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