blob: f85093b8d54dca800e6f0e8a0cdfd5e5d3cfa23f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * BRIEF MODULE DESCRIPTION
3 * Au1000 Power Management routines.
4 *
5 * Copyright 2001 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc.
7 * ppopov@mvista.com or source@mvista.com
8 *
9 * Some of the routines are right out of init/main.c, whose
10 * copyrights apply here.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 */
32#include <linux/config.h>
33#include <linux/init.h>
34#include <linux/pm.h>
35#include <linux/slab.h>
36#include <linux/sysctl.h>
Pete Popov3ce86ee2005-07-19 07:05:36 +000037#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/string.h>
40#include <asm/uaccess.h>
41#include <asm/io.h>
42#include <asm/system.h>
Pete Popov3ce86ee2005-07-19 07:05:36 +000043#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/mach-au1x00/au1000.h>
45
46#ifdef CONFIG_PM
47
48#define DEBUG 1
49#ifdef DEBUG
50# define DPRINTK(fmt, args...) printk("%s: " fmt, __FUNCTION__ , ## args)
51#else
52# define DPRINTK(fmt, args...)
53#endif
54
Pete Popov3ce86ee2005-07-19 07:05:36 +000055static void au1000_calibrate_delay(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57extern void set_au1x00_speed(unsigned int new_freq);
58extern unsigned int get_au1x00_speed(void);
59extern unsigned long get_au1x00_uart_baud_base(void);
60extern void set_au1x00_uart_baud_base(unsigned long new_baud_base);
61extern unsigned long save_local_and_disable(int controller);
62extern void restore_local_and_enable(int controller, unsigned long mask);
63extern void local_enable_irq(unsigned int irq_nr);
64
65/* Quick acpi hack. This will have to change! */
66#define CTL_ACPI 9999
67#define ACPI_S1_SLP_TYP 19
68#define ACPI_SLEEP 21
69
70
71static DEFINE_SPINLOCK(pm_lock);
72
73/* We need to save/restore a bunch of core registers that are
74 * either volatile or reset to some state across a processor sleep.
75 * If reading a register doesn't provide a proper result for a
76 * later restore, we have to provide a function for loading that
77 * register and save a copy.
78 *
79 * We only have to save/restore registers that aren't otherwise
80 * done as part of a driver pm_* function.
81 */
82static uint sleep_aux_pll_cntrl;
83static uint sleep_cpu_pll_cntrl;
84static uint sleep_pin_function;
85static uint sleep_uart0_inten;
86static uint sleep_uart0_fifoctl;
87static uint sleep_uart0_linectl;
88static uint sleep_uart0_clkdiv;
89static uint sleep_uart0_enable;
90static uint sleep_usbhost_enable;
91static uint sleep_usbdev_enable;
92static uint sleep_static_memctlr[4][3];
93
94/* Define this to cause the value you write to /proc/sys/pm/sleep to
95 * set the TOY timer for the amount of time you want to sleep.
96 * This is done mainly for testing, but may be useful in other cases.
97 * The value is number of 32KHz ticks to sleep.
98 */
99#define SLEEP_TEST_TIMEOUT 1
100#ifdef SLEEP_TEST_TIMEOUT
101static int sleep_ticks;
102void wakeup_counter0_set(int ticks);
103#endif
104
105static void
106save_core_regs(void)
107{
108 extern void save_au1xxx_intctl(void);
109 extern void pm_eth0_shutdown(void);
110
111 /* Do the serial ports.....these really should be a pm_*
112 * registered function by the driver......but of course the
113 * standard serial driver doesn't understand our Au1xxx
114 * unique registers.
115 */
116 sleep_uart0_inten = au_readl(UART0_ADDR + UART_IER);
117 sleep_uart0_fifoctl = au_readl(UART0_ADDR + UART_FCR);
118 sleep_uart0_linectl = au_readl(UART0_ADDR + UART_LCR);
119 sleep_uart0_clkdiv = au_readl(UART0_ADDR + UART_CLK);
120 sleep_uart0_enable = au_readl(UART0_ADDR + UART_MOD_CNTRL);
121
122 /* Shutdown USB host/device.
123 */
124 sleep_usbhost_enable = au_readl(USB_HOST_CONFIG);
125
126 /* There appears to be some undocumented reset register....
127 */
128 au_writel(0, 0xb0100004); au_sync();
129 au_writel(0, USB_HOST_CONFIG); au_sync();
130
131 sleep_usbdev_enable = au_readl(USBD_ENABLE);
132 au_writel(0, USBD_ENABLE); au_sync();
133
134 /* Save interrupt controller state.
135 */
136 save_au1xxx_intctl();
137
138 /* Clocks and PLLs.
139 */
140 sleep_aux_pll_cntrl = au_readl(SYS_AUXPLL);
141
142 /* We don't really need to do this one, but unless we
143 * write it again it won't have a valid value if we
144 * happen to read it.
145 */
146 sleep_cpu_pll_cntrl = au_readl(SYS_CPUPLL);
147
148 sleep_pin_function = au_readl(SYS_PINFUNC);
149
150 /* Save the static memory controller configuration.
151 */
152 sleep_static_memctlr[0][0] = au_readl(MEM_STCFG0);
153 sleep_static_memctlr[0][1] = au_readl(MEM_STTIME0);
154 sleep_static_memctlr[0][2] = au_readl(MEM_STADDR0);
155 sleep_static_memctlr[1][0] = au_readl(MEM_STCFG1);
156 sleep_static_memctlr[1][1] = au_readl(MEM_STTIME1);
157 sleep_static_memctlr[1][2] = au_readl(MEM_STADDR1);
158 sleep_static_memctlr[2][0] = au_readl(MEM_STCFG2);
159 sleep_static_memctlr[2][1] = au_readl(MEM_STTIME2);
160 sleep_static_memctlr[2][2] = au_readl(MEM_STADDR2);
161 sleep_static_memctlr[3][0] = au_readl(MEM_STCFG3);
162 sleep_static_memctlr[3][1] = au_readl(MEM_STTIME3);
163 sleep_static_memctlr[3][2] = au_readl(MEM_STADDR3);
164}
165
166static void
167restore_core_regs(void)
168{
169 extern void restore_au1xxx_intctl(void);
170 extern void wakeup_counter0_adjust(void);
171
172 au_writel(sleep_aux_pll_cntrl, SYS_AUXPLL); au_sync();
173 au_writel(sleep_cpu_pll_cntrl, SYS_CPUPLL); au_sync();
174 au_writel(sleep_pin_function, SYS_PINFUNC); au_sync();
175
176 /* Restore the static memory controller configuration.
177 */
178 au_writel(sleep_static_memctlr[0][0], MEM_STCFG0);
179 au_writel(sleep_static_memctlr[0][1], MEM_STTIME0);
180 au_writel(sleep_static_memctlr[0][2], MEM_STADDR0);
181 au_writel(sleep_static_memctlr[1][0], MEM_STCFG1);
182 au_writel(sleep_static_memctlr[1][1], MEM_STTIME1);
183 au_writel(sleep_static_memctlr[1][2], MEM_STADDR1);
184 au_writel(sleep_static_memctlr[2][0], MEM_STCFG2);
185 au_writel(sleep_static_memctlr[2][1], MEM_STTIME2);
186 au_writel(sleep_static_memctlr[2][2], MEM_STADDR2);
187 au_writel(sleep_static_memctlr[3][0], MEM_STCFG3);
188 au_writel(sleep_static_memctlr[3][1], MEM_STTIME3);
189 au_writel(sleep_static_memctlr[3][2], MEM_STADDR3);
190
191 /* Enable the UART if it was enabled before sleep.
192 * I guess I should define module control bits........
193 */
194 if (sleep_uart0_enable & 0x02) {
195 au_writel(0, UART0_ADDR + UART_MOD_CNTRL); au_sync();
196 au_writel(1, UART0_ADDR + UART_MOD_CNTRL); au_sync();
197 au_writel(3, UART0_ADDR + UART_MOD_CNTRL); au_sync();
198 au_writel(sleep_uart0_inten, UART0_ADDR + UART_IER); au_sync();
199 au_writel(sleep_uart0_fifoctl, UART0_ADDR + UART_FCR); au_sync();
200 au_writel(sleep_uart0_linectl, UART0_ADDR + UART_LCR); au_sync();
201 au_writel(sleep_uart0_clkdiv, UART0_ADDR + UART_CLK); au_sync();
202 }
203
204 restore_au1xxx_intctl();
205 wakeup_counter0_adjust();
206}
207
208unsigned long suspend_mode;
209
210void wakeup_from_suspend(void)
211{
212 suspend_mode = 0;
213}
214
215int au_sleep(void)
216{
217 unsigned long wakeup, flags;
218 extern void save_and_sleep(void);
219
220 spin_lock_irqsave(&pm_lock,flags);
221
222 save_core_regs();
223
224 flush_cache_all();
225
226 /** The code below is all system dependent and we should probably
227 ** have a function call out of here to set this up. You need
228 ** to configure the GPIO or timer interrupts that will bring
229 ** you out of sleep.
230 ** For testing, the TOY counter wakeup is useful.
231 **/
232
233#if 0
234 au_writel(au_readl(SYS_PINSTATERD) & ~(1 << 11), SYS_PINSTATERD);
235
236 /* gpio 6 can cause a wake up event */
237 wakeup = au_readl(SYS_WAKEMSK);
238 wakeup &= ~(1 << 8); /* turn off match20 wakeup */
239 wakeup |= 1 << 6; /* turn on gpio 6 wakeup */
240#else
241 /* For testing, allow match20 to wake us up.
242 */
243#ifdef SLEEP_TEST_TIMEOUT
244 wakeup_counter0_set(sleep_ticks);
245#endif
246 wakeup = 1 << 8; /* turn on match20 wakeup */
247 wakeup = 0;
248#endif
249 au_writel(1, SYS_WAKESRC); /* clear cause */
250 au_sync();
251 au_writel(wakeup, SYS_WAKEMSK);
252 au_sync();
253
254 save_and_sleep();
255
256 /* after a wakeup, the cpu vectors back to 0x1fc00000 so
257 * it's up to the boot code to get us back here.
258 */
259 restore_core_regs();
260 spin_unlock_irqrestore(&pm_lock, flags);
261 return 0;
262}
263
264static int pm_do_sleep(ctl_table * ctl, int write, struct file *file,
Pete Popov3ce86ee2005-07-19 07:05:36 +0000265 void __user *buffer, size_t * len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int retval = 0;
268#ifdef SLEEP_TEST_TIMEOUT
269#define TMPBUFLEN2 16
270 char buf[TMPBUFLEN2], *p;
271#endif
272
273 if (!write) {
274 *len = 0;
275 } else {
276#ifdef SLEEP_TEST_TIMEOUT
277 if (*len > TMPBUFLEN2 - 1) {
278 return -EFAULT;
279 }
280 if (copy_from_user(buf, buffer, *len)) {
281 return -EFAULT;
282 }
283 buf[*len] = 0;
284 p = buf;
285 sleep_ticks = simple_strtoul(p, &p, 0);
286#endif
287 retval = pm_send_all(PM_SUSPEND, (void *) 2);
288
289 if (retval)
290 return retval;
291
292 au_sleep();
293 retval = pm_send_all(PM_RESUME, (void *) 0);
294 }
295 return retval;
296}
297
298static int pm_do_suspend(ctl_table * ctl, int write, struct file *file,
Pete Popov3ce86ee2005-07-19 07:05:36 +0000299 void __user *buffer, size_t * len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (!write) {
304 *len = 0;
305 } else {
306 retval = pm_send_all(PM_SUSPEND, (void *) 2);
307 if (retval)
308 return retval;
309 suspend_mode = 1;
Pete Popov494900a2005-04-07 00:42:10 +0000310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 retval = pm_send_all(PM_RESUME, (void *) 0);
312 }
313 return retval;
314}
315
316
317static int pm_do_freq(ctl_table * ctl, int write, struct file *file,
Pete Popov3ce86ee2005-07-19 07:05:36 +0000318 void __user *buffer, size_t * len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 int retval = 0, i;
321 unsigned long val, pll;
322#define TMPBUFLEN 64
323#define MAX_CPU_FREQ 396
324 char buf[TMPBUFLEN], *p;
325 unsigned long flags, intc0_mask, intc1_mask;
326 unsigned long old_baud_base, old_cpu_freq, baud_rate, old_clk,
327 old_refresh;
328 unsigned long new_baud_base, new_cpu_freq, new_clk, new_refresh;
329
330 spin_lock_irqsave(&pm_lock, flags);
331 if (!write) {
332 *len = 0;
333 } else {
334 /* Parse the new frequency */
335 if (*len > TMPBUFLEN - 1) {
336 spin_unlock_irqrestore(&pm_lock, flags);
337 return -EFAULT;
338 }
339 if (copy_from_user(buf, buffer, *len)) {
340 spin_unlock_irqrestore(&pm_lock, flags);
341 return -EFAULT;
342 }
343 buf[*len] = 0;
344 p = buf;
345 val = simple_strtoul(p, &p, 0);
346 if (val > MAX_CPU_FREQ) {
347 spin_unlock_irqrestore(&pm_lock, flags);
348 return -EFAULT;
349 }
350
351 pll = val / 12;
352 if ((pll > 33) || (pll < 7)) { /* 396 MHz max, 84 MHz min */
353 /* revisit this for higher speed cpus */
354 spin_unlock_irqrestore(&pm_lock, flags);
355 return -EFAULT;
356 }
357
358 old_baud_base = get_au1x00_uart_baud_base();
359 old_cpu_freq = get_au1x00_speed();
360
361 new_cpu_freq = pll * 12 * 1000000;
362 new_baud_base = (new_cpu_freq / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
363 set_au1x00_speed(new_cpu_freq);
364 set_au1x00_uart_baud_base(new_baud_base);
365
366 old_refresh = au_readl(MEM_SDREFCFG) & 0x1ffffff;
367 new_refresh =
368 ((old_refresh * new_cpu_freq) /
369 old_cpu_freq) | (au_readl(MEM_SDREFCFG) & ~0x1ffffff);
370
371 au_writel(pll, SYS_CPUPLL);
372 au_sync_delay(1);
373 au_writel(new_refresh, MEM_SDREFCFG);
374 au_sync_delay(1);
375
376 for (i = 0; i < 4; i++) {
377 if (au_readl
378 (UART_BASE + UART_MOD_CNTRL +
379 i * 0x00100000) == 3) {
380 old_clk =
381 au_readl(UART_BASE + UART_CLK +
382 i * 0x00100000);
383 // baud_rate = baud_base/clk
384 baud_rate = old_baud_base / old_clk;
385 /* we won't get an exact baud rate and the error
386 * could be significant enough that our new
387 * calculation will result in a clock that will
388 * give us a baud rate that's too far off from
389 * what we really want.
390 */
391 if (baud_rate > 100000)
392 baud_rate = 115200;
393 else if (baud_rate > 50000)
394 baud_rate = 57600;
395 else if (baud_rate > 30000)
396 baud_rate = 38400;
397 else if (baud_rate > 17000)
398 baud_rate = 19200;
399 else
400 (baud_rate = 9600);
401 // new_clk = new_baud_base/baud_rate
402 new_clk = new_baud_base / baud_rate;
403 au_writel(new_clk,
404 UART_BASE + UART_CLK +
405 i * 0x00100000);
406 au_sync_delay(10);
407 }
408 }
409 }
410
411
412 /* We don't want _any_ interrupts other than
Pete Popov3ce86ee2005-07-19 07:05:36 +0000413 * match20. Otherwise our au1000_calibrate_delay()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * calculation will be off, potentially a lot.
415 */
416 intc0_mask = save_local_and_disable(0);
417 intc1_mask = save_local_and_disable(1);
418 local_enable_irq(AU1000_TOY_MATCH2_INT);
419 spin_unlock_irqrestore(&pm_lock, flags);
Pete Popov3ce86ee2005-07-19 07:05:36 +0000420 au1000_calibrate_delay();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 restore_local_and_enable(0, intc0_mask);
422 restore_local_and_enable(1, intc1_mask);
423 return retval;
424}
425
426
427static struct ctl_table pm_table[] = {
428 {ACPI_S1_SLP_TYP, "suspend", NULL, 0, 0600, NULL, &pm_do_suspend},
429 {ACPI_SLEEP, "sleep", NULL, 0, 0600, NULL, &pm_do_sleep},
430 {CTL_ACPI, "freq", NULL, 0, 0600, NULL, &pm_do_freq},
431 {0}
432};
433
434static struct ctl_table pm_dir_table[] = {
435 {CTL_ACPI, "pm", NULL, 0, 0555, pm_table},
436 {0}
437};
438
439/*
440 * Initialize power interface
441 */
442static int __init pm_init(void)
443{
444 register_sysctl_table(pm_dir_table, 1);
445 return 0;
446}
447
448__initcall(pm_init);
449
450
451/*
452 * This is right out of init/main.c
453 */
454
455/* This is the number of bits of precision for the loops_per_jiffy. Each
456 bit takes on average 1.5/HZ seconds. This (like the original) is a little
457 better than 1% */
458#define LPS_PREC 8
459
Pete Popov3ce86ee2005-07-19 07:05:36 +0000460static void au1000_calibrate_delay(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 unsigned long ticks, loopbit;
463 int lps_precision = LPS_PREC;
464
465 loops_per_jiffy = (1 << 12);
466
467 while (loops_per_jiffy <<= 1) {
468 /* wait for "start of" clock tick */
469 ticks = jiffies;
470 while (ticks == jiffies)
471 /* nothing */ ;
472 /* Go .. */
473 ticks = jiffies;
474 __delay(loops_per_jiffy);
475 ticks = jiffies - ticks;
476 if (ticks)
477 break;
478 }
479
480/* Do a binary approximation to get loops_per_jiffy set to equal one clock
481 (up to lps_precision bits) */
482 loops_per_jiffy >>= 1;
483 loopbit = loops_per_jiffy;
484 while (lps_precision-- && (loopbit >>= 1)) {
485 loops_per_jiffy |= loopbit;
486 ticks = jiffies;
487 while (ticks == jiffies);
488 ticks = jiffies;
489 __delay(loops_per_jiffy);
490 if (jiffies != ticks) /* longer than 1 tick */
491 loops_per_jiffy &= ~loopbit;
492 }
493}
494#endif /* CONFIG_PM */