pm.c 12 KB

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