Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[pandora-kernel.git] / arch / frv / kernel / pm.c
1 /*
2  * FR-V Power Management Routines
3  *
4  * Copyright (c) 2004 Red Hat, Inc.
5  *
6  * Based on SA1100 version:
7  * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License.
11  *
12  */
13
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/pm.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>
24
25 #include <asm/mb86943a.h>
26
27 #include "local.h"
28
29 /*
30  * Debug macros
31  */
32 #define DEBUG
33
34 int pm_do_suspend(void)
35 {
36         local_irq_disable();
37
38         __set_LEDS(0xb1);
39
40         /* go zzz */
41         frv_cpu_suspend(pdm_suspend_mode);
42
43         __set_LEDS(0xb2);
44
45         local_irq_enable();
46
47         return 0;
48 }
49
50 static unsigned long __irq_mask;
51
52 /*
53  * Setup interrupt masks, etc to enable wakeup by power switch
54  */
55 static void __default_power_switch_setup(void)
56 {
57         /* default is to mask all interrupt sources. */
58         __irq_mask = *(unsigned long *)0xfeff9820;
59         *(unsigned long *)0xfeff9820 = 0xfffe0000;
60 }
61
62 /*
63  * Cleanup interrupt masks, etc after wakeup by power switch
64  */
65 static void __default_power_switch_cleanup(void)
66 {
67         *(unsigned long *)0xfeff9820 = __irq_mask;
68 }
69
70 /*
71  * Return non-zero if wakeup irq was caused by power switch
72  */
73 static int __default_power_switch_check(void)
74 {
75         return 1;
76 }
77
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;
81
82 int pm_do_bus_sleep(void)
83 {
84         local_irq_disable();
85
86         /*
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.
90          */
91         __power_switch_wake_setup();
92
93         __set_LEDS(0xa1);
94
95         /* go zzz
96          *
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.
100          */
101         do {
102                 frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
103         } while (__power_switch_wake_check && !__power_switch_wake_check());
104
105         __set_LEDS(0xa2);
106
107         /*
108          * Here is where we need some platform-dependent restore
109          * of the interrupt state prior to being called.
110          */
111         __power_switch_wake_cleanup();
112
113         local_irq_enable();
114
115         return 0;
116 }
117
118 unsigned long sleep_phys_sp(void *sp)
119 {
120         return virt_to_phys(sp);
121 }
122
123 #ifdef CONFIG_SYSCTL
124 /*
125  * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
126  * when all the PM interfaces exist nicely.
127  */
128 #define CTL_PM_SUSPEND 1
129 #define CTL_PM_CMODE 2
130 #define CTL_PM_P0 4
131 #define CTL_PM_CM 5
132
133 static int user_atoi(char __user *ubuf, size_t len)
134 {
135         char buf[16];
136         unsigned long ret;
137
138         if (len > 15)
139                 return -EINVAL;
140
141         if (copy_from_user(buf, ubuf, len))
142                 return -EFAULT;
143
144         buf[len] = 0;
145         ret = simple_strtoul(buf, NULL, 0);
146         if (ret > INT_MAX)
147                 return -ERANGE;
148         return ret;
149 }
150
151 /*
152  * Send us to sleep.
153  */
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)
156 {
157         int retval, mode;
158
159         if (*lenp <= 0)
160                 return -EIO;
161
162         mode = user_atoi(buffer, *lenp);
163         if ((mode != 1) && (mode != 5))
164                 return -EINVAL;
165
166         if (retval == 0) {
167                 if (mode == 5)
168                     retval = pm_do_bus_sleep();
169                 else
170                     retval = pm_do_suspend();
171         }
172
173         return retval;
174 }
175
176 static int try_set_cmode(int new_cmode)
177 {
178         if (new_cmode > 15)
179                 return -EINVAL;
180         if (!(clock_cmodes_permitted & (1<<new_cmode)))
181                 return -EINVAL;
182
183         /* now change cmode */
184         local_irq_disable();
185         frv_dma_pause_all();
186
187         frv_change_cmode(new_cmode);
188
189         determine_clocks(0);
190         time_divisor_init();
191
192 #ifdef DEBUG
193         determine_clocks(1);
194 #endif
195         frv_dma_resume_all();
196         local_irq_enable();
197
198         return 0;
199 }
200
201
202 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
203                          void __user *buffer, size_t *lenp, loff_t *fpos)
204 {
205         int new_cmode;
206
207         if (!write)
208                 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
209
210         new_cmode = user_atoi(buffer, *lenp);
211
212         return try_set_cmode(new_cmode)?:*lenp;
213 }
214
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)
218 {
219         if (oldval && oldlenp) {
220                 size_t oldlen;
221
222                 if (get_user(oldlen, oldlenp))
223                         return -EFAULT;
224
225                 if (oldlen != sizeof(int))
226                         return -EINVAL;
227
228                 if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
229                     put_user(sizeof(int), oldlenp))
230                         return -EFAULT;
231         }
232         if (newval && newlen) {
233                 int new_cmode;
234
235                 if (newlen != sizeof(int))
236                         return -EINVAL;
237
238                 if (get_user(new_cmode, (int __user *)newval))
239                         return -EFAULT;
240
241                 return try_set_cmode(new_cmode)?:1;
242         }
243         return 1;
244 }
245
246 static int try_set_p0(int new_p0)
247 {
248         unsigned long flags, clkc;
249
250         if (new_p0 < 0 || new_p0 > 1)
251                 return -EINVAL;
252
253         local_irq_save(flags);
254         __set_PSR(flags & ~PSR_ET);
255
256         frv_dma_pause_all();
257
258         clkc = __get_CLKC();
259         if (new_p0)
260                 clkc |= CLKC_P0;
261         else
262                 clkc &= ~CLKC_P0;
263         __set_CLKC(clkc);
264
265         determine_clocks(0);
266         time_divisor_init();
267
268 #ifdef DEBUG
269         determine_clocks(1);
270 #endif
271         frv_dma_resume_all();
272         local_irq_restore(flags);
273         return 0;
274 }
275
276 static int try_set_cm(int new_cm)
277 {
278         unsigned long flags, clkc;
279
280         if (new_cm < 0 || new_cm > 1)
281                 return -EINVAL;
282
283         local_irq_save(flags);
284         __set_PSR(flags & ~PSR_ET);
285
286         frv_dma_pause_all();
287
288         clkc = __get_CLKC();
289         clkc &= ~CLKC_CM;
290         clkc |= new_cm;
291         __set_CLKC(clkc);
292
293         determine_clocks(0);
294         time_divisor_init();
295
296 #if 1 //def DEBUG
297         determine_clocks(1);
298 #endif
299
300         frv_dma_resume_all();
301         local_irq_restore(flags);
302         return 0;
303 }
304
305 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
306                       void __user *buffer, size_t *lenp, loff_t *fpos)
307 {
308         int new_p0;
309
310         if (!write)
311                 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
312
313         new_p0 = user_atoi(buffer, *lenp);
314
315         return try_set_p0(new_p0)?:*lenp;
316 }
317
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)
321 {
322         if (oldval && oldlenp) {
323                 size_t oldlen;
324
325                 if (get_user(oldlen, oldlenp))
326                         return -EFAULT;
327
328                 if (oldlen != sizeof(int))
329                         return -EINVAL;
330
331                 if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
332                     put_user(sizeof(int), oldlenp))
333                         return -EFAULT;
334         }
335         if (newval && newlen) {
336                 int new_p0;
337
338                 if (newlen != sizeof(int))
339                         return -EINVAL;
340
341                 if (get_user(new_p0, (int __user *)newval))
342                         return -EFAULT;
343
344                 return try_set_p0(new_p0)?:1;
345         }
346         return 1;
347 }
348
349 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
350                       void __user *buffer, size_t *lenp, loff_t *fpos)
351 {
352         int new_cm;
353
354         if (!write)
355                 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
356
357         new_cm = user_atoi(buffer, *lenp);
358
359         return try_set_cm(new_cm)?:*lenp;
360 }
361
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)
365 {
366         if (oldval && oldlenp) {
367                 size_t oldlen;
368
369                 if (get_user(oldlen, oldlenp))
370                         return -EFAULT;
371
372                 if (oldlen != sizeof(int))
373                         return -EINVAL;
374
375                 if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
376                     put_user(sizeof(int), oldlenp))
377                         return -EFAULT;
378         }
379         if (newval && newlen) {
380                 int new_cm;
381
382                 if (newlen != sizeof(int))
383                         return -EINVAL;
384
385                 if (get_user(new_cm, (int __user *)newval))
386                         return -EFAULT;
387
388                 return try_set_cm(new_cm)?:1;
389         }
390         return 1;
391 }
392
393
394 static struct ctl_table pm_table[] =
395 {
396         {
397                 .ctl_name       = CTL_PM_SUSPEND,
398                 .procname       = "suspend",
399                 .data           = NULL,
400                 .maxlen         = 0,
401                 .mode           = 0200,
402                 .proc_handler   = &sysctl_pm_do_suspend,
403         },
404         {
405                 .ctl_name       = CTL_PM_CMODE,
406                 .procname       = "cmode",
407                 .data           = &clock_cmode_current,
408                 .maxlen         = sizeof(int),
409                 .mode           = 0644,
410                 .proc_handler   = &cmode_procctl,
411                 .strategy       = &cmode_sysctl,
412         },
413         {
414                 .ctl_name       = CTL_PM_P0,
415                 .procname       = "p0",
416                 .data           = &clock_p0_current,
417                 .maxlen         = sizeof(int),
418                 .mode           = 0644,
419                 .proc_handler   = &p0_procctl,
420                 .strategy       = &p0_sysctl,
421         },
422         {
423                 .ctl_name       = CTL_PM_CM,
424                 .procname       = "cm",
425                 .data           = &clock_cm_current,
426                 .maxlen         = sizeof(int),
427                 .mode           = 0644,
428                 .proc_handler   = &cm_procctl,
429                 .strategy       = &cm_sysctl,
430         },
431         { .ctl_name = 0}
432 };
433
434 static struct ctl_table pm_dir_table[] =
435 {
436         {
437                 .ctl_name       = CTL_PM,
438                 .procname       = "pm",
439                 .mode           = 0555,
440                 .child          = pm_table,
441         },
442         { .ctl_name = 0}
443 };
444
445 /*
446  * Initialize power interface
447  */
448 static int __init pm_init(void)
449 {
450         register_sysctl_table(pm_dir_table);
451         return 0;
452 }
453
454 __initcall(pm_init);
455
456 #endif