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