aba2ff6e443d0c25364b61504488f495f1fbc69f
[cascardo/linux.git] / arch / arm / mach-exynos / pm.c
1 /*
2  * Copyright (c) 2011-2012 Samsung Electronics Co., Ltd.
3  *              http://www.samsung.com
4  *
5  * EXYNOS - Power Management support
6  *
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>
18 #include <linux/syscore_ops.h>
19 #include <linux/io.h>
20 #include <linux/irqchip/arm-gic.h>
21 #include <linux/err.h>
22 #include <linux/clk.h>
23
24 #include <asm/cacheflush.h>
25 #include <asm/hardware/cache-l2x0.h>
26 #include <asm/smp_scu.h>
27 #include <asm/suspend.h>
28
29 #include <plat/pm-common.h>
30 #include <plat/pll.h>
31 #include <plat/regs-srom.h>
32
33 #include <mach/map.h>
34
35 #include "common.h"
36 #include "regs-pmu.h"
37
38 /**
39  * struct exynos_wkup_irq - Exynos GIC to PMU IRQ mapping
40  * @hwirq: Hardware IRQ signal of the GIC
41  * @mask: Mask in PMU wake-up mask register
42  */
43 struct exynos_wkup_irq {
44         unsigned int hwirq;
45         u32 mask;
46 };
47
48 static struct sleep_save exynos5_sys_save[] = {
49         SAVE_ITEM(EXYNOS5_SYS_I2C_CFG),
50 };
51
52 static struct sleep_save exynos_core_save[] = {
53         /* SROM side */
54         SAVE_ITEM(S5P_SROM_BW),
55         SAVE_ITEM(S5P_SROM_BC0),
56         SAVE_ITEM(S5P_SROM_BC1),
57         SAVE_ITEM(S5P_SROM_BC2),
58         SAVE_ITEM(S5P_SROM_BC3),
59 };
60
61 /*
62  * GIC wake-up support
63  */
64
65 static u32 exynos_irqwake_intmask = 0xffffffff;
66
67 static const struct exynos_wkup_irq exynos4_wkup_irq[] = {
68         { 76, BIT(1) }, /* RTC alarm */
69         { 77, BIT(2) }, /* RTC tick */
70         { /* sentinel */ },
71 };
72
73 static const struct exynos_wkup_irq exynos5250_wkup_irq[] = {
74         { 75, BIT(1) }, /* RTC alarm */
75         { 76, BIT(2) }, /* RTC tick */
76         { /* sentinel */ },
77 };
78
79 static int exynos_irq_set_wake(struct irq_data *data, unsigned int state)
80 {
81         const struct exynos_wkup_irq *wkup_irq;
82
83         if (soc_is_exynos5250())
84                 wkup_irq = exynos5250_wkup_irq;
85         else
86                 wkup_irq = exynos4_wkup_irq;
87
88         while (wkup_irq->mask) {
89                 if (wkup_irq->hwirq == data->hwirq) {
90                         if (!state)
91                                 exynos_irqwake_intmask |= wkup_irq->mask;
92                         else
93                                 exynos_irqwake_intmask &= ~wkup_irq->mask;
94                         return 0;
95                 }
96                 ++wkup_irq;
97         }
98
99         return -ENOENT;
100 }
101
102 /**
103  * exynos_core_power_down : power down the specified cpu
104  * @cpu : the cpu to power down
105  *
106  * Power down the specified cpu. The sequence must be finished by a
107  * call to cpu_do_idle()
108  *
109  */
110 void exynos_cpu_power_down(int cpu)
111 {
112         __raw_writel(0, EXYNOS_ARM_CORE_CONFIGURATION(cpu));
113 }
114
115 /**
116  * exynos_cpu_power_up : power up the specified cpu
117  * @cpu : the cpu to power up
118  *
119  * Power up the specified cpu
120  */
121 void exynos_cpu_power_up(int cpu)
122 {
123         __raw_writel(S5P_CORE_LOCAL_PWR_EN,
124                      EXYNOS_ARM_CORE_CONFIGURATION(cpu));
125 }
126
127 /**
128  * exynos_cpu_power_state : returns the power state of the cpu
129  * @cpu : the cpu to retrieve the power state from
130  *
131  */
132 int exynos_cpu_power_state(int cpu)
133 {
134         return (__raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) &
135                         S5P_CORE_LOCAL_PWR_EN);
136 }
137
138 /**
139  * exynos_cluster_power_down : power down the specified cluster
140  * @cluster : the cluster to power down
141  */
142 void exynos_cluster_power_down(int cluster)
143 {
144         __raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster));
145 }
146
147 /**
148  * exynos_cluster_power_up : power up the specified cluster
149  * @cluster : the cluster to power up
150  */
151 void exynos_cluster_power_up(int cluster)
152 {
153         __raw_writel(S5P_CORE_LOCAL_PWR_EN,
154                      EXYNOS_COMMON_CONFIGURATION(cluster));
155 }
156
157 /**
158  * exynos_cluster_power_state : returns the power state of the cluster
159  * @cluster : the cluster to retrieve the power state from
160  *
161  */
162 int exynos_cluster_power_state(int cluster)
163 {
164         return (__raw_readl(EXYNOS_COMMON_STATUS(cluster)) &
165                         S5P_CORE_LOCAL_PWR_EN);
166 }
167
168 /* For Cortex-A9 Diagnostic and Power control register */
169 static unsigned int save_arm_register[2];
170
171 static int exynos_cpu_suspend(unsigned long arg)
172 {
173 #ifdef CONFIG_CACHE_L2X0
174         outer_flush_all();
175 #endif
176
177         if (soc_is_exynos5250())
178                 flush_cache_all();
179
180         /* issue the standby signal into the pm unit. */
181         cpu_do_idle();
182
183         pr_info("Failed to suspend the system\n");
184         return 1; /* Aborting suspend */
185 }
186
187 static void exynos_pm_prepare(void)
188 {
189         unsigned int tmp;
190
191         /* Set wake-up mask registers */
192         __raw_writel(exynos_get_eint_wake_mask(), S5P_EINT_WAKEUP_MASK);
193         __raw_writel(exynos_irqwake_intmask & ~(1 << 31), S5P_WAKEUP_MASK);
194
195         s3c_pm_do_save(exynos_core_save, ARRAY_SIZE(exynos_core_save));
196
197         if (soc_is_exynos5250()) {
198                 s3c_pm_do_save(exynos5_sys_save, ARRAY_SIZE(exynos5_sys_save));
199                 /* Disable USE_RETENTION of JPEG_MEM_OPTION */
200                 tmp = __raw_readl(EXYNOS5_JPEG_MEM_OPTION);
201                 tmp &= ~EXYNOS5_OPTION_USE_RETENTION;
202                 __raw_writel(tmp, EXYNOS5_JPEG_MEM_OPTION);
203         }
204
205         /* Set value of power down register for sleep mode */
206
207         exynos_sys_powerdown_conf(SYS_SLEEP);
208         __raw_writel(S5P_CHECK_SLEEP, S5P_INFORM1);
209
210         /* ensure at least INFORM0 has the resume address */
211
212         __raw_writel(virt_to_phys(exynos_cpu_resume), S5P_INFORM0);
213 }
214
215 static int exynos_pm_suspend(void)
216 {
217         unsigned long tmp;
218
219         /* Setting Central Sequence Register for power down mode */
220
221         tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
222         tmp &= ~S5P_CENTRAL_LOWPWR_CFG;
223         __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
224
225         /* Setting SEQ_OPTION register */
226
227         tmp = (S5P_USE_STANDBY_WFI0 | S5P_USE_STANDBY_WFE0);
228         __raw_writel(tmp, S5P_CENTRAL_SEQ_OPTION);
229
230         if (!soc_is_exynos5250()) {
231                 /* Save Power control register */
232                 asm ("mrc p15, 0, %0, c15, c0, 0"
233                      : "=r" (tmp) : : "cc");
234                 save_arm_register[0] = tmp;
235
236                 /* Save Diagnostic register */
237                 asm ("mrc p15, 0, %0, c15, c0, 1"
238                      : "=r" (tmp) : : "cc");
239                 save_arm_register[1] = tmp;
240         }
241
242         return 0;
243 }
244
245 static void exynos_pm_resume(void)
246 {
247         unsigned long tmp;
248
249         /*
250          * If PMU failed while entering sleep mode, WFI will be
251          * ignored by PMU and then exiting cpu_do_idle().
252          * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically
253          * in this situation.
254          */
255         tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
256         if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) {
257                 tmp |= S5P_CENTRAL_LOWPWR_CFG;
258                 __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
259                 /* clear the wakeup state register */
260                 __raw_writel(0x0, S5P_WAKEUP_STAT);
261                 /* No need to perform below restore code */
262                 goto early_wakeup;
263         }
264         if (!soc_is_exynos5250()) {
265                 /* Restore Power control register */
266                 tmp = save_arm_register[0];
267                 asm volatile ("mcr p15, 0, %0, c15, c0, 0"
268                               : : "r" (tmp)
269                               : "cc");
270
271                 /* Restore Diagnostic register */
272                 tmp = save_arm_register[1];
273                 asm volatile ("mcr p15, 0, %0, c15, c0, 1"
274                               : : "r" (tmp)
275                               : "cc");
276         }
277
278         /* For release retention */
279
280         __raw_writel((1 << 28), S5P_PAD_RET_MAUDIO_OPTION);
281         __raw_writel((1 << 28), S5P_PAD_RET_GPIO_OPTION);
282         __raw_writel((1 << 28), S5P_PAD_RET_UART_OPTION);
283         __raw_writel((1 << 28), S5P_PAD_RET_MMCA_OPTION);
284         __raw_writel((1 << 28), S5P_PAD_RET_MMCB_OPTION);
285         __raw_writel((1 << 28), S5P_PAD_RET_EBIA_OPTION);
286         __raw_writel((1 << 28), S5P_PAD_RET_EBIB_OPTION);
287
288         if (soc_is_exynos5250())
289                 s3c_pm_do_restore(exynos5_sys_save,
290                         ARRAY_SIZE(exynos5_sys_save));
291
292         s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));
293
294         if (IS_ENABLED(CONFIG_SMP) && !soc_is_exynos5250())
295                 scu_enable(S5P_VA_SCU);
296
297 early_wakeup:
298
299         /* Clear SLEEP mode set in INFORM1 */
300         __raw_writel(0x0, S5P_INFORM1);
301
302         return;
303 }
304
305 static struct syscore_ops exynos_pm_syscore_ops = {
306         .suspend        = exynos_pm_suspend,
307         .resume         = exynos_pm_resume,
308 };
309
310 /*
311  * Suspend Ops
312  */
313
314 static int exynos_suspend_enter(suspend_state_t state)
315 {
316         int ret;
317
318         s3c_pm_debug_init();
319
320         S3C_PMDBG("%s: suspending the system...\n", __func__);
321
322         S3C_PMDBG("%s: wakeup masks: %08x,%08x\n", __func__,
323                         exynos_irqwake_intmask, exynos_get_eint_wake_mask());
324
325         if (exynos_irqwake_intmask == -1U
326             && exynos_get_eint_wake_mask() == -1U) {
327                 pr_err("%s: No wake-up sources!\n", __func__);
328                 pr_err("%s: Aborting sleep\n", __func__);
329                 return -EINVAL;
330         }
331
332         s3c_pm_save_uarts();
333         exynos_pm_prepare();
334         flush_cache_all();
335         s3c_pm_check_store();
336
337         ret = cpu_suspend(0, exynos_cpu_suspend);
338         if (ret)
339                 return ret;
340
341         s3c_pm_restore_uarts();
342
343         S3C_PMDBG("%s: wakeup stat: %08x\n", __func__,
344                         __raw_readl(S5P_WAKEUP_STAT));
345
346         s3c_pm_check_restore();
347
348         S3C_PMDBG("%s: resuming the system...\n", __func__);
349
350         return 0;
351 }
352
353 static int exynos_suspend_prepare(void)
354 {
355         s3c_pm_check_prepare();
356
357         return 0;
358 }
359
360 static void exynos_suspend_finish(void)
361 {
362         s3c_pm_check_cleanup();
363 }
364
365 static const struct platform_suspend_ops exynos_suspend_ops = {
366         .enter          = exynos_suspend_enter,
367         .prepare        = exynos_suspend_prepare,
368         .finish         = exynos_suspend_finish,
369         .valid          = suspend_valid_only_mem,
370 };
371
372 void __init exynos_pm_init(void)
373 {
374         u32 tmp;
375
376         /* Platform-specific GIC callback */
377         gic_arch_extn.irq_set_wake = exynos_irq_set_wake;
378
379         /* All wakeup disable */
380         tmp = __raw_readl(S5P_WAKEUP_MASK);
381         tmp |= ((0xFF << 8) | (0x1F << 1));
382         __raw_writel(tmp, S5P_WAKEUP_MASK);
383
384         register_syscore_ops(&exynos_pm_syscore_ops);
385         suspend_set_ops(&exynos_suspend_ops);
386 }