blob: 9c1506b499bca6f4599a20ece67494078a95d4d0 [file] [log] [blame]
Jongpill Leec9347102012-02-17 09:49:54 +09001/*
Bartlomiej Zolnierkiewicz0d713cf2014-09-25 18:02:45 +09002 * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd.
Jaecheol Lee16638952011-03-10 13:33:59 +09003 * http://www.samsung.com
4 *
Jongpill Leec9347102012-02-17 09:49:54 +09005 * EXYNOS - Power Management support
Jaecheol Lee16638952011-03-10 13:33:59 +09006 *
7 * Based on arch/arm/mach-s3c2410/pm.c
8 * Copyright (c) 2006 Simtec Electronics
9 * Ben Dooks <ben@simtec.co.uk>
10 *
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.
14*/
15
16#include <linux/init.h>
17#include <linux/suspend.h>
Daniel Lezcano85f9f902014-05-09 06:43:27 +090018#include <linux/cpu_pm.h>
Jaecheol Lee16638952011-03-10 13:33:59 +090019#include <linux/io.h>
Jaecheol Lee56c03d92011-07-18 19:25:13 +090020#include <linux/err.h>
Jaecheol Lee16638952011-03-10 13:33:59 +090021
Tomasz Figa2b9d9c32014-09-24 01:24:39 +090022#include <asm/firmware.h>
Shawn Guo63b870f2011-11-17 01:19:11 +090023#include <asm/smp_scu.h>
Tomasz Figad710aa32014-03-18 07:28:27 +090024#include <asm/suspend.h>
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +010025#include <asm/cacheflush.h>
Jaecheol Lee16638952011-03-10 13:33:59 +090026
Pankaj Dubey32b0aa92015-01-09 01:14:23 +090027#include <mach/map.h>
28
Tomasz Figad710aa32014-03-18 07:28:27 +090029#include <plat/pm-common.h>
Kukjin Kimccd458c2012-12-31 10:06:48 -080030
31#include "common.h"
Pankaj Dubey6b7bfd82014-11-07 09:26:47 +090032#include "exynos-pmu.h"
Kukjin Kim65c9a852013-12-19 04:06:56 +090033#include "regs-pmu.h"
Jaecheol Lee16638952011-03-10 13:33:59 +090034
Bartlomiej Zolnierkiewicz134abc22014-09-25 17:59:40 +090035static inline void __iomem *exynos_boot_vector_addr(void)
36{
37 if (samsung_rev() == EXYNOS4210_REV_1_1)
38 return pmu_base_addr + S5P_INFORM7;
39 else if (samsung_rev() == EXYNOS4210_REV_1_0)
40 return sysram_base_addr + 0x24;
41 return pmu_base_addr + S5P_INFORM0;
42}
43
44static inline void __iomem *exynos_boot_vector_flag(void)
45{
46 if (samsung_rev() == EXYNOS4210_REV_1_1)
47 return pmu_base_addr + S5P_INFORM6;
48 else if (samsung_rev() == EXYNOS4210_REV_1_0)
49 return sysram_base_addr + 0x20;
50 return pmu_base_addr + S5P_INFORM1;
51}
Daniel Lezcano3681baf2014-05-09 06:53:00 +090052
Daniel Lezcanoe30b1542014-05-09 06:56:24 +090053#define S5P_CHECK_AFTR 0xFCBA0D10
Daniel Lezcano3681baf2014-05-09 06:53:00 +090054
Jaecheol Leef4ba4b02011-07-18 19:25:03 +090055/* For Cortex-A9 Diagnostic and Power control register */
56static unsigned int save_arm_register[2];
57
Bartlomiej Zolnierkiewicz0d713cf2014-09-25 18:02:45 +090058void exynos_cpu_save_register(void)
Daniel Lezcano309e08c2014-05-09 06:43:27 +090059{
60 unsigned long tmp;
61
62 /* Save Power control register */
63 asm ("mrc p15, 0, %0, c15, c0, 0"
64 : "=r" (tmp) : : "cc");
65
66 save_arm_register[0] = tmp;
67
68 /* Save Diagnostic register */
69 asm ("mrc p15, 0, %0, c15, c0, 1"
70 : "=r" (tmp) : : "cc");
71
72 save_arm_register[1] = tmp;
73}
74
Bartlomiej Zolnierkiewicz0d713cf2014-09-25 18:02:45 +090075void exynos_cpu_restore_register(void)
Daniel Lezcano309e08c2014-05-09 06:43:27 +090076{
77 unsigned long tmp;
78
79 /* Restore Power control register */
80 tmp = save_arm_register[0];
81
82 asm volatile ("mcr p15, 0, %0, c15, c0, 0"
83 : : "r" (tmp)
84 : "cc");
85
86 /* Restore Diagnostic register */
87 tmp = save_arm_register[1];
88
89 asm volatile ("mcr p15, 0, %0, c15, c0, 1"
90 : : "r" (tmp)
91 : "cc");
92}
93
Bartlomiej Zolnierkiewicz0d713cf2014-09-25 18:02:45 +090094void exynos_pm_central_suspend(void)
Tomasz Figa01601b32014-08-05 14:43:10 +020095{
96 unsigned long tmp;
97
98 /* Setting Central Sequence Register for power down mode */
99 tmp = pmu_raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
100 tmp &= ~S5P_CENTRAL_LOWPWR_CFG;
101 pmu_raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
102}
103
Bartlomiej Zolnierkiewicz0d713cf2014-09-25 18:02:45 +0900104int exynos_pm_central_resume(void)
Tomasz Figa01601b32014-08-05 14:43:10 +0200105{
106 unsigned long tmp;
107
108 /*
109 * If PMU failed while entering sleep mode, WFI will be
110 * ignored by PMU and then exiting cpu_do_idle().
111 * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically
112 * in this situation.
113 */
114 tmp = pmu_raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
115 if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) {
116 tmp |= S5P_CENTRAL_LOWPWR_CFG;
117 pmu_raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
118 /* clear the wakeup state register */
119 pmu_raw_writel(0x0, S5P_WAKEUP_STAT);
120 /* No need to perform below restore code */
121 return -1;
122 }
123
124 return 0;
125}
126
127/* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */
128static void exynos_set_wakeupmask(long mask)
129{
130 pmu_raw_writel(mask, S5P_WAKEUP_MASK);
Bartlomiej Zolnierkiewicz89366402015-03-27 02:35:48 +0900131 if (soc_is_exynos3250())
132 pmu_raw_writel(0x0, S5P_WAKEUP_MASK2);
Tomasz Figa01601b32014-08-05 14:43:10 +0200133}
134
135static void exynos_cpu_set_boot_vector(long flags)
136{
Bartlomiej Zolnierkiewicz134abc22014-09-25 17:59:40 +0900137 __raw_writel(virt_to_phys(exynos_cpu_resume),
138 exynos_boot_vector_addr());
139 __raw_writel(flags, exynos_boot_vector_flag());
Tomasz Figa01601b32014-08-05 14:43:10 +0200140}
141
142static int exynos_aftr_finisher(unsigned long flags)
143{
Bartlomiej Zolnierkiewicza135e202014-09-25 17:59:41 +0900144 int ret;
145
Bartlomiej Zolnierkiewicz89366402015-03-27 02:35:48 +0900146 exynos_set_wakeupmask(soc_is_exynos3250() ? 0x40003ffe : 0x0000ff3e);
Tomasz Figa01601b32014-08-05 14:43:10 +0200147 /* Set value of power down register for aftr mode */
148 exynos_sys_powerdown_conf(SYS_AFTR);
Bartlomiej Zolnierkiewicza135e202014-09-25 17:59:41 +0900149
150 ret = call_firmware_op(do_idle, FW_DO_IDLE_AFTR);
151 if (ret == -ENOSYS) {
152 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
153 exynos_cpu_save_register();
154 exynos_cpu_set_boot_vector(S5P_CHECK_AFTR);
155 cpu_do_idle();
156 }
Tomasz Figa01601b32014-08-05 14:43:10 +0200157
158 return 1;
159}
160
161void exynos_enter_aftr(void)
162{
Bartlomiej Zolnierkiewicz89366402015-03-27 02:35:48 +0900163 unsigned int cpuid = smp_processor_id();
164
Tomasz Figa01601b32014-08-05 14:43:10 +0200165 cpu_pm_enter();
166
Bartlomiej Zolnierkiewicz89366402015-03-27 02:35:48 +0900167 if (soc_is_exynos3250())
168 exynos_set_boot_flag(cpuid, C2_STATE);
169
Tomasz Figa01601b32014-08-05 14:43:10 +0200170 exynos_pm_central_suspend();
Tomasz Figa01601b32014-08-05 14:43:10 +0200171
Bartlomiej Zolnierkiewicz865e8b72015-01-24 14:05:50 +0900172 if (of_machine_is_compatible("samsung,exynos4212") ||
173 of_machine_is_compatible("samsung,exynos4412")) {
174 /* Setting SEQ_OPTION register */
175 pmu_raw_writel(S5P_USE_STANDBY_WFI0 | S5P_USE_STANDBY_WFE0,
176 S5P_CENTRAL_SEQ_OPTION);
177 }
178
Tomasz Figa01601b32014-08-05 14:43:10 +0200179 cpu_suspend(0, exynos_aftr_finisher);
180
181 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
182 scu_enable(S5P_VA_SCU);
Bartlomiej Zolnierkiewicza135e202014-09-25 17:59:41 +0900183 if (call_firmware_op(resume) == -ENOSYS)
184 exynos_cpu_restore_register();
Tomasz Figa01601b32014-08-05 14:43:10 +0200185 }
186
187 exynos_pm_central_resume();
188
Bartlomiej Zolnierkiewicz89366402015-03-27 02:35:48 +0900189 if (soc_is_exynos3250())
190 exynos_clear_boot_flag(cpuid, C2_STATE);
191
Tomasz Figa01601b32014-08-05 14:43:10 +0200192 cpu_pm_exit();
193}
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900194
Bartlomiej Zolnierkiewiczcfdda352015-03-18 03:26:11 +0900195#if defined(CONFIG_SMP) && defined(CONFIG_ARM_EXYNOS_CPUIDLE)
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900196static atomic_t cpu1_wakeup = ATOMIC_INIT(0);
197
198static int exynos_cpu0_enter_aftr(void)
199{
200 int ret = -1;
201
202 /*
203 * If the other cpu is powered on, we have to power it off, because
204 * the AFTR state won't work otherwise
205 */
206 if (cpu_online(1)) {
207 /*
208 * We reach a sync point with the coupled idle state, we know
209 * the other cpu will power down itself or will abort the
210 * sequence, let's wait for one of these to happen
211 */
212 while (exynos_cpu_power_state(1)) {
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100213 unsigned long boot_addr;
214
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900215 /*
216 * The other cpu may skip idle and boot back
217 * up again
218 */
219 if (atomic_read(&cpu1_wakeup))
220 goto abort;
221
222 /*
223 * The other cpu may bounce through idle and
224 * boot back up again, getting stuck in the
225 * boot rom code
226 */
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100227 ret = exynos_get_boot_addr(1, &boot_addr);
228 if (ret)
229 goto fail;
230 ret = -1;
231 if (boot_addr == 0)
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900232 goto abort;
233
234 cpu_relax();
235 }
236 }
237
238 exynos_enter_aftr();
239 ret = 0;
240
241abort:
242 if (cpu_online(1)) {
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100243 unsigned long boot_addr = virt_to_phys(exynos_cpu_resume);
244
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900245 /*
246 * Set the boot vector to something non-zero
247 */
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100248 ret = exynos_set_boot_addr(1, boot_addr);
249 if (ret)
250 goto fail;
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900251 dsb();
252
253 /*
254 * Turn on cpu1 and wait for it to be on
255 */
256 exynos_cpu_power_up(1);
257 while (exynos_cpu_power_state(1) != S5P_CORE_LOCAL_PWR_EN)
258 cpu_relax();
259
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100260 if (soc_is_exynos3250()) {
261 while (!pmu_raw_readl(S5P_PMU_SPARE2) &&
262 !atomic_read(&cpu1_wakeup))
263 cpu_relax();
264
265 if (!atomic_read(&cpu1_wakeup))
266 exynos_core_restart(1);
267 }
268
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900269 while (!atomic_read(&cpu1_wakeup)) {
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100270 smp_rmb();
271
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900272 /*
273 * Poke cpu1 out of the boot rom
274 */
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900275
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100276 ret = exynos_set_boot_addr(1, boot_addr);
277 if (ret)
278 goto fail;
279
280 call_firmware_op(cpu_boot, 1);
281
282 if (soc_is_exynos3250())
283 dsb_sev();
284 else
285 arch_send_wakeup_ipi_mask(cpumask_of(1));
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900286 }
287 }
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100288fail:
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900289 return ret;
290}
291
292static int exynos_wfi_finisher(unsigned long flags)
293{
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100294 if (soc_is_exynos3250())
295 flush_cache_all();
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900296 cpu_do_idle();
297
298 return -1;
299}
300
301static int exynos_cpu1_powerdown(void)
302{
303 int ret = -1;
304
305 /*
306 * Idle sequence for cpu1
307 */
308 if (cpu_pm_enter())
309 goto cpu1_aborted;
310
311 /*
312 * Turn off cpu 1
313 */
314 exynos_cpu_power_down(1);
315
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100316 if (soc_is_exynos3250())
317 pmu_raw_writel(0, S5P_PMU_SPARE2);
318
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900319 ret = cpu_suspend(0, exynos_wfi_finisher);
320
321 cpu_pm_exit();
322
323cpu1_aborted:
324 dsb();
325 /*
326 * Notify cpu 0 that cpu 1 is awake
327 */
328 atomic_set(&cpu1_wakeup, 1);
329
330 return ret;
331}
332
333static void exynos_pre_enter_aftr(void)
334{
Bartlomiej Zolnierkiewiczaf997112015-03-18 14:09:57 +0100335 unsigned long boot_addr = virt_to_phys(exynos_cpu_resume);
336
337 (void)exynos_set_boot_addr(1, boot_addr);
Bartlomiej Zolnierkiewicz712eddf2015-01-24 14:05:50 +0900338}
339
340static void exynos_post_enter_aftr(void)
341{
342 atomic_set(&cpu1_wakeup, 0);
343}
344
345struct cpuidle_exynos_data cpuidle_coupled_exynos_data = {
346 .cpu0_enter_aftr = exynos_cpu0_enter_aftr,
347 .cpu1_powerdown = exynos_cpu1_powerdown,
348 .pre_enter_aftr = exynos_pre_enter_aftr,
349 .post_enter_aftr = exynos_post_enter_aftr,
350};
Bartlomiej Zolnierkiewiczcfdda352015-03-18 03:26:11 +0900351#endif /* CONFIG_SMP && CONFIG_ARM_EXYNOS_CPUIDLE */