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