/*
 * (C) Copyright 2003
 * 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 <command.h>
#include <asm/addrspace.h>
#include <epi3.h>

extern uint x300_get_cpu_cap(void);
extern uint x300_get_chipid(void);

int bootflash_attached = 0;

/* atl_bootrom_board_attached()
 *
 * used by bootmenu to determine weather or not to print
 * option '5' (EPROM) in upgrade bootloader menu
 */

int atl_bootrom_board_attached(void)
{
	return bootflash_attached;
}

/* atl_get_offset_from_base()
 *
 * not needed on this product
 */
 
u32 atl_get_offset_from_base(void)
{
	return 0;
}

/* atl_get_env_addr()
 *
 * return address of env for places where a macro can't be used.
 */

u32 atl_get_env_addr(void)
{
	return CFG_ENV_ADDR;	
}

long int initdram(int board_type)
{
	/* Sdram is setup by assembler code */
	/* If memory could be changed, we should return the true value here */
	return 128 * 1024 * 1024; /* should be 128M must fix */
}

long int x300_get_cpuclk(void)
{
	uint ratio;
	uint rate;

	rate = 133000000;
	ratio = (x300_get_cpu_cap() >> 4) & 3;
	switch (ratio) {
	case 0:
		rate = rate*2;
		break;
	case 1:
		rate = (rate*9)/4;
		break;
	case 2:
		rate = (rate*6)/4;
		break;
	case 3:
		break;
	}

	return (rate);
}

int checkboard (void)
{
	uint chipid;
	
	printf("Board: ATRD x300-24Ts\n");
	
	chipid = x300_get_chipid();
	printf ("Chip revision %04x device %04x, ", (chipid >> 16), (chipid & 0xffff));
	printf("CPU Speed %d MHz\n", x300_get_cpuclk()/1000000);

	return 0;
}


int misc_init_r (void)
{
	/* GPIO Initial State
	 *
	 * RESET high
	 * FAULT LED off
	 * CPU_MEM_CS1 high
	 * CPU_MEM_CS2 high
	 * All others low
	 */
	*(u32 *)0xb8000064 = 0x87;
		
	/* GPIO Directionality 
	 * All outputs
	 */
	*(u32 *)0xb8000068 = 0x00;
		
	/* Adjust pointers in epi3 name arrays 
	 */
	diag_epi3_reloc_names();
	return 0;
}

u32 x300_flash_init(void)
{
	u32 size;
	
	while (!(size = flash_init()))
	{
		bootflash_attached = 1;
		
		/* Grab a copy of the ROM image now before the user removes the jumper - 
	 	* removing our access to the original copy of the code in the boot flash
	 	* chip - then store it on the 8MB boundary (CONFIG_ROMIMAGE_BASE) for
	 	* use by the booloader upgrade menu later on.
	 	*/
		
		memcpy(CONFIG_ROMIMAGE_BASE, CONFIG_UBOOT_FLASH_BASE, CONFIG_UBOOT_SIZE);
				
		bootmenu_printf("System appears to have been booted from EPROM.\n");
		bootmenu_printf("Remove JP530 now then press any key to continue...");
		
		serial_getc();

		bootmenu_printf("\n");
	}
	
	
	return size;
}

int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	/* Clear reset bit */
	*(u32 *)0xb8000064 &= ~0x80;
}