cmd_ide.c 39.6 KB
Newer Older
W
wdenk 已提交
1
/*
2
 * (C) Copyright 2000-2011
W
wdenk 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 */

/*
 * IDE support
 */
28

W
wdenk 已提交
29 30 31 32 33 34
#include <common.h>
#include <config.h>
#include <watchdog.h>
#include <command.h>
#include <image.h>
#include <asm/byteorder.h>
35
#include <asm/io.h>
36

W
wdenk 已提交
37 38 39
#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
# include <pcmcia.h>
#endif
40

W
wdenk 已提交
41 42 43
#ifdef CONFIG_8xx
# include <mpc8xx.h>
#endif
44

W
wdenk 已提交
45 46 47
#ifdef CONFIG_MPC5xxx
#include <mpc5xxx.h>
#endif
48

W
wdenk 已提交
49 50
#include <ide.h>
#include <ata.h>
51

W
wdenk 已提交
52 53 54
#ifdef CONFIG_STATUS_LED
# include <status_led.h>
#endif
55

56 57
#ifdef __PPC__
# define EIEIO		__asm__ volatile ("eieio")
58
# define SYNC		__asm__ volatile ("sync")
59 60
#else
# define EIEIO		/* nothing */
61
# define SYNC		/* nothing */
W
wdenk 已提交
62 63 64 65 66 67 68 69
#endif

/* ------------------------------------------------------------------------- */

/* Current I/O Device	*/
static int curr_device = -1;

/* Current offset for IDE0 / IDE1 bus access	*/
70 71 72
ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
	CONFIG_SYS_ATA_IDE0_OFFSET,
W
wdenk 已提交
73
#endif
74 75
#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
	CONFIG_SYS_ATA_IDE1_OFFSET,
W
wdenk 已提交
76 77 78
#endif
};

79
static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
W
wdenk 已提交
80

81
block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
W
wdenk 已提交
82 83 84
/* ------------------------------------------------------------------------- */

#ifdef CONFIG_IDE_LED
W
Wolfgang Denk 已提交
85 86 87 88
# if !defined(CONFIG_BMS2003)	&& \
     !defined(CONFIG_CPC45)	&& \
     !defined(CONFIG_KUP4K) && \
     !defined(CONFIG_KUP4X)
W
wdenk 已提交
89 90
static void  ide_led   (uchar led, uchar status);
#else
91 92 93
extern void  ide_led   (uchar led, uchar status);
#endif
#else
W
wdenk 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#define ide_led(a,b)	/* dummy */
#endif

#ifdef CONFIG_IDE_RESET
static void  ide_reset (void);
#else
#define ide_reset()	/* dummy */
#endif

static void  ide_ident (block_dev_desc_t *dev_desc);
static uchar ide_wait  (int dev, ulong t);

#define IDE_TIME_OUT	2000	/* 2 sec timeout */

#define ATAPI_TIME_OUT	7000	/* 7 sec timeout (5 sec seems to work...) */

#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */

static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);

114 115
#ifndef CONFIG_SYS_ATA_PORT_ADDR
#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
116
#endif
W
wdenk 已提交
117 118 119

#ifdef CONFIG_ATAPI
static void	atapi_inquiry(block_dev_desc_t *dev_desc);
120
ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
W
wdenk 已提交
121 122 123 124 125
#endif


/* ------------------------------------------------------------------------- */

126
int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
W
wdenk 已提交
127
{
128 129 130 131 132
	int rcode = 0;

	switch (argc) {
	case 0:
	case 1:
133
		return CMD_RET_USAGE;
134 135 136
	case 2:
		if (strncmp(argv[1], "res", 3) == 0) {
			puts("\nReset IDE"
W
wdenk 已提交
137
#ifdef CONFIG_IDE_8xx_DIRECT
138
			     " on PCMCIA " PCMCIA_SLOT_MSG
W
wdenk 已提交
139
#endif
140
			     ": ");
W
wdenk 已提交
141

142 143 144 145
			ide_init();
			return 0;
		} else if (strncmp(argv[1], "inf", 3) == 0) {
			int i;
W
wdenk 已提交
146

147
			putc('\n');
W
wdenk 已提交
148

149 150 151 152 153 154 155
			for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
				if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
					continue;  /* list only known devices */
				printf("IDE device %d: ", i);
				dev_print(&ide_dev_desc[i]);
			}
			return 0;
W
wdenk 已提交
156

157 158 159 160 161
		} else if (strncmp(argv[1], "dev", 3) == 0) {
			if ((curr_device < 0)
			    || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
				puts("\nno IDE devices available\n");
				return 1;
W
wdenk 已提交
162
			}
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
			printf("\nIDE device %d: ", curr_device);
			dev_print(&ide_dev_desc[curr_device]);
			return 0;
		} else if (strncmp(argv[1], "part", 4) == 0) {
			int dev, ok;

			for (ok = 0, dev = 0;
			     dev < CONFIG_SYS_IDE_MAXDEVICE;
			     ++dev) {
				if (ide_dev_desc[dev].part_type !=
				    PART_TYPE_UNKNOWN) {
					++ok;
					if (dev)
						putc('\n');
					print_part(&ide_dev_desc[dev]);
				}
			}
			if (!ok) {
				puts("\nno IDE devices available\n");
				rcode++;
			}
			return rcode;
W
wdenk 已提交
185
		}
186
		return CMD_RET_USAGE;
187 188 189
	case 3:
		if (strncmp(argv[1], "dev", 3) == 0) {
			int dev = (int) simple_strtoul(argv[2], NULL, 10);
W
wdenk 已提交
190

191 192 193 194 195 196 197
			printf("\nIDE device %d: ", dev);
			if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
				puts("unknown device\n");
				return 1;
			}
			dev_print(&ide_dev_desc[dev]);
			/*ide_print (dev); */
W
wdenk 已提交
198

199 200
			if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
				return 1;
W
wdenk 已提交
201

202
			curr_device = dev;
W
wdenk 已提交
203

204 205 206 207 208
			puts("... is now current device\n");

			return 0;
		} else if (strncmp(argv[1], "part", 4) == 0) {
			int dev = (int) simple_strtoul(argv[2], NULL, 10);
W
wdenk 已提交
209

210
			if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
W
wdenk 已提交
211
				print_part(&ide_dev_desc[dev]);
212 213 214 215 216 217
			} else {
				printf("\nIDE device %d not available\n",
				       dev);
				rcode = 1;
			}
			return rcode;
W
wdenk 已提交
218 219
		}

220
		return CMD_RET_USAGE;
221 222
	default:
		/* at least 4 args */
W
wdenk 已提交
223

