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