pm.c 19 KB

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