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