--- cmd_ide.c	2004-02-02 18:42:15.000000000 +0900
+++ ../../../bootloader/u-boot-298e-01252004/common/cmd_ide.c	2003-09-25 12:31:46.000000000 +0900
@@ -38,15 +38,12 @@
 # include <mpc8xx.h>
 #endif
 #include <ide.h>
+#include <ata.h>
 #ifdef CONFIG_STATUS_LED
 # include <status_led.h>
 #endif
-#ifndef __PPC__
+#ifdef __I386__
 #include <asm/io.h>
-#ifdef __MIPS__
-/* Macros depend on this variable */
-static unsigned long mips_io_port_base = 0;
-#endif
 #endif
 
 #ifdef CONFIG_SHOW_BOOT_PROGRESS
@@ -68,7 +65,6 @@
 
 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
 
-#ifdef CONFIG_IDE_8xx_DIRECT
 /* Timings for IDE Interface
  *
  * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
@@ -105,8 +101,6 @@
 
 #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
 
-#endif /* CONFIG_IDE_8xx_DIRECT */
-
 /* ------------------------------------------------------------------------- */
 
 /* Current I/O Device	*/
@@ -122,15 +116,11 @@
 #endif
 };
 
-ulong ide_bus_data_offset = CFG_ATA_DATA_OFFSET;
-ulong ide_bus_reg_offset = CFG_ATA_REG_OFFSET;
-ulong ide_bus_alt_offset = CFG_ATA_ALT_OFFSET;
-
-#include <ata.h>
-
+#ifdef __PPC__
 #define	ATA_CURR_BASE(dev)	(CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
+#endif
 
-#if !(defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT) )
+#if !(defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553) )
 static int	    ide_bus_ok[CFG_IDE_MAXBUS];
 #else
 static int	    ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
@@ -140,7 +130,7 @@
 /* ------------------------------------------------------------------------- */
 
 #ifdef CONFIG_IDE_LED
-#if !defined(CONFIG_KUP4K) && !defined(CONFIG_BMS2003)
+#ifndef CONFIG_KUP4K
 static void  ide_led   (uchar led, uchar status);
 #else
 extern void  ide_led   (uchar led, uchar status);
@@ -174,6 +164,9 @@
 
 static void __inline__ ide_outb(int dev, int port, unsigned char val);
 static unsigned char __inline__ ide_inb(int dev, int port);
+#ifdef __PPC__
+static void input_swap_data(int dev, ulong *sect_buf, int words);
+#endif
 static void input_data(int dev, ulong *sect_buf, int words);
 static void output_data(int dev, ulong *sect_buf, int words);
 static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
@@ -187,6 +180,8 @@
 
 #ifdef CONFIG_IDE_8xx_DIRECT
 static void set_pcmcia_timing (int pmode);
+#else
+#define set_pcmcia_timing(a)	/* dummy */
 #endif
 
 /* ------------------------------------------------------------------------- */
@@ -477,32 +472,19 @@
 
 void ide_init (void)
 {
+	DECLARE_GLOBAL_DATA_PTR;
 
 #ifdef CONFIG_IDE_8xx_DIRECT
-	DECLARE_GLOBAL_DATA_PTR;
 	volatile immap_t *immr = (immap_t *)CFG_IMMR;
 	volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
 #endif
 	unsigned char c;
 	int i, bus;
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 	unsigned int max_bus_scan;
 	unsigned int ata_reset_time;
 	char *s;
 #endif
-#ifdef CONFIG_IDE_8xx_PCCARD
-	extern int pcmcia_on (void);
-	extern int ide_devices_found; /* Initialized in check_ide_device() */
-#endif	/* CONFIG_IDE_8xx_PCCARD */
-
-#ifdef CONFIG_IDE_PREINIT
-	WATCHDOG_RESET();
-
-	if (ide_preinit ()) {
-		puts ("ide_preinit failed\n");
-		return;
-	}
-#endif	/* CONFIG_IDE_PREINIT */
 
 #ifdef CONFIG_IDE_8xx_PCCARD
 	extern int pcmcia_on (void);
@@ -520,7 +502,6 @@
 
 	WATCHDOG_RESET();
 
-#ifdef CONFIG_IDE_8xx_DIRECT
 	/* Initialize PIO timing tables */
 	for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
 	    pio_config_clk[i].t_setup  = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
@@ -537,7 +518,6 @@
 		    pio_config_ns[i].t_length, pio_config_clk[i].t_length,
 		    pio_config_ns[i].t_hold,   pio_config_clk[i].t_hold);
 	}