224 225 226 227
		if (strcmp(argv[1], "read") == 0) {
			ulong addr = simple_strtoul(argv[2], NULL, 16);
			ulong cnt = simple_strtoul(argv[4], NULL, 16);
			ulong n;
W
wdenk 已提交
228

229
#ifdef CONFIG_SYS_64BIT_LBA
230
			lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
W
wdenk 已提交
231

232 233
			printf("\nIDE read: device %d block # %lld, count %ld ... ",
				curr_device, blk, cnt);
234
#else
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
			lbaint_t blk = simple_strtoul(argv[3], NULL, 16);

			printf("\nIDE read: device %d block # %ld, count %ld ... ",
				curr_device, blk, cnt);
#endif

			n = ide_dev_desc[curr_device].block_read(curr_device,
								 blk, cnt,
								 (ulong *)addr);
			/* flush cache after read */
			flush_cache(addr,
				    cnt * ide_dev_desc[curr_device].blksz);

			printf("%ld blocks read: %s\n",
			       n, (n == cnt) ? "OK" : "ERROR");
			if (n == cnt)
				return 0;
			else
				return 1;
		} else if (strcmp(argv[1], "write") == 0) {
			ulong addr = simple_strtoul(argv[2], NULL, 16);
			ulong cnt = simple_strtoul(argv[4], NULL, 16);
			ulong n;
W
wdenk 已提交
258

259
#ifdef CONFIG_SYS_64BIT_LBA
260
			lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
W
wdenk 已提交
261

262 263
			printf("\nIDE write: device %d block # %lld, count %ld ... ",
				curr_device, blk, cnt);
264
#else
265
			lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
266

267 268
			printf("\nIDE write: device %d block # %ld, count %ld ... ",
				curr_device, blk, cnt);
269
#endif
270
			n = ide_write(curr_device, blk, cnt, (ulong *) addr);
W
wdenk 已提交
271

272 273 274 275 276 277 278
			printf("%ld blocks written: %s\n",
				n, (n == cnt) ? "OK" : "ERROR");
			if (n == cnt)
				return 0;
			else
				return 1;
		} else {
279
			return CMD_RET_USAGE;
280
		}
W
wdenk 已提交
281

282
		return rcode;
W
wdenk 已提交
283 284 285
	}
}

286
int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
W
wdenk 已提交
287
{
288
	return common_diskboot(cmdtp, "ide", argc, argv);
W
wdenk 已提交
289 290 291 292
}

/* ------------------------------------------------------------------------- */

293
inline void __ide_outb(int dev, int port, unsigned char val)
S
Stefan Roese 已提交
294
{
295 296 297
	debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
	      dev, port, val,
	      (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
298 299 300 301 302 303 304 305 306 307

#if defined(CONFIG_IDE_AHB)
	if (port) {
		/* write command */
		ide_write_register(dev, port, val);
	} else {
		/* write data */
		outb(val, (ATA_CURR_BASE(dev)));
	}
#else
308
	outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
309
#endif
S
Stefan Roese 已提交
310
}
311

312 313
void ide_outb(int dev, int port, unsigned char val)
	__attribute__ ((weak, alias("__ide_outb")));
S
Stefan Roese 已提交
314

315
inline unsigned char __ide_inb(int dev, int port)
S
Stefan Roese 已提交
316 317
{
	uchar val;
318 319 320 321

#if defined(CONFIG_IDE_AHB)
	val = ide_read_register(dev, port);
#else
322
	val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
323 324
#endif

325 326 327
	debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
	      dev, port,
	      (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
S
Stefan Roese 已提交
328 329
	return val;
}
330

331
unsigned char ide_inb(int dev, int port)
332
	__attribute__ ((weak, alias("__ide_inb")));
S
Stefan Roese 已提交
333

334
#ifdef CONFIG_TUNE_PIO
335
inline int __ide_set_piomode(int pio_mode)
336 337 338
{
	return 0;
}
339 340 341

inline int ide_set_piomode(int pio_mode)
	__attribute__ ((weak, alias("__ide_set_piomode")));
342 343
#endif

344
void ide_init(void)
W
wdenk 已提交
345 346 347
{
	unsigned char c;
	int i, bus;
348

W
wdenk 已提交
349
#ifdef CONFIG_IDE_8xx_PCCARD
350 351
	extern int ide_devices_found;	/* Initialized in check_ide_device() */
#endif /* CONFIG_IDE_8xx_PCCARD */
W
wdenk 已提交
352 353 354 355

#ifdef CONFIG_IDE_PREINIT
	WATCHDOG_RESET();

356 357
	if (ide_preinit()) {
		puts("ide_preinit failed\n");
W
wdenk 已提交
358 359
		return;
	}
360
#endif /* CONFIG_IDE_PREINIT */
W
wdenk 已提交
361 362 363

	WATCHDOG_RESET();

364 365
	/*
	 * Reset the IDE just to be sure.
W
wdenk 已提交
366 367
	 * Light LED's to show
	 */
368 369 370 371
	ide_led((LED_IDE1 | LED_IDE2), 1);	/* LED's on     */

	/* ATAPI Drives seems to need a proper IDE Reset */
	ide_reset();
W
wdenk 已提交
372

P
Pavel Herrmann 已提交
373 374
#ifdef CONFIG_IDE_INIT_POSTRESET
	WATCHDOG_RESET();
W
wdenk 已提交
375

P
Pavel Herrmann 已提交
376 377 378 379 380
	if (ide_init_postreset()) {
		puts("ide_preinit_postreset failed\n");
		return;
	}
#endif /* CONFIG_IDE_INIT_POSTRESET */
W
wdenk 已提交
381 382 383 384 385

	/*
	 * Wait for IDE to get ready.
	 * According to spec, this can take up to 31 seconds!
	 */
386 387 388 389
	for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
		int dev =
			bus * (CONFIG_SYS_IDE_MAXDEVICE /
			       CONFIG_SYS_IDE_MAXBUS);
W
wdenk 已提交
390

391 392 393
#ifdef CONFIG_IDE_8xx_PCCARD
		/* Skip non-ide devices from probing */
		if ((ide_devices_found & (1 << bus)) == 0) {
394
			ide_led((LED_IDE1 | LED_IDE2), 0);	/* LED's off */
395 396 397
			continue;
		}
#endif
398
		printf("Bus %d: ", bus);
W
wdenk 已提交
399 400 401 402 403

		ide_bus_ok[bus] = 0;

		/* Select device
		 */
404 405 406
		udelay(100000);	/* 100 ms */
		ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
		udelay(100000);	/* 100 ms */
W
wdenk 已提交
407 408
		i = 0;
		do {
409
			udelay(10000);	/* 10 ms */
W
wdenk 已提交
410

411
			c = ide_inb(dev, ATA_STATUS);
W
wdenk 已提交
412 413
			i++;
			if (i > (ATA_RESET_TIME * 100)) {
414 415 416
				puts("** Timeout **\n");
				/* LED's off */
				ide_led((LED_IDE1 | LED_IDE2), 0);
W
wdenk 已提交
417 418
				return;
			}
419 420 421
			if ((i >= 100) && ((i % 100) == 0))
				putc('.');

W
wdenk 已提交
422 423 424
		} while (c & ATA_STAT_BUSY);

		if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
425 426 427 428 429 430
			puts("not available  ");
			debug("Status = 0x%02X ", c);
#ifndef CONFIG_ATAPI		/* ATAPI Devices do not set DRDY */
		} else if ((c & ATA_STAT_READY) == 0) {
			puts("not available  ");
			debug("Status = 0x%02X ", c);
W
wdenk 已提交
431 432
#endif
		} else {
433
			puts("OK ");
W
wdenk 已提交
434 435 436 437
			ide_bus_ok[bus] = 1;
		}
		WATCHDOG_RESET();
	}
W
wdenk 已提交
438

439
	putc('\n');
W
wdenk 已提交
440

441
	ide_led((LED_IDE1 | LED_IDE2), 0);	/* LED's off    */
W
wdenk 已提交
442 443

	curr_device = -1;
444
	for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
W
wdenk 已提交
445 446 447
#ifdef CONFIG_IDE_LED
		int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
#endif
448 449 450 451 452 453 454
		ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
		ide_dev_desc[i].if_type = IF_TYPE_IDE;
		ide_dev_desc[i].dev = i;
		ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
		ide_dev_desc[i].blksz = 0;
		ide_dev_desc[i].lba = 0;
		ide_dev_desc[i].block_read = ide_read;
455
		ide_dev_desc[i].block_write = ide_write;
W
wdenk 已提交
456 457
		if (!ide_bus_ok[IDE_BUS(i)])
			continue;
458
		ide_led(led, 1);	/* LED on       */
W
wdenk 已提交
459
		ide_ident(&ide_dev_desc[i]);
460
		ide_led(led, 0);	/* LED off      */
W
wdenk 已提交
461
		dev_print(&ide_dev_desc[i]);
462

W
wdenk 已提交
463
		if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
464 465
			/* initialize partition type */
			init_part(&ide_dev_desc[i]);
W
wdenk 已提交
466 467 468 469 470 471 472 473 474
			if (curr_device < 0)
				curr_device = i;
		}
	}
	WATCHDOG_RESET();
}

