1 /* linux/arch/arm/plat-s3c/pm.c
3 * Copyright 2008 Openmoko, Inc.
4 * Copyright 2004,2006,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>
19 #include <linux/serial_core.h>
22 #include <asm/cacheflush.h>
23 #include <mach/hardware.h>
25 #include <plat/regs-serial.h>
26 #include <mach/regs-clock.h>
27 #include <mach/regs-gpio.h>
28 #include <mach/regs-mem.h>
29 #include <mach/regs-irq.h>
33 #include <plat/pm-core.h>
35 /* for external use */
37 unsigned long s3c_pm_flags;
41 * This code supports debug output to the low level UARTs for use on
42 * resume before the console layer is available.
45 #ifdef CONFIG_S3C2410_PM_DEBUG
46 extern void printascii(const char *);
48 void s3c_pm_dbg(const char *fmt, ...)
54 vsprintf(buff, fmt, va);
60 static inline void s3c_pm_debug_init(void)
62 /* restart uart clocks so we can use them to output */
63 s3c_pm_debug_init_uart();
67 #define s3c_pm_debug_init() do { } while(0)
69 #endif /* CONFIG_S3C2410_PM_DEBUG */
71 /* Save the UART configurations if we are configured for debug. */
73 #ifdef CONFIG_S3C2410_PM_DEBUG
75 #define SAVE_UART(va) \
76 SAVE_ITEM((va) + S3C2410_ULCON), \
77 SAVE_ITEM((va) + S3C2410_UCON), \
78 SAVE_ITEM((va) + S3C2410_UFCON), \
79 SAVE_ITEM((va) + S3C2410_UMCON), \
80 SAVE_ITEM((va) + S3C2410_UBRDIV)
82 static struct sleep_save uart_save[] = {
83 SAVE_UART(S3C_VA_UART0),
84 SAVE_UART(S3C_VA_UART1),
85 #ifndef CONFIG_CPU_S3C2400
86 SAVE_UART(S3C_VA_UART2),
90 static void s3c_pm_save_uart(void)
92 s3c_pm_do_save(uart_save, ARRAY_SIZE(uart_save));
95 static void s3c_pm_restore_uart(void)
97 s3c_pm_do_restore(uart_save, ARRAY_SIZE(uart_save));
100 static void s3c_pm_save_uart(void) { }
101 static void s3c_pm_restore_uart(void) { }
104 /* The IRQ ext-int code goes here, it is too small to currently bother
105 * with its own file. */
107 unsigned long s3c_irqwake_intmask = 0xffffffffL;
108 unsigned long s3c_irqwake_eintmask = 0xffffffffL;
110 int s3c_irqext_wake(unsigned int irqno, unsigned int state)
112 unsigned long bit = 1L << IRQ_EINT_BIT(irqno);
114 if (!(s3c_irqwake_eintallow & bit))
117 printk(KERN_INFO "wake %s for irq %d\n",
118 state ? "enabled" : "disabled", irqno);
121 s3c_irqwake_eintmask |= bit;
123 s3c_irqwake_eintmask &= ~bit;
128 /* helper functions to save and restore register state */
131 * s3c_pm_do_save() - save a set of registers for restoration on resume.
132 * @ptr: Pointer to an array of registers.
133 * @count: Size of the ptr array.
135 * Run through the list of registers given, saving their contents in the
136 * array for later restoration when we wakeup.
138 void s3c_pm_do_save(struct sleep_save *ptr, int count)
140 for (; count > 0; count--, ptr++) {
141 ptr->val = __raw_readl(ptr->reg);
142 S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val);
147 * s3c_pm_do_restore() - restore register values from the save list.
148 * @ptr: Pointer to an array of registers.
149 * @count: Size of the ptr array.
151 * Restore the register values saved from s3c_pm_do_save().
153 * Note, we do not use S3C_PMDBG() in here, as the system may not have
154 * restore the UARTs state yet
157 void s3c_pm_do_restore(struct sleep_save *ptr, int count)
159 for (; count > 0; count--, ptr++) {
160 printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
161 ptr->reg, ptr->val, __raw_readl(ptr->reg));
163 __raw_writel(ptr->val, ptr->reg);
168 * s3c_pm_do_restore_core() - early restore register values from save list.
170 * This is similar to s3c_pm_do_restore() except we try and minimise the
171 * side effects of the function in case registers that hardware might need
172 * to work has been restored.
174 * WARNING: Do not put any debug in here that may effect memory or use
175 * peripherals, as things may be changing!
178 void s3c_pm_do_restore_core(struct sleep_save *ptr, int count)
180 for (; count > 0; count--, ptr++)
181 __raw_writel(ptr->val, ptr->reg);
184 /* s3c2410_pm_show_resume_irqs
186 * print any IRQs asserted at resume time (ie, we woke from)
188 static void s3c_pm_show_resume_irqs(int start, unsigned long which,
195 for (i = 0; i <= 31; i++) {
196 if (which & (1L<<i)) {
197 S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
203 void (*pm_cpu_prep)(void);
204 void (*pm_cpu_sleep)(void);
206 #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
210 * central control for sleep/resume process
213 static int s3c_pm_enter(suspend_state_t state)
215 unsigned long regs_save[16];
217 /* ensure the debug is initialised (if enabled) */
221 S3C_PMDBG("%s(%d)\n", __func__, state);
223 if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
224 printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__);
228 /* check if we have anything to wake-up with... bad things seem
229 * to happen if you suspend with no wakeup (system will often
230 * require a full power-cycle)
233 if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
234 !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
235 printk(KERN_ERR "%s: No wake-up sources!\n", __func__);
236 printk(KERN_ERR "%s: Aborting sleep\n", __func__);
240 /* prepare check area if configured */
242 s3c_pm_check_prepare();
244 /* store the physical address of the register recovery block */
246 s3c_sleep_save_phys = virt_to_phys(regs_save);
248 S3C_PMDBG("s3c_sleep_save_phys=0x%08lx\n", s3c_sleep_save_phys);
250 /* save all necessary core registers not covered by the drivers */
256 /* set the irq configuration for wake */
258 s3c_pm_configure_extint();
260 S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
261 s3c_irqwake_intmask, s3c_irqwake_eintmask);
263 s3c_pm_arch_prepare_irqs();
265 /* call cpu specific preparation */
269 /* flush cache back to ram */
273 s3c_pm_check_store();
275 /* send the cpu to sleep... */
277 s3c_pm_arch_stop_clocks();
279 /* s3c2410_cpu_save will also act as our return point from when
280 * we resume as it saves its own register state, so use the return
281 * code to differentiate return from save and return from sleep */
283 if (s3c2410_cpu_save(regs_save) == 0) {
288 /* restore the cpu state using the kernel's cpu init code. */
292 /* restore the system state */
294 s3c_pm_restore_core();
295 s3c_pm_restore_uart();
296 s3c_pm_restore_gpios();
300 /* check what irq (if any) restored the system */
302 s3c_pm_arch_show_resume_irqs();
304 S3C_PMDBG("%s: post sleep, preparing to return\n", __func__);
306 s3c_pm_check_restore();
308 /* ok, let's return from sleep */
310 S3C_PMDBG("S3C PM Resume (post-restore)\n");
314 static struct platform_suspend_ops s3c_pm_ops = {
315 .enter = s3c_pm_enter,
316 .valid = suspend_valid_only_mem,
321 * Attach the power management functions. This should be called
322 * from the board specific initialisation if the board supports
326 int __init s3c2410_pm_init(void)
328 printk("S3C Power Management, Copyright 2004 Simtec Electronics\n");
330 suspend_set_ops(&s3c_pm_ops);