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