/* ------------------------------------------------------------------------- */

475
#ifdef CONFIG_PARTITIONS
476
block_dev_desc_t *ide_get_dev(int dev)
W
wdenk 已提交
477
{
478
	return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
W
wdenk 已提交
479
}
480
#endif
W
wdenk 已提交
481 482 483

/* ------------------------------------------------------------------------- */

484 485 486 487 488 489 490 491 492
void ide_input_swap_data(int dev, ulong *sect_buf, int words)
	__attribute__ ((weak, alias("__ide_input_swap_data")));

void ide_input_data(int dev, ulong *sect_buf, int words)
	__attribute__ ((weak, alias("__ide_input_data")));

void ide_output_data(int dev, const ulong *sect_buf, int words)
	__attribute__ ((weak, alias("__ide_output_data")));

W
wdenk 已提交
493 494
/* We only need to swap data if we are running on a big endian cpu. */
/* But Au1x00 cpu:s already swaps data in big endian mode! */
T
Tom Rini 已提交
495
#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
496 497 498 499
void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
{
	ide_input_data(dev, sect_buf, words);
}
W
wdenk 已提交
500
#else
501
void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
W
wdenk 已提交
502
{
503 504 505
	volatile ushort *pbuf =
		(ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
	ushort *dbuf = (ushort *) sect_buf;
506

507 508
	debug("in input swap data base for read is %lx\n",
	      (unsigned long) pbuf);
509 510

	while (words--) {
W
Wolfgang Denk 已提交
511
#ifdef __MIPS__
512 513
		*dbuf++ = swab16p((u16 *) pbuf);
		*dbuf++ = swab16p((u16 *) pbuf);
514 515 516
#elif defined(CONFIG_PCS440EP)
		*dbuf++ = *pbuf;
		*dbuf++ = *pbuf;
W
Wolfgang Denk 已提交
517
#else
518 519
		*dbuf++ = ld_le16(pbuf);
		*dbuf++ = ld_le16(pbuf);
W
Wolfgang Denk 已提交
520
#endif /* !MIPS */
521
	}
W
wdenk 已提交
522
}
523
#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
W
wdenk 已提交
524 525


A
Albert Aribaud 已提交
526
#if defined(CONFIG_IDE_SWAP_IO)
527
void __ide_output_data(int dev, const ulong *sect_buf, int words)
W
wdenk 已提交
528
{
529 530
	ushort *dbuf;
	volatile ushort *pbuf;
531

532 533
	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
	dbuf = (ushort *) sect_buf;
534
	while (words--) {
535 536 537 538 539 540 541
#if defined(CONFIG_PCS440EP)
		/* not tested, because CF was write protected */
		EIEIO;
		*pbuf = ld_le16(dbuf++);
		EIEIO;
		*pbuf = ld_le16(dbuf++);
#else
542 543 544 545
		EIEIO;
		*pbuf = *dbuf++;
		EIEIO;
		*pbuf = *dbuf++;
546
#endif
547
	}
W
wdenk 已提交
548
}
549
#else  /* ! CONFIG_IDE_SWAP_IO */
550
void __ide_output_data(int dev, const ulong *sect_buf, int words)
W
wdenk 已提交
551
{
552 553 554
#if defined(CONFIG_IDE_AHB)
	ide_write_data(dev, sect_buf, words);
#else
555
	outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
556
#endif
W
wdenk 已提交
557
}
558
#endif /* CONFIG_IDE_SWAP_IO */
W
wdenk 已提交
559

A
Albert Aribaud 已提交
560
#if defined(CONFIG_IDE_SWAP_IO)
561
void __ide_input_data(int dev, ulong *sect_buf, int words)
W
wdenk 已提交
562
{
563 564
	ushort *dbuf;
	volatile ushort *pbuf;
565

566 567
	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
	dbuf = (ushort *) sect_buf;
568 569 570 571

	debug("in input data base for read is %lx\n", (unsigned long) pbuf);

	while (words--) {
572 573 574 575 576 577
#if defined(CONFIG_PCS440EP)
		EIEIO;
		*dbuf++ = ld_le16(pbuf);
		EIEIO;
		*dbuf++ = ld_le16(pbuf);
#else
578
		EIEIO;
579
		*dbuf++ = *pbuf;
580
		EIEIO;
581
		*dbuf++ = *pbuf;
582
#endif
583
	}
W
wdenk 已提交
584
}
585
#else  /* ! CONFIG_IDE_SWAP_IO */
586
void __ide_input_data(int dev, ulong *sect_buf, int words)
W
wdenk 已提交
587
{
588 589 590
#if defined(CONFIG_IDE_AHB)
	ide_read_data(dev, sect_buf, words);
#else
591
	insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
592
#endif
W
wdenk 已提交
593 594
}

595
#endif /* CONFIG_IDE_SWAP_IO */
W
wdenk 已提交
596 597 598

/* -------------------------------------------------------------------------
 */
599
static void ide_ident(block_dev_desc_t *dev_desc)
W
wdenk 已提交
600 601
{
	unsigned char c;
602
	hd_driveid_t iop;
W
wdenk 已提交
603

604 605
#ifdef CONFIG_ATAPI
	int retries = 0;
W
wdenk 已提交
606 607
#endif

608 609 610 611
#ifdef CONFIG_TUNE_PIO
	int pio_mode;
#endif

W
wdenk 已提交
612 613 614 615 616
#if 0
	int mode, cycle_time;
#endif
	int device;

617 618 619 620
	device = dev_desc->dev;
	printf("  Device %d: ", device);

	ide_led(DEVICE_LED(device), 1);	/* LED on       */
W
wdenk 已提交
621 622
	/* Select device
	 */
623 624
	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
	dev_desc->if_type = IF_TYPE_IDE;
W
wdenk 已提交
625
#ifdef CONFIG_ATAPI
W
wdenk 已提交
626

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	retries = 0;

	/* Warning: This will be tricky to read */
	while (retries <= 1) {
		/* check signature */
		if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
		    (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
		    (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
		    (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
			/* ATAPI Signature found */
			dev_desc->if_type = IF_TYPE_ATAPI;
			/*
			 * Start Ident Command
			 */
			ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
			/*
			 * Wait for completion - ATAPI devices need more time
			 * to become ready
			 */
			c = ide_wait(device, ATAPI_TIME_OUT);
		} else
W
wdenk 已提交
648
#endif
649
		{
650 651 652 653 654 655 656 657 658
			/*
			 * Start Ident Command
			 */
			ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);

			/*
			 * Wait for completion
			 */
			c = ide_wait(device, IDE_TIME_OUT);
659
		}
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
		ide_led(DEVICE_LED(device), 0);	/* LED off      */

		if (((c & ATA_STAT_DRQ) == 0) ||
		    ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
#ifdef CONFIG_ATAPI
			{
				/*
				 * Need to soft reset the device
				 * in case it's an ATAPI...
				 */
				debug("Retrying...\n");
				ide_outb(device, ATA_DEV_HD,
					 ATA_LBA | ATA_DEVICE(device));
				udelay(100000);
				ide_outb(device, ATA_COMMAND, 0x08);
				udelay(500000);	/* 500 ms */
			}
			/*
			 * Select device
			 */
			ide_outb(device, ATA_DEV_HD,
				 ATA_LBA | ATA_DEVICE(device));
			retries++;
683
#else
684
			return;
685
#endif
686
		}
687
#ifdef CONFIG_ATAPI
688 689 690
		else
			break;
	}			/* see above - ugly to read */
691

692
	if (retries == 2)	/* Not found */
693 694
		return;
#endif
W
wdenk 已提交
695

696
	ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
W
wdenk 已提交
697

698 699 700 701 702 703
	ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
		  sizeof(dev_desc->revision));
	ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
		  sizeof(dev_desc->vendor));
	ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
		  sizeof(dev_desc->product));
