pm.c 9.0 KB

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