2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
3 * Author: Tony Xie <tony.xie@rock-chips.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/init.h>
18 #include <linux/kernel.h>
20 #include <linux/of_address.h>
21 #include <linux/regmap.h>
22 #include <linux/suspend.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/regulator/machine.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
28 #include <asm/suspend.h>
32 /* These enum are option of low power mode */
34 ROCKCHIP_ARM_OFF_LOGIC_NORMAL = 0,
35 ROCKCHIP_ARM_OFF_LOGIC_DEEP = 1,
38 struct rockchip_pm_data {
39 const struct platform_suspend_ops *ops;
40 int (*init)(struct device_node *np);
43 static void __iomem *rk3288_bootram_base;
44 static phys_addr_t rk3288_bootram_phy;
46 static struct regmap *pmu_regmap;
47 static struct regmap *sgrf_regmap;
48 static struct regmap *grf_regmap;
50 static u32 rk3288_pmu_pwr_mode_con;
51 static u32 rk3288_sgrf_soc_con0;
52 static u32 rk3288_sgrf_cpu_con0;
54 static inline u32 rk3288_l2_config(void)
58 asm("mrc p15, 1, %0, c9, c0, 2" : "=r" (l2ctlr));
62 static void rk3288_config_bootdata(void)
64 rkpm_bootdata_cpusp = rk3288_bootram_phy + (SZ_4K - 8);
65 rkpm_bootdata_cpu_code = virt_to_phys(cpu_resume);
67 rkpm_bootdata_l2ctlr_f = 1;
68 rkpm_bootdata_l2ctlr = rk3288_l2_config();
71 #define GRF_UOC0_CON0 0x320
72 #define GRF_UOC1_CON0 0x334
73 #define GRF_UOC2_CON0 0x348
74 #define GRF_SIDDQ BIT(13)
76 static bool rk3288_slp_disable_osc(void)
78 static const u32 reg_offset[] = { GRF_UOC0_CON0, GRF_UOC1_CON0,
83 * if any usb phy is still on(GRF_SIDDQ==0), that means we need the
84 * function of usb wakeup, so do not switch to 32khz, since the usb phy
85 * clk does not connect to 32khz osc
87 for (i = 0; i < ARRAY_SIZE(reg_offset); i++) {
88 regmap_read(grf_regmap, reg_offset[i], ®);
89 if (!(reg & GRF_SIDDQ))
96 static void rk3288_slp_mode_set(int level)
98 u32 mode_set, mode_set1;
99 bool osc_switch_to_32k = rk3288_slp_disable_osc();
101 regmap_read(sgrf_regmap, RK3288_SGRF_CPU_CON0, &rk3288_sgrf_cpu_con0);
102 regmap_read(sgrf_regmap, RK3288_SGRF_SOC_CON0, &rk3288_sgrf_soc_con0);
104 regmap_read(pmu_regmap, RK3288_PMU_PWRMODE_CON,
105 &rk3288_pmu_pwr_mode_con);
108 * SGRF_FAST_BOOT_EN - system to boot from FAST_BOOT_ADDR
109 * PCLK_WDT_GATE - disable WDT during suspend.
111 regmap_write(sgrf_regmap, RK3288_SGRF_SOC_CON0,
112 SGRF_PCLK_WDT_GATE | SGRF_FAST_BOOT_EN
113 | SGRF_PCLK_WDT_GATE_WRITE | SGRF_FAST_BOOT_EN_WRITE);
116 * The dapswjdp can not auto reset before resume, that cause it may
117 * access some illegal address during resume. Let's disable it before
118 * suspend, and the MASKROM will enable it back.
120 regmap_write(sgrf_regmap, RK3288_SGRF_CPU_CON0, SGRF_DAPDEVICEEN_WRITE);
122 /* booting address of resuming system is from this register value */
123 regmap_write(sgrf_regmap, RK3288_SGRF_FAST_BOOT_ADDR,
126 regmap_write(pmu_regmap, RK3288_PMU_WAKEUP_CFG1,
127 PMU_ARMINT_WAKEUP_EN);
129 mode_set = BIT(PMU_GLOBAL_INT_DISABLE) | BIT(PMU_L2FLUSH_EN) |
130 BIT(PMU_SREF0_ENTER_EN) | BIT(PMU_SREF1_ENTER_EN) |
131 BIT(PMU_DDR0_GATING_EN) | BIT(PMU_DDR1_GATING_EN) |
132 BIT(PMU_PWR_MODE_EN) | BIT(PMU_CHIP_PD_EN) |
135 mode_set1 = BIT(PMU_CLR_CORE) | BIT(PMU_CLR_CPUP);
137 if (level == ROCKCHIP_ARM_OFF_LOGIC_DEEP) {
138 /* arm off, logic deep sleep */
139 mode_set |= BIT(PMU_BUS_PD_EN) | BIT(PMU_PMU_USE_LF) |
140 BIT(PMU_DDR1IO_RET_EN) | BIT(PMU_DDR0IO_RET_EN) |
141 BIT(PMU_ALIVE_USE_LF) | BIT(PMU_PLL_PD_EN);
143 if (osc_switch_to_32k)
144 mode_set |= BIT(PMU_OSC_24M_DIS);
146 mode_set1 |= BIT(PMU_CLR_ALIVE) | BIT(PMU_CLR_BUS) |
147 BIT(PMU_CLR_PERI) | BIT(PMU_CLR_DMA);
150 * arm off, logic normal
151 * if pmu_clk_core_src_gate_en is not set,
152 * wakeup will be error
154 mode_set |= BIT(PMU_CLK_CORE_SRC_GATE_EN);
157 regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON, mode_set);
158 regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON1, mode_set1);
161 static void rk3288_slp_mode_set_resume(void)
163 regmap_write(sgrf_regmap, RK3288_SGRF_CPU_CON0,
164 rk3288_sgrf_cpu_con0 | SGRF_DAPDEVICEEN_WRITE);
166 regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON,
167 rk3288_pmu_pwr_mode_con);
169 regmap_write(sgrf_regmap, RK3288_SGRF_SOC_CON0,
170 rk3288_sgrf_soc_con0 | SGRF_PCLK_WDT_GATE_WRITE
171 | SGRF_FAST_BOOT_EN_WRITE);
174 static int rockchip_lpmode_enter(unsigned long arg)
180 pr_err("%s: Failed to suspend\n", __func__);
185 static int rk3288_suspend_enter(suspend_state_t state)
189 rk3288_slp_mode_set(ROCKCHIP_ARM_OFF_LOGIC_NORMAL);
191 cpu_suspend(0, rockchip_lpmode_enter);
193 rk3288_slp_mode_set_resume();
200 static int rk3288_suspend_prepare(void)
202 return regulator_suspend_prepare(PM_SUSPEND_MEM);
205 static void rk3288_suspend_finish(void)
207 if (regulator_suspend_finish())
208 pr_err("%s: Suspend finish failed\n", __func__);
211 static int rk3288_suspend_init(struct device_node *np)
213 struct device_node *sram_np;
217 pmu_regmap = syscon_node_to_regmap(np);
218 if (IS_ERR(pmu_regmap)) {
219 pr_err("%s: could not find pmu regmap\n", __func__);
220 return PTR_ERR(pmu_regmap);
223 sgrf_regmap = syscon_regmap_lookup_by_compatible(
224 "rockchip,rk3288-sgrf");
225 if (IS_ERR(sgrf_regmap)) {
226 pr_err("%s: could not find sgrf regmap\n", __func__);
227 return PTR_ERR(pmu_regmap);
230 grf_regmap = syscon_regmap_lookup_by_compatible(
231 "rockchip,rk3288-grf");
232 if (IS_ERR(grf_regmap)) {
233 pr_err("%s: could not find grf regmap\n", __func__);
234 return PTR_ERR(pmu_regmap);
237 sram_np = of_find_compatible_node(NULL, NULL,
238 "rockchip,rk3288-pmu-sram");
240 pr_err("%s: could not find bootram dt node\n", __func__);
244 rk3288_bootram_base = of_iomap(sram_np, 0);
245 if (!rk3288_bootram_base) {
246 pr_err("%s: could not map bootram base\n", __func__);
250 ret = of_address_to_resource(sram_np, 0, &res);
252 pr_err("%s: could not get bootram phy addr\n", __func__);
255 rk3288_bootram_phy = res.start;
257 of_node_put(sram_np);
259 rk3288_config_bootdata();
261 /* copy resume code and data to bootsram */
262 memcpy(rk3288_bootram_base, rockchip_slp_cpu_resume,
265 regmap_write(pmu_regmap, RK3288_PMU_OSC_CNT, OSC_STABL_CNT_THRESH);
266 regmap_write(pmu_regmap, RK3288_PMU_STABL_CNT, PMU_STABL_CNT_THRESH);
271 static const struct platform_suspend_ops rk3288_suspend_ops = {
272 .enter = rk3288_suspend_enter,
273 .valid = suspend_valid_only_mem,
274 .prepare = rk3288_suspend_prepare,
275 .finish = rk3288_suspend_finish,
278 static const struct rockchip_pm_data rk3288_pm_data __initconst = {
279 .ops = &rk3288_suspend_ops,
280 .init = rk3288_suspend_init,
283 static const struct of_device_id rockchip_pmu_of_device_ids[] __initconst = {
285 .compatible = "rockchip,rk3288-pmu",
286 .data = &rk3288_pm_data,
291 void __init rockchip_suspend_init(void)
293 const struct rockchip_pm_data *pm_data;
294 const struct of_device_id *match;
295 struct device_node *np;
298 np = of_find_matching_node_and_match(NULL, rockchip_pmu_of_device_ids,
301 pr_err("Failed to find PMU node\n");
304 pm_data = (struct rockchip_pm_data *) match->data;
307 ret = pm_data->init(np);
310 pr_err("%s: matches init error %d\n", __func__, ret);
315 suspend_set_ops(pm_data->ops);