/*
 * (C) Copyright 2001
 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
 *
 * (C) Copyright 2001, 2002
 * 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
 */

#include <common.h>
#include <mpc824x.h>
#include <asm/processor.h>
#include <asm/mmu.h>
#include <pci.h>

DECLARE_GLOBAL_DATA_PTR;

int checkboard (void)
{
	char  buf[32];
	puts ("Board: Allied Telesis Labs AT8948\n");
	return 0;
}

long int initdram(int board_type)
{
	long size;
	
	size = spd_sdram();

	return (size);
}

/*
 * Initialize PCI Devices, report devices found.
 */


static struct pci_config_table pci_at8948_config_table[] = 
{
  	{
		0x8086, 0x1229, PCI_ANY_ID, PCI_ANY_ID, /* Intel Pro/100 */
		13, PCI_ANY_ID, pci_cfgfunc_config_device,
		{ 0x80000000, 0x80000000, PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER }
	},
	{
		0x1022, 0x2000, PCI_ANY_ID, PCI_ANY_ID, /* AMD PCnet 32 */
		13, PCI_ANY_ID, pci_cfgfunc_config_device,
		{ 0x00000000, 0x80000000, PCI_COMMAND_IO | PCI_COMMAND_MASTER  }
	},
	{
		0x1022, 0x2000, PCI_ANY_ID, PCI_ANY_ID, /* AMD PCnet 32 */
		14, PCI_ANY_ID, pci_cfgfunc_config_device,
		{ 0x00100000, 0x80100000, PCI_COMMAND_IO | PCI_COMMAND_MASTER  }
	}
};

void hw_watchdog_reset(void)
{
	*(u8 *)0xFFE20058 = 0xFF;
	asm("sync");
}

struct pci_controller hose = 
{
	config_table: pci_at8948_config_table,
};

void ide_led (uchar led, uchar status)
{
	if (led)
	{
		if (status)
		{
			*(u8 *)0xFFE200D8 = 0x1;
			asm("sync");
		}
		else
		{
  			*(u8 *)0xFFE200D8 = 0x0;
			asm("sync");
		}
	}
}

int cflash_present(void)
{
	if(!(*(u8 *)0xFFE200B0 & 0x1)) /* Card present */
	{
		if (!!(*(u8 *)0xFFE200B0 & 0x8))  /* Card ready */
		{
			return 1;
		}
	}
	
	return 0;
}

void pci_init_board(void)
{
	pci_mpc824x_init(&hose);
}

int ide_preinit(void)
{
	return !cflash_present();
}

#ifdef CONFIG_MISC_INIT_R
int misc_init_r (void)
{
	*(u8 *)0xFFE2007A = 0x0;  
	asm("sync");
  
	flash_cache_range(0x040C0000, 0x040DFFFF);
	flash_cache_range(0x040E0000, 0x040FFFFF);
	
	//flash_cache_range(CFG_FLASH_BASE, CFG_PAGED_FLASH_END);
	return (0);
}
#endif /* CONFIG_MISC_INIT_R */

void mpc824x_board_reset(void)
{
	*(u8 *)0xffe20080 = 0xff;	
}

int atl_bootrom_board_attached(void)
{
	return 1;
}

u32 atl_get_env_addr(void)
{
	return (CFG_ENV_ADDR);
}

u32 atl_get_offset_from_base(void)
{
	return 0x0;
}

