Linux 2.6.25.3
[linux/fpc-iii.git] / arch / frv / kernel / pm.c
blobc57ce3f1f2e281b2c38a7de860d390cbc08e5fd0
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/init.h>
15 #include <linux/module.h>
16 #include <linux/pm.h>
17 #include <linux/pm_legacy.h>
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>
25 #include <asm/mb86943a.h>
27 #include "local.h"
30 * Debug macros
32 #define DEBUG
34 int pm_do_suspend(void)
36 local_irq_disable();
38 __set_LEDS(0xb1);
40 /* go zzz */
41 frv_cpu_suspend(pdm_suspend_mode);
43 __set_LEDS(0xb2);
45 local_irq_enable();
47 return 0;
50 static unsigned long __irq_mask;
53 * Setup interrupt masks, etc to enable wakeup by power switch
55 static void __default_power_switch_setup(void)
57 /* default is to mask all interrupt sources. */
58 __irq_mask = *(unsigned long *)0xfeff9820;
59 *(unsigned long *)0xfeff9820 = 0xfffe0000;
63 * Cleanup interrupt masks, etc after wakeup by power switch
65 static void __default_power_switch_cleanup(void)
67 *(unsigned long *)0xfeff9820 = __irq_mask;
71 * Return non-zero if wakeup irq was caused by power switch
73 static int __default_power_switch_check(void)
75 return 1;
78 void (*__power_switch_wake_setup)(void) = __default_power_switch_setup;
79 int (*__power_switch_wake_check)(void) = __default_power_switch_check;
80 void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup;
82 int pm_do_bus_sleep(void)
84 local_irq_disable();
87 * Here is where we need some platform-dependent setup
88 * of the interrupt state so that appropriate wakeup
89 * sources are allowed and all others are masked.
91 __power_switch_wake_setup();
93 __set_LEDS(0xa1);
95 /* go zzz
97 * This is in a loop in case power switch shares an irq with other
98 * devices. The wake_check() tells us if we need to finish waking
99 * or go back to sleep.
101 do {
102 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
103 } while (__power_switch_wake_check && !__power_switch_wake_check());
105 __set_LEDS(0xa2);
108 * Here is where we need some platform-dependent restore
109 * of the interrupt state prior to being called.
111 __power_switch_wake_cleanup();
113 local_irq_enable();
115 return 0;
118 unsigned long sleep_phys_sp(void *sp)
120 return virt_to_phys(sp);
123 #ifdef CONFIG_SYSCTL
125 * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
126 * when all the PM interfaces exist nicely.
128 #define CTL_PM_SUSPEND 1
129 #define CTL_PM_CMODE 2
130 #define CTL_PM_P0 4
131 #define CTL_PM_CM 5
133 static int user_atoi(char __user *ubuf, size_t len)
135 char buf[16];
136 unsigned long ret;
138 if (len > 15)
139 return -EINVAL;
141 if (copy_from_user(buf, ubuf, len))
142 return -EFAULT;
144 buf[len] = 0;
145 ret = simple_strtoul(buf, NULL, 0);
146 if (ret > INT_MAX)
147 return -ERANGE;
148 return ret;
152 * Send us to sleep.
154 static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
155 void __user *buffer, size_t *lenp, loff_t *fpos)
157 int retval, mode;
159 if (*lenp <= 0)
160 return -EIO;
162 mode = user_atoi(buffer, *lenp);
163 if ((mode != 1) && (mode != 5))
164 return -EINVAL;
166 retval = pm_send_all(PM_SUSPEND, (void *)3);
168 if (retval == 0) {
169 if (mode == 5)
170 retval = pm_do_bus_sleep();
171 else
172 retval = pm_do_suspend();
173 pm_send_all(PM_RESUME, (void *)0);
176 return retval;
179 static int try_set_cmode(int new_cmode)
181 if (new_cmode > 15)
182 return -EINVAL;
183 if (!(clock_cmodes_permitted & (1<<new_cmode)))
184 return -EINVAL;
186 /* tell all the drivers we're suspending */
187 pm_send_all(PM_SUSPEND, (void *)3);
189 /* now change cmode */
190 local_irq_disable();
191 frv_dma_pause_all();
193 frv_change_cmode(new_cmode);
195 determine_clocks(0);
196 time_divisor_init();
198 #ifdef DEBUG
199 determine_clocks(1);
200 #endif
201 frv_dma_resume_all();
202 local_irq_enable();
204 /* tell all the drivers we're resuming */
205 pm_send_all(PM_RESUME, (void *)0);
206 return 0;
210 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
211 void __user *buffer, size_t *lenp, loff_t *fpos)
213 int new_cmode;
215 if (!write)
216 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
218 new_cmode = user_atoi(buffer, *lenp);
220 return try_set_cmode(new_cmode)?:*lenp;
223 static int cmode_sysctl(ctl_table *table, int __user *name, int nlen,
224 void __user *oldval, size_t __user *oldlenp,
225 void __user *newval, size_t newlen)
227 if (oldval && oldlenp) {
228 size_t oldlen;
230 if (get_user(oldlen, oldlenp))
231 return -EFAULT;
233 if (oldlen != sizeof(int))
234 return -EINVAL;
236 if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
237 put_user(sizeof(int), oldlenp))
238 return -EFAULT;
240 if (newval && newlen) {
241 int new_cmode;
243 if (newlen != sizeof(int))
244 return -EINVAL;
246 if (get_user(new_cmode, (int __user *)newval))
247 return -EFAULT;
249 return try_set_cmode(new_cmode)?:1;
251 return 1;
254 static int try_set_p0(int new_p0)
256 unsigned long flags, clkc;
258 if (new_p0 < 0 || new_p0 > 1)
259 return -EINVAL;
261 local_irq_save(flags);
262 __set_PSR(flags & ~PSR_ET);
264 frv_dma_pause_all();
266 clkc = __get_CLKC();
267 if (new_p0)
268 clkc |= CLKC_P0;
269 else
270 clkc &= ~CLKC_P0;
271 __set_CLKC(clkc);
273 determine_clocks(0);
274 time_divisor_init();
276 #ifdef DEBUG
277 determine_clocks(1);
278 #endif
279 frv_dma_resume_all();
280 local_irq_restore(flags);
281 return 0;
284 static int try_set_cm(int new_cm)
286 unsigned long flags, clkc;
288 if (new_cm < 0 || new_cm > 1)
289 return -EINVAL;
291 local_irq_save(flags);
292 __set_PSR(flags & ~PSR_ET);
294 frv_dma_pause_all();
296 clkc = __get_CLKC();
297 clkc &= ~CLKC_CM;
298 clkc |= new_cm;
299 __set_CLKC(clkc);
301 determine_clocks(0);
302 time_divisor_init();
304 #if 1 //def DEBUG
305 determine_clocks(1);
306 #endif
308 frv_dma_resume_all();
309 local_irq_restore(flags);
310 return 0;
313 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
314 void __user *buffer, size_t *lenp, loff_t *fpos)
316 int new_p0;
318 if (!write)
319 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
321 new_p0 = user_atoi(buffer, *lenp);
323 return try_set_p0(new_p0)?:*lenp;
326 static int p0_sysctl(ctl_table *table, int __user *name, int nlen,
327 void __user *oldval, size_t __user *oldlenp,
328 void __user *newval, size_t newlen)
330 if (oldval && oldlenp) {
331 size_t oldlen;
333 if (get_user(oldlen, oldlenp))
334 return -EFAULT;
336 if (oldlen != sizeof(int))
337 return -EINVAL;
339 if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
340 put_user(sizeof(int), oldlenp))
341 return -EFAULT;
343 if (newval && newlen) {
344 int new_p0;
346 if (newlen != sizeof(int))
347 return -EINVAL;
349 if (get_user(new_p0, (int __user *)newval))
350 return -EFAULT;
352 return try_set_p0(new_p0)?:1;
354 return 1;
357 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
358 void __user *buffer, size_t *lenp, loff_t *fpos)
360 int new_cm;
362 if (!write)
363 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
365 new_cm = user_atoi(buffer, *lenp);
367 return try_set_cm(new_cm)?:*lenp;
370 static int cm_sysctl(ctl_table *table, int __user *name, int nlen,
371 void __user *oldval, size_t __user *oldlenp,
372 void __user *newval, size_t newlen)
374 if (oldval && oldlenp) {
375 size_t oldlen;
377 if (get_user(oldlen, oldlenp))
378 return -EFAULT;
380 if (oldlen != sizeof(int))
381 return -EINVAL;
383 if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
384 put_user(sizeof(int), oldlenp))
385 return -EFAULT;
387 if (newval && newlen) {
388 int new_cm;
390 if (newlen != sizeof(int))
391 return -EINVAL;
393 if (get_user(new_cm, (int __user *)newval))
394 return -EFAULT;
396 return try_set_cm(new_cm)?:1;
398 return 1;
402 static struct ctl_table pm_table[] =
405 .ctl_name = CTL_PM_SUSPEND,
406 .procname = "suspend",
407 .data = NULL,
408 .maxlen = 0,
409 .mode = 0200,
410 .proc_handler = &sysctl_pm_do_suspend,
413 .ctl_name = CTL_PM_CMODE,
414 .procname = "cmode",
415 .data = &clock_cmode_current,
416 .maxlen = sizeof(int),
417 .mode = 0644,
418 .proc_handler = &cmode_procctl,
419 .strategy = &cmode_sysctl,
422 .ctl_name = CTL_PM_P0,
423 .procname = "p0",
424 .data = &clock_p0_current,
425 .maxlen = sizeof(int),
426 .mode = 0644,
427 .proc_handler = &p0_procctl,
428 .strategy = &p0_sysctl,
431 .ctl_name = CTL_PM_CM,
432 .procname = "cm",
433 .data = &clock_cm_current,
434 .maxlen = sizeof(int),
435 .mode = 0644,
436 .proc_handler = &cm_procctl,
437 .strategy = &cm_sysctl,
439 { .ctl_name = 0}
442 static struct ctl_table pm_dir_table[] =
445 .ctl_name = CTL_PM,
446 .procname = "pm",
447 .mode = 0555,
448 .child = pm_table,
450 { .ctl_name = 0}
454 * Initialize power interface
456 static int __init pm_init(void)
458 register_sysctl_table(pm_dir_table);
459 return 0;
462 __initcall(pm_init);
464 #endif