W
wdenk 已提交
704 705
#ifdef __LITTLE_ENDIAN
	/*
706 707
	 * firmware revision, model, and serial number have Big Endian Byte
	 * order in Word. Convert all three to little endian.
W
wdenk 已提交
708 709
	 *
	 * See CF+ and CompactFlash Specification Revision 2.0:
710
	 * 6.2.1.6: Identify Drive, Table 39 for more details
W
wdenk 已提交
711 712
	 */

713 714 715
	strswab(dev_desc->revision);
	strswab(dev_desc->vendor);
	strswab(dev_desc->product);
W
wdenk 已提交
716
#endif /* __LITTLE_ENDIAN */
W
wdenk 已提交
717

718
	if ((iop.config & 0x0080) == 0x0080)
W
wdenk 已提交
719 720 721 722
		dev_desc->removable = 1;
	else
		dev_desc->removable = 0;

723 724
#ifdef CONFIG_TUNE_PIO
	/* Mode 0 - 2 only, are directly determined by word 51. */
725
	pio_mode = iop.tPIO;
726 727
	if (pio_mode > 2) {
		printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
728 729
		/* Force it to dead slow, and hope for the best... */
		pio_mode = 0;
730 731 732 733 734 735
	}

	/* Any CompactFlash Storage Card that supports PIO mode 3 or above
	 * shall set bit 1 of word 53 to one and support the fields contained
	 * in words 64 through 70.
	 */
736
	if (iop.field_valid & 0x02) {
737 738
		/*
		 * Mode 3 and above are possible.  Check in order from slow
739 740
		 * to fast, so we wind up with the highest mode allowed.
		 */
741
		if (iop.eide_pio_modes & 0x01)
742
			pio_mode = 3;
743
		if (iop.eide_pio_modes & 0x02)
744
			pio_mode = 4;
745 746
		if (ata_id_is_cfa((u16 *)&iop)) {
			if ((iop.cf_advanced_caps & 0x07) == 0x01)
747
				pio_mode = 5;
748
			if ((iop.cf_advanced_caps & 0x07) == 0x02)
749 750 751 752 753 754 755 756
				pio_mode = 6;
		}
	}

	/* System-specific, depends on bus speeds, etc. */
	ide_set_piomode(pio_mode);
#endif /* CONFIG_TUNE_PIO */

W
wdenk 已提交
757 758 759 760
#if 0
	/*
	 * Drive PIO mode autoselection
	 */
761
	mode = iop.tPIO;
W
wdenk 已提交
762

763
	printf("tPIO = 0x%02x = %d\n", mode, mode);
W
wdenk 已提交
764 765
	if (mode > 2) {		/* 2 is maximum allowed tPIO value */
		mode = 2;
766
		debug("Override tPIO -> 2\n");
W
wdenk 已提交
767
	}
768
	if (iop.field_valid & 2) {	/* drive implements ATA2? */
769
		debug("Drive implements ATA2\n");
770 771
		if (iop.capability & 8) {	/* drive supports use_iordy? */
			cycle_time = iop.eide_pio_iordy;
W
wdenk 已提交
772
		} else {
773
			cycle_time = iop.eide_pio;
W
wdenk 已提交
774
		}
775
		debug("cycle time = %d\n", cycle_time);
W
wdenk 已提交
776
		mode = 4;
777 778 779 780 781 782 783 784
		if (cycle_time > 120)
			mode = 3;	/* 120 ns for PIO mode 4 */
		if (cycle_time > 180)
			mode = 2;	/* 180 ns for PIO mode 3 */
		if (cycle_time > 240)
			mode = 1;	/* 240 ns for PIO mode 4 */
		if (cycle_time > 383)
			mode = 0;	/* 383 ns for PIO mode 4 */
W
wdenk 已提交
785
	}
786
	printf("PIO mode to use: PIO %d\n", mode);
W
wdenk 已提交
787 788 789
#endif /* 0 */

#ifdef CONFIG_ATAPI
790
	if (dev_desc->if_type == IF_TYPE_ATAPI) {
W
wdenk 已提交
791 792 793 794 795
		atapi_inquiry(dev_desc);
		return;
	}
#endif /* CONFIG_ATAPI */

W
wdenk 已提交
796
#ifdef __BIG_ENDIAN
W
wdenk 已提交
797
	/* swap shorts */
798
	dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
799
#else  /* ! __BIG_ENDIAN */
W
wdenk 已提交
800 801 802 803 804 805
	/*
	 * do not swap shorts on little endian
	 *
	 * See CF+ and CompactFlash Specification Revision 2.0:
	 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
	 */
806
	dev_desc->lba = iop.lba_capacity;
807
#endif /* __BIG_ENDIAN */
W
wdenk 已提交
808

809
#ifdef CONFIG_LBA48
810
	if (iop.command_set_2 & 0x0400) {	/* LBA 48 support */
W
wdenk 已提交
811
		dev_desc->lba48 = 1;
812 813 814 815
		dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
			((unsigned long long) iop.lba48_capacity[1] << 16) |
			((unsigned long long) iop.lba48_capacity[2] << 32) |
			((unsigned long long) iop.lba48_capacity[3] << 48);
W
wdenk 已提交
816 817 818 819
	} else {
		dev_desc->lba48 = 0;
	}
#endif /* CONFIG_LBA48 */
W
wdenk 已提交
820
	/* assuming HD */
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	dev_desc->type = DEV_TYPE_HARDDISK;
	dev_desc->blksz = ATA_BLOCKSIZE;
	dev_desc->lun = 0;	/* just to fill something in... */

