pm.c 9.0 KB

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