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>
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>
29 void (*pm_power_off)(void);
30 EXPORT_SYMBOL(pm_power_off);
32 extern void frv_change_cmode(int);
39 int pm_do_suspend(void)
46 frv_cpu_suspend(pdm_suspend_mode);
55 static unsigned long __irq_mask;
58 * Setup interrupt masks, etc to enable wakeup by power switch
60 static void __default_power_switch_setup(void)
62 /* default is to mask all interrupt sources. */
63 __irq_mask = *(unsigned long *)0xfeff9820;
64 *(unsigned long *)0xfeff9820 = 0xfffe0000;
68 * Cleanup interrupt masks, etc after wakeup by power switch
70 static void __default_power_switch_cleanup(void)
72 *(unsigned long *)0xfeff9820 = __irq_mask;
76 * Return non-zero if wakeup irq was caused by power switch
78 static int __default_power_switch_check(void)
83 void (*__power_switch_wake_setup)(void) = __default_power_switch_setup;
84 int (*__power_switch_wake_check)(void) = __default_power_switch_check;
85 void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup;
87 int pm_do_bus_sleep(void)
92 * Here is where we need some platform-dependent setup
93 * of the interrupt state so that appropriate wakeup
94 * sources are allowed and all others are masked.
96 __power_switch_wake_setup();
102 * This is in a loop in case power switch shares an irq with other
103 * devices. The wake_check() tells us if we need to finish waking
104 * or go back to sleep.
107 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
108 } while (__power_switch_wake_check && !__power_switch_wake_check());
113 * Here is where we need some platform-dependent restore
114 * of the interrupt state prior to being called.
116 __power_switch_wake_cleanup();
123 unsigned long sleep_phys_sp(void *sp)
125 return virt_to_phys(sp);
130 * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
131 * when all the PM interfaces exist nicely.
134 #define CTL_PM_SUSPEND 1
135 #define CTL_PM_CMODE 2
139 static int user_atoi(char __user *ubuf, size_t len)
147 if (copy_from_user(buf, ubuf, len))
151 ret = simple_strtoul(buf, NULL, 0);
160 static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
161 void __user *buffer, size_t *lenp, loff_t *fpos)
168 mode = user_atoi(buffer, *lenp);
169 if ((mode != 1) && (mode != 5))
172 retval = pm_send_all(PM_SUSPEND, (void *)3);
176 retval = pm_do_bus_sleep();
178 retval = pm_do_suspend();
179 pm_send_all(PM_RESUME, (void *)0);
185 static int try_set_cmode(int new_cmode)
189 if (!(clock_cmodes_permitted & (1<<new_cmode)))
192 /* tell all the drivers we're suspending */
193 pm_send_all(PM_SUSPEND, (void *)3);
195 /* now change cmode */
199 frv_change_cmode(new_cmode);
207 frv_dma_resume_all();
210 /* tell all the drivers we're resuming */
211 pm_send_all(PM_RESUME, (void *)0);
216 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
217 void __user *buffer, size_t *lenp, loff_t *fpos)
222 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
224 new_cmode = user_atoi(buffer, *lenp);
226 return try_set_cmode(new_cmode)?:*lenp;
229 static int cmode_sysctl(ctl_table *table, int __user *name, int nlen,
230 void __user *oldval, size_t __user *oldlenp,
231 void __user *newval, size_t newlen, void **context)
233 if (oldval && oldlenp) {
236 if (get_user(oldlen, oldlenp))
239 if (oldlen != sizeof(int))
242 if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
243 put_user(sizeof(int), oldlenp))
246 if (newval && newlen) {
249 if (newlen != sizeof(int))
252 if (get_user(new_cmode, (int __user *)newval))
255 return try_set_cmode(new_cmode)?:1;
260 static int try_set_p0(int new_p0)
262 unsigned long flags, clkc;
264 if (new_p0 < 0 || new_p0 > 1)
267 local_irq_save(flags);
268 __set_PSR(flags & ~PSR_ET);
285 frv_dma_resume_all();
286 local_irq_restore(flags);
290 static int try_set_cm(int new_cm)
292 unsigned long flags, clkc;
294 if (new_cm < 0 || new_cm > 1)
297 local_irq_save(flags);
298 __set_PSR(flags & ~PSR_ET);
314 frv_dma_resume_all();
315 local_irq_restore(flags);
319 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
320 void __user *buffer, size_t *lenp, loff_t *fpos)
325 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
327 new_p0 = user_atoi(buffer, *lenp);
329 return try_set_p0(new_p0)?:*lenp;
332 static int p0_sysctl(ctl_table *table, int __user *name, int nlen,
333 void __user *oldval, size_t __user *oldlenp,
334 void __user *newval, size_t newlen, void **context)
336 if (oldval && oldlenp) {
339 if (get_user(oldlen, oldlenp))
342 if (oldlen != sizeof(int))
345 if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
346 put_user(sizeof(int), oldlenp))
349 if (newval && newlen) {
352 if (newlen != sizeof(int))
355 if (get_user(new_p0, (int __user *)newval))
358 return try_set_p0(new_p0)?:1;
363 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
364 void __user *buffer, size_t *lenp, loff_t *fpos)
369 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
371 new_cm = user_atoi(buffer, *lenp);
373 return try_set_cm(new_cm)?:*lenp;
376 static int cm_sysctl(ctl_table *table, int __user *name, int nlen,
377 void __user *oldval, size_t __user *oldlenp,
378 void __user *newval, size_t newlen, void **context)
380 if (oldval && oldlenp) {
383 if (get_user(oldlen, oldlenp))
386 if (oldlen != sizeof(int))
389 if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
390 put_user(sizeof(int), oldlenp))
393 if (newval && newlen) {
396 if (newlen != sizeof(int))
399 if (get_user(new_cm, (int __user *)newval))
402 return try_set_cm(new_cm)?:1;
408 static struct ctl_table pm_table[] =
410 {CTL_PM_SUSPEND, "suspend", NULL, 0, 0200, NULL, &sysctl_pm_do_suspend},
411 {CTL_PM_CMODE, "cmode", &clock_cmode_current, sizeof(int), 0644, NULL, &cmode_procctl, &cmode_sysctl, NULL},
412 {CTL_PM_P0, "p0", &clock_p0_current, sizeof(int), 0644, NULL, &p0_procctl, &p0_sysctl, NULL},
413 {CTL_PM_CM, "cm", &clock_cm_current, sizeof(int), 0644, NULL, &cm_procctl, &cm_sysctl, NULL},
417 static struct ctl_table pm_dir_table[] =
419 {CTL_PM, "pm", NULL, 0, 0555, pm_table},
424 * Initialize power interface
426 static int __init pm_init(void)
428 register_sysctl_table(pm_dir_table, 1);