#if 0				/* only used to test the powersaving mode,
				 * if enabled, the drive goes after 5 sec
				 * in standby mode */
	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
	c = ide_wait(device, IDE_TIME_OUT);
	ide_outb(device, ATA_SECT_CNT, 1);
	ide_outb(device, ATA_LBA_LOW, 0);
	ide_outb(device, ATA_LBA_MID, 0);
	ide_outb(device, ATA_LBA_HIGH, 0);
	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
	ide_outb(device, ATA_COMMAND, 0xe3);
	udelay(50);
	c = ide_wait(device, IDE_TIME_OUT);	/* can't take over 500 ms */
W
wdenk 已提交
838 839 840 841 842 843
#endif
}


/* ------------------------------------------------------------------------- */

844
ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
W
wdenk 已提交
845 846 847
{
	ulong n = 0;
	unsigned char c;
848 849
	unsigned char pwrsave = 0;	/* power save */

850
#ifdef CONFIG_LBA48
W
wdenk 已提交
851
	unsigned char lba48 = 0;
W
wdenk 已提交
852

853
	if (blknr & 0x0000fffff0000000ULL) {
W
wdenk 已提交
854 855 856 857
		/* more than 28 bits used, use 48bit mode */
		lba48 = 1;
	}
#endif
M
Marek Vasut 已提交
858
	debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
859
	      device, blknr, blkcnt, (ulong) buffer);
W
wdenk 已提交
860

861
	ide_led(DEVICE_LED(device), 1);	/* LED on       */
W
wdenk 已提交
862 863 864

	/* Select device
	 */
865 866
	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
	c = ide_wait(device, IDE_TIME_OUT);
W
wdenk 已提交
867 868

	if (c & ATA_STAT_BUSY) {
869
		printf("IDE read: device %d not ready\n", device);
W
wdenk 已提交
870 871 872 873 874
		goto IDE_READ_E;
	}

	/* first check if the drive is in Powersaving mode, if yes,
	 * increase the timeout value */
875 876
	ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
	udelay(50);
W
wdenk 已提交
877

878
	c = ide_wait(device, IDE_TIME_OUT);	/* can't take over 500 ms */
W
wdenk 已提交
879 880

	if (c & ATA_STAT_BUSY) {
881
		printf("IDE read: device %d not ready\n", device);
W
wdenk 已提交
882 883 884
		goto IDE_READ_E;
	}
	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
885
		printf("No Powersaving mode %X\n", c);
W
wdenk 已提交
886
	} else {
887 888 889 890
		c = ide_inb(device, ATA_SECT_CNT);
		debug("Powersaving %02X\n", c);
		if (c == 0)
			pwrsave = 1;
W
wdenk 已提交
891 892 893 894 895
	}


	while (blkcnt-- > 0) {

896
		c = ide_wait(device, IDE_TIME_OUT);
W
wdenk 已提交
897 898

		if (c & ATA_STAT_BUSY) {
899
			printf("IDE read: device %d not ready\n", device);
W
wdenk 已提交
900 901
			break;
		}
902
#ifdef CONFIG_LBA48
W
wdenk 已提交
903 904
		if (lba48) {
			/* write high bits */
905 906
			ide_outb(device, ATA_SECT_CNT, 0);
			ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
907
#ifdef CONFIG_SYS_64BIT_LBA
908 909
			ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
			ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
910
#else
911 912
			ide_outb(device, ATA_LBA_MID, 0);
			ide_outb(device, ATA_LBA_HIGH, 0);
913
#endif
W
wdenk 已提交
914 915
		}
#endif
916 917 918 919
		ide_outb(device, ATA_SECT_CNT, 1);
		ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
		ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
		ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
W
wdenk 已提交
920

921
#ifdef CONFIG_LBA48
W
wdenk 已提交
922
		if (lba48) {
923 924 925
			ide_outb(device, ATA_DEV_HD,
				 ATA_LBA | ATA_DEVICE(device));
			ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
W
wdenk 已提交
926 927 928 929

		} else
#endif
		{
930 931 932
			ide_outb(device, ATA_DEV_HD, ATA_LBA |
				 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
			ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
W
wdenk 已提交
933
		}
W
wdenk 已提交
934

935
		udelay(50);
W
wdenk 已提交
936

937 938 939 940
		if (pwrsave) {
			/* may take up to 4 sec */
			c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
			pwrsave = 0;
W
wdenk 已提交
941
		} else {
942 943
			/* can't take over 500 ms */
			c = ide_wait(device, IDE_TIME_OUT);
W
wdenk 已提交
944 945
		}

946 947
		if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
		    ATA_STAT_DRQ) {
948
#if defined(CONFIG_SYS_64BIT_LBA)
949
			printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
W
wdenk 已提交
950
				device, blknr, c);
W
wdenk 已提交
951
#else
952 953
			printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
				device, (ulong) blknr, c);
W
wdenk 已提交
954
#endif
W
wdenk 已提交
955 956 957
			break;
		}

958
		ide_input_data(device, buffer, ATA_SECTORWORDS);
959
		(void) ide_inb(device, ATA_STATUS);	/* clear IRQ */
W
wdenk 已提交
960 961 962

		++n;
		++blknr;
963
		buffer += ATA_BLOCKSIZE;
W
wdenk 已提交
964 965
	}
IDE_READ_E:
966
	ide_led(DEVICE_LED(device), 0);	/* LED off      */
W
wdenk 已提交
967 968 969 970 971 972
	return (n);
}

/* ------------------------------------------------------------------------- */


973
ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
W
wdenk 已提交
974 975 976
{
	ulong n = 0;
	unsigned char c;
977

978
#ifdef CONFIG_LBA48
W
wdenk 已提交
979 980
	unsigned char lba48 = 0;

981
	if (blknr & 0x0000fffff0000000ULL) {
W
wdenk 已提交
982 983 984 985
		/* more than 28 bits used, use 48bit mode */
		lba48 = 1;
	}
#endif
W
wdenk 已提交
986

987
	ide_led(DEVICE_LED(device), 1);	/* LED on       */
W
wdenk 已提交
988 989 990

	/* Select device
	 */
991
	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
W
wdenk 已提交
992 993 994

	while (blkcnt-- > 0) {

995
		c = ide_wait(device, IDE_TIME_OUT);
W
wdenk 已提交
996 997

		if (c & ATA_STAT_BUSY) {
998
			printf("IDE read: device %d not ready\n", device);
W
wdenk 已提交
999 1000
			goto WR_OUT;
		}
1001
#ifdef CONFIG_LBA48
W
wdenk 已提交
1002 1003
		if (lba48) {
			/* write high bits */
1004 1005
			ide_outb(device, ATA_SECT_CNT, 0);
			ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1006
#ifdef CONFIG_SYS_64BIT_LBA
1007 1008
			ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
			ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1009
#else
1010 1011
			ide_outb(device, ATA_LBA_MID, 0);
			ide_outb(device, ATA_LBA_HIGH, 0);
1012
#endif
W
wdenk 已提交
1013 1014
		}
#endif
1015 1016 1017 1018
		ide_outb(device, ATA_SECT_CNT, 1);
		ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
		ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
		ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
W
wdenk 已提交
1019

1020
#ifdef CONFIG_LBA48
W
wdenk 已提交
1021
		if (lba48) {
1022 1023 1024
			ide_outb(device, ATA_DEV_HD,
				 ATA_LBA | ATA_DEVICE(device));
			ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
W
wdenk 已提交
1025 1026 1027 1028

		} else
#endif
		{
1029 1030 1031
			ide_outb(device, ATA_DEV_HD, ATA_LBA |
				 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
			ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
W
wdenk 已提交
1032
		}
W
wdenk 已提交
1033

1034
		udelay(50);
W
wdenk 已提交
1035

1036 1037
		/* can't take over 500 ms */
		c = ide_wait(device, IDE_TIME_OUT);
W
wdenk 已提交
1038

1039 1040
		if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
		    ATA_STAT_DRQ) {
1041
#if defined(CONFIG_SYS_64BIT_LBA)
1042
			printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
W
wdenk 已提交
1043
				device, blknr, c);
W
wdenk 已提交
1044
#else
1045 1046
			printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
				device, (ulong) blknr, c);
W
wdenk 已提交
1047
#endif
W
wdenk 已提交
1048 1049 1050
			goto WR_OUT;
		}

