blob: 33a3cdb7444a8b83bd337a65ad6b78ff7c114c71 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * BRIEF MODULE DESCRIPTION
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +04003 * Au1xx0 Power Management routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +04005 * Copyright 2001, 2008 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc. <source@mvista.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Some of the routines are right out of init/main.c, whose
9 * copyrights apply here.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
Sergei Shtylyovce28f942008-04-23 22:43:55 +040031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
33#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sysctl.h>
Pete Popov3ce86ee2005-07-19 07:05:36 +000035#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
Pete Popov3ce86ee2005-07-19 07:05:36 +000038#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/mach-au1x00/au1000.h>
40
41#ifdef CONFIG_PM
42
43#define DEBUG 1
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040044#ifdef DEBUG
45#define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#else
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040047#define DPRINTK(fmt, args...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#endif
49
Pete Popov3ce86ee2005-07-19 07:05:36 +000050static void au1000_calibrate_delay(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052extern unsigned long save_local_and_disable(int controller);
53extern void restore_local_and_enable(int controller, unsigned long mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static DEFINE_SPINLOCK(pm_lock);
56
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040057/*
58 * We need to save/restore a bunch of core registers that are
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * either volatile or reset to some state across a processor sleep.
60 * If reading a register doesn't provide a proper result for a
61 * later restore, we have to provide a function for loading that
62 * register and save a copy.
63 *
64 * We only have to save/restore registers that aren't otherwise
65 * done as part of a driver pm_* function.
66 */
Ralf Baechlefc103342006-06-28 11:24:12 +010067static unsigned int sleep_aux_pll_cntrl;
68static unsigned int sleep_cpu_pll_cntrl;
69static unsigned int sleep_pin_function;
70static unsigned int sleep_uart0_inten;
71static unsigned int sleep_uart0_fifoctl;
72static unsigned int sleep_uart0_linectl;
73static unsigned int sleep_uart0_clkdiv;
74static unsigned int sleep_uart0_enable;
75static unsigned int sleep_usbhost_enable;
76static unsigned int sleep_usbdev_enable;
77static unsigned int sleep_static_memctlr[4][3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040079/*
80 * Define this to cause the value you write to /proc/sys/pm/sleep to
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * set the TOY timer for the amount of time you want to sleep.
82 * This is done mainly for testing, but may be useful in other cases.
83 * The value is number of 32KHz ticks to sleep.
84 */
85#define SLEEP_TEST_TIMEOUT 1
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040086#ifdef SLEEP_TEST_TIMEOUT
87static int sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088void wakeup_counter0_set(int ticks);
89#endif
90
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040091static void save_core_regs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 extern void save_au1xxx_intctl(void);
94 extern void pm_eth0_shutdown(void);
95
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040096 /*
97 * Do the serial ports.....these really should be a pm_*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * registered function by the driver......but of course the
99 * standard serial driver doesn't understand our Au1xxx
100 * unique registers.
101 */
102 sleep_uart0_inten = au_readl(UART0_ADDR + UART_IER);
103 sleep_uart0_fifoctl = au_readl(UART0_ADDR + UART_FCR);
104 sleep_uart0_linectl = au_readl(UART0_ADDR + UART_LCR);
105 sleep_uart0_clkdiv = au_readl(UART0_ADDR + UART_CLK);
106 sleep_uart0_enable = au_readl(UART0_ADDR + UART_MOD_CNTRL);
107
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400108 /* Shutdown USB host/device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 sleep_usbhost_enable = au_readl(USB_HOST_CONFIG);
110
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400111 /* There appears to be some undocumented reset register.... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 au_writel(0, 0xb0100004); au_sync();
113 au_writel(0, USB_HOST_CONFIG); au_sync();
114
115 sleep_usbdev_enable = au_readl(USBD_ENABLE);
116 au_writel(0, USBD_ENABLE); au_sync();
117
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400118 /* Save interrupt controller state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 save_au1xxx_intctl();
120
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400121 /* Clocks and PLLs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 sleep_aux_pll_cntrl = au_readl(SYS_AUXPLL);
123
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400124 /*
125 * We don't really need to do this one, but unless we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * write it again it won't have a valid value if we
127 * happen to read it.
128 */
129 sleep_cpu_pll_cntrl = au_readl(SYS_CPUPLL);
130
131 sleep_pin_function = au_readl(SYS_PINFUNC);
132
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400133 /* Save the static memory controller configuration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 sleep_static_memctlr[0][0] = au_readl(MEM_STCFG0);
135 sleep_static_memctlr[0][1] = au_readl(MEM_STTIME0);
136 sleep_static_memctlr[0][2] = au_readl(MEM_STADDR0);
137 sleep_static_memctlr[1][0] = au_readl(MEM_STCFG1);
138 sleep_static_memctlr[1][1] = au_readl(MEM_STTIME1);
139 sleep_static_memctlr[1][2] = au_readl(MEM_STADDR1);
140 sleep_static_memctlr[2][0] = au_readl(MEM_STCFG2);
141 sleep_static_memctlr[2][1] = au_readl(MEM_STTIME2);
142 sleep_static_memctlr[2][2] = au_readl(MEM_STADDR2);
143 sleep_static_memctlr[3][0] = au_readl(MEM_STCFG3);
144 sleep_static_memctlr[3][1] = au_readl(MEM_STTIME3);
145 sleep_static_memctlr[3][2] = au_readl(MEM_STADDR3);
146}
147
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400148static void restore_core_regs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 extern void restore_au1xxx_intctl(void);
151 extern void wakeup_counter0_adjust(void);
152
153 au_writel(sleep_aux_pll_cntrl, SYS_AUXPLL); au_sync();
154 au_writel(sleep_cpu_pll_cntrl, SYS_CPUPLL); au_sync();
155 au_writel(sleep_pin_function, SYS_PINFUNC); au_sync();
156
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400157 /* Restore the static memory controller configuration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 au_writel(sleep_static_memctlr[0][0], MEM_STCFG0);
159 au_writel(sleep_static_memctlr[0][1], MEM_STTIME0);
160 au_writel(sleep_static_memctlr[0][2], MEM_STADDR0);
161 au_writel(sleep_static_memctlr[1][0], MEM_STCFG1);
162 au_writel(sleep_static_memctlr[1][1], MEM_STTIME1);
163 au_writel(sleep_static_memctlr[1][2], MEM_STADDR1);
164 au_writel(sleep_static_memctlr[2][0], MEM_STCFG2);
165 au_writel(sleep_static_memctlr[2][1], MEM_STTIME2);
166 au_writel(sleep_static_memctlr[2][2], MEM_STADDR2);
167 au_writel(sleep_static_memctlr[3][0], MEM_STCFG3);
168 au_writel(sleep_static_memctlr[3][1], MEM_STTIME3);
169 au_writel(sleep_static_memctlr[3][2], MEM_STADDR3);
170
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400171 /*
172 * Enable the UART if it was enabled before sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * I guess I should define module control bits........
174 */
175 if (sleep_uart0_enable & 0x02) {
176 au_writel(0, UART0_ADDR + UART_MOD_CNTRL); au_sync();
177 au_writel(1, UART0_ADDR + UART_MOD_CNTRL); au_sync();
178 au_writel(3, UART0_ADDR + UART_MOD_CNTRL); au_sync();
179 au_writel(sleep_uart0_inten, UART0_ADDR + UART_IER); au_sync();
180 au_writel(sleep_uart0_fifoctl, UART0_ADDR + UART_FCR); au_sync();
181 au_writel(sleep_uart0_linectl, UART0_ADDR + UART_LCR); au_sync();
182 au_writel(sleep_uart0_clkdiv, UART0_ADDR + UART_CLK); au_sync();
183 }
184
185 restore_au1xxx_intctl();
186 wakeup_counter0_adjust();
187}
188
189unsigned long suspend_mode;
190
191void wakeup_from_suspend(void)
192{
193 suspend_mode = 0;
194}
195
196int au_sleep(void)
197{
198 unsigned long wakeup, flags;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400199 extern void save_and_sleep(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Ralf Baechle21a151d2007-10-11 23:46:15 +0100201 spin_lock_irqsave(&pm_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 save_core_regs();
204
205 flush_cache_all();
206
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400207 /**
208 ** The code below is all system dependent and we should probably
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ** have a function call out of here to set this up. You need
210 ** to configure the GPIO or timer interrupts that will bring
211 ** you out of sleep.
212 ** For testing, the TOY counter wakeup is useful.
213 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#if 0
215 au_writel(au_readl(SYS_PINSTATERD) & ~(1 << 11), SYS_PINSTATERD);
216
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400217 /* GPIO 6 can cause a wake up event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 wakeup = au_readl(SYS_WAKEMSK);
219 wakeup &= ~(1 << 8); /* turn off match20 wakeup */
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400220 wakeup |= 1 << 6; /* turn on GPIO 6 wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#else
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400222 /* For testing, allow match20 to wake us up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#ifdef SLEEP_TEST_TIMEOUT
224 wakeup_counter0_set(sleep_ticks);
225#endif
226 wakeup = 1 << 8; /* turn on match20 wakeup */
227 wakeup = 0;
228#endif
229 au_writel(1, SYS_WAKESRC); /* clear cause */
230 au_sync();
231 au_writel(wakeup, SYS_WAKEMSK);
232 au_sync();
233
234 save_and_sleep();
235
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400236 /*
237 * After a wakeup, the cpu vectors back to 0x1fc00000, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * it's up to the boot code to get us back here.
239 */
240 restore_core_regs();
241 spin_unlock_irqrestore(&pm_lock, flags);
242 return 0;
243}
244
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400245static int pm_do_sleep(ctl_table *ctl, int write, struct file *file,
246 void __user *buffer, size_t *len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#ifdef SLEEP_TEST_TIMEOUT
249#define TMPBUFLEN2 16
250 char buf[TMPBUFLEN2], *p;
251#endif
252
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400253 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *len = 0;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400255 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#ifdef SLEEP_TEST_TIMEOUT
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400257 if (*len > TMPBUFLEN2 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -EFAULT;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400259 if (copy_from_user(buf, buffer, *len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 buf[*len] = 0;
262 p = buf;
263 sleep_ticks = simple_strtoul(p, &p, 0);
264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 au_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Pavel Machek6afe1a12008-03-13 23:52:49 +0100268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400271static int pm_do_freq(ctl_table *ctl, int write, struct file *file,
272 void __user *buffer, size_t *len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 int retval = 0, i;
275 unsigned long val, pll;
276#define TMPBUFLEN 64
277#define MAX_CPU_FREQ 396
278 char buf[TMPBUFLEN], *p;
279 unsigned long flags, intc0_mask, intc1_mask;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400280 unsigned long old_baud_base, old_cpu_freq, old_clk, old_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 unsigned long new_baud_base, new_cpu_freq, new_clk, new_refresh;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400282 unsigned long baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 spin_lock_irqsave(&pm_lock, flags);
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400285 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *len = 0;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400287 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Parse the new frequency */
289 if (*len > TMPBUFLEN - 1) {
290 spin_unlock_irqrestore(&pm_lock, flags);
291 return -EFAULT;
292 }
293 if (copy_from_user(buf, buffer, *len)) {
294 spin_unlock_irqrestore(&pm_lock, flags);
295 return -EFAULT;
296 }
297 buf[*len] = 0;
298 p = buf;
299 val = simple_strtoul(p, &p, 0);
300 if (val > MAX_CPU_FREQ) {
301 spin_unlock_irqrestore(&pm_lock, flags);
302 return -EFAULT;
303 }
304
305 pll = val / 12;
306 if ((pll > 33) || (pll < 7)) { /* 396 MHz max, 84 MHz min */
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400307 /* Revisit this for higher speed CPUs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_unlock_irqrestore(&pm_lock, flags);
309 return -EFAULT;
310 }
311
312 old_baud_base = get_au1x00_uart_baud_base();
313 old_cpu_freq = get_au1x00_speed();
314
315 new_cpu_freq = pll * 12 * 1000000;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400316 new_baud_base = (new_cpu_freq / (2 * ((int)(au_readl(SYS_POWERCTRL)
317 & 0x03) + 2) * 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 set_au1x00_speed(new_cpu_freq);
319 set_au1x00_uart_baud_base(new_baud_base);
320
321 old_refresh = au_readl(MEM_SDREFCFG) & 0x1ffffff;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400322 new_refresh = ((old_refresh * new_cpu_freq) / old_cpu_freq) |
323 (au_readl(MEM_SDREFCFG) & ~0x1ffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 au_writel(pll, SYS_CPUPLL);
326 au_sync_delay(1);
327 au_writel(new_refresh, MEM_SDREFCFG);
328 au_sync_delay(1);
329
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400330 for (i = 0; i < 4; i++)
331 if (au_readl(UART_BASE + UART_MOD_CNTRL +
332 i * 0x00100000) == 3) {
333 old_clk = au_readl(UART_BASE + UART_CLK +
334 i * 0x00100000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 baud_rate = old_baud_base / old_clk;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400336 /*
337 * We won't get an exact baud rate and the error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * could be significant enough that our new
339 * calculation will result in a clock that will
340 * give us a baud rate that's too far off from
341 * what we really want.
342 */
343 if (baud_rate > 100000)
344 baud_rate = 115200;
345 else if (baud_rate > 50000)
346 baud_rate = 57600;
347 else if (baud_rate > 30000)
348 baud_rate = 38400;
349 else if (baud_rate > 17000)
350 baud_rate = 19200;
351 else
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400352 baud_rate = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 new_clk = new_baud_base / baud_rate;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400354 au_writel(new_clk, UART_BASE + UART_CLK +
355 i * 0x00100000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 au_sync_delay(10);
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Ralf Baechlec30db242007-10-17 15:36:53 +0100360 /*
361 * We don't want _any_ interrupts other than match20. Otherwise our
362 * au1000_calibrate_delay() calculation will be off, potentially a lot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
364 intc0_mask = save_local_and_disable(0);
365 intc1_mask = save_local_and_disable(1);
Manuel Lauss785e3262008-12-21 09:26:17 +0100366 val = 1 << (AU1000_TOY_MATCH2_INT - AU1000_INTC0_INT_BASE);
367 au_writel(val, IC0_MASKSET); /* unmask */
368 au_writel(val, IC0_WAKESET); /* enable wake-from-sleep */
369 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_unlock_irqrestore(&pm_lock, flags);
Pete Popov3ce86ee2005-07-19 07:05:36 +0000371 au1000_calibrate_delay();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 restore_local_and_enable(0, intc0_mask);
373 restore_local_and_enable(1, intc1_mask);
Ralf Baechlec30db242007-10-17 15:36:53 +0100374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return retval;
376}
377
378
379static struct ctl_table pm_table[] = {
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800380 {
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800381 .ctl_name = CTL_UNNUMBERED,
382 .procname = "sleep",
383 .data = NULL,
384 .maxlen = 0,
385 .mode = 0600,
386 .proc_handler = &pm_do_sleep
387 },
388 {
389 .ctl_name = CTL_UNNUMBERED,
390 .procname = "freq",
391 .data = NULL,
392 .maxlen = 0,
393 .mode = 0600,
394 .proc_handler = &pm_do_freq
395 },
396 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
399static struct ctl_table pm_dir_table[] = {
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800400 {
401 .ctl_name = CTL_UNNUMBERED,
402 .procname = "pm",
403 .mode = 0555,
404 .child = pm_table
405 },
406 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407};
408
409/*
410 * Initialize power interface
411 */
412static int __init pm_init(void)
413{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800414 register_sysctl_table(pm_dir_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416}
417
418__initcall(pm_init);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
421 * This is right out of init/main.c
422 */
423
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400424/*
425 * This is the number of bits of precision for the loops_per_jiffy.
426 * Each bit takes on average 1.5/HZ seconds. This (like the original)
427 * is a little better than 1%.
428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#define LPS_PREC 8
430
Pete Popov3ce86ee2005-07-19 07:05:36 +0000431static void au1000_calibrate_delay(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 unsigned long ticks, loopbit;
434 int lps_precision = LPS_PREC;
435
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400436 loops_per_jiffy = 1 << 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 while (loops_per_jiffy <<= 1) {
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400439 /* Wait for "start of" clock tick */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ticks = jiffies;
441 while (ticks == jiffies)
442 /* nothing */ ;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400443 /* Go ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 ticks = jiffies;
445 __delay(loops_per_jiffy);
446 ticks = jiffies - ticks;
447 if (ticks)
448 break;
449 }
450
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400451 /*
452 * Do a binary approximation to get loops_per_jiffy set to be equal
453 * one clock (up to lps_precision bits)
454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 loops_per_jiffy >>= 1;
456 loopbit = loops_per_jiffy;
457 while (lps_precision-- && (loopbit >>= 1)) {
458 loops_per_jiffy |= loopbit;
459 ticks = jiffies;
460 while (ticks == jiffies);
461 ticks = jiffies;
462 __delay(loops_per_jiffy);
463 if (jiffies != ticks) /* longer than 1 tick */
464 loops_per_jiffy &= ~loopbit;
465 }
466}
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400467#endif /* CONFIG_PM */