PageRenderTime 41ms CodeModel.GetById 12ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/frv/kernel/pm.c

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