6c5227a5c0e2e88f29befcdb9fa76364ffa527d3
[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 static 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 static unsigned char atapi_issue_autoreq(int device, unsigned char *ccb,
347                                          int ccblen,
348                                          unsigned char *buffer, int buflen)
349 {
350         unsigned char sense_data[18], sense_ccb[12];
351         unsigned char res, key, asc, ascq;
352         int notready, unitattn;
353
354         unitattn = ATAPI_UNIT_ATTN;
355         notready = ATAPI_DRIVE_NOT_READY;
356
357 retry:
358         res = atapi_issue(device, ccb, ccblen, buffer, buflen);
359         if (res == 0)
360                 return 0;       /* Ok */
361
362         if (res == 0xFF)
363                 return 0xFF;    /* error */
364
365         debug("(auto_req)atapi_issue returned sense key %X\n", res);
366
367         memset(sense_ccb, 0, sizeof(sense_ccb));
368         memset(sense_data, 0, sizeof(sense_data));
369         sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
370         sense_ccb[4] = 18;      /* allocation Length */
371
372         res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
373         key = (sense_data[2] & 0xF);
374         asc = (sense_data[12]);
375         ascq = (sense_data[13]);
376
377         debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
378         debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
379               sense_data[0], key, asc, ascq);
380
381         if ((key == 0))
382                 return 0;       /* ok device ready */
383
384         if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
385                 if (unitattn-- > 0) {
386                         mdelay(200);
387                         goto retry;
388                 }
389                 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
390                 goto error;
391         }
392         if ((asc == 0x4) && (ascq == 0x1)) {
393                 /* not ready, but will be ready soon */
394                 if (notready-- > 0) {
395                         mdelay(200);
396                         goto retry;
397                 }
398                 printf("Drive not ready, tried %d times\n",
399                        ATAPI_DRIVE_NOT_READY);
400                 goto error;
401         }
402         if (asc == 0x3a) {
403                 debug("Media not present\n");
404                 goto error;
405         }
406
407         printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
408                ascq);
409 error:
410         debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
411         return 0xFF;
412 }
413
414 /*
415  * atapi_read:
416  * we transfer only one block per command, since the multiple DRQ per
417  * command is not yet implemented
418  */
419 #define ATAPI_READ_MAX_BYTES    2048    /* we read max 2kbytes */
420 #define ATAPI_READ_BLOCK_SIZE   2048    /* assuming CD part */
421 #define ATAPI_READ_MAX_BLOCK    (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
422
423 static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
424                         void *buffer)
425 {
426         struct blk_desc *block_dev = dev_get_uclass_plat(dev);
427         int device = block_dev->devnum;
428         ulong n = 0;
429         unsigned char ccb[12];  /* Command descriptor block */
430         ulong cnt;
431
432         debug("atapi_read dev %d start " LBAF " blocks " LBAF
433               " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
434
435         do {
436                 if (blkcnt > ATAPI_READ_MAX_BLOCK)
437                         cnt = ATAPI_READ_MAX_BLOCK;
438                 else
439                         cnt = blkcnt;
440
441                 ccb[0] = ATAPI_CMD_READ_12;
442                 ccb[1] = 0;     /* reserved */
443                 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF;  /* MSB Block */
444                 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF;  /*  */
445                 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
446                 ccb[5] = (unsigned char) blknr & 0xFF;  /* LSB Block */
447                 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
448                 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
449                 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
450                 ccb[9] = (unsigned char) cnt & 0xFF;    /* LSB Block */
451                 ccb[10] = 0;    /* reserved */
452                 ccb[11] = 0;    /* reserved */
453
454                 if (atapi_issue_autoreq(device, ccb, 12,
455                                         (unsigned char *)buffer,
456                                         cnt * ATAPI_READ_BLOCK_SIZE)
457                     == 0xFF) {
458                         return n;
459                 }
460                 n += cnt;
461                 blkcnt -= cnt;
462                 blknr += cnt;
463                 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
464         } while (blkcnt > 0);
465         return n;
466 }
467
468 static void atapi_inquiry(struct blk_desc *dev_desc)
469 {
470         unsigned char ccb[12];  /* Command descriptor block */
471         unsigned char iobuf[64];        /* temp buf */
472         unsigned char c;
473         int device;
474
475         device = dev_desc->devnum;
476         dev_desc->type = DEV_TYPE_UNKNOWN;      /* not yet valid */
477
478         memset(ccb, 0, sizeof(ccb));
479         memset(iobuf, 0, sizeof(iobuf));
480
481         ccb[0] = ATAPI_CMD_INQUIRY;
482         ccb[4] = 40;            /* allocation Legnth */
483         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
484
485         debug("ATAPI_CMD_INQUIRY returned %x\n", c);
486         if (c != 0)
487                 return;
488
489         /* copy device ident strings */
490         ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
491         ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
492         ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
493
494         dev_desc->lun = 0;
495         dev_desc->lba = 0;
496         dev_desc->blksz = 0;
497         dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
498         dev_desc->type = iobuf[0] & 0x1f;
499
500         if ((iobuf[1] & 0x80) == 0x80)
501                 dev_desc->removable = 1;
502         else
503                 dev_desc->removable = 0;
504
505         memset(ccb, 0, sizeof(ccb));
506         memset(iobuf, 0, sizeof(iobuf));
507         ccb[0] = ATAPI_CMD_START_STOP;
508         ccb[4] = 0x03;          /* start */
509
510         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
511
512         debug("ATAPI_CMD_START_STOP returned %x\n", c);
513         if (c != 0)
514                 return;
515
516         memset(ccb, 0, sizeof(ccb));
517         memset(iobuf, 0, sizeof(iobuf));
518         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
519
520         debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
521         if (c != 0)
522                 return;
523
524         memset(ccb, 0, sizeof(ccb));
525         memset(iobuf, 0, sizeof(iobuf));
526         ccb[0] = ATAPI_CMD_READ_CAP;
527         c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
528         debug("ATAPI_CMD_READ_CAP returned %x\n", c);
529         if (c != 0)
530                 return;
531
532         debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
533               iobuf[0], iobuf[1], iobuf[2], iobuf[3],
534               iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
535
536         dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
537                 ((unsigned long) iobuf[1] << 16) +
538                 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
539         dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
540                 ((unsigned long) iobuf[5] << 16) +
541                 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
542         dev_desc->log2blksz = LOG2(dev_desc->blksz);
543 #ifdef CONFIG_LBA48
544         /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
545         dev_desc->lba48 = 0;
546 #endif
547         return;
548 }
549
550 static void ide_ident(struct blk_desc *dev_desc)
551 {
552         unsigned char c;
553         hd_driveid_t iop;
554         bool is_atapi = false;
555         int tries = 1;
556         int device;
557
558         device = dev_desc->devnum;
559         printf("  Device %d: ", device);
560
561         /* Select device
562          */
563         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
564         dev_desc->uclass_id = UCLASS_IDE;
565         if (IS_ENABLED(CONFIG_ATAPI))
566                 tries = 2;
567
568         while (tries) {
569                 /* check signature */
570                 if (IS_ENABLED(CONFIG_ATAPI) &&
571                     ide_inb(device, ATA_SECT_CNT) == 0x01 &&
572                     ide_inb(device, ATA_SECT_NUM) == 0x01 &&
573                     ide_inb(device, ATA_CYL_LOW) == 0x14 &&
574                     ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
575                         /* ATAPI Signature found */
576                         is_atapi = true;
577                         /*
578                          * Start Ident Command
579                          */
580                         ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
581                         /*
582                          * Wait for completion - ATAPI devices need more time
583                          * to become ready
584                          */
585                         c = ide_wait(device, ATAPI_TIME_OUT);
586                 } else {
587                         /*
588                          * Start Ident Command
589                          */
590                         ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
591
592                         /*
593                          * Wait for completion
594                          */
595                         c = ide_wait(device, IDE_TIME_OUT);
596                 }
597
598                 if ((c & ATA_STAT_DRQ) &&
599                     !(c & (ATA_STAT_FAULT | ATA_STAT_ERR))) {
600                         break;
601                 } else if (IS_ENABLED(CONFIG_ATAPI)) {
602                         /*
603                          * Need to soft reset the device
604                          * in case it's an ATAPI...
605                          */
606                         debug("Retrying...\n");
607                         ide_outb(device, ATA_DEV_HD,
608                                  ATA_LBA | ATA_DEVICE(device));
609                         mdelay(100);
610                         ide_outb(device, ATA_COMMAND, 0x08);
611                         mdelay(500);
612                         /* Select device */
613                         ide_outb(device, ATA_DEV_HD,
614                                  ATA_LBA | ATA_DEVICE(device));
615                 }
616                 tries--;
617         }
618
619         if (!tries)     /* Not found */
620                 return;
621
622         ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
623
624         ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
625                   sizeof(dev_desc->revision));
626         ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
627                   sizeof(dev_desc->vendor));
628         ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
629                   sizeof(dev_desc->product));
630
631         if ((iop.config & 0x0080) == 0x0080)
632                 dev_desc->removable = 1;
633         else
634                 dev_desc->removable = 0;
635
636         if (IS_ENABLED(CONFIG_ATAPI) && is_atapi) {
637                 dev_desc->atapi = true;
638                 atapi_inquiry(dev_desc);
639                 return;
640         }
641
642         iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
643         iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
644         dev_desc->lba =
645                         ((unsigned long)iop.lba_capacity[0]) |
646                         ((unsigned long)iop.lba_capacity[1] << 16);
647
648 #ifdef CONFIG_LBA48
649         if (iop.command_set_2 & 0x0400) {       /* LBA 48 support */
650                 dev_desc->lba48 = 1;
651                 for (int i = 0; i < 4; i++)
652                         iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
653                 dev_desc->lba =
654                         ((unsigned long long)iop.lba48_capacity[0] |
655                         ((unsigned long long)iop.lba48_capacity[1] << 16) |
656                         ((unsigned long long)iop.lba48_capacity[2] << 32) |
657                         ((unsigned long long)iop.lba48_capacity[3] << 48));
658         } else {
659                 dev_desc->lba48 = 0;
660         }
661 #endif /* CONFIG_LBA48 */
662         /* assuming HD */
663         dev_desc->type = DEV_TYPE_HARDDISK;
664         dev_desc->blksz = ATA_BLOCKSIZE;
665         dev_desc->log2blksz = LOG2(dev_desc->blksz);
666         dev_desc->lun = 0;      /* just to fill something in... */
667
668 #if 0                           /* only used to test the powersaving mode,
669                                  * if enabled, the drive goes after 5 sec
670                                  * in standby mode */
671         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
672         c = ide_wait(device, IDE_TIME_OUT);
673         ide_outb(device, ATA_SECT_CNT, 1);
674         ide_outb(device, ATA_LBA_LOW, 0);
675         ide_outb(device, ATA_LBA_MID, 0);
676         ide_outb(device, ATA_LBA_HIGH, 0);
677         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
678         ide_outb(device, ATA_COMMAND, 0xe3);
679         udelay(50);
680         c = ide_wait(device, IDE_TIME_OUT);     /* can't take over 500 ms */
681 #endif
682 }
683
684 static void ide_init(void)
685 {
686         unsigned char c;
687         int i, bus;
688
689         schedule();
690
691         /* ATAPI Drives seems to need a proper IDE Reset */
692         ide_reset();
693
694         /*
695          * Wait for IDE to get ready.
696          * According to spec, this can take up to 31 seconds!
697          */
698         for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
699                 int dev =
700                         bus * (CONFIG_SYS_IDE_MAXDEVICE /
701                                CONFIG_SYS_IDE_MAXBUS);
702
703                 printf("Bus %d: ", bus);
704
705                 ide_bus_ok[bus] = 0;
706
707                 /* Select device */
708                 mdelay(100);
709                 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
710                 mdelay(100);
711                 i = 0;
712                 do {
713                         mdelay(10);
714
715                         c = ide_inb(dev, ATA_STATUS);
716                         i++;
717                         if (i > (ATA_RESET_TIME * 100)) {
718                                 puts("** Timeout **\n");
719                                 return;
720                         }
721                         if ((i >= 100) && ((i % 100) == 0))
722                                 putc('.');
723
724                 } while (c & ATA_STAT_BUSY);
725
726                 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
727                         puts("not available  ");
728                         debug("Status = 0x%02X ", c);
729                 } else if (IS_ENABLED(CONFIG_ATAPI) && !(c & ATA_STAT_READY)) {
730                         /* ATAPI Devices do not set DRDY */
731                         puts("not available  ");
732                         debug("Status = 0x%02X ", c);
733                 } else {
734                         puts("OK ");
735                         ide_bus_ok[bus] = 1;
736                 }
737                 schedule();
738         }
739
740         putc('\n');
741
742         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
743                 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
744                 ide_dev_desc[i].uclass_id = UCLASS_IDE;
745                 ide_dev_desc[i].devnum = i;
746                 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
747                 ide_dev_desc[i].blksz = 0;
748                 ide_dev_desc[i].log2blksz =
749                         LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
750                 ide_dev_desc[i].lba = 0;
751                 if (!ide_bus_ok[IDE_BUS(i)])
752                         continue;
753                 ide_ident(&ide_dev_desc[i]);
754                 dev_print(&ide_dev_desc[i]);
755         }
756         schedule();
757 }
758
759 static void ide_output_data(int dev, const ulong *sect_buf, int words)
760 {
761         uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
762         ushort *dbuf;
763
764         dbuf = (ushort *)sect_buf;
765         while (words--) {
766                 EIEIO;
767                 outw(cpu_to_le16(*dbuf++), paddr);
768                 EIEIO;
769                 outw(cpu_to_le16(*dbuf++), paddr);
770         }
771 }
772
773 static void ide_input_data(int dev, ulong *sect_buf, int words)
774 {
775         uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
776         ushort *dbuf;
777
778         dbuf = (ushort *)sect_buf;
779
780         debug("in input data base for read is %p\n", (void *)paddr);
781
782         while (words--) {
783                 EIEIO;
784                 *dbuf++ = le16_to_cpu(inw(paddr));
785                 EIEIO;
786                 *dbuf++ = le16_to_cpu(inw(paddr));
787         }
788 }
789
790 static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
791                       void *buffer)
792 {
793         struct blk_desc *block_dev = dev_get_uclass_plat(dev);
794         int device = block_dev->devnum;
795         ulong n = 0;
796         unsigned char c;
797         unsigned char pwrsave = 0;      /* power save */
798
799 #ifdef CONFIG_LBA48
800         unsigned char lba48 = 0;
801
802         if (blknr & 0x0000fffff0000000ULL) {
803                 /* more than 28 bits used, use 48bit mode */
804                 lba48 = 1;
805         }
806 #endif
807         debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
808               device, blknr, blkcnt, (ulong) buffer);
809
810         /* Select device
811          */
812         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
813         c = ide_wait(device, IDE_TIME_OUT);
814
815         if (c & ATA_STAT_BUSY) {
816                 printf("IDE read: device %d not ready\n", device);
817                 goto IDE_READ_E;
818         }
819
820         /* first check if the drive is in Powersaving mode, if yes,
821          * increase the timeout value */
822         ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
823         udelay(50);
824
825         c = ide_wait(device, IDE_TIME_OUT);     /* can't take over 500 ms */
826
827         if (c & ATA_STAT_BUSY) {
828                 printf("IDE read: device %d not ready\n", device);
829                 goto IDE_READ_E;
830         }
831         if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
832                 printf("No Powersaving mode %X\n", c);
833         } else {
834                 c = ide_inb(device, ATA_SECT_CNT);
835                 debug("Powersaving %02X\n", c);
836                 if (c == 0)
837                         pwrsave = 1;
838         }
839
840
841         while (blkcnt-- > 0) {
842                 c = ide_wait(device, IDE_TIME_OUT);
843
844                 if (c & ATA_STAT_BUSY) {
845                         printf("IDE read: device %d not ready\n", device);
846                         break;
847                 }
848 #ifdef CONFIG_LBA48
849                 if (lba48) {
850                         /* write high bits */
851                         ide_outb(device, ATA_SECT_CNT, 0);
852                         ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
853 #ifdef CONFIG_SYS_64BIT_LBA
854                         ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
855                         ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
856 #else
857                         ide_outb(device, ATA_LBA_MID, 0);
858                         ide_outb(device, ATA_LBA_HIGH, 0);
859 #endif
860                 }
861 #endif
862                 ide_outb(device, ATA_SECT_CNT, 1);
863                 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
864                 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
865                 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
866
867 #ifdef CONFIG_LBA48
868                 if (lba48) {
869                         ide_outb(device, ATA_DEV_HD,
870                                  ATA_LBA | ATA_DEVICE(device));
871                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
872
873                 } else
874 #endif
875                 {
876                         ide_outb(device, ATA_DEV_HD, ATA_LBA |
877                                  ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
878                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
879                 }
880
881                 udelay(50);
882
883                 if (pwrsave) {
884                         /* may take up to 4 sec */
885                         c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
886                         pwrsave = 0;
887                 } else {
888                         /* can't take over 500 ms */
889                         c = ide_wait(device, IDE_TIME_OUT);
890                 }
891
892                 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
893                     ATA_STAT_DRQ) {
894                         printf("Error (no IRQ) dev %d blk " LBAF
895                                ": status %#02x\n", device, blknr, c);
896                         break;
897                 }
898
899                 ide_input_data(device, buffer, ATA_SECTORWORDS);
900                 (void) ide_inb(device, ATA_STATUS);     /* clear IRQ */
901
902                 ++n;
903                 ++blknr;
904                 buffer += ATA_BLOCKSIZE;
905         }
906 IDE_READ_E:
907         return n;
908 }
909
910 static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
911                        const void *buffer)
912 {
913         struct blk_desc *block_dev = dev_get_uclass_plat(dev);
914         int device = block_dev->devnum;
915         ulong n = 0;
916         unsigned char c;
917
918 #ifdef CONFIG_LBA48
919         unsigned char lba48 = 0;
920
921         if (blknr & 0x0000fffff0000000ULL) {
922                 /* more than 28 bits used, use 48bit mode */
923                 lba48 = 1;
924         }
925 #endif
926
927         /* Select device
928          */
929         ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
930
931         while (blkcnt-- > 0) {
932                 c = ide_wait(device, IDE_TIME_OUT);
933
934                 if (c & ATA_STAT_BUSY) {
935                         printf("IDE read: device %d not ready\n", device);
936                         goto WR_OUT;
937                 }
938 #ifdef CONFIG_LBA48
939                 if (lba48) {
940                         /* write high bits */
941                         ide_outb(device, ATA_SECT_CNT, 0);
942                         ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
943 #ifdef CONFIG_SYS_64BIT_LBA
944                         ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
945                         ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
946 #else
947                         ide_outb(device, ATA_LBA_MID, 0);
948                         ide_outb(device, ATA_LBA_HIGH, 0);
949 #endif
950                 }
951 #endif
952                 ide_outb(device, ATA_SECT_CNT, 1);
953                 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
954                 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
955                 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
956
957 #ifdef CONFIG_LBA48
958                 if (lba48) {
959                         ide_outb(device, ATA_DEV_HD,
960                                  ATA_LBA | ATA_DEVICE(device));
961                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
962
963                 } else
964 #endif
965                 {
966                         ide_outb(device, ATA_DEV_HD, ATA_LBA |
967                                  ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
968                         ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
969                 }
970
971                 udelay(50);
972
973                 /* can't take over 500 ms */
974                 c = ide_wait(device, IDE_TIME_OUT);
975
976                 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
977                     ATA_STAT_DRQ) {
978                         printf("Error (no IRQ) dev %d blk " LBAF
979                                ": status %#02x\n", device, blknr, c);
980                         goto WR_OUT;
981                 }
982
983                 ide_output_data(device, buffer, ATA_SECTORWORDS);
984                 c = ide_inb(device, ATA_STATUS);        /* clear IRQ */
985                 ++n;
986                 ++blknr;
987                 buffer += ATA_BLOCKSIZE;
988         }
989 WR_OUT:
990         return n;
991 }
992
993 ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
994                         void *buffer)
995 {
996         struct blk_desc *desc = dev_get_uclass_plat(dev);
997
998         if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
999                 return atapi_read(dev, blknr, blkcnt, buffer);
1000
1001         return ide_read(dev, blknr, blkcnt, buffer);
1002 }
1003
1004 static int ide_blk_probe(struct udevice *udev)
1005 {
1006         struct blk_desc *desc = dev_get_uclass_plat(udev);
1007
1008         /* fill in device vendor/product/rev strings */
1009         strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1010                 BLK_VEN_SIZE);
1011         desc->vendor[BLK_VEN_SIZE] = '\0';
1012         strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1013                 BLK_PRD_SIZE);
1014         desc->product[BLK_PRD_SIZE] = '\0';
1015         strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1016                 BLK_REV_SIZE);
1017         desc->revision[BLK_REV_SIZE] = '\0';
1018
1019         return 0;
1020 }
1021
1022 static const struct blk_ops ide_blk_ops = {
1023         .read   = ide_or_atapi_read,
1024         .write  = ide_write,
1025 };
1026
1027 U_BOOT_DRIVER(ide_blk) = {
1028         .name           = "ide_blk",
1029         .id             = UCLASS_BLK,
1030         .ops            = &ide_blk_ops,
1031         .probe          = ide_blk_probe,
1032 };
1033
1034 static int ide_bootdev_bind(struct udevice *dev)
1035 {
1036         struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
1037
1038         ucp->prio = BOOTDEVP_5_SCAN_SLOW;
1039
1040         return 0;
1041 }
1042
1043 static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
1044 {
1045         struct udevice *dev;
1046
1047         uclass_first_device(UCLASS_IDE, &dev);
1048
1049         return 0;
1050 }
1051
1052 struct bootdev_ops ide_bootdev_ops = {
1053 };
1054
1055 static const struct udevice_id ide_bootdev_ids[] = {
1056         { .compatible = "u-boot,bootdev-ide" },
1057         { }
1058 };
1059
1060 U_BOOT_DRIVER(ide_bootdev) = {
1061         .name           = "ide_bootdev",
1062         .id             = UCLASS_BOOTDEV,
1063         .ops            = &ide_bootdev_ops,
1064         .bind           = ide_bootdev_bind,
1065         .of_match       = ide_bootdev_ids,
1066 };
1067
1068 BOOTDEV_HUNTER(ide_bootdev_hunter) = {
1069         .prio           = BOOTDEVP_5_SCAN_SLOW,
1070         .uclass         = UCLASS_IDE,
1071         .hunt           = ide_bootdev_hunt,
1072         .drv            = DM_DRIVER_REF(ide_bootdev),
1073 };
1074
1075 static int ide_probe(struct udevice *udev)
1076 {
1077         struct udevice *blk_dev;
1078         char name[20];
1079         int blksz;
1080         lbaint_t size;
1081         int i;
1082         int ret;
1083
1084         ide_init();
1085
1086         for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1087                 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1088                         sprintf(name, "blk#%d", i);
1089
1090                         blksz = ide_dev_desc[i].blksz;
1091                         size = blksz * ide_dev_desc[i].lba;
1092
1093                         /*
1094                          * With CDROM, if there is no CD inserted, blksz will
1095                          * be zero, don't bother to create IDE block device.
1096                          */
1097                         if (!blksz)
1098                                 continue;
1099                         ret = blk_create_devicef(udev, "ide_blk", name,
1100                                                  UCLASS_IDE, i,
1101                                                  blksz, size, &blk_dev);
1102                         if (ret)
1103                                 return ret;
1104
1105                         ret = blk_probe_or_unbind(blk_dev);
1106                         if (ret)
1107                                 return ret;
1108
1109                         ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1110                         if (ret)
1111                                 return log_msg_ret("bootdev", ret);
1112                 }
1113         }
1114
1115         return 0;
1116 }
1117
1118 U_BOOT_DRIVER(ide) = {
1119         .name           = "ide",
1120         .id             = UCLASS_IDE,
1121         .probe          = ide_probe,
1122 };
1123
1124 struct pci_device_id ide_supported[] = {
1125         { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1126         { }
1127 };
1128
1129 U_BOOT_PCI_DEVICE(ide, ide_supported);
1130
1131 UCLASS_DRIVER(ide) = {
1132         .name           = "ide",
1133         .id             = UCLASS_IDE,
1134 };