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.
129 #define CTL_PM_SUSPEND 1
130 #define CTL_PM_CMODE 2
134 static int user_atoi(char __user
*ubuf
, size_t len
)
142 if (copy_from_user(buf
, ubuf
, len
))
146 ret
= simple_strtoul(buf
, NULL
, 0);
155 static int sysctl_pm_do_suspend(ctl_table
*ctl
, int write
, struct file
*filp
,
156 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
163 mode
= user_atoi(buffer
, *lenp
);
164 if ((mode
!= 1) && (mode
!= 5))
167 retval
= pm_send_all(PM_SUSPEND
, (void *)3);
171 retval
= pm_do_bus_sleep();
173 retval
= pm_do_suspend();
174 pm_send_all(PM_RESUME
, (void *)0);
180 static int try_set_cmode(int new_cmode
)
184 if (!(clock_cmodes_permitted
& (1<<new_cmode
)))
187 /* tell all the drivers we're suspending */
188 pm_send_all(PM_SUSPEND
, (void *)3);
190 /* now change cmode */
194 frv_change_cmode(new_cmode
);
202 frv_dma_resume_all();
205 /* tell all the drivers we're resuming */
206 pm_send_all(PM_RESUME
, (void *)0);
211 static int cmode_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
212 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
217 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
219 new_cmode
= user_atoi(buffer
, *lenp
);
221 return try_set_cmode(new_cmode
)?:*lenp
;
224 static int cmode_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
225 void __user
*oldval
, size_t __user
*oldlenp
,
226 void __user
*newval
, size_t newlen
, void **context
)
228 if (oldval
&& oldlenp
) {
231 if (get_user(oldlen
, oldlenp
))
234 if (oldlen
!= sizeof(int))
237 if (put_user(clock_cmode_current
, (unsigned __user
*)oldval
) ||
238 put_user(sizeof(int), oldlenp
))
241 if (newval
&& newlen
) {
244 if (newlen
!= sizeof(int))
247 if (get_user(new_cmode
, (int __user
*)newval
))
250 return try_set_cmode(new_cmode
)?:1;
255 static int try_set_p0(int new_p0
)
257 unsigned long flags
, clkc
;
259 if (new_p0
< 0 || new_p0
> 1)
262 local_irq_save(flags
);
263 __set_PSR(flags
& ~PSR_ET
);
280 frv_dma_resume_all();
281 local_irq_restore(flags
);
285 static int try_set_cm(int new_cm
)
287 unsigned long flags
, clkc
;
289 if (new_cm
< 0 || new_cm
> 1)
292 local_irq_save(flags
);
293 __set_PSR(flags
& ~PSR_ET
);
309 frv_dma_resume_all();
310 local_irq_restore(flags
);
314 static int p0_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
315 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
320 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
322 new_p0
= user_atoi(buffer
, *lenp
);
324 return try_set_p0(new_p0
)?:*lenp
;
327 static int p0_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
328 void __user
*oldval
, size_t __user
*oldlenp
,
329 void __user
*newval
, size_t newlen
, void **context
)
331 if (oldval
&& oldlenp
) {
334 if (get_user(oldlen
, oldlenp
))
337 if (oldlen
!= sizeof(int))
340 if (put_user(clock_p0_current
, (unsigned __user
*)oldval
) ||
341 put_user(sizeof(int), oldlenp
))
344 if (newval
&& newlen
) {
347 if (newlen
!= sizeof(int))
350 if (get_user(new_p0
, (int __user
*)newval
))
353 return try_set_p0(new_p0
)?:1;
358 static int cm_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
359 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
364 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
366 new_cm
= user_atoi(buffer
, *lenp
);
368 return try_set_cm(new_cm
)?:*lenp
;
371 static int cm_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
372 void __user
*oldval
, size_t __user
*oldlenp
,
373 void __user
*newval
, size_t newlen
, void **context
)
375 if (oldval
&& oldlenp
) {
378 if (get_user(oldlen
, oldlenp
))
381 if (oldlen
!= sizeof(int))
384 if (put_user(clock_cm_current
, (unsigned __user
*)oldval
) ||
385 put_user(sizeof(int), oldlenp
))
388 if (newval
&& newlen
) {
391 if (newlen
!= sizeof(int))
394 if (get_user(new_cm
, (int __user
*)newval
))
397 return try_set_cm(new_cm
)?:1;
403 static struct ctl_table pm_table
[] =
405 {CTL_PM_SUSPEND
, "suspend", NULL
, 0, 0200, NULL
, &sysctl_pm_do_suspend
},
406 {CTL_PM_CMODE
, "cmode", &clock_cmode_current
, sizeof(int), 0644, NULL
, &cmode_procctl
, &cmode_sysctl
, NULL
},
407 {CTL_PM_P0
, "p0", &clock_p0_current
, sizeof(int), 0644, NULL
, &p0_procctl
, &p0_sysctl
, NULL
},
408 {CTL_PM_CM
, "cm", &clock_cm_current
, sizeof(int), 0644, NULL
, &cm_procctl
, &cm_sysctl
, NULL
},
412 static struct ctl_table pm_dir_table
[] =
414 {CTL_PM
, "pm", NULL
, 0, 0555, pm_table
},
419 * Initialize power interface
421 static int __init
pm_init(void)
423 register_sysctl_table(pm_dir_table
, 1);