power.c 13 KB

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