pm.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/genalloc.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/io.h>
  24. #include <linux/clk/at91_pmc.h>
  25. #include <asm/irq.h>
  26. #include <linux/atomic.h>
  27. #include <asm/mach/time.h>
  28. #include <asm/mach/irq.h>
  29. #include <mach/cpu.h>
  30. #include <mach/hardware.h>
  31. #include "generic.h"
  32. #include "pm.h"
  33. static struct {
  34. unsigned long uhp_udp_mask;
  35. int memctrl;
  36. } at91_pm_data;
  37. static void (*at91_pm_standby)(void);
  38. static int at91_pm_valid_state(suspend_state_t state)
  39. {
  40. switch (state) {
  41. case PM_SUSPEND_ON:
  42. case PM_SUSPEND_STANDBY:
  43. case PM_SUSPEND_MEM:
  44. return 1;
  45. default:
  46. return 0;
  47. }
  48. }
  49. static suspend_state_t target_state;
  50. /*
  51. * Called after processes are frozen, but before we shutdown devices.
  52. */
  53. static int at91_pm_begin(suspend_state_t state)
  54. {
  55. target_state = state;
  56. return 0;
  57. }
  58. /*
  59. * Verify that all the clocks are correct before entering
  60. * slow-clock mode.
  61. */
  62. static int at91_pm_verify_clocks(void)
  63. {
  64. unsigned long scsr;
  65. int i;
  66. scsr = at91_pmc_read(AT91_PMC_SCSR);
  67. /* USB must not be using PLLB */
  68. if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
  69. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  70. return 0;
  71. }
  72. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  73. for (i = 0; i < 4; i++) {
  74. u32 css;
  75. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  76. continue;
  77. css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  78. if (css != AT91_PMC_CSS_SLOW) {
  79. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  80. return 0;
  81. }
  82. }
  83. return 1;
  84. }
  85. /*
  86. * Call this from platform driver suspend() to see how deeply to suspend.
  87. * For example, some controllers (like OHCI) need one of the PLL clocks
  88. * in order to act as a wakeup source, and those are not available when
  89. * going into slow clock mode.
  90. *
  91. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  92. * the very same problem (but not using at91 main_clk), and it'd be better
  93. * to add one generic API rather than lots of platform-specific ones.
  94. */
  95. int at91_suspend_entering_slow_clock(void)
  96. {
  97. return (target_state == PM_SUSPEND_MEM);
  98. }
  99. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  100. static void (*slow_clock)(void __iomem *pmc, void __iomem *ramc0,
  101. void __iomem *ramc1, int memctrl);
  102. #ifdef CONFIG_AT91_SLOW_CLOCK
  103. extern void at91_slow_clock(void __iomem *pmc, void __iomem *ramc0,
  104. void __iomem *ramc1, int memctrl);
  105. extern u32 at91_slow_clock_sz;
  106. #endif
  107. static int at91_pm_enter(suspend_state_t state)
  108. {
  109. at91_pinctrl_gpio_suspend();
  110. switch (state) {
  111. /*
  112. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  113. * drivers must suspend more deeply: only the master clock
  114. * controller may be using the main oscillator.
  115. */
  116. case PM_SUSPEND_MEM:
  117. /*
  118. * Ensure that clocks are in a valid state.
  119. */
  120. if (!at91_pm_verify_clocks())
  121. goto error;
  122. /*
  123. * Enter slow clock mode by switching over to clk32k and
  124. * turning off the main oscillator; reverse on wakeup.
  125. */
  126. if (slow_clock) {
  127. #ifdef CONFIG_AT91_SLOW_CLOCK
  128. /* copy slow_clock handler to SRAM, and call it */
  129. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  130. #endif
  131. slow_clock(at91_pmc_base, at91_ramc_base[0],
  132. at91_ramc_base[1],
  133. at91_pm_data.memctrl);
  134. break;
  135. } else {
  136. pr_info("AT91: PM - no slow clock mode enabled ...\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 no SDRAM accesses are needed until the
  149. * wakeup IRQ occurs and self-refresh is terminated.
  150. * For ARM 926 based chips, this requirement is weaker
  151. * as at91sam9 can access a RAM in self-refresh mode.
  152. */
  153. if (at91_pm_standby)
  154. at91_pm_standby();
  155. break;
  156. case PM_SUSPEND_ON:
  157. cpu_do_idle();
  158. break;
  159. default:
  160. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  161. goto error;
  162. }
  163. error:
  164. target_state = PM_SUSPEND_ON;
  165. at91_pinctrl_gpio_resume();
  166. return 0;
  167. }
  168. /*
  169. * Called right prior to thawing processes.
  170. */
  171. static void at91_pm_end(void)
  172. {
  173. target_state = PM_SUSPEND_ON;
  174. }
  175. static const struct platform_suspend_ops at91_pm_ops = {
  176. .valid = at91_pm_valid_state,
  177. .begin = at91_pm_begin,
  178. .enter = at91_pm_enter,
  179. .end = at91_pm_end,
  180. };
  181. static struct platform_device at91_cpuidle_device = {
  182. .name = "cpuidle-at91",
  183. };
  184. void at91_pm_set_standby(void (*at91_standby)(void))
  185. {
  186. if (at91_standby) {
  187. at91_cpuidle_device.dev.platform_data = at91_standby;
  188. at91_pm_standby = at91_standby;
  189. }
  190. }
  191. #ifdef CONFIG_AT91_SLOW_CLOCK
  192. static void __init at91_pm_sram_init(void)
  193. {
  194. struct gen_pool *sram_pool;
  195. phys_addr_t sram_pbase;
  196. unsigned long sram_base;
  197. struct device_node *node;
  198. struct platform_device *pdev;
  199. node = of_find_compatible_node(NULL, NULL, "mmio-sram");
  200. if (!node) {
  201. pr_warn("%s: failed to find sram node!\n", __func__);
  202. return;
  203. }
  204. pdev = of_find_device_by_node(node);
  205. if (!pdev) {
  206. pr_warn("%s: failed to find sram device!\n", __func__);
  207. goto put_node;
  208. }
  209. sram_pool = dev_get_gen_pool(&pdev->dev);
  210. if (!sram_pool) {
  211. pr_warn("%s: sram pool unavailable!\n", __func__);
  212. goto put_node;
  213. }
  214. sram_base = gen_pool_alloc(sram_pool, at91_slow_clock_sz);
  215. if (!sram_base) {
  216. pr_warn("%s: unable to alloc ocram!\n", __func__);
  217. goto put_node;
  218. }
  219. sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
  220. slow_clock = __arm_ioremap_exec(sram_pbase, at91_slow_clock_sz, false);
  221. put_node:
  222. of_node_put(node);
  223. }
  224. #endif
  225. static void __init at91_pm_init(void)
  226. {
  227. #ifdef CONFIG_AT91_SLOW_CLOCK
  228. at91_pm_sram_init();
  229. #endif
  230. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  231. if (at91_cpuidle_device.dev.platform_data)
  232. platform_device_register(&at91_cpuidle_device);
  233. suspend_set_ops(&at91_pm_ops);
  234. }
  235. void __init at91_rm9200_pm_init(void)
  236. {
  237. /*
  238. * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
  239. */
  240. at91_ramc_write(0, AT91RM9200_SDRAMC_LPR, 0);
  241. at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP;
  242. at91_pm_data.memctrl = AT91_MEMCTRL_MC;
  243. at91_pm_init();
  244. }
  245. void __init at91_sam9260_pm_init(void)
  246. {
  247. at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
  248. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  249. return at91_pm_init();
  250. }
  251. void __init at91_sam9g45_pm_init(void)
  252. {
  253. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP;
  254. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  255. return at91_pm_init();
  256. }
  257. void __init at91_sam9x5_pm_init(void)
  258. {
  259. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  260. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  261. return at91_pm_init();
  262. }