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