pm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/suspend.h>
  13. #include <linux/sched.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <asm/irq.h>
  21. #include <asm/atomic.h>
  22. #include <asm/mach/time.h>
  23. #include <asm/mach/irq.h>
  24. #include <mach/at91_pmc.h>
  25. #include <mach/gpio.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_SHDWC)
  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. reset_type = at91_sys_read(AT91_RSTC_SR) & AT91_RSTC_RSTTYP;
  51. wake_type = at91_sys_read(AT91_SHDW_SR);
  52. switch (reset_type) {
  53. case AT91_RSTC_RSTTYP_GENERAL:
  54. reason = general;
  55. break;
  56. case AT91_RSTC_RSTTYP_WAKEUP:
  57. /* board-specific code enabled the wakeup sources */
  58. reason = wakeup;
  59. /* "wakeup signal" */
  60. if (wake_type & AT91_SHDW_WAKEUP0)
  61. r2 = signal;
  62. else {
  63. r2 = reason;
  64. if (wake_type & AT91_SHDW_RTTWK) /* rtt wakeup */
  65. reason = rtt;
  66. else if (wake_type & AT91_SHDW_RTCWK) /* rtc wakeup */
  67. reason = rtc;
  68. else if (wake_type == 0) /* power-restored wakeup */
  69. reason = restore;
  70. else /* unknown wakeup */
  71. reason = unknown;
  72. }
  73. break;
  74. case AT91_RSTC_RSTTYP_WATCHDOG:
  75. reason = watchdog;
  76. break;
  77. case AT91_RSTC_RSTTYP_SOFTWARE:
  78. reason = software;
  79. break;
  80. case AT91_RSTC_RSTTYP_USER:
  81. reason = user;
  82. break;
  83. default:
  84. reason = unknown;
  85. break;
  86. }
  87. pr_info("AT91: Starting after %s %s\n", reason, r2);
  88. }
  89. #else
  90. static void __init show_reset_status(void) {}
  91. #endif
  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_sys_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. } else if (cpu_is_at91cap9()) {
  134. if ((scsr & AT91CAP9_PMC_UHP) != 0) {
  135. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  136. return 0;
  137. }
  138. }
  139. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  140. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  141. for (i = 0; i < 4; i++) {
  142. u32 css;
  143. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  144. continue;
  145. css = at91_sys_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  146. if (css != AT91_PMC_CSS_SLOW) {
  147. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  148. return 0;
  149. }
  150. }
  151. #endif
  152. return 1;
  153. }
  154. /*
  155. * Call this from platform driver suspend() to see how deeply to suspend.
  156. * For example, some controllers (like OHCI) need one of the PLL clocks
  157. * in order to act as a wakeup source, and those are not available when
  158. * going into slow clock mode.
  159. *
  160. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  161. * the very same problem (but not using at91 main_clk), and it'd be better
  162. * to add one generic API rather than lots of platform-specific ones.
  163. */
  164. int at91_suspend_entering_slow_clock(void)
  165. {
  166. return (target_state == PM_SUSPEND_MEM);
  167. }
  168. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  169. static void (*slow_clock)(void);
  170. #ifdef CONFIG_AT91_SLOW_CLOCK
  171. extern void at91_slow_clock(void);
  172. extern u32 at91_slow_clock_sz;
  173. #endif
  174. static int at91_pm_enter(suspend_state_t state)
  175. {
  176. u32 saved_lpr;
  177. at91_gpio_suspend();
  178. at91_irq_suspend();
  179. pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
  180. /* remember all the always-wake irqs */
  181. (at91_sys_read(AT91_PMC_PCSR)
  182. | (1 << AT91_ID_FIQ)
  183. | (1 << AT91_ID_SYS)
  184. | (at91_extern_irq))
  185. & at91_sys_read(AT91_AIC_IMR),
  186. state);
  187. switch (state) {
  188. /*
  189. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  190. * drivers must suspend more deeply: only the master clock
  191. * controller may be using the main oscillator.
  192. */
  193. case PM_SUSPEND_MEM:
  194. /*
  195. * Ensure that clocks are in a valid state.
  196. */
  197. if (!at91_pm_verify_clocks())
  198. goto error;
  199. /*
  200. * Enter slow clock mode by switching over to clk32k and
  201. * turning off the main oscillator; reverse on wakeup.
  202. */
  203. if (slow_clock) {
  204. #ifdef CONFIG_AT91_SLOW_CLOCK
  205. /* copy slow_clock handler to SRAM, and call it */
  206. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  207. #endif
  208. slow_clock();
  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. */
  226. asm("b 1f; .align 5; 1:");
  227. asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
  228. saved_lpr = sdram_selfrefresh_enable();
  229. asm("mcr p15, 0, r0, c7, c0, 4"); /* wait for interrupt */
  230. sdram_selfrefresh_disable(saved_lpr);
  231. break;
  232. case PM_SUSPEND_ON:
  233. asm("mcr p15, 0, r0, c7, c0, 4"); /* wait for interrupt */
  234. break;
  235. default:
  236. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  237. goto error;
  238. }
  239. pr_debug("AT91: PM - wakeup %08x\n",
  240. at91_sys_read(AT91_AIC_IPR) & at91_sys_read(AT91_AIC_IMR));
  241. error:
  242. target_state = PM_SUSPEND_ON;
  243. at91_irq_resume();
  244. at91_gpio_resume();
  245. return 0;
  246. }
  247. /*
  248. * Called right prior to thawing processes.
  249. */
  250. static void at91_pm_end(void)
  251. {
  252. target_state = PM_SUSPEND_ON;
  253. }
  254. static struct platform_suspend_ops at91_pm_ops ={
  255. .valid = at91_pm_valid_state,
  256. .begin = at91_pm_begin,
  257. .enter = at91_pm_enter,
  258. .end = at91_pm_end,
  259. };
  260. static int __init at91_pm_init(void)
  261. {
  262. #ifdef CONFIG_AT91_SLOW_CLOCK
  263. slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
  264. #endif
  265. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  266. #ifdef CONFIG_ARCH_AT91RM9200
  267. /* AT91RM9200 SDRAM low-power mode cannot be used with self-refresh. */
  268. at91_sys_write(AT91_SDRAMC_LPR, 0);
  269. #endif
  270. suspend_set_ops(&at91_pm_ops);
  271. show_reset_status();
  272. return 0;
  273. }
  274. arch_initcall(at91_pm_init);