pm.c 9.1 KB

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