pm.c 13 KB

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