block: ide: Remove ide_preinit function
[platform/kernel/u-boot.git] / drivers / block / ide.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2011
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #define LOG_CATEGORY UCLASS_IDE
8
9 #include <common.h>
10 #include <ata.h>
11 #include <blk.h>
12 #include <dm.h>
13 #include <ide.h>
14 #include <log.h>
15 #include <part.h>
16 #include <watchdog.h>
17 #include <asm/io.h>
18 #include <linux/delay.h>
19
20 #ifdef __PPC__
21 # define EIEIO          __asm__ volatile ("eieio")
22 # define SYNC           __asm__ volatile ("sync")
23 #else
24 # define EIEIO          /* nothing */
25 # define SYNC           /* nothing */
26 #endif
27
28 /* Current offset for IDE0 / IDE1 bus access    */
29 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
30 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
31         CONFIG_SYS_ATA_IDE0_OFFSET,
32 #endif
33 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
34         CONFIG_SYS_ATA_IDE1_OFFSET,
35 #endif
36 };
37
38 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
39
40 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
41
42 #define IDE_TIME_OUT    2000    /* 2 sec timeout */
43
44 #define ATAPI_TIME_OUT  7000    /* 7 sec timeout (5 sec seems to work...) */
45
46 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
47
48 #ifdef CONFIG_IDE_RESET
49 extern void ide_set_reset(int idereset);
50
51 static void ide_reset(void)
52 {
53         int i;
54
55         for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
56                 ide_bus_ok[i] = 0;
57         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
58                 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
59
60         ide_set_reset(1);       /* assert reset */
61
62         /* the reset signal shall be asserted for et least 25 us */
63         udelay(25);
64
65         WATCHDOG_RESET();
66
67         /* de-assert RESET signal */
68         ide_set_reset(0);
69
70         /* wait 250 ms */
71         for (i = 0; i < 250; ++i)
72                 udelay(1000);
73 }
74 #else
75 #define ide_reset()     /* dummy */
76 #endif /* CONFIG_IDE_RESET */
77
78 /*
79  * Wait until Busy bit is off, or timeout (in ms)
80  * Return last status
81  */
82 static uchar ide_wait(int dev, ulong t)
83 {
84         ulong delay = 10 * t;   /* poll every 100 us */
85         uchar c;
86
87         while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
88                 udelay(100);
89                 if (delay-- == 0)
90                         break;
91         }
92         return c;
93 }
94
95 /*
96  * copy src to dest, skipping leading and trailing blanks and null
97  * terminate the string
98  * "len" is the size of available memory including the terminating '\0'
99  */
100 static void ident_cpy(unsigned char *dst, unsigned char *src,
101                       unsigned int len)
102 {
103         unsigned char *end, *last;
104
105         last = dst;
106         end = src + len - 1;
107
108         /* reserve space for '\0' */
109         if (len < 2)
110                 goto OUT;
111
112         /* skip leading white space */
113         while ((*src) && (src < end) && (*src == ' '))
114                 ++src;
115
116         /* copy string, omitting trailing white space */
117         while ((*src) && (src < end)) {
118                 *dst++ = *src;
119                 if (*src++ != ' ')
120                         last = dst;
121         }
122 OUT:
123         *last = '\0';
124 }
125
126 #ifdef CONFIG_ATAPI
127 /****************************************************************************
128  * ATAPI Support
129  */
130
131 /* since ATAPI may use commands with not 4 bytes alligned length
132  * we have our own transfer functions, 2 bytes alligned */
133 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
134 {
135         uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
136         ushort *dbuf;
137
138         dbuf = (ushort *)sect_buf;
139
140         debug("in output data shorts base for read is %p\n", (void *)paddr);
141
142         while (shorts--) {
143                 EIEIO;
144                 outw(cpu_to_le16(*dbuf++), paddr);
145         }
146 }
147
148 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
149 {
150         uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
151         ushort *dbuf;
152
153         dbuf = (ushort *)sect_buf;
154
155         debug("in input data shorts base for read is %p\n", (void *)paddr);
156
157         while (shorts--) {
158                 EIEIO;
159                 *dbuf++ = le16_to_cpu(inw(paddr));
160         }
161 }
162
163 /*
164  * Wait until (Status & mask) == res, or timeout (in ms)
165  * Return last status
166  * This is used since some ATAPI CD ROMs clears their Busy Bit first
167  * and then they set their DRQ Bit
168  */
169 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
170 {
171         ulong delay = 10 * t;   /* poll every 100 us */
172         uchar c;
173
174         /* prevents to read the status before valid */
175         c = ide_inb(dev, ATA_DEV_CTL);
176
177         while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
178                 /* break if error occurs (doesn't make sense to wait more) */
179                 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
180                         break;
181                 udelay(100);
182                 if (delay-- == 0)
183                         break;
184         }
185         return c;
186 }
187
188 /*
189  * issue an atapi command
190  */
191 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
192                           unsigned char *buffer, int buflen)
193 {
194         unsigned char c, err, mask, res;
195         int n;
196
197         /* Select device
198          */
199         mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
200         res = 0;
201         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
202         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
203         if ((c & mask) != res) {
204                 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
205                        c);
206                 err = 0xFF;
207                 goto AI_OUT;
208         }
209         /* write taskfile */
210         ide_outb(device, ATA_ERROR_REG, 0);     /* no DMA, no overlaped */
211         ide_outb(device, ATA_SECT_CNT, 0);
212         ide_outb(device, ATA_SECT_NUM, 0);
213         ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
214         ide_outb(device, ATA_CYL_HIGH,
215                  (unsigned char) ((buflen >> 8) & 0xFF));
216         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
217
218         ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
219         udelay(50);
220
221         mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
222         res = ATA_STAT_DRQ;
223         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
224
225         if ((c & mask) != res) {        /* DRQ must be 1, BSY 0 */
226                 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
227                        device, c);
228                 err = 0xFF;
229                 goto AI_OUT;
230         }
231
232         /* write command block */
233         ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
234
235         /* ATAPI Command written wait for completition */
236         udelay(5000);           /* device must set bsy */
237
238         mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
239         /*
240          * if no data wait for DRQ = 0 BSY = 0
241          * if data wait for DRQ = 1 BSY = 0
242          */
243         res = 0;
244         if (buflen)
245                 res = ATA_STAT_DRQ;
246         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
247         if ((c & mask) != res) {
248                 if (c & ATA_STAT_ERR) {
249                         err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
250                         debug("atapi_issue 1 returned sense key %X status %02X\n",
251                               err, c);
252                 } else {
253                         printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x)  status 0x%02x\n",
254                                ccb[0], c);
255                         err = 0xFF;
256                 }
257                 goto AI_OUT;
258         }
259         n = ide_inb(device, ATA_CYL_HIGH);
260         n <<= 8;
261         n += ide_inb(device, ATA_CYL_LOW);
262         if (n > buflen) {
263                 printf("ERROR, transfer bytes %d requested only %d\n", n,
264                        buflen);
265                 err = 0xff;
266                 goto AI_OUT;
267         }
268         if ((n == 0) && (buflen < 0)) {
269                 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
270                 err = 0xff;
271                 goto AI_OUT;
272         }
273         if (n != buflen) {
274                 debug("WARNING, transfer bytes %d not equal with requested %d\n",
275                       n, buflen);
276         }
277         if (n != 0) {           /* data transfer */
278                 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
279                 /* we transfer shorts */
280                 n >>= 1;
281                 /* ok now decide if it is an in or output */
282                 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
283                         debug("Write to device\n");
284                         ide_output_data_shorts(device, (unsigned short *)buffer,
285                                                n);
286                 } else {
287                         debug("Read from device @ %p shorts %d\n", buffer, n);
288                         ide_input_data_shorts(device, (unsigned short *)buffer,
289                                               n);
290                 }
291         }
292         udelay(5000);           /* seems that some CD ROMs need this... */
293         mask = ATA_STAT_BUSY | ATA_STAT_ERR;
294         res = 0;
295         c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
296         if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
297                 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
298                 debug("atapi_issue 2 returned sense key %X status %X\n", err,
299                       c);
300         } else {
301                 err = 0;
302         }
303 AI_OUT:
304         return err;
305 }
306
307 /*
308  * sending the command to atapi_issue. If an status other than good
309  * returns, an request_sense will be issued
310  */
311
312 #define ATAPI_DRIVE_NOT_READY   100
313 #define ATAPI_UNIT_ATTN         10
314
315 unsigned char atapi_issue_autoreq(int device,
316                                   unsigned char *ccb,
317                                   int ccblen,
318                                   unsigned char *buffer, int buflen)
319 {
320         unsigned char sense_data[18], sense_ccb[12];
321         unsigned char res, key, asc, ascq;
322         int notready, unitattn;
323
324         unitattn = ATAPI_UNIT_ATTN;
325         notready = ATAPI_DRIVE_NOT_READY;
326
327 retry:
328         res = atapi_issue(device, ccb, ccblen, buffer, buflen);
329         if (res == 0)
330                 return 0;       /* Ok */
331
332         if (res == 0xFF)
333                 return 0xFF;    /* error */
334
335         debug("(auto_req)atapi_issue returned sense key %X\n", res);
336
337         memset(sense_ccb, 0, sizeof(sense_ccb));
338         memset(sense_data, 0, sizeof(sense_data));
339         sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
340         sense_ccb[4] = 18;      /* allocation Length */
341
342         res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
343         key = (sense_data[2] & 0xF);
344         asc = (sense_data[12]);
345         ascq = (sense_data[13]);
346
347         debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
348         debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
349               sense_data[0], key, asc, ascq);
350
351         if ((key == 0))
352                 return 0;       /* ok device ready */
353
354         if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
355                 if (unitattn-- > 0) {
356                         udelay(200 * 1000);
357                         goto retry;
358                 }
359                 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
360                 goto error;
361         }
362         if ((asc == 0x4) && (ascq == 0x1)) {
363                 /* not ready, but will be ready soon */
364                 if (notready-- > 0) {
365                         udelay(200 * 1000);
366                         goto retry;
367                 }
368                 printf("Drive not ready, tried %d times\n",
369                        ATAPI_DRIVE_NOT_READY);
370                 goto error;
371         }
372         if (asc == 0x3a) {
373                 debug("Media not present\n");
374                 goto error;
375         }
376
377         printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
378                ascq);
379 error:
380         debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
381         return 0xFF;
382 }
383
384 /*
385  * atapi_read:
386  * we transfer only one block per command, since the multiple DRQ per
387  * command is not yet implemented
388  */
389 #define ATAPI_READ_MAX_BYTES    2048    /* we read max 2kbytes */
390 #define ATAPI_READ_BLOCK_SIZE   2048    /* assuming CD part */
391 #define ATAPI_READ_MAX_BLOCK    (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
392
393 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
394                  void *buffer)
395 {
396         int device = block_dev->devnum;
397         ulong n = 0;
398         unsigned char ccb[12];  /* Command descriptor block */
399         ulong cnt;
400
401         debug("atapi_read dev %d start " LBAF " blocks " LBAF
402               " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
403
404         do {
405                 if (blkcnt > ATAPI_READ_MAX_BLOCK)
406                         cnt = ATAPI_READ_MAX_BLOCK;
407                 else
408                         cnt = blkcnt;
409
410                 ccb[0] = ATAPI_CMD_READ_12;
411                 ccb[1] = 0;     /* reserved */
412                 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF;  /* MSB Block */
413                 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF;  /*  */
414                 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
415                 ccb[5] = (unsigned char) blknr & 0xFF;  /* LSB Block */
416                 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
417                 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
418                 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
419                 ccb[9] = (unsigned char) cnt & 0xFF;    /* LSB Block */
420                 ccb[10] = 0;    /* reserved */
421                 ccb[11] = 0;    /* reserved */
422
423                 if (atapi_issue_autoreq(device, ccb, 12,
424                                         (unsigned char *)buffer,
425                                         cnt * ATAPI_READ_BLOCK_SIZE)
426                     == 0xFF) {
427                         return n;
428                 }
429                 n += cnt;
430                 blkcnt -= cnt;
431                 blknr += cnt;
432                 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
433         } while (blkcnt > 0);
434         return n;
435 }
436
437 static void atapi_inquiry(struct blk_desc *dev_desc)
438 {
439         unsigned char ccb[12];  /* Command descriptor block */
440         unsigned char iobuf[64];        /* temp buf */
441         unsigned char c;
442         int device;
443
444         device = dev_desc->devnum;
445         dev_desc->type = DEV_TYPE_UNKNOWN;      /* not yet valid */
446 #ifndef CONFIG_BLK
447         dev_desc->block_read = atapi_read;
448 #endif
449
450         memset(ccb, 0, sizeof(ccb));
451         memset(iobuf, 0, sizeof(iobuf));
452
453         ccb[0] = ATAPI_CMD_INQUIRY;
454         ccb[4] = 40;            /* allocation Legnth */
455         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
456
457         debug("ATAPI_CMD_INQUIRY returned %x\n", c);
458         if (c != 0)
459                 return;
460
461         /* copy device ident strings */
462         ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
463         ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
464         ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
465
466         dev_desc->lun = 0;
467         dev_desc->lba = 0;
468         dev_desc->blksz = 0;
469         dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
470         dev_desc->type = iobuf[0] & 0x1f;
471
472         if ((iobuf[1] & 0x80) == 0x80)
473                 dev_desc->removable = 1;
474         else
475                 dev_desc->removable = 0;
476
477         memset(ccb, 0, sizeof(ccb));
478         memset(iobuf, 0, sizeof(iobuf));
479         ccb[0] = ATAPI_CMD_START_STOP;
480         ccb[4] = 0x03;          /* start */
481
482         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
483
484         debug("ATAPI_CMD_START_STOP returned %x\n", c);
485         if (c != 0)
486                 return;
487
488         memset(ccb, 0, sizeof(ccb));
489         memset(iobuf, 0, sizeof(iobuf));
490         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
491
492         debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
493         if (c != 0)
494                 return;
495
496         memset(ccb, 0, sizeof(ccb));
497         memset(iobuf, 0, sizeof(iobuf));
498         ccb[0] = ATAPI_CMD_READ_CAP;
499         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
500         debug("ATAPI_CMD_READ_CAP returned %x\n", c);
501         if (c != 0)
502                 return;
503
504         debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
505               iobuf[0], iobuf[1], iobuf[2], iobuf[3],
506               iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
507
508         dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
509                 ((unsigned long) iobuf[1] << 16) +
510                 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
511         dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
512                 ((unsigned long) iobuf[5] << 16) +
513                 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
514         dev_desc->log2blksz = LOG2(dev_desc->blksz);
515 #ifdef CONFIG_LBA48
516         /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
517         dev_desc->lba48 = 0;
518 #endif
519         return;
520 }
521
522 #endif /* CONFIG_ATAPI */
523
524 static void ide_ident(struct blk_desc *dev_desc)
525 {
526         unsigned char c;
527         hd_driveid_t iop;
528
529 #ifdef CONFIG_ATAPI
530         int retries = 0;
531 #endif
532         int device;
533
534         device = dev_desc->devnum;
535         printf("  Device %d: ", device);
536
537         /* Select device
538          */
539         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
540         dev_desc->if_type = IF_TYPE_IDE;
541 #ifdef CONFIG_ATAPI
542
543         retries = 0;
544
545         /* Warning: This will be tricky to read */
546         while (retries <= 1) {
547                 /* check signature */
548                 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
549                     (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
550                     (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
551                     (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
552                         /* ATAPI Signature found */
553                         dev_desc->if_type = IF_TYPE_ATAPI;
554                         /*
555                          * Start Ident Command
556                          */
557                         ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
558                         /*
559                          * Wait for completion - ATAPI devices need more time
560                          * to become ready
561                          */
562                         c = ide_wait(device, ATAPI_TIME_OUT);
563                 } else
564 #endif
565                 {
566                         /*
567                          * Start Ident Command
568                          */
569                         ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
570
571                         /*
572                          * Wait for completion
573                          */
574                         c = ide_wait(device, IDE_TIME_OUT);
575                 }
576
577                 if (((c & ATA_STAT_DRQ) == 0) ||
578                     ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
579 #ifdef CONFIG_ATAPI
580                         {
581                                 /*
582                                  * Need to soft reset the device
583                                  * in case it's an ATAPI...
584                                  */
585                                 debug("Retrying...\n");
586                                 ide_outb(device, ATA_DEV_HD,
587                                          ATA_LBA | ATA_DEVICE(device));
588                                 udelay(100000);
589                                 ide_outb(device, ATA_COMMAND, 0x08);
590                                 udelay(500000); /* 500 ms */
591                         }
592                         /*
593                          * Select device
594                          */
595                         ide_outb(device, ATA_DEV_HD,
596                                  ATA_LBA | ATA_DEVICE(device));
597                         retries++;
598 #else
599                         return;
600 #endif
601                 }
602 #ifdef CONFIG_ATAPI
603                 else
604                         break;
605         }                       /* see above - ugly to read */
606
607         if (retries == 2)       /* Not found */
608                 return;
609 #endif
610
611         ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
612
613         ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
614                   sizeof(dev_desc->revision));
615         ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
616                   sizeof(dev_desc->vendor));
617         ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
618                   sizeof(dev_desc->product));
619
620         if ((iop.config & 0x0080) == 0x0080)
621                 dev_desc->removable = 1;
622         else
623                 dev_desc->removable = 0;
624
625 #ifdef CONFIG_ATAPI
626         if (dev_desc->if_type == IF_TYPE_ATAPI) {
627                 atapi_inquiry(dev_desc);
628                 return;
629         }
630 #endif /* CONFIG_ATAPI */
631
632         iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
633         iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
634         dev_desc->lba =
635                         ((unsigned long)iop.lba_capacity[0]) |
636                         ((unsigned long)iop.lba_capacity[1] << 16);
637
638 #ifdef CONFIG_LBA48
639         if (iop.command_set_2 & 0x0400) {       /* LBA 48 support */
640                 dev_desc->lba48 = 1;
641                 for (int i = 0; i < 4; i++)
642                         iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
643                 dev_desc->lba =
644                         ((unsigned long long)iop.lba48_capacity[0] |
645                         ((unsigned long long)iop.lba48_capacity[1] << 16) |
646                         ((unsigned long long)iop.lba48_capacity[2] << 32) |
647                         ((unsigned long long)iop.lba48_capacity[3] << 48));
648         } else {
649                 dev_desc->lba48 = 0;
650         }
651 #endif /* CONFIG_LBA48 */
652         /* assuming HD */
653         dev_desc->type = DEV_TYPE_HARDDISK;
654         dev_desc->blksz = ATA_BLOCKSIZE;
655         dev_desc->log2blksz = LOG2(dev_desc->blksz);
656         dev_desc->lun = 0;      /* just to fill something in... */
657
658 #if 0                           /* only used to test the powersaving mode,
659                                  * if enabled, the drive goes after 5 sec
660                                  * in standby mode */
661         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
662         c = ide_wait(device, IDE_TIME_OUT);
663         ide_outb(device, ATA_SECT_CNT, 1);
664         ide_outb(device, ATA_LBA_LOW, 0);
665         ide_outb(device, ATA_LBA_MID, 0);
666         ide_outb(device, ATA_LBA_HIGH, 0);
667         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
668         ide_outb(device, ATA_COMMAND, 0xe3);
669         udelay(50);
670         c = ide_wait(device, IDE_TIME_OUT);     /* can't take over 500 ms */
671 #endif
672 }
673
674 __weak void ide_outb(int dev, int port, unsigned char val)
675 {
676         debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
677               dev, port, val, ATA_CURR_BASE(dev) + port);
678
679         outb(val, ATA_CURR_BASE(dev) + port);
680 }
681
682 __weak unsigned char ide_inb(int dev, int port)
683 {
684         uchar val;
685
686         val = inb(ATA_CURR_BASE(dev) + port);
687
688         debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
689               dev, port, ATA_CURR_BASE(dev) + port, val);
690         return val;
691 }
692
693 void ide_init(void)
694 {
695         unsigned char c;
696         int i, bus;
697
698         WATCHDOG_RESET();
699
700         /* ATAPI Drives seems to need a proper IDE Reset */
701         ide_reset();
702
703         /*
704          * Wait for IDE to get ready.
705          * According to spec, this can take up to 31 seconds!
706          */
707         for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
708                 int dev =
709                         bus * (CONFIG_SYS_IDE_MAXDEVICE /
710                                CONFIG_SYS_IDE_MAXBUS);
711
712                 printf("Bus %d: ", bus);
713
714                 ide_bus_ok[bus] = 0;
715
716                 /* Select device
717                  */
718                 udelay(100000); /* 100 ms */
719                 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
720                 udelay(100000); /* 100 ms */
721                 i = 0;
722                 do {
723                         udelay(10000);  /* 10 ms */
724
725                         c = ide_inb(dev, ATA_STATUS);
726                         i++;
727                         if (i > (ATA_RESET_TIME * 100)) {
728                                 puts("** Timeout **\n");
729                                 return;
730                         }
731                         if ((i >= 100) && ((i % 100) == 0))
732                                 putc('.');
733
734                 } while (c & ATA_STAT_BUSY);
735
736                 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
737                         puts("not available  ");
738                         debug("Status = 0x%02X ", c);
739 #ifndef CONFIG_ATAPI            /* ATAPI Devices do not set DRDY */
740                 } else if ((c & ATA_STAT_READY) == 0) {
741                         puts("not available  ");
742                         debug("Status = 0x%02X ", c);
743 #endif
744                 } else {
745                         puts("OK ");
746                         ide_bus_ok[bus] = 1;
747                 }
748                 WATCHDOG_RESET();
749         }
750
751         putc('\n');
752
753         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
754                 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
755                 ide_dev_desc[i].if_type = IF_TYPE_IDE;
756                 ide_dev_desc[i].devnum = i;
757                 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
758                 ide_dev_desc[i].blksz = 0;
759                 ide_dev_desc[i].log2blksz =
760                         LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
761                 ide_dev_desc[i].lba = 0;
762 #ifndef CONFIG_BLK
763                 ide_dev_desc[i].block_read = ide_read;
764                 ide_dev_desc[i].block_write = ide_write;
765 #endif
766                 if (!ide_bus_ok[IDE_BUS(i)])
767                         continue;
768                 ide_ident(&ide_dev_desc[i]);
769                 dev_print(&ide_dev_desc[i]);
770
771 #ifndef CONFIG_BLK
772                 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
773                         /* initialize partition type */
774                         part_init(&ide_dev_desc[i]);
775                 }
776 #endif
777         }
778         WATCHDOG_RESET();
779
780 #ifdef CONFIG_BLK
781         struct udevice *dev;
782
783         uclass_first_device(UCLASS_IDE, &dev);
784 #endif
785 }
786
787 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
788 {
789         uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
790         ushort *dbuf = (ushort *)sect_buf;
791
792         debug("in input swap data base for read is %p\n", (void *)paddr);
793
794         while (words--) {
795                 EIEIO;
796                 *dbuf++ = be16_to_cpu(inw(paddr));
797                 EIEIO;
798                 *dbuf++ = be16_to_cpu(inw(paddr));
799         }
800 }
801
802 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
803 {
804         uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
805         ushort *dbuf;
806
807         dbuf = (ushort *)sect_buf;
808         while (words--) {
809                 EIEIO;
810                 outw(cpu_to_le16(*dbuf++), paddr);
811                 EIEIO;
812                 outw(cpu_to_le16(*dbuf++), paddr);
813         }
814 }
815
816 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
817 {
818         uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
819         ushort *dbuf;
820
821         dbuf = (ushort *)sect_buf;
822
823         debug("in input data base for read is %p\n", (void *)paddr);
824
825         while (words--) {
826                 EIEIO;
827                 *dbuf++ = le16_to_cpu(inw(paddr));
828                 EIEIO;
829                 *dbuf++ = le16_to_cpu(inw(paddr));
830         }
831 }
832
833 #ifdef CONFIG_BLK
834 ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
835                void *buffer)
836 #else
837 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
838                void *buffer)
839 #endif
840 {
841 #ifdef CONFIG_BLK
842         struct blk_desc *block_dev = dev_get_uclass_plat(dev);
843 #endif
844         int device = block_dev->devnum;
845         ulong n = 0;
846         unsigned char c;
847         unsigned char pwrsave = 0;      /* power save */
848
849 #ifdef CONFIG_LBA48
850         unsigned char lba48 = 0;
851
852         if (blknr & 0x0000fffff0000000ULL) {
853                 /* more than 28 bits used, use 48bit mode */
854                 lba48 = 1;
855         }
856 #endif
857         debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
858               device, blknr, blkcnt, (ulong) buffer);
859
860         /* Select device
861          */
862         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
863         c = ide_wait(device, IDE_TIME_OUT);
864
865         if (c & ATA_STAT_BUSY) {
866                 printf("IDE read: device %d not ready\n", device);
867                 goto IDE_READ_E;
868         }
869
870         /* first check if the drive is in Powersaving mode, if yes,
871          * increase the timeout value */
872         ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
873         udelay(50);
874
875         c = ide_wait(device, IDE_TIME_OUT);     /* can't take over 500 ms */
876
877         if (c & ATA_STAT_BUSY) {
878                 printf("IDE read: device %d not ready\n", device);
879                 goto IDE_READ_E;
880         }
881         if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
882                 printf("No Powersaving mode %X\n", c);
883         } else {
884                 c = ide_inb(device, ATA_SECT_CNT);
885                 debug("Powersaving %02X\n", c);
886                 if (c == 0)
887                         pwrsave = 1;
888         }
889
890
891         while (blkcnt-- > 0) {
892                 c = ide_wait(device, IDE_TIME_OUT);
893
894                 if (c & ATA_STAT_BUSY) {
895                         printf("IDE read: device %d not ready\n", device);
896                         break;
897                 }
898 #ifdef CONFIG_LBA48
899                 if (lba48) {
900                         /* write high bits */
901                         ide_outb(device, ATA_SECT_CNT, 0);
902                         ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
903 #ifdef CONFIG_SYS_64BIT_LBA
904                         ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
905                         ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
906 #else
907                         ide_outb(device, ATA_LBA_MID, 0);
908                         ide_outb(device, ATA_LBA_HIGH, 0);
909 #endif
910                 }
911 #endif
912                 ide_outb(device, ATA_SECT_CNT, 1);
913                 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
914                 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
915                 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
916
917 #ifdef CONFIG_LBA48
918                 if (lba48) {
919                         ide_outb(device, ATA_DEV_HD,
920                                  ATA_LBA | ATA_DEVICE(device));
921                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
922
923                 } else
924 #endif
925                 {
926                         ide_outb(device, ATA_DEV_HD, ATA_LBA |
927                                  ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
928                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
929                 }
930
931                 udelay(50);
932
933                 if (pwrsave) {
934                         /* may take up to 4 sec */
935                         c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
936                         pwrsave = 0;
937                 } else {
938                         /* can't take over 500 ms */
939                         c = ide_wait(device, IDE_TIME_OUT);
940                 }
941
942                 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
943                     ATA_STAT_DRQ) {
944                         printf("Error (no IRQ) dev %d blk " LBAF
945                                ": status %#02x\n", device, blknr, c);
946                         break;
947                 }
948
949                 ide_input_data(device, buffer, ATA_SECTORWORDS);
950                 (void) ide_inb(device, ATA_STATUS);     /* clear IRQ */
951
952                 ++n;
953                 ++blknr;
954                 buffer += ATA_BLOCKSIZE;
955         }
956 IDE_READ_E:
957         return n;
958 }
959
960 #ifdef CONFIG_BLK
961 ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
962                 const void *buffer)
963 #else
964 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
965                 const void *buffer)
966 #endif
967 {
968 #ifdef CONFIG_BLK
969         struct blk_desc *block_dev = dev_get_uclass_plat(dev);
970 #endif
971         int device = block_dev->devnum;
972         ulong n = 0;
973         unsigned char c;
974
975 #ifdef CONFIG_LBA48
976         unsigned char lba48 = 0;
977
978         if (blknr & 0x0000fffff0000000ULL) {
979                 /* more than 28 bits used, use 48bit mode */
980                 lba48 = 1;
981         }
982 #endif
983
984         /* Select device
985          */
986         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
987
988         while (blkcnt-- > 0) {
989                 c = ide_wait(device, IDE_TIME_OUT);
990
991                 if (c & ATA_STAT_BUSY) {
992                         printf("IDE read: device %d not ready\n", device);
993                         goto WR_OUT;
994                 }
995 #ifdef CONFIG_LBA48
996                 if (lba48) {
997                         /* write high bits */
998                         ide_outb(device, ATA_SECT_CNT, 0);
999                         ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1000 #ifdef CONFIG_SYS_64BIT_LBA
1001                         ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1002                         ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1003 #else
1004                         ide_outb(device, ATA_LBA_MID, 0);
1005                         ide_outb(device, ATA_LBA_HIGH, 0);
1006 #endif
1007                 }
1008 #endif
1009                 ide_outb(device, ATA_SECT_CNT, 1);
1010                 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1011                 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1012                 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1013
1014 #ifdef CONFIG_LBA48
1015                 if (lba48) {
1016                         ide_outb(device, ATA_DEV_HD,
1017                                  ATA_LBA | ATA_DEVICE(device));
1018                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
1019
1020                 } else
1021 #endif
1022                 {
1023                         ide_outb(device, ATA_DEV_HD, ATA_LBA |
1024                                  ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1025                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
1026                 }
1027
1028                 udelay(50);
1029
1030                 /* can't take over 500 ms */
1031                 c = ide_wait(device, IDE_TIME_OUT);
1032
1033                 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1034                     ATA_STAT_DRQ) {
1035                         printf("Error (no IRQ) dev %d blk " LBAF
1036                                ": status %#02x\n", device, blknr, c);
1037                         goto WR_OUT;
1038                 }
1039
1040                 ide_output_data(device, buffer, ATA_SECTORWORDS);
1041                 c = ide_inb(device, ATA_STATUS);        /* clear IRQ */
1042                 ++n;
1043                 ++blknr;
1044                 buffer += ATA_BLOCKSIZE;
1045         }
1046 WR_OUT:
1047         return n;
1048 }
1049
1050 #if defined(CONFIG_OF_IDE_FIXUP)
1051 int ide_device_present(int dev)
1052 {
1053         if (dev >= CONFIG_SYS_IDE_MAXBUS)
1054                 return 0;
1055         return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1056 }
1057 #endif
1058
1059 #ifdef CONFIG_BLK
1060 static int ide_blk_probe(struct udevice *udev)
1061 {
1062         struct blk_desc *desc = dev_get_uclass_plat(udev);
1063
1064         /* fill in device vendor/product/rev strings */
1065         strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1066                 BLK_VEN_SIZE);
1067         desc->vendor[BLK_VEN_SIZE] = '\0';
1068         strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1069                 BLK_PRD_SIZE);
1070         desc->product[BLK_PRD_SIZE] = '\0';
1071         strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1072                 BLK_REV_SIZE);
1073         desc->revision[BLK_REV_SIZE] = '\0';
1074
1075         return 0;
1076 }
1077
1078 static const struct blk_ops ide_blk_ops = {
1079         .read   = ide_read,
1080         .write  = ide_write,
1081 };
1082
1083 U_BOOT_DRIVER(ide_blk) = {
1084         .name           = "ide_blk",
1085         .id             = UCLASS_BLK,
1086         .ops            = &ide_blk_ops,
1087         .probe          = ide_blk_probe,
1088 };
1089
1090 static int ide_probe(struct udevice *udev)
1091 {
1092         struct udevice *blk_dev;
1093         char name[20];
1094         int blksz;
1095         lbaint_t size;
1096         int i;
1097         int ret;
1098
1099         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1100                 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1101                         sprintf(name, "blk#%d", i);
1102
1103                         blksz = ide_dev_desc[i].blksz;
1104                         size = blksz * ide_dev_desc[i].lba;
1105
1106                         /*
1107                          * With CDROM, if there is no CD inserted, blksz will
1108                          * be zero, don't bother to create IDE block device.
1109                          */
1110                         if (!blksz)
1111                                 continue;
1112                         ret = blk_create_devicef(udev, "ide_blk", name,
1113                                                  IF_TYPE_IDE, i,
1114                                                  blksz, size, &blk_dev);
1115                         if (ret)
1116                                 return ret;
1117
1118                         ret = blk_probe_or_unbind(blk_dev);
1119                         if (ret)
1120                                 return ret;
1121                 }
1122         }
1123
1124         return 0;
1125 }
1126
1127 U_BOOT_DRIVER(ide) = {
1128         .name           = "ide",
1129         .id             = UCLASS_IDE,
1130         .probe          = ide_probe,
1131 };
1132
1133 struct pci_device_id ide_supported[] = {
1134         { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1135         { }
1136 };
1137
1138 U_BOOT_PCI_DEVICE(ide, ide_supported);
1139
1140 UCLASS_DRIVER(ide) = {
1141         .name           = "ide",
1142         .id             = UCLASS_IDE,
1143 };
1144 #else
1145 U_BOOT_LEGACY_BLK(ide) = {
1146         .if_typename    = "ide",
1147         .if_type        = IF_TYPE_IDE,
1148         .max_devs       = CONFIG_SYS_IDE_MAXDEVICE,
1149         .desc           = ide_dev_desc,
1150 };
1151 #endif