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