pm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <asm/fncpy.h>
  31. #include <asm/cacheflush.h>
  32. #include "generic.h"
  33. #include "pm.h"
  34. /*
  35. * FIXME: this is needed to communicate between the pinctrl driver and
  36. * the PM implementation in the machine. Possibly part of the PM
  37. * implementation should be moved down into the pinctrl driver and get
  38. * called as part of the generic suspend/resume path.
  39. */
  40. extern void at91_pinctrl_gpio_suspend(void);
  41. extern void at91_pinctrl_gpio_resume(void);
  42. static struct {
  43. unsigned long uhp_udp_mask;
  44. int memctrl;
  45. } at91_pm_data;
  46. void __iomem *at91_ramc_base[2];
  47. static int at91_pm_valid_state(suspend_state_t state)
  48. {
  49. switch (state) {
  50. case PM_SUSPEND_ON:
  51. case PM_SUSPEND_STANDBY:
  52. case PM_SUSPEND_MEM:
  53. return 1;
  54. default:
  55. return 0;
  56. }
  57. }
  58. static suspend_state_t target_state;
  59. /*
  60. * Called after processes are frozen, but before we shutdown devices.
  61. */
  62. static int at91_pm_begin(suspend_state_t state)
  63. {
  64. target_state = state;
  65. return 0;
  66. }
  67. /*
  68. * Verify that all the clocks are correct before entering
  69. * slow-clock mode.
  70. */
  71. static int at91_pm_verify_clocks(void)
  72. {
  73. unsigned long scsr;
  74. int i;
  75. scsr = at91_pmc_read(AT91_PMC_SCSR);
  76. /* USB must not be using PLLB */
  77. if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
  78. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  79. return 0;
  80. }
  81. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  82. for (i = 0; i < 4; i++) {
  83. u32 css;
  84. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  85. continue;
  86. css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  87. if (css != AT91_PMC_CSS_SLOW) {
  88. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  89. return 0;
  90. }
  91. }
  92. return 1;
  93. }
  94. /*
  95. * Call this from platform driver suspend() to see how deeply to suspend.
  96. * For example, some controllers (like OHCI) need one of the PLL clocks
  97. * in order to act as a wakeup source, and those are not available when
  98. * going into slow clock mode.
  99. *
  100. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  101. * the very same problem (but not using at91 main_clk), and it'd be better
  102. * to add one generic API rather than lots of platform-specific ones.
  103. */
  104. int at91_suspend_entering_slow_clock(void)
  105. {
  106. return (target_state == PM_SUSPEND_MEM);
  107. }
  108. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  109. static void (*at91_suspend_sram_fn)(void __iomem *pmc, void __iomem *ramc0,
  110. void __iomem *ramc1, int memctrl);
  111. extern void at91_pm_suspend_in_sram(void __iomem *pmc, void __iomem *ramc0,
  112. void __iomem *ramc1, int memctrl);
  113. extern u32 at91_pm_suspend_in_sram_sz;
  114. static void at91_pm_suspend(suspend_state_t state)
  115. {
  116. unsigned int pm_data = at91_pm_data.memctrl;
  117. pm_data |= (state == PM_SUSPEND_MEM) ?
  118. AT91_PM_MODE(AT91_PM_SLOW_CLOCK) : 0;
  119. flush_cache_all();
  120. outer_disable();
  121. at91_suspend_sram_fn(at91_pmc_base, at91_ramc_base[0],
  122. at91_ramc_base[1], pm_data);
  123. outer_resume();
  124. }
  125. static int at91_pm_enter(suspend_state_t state)
  126. {
  127. at91_pinctrl_gpio_suspend();
  128. switch (state) {
  129. /*
  130. * Suspend-to-RAM is like STANDBY plus slow clock mode, so
  131. * drivers must suspend more deeply, the master clock switches
  132. * to the clk32k and turns off the main oscillator
  133. */
  134. case PM_SUSPEND_MEM:
  135. /*
  136. * Ensure that clocks are in a valid state.
  137. */
  138. if (!at91_pm_verify_clocks())
  139. goto error;
  140. at91_pm_suspend(state);
  141. break;
  142. /*
  143. * STANDBY mode has *all* drivers suspended; ignores irqs not
  144. * marked as 'wakeup' event sources; and reduces DRAM power.
  145. * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
  146. * nothing fancy done with main or cpu clocks.
  147. */
  148. case PM_SUSPEND_STANDBY:
  149. at91_pm_suspend(state);
  150. break;
  151. case PM_SUSPEND_ON:
  152. cpu_do_idle();
  153. break;
  154. default:
  155. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  156. goto error;
  157. }
  158. error:
  159. target_state = PM_SUSPEND_ON;
  160. at91_pinctrl_gpio_resume();
  161. return 0;
  162. }
  163. /*
  164. * Called right prior to thawing processes.
  165. */
  166. static void at91_pm_end(void)
  167. {
  168. target_state = PM_SUSPEND_ON;
  169. }
  170. static const struct platform_suspend_ops at91_pm_ops = {
  171. .valid = at91_pm_valid_state,
  172. .begin = at91_pm_begin,
  173. .enter = at91_pm_enter,
  174. .end = at91_pm_end,
  175. };
  176. static struct platform_device at91_cpuidle_device = {
  177. .name = "cpuidle-at91",
  178. };
  179. static void at91_pm_set_standby(void (*at91_standby)(void))
  180. {
  181. if (at91_standby)
  182. at91_cpuidle_device.dev.platform_data = at91_standby;
  183. }
  184. /*
  185. * The AT91RM9200 goes into self-refresh mode with this command, and will
  186. * terminate self-refresh automatically on the next SDRAM access.
  187. *
  188. * Self-refresh mode is exited as soon as a memory access is made, but we don't
  189. * know for sure when that happens. However, we need to restore the low-power
  190. * mode if it was enabled before going idle. Restoring low-power mode while
  191. * still in self-refresh is "not recommended", but seems to work.
  192. */
  193. static void at91rm9200_standby(void)
  194. {
  195. u32 lpr = at91_ramc_read(0, AT91_MC_SDRAMC_LPR);
  196. asm volatile(
  197. "b 1f\n\t"
  198. ".align 5\n\t"
  199. "1: mcr p15, 0, %0, c7, c10, 4\n\t"
  200. " str %0, [%1, %2]\n\t"
  201. " str %3, [%1, %4]\n\t"
  202. " mcr p15, 0, %0, c7, c0, 4\n\t"
  203. " str %5, [%1, %2]"
  204. :
  205. : "r" (0), "r" (at91_ramc_base[0]), "r" (AT91_MC_SDRAMC_LPR),
  206. "r" (1), "r" (AT91_MC_SDRAMC_SRR),
  207. "r" (lpr));
  208. }
  209. /* We manage both DDRAM/SDRAM controllers, we need more than one value to
  210. * remember.
  211. */
  212. static void at91_ddr_standby(void)
  213. {
  214. /* Those two values allow us to delay self-refresh activation
  215. * to the maximum. */
  216. u32 lpr0, lpr1 = 0;
  217. u32 saved_lpr0, saved_lpr1 = 0;
  218. if (at91_ramc_base[1]) {
  219. saved_lpr1 = at91_ramc_read(1, AT91_DDRSDRC_LPR);
  220. lpr1 = saved_lpr1 & ~AT91_DDRSDRC_LPCB;
  221. lpr1 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
  222. }
  223. saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
  224. lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
  225. lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
  226. /* self-refresh mode now */
  227. at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
  228. if (at91_ramc_base[1])
  229. at91_ramc_write(1, AT91_DDRSDRC_LPR, lpr1);
  230. cpu_do_idle();
  231. at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
  232. if (at91_ramc_base[1])
  233. at91_ramc_write(1, AT91_DDRSDRC_LPR, saved_lpr1);
  234. }
  235. /* We manage both DDRAM/SDRAM controllers, we need more than one value to
  236. * remember.
  237. */
  238. static void at91sam9_sdram_standby(void)
  239. {
  240. u32 lpr0, lpr1 = 0;
  241. u32 saved_lpr0, saved_lpr1 = 0;
  242. if (at91_ramc_base[1]) {
  243. saved_lpr1 = at91_ramc_read(1, AT91_SDRAMC_LPR);
  244. lpr1 = saved_lpr1 & ~AT91_SDRAMC_LPCB;
  245. lpr1 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
  246. }
  247. saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
  248. lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
  249. lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
  250. /* self-refresh mode now */
  251. at91_ramc_write(0, AT91_SDRAMC_LPR, lpr0);
  252. if (at91_ramc_base[1])
  253. at91_ramc_write(1, AT91_SDRAMC_LPR, lpr1);
  254. cpu_do_idle();
  255. at91_ramc_write(0, AT91_SDRAMC_LPR, saved_lpr0);
  256. if (at91_ramc_base[1])
  257. at91_ramc_write(1, AT91_SDRAMC_LPR, saved_lpr1);
  258. }
  259. static const struct of_device_id const ramc_ids[] __initconst = {
  260. { .compatible = "atmel,at91rm9200-sdramc", .data = at91rm9200_standby },
  261. { .compatible = "atmel,at91sam9260-sdramc", .data = at91sam9_sdram_standby },
  262. { .compatible = "atmel,at91sam9g45-ddramc", .data = at91_ddr_standby },
  263. { .compatible = "atmel,sama5d3-ddramc", .data = at91_ddr_standby },
  264. { /*sentinel*/ }
  265. };
  266. static __init void at91_dt_ramc(void)
  267. {
  268. struct device_node *np;
  269. const struct of_device_id *of_id;
  270. int idx = 0;
  271. const void *standby = NULL;
  272. for_each_matching_node_and_match(np, ramc_ids, &of_id) {
  273. at91_ramc_base[idx] = of_iomap(np, 0);
  274. if (!at91_ramc_base[idx])
  275. panic(pr_fmt("unable to map ramc[%d] cpu registers\n"), idx);
  276. if (!standby)
  277. standby = of_id->data;
  278. idx++;
  279. }
  280. if (!idx)
  281. panic(pr_fmt("unable to find compatible ram controller node in dtb\n"));
  282. if (!standby) {
  283. pr_warn("ramc no standby function available\n");
  284. return;
  285. }
  286. at91_pm_set_standby(standby);
  287. }
  288. static void __init at91_pm_sram_init(void)
  289. {
  290. struct gen_pool *sram_pool;
  291. phys_addr_t sram_pbase;
  292. unsigned long sram_base;
  293. struct device_node *node;
  294. struct platform_device *pdev = NULL;
  295. for_each_compatible_node(node, NULL, "mmio-sram") {
  296. pdev = of_find_device_by_node(node);
  297. if (pdev) {
  298. of_node_put(node);
  299. break;
  300. }
  301. }
  302. if (!pdev) {
  303. pr_warn("%s: failed to find sram device!\n", __func__);
  304. return;
  305. }
  306. sram_pool = gen_pool_get(&pdev->dev, NULL);
  307. if (!sram_pool) {
  308. pr_warn("%s: sram pool unavailable!\n", __func__);
  309. return;
  310. }
  311. sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
  312. if (!sram_base) {
  313. pr_warn("%s: unable to alloc sram!\n", __func__);
  314. return;
  315. }
  316. sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
  317. at91_suspend_sram_fn = __arm_ioremap_exec(sram_pbase,
  318. at91_pm_suspend_in_sram_sz, false);
  319. if (!at91_suspend_sram_fn) {
  320. pr_warn("SRAM: Could not map\n");
  321. return;
  322. }
  323. /* Copy the pm suspend handler to SRAM */
  324. at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
  325. &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
  326. }
  327. static void __init at91_pm_init(void)
  328. {
  329. at91_pm_sram_init();
  330. if (at91_cpuidle_device.dev.platform_data)
  331. platform_device_register(&at91_cpuidle_device);
  332. if (at91_suspend_sram_fn)
  333. suspend_set_ops(&at91_pm_ops);
  334. else
  335. pr_info("AT91: PM not supported, due to no SRAM allocated\n");
  336. }
  337. void __init at91rm9200_pm_init(void)
  338. {
  339. at91_dt_ramc();
  340. /*
  341. * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
  342. */
  343. at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
  344. at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP;
  345. at91_pm_data.memctrl = AT91_MEMCTRL_MC;
  346. at91_pm_init();
  347. }
  348. void __init at91sam9260_pm_init(void)
  349. {
  350. at91_dt_ramc();
  351. at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
  352. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  353. return at91_pm_init();
  354. }
  355. void __init at91sam9g45_pm_init(void)
  356. {
  357. at91_dt_ramc();
  358. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP;
  359. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  360. return at91_pm_init();
  361. }
  362. void __init at91sam9x5_pm_init(void)
  363. {
  364. at91_dt_ramc();
  365. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  366. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  367. return at91_pm_init();
  368. }