rockbox/firmware/target/arm/imx233/system-imx233.c
Amaury Pouly 5aa19f3eeb imx233: implement emi frequency scaling (disabled by default)
CPU frequency scaling is basically useless without scaling the
memory frequency. On the i.MX233, the EMI (external memory
interface) and DRAM blocks are responsable for the DDR settings.
This commits implements emi frequency scaling. Only some settings
are implemented and the timings values only apply to mDDR
(extracted from Sigmatel linux port) and have been checked to
work on the Fuze+ and Zen X-Fi2/3. This feature is still disabled
by default but I expected some battery life savings by boosting
higher to 454MHz and unboosting lower to 64MHz.
Note that changing the emi frequency is particularly tricky and
to avoid writing it entirely in assembly we rely on the compiler
to not use the stack except in the prolog and epilog (because
it's in dram which is disabled when doing the change) and to put
constant pools in iram which should always be true if the
compiler isn't completely dumb and since the code itself is put
in iram. If this proves to be insufficient, one can always switch
the stack to the irq stack since interrupts are disabled during
the change.

Change-Id: If6ef5357f7ff091130ca1063e48536c6028f23ba
2013-01-10 00:51:35 +00:00

270 lines
8.4 KiB
C

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2011 by Amaury Pouly
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "kernel.h"
#include "system.h"
#include "gcc_extensions.h"
#include "system-target.h"
#include "cpu.h"
#include "clkctrl-imx233.h"
#include "pinctrl-imx233.h"
#include "timrot-imx233.h"
#include "dma-imx233.h"
#include "ssp-imx233.h"
#include "i2c-imx233.h"
#include "dcp-imx233.h"
#include "pwm-imx233.h"
#include "icoll-imx233.h"
#include "lradc-imx233.h"
#include "rtc-imx233.h"
#include "power-imx233.h"
#include "emi-imx233.h"
#include "lcd.h"
#include "backlight-target.h"
#include "button.h"
#include "fmradio_i2c.h"
void imx233_chip_reset(void)
{
HW_CLKCTRL_RESET = HW_CLKCTRL_RESET_CHIP;
}
void system_reboot(void)
{
_backlight_off();
disable_irq();
/* use watchdog to reset */
imx233_chip_reset();
while(1);
}
void system_exception_wait(void)
{
/* make sure lcd and backlight are on */
lcd_update();
_backlight_on();
_backlight_set_brightness(DEFAULT_BRIGHTNESS_SETTING);
/* wait until button release (if a button is pressed) */
#ifdef HAVE_BUTTON_DATA
int data;
while(button_read_device(&data));
/* then wait until next button press */
while(!button_read_device(&data));
#else
while(button_read_device());
/* then wait until next button press */
while(!button_read_device());
#endif
}
int system_memory_guard(int newmode)
{
(void)newmode;
return 0;
}
static void set_page_tables(void)
{
/* map every memory region to itself */
map_section(0, 0, 0x1000, CACHE_NONE);
/* map RAM and enable caching for it */
map_section(DRAM_ORIG, CACHED_DRAM_ADDR, MEMORYSIZE, CACHE_ALL);
map_section(DRAM_ORIG, BUFFERED_DRAM_ADDR, MEMORYSIZE, BUFFERED);
}
void memory_init(void)
{
ttb_init();
set_page_tables();
enable_mmu();
}
void system_init(void)
{
/* NOTE: don't use anything here that might require tick task !
* It is initialized by kernel_init *after* system_init().
* The main() will naturally set cpu speed to normal after kernel_init()
* so don't bother if the cpu is running at 24MHz here. */
imx233_clkctrl_enable_clock(CLK_PLL, true);
imx233_rtc_init();
imx233_icoll_init();
imx233_pinctrl_init();
imx233_timrot_init();
imx233_dma_init();
imx233_ssp_init();
imx233_dcp_init();
imx233_pwm_init();
imx233_lradc_init();
imx233_power_init();
imx233_i2c_init();
imx233_clkctrl_enable_auto_slow_monitor(AS_CPU_INSTR, true);
imx233_clkctrl_enable_auto_slow_monitor(AS_CPU_DATA, true);
imx233_clkctrl_enable_auto_slow_monitor(AS_TRAFFIC, true);
imx233_clkctrl_enable_auto_slow_monitor(AS_TRAFFIC_JAM, true);
imx233_clkctrl_enable_auto_slow_monitor(AS_APBXDMA, true);
imx233_clkctrl_enable_auto_slow_monitor(AS_APBHDMA, true);
imx233_clkctrl_set_auto_slow_divisor(AS_DIV_8);
imx233_clkctrl_enable_auto_slow(true);
#if !defined(BOOTLOADER) &&(defined(SANSA_FUZEPLUS) || \
defined(CREATIVE_ZENXFI3) || defined(CREATIVE_ZENXFI2))
fmradio_i2c_init();
#endif
}
bool imx233_us_elapsed(uint32_t ref, unsigned us_delay)
{
uint32_t cur = HW_DIGCTL_MICROSECONDS;
if(ref + us_delay <= ref)
return !(cur > ref) && !(cur < (ref + us_delay));
else
return (cur < ref) || cur >= (ref + us_delay);
}
void imx233_reset_block(volatile uint32_t *block_reg)
{
/* soft-reset */
__REG_SET(*block_reg) = __BLOCK_SFTRST;
/* make sure block is gated off */
while(!(*block_reg & __BLOCK_CLKGATE));
/* bring block out of reset */
__REG_CLR(*block_reg) = __BLOCK_SFTRST;
while(*block_reg & __BLOCK_SFTRST);
/* make sure clock is running */
__REG_CLR(*block_reg) = __BLOCK_CLKGATE;
while(*block_reg & __BLOCK_CLKGATE);
}
void udelay(unsigned us)
{
uint32_t ref = HW_DIGCTL_MICROSECONDS;
while(!imx233_us_elapsed(ref, us));
}
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
void set_cpu_frequency(long frequency)
{
/* don't change the frequency if it is useless (changes are expensive) */
if(cpu_frequency == frequency)
return;
cpu_frequency = frequency;
/* disable auto-slow (enable back afterwards) */
bool as = imx233_clkctrl_is_auto_slow_enabled();
imx233_clkctrl_enable_auto_slow(false);
/* go back to a known state in safe way:
* clk_p@24 MHz
* clk_h@6 MHz
* WARNING: we must absolutely avoid that clk_h be too low or too high
* during the change. We first change the clk_p/clk_h ratio to 4 so
* that it cannot be too high (480/4=120 MHz max) or too low
* (24/4=6 MHz min). Then we switch clk_p to bypass. We chose a ratio of 4
* which is greater than all clk_p/clk_h ratios used below so that further
* changes are safe too */
imx233_clkctrl_set_clock_divisor(CLK_HBUS, 4);
imx233_clkctrl_set_bypass_pll(CLK_CPU, true);
switch(frequency)
{
case IMX233_CPUFREQ_454_MHz:
/* set VDDD to 1.550 mV (brownout at 1.450 mV) */
imx233_power_set_regulator(REGULATOR_VDDD, 1550, 1450);
/* clk_h@clk_p/3 */
imx233_clkctrl_set_clock_divisor(CLK_HBUS, 3);
/* clk_p@ref_cpu/1*18/19 */
imx233_clkctrl_set_fractional_divisor(CLK_CPU, 19);
imx233_clkctrl_set_clock_divisor(CLK_CPU, 1);
imx233_clkctrl_set_bypass_pll(CLK_CPU, false);
imx233_emi_set_frequency(IMX233_EMIFREQ_130_MHz);
/* ref_cpu@480 MHz
* ref_emi@480 MHz
* clk_emi@130.91 MHz
* clk_p@454.74 MHz
* clk_h@130.91 MHz */
break;
case IMX233_CPUFREQ_261_MHz:
/* set VDDD to 1.275 mV (brownout at 1.175 mV) */
imx233_power_set_regulator(REGULATOR_VDDD, 1275, 1175);
/* clk_h@clk_p/2 */
imx233_clkctrl_set_clock_divisor(CLK_HBUS, 2);
/* clk_p@ref_cpu/1*18/33 */
imx233_clkctrl_set_fractional_divisor(CLK_CPU, 33);
imx233_clkctrl_set_clock_divisor(CLK_CPU, 1);
imx233_clkctrl_set_bypass_pll(CLK_CPU, false);
imx233_emi_set_frequency(IMX233_EMIFREQ_130_MHz);
/* ref_cpu@480 MHz
* ref_emi@480 MHz
* clk_emi@130.91 MHz
* clk_p@261.82 MHz
* clk_h@130.91 MHz */
break;
case IMX233_CPUFREQ_64_MHz:
/* set VDDD to 1.050 mV (brownout at 0.975 mV) */
imx233_power_set_regulator(REGULATOR_VDDD, 1050, 975);
/* clk_h@clk_p */
imx233_clkctrl_set_clock_divisor(CLK_HBUS, 1);
/* clk_p@ref_cpu/5*18/27 */
imx233_clkctrl_set_fractional_divisor(CLK_CPU, 27);
imx233_clkctrl_set_clock_divisor(CLK_CPU, 5);
imx233_clkctrl_set_bypass_pll(CLK_CPU, false);
imx233_emi_set_frequency(IMX233_EMIFREQ_64_MHz);
/* ref_cpu@480 MHz
* ref_emi@480 MHz
* clk_emi@64 MHz
* clk_p@64 MHz
* clk_h@64 MHz */
default:
break;
}
/* enable auto slow again */
imx233_clkctrl_enable_auto_slow(as);
}
#endif
void imx233_enable_usb_controller(bool enable)
{
if(enable)
__REG_CLR(HW_DIGCTL_CTRL) = HW_DIGCTL_CTRL__USB_CLKGATE;
else
__REG_SET(HW_DIGCTL_CTRL) = HW_DIGCTL_CTRL__USB_CLKGATE;
}
void imx233_enable_usb_phy(bool enable)
{
if(enable)
{
__REG_CLR(HW_USBPHY_CTRL) = __BLOCK_CLKGATE | __BLOCK_SFTRST;
__REG_CLR(HW_USBPHY_PWD) = HW_USBPHY_PWD__ALL;
}
else
{
__REG_SET(HW_USBPHY_PWD) = HW_USBPHY_PWD__ALL;
__REG_SET(HW_USBPHY_CTRL) = __BLOCK_CLKGATE | __BLOCK_SFTRST;
}
}