Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[sfrench/cifs-2.6.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         retval = pm_send_all(PM_SUSPEND, (void *)3);
167
168         if (retval == 0) {
169                 if (mode == 5)
170                     retval = pm_do_bus_sleep();
171                 else
172                     retval = pm_do_suspend();
173                 pm_send_all(PM_RESUME, (void *)0);
174         }
175
176         return retval;
177 }
178
179 static int try_set_cmode(int new_cmode)
180 {
181         if (new_cmode > 15)
182                 return -EINVAL;
183         if (!(clock_cmodes_permitted & (1<<new_cmode)))
184                 return -EINVAL;
185
186         /* tell all the drivers we're suspending */
187         pm_send_all(PM_SUSPEND, (void *)3);
188
189         /* now change cmode */
190         local_irq_disable();
191         frv_dma_pause_all();
192
193         frv_change_cmode(new_cmode);
194
195         determine_clocks(0);
196         time_divisor_init();
197
198 #ifdef DEBUG
199         determine_clocks(1);
200 #endif
201         frv_dma_resume_all();
202         local_irq_enable();
203
204         /* tell all the drivers we're resuming */
205         pm_send_all(PM_RESUME, (void *)0);
206         return 0;
207 }
208
209
210 static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
211                          void __user *buffer, size_t *lenp, loff_t *fpos)
212 {
213         int new_cmode;
214
215         if (!write)
216                 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
217
218         new_cmode = user_atoi(buffer, *lenp);
219
220         return try_set_cmode(new_cmode)?:*lenp;
221 }
222
223 static int cmode_sysctl(ctl_table *table, int __user *name, int nlen,
224                         void __user *oldval, size_t __user *oldlenp,
225                         void __user *newval, size_t newlen)
226 {
227         if (oldval && oldlenp) {
228                 size_t oldlen;
229
230                 if (get_user(oldlen, oldlenp))
231                         return -EFAULT;
232
233                 if (oldlen != sizeof(int))
234                         return -EINVAL;
235
236                 if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
237                     put_user(sizeof(int), oldlenp))
238                         return -EFAULT;
239         }
240         if (newval && newlen) {
241                 int new_cmode;
242
243                 if (newlen != sizeof(int))
244                         return -EINVAL;
245
246                 if (get_user(new_cmode, (int __user *)newval))
247                         return -EFAULT;
248
249                 return try_set_cmode(new_cmode)?:1;
250         }
251         return 1;
252 }
253
254 static int try_set_p0(int new_p0)
255 {
256         unsigned long flags, clkc;
257
258         if (new_p0 < 0 || new_p0 > 1)
259                 return -EINVAL;
260
261         local_irq_save(flags);
262         __set_PSR(flags & ~PSR_ET);
263
264         frv_dma_pause_all();
265
266         clkc = __get_CLKC();
267         if (new_p0)
268                 clkc |= CLKC_P0;
269         else
270                 clkc &= ~CLKC_P0;
271         __set_CLKC(clkc);
272
273         determine_clocks(0);
274         time_divisor_init();
275
276 #ifdef DEBUG
277         determine_clocks(1);
278 #endif
279         frv_dma_resume_all();
280         local_irq_restore(flags);
281         return 0;
282 }
283
284 static int try_set_cm(int new_cm)
285 {
286         unsigned long flags, clkc;
287
288         if (new_cm < 0 || new_cm > 1)
289                 return -EINVAL;
290
291         local_irq_save(flags);
292         __set_PSR(flags & ~PSR_ET);
293
294         frv_dma_pause_all();
295
296         clkc = __get_CLKC();
297         clkc &= ~CLKC_CM;
298         clkc |= new_cm;
299         __set_CLKC(clkc);
300
301         determine_clocks(0);
302         time_divisor_init();
303
304 #if 1 //def DEBUG
305         determine_clocks(1);
306 #endif
307
308         frv_dma_resume_all();
309         local_irq_restore(flags);
310         return 0;
311 }
312
313 static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
314                       void __user *buffer, size_t *lenp, loff_t *fpos)
315 {
316         int new_p0;
317
318         if (!write)
319                 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
320
321         new_p0 = user_atoi(buffer, *lenp);
322
323         return try_set_p0(new_p0)?:*lenp;
324 }
325
326 static int p0_sysctl(ctl_table *table, int __user *name, int nlen,
327                      void __user *oldval, size_t __user *oldlenp,
328                      void __user *newval, size_t newlen)
329 {
330         if (oldval && oldlenp) {
331                 size_t oldlen;
332
333                 if (get_user(oldlen, oldlenp))
334                         return -EFAULT;
335
336                 if (oldlen != sizeof(int))
337                         return -EINVAL;
338
339                 if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
340                     put_user(sizeof(int), oldlenp))
341                         return -EFAULT;
342         }
343         if (newval && newlen) {
344                 int new_p0;
345
346                 if (newlen != sizeof(int))
347                         return -EINVAL;
348
349                 if (get_user(new_p0, (int __user *)newval))
350                         return -EFAULT;
351
352                 return try_set_p0(new_p0)?:1;
353         }
354         return 1;
355 }
356
357 static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
358                       void __user *buffer, size_t *lenp, loff_t *fpos)
359 {
360         int new_cm;
361
362         if (!write)
363                 return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
364
365         new_cm = user_atoi(buffer, *lenp);
366
367         return try_set_cm(new_cm)?:*lenp;
368 }
369
370 static int cm_sysctl(ctl_table *table, int __user *name, int nlen,
371                      void __user *oldval, size_t __user *oldlenp,
372                      void __user *newval, size_t newlen)
373 {
374         if (oldval && oldlenp) {
375                 size_t oldlen;
376
377                 if (get_user(oldlen, oldlenp))
378                         return -EFAULT;
379
380                 if (oldlen != sizeof(int))
381                         return -EINVAL;
382
383                 if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
384                     put_user(sizeof(int), oldlenp))
385                         return -EFAULT;
386         }
387         if (newval && newlen) {
388                 int new_cm;
389
390                 if (newlen != sizeof(int))
391                         return -EINVAL;
392
393                 if (get_user(new_cm, (int __user *)newval))
394                         return -EFAULT;
395
396                 return try_set_cm(new_cm)?:1;
397         }
398         return 1;
399 }
400
401
402 static struct ctl_table pm_table[] =
403 {
404         {
405                 .ctl_name       = CTL_PM_SUSPEND,
406                 .procname       = "suspend",
407                 .data           = NULL,
408                 .maxlen         = 0,
409                 .mode           = 0200,
410                 .proc_handler   = &sysctl_pm_do_suspend,
411         },
412         {
413                 .ctl_name       = CTL_PM_CMODE,
414                 .procname       = "cmode",
415                 .data           = &clock_cmode_current,
416                 .maxlen         = sizeof(int),
417                 .mode           = 0644,
418                 .proc_handler   = &cmode_procctl,
419                 .strategy       = &cmode_sysctl,
420         },
421         {
422                 .ctl_name       = CTL_PM_P0,
423                 .procname       = "p0",
424                 .data           = &clock_p0_current,
425                 .maxlen         = sizeof(int),
426                 .mode           = 0644,
427                 .proc_handler   = &p0_procctl,
428                 .strategy       = &p0_sysctl,
429         },
430         {
431                 .ctl_name       = CTL_PM_CM,
432                 .procname       = "cm",
433                 .data           = &clock_cm_current,
434                 .maxlen         = sizeof(int),
435                 .mode           = 0644,
436                 .proc_handler   = &cm_procctl,
437                 .strategy       = &cm_sysctl,
438         },
439         { .ctl_name = 0}
440 };
441
442 static struct ctl_table pm_dir_table[] =
443 {
444         {
445                 .ctl_name       = CTL_PM,
446                 .procname       = "pm",
447                 .mode           = 0555,
448                 .child          = pm_table,
449         },
450         { .ctl_name = 0}
451 };
452
453 /*
454  * Initialize power interface
455  */
456 static int __init pm_init(void)
457 {
458         register_sysctl_table(pm_dir_table);
459         return 0;
460 }
461
462 __initcall(pm_init);
463
464 #endif