]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/arm/mach-at91/pm.c
ARM: at91: pm: introduce at91_soc_pm structure
[linux.git] / arch / arm / mach-at91 / pm.c
1 /*
2  * arch/arm/mach-at91/pm.c
3  * AT91 Power Management
4  *
5  * Copyright (C) 2005 David Brownell
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include <linux/genalloc.h>
14 #include <linux/io.h>
15 #include <linux/of_address.h>
16 #include <linux/of.h>
17 #include <linux/of_platform.h>
18 #include <linux/parser.h>
19 #include <linux/suspend.h>
20
21 #include <linux/clk/at91_pmc.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 /*
32  * FIXME: this is needed to communicate between the pinctrl driver and
33  * the PM implementation in the machine. Possibly part of the PM
34  * implementation should be moved down into the pinctrl driver and get
35  * called as part of the generic suspend/resume path.
36  */
37 #ifdef CONFIG_PINCTRL_AT91
38 extern void at91_pinctrl_gpio_suspend(void);
39 extern void at91_pinctrl_gpio_resume(void);
40 #endif
41
42 struct at91_soc_pm {
43         struct at91_pm_data data;
44 };
45
46 static struct at91_soc_pm soc_pm = {
47         .data = {
48                 .standby_mode = AT91_PM_STANDBY,
49                 .suspend_mode = AT91_PM_ULP0,
50         },
51 };
52
53 static const match_table_t pm_modes __initconst = {
54         { AT91_PM_STANDBY, "standby" },
55         { AT91_PM_ULP0, "ulp0" },
56         { AT91_PM_ULP1, "ulp1" },
57         { AT91_PM_BACKUP, "backup" },
58         { -1, NULL },
59 };
60
61 #define at91_ramc_read(id, field) \
62         __raw_readl(soc_pm.data.ramc[id] + field)
63
64 #define at91_ramc_write(id, field, value) \
65         __raw_writel(value, soc_pm.data.ramc[id] + field)
66
67 static int at91_pm_valid_state(suspend_state_t state)
68 {
69         switch (state) {
70                 case PM_SUSPEND_ON:
71                 case PM_SUSPEND_STANDBY:
72                 case PM_SUSPEND_MEM:
73                         return 1;
74
75                 default:
76                         return 0;
77         }
78 }
79
80 static int canary = 0xA5A5A5A5;
81
82 static struct at91_pm_bu {
83         int suspended;
84         unsigned long reserved;
85         phys_addr_t canary;
86         phys_addr_t resume;
87 } *pm_bu;
88
89 struct wakeup_source_info {
90         unsigned int pmc_fsmr_bit;
91         unsigned int shdwc_mr_bit;
92         bool set_polarity;
93 };
94
95 static const struct wakeup_source_info ws_info[] = {
96         { .pmc_fsmr_bit = AT91_PMC_FSTT(10),    .set_polarity = true },
97         { .pmc_fsmr_bit = AT91_PMC_RTCAL,       .shdwc_mr_bit = BIT(17) },
98         { .pmc_fsmr_bit = AT91_PMC_USBAL },
99         { .pmc_fsmr_bit = AT91_PMC_SDMMC_CD },
100 };
101
102 static const struct of_device_id sama5d2_ws_ids[] = {
103         { .compatible = "atmel,sama5d2-gem",            .data = &ws_info[0] },
104         { .compatible = "atmel,at91rm9200-rtc",         .data = &ws_info[1] },
105         { .compatible = "atmel,sama5d3-udc",            .data = &ws_info[2] },
106         { .compatible = "atmel,at91rm9200-ohci",        .data = &ws_info[2] },
107         { .compatible = "usb-ohci",                     .data = &ws_info[2] },
108         { .compatible = "atmel,at91sam9g45-ehci",       .data = &ws_info[2] },
109         { .compatible = "usb-ehci",                     .data = &ws_info[2] },
110         { .compatible = "atmel,sama5d2-sdhci",          .data = &ws_info[3] },
111         { /* sentinel */ }
112 };
113
114 static int at91_pm_config_ws(unsigned int pm_mode, bool set)
115 {
116         const struct wakeup_source_info *wsi;
117         const struct of_device_id *match;
118         struct platform_device *pdev;
119         struct device_node *np;
120         unsigned int mode = 0, polarity = 0, val = 0;
121
122         if (pm_mode != AT91_PM_ULP1)
123                 return 0;
124
125         if (!soc_pm.data.pmc || !soc_pm.data.shdwc)
126                 return -EPERM;
127
128         if (!set) {
129                 writel(mode, soc_pm.data.pmc + AT91_PMC_FSMR);
130                 return 0;
131         }
132
133         /* SHDWC.WUIR */
134         val = readl(soc_pm.data.shdwc + 0x0c);
135         mode |= (val & 0x3ff);
136         polarity |= ((val >> 16) & 0x3ff);
137
138         /* SHDWC.MR */
139         val = readl(soc_pm.data.shdwc + 0x04);
140
141         /* Loop through defined wakeup sources. */
142         for_each_matching_node_and_match(np, sama5d2_ws_ids, &match) {
143                 pdev = of_find_device_by_node(np);
144                 if (!pdev)
145                         continue;
146
147                 if (device_may_wakeup(&pdev->dev)) {
148                         wsi = match->data;
149
150                         /* Check if enabled on SHDWC. */
151                         if (wsi->shdwc_mr_bit && !(val & wsi->shdwc_mr_bit))
152                                 goto put_device;
153
154                         mode |= wsi->pmc_fsmr_bit;
155                         if (wsi->set_polarity)
156                                 polarity |= wsi->pmc_fsmr_bit;
157                 }
158
159 put_device:
160                 put_device(&pdev->dev);
161         }
162
163         if (mode) {
164                 writel(mode, soc_pm.data.pmc + AT91_PMC_FSMR);
165                 writel(polarity, soc_pm.data.pmc + AT91_PMC_FSPR);
166         } else {
167                 pr_err("AT91: PM: no ULP1 wakeup sources found!");
168         }
169
170         return mode ? 0 : -EPERM;
171 }
172
173 /*
174  * Called after processes are frozen, but before we shutdown devices.
175  */
176 static int at91_pm_begin(suspend_state_t state)
177 {
178         switch (state) {
179         case PM_SUSPEND_MEM:
180                 soc_pm.data.mode = soc_pm.data.suspend_mode;
181                 break;
182
183         case PM_SUSPEND_STANDBY:
184                 soc_pm.data.mode = soc_pm.data.standby_mode;
185                 break;
186
187         default:
188                 soc_pm.data.mode = -1;
189         }
190
191         return at91_pm_config_ws(soc_pm.data.mode, true);
192 }
193
194 /*
195  * Verify that all the clocks are correct before entering
196  * slow-clock mode.
197  */
198 static int at91_pm_verify_clocks(void)
199 {
200         unsigned long scsr;
201         int i;
202
203         scsr = readl(soc_pm.data.pmc + AT91_PMC_SCSR);
204
205         /* USB must not be using PLLB */
206         if ((scsr & soc_pm.data.uhp_udp_mask) != 0) {
207                 pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
208                 return 0;
209         }
210
211         /* PCK0..PCK3 must be disabled, or configured to use clk32k */
212         for (i = 0; i < 4; i++) {
213                 u32 css;
214
215                 if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
216                         continue;
217                 css = readl(soc_pm.data.pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
218                 if (css != AT91_PMC_CSS_SLOW) {
219                         pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
220                         return 0;
221                 }
222         }
223
224         return 1;
225 }
226
227 /*
228  * Call this from platform driver suspend() to see how deeply to suspend.
229  * For example, some controllers (like OHCI) need one of the PLL clocks
230  * in order to act as a wakeup source, and those are not available when
231  * going into slow clock mode.
232  *
233  * REVISIT: generalize as clk_will_be_available(clk)?  Other platforms have
234  * the very same problem (but not using at91 main_clk), and it'd be better
235  * to add one generic API rather than lots of platform-specific ones.
236  */
237 int at91_suspend_entering_slow_clock(void)
238 {
239         return (soc_pm.data.mode >= AT91_PM_ULP0);
240 }
241 EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
242
243 static void (*at91_suspend_sram_fn)(struct at91_pm_data *);
244 extern void at91_pm_suspend_in_sram(struct at91_pm_data *pm_data);
245 extern u32 at91_pm_suspend_in_sram_sz;
246
247 static int at91_suspend_finish(unsigned long val)
248 {
249         flush_cache_all();
250         outer_disable();
251
252         at91_suspend_sram_fn(&soc_pm.data);
253
254         return 0;
255 }
256
257 static void at91_pm_suspend(suspend_state_t state)
258 {
259         if (soc_pm.data.mode == AT91_PM_BACKUP) {
260                 pm_bu->suspended = 1;
261
262                 cpu_suspend(0, at91_suspend_finish);
263
264                 /* The SRAM is lost between suspend cycles */
265                 at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
266                                              &at91_pm_suspend_in_sram,
267                                              at91_pm_suspend_in_sram_sz);
268         } else {
269                 at91_suspend_finish(0);
270         }
271
272         outer_resume();
273 }
274
275 /*
276  * STANDBY mode has *all* drivers suspended; ignores irqs not marked as 'wakeup'
277  * event sources; and reduces DRAM power.  But otherwise it's identical to
278  * PM_SUSPEND_ON: cpu idle, and nothing fancy done with main or cpu clocks.
279  *
280  * AT91_PM_ULP0 is like STANDBY plus slow clock mode, so drivers must
281  * suspend more deeply, the master clock switches to the clk32k and turns off
282  * the main oscillator
283  *
284  * AT91_PM_BACKUP turns off the whole SoC after placing the DDR in self refresh
285  */
286 static int at91_pm_enter(suspend_state_t state)
287 {
288 #ifdef CONFIG_PINCTRL_AT91
289         at91_pinctrl_gpio_suspend();
290 #endif
291
292         switch (state) {
293         case PM_SUSPEND_MEM:
294         case PM_SUSPEND_STANDBY:
295                 /*
296                  * Ensure that clocks are in a valid state.
297                  */
298                 if (soc_pm.data.mode >= AT91_PM_ULP0 &&
299                     !at91_pm_verify_clocks())
300                         goto error;
301
302                 at91_pm_suspend(state);
303
304                 break;
305
306         case PM_SUSPEND_ON:
307                 cpu_do_idle();
308                 break;
309
310         default:
311                 pr_debug("AT91: PM - bogus suspend state %d\n", state);
312                 goto error;
313         }
314
315 error:
316 #ifdef CONFIG_PINCTRL_AT91
317         at91_pinctrl_gpio_resume();
318 #endif
319         return 0;
320 }
321
322 /*
323  * Called right prior to thawing processes.
324  */
325 static void at91_pm_end(void)
326 {
327         at91_pm_config_ws(soc_pm.data.mode, false);
328 }
329
330
331 static const struct platform_suspend_ops at91_pm_ops = {
332         .valid  = at91_pm_valid_state,
333         .begin  = at91_pm_begin,
334         .enter  = at91_pm_enter,
335         .end    = at91_pm_end,
336 };
337
338 static struct platform_device at91_cpuidle_device = {
339         .name = "cpuidle-at91",
340 };
341
342 /*
343  * The AT91RM9200 goes into self-refresh mode with this command, and will
344  * terminate self-refresh automatically on the next SDRAM access.
345  *
346  * Self-refresh mode is exited as soon as a memory access is made, but we don't
347  * know for sure when that happens. However, we need to restore the low-power
348  * mode if it was enabled before going idle. Restoring low-power mode while
349  * still in self-refresh is "not recommended", but seems to work.
350  */
351 static void at91rm9200_standby(void)
352 {
353         asm volatile(
354                 "b    1f\n\t"
355                 ".align    5\n\t"
356                 "1:  mcr    p15, 0, %0, c7, c10, 4\n\t"
357                 "    str    %2, [%1, %3]\n\t"
358                 "    mcr    p15, 0, %0, c7, c0, 4\n\t"
359                 :
360                 : "r" (0), "r" (soc_pm.data.ramc[0]),
361                   "r" (1), "r" (AT91_MC_SDRAMC_SRR));
362 }
363
364 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
365  * remember.
366  */
367 static void at91_ddr_standby(void)
368 {
369         /* Those two values allow us to delay self-refresh activation
370          * to the maximum. */
371         u32 lpr0, lpr1 = 0;
372         u32 mdr, saved_mdr0, saved_mdr1 = 0;
373         u32 saved_lpr0, saved_lpr1 = 0;
374
375         /* LPDDR1 --> force DDR2 mode during self-refresh */
376         saved_mdr0 = at91_ramc_read(0, AT91_DDRSDRC_MDR);
377         if ((saved_mdr0 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
378                 mdr = saved_mdr0 & ~AT91_DDRSDRC_MD;
379                 mdr |= AT91_DDRSDRC_MD_DDR2;
380                 at91_ramc_write(0, AT91_DDRSDRC_MDR, mdr);
381         }
382
383         if (soc_pm.data.ramc[1]) {
384                 saved_lpr1 = at91_ramc_read(1, AT91_DDRSDRC_LPR);
385                 lpr1 = saved_lpr1 & ~AT91_DDRSDRC_LPCB;
386                 lpr1 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
387                 saved_mdr1 = at91_ramc_read(1, AT91_DDRSDRC_MDR);
388                 if ((saved_mdr1 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
389                         mdr = saved_mdr1 & ~AT91_DDRSDRC_MD;
390                         mdr |= AT91_DDRSDRC_MD_DDR2;
391                         at91_ramc_write(1, AT91_DDRSDRC_MDR, mdr);
392                 }
393         }
394
395         saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
396         lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
397         lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
398
399         /* self-refresh mode now */
400         at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
401         if (soc_pm.data.ramc[1])
402                 at91_ramc_write(1, AT91_DDRSDRC_LPR, lpr1);
403
404         cpu_do_idle();
405
406         at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr0);
407         at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
408         if (soc_pm.data.ramc[1]) {
409                 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr1);
410                 at91_ramc_write(1, AT91_DDRSDRC_LPR, saved_lpr1);
411         }
412 }
413
414 static void sama5d3_ddr_standby(void)
415 {
416         u32 lpr0;
417         u32 saved_lpr0;
418
419         saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
420         lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
421         lpr0 |= AT91_DDRSDRC_LPCB_POWER_DOWN;
422
423         at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
424
425         cpu_do_idle();
426
427         at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
428 }
429
430 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
431  * remember.
432  */
433 static void at91sam9_sdram_standby(void)
434 {
435         u32 lpr0, lpr1 = 0;
436         u32 saved_lpr0, saved_lpr1 = 0;
437
438         if (soc_pm.data.ramc[1]) {
439                 saved_lpr1 = at91_ramc_read(1, AT91_SDRAMC_LPR);
440                 lpr1 = saved_lpr1 & ~AT91_SDRAMC_LPCB;
441                 lpr1 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
442         }
443
444         saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
445         lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
446         lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
447
448         /* self-refresh mode now */
449         at91_ramc_write(0, AT91_SDRAMC_LPR, lpr0);
450         if (soc_pm.data.ramc[1])
451                 at91_ramc_write(1, AT91_SDRAMC_LPR, lpr1);
452
453         cpu_do_idle();
454
455         at91_ramc_write(0, AT91_SDRAMC_LPR, saved_lpr0);
456         if (soc_pm.data.ramc[1])
457                 at91_ramc_write(1, AT91_SDRAMC_LPR, saved_lpr1);
458 }
459
460 struct ramc_info {
461         void (*idle)(void);
462         unsigned int memctrl;
463 };
464
465 static const struct ramc_info ramc_infos[] __initconst = {
466         { .idle = at91rm9200_standby, .memctrl = AT91_MEMCTRL_MC},
467         { .idle = at91sam9_sdram_standby, .memctrl = AT91_MEMCTRL_SDRAMC},
468         { .idle = at91_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
469         { .idle = sama5d3_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
470 };
471
472 static const struct of_device_id ramc_ids[] __initconst = {
473         { .compatible = "atmel,at91rm9200-sdramc", .data = &ramc_infos[0] },
474         { .compatible = "atmel,at91sam9260-sdramc", .data = &ramc_infos[1] },
475         { .compatible = "atmel,at91sam9g45-ddramc", .data = &ramc_infos[2] },
476         { .compatible = "atmel,sama5d3-ddramc", .data = &ramc_infos[3] },
477         { /*sentinel*/ }
478 };
479
480 static __init void at91_dt_ramc(void)
481 {
482         struct device_node *np;
483         const struct of_device_id *of_id;
484         int idx = 0;
485         void *standby = NULL;
486         const struct ramc_info *ramc;
487
488         for_each_matching_node_and_match(np, ramc_ids, &of_id) {
489                 soc_pm.data.ramc[idx] = of_iomap(np, 0);
490                 if (!soc_pm.data.ramc[idx])
491                         panic(pr_fmt("unable to map ramc[%d] cpu registers\n"), idx);
492
493                 ramc = of_id->data;
494                 if (!standby)
495                         standby = ramc->idle;
496                 soc_pm.data.memctrl = ramc->memctrl;
497
498                 idx++;
499         }
500
501         if (!idx)
502                 panic(pr_fmt("unable to find compatible ram controller node in dtb\n"));
503
504         if (!standby) {
505                 pr_warn("ramc no standby function available\n");
506                 return;
507         }
508
509         at91_cpuidle_device.dev.platform_data = standby;
510 }
511
512 static void at91rm9200_idle(void)
513 {
514         /*
515          * Disable the processor clock.  The processor will be automatically
516          * re-enabled by an interrupt or by a reset.
517          */
518         writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
519 }
520
521 static void at91sam9_idle(void)
522 {
523         writel(AT91_PMC_PCK, soc_pm.data.pmc + AT91_PMC_SCDR);
524         cpu_do_idle();
525 }
526
527 static void __init at91_pm_sram_init(void)
528 {
529         struct gen_pool *sram_pool;
530         phys_addr_t sram_pbase;
531         unsigned long sram_base;
532         struct device_node *node;
533         struct platform_device *pdev = NULL;
534
535         for_each_compatible_node(node, NULL, "mmio-sram") {
536                 pdev = of_find_device_by_node(node);
537                 if (pdev) {
538                         of_node_put(node);
539                         break;
540                 }
541         }
542
543         if (!pdev) {
544                 pr_warn("%s: failed to find sram device!\n", __func__);
545                 return;
546         }
547
548         sram_pool = gen_pool_get(&pdev->dev, NULL);
549         if (!sram_pool) {
550                 pr_warn("%s: sram pool unavailable!\n", __func__);
551                 return;
552         }
553
554         sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
555         if (!sram_base) {
556                 pr_warn("%s: unable to alloc sram!\n", __func__);
557                 return;
558         }
559
560         sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
561         at91_suspend_sram_fn = __arm_ioremap_exec(sram_pbase,
562                                         at91_pm_suspend_in_sram_sz, false);
563         if (!at91_suspend_sram_fn) {
564                 pr_warn("SRAM: Could not map\n");
565                 return;
566         }
567
568         /* Copy the pm suspend handler to SRAM */
569         at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
570                         &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
571 }
572
573 static bool __init at91_is_pm_mode_active(int pm_mode)
574 {
575         return (soc_pm.data.standby_mode == pm_mode ||
576                 soc_pm.data.suspend_mode == pm_mode);
577 }
578
579 static int __init at91_pm_backup_init(void)
580 {
581         struct gen_pool *sram_pool;
582         struct device_node *np;
583         struct platform_device *pdev = NULL;
584         int ret = -ENODEV;
585
586         if (!at91_is_pm_mode_active(AT91_PM_BACKUP))
587                 return 0;
588
589         np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-sfrbu");
590         if (!np) {
591                 pr_warn("%s: failed to find sfrbu!\n", __func__);
592                 return ret;
593         }
594
595         soc_pm.data.sfrbu = of_iomap(np, 0);
596         of_node_put(np);
597
598         np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
599         if (!np)
600                 goto securam_fail;
601
602         pdev = of_find_device_by_node(np);
603         of_node_put(np);
604         if (!pdev) {
605                 pr_warn("%s: failed to find securam device!\n", __func__);
606                 goto securam_fail;
607         }
608
609         sram_pool = gen_pool_get(&pdev->dev, NULL);
610         if (!sram_pool) {
611                 pr_warn("%s: securam pool unavailable!\n", __func__);
612                 goto securam_fail;
613         }
614
615         pm_bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
616         if (!pm_bu) {
617                 pr_warn("%s: unable to alloc securam!\n", __func__);
618                 ret = -ENOMEM;
619                 goto securam_fail;
620         }
621
622         pm_bu->suspended = 0;
623         pm_bu->canary = __pa_symbol(&canary);
624         pm_bu->resume = __pa_symbol(cpu_resume);
625
626         return 0;
627
628 securam_fail:
629         iounmap(soc_pm.data.sfrbu);
630         soc_pm.data.sfrbu = NULL;
631         return ret;
632 }
633
634 static void __init at91_pm_use_default_mode(int pm_mode)
635 {
636         if (pm_mode != AT91_PM_ULP1 && pm_mode != AT91_PM_BACKUP)
637                 return;
638
639         if (soc_pm.data.standby_mode == pm_mode)
640                 soc_pm.data.standby_mode = AT91_PM_ULP0;
641         if (soc_pm.data.suspend_mode == pm_mode)
642                 soc_pm.data.suspend_mode = AT91_PM_ULP0;
643 }
644
645 static void __init at91_pm_modes_init(void)
646 {
647         struct device_node *np;
648         int ret;
649
650         if (!at91_is_pm_mode_active(AT91_PM_BACKUP) &&
651             !at91_is_pm_mode_active(AT91_PM_ULP1))
652                 return;
653
654         np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-shdwc");
655         if (!np) {
656                 pr_warn("%s: failed to find shdwc!\n", __func__);
657                 goto ulp1_default;
658         }
659
660         soc_pm.data.shdwc = of_iomap(np, 0);
661         of_node_put(np);
662
663         ret = at91_pm_backup_init();
664         if (ret) {
665                 if (!at91_is_pm_mode_active(AT91_PM_ULP1))
666                         goto unmap;
667                 else
668                         goto backup_default;
669         }
670
671         return;
672
673 unmap:
674         iounmap(soc_pm.data.shdwc);
675         soc_pm.data.shdwc = NULL;
676 ulp1_default:
677         at91_pm_use_default_mode(AT91_PM_ULP1);
678 backup_default:
679         at91_pm_use_default_mode(AT91_PM_BACKUP);
680 }
681
682 struct pmc_info {
683         unsigned long uhp_udp_mask;
684 };
685
686 static const struct pmc_info pmc_infos[] __initconst = {
687         { .uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP },
688         { .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP },
689         { .uhp_udp_mask = AT91SAM926x_PMC_UHP },
690         { .uhp_udp_mask = 0 },
691 };
692
693 static const struct of_device_id atmel_pmc_ids[] __initconst = {
694         { .compatible = "atmel,at91rm9200-pmc", .data = &pmc_infos[0] },
695         { .compatible = "atmel,at91sam9260-pmc", .data = &pmc_infos[1] },
696         { .compatible = "atmel,at91sam9261-pmc", .data = &pmc_infos[1] },
697         { .compatible = "atmel,at91sam9263-pmc", .data = &pmc_infos[1] },
698         { .compatible = "atmel,at91sam9g45-pmc", .data = &pmc_infos[2] },
699         { .compatible = "atmel,at91sam9n12-pmc", .data = &pmc_infos[1] },
700         { .compatible = "atmel,at91sam9rl-pmc", .data = &pmc_infos[3] },
701         { .compatible = "atmel,at91sam9x5-pmc", .data = &pmc_infos[1] },
702         { .compatible = "atmel,sama5d3-pmc", .data = &pmc_infos[1] },
703         { .compatible = "atmel,sama5d4-pmc", .data = &pmc_infos[1] },
704         { .compatible = "atmel,sama5d2-pmc", .data = &pmc_infos[1] },
705         { /* sentinel */ },
706 };
707
708 static void __init at91_pm_init(void (*pm_idle)(void))
709 {
710         struct device_node *pmc_np;
711         const struct of_device_id *of_id;
712         const struct pmc_info *pmc;
713
714         if (at91_cpuidle_device.dev.platform_data)
715                 platform_device_register(&at91_cpuidle_device);
716
717         pmc_np = of_find_matching_node_and_match(NULL, atmel_pmc_ids, &of_id);
718         soc_pm.data.pmc = of_iomap(pmc_np, 0);
719         if (!soc_pm.data.pmc) {
720                 pr_err("AT91: PM not supported, PMC not found\n");
721                 return;
722         }
723
724         pmc = of_id->data;
725         soc_pm.data.uhp_udp_mask = pmc->uhp_udp_mask;
726
727         if (pm_idle)
728                 arm_pm_idle = pm_idle;
729
730         at91_pm_sram_init();
731
732         if (at91_suspend_sram_fn) {
733                 suspend_set_ops(&at91_pm_ops);
734                 pr_info("AT91: PM: standby: %s, suspend: %s\n",
735                         pm_modes[soc_pm.data.standby_mode].pattern,
736                         pm_modes[soc_pm.data.suspend_mode].pattern);
737         } else {
738                 pr_info("AT91: PM not supported, due to no SRAM allocated\n");
739         }
740 }
741
742 void __init at91rm9200_pm_init(void)
743 {
744         if (!IS_ENABLED(CONFIG_SOC_AT91RM9200))
745                 return;
746
747         at91_dt_ramc();
748
749         /*
750          * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
751          */
752         at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
753
754         at91_pm_init(at91rm9200_idle);
755 }
756
757 void __init at91sam9_pm_init(void)
758 {
759         if (!IS_ENABLED(CONFIG_SOC_AT91SAM9))
760                 return;
761
762         at91_dt_ramc();
763         at91_pm_init(at91sam9_idle);
764 }
765
766 void __init sama5_pm_init(void)
767 {
768         if (!IS_ENABLED(CONFIG_SOC_SAMA5))
769                 return;
770
771         at91_dt_ramc();
772         at91_pm_init(NULL);
773 }
774
775 void __init sama5d2_pm_init(void)
776 {
777         if (!IS_ENABLED(CONFIG_SOC_SAMA5D2))
778                 return;
779
780         at91_pm_modes_init();
781         sama5_pm_init();
782 }
783
784 static int __init at91_pm_modes_select(char *str)
785 {
786         char *s;
787         substring_t args[MAX_OPT_ARGS];
788         int standby, suspend;
789
790         if (!str)
791                 return 0;
792
793         s = strsep(&str, ",");
794         standby = match_token(s, pm_modes, args);
795         if (standby < 0)
796                 return 0;
797
798         suspend = match_token(str, pm_modes, args);
799         if (suspend < 0)
800                 return 0;
801
802         soc_pm.data.standby_mode = standby;
803         soc_pm.data.suspend_mode = suspend;
804
805         return 0;
806 }
807 early_param("atmel.pm_modes", at91_pm_modes_select);