pm.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * arch/arm/mach-at91/pm.c
  4. * AT91 Power Management
  5. *
  6. * Copyright (C) 2005 David Brownell
  7. */
  8. #include <linux/genalloc.h>
  9. #include <linux/io.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/parser.h>
  14. #include <linux/suspend.h>
  15. #include <linux/clk/at91_pmc.h>
  16. #include <linux/platform_data/atmel.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/fncpy.h>
  19. #include <asm/system_misc.h>
  20. #include <asm/suspend.h>
  21. #include "generic.h"
  22. #include "pm.h"
  23. /*
  24. * FIXME: this is needed to communicate between the pinctrl driver and
  25. * the PM implementation in the machine. Possibly part of the PM
  26. * implementation should be moved down into the pinctrl driver and get
  27. * called as part of the generic suspend/resume path.
  28. */
  29. #ifdef CONFIG_PINCTRL_AT91
  30. extern void at91_pinctrl_gpio_suspend(void);
  31. extern void at91_pinctrl_gpio_resume(void);
  32. #endif
  33. struct at91_soc_pm {
  34. int (*config_shdwc_ws)(void __iomem *shdwc, u32 *mode, u32 *polarity);
  35. int (*config_pmc_ws)(void __iomem *pmc, u32 mode, u32 polarity);
  36. const struct of_device_id *ws_ids;
  37. struct at91_pm_data data;
  38. };
  39. static struct at91_soc_pm soc_pm = {
  40. .data = {
  41. .standby_mode = AT91_PM_STANDBY,
  42. .suspend_mode = AT91_PM_ULP0,
  43. },
  44. };
  45. static const match_table_t pm_modes __initconst = {
  46. { AT91_PM_STANDBY, "standby" },
  47. { AT91_PM_ULP0, "ulp0" },
  48. { AT91_PM_ULP0_FAST, "ulp0-fast" },
  49. { AT91_PM_ULP1, "ulp1" },
  50. { AT91_PM_BACKUP, "backup" },
  51. { -1, NULL },
  52. };
  53. #define at91_ramc_read(id, field) \
  54. __raw_readl(soc_pm.data.ramc[id] + field)
  55. #define at91_ramc_write(id, field, value) \
  56. __raw_writel(value, soc_pm.data.ramc[id] + field)
  57. static int at91_pm_valid_state(suspend_state_t state)
  58. {
  59. switch (state) {
  60. case PM_SUSPEND_ON:
  61. case PM_SUSPEND_STANDBY:
  62. case PM_SUSPEND_MEM:
  63. return 1;
  64. default:
  65. return 0;
  66. }
  67. }
  68. static int canary = 0xA5A5A5A5;
  69. static struct at91_pm_bu {
  70. int suspended;
  71. unsigned long reserved;
  72. phys_addr_t canary;
  73. phys_addr_t resume;
  74. } *pm_bu;
  75. struct wakeup_source_info {
  76. unsigned int pmc_fsmr_bit;
  77. unsigned int shdwc_mr_bit;
  78. bool set_polarity;
  79. };
  80. static const struct wakeup_source_info ws_info[] = {
  81. { .pmc_fsmr_bit = AT91_PMC_FSTT(10), .set_polarity = true },
  82. { .pmc_fsmr_bit = AT91_PMC_RTCAL, .shdwc_mr_bit = BIT(17) },
  83. { .pmc_fsmr_bit = AT91_PMC_USBAL },
  84. { .pmc_fsmr_bit = AT91_PMC_SDMMC_CD },
  85. { .pmc_fsmr_bit = AT91_PMC_RTTAL },
  86. { .pmc_fsmr_bit = AT91_PMC_RXLP_MCE },
  87. };
  88. static const struct of_device_id sama5d2_ws_ids[] = {
  89. { .compatible = "atmel,sama5d2-gem", .data = &ws_info[0] },
  90. { .compatible = "atmel,sama5d2-rtc", .data = &ws_info[1] },
  91. { .compatible = "atmel,sama5d3-udc", .data = &ws_info[2] },
  92. { .compatible = "atmel,at91rm9200-ohci", .data = &ws_info[2] },
  93. { .compatible = "usb-ohci", .data = &ws_info[2] },
  94. { .compatible = "atmel,at91sam9g45-ehci", .data = &ws_info[2] },
  95. { .compatible = "usb-ehci", .data = &ws_info[2] },
  96. { .compatible = "atmel,sama5d2-sdhci", .data = &ws_info[3] },
  97. { /* sentinel */ }
  98. };
  99. static const struct of_device_id sam9x60_ws_ids[] = {
  100. { .compatible = "microchip,sam9x60-rtc", .data = &ws_info[1] },
  101. { .compatible = "atmel,at91rm9200-ohci", .data = &ws_info[2] },
  102. { .compatible = "usb-ohci", .data = &ws_info[2] },
  103. { .compatible = "atmel,at91sam9g45-ehci", .data = &ws_info[2] },
  104. { .compatible = "usb-ehci", .data = &ws_info[2] },
  105. { .compatible = "microchip,sam9x60-rtt", .data = &ws_info[4] },
  106. { .compatible = "cdns,sam9x60-macb", .data = &ws_info[5] },
  107. { /* sentinel */ }
  108. };
  109. static int at91_pm_config_ws(unsigned int pm_mode, bool set)
  110. {
  111. const struct wakeup_source_info *wsi;
  112. const struct of_device_id *match;
  113. struct platform_device *pdev;
  114. struct device_node *np;
  115. unsigned int mode = 0, polarity = 0, val = 0;
  116. if (pm_mode != AT91_PM_ULP1)
  117. return 0;
  118. if (!soc_pm.data.pmc || !soc_pm.data.shdwc || !soc_pm.ws_ids)
  119. return -EPERM;
  120. if (!set) {
  121. writel(mode, soc_pm.data.pmc + AT91_PMC_FSMR);
  122. return 0;
  123. }
  124. if (soc_pm.config_shdwc_ws)
  125. soc_pm.config_shdwc_ws(soc_pm.data.shdwc, &mode, &polarity);
  126. /* SHDWC.MR */
  127. val = readl(soc_pm.data.shdwc + 0x04);
  128. /* Loop through defined wakeup sources. */
  129. for_each_matching_node_and_match(np, soc_pm.ws_ids, &match) {
  130. pdev = of_find_device_by_node(np);
  131. if (!pdev)
  132. continue;
  133. if (device_may_wakeup(&pdev->dev)) {
  134. wsi = match->data;
  135. /* Check if enabled on SHDWC. */
  136. if (wsi->shdwc_mr_bit && !(val & wsi->shdwc_mr_bit))
  137. goto put_device;
  138. mode |= wsi->pmc_fsmr_bit;
  139. if (wsi->set_polarity)
  140. polarity |= wsi->pmc_fsmr_bit;
  141. }
  142. put_device:
  143. put_device(&pdev->dev);
  144. }
  145. if (mode) {
  146. if (soc_pm.config_pmc_ws)
  147. soc_pm.config_pmc_ws(soc_pm.data.pmc, mode, polarity);
  148. } else {
  149. pr_err("AT91: PM: no ULP1 wakeup sources found!");
  150. }
  151. return mode ? 0 : -EPERM;
  152. }
  153. static int at91_sama5d2_config_shdwc_ws(void __iomem *shdwc, u32 *mode,
  154. u32 *polarity)
  155. {
  156. u32 val;
  157. /* SHDWC.WUIR */
  158. val = readl(shdwc + 0x0c);
  159. *mode |= (val & 0x3ff);
  160. *polarity |= ((val >> 16) & 0x3ff);
  161. return 0;
  162. }
  163. static int at91_sama5d2_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
  164. {
  165. writel(mode, pmc + AT91_PMC_FSMR);
  166. writel(polarity, pmc + AT91_PMC_FSPR);
  167. return 0;
  168. }
  169. static int at91_sam9x60_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
  170. {
  171. writel(mode, pmc + AT91_PMC_FSMR);
  172. return 0;
  173. }
  174. /*
  175. * Called after processes are frozen, but before we shutdown devices.
  176. */
  177. static int at91_pm_begin(suspend_state_t state)
  178. {
  179. switch (state) {
  180. case PM_SUSPEND_MEM:
  181. soc_pm.data.mode = soc_pm.data.suspend_mode;
  182. break;
  183. case PM_SUSPEND_STANDBY:
  184. soc_pm.data.mode = soc_pm.data.standby_mode;
  185. break;
  186. default:
  187. soc_pm.data.mode = -1;
  188. }
  189. return at91_pm_config_ws(soc_pm.data.mode, true);
  190. }
  191. /*
  192. * Verify that all the clocks are correct before entering
  193. * slow-clock mode.
  194. */
  195. static int at91_pm_verify_clocks(void)
  196. {
  197. unsigned long scsr;
  198. int i;
  199. scsr = readl(soc_pm.data.pmc + AT91_PMC_SCSR);
  200. /* USB must not be using PLLB */
  201. if ((scsr & soc_pm.data.uhp_udp_mask) != 0) {
  202. pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
  203. return 0;
  204. }
  205. /* PCK0..PCK3 must be disabled, or configured to use clk32k */
  206. for (i = 0; i < 4; i++) {
  207. u32 css;
  208. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  209. continue;
  210. css = readl(soc_pm.data.pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  211. if (css != AT91_PMC_CSS_SLOW) {
  212. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  213. return 0;
  214. }
  215. }
  216. return 1;
  217. }
  218. /*
  219. * Call this from platform driver suspend() to see how deeply to suspend.
  220. * For example, some controllers (like OHCI) need one of the PLL clocks
  221. * in order to act as a wakeup source, and those are not available when
  222. * going into slow clock mode.
  223. *
  224. * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
  225. * the very same problem (but not using at91 main_clk), and it'd be better
  226. * to add one generic API rather than lots of platform-specific ones.
  227. */
  228. int at91_suspend_entering_slow_clock(void)
  229. {
  230. return (soc_pm.data.mode >= AT91_PM_ULP0);
  231. }
  232. EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
  233. static void (*at91_suspend_sram_fn)(struct at91_pm_data *);
  234. extern void at91_pm_suspend_in_sram(struct at91_pm_data *pm_data);
  235. extern u32 at91_pm_suspend_in_sram_sz;
  236. static int at91_suspend_finish(unsigned long val)
  237. {
  238. flush_cache_all();
  239. outer_disable();
  240. at91_suspend_sram_fn(&soc_pm.data);
  241. return 0;
  242. }
  243. static void at91_pm_suspend(suspend_state_t state)
  244. {
  245. if (soc_pm.data.mode == AT91_PM_BACKUP) {
  246. pm_bu->suspended = 1;
  247. cpu_suspend(0, at91_suspend_finish);
  248. /* The SRAM is lost between suspend cycles */
  249. at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
  250. &at91_pm_suspend_in_sram,
  251. at91_pm_suspend_in_sram_sz);
  252. } else {
  253. at91_suspend_finish(0);
  254. }
  255. outer_resume();
  256. }
  257. /*
  258. * STANDBY mode has *all* drivers suspended; ignores irqs not marked as 'wakeup'
  259. * event sources; and reduces DRAM power. But otherwise it's identical to
  260. * PM_SUSPEND_ON: cpu idle, and nothing fancy done with main or cpu clocks.
  261. *
  262. * AT91_PM_ULP0 is like STANDBY plus slow clock mode, so drivers must
  263. * suspend more deeply, the master clock switches to the clk32k and turns off
  264. * the main oscillator
  265. *
  266. * AT91_PM_BACKUP turns off the whole SoC after placing the DDR in self refresh
  267. */
  268. static int at91_pm_enter(suspend_state_t state)
  269. {
  270. #ifdef CONFIG_PINCTRL_AT91
  271. at91_pinctrl_gpio_suspend();
  272. #endif
  273. switch (state) {
  274. case PM_SUSPEND_MEM:
  275. case PM_SUSPEND_STANDBY:
  276. /*
  277. * Ensure that clocks are in a valid state.
  278. */
  279. if (soc_pm.data.mode >= AT91_PM_ULP0 &&
  280. !at91_pm_verify_clocks())
  281. goto error;
  282. at91_pm_suspend(state);
  283. break;
  284. case PM_SUSPEND_ON:
  285. cpu_do_idle();
  286. break;
  287. default:
  288. pr_debug("AT91: PM - bogus suspend state %d\n", state);
  289. goto error;
  290. }
  291. error:
  292. #ifdef CONFIG_PINCTRL_AT91
  293. at91_pinctrl_gpio_resume();
  294. #endif
  295. return 0;
  296. }
  297. /*
  298. * Called right prior to thawing processes.
  299. */
  300. static void at91_pm_end(void)
  301. {
  302. at91_pm_config_ws(soc_pm.data.mode, false);
  303. }
  304. static const struct platform_suspend_ops at91_pm_ops = {
  305. .valid = at91_pm_valid_state,
  306. .begin = at91_pm_begin,
  307. .enter = at91_pm_enter,
  308. .end = at91_pm_end,
  309. };
  310. static struct platform_device at91_cpuidle_device = {
  311. .name = "cpuidle-at91",
  312. };
  313. /*
  314. * The AT91RM9200 goes into self-refresh mode with this command, and will
  315. * terminate self-refresh automatically on the next SDRAM access.
  316. *
  317. * Self-refresh mode is exited as soon as a memory access is made, but we don't
  318. * know for sure when that happens. However, we need to restore the low-power
  319. * mode if it was enabled before going idle. Restoring low-power mode while
  320. * still in self-refresh is "not recommended", but seems to work.
  321. */
  322. static void at91rm9200_standby(void)
  323. {
  324. asm volatile(
  325. "b 1f\n\t"
  326. ".align 5\n\t"
  327. "1: mcr p15, 0, %0, c7, c10, 4\n\t"
  328. " str %2, [%1, %3]\n\t"
  329. " mcr p15, 0, %0, c7, c0, 4\n\t"
  330. :
  331. : "r" (0), "r" (soc_pm.data.ramc[0]),
  332. "r" (1), "r" (AT91_MC_SDRAMC_SRR));
  333. }
  334. /* We manage both DDRAM/SDRAM controllers, we need more than one value to
  335. * remember.
  336. */
  337. static void at91_ddr_standby(void)
  338. {
  339. /* Those two values allow us to delay self-refresh activation
  340. * to the maximum. */
  341. u32 lpr0, lpr1 = 0;
  342. u32 mdr, saved_mdr0, saved_mdr1 = 0;
  343. u32 saved_lpr0, saved_lpr1 = 0;
  344. /* LPDDR1 --> force DDR2 mode during self-refresh */
  345. saved_mdr0 = at91_ramc_read(0, AT91_DDRSDRC_MDR);
  346. if ((saved_mdr0 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
  347. mdr = saved_mdr0 & ~AT91_DDRSDRC_MD;
  348. mdr |= AT91_DDRSDRC_MD_DDR2;
  349. at91_ramc_write(0, AT91_DDRSDRC_MDR, mdr);
  350. }
  351. if (soc_pm.data.ramc[1]) {
  352. saved_lpr1 = at91_ramc_read(1, AT91_DDRSDRC_LPR);
  353. lpr1 = saved_lpr1 & ~AT91_DDRSDRC_LPCB;
  354. lpr1 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
  355. saved_mdr1 = at91_ramc_read(1, AT91_DDRSDRC_MDR);
  356. if ((saved_mdr1 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
  357. mdr = saved_mdr1 & ~AT91_DDRSDRC_MD;
  358. mdr |= AT91_DDRSDRC_MD_DDR2;
  359. at91_ramc_write(1, AT91_DDRSDRC_MDR, mdr);
  360. }
  361. }
  362. saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
  363. lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
  364. lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
  365. /* self-refresh mode now */
  366. at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
  367. if (soc_pm.data.ramc[1])
  368. at91_ramc_write(1, AT91_DDRSDRC_LPR, lpr1);
  369. cpu_do_idle();
  370. at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr0);
  371. at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
  372. if (soc_pm.data.ramc[1]) {
  373. at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr1);
  374. at91_ramc_write(1, AT91_DDRSDRC_LPR, saved_lpr1);
  375. }
  376. }
  377. static void sama5d3_ddr_standby(void)
  378. {
  379. u32 lpr0;
  380. u32 saved_lpr0;
  381. saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
  382. lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
  383. lpr0 |= AT91_DDRSDRC_LPCB_POWER_DOWN;
  384. at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
  385. cpu_do_idle();
  386. at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
  387. }
  388. /* We manage both DDRAM/SDRAM controllers, we need more than one value to
  389. * remember.
  390. */
  391. static void at91sam9_sdram_standby(void)
  392. {
  393. u32 lpr0, lpr1 = 0;
  394. u32 saved_lpr0, saved_lpr1 = 0;
  395. if (soc_pm.data.ramc[1]) {
  396. saved_lpr1 = at91_ramc_read(1, AT91_SDRAMC_LPR);
  397. lpr1 = saved_lpr1 & ~AT91_SDRAMC_LPCB;
  398. lpr1 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
  399. }
  400. saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
  401. lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
  402. lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
  403. /* self-refresh mode now */
  404. at91_ramc_write(0, AT91_SDRAMC_LPR, lpr0);
  405. if (soc_pm.data.ramc[1])
  406. at91_ramc_write(1, AT91_SDRAMC_LPR, lpr1);
  407. cpu_do_idle();
  408. at91_ramc_write(0, AT91_SDRAMC_LPR, saved_lpr0);
  409. if (soc_pm.data.ramc[1])
  410. at91_ramc_write(1, AT91_SDRAMC_LPR, saved_lpr1);
  411. }
  412. struct ramc_info {
  413. void (*idle)(void);
  414. unsigned int memctrl;
  415. };
  416. static const struct ramc_info ramc_infos[] __initconst = {
  417. { .idle = at91rm9200_standby, .memctrl = AT91_MEMCTRL_MC},
  418. { .idle = at91sam9_sdram_standby, .memctrl = AT91_MEMCTRL_SDRAMC},
  419. { .idle = at91_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
  420. { .idle = sama5d3_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
  421. };
  422. static const struct of_device_id ramc_ids[] __initconst = {
  423. { .compatible = "atmel,at91rm9200-sdramc", .data = &ramc_infos[0] },
  424. { .compatible = "atmel,at91sam9260-sdramc", .data = &ramc_infos[1] },
  425. { .compatible = "atmel,at91sam9g45-ddramc", .data = &ramc_infos[2] },
  426. { .compatible = "atmel,sama5d3-ddramc", .data = &ramc_infos[3] },
  427. { /*sentinel*/ }
  428. };
  429. static __init int at91_dt_ramc(void)
  430. {
  431. struct device_node *np;
  432. const struct of_device_id *of_id;
  433. int idx = 0;
  434. void *standby = NULL;
  435. const struct ramc_info *ramc;
  436. int ret;
  437. for_each_matching_node_and_match(np, ramc_ids, &of_id) {
  438. soc_pm.data.ramc[idx] = of_iomap(np, 0);
  439. if (!soc_pm.data.ramc[idx]) {
  440. pr_err("unable to map ramc[%d] cpu registers\n", idx);
  441. ret = -ENOMEM;
  442. goto unmap_ramc;
  443. }
  444. ramc = of_id->data;
  445. if (!standby)
  446. standby = ramc->idle;
  447. soc_pm.data.memctrl = ramc->memctrl;
  448. idx++;
  449. }
  450. if (!idx) {
  451. pr_err("unable to find compatible ram controller node in dtb\n");
  452. ret = -ENODEV;
  453. goto unmap_ramc;
  454. }
  455. if (!standby) {
  456. pr_warn("ramc no standby function available\n");
  457. return 0;
  458. }
  459. at91_cpuidle_device.dev.platform_data = standby;
  460. return 0;
  461. unmap_ramc:
  462. while (idx)
  463. iounmap(soc_pm.data.ramc[--idx]);
  464. return ret;
  465. }
  466. static void at91rm9200_idle(void)
  467. {
  468. /*
  469. * Disable the processor clock. The processor will be automatically
  470. * re-enabled by an interrupt or by a reset.
  471. */
  472. writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
  473. }
  474. static void at91sam9_idle(void)
  475. {
  476. writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
  477. cpu_do_idle();
  478. }
  479. static void __init at91_pm_sram_init(void)
  480. {
  481. struct gen_pool *sram_pool;
  482. phys_addr_t sram_pbase;
  483. unsigned long sram_base;
  484. struct device_node *node;
  485. struct platform_device *pdev = NULL;
  486. for_each_compatible_node(node, NULL, "mmio-sram") {
  487. pdev = of_find_device_by_node(node);
  488. if (pdev) {
  489. of_node_put(node);
  490. break;
  491. }
  492. }
  493. if (!pdev) {
  494. pr_warn("%s: failed to find sram device!\n", __func__);
  495. return;
  496. }
  497. sram_pool = gen_pool_get(&pdev->dev, NULL);
  498. if (!sram_pool) {
  499. pr_warn("%s: sram pool unavailable!\n", __func__);
  500. goto out_put_device;
  501. }
  502. sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
  503. if (!sram_base) {
  504. pr_warn("%s: unable to alloc sram!\n", __func__);
  505. goto out_put_device;
  506. }
  507. sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
  508. at91_suspend_sram_fn = __arm_ioremap_exec(sram_pbase,
  509. at91_pm_suspend_in_sram_sz, false);
  510. if (!at91_suspend_sram_fn) {
  511. pr_warn("SRAM: Could not map\n");
  512. goto out_put_device;
  513. }
  514. /* Copy the pm suspend handler to SRAM */
  515. at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
  516. &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
  517. return;
  518. out_put_device:
  519. put_device(&pdev->dev);
  520. return;
  521. }
  522. static bool __init at91_is_pm_mode_active(int pm_mode)
  523. {
  524. return (soc_pm.data.standby_mode == pm_mode ||
  525. soc_pm.data.suspend_mode == pm_mode);
  526. }
  527. static int __init at91_pm_backup_init(void)
  528. {
  529. struct gen_pool *sram_pool;
  530. struct device_node *np;
  531. struct platform_device *pdev = NULL;
  532. int ret = -ENODEV;
  533. if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
  534. return -EPERM;
  535. if (!at91_is_pm_mode_active(AT91_PM_BACKUP))
  536. return 0;
  537. np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-sfrbu");
  538. if (!np) {
  539. pr_warn("%s: failed to find sfrbu!\n", __func__);
  540. return ret;
  541. }
  542. soc_pm.data.sfrbu = of_iomap(np, 0);
  543. of_node_put(np);
  544. np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
  545. if (!np)
  546. goto securam_fail_no_ref_dev;
  547. pdev = of_find_device_by_node(np);
  548. of_node_put(np);
  549. if (!pdev) {
  550. pr_warn("%s: failed to find securam device!\n", __func__);
  551. goto securam_fail_no_ref_dev;
  552. }
  553. sram_pool = gen_pool_get(&pdev->dev, NULL);
  554. if (!sram_pool) {
  555. pr_warn("%s: securam pool unavailable!\n", __func__);
  556. goto securam_fail;
  557. }
  558. pm_bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
  559. if (!pm_bu) {
  560. pr_warn("%s: unable to alloc securam!\n", __func__);
  561. ret = -ENOMEM;
  562. goto securam_fail;
  563. }
  564. pm_bu->suspended = 0;
  565. pm_bu->canary = __pa_symbol(&canary);
  566. pm_bu->resume = __pa_symbol(cpu_resume);
  567. return 0;
  568. securam_fail:
  569. put_device(&pdev->dev);
  570. securam_fail_no_ref_dev:
  571. iounmap(soc_pm.data.sfrbu);
  572. soc_pm.data.sfrbu = NULL;
  573. return ret;
  574. }
  575. static void __init at91_pm_use_default_mode(int pm_mode)
  576. {
  577. if (pm_mode != AT91_PM_ULP1 && pm_mode != AT91_PM_BACKUP)
  578. return;
  579. if (soc_pm.data.standby_mode == pm_mode)
  580. soc_pm.data.standby_mode = AT91_PM_ULP0;
  581. if (soc_pm.data.suspend_mode == pm_mode)
  582. soc_pm.data.suspend_mode = AT91_PM_ULP0;
  583. }
  584. static const struct of_device_id atmel_shdwc_ids[] = {
  585. { .compatible = "atmel,sama5d2-shdwc" },
  586. { .compatible = "microchip,sam9x60-shdwc" },
  587. { /* sentinel. */ }
  588. };
  589. static void __init at91_pm_modes_init(void)
  590. {
  591. struct device_node *np;
  592. int ret;
  593. if (!at91_is_pm_mode_active(AT91_PM_BACKUP) &&
  594. !at91_is_pm_mode_active(AT91_PM_ULP1))
  595. return;
  596. np = of_find_matching_node(NULL, atmel_shdwc_ids);
  597. if (!np) {
  598. pr_warn("%s: failed to find shdwc!\n", __func__);
  599. goto ulp1_default;
  600. }
  601. soc_pm.data.shdwc = of_iomap(np, 0);
  602. of_node_put(np);
  603. ret = at91_pm_backup_init();
  604. if (ret) {
  605. if (!at91_is_pm_mode_active(AT91_PM_ULP1))
  606. goto unmap;
  607. else
  608. goto backup_default;
  609. }
  610. return;
  611. unmap:
  612. iounmap(soc_pm.data.shdwc);
  613. soc_pm.data.shdwc = NULL;
  614. ulp1_default:
  615. at91_pm_use_default_mode(AT91_PM_ULP1);
  616. backup_default:
  617. at91_pm_use_default_mode(AT91_PM_BACKUP);
  618. }
  619. struct pmc_info {
  620. unsigned long uhp_udp_mask;
  621. unsigned long mckr;
  622. unsigned long version;
  623. };
  624. static const struct pmc_info pmc_infos[] __initconst = {
  625. {
  626. .uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP,
  627. .mckr = 0x30,
  628. .version = AT91_PMC_V1,
  629. },
  630. {
  631. .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
  632. .mckr = 0x30,
  633. .version = AT91_PMC_V1,
  634. },
  635. {
  636. .uhp_udp_mask = AT91SAM926x_PMC_UHP,
  637. .mckr = 0x30,
  638. .version = AT91_PMC_V1,
  639. },
  640. { .uhp_udp_mask = 0,
  641. .mckr = 0x30,
  642. .version = AT91_PMC_V1,
  643. },
  644. {
  645. .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
  646. .mckr = 0x28,
  647. .version = AT91_PMC_V2,
  648. },
  649. };
  650. static const struct of_device_id atmel_pmc_ids[] __initconst = {
  651. { .compatible = "atmel,at91rm9200-pmc", .data = &pmc_infos[0] },
  652. { .compatible = "atmel,at91sam9260-pmc", .data = &pmc_infos[1] },
  653. { .compatible = "atmel,at91sam9261-pmc", .data = &pmc_infos[1] },
  654. { .compatible = "atmel,at91sam9263-pmc", .data = &pmc_infos[1] },
  655. { .compatible = "atmel,at91sam9g45-pmc", .data = &pmc_infos[2] },
  656. { .compatible = "atmel,at91sam9n12-pmc", .data = &pmc_infos[1] },
  657. { .compatible = "atmel,at91sam9rl-pmc", .data = &pmc_infos[3] },
  658. { .compatible = "atmel,at91sam9x5-pmc", .data = &pmc_infos[1] },
  659. { .compatible = "atmel,sama5d3-pmc", .data = &pmc_infos[1] },
  660. { .compatible = "atmel,sama5d4-pmc", .data = &pmc_infos[1] },
  661. { .compatible = "atmel,sama5d2-pmc", .data = &pmc_infos[1] },
  662. { .compatible = "microchip,sam9x60-pmc", .data = &pmc_infos[4] },
  663. { /* sentinel */ },
  664. };
  665. static void __init at91_pm_modes_validate(const int *modes, int len)
  666. {
  667. u8 i, standby = 0, suspend = 0;
  668. int mode;
  669. for (i = 0; i < len; i++) {
  670. if (standby && suspend)
  671. break;
  672. if (modes[i] == soc_pm.data.standby_mode && !standby) {
  673. standby = 1;
  674. continue;
  675. }
  676. if (modes[i] == soc_pm.data.suspend_mode && !suspend) {
  677. suspend = 1;
  678. continue;
  679. }
  680. }
  681. if (!standby) {
  682. if (soc_pm.data.suspend_mode == AT91_PM_STANDBY)
  683. mode = AT91_PM_ULP0;
  684. else
  685. mode = AT91_PM_STANDBY;
  686. pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
  687. pm_modes[soc_pm.data.standby_mode].pattern,
  688. pm_modes[mode].pattern);
  689. soc_pm.data.standby_mode = mode;
  690. }
  691. if (!suspend) {
  692. if (soc_pm.data.standby_mode == AT91_PM_ULP0)
  693. mode = AT91_PM_STANDBY;
  694. else
  695. mode = AT91_PM_ULP0;
  696. pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
  697. pm_modes[soc_pm.data.suspend_mode].pattern,
  698. pm_modes[mode].pattern);
  699. soc_pm.data.suspend_mode = mode;
  700. }
  701. }
  702. static void __init at91_pm_init(void (*pm_idle)(void))
  703. {
  704. struct device_node *pmc_np;
  705. const struct of_device_id *of_id;
  706. const struct pmc_info *pmc;
  707. if (at91_cpuidle_device.dev.platform_data)
  708. platform_device_register(&at91_cpuidle_device);
  709. pmc_np = of_find_matching_node_and_match(NULL, atmel_pmc_ids, &of_id);
  710. soc_pm.data.pmc = of_iomap(pmc_np, 0);
  711. of_node_put(pmc_np);
  712. if (!soc_pm.data.pmc) {
  713. pr_err("AT91: PM not supported, PMC not found\n");
  714. return;
  715. }
  716. pmc = of_id->data;
  717. soc_pm.data.uhp_udp_mask = pmc->uhp_udp_mask;
  718. soc_pm.data.pmc_mckr_offset = pmc->mckr;
  719. soc_pm.data.pmc_version = pmc->version;
  720. if (pm_idle)
  721. arm_pm_idle = pm_idle;
  722. at91_pm_sram_init();
  723. if (at91_suspend_sram_fn) {
  724. suspend_set_ops(&at91_pm_ops);
  725. pr_info("AT91: PM: standby: %s, suspend: %s\n",
  726. pm_modes[soc_pm.data.standby_mode].pattern,
  727. pm_modes[soc_pm.data.suspend_mode].pattern);
  728. } else {
  729. pr_info("AT91: PM not supported, due to no SRAM allocated\n");
  730. }
  731. }
  732. void __init at91rm9200_pm_init(void)
  733. {
  734. int ret;
  735. if (!IS_ENABLED(CONFIG_SOC_AT91RM9200))
  736. return;
  737. /*
  738. * Force STANDBY and ULP0 mode to avoid calling
  739. * at91_pm_modes_validate() which may increase booting time.
  740. * Platform supports anyway only STANDBY and ULP0 modes.
  741. */
  742. soc_pm.data.standby_mode = AT91_PM_STANDBY;
  743. soc_pm.data.suspend_mode = AT91_PM_ULP0;
  744. ret = at91_dt_ramc();
  745. if (ret)
  746. return;
  747. /*
  748. * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
  749. */
  750. at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
  751. at91_pm_init(at91rm9200_idle);
  752. }
  753. void __init sam9x60_pm_init(void)
  754. {
  755. static const int modes[] __initconst = {
  756. AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
  757. };
  758. int ret;
  759. if (!IS_ENABLED(CONFIG_SOC_SAM9X60))
  760. return;
  761. at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
  762. at91_pm_modes_init();
  763. ret = at91_dt_ramc();
  764. if (ret)
  765. return;
  766. at91_pm_init(NULL);
  767. soc_pm.ws_ids = sam9x60_ws_ids;
  768. soc_pm.config_pmc_ws = at91_sam9x60_config_pmc_ws;
  769. }
  770. void __init at91sam9_pm_init(void)
  771. {
  772. int ret;
  773. if (!IS_ENABLED(CONFIG_SOC_AT91SAM9))
  774. return;
  775. /*
  776. * Force STANDBY and ULP0 mode to avoid calling
  777. * at91_pm_modes_validate() which may increase booting time.
  778. * Platform supports anyway only STANDBY and ULP0 modes.
  779. */
  780. soc_pm.data.standby_mode = AT91_PM_STANDBY;
  781. soc_pm.data.suspend_mode = AT91_PM_ULP0;
  782. ret = at91_dt_ramc();
  783. if (ret)
  784. return;
  785. at91_pm_init(at91sam9_idle);
  786. }
  787. void __init sama5_pm_init(void)
  788. {
  789. static const int modes[] __initconst = {
  790. AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST,
  791. };
  792. int ret;
  793. if (!IS_ENABLED(CONFIG_SOC_SAMA5))
  794. return;
  795. at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
  796. ret = at91_dt_ramc();
  797. if (ret)
  798. return;
  799. at91_pm_init(NULL);
  800. }
  801. void __init sama5d2_pm_init(void)
  802. {
  803. static const int modes[] __initconst = {
  804. AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
  805. AT91_PM_BACKUP,
  806. };
  807. int ret;
  808. if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
  809. return;
  810. at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
  811. at91_pm_modes_init();
  812. ret = at91_dt_ramc();
  813. if (ret)
  814. return;
  815. at91_pm_init(NULL);
  816. soc_pm.ws_ids = sama5d2_ws_ids;
  817. soc_pm.config_shdwc_ws = at91_sama5d2_config_shdwc_ws;
  818. soc_pm.config_pmc_ws = at91_sama5d2_config_pmc_ws;
  819. }
  820. static int __init at91_pm_modes_select(char *str)
  821. {
  822. char *s;
  823. substring_t args[MAX_OPT_ARGS];
  824. int standby, suspend;
  825. if (!str)
  826. return 0;
  827. s = strsep(&str, ",");
  828. standby = match_token(s, pm_modes, args);
  829. if (standby < 0)
  830. return 0;
  831. suspend = match_token(str, pm_modes, args);
  832. if (suspend < 0)
  833. return 0;
  834. soc_pm.data.standby_mode = standby;
  835. soc_pm.data.suspend_mode = suspend;
  836. return 0;
  837. }
  838. early_param("atmel.pm_modes", at91_pm_modes_select);