xref: /openbmc/linux/arch/arm/mach-at91/pm.c (revision 228662b0)
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 
9 #include <linux/genalloc.h>
10 #include <linux/io.h>
11 #include <linux/of_address.h>
12 #include <linux/of.h>
13 #include <linux/of_fdt.h>
14 #include <linux/of_platform.h>
15 #include <linux/parser.h>
16 #include <linux/suspend.h>
17 
18 #include <linux/clk/at91_pmc.h>
19 #include <linux/platform_data/atmel.h>
20 
21 #include <soc/at91/pm.h>
22 
23 #include <asm/cacheflush.h>
24 #include <asm/fncpy.h>
25 #include <asm/system_misc.h>
26 #include <asm/suspend.h>
27 
28 #include "generic.h"
29 #include "pm.h"
30 
31 #define BACKUP_DDR_PHY_CALIBRATION	(9)
32 
33 /**
34  * struct at91_pm_bu - AT91 power management backup unit data structure
35  * @suspended: true if suspended to backup mode
36  * @reserved: reserved
37  * @canary: canary data for memory checking after exit from backup mode
38  * @resume: resume API
39  * @ddr_phy_calibration: DDR PHY calibration data: ZQ0CR0, first 8 words
40  * of the memory
41  */
42 struct at91_pm_bu {
43 	int suspended;
44 	unsigned long reserved;
45 	phys_addr_t canary;
46 	phys_addr_t resume;
47 	unsigned long ddr_phy_calibration[BACKUP_DDR_PHY_CALIBRATION];
48 };
49 
50 /*
51  * struct at91_pm_sfrbu_offsets: registers mapping for SFRBU
52  * @pswbu: power switch BU control registers
53  */
54 struct at91_pm_sfrbu_regs {
55 	struct {
56 		u32 key;
57 		u32 ctrl;
58 		u32 state;
59 		u32 softsw;
60 	} pswbu;
61 };
62 
63 /**
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
72  */
73 struct at91_soc_pm {
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;
80 	void *memcs;
81 };
82 
83 /**
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
87  */
88 enum at91_pm_iomaps {
89 	AT91_PM_IOMAP_SHDWC,
90 	AT91_PM_IOMAP_SFRBU,
91 };
92 
93 #define AT91_PM_IOMAP(name)	BIT(AT91_PM_IOMAP_##name)
94 
95 static struct at91_soc_pm soc_pm = {
96 	.data = {
97 		.standby_mode = AT91_PM_STANDBY,
98 		.suspend_mode = AT91_PM_ULP0,
99 	},
100 };
101 
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" },
108 	{ -1, NULL },
109 };
110 
111 #define at91_ramc_read(id, field) \
112 	__raw_readl(soc_pm.data.ramc[id] + field)
113 
114 #define at91_ramc_write(id, field, value) \
115 	__raw_writel(value, soc_pm.data.ramc[id] + field)
116 
117 static int at91_pm_valid_state(suspend_state_t state)
118 {
119 	switch (state) {
120 		case PM_SUSPEND_ON:
121 		case PM_SUSPEND_STANDBY:
122 		case PM_SUSPEND_MEM:
123 			return 1;
124 
125 		default:
126 			return 0;
127 	}
128 }
129 
130 static int canary = 0xA5A5A5A5;
131 
132 struct wakeup_source_info {
133 	unsigned int pmc_fsmr_bit;
134 	unsigned int shdwc_mr_bit;
135 	bool set_polarity;
136 };
137 
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 },
145 };
146 
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] },
156 	{ /* sentinel */ }
157 };
158 
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] },
167 	{ /* sentinel */ }
168 };
169 
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] },
178 	{ /* sentinel */ }
179 };
180 
181 static int at91_pm_config_ws(unsigned int pm_mode, bool set)
182 {
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;
188 
189 	if (pm_mode != AT91_PM_ULP1)
190 		return 0;
191 
192 	if (!soc_pm.data.pmc || !soc_pm.data.shdwc || !soc_pm.ws_ids)
193 		return -EPERM;
194 
195 	if (!set) {
196 		writel(mode, soc_pm.data.pmc + AT91_PMC_FSMR);
197 		return 0;
198 	}
199 
200 	if (soc_pm.config_shdwc_ws)
201 		soc_pm.config_shdwc_ws(soc_pm.data.shdwc, &mode, &polarity);
202 
203 	/* SHDWC.MR */
204 	val = readl(soc_pm.data.shdwc + 0x04);
205 
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);
209 		if (!pdev)
210 			continue;
211 
212 		if (device_may_wakeup(&pdev->dev)) {
213 			wsi = match->data;
214 
215 			/* Check if enabled on SHDWC. */
216 			if (wsi->shdwc_mr_bit && !(val & wsi->shdwc_mr_bit))
217 				goto put_device;
218 
219 			mode |= wsi->pmc_fsmr_bit;
220 			if (wsi->set_polarity)
221 				polarity |= wsi->pmc_fsmr_bit;
222 		}
223 
224 put_device:
225 		put_device(&pdev->dev);
226 	}
227 
228 	if (mode) {
229 		if (soc_pm.config_pmc_ws)
230 			soc_pm.config_pmc_ws(soc_pm.data.pmc, mode, polarity);
231 	} else {
232 		pr_err("AT91: PM: no ULP1 wakeup sources found!");
233 	}
234 
235 	return mode ? 0 : -EPERM;
236 }
237 
238 static int at91_sama5d2_config_shdwc_ws(void __iomem *shdwc, u32 *mode,
239 					u32 *polarity)
240 {
241 	u32 val;
242 
243 	/* SHDWC.WUIR */
244 	val = readl(shdwc + 0x0c);
245 	*mode |= (val & 0x3ff);
246 	*polarity |= ((val >> 16) & 0x3ff);
247 
248 	return 0;
249 }
250 
251 static int at91_sama5d2_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
252 {
253 	writel(mode, pmc + AT91_PMC_FSMR);
254 	writel(polarity, pmc + AT91_PMC_FSPR);
255 
256 	return 0;
257 }
258 
259 static int at91_sam9x60_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
260 {
261 	writel(mode, pmc + AT91_PMC_FSMR);
262 
263 	return 0;
264 }
265 
266 /*
267  * Called after processes are frozen, but before we shutdown devices.
268  */
269 static int at91_pm_begin(suspend_state_t state)
270 {
271 	int ret;
272 
273 	switch (state) {
274 	case PM_SUSPEND_MEM:
275 		soc_pm.data.mode = soc_pm.data.suspend_mode;
276 		break;
277 
278 	case PM_SUSPEND_STANDBY:
279 		soc_pm.data.mode = soc_pm.data.standby_mode;
280 		break;
281 
282 	default:
283 		soc_pm.data.mode = -1;
284 	}
285 
286 	ret = at91_pm_config_ws(soc_pm.data.mode, true);
287 	if (ret)
288 		return ret;
289 
290 	if (soc_pm.data.mode == AT91_PM_BACKUP)
291 		soc_pm.bu->suspended = 1;
292 	else if (soc_pm.bu)
293 		soc_pm.bu->suspended = 0;
294 
295 	return 0;
296 }
297 
298 /*
299  * Verify that all the clocks are correct before entering
300  * slow-clock mode.
301  */
302 static int at91_pm_verify_clocks(void)
303 {
304 	unsigned long scsr;
305 	int i;
306 
307 	scsr = readl(soc_pm.data.pmc + AT91_PMC_SCSR);
308 
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");
312 		return 0;
313 	}
314 
315 	/* PCK0..PCK3 must be disabled, or configured to use clk32k */
316 	for (i = 0; i < 4; i++) {
317 		u32 css;
318 
319 		if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
320 			continue;
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);
324 			return 0;
325 		}
326 	}
327 
328 	return 1;
329 }
330 
331 /*
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.
336  *
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.
340  */
341 int at91_suspend_entering_slow_clock(void)
342 {
343 	return (soc_pm.data.mode >= AT91_PM_ULP0);
344 }
345 EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
346 
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;
350 
351 static int at91_suspend_finish(unsigned long val)
352 {
353 	int i;
354 
355 	if (soc_pm.data.mode == AT91_PM_BACKUP && soc_pm.data.ramc_phy) {
356 		/*
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
360 		 */
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));
364 	}
365 
366 	flush_cache_all();
367 	outer_disable();
368 
369 	at91_suspend_sram_fn(&soc_pm.data);
370 
371 	return 0;
372 }
373 
374 static void at91_pm_switch_ba_to_vbat(void)
375 {
376 	unsigned int offset = offsetof(struct at91_pm_sfrbu_regs, pswbu);
377 	unsigned int val;
378 
379 	/* Just for safety. */
380 	if (!soc_pm.data.sfrbu)
381 		return;
382 
383 	val = readl(soc_pm.data.sfrbu + offset);
384 
385 	/* Already on VBAT. */
386 	if (!(val & soc_pm.sfrbu_regs.pswbu.state))
387 		return;
388 
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);
392 
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);
397 }
398 
399 static void at91_pm_suspend(suspend_state_t state)
400 {
401 	if (soc_pm.data.mode == AT91_PM_BACKUP) {
402 		at91_pm_switch_ba_to_vbat();
403 
404 		cpu_suspend(0, at91_suspend_finish);
405 
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);
410 	} else {
411 		at91_suspend_finish(0);
412 	}
413 
414 	outer_resume();
415 }
416 
417 /*
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.
421  *
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
425  *
426  * AT91_PM_BACKUP turns off the whole SoC after placing the DDR in self refresh
427  */
428 static int at91_pm_enter(suspend_state_t state)
429 {
430 #ifdef CONFIG_PINCTRL_AT91
431 	/*
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.
436 	 */
437 	at91_pinctrl_gpio_suspend();
438 #endif
439 
440 	switch (state) {
441 	case PM_SUSPEND_MEM:
442 	case PM_SUSPEND_STANDBY:
443 		/*
444 		 * Ensure that clocks are in a valid state.
445 		 */
446 		if (soc_pm.data.mode >= AT91_PM_ULP0 &&
447 		    !at91_pm_verify_clocks())
448 			goto error;
449 
450 		at91_pm_suspend(state);
451 
452 		break;
453 
454 	case PM_SUSPEND_ON:
455 		cpu_do_idle();
456 		break;
457 
458 	default:
459 		pr_debug("AT91: PM - bogus suspend state %d\n", state);
460 		goto error;
461 	}
462 
463 error:
464 #ifdef CONFIG_PINCTRL_AT91
465 	at91_pinctrl_gpio_resume();
466 #endif
467 	return 0;
468 }
469 
470 /*
471  * Called right prior to thawing processes.
472  */
473 static void at91_pm_end(void)
474 {
475 	at91_pm_config_ws(soc_pm.data.mode, false);
476 }
477 
478 
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,
483 	.end	= at91_pm_end,
484 };
485 
486 static struct platform_device at91_cpuidle_device = {
487 	.name = "cpuidle-at91",
488 };
489 
490 /*
491  * The AT91RM9200 goes into self-refresh mode with this command, and will
492  * terminate self-refresh automatically on the next SDRAM access.
493  *
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.
498  */
499 static void at91rm9200_standby(void)
500 {
501 	asm volatile(
502 		"b    1f\n\t"
503 		".align    5\n\t"
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"
507 		:
508 		: "r" (0), "r" (soc_pm.data.ramc[0]),
509 		  "r" (1), "r" (AT91_MC_SDRAMC_SRR));
510 }
511 
512 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
513  * remember.
514  */
515 static void at91_ddr_standby(void)
516 {
517 	/* Those two values allow us to delay self-refresh activation
518 	 * to the maximum. */
519 	u32 lpr0, lpr1 = 0;
520 	u32 mdr, saved_mdr0, saved_mdr1 = 0;
521 	u32 saved_lpr0, saved_lpr1 = 0;
522 
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);
529 	}
530 
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);
540 		}
541 	}
542 
543 	saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
544 	lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
545 	lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
546 
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);
551 
552 	cpu_do_idle();
553 
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);
559 	}
560 }
561 
562 static void sama5d3_ddr_standby(void)
563 {
564 	u32 lpr0;
565 	u32 saved_lpr0;
566 
567 	saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
568 	lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
569 	lpr0 |= AT91_DDRSDRC_LPCB_POWER_DOWN;
570 
571 	at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
572 
573 	cpu_do_idle();
574 
575 	at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
576 }
577 
578 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
579  * remember.
580  */
581 static void at91sam9_sdram_standby(void)
582 {
583 	u32 lpr0, lpr1 = 0;
584 	u32 saved_lpr0, saved_lpr1 = 0;
585 
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;
590 	}
591 
592 	saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
593 	lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
594 	lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
595 
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);
600 
601 	cpu_do_idle();
602 
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);
606 }
607 
608 static void sama7g5_standby(void)
609 {
610 	int pwrtmg, ratio;
611 
612 	pwrtmg = readl(soc_pm.data.ramc[0] + UDDRC_PWRCTL);
613 	ratio = readl(soc_pm.data.pmc + AT91_PMC_RATIO);
614 
615 	/*
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.
619 	 */
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);
624 
625 	cpu_do_idle();
626 
627 	/* Restore previous configuration. */
628 	writel(ratio, soc_pm.data.pmc + AT91_PMC_RATIO);
629 	writel(pwrtmg, soc_pm.data.ramc[0] + UDDRC_PWRCTL);
630 }
631 
632 struct ramc_info {
633 	void (*idle)(void);
634 	unsigned int memctrl;
635 };
636 
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, },
643 };
644 
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], },
651 	{ /*sentinel*/ }
652 };
653 
654 static const struct of_device_id ramc_phy_ids[] __initconst = {
655 	{ .compatible = "microchip,sama7g5-ddr3phy", },
656 	{ /* Sentinel. */ },
657 };
658 
659 static __init int at91_dt_ramc(bool phy_mandatory)
660 {
661 	struct device_node *np;
662 	const struct of_device_id *of_id;
663 	int idx = 0;
664 	void *standby = NULL;
665 	const struct ramc_info *ramc;
666 	int ret;
667 
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);
672 			ret = -ENOMEM;
673 			of_node_put(np);
674 			goto unmap_ramc;
675 		}
676 
677 		ramc = of_id->data;
678 		if (ramc) {
679 			if (!standby)
680 				standby = ramc->idle;
681 			soc_pm.data.memctrl = ramc->memctrl;
682 		}
683 
684 		idx++;
685 	}
686 
687 	if (!idx) {
688 		pr_err("unable to find compatible ram controller node in dtb\n");
689 		ret = -ENODEV;
690 		goto unmap_ramc;
691 	}
692 
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");
698 			ret = -ENOMEM;
699 			of_node_put(np);
700 			goto unmap_ramc;
701 		}
702 	}
703 
704 	if (phy_mandatory && !soc_pm.data.ramc_phy) {
705 		pr_err("DDR PHY is mandatory!\n");
706 		ret = -ENODEV;
707 		goto unmap_ramc;
708 	}
709 
710 	if (!standby) {
711 		pr_warn("ramc no standby function available\n");
712 		return 0;
713 	}
714 
715 	at91_cpuidle_device.dev.platform_data = standby;
716 
717 	return 0;
718 
719 unmap_ramc:
720 	while (idx)
721 		iounmap(soc_pm.data.ramc[--idx]);
722 
723 	return ret;
724 }
725 
726 static void at91rm9200_idle(void)
727 {
728 	/*
729 	 * Disable the processor clock.  The processor will be automatically
730 	 * re-enabled by an interrupt or by a reset.
731 	 */
732 	writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
733 }
734 
735 static void at91sam9_idle(void)
736 {
737 	writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
738 	cpu_do_idle();
739 }
740 
741 static void __init at91_pm_sram_init(void)
742 {
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;
748 
749 	for_each_compatible_node(node, NULL, "mmio-sram") {
750 		pdev = of_find_device_by_node(node);
751 		if (pdev) {
752 			of_node_put(node);
753 			break;
754 		}
755 	}
756 
757 	if (!pdev) {
758 		pr_warn("%s: failed to find sram device!\n", __func__);
759 		return;
760 	}
761 
762 	sram_pool = gen_pool_get(&pdev->dev, NULL);
763 	if (!sram_pool) {
764 		pr_warn("%s: sram pool unavailable!\n", __func__);
765 		goto out_put_device;
766 	}
767 
768 	sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
769 	if (!sram_base) {
770 		pr_warn("%s: unable to alloc sram!\n", __func__);
771 		goto out_put_device;
772 	}
773 
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");
779 		goto out_put_device;
780 	}
781 
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);
785 	return;
786 
787 out_put_device:
788 	put_device(&pdev->dev);
789 	return;
790 }
791 
792 static bool __init at91_is_pm_mode_active(int pm_mode)
793 {
794 	return (soc_pm.data.standby_mode == pm_mode ||
795 		soc_pm.data.suspend_mode == pm_mode);
796 }
797 
798 static int __init at91_pm_backup_scan_memcs(unsigned long node,
799 					    const char *uname, int depth,
800 					    void *data)
801 {
802 	const char *type;
803 	const __be32 *reg;
804 	int *located = data;
805 	int size;
806 
807 	/* Memory node already located. */
808 	if (*located)
809 		return 0;
810 
811 	type = of_get_flat_dt_prop(node, "device_type", NULL);
812 
813 	/* We are scanning "memory" nodes only. */
814 	if (!type || strcmp(type, "memory"))
815 		return 0;
816 
817 	reg = of_get_flat_dt_prop(node, "reg", &size);
818 	if (reg) {
819 		soc_pm.memcs = __va((phys_addr_t)be32_to_cpu(*reg));
820 		*located = 1;
821 	}
822 
823 	return 0;
824 }
825 
826 static int __init at91_pm_backup_init(void)
827 {
828 	struct gen_pool *sram_pool;
829 	struct device_node *np;
830 	struct platform_device *pdev;
831 	int ret = -ENODEV, located = 0;
832 
833 	if (!IS_ENABLED(CONFIG_SOC_SAMA5D2) &&
834 	    !IS_ENABLED(CONFIG_SOC_SAMA7G5))
835 		return -EPERM;
836 
837 	if (!at91_is_pm_mode_active(AT91_PM_BACKUP))
838 		return 0;
839 
840 	np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
841 	if (!np)
842 		return ret;
843 
844 	pdev = of_find_device_by_node(np);
845 	of_node_put(np);
846 	if (!pdev) {
847 		pr_warn("%s: failed to find securam device!\n", __func__);
848 		return ret;
849 	}
850 
851 	sram_pool = gen_pool_get(&pdev->dev, NULL);
852 	if (!sram_pool) {
853 		pr_warn("%s: securam pool unavailable!\n", __func__);
854 		goto securam_fail;
855 	}
856 
857 	soc_pm.bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
858 	if (!soc_pm.bu) {
859 		pr_warn("%s: unable to alloc securam!\n", __func__);
860 		ret = -ENOMEM;
861 		goto securam_fail;
862 	}
863 
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);
869 		if (!located)
870 			goto securam_fail;
871 
872 		/* DDR3PHY_ZQ0SR0 */
873 		soc_pm.bu->ddr_phy_calibration[0] = readl(soc_pm.data.ramc_phy +
874 							  0x188);
875 	}
876 
877 	return 0;
878 
879 securam_fail:
880 	put_device(&pdev->dev);
881 	return ret;
882 }
883 
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" },
888 	{ /* sentinel. */ }
889 };
890 
891 static void __init at91_pm_modes_init(const u32 *maps, int len)
892 {
893 	struct device_node *np;
894 	int ret, mode;
895 
896 	ret = at91_pm_backup_init();
897 	if (ret) {
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;
902 	}
903 
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);
907 		if (!np) {
908 			pr_warn("%s: failed to find shdwc!\n", __func__);
909 
910 			/* Use ULP0 if it doesn't needs SHDWC.*/
911 			if (!(maps[AT91_PM_ULP0] & AT91_PM_IOMAP(SHDWC)))
912 				mode = AT91_PM_ULP0;
913 			else
914 				mode = AT91_PM_STANDBY;
915 
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;
920 		} else {
921 			soc_pm.data.shdwc = of_iomap(np, 0);
922 			of_node_put(np);
923 		}
924 	}
925 
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");
929 		if (!np) {
930 			pr_warn("%s: failed to find sfrbu!\n", __func__);
931 
932 			/*
933 			 * Use ULP0 if it doesn't need SHDWC or if SHDWC
934 			 * was already located.
935 			 */
936 			if (!(maps[AT91_PM_ULP0] & AT91_PM_IOMAP(SHDWC)) ||
937 			    soc_pm.data.shdwc)
938 				mode = AT91_PM_ULP0;
939 			else
940 				mode = AT91_PM_STANDBY;
941 
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;
946 		} else {
947 			soc_pm.data.sfrbu = of_iomap(np, 0);
948 			of_node_put(np);
949 		}
950 	}
951 
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;
958 	}
959 
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;
965 	}
966 
967 	return;
968 }
969 
970 struct pmc_info {
971 	unsigned long uhp_udp_mask;
972 	unsigned long mckr;
973 	unsigned long version;
974 };
975 
976 static const struct pmc_info pmc_infos[] __initconst = {
977 	{
978 		.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP,
979 		.mckr = 0x30,
980 		.version = AT91_PMC_V1,
981 	},
982 
983 	{
984 		.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
985 		.mckr = 0x30,
986 		.version = AT91_PMC_V1,
987 	},
988 	{
989 		.uhp_udp_mask = AT91SAM926x_PMC_UHP,
990 		.mckr = 0x30,
991 		.version = AT91_PMC_V1,
992 	},
993 	{	.uhp_udp_mask = 0,
994 		.mckr = 0x30,
995 		.version = AT91_PMC_V1,
996 	},
997 	{
998 		.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
999 		.mckr = 0x28,
1000 		.version = AT91_PMC_V2,
1001 	},
1002 	{
1003 		.mckr = 0x28,
1004 		.version = AT91_PMC_V2,
1005 	},
1006 
1007 };
1008 
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] },
1023 	{ /* sentinel */ },
1024 };
1025 
1026 static void __init at91_pm_modes_validate(const int *modes, int len)
1027 {
1028 	u8 i, standby = 0, suspend = 0;
1029 	int mode;
1030 
1031 	for (i = 0; i < len; i++) {
1032 		if (standby && suspend)
1033 			break;
1034 
1035 		if (modes[i] == soc_pm.data.standby_mode && !standby) {
1036 			standby = 1;
1037 			continue;
1038 		}
1039 
1040 		if (modes[i] == soc_pm.data.suspend_mode && !suspend) {
1041 			suspend = 1;
1042 			continue;
1043 		}
1044 	}
1045 
1046 	if (!standby) {
1047 		if (soc_pm.data.suspend_mode == AT91_PM_STANDBY)
1048 			mode = AT91_PM_ULP0;
1049 		else
1050 			mode = AT91_PM_STANDBY;
1051 
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;
1056 	}
1057 
1058 	if (!suspend) {
1059 		if (soc_pm.data.standby_mode == AT91_PM_ULP0)
1060 			mode = AT91_PM_STANDBY;
1061 		else
1062 			mode = AT91_PM_ULP0;
1063 
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;
1068 	}
1069 }
1070 
1071 static void __init at91_pm_init(void (*pm_idle)(void))
1072 {
1073 	struct device_node *pmc_np;
1074 	const struct of_device_id *of_id;
1075 	const struct pmc_info *pmc;
1076 
1077 	if (at91_cpuidle_device.dev.platform_data)
1078 		platform_device_register(&at91_cpuidle_device);
1079 
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");
1085 		return;
1086 	}
1087 
1088 	pmc = of_id->data;
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;
1092 
1093 	if (pm_idle)
1094 		arm_pm_idle = pm_idle;
1095 
1096 	at91_pm_sram_init();
1097 
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);
1103 	} else {
1104 		pr_info("AT91: PM not supported, due to no SRAM allocated\n");
1105 	}
1106 }
1107 
1108 void __init at91rm9200_pm_init(void)
1109 {
1110 	int ret;
1111 
1112 	if (!IS_ENABLED(CONFIG_SOC_AT91RM9200))
1113 		return;
1114 
1115 	/*
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.
1119 	 */
1120 	soc_pm.data.standby_mode = AT91_PM_STANDBY;
1121 	soc_pm.data.suspend_mode = AT91_PM_ULP0;
1122 
1123 	ret = at91_dt_ramc(false);
1124 	if (ret)
1125 		return;
1126 
1127 	/*
1128 	 * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
1129 	 */
1130 	at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
1131 
1132 	at91_pm_init(at91rm9200_idle);
1133 }
1134 
1135 void __init sam9x60_pm_init(void)
1136 {
1137 	static const int modes[] __initconst = {
1138 		AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
1139 	};
1140 	static const int iomaps[] __initconst = {
1141 		[AT91_PM_ULP1]		= AT91_PM_IOMAP(SHDWC),
1142 	};
1143 	int ret;
1144 
1145 	if (!IS_ENABLED(CONFIG_SOC_SAM9X60))
1146 		return;
1147 
1148 	at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1149 	at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1150 	ret = at91_dt_ramc(false);
1151 	if (ret)
1152 		return;
1153 
1154 	at91_pm_init(NULL);
1155 
1156 	soc_pm.ws_ids = sam9x60_ws_ids;
1157 	soc_pm.config_pmc_ws = at91_sam9x60_config_pmc_ws;
1158 }
1159 
1160 void __init at91sam9_pm_init(void)
1161 {
1162 	int ret;
1163 
1164 	if (!IS_ENABLED(CONFIG_SOC_AT91SAM9))
1165 		return;
1166 
1167 	/*
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.
1171 	 */
1172 	soc_pm.data.standby_mode = AT91_PM_STANDBY;
1173 	soc_pm.data.suspend_mode = AT91_PM_ULP0;
1174 
1175 	ret = at91_dt_ramc(false);
1176 	if (ret)
1177 		return;
1178 
1179 	at91_pm_init(at91sam9_idle);
1180 }
1181 
1182 void __init sama5_pm_init(void)
1183 {
1184 	static const int modes[] __initconst = {
1185 		AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST,
1186 	};
1187 	int ret;
1188 
1189 	if (!IS_ENABLED(CONFIG_SOC_SAMA5))
1190 		return;
1191 
1192 	at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1193 	ret = at91_dt_ramc(false);
1194 	if (ret)
1195 		return;
1196 
1197 	at91_pm_init(NULL);
1198 }
1199 
1200 void __init sama5d2_pm_init(void)
1201 {
1202 	static const int modes[] __initconst = {
1203 		AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
1204 		AT91_PM_BACKUP,
1205 	};
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),
1210 	};
1211 	int ret;
1212 
1213 	if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
1214 		return;
1215 
1216 	at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1217 	at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1218 	ret = at91_dt_ramc(false);
1219 	if (ret)
1220 		return;
1221 
1222 	at91_pm_init(NULL);
1223 
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;
1227 
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);
1232 }
1233 
1234 void __init sama7_pm_init(void)
1235 {
1236 	static const int modes[] __initconst = {
1237 		AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP1, AT91_PM_BACKUP,
1238 	};
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),
1245 	};
1246 	int ret;
1247 
1248 	if (!IS_ENABLED(CONFIG_SOC_SAMA7))
1249 		return;
1250 
1251 	at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1252 
1253 	ret = at91_dt_ramc(true);
1254 	if (ret)
1255 		return;
1256 
1257 	at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1258 	at91_pm_init(NULL);
1259 
1260 	soc_pm.ws_ids = sama7g5_ws_ids;
1261 	soc_pm.config_pmc_ws = at91_sam9x60_config_pmc_ws;
1262 
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);
1267 }
1268 
1269 static int __init at91_pm_modes_select(char *str)
1270 {
1271 	char *s;
1272 	substring_t args[MAX_OPT_ARGS];
1273 	int standby, suspend;
1274 
1275 	if (!str)
1276 		return 0;
1277 
1278 	s = strsep(&str, ",");
1279 	standby = match_token(s, pm_modes, args);
1280 	if (standby < 0)
1281 		return 0;
1282 
1283 	suspend = match_token(str, pm_modes, args);
1284 	if (suspend < 0)
1285 		return 0;
1286 
1287 	soc_pm.data.standby_mode = standby;
1288 	soc_pm.data.suspend_mode = suspend;
1289 
1290 	return 0;
1291 }
1292 early_param("atmel.pm_modes", at91_pm_modes_select);
1293