1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * arch/arm/mach-at91/pm.c
4 * AT91 Power Management
6 * Copyright (C) 2005 David Brownell
9 #include <linux/genalloc.h>
11 #include <linux/of_address.h>
13 #include <linux/of_fdt.h>
14 #include <linux/of_platform.h>
15 #include <linux/parser.h>
16 #include <linux/suspend.h>
18 #include <linux/clk/at91_pmc.h>
19 #include <linux/platform_data/atmel.h>
21 #include <soc/at91/pm.h>
23 #include <asm/cacheflush.h>
24 #include <asm/fncpy.h>
25 #include <asm/system_misc.h>
26 #include <asm/suspend.h>
31 #define BACKUP_DDR_PHY_CALIBRATION (9)
34 * struct at91_pm_bu - AT91 power management backup unit data structure
35 * @suspended: true if suspended to backup mode
37 * @canary: canary data for memory checking after exit from backup mode
39 * @ddr_phy_calibration: DDR PHY calibration data: ZQ0CR0, first 8 words
44 unsigned long reserved;
47 unsigned long ddr_phy_calibration[BACKUP_DDR_PHY_CALIBRATION];
51 * struct at91_pm_sfrbu_offsets: registers mapping for SFRBU
52 * @pswbu: power switch BU control registers
54 struct at91_pm_sfrbu_regs {
64 * struct at91_soc_pm - AT91 SoC power management data structure
65 * @config_shdwc_ws: wakeup sources configuration function for SHDWC
66 * @config_pmc_ws: wakeup srouces configuration function for PMC
67 * @ws_ids: wakup sources of_device_id array
68 * @data: PM data to be used on last phase of suspend
69 * @sfrbu_regs: SFRBU registers mapping
70 * @bu: backup unit mapped data (for backup mode)
71 * @memcs: memory chip select
74 int (*config_shdwc_ws)(void __iomem *shdwc, u32 *mode, u32 *polarity);
75 int (*config_pmc_ws)(void __iomem *pmc, u32 mode, u32 polarity);
76 const struct of_device_id *ws_ids;
77 struct at91_pm_bu *bu;
78 struct at91_pm_data data;
79 struct at91_pm_sfrbu_regs sfrbu_regs;
84 * enum at91_pm_iomaps: IOs that needs to be mapped for different PM modes
85 * @AT91_PM_IOMAP_SHDWC: SHDWC controller
86 * @AT91_PM_IOMAP_SFRBU: SFRBU controller
93 #define AT91_PM_IOMAP(name) BIT(AT91_PM_IOMAP_##name)
95 static struct at91_soc_pm soc_pm = {
97 .standby_mode = AT91_PM_STANDBY,
98 .suspend_mode = AT91_PM_ULP0,
102 static const match_table_t pm_modes __initconst = {
103 { AT91_PM_STANDBY, "standby" },
104 { AT91_PM_ULP0, "ulp0" },
105 { AT91_PM_ULP0_FAST, "ulp0-fast" },
106 { AT91_PM_ULP1, "ulp1" },
107 { AT91_PM_BACKUP, "backup" },
111 #define at91_ramc_read(id, field) \
112 __raw_readl(soc_pm.data.ramc[id] + field)
114 #define at91_ramc_write(id, field, value) \
115 __raw_writel(value, soc_pm.data.ramc[id] + field)
117 static int at91_pm_valid_state(suspend_state_t state)
121 case PM_SUSPEND_STANDBY:
130 static int canary = 0xA5A5A5A5;
132 struct wakeup_source_info {
133 unsigned int pmc_fsmr_bit;
134 unsigned int shdwc_mr_bit;
138 static const struct wakeup_source_info ws_info[] = {
139 { .pmc_fsmr_bit = AT91_PMC_FSTT(10), .set_polarity = true },
140 { .pmc_fsmr_bit = AT91_PMC_RTCAL, .shdwc_mr_bit = BIT(17) },
141 { .pmc_fsmr_bit = AT91_PMC_USBAL },
142 { .pmc_fsmr_bit = AT91_PMC_SDMMC_CD },
143 { .pmc_fsmr_bit = AT91_PMC_RTTAL },
144 { .pmc_fsmr_bit = AT91_PMC_RXLP_MCE },
147 static const struct of_device_id sama5d2_ws_ids[] = {
148 { .compatible = "atmel,sama5d2-gem", .data = &ws_info[0] },
149 { .compatible = "atmel,at91rm9200-rtc", .data = &ws_info[1] },
150 { .compatible = "atmel,sama5d3-udc", .data = &ws_info[2] },
151 { .compatible = "atmel,at91rm9200-ohci", .data = &ws_info[2] },
152 { .compatible = "usb-ohci", .data = &ws_info[2] },
153 { .compatible = "atmel,at91sam9g45-ehci", .data = &ws_info[2] },
154 { .compatible = "usb-ehci", .data = &ws_info[2] },
155 { .compatible = "atmel,sama5d2-sdhci", .data = &ws_info[3] },
159 static const struct of_device_id sam9x60_ws_ids[] = {
160 { .compatible = "atmel,at91sam9x5-rtc", .data = &ws_info[1] },
161 { .compatible = "atmel,at91rm9200-ohci", .data = &ws_info[2] },
162 { .compatible = "usb-ohci", .data = &ws_info[2] },
163 { .compatible = "atmel,at91sam9g45-ehci", .data = &ws_info[2] },
164 { .compatible = "usb-ehci", .data = &ws_info[2] },
165 { .compatible = "atmel,at91sam9260-rtt", .data = &ws_info[4] },
166 { .compatible = "cdns,sam9x60-macb", .data = &ws_info[5] },
170 static const struct of_device_id sama7g5_ws_ids[] = {
171 { .compatible = "atmel,at91sam9x5-rtc", .data = &ws_info[1] },
172 { .compatible = "microchip,sama7g5-ohci", .data = &ws_info[2] },
173 { .compatible = "usb-ohci", .data = &ws_info[2] },
174 { .compatible = "atmel,at91sam9g45-ehci", .data = &ws_info[2] },
175 { .compatible = "usb-ehci", .data = &ws_info[2] },
176 { .compatible = "microchip,sama7g5-sdhci", .data = &ws_info[3] },
177 { .compatible = "atmel,at91sam9260-rtt", .data = &ws_info[4] },
181 static int at91_pm_config_ws(unsigned int pm_mode, bool set)
183 const struct wakeup_source_info *wsi;
184 const struct of_device_id *match;
185 struct platform_device *pdev;
186 struct device_node *np;
187 unsigned int mode = 0, polarity = 0, val = 0;
189 if (pm_mode != AT91_PM_ULP1)
192 if (!soc_pm.data.pmc || !soc_pm.data.shdwc || !soc_pm.ws_ids)
196 writel(mode, soc_pm.data.pmc + AT91_PMC_FSMR);
200 if (soc_pm.config_shdwc_ws)
201 soc_pm.config_shdwc_ws(soc_pm.data.shdwc, &mode, &polarity);
204 val = readl(soc_pm.data.shdwc + 0x04);
206 /* Loop through defined wakeup sources. */
207 for_each_matching_node_and_match(np, soc_pm.ws_ids, &match) {
208 pdev = of_find_device_by_node(np);
212 if (device_may_wakeup(&pdev->dev)) {
215 /* Check if enabled on SHDWC. */
216 if (wsi->shdwc_mr_bit && !(val & wsi->shdwc_mr_bit))
219 mode |= wsi->pmc_fsmr_bit;
220 if (wsi->set_polarity)
221 polarity |= wsi->pmc_fsmr_bit;
225 put_device(&pdev->dev);
229 if (soc_pm.config_pmc_ws)
230 soc_pm.config_pmc_ws(soc_pm.data.pmc, mode, polarity);
232 pr_err("AT91: PM: no ULP1 wakeup sources found!");
235 return mode ? 0 : -EPERM;
238 static int at91_sama5d2_config_shdwc_ws(void __iomem *shdwc, u32 *mode,
244 val = readl(shdwc + 0x0c);
245 *mode |= (val & 0x3ff);
246 *polarity |= ((val >> 16) & 0x3ff);
251 static int at91_sama5d2_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
253 writel(mode, pmc + AT91_PMC_FSMR);
254 writel(polarity, pmc + AT91_PMC_FSPR);
259 static int at91_sam9x60_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
261 writel(mode, pmc + AT91_PMC_FSMR);
267 * Called after processes are frozen, but before we shutdown devices.
269 static int at91_pm_begin(suspend_state_t state)
275 soc_pm.data.mode = soc_pm.data.suspend_mode;
278 case PM_SUSPEND_STANDBY:
279 soc_pm.data.mode = soc_pm.data.standby_mode;
283 soc_pm.data.mode = -1;
286 ret = at91_pm_config_ws(soc_pm.data.mode, true);
290 if (soc_pm.data.mode == AT91_PM_BACKUP)
291 soc_pm.bu->suspended = 1;
293 soc_pm.bu->suspended = 0;
299 * Verify that all the clocks are correct before entering
302 static int at91_pm_verify_clocks(void)
307 scsr = readl(soc_pm.data.pmc + AT91_PMC_SCSR);
309 /* USB must not be using PLLB */
310 if ((scsr & soc_pm.data.uhp_udp_mask) != 0) {
311 pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
315 /* PCK0..PCK3 must be disabled, or configured to use clk32k */
316 for (i = 0; i < 4; i++) {
319 if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
321 css = readl(soc_pm.data.pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
322 if (css != AT91_PMC_CSS_SLOW) {
323 pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
332 * Call this from platform driver suspend() to see how deeply to suspend.
333 * For example, some controllers (like OHCI) need one of the PLL clocks
334 * in order to act as a wakeup source, and those are not available when
335 * going into slow clock mode.
337 * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
338 * the very same problem (but not using at91 main_clk), and it'd be better
339 * to add one generic API rather than lots of platform-specific ones.
341 int at91_suspend_entering_slow_clock(void)
343 return (soc_pm.data.mode >= AT91_PM_ULP0);
345 EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
347 static void (*at91_suspend_sram_fn)(struct at91_pm_data *);
348 extern void at91_pm_suspend_in_sram(struct at91_pm_data *pm_data);
349 extern u32 at91_pm_suspend_in_sram_sz;
351 static int at91_suspend_finish(unsigned long val)
355 if (soc_pm.data.mode == AT91_PM_BACKUP && soc_pm.data.ramc_phy) {
357 * The 1st 8 words of memory might get corrupted in the process
358 * of DDR PHY recalibration; it is saved here in securam and it
359 * will be restored later, after recalibration, by bootloader
361 for (i = 1; i < BACKUP_DDR_PHY_CALIBRATION; i++)
362 soc_pm.bu->ddr_phy_calibration[i] =
363 *((unsigned int *)soc_pm.memcs + (i - 1));
369 at91_suspend_sram_fn(&soc_pm.data);
374 static void at91_pm_switch_ba_to_vbat(void)
376 unsigned int offset = offsetof(struct at91_pm_sfrbu_regs, pswbu);
379 /* Just for safety. */
380 if (!soc_pm.data.sfrbu)
383 val = readl(soc_pm.data.sfrbu + offset);
385 /* Already on VBAT. */
386 if (!(val & soc_pm.sfrbu_regs.pswbu.state))
389 val &= ~soc_pm.sfrbu_regs.pswbu.softsw;
390 val |= soc_pm.sfrbu_regs.pswbu.key | soc_pm.sfrbu_regs.pswbu.ctrl;
391 writel(val, soc_pm.data.sfrbu + offset);
393 /* Wait for update. */
394 val = readl(soc_pm.data.sfrbu + offset);
395 while (val & soc_pm.sfrbu_regs.pswbu.state)
396 val = readl(soc_pm.data.sfrbu + offset);
399 static void at91_pm_suspend(suspend_state_t state)
401 if (soc_pm.data.mode == AT91_PM_BACKUP) {
402 at91_pm_switch_ba_to_vbat();
404 cpu_suspend(0, at91_suspend_finish);
406 /* The SRAM is lost between suspend cycles */
407 at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
408 &at91_pm_suspend_in_sram,
409 at91_pm_suspend_in_sram_sz);
411 at91_suspend_finish(0);
418 * STANDBY mode has *all* drivers suspended; ignores irqs not marked as 'wakeup'
419 * event sources; and reduces DRAM power. But otherwise it's identical to
420 * PM_SUSPEND_ON: cpu idle, and nothing fancy done with main or cpu clocks.
422 * AT91_PM_ULP0 is like STANDBY plus slow clock mode, so drivers must
423 * suspend more deeply, the master clock switches to the clk32k and turns off
424 * the main oscillator
426 * AT91_PM_BACKUP turns off the whole SoC after placing the DDR in self refresh
428 static int at91_pm_enter(suspend_state_t state)
430 #ifdef CONFIG_PINCTRL_AT91
432 * FIXME: this is needed to communicate between the pinctrl driver and
433 * the PM implementation in the machine. Possibly part of the PM
434 * implementation should be moved down into the pinctrl driver and get
435 * called as part of the generic suspend/resume path.
437 at91_pinctrl_gpio_suspend();
442 case PM_SUSPEND_STANDBY:
444 * Ensure that clocks are in a valid state.
446 if (soc_pm.data.mode >= AT91_PM_ULP0 &&
447 !at91_pm_verify_clocks())
450 at91_pm_suspend(state);
459 pr_debug("AT91: PM - bogus suspend state %d\n", state);
464 #ifdef CONFIG_PINCTRL_AT91
465 at91_pinctrl_gpio_resume();
471 * Called right prior to thawing processes.
473 static void at91_pm_end(void)
475 at91_pm_config_ws(soc_pm.data.mode, false);
479 static const struct platform_suspend_ops at91_pm_ops = {
480 .valid = at91_pm_valid_state,
481 .begin = at91_pm_begin,
482 .enter = at91_pm_enter,
486 static struct platform_device at91_cpuidle_device = {
487 .name = "cpuidle-at91",
491 * The AT91RM9200 goes into self-refresh mode with this command, and will
492 * terminate self-refresh automatically on the next SDRAM access.
494 * Self-refresh mode is exited as soon as a memory access is made, but we don't
495 * know for sure when that happens. However, we need to restore the low-power
496 * mode if it was enabled before going idle. Restoring low-power mode while
497 * still in self-refresh is "not recommended", but seems to work.
499 static void at91rm9200_standby(void)
504 "1: mcr p15, 0, %0, c7, c10, 4\n\t"
505 " str %2, [%1, %3]\n\t"
506 " mcr p15, 0, %0, c7, c0, 4\n\t"
508 : "r" (0), "r" (soc_pm.data.ramc[0]),
509 "r" (1), "r" (AT91_MC_SDRAMC_SRR));
512 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
515 static void at91_ddr_standby(void)
517 /* Those two values allow us to delay self-refresh activation
520 u32 mdr, saved_mdr0, saved_mdr1 = 0;
521 u32 saved_lpr0, saved_lpr1 = 0;
523 /* LPDDR1 --> force DDR2 mode during self-refresh */
524 saved_mdr0 = at91_ramc_read(0, AT91_DDRSDRC_MDR);
525 if ((saved_mdr0 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
526 mdr = saved_mdr0 & ~AT91_DDRSDRC_MD;
527 mdr |= AT91_DDRSDRC_MD_DDR2;
528 at91_ramc_write(0, AT91_DDRSDRC_MDR, mdr);
531 if (soc_pm.data.ramc[1]) {
532 saved_lpr1 = at91_ramc_read(1, AT91_DDRSDRC_LPR);
533 lpr1 = saved_lpr1 & ~AT91_DDRSDRC_LPCB;
534 lpr1 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
535 saved_mdr1 = at91_ramc_read(1, AT91_DDRSDRC_MDR);
536 if ((saved_mdr1 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
537 mdr = saved_mdr1 & ~AT91_DDRSDRC_MD;
538 mdr |= AT91_DDRSDRC_MD_DDR2;
539 at91_ramc_write(1, AT91_DDRSDRC_MDR, mdr);
543 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
544 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
545 lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
547 /* self-refresh mode now */
548 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
549 if (soc_pm.data.ramc[1])
550 at91_ramc_write(1, AT91_DDRSDRC_LPR, lpr1);
554 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr0);
555 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
556 if (soc_pm.data.ramc[1]) {
557 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr1);
558 at91_ramc_write(1, AT91_DDRSDRC_LPR, saved_lpr1);
562 static void sama5d3_ddr_standby(void)
567 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
568 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
569 lpr0 |= AT91_DDRSDRC_LPCB_POWER_DOWN;
571 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
575 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
578 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
581 static void at91sam9_sdram_standby(void)
584 u32 saved_lpr0, saved_lpr1 = 0;
586 if (soc_pm.data.ramc[1]) {
587 saved_lpr1 = at91_ramc_read(1, AT91_SDRAMC_LPR);
588 lpr1 = saved_lpr1 & ~AT91_SDRAMC_LPCB;
589 lpr1 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
592 saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
593 lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
594 lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
596 /* self-refresh mode now */
597 at91_ramc_write(0, AT91_SDRAMC_LPR, lpr0);
598 if (soc_pm.data.ramc[1])
599 at91_ramc_write(1, AT91_SDRAMC_LPR, lpr1);
603 at91_ramc_write(0, AT91_SDRAMC_LPR, saved_lpr0);
604 if (soc_pm.data.ramc[1])
605 at91_ramc_write(1, AT91_SDRAMC_LPR, saved_lpr1);
608 static void sama7g5_standby(void)
612 pwrtmg = readl(soc_pm.data.ramc[0] + UDDRC_PWRCTL);
613 ratio = readl(soc_pm.data.pmc + AT91_PMC_RATIO);
616 * Place RAM into self-refresh after a maximum idle clocks. The maximum
617 * idle clocks is configured by bootloader in
618 * UDDRC_PWRMGT.SELFREF_TO_X32.
620 writel(pwrtmg | UDDRC_PWRCTL_SELFREF_EN,
621 soc_pm.data.ramc[0] + UDDRC_PWRCTL);
622 /* Divide CPU clock by 16. */
623 writel(ratio & ~AT91_PMC_RATIO_RATIO, soc_pm.data.pmc + AT91_PMC_RATIO);
627 /* Restore previous configuration. */
628 writel(ratio, soc_pm.data.pmc + AT91_PMC_RATIO);
629 writel(pwrtmg, soc_pm.data.ramc[0] + UDDRC_PWRCTL);
634 unsigned int memctrl;
637 static const struct ramc_info ramc_infos[] __initconst = {
638 { .idle = at91rm9200_standby, .memctrl = AT91_MEMCTRL_MC},
639 { .idle = at91sam9_sdram_standby, .memctrl = AT91_MEMCTRL_SDRAMC},
640 { .idle = at91_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
641 { .idle = sama5d3_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
642 { .idle = sama7g5_standby, },
645 static const struct of_device_id ramc_ids[] __initconst = {
646 { .compatible = "atmel,at91rm9200-sdramc", .data = &ramc_infos[0] },
647 { .compatible = "atmel,at91sam9260-sdramc", .data = &ramc_infos[1] },
648 { .compatible = "atmel,at91sam9g45-ddramc", .data = &ramc_infos[2] },
649 { .compatible = "atmel,sama5d3-ddramc", .data = &ramc_infos[3] },
650 { .compatible = "microchip,sama7g5-uddrc", .data = &ramc_infos[4], },
654 static const struct of_device_id ramc_phy_ids[] __initconst = {
655 { .compatible = "microchip,sama7g5-ddr3phy", },
659 static __init int at91_dt_ramc(bool phy_mandatory)
661 struct device_node *np;
662 const struct of_device_id *of_id;
664 void *standby = NULL;
665 const struct ramc_info *ramc;
668 for_each_matching_node_and_match(np, ramc_ids, &of_id) {
669 soc_pm.data.ramc[idx] = of_iomap(np, 0);
670 if (!soc_pm.data.ramc[idx]) {
671 pr_err("unable to map ramc[%d] cpu registers\n", idx);
680 standby = ramc->idle;
681 soc_pm.data.memctrl = ramc->memctrl;
688 pr_err("unable to find compatible ram controller node in dtb\n");
693 /* Lookup for DDR PHY node, if any. */
694 for_each_matching_node_and_match(np, ramc_phy_ids, &of_id) {
695 soc_pm.data.ramc_phy = of_iomap(np, 0);
696 if (!soc_pm.data.ramc_phy) {
697 pr_err("unable to map ramc phy cpu registers\n");
704 if (phy_mandatory && !soc_pm.data.ramc_phy) {
705 pr_err("DDR PHY is mandatory!\n");
711 pr_warn("ramc no standby function available\n");
715 at91_cpuidle_device.dev.platform_data = standby;
721 iounmap(soc_pm.data.ramc[--idx]);
726 static void at91rm9200_idle(void)
729 * Disable the processor clock. The processor will be automatically
730 * re-enabled by an interrupt or by a reset.
732 writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
735 static void at91sam9_idle(void)
737 writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
741 static void __init at91_pm_sram_init(void)
743 struct gen_pool *sram_pool;
744 phys_addr_t sram_pbase;
745 unsigned long sram_base;
746 struct device_node *node;
747 struct platform_device *pdev = NULL;
749 for_each_compatible_node(node, NULL, "mmio-sram") {
750 pdev = of_find_device_by_node(node);
758 pr_warn("%s: failed to find sram device!\n", __func__);
762 sram_pool = gen_pool_get(&pdev->dev, NULL);
764 pr_warn("%s: sram pool unavailable!\n", __func__);
768 sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
770 pr_warn("%s: unable to alloc sram!\n", __func__);
774 sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
775 at91_suspend_sram_fn = __arm_ioremap_exec(sram_pbase,
776 at91_pm_suspend_in_sram_sz, false);
777 if (!at91_suspend_sram_fn) {
778 pr_warn("SRAM: Could not map\n");
782 /* Copy the pm suspend handler to SRAM */
783 at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
784 &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
788 put_device(&pdev->dev);
792 static bool __init at91_is_pm_mode_active(int pm_mode)
794 return (soc_pm.data.standby_mode == pm_mode ||
795 soc_pm.data.suspend_mode == pm_mode);
798 static int __init at91_pm_backup_scan_memcs(unsigned long node,
799 const char *uname, int depth,
807 /* Memory node already located. */
811 type = of_get_flat_dt_prop(node, "device_type", NULL);
813 /* We are scanning "memory" nodes only. */
814 if (!type || strcmp(type, "memory"))
817 reg = of_get_flat_dt_prop(node, "reg", &size);
819 soc_pm.memcs = __va((phys_addr_t)be32_to_cpu(*reg));
826 static int __init at91_pm_backup_init(void)
828 struct gen_pool *sram_pool;
829 struct device_node *np;
830 struct platform_device *pdev;
831 int ret = -ENODEV, located = 0;
833 if (!IS_ENABLED(CONFIG_SOC_SAMA5D2) &&
834 !IS_ENABLED(CONFIG_SOC_SAMA7G5))
837 if (!at91_is_pm_mode_active(AT91_PM_BACKUP))
840 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
844 pdev = of_find_device_by_node(np);
847 pr_warn("%s: failed to find securam device!\n", __func__);
851 sram_pool = gen_pool_get(&pdev->dev, NULL);
853 pr_warn("%s: securam pool unavailable!\n", __func__);
857 soc_pm.bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
859 pr_warn("%s: unable to alloc securam!\n", __func__);
864 soc_pm.bu->suspended = 0;
865 soc_pm.bu->canary = __pa_symbol(&canary);
866 soc_pm.bu->resume = __pa_symbol(cpu_resume);
867 if (soc_pm.data.ramc_phy) {
868 of_scan_flat_dt(at91_pm_backup_scan_memcs, &located);
873 soc_pm.bu->ddr_phy_calibration[0] = readl(soc_pm.data.ramc_phy +
880 put_device(&pdev->dev);
884 static const struct of_device_id atmel_shdwc_ids[] = {
885 { .compatible = "atmel,sama5d2-shdwc" },
886 { .compatible = "microchip,sam9x60-shdwc" },
887 { .compatible = "microchip,sama7g5-shdwc" },
891 static void __init at91_pm_modes_init(const u32 *maps, int len)
893 struct device_node *np;
896 ret = at91_pm_backup_init();
898 if (soc_pm.data.standby_mode == AT91_PM_BACKUP)
899 soc_pm.data.standby_mode = AT91_PM_ULP0;
900 if (soc_pm.data.suspend_mode == AT91_PM_BACKUP)
901 soc_pm.data.suspend_mode = AT91_PM_ULP0;
904 if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SHDWC) ||
905 maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SHDWC)) {
906 np = of_find_matching_node(NULL, atmel_shdwc_ids);
908 pr_warn("%s: failed to find shdwc!\n", __func__);
910 /* Use ULP0 if it doesn't needs SHDWC.*/
911 if (!(maps[AT91_PM_ULP0] & AT91_PM_IOMAP(SHDWC)))
914 mode = AT91_PM_STANDBY;
916 if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SHDWC))
917 soc_pm.data.standby_mode = mode;
918 if (maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SHDWC))
919 soc_pm.data.suspend_mode = mode;
921 soc_pm.data.shdwc = of_iomap(np, 0);
926 if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SFRBU) ||
927 maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SFRBU)) {
928 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-sfrbu");
930 pr_warn("%s: failed to find sfrbu!\n", __func__);
933 * Use ULP0 if it doesn't need SHDWC or if SHDWC
934 * was already located.
936 if (!(maps[AT91_PM_ULP0] & AT91_PM_IOMAP(SHDWC)) ||
940 mode = AT91_PM_STANDBY;
942 if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SFRBU))
943 soc_pm.data.standby_mode = mode;
944 if (maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SFRBU))
945 soc_pm.data.suspend_mode = mode;
947 soc_pm.data.sfrbu = of_iomap(np, 0);
952 /* Unmap all unnecessary. */
953 if (soc_pm.data.shdwc &&
954 !(maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SHDWC) ||
955 maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SHDWC))) {
956 iounmap(soc_pm.data.shdwc);
957 soc_pm.data.shdwc = NULL;
960 if (soc_pm.data.sfrbu &&
961 !(maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SFRBU) ||
962 maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SFRBU))) {
963 iounmap(soc_pm.data.sfrbu);
964 soc_pm.data.sfrbu = NULL;
971 unsigned long uhp_udp_mask;
973 unsigned long version;
976 static const struct pmc_info pmc_infos[] __initconst = {
978 .uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP,
980 .version = AT91_PMC_V1,
984 .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
986 .version = AT91_PMC_V1,
989 .uhp_udp_mask = AT91SAM926x_PMC_UHP,
991 .version = AT91_PMC_V1,
995 .version = AT91_PMC_V1,
998 .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
1000 .version = AT91_PMC_V2,
1004 .version = AT91_PMC_V2,
1009 static const struct of_device_id atmel_pmc_ids[] __initconst = {
1010 { .compatible = "atmel,at91rm9200-pmc", .data = &pmc_infos[0] },
1011 { .compatible = "atmel,at91sam9260-pmc", .data = &pmc_infos[1] },
1012 { .compatible = "atmel,at91sam9261-pmc", .data = &pmc_infos[1] },
1013 { .compatible = "atmel,at91sam9263-pmc", .data = &pmc_infos[1] },
1014 { .compatible = "atmel,at91sam9g45-pmc", .data = &pmc_infos[2] },
1015 { .compatible = "atmel,at91sam9n12-pmc", .data = &pmc_infos[1] },
1016 { .compatible = "atmel,at91sam9rl-pmc", .data = &pmc_infos[3] },
1017 { .compatible = "atmel,at91sam9x5-pmc", .data = &pmc_infos[1] },
1018 { .compatible = "atmel,sama5d3-pmc", .data = &pmc_infos[1] },
1019 { .compatible = "atmel,sama5d4-pmc", .data = &pmc_infos[1] },
1020 { .compatible = "atmel,sama5d2-pmc", .data = &pmc_infos[1] },
1021 { .compatible = "microchip,sam9x60-pmc", .data = &pmc_infos[4] },
1022 { .compatible = "microchip,sama7g5-pmc", .data = &pmc_infos[5] },
1026 static void __init at91_pm_modes_validate(const int *modes, int len)
1028 u8 i, standby = 0, suspend = 0;
1031 for (i = 0; i < len; i++) {
1032 if (standby && suspend)
1035 if (modes[i] == soc_pm.data.standby_mode && !standby) {
1040 if (modes[i] == soc_pm.data.suspend_mode && !suspend) {
1047 if (soc_pm.data.suspend_mode == AT91_PM_STANDBY)
1048 mode = AT91_PM_ULP0;
1050 mode = AT91_PM_STANDBY;
1052 pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
1053 pm_modes[soc_pm.data.standby_mode].pattern,
1054 pm_modes[mode].pattern);
1055 soc_pm.data.standby_mode = mode;
1059 if (soc_pm.data.standby_mode == AT91_PM_ULP0)
1060 mode = AT91_PM_STANDBY;
1062 mode = AT91_PM_ULP0;
1064 pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
1065 pm_modes[soc_pm.data.suspend_mode].pattern,
1066 pm_modes[mode].pattern);
1067 soc_pm.data.suspend_mode = mode;
1071 static void __init at91_pm_init(void (*pm_idle)(void))
1073 struct device_node *pmc_np;
1074 const struct of_device_id *of_id;
1075 const struct pmc_info *pmc;
1077 if (at91_cpuidle_device.dev.platform_data)
1078 platform_device_register(&at91_cpuidle_device);
1080 pmc_np = of_find_matching_node_and_match(NULL, atmel_pmc_ids, &of_id);
1081 soc_pm.data.pmc = of_iomap(pmc_np, 0);
1082 of_node_put(pmc_np);
1083 if (!soc_pm.data.pmc) {
1084 pr_err("AT91: PM not supported, PMC not found\n");
1089 soc_pm.data.uhp_udp_mask = pmc->uhp_udp_mask;
1090 soc_pm.data.pmc_mckr_offset = pmc->mckr;
1091 soc_pm.data.pmc_version = pmc->version;
1094 arm_pm_idle = pm_idle;
1096 at91_pm_sram_init();
1098 if (at91_suspend_sram_fn) {
1099 suspend_set_ops(&at91_pm_ops);
1100 pr_info("AT91: PM: standby: %s, suspend: %s\n",
1101 pm_modes[soc_pm.data.standby_mode].pattern,
1102 pm_modes[soc_pm.data.suspend_mode].pattern);
1104 pr_info("AT91: PM not supported, due to no SRAM allocated\n");
1108 void __init at91rm9200_pm_init(void)
1112 if (!IS_ENABLED(CONFIG_SOC_AT91RM9200))
1116 * Force STANDBY and ULP0 mode to avoid calling
1117 * at91_pm_modes_validate() which may increase booting time.
1118 * Platform supports anyway only STANDBY and ULP0 modes.
1120 soc_pm.data.standby_mode = AT91_PM_STANDBY;
1121 soc_pm.data.suspend_mode = AT91_PM_ULP0;
1123 ret = at91_dt_ramc(false);
1128 * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
1130 at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
1132 at91_pm_init(at91rm9200_idle);
1135 void __init sam9x60_pm_init(void)
1137 static const int modes[] __initconst = {
1138 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
1140 static const int iomaps[] __initconst = {
1141 [AT91_PM_ULP1] = AT91_PM_IOMAP(SHDWC),
1145 if (!IS_ENABLED(CONFIG_SOC_SAM9X60))
1148 at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1149 at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1150 ret = at91_dt_ramc(false);
1156 soc_pm.ws_ids = sam9x60_ws_ids;
1157 soc_pm.config_pmc_ws = at91_sam9x60_config_pmc_ws;
1160 void __init at91sam9_pm_init(void)
1164 if (!IS_ENABLED(CONFIG_SOC_AT91SAM9))
1168 * Force STANDBY and ULP0 mode to avoid calling
1169 * at91_pm_modes_validate() which may increase booting time.
1170 * Platform supports anyway only STANDBY and ULP0 modes.
1172 soc_pm.data.standby_mode = AT91_PM_STANDBY;
1173 soc_pm.data.suspend_mode = AT91_PM_ULP0;
1175 ret = at91_dt_ramc(false);
1179 at91_pm_init(at91sam9_idle);
1182 void __init sama5_pm_init(void)
1184 static const int modes[] __initconst = {
1185 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST,
1189 if (!IS_ENABLED(CONFIG_SOC_SAMA5))
1192 at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1193 ret = at91_dt_ramc(false);
1200 void __init sama5d2_pm_init(void)
1202 static const int modes[] __initconst = {
1203 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
1206 static const u32 iomaps[] __initconst = {
1207 [AT91_PM_ULP1] = AT91_PM_IOMAP(SHDWC),
1208 [AT91_PM_BACKUP] = AT91_PM_IOMAP(SHDWC) |
1209 AT91_PM_IOMAP(SFRBU),
1213 if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
1216 at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1217 at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1218 ret = at91_dt_ramc(false);
1224 soc_pm.ws_ids = sama5d2_ws_ids;
1225 soc_pm.config_shdwc_ws = at91_sama5d2_config_shdwc_ws;
1226 soc_pm.config_pmc_ws = at91_sama5d2_config_pmc_ws;
1228 soc_pm.sfrbu_regs.pswbu.key = (0x4BD20C << 8);
1229 soc_pm.sfrbu_regs.pswbu.ctrl = BIT(0);
1230 soc_pm.sfrbu_regs.pswbu.softsw = BIT(1);
1231 soc_pm.sfrbu_regs.pswbu.state = BIT(3);
1234 void __init sama7_pm_init(void)
1236 static const int modes[] __initconst = {
1237 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP1, AT91_PM_BACKUP,
1239 static const u32 iomaps[] __initconst = {
1240 [AT91_PM_ULP0] = AT91_PM_IOMAP(SFRBU),
1241 [AT91_PM_ULP1] = AT91_PM_IOMAP(SFRBU) |
1242 AT91_PM_IOMAP(SHDWC),
1243 [AT91_PM_BACKUP] = AT91_PM_IOMAP(SFRBU) |
1244 AT91_PM_IOMAP(SHDWC),
1248 if (!IS_ENABLED(CONFIG_SOC_SAMA7))
1251 at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1253 ret = at91_dt_ramc(true);
1257 at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1260 soc_pm.ws_ids = sama7g5_ws_ids;
1261 soc_pm.config_pmc_ws = at91_sam9x60_config_pmc_ws;
1263 soc_pm.sfrbu_regs.pswbu.key = (0x4BD20C << 8);
1264 soc_pm.sfrbu_regs.pswbu.ctrl = BIT(0);
1265 soc_pm.sfrbu_regs.pswbu.softsw = BIT(1);
1266 soc_pm.sfrbu_regs.pswbu.state = BIT(2);
1269 static int __init at91_pm_modes_select(char *str)
1272 substring_t args[MAX_OPT_ARGS];
1273 int standby, suspend;
1278 s = strsep(&str, ",");
1279 standby = match_token(s, pm_modes, args);
1283 suspend = match_token(str, pm_modes, args);
1287 soc_pm.data.standby_mode = standby;
1288 soc_pm.data.suspend_mode = suspend;
1292 early_param("atmel.pm_modes", at91_pm_modes_select);