pm.c 8.1 KB

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