power.c 14 KB

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