pm.c 8.0 KB

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