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/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/sysctl.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <asm/uaccess.h>
24 #include <asm/mb86943a.h>
33 int pm_do_suspend(void)
40 frv_cpu_suspend(pdm_suspend_mode
);
49 static unsigned long __irq_mask
;
52 * Setup interrupt masks, etc to enable wakeup by power switch
54 static void __default_power_switch_setup(void)
56 /* default is to mask all interrupt sources. */
57 __irq_mask
= *(unsigned long *)0xfeff9820;
58 *(unsigned long *)0xfeff9820 = 0xfffe0000;
62 * Cleanup interrupt masks, etc after wakeup by power switch
64 static void __default_power_switch_cleanup(void)
66 *(unsigned long *)0xfeff9820 = __irq_mask
;
70 * Return non-zero if wakeup irq was caused by power switch
72 static int __default_power_switch_check(void)
77 void (*__power_switch_wake_setup
)(void) = __default_power_switch_setup
;
78 int (*__power_switch_wake_check
)(void) = __default_power_switch_check
;
79 void (*__power_switch_wake_cleanup
)(void) = __default_power_switch_cleanup
;
81 int pm_do_bus_sleep(void)
86 * Here is where we need some platform-dependent setup
87 * of the interrupt state so that appropriate wakeup
88 * sources are allowed and all others are masked.
90 __power_switch_wake_setup();
96 * This is in a loop in case power switch shares an irq with other
97 * devices. The wake_check() tells us if we need to finish waking
98 * or go back to sleep.
101 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP
);
102 } while (__power_switch_wake_check
&& !__power_switch_wake_check());
107 * Here is where we need some platform-dependent restore
108 * of the interrupt state prior to being called.
110 __power_switch_wake_cleanup();
117 unsigned long sleep_phys_sp(void *sp
)
119 return virt_to_phys(sp
);
124 * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
125 * when all the PM interfaces exist nicely.
127 #define CTL_PM_SUSPEND 1
128 #define CTL_PM_CMODE 2
132 static int user_atoi(char __user
*ubuf
, size_t len
)
140 if (copy_from_user(buf
, ubuf
, len
))
144 ret
= simple_strtoul(buf
, NULL
, 0);
153 static int sysctl_pm_do_suspend(ctl_table
*ctl
, int write
, struct file
*filp
,
154 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
161 mode
= user_atoi(buffer
, *lenp
);
162 if ((mode
!= 1) && (mode
!= 5))
167 retval
= pm_do_bus_sleep();
169 retval
= pm_do_suspend();
175 static int try_set_cmode(int new_cmode
)
179 if (!(clock_cmodes_permitted
& (1<<new_cmode
)))
182 /* now change cmode */
186 frv_change_cmode(new_cmode
);
194 frv_dma_resume_all();
201 static int cmode_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
202 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
207 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
209 new_cmode
= user_atoi(buffer
, *lenp
);
211 return try_set_cmode(new_cmode
)?:*lenp
;
214 static int cmode_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
215 void __user
*oldval
, size_t __user
*oldlenp
,
216 void __user
*newval
, size_t newlen
)
218 if (oldval
&& oldlenp
) {
221 if (get_user(oldlen
, oldlenp
))
224 if (oldlen
!= sizeof(int))
227 if (put_user(clock_cmode_current
, (unsigned __user
*)oldval
) ||
228 put_user(sizeof(int), oldlenp
))
231 if (newval
&& newlen
) {
234 if (newlen
!= sizeof(int))
237 if (get_user(new_cmode
, (int __user
*)newval
))
240 return try_set_cmode(new_cmode
)?:1;
245 static int try_set_p0(int new_p0
)
247 unsigned long flags
, clkc
;
249 if (new_p0
< 0 || new_p0
> 1)
252 local_irq_save(flags
);
253 __set_PSR(flags
& ~PSR_ET
);
270 frv_dma_resume_all();
271 local_irq_restore(flags
);
275 static int try_set_cm(int new_cm
)
277 unsigned long flags
, clkc
;
279 if (new_cm
< 0 || new_cm
> 1)
282 local_irq_save(flags
);
283 __set_PSR(flags
& ~PSR_ET
);
299 frv_dma_resume_all();
300 local_irq_restore(flags
);
304 static int p0_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
305 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
310 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
312 new_p0
= user_atoi(buffer
, *lenp
);
314 return try_set_p0(new_p0
)?:*lenp
;
317 static int p0_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
318 void __user
*oldval
, size_t __user
*oldlenp
,
319 void __user
*newval
, size_t newlen
)
321 if (oldval
&& oldlenp
) {
324 if (get_user(oldlen
, oldlenp
))
327 if (oldlen
!= sizeof(int))
330 if (put_user(clock_p0_current
, (unsigned __user
*)oldval
) ||
331 put_user(sizeof(int), oldlenp
))
334 if (newval
&& newlen
) {
337 if (newlen
!= sizeof(int))
340 if (get_user(new_p0
, (int __user
*)newval
))
343 return try_set_p0(new_p0
)?:1;
348 static int cm_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
349 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
354 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
356 new_cm
= user_atoi(buffer
, *lenp
);
358 return try_set_cm(new_cm
)?:*lenp
;
361 static int cm_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
362 void __user
*oldval
, size_t __user
*oldlenp
,
363 void __user
*newval
, size_t newlen
)
365 if (oldval
&& oldlenp
) {
368 if (get_user(oldlen
, oldlenp
))
371 if (oldlen
!= sizeof(int))
374 if (put_user(clock_cm_current
, (unsigned __user
*)oldval
) ||
375 put_user(sizeof(int), oldlenp
))
378 if (newval
&& newlen
) {
381 if (newlen
!= sizeof(int))
384 if (get_user(new_cm
, (int __user
*)newval
))
387 return try_set_cm(new_cm
)?:1;
393 static struct ctl_table pm_table
[] =
396 .ctl_name
= CTL_PM_SUSPEND
,
397 .procname
= "suspend",
401 .proc_handler
= &sysctl_pm_do_suspend
,
404 .ctl_name
= CTL_PM_CMODE
,
406 .data
= &clock_cmode_current
,
407 .maxlen
= sizeof(int),
409 .proc_handler
= &cmode_procctl
,
410 .strategy
= &cmode_sysctl
,
413 .ctl_name
= CTL_PM_P0
,
415 .data
= &clock_p0_current
,
416 .maxlen
= sizeof(int),
418 .proc_handler
= &p0_procctl
,
419 .strategy
= &p0_sysctl
,
422 .ctl_name
= CTL_PM_CM
,
424 .data
= &clock_cm_current
,
425 .maxlen
= sizeof(int),
427 .proc_handler
= &cm_procctl
,
428 .strategy
= &cm_sysctl
,
433 static struct ctl_table pm_dir_table
[] =
445 * Initialize power interface
447 static int __init
pm_init(void)
449 register_sysctl_table(pm_dir_table
);