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>
34 int pm_do_suspend(void)
41 frv_cpu_suspend(pdm_suspend_mode);
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)
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)
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();
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.
102 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
103 } while (__power_switch_wake_check && !__power_switch_wake_check());
108 * Here is where we need some platform-dependent restore
109 * of the interrupt state prior to being called.
111 __power_switch_wake_cleanup();
118 unsigned long sleep_phys_sp(void *sp)
120 return virt_to_phys(sp);
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
133 static int user_atoi(char __user *ubuf, size_t len)
141 if (copy_from_user(buf, ubuf, len))
145 ret = simple_strtoul(buf, NULL, 0);
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)
162 mode = user_atoi(buffer, *lenp);
163 if ((mode != 1) && (mode != 5))
166 retval = pm_send_all(PM_SUSPEND, (void *)3);
170 retval = pm_do_bus_sleep();
172 retval = pm_do_suspend();
173 pm_send_all(PM_RESUME, (void *)0);
179 static int try_set_cmode(int new_cmode)
183 if (!(clock_cmodes_permitted & (1<<new_cmode)))
186 /* tell all the drivers we're suspending */
187 pm_send_all(PM_SUSPEND, (void *)3);
189 /* now change cmode */
193 frv_change_cmode(new_cmode);
201 frv_dma_resume_all();
204 /* tell all the drivers we're resuming */
205 pm_send_all(PM_RESUME, (void *)0);
210 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
211 void __user *buffer, size_t *lenp, loff_t *fpos)
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) {
230 if (get_user(oldlen, oldlenp))
233 if (oldlen != sizeof(int))
236 if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
237 put_user(sizeof(int), oldlenp))
240 if (newval && newlen) {
243 if (newlen != sizeof(int))
246 if (get_user(new_cmode, (int __user *)newval))
249 return try_set_cmode(new_cmode)?:1;
254 static int try_set_p0(int new_p0)
256 unsigned long flags, clkc;
258 if (new_p0 < 0 || new_p0 > 1)
261 local_irq_save(flags);
262 __set_PSR(flags & ~PSR_ET);
279 frv_dma_resume_all();
280 local_irq_restore(flags);
284 static int try_set_cm(int new_cm)
286 unsigned long flags, clkc;
288 if (new_cm < 0 || new_cm > 1)
291 local_irq_save(flags);
292 __set_PSR(flags & ~PSR_ET);
308 frv_dma_resume_all();
309 local_irq_restore(flags);
313 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
314 void __user *buffer, size_t *lenp, loff_t *fpos)
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) {
333 if (get_user(oldlen, oldlenp))
336 if (oldlen != sizeof(int))
339 if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
340 put_user(sizeof(int), oldlenp))
343 if (newval && newlen) {
346 if (newlen != sizeof(int))
349 if (get_user(new_p0, (int __user *)newval))
352 return try_set_p0(new_p0)?:1;
357 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
358 void __user *buffer, size_t *lenp, loff_t *fpos)
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) {
377 if (get_user(oldlen, oldlenp))
380 if (oldlen != sizeof(int))
383 if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
384 put_user(sizeof(int), oldlenp))
387 if (newval && newlen) {
390 if (newlen != sizeof(int))
393 if (get_user(new_cm, (int __user *)newval))
396 return try_set_cm(new_cm)?:1;
402 static struct ctl_table pm_table[] =
405 .ctl_name = CTL_PM_SUSPEND,
406 .procname = "suspend",
410 .proc_handler = &sysctl_pm_do_suspend,
413 .ctl_name = CTL_PM_CMODE,
415 .data = &clock_cmode_current,
416 .maxlen = sizeof(int),
418 .proc_handler = &cmode_procctl,
419 .strategy = &cmode_sysctl,
422 .ctl_name = CTL_PM_P0,
424 .data = &clock_p0_current,
425 .maxlen = sizeof(int),
427 .proc_handler = &p0_procctl,
428 .strategy = &p0_sysctl,
431 .ctl_name = CTL_PM_CM,
433 .data = &clock_cm_current,
434 .maxlen = sizeof(int),
436 .proc_handler = &cm_procctl,
437 .strategy = &cm_sysctl,
442 static struct ctl_table pm_dir_table[] =
454 * Initialize power interface
456 static int __init pm_init(void)
458 register_sysctl_table(pm_dir_table);