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