blob: d76ab723e1b759f18e523530dc0861f871d12e06 [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>
19#include <linux/serial_core.h>
Ben Dooks64197112008-12-12 00:24:06 +000020#include <linux/io.h>
21
Ben Dooks2261e0e2008-12-12 00:24:08 +000022#include <asm/cacheflush.h>
Russell King2c74a0c2011-06-22 17:41:48 +010023#include <asm/suspend.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000024
25#include <plat/regs-serial.h>
Tomasz Figad6280ff2013-06-15 09:17:33 +090026
27#ifdef CONFIG_SAMSUNG_ATAGS
28#include <mach/hardware.h>
29#include <mach/map.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000030#include <mach/regs-clock.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000031#include <mach/regs-irq.h>
Arnd Bergmann7ba80222013-03-05 11:28:29 +010032#include <mach/irqs.h>
Tomasz Figad6280ff2013-06-15 09:17:33 +090033#endif
34
Ben Dooks56b34422008-12-12 00:24:12 +000035#include <asm/irq.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000036
Ben Dooks64197112008-12-12 00:24:06 +000037#include <plat/pm.h>
Ben Dooks431fb7d2010-02-05 13:52:53 +010038#include <mach/pm-core.h>
Ben Dooks64197112008-12-12 00:24:06 +000039
40/* for external use */
41
42unsigned long s3c_pm_flags;
43
Ben Dooks2261e0e2008-12-12 00:24:08 +000044/* Debug code:
45 *
46 * This code supports debug output to the low level UARTs for use on
47 * resume before the console layer is available.
48*/
49
Ben Dooks80057452010-01-20 12:29:25 +090050#ifdef CONFIG_SAMSUNG_PM_DEBUG
Ben Dooks64197112008-12-12 00:24:06 +000051extern void printascii(const char *);
52
53void s3c_pm_dbg(const char *fmt, ...)
54{
55 va_list va;
56 char buff[256];
57
58 va_start(va, fmt);
Chen Gang8baaa262013-01-25 10:29:01 -080059 vsnprintf(buff, sizeof(buff), fmt, va);
Ben Dooks64197112008-12-12 00:24:06 +000060 va_end(va);
61
62 printascii(buff);
63}
Ben Dooks2261e0e2008-12-12 00:24:08 +000064
65static inline void s3c_pm_debug_init(void)
66{
67 /* restart uart clocks so we can use them to output */
68 s3c_pm_debug_init_uart();
69}
70
71#else
72#define s3c_pm_debug_init() do { } while(0)
73
Ben Dooks80057452010-01-20 12:29:25 +090074#endif /* CONFIG_SAMSUNG_PM_DEBUG */
Ben Dooks64197112008-12-12 00:24:06 +000075
Ben Dooks2261e0e2008-12-12 00:24:08 +000076/* Save the UART configurations if we are configured for debug. */
77
Ben Dooks4b637dc2008-12-12 00:24:24 +000078unsigned char pm_uart_udivslot;
79
Ben Dooks80057452010-01-20 12:29:25 +090080#ifdef CONFIG_SAMSUNG_PM_DEBUG
Ben Dooks2261e0e2008-12-12 00:24:08 +000081
Sachin Kamatc0401242012-08-07 19:17:14 +090082static struct pm_uart_save uart_save[CONFIG_SERIAL_SAMSUNG_UARTS];
Ben Dooks2261e0e2008-12-12 00:24:08 +000083
Ben Dooksd2b07fe2008-12-12 00:24:20 +000084static void s3c_pm_save_uart(unsigned int uart, struct pm_uart_save *save)
Ben Dooks2261e0e2008-12-12 00:24:08 +000085{
Ben Dooksd2b07fe2008-12-12 00:24:20 +000086 void __iomem *regs = S3C_VA_UARTx(uart);
87
88 save->ulcon = __raw_readl(regs + S3C2410_ULCON);
89 save->ucon = __raw_readl(regs + S3C2410_UCON);
90 save->ufcon = __raw_readl(regs + S3C2410_UFCON);
91 save->umcon = __raw_readl(regs + S3C2410_UMCON);
92 save->ubrdiv = __raw_readl(regs + S3C2410_UBRDIV);
Ben Dooks57699e92008-12-12 00:24:23 +000093
Ben Dooks4b637dc2008-12-12 00:24:24 +000094 if (pm_uart_udivslot)
95 save->udivslot = __raw_readl(regs + S3C2443_DIVSLOT);
96
Ben Dooks57699e92008-12-12 00:24:23 +000097 S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n",
98 uart, save->ulcon, save->ucon, save->ufcon, save->ubrdiv);
Ben Dooks2261e0e2008-12-12 00:24:08 +000099}
100
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000101static void s3c_pm_save_uarts(void)
Ben Dooks2261e0e2008-12-12 00:24:08 +0000102{
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000103 struct pm_uart_save *save = uart_save;
104 unsigned int uart;
105
106 for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
107 s3c_pm_save_uart(uart, save);
108}
109
110static void s3c_pm_restore_uart(unsigned int uart, struct pm_uart_save *save)
111{
112 void __iomem *regs = S3C_VA_UARTx(uart);
113
Ben Dooks57699e92008-12-12 00:24:23 +0000114 s3c_pm_arch_update_uart(regs, save);
115
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000116 __raw_writel(save->ulcon, regs + S3C2410_ULCON);
117 __raw_writel(save->ucon, regs + S3C2410_UCON);
118 __raw_writel(save->ufcon, regs + S3C2410_UFCON);
119 __raw_writel(save->umcon, regs + S3C2410_UMCON);
120 __raw_writel(save->ubrdiv, regs + S3C2410_UBRDIV);
Ben Dooks4b637dc2008-12-12 00:24:24 +0000121
122 if (pm_uart_udivslot)
123 __raw_writel(save->udivslot, regs + S3C2443_DIVSLOT);
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000124}
125
126static void s3c_pm_restore_uarts(void)
127{
128 struct pm_uart_save *save = uart_save;
129 unsigned int uart;
130
131 for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
132 s3c_pm_restore_uart(uart, save);
Ben Dooks2261e0e2008-12-12 00:24:08 +0000133}
134#else
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000135static void s3c_pm_save_uarts(void) { }
136static void s3c_pm_restore_uarts(void) { }
Ben Dooks2261e0e2008-12-12 00:24:08 +0000137#endif
138
Ben Dooks56b34422008-12-12 00:24:12 +0000139/* The IRQ ext-int code goes here, it is too small to currently bother
140 * with its own file. */
141
142unsigned long s3c_irqwake_intmask = 0xffffffffL;
143unsigned long s3c_irqwake_eintmask = 0xffffffffL;
144
Mark Brownf5aeffb2010-12-02 14:35:38 +0900145int s3c_irqext_wake(struct irq_data *data, unsigned int state)
Ben Dooks56b34422008-12-12 00:24:12 +0000146{
Mark Brownf5aeffb2010-12-02 14:35:38 +0900147 unsigned long bit = 1L << IRQ_EINT_BIT(data->irq);
Ben Dooks56b34422008-12-12 00:24:12 +0000148
149 if (!(s3c_irqwake_eintallow & bit))
150 return -ENOENT;
151
152 printk(KERN_INFO "wake %s for irq %d\n",
Mark Brownf5aeffb2010-12-02 14:35:38 +0900153 state ? "enabled" : "disabled", data->irq);
Ben Dooks56b34422008-12-12 00:24:12 +0000154
155 if (!state)
156 s3c_irqwake_eintmask |= bit;
157 else
158 s3c_irqwake_eintmask &= ~bit;
159
160 return 0;
161}
Ben Dooks64197112008-12-12 00:24:06 +0000162
163/* helper functions to save and restore register state */
164
165/**
166 * s3c_pm_do_save() - save a set of registers for restoration on resume.
167 * @ptr: Pointer to an array of registers.
168 * @count: Size of the ptr array.
169 *
170 * Run through the list of registers given, saving their contents in the
171 * array for later restoration when we wakeup.
172 */
173void s3c_pm_do_save(struct sleep_save *ptr, int count)
174{
175 for (; count > 0; count--, ptr++) {
176 ptr->val = __raw_readl(ptr->reg);
177 S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val);
178 }
179}
180
181/**
182 * s3c_pm_do_restore() - restore register values from the save list.
183 * @ptr: Pointer to an array of registers.
184 * @count: Size of the ptr array.
185 *
186 * Restore the register values saved from s3c_pm_do_save().
187 *
188 * Note, we do not use S3C_PMDBG() in here, as the system may not have
189 * restore the UARTs state yet
190*/
191
192void s3c_pm_do_restore(struct sleep_save *ptr, int count)
193{
194 for (; count > 0; count--, ptr++) {
195 printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
196 ptr->reg, ptr->val, __raw_readl(ptr->reg));
197
198 __raw_writel(ptr->val, ptr->reg);
199 }
200}
201
202/**
203 * s3c_pm_do_restore_core() - early restore register values from save list.
204 *
205 * This is similar to s3c_pm_do_restore() except we try and minimise the
206 * side effects of the function in case registers that hardware might need
207 * to work has been restored.
208 *
209 * WARNING: Do not put any debug in here that may effect memory or use
210 * peripherals, as things may be changing!
211*/
212
213void s3c_pm_do_restore_core(struct sleep_save *ptr, int count)
214{
215 for (; count > 0; count--, ptr++)
216 __raw_writel(ptr->val, ptr->reg);
217}
Ben Dooks2261e0e2008-12-12 00:24:08 +0000218
219/* s3c2410_pm_show_resume_irqs
220 *
221 * print any IRQs asserted at resume time (ie, we woke from)
222*/
Maurus Cuelenaerebaab7302011-04-02 10:50:22 +0900223static void __maybe_unused s3c_pm_show_resume_irqs(int start,
224 unsigned long which,
225 unsigned long mask)
Ben Dooks2261e0e2008-12-12 00:24:08 +0000226{
227 int i;
228
229 which &= ~mask;
230
231 for (i = 0; i <= 31; i++) {
232 if (which & (1L<<i)) {
233 S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
234 }
235 }
236}
237
238
239void (*pm_cpu_prep)(void);
Russell King29cb3cd2011-07-02 09:54:01 +0100240int (*pm_cpu_sleep)(unsigned long);
Ben Dooks2261e0e2008-12-12 00:24:08 +0000241
242#define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
243
244/* s3c_pm_enter
245 *
246 * central control for sleep/resume process
247*/
248
249static int s3c_pm_enter(suspend_state_t state)
250{
Abhilash Kesavand3fcacf2013-01-25 10:40:19 -0800251 int ret;
Ben Dooks2261e0e2008-12-12 00:24:08 +0000252 /* ensure the debug is initialised (if enabled) */
253
254 s3c_pm_debug_init();
255
256 S3C_PMDBG("%s(%d)\n", __func__, state);
257
258 if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
259 printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__);
260 return -EINVAL;
261 }
262
263 /* check if we have anything to wake-up with... bad things seem
264 * to happen if you suspend with no wakeup (system will often
265 * require a full power-cycle)
266 */
267
268 if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
269 !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
270 printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
271 printk(KERN_ERR "%s: Aborting sleep\n", __func__);
272 return -EINVAL;
273 }
274
Ben Dooks2261e0e2008-12-12 00:24:08 +0000275 /* save all necessary core registers not covered by the drivers */
276
Kukjin Kim782d8a32011-08-30 20:47:32 +0900277 samsung_pm_save_gpios();
278 samsung_pm_saved_gpios();
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000279 s3c_pm_save_uarts();
Ben Dooks2261e0e2008-12-12 00:24:08 +0000280 s3c_pm_save_core();
281
282 /* set the irq configuration for wake */
283
284 s3c_pm_configure_extint();
285
286 S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
287 s3c_irqwake_intmask, s3c_irqwake_eintmask);
288
289 s3c_pm_arch_prepare_irqs();
290
291 /* call cpu specific preparation */
292
293 pm_cpu_prep();
294
295 /* flush cache back to ram */
296
297 flush_cache_all();
298
299 s3c_pm_check_store();
300
301 /* send the cpu to sleep... */
302
303 s3c_pm_arch_stop_clocks();
304
Russell Kinge7089da2011-06-21 19:29:26 +0100305 /* this will also act as our return point from when
Ben Dooksfff94cd2009-03-10 11:48:07 +0000306 * we resume as it saves its own register state and restores it
307 * during the resume. */
Ben Dooks2261e0e2008-12-12 00:24:08 +0000308
Abhilash Kesavand3fcacf2013-01-25 10:40:19 -0800309 ret = cpu_suspend(0, pm_cpu_sleep);
310 if (ret)
311 return ret;
Ben Dooks2261e0e2008-12-12 00:24:08 +0000312
Ben Dooks2261e0e2008-12-12 00:24:08 +0000313 /* restore the system state */
314
315 s3c_pm_restore_core();
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000316 s3c_pm_restore_uarts();
Kukjin Kim782d8a32011-08-30 20:47:32 +0900317 samsung_pm_restore_gpios();
Ben Dooks1bac2822011-06-01 10:44:50 +0100318 s3c_pm_restored_gpios();
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}