blob: 3f015cb6ec2b0758cc38fcdc7e6c2128b797d68a [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * arch/arm/mach-at91/pm.c
4 * AT91 Power Management
5 *
6 * Copyright (C) 2005 David Brownell
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
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>
David Brazdil0f672f62019-12-10 10:32:29 +000018#include <linux/platform_data/atmel.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019
20#include <asm/cacheflush.h>
21#include <asm/fncpy.h>
22#include <asm/system_misc.h>
23#include <asm/suspend.h>
24
25#include "generic.h"
26#include "pm.h"
27
28/*
29 * FIXME: this is needed to communicate between the pinctrl driver and
30 * the PM implementation in the machine. Possibly part of the PM
31 * implementation should be moved down into the pinctrl driver and get
32 * called as part of the generic suspend/resume path.
33 */
34#ifdef CONFIG_PINCTRL_AT91
35extern void at91_pinctrl_gpio_suspend(void);
36extern void at91_pinctrl_gpio_resume(void);
37#endif
38
David Brazdil0f672f62019-12-10 10:32:29 +000039struct at91_soc_pm {
40 int (*config_shdwc_ws)(void __iomem *shdwc, u32 *mode, u32 *polarity);
41 int (*config_pmc_ws)(void __iomem *pmc, u32 mode, u32 polarity);
42 const struct of_device_id *ws_ids;
43 struct at91_pm_data data;
44};
45
46static struct at91_soc_pm soc_pm = {
47 .data = {
48 .standby_mode = AT91_PM_STANDBY,
49 .suspend_mode = AT91_PM_ULP0,
50 },
51};
52
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053static const match_table_t pm_modes __initconst = {
Olivier Deprez157378f2022-04-04 15:47:50 +020054 { AT91_PM_STANDBY, "standby" },
55 { AT91_PM_ULP0, "ulp0" },
56 { AT91_PM_ULP0_FAST, "ulp0-fast" },
57 { AT91_PM_ULP1, "ulp1" },
58 { AT91_PM_BACKUP, "backup" },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059 { -1, NULL },
60};
61
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062#define at91_ramc_read(id, field) \
David Brazdil0f672f62019-12-10 10:32:29 +000063 __raw_readl(soc_pm.data.ramc[id] + field)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064
65#define at91_ramc_write(id, field, value) \
David Brazdil0f672f62019-12-10 10:32:29 +000066 __raw_writel(value, soc_pm.data.ramc[id] + field)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067
68static int at91_pm_valid_state(suspend_state_t state)
69{
70 switch (state) {
71 case PM_SUSPEND_ON:
72 case PM_SUSPEND_STANDBY:
73 case PM_SUSPEND_MEM:
74 return 1;
75
76 default:
77 return 0;
78 }
79}
80
81static int canary = 0xA5A5A5A5;
82
83static struct at91_pm_bu {
84 int suspended;
85 unsigned long reserved;
86 phys_addr_t canary;
87 phys_addr_t resume;
88} *pm_bu;
89
90struct wakeup_source_info {
91 unsigned int pmc_fsmr_bit;
92 unsigned int shdwc_mr_bit;
93 bool set_polarity;
94};
95
96static const struct wakeup_source_info ws_info[] = {
97 { .pmc_fsmr_bit = AT91_PMC_FSTT(10), .set_polarity = true },
98 { .pmc_fsmr_bit = AT91_PMC_RTCAL, .shdwc_mr_bit = BIT(17) },
99 { .pmc_fsmr_bit = AT91_PMC_USBAL },
100 { .pmc_fsmr_bit = AT91_PMC_SDMMC_CD },
David Brazdil0f672f62019-12-10 10:32:29 +0000101 { .pmc_fsmr_bit = AT91_PMC_RTTAL },
102 { .pmc_fsmr_bit = AT91_PMC_RXLP_MCE },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103};
104
105static const struct of_device_id sama5d2_ws_ids[] = {
106 { .compatible = "atmel,sama5d2-gem", .data = &ws_info[0] },
107 { .compatible = "atmel,at91rm9200-rtc", .data = &ws_info[1] },
108 { .compatible = "atmel,sama5d3-udc", .data = &ws_info[2] },
109 { .compatible = "atmel,at91rm9200-ohci", .data = &ws_info[2] },
110 { .compatible = "usb-ohci", .data = &ws_info[2] },
111 { .compatible = "atmel,at91sam9g45-ehci", .data = &ws_info[2] },
112 { .compatible = "usb-ehci", .data = &ws_info[2] },
113 { .compatible = "atmel,sama5d2-sdhci", .data = &ws_info[3] },
114 { /* sentinel */ }
115};
116
David Brazdil0f672f62019-12-10 10:32:29 +0000117static const struct of_device_id sam9x60_ws_ids[] = {
118 { .compatible = "atmel,at91sam9x5-rtc", .data = &ws_info[1] },
119 { .compatible = "atmel,at91rm9200-ohci", .data = &ws_info[2] },
120 { .compatible = "usb-ohci", .data = &ws_info[2] },
121 { .compatible = "atmel,at91sam9g45-ehci", .data = &ws_info[2] },
122 { .compatible = "usb-ehci", .data = &ws_info[2] },
123 { .compatible = "atmel,at91sam9260-rtt", .data = &ws_info[4] },
124 { .compatible = "cdns,sam9x60-macb", .data = &ws_info[5] },
125 { /* sentinel */ }
126};
127
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128static int at91_pm_config_ws(unsigned int pm_mode, bool set)
129{
130 const struct wakeup_source_info *wsi;
131 const struct of_device_id *match;
132 struct platform_device *pdev;
133 struct device_node *np;
134 unsigned int mode = 0, polarity = 0, val = 0;
135
136 if (pm_mode != AT91_PM_ULP1)
137 return 0;
138
David Brazdil0f672f62019-12-10 10:32:29 +0000139 if (!soc_pm.data.pmc || !soc_pm.data.shdwc || !soc_pm.ws_ids)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140 return -EPERM;
141
142 if (!set) {
David Brazdil0f672f62019-12-10 10:32:29 +0000143 writel(mode, soc_pm.data.pmc + AT91_PMC_FSMR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144 return 0;
145 }
146
David Brazdil0f672f62019-12-10 10:32:29 +0000147 if (soc_pm.config_shdwc_ws)
148 soc_pm.config_shdwc_ws(soc_pm.data.shdwc, &mode, &polarity);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000149
150 /* SHDWC.MR */
David Brazdil0f672f62019-12-10 10:32:29 +0000151 val = readl(soc_pm.data.shdwc + 0x04);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152
153 /* Loop through defined wakeup sources. */
David Brazdil0f672f62019-12-10 10:32:29 +0000154 for_each_matching_node_and_match(np, soc_pm.ws_ids, &match) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000155 pdev = of_find_device_by_node(np);
156 if (!pdev)
157 continue;
158
159 if (device_may_wakeup(&pdev->dev)) {
160 wsi = match->data;
161
162 /* Check if enabled on SHDWC. */
163 if (wsi->shdwc_mr_bit && !(val & wsi->shdwc_mr_bit))
David Brazdil0f672f62019-12-10 10:32:29 +0000164 goto put_device;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165
166 mode |= wsi->pmc_fsmr_bit;
167 if (wsi->set_polarity)
168 polarity |= wsi->pmc_fsmr_bit;
169 }
170
David Brazdil0f672f62019-12-10 10:32:29 +0000171put_device:
172 put_device(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000173 }
174
175 if (mode) {
David Brazdil0f672f62019-12-10 10:32:29 +0000176 if (soc_pm.config_pmc_ws)
177 soc_pm.config_pmc_ws(soc_pm.data.pmc, mode, polarity);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 } else {
179 pr_err("AT91: PM: no ULP1 wakeup sources found!");
180 }
181
182 return mode ? 0 : -EPERM;
183}
184
David Brazdil0f672f62019-12-10 10:32:29 +0000185static int at91_sama5d2_config_shdwc_ws(void __iomem *shdwc, u32 *mode,
186 u32 *polarity)
187{
188 u32 val;
189
190 /* SHDWC.WUIR */
191 val = readl(shdwc + 0x0c);
192 *mode |= (val & 0x3ff);
193 *polarity |= ((val >> 16) & 0x3ff);
194
195 return 0;
196}
197
198static int at91_sama5d2_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
199{
200 writel(mode, pmc + AT91_PMC_FSMR);
201 writel(polarity, pmc + AT91_PMC_FSPR);
202
203 return 0;
204}
205
206static int at91_sam9x60_config_pmc_ws(void __iomem *pmc, u32 mode, u32 polarity)
207{
208 writel(mode, pmc + AT91_PMC_FSMR);
209
210 return 0;
211}
212
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000213/*
214 * Called after processes are frozen, but before we shutdown devices.
215 */
216static int at91_pm_begin(suspend_state_t state)
217{
218 switch (state) {
219 case PM_SUSPEND_MEM:
David Brazdil0f672f62019-12-10 10:32:29 +0000220 soc_pm.data.mode = soc_pm.data.suspend_mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221 break;
222
223 case PM_SUSPEND_STANDBY:
David Brazdil0f672f62019-12-10 10:32:29 +0000224 soc_pm.data.mode = soc_pm.data.standby_mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225 break;
226
227 default:
David Brazdil0f672f62019-12-10 10:32:29 +0000228 soc_pm.data.mode = -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229 }
230
David Brazdil0f672f62019-12-10 10:32:29 +0000231 return at91_pm_config_ws(soc_pm.data.mode, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232}
233
234/*
235 * Verify that all the clocks are correct before entering
236 * slow-clock mode.
237 */
238static int at91_pm_verify_clocks(void)
239{
240 unsigned long scsr;
241 int i;
242
David Brazdil0f672f62019-12-10 10:32:29 +0000243 scsr = readl(soc_pm.data.pmc + AT91_PMC_SCSR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244
245 /* USB must not be using PLLB */
David Brazdil0f672f62019-12-10 10:32:29 +0000246 if ((scsr & soc_pm.data.uhp_udp_mask) != 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
248 return 0;
249 }
250
251 /* PCK0..PCK3 must be disabled, or configured to use clk32k */
252 for (i = 0; i < 4; i++) {
253 u32 css;
254
255 if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
256 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000257 css = readl(soc_pm.data.pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000258 if (css != AT91_PMC_CSS_SLOW) {
259 pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
260 return 0;
261 }
262 }
263
264 return 1;
265}
266
267/*
268 * Call this from platform driver suspend() to see how deeply to suspend.
269 * For example, some controllers (like OHCI) need one of the PLL clocks
270 * in order to act as a wakeup source, and those are not available when
271 * going into slow clock mode.
272 *
273 * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
274 * the very same problem (but not using at91 main_clk), and it'd be better
275 * to add one generic API rather than lots of platform-specific ones.
276 */
277int at91_suspend_entering_slow_clock(void)
278{
David Brazdil0f672f62019-12-10 10:32:29 +0000279 return (soc_pm.data.mode >= AT91_PM_ULP0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280}
281EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
282
283static void (*at91_suspend_sram_fn)(struct at91_pm_data *);
284extern void at91_pm_suspend_in_sram(struct at91_pm_data *pm_data);
285extern u32 at91_pm_suspend_in_sram_sz;
286
287static int at91_suspend_finish(unsigned long val)
288{
289 flush_cache_all();
290 outer_disable();
291
David Brazdil0f672f62019-12-10 10:32:29 +0000292 at91_suspend_sram_fn(&soc_pm.data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000293
294 return 0;
295}
296
297static void at91_pm_suspend(suspend_state_t state)
298{
David Brazdil0f672f62019-12-10 10:32:29 +0000299 if (soc_pm.data.mode == AT91_PM_BACKUP) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000300 pm_bu->suspended = 1;
301
302 cpu_suspend(0, at91_suspend_finish);
303
304 /* The SRAM is lost between suspend cycles */
305 at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
306 &at91_pm_suspend_in_sram,
307 at91_pm_suspend_in_sram_sz);
308 } else {
309 at91_suspend_finish(0);
310 }
311
312 outer_resume();
313}
314
315/*
316 * STANDBY mode has *all* drivers suspended; ignores irqs not marked as 'wakeup'
317 * event sources; and reduces DRAM power. But otherwise it's identical to
318 * PM_SUSPEND_ON: cpu idle, and nothing fancy done with main or cpu clocks.
319 *
320 * AT91_PM_ULP0 is like STANDBY plus slow clock mode, so drivers must
321 * suspend more deeply, the master clock switches to the clk32k and turns off
322 * the main oscillator
323 *
324 * AT91_PM_BACKUP turns off the whole SoC after placing the DDR in self refresh
325 */
326static int at91_pm_enter(suspend_state_t state)
327{
328#ifdef CONFIG_PINCTRL_AT91
329 at91_pinctrl_gpio_suspend();
330#endif
331
332 switch (state) {
333 case PM_SUSPEND_MEM:
334 case PM_SUSPEND_STANDBY:
335 /*
336 * Ensure that clocks are in a valid state.
337 */
David Brazdil0f672f62019-12-10 10:32:29 +0000338 if (soc_pm.data.mode >= AT91_PM_ULP0 &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 !at91_pm_verify_clocks())
340 goto error;
341
342 at91_pm_suspend(state);
343
344 break;
345
346 case PM_SUSPEND_ON:
347 cpu_do_idle();
348 break;
349
350 default:
351 pr_debug("AT91: PM - bogus suspend state %d\n", state);
352 goto error;
353 }
354
355error:
356#ifdef CONFIG_PINCTRL_AT91
357 at91_pinctrl_gpio_resume();
358#endif
359 return 0;
360}
361
362/*
363 * Called right prior to thawing processes.
364 */
365static void at91_pm_end(void)
366{
David Brazdil0f672f62019-12-10 10:32:29 +0000367 at91_pm_config_ws(soc_pm.data.mode, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368}
369
370
371static const struct platform_suspend_ops at91_pm_ops = {
372 .valid = at91_pm_valid_state,
373 .begin = at91_pm_begin,
374 .enter = at91_pm_enter,
375 .end = at91_pm_end,
376};
377
378static struct platform_device at91_cpuidle_device = {
379 .name = "cpuidle-at91",
380};
381
382/*
383 * The AT91RM9200 goes into self-refresh mode with this command, and will
384 * terminate self-refresh automatically on the next SDRAM access.
385 *
386 * Self-refresh mode is exited as soon as a memory access is made, but we don't
387 * know for sure when that happens. However, we need to restore the low-power
388 * mode if it was enabled before going idle. Restoring low-power mode while
389 * still in self-refresh is "not recommended", but seems to work.
390 */
391static void at91rm9200_standby(void)
392{
393 asm volatile(
394 "b 1f\n\t"
395 ".align 5\n\t"
396 "1: mcr p15, 0, %0, c7, c10, 4\n\t"
397 " str %2, [%1, %3]\n\t"
398 " mcr p15, 0, %0, c7, c0, 4\n\t"
399 :
David Brazdil0f672f62019-12-10 10:32:29 +0000400 : "r" (0), "r" (soc_pm.data.ramc[0]),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401 "r" (1), "r" (AT91_MC_SDRAMC_SRR));
402}
403
404/* We manage both DDRAM/SDRAM controllers, we need more than one value to
405 * remember.
406 */
407static void at91_ddr_standby(void)
408{
409 /* Those two values allow us to delay self-refresh activation
410 * to the maximum. */
411 u32 lpr0, lpr1 = 0;
412 u32 mdr, saved_mdr0, saved_mdr1 = 0;
413 u32 saved_lpr0, saved_lpr1 = 0;
414
415 /* LPDDR1 --> force DDR2 mode during self-refresh */
416 saved_mdr0 = at91_ramc_read(0, AT91_DDRSDRC_MDR);
417 if ((saved_mdr0 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
418 mdr = saved_mdr0 & ~AT91_DDRSDRC_MD;
419 mdr |= AT91_DDRSDRC_MD_DDR2;
420 at91_ramc_write(0, AT91_DDRSDRC_MDR, mdr);
421 }
422
David Brazdil0f672f62019-12-10 10:32:29 +0000423 if (soc_pm.data.ramc[1]) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424 saved_lpr1 = at91_ramc_read(1, AT91_DDRSDRC_LPR);
425 lpr1 = saved_lpr1 & ~AT91_DDRSDRC_LPCB;
426 lpr1 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
427 saved_mdr1 = at91_ramc_read(1, AT91_DDRSDRC_MDR);
428 if ((saved_mdr1 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
429 mdr = saved_mdr1 & ~AT91_DDRSDRC_MD;
430 mdr |= AT91_DDRSDRC_MD_DDR2;
431 at91_ramc_write(1, AT91_DDRSDRC_MDR, mdr);
432 }
433 }
434
435 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
436 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
437 lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
438
439 /* self-refresh mode now */
440 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
David Brazdil0f672f62019-12-10 10:32:29 +0000441 if (soc_pm.data.ramc[1])
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 at91_ramc_write(1, AT91_DDRSDRC_LPR, lpr1);
443
444 cpu_do_idle();
445
446 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr0);
447 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
David Brazdil0f672f62019-12-10 10:32:29 +0000448 if (soc_pm.data.ramc[1]) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr1);
450 at91_ramc_write(1, AT91_DDRSDRC_LPR, saved_lpr1);
451 }
452}
453
454static void sama5d3_ddr_standby(void)
455{
456 u32 lpr0;
457 u32 saved_lpr0;
458
459 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
460 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
461 lpr0 |= AT91_DDRSDRC_LPCB_POWER_DOWN;
462
463 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
464
465 cpu_do_idle();
466
467 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
468}
469
470/* We manage both DDRAM/SDRAM controllers, we need more than one value to
471 * remember.
472 */
473static void at91sam9_sdram_standby(void)
474{
475 u32 lpr0, lpr1 = 0;
476 u32 saved_lpr0, saved_lpr1 = 0;
477
David Brazdil0f672f62019-12-10 10:32:29 +0000478 if (soc_pm.data.ramc[1]) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479 saved_lpr1 = at91_ramc_read(1, AT91_SDRAMC_LPR);
480 lpr1 = saved_lpr1 & ~AT91_SDRAMC_LPCB;
481 lpr1 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
482 }
483
484 saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
485 lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
486 lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
487
488 /* self-refresh mode now */
489 at91_ramc_write(0, AT91_SDRAMC_LPR, lpr0);
David Brazdil0f672f62019-12-10 10:32:29 +0000490 if (soc_pm.data.ramc[1])
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 at91_ramc_write(1, AT91_SDRAMC_LPR, lpr1);
492
493 cpu_do_idle();
494
495 at91_ramc_write(0, AT91_SDRAMC_LPR, saved_lpr0);
David Brazdil0f672f62019-12-10 10:32:29 +0000496 if (soc_pm.data.ramc[1])
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497 at91_ramc_write(1, AT91_SDRAMC_LPR, saved_lpr1);
498}
499
500struct ramc_info {
501 void (*idle)(void);
502 unsigned int memctrl;
503};
504
505static const struct ramc_info ramc_infos[] __initconst = {
506 { .idle = at91rm9200_standby, .memctrl = AT91_MEMCTRL_MC},
507 { .idle = at91sam9_sdram_standby, .memctrl = AT91_MEMCTRL_SDRAMC},
508 { .idle = at91_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
509 { .idle = sama5d3_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
510};
511
512static const struct of_device_id ramc_ids[] __initconst = {
513 { .compatible = "atmel,at91rm9200-sdramc", .data = &ramc_infos[0] },
514 { .compatible = "atmel,at91sam9260-sdramc", .data = &ramc_infos[1] },
515 { .compatible = "atmel,at91sam9g45-ddramc", .data = &ramc_infos[2] },
516 { .compatible = "atmel,sama5d3-ddramc", .data = &ramc_infos[3] },
517 { /*sentinel*/ }
518};
519
Olivier Deprez157378f2022-04-04 15:47:50 +0200520static __init int at91_dt_ramc(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000521{
522 struct device_node *np;
523 const struct of_device_id *of_id;
524 int idx = 0;
525 void *standby = NULL;
526 const struct ramc_info *ramc;
Olivier Deprez157378f2022-04-04 15:47:50 +0200527 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528
529 for_each_matching_node_and_match(np, ramc_ids, &of_id) {
David Brazdil0f672f62019-12-10 10:32:29 +0000530 soc_pm.data.ramc[idx] = of_iomap(np, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +0200531 if (!soc_pm.data.ramc[idx]) {
532 pr_err("unable to map ramc[%d] cpu registers\n", idx);
533 ret = -ENOMEM;
534 goto unmap_ramc;
535 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000536
537 ramc = of_id->data;
538 if (!standby)
539 standby = ramc->idle;
David Brazdil0f672f62019-12-10 10:32:29 +0000540 soc_pm.data.memctrl = ramc->memctrl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000541
542 idx++;
543 }
544
Olivier Deprez157378f2022-04-04 15:47:50 +0200545 if (!idx) {
546 pr_err("unable to find compatible ram controller node in dtb\n");
547 ret = -ENODEV;
548 goto unmap_ramc;
549 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000550
551 if (!standby) {
552 pr_warn("ramc no standby function available\n");
Olivier Deprez157378f2022-04-04 15:47:50 +0200553 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000554 }
555
556 at91_cpuidle_device.dev.platform_data = standby;
Olivier Deprez157378f2022-04-04 15:47:50 +0200557
558 return 0;
559
560unmap_ramc:
561 while (idx)
562 iounmap(soc_pm.data.ramc[--idx]);
563
564 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565}
566
567static void at91rm9200_idle(void)
568{
569 /*
570 * Disable the processor clock. The processor will be automatically
571 * re-enabled by an interrupt or by a reset.
572 */
David Brazdil0f672f62019-12-10 10:32:29 +0000573 writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
574}
575
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000576static void at91sam9_idle(void)
577{
David Brazdil0f672f62019-12-10 10:32:29 +0000578 writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579 cpu_do_idle();
580}
581
582static void __init at91_pm_sram_init(void)
583{
584 struct gen_pool *sram_pool;
585 phys_addr_t sram_pbase;
586 unsigned long sram_base;
587 struct device_node *node;
588 struct platform_device *pdev = NULL;
589
590 for_each_compatible_node(node, NULL, "mmio-sram") {
591 pdev = of_find_device_by_node(node);
592 if (pdev) {
593 of_node_put(node);
594 break;
595 }
596 }
597
598 if (!pdev) {
599 pr_warn("%s: failed to find sram device!\n", __func__);
600 return;
601 }
602
603 sram_pool = gen_pool_get(&pdev->dev, NULL);
604 if (!sram_pool) {
605 pr_warn("%s: sram pool unavailable!\n", __func__);
Olivier Deprez0e641232021-09-23 10:07:05 +0200606 goto out_put_device;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000607 }
608
609 sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
610 if (!sram_base) {
611 pr_warn("%s: unable to alloc sram!\n", __func__);
Olivier Deprez0e641232021-09-23 10:07:05 +0200612 goto out_put_device;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000613 }
614
615 sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
616 at91_suspend_sram_fn = __arm_ioremap_exec(sram_pbase,
617 at91_pm_suspend_in_sram_sz, false);
618 if (!at91_suspend_sram_fn) {
619 pr_warn("SRAM: Could not map\n");
Olivier Deprez0e641232021-09-23 10:07:05 +0200620 goto out_put_device;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000621 }
622
623 /* Copy the pm suspend handler to SRAM */
624 at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
625 &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
Olivier Deprez0e641232021-09-23 10:07:05 +0200626 return;
627
628out_put_device:
629 put_device(&pdev->dev);
630 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000631}
632
633static bool __init at91_is_pm_mode_active(int pm_mode)
634{
David Brazdil0f672f62019-12-10 10:32:29 +0000635 return (soc_pm.data.standby_mode == pm_mode ||
636 soc_pm.data.suspend_mode == pm_mode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000637}
638
639static int __init at91_pm_backup_init(void)
640{
641 struct gen_pool *sram_pool;
642 struct device_node *np;
643 struct platform_device *pdev = NULL;
644 int ret = -ENODEV;
645
David Brazdil0f672f62019-12-10 10:32:29 +0000646 if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
647 return -EPERM;
648
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 if (!at91_is_pm_mode_active(AT91_PM_BACKUP))
650 return 0;
651
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000652 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-sfrbu");
653 if (!np) {
654 pr_warn("%s: failed to find sfrbu!\n", __func__);
655 return ret;
656 }
657
David Brazdil0f672f62019-12-10 10:32:29 +0000658 soc_pm.data.sfrbu = of_iomap(np, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000659 of_node_put(np);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000660
661 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
662 if (!np)
David Brazdil0f672f62019-12-10 10:32:29 +0000663 goto securam_fail_no_ref_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000664
665 pdev = of_find_device_by_node(np);
666 of_node_put(np);
667 if (!pdev) {
668 pr_warn("%s: failed to find securam device!\n", __func__);
David Brazdil0f672f62019-12-10 10:32:29 +0000669 goto securam_fail_no_ref_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000670 }
671
672 sram_pool = gen_pool_get(&pdev->dev, NULL);
673 if (!sram_pool) {
674 pr_warn("%s: securam pool unavailable!\n", __func__);
675 goto securam_fail;
676 }
677
678 pm_bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
679 if (!pm_bu) {
680 pr_warn("%s: unable to alloc securam!\n", __func__);
681 ret = -ENOMEM;
682 goto securam_fail;
683 }
684
685 pm_bu->suspended = 0;
686 pm_bu->canary = __pa_symbol(&canary);
687 pm_bu->resume = __pa_symbol(cpu_resume);
688
689 return 0;
690
691securam_fail:
David Brazdil0f672f62019-12-10 10:32:29 +0000692 put_device(&pdev->dev);
693securam_fail_no_ref_dev:
694 iounmap(soc_pm.data.sfrbu);
695 soc_pm.data.sfrbu = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000696 return ret;
697}
698
699static void __init at91_pm_use_default_mode(int pm_mode)
700{
701 if (pm_mode != AT91_PM_ULP1 && pm_mode != AT91_PM_BACKUP)
702 return;
703
David Brazdil0f672f62019-12-10 10:32:29 +0000704 if (soc_pm.data.standby_mode == pm_mode)
705 soc_pm.data.standby_mode = AT91_PM_ULP0;
706 if (soc_pm.data.suspend_mode == pm_mode)
707 soc_pm.data.suspend_mode = AT91_PM_ULP0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708}
709
Olivier Deprez0e641232021-09-23 10:07:05 +0200710static const struct of_device_id atmel_shdwc_ids[] = {
711 { .compatible = "atmel,sama5d2-shdwc" },
712 { .compatible = "microchip,sam9x60-shdwc" },
713 { /* sentinel. */ }
714};
715
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716static void __init at91_pm_modes_init(void)
717{
718 struct device_node *np;
719 int ret;
720
721 if (!at91_is_pm_mode_active(AT91_PM_BACKUP) &&
722 !at91_is_pm_mode_active(AT91_PM_ULP1))
723 return;
724
Olivier Deprez0e641232021-09-23 10:07:05 +0200725 np = of_find_matching_node(NULL, atmel_shdwc_ids);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726 if (!np) {
727 pr_warn("%s: failed to find shdwc!\n", __func__);
728 goto ulp1_default;
729 }
730
David Brazdil0f672f62019-12-10 10:32:29 +0000731 soc_pm.data.shdwc = of_iomap(np, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000732 of_node_put(np);
733
734 ret = at91_pm_backup_init();
735 if (ret) {
736 if (!at91_is_pm_mode_active(AT91_PM_ULP1))
737 goto unmap;
738 else
739 goto backup_default;
740 }
741
742 return;
743
744unmap:
David Brazdil0f672f62019-12-10 10:32:29 +0000745 iounmap(soc_pm.data.shdwc);
746 soc_pm.data.shdwc = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000747ulp1_default:
748 at91_pm_use_default_mode(AT91_PM_ULP1);
749backup_default:
750 at91_pm_use_default_mode(AT91_PM_BACKUP);
751}
752
753struct pmc_info {
754 unsigned long uhp_udp_mask;
Olivier Deprez157378f2022-04-04 15:47:50 +0200755 unsigned long mckr;
756 unsigned long version;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757};
758
759static const struct pmc_info pmc_infos[] __initconst = {
Olivier Deprez157378f2022-04-04 15:47:50 +0200760 {
761 .uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP,
762 .mckr = 0x30,
763 .version = AT91_PMC_V1,
764 },
765
766 {
767 .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
768 .mckr = 0x30,
769 .version = AT91_PMC_V1,
770 },
771 {
772 .uhp_udp_mask = AT91SAM926x_PMC_UHP,
773 .mckr = 0x30,
774 .version = AT91_PMC_V1,
775 },
776 { .uhp_udp_mask = 0,
777 .mckr = 0x30,
778 .version = AT91_PMC_V1,
779 },
780 {
781 .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
782 .mckr = 0x28,
783 .version = AT91_PMC_V2,
784 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000785};
786
787static const struct of_device_id atmel_pmc_ids[] __initconst = {
788 { .compatible = "atmel,at91rm9200-pmc", .data = &pmc_infos[0] },
789 { .compatible = "atmel,at91sam9260-pmc", .data = &pmc_infos[1] },
790 { .compatible = "atmel,at91sam9261-pmc", .data = &pmc_infos[1] },
791 { .compatible = "atmel,at91sam9263-pmc", .data = &pmc_infos[1] },
792 { .compatible = "atmel,at91sam9g45-pmc", .data = &pmc_infos[2] },
793 { .compatible = "atmel,at91sam9n12-pmc", .data = &pmc_infos[1] },
794 { .compatible = "atmel,at91sam9rl-pmc", .data = &pmc_infos[3] },
795 { .compatible = "atmel,at91sam9x5-pmc", .data = &pmc_infos[1] },
796 { .compatible = "atmel,sama5d3-pmc", .data = &pmc_infos[1] },
797 { .compatible = "atmel,sama5d4-pmc", .data = &pmc_infos[1] },
798 { .compatible = "atmel,sama5d2-pmc", .data = &pmc_infos[1] },
Olivier Deprez157378f2022-04-04 15:47:50 +0200799 { .compatible = "microchip,sam9x60-pmc", .data = &pmc_infos[4] },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000800 { /* sentinel */ },
801};
802
Olivier Deprez157378f2022-04-04 15:47:50 +0200803static void __init at91_pm_modes_validate(const int *modes, int len)
804{
805 u8 i, standby = 0, suspend = 0;
806 int mode;
807
808 for (i = 0; i < len; i++) {
809 if (standby && suspend)
810 break;
811
812 if (modes[i] == soc_pm.data.standby_mode && !standby) {
813 standby = 1;
814 continue;
815 }
816
817 if (modes[i] == soc_pm.data.suspend_mode && !suspend) {
818 suspend = 1;
819 continue;
820 }
821 }
822
823 if (!standby) {
824 if (soc_pm.data.suspend_mode == AT91_PM_STANDBY)
825 mode = AT91_PM_ULP0;
826 else
827 mode = AT91_PM_STANDBY;
828
829 pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
830 pm_modes[soc_pm.data.standby_mode].pattern,
831 pm_modes[mode].pattern);
832 soc_pm.data.standby_mode = mode;
833 }
834
835 if (!suspend) {
836 if (soc_pm.data.standby_mode == AT91_PM_ULP0)
837 mode = AT91_PM_STANDBY;
838 else
839 mode = AT91_PM_ULP0;
840
841 pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
842 pm_modes[soc_pm.data.suspend_mode].pattern,
843 pm_modes[mode].pattern);
844 soc_pm.data.suspend_mode = mode;
845 }
846}
847
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000848static void __init at91_pm_init(void (*pm_idle)(void))
849{
850 struct device_node *pmc_np;
851 const struct of_device_id *of_id;
852 const struct pmc_info *pmc;
853
854 if (at91_cpuidle_device.dev.platform_data)
855 platform_device_register(&at91_cpuidle_device);
856
857 pmc_np = of_find_matching_node_and_match(NULL, atmel_pmc_ids, &of_id);
David Brazdil0f672f62019-12-10 10:32:29 +0000858 soc_pm.data.pmc = of_iomap(pmc_np, 0);
Olivier Deprez0e641232021-09-23 10:07:05 +0200859 of_node_put(pmc_np);
David Brazdil0f672f62019-12-10 10:32:29 +0000860 if (!soc_pm.data.pmc) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861 pr_err("AT91: PM not supported, PMC not found\n");
862 return;
863 }
864
865 pmc = of_id->data;
David Brazdil0f672f62019-12-10 10:32:29 +0000866 soc_pm.data.uhp_udp_mask = pmc->uhp_udp_mask;
Olivier Deprez157378f2022-04-04 15:47:50 +0200867 soc_pm.data.pmc_mckr_offset = pmc->mckr;
868 soc_pm.data.pmc_version = pmc->version;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000869
870 if (pm_idle)
871 arm_pm_idle = pm_idle;
872
873 at91_pm_sram_init();
874
875 if (at91_suspend_sram_fn) {
876 suspend_set_ops(&at91_pm_ops);
877 pr_info("AT91: PM: standby: %s, suspend: %s\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000878 pm_modes[soc_pm.data.standby_mode].pattern,
879 pm_modes[soc_pm.data.suspend_mode].pattern);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000880 } else {
881 pr_info("AT91: PM not supported, due to no SRAM allocated\n");
882 }
883}
884
885void __init at91rm9200_pm_init(void)
886{
Olivier Deprez157378f2022-04-04 15:47:50 +0200887 int ret;
888
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000889 if (!IS_ENABLED(CONFIG_SOC_AT91RM9200))
890 return;
891
Olivier Deprez157378f2022-04-04 15:47:50 +0200892 /*
893 * Force STANDBY and ULP0 mode to avoid calling
894 * at91_pm_modes_validate() which may increase booting time.
895 * Platform supports anyway only STANDBY and ULP0 modes.
896 */
897 soc_pm.data.standby_mode = AT91_PM_STANDBY;
898 soc_pm.data.suspend_mode = AT91_PM_ULP0;
899
900 ret = at91_dt_ramc();
901 if (ret)
902 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000903
904 /*
905 * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
906 */
907 at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
908
909 at91_pm_init(at91rm9200_idle);
910}
911
David Brazdil0f672f62019-12-10 10:32:29 +0000912void __init sam9x60_pm_init(void)
913{
Olivier Deprez157378f2022-04-04 15:47:50 +0200914 static const int modes[] __initconst = {
915 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
916 };
917 int ret;
918
919 if (!IS_ENABLED(CONFIG_SOC_SAM9X60))
David Brazdil0f672f62019-12-10 10:32:29 +0000920 return;
921
Olivier Deprez157378f2022-04-04 15:47:50 +0200922 at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
David Brazdil0f672f62019-12-10 10:32:29 +0000923 at91_pm_modes_init();
Olivier Deprez157378f2022-04-04 15:47:50 +0200924 ret = at91_dt_ramc();
925 if (ret)
926 return;
927
928 at91_pm_init(NULL);
David Brazdil0f672f62019-12-10 10:32:29 +0000929
930 soc_pm.ws_ids = sam9x60_ws_ids;
931 soc_pm.config_pmc_ws = at91_sam9x60_config_pmc_ws;
932}
933
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000934void __init at91sam9_pm_init(void)
935{
Olivier Deprez157378f2022-04-04 15:47:50 +0200936 int ret;
937
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000938 if (!IS_ENABLED(CONFIG_SOC_AT91SAM9))
939 return;
940
Olivier Deprez157378f2022-04-04 15:47:50 +0200941 /*
942 * Force STANDBY and ULP0 mode to avoid calling
943 * at91_pm_modes_validate() which may increase booting time.
944 * Platform supports anyway only STANDBY and ULP0 modes.
945 */
946 soc_pm.data.standby_mode = AT91_PM_STANDBY;
947 soc_pm.data.suspend_mode = AT91_PM_ULP0;
948
949 ret = at91_dt_ramc();
950 if (ret)
951 return;
952
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000953 at91_pm_init(at91sam9_idle);
954}
955
956void __init sama5_pm_init(void)
957{
Olivier Deprez157378f2022-04-04 15:47:50 +0200958 static const int modes[] __initconst = {
959 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST,
960 };
961 int ret;
962
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963 if (!IS_ENABLED(CONFIG_SOC_SAMA5))
964 return;
965
Olivier Deprez157378f2022-04-04 15:47:50 +0200966 at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
967 ret = at91_dt_ramc();
968 if (ret)
969 return;
970
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000971 at91_pm_init(NULL);
972}
973
974void __init sama5d2_pm_init(void)
975{
Olivier Deprez157378f2022-04-04 15:47:50 +0200976 static const int modes[] __initconst = {
977 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
978 AT91_PM_BACKUP,
979 };
980 int ret;
981
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982 if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
983 return;
984
Olivier Deprez157378f2022-04-04 15:47:50 +0200985 at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000986 at91_pm_modes_init();
Olivier Deprez157378f2022-04-04 15:47:50 +0200987 ret = at91_dt_ramc();
988 if (ret)
989 return;
990
991 at91_pm_init(NULL);
David Brazdil0f672f62019-12-10 10:32:29 +0000992
993 soc_pm.ws_ids = sama5d2_ws_ids;
994 soc_pm.config_shdwc_ws = at91_sama5d2_config_shdwc_ws;
995 soc_pm.config_pmc_ws = at91_sama5d2_config_pmc_ws;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000996}
997
998static int __init at91_pm_modes_select(char *str)
999{
1000 char *s;
1001 substring_t args[MAX_OPT_ARGS];
1002 int standby, suspend;
1003
1004 if (!str)
1005 return 0;
1006
1007 s = strsep(&str, ",");
1008 standby = match_token(s, pm_modes, args);
1009 if (standby < 0)
1010 return 0;
1011
1012 suspend = match_token(str, pm_modes, args);
1013 if (suspend < 0)
1014 return 0;
1015
David Brazdil0f672f62019-12-10 10:32:29 +00001016 soc_pm.data.standby_mode = standby;
1017 soc_pm.data.suspend_mode = suspend;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001018
1019 return 0;
1020}
1021early_param("atmel.pm_modes", at91_pm_modes_select);