1051
		ide_output_data(device, buffer, ATA_SECTORWORDS);
1052
		c = ide_inb(device, ATA_STATUS);	/* clear IRQ */
W
wdenk 已提交
1053 1054
		++n;
		++blknr;
1055
		buffer += ATA_BLOCKSIZE;
W
wdenk 已提交
1056 1057
	}
WR_OUT:
1058
	ide_led(DEVICE_LED(device), 0);	/* LED off      */
W
wdenk 已提交
1059 1060 1061 1062 1063 1064 1065 1066
	return (n);
}

/* ------------------------------------------------------------------------- */

/*
 * copy src to dest, skipping leading and trailing blanks and null
 * terminate the string
W
wdenk 已提交
1067
 * "len" is the size of available memory including the terminating '\0'
W
wdenk 已提交
1068
 */
1069 1070
static void ident_cpy(unsigned char *dst, unsigned char *src,
		      unsigned int len)
W
wdenk 已提交
1071
{
W
wdenk 已提交
1072 1073 1074
	unsigned char *end, *last;

	last = dst;
1075
	end = src + len - 1;
W
wdenk 已提交
1076 1077 1078 1079

	/* reserve space for '\0' */
	if (len < 2)
		goto OUT;
1080

W
wdenk 已提交
1081
	/* skip leading white space */
1082
	while ((*src) && (src < end) && (*src == ' '))
W
wdenk 已提交
1083 1084 1085
		++src;

	/* copy string, omitting trailing white space */
1086
	while ((*src) && (src < end)) {
W
wdenk 已提交
1087 1088 1089
		*dst++ = *src;
		if (*src++ != ' ')
			last = dst;
W
wdenk 已提交
1090
	}
W
wdenk 已提交
1091 1092
OUT:
	*last = '\0';
W
wdenk 已提交
1093 1094 1095 1096 1097 1098 1099 1100
}

/* ------------------------------------------------------------------------- */

/*
 * Wait until Busy bit is off, or timeout (in ms)
 * Return last status
 */
1101
static uchar ide_wait(int dev, ulong t)
W
wdenk 已提交
1102
{
1103
	ulong delay = 10 * t;	/* poll every 100 us */
W
wdenk 已提交
1104 1105
	uchar c;

W
wdenk 已提交
1106
	while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
1107 1108
		udelay(100);
		if (delay-- == 0)
W
wdenk 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
			break;
	}
	return (c);
}

/* ------------------------------------------------------------------------- */

#ifdef CONFIG_IDE_RESET
extern void ide_set_reset(int idereset);

1119
static void ide_reset(void)
W
wdenk 已提交
1120 1121 1122 1123
{
	int i;

	curr_device = -1;
1124
	for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
W
wdenk 已提交
1125
		ide_bus_ok[i] = 0;
1126
	for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
W
wdenk 已提交
1127 1128
		ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;

1129
	ide_set_reset(1);	/* assert reset */
W
wdenk 已提交
1130

M
Martin Krause 已提交
1131 1132 1133
	/* the reset signal shall be asserted for et least 25 us */
	udelay(25);

W
wdenk 已提交
1134 1135 1136 1137 1138 1139
	WATCHDOG_RESET();

	/* de-assert RESET signal */
	ide_set_reset(0);

	/* wait 250 ms */
1140 1141
	for (i = 0; i < 250; ++i)
		udelay(1000);
W
wdenk 已提交
1142 1143
}

1144
#endif /* CONFIG_IDE_RESET */
W
wdenk 已提交
1145 1146 1147

/* ------------------------------------------------------------------------- */

W
wdenk 已提交
1148 1149 1150 1151
#if defined(CONFIG_IDE_LED)	&& \
   !defined(CONFIG_CPC45)	&& \
   !defined(CONFIG_KUP4K)	&& \
   !defined(CONFIG_KUP4X)
W
wdenk 已提交
1152

1153
static uchar led_buffer;	/* Buffer for current LED status        */
W
wdenk 已提交
1154

1155
static void ide_led(uchar led, uchar status)
W
wdenk 已提交
1156 1157 1158
{
	uchar *led_port = LED_PORT;

1159 1160 1161
	if (status)		/* switch LED on        */
		led_buffer |= led;
	else			/* switch LED off       */
W
wdenk 已提交
1162 1163 1164 1165 1166
		led_buffer &= ~led;

	*led_port = led_buffer;
}

1167
#endif /* CONFIG_IDE_LED */
W
wdenk 已提交
1168

1169 1170 1171 1172 1173 1174 1175 1176
#if defined(CONFIG_OF_IDE_FIXUP)
int ide_device_present(int dev)
{
	if (dev >= CONFIG_SYS_IDE_MAXBUS)
		return 0;
	return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
}
#endif
W
wdenk 已提交
1177 1178 1179 1180 1181 1182 1183
/* ------------------------------------------------------------------------- */

#ifdef CONFIG_ATAPI
/****************************************************************************
 * ATAPI Support
 */

1184 1185 1186 1187 1188 1189 1190
void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
	__attribute__ ((weak, alias("__ide_input_data_shorts")));

void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
	__attribute__ ((weak, alias("__ide_output_data_shorts")));


A
Albert Aribaud 已提交
1191
#if defined(CONFIG_IDE_SWAP_IO)
W
wdenk 已提交
1192 1193
/* since ATAPI may use commands with not 4 bytes alligned length
 * we have our own transfer functions, 2 bytes alligned */
1194
void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
W
wdenk 已提交
1195
{
1196 1197
	ushort *dbuf;
	volatile ushort *pbuf;
W
wdenk 已提交
1198

1199 1200
	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
	dbuf = (ushort *) sect_buf;
1201

1202 1203
	debug("in output data shorts base for read is %lx\n",
	      (unsigned long) pbuf);
1204

W
wdenk 已提交
1205
	while (shorts--) {
1206
		EIEIO;
1207
		*pbuf = *dbuf++;
W
wdenk 已提交
1208
	}
1209 1210
}

1211
void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1212
{
1213 1214
	ushort *dbuf;
	volatile ushort *pbuf;
1215

1216 1217
	pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
	dbuf = (ushort *) sect_buf;
1218

1219 1220
	debug("in input data shorts base for read is %lx\n",
	      (unsigned long) pbuf);
1221 1222 1223 1224 1225

	while (shorts--) {
		EIEIO;
		*dbuf++ = *pbuf;
	}
W
wdenk 已提交
1226 1227
}

