pm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <linux/clk/at91_pmc.h>
  22. #include <asm/irq.h>
  23. #include <linux/atomic.h>
  24. #include <asm/mach/time.h>
  25. #include <asm/mach/irq.h>
  26. #include <mach/cpu.h>
  27. #include <mach/hardware.h>
  28. #include "generic.h"
  29. #include "pm.h"
  30. static void (*at91_pm_standby)(void);
  31. static int at91_pm_valid_state(suspend_state_t state)
  32. {
  33. switch (state) {
  34. case PM_SUSPEND_ON:
  35. case PM_SUSPEND_STANDBY:
  36. case PM_SUSPEND_MEM:
  37. return 1;
  38. default:
  39. return 0;
  40. }
  41. }
  42. static suspend_state_t target_state;
  43. /*
  44. * Called after processes are frozen, but before we shutdown devices.
  45. */
  46. static int at91_pm_begin(suspend_state_t state)
  47. {
  48. target_state = state;
  49. return 0;
  50. }
  51. /*
  52. * Verify that all the clocks are correct before entering
  53. * slow-clock mode.
  54. */
  55. static int at91_pm_verify_clocks(void)
  56. {
  57. unsigned long scsr;
  58. int i;
  59. scsr = at91_pmc_read(AT91_PMC_SCSR);
  60. /* USB must not be using PLLB */
  61. if (cpu_is_at91rm9200()) {
  62. if ((scsr & (AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP)) != 0) {
  63. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  64. return 0;
  65. }
  66. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()
  67. || cpu_is_at91sam9g20() || cpu_is_at91sam9g10()) {
  68. if ((scsr & (AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP)) != 0) {
  69. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  70. return 0;
  71. }
  72. }
  73. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  74. for (i = 0; i < 4; i++) {
  75. u32 css;
  76. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  77. continue;
  78. css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  79. if (css != AT91_PMC_CSS_SLOW) {
  80. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  81. return 0;
  82. }
  83. }
  84. return 1;
  85. }
  86. /*
  87. * Call this from platform driver suspend() to see how deeply to suspend.
  88. * For example, some controllers (like OHCI) need one of the PLL clocks
  89. * in order to act as a wakeup source, and those are not available when
  90. * going into slow clock mode.
  91. *
  92. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  93. * the very same problem (but not using at91 main_clk), and it'd be better
  94. * to add one generic API rather than lots of platform-specific ones.
  95. */
  96. int at91_suspend_entering_slow_clock(void)
  97. {
  98. return (target_state == PM_SUSPEND_MEM);
  99. }
  100. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  101. static void (*slow_clock)(void __iomem *pmc, void __iomem *ramc0,
  102. void __iomem *ramc1, int memctrl);
  103. #ifdef CONFIG_AT91_SLOW_CLOCK
  104. extern void at91_slow_clock(void __iomem *pmc, void __iomem *ramc0,
  105. void __iomem *ramc1, int memctrl);
  106. extern u32 at91_slow_clock_sz;
  107. #endif
  108. static int at91_pm_enter(suspend_state_t state)
  109. {
  110. at91_pinctrl_gpio_suspend();
  111. switch (state) {
  112. /*
  113. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  114. * drivers must suspend more deeply: only the master clock
  115. * controller may be using the main oscillator.
  116. */
  117. case PM_SUSPEND_MEM:
  118. /*
  119. * Ensure that clocks are in a valid state.
  120. */
  121. if (!at91_pm_verify_clocks())
  122. goto error;
  123. /*
  124. * Enter slow clock mode by switching over to clk32k and
  125. * turning off the main oscillator; reverse on wakeup.
  126. */
  127. if (slow_clock) {
  128. int memctrl = AT91_MEMCTRL_SDRAMC;
  129. if (cpu_is_at91rm9200())
  130. memctrl = AT91_MEMCTRL_MC;
  131. else if (cpu_is_at91sam9g45())
  132. memctrl = AT91_MEMCTRL_DDRSDR;
  133. #ifdef CONFIG_AT91_SLOW_CLOCK
  134. /* copy slow_clock handler to SRAM, and call it */
  135. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  136. #endif
  137. slow_clock(at91_pmc_base, at91_ramc_base[0],
  138. at91_ramc_base[1], memctrl);
  139. break;
  140. } else {
  141. pr_info("AT91: PM - no slow clock mode enabled ...\n");
  142. /* FALLTHROUGH leaving master clock alone */
  143. }
  144. /*
  145. * STANDBY mode has *all* drivers suspended; ignores irqs not
  146. * marked as 'wakeup' event sources; and reduces DRAM power.
  147. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  148. * nothing fancy done with main or cpu clocks.
  149. */
  150. case PM_SUSPEND_STANDBY:
  151. /*
  152. * NOTE: the Wait-for-Interrupt instruction needs to be
  153. * in icache so no SDRAM accesses are needed until the
  154. * wakeup IRQ occurs and self-refresh is terminated.
  155. * For ARM 926 based chips, this requirement is weaker
  156. * as at91sam9 can access a RAM in self-refresh mode.
  157. */
  158. if (at91_pm_standby)
  159. at91_pm_standby();
  160. break;
  161. case PM_SUSPEND_ON:
  162. cpu_do_idle();
  163. break;
  164. default:
  165. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  166. goto error;
  167. }
  168. error:
  169. target_state = PM_SUSPEND_ON;
  170. at91_pinctrl_gpio_resume();
  171. return 0;
  172. }
  173. /*
  174. * Called right prior to thawing processes.
  175. */
  176. static void at91_pm_end(void)
  177. {
  178. target_state = PM_SUSPEND_ON;
  179. }
  180. static const struct platform_suspend_ops at91_pm_ops = {
  181. .valid = at91_pm_valid_state,
  182. .begin = at91_pm_begin,
  183. .enter = at91_pm_enter,
  184. .end = at91_pm_end,
  185. };
  186. static struct platform_device at91_cpuidle_device = {
  187. .name = "cpuidle-at91",
  188. };
  189. void at91_pm_set_standby(void (*at91_standby)(void))
  190. {
  191. if (at91_standby) {
  192. at91_cpuidle_device.dev.platform_data = at91_standby;
  193. at91_pm_standby = at91_standby;
  194. }
  195. }
  196. static int __init at91_pm_init(void)
  197. {
  198. #ifdef CONFIG_AT91_SLOW_CLOCK
  199. slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
  200. #endif
  201. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  202. /* AT91RM9200 SDRAM low-power mode cannot be used with self-refresh. */
  203. if (cpu_is_at91rm9200())
  204. at91_ramc_write(0, AT91RM9200_SDRAMC_LPR, 0);
  205. if (at91_cpuidle_device.dev.platform_data)
  206. platform_device_register(&at91_cpuidle_device);
  207. suspend_set_ops(&at91_pm_ops);
  208. return 0;
  209. }
  210. arch_initcall(at91_pm_init);