/* 2012-09-16: File added and changed by Sony Corporation */
/* icx_etc.c
 *
 * Copyright 2011 Sony Corporation.
 *
 * 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 <config.h>

#if defined(CONFIG_ICX) || defined(CONFIG_ICD)

#include "../include/mini_config.h"
#include "../include/icx_etc.h"
#include "../../reg.h"


/****************/
/*@ Definitions */
/****************/

#define RESET_FLAG_SAVE_VALUE   0x744F6F42  /* "BoOt" */


/********************/
/*@ Global routines */
/********************/

#ifdef EMXX_MINIBOOT
/*-----------------------------------------------------------------------------
 * Function : icx_etc_board_init
 * Abstruct : Initialize ICX board
 * Input    : none
 * Returen  : none
 *----------------------------------------------------------------------------*/
void icx_etc_board_init(void)
{
#ifdef CONFIG_EMXX_EMMCBOOT
	/* set low speed clock when DeadBatt */
	if (inl(CONFIG_RESET_FLAG_ADDR) == RESET_FLAG_SAVE_VALUE)
		icx_etc_select_clock_mode(CLOCK_MODE_NORMAL_D);
#else
	/* install LED setting */
	outl((inl(CHG_PINSEL_G032) | 0x00010000), CHG_PINSEL_G032);
	outl((inl(CHG_PINSEL_G096) | 0x00002000), CHG_PINSEL_G096);
	outl((inl(CHG_PULL6 ) & ~0x0000000F), CHG_PULL6 );
	outl((inl(CHG_PULL13) & ~0x00F00000), CHG_PULL13);

	outl(0x00010000, GIO_032_E1);
	outl(0x00002000, GIO_096_E1);

	/* turn on install LED */
	outl(0x00010001, GIO_032_OH);
	outl(0x20002000, GIO_096_OL);
#endif /* CONFIG_EMXX_EMMCBOOT */
}

#ifdef CONFIG_EMXX_SDBOOT
/*-----------------------------------------------------------------------------
 * Function : icx_etc_install_error
 * Abstruct : Set error LED when install
 * Input    : none
 * Returen  : none
 *----------------------------------------------------------------------------*/
void icx_etc_install_error(void)
{   
	outl(0x00010000, GIO_032_OH);		/* turn off WRITE_LED_CTL */
}   
#endif /* CONFIG_EMXX_SDBOOT */
#endif /* EMXX_MINIBOOT */

/*-----------------------------------------------------------------------------
 * Function : icx_etc_select_clock_mode
 * Abstruct : Select clock mode
 * Input    : mode : clock mode
 * Returen  : none
 *----------------------------------------------------------------------------*/
void icx_etc_select_clock_mode(unsigned int mode)
{
	unsigned int auto_frq_change;

	if ((inl(SMU_CLK_MODE_SEL) & 0x00000F00) == (mode << 8))  
		return;

	/* auto freq change off */
	auto_frq_change = inl(SMU_CKRQ_MODE);
	outl((auto_frq_change & ~0x1), SMU_CKRQ_MODE);

	outl(mode, SMU_CLK_MODE_SEL);

	while ((inl(SMU_CLK_MODE_SEL) & 0x00000F00) != (mode << 8));

	/* restore auto freq change */
	outl(auto_frq_change, SMU_CKRQ_MODE);
}

#endif /* CONFIG_ICX || CONFIG_ICD */
