Linux-2.6.12-rc2
[linux-2.6/kvm.git] / arch / frv / kernel / pm.c
blob1a1e8a119c3d722aa7d9b64ddabe820ace45bc4b
1 /*
2 * FR-V Power Management Routines
4 * Copyright (c) 2004 Red Hat, Inc.
6 * Based on SA1100 version:
7 * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License.
14 #include <linux/config.h>
15 #include <linux/init.h>
16 #include <linux/pm.h>
17 #include <linux/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/sysctl.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <asm/uaccess.h>
24 #include <asm/mb86943a.h>
26 #include "local.h"
28 void (*pm_power_off)(void);
30 extern void frv_change_cmode(int);
33 * Debug macros
35 #define DEBUG
37 int pm_do_suspend(void)
39 local_irq_disable();
41 __set_LEDS(0xb1);
43 /* go zzz */
44 frv_cpu_suspend(pdm_suspend_mode);
46 __set_LEDS(0xb2);
48 local_irq_enable();
50 return 0;
53 static unsigned long __irq_mask;
56 * Setup interrupt masks, etc to enable wakeup by power switch
58 static void __default_power_switch_setup(void)
60 /* default is to mask all interrupt sources. */
61 __irq_mask = *(unsigned long *)0xfeff9820;
62 *(unsigned long *)0xfeff9820 = 0xfffe0000;
66 * Cleanup interrupt masks, etc after wakeup by power switch
68 static void __default_power_switch_cleanup(void)
70 *(unsigned long *)0xfeff9820 = __irq_mask;
74 * Return non-zero if wakeup irq was caused by power switch
76 static int __default_power_switch_check(void)
78 return 1;
81 void (*__power_switch_wake_setup)(void) = __default_power_switch_setup;
82 int (*__power_switch_wake_check)(void) = __default_power_switch_check;
83 void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup;
85 int pm_do_bus_sleep(void)
87 local_irq_disable();
90 * Here is where we need some platform-dependent setup
91 * of the interrupt state so that appropriate wakeup
92 * sources are allowed and all others are masked.
94 __power_switch_wake_setup();
96 __set_LEDS(0xa1);
98 /* go zzz
100 * This is in a loop in case power switch shares an irq with other
101 * devices. The wake_check() tells us if we need to finish waking
102 * or go back to sleep.
104 do {
105 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
106 } while (__power_switch_wake_check && !__power_switch_wake_check());
108 __set_LEDS(0xa2);
111 * Here is where we need some platform-dependent restore
112 * of the interrupt state prior to being called.
114 __power_switch_wake_cleanup();
116 local_irq_enable();
118 return 0;
121 unsigned long sleep_phys_sp(void *sp)
123 return virt_to_phys(sp);
126 #ifdef CONFIG_SYSCTL
128 * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
129 * when all the PM interfaces exist nicely.
131 #define CTL_PM 9899
132 #define CTL_PM_SUSPEND 1
133 #define CTL_PM_CMODE 2
134 #define CTL_PM_P0 4
135 #define CTL_PM_CM 5
137 static int user_atoi(char *ubuf, size_t len)
139 char buf[16];
140 unsigned long ret;
142 if (len > 15)
143 return -EINVAL;
145 if (copy_from_user(buf, ubuf, len))
146 return -EFAULT;
148 buf[len] = 0;
149 ret = simple_strtoul(buf, NULL, 0);
150 if (ret > INT_MAX)
151 return -ERANGE;
152 return ret;
156 * Send us to sleep.
158 static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
159 void *buffer, size_t *lenp, loff_t *fpos)
161 int retval, mode;
163 if (*lenp <= 0)
164 return -EIO;
166 mode = user_atoi(buffer, *lenp);
167 if ((mode != 1) && (mode != 5))
168 return -EINVAL;
170 retval = pm_send_all(PM_SUSPEND, (void *)3);
172 if (retval == 0) {
173 if (mode == 5)
174 retval = pm_do_bus_sleep();
175 else
176 retval = pm_do_suspend();
177 pm_send_all(PM_RESUME, (void *)0);
180 return retval;
183 static int try_set_cmode(int new_cmode)
185 if (new_cmode > 15)
186 return -EINVAL;
187 if (!(clock_cmodes_permitted & (1<<new_cmode)))
188 return -EINVAL;
190 /* tell all the drivers we're suspending */
191 pm_send_all(PM_SUSPEND, (void *)3);
193 /* now change cmode */
194 local_irq_disable();
195 frv_dma_pause_all();
197 frv_change_cmode(new_cmode);
199 determine_clocks(0);
200 time_divisor_init();
202 #ifdef DEBUG
203 determine_clocks(1);
204 #endif
205 frv_dma_resume_all();
206 local_irq_enable();
208 /* tell all the drivers we're resuming */
209 pm_send_all(PM_RESUME, (void *)0);
210 return 0;
214 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
215 void *buffer, size_t *lenp, loff_t *fpos)
217 int new_cmode;
219 if (!write)
220 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
222 new_cmode = user_atoi(buffer, *lenp);
224 return try_set_cmode(new_cmode)?:*lenp;
227 static int cmode_sysctl(ctl_table *table, int *name, int nlen,
228 void *oldval, size_t *oldlenp,
229 void *newval, size_t newlen, void **context)
231 if (oldval && oldlenp) {
232 size_t oldlen;
234 if (get_user(oldlen, oldlenp))
235 return -EFAULT;
237 if (oldlen != sizeof(int))
238 return -EINVAL;
240 if (put_user(clock_cmode_current, (unsigned int *)oldval) ||
241 put_user(sizeof(int), oldlenp))
242 return -EFAULT;
244 if (newval && newlen) {
245 int new_cmode;
247 if (newlen != sizeof(int))
248 return -EINVAL;
250 if (get_user(new_cmode, (int *)newval))
251 return -EFAULT;
253 return try_set_cmode(new_cmode)?:1;
255 return 1;
258 static int try_set_p0(int new_p0)
260 unsigned long flags, clkc;
262 if (new_p0 < 0 || new_p0 > 1)
263 return -EINVAL;
265 local_irq_save(flags);
266 __set_PSR(flags & ~PSR_ET);
268 frv_dma_pause_all();
270 clkc = __get_CLKC();
271 if (new_p0)
272 clkc |= CLKC_P0;
273 else
274 clkc &= ~CLKC_P0;
275 __set_CLKC(clkc);
277 determine_clocks(0);
278 time_divisor_init();
280 #ifdef DEBUG
281 determine_clocks(1);
282 #endif
283 frv_dma_resume_all();
284 local_irq_restore(flags);
285 return 0;
288 static int try_set_cm(int new_cm)
290 unsigned long flags, clkc;
292 if (new_cm < 0 || new_cm > 1)
293 return -EINVAL;
295 local_irq_save(flags);
296 __set_PSR(flags & ~PSR_ET);
298 frv_dma_pause_all();
300 clkc = __get_CLKC();
301 clkc &= ~CLKC_CM;
302 clkc |= new_cm;
303 __set_CLKC(clkc);
305 determine_clocks(0);
306 time_divisor_init();
308 #if 1 //def DEBUG
309 determine_clocks(1);
310 #endif
312 frv_dma_resume_all();
313 local_irq_restore(flags);
314 return 0;
317 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
318 void *buffer, size_t *lenp, loff_t *fpos)
320 int new_p0;
322 if (!write)
323 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
325 new_p0 = user_atoi(buffer, *lenp);
327 return try_set_p0(new_p0)?:*lenp;
330 static int p0_sysctl(ctl_table *table, int *name, int nlen,
331 void *oldval, size_t *oldlenp,
332 void *newval, size_t newlen, void **context)
334 if (oldval && oldlenp) {
335 size_t oldlen;
337 if (get_user(oldlen, oldlenp))
338 return -EFAULT;
340 if (oldlen != sizeof(int))
341 return -EINVAL;
343 if (put_user(clock_p0_current, (unsigned int *)oldval) ||
344 put_user(sizeof(int), oldlenp))
345 return -EFAULT;
347 if (newval && newlen) {
348 int new_p0;
350 if (newlen != sizeof(int))
351 return -EINVAL;
353 if (get_user(new_p0, (int *)newval))
354 return -EFAULT;
356 return try_set_p0(new_p0)?:1;
358 return 1;
361 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
362 void *buffer, size_t *lenp, loff_t *fpos)
364 int new_cm;
366 if (!write)
367 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
369 new_cm = user_atoi(buffer, *lenp);
371 return try_set_cm(new_cm)?:*lenp;
374 static int cm_sysctl(ctl_table *table, int *name, int nlen,
375 void *oldval, size_t *oldlenp,
376 void *newval, size_t newlen, void **context)
378 if (oldval && oldlenp) {
379 size_t oldlen;
381 if (get_user(oldlen, oldlenp))
382 return -EFAULT;
384 if (oldlen != sizeof(int))
385 return -EINVAL;
387 if (put_user(clock_cm_current, (unsigned int *)oldval) ||
388 put_user(sizeof(int), oldlenp))
389 return -EFAULT;
391 if (newval && newlen) {
392 int new_cm;
394 if (newlen != sizeof(int))
395 return -EINVAL;
397 if (get_user(new_cm, (int *)newval))
398 return -EFAULT;
400 return try_set_cm(new_cm)?:1;
402 return 1;
406 static struct ctl_table pm_table[] =
408 {CTL_PM_SUSPEND, "suspend", NULL, 0, 0200, NULL, &sysctl_pm_do_suspend},
409 {CTL_PM_CMODE, "cmode", &clock_cmode_current, sizeof(int), 0644, NULL, &cmode_procctl, &cmode_sysctl, NULL},
410 {CTL_PM_P0, "p0", &clock_p0_current, sizeof(int), 0644, NULL, &p0_procctl, &p0_sysctl, NULL},
411 {CTL_PM_CM, "cm", &clock_cm_current, sizeof(int), 0644, NULL, &cm_procctl, &cm_sysctl, NULL},
415 static struct ctl_table pm_dir_table[] =
417 {CTL_PM, "pm", NULL, 0, 0555, pm_table},
422 * Initialize power interface
424 static int __init pm_init(void)
426 register_sysctl_table(pm_dir_table, 1);
427 return 0;
430 __initcall(pm_init);
432 #endif