-#endif /* CONFIG_IDE_8xx_DIRECT */
 
 	/* Reset the IDE just to be sure.
 	 * Light LED's to show
@@ -548,17 +528,17 @@
 #ifdef CONFIG_IDE_8xx_DIRECT
 	/* PCMCIA / IDE initialization for common mem space */
 	pcmp->pcmc_pgcrb = 0;
+#endif
 
 	/* start in PIO mode 0 - most relaxed timings */
 	pio_mode = 0;
 	set_pcmcia_timing (pio_mode);
-#endif /* CONFIG_IDE_8xx_DIRECT */
 
 	/*
 	 * Wait for IDE to get ready.
 	 * According to spec, this can take up to 31 seconds!
 	 */
-#if !(defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT) )
+#if !(defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553))
 	for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
 		int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
 #else
@@ -588,10 +568,10 @@
 		udelay (100000);		/* 100 ms */
 		ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
 		udelay (100000);		/* 100 ms */
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 		ata_reset_time = ATA_RESET_TIME;
 		s = getenv("ide_reset_timeout");
-		if (s) ata_reset_time = simple_strtol(s, NULL, 10);
+		if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
 #endif
 		i = 0;
 		do {
@@ -599,14 +579,14 @@
 
 			c = ide_inb (dev, ATA_STATUS);
 			i++;
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 			if (i > (ata_reset_time * 100)) {
 #else
 			if (i > (ATA_RESET_TIME * 100)) {
 #endif
 				puts ("** Timeout **\n");
 				ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 				/* If this is the second bus, the first one was OK */
 				if (bus != 0)
 				{
@@ -636,7 +616,7 @@
 		WATCHDOG_RESET();
 	}
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT) 
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
       skip_bus:
 #endif
 	putc ('\n');
@@ -648,7 +628,6 @@
 #ifdef CONFIG_IDE_LED
 		int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
 #endif
-		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;
@@ -773,18 +752,18 @@
 static void __inline__
 ide_outb(int dev, int port, unsigned char val)
 {
-	PRINTF ("ide_outb (dev= %d, port= %d, val= 0x%02x) : @ 0x%08lx\n",
-		dev, port, val, (ATA_CURR_BASE(dev)+port));
-
 	/* Ensure I/O operations complete */
 	__asm__ volatile("eieio");
 	*((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
+#if 0
+	printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
+#endif
 }
 #else	/* ! __PPC__ */
 static void __inline__
 ide_outb(int dev, int port, unsigned char val)
 {
-	outb(val, ATA_CURR_BASE(dev)+port);
+	outb(val, port);
 }
 #endif	/* __PPC__ */
 
@@ -797,20 +776,21 @@
 	/* Ensure I/O operations complete */
 	__asm__ volatile("eieio");
 	val = *((uchar *)(ATA_CURR_BASE(dev)+port));
-	PRINTF ("ide_inb (dev= %d, port= %d) : @ 0x%08lx -> 0x%02x\n",
-		dev, port, (ATA_CURR_BASE(dev)+port), val);
+#if 0
+	printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
+#endif
 	return (val);
 }
 #else	/* ! __PPC__ */
 static unsigned char __inline__
 ide_inb(int dev, int port)
 {
-  return inb(ATA_CURR_BASE(dev)+port);
+	return inb(port);
 }
 #endif	/* __PPC__ */
 
 #ifdef __PPC__
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 static void
 output_data_short(int dev, ulong *sect_buf, int words)
 {
@@ -829,17 +809,10 @@
 	    *pbuf = 0;
 }
 # endif	/* CONFIG_AMIGAONEG3SE */
-#endif /* __PPC_ */
 
-/* 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! */
-#if defined(__LITTLE_ENDIAN) || defined(CONFIG_AU1X00)
-#define input_swap_data(x,y,z) input_data(x,y,z)
-#else
 static void
 input_swap_data(int dev, ulong *sect_buf, int words)
 {
-#ifndef CONFIG_BMS2003
 	volatile ushort	*pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
 	ushort	*dbuf = (ushort *)sect_buf;
 
@@ -847,29 +820,16 @@
 		*dbuf++ = ld_le16(pbuf);
 		*dbuf++ = ld_le16(pbuf);
 	}
-#else	/* CONFIG_BMS2003 */
-	uchar i;
-	volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
-	volatile uchar *pbuf_odd  = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
-	ushort  *dbuf = (ushort *)sect_buf;
-
-	while (words--) {
-		for (i=0; i<2; i++) {
-			*(((uchar *)(dbuf)) + 1) = *pbuf_even;
-			*(uchar *)dbuf = *pbuf_odd;
-			dbuf+=1;
-		}
-	}
-#endif	/* CONFIG_BMS2003 */
 }
-#endif	/* __LITTLE_ENDIAN || CONFIG_AU1X00 */
+#else	/* ! __PPC__ */
+#define input_swap_data(x,y,z) input_data(x,y,z)
+#endif	/* __PPC__ */
 
 
 #ifdef __PPC__
 static void
 output_data(int dev, ulong *sect_buf, int words)
 {
-#ifndef CONFIG_BMS2003
 	ushort	*dbuf;
 	volatile ushort	*pbuf;
 
@@ -881,31 +841,12 @@
 		__asm__ volatile ("eieio");
 		*pbuf = *dbuf++;
 	}
-#else	/* CONFIG_BMS2003 */
-	uchar	*dbuf;
-	volatile uchar	*pbuf_even;
-	volatile uchar	*pbuf_odd;
-
-	pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
-	pbuf_odd  = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
-	dbuf = (uchar *)sect_buf;
-	while (words--) {
-		__asm__ volatile ("eieio");
-		*pbuf_even = *dbuf++;
-		__asm__ volatile ("eieio");
-		*pbuf_odd = *dbuf++;
-		__asm__ volatile ("eieio");
-		*pbuf_even = *dbuf++;
-		__asm__ volatile ("eieio");
-		*pbuf_odd = *dbuf++;
-	}
-#endif	/* CONFIG_BMS2003 */
 }
 #else	/* ! __PPC__ */
 static void
 output_data(int dev, ulong *sect_buf, int words)
 {
-	outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
+	outsw(ATA_DATA_REG, sect_buf, words<<1);
 }
 #endif	/* __PPC__ */
 
@@ -913,7 +854,6 @@
 static void
 input_data(int dev, ulong *sect_buf, int words)
 {
-#ifndef CONFIG_BMS2003
 	ushort	*dbuf;
 	volatile ushort	*pbuf;
 
@@ -925,36 +865,17 @@
 		__asm__ volatile ("eieio");
 		*dbuf++ = *pbuf;
 	}
-#else	/* CONFIG_BMS2003 */
-	uchar	*dbuf;
-	volatile uchar	*pbuf_even;
-	volatile uchar	*pbuf_odd;
-
-	pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
-	pbuf_odd  = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
-	dbuf = (uchar *)sect_buf;
-	while (words--) {
-		__asm__ volatile ("eieio");
-		*dbuf++ = *pbuf_even;
-		__asm__ volatile ("eieio");
-		*dbuf++ = *pbuf_odd;
-		__asm__ volatile ("eieio");
-		*dbuf++ = *pbuf_even;
-		__asm__ volatile ("eieio");
-		*dbuf++ = *pbuf_odd;
-	}
-#endif	/* CONFIG_BMS2003 */
 }
 #else	/* ! __PPC__ */
 static void
 input_data(int dev, ulong *sect_buf, int words)
 {
-	insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
+	insw(ATA_DATA_REG, sect_buf, words << 1);
 }
 
 #endif	/* __PPC__ */
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 static void
 input_data_short(int dev, ulong *sect_buf, int words)
 {
@@ -985,7 +906,7 @@
 	unsigned char c;
 	hd_driveid_t *iop = (hd_driveid_t *)iobuf;
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 	int max_bus_scan;
 	int retries = 0;
 	char *s;
@@ -999,7 +920,7 @@
 	device=dev_desc->dev;
 	printf ("  Device %d: ", device);
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 	s = getenv("ide_maxbus");
 	if (s) {
 		max_bus_scan = simple_strtol(s, NULL, 10);
@@ -1019,7 +940,7 @@
 	dev_desc->if_type=IF_TYPE_IDE;
 #ifdef CONFIG_ATAPI
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
     do_retry = 0;
     retries = 0;
 
@@ -1059,18 +980,20 @@
 
 	if (((c & ATA_STAT_DRQ) == 0) ||
 	    ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 		if (retries == 0) {
 			do_retry = 1;
 		} else {
+			dev_desc->type=DEV_TYPE_UNKNOWN;
 			return;
 		}
 #else
+		dev_desc->type=DEV_TYPE_UNKNOWN;
 		return;
 #endif	/* CONFIG_AMIGAONEG3SE */
 	}
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 	s = getenv("ide_doreset");
 	if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
 		/* Need to soft reset the device in case it's an ATAPI...  */
@@ -1427,8 +1350,7 @@
 
 /* ------------------------------------------------------------------------- */
 
-#if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && \
-	!defined(CONFIG_KUP4K) && !defined(CONFIG_BMS2003) && !defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K) && !defined(CFG_VIA_82C686A) && !defined(CFG_WINBOND_83C553)
 
 static	uchar	led_buffer = 0;		/* Buffer for current LED status	*/
 
@@ -1469,7 +1391,6 @@
 static void
 output_data_shorts(int dev, ushort *sect_buf, int shorts)
 {
-#ifndef CONFIG_BMS2003
 	ushort	*dbuf;
 	volatile ushort	*pbuf;
 
@@ -1479,26 +1400,11 @@
 		__asm__ volatile ("eieio");
 		*pbuf = *dbuf++;
 	}
-#else	/* CONFIG_BMS2003 */
-	uchar	*dbuf;
-	volatile uchar	*pbuf_even;
-	volatile uchar	*pbuf_odd;
-
-	pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
-	pbuf_odd  = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
-	while (shorts--) {
-		__asm__ volatile ("eieio");
-		*pbuf_even = *dbuf++;
-		__asm__ volatile ("eieio");
-		*pbuf_odd = *dbuf++;
-	}
-#endif	/* CONFIG_BMS2003 */
 }
 
 static void
 input_data_shorts(int dev, ushort *sect_buf, int shorts)
 {
-#ifndef CONFIG_BMS2003
 	ushort	*dbuf;
 	volatile ushort	*pbuf;
 
@@ -1508,34 +1414,20 @@
 		__asm__ volatile ("eieio");
 		*dbuf++ = *pbuf;
 	}
