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