12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29c1ec8e1SChris Zhong /*
39c1ec8e1SChris Zhong * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
49c1ec8e1SChris Zhong * Author: Tony Xie <tony.xie@rock-chips.com>
59c1ec8e1SChris Zhong */
69c1ec8e1SChris Zhong
79c1ec8e1SChris Zhong #include <linux/init.h>
89c1ec8e1SChris Zhong #include <linux/io.h>
99c1ec8e1SChris Zhong #include <linux/kernel.h>
109c1ec8e1SChris Zhong #include <linux/of.h>
119c1ec8e1SChris Zhong #include <linux/of_address.h>
129c1ec8e1SChris Zhong #include <linux/regmap.h>
139c1ec8e1SChris Zhong #include <linux/suspend.h>
149c1ec8e1SChris Zhong #include <linux/mfd/syscon.h>
159c1ec8e1SChris Zhong #include <linux/regulator/machine.h>
169c1ec8e1SChris Zhong
179c1ec8e1SChris Zhong #include <asm/cacheflush.h>
189c1ec8e1SChris Zhong #include <asm/tlbflush.h>
199c1ec8e1SChris Zhong #include <asm/suspend.h>
209c1ec8e1SChris Zhong
219c1ec8e1SChris Zhong #include "pm.h"
229c1ec8e1SChris Zhong
239c1ec8e1SChris Zhong /* These enum are option of low power mode */
249c1ec8e1SChris Zhong enum {
259c1ec8e1SChris Zhong ROCKCHIP_ARM_OFF_LOGIC_NORMAL = 0,
269c1ec8e1SChris Zhong ROCKCHIP_ARM_OFF_LOGIC_DEEP = 1,
279c1ec8e1SChris Zhong };
289c1ec8e1SChris Zhong
299c1ec8e1SChris Zhong struct rockchip_pm_data {
309c1ec8e1SChris Zhong const struct platform_suspend_ops *ops;
319c1ec8e1SChris Zhong int (*init)(struct device_node *np);
329c1ec8e1SChris Zhong };
339c1ec8e1SChris Zhong
349c1ec8e1SChris Zhong static void __iomem *rk3288_bootram_base;
359c1ec8e1SChris Zhong static phys_addr_t rk3288_bootram_phy;
369c1ec8e1SChris Zhong
379c1ec8e1SChris Zhong static struct regmap *pmu_regmap;
389c1ec8e1SChris Zhong static struct regmap *sgrf_regmap;
39134f1f60SChris Zhong static struct regmap *grf_regmap;
409c1ec8e1SChris Zhong
419c1ec8e1SChris Zhong static u32 rk3288_pmu_pwr_mode_con;
429c1ec8e1SChris Zhong static u32 rk3288_sgrf_soc_con0;
4333939403SDoug Anderson static u32 rk3288_sgrf_cpu_con0;
449c1ec8e1SChris Zhong
rk3288_l2_config(void)459c1ec8e1SChris Zhong static inline u32 rk3288_l2_config(void)
469c1ec8e1SChris Zhong {
479c1ec8e1SChris Zhong u32 l2ctlr;
489c1ec8e1SChris Zhong
499c1ec8e1SChris Zhong asm("mrc p15, 1, %0, c9, c0, 2" : "=r" (l2ctlr));
509c1ec8e1SChris Zhong return l2ctlr;
519c1ec8e1SChris Zhong }
529c1ec8e1SChris Zhong
rk3288_config_bootdata(void)532dd00d31SDouglas Anderson static void __init rk3288_config_bootdata(void)
549c1ec8e1SChris Zhong {
559c1ec8e1SChris Zhong rkpm_bootdata_cpusp = rk3288_bootram_phy + (SZ_4K - 8);
5664fc2a94SFlorian Fainelli rkpm_bootdata_cpu_code = __pa_symbol(cpu_resume);
579c1ec8e1SChris Zhong
589c1ec8e1SChris Zhong rkpm_bootdata_l2ctlr_f = 1;
599c1ec8e1SChris Zhong rkpm_bootdata_l2ctlr = rk3288_l2_config();
609c1ec8e1SChris Zhong }
619c1ec8e1SChris Zhong
62134f1f60SChris Zhong #define GRF_UOC0_CON0 0x320
63134f1f60SChris Zhong #define GRF_UOC1_CON0 0x334
64134f1f60SChris Zhong #define GRF_UOC2_CON0 0x348
65134f1f60SChris Zhong #define GRF_SIDDQ BIT(13)
66134f1f60SChris Zhong
rk3288_slp_disable_osc(void)67134f1f60SChris Zhong static bool rk3288_slp_disable_osc(void)
68134f1f60SChris Zhong {
69134f1f60SChris Zhong static const u32 reg_offset[] = { GRF_UOC0_CON0, GRF_UOC1_CON0,
70134f1f60SChris Zhong GRF_UOC2_CON0 };
71134f1f60SChris Zhong u32 reg, i;
72134f1f60SChris Zhong
73134f1f60SChris Zhong /*
74134f1f60SChris Zhong * if any usb phy is still on(GRF_SIDDQ==0), that means we need the
75134f1f60SChris Zhong * function of usb wakeup, so do not switch to 32khz, since the usb phy
76134f1f60SChris Zhong * clk does not connect to 32khz osc
77134f1f60SChris Zhong */
78134f1f60SChris Zhong for (i = 0; i < ARRAY_SIZE(reg_offset); i++) {
79134f1f60SChris Zhong regmap_read(grf_regmap, reg_offset[i], ®);
80134f1f60SChris Zhong if (!(reg & GRF_SIDDQ))
81134f1f60SChris Zhong return false;
82134f1f60SChris Zhong }
83134f1f60SChris Zhong
84134f1f60SChris Zhong return true;
85134f1f60SChris Zhong }
86134f1f60SChris Zhong
rk3288_slp_mode_set(int level)879c1ec8e1SChris Zhong static void rk3288_slp_mode_set(int level)
889c1ec8e1SChris Zhong {
899c1ec8e1SChris Zhong u32 mode_set, mode_set1;
9041fe6a01SHeiko Stuebner bool osc_disable = rk3288_slp_disable_osc();
919c1ec8e1SChris Zhong
9233939403SDoug Anderson regmap_read(sgrf_regmap, RK3288_SGRF_CPU_CON0, &rk3288_sgrf_cpu_con0);
939c1ec8e1SChris Zhong regmap_read(sgrf_regmap, RK3288_SGRF_SOC_CON0, &rk3288_sgrf_soc_con0);
949c1ec8e1SChris Zhong
959c1ec8e1SChris Zhong regmap_read(pmu_regmap, RK3288_PMU_PWRMODE_CON,
969c1ec8e1SChris Zhong &rk3288_pmu_pwr_mode_con);
979c1ec8e1SChris Zhong
98a0307d18SChris Zhong /*
99a0307d18SChris Zhong * SGRF_FAST_BOOT_EN - system to boot from FAST_BOOT_ADDR
100a0307d18SChris Zhong * PCLK_WDT_GATE - disable WDT during suspend.
101a0307d18SChris Zhong */
1029c1ec8e1SChris Zhong regmap_write(sgrf_regmap, RK3288_SGRF_SOC_CON0,
103a0307d18SChris Zhong SGRF_PCLK_WDT_GATE | SGRF_FAST_BOOT_EN
104a0307d18SChris Zhong | SGRF_PCLK_WDT_GATE_WRITE | SGRF_FAST_BOOT_EN_WRITE);
1059c1ec8e1SChris Zhong
1060ea001d3SChris Zhong /*
1070ea001d3SChris Zhong * The dapswjdp can not auto reset before resume, that cause it may
1080ea001d3SChris Zhong * access some illegal address during resume. Let's disable it before
1090ea001d3SChris Zhong * suspend, and the MASKROM will enable it back.
1100ea001d3SChris Zhong */
1110ea001d3SChris Zhong regmap_write(sgrf_regmap, RK3288_SGRF_CPU_CON0, SGRF_DAPDEVICEEN_WRITE);
1120ea001d3SChris Zhong
1139c1ec8e1SChris Zhong /* booting address of resuming system is from this register value */
1149c1ec8e1SChris Zhong regmap_write(sgrf_regmap, RK3288_SGRF_FAST_BOOT_ADDR,
1159c1ec8e1SChris Zhong rk3288_bootram_phy);
1169c1ec8e1SChris Zhong
1179c1ec8e1SChris Zhong mode_set = BIT(PMU_GLOBAL_INT_DISABLE) | BIT(PMU_L2FLUSH_EN) |
1189c1ec8e1SChris Zhong BIT(PMU_SREF0_ENTER_EN) | BIT(PMU_SREF1_ENTER_EN) |
1199c1ec8e1SChris Zhong BIT(PMU_DDR0_GATING_EN) | BIT(PMU_DDR1_GATING_EN) |
1209c1ec8e1SChris Zhong BIT(PMU_PWR_MODE_EN) | BIT(PMU_CHIP_PD_EN) |
1219c1ec8e1SChris Zhong BIT(PMU_SCU_EN);
1229c1ec8e1SChris Zhong
1239c1ec8e1SChris Zhong mode_set1 = BIT(PMU_CLR_CORE) | BIT(PMU_CLR_CPUP);
1249c1ec8e1SChris Zhong
1259c1ec8e1SChris Zhong if (level == ROCKCHIP_ARM_OFF_LOGIC_DEEP) {
1269c1ec8e1SChris Zhong /* arm off, logic deep sleep */
127134f1f60SChris Zhong mode_set |= BIT(PMU_BUS_PD_EN) | BIT(PMU_PMU_USE_LF) |
1289c1ec8e1SChris Zhong BIT(PMU_DDR1IO_RET_EN) | BIT(PMU_DDR0IO_RET_EN) |
1299c1ec8e1SChris Zhong BIT(PMU_ALIVE_USE_LF) | BIT(PMU_PLL_PD_EN);
1309c1ec8e1SChris Zhong
13141fe6a01SHeiko Stuebner if (osc_disable)
132134f1f60SChris Zhong mode_set |= BIT(PMU_OSC_24M_DIS);
133134f1f60SChris Zhong
1349c1ec8e1SChris Zhong mode_set1 |= BIT(PMU_CLR_ALIVE) | BIT(PMU_CLR_BUS) |
1359c1ec8e1SChris Zhong BIT(PMU_CLR_PERI) | BIT(PMU_CLR_DMA);
136d1d3a1a1SHeiko Stuebner
1379bb91ae9SHeiko Stuebner regmap_write(pmu_regmap, RK3288_PMU_WAKEUP_CFG1,
1389bb91ae9SHeiko Stuebner PMU_ARMINT_WAKEUP_EN);
1399bb91ae9SHeiko Stuebner
140d1d3a1a1SHeiko Stuebner /*
141d1d3a1a1SHeiko Stuebner * In deep suspend we use PMU_PMU_USE_LF to let the rk3288
142d1d3a1a1SHeiko Stuebner * switch its main clock supply to the alternative 32kHz
143d1d3a1a1SHeiko Stuebner * source. Therefore set 30ms on a 32kHz clock for pmic
144d1d3a1a1SHeiko Stuebner * stabilization. Similar 30ms on 24MHz for the other
145d1d3a1a1SHeiko Stuebner * mode below.
146d1d3a1a1SHeiko Stuebner */
147d1d3a1a1SHeiko Stuebner regmap_write(pmu_regmap, RK3288_PMU_STABL_CNT, 32 * 30);
148d1d3a1a1SHeiko Stuebner
149d1d3a1a1SHeiko Stuebner /* only wait for stabilization, if we turned the osc off */
150d1d3a1a1SHeiko Stuebner regmap_write(pmu_regmap, RK3288_PMU_OSC_CNT,
151d1d3a1a1SHeiko Stuebner osc_disable ? 32 * 30 : 0);
1529c1ec8e1SChris Zhong } else {
1539c1ec8e1SChris Zhong /*
1549c1ec8e1SChris Zhong * arm off, logic normal
1559c1ec8e1SChris Zhong * if pmu_clk_core_src_gate_en is not set,
1569c1ec8e1SChris Zhong * wakeup will be error
1579c1ec8e1SChris Zhong */
1589c1ec8e1SChris Zhong mode_set |= BIT(PMU_CLK_CORE_SRC_GATE_EN);
159d1d3a1a1SHeiko Stuebner
1609bb91ae9SHeiko Stuebner regmap_write(pmu_regmap, RK3288_PMU_WAKEUP_CFG1,
1619bb91ae9SHeiko Stuebner PMU_ARMINT_WAKEUP_EN | PMU_GPIOINT_WAKEUP_EN);
1629bb91ae9SHeiko Stuebner
163d1d3a1a1SHeiko Stuebner /* 30ms on a 24MHz clock for pmic stabilization */
164d1d3a1a1SHeiko Stuebner regmap_write(pmu_regmap, RK3288_PMU_STABL_CNT, 24000 * 30);
165d1d3a1a1SHeiko Stuebner
166d1d3a1a1SHeiko Stuebner /* oscillator is still running, so no need to wait */
167d1d3a1a1SHeiko Stuebner regmap_write(pmu_regmap, RK3288_PMU_OSC_CNT, 0);
1689c1ec8e1SChris Zhong }
1699c1ec8e1SChris Zhong
1709c1ec8e1SChris Zhong regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON, mode_set);
1719c1ec8e1SChris Zhong regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON1, mode_set1);
1729c1ec8e1SChris Zhong }
1739c1ec8e1SChris Zhong
rk3288_slp_mode_set_resume(void)1749c1ec8e1SChris Zhong static void rk3288_slp_mode_set_resume(void)
1759c1ec8e1SChris Zhong {
17633939403SDoug Anderson regmap_write(sgrf_regmap, RK3288_SGRF_CPU_CON0,
17733939403SDoug Anderson rk3288_sgrf_cpu_con0 | SGRF_DAPDEVICEEN_WRITE);
17833939403SDoug Anderson
1799c1ec8e1SChris Zhong regmap_write(pmu_regmap, RK3288_PMU_PWRMODE_CON,
1809c1ec8e1SChris Zhong rk3288_pmu_pwr_mode_con);
1819c1ec8e1SChris Zhong
1829c1ec8e1SChris Zhong regmap_write(sgrf_regmap, RK3288_SGRF_SOC_CON0,
183a0307d18SChris Zhong rk3288_sgrf_soc_con0 | SGRF_PCLK_WDT_GATE_WRITE
184a0307d18SChris Zhong | SGRF_FAST_BOOT_EN_WRITE);
1859c1ec8e1SChris Zhong }
1869c1ec8e1SChris Zhong
rockchip_lpmode_enter(unsigned long arg)1879c1ec8e1SChris Zhong static int rockchip_lpmode_enter(unsigned long arg)
1889c1ec8e1SChris Zhong {
1899c1ec8e1SChris Zhong flush_cache_all();
1909c1ec8e1SChris Zhong
1919c1ec8e1SChris Zhong cpu_do_idle();
1929c1ec8e1SChris Zhong
1939c1ec8e1SChris Zhong pr_err("%s: Failed to suspend\n", __func__);
1949c1ec8e1SChris Zhong
1959c1ec8e1SChris Zhong return 1;
1969c1ec8e1SChris Zhong }
1979c1ec8e1SChris Zhong
rk3288_suspend_enter(suspend_state_t state)1989c1ec8e1SChris Zhong static int rk3288_suspend_enter(suspend_state_t state)
1999c1ec8e1SChris Zhong {
2009c1ec8e1SChris Zhong local_fiq_disable();
2019c1ec8e1SChris Zhong
2029c1ec8e1SChris Zhong rk3288_slp_mode_set(ROCKCHIP_ARM_OFF_LOGIC_NORMAL);
2039c1ec8e1SChris Zhong
2049c1ec8e1SChris Zhong cpu_suspend(0, rockchip_lpmode_enter);
2059c1ec8e1SChris Zhong
2069c1ec8e1SChris Zhong rk3288_slp_mode_set_resume();
2079c1ec8e1SChris Zhong
2089c1ec8e1SChris Zhong local_fiq_enable();
2099c1ec8e1SChris Zhong
2109c1ec8e1SChris Zhong return 0;
2119c1ec8e1SChris Zhong }
2129c1ec8e1SChris Zhong
rk3288_suspend_prepare(void)2139c1ec8e1SChris Zhong static int rk3288_suspend_prepare(void)
2149c1ec8e1SChris Zhong {
2159c1ec8e1SChris Zhong return regulator_suspend_prepare(PM_SUSPEND_MEM);
2169c1ec8e1SChris Zhong }
2179c1ec8e1SChris Zhong
rk3288_suspend_finish(void)2189c1ec8e1SChris Zhong static void rk3288_suspend_finish(void)
2199c1ec8e1SChris Zhong {
2209c1ec8e1SChris Zhong if (regulator_suspend_finish())
2219c1ec8e1SChris Zhong pr_err("%s: Suspend finish failed\n", __func__);
2229c1ec8e1SChris Zhong }
2239c1ec8e1SChris Zhong
rk3288_suspend_init(struct device_node * np)2242dd00d31SDouglas Anderson static int __init rk3288_suspend_init(struct device_node *np)
2259c1ec8e1SChris Zhong {
2269c1ec8e1SChris Zhong struct device_node *sram_np;
2279c1ec8e1SChris Zhong struct resource res;
2289c1ec8e1SChris Zhong int ret;
2299c1ec8e1SChris Zhong
2309c1ec8e1SChris Zhong pmu_regmap = syscon_node_to_regmap(np);
2319c1ec8e1SChris Zhong if (IS_ERR(pmu_regmap)) {
2329c1ec8e1SChris Zhong pr_err("%s: could not find pmu regmap\n", __func__);
2339c1ec8e1SChris Zhong return PTR_ERR(pmu_regmap);
2349c1ec8e1SChris Zhong }
2359c1ec8e1SChris Zhong
2369c1ec8e1SChris Zhong sgrf_regmap = syscon_regmap_lookup_by_compatible(
2379c1ec8e1SChris Zhong "rockchip,rk3288-sgrf");
2389c1ec8e1SChris Zhong if (IS_ERR(sgrf_regmap)) {
2399c1ec8e1SChris Zhong pr_err("%s: could not find sgrf regmap\n", __func__);
2402a03c025SFabio Estevam return PTR_ERR(sgrf_regmap);
2419c1ec8e1SChris Zhong }
2429c1ec8e1SChris Zhong
243134f1f60SChris Zhong grf_regmap = syscon_regmap_lookup_by_compatible(
244134f1f60SChris Zhong "rockchip,rk3288-grf");
245134f1f60SChris Zhong if (IS_ERR(grf_regmap)) {
246134f1f60SChris Zhong pr_err("%s: could not find grf regmap\n", __func__);
2472a03c025SFabio Estevam return PTR_ERR(grf_regmap);
248134f1f60SChris Zhong }
249134f1f60SChris Zhong
2509c1ec8e1SChris Zhong sram_np = of_find_compatible_node(NULL, NULL,
2519c1ec8e1SChris Zhong "rockchip,rk3288-pmu-sram");
2529c1ec8e1SChris Zhong if (!sram_np) {
2539c1ec8e1SChris Zhong pr_err("%s: could not find bootram dt node\n", __func__);
2549c1ec8e1SChris Zhong return -ENODEV;
2559c1ec8e1SChris Zhong }
2569c1ec8e1SChris Zhong
2579c1ec8e1SChris Zhong rk3288_bootram_base = of_iomap(sram_np, 0);
2589c1ec8e1SChris Zhong if (!rk3288_bootram_base) {
2599c1ec8e1SChris Zhong pr_err("%s: could not map bootram base\n", __func__);
260c2af88f1SWen Yang of_node_put(sram_np);
2619c1ec8e1SChris Zhong return -ENOMEM;
2629c1ec8e1SChris Zhong }
2639c1ec8e1SChris Zhong
2649c1ec8e1SChris Zhong ret = of_address_to_resource(sram_np, 0, &res);
2659c1ec8e1SChris Zhong if (ret) {
2669c1ec8e1SChris Zhong pr_err("%s: could not get bootram phy addr\n", __func__);
267c2af88f1SWen Yang of_node_put(sram_np);
2689c1ec8e1SChris Zhong return ret;
2699c1ec8e1SChris Zhong }
2709c1ec8e1SChris Zhong rk3288_bootram_phy = res.start;
2719c1ec8e1SChris Zhong
2729c1ec8e1SChris Zhong of_node_put(sram_np);
2739c1ec8e1SChris Zhong
2749c1ec8e1SChris Zhong rk3288_config_bootdata();
2759c1ec8e1SChris Zhong
2769c1ec8e1SChris Zhong /* copy resume code and data to bootsram */
2779c1ec8e1SChris Zhong memcpy(rk3288_bootram_base, rockchip_slp_cpu_resume,
2789c1ec8e1SChris Zhong rk3288_bootram_sz);
2799c1ec8e1SChris Zhong
2809c1ec8e1SChris Zhong return 0;
2819c1ec8e1SChris Zhong }
2829c1ec8e1SChris Zhong
2839c1ec8e1SChris Zhong static const struct platform_suspend_ops rk3288_suspend_ops = {
2849c1ec8e1SChris Zhong .enter = rk3288_suspend_enter,
2859c1ec8e1SChris Zhong .valid = suspend_valid_only_mem,
2869c1ec8e1SChris Zhong .prepare = rk3288_suspend_prepare,
2879c1ec8e1SChris Zhong .finish = rk3288_suspend_finish,
2889c1ec8e1SChris Zhong };
2899c1ec8e1SChris Zhong
2909c1ec8e1SChris Zhong static const struct rockchip_pm_data rk3288_pm_data __initconst = {
2919c1ec8e1SChris Zhong .ops = &rk3288_suspend_ops,
2929c1ec8e1SChris Zhong .init = rk3288_suspend_init,
2939c1ec8e1SChris Zhong };
2949c1ec8e1SChris Zhong
2959c1ec8e1SChris Zhong static const struct of_device_id rockchip_pmu_of_device_ids[] __initconst = {
2969c1ec8e1SChris Zhong {
2979c1ec8e1SChris Zhong .compatible = "rockchip,rk3288-pmu",
2989c1ec8e1SChris Zhong .data = &rk3288_pm_data,
2999c1ec8e1SChris Zhong },
3009c1ec8e1SChris Zhong { /* sentinel */ },
3019c1ec8e1SChris Zhong };
3029c1ec8e1SChris Zhong
rockchip_suspend_init(void)3039c1ec8e1SChris Zhong void __init rockchip_suspend_init(void)
3049c1ec8e1SChris Zhong {
3059c1ec8e1SChris Zhong const struct rockchip_pm_data *pm_data;
3069c1ec8e1SChris Zhong const struct of_device_id *match;
3079c1ec8e1SChris Zhong struct device_node *np;
3089c1ec8e1SChris Zhong int ret;
3099c1ec8e1SChris Zhong
3109c1ec8e1SChris Zhong np = of_find_matching_node_and_match(NULL, rockchip_pmu_of_device_ids,
3119c1ec8e1SChris Zhong &match);
3129c1ec8e1SChris Zhong if (!match) {
3139c1ec8e1SChris Zhong pr_err("Failed to find PMU node\n");
314*f4470dbfSLiang He goto out_put;
3159c1ec8e1SChris Zhong }
3169c1ec8e1SChris Zhong pm_data = (struct rockchip_pm_data *) match->data;
3179c1ec8e1SChris Zhong
3189c1ec8e1SChris Zhong if (pm_data->init) {
3199c1ec8e1SChris Zhong ret = pm_data->init(np);
3209c1ec8e1SChris Zhong
3219c1ec8e1SChris Zhong if (ret) {
3229c1ec8e1SChris Zhong pr_err("%s: matches init error %d\n", __func__, ret);
323*f4470dbfSLiang He goto out_put;
3249c1ec8e1SChris Zhong }
3259c1ec8e1SChris Zhong }
3269c1ec8e1SChris Zhong
3279c1ec8e1SChris Zhong suspend_set_ops(pm_data->ops);
328*f4470dbfSLiang He
329*f4470dbfSLiang He out_put:
330*f4470dbfSLiang He of_node_put(np);
3319c1ec8e1SChris Zhong }
332