power.c 13 KB

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