-#else	/* CONFIG_BMS2003 */
-	uchar	*dbuf;
-	volatile uchar	*pbuf_even;
-	volatile uchar	*pbuf_odd;
-
-	pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
-	pbuf_odd  = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
-	while (shorts--) {
-		__asm__ volatile ("eieio");
-		*dbuf++ = *pbuf_even;
-		__asm__ volatile ("eieio");
-		*dbuf++ = *pbuf_odd;
-	}
-#endif	/* CONFIG_BMS2003 */
 }
 
 #else	/* ! __PPC__ */
 static void
 output_data_shorts(int dev, ushort *sect_buf, int shorts)
 {
-	outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
+	outsw(ATA_DATA_REG, sect_buf, shorts);
 }
 
 
 static void
 input_data_shorts(int dev, ushort *sect_buf, int shorts)
 {
-	insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
+	insw(ATA_DATA_REG, sect_buf, shorts);
 }
 
 #endif	/* __PPC__ */
@@ -1577,7 +1469,7 @@
 	 */
 	mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
 	res = 0;
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#ifdef	CONFIG_AMIGAONEG3SE
 # warning THF: Removed LBA mode ???
 #endif
 	ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
@@ -1593,7 +1485,7 @@
 	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));
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#ifdef	CONFIG_AMIGAONEG3SE
 # warning THF: Removed LBA mode ???
 #endif
 	ide_outb (device, ATA_DEV_HD,   ATA_LBA | ATA_DEVICE(device));
@@ -1694,7 +1586,7 @@
 	unsigned char res,key,asc,ascq;
 	int notready,unitattn;
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 	char *s;
 	unsigned int timeout, retrycnt;
 
@@ -1758,7 +1650,7 @@
 		goto error;
 	}
 
-#if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_APOS_EXT)
+#if defined(CONFIG_AMIGAONEG3SE) || defined(CFG_VIA_82C686A) || defined(CFG_WINBOND_83C553)
 	if ((sense_data[2]&0xF)==0x0B) {
 		AT_PRINTF("ABORTED COMMAND...retry\n");
 		if (retrycnt++ < 4)
