Commit | Line | Data |
---|---|---|
4490e3c6 KK |
1 | // SPDX-License-Identifier: GPL-2.0 |
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. | |
6419711a BD |
9 | |
10 | #include <linux/init.h> | |
11 | #include <linux/suspend.h> | |
12 | #include <linux/errno.h> | |
2261e0e6 | 13 | #include <linux/delay.h> |
cd3fc1b9 | 14 | #include <linux/of.h> |
334a1c70 | 15 | #include <linux/serial_s3c.h> |
6419711a BD |
16 | #include <linux/io.h> |
17 | ||
2261e0e6 | 18 | #include <asm/cacheflush.h> |
2c74a0ce | 19 | #include <asm/suspend.h> |
2261e0e6 | 20 | |
c6ff132d AB |
21 | #include "map.h" |
22 | #include "regs-clock.h" | |
23 | #include "regs-irq.h" | |
c78a41fc | 24 | #include "irqs.h" |
d6280ffb | 25 | |
56b34426 | 26 | #include <asm/irq.h> |
2261e0e6 | 27 | |
c6ff132d AB |
28 | #include "cpu.h" |
29 | #include "pm.h" | |
30 | #include "pm-core.h" | |
6419711a BD |
31 | |
32 | /* for external use */ | |
33 | ||
34 | unsigned long s3c_pm_flags; | |
35 | ||
56b34426 BD |
36 | /* The IRQ ext-int code goes here, it is too small to currently bother |
37 | * with its own file. */ | |
38 | ||
39 | unsigned long s3c_irqwake_intmask = 0xffffffffL; | |
40 | unsigned long s3c_irqwake_eintmask = 0xffffffffL; | |
41 | ||
f5aeffb7 | 42 | int s3c_irqext_wake(struct irq_data *data, unsigned int state) |
56b34426 | 43 | { |
f5aeffb7 | 44 | unsigned long bit = 1L << IRQ_EINT_BIT(data->irq); |
56b34426 BD |
45 | |
46 | if (!(s3c_irqwake_eintallow & bit)) | |
47 | return -ENOENT; | |
48 | ||
49 | printk(KERN_INFO "wake %s for irq %d\n", | |
f5aeffb7 | 50 | state ? "enabled" : "disabled", data->irq); |
56b34426 BD |
51 | |
52 | if (!state) | |
53 | s3c_irqwake_eintmask |= bit; | |
54 | else | |
55 | s3c_irqwake_eintmask &= ~bit; | |
56 | ||
57 | return 0; | |
58 | } | |
6419711a | 59 | |
2261e0e6 | 60 | void (*pm_cpu_prep)(void); |
29cb3cd2 | 61 | int (*pm_cpu_sleep)(unsigned long); |
2261e0e6 BD |
62 | |
63 | #define any_allowed(mask, allow) (((mask) & (allow)) != (allow)) | |
64 | ||
65 | /* s3c_pm_enter | |
66 | * | |
67 | * central control for sleep/resume process | |
68 | */ | |
69 | ||
70 | static int s3c_pm_enter(suspend_state_t state) | |
71 | { | |
d3fcacf5 | 72 | int ret; |
2261e0e6 | 73 | /* ensure the debug is initialised (if enabled) */ |
1e574a66 | 74 | s3c_pm_debug_init_uart(); |
2261e0e6 BD |
75 | |
76 | S3C_PMDBG("%s(%d)\n", __func__, state); | |
77 | ||
78 | if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) { | |
79 | printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__); | |
80 | return -EINVAL; | |
81 | } | |
82 | ||
83 | /* check if we have anything to wake-up with... bad things seem | |
84 | * to happen if you suspend with no wakeup (system will often | |
85 | * require a full power-cycle) | |
86 | */ | |
87 | ||
cd3fc1b9 TF |
88 | if (!of_have_populated_dt() && |
89 | !any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) && | |
2261e0e6 BD |
90 | !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) { |
91 | printk(KERN_ERR "%s: No wake-up sources!\n", __func__); | |
92 | printk(KERN_ERR "%s: Aborting sleep\n", __func__); | |
93 | return -EINVAL; | |
94 | } | |
95 | ||
2261e0e6 BD |
96 | /* save all necessary core registers not covered by the drivers */ |
97 | ||
cd3fc1b9 TF |
98 | if (!of_have_populated_dt()) { |
99 | samsung_pm_save_gpios(); | |
100 | samsung_pm_saved_gpios(); | |
101 | } | |
102 | ||
0d297df0 | 103 | s3c_pm_save_uarts(false); |
2261e0e6 BD |
104 | s3c_pm_save_core(); |
105 | ||
106 | /* set the irq configuration for wake */ | |
107 | ||
108 | s3c_pm_configure_extint(); | |
109 | ||
110 | S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n", | |
111 | s3c_irqwake_intmask, s3c_irqwake_eintmask); | |
112 | ||
113 | s3c_pm_arch_prepare_irqs(); | |
114 | ||
115 | /* call cpu specific preparation */ | |
116 | ||
117 | pm_cpu_prep(); | |
118 | ||
119 | /* flush cache back to ram */ | |
120 | ||
121 | flush_cache_all(); | |
122 | ||
123 | s3c_pm_check_store(); | |
124 | ||
125 | /* send the cpu to sleep... */ | |
126 | ||
127 | s3c_pm_arch_stop_clocks(); | |
128 | ||
e7089da9 | 129 | /* this will also act as our return point from when |
fff94cd9 BD |
130 | * we resume as it saves its own register state and restores it |
131 | * during the resume. */ | |
2261e0e6 | 132 | |
d3fcacf5 AK |
133 | ret = cpu_suspend(0, pm_cpu_sleep); |
134 | if (ret) | |
135 | return ret; | |
2261e0e6 | 136 | |
2261e0e6 BD |
137 | /* restore the system state */ |
138 | ||
139 | s3c_pm_restore_core(); | |
0d297df0 | 140 | s3c_pm_restore_uarts(false); |
cd3fc1b9 TF |
141 | |
142 | if (!of_have_populated_dt()) { | |
143 | samsung_pm_restore_gpios(); | |
144 | s3c_pm_restored_gpios(); | |
145 | } | |
2261e0e6 | 146 | |
1e574a66 | 147 | s3c_pm_debug_init_uart(); |
2261e0e6 BD |
148 | |
149 | /* check what irq (if any) restored the system */ | |
150 | ||
151 | s3c_pm_arch_show_resume_irqs(); | |
152 | ||
153 | S3C_PMDBG("%s: post sleep, preparing to return\n", __func__); | |
154 | ||
155 | s3c_pm_check_restore(); | |
156 | ||
157 | /* ok, let's return from sleep */ | |
158 | ||
159 | S3C_PMDBG("S3C PM Resume (post-restore)\n"); | |
160 | return 0; | |
161 | } | |
162 | ||
aa8aba69 BD |
163 | static int s3c_pm_prepare(void) |
164 | { | |
165 | /* prepare check area if configured */ | |
166 | ||
167 | s3c_pm_check_prepare(); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | static void s3c_pm_finish(void) | |
172 | { | |
173 | s3c_pm_check_cleanup(); | |
174 | } | |
175 | ||
2f55ac07 | 176 | static const struct platform_suspend_ops s3c_pm_ops = { |
2261e0e6 | 177 | .enter = s3c_pm_enter, |
aa8aba69 BD |
178 | .prepare = s3c_pm_prepare, |
179 | .finish = s3c_pm_finish, | |
2261e0e6 BD |
180 | .valid = suspend_valid_only_mem, |
181 | }; | |
182 | ||
4e59c25d | 183 | /* s3c_pm_init |
2261e0e6 BD |
184 | * |
185 | * Attach the power management functions. This should be called | |
186 | * from the board specific initialisation if the board supports | |
187 | * it. | |
188 | */ | |
189 | ||
4e59c25d | 190 | int __init s3c_pm_init(void) |
2261e0e6 BD |
191 | { |
192 | printk("S3C Power Management, Copyright 2004 Simtec Electronics\n"); | |
193 | ||
194 | suspend_set_ops(&s3c_pm_ops); | |
195 | return 0; | |
196 | } |