ARM: at91: pm: save ddr phy calibration data to securam
[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                 return -EPERM;
734
735         if (!at91_is_pm_mode_active(AT91_PM_BACKUP))
736                 return 0;
737
738         np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
739         if (!np)
740                 return ret;
741
742         pdev = of_find_device_by_node(np);
743         of_node_put(np);
744         if (!pdev) {
745                 pr_warn("%s: failed to find securam device!\n", __func__);
746                 return ret;
747         }
748
749         sram_pool = gen_pool_get(&pdev->dev, NULL);
750         if (!sram_pool) {
751                 pr_warn("%s: securam pool unavailable!\n", __func__);
752                 goto securam_fail;
753         }
754
755         soc_pm.bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
756         if (!soc_pm.bu) {
757                 pr_warn("%s: unable to alloc securam!\n", __func__);
758                 ret = -ENOMEM;
759                 goto securam_fail;
760         }
761
762         soc_pm.bu->suspended = 0;
763         soc_pm.bu->canary = __pa_symbol(&canary);
764         soc_pm.bu->resume = __pa_symbol(cpu_resume);
765         if (soc_pm.data.ramc_phy) {
766                 of_scan_flat_dt(at91_pm_backup_scan_memcs, &located);
767                 if (!located)
768                         goto securam_fail;
769
770                 /* DDR3PHY_ZQ0SR0 */
771                 soc_pm.bu->ddr_phy_calibration[0] = readl(soc_pm.data.ramc_phy +
772                                                           0x188);
773         }
774
775         return 0;
776
777 securam_fail:
778         put_device(&pdev->dev);
779         return ret;
780 }
781
782 static const struct of_device_id atmel_shdwc_ids[] = {
783         { .compatible = "atmel,sama5d2-shdwc" },
784         { .compatible = "microchip,sam9x60-shdwc" },
785         { /* sentinel. */ }
786 };
787
788 static void __init at91_pm_modes_init(const u32 *maps, int len)
789 {
790         struct device_node *np;
791         int ret, mode;
792
793         ret = at91_pm_backup_init();
794         if (ret) {
795                 if (soc_pm.data.standby_mode == AT91_PM_BACKUP)
796                         soc_pm.data.standby_mode = AT91_PM_ULP0;
797                 if (soc_pm.data.suspend_mode == AT91_PM_BACKUP)
798                         soc_pm.data.suspend_mode = AT91_PM_ULP0;
799         }
800
801         if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SHDWC) ||
802             maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SHDWC)) {
803                 np = of_find_matching_node(NULL, atmel_shdwc_ids);
804                 if (!np) {
805                         pr_warn("%s: failed to find shdwc!\n", __func__);
806
807                         /* Use ULP0 if it doesn't needs SHDWC.*/
808                         if (!(maps[AT91_PM_ULP0] & AT91_PM_IOMAP(SHDWC)))
809                                 mode = AT91_PM_ULP0;
810                         else
811                                 mode = AT91_PM_STANDBY;
812
813                         if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SHDWC))
814                                 soc_pm.data.standby_mode = mode;
815                         if (maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SHDWC))
816                                 soc_pm.data.suspend_mode = mode;
817                 } else {
818                         soc_pm.data.shdwc = of_iomap(np, 0);
819                         of_node_put(np);
820                 }
821         }
822
823         if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SFRBU) ||
824             maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SFRBU)) {
825                 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-sfrbu");
826                 if (!np) {
827                         pr_warn("%s: failed to find sfrbu!\n", __func__);
828
829                         /*
830                          * Use ULP0 if it doesn't need SHDWC or if SHDWC
831                          * was already located.
832                          */
833                         if (!(maps[AT91_PM_ULP0] & AT91_PM_IOMAP(SHDWC)) ||
834                             soc_pm.data.shdwc)
835                                 mode = AT91_PM_ULP0;
836                         else
837                                 mode = AT91_PM_STANDBY;
838
839                         if (maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SFRBU))
840                                 soc_pm.data.standby_mode = mode;
841                         if (maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SFRBU))
842                                 soc_pm.data.suspend_mode = mode;
843                 } else {
844                         soc_pm.data.sfrbu = of_iomap(np, 0);
845                         of_node_put(np);
846                 }
847         }
848
849         /* Unmap all unnecessary. */
850         if (soc_pm.data.shdwc &&
851             !(maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SHDWC) ||
852               maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SHDWC))) {
853                 iounmap(soc_pm.data.shdwc);
854                 soc_pm.data.shdwc = NULL;
855         }
856
857         if (soc_pm.data.sfrbu &&
858             !(maps[soc_pm.data.standby_mode] & AT91_PM_IOMAP(SFRBU) ||
859               maps[soc_pm.data.suspend_mode] & AT91_PM_IOMAP(SFRBU))) {
860                 iounmap(soc_pm.data.sfrbu);
861                 soc_pm.data.sfrbu = NULL;
862         }
863
864         return;
865 }
866
867 struct pmc_info {
868         unsigned long uhp_udp_mask;
869         unsigned long mckr;
870         unsigned long version;
871 };
872
873 static const struct pmc_info pmc_infos[] __initconst = {
874         {
875                 .uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP,
876                 .mckr = 0x30,
877                 .version = AT91_PMC_V1,
878         },
879
880         {
881                 .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
882                 .mckr = 0x30,
883                 .version = AT91_PMC_V1,
884         },
885         {
886                 .uhp_udp_mask = AT91SAM926x_PMC_UHP,
887                 .mckr = 0x30,
888                 .version = AT91_PMC_V1,
889         },
890         {       .uhp_udp_mask = 0,
891                 .mckr = 0x30,
892                 .version = AT91_PMC_V1,
893         },
894         {
895                 .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP,
896                 .mckr = 0x28,
897                 .version = AT91_PMC_V2,
898         },
899 };
900
901 static const struct of_device_id atmel_pmc_ids[] __initconst = {
902         { .compatible = "atmel,at91rm9200-pmc", .data = &pmc_infos[0] },
903         { .compatible = "atmel,at91sam9260-pmc", .data = &pmc_infos[1] },
904         { .compatible = "atmel,at91sam9261-pmc", .data = &pmc_infos[1] },
905         { .compatible = "atmel,at91sam9263-pmc", .data = &pmc_infos[1] },
906         { .compatible = "atmel,at91sam9g45-pmc", .data = &pmc_infos[2] },
907         { .compatible = "atmel,at91sam9n12-pmc", .data = &pmc_infos[1] },
908         { .compatible = "atmel,at91sam9rl-pmc", .data = &pmc_infos[3] },
909         { .compatible = "atmel,at91sam9x5-pmc", .data = &pmc_infos[1] },
910         { .compatible = "atmel,sama5d3-pmc", .data = &pmc_infos[1] },
911         { .compatible = "atmel,sama5d4-pmc", .data = &pmc_infos[1] },
912         { .compatible = "atmel,sama5d2-pmc", .data = &pmc_infos[1] },
913         { .compatible = "microchip,sam9x60-pmc", .data = &pmc_infos[4] },
914         { /* sentinel */ },
915 };
916
917 static void __init at91_pm_modes_validate(const int *modes, int len)
918 {
919         u8 i, standby = 0, suspend = 0;
920         int mode;
921
922         for (i = 0; i < len; i++) {
923                 if (standby && suspend)
924                         break;
925
926                 if (modes[i] == soc_pm.data.standby_mode && !standby) {
927                         standby = 1;
928                         continue;
929                 }
930
931                 if (modes[i] == soc_pm.data.suspend_mode && !suspend) {
932                         suspend = 1;
933                         continue;
934                 }
935         }
936
937         if (!standby) {
938                 if (soc_pm.data.suspend_mode == AT91_PM_STANDBY)
939                         mode = AT91_PM_ULP0;
940                 else
941                         mode = AT91_PM_STANDBY;
942
943                 pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
944                         pm_modes[soc_pm.data.standby_mode].pattern,
945                         pm_modes[mode].pattern);
946                 soc_pm.data.standby_mode = mode;
947         }
948
949         if (!suspend) {
950                 if (soc_pm.data.standby_mode == AT91_PM_ULP0)
951                         mode = AT91_PM_STANDBY;
952                 else
953                         mode = AT91_PM_ULP0;
954
955                 pr_warn("AT91: PM: %s mode not supported! Using %s.\n",
956                         pm_modes[soc_pm.data.suspend_mode].pattern,
957                         pm_modes[mode].pattern);
958                 soc_pm.data.suspend_mode = mode;
959         }
960 }
961
962 static void __init at91_pm_init(void (*pm_idle)(void))
963 {
964         struct device_node *pmc_np;
965         const struct of_device_id *of_id;
966         const struct pmc_info *pmc;
967
968         if (at91_cpuidle_device.dev.platform_data)
969                 platform_device_register(&at91_cpuidle_device);
970
971         pmc_np = of_find_matching_node_and_match(NULL, atmel_pmc_ids, &of_id);
972         soc_pm.data.pmc = of_iomap(pmc_np, 0);
973         of_node_put(pmc_np);
974         if (!soc_pm.data.pmc) {
975                 pr_err("AT91: PM not supported, PMC not found\n");
976                 return;
977         }
978
979         pmc = of_id->data;
980         soc_pm.data.uhp_udp_mask = pmc->uhp_udp_mask;
981         soc_pm.data.pmc_mckr_offset = pmc->mckr;
982         soc_pm.data.pmc_version = pmc->version;
983
984         if (pm_idle)
985                 arm_pm_idle = pm_idle;
986
987         at91_pm_sram_init();
988
989         if (at91_suspend_sram_fn) {
990                 suspend_set_ops(&at91_pm_ops);
991                 pr_info("AT91: PM: standby: %s, suspend: %s\n",
992                         pm_modes[soc_pm.data.standby_mode].pattern,
993                         pm_modes[soc_pm.data.suspend_mode].pattern);
994         } else {
995                 pr_info("AT91: PM not supported, due to no SRAM allocated\n");
996         }
997 }
998
999 void __init at91rm9200_pm_init(void)
1000 {
1001         if (!IS_ENABLED(CONFIG_SOC_AT91RM9200))
1002                 return;
1003
1004         /*
1005          * Force STANDBY and ULP0 mode to avoid calling
1006          * at91_pm_modes_validate() which may increase booting time.
1007          * Platform supports anyway only STANDBY and ULP0 modes.
1008          */
1009         soc_pm.data.standby_mode = AT91_PM_STANDBY;
1010         soc_pm.data.suspend_mode = AT91_PM_ULP0;
1011
1012         at91_dt_ramc(false);
1013
1014         /*
1015          * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
1016          */
1017         at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
1018
1019         at91_pm_init(at91rm9200_idle);
1020 }
1021
1022 void __init sam9x60_pm_init(void)
1023 {
1024         static const int modes[] __initconst = {
1025                 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
1026         };
1027         static const int iomaps[] __initconst = {
1028                 [AT91_PM_ULP1]          = AT91_PM_IOMAP(SHDWC),
1029         };
1030
1031         if (!IS_ENABLED(CONFIG_SOC_SAM9X60))
1032                 return;
1033
1034         at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1035         at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1036         at91_dt_ramc(false);
1037         at91_pm_init(NULL);
1038
1039         soc_pm.ws_ids = sam9x60_ws_ids;
1040         soc_pm.config_pmc_ws = at91_sam9x60_config_pmc_ws;
1041 }
1042
1043 void __init at91sam9_pm_init(void)
1044 {
1045         if (!IS_ENABLED(CONFIG_SOC_AT91SAM9))
1046                 return;
1047
1048         /*
1049          * Force STANDBY and ULP0 mode to avoid calling
1050          * at91_pm_modes_validate() which may increase booting time.
1051          * Platform supports anyway only STANDBY and ULP0 modes.
1052          */
1053         soc_pm.data.standby_mode = AT91_PM_STANDBY;
1054         soc_pm.data.suspend_mode = AT91_PM_ULP0;
1055
1056         at91_dt_ramc(false);
1057         at91_pm_init(at91sam9_idle);
1058 }
1059
1060 void __init sama5_pm_init(void)
1061 {
1062         static const int modes[] __initconst = {
1063                 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST,
1064         };
1065
1066         if (!IS_ENABLED(CONFIG_SOC_SAMA5))
1067                 return;
1068
1069         at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1070         at91_dt_ramc(false);
1071         at91_pm_init(NULL);
1072 }
1073
1074 void __init sama5d2_pm_init(void)
1075 {
1076         static const int modes[] __initconst = {
1077                 AT91_PM_STANDBY, AT91_PM_ULP0, AT91_PM_ULP0_FAST, AT91_PM_ULP1,
1078                 AT91_PM_BACKUP,
1079         };
1080         static const u32 iomaps[] __initconst = {
1081                 [AT91_PM_ULP1]          = AT91_PM_IOMAP(SHDWC),
1082                 [AT91_PM_BACKUP]        = AT91_PM_IOMAP(SHDWC) |
1083                                           AT91_PM_IOMAP(SFRBU),
1084         };
1085
1086         if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
1087                 return;
1088
1089         at91_pm_modes_validate(modes, ARRAY_SIZE(modes));
1090         at91_pm_modes_init(iomaps, ARRAY_SIZE(iomaps));
1091         at91_dt_ramc(false);
1092         at91_pm_init(NULL);
1093
1094         soc_pm.ws_ids = sama5d2_ws_ids;
1095         soc_pm.config_shdwc_ws = at91_sama5d2_config_shdwc_ws;
1096         soc_pm.config_pmc_ws = at91_sama5d2_config_pmc_ws;
1097 }
1098
1099 static int __init at91_pm_modes_select(char *str)
1100 {
1101         char *s;
1102         substring_t args[MAX_OPT_ARGS];
1103         int standby, suspend;
1104
1105         if (!str)
1106                 return 0;
1107
1108         s = strsep(&str, ",");
1109         standby = match_token(s, pm_modes, args);
1110         if (standby < 0)
1111                 return 0;
1112
1113         suspend = match_token(str, pm_modes, args);
1114         if (suspend < 0)
1115                 return 0;
1116
1117         soc_pm.data.standby_mode = standby;
1118         soc_pm.data.suspend_mode = suspend;
1119
1120         return 0;
1121 }
1122 early_param("atmel.pm_modes", at91_pm_modes_select);