pm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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_pmc.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. #include <mach/at91_rstc.h>
  33. #include <mach/at91_shdwc.h>
  34. static void __init show_reset_status(void)
  35. {
  36. static char reset[] __initdata = "reset";
  37. static char general[] __initdata = "general";
  38. static char wakeup[] __initdata = "wakeup";
  39. static char watchdog[] __initdata = "watchdog";
  40. static char software[] __initdata = "software";
  41. static char user[] __initdata = "user";
  42. static char unknown[] __initdata = "unknown";
  43. static char signal[] __initdata = "signal";
  44. static char rtc[] __initdata = "rtc";
  45. static char rtt[] __initdata = "rtt";
  46. static char restore[] __initdata = "power-restored";
  47. char *reason, *r2 = reset;
  48. u32 reset_type, wake_type;
  49. if (!at91_shdwc_base || !at91_rstc_base)
  50. return;
  51. reset_type = at91_rstc_read(AT91_RSTC_SR) & AT91_RSTC_RSTTYP;
  52. wake_type = at91_shdwc_read(AT91_SHDW_SR);
  53. switch (reset_type) {
  54. case AT91_RSTC_RSTTYP_GENERAL:
  55. reason = general;
  56. break;
  57. case AT91_RSTC_RSTTYP_WAKEUP:
  58. /* board-specific code enabled the wakeup sources */
  59. reason = wakeup;
  60. /* "wakeup signal" */
  61. if (wake_type & AT91_SHDW_WAKEUP0)
  62. r2 = signal;
  63. else {
  64. r2 = reason;
  65. if (wake_type & AT91_SHDW_RTTWK) /* rtt wakeup */
  66. reason = rtt;
  67. else if (wake_type & AT91_SHDW_RTCWK) /* rtc wakeup */
  68. reason = rtc;
  69. else if (wake_type == 0) /* power-restored wakeup */
  70. reason = restore;
  71. else /* unknown wakeup */
  72. reason = unknown;
  73. }
  74. break;
  75. case AT91_RSTC_RSTTYP_WATCHDOG:
  76. reason = watchdog;
  77. break;
  78. case AT91_RSTC_RSTTYP_SOFTWARE:
  79. reason = software;
  80. break;
  81. case AT91_RSTC_RSTTYP_USER:
  82. reason = user;
  83. break;
  84. default:
  85. reason = unknown;
  86. break;
  87. }
  88. pr_info("AT91: Starting after %s %s\n", reason, r2);
  89. }
  90. static int at91_pm_valid_state(suspend_state_t state)
  91. {
  92. switch (state) {
  93. case PM_SUSPEND_ON:
  94. case PM_SUSPEND_STANDBY:
  95. case PM_SUSPEND_MEM:
  96. return 1;
  97. default:
  98. return 0;
  99. }
  100. }
  101. static suspend_state_t target_state;
  102. /*
  103. * Called after processes are frozen, but before we shutdown devices.
  104. */
  105. static int at91_pm_begin(suspend_state_t state)
  106. {
  107. target_state = state;
  108. return 0;
  109. }
  110. /*
  111. * Verify that all the clocks are correct before entering
  112. * slow-clock mode.
  113. */
  114. static int at91_pm_verify_clocks(void)
  115. {
  116. unsigned long scsr;
  117. int i;
  118. scsr = at91_pmc_read(AT91_PMC_SCSR);
  119. /* USB must not be using PLLB */
  120. if (cpu_is_at91rm9200()) {
  121. if ((scsr & (AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP)) != 0) {
  122. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  123. return 0;
  124. }
  125. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()
  126. || cpu_is_at91sam9g20() || cpu_is_at91sam9g10()) {
  127. if ((scsr & (AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP)) != 0) {
  128. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  129. return 0;
  130. }
  131. }
  132. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  133. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  134. for (i = 0; i < 4; i++) {
  135. u32 css;
  136. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  137. continue;
  138. css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  139. if (css != AT91_PMC_CSS_SLOW) {
  140. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  141. return 0;
  142. }
  143. }
  144. #endif
  145. return 1;
  146. }
  147. /*
  148. * Call this from platform driver suspend() to see how deeply to suspend.
  149. * For example, some controllers (like OHCI) need one of the PLL clocks
  150. * in order to act as a wakeup source, and those are not available when
  151. * going into slow clock mode.
  152. *
  153. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  154. * the very same problem (but not using at91 main_clk), and it'd be better
  155. * to add one generic API rather than lots of platform-specific ones.
  156. */
  157. int at91_suspend_entering_slow_clock(void)
  158. {
  159. return (target_state == PM_SUSPEND_MEM);
  160. }
  161. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  162. static void (*slow_clock)(void __iomem *pmc, void __iomem *ramc0,
  163. void __iomem *ramc1, int memctrl);
  164. #ifdef CONFIG_AT91_SLOW_CLOCK
  165. extern void at91_slow_clock(void __iomem *pmc, void __iomem *ramc0,
  166. void __iomem *ramc1, int memctrl);
  167. extern u32 at91_slow_clock_sz;
  168. #endif
  169. void __iomem *at91_ramc_base[2];
  170. void __init at91_ioremap_ramc(int id, u32 addr, u32 size)
  171. {
  172. if (id < 0 || id > 1) {
  173. pr_emerg("Wrong RAM controller id (%d), cannot continue\n", id);
  174. BUG();
  175. }
  176. at91_ramc_base[id] = ioremap(addr, size);
  177. if (!at91_ramc_base[id])
  178. panic("Impossible to ioremap ramc.%d 0x%x\n", id, addr);
  179. }
  180. static int at91_pm_enter(suspend_state_t state)
  181. {
  182. at91_gpio_suspend();
  183. at91_irq_suspend();
  184. pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
  185. /* remember all the always-wake irqs */
  186. (at91_pmc_read(AT91_PMC_PCSR)
  187. | (1 << AT91_ID_FIQ)
  188. | (1 << AT91_ID_SYS)
  189. | (at91_extern_irq))
  190. & at91_aic_read(AT91_AIC_IMR),
  191. state);
  192. switch (state) {
  193. /*
  194. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  195. * drivers must suspend more deeply: only the master clock
  196. * controller may be using the main oscillator.
  197. */
  198. case PM_SUSPEND_MEM:
  199. /*
  200. * Ensure that clocks are in a valid state.
  201. */
  202. if (!at91_pm_verify_clocks())
  203. goto error;
  204. /*
  205. * Enter slow clock mode by switching over to clk32k and
  206. * turning off the main oscillator; reverse on wakeup.
  207. */
  208. if (slow_clock) {
  209. int memctrl = AT91_MEMCTRL_SDRAMC;
  210. if (cpu_is_at91rm9200())
  211. memctrl = AT91_MEMCTRL_MC;
  212. else if (cpu_is_at91sam9g45())
  213. memctrl = AT91_MEMCTRL_DDRSDR;
  214. #ifdef CONFIG_AT91_SLOW_CLOCK
  215. /* copy slow_clock handler to SRAM, and call it */
  216. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  217. #endif
  218. slow_clock(at91_pmc_base, at91_ramc_base[0],
  219. at91_ramc_base[1], memctrl);
  220. break;
  221. } else {
  222. pr_info("AT91: PM - no slow clock mode enabled ...\n");
  223. /* FALLTHROUGH leaving master clock alone */
  224. }
  225. /*
  226. * STANDBY mode has *all* drivers suspended; ignores irqs not
  227. * marked as 'wakeup' event sources; and reduces DRAM power.
  228. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  229. * nothing fancy done with main or cpu clocks.
  230. */
  231. case PM_SUSPEND_STANDBY:
  232. /*
  233. * NOTE: the Wait-for-Interrupt instruction needs to be
  234. * in icache so no SDRAM accesses are needed until the
  235. * wakeup IRQ occurs and self-refresh is terminated.
  236. * For ARM 926 based chips, this requirement is weaker
  237. * as at91sam9 can access a RAM in self-refresh mode.
  238. */
  239. at91_standby();
  240. break;
  241. case PM_SUSPEND_ON:
  242. cpu_do_idle();
  243. break;
  244. default:
  245. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  246. goto error;
  247. }
  248. pr_debug("AT91: PM - wakeup %08x\n",
  249. at91_aic_read(AT91_AIC_IPR) & at91_aic_read(AT91_AIC_IMR));
  250. error:
  251. target_state = PM_SUSPEND_ON;
  252. at91_irq_resume();
  253. at91_gpio_resume();
  254. return 0;
  255. }
  256. /*
  257. * Called right prior to thawing processes.
  258. */
  259. static void at91_pm_end(void)
  260. {
  261. target_state = PM_SUSPEND_ON;
  262. }
  263. static const struct platform_suspend_ops at91_pm_ops = {
  264. .valid = at91_pm_valid_state,
  265. .begin = at91_pm_begin,
  266. .enter = at91_pm_enter,
  267. .end = at91_pm_end,
  268. };
  269. static int __init at91_pm_init(void)
  270. {
  271. #ifdef CONFIG_AT91_SLOW_CLOCK
  272. slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
  273. #endif
  274. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  275. #ifdef CONFIG_ARCH_AT91RM9200
  276. /* AT91RM9200 SDRAM low-power mode cannot be used with self-refresh. */
  277. at91_ramc_write(0, AT91RM9200_SDRAMC_LPR, 0);
  278. #endif
  279. suspend_set_ops(&at91_pm_ops);
  280. show_reset_status();
  281. return 0;
  282. }
  283. arch_initcall(at91_pm_init);