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 schedule();
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#ifdef CONFIG_ATAPI
529 bool is_atapi = false;
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->uclass_id = UCLASS_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 is_atapi = true;
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 (is_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 schedule();
699
700
701 ide_reset();
702
703
704
705
706
707 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
708 int dev =
709 bus * (CONFIG_SYS_IDE_MAXDEVICE /
710 CONFIG_SYS_IDE_MAXBUS);
711
712 printf("Bus %d: ", bus);
713
714 ide_bus_ok[bus] = 0;
715
716
717
718 udelay(100000);
719 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
720 udelay(100000);
721 i = 0;
722 do {
723 udelay(10000);
724
725 c = ide_inb(dev, ATA_STATUS);
726 i++;
727 if (i > (ATA_RESET_TIME * 100)) {
728 puts("** Timeout **\n");
729 return;
730 }
731 if ((i >= 100) && ((i % 100) == 0))
732 putc('.');
733
734 } while (c & ATA_STAT_BUSY);
735
736 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
737 puts("not available ");
738 debug("Status = 0x%02X ", c);
739#ifndef CONFIG_ATAPI
740 } else if ((c & ATA_STAT_READY) == 0) {
741 puts("not available ");
742 debug("Status = 0x%02X ", c);
743#endif
744 } else {
745 puts("OK ");
746 ide_bus_ok[bus] = 1;
747 }
748 schedule();
749 }
750
751 putc('\n');
752
753 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
754 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
755 ide_dev_desc[i].uclass_id = UCLASS_IDE;
756 ide_dev_desc[i].devnum = i;
757 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
758 ide_dev_desc[i].blksz = 0;
759 ide_dev_desc[i].log2blksz =
760 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
761 ide_dev_desc[i].lba = 0;
762#ifndef CONFIG_BLK
763 ide_dev_desc[i].block_read = ide_read;
764 ide_dev_desc[i].block_write = ide_write;
765#endif
766 if (!ide_bus_ok[IDE_BUS(i)])
767 continue;
768 ide_ident(&ide_dev_desc[i]);
769 dev_print(&ide_dev_desc[i]);
770
771#ifndef CONFIG_BLK
772 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
773
774 part_init(&ide_dev_desc[i]);
775 }
776#endif
777 }
778 schedule();
779
780#ifdef CONFIG_BLK
781 struct udevice *dev;
782
783 uclass_first_device(UCLASS_IDE, &dev);
784#endif
785}
786
787__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
788{
789 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
790 ushort *dbuf = (ushort *)sect_buf;
791
792 debug("in input swap data base for read is %p\n", (void *)paddr);
793
794 while (words--) {
795 EIEIO;
796 *dbuf++ = be16_to_cpu(inw(paddr));
797 EIEIO;
798 *dbuf++ = be16_to_cpu(inw(paddr));
799 }
800}
801
802__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
803{
804 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
805 ushort *dbuf;
806
807 dbuf = (ushort *)sect_buf;
808 while (words--) {
809 EIEIO;
810 outw(cpu_to_le16(*dbuf++), paddr);
811 EIEIO;
812 outw(cpu_to_le16(*dbuf++), paddr);
813 }
814}
815
816__weak void ide_input_data(int dev, ulong *sect_buf, int words)
817{
818 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
819 ushort *dbuf;
820
821 dbuf = (ushort *)sect_buf;
822
823 debug("in input data base for read is %p\n", (void *)paddr);
824
825 while (words--) {
826 EIEIO;
827 *dbuf++ = le16_to_cpu(inw(paddr));
828 EIEIO;
829 *dbuf++ = le16_to_cpu(inw(paddr));
830 }
831}
832
833#ifdef CONFIG_BLK
834ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
835 void *buffer)
836#else
837ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
838 void *buffer)
839#endif
840{
841#ifdef CONFIG_BLK
842 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
843#endif
844 int device = block_dev->devnum;
845 ulong n = 0;
846 unsigned char c;
847 unsigned char pwrsave = 0;
848
849#ifdef CONFIG_LBA48
850 unsigned char lba48 = 0;
851
852 if (blknr & 0x0000fffff0000000ULL) {
853
854 lba48 = 1;
855 }
856#endif
857 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
858 device, blknr, blkcnt, (ulong) buffer);
859
860
861
862 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
863 c = ide_wait(device, IDE_TIME_OUT);
864
865 if (c & ATA_STAT_BUSY) {
866 printf("IDE read: device %d not ready\n", device);
867 goto IDE_READ_E;
868 }
869
870
871
872 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
873 udelay(50);
874
875 c = ide_wait(device, IDE_TIME_OUT);
876
877 if (c & ATA_STAT_BUSY) {
878 printf("IDE read: device %d not ready\n", device);
879 goto IDE_READ_E;
880 }
881 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
882 printf("No Powersaving mode %X\n", c);
883 } else {
884 c = ide_inb(device, ATA_SECT_CNT);
885 debug("Powersaving %02X\n", c);
886 if (c == 0)
887 pwrsave = 1;
888 }
889
890
891 while (blkcnt-- > 0) {
892 c = ide_wait(device, IDE_TIME_OUT);
893
894 if (c & ATA_STAT_BUSY) {
895 printf("IDE read: device %d not ready\n", device);
896 break;
897 }
898#ifdef CONFIG_LBA48
899 if (lba48) {
900
901 ide_outb(device, ATA_SECT_CNT, 0);
902 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
903#ifdef CONFIG_SYS_64BIT_LBA
904 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
905 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
906#else
907 ide_outb(device, ATA_LBA_MID, 0);
908 ide_outb(device, ATA_LBA_HIGH, 0);
909#endif
910 }
911#endif
912 ide_outb(device, ATA_SECT_CNT, 1);
913 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
914 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
915 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
916
917#ifdef CONFIG_LBA48
918 if (lba48) {
919 ide_outb(device, ATA_DEV_HD,
920 ATA_LBA | ATA_DEVICE(device));
921 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
922
923 } else
924#endif
925 {
926 ide_outb(device, ATA_DEV_HD, ATA_LBA |
927 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
928 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
929 }
930
931 udelay(50);
932
933 if (pwrsave) {
934
935 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
936 pwrsave = 0;
937 } else {
938
939 c = ide_wait(device, IDE_TIME_OUT);
940 }
941
942 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
943 ATA_STAT_DRQ) {
944 printf("Error (no IRQ) dev %d blk " LBAF
945 ": status %#02x\n", device, blknr, c);
946 break;
947 }
948
949 ide_input_data(device, buffer, ATA_SECTORWORDS);
950 (void) ide_inb(device, ATA_STATUS);
951
952 ++n;
953 ++blknr;
954 buffer += ATA_BLOCKSIZE;
955 }
956IDE_READ_E:
957 return n;
958}
959
960#ifdef CONFIG_BLK
961ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
962 const void *buffer)
963#else
964ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
965 const void *buffer)
966#endif
967{
968#ifdef CONFIG_BLK
969 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
970#endif
971 int device = block_dev->devnum;
972 ulong n = 0;
973 unsigned char c;
974
975#ifdef CONFIG_LBA48
976 unsigned char lba48 = 0;
977
978 if (blknr & 0x0000fffff0000000ULL) {
979
980 lba48 = 1;
981 }
982#endif
983
984
985
986 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
987
988 while (blkcnt-- > 0) {
989 c = ide_wait(device, IDE_TIME_OUT);
990
991 if (c & ATA_STAT_BUSY) {
992 printf("IDE read: device %d not ready\n", device);
993 goto WR_OUT;
994 }
995#ifdef CONFIG_LBA48
996 if (lba48) {
997
998 ide_outb(device, ATA_SECT_CNT, 0);
999 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1000#ifdef CONFIG_SYS_64BIT_LBA
1001 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1002 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1003#else
1004 ide_outb(device, ATA_LBA_MID, 0);
1005 ide_outb(device, ATA_LBA_HIGH, 0);
1006#endif
1007 }
1008#endif
1009 ide_outb(device, ATA_SECT_CNT, 1);
1010 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1011 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1012 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1013
1014#ifdef CONFIG_LBA48
1015 if (lba48) {
1016 ide_outb(device, ATA_DEV_HD,
1017 ATA_LBA | ATA_DEVICE(device));
1018 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
1019
1020 } else
1021#endif
1022 {
1023 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1024 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1025 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
1026 }
1027
1028 udelay(50);
1029
1030
1031 c = ide_wait(device, IDE_TIME_OUT);
1032
1033 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1034 ATA_STAT_DRQ) {
1035 printf("Error (no IRQ) dev %d blk " LBAF
1036 ": status %#02x\n", device, blknr, c);
1037 goto WR_OUT;
1038 }
1039
1040 ide_output_data(device, buffer, ATA_SECTORWORDS);
1041 c = ide_inb(device, ATA_STATUS);
1042 ++n;
1043 ++blknr;
1044 buffer += ATA_BLOCKSIZE;
1045 }
1046WR_OUT:
1047 return n;
1048}
1049
1050#if defined(CONFIG_OF_IDE_FIXUP)
1051int ide_device_present(int dev)
1052{
1053 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1054 return 0;
1055 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1056}
1057#endif
1058
1059#ifdef CONFIG_BLK
1060static int ide_blk_probe(struct udevice *udev)
1061{
1062 struct blk_desc *desc = dev_get_uclass_plat(udev);
1063
1064
1065 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1066 BLK_VEN_SIZE);
1067 desc->vendor[BLK_VEN_SIZE] = '\0';
1068 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1069 BLK_PRD_SIZE);
1070 desc->product[BLK_PRD_SIZE] = '\0';
1071 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1072 BLK_REV_SIZE);
1073 desc->revision[BLK_REV_SIZE] = '\0';
1074
1075 return 0;
1076}
1077
1078static const struct blk_ops ide_blk_ops = {
1079 .read = ide_read,
1080 .write = ide_write,
1081};
1082
1083U_BOOT_DRIVER(ide_blk) = {
1084 .name = "ide_blk",
1085 .id = UCLASS_BLK,
1086 .ops = &ide_blk_ops,
1087 .probe = ide_blk_probe,
1088};
1089
1090static int ide_probe(struct udevice *udev)
1091{
1092 struct udevice *blk_dev;
1093 char name[20];
1094 int blksz;
1095 lbaint_t size;
1096 int i;
1097 int ret;
1098
1099 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1100 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1101 sprintf(name, "blk#%d", i);
1102
1103 blksz = ide_dev_desc[i].blksz;
1104 size = blksz * ide_dev_desc[i].lba;
1105
1106
1107
1108
1109
1110 if (!blksz)
1111 continue;
1112 ret = blk_create_devicef(udev, "ide_blk", name,
1113 UCLASS_IDE, i,
1114 blksz, size, &blk_dev);
1115 if (ret)
1116 return ret;
1117
1118 ret = blk_probe_or_unbind(blk_dev);
1119 if (ret)
1120 return ret;
1121 }
1122 }
1123
1124 return 0;
1125}
1126
1127U_BOOT_DRIVER(ide) = {
1128 .name = "ide",
1129 .id = UCLASS_IDE,
1130 .probe = ide_probe,
1131};
1132
1133struct pci_device_id ide_supported[] = {
1134 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1135 { }
1136};
1137
1138U_BOOT_PCI_DEVICE(ide, ide_supported);
1139
1140UCLASS_DRIVER(ide) = {
1141 .name = "ide",
1142 .id = UCLASS_IDE,
1143};
1144#else
1145U_BOOT_LEGACY_BLK(ide) = {
1146 .uclass_idname = "ide",
1147 .uclass_id = UCLASS_IDE,
1148 .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
1149 .desc = ide_dev_desc,
1150};
1151#endif
1152