]> rtime.felk.cvut.cz Git - linux-imx.git/blob - arch/arm/plat-samsung/pm.c
KVM: s390: fix pfmf non-quiescing control handling
[linux-imx.git] / arch / arm / plat-samsung / pm.c
1 /* linux/arch/arm/plat-s3c/pm.c
2  *
3  * Copyright 2008 Openmoko, Inc.
4  * Copyright 2004-2008 Simtec Electronics
5  *      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>
18 #include <linux/delay.h>
19 #include <linux/of.h>
20 #include <linux/serial_core.h>
21 #include <linux/io.h>
22
23 #include <asm/cacheflush.h>
24 #include <asm/suspend.h>
25
26 #include <plat/regs-serial.h>
27
28 #ifdef CONFIG_SAMSUNG_ATAGS
29 #include <mach/hardware.h>
30 #include <mach/map.h>
31 #include <mach/regs-clock.h>
32 #include <mach/regs-irq.h>
33 #include <mach/irqs.h>
34 #endif
35
36 #include <asm/irq.h>
37
38 #include <plat/pm.h>
39 #include <mach/pm-core.h>
40
41 /* for external use */
42
43 unsigned long s3c_pm_flags;
44
45 /* 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
51 #ifdef CONFIG_SAMSUNG_PM_DEBUG
52 extern void printascii(const char *);
53
54 void s3c_pm_dbg(const char *fmt, ...)
55 {
56         va_list va;
57         char buff[256];
58
59         va_start(va, fmt);
60         vsnprintf(buff, sizeof(buff), fmt, va);
61         va_end(va);
62
63         printascii(buff);
64 }
65
66 static 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
75 #endif /* CONFIG_SAMSUNG_PM_DEBUG */
76
77 /* Save the UART configurations if we are configured for debug. */
78
79 unsigned char pm_uart_udivslot;
80
81 #ifdef CONFIG_SAMSUNG_PM_DEBUG
82
83 static struct pm_uart_save uart_save[CONFIG_SERIAL_SAMSUNG_UARTS];
84
85 static void s3c_pm_save_uart(unsigned int uart, struct pm_uart_save *save)
86 {
87         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);
94
95         if (pm_uart_udivslot)
96                 save->udivslot = __raw_readl(regs + S3C2443_DIVSLOT);
97
98         S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n",
99                   uart, save->ulcon, save->ucon, save->ufcon, save->ubrdiv);
100 }
101
102 static void s3c_pm_save_uarts(void)
103 {
104         struct pm_uart_save *save = uart_save;
105         unsigned int uart;
106
107         for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
108                 s3c_pm_save_uart(uart, save);
109 }
110
111 static void s3c_pm_restore_uart(unsigned int uart, struct pm_uart_save *save)
112 {
113         void __iomem *regs = S3C_VA_UARTx(uart);
114
115         s3c_pm_arch_update_uart(regs, save);
116
117         __raw_writel(save->ulcon, regs + S3C2410_ULCON);
118         __raw_writel(save->ucon,  regs + S3C2410_UCON);
119         __raw_writel(save->ufcon, regs + S3C2410_UFCON);
120         __raw_writel(save->umcon, regs + S3C2410_UMCON);
121         __raw_writel(save->ubrdiv, regs + S3C2410_UBRDIV);
122
123         if (pm_uart_udivslot)
124                 __raw_writel(save->udivslot, regs + S3C2443_DIVSLOT);
125 }
126
127 static void s3c_pm_restore_uarts(void)
128 {
129         struct pm_uart_save *save = uart_save;
130         unsigned int uart;
131
132         for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++)
133                 s3c_pm_restore_uart(uart, save);
134 }
135 #else
136 static void s3c_pm_save_uarts(void) { }
137 static void s3c_pm_restore_uarts(void) { }
138 #endif
139
140 /* The IRQ ext-int code goes here, it is too small to currently bother
141  * with its own file. */
142
143 unsigned long s3c_irqwake_intmask       = 0xffffffffL;
144 unsigned long s3c_irqwake_eintmask      = 0xffffffffL;
145
146 int s3c_irqext_wake(struct irq_data *data, unsigned int state)
147 {
148         unsigned long bit = 1L << IRQ_EINT_BIT(data->irq);
149
150         if (!(s3c_irqwake_eintallow & bit))
151                 return -ENOENT;
152
153         printk(KERN_INFO "wake %s for irq %d\n",
154                state ? "enabled" : "disabled", data->irq);
155
156         if (!state)
157                 s3c_irqwake_eintmask |= bit;
158         else
159                 s3c_irqwake_eintmask &= ~bit;
160
161         return 0;
162 }
163
164 /* helper functions to save and restore register state */
165
166 /**
167  * s3c_pm_do_save() - save a set of registers for restoration on resume.
168  * @ptr: Pointer to an array of registers.
169  * @count: Size of the ptr array.
170  *
171  * Run through the list of registers given, saving their contents in the
172  * array for later restoration when we wakeup.
173  */
174 void s3c_pm_do_save(struct sleep_save *ptr, int count)
175 {
176         for (; count > 0; count--, ptr++) {
177                 ptr->val = __raw_readl(ptr->reg);
178                 S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val);
179         }
180 }
181
182 /**
183  * s3c_pm_do_restore() - restore register values from the save list.
184  * @ptr: Pointer to an array of registers.
185  * @count: Size of the ptr array.
186  *
187  * Restore the register values saved from s3c_pm_do_save().
188  *
189  * Note, we do not use S3C_PMDBG() in here, as the system may not have
190  * restore the UARTs state yet
191 */
192
193 void s3c_pm_do_restore(struct sleep_save *ptr, int count)
194 {
195         for (; count > 0; count--, ptr++) {
196                 printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
197                        ptr->reg, ptr->val, __raw_readl(ptr->reg));
198
199                 __raw_writel(ptr->val, ptr->reg);
200         }
201 }
202
203 /**
204  * s3c_pm_do_restore_core() - early restore register values from save list.
205  *
206  * This is similar to s3c_pm_do_restore() except we try and minimise the
207  * side effects of the function in case registers that hardware might need
208  * to work has been restored.
209  *
210  * WARNING: Do not put any debug in here that may effect memory or use
211  * peripherals, as things may be changing!
212 */
213
214 void s3c_pm_do_restore_core(struct sleep_save *ptr, int count)
215 {
216         for (; count > 0; count--, ptr++)
217                 __raw_writel(ptr->val, ptr->reg);
218 }
219
220 /* s3c2410_pm_show_resume_irqs
221  *
222  * print any IRQs asserted at resume time (ie, we woke from)
223 */
224 static void __maybe_unused s3c_pm_show_resume_irqs(int start,
225                                                    unsigned long which,
226                                                    unsigned long mask)
227 {
228         int i;
229
230         which &= ~mask;
231
232         for (i = 0; i <= 31; i++) {
233                 if (which & (1L<<i)) {
234                         S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
235                 }
236         }
237 }
238
239
240 void (*pm_cpu_prep)(void);
241 int (*pm_cpu_sleep)(unsigned long);
242
243 #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
244
245 /* s3c_pm_enter
246  *
247  * central control for sleep/resume process
248 */
249
250 static int s3c_pm_enter(suspend_state_t state)
251 {
252         int ret;
253         /* ensure the debug is initialised (if enabled) */
254
255         s3c_pm_debug_init();
256
257         S3C_PMDBG("%s(%d)\n", __func__, state);
258
259         if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
260                 printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__);
261                 return -EINVAL;
262         }
263
264         /* check if we have anything to wake-up with... bad things seem
265          * to happen if you suspend with no wakeup (system will often
266          * require a full power-cycle)
267         */
268
269         if (!of_have_populated_dt() &&
270             !any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
271             !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
272                 printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
273                 printk(KERN_ERR "%s: Aborting sleep\n", __func__);
274                 return -EINVAL;
275         }
276
277         /* save all necessary core registers not covered by the drivers */
278
279         if (!of_have_populated_dt()) {
280                 samsung_pm_save_gpios();
281                 samsung_pm_saved_gpios();
282         }
283
284         s3c_pm_save_uarts();
285         s3c_pm_save_core();
286
287         /* set the irq configuration for wake */
288
289         s3c_pm_configure_extint();
290
291         S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
292             s3c_irqwake_intmask, s3c_irqwake_eintmask);
293
294         s3c_pm_arch_prepare_irqs();
295
296         /* call cpu specific preparation */
297
298         pm_cpu_prep();
299
300         /* flush cache back to ram */
301
302         flush_cache_all();
303
304         s3c_pm_check_store();
305
306         /* send the cpu to sleep... */
307
308         s3c_pm_arch_stop_clocks();
309
310         /* this will also act as our return point from when
311          * we resume as it saves its own register state and restores it
312          * during the resume.  */
313
314         ret = cpu_suspend(0, pm_cpu_sleep);
315         if (ret)
316                 return ret;
317
318         /* restore the system state */
319
320         s3c_pm_restore_core();
321         s3c_pm_restore_uarts();
322
323         if (!of_have_populated_dt()) {
324                 samsung_pm_restore_gpios();
325                 s3c_pm_restored_gpios();
326         }
327
328         s3c_pm_debug_init();
329
330         /* check what irq (if any) restored the system */
331
332         s3c_pm_arch_show_resume_irqs();
333
334         S3C_PMDBG("%s: post sleep, preparing to return\n", __func__);
335
336         /* LEDs should now be 1110 */
337         s3c_pm_debug_smdkled(1 << 1, 0);
338
339         s3c_pm_check_restore();
340
341         /* ok, let's return from sleep */
342
343         S3C_PMDBG("S3C PM Resume (post-restore)\n");
344         return 0;
345 }
346
347 static int s3c_pm_prepare(void)
348 {
349         /* prepare check area if configured */
350
351         s3c_pm_check_prepare();
352         return 0;
353 }
354
355 static void s3c_pm_finish(void)
356 {
357         s3c_pm_check_cleanup();
358 }
359
360 static const struct platform_suspend_ops s3c_pm_ops = {
361         .enter          = s3c_pm_enter,
362         .prepare        = s3c_pm_prepare,
363         .finish         = s3c_pm_finish,
364         .valid          = suspend_valid_only_mem,
365 };
366
367 /* s3c_pm_init
368  *
369  * Attach the power management functions. This should be called
370  * from the board specific initialisation if the board supports
371  * it.
372 */
373
374 int __init s3c_pm_init(void)
375 {
376         printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
377
378         suspend_set_ops(&s3c_pm_ops);
379         return 0;
380 }