pm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/pm.h>
  13. #include <linux/sched.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/pm.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/io.h>
  21. #include <asm/irq.h>
  22. #include <asm/atomic.h>
  23. #include <asm/mach/time.h>
  24. #include <asm/mach/irq.h>
  25. #include <asm/mach-types.h>
  26. #include <asm/arch/at91_pmc.h>
  27. #include <asm/arch/at91rm9200_mc.h>
  28. #include <asm/arch/gpio.h>
  29. #include <asm/arch/cpu.h>
  30. #include "generic.h"
  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_set_target(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_sys_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_debug("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. if ((scsr & (AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP)) != 0) {
  68. pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
  69. return 0;
  70. }
  71. }
  72. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  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_sys_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  79. if (css != AT91_PMC_CSS_SLOW) {
  80. pr_debug("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  81. return 0;
  82. }
  83. }
  84. #endif
  85. return 1;
  86. }
  87. /*
  88. * Call this from platform driver suspend() to see how deeply to suspend.
  89. * For example, some controllers (like OHCI) need one of the PLL clocks
  90. * in order to act as a wakeup source, and those are not available when
  91. * going into slow clock mode.
  92. *
  93. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  94. * the very same problem (but not using at91 main_clk), and it'd be better
  95. * to add one generic API rather than lots of platform-specific ones.
  96. */
  97. int at91_suspend_entering_slow_clock(void)
  98. {
  99. return (target_state == PM_SUSPEND_MEM);
  100. }
  101. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  102. static void (*slow_clock)(void);
  103. static int at91_pm_enter(suspend_state_t state)
  104. {
  105. at91_gpio_suspend();
  106. at91_irq_suspend();
  107. pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
  108. /* remember all the always-wake irqs */
  109. (at91_sys_read(AT91_PMC_PCSR)
  110. | (1 << AT91_ID_FIQ)
  111. | (1 << AT91_ID_SYS)
  112. | (at91_extern_irq))
  113. & at91_sys_read(AT91_AIC_IMR),
  114. state);
  115. switch (state) {
  116. /*
  117. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  118. * drivers must suspend more deeply: only the master clock
  119. * controller may be using the main oscillator.
  120. */
  121. case PM_SUSPEND_MEM:
  122. /*
  123. * Ensure that clocks are in a valid state.
  124. */
  125. if (!at91_pm_verify_clocks())
  126. goto error;
  127. /*
  128. * Enter slow clock mode by switching over to clk32k and
  129. * turning off the main oscillator; reverse on wakeup.
  130. */
  131. if (slow_clock) {
  132. slow_clock();
  133. break;
  134. } else {
  135. /* DEVELOPMENT ONLY */
  136. pr_info("AT91: PM - no slow clock mode yet ...\n");
  137. /* FALLTHROUGH leaving master clock alone */
  138. }
  139. /*
  140. * STANDBY mode has *all* drivers suspended; ignores irqs not
  141. * marked as 'wakeup' event sources; and reduces DRAM power.
  142. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  143. * nothing fancy done with main or cpu clocks.
  144. */
  145. case PM_SUSPEND_STANDBY:
  146. /*
  147. * NOTE: the Wait-for-Interrupt instruction needs to be
  148. * in icache so the SDRAM stays in self-refresh mode until
  149. * the wakeup IRQ occurs.
  150. */
  151. asm("b 1f; .align 5; 1:");
  152. asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
  153. at91_sys_write(AT91_SDRAMC_SRR, 1); /* self-refresh mode */
  154. /* fall though to next state */
  155. case PM_SUSPEND_ON:
  156. asm("mcr p15, 0, r0, c7, c0, 4"); /* wait for interrupt */
  157. break;
  158. default:
  159. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  160. goto error;
  161. }
  162. pr_debug("AT91: PM - wakeup %08x\n",
  163. at91_sys_read(AT91_AIC_IPR) & at91_sys_read(AT91_AIC_IMR));
  164. error:
  165. target_state = PM_SUSPEND_ON;
  166. at91_irq_resume();
  167. at91_gpio_resume();
  168. return 0;
  169. }
  170. static struct pm_ops at91_pm_ops ={
  171. .valid = at91_pm_valid_state,
  172. .set_target = at91_pm_set_target,
  173. .enter = at91_pm_enter,
  174. };
  175. static int __init at91_pm_init(void)
  176. {
  177. printk("AT91: Power Management\n");
  178. #ifdef CONFIG_AT91_PM_SLOW_CLOCK
  179. /* REVISIT allocations of SRAM should be dynamically managed.
  180. * FIQ handlers and other components will want SRAM/TCM too...
  181. */
  182. slow_clock = (void *) (AT91_VA_BASE_SRAM + (3 * SZ_4K));
  183. memcpy(slow_clock, at91rm9200_slow_clock, at91rm9200_slow_clock_sz);
  184. #endif
  185. /* Disable SDRAM low-power mode. Cannot be used with self-refresh. */
  186. at91_sys_write(AT91_SDRAMC_LPR, 0);
  187. pm_set_ops(&at91_pm_ops);
  188. return 0;
  189. }
  190. arch_initcall(at91_pm_init);