Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * BRIEF MODULE DESCRIPTION |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 3 | * Au1xx0 Power Management routines. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 5 | * Copyright 2001, 2008 MontaVista Software Inc. |
| 6 | * Author: MontaVista Software, Inc. <source@mvista.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 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 Shtylyov | ce28f94 | 2008-04-23 22:43:55 +0400 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/init.h> |
| 33 | #include <linux/pm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/sysctl.h> |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 35 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 38 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/mach-au1x00/au1000.h> |
| 40 | |
| 41 | #ifdef CONFIG_PM |
| 42 | |
| 43 | #define DEBUG 1 |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 44 | #ifdef DEBUG |
| 45 | #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__, ## args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #else |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 47 | #define DPRINTK(fmt, args...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #endif |
| 49 | |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 50 | static void au1000_calibrate_delay(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | extern unsigned long save_local_and_disable(int controller); |
| 53 | extern void restore_local_and_enable(int controller, unsigned long mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static DEFINE_SPINLOCK(pm_lock); |
| 56 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 57 | /* |
| 58 | * We need to save/restore a bunch of core registers that are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * 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 Baechle | fc10334 | 2006-06-28 11:24:12 +0100 | [diff] [blame] | 67 | static unsigned int sleep_aux_pll_cntrl; |
| 68 | static unsigned int sleep_cpu_pll_cntrl; |
| 69 | static unsigned int sleep_pin_function; |
| 70 | static unsigned int sleep_uart0_inten; |
| 71 | static unsigned int sleep_uart0_fifoctl; |
| 72 | static unsigned int sleep_uart0_linectl; |
| 73 | static unsigned int sleep_uart0_clkdiv; |
| 74 | static unsigned int sleep_uart0_enable; |
| 75 | static unsigned int sleep_usbhost_enable; |
| 76 | static unsigned int sleep_usbdev_enable; |
| 77 | static unsigned int sleep_static_memctlr[4][3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 79 | /* |
| 80 | * Define this to cause the value you write to /proc/sys/pm/sleep to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * 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 Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 86 | #ifdef SLEEP_TEST_TIMEOUT |
| 87 | static int sleep_ticks; |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 88 | static void wakeup_counter0_set(int ticks) |
| 89 | { |
| 90 | au_writel(au_readl(SYS_TOYREAD) + ticks, SYS_TOYMATCH2); |
| 91 | au_sync(); |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #endif |
| 94 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 95 | static void save_core_regs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 97 | extern void save_au1xxx_intctl(void); |
| 98 | extern void pm_eth0_shutdown(void); |
| 99 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 100 | /* |
| 101 | * Do the serial ports.....these really should be a pm_* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * registered function by the driver......but of course the |
| 103 | * standard serial driver doesn't understand our Au1xxx |
| 104 | * unique registers. |
| 105 | */ |
| 106 | sleep_uart0_inten = au_readl(UART0_ADDR + UART_IER); |
| 107 | sleep_uart0_fifoctl = au_readl(UART0_ADDR + UART_FCR); |
| 108 | sleep_uart0_linectl = au_readl(UART0_ADDR + UART_LCR); |
| 109 | sleep_uart0_clkdiv = au_readl(UART0_ADDR + UART_CLK); |
| 110 | sleep_uart0_enable = au_readl(UART0_ADDR + UART_MOD_CNTRL); |
| 111 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 112 | /* Shutdown USB host/device. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | sleep_usbhost_enable = au_readl(USB_HOST_CONFIG); |
| 114 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 115 | /* There appears to be some undocumented reset register.... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | au_writel(0, 0xb0100004); au_sync(); |
| 117 | au_writel(0, USB_HOST_CONFIG); au_sync(); |
| 118 | |
| 119 | sleep_usbdev_enable = au_readl(USBD_ENABLE); |
| 120 | au_writel(0, USBD_ENABLE); au_sync(); |
| 121 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 122 | /* Save interrupt controller state. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | save_au1xxx_intctl(); |
| 124 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 125 | /* Clocks and PLLs. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | sleep_aux_pll_cntrl = au_readl(SYS_AUXPLL); |
| 127 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 128 | /* |
| 129 | * We don't really need to do this one, but unless we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | * write it again it won't have a valid value if we |
| 131 | * happen to read it. |
| 132 | */ |
| 133 | sleep_cpu_pll_cntrl = au_readl(SYS_CPUPLL); |
| 134 | |
| 135 | sleep_pin_function = au_readl(SYS_PINFUNC); |
| 136 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 137 | /* Save the static memory controller configuration. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | sleep_static_memctlr[0][0] = au_readl(MEM_STCFG0); |
| 139 | sleep_static_memctlr[0][1] = au_readl(MEM_STTIME0); |
| 140 | sleep_static_memctlr[0][2] = au_readl(MEM_STADDR0); |
| 141 | sleep_static_memctlr[1][0] = au_readl(MEM_STCFG1); |
| 142 | sleep_static_memctlr[1][1] = au_readl(MEM_STTIME1); |
| 143 | sleep_static_memctlr[1][2] = au_readl(MEM_STADDR1); |
| 144 | sleep_static_memctlr[2][0] = au_readl(MEM_STCFG2); |
| 145 | sleep_static_memctlr[2][1] = au_readl(MEM_STTIME2); |
| 146 | sleep_static_memctlr[2][2] = au_readl(MEM_STADDR2); |
| 147 | sleep_static_memctlr[3][0] = au_readl(MEM_STCFG3); |
| 148 | sleep_static_memctlr[3][1] = au_readl(MEM_STTIME3); |
| 149 | sleep_static_memctlr[3][2] = au_readl(MEM_STADDR3); |
| 150 | } |
| 151 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 152 | static void restore_core_regs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | extern void restore_au1xxx_intctl(void); |
| 155 | extern void wakeup_counter0_adjust(void); |
| 156 | |
| 157 | au_writel(sleep_aux_pll_cntrl, SYS_AUXPLL); au_sync(); |
| 158 | au_writel(sleep_cpu_pll_cntrl, SYS_CPUPLL); au_sync(); |
| 159 | au_writel(sleep_pin_function, SYS_PINFUNC); au_sync(); |
| 160 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 161 | /* Restore the static memory controller configuration. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | au_writel(sleep_static_memctlr[0][0], MEM_STCFG0); |
| 163 | au_writel(sleep_static_memctlr[0][1], MEM_STTIME0); |
| 164 | au_writel(sleep_static_memctlr[0][2], MEM_STADDR0); |
| 165 | au_writel(sleep_static_memctlr[1][0], MEM_STCFG1); |
| 166 | au_writel(sleep_static_memctlr[1][1], MEM_STTIME1); |
| 167 | au_writel(sleep_static_memctlr[1][2], MEM_STADDR1); |
| 168 | au_writel(sleep_static_memctlr[2][0], MEM_STCFG2); |
| 169 | au_writel(sleep_static_memctlr[2][1], MEM_STTIME2); |
| 170 | au_writel(sleep_static_memctlr[2][2], MEM_STADDR2); |
| 171 | au_writel(sleep_static_memctlr[3][0], MEM_STCFG3); |
| 172 | au_writel(sleep_static_memctlr[3][1], MEM_STTIME3); |
| 173 | au_writel(sleep_static_memctlr[3][2], MEM_STADDR3); |
| 174 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 175 | /* |
| 176 | * Enable the UART if it was enabled before sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | * I guess I should define module control bits........ |
| 178 | */ |
| 179 | if (sleep_uart0_enable & 0x02) { |
| 180 | au_writel(0, UART0_ADDR + UART_MOD_CNTRL); au_sync(); |
| 181 | au_writel(1, UART0_ADDR + UART_MOD_CNTRL); au_sync(); |
| 182 | au_writel(3, UART0_ADDR + UART_MOD_CNTRL); au_sync(); |
| 183 | au_writel(sleep_uart0_inten, UART0_ADDR + UART_IER); au_sync(); |
| 184 | au_writel(sleep_uart0_fifoctl, UART0_ADDR + UART_FCR); au_sync(); |
| 185 | au_writel(sleep_uart0_linectl, UART0_ADDR + UART_LCR); au_sync(); |
| 186 | au_writel(sleep_uart0_clkdiv, UART0_ADDR + UART_CLK); au_sync(); |
| 187 | } |
| 188 | |
| 189 | restore_au1xxx_intctl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | unsigned long suspend_mode; |
| 193 | |
| 194 | void wakeup_from_suspend(void) |
| 195 | { |
| 196 | suspend_mode = 0; |
| 197 | } |
| 198 | |
| 199 | int au_sleep(void) |
| 200 | { |
| 201 | unsigned long wakeup, flags; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 202 | extern void save_and_sleep(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 204 | spin_lock_irqsave(&pm_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | save_core_regs(); |
| 207 | |
| 208 | flush_cache_all(); |
| 209 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 210 | /** |
| 211 | ** The code below is all system dependent and we should probably |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | ** have a function call out of here to set this up. You need |
| 213 | ** to configure the GPIO or timer interrupts that will bring |
| 214 | ** you out of sleep. |
| 215 | ** For testing, the TOY counter wakeup is useful. |
| 216 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | #if 0 |
| 218 | au_writel(au_readl(SYS_PINSTATERD) & ~(1 << 11), SYS_PINSTATERD); |
| 219 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 220 | /* GPIO 6 can cause a wake up event */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | wakeup = au_readl(SYS_WAKEMSK); |
| 222 | wakeup &= ~(1 << 8); /* turn off match20 wakeup */ |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 223 | wakeup |= 1 << 6; /* turn on GPIO 6 wakeup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | #else |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 225 | /* For testing, allow match20 to wake us up. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | #ifdef SLEEP_TEST_TIMEOUT |
| 227 | wakeup_counter0_set(sleep_ticks); |
| 228 | #endif |
| 229 | wakeup = 1 << 8; /* turn on match20 wakeup */ |
| 230 | wakeup = 0; |
| 231 | #endif |
| 232 | au_writel(1, SYS_WAKESRC); /* clear cause */ |
| 233 | au_sync(); |
| 234 | au_writel(wakeup, SYS_WAKEMSK); |
| 235 | au_sync(); |
| 236 | |
| 237 | save_and_sleep(); |
| 238 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 239 | /* |
| 240 | * After a wakeup, the cpu vectors back to 0x1fc00000, so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | * it's up to the boot code to get us back here. |
| 242 | */ |
| 243 | restore_core_regs(); |
| 244 | spin_unlock_irqrestore(&pm_lock, flags); |
| 245 | return 0; |
| 246 | } |
| 247 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 248 | static int pm_do_sleep(ctl_table *ctl, int write, struct file *file, |
| 249 | void __user *buffer, size_t *len, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | #ifdef SLEEP_TEST_TIMEOUT |
| 252 | #define TMPBUFLEN2 16 |
| 253 | char buf[TMPBUFLEN2], *p; |
| 254 | #endif |
| 255 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 256 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | *len = 0; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 258 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | #ifdef SLEEP_TEST_TIMEOUT |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 260 | if (*len > TMPBUFLEN2 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | return -EFAULT; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 262 | if (copy_from_user(buf, buffer, *len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | buf[*len] = 0; |
| 265 | p = buf; |
| 266 | sleep_ticks = simple_strtoul(p, &p, 0); |
| 267 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | au_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
Pavel Machek | 6afe1a1 | 2008-03-13 23:52:49 +0100 | [diff] [blame] | 271 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 274 | static int pm_do_freq(ctl_table *ctl, int write, struct file *file, |
| 275 | void __user *buffer, size_t *len, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | int retval = 0, i; |
| 278 | unsigned long val, pll; |
| 279 | #define TMPBUFLEN 64 |
| 280 | #define MAX_CPU_FREQ 396 |
| 281 | char buf[TMPBUFLEN], *p; |
| 282 | unsigned long flags, intc0_mask, intc1_mask; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 283 | unsigned long old_baud_base, old_cpu_freq, old_clk, old_refresh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | unsigned long new_baud_base, new_cpu_freq, new_clk, new_refresh; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 285 | unsigned long baud_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | spin_lock_irqsave(&pm_lock, flags); |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 288 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | *len = 0; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 290 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | /* Parse the new frequency */ |
| 292 | if (*len > TMPBUFLEN - 1) { |
| 293 | spin_unlock_irqrestore(&pm_lock, flags); |
| 294 | return -EFAULT; |
| 295 | } |
| 296 | if (copy_from_user(buf, buffer, *len)) { |
| 297 | spin_unlock_irqrestore(&pm_lock, flags); |
| 298 | return -EFAULT; |
| 299 | } |
| 300 | buf[*len] = 0; |
| 301 | p = buf; |
| 302 | val = simple_strtoul(p, &p, 0); |
| 303 | if (val > MAX_CPU_FREQ) { |
| 304 | spin_unlock_irqrestore(&pm_lock, flags); |
| 305 | return -EFAULT; |
| 306 | } |
| 307 | |
| 308 | pll = val / 12; |
| 309 | if ((pll > 33) || (pll < 7)) { /* 396 MHz max, 84 MHz min */ |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 310 | /* Revisit this for higher speed CPUs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | spin_unlock_irqrestore(&pm_lock, flags); |
| 312 | return -EFAULT; |
| 313 | } |
| 314 | |
| 315 | old_baud_base = get_au1x00_uart_baud_base(); |
| 316 | old_cpu_freq = get_au1x00_speed(); |
| 317 | |
| 318 | new_cpu_freq = pll * 12 * 1000000; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 319 | new_baud_base = (new_cpu_freq / (2 * ((int)(au_readl(SYS_POWERCTRL) |
| 320 | & 0x03) + 2) * 16)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | set_au1x00_speed(new_cpu_freq); |
| 322 | set_au1x00_uart_baud_base(new_baud_base); |
| 323 | |
| 324 | old_refresh = au_readl(MEM_SDREFCFG) & 0x1ffffff; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 325 | new_refresh = ((old_refresh * new_cpu_freq) / old_cpu_freq) | |
| 326 | (au_readl(MEM_SDREFCFG) & ~0x1ffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | au_writel(pll, SYS_CPUPLL); |
| 329 | au_sync_delay(1); |
| 330 | au_writel(new_refresh, MEM_SDREFCFG); |
| 331 | au_sync_delay(1); |
| 332 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 333 | for (i = 0; i < 4; i++) |
| 334 | if (au_readl(UART_BASE + UART_MOD_CNTRL + |
| 335 | i * 0x00100000) == 3) { |
| 336 | old_clk = au_readl(UART_BASE + UART_CLK + |
| 337 | i * 0x00100000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | baud_rate = old_baud_base / old_clk; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 339 | /* |
| 340 | * We won't get an exact baud rate and the error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | * could be significant enough that our new |
| 342 | * calculation will result in a clock that will |
| 343 | * give us a baud rate that's too far off from |
| 344 | * what we really want. |
| 345 | */ |
| 346 | if (baud_rate > 100000) |
| 347 | baud_rate = 115200; |
| 348 | else if (baud_rate > 50000) |
| 349 | baud_rate = 57600; |
| 350 | else if (baud_rate > 30000) |
| 351 | baud_rate = 38400; |
| 352 | else if (baud_rate > 17000) |
| 353 | baud_rate = 19200; |
| 354 | else |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 355 | baud_rate = 9600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | new_clk = new_baud_base / baud_rate; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 357 | au_writel(new_clk, UART_BASE + UART_CLK + |
| 358 | i * 0x00100000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | au_sync_delay(10); |
| 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Ralf Baechle | c30db24 | 2007-10-17 15:36:53 +0100 | [diff] [blame] | 363 | /* |
| 364 | * We don't want _any_ interrupts other than match20. Otherwise our |
| 365 | * au1000_calibrate_delay() calculation will be off, potentially a lot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | */ |
| 367 | intc0_mask = save_local_and_disable(0); |
| 368 | intc1_mask = save_local_and_disable(1); |
Manuel Lauss | 785e326 | 2008-12-21 09:26:17 +0100 | [diff] [blame] | 369 | val = 1 << (AU1000_TOY_MATCH2_INT - AU1000_INTC0_INT_BASE); |
| 370 | au_writel(val, IC0_MASKSET); /* unmask */ |
| 371 | au_writel(val, IC0_WAKESET); /* enable wake-from-sleep */ |
| 372 | au_sync(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | spin_unlock_irqrestore(&pm_lock, flags); |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 374 | au1000_calibrate_delay(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | restore_local_and_enable(0, intc0_mask); |
| 376 | restore_local_and_enable(1, intc1_mask); |
Ralf Baechle | c30db24 | 2007-10-17 15:36:53 +0100 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | return retval; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | static struct ctl_table pm_table[] = { |
Eric W. Biederman | 7ed744d | 2007-02-14 00:33:43 -0800 | [diff] [blame] | 383 | { |
Eric W. Biederman | 7ed744d | 2007-02-14 00:33:43 -0800 | [diff] [blame] | 384 | .ctl_name = CTL_UNNUMBERED, |
| 385 | .procname = "sleep", |
| 386 | .data = NULL, |
| 387 | .maxlen = 0, |
| 388 | .mode = 0600, |
| 389 | .proc_handler = &pm_do_sleep |
| 390 | }, |
| 391 | { |
| 392 | .ctl_name = CTL_UNNUMBERED, |
| 393 | .procname = "freq", |
| 394 | .data = NULL, |
| 395 | .maxlen = 0, |
| 396 | .mode = 0600, |
| 397 | .proc_handler = &pm_do_freq |
| 398 | }, |
| 399 | {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | static struct ctl_table pm_dir_table[] = { |
Eric W. Biederman | 7ed744d | 2007-02-14 00:33:43 -0800 | [diff] [blame] | 403 | { |
| 404 | .ctl_name = CTL_UNNUMBERED, |
| 405 | .procname = "pm", |
| 406 | .mode = 0555, |
| 407 | .child = pm_table |
| 408 | }, |
| 409 | {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | /* |
| 413 | * Initialize power interface |
| 414 | */ |
| 415 | static int __init pm_init(void) |
| 416 | { |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 417 | /* init TOY to tick at 1Hz. No need to wait for access bits |
| 418 | * since there's plenty of time between here and the first |
| 419 | * suspend cycle. |
| 420 | */ |
| 421 | if (au_readl(SYS_TOYTRIM) != 32767) { |
| 422 | au_writel(32767, SYS_TOYTRIM); |
| 423 | au_sync(); |
| 424 | } |
| 425 | |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 426 | register_sysctl_table(pm_dir_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | __initcall(pm_init); |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | /* |
| 433 | * This is right out of init/main.c |
| 434 | */ |
| 435 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 436 | /* |
| 437 | * This is the number of bits of precision for the loops_per_jiffy. |
| 438 | * Each bit takes on average 1.5/HZ seconds. This (like the original) |
| 439 | * is a little better than 1%. |
| 440 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | #define LPS_PREC 8 |
| 442 | |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 443 | static void au1000_calibrate_delay(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | unsigned long ticks, loopbit; |
| 446 | int lps_precision = LPS_PREC; |
| 447 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 448 | loops_per_jiffy = 1 << 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | while (loops_per_jiffy <<= 1) { |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 451 | /* Wait for "start of" clock tick */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | ticks = jiffies; |
| 453 | while (ticks == jiffies) |
| 454 | /* nothing */ ; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 455 | /* Go ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | ticks = jiffies; |
| 457 | __delay(loops_per_jiffy); |
| 458 | ticks = jiffies - ticks; |
| 459 | if (ticks) |
| 460 | break; |
| 461 | } |
| 462 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 463 | /* |
| 464 | * Do a binary approximation to get loops_per_jiffy set to be equal |
| 465 | * one clock (up to lps_precision bits) |
| 466 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | loops_per_jiffy >>= 1; |
| 468 | loopbit = loops_per_jiffy; |
| 469 | while (lps_precision-- && (loopbit >>= 1)) { |
| 470 | loops_per_jiffy |= loopbit; |
| 471 | ticks = jiffies; |
| 472 | while (ticks == jiffies); |
| 473 | ticks = jiffies; |
| 474 | __delay(loops_per_jiffy); |
| 475 | if (jiffies != ticks) /* longer than 1 tick */ |
| 476 | loops_per_jiffy &= ~loopbit; |
| 477 | } |
| 478 | } |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 479 | #endif /* CONFIG_PM */ |