power.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * BRIEF MODULE DESCRIPTION
  3. * Au1000 Power Management routines.
  4. *
  5. * Copyright 2001 MontaVista Software Inc.
  6. * Author: MontaVista Software, Inc.
  7. * ppopov@mvista.com or source@mvista.com
  8. *
  9. * Some of the routines are right out of init/main.c, whose
  10. * copyrights apply here.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  20. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  23. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * You should have received a copy of the GNU General Public License along
  29. * with this program; if not, write to the Free Software Foundation, Inc.,
  30. * 675 Mass Ave, Cambridge, MA 02139, USA.
  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. #include <asm/uaccess.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/mach-au1x00/au1000.h>
  40. #ifdef CONFIG_PM
  41. #define DEBUG 1
  42. #ifdef DEBUG
  43. # define DPRINTK(fmt, args...) printk("%s: " fmt, __func__, ## args)
  44. #else
  45. # define DPRINTK(fmt, args...)
  46. #endif
  47. static void au1000_calibrate_delay(void);
  48. extern unsigned long save_local_and_disable(int controller);
  49. extern void restore_local_and_enable(int controller, unsigned long mask);
  50. extern void local_enable_irq(unsigned int irq_nr);
  51. static DEFINE_SPINLOCK(pm_lock);
  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. /* 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
  83. save_core_regs(void)
  84. {
  85. extern void save_au1xxx_intctl(void);
  86. extern void pm_eth0_shutdown(void);
  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. */
  99. sleep_usbhost_enable = au_readl(USB_HOST_CONFIG);
  100. /* There appears to be some undocumented reset register....
  101. */
  102. au_writel(0, 0xb0100004); au_sync();
  103. au_writel(0, USB_HOST_CONFIG); au_sync();
  104. sleep_usbdev_enable = au_readl(USBD_ENABLE);
  105. au_writel(0, USBD_ENABLE); au_sync();
  106. /* Save interrupt controller state.
  107. */
  108. save_au1xxx_intctl();
  109. /* Clocks and PLLs.
  110. */
  111. sleep_aux_pll_cntrl = au_readl(SYS_AUXPLL);
  112. /* We don't really need to do this one, but unless we
  113. * write it again it won't have a valid value if we
  114. * happen to read it.
  115. */
  116. sleep_cpu_pll_cntrl = au_readl(SYS_CPUPLL);
  117. sleep_pin_function = au_readl(SYS_PINFUNC);
  118. /* Save the static memory controller configuration.
  119. */
  120. sleep_static_memctlr[0][0] = au_readl(MEM_STCFG0);
  121. sleep_static_memctlr[0][1] = au_readl(MEM_STTIME0);
  122. sleep_static_memctlr[0][2] = au_readl(MEM_STADDR0);
  123. sleep_static_memctlr[1][0] = au_readl(MEM_STCFG1);
  124. sleep_static_memctlr[1][1] = au_readl(MEM_STTIME1);
  125. sleep_static_memctlr[1][2] = au_readl(MEM_STADDR1);
  126. sleep_static_memctlr[2][0] = au_readl(MEM_STCFG2);
  127. sleep_static_memctlr[2][1] = au_readl(MEM_STTIME2);
  128. sleep_static_memctlr[2][2] = au_readl(MEM_STADDR2);
  129. sleep_static_memctlr[3][0] = au_readl(MEM_STCFG3);
  130. sleep_static_memctlr[3][1] = au_readl(MEM_STTIME3);
  131. sleep_static_memctlr[3][2] = au_readl(MEM_STADDR3);
  132. }
  133. static void
  134. restore_core_regs(void)
  135. {
  136. extern void restore_au1xxx_intctl(void);
  137. extern void wakeup_counter0_adjust(void);
  138. au_writel(sleep_aux_pll_cntrl, SYS_AUXPLL); au_sync();
  139. au_writel(sleep_cpu_pll_cntrl, SYS_CPUPLL); au_sync();
  140. au_writel(sleep_pin_function, SYS_PINFUNC); au_sync();
  141. /* Restore the static memory controller configuration.
  142. */
  143. au_writel(sleep_static_memctlr[0][0], MEM_STCFG0);
  144. au_writel(sleep_static_memctlr[0][1], MEM_STTIME0);
  145. au_writel(sleep_static_memctlr[0][2], MEM_STADDR0);
  146. au_writel(sleep_static_memctlr[1][0], MEM_STCFG1);
  147. au_writel(sleep_static_memctlr[1][1], MEM_STTIME1);
  148. au_writel(sleep_static_memctlr[1][2], MEM_STADDR1);
  149. au_writel(sleep_static_memctlr[2][0], MEM_STCFG2);
  150. au_writel(sleep_static_memctlr[2][1], MEM_STTIME2);
  151. au_writel(sleep_static_memctlr[2][2], MEM_STADDR2);
  152. au_writel(sleep_static_memctlr[3][0], MEM_STCFG3);
  153. au_writel(sleep_static_memctlr[3][1], MEM_STTIME3);
  154. au_writel(sleep_static_memctlr[3][2], MEM_STADDR3);
  155. /* Enable the UART if it was enabled before sleep.
  156. * I guess I should define module control bits........
  157. */
  158. if (sleep_uart0_enable & 0x02) {
  159. au_writel(0, UART0_ADDR + UART_MOD_CNTRL); au_sync();
  160. au_writel(1, UART0_ADDR + UART_MOD_CNTRL); au_sync();
  161. au_writel(3, UART0_ADDR + UART_MOD_CNTRL); au_sync();
  162. au_writel(sleep_uart0_inten, UART0_ADDR + UART_IER); au_sync();
  163. au_writel(sleep_uart0_fifoctl, UART0_ADDR + UART_FCR); au_sync();
  164. au_writel(sleep_uart0_linectl, UART0_ADDR + UART_LCR); au_sync();
  165. au_writel(sleep_uart0_clkdiv, UART0_ADDR + UART_CLK); au_sync();
  166. }
  167. restore_au1xxx_intctl();
  168. wakeup_counter0_adjust();
  169. }
  170. unsigned long suspend_mode;
  171. void wakeup_from_suspend(void)
  172. {
  173. suspend_mode = 0;
  174. }
  175. int au_sleep(void)
  176. {
  177. unsigned long wakeup, flags;
  178. extern void save_and_sleep(void);
  179. spin_lock_irqsave(&pm_lock, flags);
  180. save_core_regs();
  181. flush_cache_all();
  182. /** The code below is all system dependent and we should probably
  183. ** have a function call out of here to set this up. You need
  184. ** to configure the GPIO or timer interrupts that will bring
  185. ** you out of sleep.
  186. ** For testing, the TOY counter wakeup is useful.
  187. **/
  188. #if 0
  189. au_writel(au_readl(SYS_PINSTATERD) & ~(1 << 11), SYS_PINSTATERD);
  190. /* gpio 6 can cause a wake up event */
  191. wakeup = au_readl(SYS_WAKEMSK);
  192. wakeup &= ~(1 << 8); /* turn off match20 wakeup */
  193. wakeup |= 1 << 6; /* turn on gpio 6 wakeup */
  194. #else
  195. /* For testing, allow match20 to wake us up.
  196. */
  197. #ifdef SLEEP_TEST_TIMEOUT
  198. wakeup_counter0_set(sleep_ticks);
  199. #endif
  200. wakeup = 1 << 8; /* turn on match20 wakeup */
  201. wakeup = 0;
  202. #endif
  203. au_writel(1, SYS_WAKESRC); /* clear cause */
  204. au_sync();
  205. au_writel(wakeup, SYS_WAKEMSK);
  206. au_sync();
  207. save_and_sleep();
  208. /* after a wakeup, the cpu vectors back to 0x1fc00000 so
  209. * it's up to the boot code to get us back here.
  210. */
  211. restore_core_regs();
  212. spin_unlock_irqrestore(&pm_lock, flags);
  213. return 0;
  214. }
  215. static int pm_do_sleep(ctl_table * ctl, int write, struct file *file,
  216. void __user *buffer, size_t * len, loff_t *ppos)
  217. {
  218. int retval = 0;
  219. #ifdef SLEEP_TEST_TIMEOUT
  220. #define TMPBUFLEN2 16
  221. char buf[TMPBUFLEN2], *p;
  222. #endif
  223. if (!write) {
  224. *len = 0;
  225. } else {
  226. #ifdef SLEEP_TEST_TIMEOUT
  227. if (*len > TMPBUFLEN2 - 1) {
  228. return -EFAULT;
  229. }
  230. if (copy_from_user(buf, buffer, *len)) {
  231. return -EFAULT;
  232. }
  233. buf[*len] = 0;
  234. p = buf;
  235. sleep_ticks = simple_strtoul(p, &p, 0);
  236. #endif
  237. retval = pm_send_all(PM_SUSPEND, (void *) 2);
  238. if (retval)
  239. return retval;
  240. au_sleep();
  241. retval = pm_send_all(PM_RESUME, (void *) 0);
  242. }
  243. return retval;
  244. }
  245. static int pm_do_suspend(ctl_table * ctl, int write, struct file *file,
  246. void __user *buffer, size_t * len, loff_t *ppos)
  247. {
  248. int retval = 0;
  249. if (!write) {
  250. *len = 0;
  251. } else {
  252. retval = pm_send_all(PM_SUSPEND, (void *) 2);
  253. if (retval)
  254. return retval;
  255. suspend_mode = 1;
  256. retval = pm_send_all(PM_RESUME, (void *) 0);
  257. }
  258. return retval;
  259. }
  260. static int pm_do_freq(ctl_table * ctl, int write, struct file *file,
  261. void __user *buffer, size_t * len, loff_t *ppos)
  262. {
  263. int retval = 0, i;
  264. unsigned long val, pll;
  265. #define TMPBUFLEN 64
  266. #define MAX_CPU_FREQ 396
  267. char buf[TMPBUFLEN], *p;
  268. unsigned long flags, intc0_mask, intc1_mask;
  269. unsigned long old_baud_base, old_cpu_freq, baud_rate, old_clk,
  270. old_refresh;
  271. unsigned long new_baud_base, new_cpu_freq, new_clk, new_refresh;
  272. spin_lock_irqsave(&pm_lock, flags);
  273. if (!write) {
  274. *len = 0;
  275. } else {
  276. /* Parse the new frequency */
  277. if (*len > TMPBUFLEN - 1) {
  278. spin_unlock_irqrestore(&pm_lock, flags);
  279. return -EFAULT;
  280. }
  281. if (copy_from_user(buf, buffer, *len)) {
  282. spin_unlock_irqrestore(&pm_lock, flags);
  283. return -EFAULT;
  284. }
  285. buf[*len] = 0;
  286. p = buf;
  287. val = simple_strtoul(p, &p, 0);
  288. if (val > MAX_CPU_FREQ) {
  289. spin_unlock_irqrestore(&pm_lock, flags);
  290. return -EFAULT;
  291. }
  292. pll = val / 12;
  293. if ((pll > 33) || (pll < 7)) { /* 396 MHz max, 84 MHz min */
  294. /* revisit this for higher speed cpus */
  295. spin_unlock_irqrestore(&pm_lock, flags);
  296. return -EFAULT;
  297. }
  298. old_baud_base = get_au1x00_uart_baud_base();
  299. old_cpu_freq = get_au1x00_speed();
  300. new_cpu_freq = pll * 12 * 1000000;
  301. new_baud_base = (new_cpu_freq / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
  302. set_au1x00_speed(new_cpu_freq);
  303. set_au1x00_uart_baud_base(new_baud_base);
  304. old_refresh = au_readl(MEM_SDREFCFG) & 0x1ffffff;
  305. new_refresh =
  306. ((old_refresh * new_cpu_freq) /
  307. old_cpu_freq) | (au_readl(MEM_SDREFCFG) & ~0x1ffffff);
  308. au_writel(pll, SYS_CPUPLL);
  309. au_sync_delay(1);
  310. au_writel(new_refresh, MEM_SDREFCFG);
  311. au_sync_delay(1);
  312. for (i = 0; i < 4; i++) {
  313. if (au_readl
  314. (UART_BASE + UART_MOD_CNTRL +
  315. i * 0x00100000) == 3) {
  316. old_clk =
  317. au_readl(UART_BASE + UART_CLK +
  318. i * 0x00100000);
  319. // baud_rate = baud_base/clk
  320. baud_rate = old_baud_base / old_clk;
  321. /* we won't get an exact baud rate and the error
  322. * could be significant enough that our new
  323. * calculation will result in a clock that will
  324. * give us a baud rate that's too far off from
  325. * what we really want.
  326. */
  327. if (baud_rate > 100000)
  328. baud_rate = 115200;
  329. else if (baud_rate > 50000)
  330. baud_rate = 57600;
  331. else if (baud_rate > 30000)
  332. baud_rate = 38400;
  333. else if (baud_rate > 17000)
  334. baud_rate = 19200;
  335. else
  336. (baud_rate = 9600);
  337. // new_clk = new_baud_base/baud_rate
  338. new_clk = new_baud_base / baud_rate;
  339. au_writel(new_clk,
  340. UART_BASE + UART_CLK +
  341. i * 0x00100000);
  342. au_sync_delay(10);
  343. }
  344. }
  345. }
  346. /*
  347. * We don't want _any_ interrupts other than match20. Otherwise our
  348. * au1000_calibrate_delay() calculation will be off, potentially a lot.
  349. */
  350. intc0_mask = save_local_and_disable(0);
  351. intc1_mask = save_local_and_disable(1);
  352. local_enable_irq(AU1000_TOY_MATCH2_INT);
  353. spin_unlock_irqrestore(&pm_lock, flags);
  354. au1000_calibrate_delay();
  355. restore_local_and_enable(0, intc0_mask);
  356. restore_local_and_enable(1, intc1_mask);
  357. return retval;
  358. }
  359. static struct ctl_table pm_table[] = {
  360. {
  361. .ctl_name = CTL_UNNUMBERED,
  362. .procname = "suspend",
  363. .data = NULL,
  364. .maxlen = 0,
  365. .mode = 0600,
  366. .proc_handler = &pm_do_suspend
  367. },
  368. {
  369. .ctl_name = CTL_UNNUMBERED,
  370. .procname = "sleep",
  371. .data = NULL,
  372. .maxlen = 0,
  373. .mode = 0600,
  374. .proc_handler = &pm_do_sleep
  375. },
  376. {
  377. .ctl_name = CTL_UNNUMBERED,
  378. .procname = "freq",
  379. .data = NULL,
  380. .maxlen = 0,
  381. .mode = 0600,
  382. .proc_handler = &pm_do_freq
  383. },
  384. {}
  385. };
  386. static struct ctl_table pm_dir_table[] = {
  387. {
  388. .ctl_name = CTL_UNNUMBERED,
  389. .procname = "pm",
  390. .mode = 0555,
  391. .child = pm_table
  392. },
  393. {}
  394. };
  395. /*
  396. * Initialize power interface
  397. */
  398. static int __init pm_init(void)
  399. {
  400. register_sysctl_table(pm_dir_table);
  401. return 0;
  402. }
  403. __initcall(pm_init);
  404. /*
  405. * This is right out of init/main.c
  406. */
  407. /* This is the number of bits of precision for the loops_per_jiffy. Each
  408. bit takes on average 1.5/HZ seconds. This (like the original) is a little
  409. better than 1% */
  410. #define LPS_PREC 8
  411. static void au1000_calibrate_delay(void)
  412. {
  413. unsigned long ticks, loopbit;
  414. int lps_precision = LPS_PREC;
  415. loops_per_jiffy = (1 << 12);
  416. while (loops_per_jiffy <<= 1) {
  417. /* wait for "start of" clock tick */
  418. ticks = jiffies;
  419. while (ticks == jiffies)
  420. /* nothing */ ;
  421. /* Go .. */
  422. ticks = jiffies;
  423. __delay(loops_per_jiffy);
  424. ticks = jiffies - ticks;
  425. if (ticks)
  426. break;
  427. }
  428. /* Do a binary approximation to get loops_per_jiffy set to equal one clock
  429. (up to lps_precision bits) */
  430. loops_per_jiffy >>= 1;
  431. loopbit = loops_per_jiffy;
  432. while (lps_precision-- && (loopbit >>= 1)) {
  433. loops_per_jiffy |= loopbit;
  434. ticks = jiffies;
  435. while (ticks == jiffies);
  436. ticks = jiffies;
  437. __delay(loops_per_jiffy);
  438. if (jiffies != ticks) /* longer than 1 tick */
  439. loops_per_jiffy &= ~loopbit;
  440. }
  441. }
  442. #endif /* CONFIG_PM */