1 // SPDX-License-Identifier: GPL-2.0
3 * AM33XX Arch Power Management Routines
5 * Copyright (C) 2016-2018 Texas Instruments Incorporated - https://www.ti.com/
9 #include <linux/cpuidle.h>
10 #include <linux/platform_data/pm33xx.h>
11 #include <linux/suspend.h>
12 #include <asm/cpuidle.h>
13 #include <asm/smp_scu.h>
14 #include <asm/suspend.h>
15 #include <linux/errno.h>
16 #include <linux/clk.h>
17 #include <linux/cpu.h>
18 #include <linux/platform_data/gpio-omap.h>
19 #include <linux/wkup_m3_ipc.h>
21 #include <linux/rtc.h>
26 #include "clockdomain.h"
29 #include "powerdomain.h"
33 #include "omap-secure.h"
35 static struct powerdomain *cefuse_pwrdm, *gfx_pwrdm, *per_pwrdm, *mpu_pwrdm;
36 static struct clockdomain *gfx_l4ls_clkdm;
37 static void __iomem *scu_base;
39 static int (*idle_fn)(u32 wfi_flags);
41 struct amx3_idle_state {
45 static struct amx3_idle_state *idle_states;
47 static int am43xx_map_scu(void)
49 scu_base = ioremap(scu_a9_get_base(), SZ_256);
57 static int am33xx_check_off_mode_enable(void)
60 pr_warn("WARNING: This platform does not support off-mode, entering DeepSleep suspend.\n");
62 /* off mode not supported on am335x so return 0 always */
66 static int am43xx_check_off_mode_enable(void)
69 * Check for am437x-gp-evm which has the right Hardware design to
70 * support this mode reliably.
72 if (of_machine_is_compatible("ti,am437x-gp-evm") && enable_off_mode)
73 return enable_off_mode;
74 else if (enable_off_mode)
75 pr_warn("WARNING: This platform does not support off-mode, entering DeepSleep suspend.\n");
80 static int amx3_common_init(int (*idle)(u32 wfi_flags))
82 gfx_pwrdm = pwrdm_lookup("gfx_pwrdm");
83 per_pwrdm = pwrdm_lookup("per_pwrdm");
84 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
86 if ((!gfx_pwrdm) || (!per_pwrdm) || (!mpu_pwrdm))
89 (void)clkdm_for_each(omap_pm_clkdms_setup, NULL);
91 /* CEFUSE domain can be turned off post bootup */
92 cefuse_pwrdm = pwrdm_lookup("cefuse_pwrdm");
94 pr_err("PM: Failed to get cefuse_pwrdm\n");
95 else if (omap_type() != OMAP2_DEVICE_TYPE_GP)
96 pr_info("PM: Leaving EFUSE power domain active\n");
98 omap_set_pwrdm_state(cefuse_pwrdm, PWRDM_POWER_OFF);
105 static int am33xx_suspend_init(int (*idle)(u32 wfi_flags))
107 gfx_l4ls_clkdm = clkdm_lookup("gfx_l4ls_gfx_clkdm");
109 if (!gfx_l4ls_clkdm) {
110 pr_err("PM: Cannot lookup gfx_l4ls_clkdm clockdomains\n");
114 return amx3_common_init(idle);
117 static int am43xx_suspend_init(int (*idle)(u32 wfi_flags))
121 ret = am43xx_map_scu();
123 pr_err("PM: Could not ioremap SCU\n");
127 ret = amx3_common_init(idle);
132 static int amx3_suspend_deinit(void)
138 static void amx3_pre_suspend_common(void)
140 omap_set_pwrdm_state(gfx_pwrdm, PWRDM_POWER_OFF);
143 static void amx3_post_suspend_common(void)
147 * Because gfx_pwrdm is the only one under MPU control,
148 * comment on transition status
150 status = pwrdm_read_pwrst(gfx_pwrdm);
151 if (status != PWRDM_POWER_OFF)
152 pr_err("PM: GFX domain did not transition: %x\n", status);
155 static int am33xx_suspend(unsigned int state, int (*fn)(unsigned long),
160 amx3_pre_suspend_common();
161 ret = cpu_suspend(args, fn);
162 amx3_post_suspend_common();
165 * BUG: GFX_L4LS clock domain needs to be woken up to
166 * ensure thet L4LS clock domain does not get stuck in
167 * transition. If that happens L3 module does not get
168 * disabled, thereby leading to PER power domain
172 clkdm_wakeup(gfx_l4ls_clkdm);
173 clkdm_sleep(gfx_l4ls_clkdm);
178 static int am43xx_suspend(unsigned int state, int (*fn)(unsigned long),
183 /* Suspend secure side on HS devices */
184 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
186 omap_smccc_smc(AM43xx_PPA_SVC_PM_SUSPEND, 0);
188 omap_secure_dispatcher(AM43xx_PPA_SVC_PM_SUSPEND,
193 amx3_pre_suspend_common();
194 scu_power_mode(scu_base, SCU_PM_POWEROFF);
195 ret = cpu_suspend(args, fn);
196 scu_power_mode(scu_base, SCU_PM_NORMAL);
198 if (!am43xx_check_off_mode_enable())
199 amx3_post_suspend_common();
202 * Resume secure side on HS devices.
204 * Note that even on systems with OP-TEE available this resume call is
205 * issued to the ROM. This is because upon waking from suspend the ROM
206 * is restored as the secure monitor. On systems with OP-TEE ROM will
207 * restore OP-TEE during this call.
209 if (omap_type() != OMAP2_DEVICE_TYPE_GP)
210 omap_secure_dispatcher(AM43xx_PPA_SVC_PM_RESUME,
217 static int am33xx_cpu_suspend(int (*fn)(unsigned long), unsigned long args)
221 if (omap_irq_pending() || need_resched())
224 ret = cpu_suspend(args, fn);
229 static int am43xx_cpu_suspend(int (*fn)(unsigned long), unsigned long args)
236 scu_power_mode(scu_base, SCU_PM_DORMANT);
237 ret = cpu_suspend(args, fn);
238 scu_power_mode(scu_base, SCU_PM_NORMAL);
243 static void amx3_begin_suspend(void)
245 cpu_idle_poll_ctrl(true);
248 static void amx3_finish_suspend(void)
250 cpu_idle_poll_ctrl(false);
254 static struct am33xx_pm_sram_addr *amx3_get_sram_addrs(void)
257 return &am33xx_pm_sram;
258 else if (soc_is_am437x())
259 return &am43xx_pm_sram;
264 static void am43xx_save_context(void)
268 static void am33xx_save_context(void)
270 omap_intc_save_context();
273 static void am33xx_restore_context(void)
275 omap_intc_restore_context();
278 static void am43xx_restore_context(void)
281 * HACK: restore dpll_per_clkdcoldo register contents, to avoid
282 * breaking suspend-resume
284 writel_relaxed(0x0, AM33XX_L4_WK_IO_ADDRESS(0x44df2e14));
287 static struct am33xx_pm_platform_data am33xx_ops = {
288 .init = am33xx_suspend_init,
289 .deinit = amx3_suspend_deinit,
290 .soc_suspend = am33xx_suspend,
291 .cpu_suspend = am33xx_cpu_suspend,
292 .begin_suspend = amx3_begin_suspend,
293 .finish_suspend = amx3_finish_suspend,
294 .get_sram_addrs = amx3_get_sram_addrs,
295 .save_context = am33xx_save_context,
296 .restore_context = am33xx_restore_context,
297 .check_off_mode_enable = am33xx_check_off_mode_enable,
300 static struct am33xx_pm_platform_data am43xx_ops = {
301 .init = am43xx_suspend_init,
302 .deinit = amx3_suspend_deinit,
303 .soc_suspend = am43xx_suspend,
304 .cpu_suspend = am43xx_cpu_suspend,
305 .begin_suspend = amx3_begin_suspend,
306 .finish_suspend = amx3_finish_suspend,
307 .get_sram_addrs = amx3_get_sram_addrs,
308 .save_context = am43xx_save_context,
309 .restore_context = am43xx_restore_context,
310 .check_off_mode_enable = am43xx_check_off_mode_enable,
313 static struct am33xx_pm_platform_data *am33xx_pm_get_pdata(void)
317 else if (soc_is_am437x())
323 #ifdef CONFIG_SUSPEND
325 * Block system suspend initially. Later on pm33xx sets up it's own
326 * platform_suspend_ops after probe. That depends also on loaded
327 * wkup_m3_ipc /*(DEBLOBBED)*/
328 static int amx3_suspend_block(suspend_state_t state)
330 pr_warn("PM not initialized for pm33xx, wkup_m3_ipc, or /*(DEBLOBBED)*/\n");
335 static int amx3_pm_valid(suspend_state_t state)
338 case PM_SUSPEND_STANDBY:
345 static const struct platform_suspend_ops amx3_blocked_pm_ops = {
346 .begin = amx3_suspend_block,
347 .valid = amx3_pm_valid,
350 static void __init amx3_block_suspend(void)
352 suspend_set_ops(&amx3_blocked_pm_ops);
355 static inline void amx3_block_suspend(void)
358 #endif /* CONFIG_SUSPEND */
360 int __init amx3_common_pm_init(void)
362 struct am33xx_pm_platform_data *pdata;
363 struct platform_device_info devinfo;
365 pdata = am33xx_pm_get_pdata();
367 memset(&devinfo, 0, sizeof(devinfo));
368 devinfo.name = "pm33xx";
369 devinfo.data = pdata;
370 devinfo.size_data = sizeof(*pdata);
372 platform_device_register_full(&devinfo);
373 amx3_block_suspend();
378 static int __init amx3_idle_init(struct device_node *cpu_node, int cpu)
380 struct device_node *state_node;
381 struct amx3_idle_state states[CPUIDLE_STATE_MAX];
386 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
390 if (!of_device_is_available(state_node))
393 if (i == CPUIDLE_STATE_MAX) {
394 pr_warn("%s: cpuidle states reached max possible\n",
399 states[state_count].wfi_flags = 0;
401 if (of_property_read_bool(state_node, "ti,idle-wkup-m3"))
402 states[state_count].wfi_flags |= WFI_FLAG_WAKE_M3 |
403 WFI_FLAG_FLUSH_CACHE;
408 idle_states = kcalloc(state_count, sizeof(*idle_states), GFP_KERNEL);
412 for (i = 1; i < state_count; i++)
413 idle_states[i].wfi_flags = states[i].wfi_flags;
418 static int amx3_idle_enter(unsigned long index)
420 struct amx3_idle_state *idle_state = &idle_states[index];
426 idle_fn(idle_state->wfi_flags);
431 static struct cpuidle_ops amx3_cpuidle_ops __initdata = {
432 .init = amx3_idle_init,
433 .suspend = amx3_idle_enter,
436 CPUIDLE_METHOD_OF_DECLARE(pm33xx_idle, "ti,am3352", &amx3_cpuidle_ops);
437 CPUIDLE_METHOD_OF_DECLARE(pm43xx_idle, "ti,am4372", &amx3_cpuidle_ops);