1*3c0e1947SKevin Wells /* 2*3c0e1947SKevin Wells * arch/arm/mach-lpc32xx/pm.c 3*3c0e1947SKevin Wells * 4*3c0e1947SKevin Wells * Original authors: Vitaly Wool, Dmitry Chigirev <source@mvista.com> 5*3c0e1947SKevin Wells * Modified by Kevin Wells <kevin.wells@nxp.com> 6*3c0e1947SKevin Wells * 7*3c0e1947SKevin Wells * 2005 (c) MontaVista Software, Inc. This file is licensed under 8*3c0e1947SKevin Wells * the terms of the GNU General Public License version 2. This program 9*3c0e1947SKevin Wells * is licensed "as is" without any warranty of any kind, whether express 10*3c0e1947SKevin Wells * or implied. 11*3c0e1947SKevin Wells */ 12*3c0e1947SKevin Wells 13*3c0e1947SKevin Wells /* 14*3c0e1947SKevin Wells * LPC32XX CPU and system power management 15*3c0e1947SKevin Wells * 16*3c0e1947SKevin Wells * The LCP32XX has three CPU modes for controlling system power: run, 17*3c0e1947SKevin Wells * direct-run, and halt modes. When switching between halt and run modes, 18*3c0e1947SKevin Wells * the CPU transistions through direct-run mode. For Linux, direct-run 19*3c0e1947SKevin Wells * mode is not used in normal operation. Halt mode is used when the 20*3c0e1947SKevin Wells * system is fully suspended. 21*3c0e1947SKevin Wells * 22*3c0e1947SKevin Wells * Run mode: 23*3c0e1947SKevin Wells * The ARM CPU clock (HCLK_PLL), HCLK bus clock, and PCLK bus clocks are 24*3c0e1947SKevin Wells * derived from the HCLK PLL. The HCLK and PCLK bus rates are divided from 25*3c0e1947SKevin Wells * the HCLK_PLL rate. Linux runs in this mode. 26*3c0e1947SKevin Wells * 27*3c0e1947SKevin Wells * Direct-run mode: 28*3c0e1947SKevin Wells * The ARM CPU clock, HCLK bus clock, and PCLK bus clocks are driven from 29*3c0e1947SKevin Wells * SYSCLK. SYSCLK is usually around 13MHz, but may vary based on SYSCLK 30*3c0e1947SKevin Wells * source or the frequency of the main oscillator. In this mode, the 31*3c0e1947SKevin Wells * HCLK_PLL can be safely enabled, changed, or disabled. 32*3c0e1947SKevin Wells * 33*3c0e1947SKevin Wells * Halt mode: 34*3c0e1947SKevin Wells * SYSCLK is gated off and the CPU and system clocks are halted. 35*3c0e1947SKevin Wells * Peripherals based on the 32KHz oscillator clock (ie, RTC, touch, 36*3c0e1947SKevin Wells * key scanner, etc.) still operate if enabled. In this state, an enabled 37*3c0e1947SKevin Wells * system event (ie, GPIO state change, RTC match, key press, etc.) will 38*3c0e1947SKevin Wells * wake the system up back into direct-run mode. 39*3c0e1947SKevin Wells * 40*3c0e1947SKevin Wells * DRAM refresh 41*3c0e1947SKevin Wells * DRAM clocking and refresh are slightly different for systems with DDR 42*3c0e1947SKevin Wells * DRAM or regular SDRAM devices. If SDRAM is used in the system, the 43*3c0e1947SKevin Wells * SDRAM will still be accessible in direct-run mode. In DDR based systems, 44*3c0e1947SKevin Wells * a transistion to direct-run mode will stop all DDR accesses (no clocks). 45*3c0e1947SKevin Wells * Because of this, the code to switch power modes and the code to enter 46*3c0e1947SKevin Wells * and exit DRAM self-refresh modes must not be executed in DRAM. A small 47*3c0e1947SKevin Wells * section of IRAM is used instead for this. 48*3c0e1947SKevin Wells * 49*3c0e1947SKevin Wells * Suspend is handled with the following logic: 50*3c0e1947SKevin Wells * Backup a small area of IRAM used for the suspend code 51*3c0e1947SKevin Wells * Copy suspend code to IRAM 52*3c0e1947SKevin Wells * Transfer control to code in IRAM 53*3c0e1947SKevin Wells * Places DRAMs in self-refresh mode 54*3c0e1947SKevin Wells * Enter direct-run mode 55*3c0e1947SKevin Wells * Save state of HCLK_PLL PLL 56*3c0e1947SKevin Wells * Disable HCLK_PLL PLL 57*3c0e1947SKevin Wells * Enter halt mode - CPU and buses will stop 58*3c0e1947SKevin Wells * System enters direct-run mode when an enabled event occurs 59*3c0e1947SKevin Wells * HCLK PLL state is restored 60*3c0e1947SKevin Wells * Run mode is entered 61*3c0e1947SKevin Wells * DRAMS are placed back into normal mode 62*3c0e1947SKevin Wells * Code execution returns from IRAM 63*3c0e1947SKevin Wells * IRAM code are used for suspend is restored 64*3c0e1947SKevin Wells * Suspend mode is exited 65*3c0e1947SKevin Wells */ 66*3c0e1947SKevin Wells 67*3c0e1947SKevin Wells #include <linux/suspend.h> 68*3c0e1947SKevin Wells #include <linux/io.h> 69*3c0e1947SKevin Wells #include <linux/slab.h> 70*3c0e1947SKevin Wells 71*3c0e1947SKevin Wells #include <asm/cacheflush.h> 72*3c0e1947SKevin Wells 73*3c0e1947SKevin Wells #include <mach/hardware.h> 74*3c0e1947SKevin Wells #include <mach/platform.h> 75*3c0e1947SKevin Wells #include "common.h" 76*3c0e1947SKevin Wells #include "clock.h" 77*3c0e1947SKevin Wells 78*3c0e1947SKevin Wells #define TEMP_IRAM_AREA IO_ADDRESS(LPC32XX_IRAM_BASE) 79*3c0e1947SKevin Wells 80*3c0e1947SKevin Wells /* 81*3c0e1947SKevin Wells * Both STANDBY and MEM suspend states are handled the same with no 82*3c0e1947SKevin Wells * loss of CPU or memory state 83*3c0e1947SKevin Wells */ 84*3c0e1947SKevin Wells static int lpc32xx_pm_enter(suspend_state_t state) 85*3c0e1947SKevin Wells { 86*3c0e1947SKevin Wells int (*lpc32xx_suspend_ptr) (void); 87*3c0e1947SKevin Wells void *iram_swap_area; 88*3c0e1947SKevin Wells 89*3c0e1947SKevin Wells /* Allocate some space for temporary IRAM storage */ 90*3c0e1947SKevin Wells iram_swap_area = kmalloc(lpc32xx_sys_suspend_sz, GFP_KERNEL); 91*3c0e1947SKevin Wells if (!iram_swap_area) { 92*3c0e1947SKevin Wells printk(KERN_ERR 93*3c0e1947SKevin Wells "PM Suspend: cannot allocate memory to save portion " 94*3c0e1947SKevin Wells "of SRAM\n"); 95*3c0e1947SKevin Wells return -ENOMEM; 96*3c0e1947SKevin Wells } 97*3c0e1947SKevin Wells 98*3c0e1947SKevin Wells /* Backup a small area of IRAM used for the suspend code */ 99*3c0e1947SKevin Wells memcpy(iram_swap_area, (void *) TEMP_IRAM_AREA, 100*3c0e1947SKevin Wells lpc32xx_sys_suspend_sz); 101*3c0e1947SKevin Wells 102*3c0e1947SKevin Wells /* 103*3c0e1947SKevin Wells * Copy code to suspend system into IRAM. The suspend code 104*3c0e1947SKevin Wells * needs to run from IRAM as DRAM may no longer be available 105*3c0e1947SKevin Wells * when the PLL is stopped. 106*3c0e1947SKevin Wells */ 107*3c0e1947SKevin Wells memcpy((void *) TEMP_IRAM_AREA, &lpc32xx_sys_suspend, 108*3c0e1947SKevin Wells lpc32xx_sys_suspend_sz); 109*3c0e1947SKevin Wells flush_icache_range((unsigned long)TEMP_IRAM_AREA, 110*3c0e1947SKevin Wells (unsigned long)(TEMP_IRAM_AREA) + lpc32xx_sys_suspend_sz); 111*3c0e1947SKevin Wells 112*3c0e1947SKevin Wells /* Transfer to suspend code in IRAM */ 113*3c0e1947SKevin Wells lpc32xx_suspend_ptr = (void *) TEMP_IRAM_AREA; 114*3c0e1947SKevin Wells flush_cache_all(); 115*3c0e1947SKevin Wells (void) lpc32xx_suspend_ptr(); 116*3c0e1947SKevin Wells 117*3c0e1947SKevin Wells /* Restore original IRAM contents */ 118*3c0e1947SKevin Wells memcpy((void *) TEMP_IRAM_AREA, iram_swap_area, 119*3c0e1947SKevin Wells lpc32xx_sys_suspend_sz); 120*3c0e1947SKevin Wells 121*3c0e1947SKevin Wells kfree(iram_swap_area); 122*3c0e1947SKevin Wells 123*3c0e1947SKevin Wells return 0; 124*3c0e1947SKevin Wells } 125*3c0e1947SKevin Wells 126*3c0e1947SKevin Wells static struct platform_suspend_ops lpc32xx_pm_ops = { 127*3c0e1947SKevin Wells .valid = suspend_valid_only_mem, 128*3c0e1947SKevin Wells .enter = lpc32xx_pm_enter, 129*3c0e1947SKevin Wells }; 130*3c0e1947SKevin Wells 131*3c0e1947SKevin Wells #define EMC_DYN_MEM_CTRL_OFS 0x20 132*3c0e1947SKevin Wells #define EMC_SRMMC (1 << 3) 133*3c0e1947SKevin Wells #define EMC_CTRL_REG io_p2v(LPC32XX_EMC_BASE + EMC_DYN_MEM_CTRL_OFS) 134*3c0e1947SKevin Wells static int __init lpc32xx_pm_init(void) 135*3c0e1947SKevin Wells { 136*3c0e1947SKevin Wells /* 137*3c0e1947SKevin Wells * Setup SDRAM self-refresh clock to automatically disable o 138*3c0e1947SKevin Wells * start of self-refresh. This only needs to be done once. 139*3c0e1947SKevin Wells */ 140*3c0e1947SKevin Wells __raw_writel(__raw_readl(EMC_CTRL_REG) | EMC_SRMMC, EMC_CTRL_REG); 141*3c0e1947SKevin Wells 142*3c0e1947SKevin Wells suspend_set_ops(&lpc32xx_pm_ops); 143*3c0e1947SKevin Wells 144*3c0e1947SKevin Wells return 0; 145*3c0e1947SKevin Wells } 146*3c0e1947SKevin Wells arch_initcall(lpc32xx_pm_init); 147