pm.c 11 KB

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