// SPDX-License-Identifier: GPL-2.0-only1/*2* arch/arm/mach-lpc32xx/pm.c3*4* Original authors: Vitaly Wool, Dmitry Chigirev <[email protected]>5* Modified by Kevin Wells <[email protected]>6*7* 2005 (c) MontaVista Software, Inc.8*/910/*11* LPC32XX CPU and system power management12*13* The LPC32XX has three CPU modes for controlling system power: run,14* direct-run, and halt modes. When switching between halt and run modes,15* the CPU transistions through direct-run mode. For Linux, direct-run16* mode is not used in normal operation. Halt mode is used when the17* system is fully suspended.18*19* Run mode:20* The ARM CPU clock (HCLK_PLL), HCLK bus clock, and PCLK bus clocks are21* derived from the HCLK PLL. The HCLK and PCLK bus rates are divided from22* the HCLK_PLL rate. Linux runs in this mode.23*24* Direct-run mode:25* The ARM CPU clock, HCLK bus clock, and PCLK bus clocks are driven from26* SYSCLK. SYSCLK is usually around 13MHz, but may vary based on SYSCLK27* source or the frequency of the main oscillator. In this mode, the28* HCLK_PLL can be safely enabled, changed, or disabled.29*30* Halt mode:31* SYSCLK is gated off and the CPU and system clocks are halted.32* Peripherals based on the 32KHz oscillator clock (ie, RTC, touch,33* key scanner, etc.) still operate if enabled. In this state, an enabled34* system event (ie, GPIO state change, RTC match, key press, etc.) will35* wake the system up back into direct-run mode.36*37* DRAM refresh38* DRAM clocking and refresh are slightly different for systems with DDR39* DRAM or regular SDRAM devices. If SDRAM is used in the system, the40* SDRAM will still be accessible in direct-run mode. In DDR based systems,41* a transition to direct-run mode will stop all DDR accesses (no clocks).42* Because of this, the code to switch power modes and the code to enter43* and exit DRAM self-refresh modes must not be executed in DRAM. A small44* section of IRAM is used instead for this.45*46* Suspend is handled with the following logic:47* Backup a small area of IRAM used for the suspend code48* Copy suspend code to IRAM49* Transfer control to code in IRAM50* Places DRAMs in self-refresh mode51* Enter direct-run mode52* Save state of HCLK_PLL PLL53* Disable HCLK_PLL PLL54* Enter halt mode - CPU and buses will stop55* System enters direct-run mode when an enabled event occurs56* HCLK PLL state is restored57* Run mode is entered58* DRAMS are placed back into normal mode59* Code execution returns from IRAM60* IRAM code are used for suspend is restored61* Suspend mode is exited62*/6364#include <linux/suspend.h>65#include <linux/io.h>66#include <linux/slab.h>6768#include <asm/cacheflush.h>6970#include "lpc32xx.h"71#include "common.h"7273#define TEMP_IRAM_AREA IO_ADDRESS(LPC32XX_IRAM_BASE)7475/*76* Both STANDBY and MEM suspend states are handled the same with no77* loss of CPU or memory state78*/79static int lpc32xx_pm_enter(suspend_state_t state)80{81int (*lpc32xx_suspend_ptr) (void);82void *iram_swap_area;8384/* Allocate some space for temporary IRAM storage */85iram_swap_area = kmemdup((void *)TEMP_IRAM_AREA,86lpc32xx_sys_suspend_sz, GFP_KERNEL);87if (!iram_swap_area)88return -ENOMEM;8990/*91* Copy code to suspend system into IRAM. The suspend code92* needs to run from IRAM as DRAM may no longer be available93* when the PLL is stopped.94*/95memcpy((void *) TEMP_IRAM_AREA, &lpc32xx_sys_suspend,96lpc32xx_sys_suspend_sz);97flush_icache_range((unsigned long)TEMP_IRAM_AREA,98(unsigned long)(TEMP_IRAM_AREA) + lpc32xx_sys_suspend_sz);99100/* Transfer to suspend code in IRAM */101lpc32xx_suspend_ptr = (void *) TEMP_IRAM_AREA;102flush_cache_all();103(void) lpc32xx_suspend_ptr();104105/* Restore original IRAM contents */106memcpy((void *) TEMP_IRAM_AREA, iram_swap_area,107lpc32xx_sys_suspend_sz);108109kfree(iram_swap_area);110111return 0;112}113114static const struct platform_suspend_ops lpc32xx_pm_ops = {115.valid = suspend_valid_only_mem,116.enter = lpc32xx_pm_enter,117};118119#define EMC_DYN_MEM_CTRL_OFS 0x20120#define EMC_SRMMC (1 << 3)121#define EMC_CTRL_REG io_p2v(LPC32XX_EMC_BASE + EMC_DYN_MEM_CTRL_OFS)122static int __init lpc32xx_pm_init(void)123{124/*125* Setup SDRAM self-refresh clock to automatically disable o126* start of self-refresh. This only needs to be done once.127*/128__raw_writel(__raw_readl(EMC_CTRL_REG) | EMC_SRMMC, EMC_CTRL_REG);129130suspend_set_ops(&lpc32xx_pm_ops);131132return 0;133}134arch_initcall(lpc32xx_pm_init);135136137