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