blob: d0c23010b693a57625d002694528d0cd2fffaaa5 [file] [log] [blame]
Ben Dooks64197112008-12-12 00:24:06 +00001/* linux/arch/arm/plat-s3c/pm.c
2 *
3 * Copyright 2008 Openmoko, Inc.
Ben Dooksccae9412009-11-13 22:54:14 +00004 * Copyright 2004-2008 Simtec Electronics
Ben Dooks64197112008-12-12 00:24:06 +00005 * Ben Dooks <ben@simtec.co.uk>
6 * http://armlinux.simtec.co.uk/
7 *
8 * S3C common power management (suspend to ram) support.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13*/
14
15#include <linux/init.h>
16#include <linux/suspend.h>
17#include <linux/errno.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000018#include <linux/delay.h>
Tomasz Figacd3fc1b2013-05-17 18:24:29 +020019#include <linux/of.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000020#include <linux/serial_core.h>
Ben Dooks64197112008-12-12 00:24:06 +000021#include <linux/io.h>
22
Ben Dooks2261e0e2008-12-12 00:24:08 +000023#include <asm/cacheflush.h>
Russell King2c74a0c2011-06-22 17:41:48 +010024#include <asm/suspend.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000025
26#include <plat/regs-serial.h>
Tomasz Figad6280ff2013-06-15 09:17:33 +090027
28#ifdef CONFIG_SAMSUNG_ATAGS
29#include <mach/hardware.h>
30#include <mach/map.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000031#include <mach/regs-clock.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000032#include <mach/regs-irq.h>
Arnd Bergmann7ba80222013-03-05 11:28:29 +010033#include <mach/irqs.h>
Tomasz Figad6280ff2013-06-15 09:17:33 +090034#endif
35
Ben Dooks56b34422008-12-12 00:24:12 +000036#include <asm/irq.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000037
Ben Dooks64197112008-12-12 00:24:06 +000038#include <plat/pm.h>
Ben Dooks431fb7d2010-02-05 13:52:53 +010039#include <mach/pm-core.h>
Ben Dooks64197112008-12-12 00:24:06 +000040
41/* for external use */
42
43unsigned long s3c_pm_flags;
44
Ben Dooks2261e0e2008-12-12 00:24:08 +000045/* Debug code:
46 *
47 * This code supports debug output to the low level UARTs for use on
48 * resume before the console layer is available.
49*/
50
Ben Dooks80057452010-01-20 12:29:25 +090051#ifdef CONFIG_SAMSUNG_PM_DEBUG
Ben Dooks64197112008-12-12 00:24:06 +000052extern void printascii(const char *);
53
54void s3c_pm_dbg(const char *fmt, ...)
55{
56 va_list va;
57 char buff[256];
58
59 va_start(va, fmt);
Chen Gang8baaa262013-01-25 10:29:01 -080060 vsnprintf(buff, sizeof(buff), fmt, va);
Ben Dooks64197112008-12-12 00:24:06 +000061 va_end(va);
62
63 printascii(buff);
64}
Ben Dooks2261e0e2008-12-12 00:24:08 +000065
66static inline void s3c_pm_debug_init(void)
67{
68 /* restart uart clocks so we can use them to output */
69 s3c_pm_debug_init_uart();
70}
71
72#else
73#define s3c_pm_debug_init() do { } while(0)
74
Ben Dooks80057452010-01-20 12:29:25 +090075#endif /* CONFIG_SAMSUNG_PM_DEBUG */
Ben Dooks64197112008-12-12 00:24:06 +000076
Ben Dooks2261e0e2008-12-12 00:24:08 +000077/* Save the UART configurations if we are configured for debug. */
78
Ben Dooks4b637dc2008-12-12 00:24:24 +000079unsigned char pm_uart_udivslot;
80
Ben Dooks80057452010-01-20 12:29:25 +090081#ifdef CONFIG_SAMSUNG_PM_DEBUG
Ben Dooks2261e0e2008-12-12 00:24:08 +000082
Yadwinder Singh Brar7bdc84f2013-07-24 17:05:07 +090083static struct pm_uart_save uart_save;
Ben Dooks2261e0e2008-12-12 00:24:08 +000084
Ben Dooksd2b07fe2008-12-12 00:24:20 +000085static void s3c_pm_save_uart(unsigned int uart, struct pm_uart_save *save)
Ben Dooks2261e0e2008-12-12 00:24:08 +000086{
Ben Dooksd2b07fe2008-12-12 00:24:20 +000087 void __iomem *regs = S3C_VA_UARTx(uart);
88
89 save->ulcon = __raw_readl(regs + S3C2410_ULCON);
90 save->ucon = __raw_readl(regs + S3C2410_UCON);
91 save->ufcon = __raw_readl(regs + S3C2410_UFCON);
92 save->umcon = __raw_readl(regs + S3C2410_UMCON);
93 save->ubrdiv = __raw_readl(regs + S3C2410_UBRDIV);
Ben Dooks57699e92008-12-12 00:24:23 +000094
Ben Dooks4b637dc2008-12-12 00:24:24 +000095 if (pm_uart_udivslot)
96 save->udivslot = __raw_readl(regs + S3C2443_DIVSLOT);
97
Ben Dooks57699e92008-12-12 00:24:23 +000098 S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n",
99 uart, save->ulcon, save->ucon, save->ufcon, save->ubrdiv);
Ben Dooks2261e0e2008-12-12 00:24:08 +0000100}
101
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000102static void s3c_pm_save_uarts(void)
Ben Dooks2261e0e2008-12-12 00:24:08 +0000103{
Yadwinder Singh Brar7bdc84f2013-07-24 17:05:07 +0900104 s3c_pm_save_uart(CONFIG_DEBUG_S3C_UART, &uart_save);
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000105}
106
107static void s3c_pm_restore_uart(unsigned int uart, struct pm_uart_save *save)
108{
109 void __iomem *regs = S3C_VA_UARTx(uart);
110
Ben Dooks57699e92008-12-12 00:24:23 +0000111 s3c_pm_arch_update_uart(regs, save);
112
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000113 __raw_writel(save->ulcon, regs + S3C2410_ULCON);
114 __raw_writel(save->ucon, regs + S3C2410_UCON);
115 __raw_writel(save->ufcon, regs + S3C2410_UFCON);
116 __raw_writel(save->umcon, regs + S3C2410_UMCON);
117 __raw_writel(save->ubrdiv, regs + S3C2410_UBRDIV);
Ben Dooks4b637dc2008-12-12 00:24:24 +0000118
119 if (pm_uart_udivslot)
120 __raw_writel(save->udivslot, regs + S3C2443_DIVSLOT);
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000121}
122
123static void s3c_pm_restore_uarts(void)
124{
Yadwinder Singh Brar7bdc84f2013-07-24 17:05:07 +0900125 s3c_pm_restore_uart(CONFIG_DEBUG_S3C_UART, &uart_save);
Ben Dooks2261e0e2008-12-12 00:24:08 +0000126}
127#else
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000128static void s3c_pm_save_uarts(void) { }
129static void s3c_pm_restore_uarts(void) { }
Ben Dooks2261e0e2008-12-12 00:24:08 +0000130#endif
131
Ben Dooks56b34422008-12-12 00:24:12 +0000132/* The IRQ ext-int code goes here, it is too small to currently bother
133 * with its own file. */
134
135unsigned long s3c_irqwake_intmask = 0xffffffffL;
136unsigned long s3c_irqwake_eintmask = 0xffffffffL;
137
Mark Brownf5aeffb2010-12-02 14:35:38 +0900138int s3c_irqext_wake(struct irq_data *data, unsigned int state)
Ben Dooks56b34422008-12-12 00:24:12 +0000139{
Mark Brownf5aeffb2010-12-02 14:35:38 +0900140 unsigned long bit = 1L << IRQ_EINT_BIT(data->irq);
Ben Dooks56b34422008-12-12 00:24:12 +0000141
142 if (!(s3c_irqwake_eintallow & bit))
143 return -ENOENT;
144
145 printk(KERN_INFO "wake %s for irq %d\n",
Mark Brownf5aeffb2010-12-02 14:35:38 +0900146 state ? "enabled" : "disabled", data->irq);
Ben Dooks56b34422008-12-12 00:24:12 +0000147
148 if (!state)
149 s3c_irqwake_eintmask |= bit;
150 else
151 s3c_irqwake_eintmask &= ~bit;
152
153 return 0;
154}
Ben Dooks64197112008-12-12 00:24:06 +0000155
156/* helper functions to save and restore register state */
157
158/**
159 * s3c_pm_do_save() - save a set of registers for restoration on resume.
160 * @ptr: Pointer to an array of registers.
161 * @count: Size of the ptr array.
162 *
163 * Run through the list of registers given, saving their contents in the
164 * array for later restoration when we wakeup.
165 */
166void s3c_pm_do_save(struct sleep_save *ptr, int count)
167{
168 for (; count > 0; count--, ptr++) {
169 ptr->val = __raw_readl(ptr->reg);
170 S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val);
171 }
172}
173
174/**
175 * s3c_pm_do_restore() - restore register values from the save list.
176 * @ptr: Pointer to an array of registers.
177 * @count: Size of the ptr array.
178 *
179 * Restore the register values saved from s3c_pm_do_save().
180 *
181 * Note, we do not use S3C_PMDBG() in here, as the system may not have
182 * restore the UARTs state yet
183*/
184
185void s3c_pm_do_restore(struct sleep_save *ptr, int count)
186{
187 for (; count > 0; count--, ptr++) {
188 printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
189 ptr->reg, ptr->val, __raw_readl(ptr->reg));
190
191 __raw_writel(ptr->val, ptr->reg);
192 }
193}
194
195/**
196 * s3c_pm_do_restore_core() - early restore register values from save list.
197 *
198 * This is similar to s3c_pm_do_restore() except we try and minimise the
199 * side effects of the function in case registers that hardware might need
200 * to work has been restored.
201 *
202 * WARNING: Do not put any debug in here that may effect memory or use
203 * peripherals, as things may be changing!
204*/
205
206void s3c_pm_do_restore_core(struct sleep_save *ptr, int count)
207{
208 for (; count > 0; count--, ptr++)
209 __raw_writel(ptr->val, ptr->reg);
210}
Ben Dooks2261e0e2008-12-12 00:24:08 +0000211
212/* s3c2410_pm_show_resume_irqs
213 *
214 * print any IRQs asserted at resume time (ie, we woke from)
215*/
Maurus Cuelenaerebaab7302011-04-02 10:50:22 +0900216static void __maybe_unused s3c_pm_show_resume_irqs(int start,
217 unsigned long which,
218 unsigned long mask)
Ben Dooks2261e0e2008-12-12 00:24:08 +0000219{
220 int i;
221
222 which &= ~mask;
223
224 for (i = 0; i <= 31; i++) {
225 if (which & (1L<<i)) {
226 S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
227 }
228 }
229}
230
231
232void (*pm_cpu_prep)(void);
Russell King29cb3cd2011-07-02 09:54:01 +0100233int (*pm_cpu_sleep)(unsigned long);
Ben Dooks2261e0e2008-12-12 00:24:08 +0000234
235#define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
236
237/* s3c_pm_enter
238 *
239 * central control for sleep/resume process
240*/
241
242static int s3c_pm_enter(suspend_state_t state)
243{
Abhilash Kesavand3fcacf2013-01-25 10:40:19 -0800244 int ret;
Ben Dooks2261e0e2008-12-12 00:24:08 +0000245 /* ensure the debug is initialised (if enabled) */
246
247 s3c_pm_debug_init();
248
249 S3C_PMDBG("%s(%d)\n", __func__, state);
250
251 if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
252 printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__);
253 return -EINVAL;
254 }
255
256 /* check if we have anything to wake-up with... bad things seem
257 * to happen if you suspend with no wakeup (system will often
258 * require a full power-cycle)
259 */
260
Tomasz Figacd3fc1b2013-05-17 18:24:29 +0200261 if (!of_have_populated_dt() &&
262 !any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
Ben Dooks2261e0e2008-12-12 00:24:08 +0000263 !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
264 printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
265 printk(KERN_ERR "%s: Aborting sleep\n", __func__);
266 return -EINVAL;
267 }
268
Ben Dooks2261e0e2008-12-12 00:24:08 +0000269 /* save all necessary core registers not covered by the drivers */
270
Tomasz Figacd3fc1b2013-05-17 18:24:29 +0200271 if (!of_have_populated_dt()) {
272 samsung_pm_save_gpios();
273 samsung_pm_saved_gpios();
274 }
275
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000276 s3c_pm_save_uarts();
Ben Dooks2261e0e2008-12-12 00:24:08 +0000277 s3c_pm_save_core();
278
279 /* set the irq configuration for wake */
280
281 s3c_pm_configure_extint();
282
283 S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
284 s3c_irqwake_intmask, s3c_irqwake_eintmask);
285
286 s3c_pm_arch_prepare_irqs();
287
288 /* call cpu specific preparation */
289
290 pm_cpu_prep();
291
292 /* flush cache back to ram */
293
294 flush_cache_all();
295
296 s3c_pm_check_store();
297
298 /* send the cpu to sleep... */
299
300 s3c_pm_arch_stop_clocks();
301
Russell Kinge7089da2011-06-21 19:29:26 +0100302 /* this will also act as our return point from when
Ben Dooksfff94cd2009-03-10 11:48:07 +0000303 * we resume as it saves its own register state and restores it
304 * during the resume. */
Ben Dooks2261e0e2008-12-12 00:24:08 +0000305
Abhilash Kesavand3fcacf2013-01-25 10:40:19 -0800306 ret = cpu_suspend(0, pm_cpu_sleep);
307 if (ret)
308 return ret;
Ben Dooks2261e0e2008-12-12 00:24:08 +0000309
Ben Dooks2261e0e2008-12-12 00:24:08 +0000310 /* restore the system state */
311
312 s3c_pm_restore_core();
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000313 s3c_pm_restore_uarts();
Tomasz Figacd3fc1b2013-05-17 18:24:29 +0200314
315 if (!of_have_populated_dt()) {
316 samsung_pm_restore_gpios();
317 s3c_pm_restored_gpios();
318 }
Ben Dooks2261e0e2008-12-12 00:24:08 +0000319
320 s3c_pm_debug_init();
321
322 /* check what irq (if any) restored the system */
323
324 s3c_pm_arch_show_resume_irqs();
325
326 S3C_PMDBG("%s: post sleep, preparing to return\n", __func__);
327
Ben Dooksbd117bd2009-03-10 18:19:35 +0000328 /* LEDs should now be 1110 */
329 s3c_pm_debug_smdkled(1 << 1, 0);
330
Ben Dooks2261e0e2008-12-12 00:24:08 +0000331 s3c_pm_check_restore();
332
333 /* ok, let's return from sleep */
334
335 S3C_PMDBG("S3C PM Resume (post-restore)\n");
336 return 0;
337}
338
Ben Dooksaa8aba62008-12-12 00:24:34 +0000339static int s3c_pm_prepare(void)
340{
341 /* prepare check area if configured */
342
343 s3c_pm_check_prepare();
344 return 0;
345}
346
347static void s3c_pm_finish(void)
348{
349 s3c_pm_check_cleanup();
350}
351
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100352static const struct platform_suspend_ops s3c_pm_ops = {
Ben Dooks2261e0e2008-12-12 00:24:08 +0000353 .enter = s3c_pm_enter,
Ben Dooksaa8aba62008-12-12 00:24:34 +0000354 .prepare = s3c_pm_prepare,
355 .finish = s3c_pm_finish,
Ben Dooks2261e0e2008-12-12 00:24:08 +0000356 .valid = suspend_valid_only_mem,
357};
358
Ben Dooks4e59c252008-12-12 00:24:18 +0000359/* s3c_pm_init
Ben Dooks2261e0e2008-12-12 00:24:08 +0000360 *
361 * Attach the power management functions. This should be called
362 * from the board specific initialisation if the board supports
363 * it.
364*/
365
Ben Dooks4e59c252008-12-12 00:24:18 +0000366int __init s3c_pm_init(void)
Ben Dooks2261e0e2008-12-12 00:24:08 +0000367{
368 printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
369
370 suspend_set_ops(&s3c_pm_ops);
371 return 0;
372}