pm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * arch/arm/mach-at91/pm.c
  3. * AT91 Power Management
  4. *
  5. * Copyright (C) 2005 David Brownell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/suspend.h>
  13. #include <linux/sched.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <asm/io.h>
  20. #include <asm/irq.h>
  21. #include <asm/atomic.h>
  22. #include <asm/mach/time.h>
  23. #include <asm/mach/irq.h>
  24. #include <asm/mach-types.h>
  25. #include <asm/arch/at91_pmc.h>
  26. #include <asm/arch/gpio.h>
  27. #include <asm/arch/cpu.h>
  28. #include "generic.h"
  29. #ifdef CONFIG_ARCH_AT91RM9200
  30. #include <asm/arch/at91rm9200_mc.h>
  31. /*
  32. * The AT91RM9200 goes into self-refresh mode with this command, and will
  33. * terminate self-refresh automatically on the next SDRAM access.
  34. */
  35. #define sdram_selfrefresh_enable() at91_sys_write(AT91_SDRAMC_SRR, 1)
  36. #define sdram_selfrefresh_disable() do {} while (0)
  37. #elif defined(CONFIG_ARCH_AT91CAP9)
  38. #include <asm/arch/at91cap9_ddrsdr.h>
  39. static u32 saved_lpr;
  40. static inline void sdram_selfrefresh_enable(void)
  41. {
  42. u32 lpr;
  43. saved_lpr = at91_sys_read(AT91_DDRSDRC_LPR);
  44. lpr = saved_lpr & ~AT91_DDRSDRC_LPCB;
  45. at91_sys_write(AT91_DDRSDRC_LPR, lpr | AT91_DDRSDRC_LPCB_SELF_REFRESH);
  46. }
  47. #define sdram_selfrefresh_disable() at91_sys_write(AT91_DDRSDRC_LPR, saved_lpr)
  48. #else
  49. #include <asm/arch/at91sam9_sdramc.h>
  50. static u32 saved_lpr;
  51. static inline void sdram_selfrefresh_enable(void)
  52. {
  53. u32 lpr;
  54. saved_lpr = at91_sys_read(AT91_SDRAMC_LPR);
  55. lpr = saved_lpr & ~AT91_SDRAMC_LPCB;
  56. at91_sys_write(AT91_SDRAMC_LPR, lpr | AT91_SDRAMC_LPCB_SELF_REFRESH);
  57. }
  58. #define sdram_selfrefresh_disable() at91_sys_write(AT91_SDRAMC_LPR, saved_lpr)
  59. /*
  60. * FIXME: The AT91SAM9263 has a second EBI controller which may have
  61. * additional SDRAM. pm_slowclock.S will require a similar fix.
  62. */
  63. #endif
  64. static int at91_pm_valid_state(suspend_state_t state)
  65. {
  66. switch (state) {
  67. case PM_SUSPEND_ON:
  68. case PM_SUSPEND_STANDBY:
  69. case PM_SUSPEND_MEM:
  70. return 1;
  71. default:
  72. return 0;
  73. }
  74. }
  75. static suspend_state_t target_state;
  76. /*
  77. * Called after processes are frozen, but before we shutdown devices.
  78. */
  79. static int at91_pm_begin(suspend_state_t state)
  80. {
  81. target_state = state;
  82. return 0;
  83. }
  84. /*
  85. * Verify that all the clocks are correct before entering
  86. * slow-clock mode.
  87. */
  88. static int at91_pm_verify_clocks(void)
  89. {
  90. unsigned long scsr;
  91. int i;
  92. scsr = at91_sys_read(AT91_PMC_SCSR);
  93. /* USB must not be using PLLB */
  94. if (cpu_is_at91rm9200()) {
  95. if ((scsr & (AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP)) != 0) {
  96. pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
  97. return 0;
  98. }
  99. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()) {
  100. if ((scsr & (AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP)) != 0) {
  101. pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
  102. return 0;
  103. }
  104. } else if (cpu_is_at91cap9()) {
  105. if ((scsr & AT91CAP9_PMC_UHP) != 0) {
  106. pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
  107. return 0;
  108. }
  109. }
  110. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  111. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  112. for (i = 0; i < 4; i++) {
  113. u32 css;
  114. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  115. continue;
  116. css = at91_sys_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  117. if (css != AT91_PMC_CSS_SLOW) {
  118. pr_debug("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  119. return 0;
  120. }
  121. }
  122. #endif
  123. return 1;
  124. }
  125. /*
  126. * Call this from platform driver suspend() to see how deeply to suspend.
  127. * For example, some controllers (like OHCI) need one of the PLL clocks
  128. * in order to act as a wakeup source, and those are not available when
  129. * going into slow clock mode.
  130. *
  131. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  132. * the very same problem (but not using at91 main_clk), and it'd be better
  133. * to add one generic API rather than lots of platform-specific ones.
  134. */
  135. int at91_suspend_entering_slow_clock(void)
  136. {
  137. return (target_state == PM_SUSPEND_MEM);
  138. }
  139. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  140. static void (*slow_clock)(void);
  141. #ifdef CONFIG_AT91_SLOW_CLOCK
  142. extern void at91_slow_clock(void);
  143. extern u32 at91_slow_clock_sz;
  144. #endif
  145. static int at91_pm_enter(suspend_state_t state)
  146. {
  147. at91_gpio_suspend();
  148. at91_irq_suspend();
  149. pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
  150. /* remember all the always-wake irqs */
  151. (at91_sys_read(AT91_PMC_PCSR)
  152. | (1 << AT91_ID_FIQ)
  153. | (1 << AT91_ID_SYS)
  154. | (at91_extern_irq))
  155. & at91_sys_read(AT91_AIC_IMR),
  156. state);
  157. switch (state) {
  158. /*
  159. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  160. * drivers must suspend more deeply: only the master clock
  161. * controller may be using the main oscillator.
  162. */
  163. case PM_SUSPEND_MEM:
  164. /*
  165. * Ensure that clocks are in a valid state.
  166. */
  167. if (!at91_pm_verify_clocks())
  168. goto error;
  169. /*
  170. * Enter slow clock mode by switching over to clk32k and
  171. * turning off the main oscillator; reverse on wakeup.
  172. */
  173. if (slow_clock) {
  174. #ifdef CONFIG_AT91_SLOW_CLOCK
  175. /* copy slow_clock handler to SRAM, and call it */
  176. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  177. #endif
  178. slow_clock();
  179. break;
  180. } else {
  181. pr_info("AT91: PM - no slow clock mode enabled ...\n");
  182. /* FALLTHROUGH leaving master clock alone */
  183. }
  184. /*
  185. * STANDBY mode has *all* drivers suspended; ignores irqs not
  186. * marked as 'wakeup' event sources; and reduces DRAM power.
  187. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  188. * nothing fancy done with main or cpu clocks.
  189. */
  190. case PM_SUSPEND_STANDBY:
  191. /*
  192. * NOTE: the Wait-for-Interrupt instruction needs to be
  193. * in icache so no SDRAM accesses are needed until the
  194. * wakeup IRQ occurs and self-refresh is terminated.
  195. */
  196. asm("b 1f; .align 5; 1:");
  197. asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
  198. sdram_selfrefresh_enable();
  199. asm("mcr p15, 0, r0, c7, c0, 4"); /* wait for interrupt */
  200. sdram_selfrefresh_disable();
  201. break;
  202. case PM_SUSPEND_ON:
  203. asm("mcr p15, 0, r0, c7, c0, 4"); /* wait for interrupt */
  204. break;
  205. default:
  206. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  207. goto error;
  208. }
  209. pr_debug("AT91: PM - wakeup %08x\n",
  210. at91_sys_read(AT91_AIC_IPR) & at91_sys_read(AT91_AIC_IMR));
  211. error:
  212. sdram_selfrefresh_disable();
  213. target_state = PM_SUSPEND_ON;
  214. at91_irq_resume();
  215. at91_gpio_resume();
  216. return 0;
  217. }
  218. /*
  219. * Called right prior to thawing processes.
  220. */
  221. static void at91_pm_end(void)
  222. {
  223. target_state = PM_SUSPEND_ON;
  224. }
  225. static struct platform_suspend_ops at91_pm_ops ={
  226. .valid = at91_pm_valid_state,
  227. .begin = at91_pm_begin,
  228. .enter = at91_pm_enter,
  229. .end = at91_pm_end,
  230. };
  231. static int __init at91_pm_init(void)
  232. {
  233. #ifdef CONFIG_AT91_SLOW_CLOCK
  234. slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
  235. #endif
  236. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  237. #ifdef CONFIG_ARCH_AT91RM9200
  238. /* AT91RM9200 SDRAM low-power mode cannot be used with self-refresh. */
  239. at91_sys_write(AT91_SDRAMC_LPR, 0);
  240. #endif
  241. suspend_set_ops(&at91_pm_ops);
  242. return 0;
  243. }
  244. arch_initcall(at91_pm_init);