pm.c 8.4 KB

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