1228
#else  /* ! CONFIG_IDE_SWAP_IO */
1229
void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
W
wdenk 已提交
1230
{
1231
	outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
W
wdenk 已提交
1232 1233
}

1234
void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
W
wdenk 已提交
1235
{
1236
	insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
W
wdenk 已提交
1237 1238
}

1239
#endif /* CONFIG_IDE_SWAP_IO */
W
wdenk 已提交
1240

W
wdenk 已提交
1241 1242 1243 1244 1245 1246
/*
 * Wait until (Status & mask) == res, or timeout (in ms)
 * Return last status
 * This is used since some ATAPI CD ROMs clears their Busy Bit first
 * and then they set their DRQ Bit
 */
1247
static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
W
wdenk 已提交
1248
{
1249
	ulong delay = 10 * t;	/* poll every 100 us */
W
wdenk 已提交
1250 1251
	uchar c;

1252 1253 1254
	/* prevents to read the status before valid */
	c = ide_inb(dev, ATA_DEV_CTL);

W
wdenk 已提交
1255
	while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
W
wdenk 已提交
1256
		/* break if error occurs (doesn't make sense to wait more) */
1257
		if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
W
wdenk 已提交
1258
			break;
1259 1260
		udelay(100);
		if (delay-- == 0)
W
wdenk 已提交
1261 1262 1263 1264 1265 1266 1267 1268
			break;
	}
	return (c);
}

/*
 * issue an atapi command
 */
1269 1270
unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
			  unsigned char *buffer, int buflen)
W
wdenk 已提交
1271
{
1272
	unsigned char c, err, mask, res;
W
wdenk 已提交
1273
	int n;
1274 1275

	ide_led(DEVICE_LED(device), 1);	/* LED on       */
W
wdenk 已提交
1276 1277 1278

	/* Select device
	 */
1279
	mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
W
wdenk 已提交
1280
	res = 0;
1281 1282
	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
W
wdenk 已提交
1283
	if ((c & mask) != res) {
1284 1285 1286
		printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
		       c);
		err = 0xFF;
W
wdenk 已提交
1287 1288 1289
		goto AI_OUT;
	}
	/* write taskfile */
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	ide_outb(device, ATA_ERROR_REG, 0);	/* no DMA, no overlaped */
	ide_outb(device, ATA_SECT_CNT, 0);
	ide_outb(device, ATA_SECT_NUM, 0);
	ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
	ide_outb(device, ATA_CYL_HIGH,
		 (unsigned char) ((buflen >> 8) & 0xFF));
	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));

	ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
	udelay(50);

	mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
W
wdenk 已提交
1302
	res = ATA_STAT_DRQ;
1303
	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
W
wdenk 已提交
1304

1305 1306 1307 1308
	if ((c & mask) != res) {	/* DRQ must be 1, BSY 0 */
		printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
			device, c);
		err = 0xFF;
W
wdenk 已提交
1309 1310 1311
		goto AI_OUT;
	}

1312
	/* write command block */
1313
	ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1314

W
Wolfgang Denk 已提交
1315
	/* ATAPI Command written wait for completition */
1316
	udelay(5000);		/* device must set bsy */
W
wdenk 已提交
1317

1318 1319 1320 1321 1322 1323 1324
	mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
	/*
	 * if no data wait for DRQ = 0 BSY = 0
	 * if data wait for DRQ = 1 BSY = 0
	 */
	res = 0;
	if (buflen)
W
wdenk 已提交
1325
		res = ATA_STAT_DRQ;
1326 1327
	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
	if ((c & mask) != res) {
W
wdenk 已提交
1328
		if (c & ATA_STAT_ERR) {
1329 1330 1331
			err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
			debug("atapi_issue 1 returned sense key %X status %02X\n",
				err, c);
W
wdenk 已提交
1332
		} else {
1333 1334 1335
			printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x)  status 0x%02x\n",
				ccb[0], c);
			err = 0xFF;
W
wdenk 已提交
1336 1337 1338
		}
		goto AI_OUT;
	}
1339 1340 1341 1342 1343 1344 1345
	n = ide_inb(device, ATA_CYL_HIGH);
	n <<= 8;
	n += ide_inb(device, ATA_CYL_LOW);
	if (n > buflen) {
		printf("ERROR, transfer bytes %d requested only %d\n", n,
		       buflen);
		err = 0xff;
W
wdenk 已提交
1346 1347
		goto AI_OUT;
	}
1348 1349 1350
	if ((n == 0) && (buflen < 0)) {
		printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
		err = 0xff;
W
wdenk 已提交
1351 1352
		goto AI_OUT;
	}
1353 1354 1355
	if (n != buflen) {
		debug("WARNING, transfer bytes %d not equal with requested %d\n",
			n, buflen);
W
wdenk 已提交
1356
	}
1357 1358 1359 1360
	if (n != 0) {		/* data transfer */
		debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
		/* we transfer shorts */
		n >>= 1;
W
wdenk 已提交
1361
		/* ok now decide if it is an in or output */
1362 1363
		if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
			debug("Write to device\n");
1364 1365
			ide_output_data_shorts(device,
				(unsigned short *) buffer, n);
W
wdenk 已提交
1366
		} else {
1367
			debug("Read from device @ %p shorts %d\n", buffer, n);
1368 1369
			ide_input_data_shorts(device,
				(unsigned short *) buffer, n);
W
wdenk 已提交
1370 1371
		}
	}
1372 1373 1374 1375
	udelay(5000);		/* seems that some CD ROMs need this... */
	mask = ATA_STAT_BUSY | ATA_STAT_ERR;
	res = 0;
	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
W
wdenk 已提交
1376
	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1377 1378 1379
		err = (ide_inb(device, ATA_ERROR_REG) >> 4);
		debug("atapi_issue 2 returned sense key %X status %X\n", err,
		      c);
W
wdenk 已提交
1380 1381 1382 1383
	} else {
		err = 0;
	}
AI_OUT:
1384
	ide_led(DEVICE_LED(device), 0);	/* LED off      */
W
wdenk 已提交
1385 1386 1387 1388 1389 1390 1391 1392
	return (err);
}

/*
 * sending the command to atapi_issue. If an status other than good
 * returns, an request_sense will be issued
 */

W
Wolfgang Denk 已提交
1393
#define ATAPI_DRIVE_NOT_READY	100
W
wdenk 已提交
1394 1395
#define ATAPI_UNIT_ATTN		10

1396 1397 1398 1399
unsigned char atapi_issue_autoreq(int device,
				  unsigned char *ccb,
				  int ccblen,
				  unsigned char *buffer, int buflen)
W
wdenk 已提交
1400
{
1401 1402 1403
	unsigned char sense_data[18], sense_ccb[12];
	unsigned char res, key, asc, ascq;
	int notready, unitattn;
W
wdenk 已提交
1404

1405 1406
	unitattn = ATAPI_UNIT_ATTN;
	notready = ATAPI_DRIVE_NOT_READY;
W
wdenk 已提交
1407 1408

retry:
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
	res = atapi_issue(device, ccb, ccblen, buffer, buflen);
	if (res == 0)
		return 0;	/* Ok */

	if (res == 0xFF)
		return 0xFF;	/* error */

	debug("(auto_req)atapi_issue returned sense key %X\n", res);

	memset(sense_ccb, 0, sizeof(sense_ccb));
	memset(sense_data, 0, sizeof(sense_data));
	sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
	sense_ccb[4] = 18;	/* allocation Length */

	res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
	key = (sense_data[2] & 0xF);
	asc = (sense_data[12]);
	ascq = (sense_data[13]);

	debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
	debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
	      sense_data[0], key, asc, ascq);

	if ((key == 0))
		return 0;	/* ok device ready */

	if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
		if (unitattn-- > 0) {
			udelay(200 * 1000);
W
wdenk 已提交
1438 1439
			goto retry;
		}
