pm.c 7.8 KB

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