2 * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5 * EXYNOS - Suspend support
7 * Based on arch/arm/mach-s3c2410/pm.c
8 * Copyright (c) 2006 Simtec Electronics
9 * Ben Dooks <ben@simtec.co.uk>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/init.h>
17 #include <linux/suspend.h>
18 #include <linux/syscore_ops.h>
19 #include <linux/cpu_pm.h>
21 #include <linux/irqchip/arm-gic.h>
22 #include <linux/err.h>
23 #include <linux/regulator/machine.h>
25 #include <asm/cacheflush.h>
26 #include <asm/hardware/cache-l2x0.h>
27 #include <asm/firmware.h>
29 #include <asm/smp_scu.h>
30 #include <asm/suspend.h>
32 #include <plat/pm-common.h>
33 #include <plat/regs-srom.h>
38 #include "exynos-pmu.h"
40 #define S5P_CHECK_SLEEP 0x00000BAD
42 #define REG_TABLE_END (-1U)
44 #define EXYNOS5420_CPU_STATE 0x28
47 * struct exynos_wkup_irq - Exynos GIC to PMU IRQ mapping
48 * @hwirq: Hardware IRQ signal of the GIC
49 * @mask: Mask in PMU wake-up mask register
51 struct exynos_wkup_irq {
56 static struct sleep_save exynos5_sys_save[] = {
57 SAVE_ITEM(EXYNOS5_SYS_I2C_CFG),
60 static struct sleep_save exynos_core_save[] = {
62 SAVE_ITEM(S5P_SROM_BW),
63 SAVE_ITEM(S5P_SROM_BC0),
64 SAVE_ITEM(S5P_SROM_BC1),
65 SAVE_ITEM(S5P_SROM_BC2),
66 SAVE_ITEM(S5P_SROM_BC3),
69 struct exynos_pm_data {
70 const struct exynos_wkup_irq *wkup_irq;
71 struct sleep_save *extra_save;
73 unsigned int wake_disable_mask;
74 unsigned int *release_ret_regs;
76 void (*pm_prepare)(void);
77 void (*pm_resume_prepare)(void);
78 void (*pm_resume)(void);
79 int (*pm_suspend)(void);
80 int (*cpu_suspend)(unsigned long);
83 struct exynos_pm_data *pm_data;
85 static int exynos5420_cpu_state;
86 static unsigned int exynos_pmu_spare3;
92 static u32 exynos_irqwake_intmask = 0xffffffff;
94 static const struct exynos_wkup_irq exynos4_wkup_irq[] = {
95 { 76, BIT(1) }, /* RTC alarm */
96 { 77, BIT(2) }, /* RTC tick */
100 static const struct exynos_wkup_irq exynos5250_wkup_irq[] = {
101 { 75, BIT(1) }, /* RTC alarm */
102 { 76, BIT(2) }, /* RTC tick */
106 unsigned int exynos_release_ret_regs[] = {
107 S5P_PAD_RET_MAUDIO_OPTION,
108 S5P_PAD_RET_GPIO_OPTION,
109 S5P_PAD_RET_UART_OPTION,
110 S5P_PAD_RET_MMCA_OPTION,
111 S5P_PAD_RET_MMCB_OPTION,
112 S5P_PAD_RET_EBIA_OPTION,
113 S5P_PAD_RET_EBIB_OPTION,
117 unsigned int exynos5420_release_ret_regs[] = {
118 EXYNOS_PAD_RET_DRAM_OPTION,
119 EXYNOS_PAD_RET_MAUDIO_OPTION,
120 EXYNOS_PAD_RET_JTAG_OPTION,
121 EXYNOS5420_PAD_RET_GPIO_OPTION,
122 EXYNOS5420_PAD_RET_UART_OPTION,
123 EXYNOS5420_PAD_RET_MMCA_OPTION,
124 EXYNOS5420_PAD_RET_MMCB_OPTION,
125 EXYNOS5420_PAD_RET_MMCC_OPTION,
126 EXYNOS5420_PAD_RET_HSI_OPTION,
127 EXYNOS_PAD_RET_EBIA_OPTION,
128 EXYNOS_PAD_RET_EBIB_OPTION,
129 EXYNOS5420_PAD_RET_SPI_OPTION,
130 EXYNOS5420_PAD_RET_DRAM_COREBLK_OPTION,
134 static int exynos_irq_set_wake(struct irq_data *data, unsigned int state)
136 const struct exynos_wkup_irq *wkup_irq;
138 if (!pm_data->wkup_irq)
140 wkup_irq = pm_data->wkup_irq;
142 while (wkup_irq->mask) {
143 if (wkup_irq->hwirq == data->hwirq) {
145 exynos_irqwake_intmask |= wkup_irq->mask;
147 exynos_irqwake_intmask &= ~wkup_irq->mask;
156 static int exynos_cpu_do_idle(void)
158 /* issue the standby signal into the pm unit. */
161 pr_info("Failed to suspend the system\n");
162 return 1; /* Aborting suspend */
164 static void exynos_flush_cache_all(void)
170 static int exynos_cpu_suspend(unsigned long arg)
172 exynos_flush_cache_all();
173 return exynos_cpu_do_idle();
176 static int exynos5420_cpu_suspend(unsigned long arg)
178 /* MCPM works with HW CPU identifiers */
179 unsigned int mpidr = read_cpuid_mpidr();
180 unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
181 unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
183 __raw_writel(0x0, sysram_base_addr + EXYNOS5420_CPU_STATE);
185 if (IS_ENABLED(CONFIG_EXYNOS5420_MCPM)) {
186 mcpm_set_entry_vector(cpu, cluster, exynos_cpu_resume);
189 * Residency value passed to mcpm_cpu_suspend back-end
190 * has to be given clear semantics. Set to 0 as a
196 pr_info("Failed to suspend the system\n");
198 /* return value != 0 means failure */
202 static void exynos_pm_set_wakeup_mask(void)
204 /* Set wake-up mask registers */
205 pmu_raw_writel(exynos_get_eint_wake_mask(), S5P_EINT_WAKEUP_MASK);
206 pmu_raw_writel(exynos_irqwake_intmask & ~(1 << 31), S5P_WAKEUP_MASK);
209 static void exynos_pm_enter_sleep_mode(void)
211 /* Set value of power down register for sleep mode */
212 exynos_sys_powerdown_conf(SYS_SLEEP);
213 pmu_raw_writel(S5P_CHECK_SLEEP, S5P_INFORM1);
216 static void exynos_pm_prepare(void)
218 /* Set wake-up mask registers */
219 exynos_pm_set_wakeup_mask();
221 s3c_pm_do_save(exynos_core_save, ARRAY_SIZE(exynos_core_save));
223 if (pm_data->extra_save)
224 s3c_pm_do_save(pm_data->extra_save,
225 pm_data->num_extra_save);
227 exynos_pm_enter_sleep_mode();
229 /* ensure at least INFORM0 has the resume address */
230 pmu_raw_writel(virt_to_phys(exynos_cpu_resume), S5P_INFORM0);
233 static void exynos5420_pm_prepare(void)
237 /* Set wake-up mask registers */
238 exynos_pm_set_wakeup_mask();
240 s3c_pm_do_save(exynos_core_save, ARRAY_SIZE(exynos_core_save));
242 exynos_pmu_spare3 = pmu_raw_readl(S5P_PMU_SPARE3);
244 * The cpu state needs to be saved and restored so that the
245 * secondary CPUs will enter low power start. Though the U-Boot
246 * is setting the cpu state with low power flag, the kernel
247 * needs to restore it back in case, the primary cpu fails to
248 * suspend for any reason.
250 exynos5420_cpu_state = __raw_readl(sysram_base_addr +
251 EXYNOS5420_CPU_STATE);
253 exynos_pm_enter_sleep_mode();
255 /* ensure at least INFORM0 has the resume address */
256 if (IS_ENABLED(CONFIG_EXYNOS5420_MCPM))
257 pmu_raw_writel(virt_to_phys(mcpm_entry_point), S5P_INFORM0);
259 tmp = pmu_raw_readl(EXYNOS5_ARM_L2_OPTION);
260 tmp &= ~EXYNOS5_USE_RETENTION;
261 pmu_raw_writel(tmp, EXYNOS5_ARM_L2_OPTION);
263 tmp = pmu_raw_readl(EXYNOS5420_SFR_AXI_CGDIS1);
264 tmp |= EXYNOS5420_UFS;
265 pmu_raw_writel(tmp, EXYNOS5420_SFR_AXI_CGDIS1);
267 tmp = pmu_raw_readl(EXYNOS5420_ARM_COMMON_OPTION);
268 tmp &= ~EXYNOS5420_L2RSTDISABLE_VALUE;
269 pmu_raw_writel(tmp, EXYNOS5420_ARM_COMMON_OPTION);
271 tmp = pmu_raw_readl(EXYNOS5420_FSYS2_OPTION);
272 tmp |= EXYNOS5420_EMULATION;
273 pmu_raw_writel(tmp, EXYNOS5420_FSYS2_OPTION);
275 tmp = pmu_raw_readl(EXYNOS5420_PSGEN_OPTION);
276 tmp |= EXYNOS5420_EMULATION;
277 pmu_raw_writel(tmp, EXYNOS5420_PSGEN_OPTION);
281 static int exynos_pm_suspend(void)
283 exynos_pm_central_suspend();
285 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
286 exynos_cpu_save_register();
291 static int exynos5420_pm_suspend(void)
295 exynos_pm_central_suspend();
297 /* Setting SEQ_OPTION register */
299 this_cluster = MPIDR_AFFINITY_LEVEL(read_cpuid_mpidr(), 1);
301 pmu_raw_writel(EXYNOS5420_ARM_USE_STANDBY_WFI0,
302 S5P_CENTRAL_SEQ_OPTION);
304 pmu_raw_writel(EXYNOS5420_KFC_USE_STANDBY_WFI0,
305 S5P_CENTRAL_SEQ_OPTION);
309 static void exynos_pm_release_retention(void)
313 for (i = 0; (pm_data->release_ret_regs[i] != REG_TABLE_END); i++)
314 pmu_raw_writel(EXYNOS_WAKEUP_FROM_LOWPWR,
315 pm_data->release_ret_regs[i]);
318 static void exynos_pm_resume(void)
320 u32 cpuid = read_cpuid_part();
322 if (exynos_pm_central_resume())
325 /* For release retention */
326 exynos_pm_release_retention();
328 if (pm_data->extra_save)
329 s3c_pm_do_restore_core(pm_data->extra_save,
330 pm_data->num_extra_save);
332 s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));
334 if (cpuid == ARM_CPU_PART_CORTEX_A9)
335 scu_enable(S5P_VA_SCU);
337 if (call_firmware_op(resume) == -ENOSYS
338 && cpuid == ARM_CPU_PART_CORTEX_A9)
339 exynos_cpu_restore_register();
343 /* Clear SLEEP mode set in INFORM1 */
344 pmu_raw_writel(0x0, S5P_INFORM1);
347 static void exynos5420_prepare_pm_resume(void)
349 if (IS_ENABLED(CONFIG_EXYNOS5420_MCPM))
350 WARN_ON(mcpm_cpu_powered_up());
353 static void exynos5420_pm_resume(void)
357 /* Restore the CPU0 low power state register */
358 tmp = pmu_raw_readl(EXYNOS5_ARM_CORE0_SYS_PWR_REG);
359 pmu_raw_writel(tmp | S5P_CORE_LOCAL_PWR_EN,
360 EXYNOS5_ARM_CORE0_SYS_PWR_REG);
362 /* Restore the sysram cpu state register */
363 __raw_writel(exynos5420_cpu_state,
364 sysram_base_addr + EXYNOS5420_CPU_STATE);
366 pmu_raw_writel(EXYNOS5420_USE_STANDBY_WFI_ALL,
367 S5P_CENTRAL_SEQ_OPTION);
369 if (exynos_pm_central_resume())
372 /* For release retention */
373 exynos_pm_release_retention();
375 pmu_raw_writel(exynos_pmu_spare3, S5P_PMU_SPARE3);
377 s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));
381 tmp = pmu_raw_readl(EXYNOS5420_SFR_AXI_CGDIS1);
382 tmp &= ~EXYNOS5420_UFS;
383 pmu_raw_writel(tmp, EXYNOS5420_SFR_AXI_CGDIS1);
385 tmp = pmu_raw_readl(EXYNOS5420_FSYS2_OPTION);
386 tmp &= ~EXYNOS5420_EMULATION;
387 pmu_raw_writel(tmp, EXYNOS5420_FSYS2_OPTION);
389 tmp = pmu_raw_readl(EXYNOS5420_PSGEN_OPTION);
390 tmp &= ~EXYNOS5420_EMULATION;
391 pmu_raw_writel(tmp, EXYNOS5420_PSGEN_OPTION);
393 /* Clear SLEEP mode set in INFORM1 */
394 pmu_raw_writel(0x0, S5P_INFORM1);
401 static int exynos_suspend_enter(suspend_state_t state)
407 S3C_PMDBG("%s: suspending the system...\n", __func__);
409 S3C_PMDBG("%s: wakeup masks: %08x,%08x\n", __func__,
410 exynos_irqwake_intmask, exynos_get_eint_wake_mask());
412 if (exynos_irqwake_intmask == -1U
413 && exynos_get_eint_wake_mask() == -1U) {
414 pr_err("%s: No wake-up sources!\n", __func__);
415 pr_err("%s: Aborting sleep\n", __func__);
420 if (pm_data->pm_prepare)
421 pm_data->pm_prepare();
423 s3c_pm_check_store();
425 ret = call_firmware_op(suspend);
427 ret = cpu_suspend(0, pm_data->cpu_suspend);
431 if (pm_data->pm_resume_prepare)
432 pm_data->pm_resume_prepare();
433 s3c_pm_restore_uarts();
435 S3C_PMDBG("%s: wakeup stat: %08x\n", __func__,
436 pmu_raw_readl(S5P_WAKEUP_STAT));
438 s3c_pm_check_restore();
440 S3C_PMDBG("%s: resuming the system...\n", __func__);
445 static int exynos_suspend_prepare(void)
450 * REVISIT: It would be better if struct platform_suspend_ops
451 * .prepare handler get the suspend_state_t as a parameter to
452 * avoid hard-coding the suspend to mem state. It's safe to do
453 * it now only because the suspend_valid_only_mem function is
454 * used as the .valid callback used to check if a given state
455 * is supported by the platform anyways.
457 ret = regulator_suspend_prepare(PM_SUSPEND_MEM);
459 pr_err("Failed to prepare regulators for suspend (%d)\n", ret);
463 s3c_pm_check_prepare();
468 static void exynos_suspend_finish(void)
472 s3c_pm_check_cleanup();
474 ret = regulator_suspend_finish();
476 pr_warn("Failed to resume regulators from suspend (%d)\n", ret);
479 static const struct platform_suspend_ops exynos_suspend_ops = {
480 .enter = exynos_suspend_enter,
481 .prepare = exynos_suspend_prepare,
482 .finish = exynos_suspend_finish,
483 .valid = suspend_valid_only_mem,
486 static const struct exynos_pm_data exynos4_pm_data = {
487 .wkup_irq = exynos4_wkup_irq,
488 .wake_disable_mask = ((0xFF << 8) | (0x1F << 1)),
489 .release_ret_regs = exynos_release_ret_regs,
490 .pm_suspend = exynos_pm_suspend,
491 .pm_resume = exynos_pm_resume,
492 .pm_prepare = exynos_pm_prepare,
493 .cpu_suspend = exynos_cpu_suspend,
496 static const struct exynos_pm_data exynos5250_pm_data = {
497 .wkup_irq = exynos5250_wkup_irq,
498 .wake_disable_mask = ((0xFF << 8) | (0x1F << 1)),
499 .release_ret_regs = exynos_release_ret_regs,
500 .extra_save = exynos5_sys_save,
501 .num_extra_save = ARRAY_SIZE(exynos5_sys_save),
502 .pm_suspend = exynos_pm_suspend,
503 .pm_resume = exynos_pm_resume,
504 .pm_prepare = exynos_pm_prepare,
505 .cpu_suspend = exynos_cpu_suspend,
508 static struct exynos_pm_data exynos5420_pm_data = {
509 .wkup_irq = exynos5250_wkup_irq,
510 .wake_disable_mask = (0x7F << 7) | (0x1F << 1),
511 .release_ret_regs = exynos5420_release_ret_regs,
512 .pm_resume_prepare = exynos5420_prepare_pm_resume,
513 .pm_resume = exynos5420_pm_resume,
514 .pm_suspend = exynos5420_pm_suspend,
515 .pm_prepare = exynos5420_pm_prepare,
516 .cpu_suspend = exynos5420_cpu_suspend,
519 static struct of_device_id exynos_pmu_of_device_ids[] = {
521 .compatible = "samsung,exynos4210-pmu",
522 .data = &exynos4_pm_data,
524 .compatible = "samsung,exynos4212-pmu",
525 .data = &exynos4_pm_data,
527 .compatible = "samsung,exynos4412-pmu",
528 .data = &exynos4_pm_data,
530 .compatible = "samsung,exynos5250-pmu",
531 .data = &exynos5250_pm_data,
533 .compatible = "samsung,exynos5420-pmu",
534 .data = &exynos5420_pm_data,
539 static struct syscore_ops exynos_pm_syscore_ops;
541 void __init exynos_pm_init(void)
543 const struct of_device_id *match;
546 of_find_matching_node_and_match(NULL, exynos_pmu_of_device_ids, &match);
548 pr_err("Failed to find PMU node\n");
551 pm_data = (struct exynos_pm_data *) match->data;
553 /* Platform-specific GIC callback */
554 gic_arch_extn.irq_set_wake = exynos_irq_set_wake;
556 /* All wakeup disable */
557 tmp = pmu_raw_readl(S5P_WAKEUP_MASK);
558 tmp |= pm_data->wake_disable_mask;
559 pmu_raw_writel(tmp, S5P_WAKEUP_MASK);
561 exynos_pm_syscore_ops.suspend = pm_data->pm_suspend;
562 exynos_pm_syscore_ops.resume = pm_data->pm_resume;
564 register_syscore_ops(&exynos_pm_syscore_ops);
565 suspend_set_ops(&exynos_suspend_ops);