pm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/gpio.h>
  13. #include <linux/suspend.h>
  14. #include <linux/sched.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/io.h>
  21. #include <linux/clk/at91_pmc.h>
  22. #include <asm/irq.h>
  23. #include <linux/atomic.h>
  24. #include <asm/mach/time.h>
  25. #include <asm/mach/irq.h>
  26. #include <mach/cpu.h>
  27. #include <mach/hardware.h>
  28. #include "at91_aic.h"
  29. #include "generic.h"
  30. #include "pm.h"
  31. #include "gpio.h"
  32. static void (*at91_pm_standby)(void);
  33. static int at91_pm_valid_state(suspend_state_t state)
  34. {
  35. switch (state) {
  36. case PM_SUSPEND_ON:
  37. case PM_SUSPEND_STANDBY:
  38. case PM_SUSPEND_MEM:
  39. return 1;
  40. default:
  41. return 0;
  42. }
  43. }
  44. static suspend_state_t target_state;
  45. /*
  46. * Called after processes are frozen, but before we shutdown devices.
  47. */
  48. static int at91_pm_begin(suspend_state_t state)
  49. {
  50. target_state = state;
  51. return 0;
  52. }
  53. /*
  54. * Verify that all the clocks are correct before entering
  55. * slow-clock mode.
  56. */
  57. static int at91_pm_verify_clocks(void)
  58. {
  59. unsigned long scsr;
  60. int i;
  61. scsr = at91_pmc_read(AT91_PMC_SCSR);
  62. /* USB must not be using PLLB */
  63. if (cpu_is_at91rm9200()) {
  64. if ((scsr & (AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP)) != 0) {
  65. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  66. return 0;
  67. }
  68. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()
  69. || cpu_is_at91sam9g20() || cpu_is_at91sam9g10()) {
  70. if ((scsr & (AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP)) != 0) {
  71. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  72. return 0;
  73. }
  74. }
  75. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  76. for (i = 0; i < 4; i++) {
  77. u32 css;
  78. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  79. continue;
  80. css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  81. if (css != AT91_PMC_CSS_SLOW) {
  82. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  83. return 0;
  84. }
  85. }
  86. return 1;
  87. }
  88. /*
  89. * Call this from platform driver suspend() to see how deeply to suspend.
  90. * For example, some controllers (like OHCI) need one of the PLL clocks
  91. * in order to act as a wakeup source, and those are not available when
  92. * going into slow clock mode.
  93. *
  94. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  95. * the very same problem (but not using at91 main_clk), and it'd be better
  96. * to add one generic API rather than lots of platform-specific ones.
  97. */
  98. int at91_suspend_entering_slow_clock(void)
  99. {
  100. return (target_state == PM_SUSPEND_MEM);
  101. }
  102. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  103. static void (*slow_clock)(void __iomem *pmc, void __iomem *ramc0,
  104. void __iomem *ramc1, int memctrl);
  105. #ifdef CONFIG_AT91_SLOW_CLOCK
  106. extern void at91_slow_clock(void __iomem *pmc, void __iomem *ramc0,
  107. void __iomem *ramc1, int memctrl);
  108. extern u32 at91_slow_clock_sz;
  109. #endif
  110. static int at91_pm_enter(suspend_state_t state)
  111. {
  112. if (of_have_populated_dt())
  113. at91_pinctrl_gpio_suspend();
  114. else
  115. at91_gpio_suspend();
  116. if (IS_ENABLED(CONFIG_OLD_IRQ_AT91) && at91_aic_base) {
  117. at91_irq_suspend();
  118. pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
  119. /* remember all the always-wake irqs */
  120. (at91_pmc_read(AT91_PMC_PCSR)
  121. | (1 << AT91_ID_FIQ)
  122. | (1 << AT91_ID_SYS)
  123. | (at91_get_extern_irq()))
  124. & at91_aic_read(AT91_AIC_IMR),
  125. state);
  126. }
  127. switch (state) {
  128. /*
  129. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  130. * drivers must suspend more deeply: only the master clock
  131. * controller may be using the main oscillator.
  132. */
  133. case PM_SUSPEND_MEM:
  134. /*
  135. * Ensure that clocks are in a valid state.
  136. */
  137. if (!at91_pm_verify_clocks())
  138. goto error;
  139. /*
  140. * Enter slow clock mode by switching over to clk32k and
  141. * turning off the main oscillator; reverse on wakeup.
  142. */
  143. if (slow_clock) {
  144. int memctrl = AT91_MEMCTRL_SDRAMC;
  145. if (cpu_is_at91rm9200())
  146. memctrl = AT91_MEMCTRL_MC;
  147. else if (cpu_is_at91sam9g45())
  148. memctrl = AT91_MEMCTRL_DDRSDR;
  149. #ifdef CONFIG_AT91_SLOW_CLOCK
  150. /* copy slow_clock handler to SRAM, and call it */
  151. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  152. #endif
  153. slow_clock(at91_pmc_base, at91_ramc_base[0],
  154. at91_ramc_base[1], memctrl);
  155. break;
  156. } else {
  157. pr_info("AT91: PM - no slow clock mode enabled ...\n");
  158. /* FALLTHROUGH leaving master clock alone */
  159. }
  160. /*
  161. * STANDBY mode has *all* drivers suspended; ignores irqs not
  162. * marked as 'wakeup' event sources; and reduces DRAM power.
  163. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  164. * nothing fancy done with main or cpu clocks.
  165. */
  166. case PM_SUSPEND_STANDBY:
  167. /*
  168. * NOTE: the Wait-for-Interrupt instruction needs to be
  169. * in icache so no SDRAM accesses are needed until the
  170. * wakeup IRQ occurs and self-refresh is terminated.
  171. * For ARM 926 based chips, this requirement is weaker
  172. * as at91sam9 can access a RAM in self-refresh mode.
  173. */
  174. if (at91_pm_standby)
  175. at91_pm_standby();
  176. break;
  177. case PM_SUSPEND_ON:
  178. cpu_do_idle();
  179. break;
  180. default:
  181. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  182. goto error;
  183. }
  184. if (IS_ENABLED(CONFIG_OLD_IRQ_AT91) && at91_aic_base)
  185. pr_debug("AT91: PM - wakeup %08x\n",
  186. at91_aic_read(AT91_AIC_IPR) &
  187. at91_aic_read(AT91_AIC_IMR));
  188. error:
  189. target_state = PM_SUSPEND_ON;
  190. if (IS_ENABLED(CONFIG_OLD_IRQ_AT91) && at91_aic_base)
  191. at91_irq_resume();
  192. if (of_have_populated_dt())
  193. at91_pinctrl_gpio_resume();
  194. else
  195. at91_gpio_resume();
  196. return 0;
  197. }
  198. /*
  199. * Called right prior to thawing processes.
  200. */
  201. static void at91_pm_end(void)
  202. {
  203. target_state = PM_SUSPEND_ON;
  204. }
  205. static const struct platform_suspend_ops at91_pm_ops = {
  206. .valid = at91_pm_valid_state,
  207. .begin = at91_pm_begin,
  208. .enter = at91_pm_enter,
  209. .end = at91_pm_end,
  210. };
  211. static struct platform_device at91_cpuidle_device = {
  212. .name = "cpuidle-at91",
  213. };
  214. void at91_pm_set_standby(void (*at91_standby)(void))
  215. {
  216. if (at91_standby) {
  217. at91_cpuidle_device.dev.platform_data = at91_standby;
  218. at91_pm_standby = at91_standby;
  219. }
  220. }
  221. static int __init at91_pm_init(void)
  222. {
  223. #ifdef CONFIG_AT91_SLOW_CLOCK
  224. slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
  225. #endif
  226. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  227. /* AT91RM9200 SDRAM low-power mode cannot be used with self-refresh. */
  228. if (cpu_is_at91rm9200())
  229. at91_ramc_write(0, AT91RM9200_SDRAMC_LPR, 0);
  230. if (at91_cpuidle_device.dev.platform_data)
  231. platform_device_register(&at91_cpuidle_device);
  232. suspend_set_ops(&at91_pm_ops);
  233. return 0;
  234. }
  235. arch_initcall(at91_pm_init);