Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * FR-V Power Management Routines | |
3 | * | |
4 | * Copyright (c) 2004 Red Hat, Inc. | |
5 | * | |
6 | * Based on SA1100 version: | |
7 | * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <linux/config.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/pm.h> | |
bca73e4b | 17 | #include <linux/pm_legacy.h> |
1da177e4 LT |
18 | #include <linux/sched.h> |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/sysctl.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/delay.h> | |
23 | #include <asm/uaccess.h> | |
24 | ||
25 | #include <asm/mb86943a.h> | |
26 | ||
27 | #include "local.h" | |
28 | ||
29 | void (*pm_power_off)(void); | |
30 | ||
31 | extern void frv_change_cmode(int); | |
32 | ||
33 | /* | |
34 | * Debug macros | |
35 | */ | |
36 | #define DEBUG | |
37 | ||
38 | int pm_do_suspend(void) | |
39 | { | |
40 | local_irq_disable(); | |
41 | ||
42 | __set_LEDS(0xb1); | |
43 | ||
44 | /* go zzz */ | |
45 | frv_cpu_suspend(pdm_suspend_mode); | |
46 | ||
47 | __set_LEDS(0xb2); | |
48 | ||
49 | local_irq_enable(); | |
50 | ||
51 | return 0; | |
52 | } | |
53 | ||
54 | static unsigned long __irq_mask; | |
55 | ||
56 | /* | |
57 | * Setup interrupt masks, etc to enable wakeup by power switch | |
58 | */ | |
59 | static void __default_power_switch_setup(void) | |
60 | { | |
61 | /* default is to mask all interrupt sources. */ | |
62 | __irq_mask = *(unsigned long *)0xfeff9820; | |
63 | *(unsigned long *)0xfeff9820 = 0xfffe0000; | |
64 | } | |
65 | ||
66 | /* | |
67 | * Cleanup interrupt masks, etc after wakeup by power switch | |
68 | */ | |
69 | static void __default_power_switch_cleanup(void) | |
70 | { | |
71 | *(unsigned long *)0xfeff9820 = __irq_mask; | |
72 | } | |
73 | ||
74 | /* | |
75 | * Return non-zero if wakeup irq was caused by power switch | |
76 | */ | |
77 | static int __default_power_switch_check(void) | |
78 | { | |
79 | return 1; | |
80 | } | |
81 | ||
82 | void (*__power_switch_wake_setup)(void) = __default_power_switch_setup; | |
83 | int (*__power_switch_wake_check)(void) = __default_power_switch_check; | |
84 | void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup; | |
85 | ||
86 | int pm_do_bus_sleep(void) | |
87 | { | |
88 | local_irq_disable(); | |
89 | ||
90 | /* | |
91 | * Here is where we need some platform-dependent setup | |
92 | * of the interrupt state so that appropriate wakeup | |
93 | * sources are allowed and all others are masked. | |
94 | */ | |
95 | __power_switch_wake_setup(); | |
96 | ||
97 | __set_LEDS(0xa1); | |
98 | ||
99 | /* go zzz | |
100 | * | |
101 | * This is in a loop in case power switch shares an irq with other | |
102 | * devices. The wake_check() tells us if we need to finish waking | |
103 | * or go back to sleep. | |
104 | */ | |
105 | do { | |
106 | frv_cpu_suspend(HSR0_PDM_BUS_SLEEP); | |
107 | } while (__power_switch_wake_check && !__power_switch_wake_check()); | |
108 | ||
109 | __set_LEDS(0xa2); | |
110 | ||
111 | /* | |
112 | * Here is where we need some platform-dependent restore | |
113 | * of the interrupt state prior to being called. | |
114 | */ | |
115 | __power_switch_wake_cleanup(); | |
116 | ||
117 | local_irq_enable(); | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
122 | unsigned long sleep_phys_sp(void *sp) | |
123 | { | |
124 | return virt_to_phys(sp); | |
125 | } | |
126 | ||
127 | #ifdef CONFIG_SYSCTL | |
128 | /* | |
129 | * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6 | |
130 | * when all the PM interfaces exist nicely. | |
131 | */ | |
132 | #define CTL_PM 9899 | |
133 | #define CTL_PM_SUSPEND 1 | |
134 | #define CTL_PM_CMODE 2 | |
135 | #define CTL_PM_P0 4 | |
136 | #define CTL_PM_CM 5 | |
137 | ||
138 | static int user_atoi(char *ubuf, size_t len) | |
139 | { | |
140 | char buf[16]; | |
141 | unsigned long ret; | |
142 | ||
143 | if (len > 15) | |
144 | return -EINVAL; | |
145 | ||
146 | if (copy_from_user(buf, ubuf, len)) | |
147 | return -EFAULT; | |
148 | ||
149 | buf[len] = 0; | |
150 | ret = simple_strtoul(buf, NULL, 0); | |
151 | if (ret > INT_MAX) | |
152 | return -ERANGE; | |
153 | return ret; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Send us to sleep. | |
158 | */ | |
159 | static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp, | |
160 | void *buffer, size_t *lenp, loff_t *fpos) | |
161 | { | |
162 | int retval, mode; | |
163 | ||
164 | if (*lenp <= 0) | |
165 | return -EIO; | |
166 | ||
167 | mode = user_atoi(buffer, *lenp); | |
168 | if ((mode != 1) && (mode != 5)) | |
169 | return -EINVAL; | |
170 | ||
171 | retval = pm_send_all(PM_SUSPEND, (void *)3); | |
172 | ||
173 | if (retval == 0) { | |
174 | if (mode == 5) | |
175 | retval = pm_do_bus_sleep(); | |
176 | else | |
177 | retval = pm_do_suspend(); | |
178 | pm_send_all(PM_RESUME, (void *)0); | |
179 | } | |
180 | ||
181 | return retval; | |
182 | } | |
183 | ||
184 | static int try_set_cmode(int new_cmode) | |
185 | { | |
186 | if (new_cmode > 15) | |
187 | return -EINVAL; | |
188 | if (!(clock_cmodes_permitted & (1<<new_cmode))) | |
189 | return -EINVAL; | |
190 | ||
191 | /* tell all the drivers we're suspending */ | |
192 | pm_send_all(PM_SUSPEND, (void *)3); | |
193 | ||
194 | /* now change cmode */ | |
195 | local_irq_disable(); | |
196 | frv_dma_pause_all(); | |
197 | ||
198 | frv_change_cmode(new_cmode); | |
199 | ||
200 | determine_clocks(0); | |
201 | time_divisor_init(); | |
202 | ||
203 | #ifdef DEBUG | |
204 | determine_clocks(1); | |
205 | #endif | |
206 | frv_dma_resume_all(); | |
207 | local_irq_enable(); | |
208 | ||
209 | /* tell all the drivers we're resuming */ | |
210 | pm_send_all(PM_RESUME, (void *)0); | |
211 | return 0; | |
212 | } | |
213 | ||
214 | ||
215 | static int cmode_procctl(ctl_table *ctl, int write, struct file *filp, | |
216 | void *buffer, size_t *lenp, loff_t *fpos) | |
217 | { | |
218 | int new_cmode; | |
219 | ||
220 | if (!write) | |
221 | return proc_dointvec(ctl, write, filp, buffer, lenp, fpos); | |
222 | ||
223 | new_cmode = user_atoi(buffer, *lenp); | |
224 | ||
225 | return try_set_cmode(new_cmode)?:*lenp; | |
226 | } | |
227 | ||
228 | static int cmode_sysctl(ctl_table *table, int *name, int nlen, | |
229 | void *oldval, size_t *oldlenp, | |
230 | void *newval, size_t newlen, void **context) | |
231 | { | |
232 | if (oldval && oldlenp) { | |
233 | size_t oldlen; | |
234 | ||
235 | if (get_user(oldlen, oldlenp)) | |
236 | return -EFAULT; | |
237 | ||
238 | if (oldlen != sizeof(int)) | |
239 | return -EINVAL; | |
240 | ||
241 | if (put_user(clock_cmode_current, (unsigned int *)oldval) || | |
242 | put_user(sizeof(int), oldlenp)) | |
243 | return -EFAULT; | |
244 | } | |
245 | if (newval && newlen) { | |
246 | int new_cmode; | |
247 | ||
248 | if (newlen != sizeof(int)) | |
249 | return -EINVAL; | |
250 | ||
251 | if (get_user(new_cmode, (int *)newval)) | |
252 | return -EFAULT; | |
253 | ||
254 | return try_set_cmode(new_cmode)?:1; | |
255 | } | |
256 | return 1; | |
257 | } | |
258 | ||
259 | static int try_set_p0(int new_p0) | |
260 | { | |
261 | unsigned long flags, clkc; | |
262 | ||
263 | if (new_p0 < 0 || new_p0 > 1) | |
264 | return -EINVAL; | |
265 | ||
266 | local_irq_save(flags); | |
267 | __set_PSR(flags & ~PSR_ET); | |
268 | ||
269 | frv_dma_pause_all(); | |
270 | ||
271 | clkc = __get_CLKC(); | |
272 | if (new_p0) | |
273 | clkc |= CLKC_P0; | |
274 | else | |
275 | clkc &= ~CLKC_P0; | |
276 | __set_CLKC(clkc); | |
277 | ||
278 | determine_clocks(0); | |
279 | time_divisor_init(); | |
280 | ||
281 | #ifdef DEBUG | |
282 | determine_clocks(1); | |
283 | #endif | |
284 | frv_dma_resume_all(); | |
285 | local_irq_restore(flags); | |
286 | return 0; | |
287 | } | |
288 | ||
289 | static int try_set_cm(int new_cm) | |
290 | { | |
291 | unsigned long flags, clkc; | |
292 | ||
293 | if (new_cm < 0 || new_cm > 1) | |
294 | return -EINVAL; | |
295 | ||
296 | local_irq_save(flags); | |
297 | __set_PSR(flags & ~PSR_ET); | |
298 | ||
299 | frv_dma_pause_all(); | |
300 | ||
301 | clkc = __get_CLKC(); | |
302 | clkc &= ~CLKC_CM; | |
303 | clkc |= new_cm; | |
304 | __set_CLKC(clkc); | |
305 | ||
306 | determine_clocks(0); | |
307 | time_divisor_init(); | |
308 | ||
309 | #if 1 //def DEBUG | |
310 | determine_clocks(1); | |
311 | #endif | |
312 | ||
313 | frv_dma_resume_all(); | |
314 | local_irq_restore(flags); | |
315 | return 0; | |
316 | } | |
317 | ||
318 | static int p0_procctl(ctl_table *ctl, int write, struct file *filp, | |
319 | void *buffer, size_t *lenp, loff_t *fpos) | |
320 | { | |
321 | int new_p0; | |
322 | ||
323 | if (!write) | |
324 | return proc_dointvec(ctl, write, filp, buffer, lenp, fpos); | |
325 | ||
326 | new_p0 = user_atoi(buffer, *lenp); | |
327 | ||
328 | return try_set_p0(new_p0)?:*lenp; | |
329 | } | |
330 | ||
331 | static int p0_sysctl(ctl_table *table, int *name, int nlen, | |
332 | void *oldval, size_t *oldlenp, | |
333 | void *newval, size_t newlen, void **context) | |
334 | { | |
335 | if (oldval && oldlenp) { | |
336 | size_t oldlen; | |
337 | ||
338 | if (get_user(oldlen, oldlenp)) | |
339 | return -EFAULT; | |
340 | ||
341 | if (oldlen != sizeof(int)) | |
342 | return -EINVAL; | |
343 | ||
344 | if (put_user(clock_p0_current, (unsigned int *)oldval) || | |
345 | put_user(sizeof(int), oldlenp)) | |
346 | return -EFAULT; | |
347 | } | |
348 | if (newval && newlen) { | |
349 | int new_p0; | |
350 | ||
351 | if (newlen != sizeof(int)) | |
352 | return -EINVAL; | |
353 | ||
354 | if (get_user(new_p0, (int *)newval)) | |
355 | return -EFAULT; | |
356 | ||
357 | return try_set_p0(new_p0)?:1; | |
358 | } | |
359 | return 1; | |
360 | } | |
361 | ||
362 | static int cm_procctl(ctl_table *ctl, int write, struct file *filp, | |
363 | void *buffer, size_t *lenp, loff_t *fpos) | |
364 | { | |
365 | int new_cm; | |
366 | ||
367 | if (!write) | |
368 | return proc_dointvec(ctl, write, filp, buffer, lenp, fpos); | |
369 | ||
370 | new_cm = user_atoi(buffer, *lenp); | |
371 | ||
372 | return try_set_cm(new_cm)?:*lenp; | |
373 | } | |
374 | ||
375 | static int cm_sysctl(ctl_table *table, int *name, int nlen, | |
376 | void *oldval, size_t *oldlenp, | |
377 | void *newval, size_t newlen, void **context) | |
378 | { | |
379 | if (oldval && oldlenp) { | |
380 | size_t oldlen; | |
381 | ||
382 | if (get_user(oldlen, oldlenp)) | |
383 | return -EFAULT; | |
384 | ||
385 | if (oldlen != sizeof(int)) | |
386 | return -EINVAL; | |
387 | ||
388 | if (put_user(clock_cm_current, (unsigned int *)oldval) || | |
389 | put_user(sizeof(int), oldlenp)) | |
390 | return -EFAULT; | |
391 | } | |
392 | if (newval && newlen) { | |
393 | int new_cm; | |
394 | ||
395 | if (newlen != sizeof(int)) | |
396 | return -EINVAL; | |
397 | ||
398 | if (get_user(new_cm, (int *)newval)) | |
399 | return -EFAULT; | |
400 | ||
401 | return try_set_cm(new_cm)?:1; | |
402 | } | |
403 | return 1; | |
404 | } | |
405 | ||
406 | ||
407 | static struct ctl_table pm_table[] = | |
408 | { | |
409 | {CTL_PM_SUSPEND, "suspend", NULL, 0, 0200, NULL, &sysctl_pm_do_suspend}, | |
410 | {CTL_PM_CMODE, "cmode", &clock_cmode_current, sizeof(int), 0644, NULL, &cmode_procctl, &cmode_sysctl, NULL}, | |
411 | {CTL_PM_P0, "p0", &clock_p0_current, sizeof(int), 0644, NULL, &p0_procctl, &p0_sysctl, NULL}, | |
412 | {CTL_PM_CM, "cm", &clock_cm_current, sizeof(int), 0644, NULL, &cm_procctl, &cm_sysctl, NULL}, | |
413 | {0} | |
414 | }; | |
415 | ||
416 | static struct ctl_table pm_dir_table[] = | |
417 | { | |
418 | {CTL_PM, "pm", NULL, 0, 0555, pm_table}, | |
419 | {0} | |
420 | }; | |
421 | ||
422 | /* | |
423 | * Initialize power interface | |
424 | */ | |
425 | static int __init pm_init(void) | |
426 | { | |
427 | register_sysctl_table(pm_dir_table, 1); | |
428 | return 0; | |
429 | } | |
430 | ||
431 | __initcall(pm_init); | |
432 | ||
433 | #endif |