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/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>
28 void (*pm_power_off
)(void);
30 extern void frv_change_cmode(int);
37 int pm_do_suspend(void)
44 frv_cpu_suspend(pdm_suspend_mode
);
53 static unsigned long __irq_mask
;
56 * Setup interrupt masks, etc to enable wakeup by power switch
58 static void __default_power_switch_setup(void)
60 /* default is to mask all interrupt sources. */
61 __irq_mask
= *(unsigned long *)0xfeff9820;
62 *(unsigned long *)0xfeff9820 = 0xfffe0000;
66 * Cleanup interrupt masks, etc after wakeup by power switch
68 static void __default_power_switch_cleanup(void)
70 *(unsigned long *)0xfeff9820 = __irq_mask
;
74 * Return non-zero if wakeup irq was caused by power switch
76 static int __default_power_switch_check(void)
81 void (*__power_switch_wake_setup
)(void) = __default_power_switch_setup
;
82 int (*__power_switch_wake_check
)(void) = __default_power_switch_check
;
83 void (*__power_switch_wake_cleanup
)(void) = __default_power_switch_cleanup
;
85 int pm_do_bus_sleep(void)
90 * Here is where we need some platform-dependent setup
91 * of the interrupt state so that appropriate wakeup
92 * sources are allowed and all others are masked.
94 __power_switch_wake_setup();
100 * This is in a loop in case power switch shares an irq with other
101 * devices. The wake_check() tells us if we need to finish waking
102 * or go back to sleep.
105 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP
);
106 } while (__power_switch_wake_check
&& !__power_switch_wake_check());
111 * Here is where we need some platform-dependent restore
112 * of the interrupt state prior to being called.
114 __power_switch_wake_cleanup();
121 unsigned long sleep_phys_sp(void *sp
)
123 return virt_to_phys(sp
);
128 * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
129 * when all the PM interfaces exist nicely.
132 #define CTL_PM_SUSPEND 1
133 #define CTL_PM_CMODE 2
137 static int user_atoi(char *ubuf
, size_t len
)
145 if (copy_from_user(buf
, ubuf
, len
))
149 ret
= simple_strtoul(buf
, NULL
, 0);
158 static int sysctl_pm_do_suspend(ctl_table
*ctl
, int write
, struct file
*filp
,
159 void *buffer
, size_t *lenp
, loff_t
*fpos
)
166 mode
= user_atoi(buffer
, *lenp
);
167 if ((mode
!= 1) && (mode
!= 5))
170 retval
= pm_send_all(PM_SUSPEND
, (void *)3);
174 retval
= pm_do_bus_sleep();
176 retval
= pm_do_suspend();
177 pm_send_all(PM_RESUME
, (void *)0);
183 static int try_set_cmode(int new_cmode
)
187 if (!(clock_cmodes_permitted
& (1<<new_cmode
)))
190 /* tell all the drivers we're suspending */
191 pm_send_all(PM_SUSPEND
, (void *)3);
193 /* now change cmode */
197 frv_change_cmode(new_cmode
);
205 frv_dma_resume_all();
208 /* tell all the drivers we're resuming */
209 pm_send_all(PM_RESUME
, (void *)0);
214 static int cmode_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
215 void *buffer
, size_t *lenp
, loff_t
*fpos
)
220 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
222 new_cmode
= user_atoi(buffer
, *lenp
);
224 return try_set_cmode(new_cmode
)?:*lenp
;
227 static int cmode_sysctl(ctl_table
*table
, int *name
, int nlen
,
228 void *oldval
, size_t *oldlenp
,
229 void *newval
, size_t newlen
, void **context
)
231 if (oldval
&& oldlenp
) {
234 if (get_user(oldlen
, oldlenp
))
237 if (oldlen
!= sizeof(int))
240 if (put_user(clock_cmode_current
, (unsigned int *)oldval
) ||
241 put_user(sizeof(int), oldlenp
))
244 if (newval
&& newlen
) {
247 if (newlen
!= sizeof(int))
250 if (get_user(new_cmode
, (int *)newval
))
253 return try_set_cmode(new_cmode
)?:1;
258 static int try_set_p0(int new_p0
)
260 unsigned long flags
, clkc
;
262 if (new_p0
< 0 || new_p0
> 1)
265 local_irq_save(flags
);
266 __set_PSR(flags
& ~PSR_ET
);
283 frv_dma_resume_all();
284 local_irq_restore(flags
);
288 static int try_set_cm(int new_cm
)
290 unsigned long flags
, clkc
;
292 if (new_cm
< 0 || new_cm
> 1)
295 local_irq_save(flags
);
296 __set_PSR(flags
& ~PSR_ET
);
312 frv_dma_resume_all();
313 local_irq_restore(flags
);
317 static int p0_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
318 void *buffer
, size_t *lenp
, loff_t
*fpos
)
323 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
325 new_p0
= user_atoi(buffer
, *lenp
);
327 return try_set_p0(new_p0
)?:*lenp
;
330 static int p0_sysctl(ctl_table
*table
, int *name
, int nlen
,
331 void *oldval
, size_t *oldlenp
,
332 void *newval
, size_t newlen
, void **context
)
334 if (oldval
&& oldlenp
) {
337 if (get_user(oldlen
, oldlenp
))
340 if (oldlen
!= sizeof(int))
343 if (put_user(clock_p0_current
, (unsigned int *)oldval
) ||
344 put_user(sizeof(int), oldlenp
))
347 if (newval
&& newlen
) {
350 if (newlen
!= sizeof(int))
353 if (get_user(new_p0
, (int *)newval
))
356 return try_set_p0(new_p0
)?:1;
361 static int cm_procctl(ctl_table
*ctl
, int write
, struct file
*filp
,
362 void *buffer
, size_t *lenp
, loff_t
*fpos
)
367 return proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, fpos
);
369 new_cm
= user_atoi(buffer
, *lenp
);
371 return try_set_cm(new_cm
)?:*lenp
;
374 static int cm_sysctl(ctl_table
*table
, int *name
, int nlen
,
375 void *oldval
, size_t *oldlenp
,
376 void *newval
, size_t newlen
, void **context
)
378 if (oldval
&& oldlenp
) {
381 if (get_user(oldlen
, oldlenp
))
384 if (oldlen
!= sizeof(int))
387 if (put_user(clock_cm_current
, (unsigned int *)oldval
) ||
388 put_user(sizeof(int), oldlenp
))
391 if (newval
&& newlen
) {
394 if (newlen
!= sizeof(int))
397 if (get_user(new_cm
, (int *)newval
))
400 return try_set_cm(new_cm
)?:1;
406 static struct ctl_table pm_table
[] =
408 {CTL_PM_SUSPEND
, "suspend", NULL
, 0, 0200, NULL
, &sysctl_pm_do_suspend
},
409 {CTL_PM_CMODE
, "cmode", &clock_cmode_current
, sizeof(int), 0644, NULL
, &cmode_procctl
, &cmode_sysctl
, NULL
},
410 {CTL_PM_P0
, "p0", &clock_p0_current
, sizeof(int), 0644, NULL
, &p0_procctl
, &p0_sysctl
, NULL
},
411 {CTL_PM_CM
, "cm", &clock_cm_current
, sizeof(int), 0644, NULL
, &cm_procctl
, &cm_sysctl
, NULL
},
415 static struct ctl_table pm_dir_table
[] =
417 {CTL_PM
, "pm", NULL
, 0, 0555, pm_table
},
422 * Initialize power interface
424 static int __init
pm_init(void)
426 register_sysctl_table(pm_dir_table
, 1);