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/module.h>
18 #include <linux/pm_legacy.h>
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/sysctl.h>
22 #include <linux/errno.h>
23 #include <linux/delay.h>
24 #include <asm/uaccess.h>
26 #include <asm/mb86943a.h>
30 void (*pm_power_off)(void);
31 EXPORT_SYMBOL(pm_power_off);
33 extern void frv_change_cmode(int);
40 int pm_do_suspend(void)
47 frv_cpu_suspend(pdm_suspend_mode);
56 static unsigned long __irq_mask;
59 * Setup interrupt masks, etc to enable wakeup by power switch
61 static void __default_power_switch_setup(void)
63 /* default is to mask all interrupt sources. */
64 __irq_mask = *(unsigned long *)0xfeff9820;
65 *(unsigned long *)0xfeff9820 = 0xfffe0000;
69 * Cleanup interrupt masks, etc after wakeup by power switch
71 static void __default_power_switch_cleanup(void)
73 *(unsigned long *)0xfeff9820 = __irq_mask;
77 * Return non-zero if wakeup irq was caused by power switch
79 static int __default_power_switch_check(void)
84 void (*__power_switch_wake_setup)(void) = __default_power_switch_setup;
85 int (*__power_switch_wake_check)(void) = __default_power_switch_check;
86 void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup;
88 int pm_do_bus_sleep(void)
93 * Here is where we need some platform-dependent setup
94 * of the interrupt state so that appropriate wakeup
95 * sources are allowed and all others are masked.
97 __power_switch_wake_setup();
103 * This is in a loop in case power switch shares an irq with other
104 * devices. The wake_check() tells us if we need to finish waking
105 * or go back to sleep.
108 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
109 } while (__power_switch_wake_check && !__power_switch_wake_check());
114 * Here is where we need some platform-dependent restore
115 * of the interrupt state prior to being called.
117 __power_switch_wake_cleanup();
124 unsigned long sleep_phys_sp(void *sp)
126 return virt_to_phys(sp);
131 * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
132 * when all the PM interfaces exist nicely.
135 #define CTL_PM_SUSPEND 1
136 #define CTL_PM_CMODE 2
140 static int user_atoi(char __user *ubuf, size_t len)
148 if (copy_from_user(buf, ubuf, len))
152 ret = simple_strtoul(buf, NULL, 0);
161 static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
162 void __user *buffer, size_t *lenp, loff_t *fpos)
169 mode = user_atoi(buffer, *lenp);
170 if ((mode != 1) && (mode != 5))
173 retval = pm_send_all(PM_SUSPEND, (void *)3);
177 retval = pm_do_bus_sleep();
179 retval = pm_do_suspend();
180 pm_send_all(PM_RESUME, (void *)0);
186 static int try_set_cmode(int new_cmode)
190 if (!(clock_cmodes_permitted & (1<<new_cmode)))
193 /* tell all the drivers we're suspending */
194 pm_send_all(PM_SUSPEND, (void *)3);
196 /* now change cmode */
200 frv_change_cmode(new_cmode);
208 frv_dma_resume_all();
211 /* tell all the drivers we're resuming */
212 pm_send_all(PM_RESUME, (void *)0);
217 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
218 void __user *buffer, size_t *lenp, loff_t *fpos)
223 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
225 new_cmode = user_atoi(buffer, *lenp);
227 return try_set_cmode(new_cmode)?:*lenp;
230 static int cmode_sysctl(ctl_table *table, int __user *name, int nlen,
231 void __user *oldval, size_t __user *oldlenp,
232 void __user *newval, size_t newlen, void **context)
234 if (oldval && oldlenp) {
237 if (get_user(oldlen, oldlenp))
240 if (oldlen != sizeof(int))
243 if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
244 put_user(sizeof(int), oldlenp))
247 if (newval && newlen) {
250 if (newlen != sizeof(int))
253 if (get_user(new_cmode, (int __user *)newval))
256 return try_set_cmode(new_cmode)?:1;
261 static int try_set_p0(int new_p0)
263 unsigned long flags, clkc;
265 if (new_p0 < 0 || new_p0 > 1)
268 local_irq_save(flags);
269 __set_PSR(flags & ~PSR_ET);
286 frv_dma_resume_all();
287 local_irq_restore(flags);
291 static int try_set_cm(int new_cm)
293 unsigned long flags, clkc;
295 if (new_cm < 0 || new_cm > 1)
298 local_irq_save(flags);
299 __set_PSR(flags & ~PSR_ET);
315 frv_dma_resume_all();
316 local_irq_restore(flags);
320 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
321 void __user *buffer, size_t *lenp, loff_t *fpos)
326 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
328 new_p0 = user_atoi(buffer, *lenp);
330 return try_set_p0(new_p0)?:*lenp;
333 static int p0_sysctl(ctl_table *table, int __user *name, int nlen,
334 void __user *oldval, size_t __user *oldlenp,
335 void __user *newval, size_t newlen, void **context)
337 if (oldval && oldlenp) {
340 if (get_user(oldlen, oldlenp))
343 if (oldlen != sizeof(int))
346 if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
347 put_user(sizeof(int), oldlenp))
350 if (newval && newlen) {
353 if (newlen != sizeof(int))
356 if (get_user(new_p0, (int __user *)newval))
359 return try_set_p0(new_p0)?:1;
364 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
365 void __user *buffer, size_t *lenp, loff_t *fpos)
370 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
372 new_cm = user_atoi(buffer, *lenp);
374 return try_set_cm(new_cm)?:*lenp;
377 static int cm_sysctl(ctl_table *table, int __user *name, int nlen,
378 void __user *oldval, size_t __user *oldlenp,
379 void __user *newval, size_t newlen, void **context)
381 if (oldval && oldlenp) {
384 if (get_user(oldlen, oldlenp))
387 if (oldlen != sizeof(int))
390 if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
391 put_user(sizeof(int), oldlenp))
394 if (newval && newlen) {
397 if (newlen != sizeof(int))
400 if (get_user(new_cm, (int __user *)newval))
403 return try_set_cm(new_cm)?:1;
409 static struct ctl_table pm_table[] =
411 {CTL_PM_SUSPEND, "suspend", NULL, 0, 0200, NULL, &sysctl_pm_do_suspend},
412 {CTL_PM_CMODE, "cmode", &clock_cmode_current, sizeof(int), 0644, NULL, &cmode_procctl, &cmode_sysctl, NULL},
413 {CTL_PM_P0, "p0", &clock_p0_current, sizeof(int), 0644, NULL, &p0_procctl, &p0_sysctl, NULL},
414 {CTL_PM_CM, "cm", &clock_cm_current, sizeof(int), 0644, NULL, &cm_procctl, &cm_sysctl, NULL},
418 static struct ctl_table pm_dir_table[] =
420 {CTL_PM, "pm", NULL, 0, 0555, pm_table},
425 * Initialize power interface
427 static int __init pm_init(void)
429 register_sysctl_table(pm_dir_table, 1);