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