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))
168 retval
= pm_do_bus_sleep();
170 retval
= pm_do_suspend();
176 static int try_set_cmode(int new_cmode
)
180 if (!(clock_cmodes_permitted
& (1<<new_cmode
)))
183 /* now change cmode */
187 frv_change_cmode(new_cmode
);
195 frv_dma_resume_all();
202 static int cmode_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
203 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
208 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
210 new_cmode
= user_atoi(buffer
, *lenp
);
212 return try_set_cmode(new_cmode
)?:*lenp
;
215 static int cmode_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
216 void __user
*oldval
, size_t __user
*oldlenp
,
217 void __user
*newval
, size_t newlen
)
219 if (oldval
&& oldlenp
) {
222 if (get_user(oldlen
, oldlenp
))
225 if (oldlen
!= sizeof(int))
228 if (put_user(clock_cmode_current
, (unsigned __user
*)oldval
) ||
229 put_user(sizeof(int), oldlenp
))
232 if (newval
&& newlen
) {
235 if (newlen
!= sizeof(int))
238 if (get_user(new_cmode
, (int __user
*)newval
))
241 return try_set_cmode(new_cmode
)?:1;
246 static int try_set_p0(int new_p0
)
248 unsigned long flags
, clkc
;
250 if (new_p0
< 0 || new_p0
> 1)
253 local_irq_save(flags
);
254 __set_PSR(flags
& ~PSR_ET
);
271 frv_dma_resume_all();
272 local_irq_restore(flags
);
276 static int try_set_cm(int new_cm
)
278 unsigned long flags
, clkc
;
280 if (new_cm
< 0 || new_cm
> 1)
283 local_irq_save(flags
);
284 __set_PSR(flags
& ~PSR_ET
);
300 frv_dma_resume_all();
301 local_irq_restore(flags
);
305 static int p0_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
306 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
311 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
313 new_p0
= user_atoi(buffer
, *lenp
);
315 return try_set_p0(new_p0
)?:*lenp
;
318 static int p0_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
319 void __user
*oldval
, size_t __user
*oldlenp
,
320 void __user
*newval
, size_t newlen
)
322 if (oldval
&& oldlenp
) {
325 if (get_user(oldlen
, oldlenp
))
328 if (oldlen
!= sizeof(int))
331 if (put_user(clock_p0_current
, (unsigned __user
*)oldval
) ||
332 put_user(sizeof(int), oldlenp
))
335 if (newval
&& newlen
) {
338 if (newlen
!= sizeof(int))
341 if (get_user(new_p0
, (int __user
*)newval
))
344 return try_set_p0(new_p0
)?:1;
349 static int cm_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
350 void __user
*buffer
, size_t *lenp
, loff_t
*fpos
)
355 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
357 new_cm
= user_atoi(buffer
, *lenp
);
359 return try_set_cm(new_cm
)?:*lenp
;
362 static int cm_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
363 void __user
*oldval
, size_t __user
*oldlenp
,
364 void __user
*newval
, size_t newlen
)
366 if (oldval
&& oldlenp
) {
369 if (get_user(oldlen
, oldlenp
))
372 if (oldlen
!= sizeof(int))
375 if (put_user(clock_cm_current
, (unsigned __user
*)oldval
) ||
376 put_user(sizeof(int), oldlenp
))
379 if (newval
&& newlen
) {
382 if (newlen
!= sizeof(int))
385 if (get_user(new_cm
, (int __user
*)newval
))
388 return try_set_cm(new_cm
)?:1;
394 static struct ctl_table pm_table
[] =
397 .ctl_name
= CTL_PM_SUSPEND
,
398 .procname
= "suspend",
402 .proc_handler
= &sysctl_pm_do_suspend
,
405 .ctl_name
= CTL_PM_CMODE
,
407 .data
= &clock_cmode_current
,
408 .maxlen
= sizeof(int),
410 .proc_handler
= &cmode_procctl
,
411 .strategy
= &cmode_sysctl
,
414 .ctl_name
= CTL_PM_P0
,
416 .data
= &clock_p0_current
,
417 .maxlen
= sizeof(int),
419 .proc_handler
= &p0_procctl
,
420 .strategy
= &p0_sysctl
,
423 .ctl_name
= CTL_PM_CM
,
425 .data
= &clock_cm_current
,
426 .maxlen
= sizeof(int),
428 .proc_handler
= &cm_procctl
,
429 .strategy
= &cm_sysctl
,
434 static struct ctl_table pm_dir_table
[] =
446 * Initialize power interface
448 static int __init
pm_init(void)
450 register_sysctl_table(pm_dir_table
);