Linux Audio

Check our new training course

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