pm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/at91rm9200_mc.h>
  27. #include <asm/arch/gpio.h>
  28. #include <asm/arch/cpu.h>
  29. #include "generic.h"
  30. static int at91_pm_valid_state(suspend_state_t state)
  31. {
  32. switch (state) {
  33. case PM_SUSPEND_ON:
  34. case PM_SUSPEND_STANDBY:
  35. case PM_SUSPEND_MEM:
  36. return 1;
  37. default:
  38. return 0;
  39. }
  40. }
  41. static suspend_state_t target_state;
  42. /*
  43. * Called after processes are frozen, but before we shutdown devices.
  44. */
  45. static int at91_pm_begin(suspend_state_t state)
  46. {
  47. target_state = state;
  48. return 0;
  49. }
  50. /*
  51. * Verify that all the clocks are correct before entering
  52. * slow-clock mode.
  53. */
  54. static int at91_pm_verify_clocks(void)
  55. {
  56. unsigned long scsr;
  57. int i;
  58. scsr = at91_sys_read(AT91_PMC_SCSR);
  59. /* USB must not be using PLLB */
  60. if (cpu_is_at91rm9200()) {
  61. if ((scsr & (AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP)) != 0) {
  62. pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
  63. return 0;
  64. }
  65. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()) {
  66. if ((scsr & (AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP)) != 0) {
  67. pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
  68. return 0;
  69. }
  70. } else if (cpu_is_at91cap9()) {
  71. if ((scsr & AT91CAP9_PMC_UHP) != 0) {
  72. pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
  73. return 0;
  74. }
  75. }
  76. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  77. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  78. for (i = 0; i < 4; i++) {
  79. u32 css;
  80. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  81. continue;
  82. css = at91_sys_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  83. if (css != AT91_PMC_CSS_SLOW) {
  84. pr_debug("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  85. return 0;
  86. }
  87. }
  88. #endif
  89. return 1;
  90. }
  91. /*
  92. * Call this from platform driver suspend() to see how deeply to suspend.
  93. * For example, some controllers (like OHCI) need one of the PLL clocks
  94. * in order to act as a wakeup source, and those are not available when
  95. * going into slow clock mode.
  96. *
  97. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  98. * the very same problem (but not using at91 main_clk), and it'd be better
  99. * to add one generic API rather than lots of platform-specific ones.
  100. */
  101. int at91_suspend_entering_slow_clock(void)
  102. {
  103. return (target_state == PM_SUSPEND_MEM);
  104. }
  105. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  106. static void (*slow_clock)(void);
  107. static int at91_pm_enter(suspend_state_t state)
  108. {
  109. at91_gpio_suspend();
  110. at91_irq_suspend();
  111. pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
  112. /* remember all the always-wake irqs */
  113. (at91_sys_read(AT91_PMC_PCSR)
  114. | (1 << AT91_ID_FIQ)
  115. | (1 << AT91_ID_SYS)
  116. | (at91_extern_irq))
  117. & at91_sys_read(AT91_AIC_IMR),
  118. state);
  119. switch (state) {
  120. /*
  121. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  122. * drivers must suspend more deeply: only the master clock
  123. * controller may be using the main oscillator.
  124. */
  125. case PM_SUSPEND_MEM:
  126. /*
  127. * Ensure that clocks are in a valid state.
  128. */
  129. if (!at91_pm_verify_clocks())
  130. goto error;
  131. /*
  132. * Enter slow clock mode by switching over to clk32k and
  133. * turning off the main oscillator; reverse on wakeup.
  134. */
  135. if (slow_clock) {
  136. slow_clock();
  137. break;
  138. } else {
  139. /* DEVELOPMENT ONLY */
  140. pr_info("AT91: PM - no slow clock mode yet ...\n");
  141. /* FALLTHROUGH leaving master clock alone */
  142. }
  143. /*
  144. * STANDBY mode has *all* drivers suspended; ignores irqs not
  145. * marked as 'wakeup' event sources; and reduces DRAM power.
  146. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  147. * nothing fancy done with main or cpu clocks.
  148. */
  149. case PM_SUSPEND_STANDBY:
  150. /*
  151. * NOTE: the Wait-for-Interrupt instruction needs to be
  152. * in icache so the SDRAM stays in self-refresh mode until
  153. * the wakeup IRQ occurs.
  154. */
  155. asm("b 1f; .align 5; 1:");
  156. asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
  157. at91_sys_write(AT91_SDRAMC_SRR, 1); /* self-refresh mode */
  158. /* fall though to next state */
  159. case PM_SUSPEND_ON:
  160. asm("mcr p15, 0, r0, c7, c0, 4"); /* wait for interrupt */
  161. break;
  162. default:
  163. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  164. goto error;
  165. }
  166. pr_debug("AT91: PM - wakeup %08x\n",
  167. at91_sys_read(AT91_AIC_IPR) & at91_sys_read(AT91_AIC_IMR));
  168. error:
  169. target_state = PM_SUSPEND_ON;
  170. at91_irq_resume();
  171. at91_gpio_resume();
  172. return 0;
  173. }
  174. /*
  175. * Called right prior to thawing processes.
  176. */
  177. static void at91_pm_end(void)
  178. {
  179. target_state = PM_SUSPEND_ON;
  180. }
  181. static struct platform_suspend_ops at91_pm_ops ={
  182. .valid = at91_pm_valid_state,
  183. .begin = at91_pm_begin,
  184. .enter = at91_pm_enter,
  185. .end = at91_pm_end,
  186. };
  187. static int __init at91_pm_init(void)
  188. {
  189. printk("AT91: Power Management\n");
  190. #ifdef CONFIG_AT91_PM_SLOW_CLOCK
  191. /* REVISIT allocations of SRAM should be dynamically managed.
  192. * FIQ handlers and other components will want SRAM/TCM too...
  193. */
  194. slow_clock = (void *) (AT91_VA_BASE_SRAM + (3 * SZ_4K));
  195. memcpy(slow_clock, at91rm9200_slow_clock, at91rm9200_slow_clock_sz);
  196. #endif
  197. /* Disable SDRAM low-power mode. Cannot be used with self-refresh. */
  198. at91_sys_write(AT91_SDRAMC_LPR, 0);
  199. suspend_set_ops(&at91_pm_ops);
  200. return 0;
  201. }
  202. arch_initcall(at91_pm_init);