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