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