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>
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);
31 extern void frv_change_cmode(int);
38 int pm_do_suspend(void)
45 frv_cpu_suspend(pdm_suspend_mode);
54 static unsigned long __irq_mask;
57 * Setup interrupt masks, etc to enable wakeup by power switch
59 static void __default_power_switch_setup(void)
61 /* default is to mask all interrupt sources. */
62 __irq_mask = *(unsigned long *)0xfeff9820;
63 *(unsigned long *)0xfeff9820 = 0xfffe0000;
67 * Cleanup interrupt masks, etc after wakeup by power switch
69 static void __default_power_switch_cleanup(void)
71 *(unsigned long *)0xfeff9820 = __irq_mask;
75 * Return non-zero if wakeup irq was caused by power switch
77 static int __default_power_switch_check(void)
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;
86 int pm_do_bus_sleep(void)
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.
95 __power_switch_wake_setup();
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.
106 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
107 } while (__power_switch_wake_check && !__power_switch_wake_check());
112 * Here is where we need some platform-dependent restore
113 * of the interrupt state prior to being called.
115 __power_switch_wake_cleanup();
122 unsigned long sleep_phys_sp(void *sp)
124 return virt_to_phys(sp);
129 * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
130 * when all the PM interfaces exist nicely.
133 #define CTL_PM_SUSPEND 1
134 #define CTL_PM_CMODE 2
138 static int user_atoi(char *ubuf, size_t len)
146 if (copy_from_user(buf, ubuf, len))
150 ret = simple_strtoul(buf, NULL, 0);
159 static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
160 void *buffer, size_t *lenp, loff_t *fpos)
167 mode = user_atoi(buffer, *lenp);
168 if ((mode != 1) && (mode != 5))
171 retval = pm_send_all(PM_SUSPEND, (void *)3);
175 retval = pm_do_bus_sleep();
177 retval = pm_do_suspend();
178 pm_send_all(PM_RESUME, (void *)0);
184 static int try_set_cmode(int new_cmode)
188 if (!(clock_cmodes_permitted & (1<<new_cmode)))
191 /* tell all the drivers we're suspending */
192 pm_send_all(PM_SUSPEND, (void *)3);
194 /* now change cmode */
198 frv_change_cmode(new_cmode);
206 frv_dma_resume_all();
209 /* tell all the drivers we're resuming */
210 pm_send_all(PM_RESUME, (void *)0);
215 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
216 void *buffer, size_t *lenp, loff_t *fpos)
221 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
223 new_cmode = user_atoi(buffer, *lenp);
225 return try_set_cmode(new_cmode)?:*lenp;
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)
232 if (oldval && oldlenp) {
235 if (get_user(oldlen, oldlenp))
238 if (oldlen != sizeof(int))
241 if (put_user(clock_cmode_current, (unsigned int *)oldval) ||
242 put_user(sizeof(int), oldlenp))
245 if (newval && newlen) {
248 if (newlen != sizeof(int))
251 if (get_user(new_cmode, (int *)newval))
254 return try_set_cmode(new_cmode)?:1;
259 static int try_set_p0(int new_p0)
261 unsigned long flags, clkc;
263 if (new_p0 < 0 || new_p0 > 1)
266 local_irq_save(flags);
267 __set_PSR(flags & ~PSR_ET);
284 frv_dma_resume_all();
285 local_irq_restore(flags);
289 static int try_set_cm(int new_cm)
291 unsigned long flags, clkc;
293 if (new_cm < 0 || new_cm > 1)
296 local_irq_save(flags);
297 __set_PSR(flags & ~PSR_ET);
313 frv_dma_resume_all();
314 local_irq_restore(flags);
318 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
319 void *buffer, size_t *lenp, loff_t *fpos)
324 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
326 new_p0 = user_atoi(buffer, *lenp);
328 return try_set_p0(new_p0)?:*lenp;
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)
335 if (oldval && oldlenp) {
338 if (get_user(oldlen, oldlenp))
341 if (oldlen != sizeof(int))
344 if (put_user(clock_p0_current, (unsigned int *)oldval) ||
345 put_user(sizeof(int), oldlenp))
348 if (newval && newlen) {
351 if (newlen != sizeof(int))
354 if (get_user(new_p0, (int *)newval))
357 return try_set_p0(new_p0)?:1;
362 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
363 void *buffer, size_t *lenp, loff_t *fpos)
368 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
370 new_cm = user_atoi(buffer, *lenp);
372 return try_set_cm(new_cm)?:*lenp;
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)
379 if (oldval && oldlenp) {
382 if (get_user(oldlen, oldlenp))
385 if (oldlen != sizeof(int))
388 if (put_user(clock_cm_current, (unsigned int *)oldval) ||
389 put_user(sizeof(int), oldlenp))
392 if (newval && newlen) {
395 if (newlen != sizeof(int))
398 if (get_user(new_cm, (int *)newval))
401 return try_set_cm(new_cm)?:1;
407 static struct ctl_table pm_table[] =
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},
416 static struct ctl_table pm_dir_table[] =
418 {CTL_PM, "pm", NULL, 0, 0555, pm_table},
423 * Initialize power interface
425 static int __init pm_init(void)
427 register_sysctl_table(pm_dir_table, 1);