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