/*1* arch/arm/mach-lpc32xx/pm.c2*3* Original authors: Vitaly Wool, Dmitry Chigirev <[email protected]>4* Modified by Kevin Wells <[email protected]>5*6* 2005 (c) MontaVista Software, Inc. This file is licensed under7* the terms of the GNU General Public License version 2. This program8* is licensed "as is" without any warranty of any kind, whether express9* or implied.10*/1112/*13* LPC32XX CPU and system power management14*15* The LCP32XX has three CPU modes for controlling system power: run,16* direct-run, and halt modes. When switching between halt and run modes,17* the CPU transistions through direct-run mode. For Linux, direct-run18* mode is not used in normal operation. Halt mode is used when the19* system is fully suspended.20*21* Run mode:22* The ARM CPU clock (HCLK_PLL), HCLK bus clock, and PCLK bus clocks are23* derived from the HCLK PLL. The HCLK and PCLK bus rates are divided from24* the HCLK_PLL rate. Linux runs in this mode.25*26* Direct-run mode:27* The ARM CPU clock, HCLK bus clock, and PCLK bus clocks are driven from28* SYSCLK. SYSCLK is usually around 13MHz, but may vary based on SYSCLK29* source or the frequency of the main oscillator. In this mode, the30* HCLK_PLL can be safely enabled, changed, or disabled.31*32* Halt mode:33* SYSCLK is gated off and the CPU and system clocks are halted.34* Peripherals based on the 32KHz oscillator clock (ie, RTC, touch,35* key scanner, etc.) still operate if enabled. In this state, an enabled36* system event (ie, GPIO state change, RTC match, key press, etc.) will37* wake the system up back into direct-run mode.38*39* DRAM refresh40* DRAM clocking and refresh are slightly different for systems with DDR41* DRAM or regular SDRAM devices. If SDRAM is used in the system, the42* SDRAM will still be accessible in direct-run mode. In DDR based systems,43* a transition to direct-run mode will stop all DDR accesses (no clocks).44* Because of this, the code to switch power modes and the code to enter45* and exit DRAM self-refresh modes must not be executed in DRAM. A small46* section of IRAM is used instead for this.47*48* Suspend is handled with the following logic:49* Backup a small area of IRAM used for the suspend code50* Copy suspend code to IRAM51* Transfer control to code in IRAM52* Places DRAMs in self-refresh mode53* Enter direct-run mode54* Save state of HCLK_PLL PLL55* Disable HCLK_PLL PLL56* Enter halt mode - CPU and buses will stop57* System enters direct-run mode when an enabled event occurs58* HCLK PLL state is restored59* Run mode is entered60* DRAMS are placed back into normal mode61* Code execution returns from IRAM62* IRAM code are used for suspend is restored63* Suspend mode is exited64*/6566#include <linux/suspend.h>67#include <linux/io.h>68#include <linux/slab.h>6970#include <asm/cacheflush.h>7172#include <mach/hardware.h>73#include <mach/platform.h>74#include "common.h"75#include "clock.h"7677#define TEMP_IRAM_AREA IO_ADDRESS(LPC32XX_IRAM_BASE)7879/*80* Both STANDBY and MEM suspend states are handled the same with no81* loss of CPU or memory state82*/83static int lpc32xx_pm_enter(suspend_state_t state)84{85int (*lpc32xx_suspend_ptr) (void);86void *iram_swap_area;8788/* Allocate some space for temporary IRAM storage */89iram_swap_area = kmalloc(lpc32xx_sys_suspend_sz, GFP_KERNEL);90if (!iram_swap_area) {91printk(KERN_ERR92"PM Suspend: cannot allocate memory to save portion "93"of SRAM\n");94return -ENOMEM;95}9697/* Backup a small area of IRAM used for the suspend code */98memcpy(iram_swap_area, (void *) TEMP_IRAM_AREA,99lpc32xx_sys_suspend_sz);100101/*102* Copy code to suspend system into IRAM. The suspend code103* needs to run from IRAM as DRAM may no longer be available104* when the PLL is stopped.105*/106memcpy((void *) TEMP_IRAM_AREA, &lpc32xx_sys_suspend,107lpc32xx_sys_suspend_sz);108flush_icache_range((unsigned long)TEMP_IRAM_AREA,109(unsigned long)(TEMP_IRAM_AREA) + lpc32xx_sys_suspend_sz);110111/* Transfer to suspend code in IRAM */112lpc32xx_suspend_ptr = (void *) TEMP_IRAM_AREA;113flush_cache_all();114(void) lpc32xx_suspend_ptr();115116/* Restore original IRAM contents */117memcpy((void *) TEMP_IRAM_AREA, iram_swap_area,118lpc32xx_sys_suspend_sz);119120kfree(iram_swap_area);121122return 0;123}124125static const struct platform_suspend_ops lpc32xx_pm_ops = {126.valid = suspend_valid_only_mem,127.enter = lpc32xx_pm_enter,128};129130#define EMC_DYN_MEM_CTRL_OFS 0x20131#define EMC_SRMMC (1 << 3)132#define EMC_CTRL_REG io_p2v(LPC32XX_EMC_BASE + EMC_DYN_MEM_CTRL_OFS)133static int __init lpc32xx_pm_init(void)134{135/*136* Setup SDRAM self-refresh clock to automatically disable o137* start of self-refresh. This only needs to be done once.138*/139__raw_writel(__raw_readl(EMC_CTRL_REG) | EMC_SRMMC, EMC_CTRL_REG);140141suspend_set_ops(&lpc32xx_pm_ops);142143return 0;144}145arch_initcall(lpc32xx_pm_init);146147148