blob: db26f4320dc8d76a4b69d5f5a0a1c1654fbf18c4 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk34c202c2011-10-29 09:41:40 +00002 * (C) Copyright 2000-2011
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8/*
9 * IDE support
10 */
Albert Aribaud113bfe42010-08-08 05:17:06 +053011
wdenkc6097192002-11-03 00:24:07 +000012#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -070013#include <blk.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <config.h>
15#include <watchdog.h>
16#include <command.h>
17#include <image.h>
18#include <asm/byteorder.h>
Heiko Schocherf98984c2007-08-28 17:39:14 +020019#include <asm/io.h>
Grant Likely735dd972007-02-20 09:04:34 +010020
wdenkc6097192002-11-03 00:24:07 +000021#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
22# include <pcmcia.h>
23#endif
Grant Likely735dd972007-02-20 09:04:34 +010024
wdenkc6097192002-11-03 00:24:07 +000025#include <ide.h>
26#include <ata.h>
Grant Likely735dd972007-02-20 09:04:34 +010027
wdenkc6097192002-11-03 00:24:07 +000028#ifdef CONFIG_STATUS_LED
29# include <status_led.h>
30#endif
Grant Likely735dd972007-02-20 09:04:34 +010031
wdenk5cf91d62004-04-23 20:32:05 +000032#ifdef __PPC__
33# define EIEIO __asm__ volatile ("eieio")
wdenk1a344f22005-02-03 23:00:49 +000034# define SYNC __asm__ volatile ("sync")
wdenk5cf91d62004-04-23 20:32:05 +000035#else
36# define EIEIO /* nothing */
wdenk1a344f22005-02-03 23:00:49 +000037# define SYNC /* nothing */
wdenkc6097192002-11-03 00:24:07 +000038#endif
39
wdenkc6097192002-11-03 00:24:07 +000040/* ------------------------------------------------------------------------- */
41
42/* Current I/O Device */
43static int curr_device = -1;
44
45/* Current offset for IDE0 / IDE1 bus access */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020046ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
47#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
48 CONFIG_SYS_ATA_IDE0_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000049#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
51 CONFIG_SYS_ATA_IDE1_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000052#endif
53};
54
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020055static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +000056
Simon Glass4101f682016-02-29 15:25:34 -070057struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +000058/* ------------------------------------------------------------------------- */
59
wdenkc6097192002-11-03 00:24:07 +000060#define IDE_TIME_OUT 2000 /* 2 sec timeout */
61
62#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
63
64#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
65
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066#ifndef CONFIG_SYS_ATA_PORT_ADDR
67#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher566a4942007-06-22 19:11:54 +020068#endif
wdenkc6097192002-11-03 00:24:07 +000069
Simon Glassed735082016-05-01 11:36:00 -060070#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
71# define DEVICE_LED(x) 0
72# define LED_IDE1 1
73# define LED_IDE2 2
wdenkc6097192002-11-03 00:24:07 +000074#endif
75
Simon Glassed735082016-05-01 11:36:00 -060076#ifdef CONFIG_IDE_RESET
77extern void ide_set_reset(int idereset);
78
79static void ide_reset(void)
80{
81 int i;
82
83 curr_device = -1;
84 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
85 ide_bus_ok[i] = 0;
86 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
87 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
88
89 ide_set_reset(1); /* assert reset */
90
91 /* the reset signal shall be asserted for et least 25 us */
92 udelay(25);
93
94 WATCHDOG_RESET();
95
96 /* de-assert RESET signal */
97 ide_set_reset(0);
98
99 /* wait 250 ms */
100 for (i = 0; i < 250; ++i)
101 udelay(1000);
102}
103#else
104#define ide_reset() /* dummy */
105#endif /* CONFIG_IDE_RESET */
106
107/*
108 * Wait until Busy bit is off, or timeout (in ms)
109 * Return last status
110 */
111static uchar ide_wait(int dev, ulong t)
112{
113 ulong delay = 10 * t; /* poll every 100 us */
114 uchar c;
115
116 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
117 udelay(100);
118 if (delay-- == 0)
119 break;
120 }
121 return c;
122}
123
124/*
125 * copy src to dest, skipping leading and trailing blanks and null
126 * terminate the string
127 * "len" is the size of available memory including the terminating '\0'
128 */
129static void ident_cpy(unsigned char *dst, unsigned char *src,
130 unsigned int len)
131{
132 unsigned char *end, *last;
133
134 last = dst;
135 end = src + len - 1;
136
137 /* reserve space for '\0' */
138 if (len < 2)
139 goto OUT;
140
141 /* skip leading white space */
142 while ((*src) && (src < end) && (*src == ' '))
143 ++src;
144
145 /* copy string, omitting trailing white space */
146 while ((*src) && (src < end)) {
147 *dst++ = *src;
148 if (*src++ != ' ')
149 last = dst;
150 }
151OUT:
152 *last = '\0';
153}
154
155#ifdef CONFIG_ATAPI
156/****************************************************************************
157 * ATAPI Support
158 */
159
160#if defined(CONFIG_IDE_SWAP_IO)
161/* since ATAPI may use commands with not 4 bytes alligned length
162 * we have our own transfer functions, 2 bytes alligned */
163__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
164{
165 ushort *dbuf;
166 volatile ushort *pbuf;
167
168 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
169 dbuf = (ushort *)sect_buf;
170
171 debug("in output data shorts base for read is %lx\n",
172 (unsigned long) pbuf);
173
174 while (shorts--) {
175 EIEIO;
176 *pbuf = *dbuf++;
177 }
178}
179
180__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
181{
182 ushort *dbuf;
183 volatile ushort *pbuf;
184
185 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
186 dbuf = (ushort *)sect_buf;
187
188 debug("in input data shorts base for read is %lx\n",
189 (unsigned long) pbuf);
190
191 while (shorts--) {
192 EIEIO;
193 *dbuf++ = *pbuf;
194 }
195}
196
197#else /* ! CONFIG_IDE_SWAP_IO */
198__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
199{
200 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
201}
202
203__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
204{
205 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
206}
207
208#endif /* CONFIG_IDE_SWAP_IO */
209
210/*
211 * Wait until (Status & mask) == res, or timeout (in ms)
212 * Return last status
213 * This is used since some ATAPI CD ROMs clears their Busy Bit first
214 * and then they set their DRQ Bit
215 */
216static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
217{
218 ulong delay = 10 * t; /* poll every 100 us */
219 uchar c;
220
221 /* prevents to read the status before valid */
222 c = ide_inb(dev, ATA_DEV_CTL);
223
224 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
225 /* break if error occurs (doesn't make sense to wait more) */
226 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
227 break;
228 udelay(100);
229 if (delay-- == 0)
230 break;
231 }
232 return c;
233}
234
235/*
236 * issue an atapi command
237 */
238unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
239 unsigned char *buffer, int buflen)
240{
241 unsigned char c, err, mask, res;
242 int n;
243
244 ide_led(DEVICE_LED(device), 1); /* LED on */
245
246 /* Select device
247 */
248 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
249 res = 0;
250 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
251 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
252 if ((c & mask) != res) {
253 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
254 c);
255 err = 0xFF;
256 goto AI_OUT;
257 }
258 /* write taskfile */
259 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
260 ide_outb(device, ATA_SECT_CNT, 0);
261 ide_outb(device, ATA_SECT_NUM, 0);
262 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
263 ide_outb(device, ATA_CYL_HIGH,
264 (unsigned char) ((buflen >> 8) & 0xFF));
265 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
266
267 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
268 udelay(50);
269
270 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
271 res = ATA_STAT_DRQ;
272 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
273
274 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
275 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
276 device, c);
277 err = 0xFF;
278 goto AI_OUT;
279 }
280
281 /* write command block */
282 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
283
284 /* ATAPI Command written wait for completition */
285 udelay(5000); /* device must set bsy */
286
287 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
288 /*
289 * if no data wait for DRQ = 0 BSY = 0
290 * if data wait for DRQ = 1 BSY = 0
291 */
292 res = 0;
293 if (buflen)
294 res = ATA_STAT_DRQ;
295 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
296 if ((c & mask) != res) {
297 if (c & ATA_STAT_ERR) {
298 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
299 debug("atapi_issue 1 returned sense key %X status %02X\n",
300 err, c);
301 } else {
302 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
303 ccb[0], c);
304 err = 0xFF;
305 }
306 goto AI_OUT;
307 }
308 n = ide_inb(device, ATA_CYL_HIGH);
309 n <<= 8;
310 n += ide_inb(device, ATA_CYL_LOW);
311 if (n > buflen) {
312 printf("ERROR, transfer bytes %d requested only %d\n", n,
313 buflen);
314 err = 0xff;
315 goto AI_OUT;
316 }
317 if ((n == 0) && (buflen < 0)) {
318 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
319 err = 0xff;
320 goto AI_OUT;
321 }
322 if (n != buflen) {
323 debug("WARNING, transfer bytes %d not equal with requested %d\n",
324 n, buflen);
325 }
326 if (n != 0) { /* data transfer */
327 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
328 /* we transfer shorts */
329 n >>= 1;
330 /* ok now decide if it is an in or output */
331 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
332 debug("Write to device\n");
333 ide_output_data_shorts(device, (unsigned short *)buffer,
334 n);
335 } else {
336 debug("Read from device @ %p shorts %d\n", buffer, n);
337 ide_input_data_shorts(device, (unsigned short *)buffer,
338 n);
339 }
340 }
341 udelay(5000); /* seems that some CD ROMs need this... */
342 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
343 res = 0;
344 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
345 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
346 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
347 debug("atapi_issue 2 returned sense key %X status %X\n", err,
348 c);
349 } else {
350 err = 0;
351 }
352AI_OUT:
353 ide_led(DEVICE_LED(device), 0); /* LED off */
354 return err;
355}
356
357/*
358 * sending the command to atapi_issue. If an status other than good
359 * returns, an request_sense will be issued
360 */
361
362#define ATAPI_DRIVE_NOT_READY 100
363#define ATAPI_UNIT_ATTN 10
364
365unsigned char atapi_issue_autoreq(int device,
366 unsigned char *ccb,
367 int ccblen,
368 unsigned char *buffer, int buflen)
369{
370 unsigned char sense_data[18], sense_ccb[12];
371 unsigned char res, key, asc, ascq;
372 int notready, unitattn;
373
374 unitattn = ATAPI_UNIT_ATTN;
375 notready = ATAPI_DRIVE_NOT_READY;
376
377retry:
378 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
379 if (res == 0)
380 return 0; /* Ok */
381
382 if (res == 0xFF)
383 return 0xFF; /* error */
384
385 debug("(auto_req)atapi_issue returned sense key %X\n", res);
386
387 memset(sense_ccb, 0, sizeof(sense_ccb));
388 memset(sense_data, 0, sizeof(sense_data));
389 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
390 sense_ccb[4] = 18; /* allocation Length */
391
392 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
393 key = (sense_data[2] & 0xF);
394 asc = (sense_data[12]);
395 ascq = (sense_data[13]);
396
397 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
398 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
399 sense_data[0], key, asc, ascq);
400
401 if ((key == 0))
402 return 0; /* ok device ready */
403
404 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
405 if (unitattn-- > 0) {
406 udelay(200 * 1000);
407 goto retry;
408 }
409 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
410 goto error;
411 }
412 if ((asc == 0x4) && (ascq == 0x1)) {
413 /* not ready, but will be ready soon */
414 if (notready-- > 0) {
415 udelay(200 * 1000);
416 goto retry;
417 }
418 printf("Drive not ready, tried %d times\n",
419 ATAPI_DRIVE_NOT_READY);
420 goto error;
421 }
422 if (asc == 0x3a) {
423 debug("Media not present\n");
424 goto error;
425 }
426
427 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
428 ascq);
429error:
430 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
431 return 0xFF;
432}
433
434/*
435 * atapi_read:
436 * we transfer only one block per command, since the multiple DRQ per
437 * command is not yet implemented
438 */
439#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
440#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
441#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
442
443ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
444 void *buffer)
445{
446 int device = block_dev->devnum;
447 ulong n = 0;
448 unsigned char ccb[12]; /* Command descriptor block */
449 ulong cnt;
450
451 debug("atapi_read dev %d start " LBAF " blocks " LBAF
452 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
453
454 do {
455 if (blkcnt > ATAPI_READ_MAX_BLOCK)
456 cnt = ATAPI_READ_MAX_BLOCK;
457 else
458 cnt = blkcnt;
459
460 ccb[0] = ATAPI_CMD_READ_12;
461 ccb[1] = 0; /* reserved */
462 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
463 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
464 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
465 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
466 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
467 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
468 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
469 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
470 ccb[10] = 0; /* reserved */
471 ccb[11] = 0; /* reserved */
472
473 if (atapi_issue_autoreq(device, ccb, 12,
474 (unsigned char *)buffer,
475 cnt * ATAPI_READ_BLOCK_SIZE)
476 == 0xFF) {
477 return n;
478 }
479 n += cnt;
480 blkcnt -= cnt;
481 blknr += cnt;
482 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
483 } while (blkcnt > 0);
484 return n;
485}
486
487static void atapi_inquiry(struct blk_desc *dev_desc)
488{
489 unsigned char ccb[12]; /* Command descriptor block */
490 unsigned char iobuf[64]; /* temp buf */
491 unsigned char c;
492 int device;
493
494 device = dev_desc->devnum;
495 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
496 dev_desc->block_read = atapi_read;
497
498 memset(ccb, 0, sizeof(ccb));
499 memset(iobuf, 0, sizeof(iobuf));
500
501 ccb[0] = ATAPI_CMD_INQUIRY;
502 ccb[4] = 40; /* allocation Legnth */
503 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
504
505 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
506 if (c != 0)
507 return;
508
509 /* copy device ident strings */
510 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
511 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
512 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
513
514 dev_desc->lun = 0;
515 dev_desc->lba = 0;
516 dev_desc->blksz = 0;
517 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
518 dev_desc->type = iobuf[0] & 0x1f;
519
520 if ((iobuf[1] & 0x80) == 0x80)
521 dev_desc->removable = 1;
522 else
523 dev_desc->removable = 0;
524
525 memset(ccb, 0, sizeof(ccb));
526 memset(iobuf, 0, sizeof(iobuf));
527 ccb[0] = ATAPI_CMD_START_STOP;
528 ccb[4] = 0x03; /* start */
529
530 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
531
532 debug("ATAPI_CMD_START_STOP returned %x\n", c);
533 if (c != 0)
534 return;
535
536 memset(ccb, 0, sizeof(ccb));
537 memset(iobuf, 0, sizeof(iobuf));
538 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
539
540 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
541 if (c != 0)
542 return;
543
544 memset(ccb, 0, sizeof(ccb));
545 memset(iobuf, 0, sizeof(iobuf));
546 ccb[0] = ATAPI_CMD_READ_CAP;
547 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
548 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
549 if (c != 0)
550 return;
551
552 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
553 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
554 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
555
556 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
557 ((unsigned long) iobuf[1] << 16) +
558 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
559 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
560 ((unsigned long) iobuf[5] << 16) +
561 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
562 dev_desc->log2blksz = LOG2(dev_desc->blksz);
563#ifdef CONFIG_LBA48
564 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
565 dev_desc->lba48 = 0;
566#endif
567 return;
568}
569
570#endif /* CONFIG_ATAPI */
571
572static void ide_ident(struct blk_desc *dev_desc)
573{
574 unsigned char c;
575 hd_driveid_t iop;
576
577#ifdef CONFIG_ATAPI
578 int retries = 0;
579#endif
580 int device;
581
582 device = dev_desc->devnum;
583 printf(" Device %d: ", device);
584
585 ide_led(DEVICE_LED(device), 1); /* LED on */
586 /* Select device
587 */
588 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
589 dev_desc->if_type = IF_TYPE_IDE;
590#ifdef CONFIG_ATAPI
591
592 retries = 0;
593
594 /* Warning: This will be tricky to read */
595 while (retries <= 1) {
596 /* check signature */
597 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
598 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
599 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
600 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
601 /* ATAPI Signature found */
602 dev_desc->if_type = IF_TYPE_ATAPI;
603 /*
604 * Start Ident Command
605 */
606 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
607 /*
608 * Wait for completion - ATAPI devices need more time
609 * to become ready
610 */
611 c = ide_wait(device, ATAPI_TIME_OUT);
612 } else
613#endif
614 {
615 /*
616 * Start Ident Command
617 */
618 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
619
620 /*
621 * Wait for completion
622 */
623 c = ide_wait(device, IDE_TIME_OUT);
624 }
625 ide_led(DEVICE_LED(device), 0); /* LED off */
626
627 if (((c & ATA_STAT_DRQ) == 0) ||
628 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
629#ifdef CONFIG_ATAPI
630 {
631 /*
632 * Need to soft reset the device
633 * in case it's an ATAPI...
634 */
635 debug("Retrying...\n");
636 ide_outb(device, ATA_DEV_HD,
637 ATA_LBA | ATA_DEVICE(device));
638 udelay(100000);
639 ide_outb(device, ATA_COMMAND, 0x08);
640 udelay(500000); /* 500 ms */
641 }
642 /*
643 * Select device
644 */
645 ide_outb(device, ATA_DEV_HD,
646 ATA_LBA | ATA_DEVICE(device));
647 retries++;
648#else
649 return;
650#endif
651 }
652#ifdef CONFIG_ATAPI
653 else
654 break;
655 } /* see above - ugly to read */
656
657 if (retries == 2) /* Not found */
658 return;
659#endif
660
661 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
662
663 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
664 sizeof(dev_desc->revision));
665 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
666 sizeof(dev_desc->vendor));
667 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
668 sizeof(dev_desc->product));
669#ifdef __LITTLE_ENDIAN
670 /*
671 * firmware revision, model, and serial number have Big Endian Byte
672 * order in Word. Convert all three to little endian.
673 *
674 * See CF+ and CompactFlash Specification Revision 2.0:
675 * 6.2.1.6: Identify Drive, Table 39 for more details
676 */
677
678 strswab(dev_desc->revision);
679 strswab(dev_desc->vendor);
680 strswab(dev_desc->product);
681#endif /* __LITTLE_ENDIAN */
682
683 if ((iop.config & 0x0080) == 0x0080)
684 dev_desc->removable = 1;
685 else
686 dev_desc->removable = 0;
687
688#ifdef CONFIG_ATAPI
689 if (dev_desc->if_type == IF_TYPE_ATAPI) {
690 atapi_inquiry(dev_desc);
691 return;
692 }
693#endif /* CONFIG_ATAPI */
694
695#ifdef __BIG_ENDIAN
696 /* swap shorts */
697 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
698#else /* ! __BIG_ENDIAN */
699 /*
700 * do not swap shorts on little endian
701 *
702 * See CF+ and CompactFlash Specification Revision 2.0:
703 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
704 */
705 dev_desc->lba = iop.lba_capacity;
706#endif /* __BIG_ENDIAN */
707
708#ifdef CONFIG_LBA48
709 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
710 dev_desc->lba48 = 1;
711 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
712 ((unsigned long long) iop.lba48_capacity[1] << 16) |
713 ((unsigned long long) iop.lba48_capacity[2] << 32) |
714 ((unsigned long long) iop.lba48_capacity[3] << 48);
715 } else {
716 dev_desc->lba48 = 0;
717 }
718#endif /* CONFIG_LBA48 */
719 /* assuming HD */
720 dev_desc->type = DEV_TYPE_HARDDISK;
721 dev_desc->blksz = ATA_BLOCKSIZE;
722 dev_desc->log2blksz = LOG2(dev_desc->blksz);
723 dev_desc->lun = 0; /* just to fill something in... */
724
725#if 0 /* only used to test the powersaving mode,
726 * if enabled, the drive goes after 5 sec
727 * in standby mode */
728 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
729 c = ide_wait(device, IDE_TIME_OUT);
730 ide_outb(device, ATA_SECT_CNT, 1);
731 ide_outb(device, ATA_LBA_LOW, 0);
732 ide_outb(device, ATA_LBA_MID, 0);
733 ide_outb(device, ATA_LBA_HIGH, 0);
734 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
735 ide_outb(device, ATA_COMMAND, 0xe3);
736 udelay(50);
737 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
738#endif
739}
wdenkc6097192002-11-03 00:24:07 +0000740
wdenkc6097192002-11-03 00:24:07 +0000741/* ------------------------------------------------------------------------- */
742
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000743int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000744{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000745 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +0000746
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000747 switch (argc) {
748 case 0:
749 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000750 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000751 case 2:
752 if (strncmp(argv[1], "res", 3) == 0) {
753 puts("\nReset IDE"
754#ifdef CONFIG_IDE_8xx_DIRECT
755 " on PCMCIA " PCMCIA_SLOT_MSG
756#endif
757 ": ");
wdenkc6097192002-11-03 00:24:07 +0000758
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000759 ide_init();
760 return 0;
761 } else if (strncmp(argv[1], "inf", 3) == 0) {
762 int i;
763
764 putc('\n');
765
766 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
767 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
768 continue; /* list only known devices */
769 printf("IDE device %d: ", i);
770 dev_print(&ide_dev_desc[i]);
771 }
772 return 0;
773
774 } else if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glassed735082016-05-01 11:36:00 -0600775 if (curr_device < 0 ||
776 curr_device >= CONFIG_SYS_IDE_MAXDEVICE) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000777 puts("\nno IDE devices available\n");
778 return 1;
779 }
780 printf("\nIDE device %d: ", curr_device);
781 dev_print(&ide_dev_desc[curr_device]);
782 return 0;
783 } else if (strncmp(argv[1], "part", 4) == 0) {
784 int dev, ok;
785
786 for (ok = 0, dev = 0;
787 dev < CONFIG_SYS_IDE_MAXDEVICE;
788 ++dev) {
789 if (ide_dev_desc[dev].part_type !=
790 PART_TYPE_UNKNOWN) {
791 ++ok;
792 if (dev)
793 putc('\n');
Simon Glass3e8bd462016-02-29 15:25:48 -0700794 part_print(&ide_dev_desc[dev]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000795 }
796 }
797 if (!ok) {
798 puts("\nno IDE devices available\n");
799 rcode++;
800 }
801 return rcode;
802 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000803 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000804 case 3:
805 if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glass53dbcdd2016-05-01 11:35:59 -0600806 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000807
808 printf("\nIDE device %d: ", dev);
809 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
810 puts("unknown device\n");
811 return 1;
812 }
813 dev_print(&ide_dev_desc[dev]);
814 /*ide_print (dev); */
815
816 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
817 return 1;
818
819 curr_device = dev;
820
821 puts("... is now current device\n");
822
823 return 0;
824 } else if (strncmp(argv[1], "part", 4) == 0) {
Simon Glass53dbcdd2016-05-01 11:35:59 -0600825 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000826
827 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700828 part_print(&ide_dev_desc[dev]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000829 } else {
830 printf("\nIDE device %d not available\n",
831 dev);
832 rcode = 1;
833 }
834 return rcode;
835 }
836
Simon Glass4c12eeb2011-12-10 08:44:01 +0000837 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000838 default:
839 /* at least 4 args */
840
841 if (strcmp(argv[1], "read") == 0) {
842 ulong addr = simple_strtoul(argv[2], NULL, 16);
843 ulong cnt = simple_strtoul(argv[4], NULL, 16);
Simon Glass4101f682016-02-29 15:25:34 -0700844 struct blk_desc *dev_desc;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000845 ulong n;
846
847#ifdef CONFIG_SYS_64BIT_LBA
848 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
849
Simon Glass53dbcdd2016-05-01 11:35:59 -0600850 printf("\nIDE read: device %d block # %lld, count %ld...",
851 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000852#else
853 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
854
Simon Glass53dbcdd2016-05-01 11:35:59 -0600855 printf("\nIDE read: device %d block # %ld, count %ld...",
856 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000857#endif
858
Stephen Warren7c4213f2015-12-07 11:38:48 -0700859 dev_desc = &ide_dev_desc[curr_device];
Simon Glass2a981dc2016-02-29 15:25:52 -0700860 n = blk_dread(dev_desc, blk, cnt, (ulong *)addr);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000861 /* flush cache after read */
862 flush_cache(addr,
863 cnt * ide_dev_desc[curr_device].blksz);
864
865 printf("%ld blocks read: %s\n",
866 n, (n == cnt) ? "OK" : "ERROR");
867 if (n == cnt)
868 return 0;
869 else
870 return 1;
871 } else if (strcmp(argv[1], "write") == 0) {
872 ulong addr = simple_strtoul(argv[2], NULL, 16);
873 ulong cnt = simple_strtoul(argv[4], NULL, 16);
874 ulong n;
875
876#ifdef CONFIG_SYS_64BIT_LBA
877 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
878
Simon Glass53dbcdd2016-05-01 11:35:59 -0600879 printf("\nIDE write: device %d block # %lld, count %ld...",
880 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000881#else
882 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
883
Simon Glass53dbcdd2016-05-01 11:35:59 -0600884 printf("\nIDE write: device %d block # %ld, count %ld...",
885 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000886#endif
Stephen Warren7c4213f2015-12-07 11:38:48 -0700887 n = ide_write(&ide_dev_desc[curr_device], blk, cnt,
888 (ulong *)addr);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000889
Simon Glassed735082016-05-01 11:36:00 -0600890 printf("%ld blocks written: %s\n", n,
891 n == cnt ? "OK" : "ERROR");
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000892 if (n == cnt)
893 return 0;
894 else
895 return 1;
896 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000897 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000898 }
899
900 return rcode;
901 }
wdenkc6097192002-11-03 00:24:07 +0000902}
903
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000904int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000905{
Rob Herring7405a132012-09-21 04:02:30 +0000906 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000907}
908
909/* ------------------------------------------------------------------------- */
910
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200911__weak void ide_led(uchar led, uchar status)
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000912{
913#if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
914 static uchar led_buffer; /* Buffer for current LED status */
915
916 uchar *led_port = LED_PORT;
917
918 if (status) /* switch LED on */
919 led_buffer |= led;
920 else /* switch LED off */
921 led_buffer &= ~led;
922
923 *led_port = led_buffer;
924#endif
925}
926
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000927/* ------------------------------------------------------------------------- */
928
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200929__weak void ide_outb(int dev, int port, unsigned char val)
Stefan Roesef2302d42008-08-06 14:05:38 +0200930{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000931 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
932 dev, port, val,
933 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000934
935#if defined(CONFIG_IDE_AHB)
936 if (port) {
937 /* write command */
938 ide_write_register(dev, port, val);
939 } else {
940 /* write data */
941 outb(val, (ATA_CURR_BASE(dev)));
942 }
943#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000944 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000945#endif
Stefan Roesef2302d42008-08-06 14:05:38 +0200946}
Macpaul Lin0abddf82011-04-11 20:45:32 +0000947
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200948__weak unsigned char ide_inb(int dev, int port)
Stefan Roesef2302d42008-08-06 14:05:38 +0200949{
950 uchar val;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000951
952#if defined(CONFIG_IDE_AHB)
953 val = ide_read_register(dev, port);
954#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000955 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000956#endif
957
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000958 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
959 dev, port,
960 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roesef2302d42008-08-06 14:05:38 +0200961 return val;
962}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000963
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000964void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000965{
wdenkc6097192002-11-03 00:24:07 +0000966 unsigned char c;
967 int i, bus;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000968
wdenk9fd5e312003-12-07 23:55:12 +0000969#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000970 extern int ide_devices_found; /* Initialized in check_ide_device() */
971#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk9fd5e312003-12-07 23:55:12 +0000972
973#ifdef CONFIG_IDE_PREINIT
974 WATCHDOG_RESET();
975
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000976 if (ide_preinit()) {
977 puts("ide_preinit failed\n");
wdenk9fd5e312003-12-07 23:55:12 +0000978 return;
979 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000980#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000981
wdenkc6097192002-11-03 00:24:07 +0000982 WATCHDOG_RESET();
983
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000984 /*
985 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000986 * Light LED's to show
987 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000988 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
989
990 /* ATAPI Drives seems to need a proper IDE Reset */
991 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000992
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000993#ifdef CONFIG_IDE_INIT_POSTRESET
994 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000995
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000996 if (ide_init_postreset()) {
997 puts("ide_preinit_postreset failed\n");
998 return;
999 }
1000#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +00001001
1002 /*
1003 * Wait for IDE to get ready.
1004 * According to spec, this can take up to 31 seconds!
1005 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001006 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1007 int dev =
1008 bus * (CONFIG_SYS_IDE_MAXDEVICE /
1009 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +00001010
wdenk6069ff22003-02-28 00:49:47 +00001011#ifdef CONFIG_IDE_8xx_PCCARD
1012 /* Skip non-ide devices from probing */
1013 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001014 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk6069ff22003-02-28 00:49:47 +00001015 continue;
1016 }
1017#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001018 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +00001019
1020 ide_bus_ok[bus] = 0;
1021
1022 /* Select device
1023 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001024 udelay(100000); /* 100 ms */
1025 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
1026 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +00001027 i = 0;
1028 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001029 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +00001030
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001031 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +00001032 i++;
1033 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001034 puts("** Timeout **\n");
1035 /* LED's off */
1036 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +00001037 return;
1038 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001039 if ((i >= 100) && ((i % 100) == 0))
1040 putc('.');
1041
wdenkc6097192002-11-03 00:24:07 +00001042 } while (c & ATA_STAT_BUSY);
1043
1044 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001045 puts("not available ");
1046 debug("Status = 0x%02X ", c);
1047#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
1048 } else if ((c & ATA_STAT_READY) == 0) {
1049 puts("not available ");
1050 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +00001051#endif
1052 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001053 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +00001054 ide_bus_ok[bus] = 1;
1055 }
1056 WATCHDOG_RESET();
1057 }
wdenkc7de8292002-11-19 11:04:11 +00001058
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001059 putc('\n');
wdenkc6097192002-11-03 00:24:07 +00001060
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001061 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +00001062
1063 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001064 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +00001065 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001066 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1067 ide_dev_desc[i].if_type = IF_TYPE_IDE;
Simon Glassbcce53d2016-02-29 15:25:51 -07001068 ide_dev_desc[i].devnum = i;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001069 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
1070 ide_dev_desc[i].blksz = 0;
Egbert Eich0472fbf2013-04-09 21:11:56 +00001071 ide_dev_desc[i].log2blksz =
1072 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001073 ide_dev_desc[i].lba = 0;
1074 ide_dev_desc[i].block_read = ide_read;
Macpaul Lin0abddf82011-04-11 20:45:32 +00001075 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +00001076 if (!ide_bus_ok[IDE_BUS(i)])
1077 continue;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001078 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001079 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001080 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001081 dev_print(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001082
wdenkc6097192002-11-03 00:24:07 +00001083 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001084 /* initialize partition type */
Simon Glass3e8bd462016-02-29 15:25:48 -07001085 part_init(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +00001086 if (curr_device < 0)
1087 curr_device = i;
1088 }
1089 }
1090 WATCHDOG_RESET();
1091}
1092
1093/* ------------------------------------------------------------------------- */
1094
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001095#ifdef CONFIG_PARTITIONS
Simon Glass4101f682016-02-29 15:25:34 -07001096struct blk_desc *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +00001097{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001098 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +00001099}
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001100#endif
wdenkc6097192002-11-03 00:24:07 +00001101
wdenkc6097192002-11-03 00:24:07 +00001102/* ------------------------------------------------------------------------- */
1103
wdenk5da627a2003-10-09 20:09:04 +00001104/* We only need to swap data if we are running on a big endian cpu. */
Pavel Herrmann4d1361d2012-10-09 07:10:08 +00001105#if defined(__LITTLE_ENDIAN)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001106__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001107{
1108 ide_input_data(dev, sect_buf, words);
1109}
wdenk5da627a2003-10-09 20:09:04 +00001110#else
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001111__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +00001112{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001113 volatile ushort *pbuf =
Simon Glassed735082016-05-01 11:36:00 -06001114 (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1115 ushort *dbuf = (ushort *)sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001116
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001117 debug("in input swap data base for read is %lx\n",
1118 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +00001119
1120 while (words--) {
Wolfgang Denk0c32d962006-06-16 17:32:31 +02001121#ifdef __MIPS__
Simon Glassed735082016-05-01 11:36:00 -06001122 *dbuf++ = swab16p((u16 *)pbuf);
1123 *dbuf++ = swab16p((u16 *)pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +02001124#else
wdenk1a344f22005-02-03 23:00:49 +00001125 *dbuf++ = ld_le16(pbuf);
1126 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +02001127#endif /* !MIPS */
wdenk1a344f22005-02-03 23:00:49 +00001128 }
wdenkc6097192002-11-03 00:24:07 +00001129}
Pavel Herrmann4d1361d2012-10-09 07:10:08 +00001130#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +00001131
wdenk2262cfe2002-11-18 00:14:45 +00001132
Albert Aribaudf2a37fc2010-08-08 05:17:05 +05301133#if defined(CONFIG_IDE_SWAP_IO)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001134__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +00001135{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001136 ushort *dbuf;
1137 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +00001138
Simon Glassed735082016-05-01 11:36:00 -06001139 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1140 dbuf = (ushort *)sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001141 while (words--) {
1142 EIEIO;
1143 *pbuf = *dbuf++;
1144 EIEIO;
1145 *pbuf = *dbuf++;
1146 }
wdenkc6097192002-11-03 00:24:07 +00001147}
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001148#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001149__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +00001150{
Macpaul Lin0abddf82011-04-11 20:45:32 +00001151#if defined(CONFIG_IDE_AHB)
1152 ide_write_data(dev, sect_buf, words);
1153#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001154 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +00001155#endif
wdenk2262cfe2002-11-18 00:14:45 +00001156}
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001157#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +00001158
Albert Aribaudf2a37fc2010-08-08 05:17:05 +05301159#if defined(CONFIG_IDE_SWAP_IO)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001160__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +00001161{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001162 ushort *dbuf;
1163 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +00001164
Simon Glassed735082016-05-01 11:36:00 -06001165 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1166 dbuf = (ushort *)sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001167
1168 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
1169
1170 while (words--) {
1171 EIEIO;
1172 *dbuf++ = *pbuf;
1173 EIEIO;
1174 *dbuf++ = *pbuf;
1175 }
wdenkc6097192002-11-03 00:24:07 +00001176}
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001177#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001178__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +00001179{
Macpaul Lin0abddf82011-04-11 20:45:32 +00001180#if defined(CONFIG_IDE_AHB)
1181 ide_read_data(dev, sect_buf, words);
1182#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001183 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +00001184#endif
wdenk2262cfe2002-11-18 00:14:45 +00001185}
1186
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001187#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +00001188
wdenkc6097192002-11-03 00:24:07 +00001189/* ------------------------------------------------------------------------- */
1190
Simon Glass4101f682016-02-29 15:25:34 -07001191ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
Stephen Warren7c4213f2015-12-07 11:38:48 -07001192 void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001193{
Simon Glassbcce53d2016-02-29 15:25:51 -07001194 int device = block_dev->devnum;
wdenkc6097192002-11-03 00:24:07 +00001195 ulong n = 0;
1196 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001197 unsigned char pwrsave = 0; /* power save */
1198
wdenk42dfe7a2004-03-14 22:25:36 +00001199#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001200 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +00001201
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001202 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +00001203 /* more than 28 bits used, use 48bit mode */
1204 lba48 = 1;
1205 }
1206#endif
Sascha Silbeff8fef52013-06-14 13:07:25 +02001207 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001208 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001209
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001210 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001211
1212 /* Select device
1213 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001214 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1215 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001216
1217 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001218 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001219 goto IDE_READ_E;
1220 }
1221
1222 /* first check if the drive is in Powersaving mode, if yes,
1223 * increase the timeout value */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001224 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1225 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001226
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001227 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001228
1229 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001230 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001231 goto IDE_READ_E;
1232 }
1233 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001234 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +00001235 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001236 c = ide_inb(device, ATA_SECT_CNT);
1237 debug("Powersaving %02X\n", c);
1238 if (c == 0)
1239 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +00001240 }
1241
1242
1243 while (blkcnt-- > 0) {
1244
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001245 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001246
1247 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001248 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001249 break;
1250 }
wdenk42dfe7a2004-03-14 22:25:36 +00001251#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001252 if (lba48) {
1253 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001254 ide_outb(device, ATA_SECT_CNT, 0);
1255 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001256#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001257 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1258 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001259#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001260 ide_outb(device, ATA_LBA_MID, 0);
1261 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001262#endif
wdenkc40b2952004-03-13 23:29:43 +00001263 }
1264#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001265 ide_outb(device, ATA_SECT_CNT, 1);
1266 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1267 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1268 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +00001269
wdenk42dfe7a2004-03-14 22:25:36 +00001270#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001271 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001272 ide_outb(device, ATA_DEV_HD,
1273 ATA_LBA | ATA_DEVICE(device));
1274 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkc40b2952004-03-13 23:29:43 +00001275
1276 } else
1277#endif
1278 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001279 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1280 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1281 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkc40b2952004-03-13 23:29:43 +00001282 }
wdenkc6097192002-11-03 00:24:07 +00001283
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001284 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001285
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001286 if (pwrsave) {
1287 /* may take up to 4 sec */
1288 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1289 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +00001290 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001291 /* can't take over 500 ms */
1292 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001293 }
1294
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001295 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1296 ATA_STAT_DRQ) {
Simon Glassed735082016-05-01 11:36:00 -06001297 printf("Error (no IRQ) dev %d blk " LBAF
1298 ": status %#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +00001299 break;
1300 }
1301
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001302 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001303 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001304
1305 ++n;
1306 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +02001307 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001308 }
1309IDE_READ_E:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001310 ide_led(DEVICE_LED(device), 0); /* LED off */
Simon Glass53dbcdd2016-05-01 11:35:59 -06001311 return n;
wdenkc6097192002-11-03 00:24:07 +00001312}
1313
1314/* ------------------------------------------------------------------------- */
1315
1316
Simon Glass4101f682016-02-29 15:25:34 -07001317ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
Stephen Warren7c4213f2015-12-07 11:38:48 -07001318 const void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001319{
Simon Glassbcce53d2016-02-29 15:25:51 -07001320 int device = block_dev->devnum;
wdenkc6097192002-11-03 00:24:07 +00001321 ulong n = 0;
1322 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001323
wdenk42dfe7a2004-03-14 22:25:36 +00001324#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001325 unsigned char lba48 = 0;
1326
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001327 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +00001328 /* more than 28 bits used, use 48bit mode */
1329 lba48 = 1;
1330 }
1331#endif
wdenkc6097192002-11-03 00:24:07 +00001332
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001333 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001334
1335 /* Select device
1336 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001337 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001338
1339 while (blkcnt-- > 0) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001340 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001341
1342 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001343 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001344 goto WR_OUT;
1345 }
wdenk42dfe7a2004-03-14 22:25:36 +00001346#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001347 if (lba48) {
1348 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001349 ide_outb(device, ATA_SECT_CNT, 0);
1350 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001351#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001352 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1353 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001354#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001355 ide_outb(device, ATA_LBA_MID, 0);
1356 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001357#endif
wdenkc40b2952004-03-13 23:29:43 +00001358 }
1359#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001360 ide_outb(device, ATA_SECT_CNT, 1);
1361 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1362 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1363 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +00001364
wdenk42dfe7a2004-03-14 22:25:36 +00001365#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001366 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001367 ide_outb(device, ATA_DEV_HD,
1368 ATA_LBA | ATA_DEVICE(device));
1369 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkc40b2952004-03-13 23:29:43 +00001370
1371 } else
1372#endif
1373 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001374 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1375 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1376 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc40b2952004-03-13 23:29:43 +00001377 }
wdenkc6097192002-11-03 00:24:07 +00001378
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001379 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001380
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001381 /* can't take over 500 ms */
1382 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001383
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001384 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1385 ATA_STAT_DRQ) {
Simon Glassed735082016-05-01 11:36:00 -06001386 printf("Error (no IRQ) dev %d blk " LBAF
1387 ": status %#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +00001388 goto WR_OUT;
1389 }
1390
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001391 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001392 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001393 ++n;
1394 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +02001395 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001396 }
1397WR_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001398 ide_led(DEVICE_LED(device), 0); /* LED off */
Simon Glass53dbcdd2016-05-01 11:35:59 -06001399 return n;
wdenkc6097192002-11-03 00:24:07 +00001400}
1401
1402/* ------------------------------------------------------------------------- */
1403
Heiko Schocher3887c3f2009-09-23 07:56:08 +02001404#if defined(CONFIG_OF_IDE_FIXUP)
1405int ide_device_present(int dev)
1406{
1407 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1408 return 0;
Simon Glassed735082016-05-01 11:36:00 -06001409 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
Heiko Schocher3887c3f2009-09-23 07:56:08 +02001410}
1411#endif
wdenkc6097192002-11-03 00:24:07 +00001412/* ------------------------------------------------------------------------- */
1413
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001414U_BOOT_CMD(ide, 5, 1, do_ide,
1415 "IDE sub-system",
1416 "reset - reset IDE controller\n"
1417 "ide info - show available IDE devices\n"
1418 "ide device [dev] - show or set current device\n"
1419 "ide part [dev] - print partition table of one or all IDE devices\n"
1420 "ide read addr blk# cnt\n"
1421 "ide write addr blk# cnt - read/write `cnt'"
1422 " blocks starting at block `blk#'\n"
1423 " to/from memory address `addr'");
wdenk8bde7f72003-06-27 21:31:46 +00001424
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001425U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1426 "boot from IDE device", "loadAddr dev:part");