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/pinctrl/pinmux.h>
20 #include <linux/wkup_m3_ipc.h>
22 #include <linux/rtc.h>
27 #include "clockdomain.h"
30 #include "powerdomain.h"
34 #include "omap-secure.h"
36 static struct powerdomain *cefuse_pwrdm, *gfx_pwrdm, *per_pwrdm, *mpu_pwrdm;
37 static struct clockdomain *gfx_l4ls_clkdm;
38 static void __iomem *scu_base;
40 static int (*idle_fn)(u32 wfi_flags);
42 struct amx3_idle_state {
46 static struct amx3_idle_state *idle_states;
48 static int am43xx_map_scu(void)
50 scu_base = ioremap(scu_a9_get_base(), SZ_256);
58 static int am33xx_check_off_mode_enable(void)
61 pr_warn("WARNING: This platform does not support off-mode, entering DeepSleep suspend.\n");
63 /* off mode not supported on am335x so return 0 always */
67 static int am43xx_check_off_mode_enable(void)
70 * Check for am437x-gp-evm which has the right Hardware design to
71 * support this mode reliably.
73 if (of_machine_is_compatible("ti,am437x-gp-evm") && enable_off_mode)
74 return enable_off_mode;
75 else if (enable_off_mode)
76 pr_warn("WARNING: This platform does not support off-mode, entering DeepSleep suspend.\n");
81 static int amx3_common_init(int (*idle)(u32 wfi_flags))
83 gfx_pwrdm = pwrdm_lookup("gfx_pwrdm");
84 per_pwrdm = pwrdm_lookup("per_pwrdm");
85 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
87 if ((!gfx_pwrdm) || (!per_pwrdm) || (!mpu_pwrdm))
90 (void)clkdm_for_each(omap_pm_clkdms_setup, NULL);
92 /* CEFUSE domain can be turned off post bootup */
93 cefuse_pwrdm = pwrdm_lookup("cefuse_pwrdm");
95 pr_err("PM: Failed to get cefuse_pwrdm\n");
96 else if (omap_type() != OMAP2_DEVICE_TYPE_GP)
97 pr_info("PM: Leaving EFUSE power domain active\n");
99 omap_set_pwrdm_state(cefuse_pwrdm, PWRDM_POWER_OFF);
106 static int am33xx_suspend_init(int (*idle)(u32 wfi_flags))
110 gfx_l4ls_clkdm = clkdm_lookup("gfx_l4ls_gfx_clkdm");
112 if (!gfx_l4ls_clkdm) {
113 pr_err("PM: Cannot lookup gfx_l4ls_clkdm clockdomains\n");
117 ret = amx3_common_init(idle);
122 static int am43xx_suspend_init(int (*idle)(u32 wfi_flags))
126 ret = am43xx_map_scu();
128 pr_err("PM: Could not ioremap SCU\n");
132 ret = amx3_common_init(idle);
137 static int amx3_suspend_deinit(void)
143 static void amx3_pre_suspend_common(void)
145 omap_set_pwrdm_state(gfx_pwrdm, PWRDM_POWER_OFF);
148 static void amx3_post_suspend_common(void)
152 * Because gfx_pwrdm is the only one under MPU control,
153 * comment on transition status
155 status = pwrdm_read_pwrst(gfx_pwrdm);
156 if (status != PWRDM_POWER_OFF)
157 pr_err("PM: GFX domain did not transition: %x\n", status);
160 static int am33xx_suspend(unsigned int state, int (*fn)(unsigned long),
165 amx3_pre_suspend_common();
166 ret = cpu_suspend(args, fn);
167 amx3_post_suspend_common();
170 * BUG: GFX_L4LS clock domain needs to be woken up to
171 * ensure thet L4LS clock domain does not get stuck in
172 * transition. If that happens L3 module does not get
173 * disabled, thereby leading to PER power domain
177 clkdm_wakeup(gfx_l4ls_clkdm);
178 clkdm_sleep(gfx_l4ls_clkdm);
183 static int am43xx_suspend(unsigned int state, int (*fn)(unsigned long),
188 /* Suspend secure side on HS devices */
189 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
191 omap_smccc_smc(AM43xx_PPA_SVC_PM_SUSPEND, 0);
193 omap_secure_dispatcher(AM43xx_PPA_SVC_PM_SUSPEND,
198 amx3_pre_suspend_common();
199 scu_power_mode(scu_base, SCU_PM_POWEROFF);
200 ret = cpu_suspend(args, fn);
201 scu_power_mode(scu_base, SCU_PM_NORMAL);
203 if (!am43xx_check_off_mode_enable())
204 amx3_post_suspend_common();
207 * Resume secure side on HS devices.
209 * Note that even on systems with OP-TEE available this resume call is
210 * issued to the ROM. This is because upon waking from suspend the ROM
211 * is restored as the secure monitor. On systems with OP-TEE ROM will
212 * restore OP-TEE during this call.
214 if (omap_type() != OMAP2_DEVICE_TYPE_GP)
215 omap_secure_dispatcher(AM43xx_PPA_SVC_PM_RESUME,
222 static int am33xx_cpu_suspend(int (*fn)(unsigned long), unsigned long args)
226 if (omap_irq_pending() || need_resched())
229 ret = cpu_suspend(args, fn);
234 static int am43xx_cpu_suspend(int (*fn)(unsigned long), unsigned long args)
241 scu_power_mode(scu_base, SCU_PM_DORMANT);
242 ret = cpu_suspend(args, fn);
243 scu_power_mode(scu_base, SCU_PM_NORMAL);
248 static void amx3_begin_suspend(void)
250 cpu_idle_poll_ctrl(true);
253 static void amx3_finish_suspend(void)
255 cpu_idle_poll_ctrl(false);
259 static struct am33xx_pm_sram_addr *amx3_get_sram_addrs(void)
262 return &am33xx_pm_sram;
263 else if (soc_is_am437x())
264 return &am43xx_pm_sram;
269 static void am43xx_save_context(void)
273 static void am33xx_save_context(void)
275 omap_intc_save_context();
278 static void am33xx_restore_context(void)
280 omap_intc_restore_context();
283 static void am43xx_restore_context(void)
286 * HACK: restore dpll_per_clkdcoldo register contents, to avoid
287 * breaking suspend-resume
289 writel_relaxed(0x0, AM33XX_L4_WK_IO_ADDRESS(0x44df2e14));
292 static struct am33xx_pm_platform_data am33xx_ops = {
293 .init = am33xx_suspend_init,
294 .deinit = amx3_suspend_deinit,
295 .soc_suspend = am33xx_suspend,
296 .cpu_suspend = am33xx_cpu_suspend,
297 .begin_suspend = amx3_begin_suspend,
298 .finish_suspend = amx3_finish_suspend,
299 .get_sram_addrs = amx3_get_sram_addrs,
300 .save_context = am33xx_save_context,
301 .restore_context = am33xx_restore_context,
302 .check_off_mode_enable = am33xx_check_off_mode_enable,
305 static struct am33xx_pm_platform_data am43xx_ops = {
306 .init = am43xx_suspend_init,
307 .deinit = amx3_suspend_deinit,
308 .soc_suspend = am43xx_suspend,
309 .cpu_suspend = am43xx_cpu_suspend,
310 .begin_suspend = amx3_begin_suspend,
311 .finish_suspend = amx3_finish_suspend,
312 .get_sram_addrs = amx3_get_sram_addrs,
313 .save_context = am43xx_save_context,
314 .restore_context = am43xx_restore_context,
315 .check_off_mode_enable = am43xx_check_off_mode_enable,
318 static struct am33xx_pm_platform_data *am33xx_pm_get_pdata(void)
322 else if (soc_is_am437x())
328 #ifdef CONFIG_SUSPEND
330 * Block system suspend initially. Later on pm33xx sets up it's own
331 * platform_suspend_ops after probe. That depends also on loaded
332 * wkup_m3_ipc /*(DEBLOBBED)*/
333 static int amx3_suspend_block(suspend_state_t state)
335 pr_warn("PM not initialized for pm33xx, wkup_m3_ipc, or /*(DEBLOBBED)*/\n");
340 static int amx3_pm_valid(suspend_state_t state)
343 case PM_SUSPEND_STANDBY:
350 static const struct platform_suspend_ops amx3_blocked_pm_ops = {
351 .begin = amx3_suspend_block,
352 .valid = amx3_pm_valid,
355 static void __init amx3_block_suspend(void)
357 suspend_set_ops(&amx3_blocked_pm_ops);
360 static inline void amx3_block_suspend(void)
363 #endif /* CONFIG_SUSPEND */
365 int __init amx3_common_pm_init(void)
367 struct am33xx_pm_platform_data *pdata;
368 struct platform_device_info devinfo;
370 pdata = am33xx_pm_get_pdata();
372 memset(&devinfo, 0, sizeof(devinfo));
373 devinfo.name = "pm33xx";
374 devinfo.data = pdata;
375 devinfo.size_data = sizeof(*pdata);
377 platform_device_register_full(&devinfo);
378 amx3_block_suspend();
383 static int __init amx3_idle_init(struct device_node *cpu_node, int cpu)
385 struct device_node *state_node;
386 struct amx3_idle_state states[CPUIDLE_STATE_MAX];
391 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
395 if (!of_device_is_available(state_node))
398 if (i == CPUIDLE_STATE_MAX) {
399 pr_warn("%s: cpuidle states reached max possible\n",
404 states[state_count].wfi_flags = 0;
406 if (of_property_read_bool(state_node, "ti,idle-wkup-m3"))
407 states[state_count].wfi_flags |= WFI_FLAG_WAKE_M3 |
408 WFI_FLAG_FLUSH_CACHE;
413 idle_states = kcalloc(state_count, sizeof(*idle_states), GFP_KERNEL);
417 for (i = 1; i < state_count; i++)
418 idle_states[i].wfi_flags = states[i].wfi_flags;
423 static int amx3_idle_enter(unsigned long index)
425 struct amx3_idle_state *idle_state = &idle_states[index];
431 idle_fn(idle_state->wfi_flags);
436 static struct cpuidle_ops amx3_cpuidle_ops __initdata = {
437 .init = amx3_idle_init,
438 .suspend = amx3_idle_enter,
441 CPUIDLE_METHOD_OF_DECLARE(pm33xx_idle, "ti,am3352", &amx3_cpuidle_ops);
442 CPUIDLE_METHOD_OF_DECLARE(pm43xx_idle, "ti,am4372", &amx3_cpuidle_ops);