1440
		printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
W
wdenk 已提交
1441 1442
		goto error;
	}
1443 1444 1445 1446
	if ((asc == 0x4) && (ascq == 0x1)) {
		/* not ready, but will be ready soon */
		if (notready-- > 0) {
			udelay(200 * 1000);
W
wdenk 已提交
1447 1448
			goto retry;
		}
1449 1450
		printf("Drive not ready, tried %d times\n",
		       ATAPI_DRIVE_NOT_READY);
W
wdenk 已提交
1451 1452
		goto error;
	}
1453 1454
	if (asc == 0x3a) {
		debug("Media not present\n");
W
wdenk 已提交
1455 1456
		goto error;
	}
W
wdenk 已提交
1457

1458 1459
	printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
	       ascq);
W
wdenk 已提交
1460
error:
1461
	debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
W
wdenk 已提交
1462 1463 1464 1465
	return (0xFF);
}


1466
static void atapi_inquiry(block_dev_desc_t *dev_desc)
W
wdenk 已提交
1467
{
1468 1469
	unsigned char ccb[12];	/* Command descriptor block */
	unsigned char iobuf[64];	/* temp buf */
W
wdenk 已提交
1470 1471 1472
	unsigned char c;
	int device;

1473 1474 1475
	device = dev_desc->dev;
	dev_desc->type = DEV_TYPE_UNKNOWN;	/* not yet valid */
	dev_desc->block_read = atapi_read;
W
wdenk 已提交
1476

1477 1478
	memset(ccb, 0, sizeof(ccb));
	memset(iobuf, 0, sizeof(iobuf));
W
wdenk 已提交
1479

1480 1481 1482
	ccb[0] = ATAPI_CMD_INQUIRY;
	ccb[4] = 40;		/* allocation Legnth */
	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
W
wdenk 已提交
1483

1484 1485
	debug("ATAPI_CMD_INQUIRY returned %x\n", c);
	if (c != 0)
W
wdenk 已提交
1486 1487 1488
		return;

	/* copy device ident strings */
1489 1490 1491
	ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
	ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
	ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
W
wdenk 已提交
1492

1493 1494 1495 1496
	dev_desc->lun = 0;
	dev_desc->lba = 0;
	dev_desc->blksz = 0;
	dev_desc->type = iobuf[0] & 0x1f;
W
wdenk 已提交
1497

1498
	if ((iobuf[1] & 0x80) == 0x80)
W
wdenk 已提交
1499 1500 1501 1502
		dev_desc->removable = 1;
	else
		dev_desc->removable = 0;

1503 1504 1505 1506
	memset(ccb, 0, sizeof(ccb));
	memset(iobuf, 0, sizeof(iobuf));
	ccb[0] = ATAPI_CMD_START_STOP;
	ccb[4] = 0x03;		/* start */
W
wdenk 已提交
1507

1508
	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
W
wdenk 已提交
1509

1510 1511
	debug("ATAPI_CMD_START_STOP returned %x\n", c);
	if (c != 0)
W
wdenk 已提交
1512 1513
		return;

1514 1515 1516
	memset(ccb, 0, sizeof(ccb));
	memset(iobuf, 0, sizeof(iobuf));
	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
W
wdenk 已提交
1517

1518 1519
	debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
	if (c != 0)
W
wdenk 已提交
1520 1521
		return;

1522 1523 1524 1525 1526 1527
	memset(ccb, 0, sizeof(ccb));
	memset(iobuf, 0, sizeof(iobuf));
	ccb[0] = ATAPI_CMD_READ_CAP;
	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
	debug("ATAPI_CMD_READ_CAP returned %x\n", c);
	if (c != 0)
W
wdenk 已提交
1528 1529
		return;

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
	      iobuf[0], iobuf[1], iobuf[2], iobuf[3],
	      iobuf[4], iobuf[5], iobuf[6], iobuf[7]);

	dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
		((unsigned long) iobuf[1] << 16) +
		((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
	dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
		((unsigned long) iobuf[5] << 16) +
		((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
1540
#ifdef CONFIG_LBA48
1541 1542
	/* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
	dev_desc->lba48 = 0;
1543
#endif
W
wdenk 已提交
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
	return;
}


/*
 * atapi_read:
 * we transfer only one block per command, since the multiple DRQ per
 * command is not yet implemented
 */
#define ATAPI_READ_MAX_BYTES	2048	/* we read max 2kbytes */
#define ATAPI_READ_BLOCK_SIZE	2048	/* assuming CD part */
1555
#define ATAPI_READ_MAX_BLOCK	(ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
W
wdenk 已提交
1556

1557
ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
W
wdenk 已提交
1558 1559
{
	ulong n = 0;
1560
	unsigned char ccb[12];	/* Command descriptor block */
W
wdenk 已提交
1561 1562
	ulong cnt;

1563 1564
	debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
	      device, blknr, blkcnt, (ulong) buffer);
W
wdenk 已提交
1565 1566

	do {
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
		if (blkcnt > ATAPI_READ_MAX_BLOCK)
			cnt = ATAPI_READ_MAX_BLOCK;
		else
			cnt = blkcnt;

		ccb[0] = ATAPI_CMD_READ_12;
		ccb[1] = 0;	/* reserved */
		ccb[2] = (unsigned char) (blknr >> 24) & 0xFF;	/* MSB Block */
		ccb[3] = (unsigned char) (blknr >> 16) & 0xFF;	/*  */
		ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
		ccb[5] = (unsigned char) blknr & 0xFF;	/* LSB Block */
		ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
		ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
		ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
		ccb[9] = (unsigned char) cnt & 0xFF;	/* LSB Block */
		ccb[10] = 0;	/* reserved */
		ccb[11] = 0;	/* reserved */

		if (atapi_issue_autoreq(device, ccb, 12,
					(unsigned char *) buffer,
					cnt * ATAPI_READ_BLOCK_SIZE)
		    == 0xFF) {
W
wdenk 已提交
1589 1590
			return (n);
		}
1591 1592 1593 1594
		n += cnt;
		blkcnt -= cnt;
		blknr += cnt;
		buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
W
wdenk 已提交
1595 1596 1597 1598 1599 1600 1601 1602
	} while (blkcnt > 0);
	return (n);
}

/* ------------------------------------------------------------------------- */

#endif /* CONFIG_ATAPI */

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
U_BOOT_CMD(ide, 5, 1, do_ide,
	   "IDE sub-system",
	   "reset - reset IDE controller\n"
	   "ide info  - show available IDE devices\n"
	   "ide device [dev] - show or set current device\n"
	   "ide part [dev] - print partition table of one or all IDE devices\n"
	   "ide read  addr blk# cnt\n"
	   "ide write addr blk# cnt - read/write `cnt'"
	   " blocks starting at block `blk#'\n"
	   "    to/from memory address `addr'");

U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
	   "boot from IDE device", "loadAddr dev:part");