pm.c 7.9 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 "generic.h"
  28. #include "pm.h"
  29. /*
  30. * Show the reason for the previous system reset.
  31. */
  32. #if defined(AT91_RSTC)
  33. #include <mach/at91_rstc.h>
  34. #include <mach/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)
  51. return;
  52. reset_type = at91_sys_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. #else
  92. static void __init show_reset_status(void) {}
  93. #endif
  94. static int at91_pm_valid_state(suspend_state_t state)
  95. {
  96. switch (state) {
  97. case PM_SUSPEND_ON:
  98. case PM_SUSPEND_STANDBY:
  99. case PM_SUSPEND_MEM:
  100. return 1;
  101. default:
  102. return 0;
  103. }
  104. }
  105. static suspend_state_t target_state;
  106. /*
  107. * Called after processes are frozen, but before we shutdown devices.
  108. */
  109. static int at91_pm_begin(suspend_state_t state)
  110. {
  111. target_state = state;
  112. return 0;
  113. }
  114. /*
  115. * Verify that all the clocks are correct before entering
  116. * slow-clock mode.
  117. */
  118. static int at91_pm_verify_clocks(void)
  119. {
  120. unsigned long scsr;
  121. int i;
  122. scsr = at91_sys_read(AT91_PMC_SCSR);
  123. /* USB must not be using PLLB */
  124. if (cpu_is_at91rm9200()) {
  125. if ((scsr & (AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP)) != 0) {
  126. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  127. return 0;
  128. }
  129. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()
  130. || cpu_is_at91sam9g20() || cpu_is_at91sam9g10()) {
  131. if ((scsr & (AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP)) != 0) {
  132. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  133. return 0;
  134. }
  135. } else if (cpu_is_at91cap9()) {
  136. if ((scsr & AT91CAP9_PMC_UHP) != 0) {
  137. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  138. return 0;
  139. }
  140. }
  141. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  142. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  143. for (i = 0; i < 4; i++) {
  144. u32 css;
  145. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  146. continue;
  147. css = at91_sys_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  148. if (css != AT91_PMC_CSS_SLOW) {
  149. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  150. return 0;
  151. }
  152. }
  153. #endif
  154. return 1;
  155. }
  156. /*
  157. * Call this from platform driver suspend() to see how deeply to suspend.
  158. * For example, some controllers (like OHCI) need one of the PLL clocks
  159. * in order to act as a wakeup source, and those are not available when
  160. * going into slow clock mode.
  161. *
  162. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  163. * the very same problem (but not using at91 main_clk), and it'd be better
  164. * to add one generic API rather than lots of platform-specific ones.
  165. */
  166. int at91_suspend_entering_slow_clock(void)
  167. {
  168. return (target_state == PM_SUSPEND_MEM);
  169. }
  170. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  171. static void (*slow_clock)(void);
  172. #ifdef CONFIG_AT91_SLOW_CLOCK
  173. extern void at91_slow_clock(void);
  174. extern u32 at91_slow_clock_sz;
  175. #endif
  176. static int at91_pm_enter(suspend_state_t state)
  177. {
  178. u32 saved_lpr;
  179. at91_gpio_suspend();
  180. at91_irq_suspend();
  181. pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
  182. /* remember all the always-wake irqs */
  183. (at91_sys_read(AT91_PMC_PCSR)
  184. | (1 << AT91_ID_FIQ)
  185. | (1 << AT91_ID_SYS)
  186. | (at91_extern_irq))
  187. & at91_aic_read(AT91_AIC_IMR),
  188. state);
  189. switch (state) {
  190. /*
  191. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  192. * drivers must suspend more deeply: only the master clock
  193. * controller may be using the main oscillator.
  194. */
  195. case PM_SUSPEND_MEM:
  196. /*
  197. * Ensure that clocks are in a valid state.
  198. */
  199. if (!at91_pm_verify_clocks())
  200. goto error;
  201. /*
  202. * Enter slow clock mode by switching over to clk32k and
  203. * turning off the main oscillator; reverse on wakeup.
  204. */
  205. if (slow_clock) {
  206. #ifdef CONFIG_AT91_SLOW_CLOCK
  207. /* copy slow_clock handler to SRAM, and call it */
  208. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  209. #endif
  210. slow_clock();
  211. break;
  212. } else {
  213. pr_info("AT91: PM - no slow clock mode enabled ...\n");
  214. /* FALLTHROUGH leaving master clock alone */
  215. }
  216. /*
  217. * STANDBY mode has *all* drivers suspended; ignores irqs not
  218. * marked as 'wakeup' event sources; and reduces DRAM power.
  219. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  220. * nothing fancy done with main or cpu clocks.
  221. */
  222. case PM_SUSPEND_STANDBY:
  223. /*
  224. * NOTE: the Wait-for-Interrupt instruction needs to be
  225. * in icache so no SDRAM accesses are needed until the
  226. * wakeup IRQ occurs and self-refresh is terminated.
  227. * For ARM 926 based chips, this requirement is weaker
  228. * as at91sam9 can access a RAM in self-refresh mode.
  229. */
  230. asm volatile ( "mov r0, #0\n\t"
  231. "b 1f\n\t"
  232. ".align 5\n\t"
  233. "1: mcr p15, 0, r0, c7, c10, 4\n\t"
  234. : /* no output */
  235. : /* no input */
  236. : "r0");
  237. saved_lpr = sdram_selfrefresh_enable();
  238. wait_for_interrupt_enable();
  239. sdram_selfrefresh_disable(saved_lpr);
  240. break;
  241. case PM_SUSPEND_ON:
  242. cpu_do_idle();
  243. break;
  244. default:
  245. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  246. goto error;
  247. }
  248. pr_debug("AT91: PM - wakeup %08x\n",
  249. at91_aic_read(AT91_AIC_IPR) & at91_aic_read(AT91_AIC_IMR));
  250. error:
  251. target_state = PM_SUSPEND_ON;
  252. at91_irq_resume();
  253. at91_gpio_resume();
  254. return 0;
  255. }
  256. /*
  257. * Called right prior to thawing processes.
  258. */
  259. static void at91_pm_end(void)
  260. {
  261. target_state = PM_SUSPEND_ON;
  262. }
  263. static const struct platform_suspend_ops at91_pm_ops = {
  264. .valid = at91_pm_valid_state,
  265. .begin = at91_pm_begin,
  266. .enter = at91_pm_enter,
  267. .end = at91_pm_end,
  268. };
  269. static int __init at91_pm_init(void)
  270. {
  271. #ifdef CONFIG_AT91_SLOW_CLOCK
  272. slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
  273. #endif
  274. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  275. #ifdef CONFIG_ARCH_AT91RM9200
  276. /* AT91RM9200 SDRAM low-power mode cannot be used with self-refresh. */
  277. at91_sys_write(AT91_SDRAMC_LPR, 0);
  278. #endif
  279. suspend_set_ops(&at91_pm_ops);
  280. show_reset_status();
  281. return 0;
  282. }
  283. arch_initcall(at91_pm_init);