pm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_sys_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. } else if (cpu_is_at91cap9()) {
  132. if ((scsr & AT91CAP9_PMC_UHP) != 0) {
  133. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  134. return 0;
  135. }
  136. }
  137. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  138. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  139. for (i = 0; i < 4; i++) {
  140. u32 css;
  141. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  142. continue;
  143. css = at91_sys_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  144. if (css != AT91_PMC_CSS_SLOW) {
  145. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  146. return 0;
  147. }
  148. }
  149. #endif
  150. return 1;
  151. }
  152. /*
  153. * Call this from platform driver suspend() to see how deeply to suspend.
  154. * For example, some controllers (like OHCI) need one of the PLL clocks
  155. * in order to act as a wakeup source, and those are not available when
  156. * going into slow clock mode.
  157. *
  158. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  159. * the very same problem (but not using at91 main_clk), and it'd be better
  160. * to add one generic API rather than lots of platform-specific ones.
  161. */
  162. int at91_suspend_entering_slow_clock(void)
  163. {
  164. return (target_state == PM_SUSPEND_MEM);
  165. }
  166. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  167. static void (*slow_clock)(void);
  168. #ifdef CONFIG_AT91_SLOW_CLOCK
  169. extern void at91_slow_clock(void);
  170. extern u32 at91_slow_clock_sz;
  171. #endif
  172. static int at91_pm_enter(suspend_state_t state)
  173. {
  174. u32 saved_lpr;
  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_sys_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. #ifdef CONFIG_AT91_SLOW_CLOCK
  203. /* copy slow_clock handler to SRAM, and call it */
  204. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  205. #endif
  206. slow_clock();
  207. break;
  208. } else {
  209. pr_info("AT91: PM - no slow clock mode enabled ...\n");
  210. /* FALLTHROUGH leaving master clock alone */
  211. }
  212. /*
  213. * STANDBY mode has *all* drivers suspended; ignores irqs not
  214. * marked as 'wakeup' event sources; and reduces DRAM power.
  215. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  216. * nothing fancy done with main or cpu clocks.
  217. */
  218. case PM_SUSPEND_STANDBY:
  219. /*
  220. * NOTE: the Wait-for-Interrupt instruction needs to be
  221. * in icache so no SDRAM accesses are needed until the
  222. * wakeup IRQ occurs and self-refresh is terminated.
  223. * For ARM 926 based chips, this requirement is weaker
  224. * as at91sam9 can access a RAM in self-refresh mode.
  225. */
  226. asm volatile ( "mov r0, #0\n\t"
  227. "b 1f\n\t"
  228. ".align 5\n\t"
  229. "1: mcr p15, 0, r0, c7, c10, 4\n\t"
  230. : /* no output */
  231. : /* no input */
  232. : "r0");
  233. saved_lpr = sdram_selfrefresh_enable();
  234. wait_for_interrupt_enable();
  235. sdram_selfrefresh_disable(saved_lpr);
  236. break;
  237. case PM_SUSPEND_ON:
  238. cpu_do_idle();
  239. break;
  240. default:
  241. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  242. goto error;
  243. }
  244. pr_debug("AT91: PM - wakeup %08x\n",
  245. at91_aic_read(AT91_AIC_IPR) & at91_aic_read(AT91_AIC_IMR));
  246. error:
  247. target_state = PM_SUSPEND_ON;
  248. at91_irq_resume();
  249. at91_gpio_resume();
  250. return 0;
  251. }
  252. /*
  253. * Called right prior to thawing processes.
  254. */
  255. static void at91_pm_end(void)
  256. {
  257. target_state = PM_SUSPEND_ON;
  258. }
  259. static const struct platform_suspend_ops at91_pm_ops = {
  260. .valid = at91_pm_valid_state,
  261. .begin = at91_pm_begin,
  262. .enter = at91_pm_enter,
  263. .end = at91_pm_end,
  264. };
  265. static int __init at91_pm_init(void)
  266. {
  267. #ifdef CONFIG_AT91_SLOW_CLOCK
  268. slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
  269. #endif
  270. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  271. #ifdef CONFIG_ARCH_AT91RM9200
  272. /* AT91RM9200 SDRAM low-power mode cannot be used with self-refresh. */
  273. at91_sys_write(AT91_SDRAMC_LPR, 0);
  274. #endif
  275. suspend_set_ops(&at91_pm_ops);
  276. show_reset_status();
  277. return 0;
  278. }
  279. arch_initcall(at91_pm_init);