blob: f58e151b38d495a5105478b551e74ebb92b9b2a0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/mach-au1x00/au1000.h>
Manuel Laussac15dad2008-12-21 09:26:26 +010039#if defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200)
40#include <asm/mach-au1x00/au1xxx_dbdma.h>
41#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#ifdef CONFIG_PM
44
45#define DEBUG 1
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040046#ifdef DEBUG
47#define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#else
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040049#define DPRINTK(fmt, args...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#endif
51
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 */
Manuel Lauss564365b2008-12-21 09:26:25 +010067static unsigned int sleep_uart0_inten;
68static unsigned int sleep_uart0_fifoctl;
69static unsigned int sleep_uart0_linectl;
70static unsigned int sleep_uart0_clkdiv;
71static unsigned int sleep_uart0_enable;
72static unsigned int sleep_usb[2];
73static unsigned int sleep_sys_clocks[5];
74static unsigned int sleep_sys_pinfunc;
75static unsigned int sleep_static_memctlr[4][3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040077/*
78 * Define this to cause the value you write to /proc/sys/pm/sleep to
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * set the TOY timer for the amount of time you want to sleep.
80 * This is done mainly for testing, but may be useful in other cases.
81 * The value is number of 32KHz ticks to sleep.
82 */
83#define SLEEP_TEST_TIMEOUT 1
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040084#ifdef SLEEP_TEST_TIMEOUT
85static int sleep_ticks;
Manuel Lauss0c694de2008-12-21 09:26:23 +010086static void wakeup_counter0_set(int ticks)
87{
88 au_writel(au_readl(SYS_TOYREAD) + ticks, SYS_TOYMATCH2);
89 au_sync();
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#endif
92
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040093static void save_core_regs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 extern void save_au1xxx_intctl(void);
96 extern void pm_eth0_shutdown(void);
97
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040098 /*
99 * Do the serial ports.....these really should be a pm_*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * registered function by the driver......but of course the
101 * standard serial driver doesn't understand our Au1xxx
102 * unique registers.
103 */
104 sleep_uart0_inten = au_readl(UART0_ADDR + UART_IER);
105 sleep_uart0_fifoctl = au_readl(UART0_ADDR + UART_FCR);
106 sleep_uart0_linectl = au_readl(UART0_ADDR + UART_LCR);
107 sleep_uart0_clkdiv = au_readl(UART0_ADDR + UART_CLK);
108 sleep_uart0_enable = au_readl(UART0_ADDR + UART_MOD_CNTRL);
Manuel Lauss564365b2008-12-21 09:26:25 +0100109 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Manuel Lauss564365b2008-12-21 09:26:25 +0100111#ifndef CONFIG_SOC_AU1200
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400112 /* Shutdown USB host/device. */
Manuel Lauss564365b2008-12-21 09:26:25 +0100113 sleep_usb[0] = au_readl(USB_HOST_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400115 /* There appears to be some undocumented reset register.... */
Manuel Lauss564365b2008-12-21 09:26:25 +0100116 au_writel(0, 0xb0100004);
117 au_sync();
118 au_writel(0, USB_HOST_CONFIG);
119 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Manuel Lauss564365b2008-12-21 09:26:25 +0100121 sleep_usb[1] = au_readl(USBD_ENABLE);
122 au_writel(0, USBD_ENABLE);
123 au_sync();
124
125#else /* AU1200 */
126
127 /* enable access to OTG mmio so we can save OTG CAP/MUX.
128 * FIXME: write an OTG driver and move this stuff there!
129 */
130 au_writel(au_readl(USB_MSR_BASE + 4) | (1 << 6), USB_MSR_BASE + 4);
131 au_sync();
132 sleep_usb[0] = au_readl(0xb4020020); /* OTG_CAP */
133 sleep_usb[1] = au_readl(0xb4020024); /* OTG_MUX */
134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400136 /* Save interrupt controller state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 save_au1xxx_intctl();
138
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400139 /* Clocks and PLLs. */
Manuel Lauss564365b2008-12-21 09:26:25 +0100140 sleep_sys_clocks[0] = au_readl(SYS_FREQCTRL0);
141 sleep_sys_clocks[1] = au_readl(SYS_FREQCTRL1);
142 sleep_sys_clocks[2] = au_readl(SYS_CLKSRC);
143 sleep_sys_clocks[3] = au_readl(SYS_CPUPLL);
144 sleep_sys_clocks[4] = au_readl(SYS_AUXPLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Manuel Lauss564365b2008-12-21 09:26:25 +0100146 /* pin mux config */
147 sleep_sys_pinfunc = au_readl(SYS_PINFUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400149 /* Save the static memory controller configuration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sleep_static_memctlr[0][0] = au_readl(MEM_STCFG0);
151 sleep_static_memctlr[0][1] = au_readl(MEM_STTIME0);
152 sleep_static_memctlr[0][2] = au_readl(MEM_STADDR0);
153 sleep_static_memctlr[1][0] = au_readl(MEM_STCFG1);
154 sleep_static_memctlr[1][1] = au_readl(MEM_STTIME1);
155 sleep_static_memctlr[1][2] = au_readl(MEM_STADDR1);
156 sleep_static_memctlr[2][0] = au_readl(MEM_STCFG2);
157 sleep_static_memctlr[2][1] = au_readl(MEM_STTIME2);
158 sleep_static_memctlr[2][2] = au_readl(MEM_STADDR2);
159 sleep_static_memctlr[3][0] = au_readl(MEM_STCFG3);
160 sleep_static_memctlr[3][1] = au_readl(MEM_STTIME3);
161 sleep_static_memctlr[3][2] = au_readl(MEM_STADDR3);
Manuel Laussac15dad2008-12-21 09:26:26 +0100162
163#if defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200)
164 au1xxx_dbdma_suspend();
165#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400168static void restore_core_regs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Manuel Lauss564365b2008-12-21 09:26:25 +0100170 /* restore clock configuration. Writing CPUPLL last will
171 * stall a bit and stabilize other clocks (unless this is
172 * one of those Au1000 with a write-only PLL, where we dont
173 * have a valid value)
174 */
175 au_writel(sleep_sys_clocks[0], SYS_FREQCTRL0);
176 au_writel(sleep_sys_clocks[1], SYS_FREQCTRL1);
177 au_writel(sleep_sys_clocks[2], SYS_CLKSRC);
178 au_writel(sleep_sys_clocks[4], SYS_AUXPLL);
179 if (!au1xxx_cpu_has_pll_wo())
180 au_writel(sleep_sys_clocks[3], SYS_CPUPLL);
181 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Manuel Lauss564365b2008-12-21 09:26:25 +0100183 au_writel(sleep_sys_pinfunc, SYS_PINFUNC);
184 au_sync();
185
186#ifndef CONFIG_SOC_AU1200
187 au_writel(sleep_usb[0], USB_HOST_CONFIG);
188 au_writel(sleep_usb[1], USBD_ENABLE);
189 au_sync();
190#else
191 /* enable accces to OTG memory */
192 au_writel(au_readl(USB_MSR_BASE + 4) | (1 << 6), USB_MSR_BASE + 4);
193 au_sync();
194
195 /* restore OTG caps and port mux. */
196 au_writel(sleep_usb[0], 0xb4020020 + 0); /* OTG_CAP */
197 au_sync();
198 au_writel(sleep_usb[1], 0xb4020020 + 4); /* OTG_MUX */
199 au_sync();
200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400202 /* Restore the static memory controller configuration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 au_writel(sleep_static_memctlr[0][0], MEM_STCFG0);
204 au_writel(sleep_static_memctlr[0][1], MEM_STTIME0);
205 au_writel(sleep_static_memctlr[0][2], MEM_STADDR0);
206 au_writel(sleep_static_memctlr[1][0], MEM_STCFG1);
207 au_writel(sleep_static_memctlr[1][1], MEM_STTIME1);
208 au_writel(sleep_static_memctlr[1][2], MEM_STADDR1);
209 au_writel(sleep_static_memctlr[2][0], MEM_STCFG2);
210 au_writel(sleep_static_memctlr[2][1], MEM_STTIME2);
211 au_writel(sleep_static_memctlr[2][2], MEM_STADDR2);
212 au_writel(sleep_static_memctlr[3][0], MEM_STCFG3);
213 au_writel(sleep_static_memctlr[3][1], MEM_STTIME3);
214 au_writel(sleep_static_memctlr[3][2], MEM_STADDR3);
215
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400216 /*
217 * Enable the UART if it was enabled before sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * I guess I should define module control bits........
219 */
220 if (sleep_uart0_enable & 0x02) {
221 au_writel(0, UART0_ADDR + UART_MOD_CNTRL); au_sync();
222 au_writel(1, UART0_ADDR + UART_MOD_CNTRL); au_sync();
223 au_writel(3, UART0_ADDR + UART_MOD_CNTRL); au_sync();
224 au_writel(sleep_uart0_inten, UART0_ADDR + UART_IER); au_sync();
225 au_writel(sleep_uart0_fifoctl, UART0_ADDR + UART_FCR); au_sync();
226 au_writel(sleep_uart0_linectl, UART0_ADDR + UART_LCR); au_sync();
227 au_writel(sleep_uart0_clkdiv, UART0_ADDR + UART_CLK); au_sync();
228 }
229
230 restore_au1xxx_intctl();
Manuel Laussac15dad2008-12-21 09:26:26 +0100231
232#if defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200)
233 au1xxx_dbdma_resume();
234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237unsigned long suspend_mode;
238
239void wakeup_from_suspend(void)
240{
241 suspend_mode = 0;
242}
243
Manuel Lauss564365b2008-12-21 09:26:25 +0100244void au_sleep(void)
245{
246 save_core_regs();
247 au1xxx_save_and_sleep();
248 restore_core_regs();
249}
250
251static int pm_do_sleep(ctl_table *ctl, int write, struct file *file,
252 void __user *buffer, size_t *len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned long wakeup, flags;
Manuel Lauss564365b2008-12-21 09:26:25 +0100255 int ret;
256#ifdef SLEEP_TEST_TIMEOUT
257#define TMPBUFLEN2 16
258 char buf[TMPBUFLEN2], *p;
259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Ralf Baechle21a151d2007-10-11 23:46:15 +0100261 spin_lock_irqsave(&pm_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Manuel Lauss564365b2008-12-21 09:26:25 +0100263 if (!write) {
264 *len = 0;
265 ret = 0;
266 goto out_unlock;
267 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Manuel Lauss564365b2008-12-21 09:26:25 +0100269#ifdef SLEEP_TEST_TIMEOUT
270 if (*len > TMPBUFLEN2 - 1) {
271 ret = -EFAULT;
272 goto out_unlock;
273 }
274 if (copy_from_user(buf, buffer, *len)) {
275 return -EFAULT;
276 goto out_unlock;
277 }
278 buf[*len] = 0;
279 p = buf;
280 sleep_ticks = simple_strtoul(p, &p, 0);
281 wakeup_counter0_set(sleep_ticks);
282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400284 /**
285 ** The code below is all system dependent and we should probably
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 ** have a function call out of here to set this up. You need
287 ** to configure the GPIO or timer interrupts that will bring
288 ** you out of sleep.
289 ** For testing, the TOY counter wakeup is useful.
290 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#if 0
292 au_writel(au_readl(SYS_PINSTATERD) & ~(1 << 11), SYS_PINSTATERD);
293
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400294 /* GPIO 6 can cause a wake up event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 wakeup = au_readl(SYS_WAKEMSK);
296 wakeup &= ~(1 << 8); /* turn off match20 wakeup */
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400297 wakeup |= 1 << 6; /* turn on GPIO 6 wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#else
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400299 /* For testing, allow match20 to wake us up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 wakeup = 1 << 8; /* turn on match20 wakeup */
301 wakeup = 0;
302#endif
303 au_writel(1, SYS_WAKESRC); /* clear cause */
304 au_sync();
305 au_writel(wakeup, SYS_WAKEMSK);
306 au_sync();
307
Manuel Lauss564365b2008-12-21 09:26:25 +0100308 au_sleep();
309 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Manuel Lauss564365b2008-12-21 09:26:25 +0100311out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_unlock_irqrestore(&pm_lock, flags);
Manuel Lauss564365b2008-12-21 09:26:25 +0100313 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Manuel Lauss564365b2008-12-21 09:26:25 +0100316#if !defined(CONFIG_SOC_AU1200) && !defined(CONFIG_SOC_AU1550)
317
318/*
319 * This is right out of init/main.c
320 */
321
322/*
323 * This is the number of bits of precision for the loops_per_jiffy.
324 * Each bit takes on average 1.5/HZ seconds. This (like the original)
325 * is a little better than 1%.
326 */
327#define LPS_PREC 8
328
329static void au1000_calibrate_delay(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Manuel Lauss564365b2008-12-21 09:26:25 +0100331 unsigned long ticks, loopbit;
332 int lps_precision = LPS_PREC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Manuel Lauss564365b2008-12-21 09:26:25 +0100334 loops_per_jiffy = 1 << 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Manuel Lauss564365b2008-12-21 09:26:25 +0100336 while (loops_per_jiffy <<= 1) {
337 /* Wait for "start of" clock tick */
338 ticks = jiffies;
339 while (ticks == jiffies)
340 /* nothing */ ;
341 /* Go ... */
342 ticks = jiffies;
343 __delay(loops_per_jiffy);
344 ticks = jiffies - ticks;
345 if (ticks)
346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Manuel Lauss564365b2008-12-21 09:26:25 +0100348
349 /*
350 * Do a binary approximation to get loops_per_jiffy set to be equal
351 * one clock (up to lps_precision bits)
352 */
353 loops_per_jiffy >>= 1;
354 loopbit = loops_per_jiffy;
355 while (lps_precision-- && (loopbit >>= 1)) {
356 loops_per_jiffy |= loopbit;
357 ticks = jiffies;
358 while (ticks == jiffies);
359 ticks = jiffies;
360 __delay(loops_per_jiffy);
361 if (jiffies != ticks) /* longer than 1 tick */
362 loops_per_jiffy &= ~loopbit;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400366static int pm_do_freq(ctl_table *ctl, int write, struct file *file,
367 void __user *buffer, size_t *len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int retval = 0, i;
370 unsigned long val, pll;
371#define TMPBUFLEN 64
372#define MAX_CPU_FREQ 396
373 char buf[TMPBUFLEN], *p;
374 unsigned long flags, intc0_mask, intc1_mask;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400375 unsigned long old_baud_base, old_cpu_freq, old_clk, old_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned long new_baud_base, new_cpu_freq, new_clk, new_refresh;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400377 unsigned long baud_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 spin_lock_irqsave(&pm_lock, flags);
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400380 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *len = 0;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400382 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Parse the new frequency */
384 if (*len > TMPBUFLEN - 1) {
385 spin_unlock_irqrestore(&pm_lock, flags);
386 return -EFAULT;
387 }
388 if (copy_from_user(buf, buffer, *len)) {
389 spin_unlock_irqrestore(&pm_lock, flags);
390 return -EFAULT;
391 }
392 buf[*len] = 0;
393 p = buf;
394 val = simple_strtoul(p, &p, 0);
395 if (val > MAX_CPU_FREQ) {
396 spin_unlock_irqrestore(&pm_lock, flags);
397 return -EFAULT;
398 }
399
400 pll = val / 12;
401 if ((pll > 33) || (pll < 7)) { /* 396 MHz max, 84 MHz min */
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400402 /* Revisit this for higher speed CPUs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 spin_unlock_irqrestore(&pm_lock, flags);
404 return -EFAULT;
405 }
406
407 old_baud_base = get_au1x00_uart_baud_base();
408 old_cpu_freq = get_au1x00_speed();
409
410 new_cpu_freq = pll * 12 * 1000000;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400411 new_baud_base = (new_cpu_freq / (2 * ((int)(au_readl(SYS_POWERCTRL)
412 & 0x03) + 2) * 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 set_au1x00_speed(new_cpu_freq);
414 set_au1x00_uart_baud_base(new_baud_base);
415
416 old_refresh = au_readl(MEM_SDREFCFG) & 0x1ffffff;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400417 new_refresh = ((old_refresh * new_cpu_freq) / old_cpu_freq) |
418 (au_readl(MEM_SDREFCFG) & ~0x1ffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 au_writel(pll, SYS_CPUPLL);
421 au_sync_delay(1);
422 au_writel(new_refresh, MEM_SDREFCFG);
423 au_sync_delay(1);
424
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400425 for (i = 0; i < 4; i++)
426 if (au_readl(UART_BASE + UART_MOD_CNTRL +
427 i * 0x00100000) == 3) {
428 old_clk = au_readl(UART_BASE + UART_CLK +
429 i * 0x00100000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 baud_rate = old_baud_base / old_clk;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400431 /*
432 * We won't get an exact baud rate and the error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * could be significant enough that our new
434 * calculation will result in a clock that will
435 * give us a baud rate that's too far off from
436 * what we really want.
437 */
438 if (baud_rate > 100000)
439 baud_rate = 115200;
440 else if (baud_rate > 50000)
441 baud_rate = 57600;
442 else if (baud_rate > 30000)
443 baud_rate = 38400;
444 else if (baud_rate > 17000)
445 baud_rate = 19200;
446 else
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400447 baud_rate = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 new_clk = new_baud_base / baud_rate;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400449 au_writel(new_clk, UART_BASE + UART_CLK +
450 i * 0x00100000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 au_sync_delay(10);
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
Ralf Baechlec30db242007-10-17 15:36:53 +0100455 /*
456 * We don't want _any_ interrupts other than match20. Otherwise our
457 * au1000_calibrate_delay() calculation will be off, potentially a lot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
459 intc0_mask = save_local_and_disable(0);
460 intc1_mask = save_local_and_disable(1);
Manuel Lauss785e3262008-12-21 09:26:17 +0100461 val = 1 << (AU1000_TOY_MATCH2_INT - AU1000_INTC0_INT_BASE);
462 au_writel(val, IC0_MASKSET); /* unmask */
463 au_writel(val, IC0_WAKESET); /* enable wake-from-sleep */
464 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_unlock_irqrestore(&pm_lock, flags);
Pete Popov3ce86ee2005-07-19 07:05:36 +0000466 au1000_calibrate_delay();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 restore_local_and_enable(0, intc0_mask);
468 restore_local_and_enable(1, intc1_mask);
Ralf Baechlec30db242007-10-17 15:36:53 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return retval;
471}
Manuel Lauss564365b2008-12-21 09:26:25 +0100472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474static struct ctl_table pm_table[] = {
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800475 {
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800476 .ctl_name = CTL_UNNUMBERED,
477 .procname = "sleep",
478 .data = NULL,
479 .maxlen = 0,
480 .mode = 0600,
481 .proc_handler = &pm_do_sleep
482 },
Manuel Lauss564365b2008-12-21 09:26:25 +0100483#if !defined(CONFIG_SOC_AU1200) && !defined(CONFIG_SOC_AU1550)
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800484 {
485 .ctl_name = CTL_UNNUMBERED,
486 .procname = "freq",
487 .data = NULL,
488 .maxlen = 0,
489 .mode = 0600,
490 .proc_handler = &pm_do_freq
491 },
Manuel Lauss564365b2008-12-21 09:26:25 +0100492#endif
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800493 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494};
495
496static struct ctl_table pm_dir_table[] = {
Eric W. Biederman7ed744d2007-02-14 00:33:43 -0800497 {
498 .ctl_name = CTL_UNNUMBERED,
499 .procname = "pm",
500 .mode = 0555,
501 .child = pm_table
502 },
503 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504};
505
506/*
507 * Initialize power interface
508 */
509static int __init pm_init(void)
510{
Manuel Lauss0c694de2008-12-21 09:26:23 +0100511 /* init TOY to tick at 1Hz. No need to wait for access bits
512 * since there's plenty of time between here and the first
513 * suspend cycle.
514 */
515 if (au_readl(SYS_TOYTRIM) != 32767) {
516 au_writel(32767, SYS_TOYTRIM);
517 au_sync();
518 }
519
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800520 register_sysctl_table(pm_dir_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522}
523
524__initcall(pm_init);
525
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400526#endif /* CONFIG_PM */