1 /* linux/arch/arm/plat-s3c/pm.c
3 * Copyright 2008 Openmoko, Inc.
4 * Copyright 2004-2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
6 * http://armlinux.simtec.co.uk/
8 * S3C common power management (suspend to ram) support.
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.
15 #include <linux/init.h>
16 #include <linux/suspend.h>
17 #include <linux/errno.h>
18 #include <linux/delay.h>
20 #include <linux/serial_core.h>
23 #include <asm/cacheflush.h>
24 #include <asm/suspend.h>
25 #include <mach/hardware.h>
28 #include <plat/regs-serial.h>
29 #include <mach/regs-clock.h>
30 #include <mach/regs-irq.h>
31 #include <mach/irqs.h>
35 #include <mach/pm-core.h>
37 /* for external use */
39 unsigned long s3c_pm_flags
;
43 * This code supports debug output to the low level UARTs for use on
44 * resume before the console layer is available.
47 #ifdef CONFIG_SAMSUNG_PM_DEBUG
48 extern void printascii(const char *);
50 void s3c_pm_dbg(const char *fmt
, ...)
56 vsnprintf(buff
, sizeof(buff
), fmt
, va
);
62 static inline void s3c_pm_debug_init(void)
64 /* restart uart clocks so we can use them to output */
65 s3c_pm_debug_init_uart();
69 #define s3c_pm_debug_init() do { } while(0)
71 #endif /* CONFIG_SAMSUNG_PM_DEBUG */
73 /* Save the UART configurations if we are configured for debug. */
75 unsigned char pm_uart_udivslot
;
77 #ifdef CONFIG_SAMSUNG_PM_DEBUG
79 static struct pm_uart_save uart_save
[CONFIG_SERIAL_SAMSUNG_UARTS
];
81 static void s3c_pm_save_uart(unsigned int uart
, struct pm_uart_save
*save
)
83 void __iomem
*regs
= S3C_VA_UARTx(uart
);
85 save
->ulcon
= __raw_readl(regs
+ S3C2410_ULCON
);
86 save
->ucon
= __raw_readl(regs
+ S3C2410_UCON
);
87 save
->ufcon
= __raw_readl(regs
+ S3C2410_UFCON
);
88 save
->umcon
= __raw_readl(regs
+ S3C2410_UMCON
);
89 save
->ubrdiv
= __raw_readl(regs
+ S3C2410_UBRDIV
);
92 save
->udivslot
= __raw_readl(regs
+ S3C2443_DIVSLOT
);
94 S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n",
95 uart
, save
->ulcon
, save
->ucon
, save
->ufcon
, save
->ubrdiv
);
98 static void s3c_pm_save_uarts(void)
100 struct pm_uart_save
*save
= uart_save
;
103 for (uart
= 0; uart
< CONFIG_SERIAL_SAMSUNG_UARTS
; uart
++, save
++)
104 s3c_pm_save_uart(uart
, save
);
107 static void s3c_pm_restore_uart(unsigned int uart
, struct pm_uart_save
*save
)
109 void __iomem
*regs
= S3C_VA_UARTx(uart
);
111 s3c_pm_arch_update_uart(regs
, save
);
113 __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
);
119 if (pm_uart_udivslot
)
120 __raw_writel(save
->udivslot
, regs
+ S3C2443_DIVSLOT
);
123 static void s3c_pm_restore_uarts(void)
125 struct pm_uart_save
*save
= uart_save
;
128 for (uart
= 0; uart
< CONFIG_SERIAL_SAMSUNG_UARTS
; uart
++, save
++)
129 s3c_pm_restore_uart(uart
, save
);
132 static void s3c_pm_save_uarts(void) { }
133 static void s3c_pm_restore_uarts(void) { }
136 /* The IRQ ext-int code goes here, it is too small to currently bother
137 * with its own file. */
139 unsigned long s3c_irqwake_intmask
= 0xffffffffL
;
140 unsigned long s3c_irqwake_eintmask
= 0xffffffffL
;
142 int s3c_irqext_wake(struct irq_data
*data
, unsigned int state
)
144 unsigned long bit
= 1L << IRQ_EINT_BIT(data
->irq
);
146 if (!(s3c_irqwake_eintallow
& bit
))
149 printk(KERN_INFO
"wake %s for irq %d\n",
150 state
? "enabled" : "disabled", data
->irq
);
153 s3c_irqwake_eintmask
|= bit
;
155 s3c_irqwake_eintmask
&= ~bit
;
160 /* helper functions to save and restore register state */
163 * s3c_pm_do_save() - save a set of registers for restoration on resume.
164 * @ptr: Pointer to an array of registers.
165 * @count: Size of the ptr array.
167 * Run through the list of registers given, saving their contents in the
168 * array for later restoration when we wakeup.
170 void s3c_pm_do_save(struct sleep_save
*ptr
, int count
)
172 for (; count
> 0; count
--, ptr
++) {
173 ptr
->val
= __raw_readl(ptr
->reg
);
174 S3C_PMDBG("saved %p value %08lx\n", ptr
->reg
, ptr
->val
);
179 * s3c_pm_do_restore() - restore register values from the save list.
180 * @ptr: Pointer to an array of registers.
181 * @count: Size of the ptr array.
183 * Restore the register values saved from s3c_pm_do_save().
185 * Note, we do not use S3C_PMDBG() in here, as the system may not have
186 * restore the UARTs state yet
189 void s3c_pm_do_restore(struct sleep_save
*ptr
, int count
)
191 for (; count
> 0; count
--, ptr
++) {
192 printk(KERN_DEBUG
"restore %p (restore %08lx, was %08x)\n",
193 ptr
->reg
, ptr
->val
, __raw_readl(ptr
->reg
));
195 __raw_writel(ptr
->val
, ptr
->reg
);
200 * s3c_pm_do_restore_core() - early restore register values from save list.
202 * This is similar to s3c_pm_do_restore() except we try and minimise the
203 * side effects of the function in case registers that hardware might need
204 * to work has been restored.
206 * WARNING: Do not put any debug in here that may effect memory or use
207 * peripherals, as things may be changing!
210 void s3c_pm_do_restore_core(struct sleep_save
*ptr
, int count
)
212 for (; count
> 0; count
--, ptr
++)
213 __raw_writel(ptr
->val
, ptr
->reg
);
216 /* s3c2410_pm_show_resume_irqs
218 * print any IRQs asserted at resume time (ie, we woke from)
220 static void __maybe_unused
s3c_pm_show_resume_irqs(int start
,
228 for (i
= 0; i
<= 31; i
++) {
229 if (which
& (1L<<i
)) {
230 S3C_PMDBG("IRQ %d asserted at resume\n", start
+i
);
236 void (*pm_cpu_prep
)(void);
237 int (*pm_cpu_sleep
)(unsigned long);
239 #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
243 * central control for sleep/resume process
246 static int s3c_pm_enter(suspend_state_t state
)
249 /* ensure the debug is initialised (if enabled) */
253 S3C_PMDBG("%s(%d)\n", __func__
, state
);
255 if (pm_cpu_prep
== NULL
|| pm_cpu_sleep
== NULL
) {
256 printk(KERN_ERR
"%s: error: no cpu sleep function\n", __func__
);
260 /* check if we have anything to wake-up with... bad things seem
261 * to happen if you suspend with no wakeup (system will often
262 * require a full power-cycle)
265 if (!of_have_populated_dt() &&
266 !any_allowed(s3c_irqwake_intmask
, s3c_irqwake_intallow
) &&
267 !any_allowed(s3c_irqwake_eintmask
, s3c_irqwake_eintallow
)) {
268 printk(KERN_ERR
"%s: No wake-up sources!\n", __func__
);
269 printk(KERN_ERR
"%s: Aborting sleep\n", __func__
);
273 /* save all necessary core registers not covered by the drivers */
275 if (!of_have_populated_dt()) {
276 samsung_pm_save_gpios();
277 samsung_pm_saved_gpios();
283 /* set the irq configuration for wake */
285 s3c_pm_configure_extint();
287 S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
288 s3c_irqwake_intmask
, s3c_irqwake_eintmask
);
290 s3c_pm_arch_prepare_irqs();
292 /* call cpu specific preparation */
296 /* flush cache back to ram */
300 s3c_pm_check_store();
302 /* send the cpu to sleep... */
304 s3c_pm_arch_stop_clocks();
306 /* this will also act as our return point from when
307 * we resume as it saves its own register state and restores it
308 * during the resume. */
310 ret
= cpu_suspend(0, pm_cpu_sleep
);
314 /* restore the system state */
316 s3c_pm_restore_core();
317 s3c_pm_restore_uarts();
319 if (!of_have_populated_dt()) {
320 samsung_pm_restore_gpios();
321 s3c_pm_restored_gpios();
326 /* check what irq (if any) restored the system */
328 s3c_pm_arch_show_resume_irqs();
330 S3C_PMDBG("%s: post sleep, preparing to return\n", __func__
);
332 /* LEDs should now be 1110 */
333 s3c_pm_debug_smdkled(1 << 1, 0);
335 s3c_pm_check_restore();
337 /* ok, let's return from sleep */
339 S3C_PMDBG("S3C PM Resume (post-restore)\n");
343 static int s3c_pm_prepare(void)
345 /* prepare check area if configured */
347 s3c_pm_check_prepare();
351 static void s3c_pm_finish(void)
353 s3c_pm_check_cleanup();
356 static const struct platform_suspend_ops s3c_pm_ops
= {
357 .enter
= s3c_pm_enter
,
358 .prepare
= s3c_pm_prepare
,
359 .finish
= s3c_pm_finish
,
360 .valid
= suspend_valid_only_mem
,
365 * Attach the power management functions. This should be called
366 * from the board specific initialisation if the board supports
370 int __init
s3c_pm_init(void)
372 printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
374 suspend_set_ops(&s3c_pm_ops
);