blob: 5c0a440d6e16741f34f547b7aa6583a16997b5c4 [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>
23#include <mach/hardware.h>
Ben Dooksbd117bd2009-03-10 18:19:35 +000024#include <mach/map.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000025
26#include <plat/regs-serial.h>
27#include <mach/regs-clock.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000028#include <mach/regs-irq.h>
Ben Dooks56b34422008-12-12 00:24:12 +000029#include <asm/irq.h>
Ben Dooks2261e0e2008-12-12 00:24:08 +000030
Ben Dooks64197112008-12-12 00:24:06 +000031#include <plat/pm.h>
Ben Dooks431fb7d2010-02-05 13:52:53 +010032#include <mach/pm-core.h>
Ben Dooks64197112008-12-12 00:24:06 +000033
34/* for external use */
35
36unsigned long s3c_pm_flags;
37
Ben Dooks2261e0e2008-12-12 00:24:08 +000038/* Debug code:
39 *
40 * This code supports debug output to the low level UARTs for use on
41 * resume before the console layer is available.
42*/
43
Ben Dooks80057452010-01-20 12:29:25 +090044#ifdef CONFIG_SAMSUNG_PM_DEBUG
Ben Dooks64197112008-12-12 00:24:06 +000045extern void printascii(const char *);
46
47void s3c_pm_dbg(const char *fmt, ...)
48{
49 va_list va;
50 char buff[256];
51
52 va_start(va, fmt);
53 vsprintf(buff, fmt, va);
54 va_end(va);
55
56 printascii(buff);
57}
Ben Dooks2261e0e2008-12-12 00:24:08 +000058
59static inline void s3c_pm_debug_init(void)
60{
61 /* restart uart clocks so we can use them to output */
62 s3c_pm_debug_init_uart();
63}
64
65#else
66#define s3c_pm_debug_init() do { } while(0)
67
Ben Dooks80057452010-01-20 12:29:25 +090068#endif /* CONFIG_SAMSUNG_PM_DEBUG */
Ben Dooks64197112008-12-12 00:24:06 +000069
Ben Dooks2261e0e2008-12-12 00:24:08 +000070/* Save the UART configurations if we are configured for debug. */
71
Ben Dooks4b637dc2008-12-12 00:24:24 +000072unsigned char pm_uart_udivslot;
73
Ben Dooks80057452010-01-20 12:29:25 +090074#ifdef CONFIG_SAMSUNG_PM_DEBUG
Ben Dooks2261e0e2008-12-12 00:24:08 +000075
Ben Dooksd2b07fe2008-12-12 00:24:20 +000076struct pm_uart_save uart_save[CONFIG_SERIAL_SAMSUNG_UARTS];
Ben Dooks2261e0e2008-12-12 00:24:08 +000077
Ben Dooksd2b07fe2008-12-12 00:24:20 +000078static void s3c_pm_save_uart(unsigned int uart, struct pm_uart_save *save)
Ben Dooks2261e0e2008-12-12 00:24:08 +000079{
Ben Dooksd2b07fe2008-12-12 00:24:20 +000080 void __iomem *regs = S3C_VA_UARTx(uart);
81
82 save->ulcon = __raw_readl(regs + S3C2410_ULCON);
83 save->ucon = __raw_readl(regs + S3C2410_UCON);
84 save->ufcon = __raw_readl(regs + S3C2410_UFCON);
85 save->umcon = __raw_readl(regs + S3C2410_UMCON);
86 save->ubrdiv = __raw_readl(regs + S3C2410_UBRDIV);
Ben Dooks57699e92008-12-12 00:24:23 +000087
Ben Dooks4b637dc2008-12-12 00:24:24 +000088 if (pm_uart_udivslot)
89 save->udivslot = __raw_readl(regs + S3C2443_DIVSLOT);
90
Ben Dooks57699e92008-12-12 00:24:23 +000091 S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n",
92 uart, save->ulcon, save->ucon, save->ufcon, save->ubrdiv);
Ben Dooks2261e0e2008-12-12 00:24:08 +000093}
94
Ben Dooksd2b07fe2008-12-12 00:24:20 +000095static void s3c_pm_save_uarts(void)
Ben Dooks2261e0e2008-12-12 00:24:08 +000096{
Ben Dooksd2b07fe2008-12-12 00:24:20 +000097 struct pm_uart_save *save = uart_save;
98 unsigned int uart;
99
100 for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
101 s3c_pm_save_uart(uart, save);
102}
103
104static void s3c_pm_restore_uart(unsigned int uart, struct pm_uart_save *save)
105{
106 void __iomem *regs = S3C_VA_UARTx(uart);
107
Ben Dooks57699e92008-12-12 00:24:23 +0000108 s3c_pm_arch_update_uart(regs, save);
109
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000110 __raw_writel(save->ulcon, regs + S3C2410_ULCON);
111 __raw_writel(save->ucon, regs + S3C2410_UCON);
112 __raw_writel(save->ufcon, regs + S3C2410_UFCON);
113 __raw_writel(save->umcon, regs + S3C2410_UMCON);
114 __raw_writel(save->ubrdiv, regs + S3C2410_UBRDIV);
Ben Dooks4b637dc2008-12-12 00:24:24 +0000115
116 if (pm_uart_udivslot)
117 __raw_writel(save->udivslot, regs + S3C2443_DIVSLOT);
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000118}
119
120static void s3c_pm_restore_uarts(void)
121{
122 struct pm_uart_save *save = uart_save;
123 unsigned int uart;
124
125 for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
126 s3c_pm_restore_uart(uart, save);
Ben Dooks2261e0e2008-12-12 00:24:08 +0000127}
128#else
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000129static void s3c_pm_save_uarts(void) { }
130static void s3c_pm_restore_uarts(void) { }
Ben Dooks2261e0e2008-12-12 00:24:08 +0000131#endif
132
Ben Dooks56b34422008-12-12 00:24:12 +0000133/* The IRQ ext-int code goes here, it is too small to currently bother
134 * with its own file. */
135
136unsigned long s3c_irqwake_intmask = 0xffffffffL;
137unsigned long s3c_irqwake_eintmask = 0xffffffffL;
138
Mark Brownf5aeffb2010-12-02 14:35:38 +0900139int s3c_irqext_wake(struct irq_data *data, unsigned int state)
Ben Dooks56b34422008-12-12 00:24:12 +0000140{
Mark Brownf5aeffb2010-12-02 14:35:38 +0900141 unsigned long bit = 1L << IRQ_EINT_BIT(data->irq);
Ben Dooks56b34422008-12-12 00:24:12 +0000142
143 if (!(s3c_irqwake_eintallow & bit))
144 return -ENOENT;
145
146 printk(KERN_INFO "wake %s for irq %d\n",
Mark Brownf5aeffb2010-12-02 14:35:38 +0900147 state ? "enabled" : "disabled", data->irq);
Ben Dooks56b34422008-12-12 00:24:12 +0000148
149 if (!state)
150 s3c_irqwake_eintmask |= bit;
151 else
152 s3c_irqwake_eintmask &= ~bit;
153
154 return 0;
155}
Ben Dooks64197112008-12-12 00:24:06 +0000156
157/* helper functions to save and restore register state */
158
159/**
160 * s3c_pm_do_save() - save a set of registers for restoration on resume.
161 * @ptr: Pointer to an array of registers.
162 * @count: Size of the ptr array.
163 *
164 * Run through the list of registers given, saving their contents in the
165 * array for later restoration when we wakeup.
166 */
167void s3c_pm_do_save(struct sleep_save *ptr, int count)
168{
169 for (; count > 0; count--, ptr++) {
170 ptr->val = __raw_readl(ptr->reg);
171 S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val);
172 }
173}
174
175/**
176 * s3c_pm_do_restore() - restore register values from the save list.
177 * @ptr: Pointer to an array of registers.
178 * @count: Size of the ptr array.
179 *
180 * Restore the register values saved from s3c_pm_do_save().
181 *
182 * Note, we do not use S3C_PMDBG() in here, as the system may not have
183 * restore the UARTs state yet
184*/
185
186void s3c_pm_do_restore(struct sleep_save *ptr, int count)
187{
188 for (; count > 0; count--, ptr++) {
189 printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
190 ptr->reg, ptr->val, __raw_readl(ptr->reg));
191
192 __raw_writel(ptr->val, ptr->reg);
193 }
194}
195
196/**
197 * s3c_pm_do_restore_core() - early restore register values from save list.
198 *
199 * This is similar to s3c_pm_do_restore() except we try and minimise the
200 * side effects of the function in case registers that hardware might need
201 * to work has been restored.
202 *
203 * WARNING: Do not put any debug in here that may effect memory or use
204 * peripherals, as things may be changing!
205*/
206
207void s3c_pm_do_restore_core(struct sleep_save *ptr, int count)
208{
209 for (; count > 0; count--, ptr++)
210 __raw_writel(ptr->val, ptr->reg);
211}
Ben Dooks2261e0e2008-12-12 00:24:08 +0000212
213/* s3c2410_pm_show_resume_irqs
214 *
215 * print any IRQs asserted at resume time (ie, we woke from)
216*/
Maurus Cuelenaerebaab7302011-04-02 10:50:22 +0900217static void __maybe_unused s3c_pm_show_resume_irqs(int start,
218 unsigned long which,
219 unsigned long mask)
Ben Dooks2261e0e2008-12-12 00:24:08 +0000220{
221 int i;
222
223 which &= ~mask;
224
225 for (i = 0; i <= 31; i++) {
226 if (which & (1L<<i)) {
227 S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
228 }
229 }
230}
231
232
233void (*pm_cpu_prep)(void);
234void (*pm_cpu_sleep)(void);
235
236#define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
237
238/* s3c_pm_enter
239 *
240 * central control for sleep/resume process
241*/
242
243static int s3c_pm_enter(suspend_state_t state)
244{
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
261 if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
262 !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
263 printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
264 printk(KERN_ERR "%s: Aborting sleep\n", __func__);
265 return -EINVAL;
266 }
267
Ben Dooks2261e0e2008-12-12 00:24:08 +0000268 /* save all necessary core registers not covered by the drivers */
269
270 s3c_pm_save_gpios();
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000271 s3c_pm_save_uarts();
Ben Dooks2261e0e2008-12-12 00:24:08 +0000272 s3c_pm_save_core();
273
274 /* set the irq configuration for wake */
275
276 s3c_pm_configure_extint();
277
278 S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
279 s3c_irqwake_intmask, s3c_irqwake_eintmask);
280
281 s3c_pm_arch_prepare_irqs();
282
283 /* call cpu specific preparation */
284
285 pm_cpu_prep();
286
287 /* flush cache back to ram */
288
289 flush_cache_all();
290
291 s3c_pm_check_store();
292
293 /* send the cpu to sleep... */
294
295 s3c_pm_arch_stop_clocks();
296
Ben Dooksfff94cd2009-03-10 11:48:07 +0000297 /* s3c_cpu_save will also act as our return point from when
298 * we resume as it saves its own register state and restores it
299 * during the resume. */
Ben Dooks2261e0e2008-12-12 00:24:08 +0000300
Russell King2e2f3d32011-02-06 17:39:31 +0000301 s3c_cpu_save(0, PLAT_PHYS_OFFSET - PAGE_OFFSET);
Ben Dooks2261e0e2008-12-12 00:24:08 +0000302
303 /* restore the cpu state using the kernel's cpu init code. */
304
305 cpu_init();
306
307 /* restore the system state */
308
309 s3c_pm_restore_core();
Ben Dooksd2b07fe2008-12-12 00:24:20 +0000310 s3c_pm_restore_uarts();
Ben Dooks2261e0e2008-12-12 00:24:08 +0000311 s3c_pm_restore_gpios();
312
313 s3c_pm_debug_init();
314
315 /* check what irq (if any) restored the system */
316
317 s3c_pm_arch_show_resume_irqs();
318
319 S3C_PMDBG("%s: post sleep, preparing to return\n", __func__);
320
Ben Dooksbd117bd2009-03-10 18:19:35 +0000321 /* LEDs should now be 1110 */
322 s3c_pm_debug_smdkled(1 << 1, 0);
323
Ben Dooks2261e0e2008-12-12 00:24:08 +0000324 s3c_pm_check_restore();
325
326 /* ok, let's return from sleep */
327
328 S3C_PMDBG("S3C PM Resume (post-restore)\n");
329 return 0;
330}
331
Ben Dooksaa8aba62008-12-12 00:24:34 +0000332static int s3c_pm_prepare(void)
333{
334 /* prepare check area if configured */
335
336 s3c_pm_check_prepare();
337 return 0;
338}
339
340static void s3c_pm_finish(void)
341{
342 s3c_pm_check_cleanup();
343}
344
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100345static const struct platform_suspend_ops s3c_pm_ops = {
Ben Dooks2261e0e2008-12-12 00:24:08 +0000346 .enter = s3c_pm_enter,
Ben Dooksaa8aba62008-12-12 00:24:34 +0000347 .prepare = s3c_pm_prepare,
348 .finish = s3c_pm_finish,
Ben Dooks2261e0e2008-12-12 00:24:08 +0000349 .valid = suspend_valid_only_mem,
350};
351
Ben Dooks4e59c252008-12-12 00:24:18 +0000352/* s3c_pm_init
Ben Dooks2261e0e2008-12-12 00:24:08 +0000353 *
354 * Attach the power management functions. This should be called
355 * from the board specific initialisation if the board supports
356 * it.
357*/
358
Ben Dooks4e59c252008-12-12 00:24:18 +0000359int __init s3c_pm_init(void)
Ben Dooks2261e0e2008-12-12 00:24:08 +0000360{
361 printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
362
363 suspend_set_ops(&s3c_pm_ops);
364 return 0;
365}