xref: /rk3399_rockchip-uboot/cmd/ide.c (revision 53dbcdd0cd4b90077b0b3893bcf25354ed825cec)
1 /*
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 /*
9  * IDE support
10  */
11 
12 #include <common.h>
13 #include <blk.h>
14 #include <config.h>
15 #include <watchdog.h>
16 #include <command.h>
17 #include <image.h>
18 #include <asm/byteorder.h>
19 #include <asm/io.h>
20 
21 #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
22 # include <pcmcia.h>
23 #endif
24 
25 #include <ide.h>
26 #include <ata.h>
27 
28 #ifdef CONFIG_STATUS_LED
29 # include <status_led.h>
30 #endif
31 
32 #ifdef __PPC__
33 # define EIEIO		__asm__ volatile ("eieio")
34 # define SYNC		__asm__ volatile ("sync")
35 #else
36 # define EIEIO		/* nothing */
37 # define SYNC		/* nothing */
38 #endif
39 
40 /* ------------------------------------------------------------------------- */
41 
42 /* Current I/O Device	*/
43 static int curr_device = -1;
44 
45 /* Current offset for IDE0 / IDE1 bus access	*/
46 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
47 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
48 	CONFIG_SYS_ATA_IDE0_OFFSET,
49 #endif
50 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
51 	CONFIG_SYS_ATA_IDE1_OFFSET,
52 #endif
53 };
54 
55 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
56 
57 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
58 /* ------------------------------------------------------------------------- */
59 
60 #ifdef CONFIG_IDE_RESET
61 static void  ide_reset(void);
62 #else
63 #define ide_reset()	/* dummy */
64 #endif
65 
66 static void ide_ident(struct blk_desc *dev_desc);
67 static uchar ide_wait(int dev, ulong t);
68 
69 #define IDE_TIME_OUT	2000	/* 2 sec timeout */
70 
71 #define ATAPI_TIME_OUT	7000	/* 7 sec timeout (5 sec seems to work...) */
72 
73 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
74 
75 static void ident_cpy(unsigned char *dest, unsigned char *src,
76 		      unsigned int len);
77 
78 #ifndef CONFIG_SYS_ATA_PORT_ADDR
79 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
80 #endif
81 
82 #ifdef CONFIG_ATAPI
83 static void atapi_inquiry(struct blk_desc *dev_desc);
84 static ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr,
85 			lbaint_t blkcnt, void *buffer);
86 #endif
87 
88 
89 /* ------------------------------------------------------------------------- */
90 
91 int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
92 {
93 	int rcode = 0;
94 
95 	switch (argc) {
96 	case 0:
97 	case 1:
98 		return CMD_RET_USAGE;
99 	case 2:
100 		if (strncmp(argv[1], "res", 3) == 0) {
101 			puts("\nReset IDE"
102 #ifdef CONFIG_IDE_8xx_DIRECT
103 			     " on PCMCIA " PCMCIA_SLOT_MSG
104 #endif
105 			     ": ");
106 
107 			ide_init();
108 			return 0;
109 		} else if (strncmp(argv[1], "inf", 3) == 0) {
110 			int i;
111 
112 			putc('\n');
113 
114 			for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
115 				if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
116 					continue;  /* list only known devices */
117 				printf("IDE device %d: ", i);
118 				dev_print(&ide_dev_desc[i]);
119 			}
120 			return 0;
121 
122 		} else if (strncmp(argv[1], "dev", 3) == 0) {
123 			if ((curr_device < 0)
124 			    || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
125 				puts("\nno IDE devices available\n");
126 				return 1;
127 			}
128 			printf("\nIDE device %d: ", curr_device);
129 			dev_print(&ide_dev_desc[curr_device]);
130 			return 0;
131 		} else if (strncmp(argv[1], "part", 4) == 0) {
132 			int dev, ok;
133 
134 			for (ok = 0, dev = 0;
135 			     dev < CONFIG_SYS_IDE_MAXDEVICE;
136 			     ++dev) {
137 				if (ide_dev_desc[dev].part_type !=
138 				    PART_TYPE_UNKNOWN) {
139 					++ok;
140 					if (dev)
141 						putc('\n');
142 					part_print(&ide_dev_desc[dev]);
143 				}
144 			}
145 			if (!ok) {
146 				puts("\nno IDE devices available\n");
147 				rcode++;
148 			}
149 			return rcode;
150 		}
151 		return CMD_RET_USAGE;
152 	case 3:
153 		if (strncmp(argv[1], "dev", 3) == 0) {
154 			int dev = (int)simple_strtoul(argv[2], NULL, 10);
155 
156 			printf("\nIDE device %d: ", dev);
157 			if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
158 				puts("unknown device\n");
159 				return 1;
160 			}
161 			dev_print(&ide_dev_desc[dev]);
162 			/*ide_print (dev); */
163 
164 			if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
165 				return 1;
166 
167 			curr_device = dev;
168 
169 			puts("... is now current device\n");
170 
171 			return 0;
172 		} else if (strncmp(argv[1], "part", 4) == 0) {
173 			int dev = (int)simple_strtoul(argv[2], NULL, 10);
174 
175 			if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
176 				part_print(&ide_dev_desc[dev]);
177 			} else {
178 				printf("\nIDE device %d not available\n",
179 				       dev);
180 				rcode = 1;
181 			}
182 			return rcode;
183 		}
184 
185 		return CMD_RET_USAGE;
186 	default:
187 		/* at least 4 args */
188 
189 		if (strcmp(argv[1], "read") == 0) {
190 			ulong addr = simple_strtoul(argv[2], NULL, 16);
191 			ulong cnt = simple_strtoul(argv[4], NULL, 16);
192 			struct blk_desc *dev_desc;
193 			ulong n;
194 
195 #ifdef CONFIG_SYS_64BIT_LBA
196 			lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
197 
198 			printf("\nIDE read: device %d block # %lld, count %ld...",
199 			       curr_device, blk, cnt);
200 #else
201 			lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
202 
203 			printf("\nIDE read: device %d block # %ld, count %ld...",
204 			       curr_device, blk, cnt);
205 #endif
206 
207 			dev_desc = &ide_dev_desc[curr_device];
208 			n = blk_dread(dev_desc, blk, cnt, (ulong *)addr);
209 			/* flush cache after read */
210 			flush_cache(addr,
211 				    cnt * ide_dev_desc[curr_device].blksz);
212 
213 			printf("%ld blocks read: %s\n",
214 			       n, (n == cnt) ? "OK" : "ERROR");
215 			if (n == cnt)
216 				return 0;
217 			else
218 				return 1;
219 		} else if (strcmp(argv[1], "write") == 0) {
220 			ulong addr = simple_strtoul(argv[2], NULL, 16);
221 			ulong cnt = simple_strtoul(argv[4], NULL, 16);
222 			ulong n;
223 
224 #ifdef CONFIG_SYS_64BIT_LBA
225 			lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
226 
227 			printf("\nIDE write: device %d block # %lld, count %ld...",
228 			       curr_device, blk, cnt);
229 #else
230 			lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
231 
232 			printf("\nIDE write: device %d block # %ld, count %ld...",
233 			       curr_device, blk, cnt);
234 #endif
235 			n = ide_write(&ide_dev_desc[curr_device], blk, cnt,
236 				      (ulong *)addr);
237 
238 			printf("%ld blocks written: %s\n",
239 				n, (n == cnt) ? "OK" : "ERROR");
240 			if (n == cnt)
241 				return 0;
242 			else
243 				return 1;
244 		} else {
245 			return CMD_RET_USAGE;
246 		}
247 
248 		return rcode;
249 	}
250 }
251 
252 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
253 {
254 	return common_diskboot(cmdtp, "ide", argc, argv);
255 }
256 
257 /* ------------------------------------------------------------------------- */
258 
259 __weak void ide_led(uchar led, uchar status)
260 {
261 #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
262 	static uchar led_buffer;	/* Buffer for current LED status */
263 
264 	uchar *led_port = LED_PORT;
265 
266 	if (status)		/* switch LED on        */
267 		led_buffer |= led;
268 	else			/* switch LED off       */
269 		led_buffer &= ~led;
270 
271 	*led_port = led_buffer;
272 #endif
273 }
274 
275 #ifndef CONFIG_IDE_LED	/* define LED macros, they are not used anyways */
276 # define DEVICE_LED(x) 0
277 # define LED_IDE1 1
278 # define LED_IDE2 2
279 #endif
280 
281 /* ------------------------------------------------------------------------- */
282 
283 __weak void ide_outb(int dev, int port, unsigned char val)
284 {
285 	debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
286 	      dev, port, val,
287 	      (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
288 
289 #if defined(CONFIG_IDE_AHB)
290 	if (port) {
291 		/* write command */
292 		ide_write_register(dev, port, val);
293 	} else {
294 		/* write data */
295 		outb(val, (ATA_CURR_BASE(dev)));
296 	}
297 #else
298 	outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
299 #endif
300 }
301 
302 __weak unsigned char ide_inb(int dev, int port)
303 {
304 	uchar val;
305 
306 #if defined(CONFIG_IDE_AHB)
307 	val = ide_read_register(dev, port);
308 #else
309 	val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
310 #endif
311 
312 	debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
313 	      dev, port,
314 	      (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
315 	return val;
316 }
317 
318 void ide_init(void)
319 {
320 	unsigned char c;
321 	int i, bus;
322 
323 #ifdef CONFIG_IDE_8xx_PCCARD
324 	extern int ide_devices_found;	/* Initialized in check_ide_device() */
325 #endif /* CONFIG_IDE_8xx_PCCARD */
326 
327 #ifdef CONFIG_IDE_PREINIT
328 	WATCHDOG_RESET();
329 
330 	if (ide_preinit()) {
331 		puts("ide_preinit failed\n");
332 		return;
333 	}
334 #endif /* CONFIG_IDE_PREINIT */
335 
336 	WATCHDOG_RESET();
337 
338 	/*
339 	 * Reset the IDE just to be sure.
340 	 * Light LED's to show
341 	 */
342 	ide_led((LED_IDE1 | LED_IDE2), 1);	/* LED's on     */
343 
344 	/* ATAPI Drives seems to need a proper IDE Reset */
345 	ide_reset();
346 
347 #ifdef CONFIG_IDE_INIT_POSTRESET
348 	WATCHDOG_RESET();
349 
350 	if (ide_init_postreset()) {
351 		puts("ide_preinit_postreset failed\n");
352 		return;
353 	}
354 #endif /* CONFIG_IDE_INIT_POSTRESET */
355 
356 	/*
357 	 * Wait for IDE to get ready.
358 	 * According to spec, this can take up to 31 seconds!
359 	 */
360 	for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
361 		int dev =
362 			bus * (CONFIG_SYS_IDE_MAXDEVICE /
363 			       CONFIG_SYS_IDE_MAXBUS);
364 
365 #ifdef CONFIG_IDE_8xx_PCCARD
366 		/* Skip non-ide devices from probing */
367 		if ((ide_devices_found & (1 << bus)) == 0) {
368 			ide_led((LED_IDE1 | LED_IDE2), 0);	/* LED's off */
369 			continue;
370 		}
371 #endif
372 		printf("Bus %d: ", bus);
373 
374 		ide_bus_ok[bus] = 0;
375 
376 		/* Select device
377 		 */
378 		udelay(100000);	/* 100 ms */
379 		ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
380 		udelay(100000);	/* 100 ms */
381 		i = 0;
382 		do {
383 			udelay(10000);	/* 10 ms */
384 
385 			c = ide_inb(dev, ATA_STATUS);
386 			i++;
387 			if (i > (ATA_RESET_TIME * 100)) {
388 				puts("** Timeout **\n");
389 				/* LED's off */
390 				ide_led((LED_IDE1 | LED_IDE2), 0);
391 				return;
392 			}
393 			if ((i >= 100) && ((i % 100) == 0))
394 				putc('.');
395 
396 		} while (c & ATA_STAT_BUSY);
397 
398 		if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
399 			puts("not available  ");
400 			debug("Status = 0x%02X ", c);
401 #ifndef CONFIG_ATAPI		/* ATAPI Devices do not set DRDY */
402 		} else if ((c & ATA_STAT_READY) == 0) {
403 			puts("not available  ");
404 			debug("Status = 0x%02X ", c);
405 #endif
406 		} else {
407 			puts("OK ");
408 			ide_bus_ok[bus] = 1;
409 		}
410 		WATCHDOG_RESET();
411 	}
412 
413 	putc('\n');
414 
415 	ide_led((LED_IDE1 | LED_IDE2), 0);	/* LED's off    */
416 
417 	curr_device = -1;
418 	for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
419 		int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
420 		ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
421 		ide_dev_desc[i].if_type = IF_TYPE_IDE;
422 		ide_dev_desc[i].devnum = i;
423 		ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
424 		ide_dev_desc[i].blksz = 0;
425 		ide_dev_desc[i].log2blksz =
426 			LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
427 		ide_dev_desc[i].lba = 0;
428 		ide_dev_desc[i].block_read = ide_read;
429 		ide_dev_desc[i].block_write = ide_write;
430 		if (!ide_bus_ok[IDE_BUS(i)])
431 			continue;
432 		ide_led(led, 1);	/* LED on       */
433 		ide_ident(&ide_dev_desc[i]);
434 		ide_led(led, 0);	/* LED off      */
435 		dev_print(&ide_dev_desc[i]);
436 
437 		if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
438 			/* initialize partition type */
439 			part_init(&ide_dev_desc[i]);
440 			if (curr_device < 0)
441 				curr_device = i;
442 		}
443 	}
444 	WATCHDOG_RESET();
445 }
446 
447 /* ------------------------------------------------------------------------- */
448 
449 #ifdef CONFIG_PARTITIONS
450 struct blk_desc *ide_get_dev(int dev)
451 {
452 	return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
453 }
454 #endif
455 
456 /* ------------------------------------------------------------------------- */
457 
458 /* We only need to swap data if we are running on a big endian cpu. */
459 #if defined(__LITTLE_ENDIAN)
460 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
461 {
462 	ide_input_data(dev, sect_buf, words);
463 }
464 #else
465 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
466 {
467 	volatile ushort *pbuf =
468 		(ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
469 	ushort *dbuf = (ushort *) sect_buf;
470 
471 	debug("in input swap data base for read is %lx\n",
472 	      (unsigned long) pbuf);
473 
474 	while (words--) {
475 #ifdef __MIPS__
476 		*dbuf++ = swab16p((u16 *) pbuf);
477 		*dbuf++ = swab16p((u16 *) pbuf);
478 #else
479 		*dbuf++ = ld_le16(pbuf);
480 		*dbuf++ = ld_le16(pbuf);
481 #endif /* !MIPS */
482 	}
483 }
484 #endif /* __LITTLE_ENDIAN */
485 
486 
487 #if defined(CONFIG_IDE_SWAP_IO)
488 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
489 {
490 	ushort *dbuf;
491 	volatile ushort *pbuf;
492 
493 	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
494 	dbuf = (ushort *) sect_buf;
495 	while (words--) {
496 		EIEIO;
497 		*pbuf = *dbuf++;
498 		EIEIO;
499 		*pbuf = *dbuf++;
500 	}
501 }
502 #else  /* ! CONFIG_IDE_SWAP_IO */
503 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
504 {
505 #if defined(CONFIG_IDE_AHB)
506 	ide_write_data(dev, sect_buf, words);
507 #else
508 	outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
509 #endif
510 }
511 #endif /* CONFIG_IDE_SWAP_IO */
512 
513 #if defined(CONFIG_IDE_SWAP_IO)
514 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
515 {
516 	ushort *dbuf;
517 	volatile ushort *pbuf;
518 
519 	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
520 	dbuf = (ushort *) sect_buf;
521 
522 	debug("in input data base for read is %lx\n", (unsigned long) pbuf);
523 
524 	while (words--) {
525 		EIEIO;
526 		*dbuf++ = *pbuf;
527 		EIEIO;
528 		*dbuf++ = *pbuf;
529 	}
530 }
531 #else  /* ! CONFIG_IDE_SWAP_IO */
532 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
533 {
534 #if defined(CONFIG_IDE_AHB)
535 	ide_read_data(dev, sect_buf, words);
536 #else
537 	insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
538 #endif
539 }
540 
541 #endif /* CONFIG_IDE_SWAP_IO */
542 
543 /* -------------------------------------------------------------------------
544  */
545 static void ide_ident(struct blk_desc *dev_desc)
546 {
547 	unsigned char c;
548 	hd_driveid_t iop;
549 
550 #ifdef CONFIG_ATAPI
551 	int retries = 0;
552 #endif
553 	int device;
554 
555 	device = dev_desc->devnum;
556 	printf("  Device %d: ", device);
557 
558 	ide_led(DEVICE_LED(device), 1);	/* LED on       */
559 	/* Select device
560 	 */
561 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
562 	dev_desc->if_type = IF_TYPE_IDE;
563 #ifdef CONFIG_ATAPI
564 
565 	retries = 0;
566 
567 	/* Warning: This will be tricky to read */
568 	while (retries <= 1) {
569 		/* check signature */
570 		if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
571 		    (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
572 		    (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
573 		    (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
574 			/* ATAPI Signature found */
575 			dev_desc->if_type = IF_TYPE_ATAPI;
576 			/*
577 			 * Start Ident Command
578 			 */
579 			ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
580 			/*
581 			 * Wait for completion - ATAPI devices need more time
582 			 * to become ready
583 			 */
584 			c = ide_wait(device, ATAPI_TIME_OUT);
585 		} else
586 #endif
587 		{
588 			/*
589 			 * Start Ident Command
590 			 */
591 			ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
592 
593 			/*
594 			 * Wait for completion
595 			 */
596 			c = ide_wait(device, IDE_TIME_OUT);
597 		}
598 		ide_led(DEVICE_LED(device), 0);	/* LED off      */
599 
600 		if (((c & ATA_STAT_DRQ) == 0) ||
601 		    ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
602 #ifdef CONFIG_ATAPI
603 			{
604 				/*
605 				 * Need to soft reset the device
606 				 * in case it's an ATAPI...
607 				 */
608 				debug("Retrying...\n");
609 				ide_outb(device, ATA_DEV_HD,
610 					 ATA_LBA | ATA_DEVICE(device));
611 				udelay(100000);
612 				ide_outb(device, ATA_COMMAND, 0x08);
613 				udelay(500000);	/* 500 ms */
614 			}
615 			/*
616 			 * Select device
617 			 */
618 			ide_outb(device, ATA_DEV_HD,
619 				 ATA_LBA | ATA_DEVICE(device));
620 			retries++;
621 #else
622 			return;
623 #endif
624 		}
625 #ifdef CONFIG_ATAPI
626 		else
627 			break;
628 	}			/* see above - ugly to read */
629 
630 	if (retries == 2)	/* Not found */
631 		return;
632 #endif
633 
634 	ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
635 
636 	ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
637 		  sizeof(dev_desc->revision));
638 	ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
639 		  sizeof(dev_desc->vendor));
640 	ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
641 		  sizeof(dev_desc->product));
642 #ifdef __LITTLE_ENDIAN
643 	/*
644 	 * firmware revision, model, and serial number have Big Endian Byte
645 	 * order in Word. Convert all three to little endian.
646 	 *
647 	 * See CF+ and CompactFlash Specification Revision 2.0:
648 	 * 6.2.1.6: Identify Drive, Table 39 for more details
649 	 */
650 
651 	strswab(dev_desc->revision);
652 	strswab(dev_desc->vendor);
653 	strswab(dev_desc->product);
654 #endif /* __LITTLE_ENDIAN */
655 
656 	if ((iop.config & 0x0080) == 0x0080)
657 		dev_desc->removable = 1;
658 	else
659 		dev_desc->removable = 0;
660 
661 #ifdef CONFIG_ATAPI
662 	if (dev_desc->if_type == IF_TYPE_ATAPI) {
663 		atapi_inquiry(dev_desc);
664 		return;
665 	}
666 #endif /* CONFIG_ATAPI */
667 
668 #ifdef __BIG_ENDIAN
669 	/* swap shorts */
670 	dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
671 #else  /* ! __BIG_ENDIAN */
672 	/*
673 	 * do not swap shorts on little endian
674 	 *
675 	 * See CF+ and CompactFlash Specification Revision 2.0:
676 	 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
677 	 */
678 	dev_desc->lba = iop.lba_capacity;
679 #endif /* __BIG_ENDIAN */
680 
681 #ifdef CONFIG_LBA48
682 	if (iop.command_set_2 & 0x0400) {	/* LBA 48 support */
683 		dev_desc->lba48 = 1;
684 		dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
685 			((unsigned long long) iop.lba48_capacity[1] << 16) |
686 			((unsigned long long) iop.lba48_capacity[2] << 32) |
687 			((unsigned long long) iop.lba48_capacity[3] << 48);
688 	} else {
689 		dev_desc->lba48 = 0;
690 	}
691 #endif /* CONFIG_LBA48 */
692 	/* assuming HD */
693 	dev_desc->type = DEV_TYPE_HARDDISK;
694 	dev_desc->blksz = ATA_BLOCKSIZE;
695 	dev_desc->log2blksz = LOG2(dev_desc->blksz);
696 	dev_desc->lun = 0;	/* just to fill something in... */
697 
698 #if 0				/* only used to test the powersaving mode,
699 				 * if enabled, the drive goes after 5 sec
700 				 * in standby mode */
701 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
702 	c = ide_wait(device, IDE_TIME_OUT);
703 	ide_outb(device, ATA_SECT_CNT, 1);
704 	ide_outb(device, ATA_LBA_LOW, 0);
705 	ide_outb(device, ATA_LBA_MID, 0);
706 	ide_outb(device, ATA_LBA_HIGH, 0);
707 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
708 	ide_outb(device, ATA_COMMAND, 0xe3);
709 	udelay(50);
710 	c = ide_wait(device, IDE_TIME_OUT);	/* can't take over 500 ms */
711 #endif
712 }
713 
714 
715 /* ------------------------------------------------------------------------- */
716 
717 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
718 	       void *buffer)
719 {
720 	int device = block_dev->devnum;
721 	ulong n = 0;
722 	unsigned char c;
723 	unsigned char pwrsave = 0;	/* power save */
724 
725 #ifdef CONFIG_LBA48
726 	unsigned char lba48 = 0;
727 
728 	if (blknr & 0x0000fffff0000000ULL) {
729 		/* more than 28 bits used, use 48bit mode */
730 		lba48 = 1;
731 	}
732 #endif
733 	debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
734 	      device, blknr, blkcnt, (ulong) buffer);
735 
736 	ide_led(DEVICE_LED(device), 1);	/* LED on       */
737 
738 	/* Select device
739 	 */
740 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
741 	c = ide_wait(device, IDE_TIME_OUT);
742 
743 	if (c & ATA_STAT_BUSY) {
744 		printf("IDE read: device %d not ready\n", device);
745 		goto IDE_READ_E;
746 	}
747 
748 	/* first check if the drive is in Powersaving mode, if yes,
749 	 * increase the timeout value */
750 	ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
751 	udelay(50);
752 
753 	c = ide_wait(device, IDE_TIME_OUT);	/* can't take over 500 ms */
754 
755 	if (c & ATA_STAT_BUSY) {
756 		printf("IDE read: device %d not ready\n", device);
757 		goto IDE_READ_E;
758 	}
759 	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
760 		printf("No Powersaving mode %X\n", c);
761 	} else {
762 		c = ide_inb(device, ATA_SECT_CNT);
763 		debug("Powersaving %02X\n", c);
764 		if (c == 0)
765 			pwrsave = 1;
766 	}
767 
768 
769 	while (blkcnt-- > 0) {
770 
771 		c = ide_wait(device, IDE_TIME_OUT);
772 
773 		if (c & ATA_STAT_BUSY) {
774 			printf("IDE read: device %d not ready\n", device);
775 			break;
776 		}
777 #ifdef CONFIG_LBA48
778 		if (lba48) {
779 			/* write high bits */
780 			ide_outb(device, ATA_SECT_CNT, 0);
781 			ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
782 #ifdef CONFIG_SYS_64BIT_LBA
783 			ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
784 			ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
785 #else
786 			ide_outb(device, ATA_LBA_MID, 0);
787 			ide_outb(device, ATA_LBA_HIGH, 0);
788 #endif
789 		}
790 #endif
791 		ide_outb(device, ATA_SECT_CNT, 1);
792 		ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
793 		ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
794 		ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
795 
796 #ifdef CONFIG_LBA48
797 		if (lba48) {
798 			ide_outb(device, ATA_DEV_HD,
799 				 ATA_LBA | ATA_DEVICE(device));
800 			ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
801 
802 		} else
803 #endif
804 		{
805 			ide_outb(device, ATA_DEV_HD, ATA_LBA |
806 				 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
807 			ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
808 		}
809 
810 		udelay(50);
811 
812 		if (pwrsave) {
813 			/* may take up to 4 sec */
814 			c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
815 			pwrsave = 0;
816 		} else {
817 			/* can't take over 500 ms */
818 			c = ide_wait(device, IDE_TIME_OUT);
819 		}
820 
821 		if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
822 		    ATA_STAT_DRQ) {
823 			printf("Error (no IRQ) dev %d blk " LBAF ": status "
824 			       "%#02x\n", device, blknr, c);
825 			break;
826 		}
827 
828 		ide_input_data(device, buffer, ATA_SECTORWORDS);
829 		(void) ide_inb(device, ATA_STATUS);	/* clear IRQ */
830 
831 		++n;
832 		++blknr;
833 		buffer += ATA_BLOCKSIZE;
834 	}
835 IDE_READ_E:
836 	ide_led(DEVICE_LED(device), 0);	/* LED off      */
837 	return n;
838 }
839 
840 /* ------------------------------------------------------------------------- */
841 
842 
843 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
844 		const void *buffer)
845 {
846 	int device = block_dev->devnum;
847 	ulong n = 0;
848 	unsigned char c;
849 
850 #ifdef CONFIG_LBA48
851 	unsigned char lba48 = 0;
852 
853 	if (blknr & 0x0000fffff0000000ULL) {
854 		/* more than 28 bits used, use 48bit mode */
855 		lba48 = 1;
856 	}
857 #endif
858 
859 	ide_led(DEVICE_LED(device), 1);	/* LED on       */
860 
861 	/* Select device
862 	 */
863 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
864 
865 	while (blkcnt-- > 0) {
866 
867 		c = ide_wait(device, IDE_TIME_OUT);
868 
869 		if (c & ATA_STAT_BUSY) {
870 			printf("IDE read: device %d not ready\n", device);
871 			goto WR_OUT;
872 		}
873 #ifdef CONFIG_LBA48
874 		if (lba48) {
875 			/* write high bits */
876 			ide_outb(device, ATA_SECT_CNT, 0);
877 			ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
878 #ifdef CONFIG_SYS_64BIT_LBA
879 			ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
880 			ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
881 #else
882 			ide_outb(device, ATA_LBA_MID, 0);
883 			ide_outb(device, ATA_LBA_HIGH, 0);
884 #endif
885 		}
886 #endif
887 		ide_outb(device, ATA_SECT_CNT, 1);
888 		ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
889 		ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
890 		ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
891 
892 #ifdef CONFIG_LBA48
893 		if (lba48) {
894 			ide_outb(device, ATA_DEV_HD,
895 				 ATA_LBA | ATA_DEVICE(device));
896 			ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
897 
898 		} else
899 #endif
900 		{
901 			ide_outb(device, ATA_DEV_HD, ATA_LBA |
902 				 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
903 			ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
904 		}
905 
906 		udelay(50);
907 
908 		/* can't take over 500 ms */
909 		c = ide_wait(device, IDE_TIME_OUT);
910 
911 		if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
912 		    ATA_STAT_DRQ) {
913 			printf("Error (no IRQ) dev %d blk " LBAF ": status "
914 				"%#02x\n", device, blknr, c);
915 			goto WR_OUT;
916 		}
917 
918 		ide_output_data(device, buffer, ATA_SECTORWORDS);
919 		c = ide_inb(device, ATA_STATUS);	/* clear IRQ */
920 		++n;
921 		++blknr;
922 		buffer += ATA_BLOCKSIZE;
923 	}
924 WR_OUT:
925 	ide_led(DEVICE_LED(device), 0);	/* LED off      */
926 	return n;
927 }
928 
929 /* ------------------------------------------------------------------------- */
930 
931 /*
932  * copy src to dest, skipping leading and trailing blanks and null
933  * terminate the string
934  * "len" is the size of available memory including the terminating '\0'
935  */
936 static void ident_cpy(unsigned char *dst, unsigned char *src,
937 		      unsigned int len)
938 {
939 	unsigned char *end, *last;
940 
941 	last = dst;
942 	end = src + len - 1;
943 
944 	/* reserve space for '\0' */
945 	if (len < 2)
946 		goto OUT;
947 
948 	/* skip leading white space */
949 	while ((*src) && (src < end) && (*src == ' '))
950 		++src;
951 
952 	/* copy string, omitting trailing white space */
953 	while ((*src) && (src < end)) {
954 		*dst++ = *src;
955 		if (*src++ != ' ')
956 			last = dst;
957 	}
958 OUT:
959 	*last = '\0';
960 }
961 
962 /* ------------------------------------------------------------------------- */
963 
964 /*
965  * Wait until Busy bit is off, or timeout (in ms)
966  * Return last status
967  */
968 static uchar ide_wait(int dev, ulong t)
969 {
970 	ulong delay = 10 * t;	/* poll every 100 us */
971 	uchar c;
972 
973 	while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
974 		udelay(100);
975 		if (delay-- == 0)
976 			break;
977 	}
978 	return c;
979 }
980 
981 /* ------------------------------------------------------------------------- */
982 
983 #ifdef CONFIG_IDE_RESET
984 extern void ide_set_reset(int idereset);
985 
986 static void ide_reset(void)
987 {
988 	int i;
989 
990 	curr_device = -1;
991 	for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
992 		ide_bus_ok[i] = 0;
993 	for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
994 		ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
995 
996 	ide_set_reset(1);	/* assert reset */
997 
998 	/* the reset signal shall be asserted for et least 25 us */
999 	udelay(25);
1000 
1001 	WATCHDOG_RESET();
1002 
1003 	/* de-assert RESET signal */
1004 	ide_set_reset(0);
1005 
1006 	/* wait 250 ms */
1007 	for (i = 0; i < 250; ++i)
1008 		udelay(1000);
1009 }
1010 
1011 #endif /* CONFIG_IDE_RESET */
1012 
1013 /* ------------------------------------------------------------------------- */
1014 
1015 #if defined(CONFIG_OF_IDE_FIXUP)
1016 int ide_device_present(int dev)
1017 {
1018 	if (dev >= CONFIG_SYS_IDE_MAXBUS)
1019 		return 0;
1020 	return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1021 }
1022 #endif
1023 /* ------------------------------------------------------------------------- */
1024 
1025 #ifdef CONFIG_ATAPI
1026 /****************************************************************************
1027  * ATAPI Support
1028  */
1029 
1030 #if defined(CONFIG_IDE_SWAP_IO)
1031 /* since ATAPI may use commands with not 4 bytes alligned length
1032  * we have our own transfer functions, 2 bytes alligned */
1033 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1034 {
1035 	ushort *dbuf;
1036 	volatile ushort *pbuf;
1037 
1038 	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1039 	dbuf = (ushort *) sect_buf;
1040 
1041 	debug("in output data shorts base for read is %lx\n",
1042 	      (unsigned long) pbuf);
1043 
1044 	while (shorts--) {
1045 		EIEIO;
1046 		*pbuf = *dbuf++;
1047 	}
1048 }
1049 
1050 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1051 {
1052 	ushort *dbuf;
1053 	volatile ushort *pbuf;
1054 
1055 	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1056 	dbuf = (ushort *) sect_buf;
1057 
1058 	debug("in input data shorts base for read is %lx\n",
1059 	      (unsigned long) pbuf);
1060 
1061 	while (shorts--) {
1062 		EIEIO;
1063 		*dbuf++ = *pbuf;
1064 	}
1065 }
1066 
1067 #else  /* ! CONFIG_IDE_SWAP_IO */
1068 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1069 {
1070 	outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1071 }
1072 
1073 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1074 {
1075 	insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
1076 }
1077 
1078 #endif /* CONFIG_IDE_SWAP_IO */
1079 
1080 /*
1081  * Wait until (Status & mask) == res, or timeout (in ms)
1082  * Return last status
1083  * This is used since some ATAPI CD ROMs clears their Busy Bit first
1084  * and then they set their DRQ Bit
1085  */
1086 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
1087 {
1088 	ulong delay = 10 * t;	/* poll every 100 us */
1089 	uchar c;
1090 
1091 	/* prevents to read the status before valid */
1092 	c = ide_inb(dev, ATA_DEV_CTL);
1093 
1094 	while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
1095 		/* break if error occurs (doesn't make sense to wait more) */
1096 		if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1097 			break;
1098 		udelay(100);
1099 		if (delay-- == 0)
1100 			break;
1101 	}
1102 	return c;
1103 }
1104 
1105 /*
1106  * issue an atapi command
1107  */
1108 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1109 			  unsigned char *buffer, int buflen)
1110 {
1111 	unsigned char c, err, mask, res;
1112 	int n;
1113 
1114 	ide_led(DEVICE_LED(device), 1);	/* LED on       */
1115 
1116 	/* Select device
1117 	 */
1118 	mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
1119 	res = 0;
1120 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1121 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1122 	if ((c & mask) != res) {
1123 		printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1124 		       c);
1125 		err = 0xFF;
1126 		goto AI_OUT;
1127 	}
1128 	/* write taskfile */
1129 	ide_outb(device, ATA_ERROR_REG, 0);	/* no DMA, no overlaped */
1130 	ide_outb(device, ATA_SECT_CNT, 0);
1131 	ide_outb(device, ATA_SECT_NUM, 0);
1132 	ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1133 	ide_outb(device, ATA_CYL_HIGH,
1134 		 (unsigned char) ((buflen >> 8) & 0xFF));
1135 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1136 
1137 	ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1138 	udelay(50);
1139 
1140 	mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1141 	res = ATA_STAT_DRQ;
1142 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1143 
1144 	if ((c & mask) != res) {	/* DRQ must be 1, BSY 0 */
1145 		printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1146 		       device, c);
1147 		err = 0xFF;
1148 		goto AI_OUT;
1149 	}
1150 
1151 	/* write command block */
1152 	ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1153 
1154 	/* ATAPI Command written wait for completition */
1155 	udelay(5000);		/* device must set bsy */
1156 
1157 	mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1158 	/*
1159 	 * if no data wait for DRQ = 0 BSY = 0
1160 	 * if data wait for DRQ = 1 BSY = 0
1161 	 */
1162 	res = 0;
1163 	if (buflen)
1164 		res = ATA_STAT_DRQ;
1165 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1166 	if ((c & mask) != res) {
1167 		if (c & ATA_STAT_ERR) {
1168 			err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1169 			debug("atapi_issue 1 returned sense key %X status %02X\n",
1170 			      err, c);
1171 		} else {
1172 			printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x)  status 0x%02x\n",
1173 			       ccb[0], c);
1174 			err = 0xFF;
1175 		}
1176 		goto AI_OUT;
1177 	}
1178 	n = ide_inb(device, ATA_CYL_HIGH);
1179 	n <<= 8;
1180 	n += ide_inb(device, ATA_CYL_LOW);
1181 	if (n > buflen) {
1182 		printf("ERROR, transfer bytes %d requested only %d\n", n,
1183 		       buflen);
1184 		err = 0xff;
1185 		goto AI_OUT;
1186 	}
1187 	if ((n == 0) && (buflen < 0)) {
1188 		printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1189 		err = 0xff;
1190 		goto AI_OUT;
1191 	}
1192 	if (n != buflen) {
1193 		debug("WARNING, transfer bytes %d not equal with requested %d\n",
1194 		      n, buflen);
1195 	}
1196 	if (n != 0) {		/* data transfer */
1197 		debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1198 		/* we transfer shorts */
1199 		n >>= 1;
1200 		/* ok now decide if it is an in or output */
1201 		if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1202 			debug("Write to device\n");
1203 			ide_output_data_shorts(device,
1204 				(unsigned short *) buffer, n);
1205 		} else {
1206 			debug("Read from device @ %p shorts %d\n", buffer, n);
1207 			ide_input_data_shorts(device,
1208 				(unsigned short *) buffer, n);
1209 		}
1210 	}
1211 	udelay(5000);		/* seems that some CD ROMs need this... */
1212 	mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1213 	res = 0;
1214 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1215 	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1216 		err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1217 		debug("atapi_issue 2 returned sense key %X status %X\n", err,
1218 		      c);
1219 	} else {
1220 		err = 0;
1221 	}
1222 AI_OUT:
1223 	ide_led(DEVICE_LED(device), 0);	/* LED off      */
1224 	return err;
1225 }
1226 
1227 /*
1228  * sending the command to atapi_issue. If an status other than good
1229  * returns, an request_sense will be issued
1230  */
1231 
1232 #define ATAPI_DRIVE_NOT_READY	100
1233 #define ATAPI_UNIT_ATTN		10
1234 
1235 unsigned char atapi_issue_autoreq(int device,
1236 				  unsigned char *ccb,
1237 				  int ccblen,
1238 				  unsigned char *buffer, int buflen)
1239 {
1240 	unsigned char sense_data[18], sense_ccb[12];
1241 	unsigned char res, key, asc, ascq;
1242 	int notready, unitattn;
1243 
1244 	unitattn = ATAPI_UNIT_ATTN;
1245 	notready = ATAPI_DRIVE_NOT_READY;
1246 
1247 retry:
1248 	res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1249 	if (res == 0)
1250 		return 0;	/* Ok */
1251 
1252 	if (res == 0xFF)
1253 		return 0xFF;	/* error */
1254 
1255 	debug("(auto_req)atapi_issue returned sense key %X\n", res);
1256 
1257 	memset(sense_ccb, 0, sizeof(sense_ccb));
1258 	memset(sense_data, 0, sizeof(sense_data));
1259 	sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1260 	sense_ccb[4] = 18;	/* allocation Length */
1261 
1262 	res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1263 	key = (sense_data[2] & 0xF);
1264 	asc = (sense_data[12]);
1265 	ascq = (sense_data[13]);
1266 
1267 	debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1268 	debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1269 	      sense_data[0], key, asc, ascq);
1270 
1271 	if ((key == 0))
1272 		return 0;	/* ok device ready */
1273 
1274 	if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1275 		if (unitattn-- > 0) {
1276 			udelay(200 * 1000);
1277 			goto retry;
1278 		}
1279 		printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
1280 		goto error;
1281 	}
1282 	if ((asc == 0x4) && (ascq == 0x1)) {
1283 		/* not ready, but will be ready soon */
1284 		if (notready-- > 0) {
1285 			udelay(200 * 1000);
1286 			goto retry;
1287 		}
1288 		printf("Drive not ready, tried %d times\n",
1289 		       ATAPI_DRIVE_NOT_READY);
1290 		goto error;
1291 	}
1292 	if (asc == 0x3a) {
1293 		debug("Media not present\n");
1294 		goto error;
1295 	}
1296 
1297 	printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1298 	       ascq);
1299 error:
1300 	debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
1301 	return 0xFF;
1302 }
1303 
1304 
1305 static void atapi_inquiry(struct blk_desc *dev_desc)
1306 {
1307 	unsigned char ccb[12];	/* Command descriptor block */
1308 	unsigned char iobuf[64];	/* temp buf */
1309 	unsigned char c;
1310 	int device;
1311 
1312 	device = dev_desc->devnum;
1313 	dev_desc->type = DEV_TYPE_UNKNOWN;	/* not yet valid */
1314 	dev_desc->block_read = atapi_read;
1315 
1316 	memset(ccb, 0, sizeof(ccb));
1317 	memset(iobuf, 0, sizeof(iobuf));
1318 
1319 	ccb[0] = ATAPI_CMD_INQUIRY;
1320 	ccb[4] = 40;		/* allocation Legnth */
1321 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
1322 
1323 	debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1324 	if (c != 0)
1325 		return;
1326 
1327 	/* copy device ident strings */
1328 	ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1329 	ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1330 	ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
1331 
1332 	dev_desc->lun = 0;
1333 	dev_desc->lba = 0;
1334 	dev_desc->blksz = 0;
1335 	dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
1336 	dev_desc->type = iobuf[0] & 0x1f;
1337 
1338 	if ((iobuf[1] & 0x80) == 0x80)
1339 		dev_desc->removable = 1;
1340 	else
1341 		dev_desc->removable = 0;
1342 
1343 	memset(ccb, 0, sizeof(ccb));
1344 	memset(iobuf, 0, sizeof(iobuf));
1345 	ccb[0] = ATAPI_CMD_START_STOP;
1346 	ccb[4] = 0x03;		/* start */
1347 
1348 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1349 
1350 	debug("ATAPI_CMD_START_STOP returned %x\n", c);
1351 	if (c != 0)
1352 		return;
1353 
1354 	memset(ccb, 0, sizeof(ccb));
1355 	memset(iobuf, 0, sizeof(iobuf));
1356 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
1357 
1358 	debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1359 	if (c != 0)
1360 		return;
1361 
1362 	memset(ccb, 0, sizeof(ccb));
1363 	memset(iobuf, 0, sizeof(iobuf));
1364 	ccb[0] = ATAPI_CMD_READ_CAP;
1365 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1366 	debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1367 	if (c != 0)
1368 		return;
1369 
1370 	debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1371 	      iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1372 	      iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
1373 
1374 	dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1375 		((unsigned long) iobuf[1] << 16) +
1376 		((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1377 	dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1378 		((unsigned long) iobuf[5] << 16) +
1379 		((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1380 	dev_desc->log2blksz = LOG2(dev_desc->blksz);
1381 #ifdef CONFIG_LBA48
1382 	/* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1383 	dev_desc->lba48 = 0;
1384 #endif
1385 	return;
1386 }
1387 
1388 
1389 /*
1390  * atapi_read:
1391  * we transfer only one block per command, since the multiple DRQ per
1392  * command is not yet implemented
1393  */
1394 #define ATAPI_READ_MAX_BYTES	2048	/* we read max 2kbytes */
1395 #define ATAPI_READ_BLOCK_SIZE	2048	/* assuming CD part */
1396 #define ATAPI_READ_MAX_BLOCK	(ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
1397 
1398 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1399 		 void *buffer)
1400 {
1401 	int device = block_dev->devnum;
1402 	ulong n = 0;
1403 	unsigned char ccb[12];	/* Command descriptor block */
1404 	ulong cnt;
1405 
1406 	debug("atapi_read dev %d start " LBAF " blocks " LBAF
1407 	      " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
1408 
1409 	do {
1410 		if (blkcnt > ATAPI_READ_MAX_BLOCK)
1411 			cnt = ATAPI_READ_MAX_BLOCK;
1412 		else
1413 			cnt = blkcnt;
1414 
1415 		ccb[0] = ATAPI_CMD_READ_12;
1416 		ccb[1] = 0;	/* reserved */
1417 		ccb[2] = (unsigned char) (blknr >> 24) & 0xFF;	/* MSB Block */
1418 		ccb[3] = (unsigned char) (blknr >> 16) & 0xFF;	/*  */
1419 		ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1420 		ccb[5] = (unsigned char) blknr & 0xFF;	/* LSB Block */
1421 		ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1422 		ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1423 		ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1424 		ccb[9] = (unsigned char) cnt & 0xFF;	/* LSB Block */
1425 		ccb[10] = 0;	/* reserved */
1426 		ccb[11] = 0;	/* reserved */
1427 
1428 		if (atapi_issue_autoreq(device, ccb, 12,
1429 					(unsigned char *) buffer,
1430 					cnt * ATAPI_READ_BLOCK_SIZE)
1431 		    == 0xFF) {
1432 			return n;
1433 		}
1434 		n += cnt;
1435 		blkcnt -= cnt;
1436 		blknr += cnt;
1437 		buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
1438 	} while (blkcnt > 0);
1439 	return n;
1440 }
1441 
1442 /* ------------------------------------------------------------------------- */
1443 
1444 #endif /* CONFIG_ATAPI */
1445 
1446 U_BOOT_CMD(ide, 5, 1, do_ide,
1447 	   "IDE sub-system",
1448 	   "reset - reset IDE controller\n"
1449 	   "ide info  - show available IDE devices\n"
1450 	   "ide device [dev] - show or set current device\n"
1451 	   "ide part [dev] - print partition table of one or all IDE devices\n"
1452 	   "ide read  addr blk# cnt\n"
1453 	   "ide write addr blk# cnt - read/write `cnt'"
1454 	   " blocks starting at block `blk#'\n"
1455 	   "    to/from memory address `addr'");
1456 
1457 U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1458 	   "boot from IDE device", "loadAddr dev:part");
1459