pm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/of_address.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/clk/at91_pmc.h>
  26. #include <asm/irq.h>
  27. #include <linux/atomic.h>
  28. #include <asm/mach/time.h>
  29. #include <asm/mach/irq.h>
  30. #include <mach/cpu.h>
  31. #include <mach/hardware.h>
  32. #include "generic.h"
  33. #include "pm.h"
  34. static struct {
  35. unsigned long uhp_udp_mask;
  36. int memctrl;
  37. } at91_pm_data;
  38. static void (*at91_pm_standby)(void);
  39. void __iomem *at91_ramc_base[2];
  40. static int at91_pm_valid_state(suspend_state_t state)
  41. {
  42. switch (state) {
  43. case PM_SUSPEND_ON:
  44. case PM_SUSPEND_STANDBY:
  45. case PM_SUSPEND_MEM:
  46. return 1;
  47. default:
  48. return 0;
  49. }
  50. }
  51. static suspend_state_t target_state;
  52. /*
  53. * Called after processes are frozen, but before we shutdown devices.
  54. */
  55. static int at91_pm_begin(suspend_state_t state)
  56. {
  57. target_state = state;
  58. return 0;
  59. }
  60. /*
  61. * Verify that all the clocks are correct before entering
  62. * slow-clock mode.
  63. */
  64. static int at91_pm_verify_clocks(void)
  65. {
  66. unsigned long scsr;
  67. int i;
  68. scsr = at91_pmc_read(AT91_PMC_SCSR);
  69. /* USB must not be using PLLB */
  70. if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
  71. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  72. return 0;
  73. }
  74. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  75. for (i = 0; i < 4; i++) {
  76. u32 css;
  77. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  78. continue;
  79. css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  80. if (css != AT91_PMC_CSS_SLOW) {
  81. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  82. return 0;
  83. }
  84. }
  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 __iomem *pmc, void __iomem *ramc0,
  103. void __iomem *ramc1, int memctrl);
  104. #ifdef CONFIG_AT91_SLOW_CLOCK
  105. extern void at91_slow_clock(void __iomem *pmc, void __iomem *ramc0,
  106. void __iomem *ramc1, int memctrl);
  107. extern u32 at91_slow_clock_sz;
  108. #endif
  109. static int at91_pm_enter(suspend_state_t state)
  110. {
  111. at91_pinctrl_gpio_suspend();
  112. switch (state) {
  113. /*
  114. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  115. * drivers must suspend more deeply: only the master clock
  116. * controller may be using the main oscillator.
  117. */
  118. case PM_SUSPEND_MEM:
  119. /*
  120. * Ensure that clocks are in a valid state.
  121. */
  122. if (!at91_pm_verify_clocks())
  123. goto error;
  124. /*
  125. * Enter slow clock mode by switching over to clk32k and
  126. * turning off the main oscillator; reverse on wakeup.
  127. */
  128. if (slow_clock) {
  129. #ifdef CONFIG_AT91_SLOW_CLOCK
  130. /* copy slow_clock handler to SRAM, and call it */
  131. memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
  132. #endif
  133. slow_clock(at91_pmc_base, at91_ramc_base[0],
  134. at91_ramc_base[1],
  135. at91_pm_data.memctrl);
  136. break;
  137. } else {
  138. pr_info("AT91: PM - no slow clock mode enabled ...\n");
  139. /* FALLTHROUGH leaving master clock alone */
  140. }
  141. /*
  142. * STANDBY mode has *all* drivers suspended; ignores irqs not
  143. * marked as 'wakeup' event sources; and reduces DRAM power.
  144. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  145. * nothing fancy done with main or cpu clocks.
  146. */
  147. case PM_SUSPEND_STANDBY:
  148. /*
  149. * NOTE: the Wait-for-Interrupt instruction needs to be
  150. * in icache so no SDRAM accesses are needed until the
  151. * wakeup IRQ occurs and self-refresh is terminated.
  152. * For ARM 926 based chips, this requirement is weaker
  153. * as at91sam9 can access a RAM in self-refresh mode.
  154. */
  155. if (at91_pm_standby)
  156. at91_pm_standby();
  157. break;
  158. case PM_SUSPEND_ON:
  159. cpu_do_idle();
  160. break;
  161. default:
  162. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  163. goto error;
  164. }
  165. error:
  166. target_state = PM_SUSPEND_ON;
  167. at91_pinctrl_gpio_resume();
  168. return 0;
  169. }
  170. /*
  171. * Called right prior to thawing processes.
  172. */
  173. static void at91_pm_end(void)
  174. {
  175. target_state = PM_SUSPEND_ON;
  176. }
  177. static const struct platform_suspend_ops at91_pm_ops = {
  178. .valid = at91_pm_valid_state,
  179. .begin = at91_pm_begin,
  180. .enter = at91_pm_enter,
  181. .end = at91_pm_end,
  182. };
  183. static struct platform_device at91_cpuidle_device = {
  184. .name = "cpuidle-at91",
  185. };
  186. void at91_pm_set_standby(void (*at91_standby)(void))
  187. {
  188. if (at91_standby) {
  189. at91_cpuidle_device.dev.platform_data = at91_standby;
  190. at91_pm_standby = at91_standby;
  191. }
  192. }
  193. static const struct of_device_id ramc_ids[] __initconst = {
  194. { .compatible = "atmel,at91rm9200-sdramc", .data = at91rm9200_standby },
  195. { .compatible = "atmel,at91sam9260-sdramc", .data = at91sam9_sdram_standby },
  196. { .compatible = "atmel,at91sam9g45-ddramc", .data = at91_ddr_standby },
  197. { .compatible = "atmel,sama5d3-ddramc", .data = at91_ddr_standby },
  198. { /*sentinel*/ }
  199. };
  200. static __init void at91_dt_ramc(void)
  201. {
  202. struct device_node *np;
  203. const struct of_device_id *of_id;
  204. int idx = 0;
  205. const void *standby = NULL;
  206. for_each_matching_node_and_match(np, ramc_ids, &of_id) {
  207. at91_ramc_base[idx] = of_iomap(np, 0);
  208. if (!at91_ramc_base[idx])
  209. panic(pr_fmt("unable to map ramc[%d] cpu registers\n"), idx);
  210. if (!standby)
  211. standby = of_id->data;
  212. idx++;
  213. }
  214. if (!idx)
  215. panic(pr_fmt("unable to find compatible ram controller node in dtb\n"));
  216. if (!standby) {
  217. pr_warn("ramc no standby function available\n");
  218. return;
  219. }
  220. at91_pm_set_standby(standby);
  221. }
  222. #ifdef CONFIG_AT91_SLOW_CLOCK
  223. static void __init at91_pm_sram_init(void)
  224. {
  225. struct gen_pool *sram_pool;
  226. phys_addr_t sram_pbase;
  227. unsigned long sram_base;
  228. struct device_node *node;
  229. struct platform_device *pdev = NULL;
  230. for_each_compatible_node(node, NULL, "mmio-sram") {
  231. pdev = of_find_device_by_node(node);
  232. if (pdev) {
  233. of_node_put(node);
  234. break;
  235. }
  236. }
  237. if (!pdev) {
  238. pr_warn("%s: failed to find sram device!\n", __func__);
  239. return;
  240. }
  241. sram_pool = dev_get_gen_pool(&pdev->dev);
  242. if (!sram_pool) {
  243. pr_warn("%s: sram pool unavailable!\n", __func__);
  244. return;
  245. }
  246. sram_base = gen_pool_alloc(sram_pool, at91_slow_clock_sz);
  247. if (!sram_base) {
  248. pr_warn("%s: unable to alloc ocram!\n", __func__);
  249. return;
  250. }
  251. sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
  252. slow_clock = __arm_ioremap_exec(sram_pbase, at91_slow_clock_sz, false);
  253. }
  254. #endif
  255. static void __init at91_pm_init(void)
  256. {
  257. #ifdef CONFIG_AT91_SLOW_CLOCK
  258. at91_pm_sram_init();
  259. #endif
  260. pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
  261. if (at91_cpuidle_device.dev.platform_data)
  262. platform_device_register(&at91_cpuidle_device);
  263. suspend_set_ops(&at91_pm_ops);
  264. }
  265. void __init at91rm9200_pm_init(void)
  266. {
  267. at91_dt_ramc();
  268. /*
  269. * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
  270. */
  271. at91_ramc_write(0, AT91RM9200_SDRAMC_LPR, 0);
  272. at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP;
  273. at91_pm_data.memctrl = AT91_MEMCTRL_MC;
  274. at91_pm_init();
  275. }
  276. void __init at91sam9260_pm_init(void)
  277. {
  278. at91_dt_ramc();
  279. at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
  280. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  281. return at91_pm_init();
  282. }
  283. void __init at91sam9g45_pm_init(void)
  284. {
  285. at91_dt_ramc();
  286. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP;
  287. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  288. return at91_pm_init();
  289. }
  290. void __init at91sam9x5_pm_init(void)
  291. {
  292. at91_dt_ramc();
  293. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  294. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  295. return at91_pm_init();
  296. }