2 * arch/arm/mach-at91/pm.c
3 * AT91 Power Management
5 * Copyright (C) 2005 David Brownell
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.
13 #include <linux/genalloc.h>
15 #include <linux/of_address.h>
17 #include <linux/of_platform.h>
18 #include <linux/parser.h>
19 #include <linux/suspend.h>
21 #include <linux/clk/at91_pmc.h>
23 #include <asm/cacheflush.h>
24 #include <asm/fncpy.h>
25 #include <asm/system_misc.h>
26 #include <asm/suspend.h>
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.
37 #ifdef CONFIG_PINCTRL_AT91
38 extern void at91_pinctrl_gpio_suspend(void);
39 extern void at91_pinctrl_gpio_resume(void);
42 static const match_table_t pm_modes __initconst
= {
44 { AT91_PM_SLOW_CLOCK
, "ulp0" },
45 { AT91_PM_BACKUP
, "backup" },
49 static struct at91_pm_data pm_data
= {
51 .suspend_mode
= AT91_PM_SLOW_CLOCK
,
54 #define at91_ramc_read(id, field) \
55 __raw_readl(pm_data.ramc[id] + field)
57 #define at91_ramc_write(id, field, value) \
58 __raw_writel(value, pm_data.ramc[id] + field)
60 static int at91_pm_valid_state(suspend_state_t state
)
64 case PM_SUSPEND_STANDBY
:
73 static int canary
= 0xA5A5A5A5;
75 static struct at91_pm_bu
{
77 unsigned long reserved
;
83 * Called after processes are frozen, but before we shutdown devices.
85 static int at91_pm_begin(suspend_state_t state
)
89 pm_data
.mode
= pm_data
.suspend_mode
;
92 case PM_SUSPEND_STANDBY
:
93 pm_data
.mode
= pm_data
.standby_mode
;
104 * Verify that all the clocks are correct before entering
107 static int at91_pm_verify_clocks(void)
112 scsr
= readl(pm_data
.pmc
+ AT91_PMC_SCSR
);
114 /* USB must not be using PLLB */
115 if ((scsr
& pm_data
.uhp_udp_mask
) != 0) {
116 pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
120 /* PCK0..PCK3 must be disabled, or configured to use clk32k */
121 for (i
= 0; i
< 4; i
++) {
124 if ((scsr
& (AT91_PMC_PCK0
<< i
)) == 0)
126 css
= readl(pm_data
.pmc
+ AT91_PMC_PCKR(i
)) & AT91_PMC_CSS
;
127 if (css
!= AT91_PMC_CSS_SLOW
) {
128 pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i
, css
);
137 * Call this from platform driver suspend() to see how deeply to suspend.
138 * For example, some controllers (like OHCI) need one of the PLL clocks
139 * in order to act as a wakeup source, and those are not available when
140 * going into slow clock mode.
142 * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
143 * the very same problem (but not using at91 main_clk), and it'd be better
144 * to add one generic API rather than lots of platform-specific ones.
146 int at91_suspend_entering_slow_clock(void)
148 return (pm_data
.mode
>= AT91_PM_SLOW_CLOCK
);
150 EXPORT_SYMBOL(at91_suspend_entering_slow_clock
);
152 static void (*at91_suspend_sram_fn
)(struct at91_pm_data
*);
153 extern void at91_pm_suspend_in_sram(struct at91_pm_data
*pm_data
);
154 extern u32 at91_pm_suspend_in_sram_sz
;
156 static int at91_suspend_finish(unsigned long val
)
161 at91_suspend_sram_fn(&pm_data
);
166 static void at91_pm_suspend(suspend_state_t state
)
168 if (pm_data
.mode
== AT91_PM_BACKUP
) {
169 pm_bu
->suspended
= 1;
171 cpu_suspend(0, at91_suspend_finish
);
173 /* The SRAM is lost between suspend cycles */
174 at91_suspend_sram_fn
= fncpy(at91_suspend_sram_fn
,
175 &at91_pm_suspend_in_sram
,
176 at91_pm_suspend_in_sram_sz
);
178 at91_suspend_finish(0);
185 * STANDBY mode has *all* drivers suspended; ignores irqs not marked as 'wakeup'
186 * event sources; and reduces DRAM power. But otherwise it's identical to
187 * PM_SUSPEND_ON: cpu idle, and nothing fancy done with main or cpu clocks.
189 * AT91_PM_SLOW_CLOCK is like STANDBY plus slow clock mode, so drivers must
190 * suspend more deeply, the master clock switches to the clk32k and turns off
191 * the main oscillator
193 * AT91_PM_BACKUP turns off the whole SoC after placing the DDR in self refresh
195 static int at91_pm_enter(suspend_state_t state
)
197 #ifdef CONFIG_PINCTRL_AT91
198 at91_pinctrl_gpio_suspend();
203 case PM_SUSPEND_STANDBY
:
205 * Ensure that clocks are in a valid state.
207 if ((pm_data
.mode
>= AT91_PM_SLOW_CLOCK
) &&
208 !at91_pm_verify_clocks())
211 at91_pm_suspend(state
);
220 pr_debug("AT91: PM - bogus suspend state %d\n", state
);
225 #ifdef CONFIG_PINCTRL_AT91
226 at91_pinctrl_gpio_resume();
232 * Called right prior to thawing processes.
234 static void at91_pm_end(void)
239 static const struct platform_suspend_ops at91_pm_ops
= {
240 .valid
= at91_pm_valid_state
,
241 .begin
= at91_pm_begin
,
242 .enter
= at91_pm_enter
,
246 static struct platform_device at91_cpuidle_device
= {
247 .name
= "cpuidle-at91",
251 * The AT91RM9200 goes into self-refresh mode with this command, and will
252 * terminate self-refresh automatically on the next SDRAM access.
254 * Self-refresh mode is exited as soon as a memory access is made, but we don't
255 * know for sure when that happens. However, we need to restore the low-power
256 * mode if it was enabled before going idle. Restoring low-power mode while
257 * still in self-refresh is "not recommended", but seems to work.
259 static void at91rm9200_standby(void)
264 "1: mcr p15, 0, %0, c7, c10, 4\n\t"
265 " str %2, [%1, %3]\n\t"
266 " mcr p15, 0, %0, c7, c0, 4\n\t"
268 : "r" (0), "r" (pm_data
.ramc
[0]),
269 "r" (1), "r" (AT91_MC_SDRAMC_SRR
));
272 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
275 static void at91_ddr_standby(void)
277 /* Those two values allow us to delay self-refresh activation
280 u32 mdr
, saved_mdr0
, saved_mdr1
= 0;
281 u32 saved_lpr0
, saved_lpr1
= 0;
283 /* LPDDR1 --> force DDR2 mode during self-refresh */
284 saved_mdr0
= at91_ramc_read(0, AT91_DDRSDRC_MDR
);
285 if ((saved_mdr0
& AT91_DDRSDRC_MD
) == AT91_DDRSDRC_MD_LOW_POWER_DDR
) {
286 mdr
= saved_mdr0
& ~AT91_DDRSDRC_MD
;
287 mdr
|= AT91_DDRSDRC_MD_DDR2
;
288 at91_ramc_write(0, AT91_DDRSDRC_MDR
, mdr
);
291 if (pm_data
.ramc
[1]) {
292 saved_lpr1
= at91_ramc_read(1, AT91_DDRSDRC_LPR
);
293 lpr1
= saved_lpr1
& ~AT91_DDRSDRC_LPCB
;
294 lpr1
|= AT91_DDRSDRC_LPCB_SELF_REFRESH
;
295 saved_mdr1
= at91_ramc_read(1, AT91_DDRSDRC_MDR
);
296 if ((saved_mdr1
& AT91_DDRSDRC_MD
) == AT91_DDRSDRC_MD_LOW_POWER_DDR
) {
297 mdr
= saved_mdr1
& ~AT91_DDRSDRC_MD
;
298 mdr
|= AT91_DDRSDRC_MD_DDR2
;
299 at91_ramc_write(1, AT91_DDRSDRC_MDR
, mdr
);
303 saved_lpr0
= at91_ramc_read(0, AT91_DDRSDRC_LPR
);
304 lpr0
= saved_lpr0
& ~AT91_DDRSDRC_LPCB
;
305 lpr0
|= AT91_DDRSDRC_LPCB_SELF_REFRESH
;
307 /* self-refresh mode now */
308 at91_ramc_write(0, AT91_DDRSDRC_LPR
, lpr0
);
310 at91_ramc_write(1, AT91_DDRSDRC_LPR
, lpr1
);
314 at91_ramc_write(0, AT91_DDRSDRC_MDR
, saved_mdr0
);
315 at91_ramc_write(0, AT91_DDRSDRC_LPR
, saved_lpr0
);
316 if (pm_data
.ramc
[1]) {
317 at91_ramc_write(0, AT91_DDRSDRC_MDR
, saved_mdr1
);
318 at91_ramc_write(1, AT91_DDRSDRC_LPR
, saved_lpr1
);
322 static void sama5d3_ddr_standby(void)
327 saved_lpr0
= at91_ramc_read(0, AT91_DDRSDRC_LPR
);
328 lpr0
= saved_lpr0
& ~AT91_DDRSDRC_LPCB
;
329 lpr0
|= AT91_DDRSDRC_LPCB_POWER_DOWN
;
331 at91_ramc_write(0, AT91_DDRSDRC_LPR
, lpr0
);
335 at91_ramc_write(0, AT91_DDRSDRC_LPR
, saved_lpr0
);
338 /* We manage both DDRAM/SDRAM controllers, we need more than one value to
341 static void at91sam9_sdram_standby(void)
344 u32 saved_lpr0
, saved_lpr1
= 0;
346 if (pm_data
.ramc
[1]) {
347 saved_lpr1
= at91_ramc_read(1, AT91_SDRAMC_LPR
);
348 lpr1
= saved_lpr1
& ~AT91_SDRAMC_LPCB
;
349 lpr1
|= AT91_SDRAMC_LPCB_SELF_REFRESH
;
352 saved_lpr0
= at91_ramc_read(0, AT91_SDRAMC_LPR
);
353 lpr0
= saved_lpr0
& ~AT91_SDRAMC_LPCB
;
354 lpr0
|= AT91_SDRAMC_LPCB_SELF_REFRESH
;
356 /* self-refresh mode now */
357 at91_ramc_write(0, AT91_SDRAMC_LPR
, lpr0
);
359 at91_ramc_write(1, AT91_SDRAMC_LPR
, lpr1
);
363 at91_ramc_write(0, AT91_SDRAMC_LPR
, saved_lpr0
);
365 at91_ramc_write(1, AT91_SDRAMC_LPR
, saved_lpr1
);
370 unsigned int memctrl
;
373 static const struct ramc_info ramc_infos
[] __initconst
= {
374 { .idle
= at91rm9200_standby
, .memctrl
= AT91_MEMCTRL_MC
},
375 { .idle
= at91sam9_sdram_standby
, .memctrl
= AT91_MEMCTRL_SDRAMC
},
376 { .idle
= at91_ddr_standby
, .memctrl
= AT91_MEMCTRL_DDRSDR
},
377 { .idle
= sama5d3_ddr_standby
, .memctrl
= AT91_MEMCTRL_DDRSDR
},
380 static const struct of_device_id ramc_ids
[] __initconst
= {
381 { .compatible
= "atmel,at91rm9200-sdramc", .data
= &ramc_infos
[0] },
382 { .compatible
= "atmel,at91sam9260-sdramc", .data
= &ramc_infos
[1] },
383 { .compatible
= "atmel,at91sam9g45-ddramc", .data
= &ramc_infos
[2] },
384 { .compatible
= "atmel,sama5d3-ddramc", .data
= &ramc_infos
[3] },
388 static __init
void at91_dt_ramc(void)
390 struct device_node
*np
;
391 const struct of_device_id
*of_id
;
393 void *standby
= NULL
;
394 const struct ramc_info
*ramc
;
396 for_each_matching_node_and_match(np
, ramc_ids
, &of_id
) {
397 pm_data
.ramc
[idx
] = of_iomap(np
, 0);
398 if (!pm_data
.ramc
[idx
])
399 panic(pr_fmt("unable to map ramc[%d] cpu registers\n"), idx
);
403 standby
= ramc
->idle
;
404 pm_data
.memctrl
= ramc
->memctrl
;
410 panic(pr_fmt("unable to find compatible ram controller node in dtb\n"));
413 pr_warn("ramc no standby function available\n");
417 at91_cpuidle_device
.dev
.platform_data
= standby
;
420 static void at91rm9200_idle(void)
423 * Disable the processor clock. The processor will be automatically
424 * re-enabled by an interrupt or by a reset.
426 writel(AT91_PMC_PCK
, pm_data
.pmc
+ AT91_PMC_SCDR
);
429 static void at91sam9_idle(void)
431 writel(AT91_PMC_PCK
, pm_data
.pmc
+ AT91_PMC_SCDR
);
435 static void __init
at91_pm_sram_init(void)
437 struct gen_pool
*sram_pool
;
438 phys_addr_t sram_pbase
;
439 unsigned long sram_base
;
440 struct device_node
*node
;
441 struct platform_device
*pdev
= NULL
;
443 for_each_compatible_node(node
, NULL
, "mmio-sram") {
444 pdev
= of_find_device_by_node(node
);
452 pr_warn("%s: failed to find sram device!\n", __func__
);
456 sram_pool
= gen_pool_get(&pdev
->dev
, NULL
);
458 pr_warn("%s: sram pool unavailable!\n", __func__
);
462 sram_base
= gen_pool_alloc(sram_pool
, at91_pm_suspend_in_sram_sz
);
464 pr_warn("%s: unable to alloc sram!\n", __func__
);
468 sram_pbase
= gen_pool_virt_to_phys(sram_pool
, sram_base
);
469 at91_suspend_sram_fn
= __arm_ioremap_exec(sram_pbase
,
470 at91_pm_suspend_in_sram_sz
, false);
471 if (!at91_suspend_sram_fn
) {
472 pr_warn("SRAM: Could not map\n");
476 /* Copy the pm suspend handler to SRAM */
477 at91_suspend_sram_fn
= fncpy(at91_suspend_sram_fn
,
478 &at91_pm_suspend_in_sram
, at91_pm_suspend_in_sram_sz
);
481 static void __init
at91_pm_backup_init(void)
483 struct gen_pool
*sram_pool
;
484 struct device_node
*np
;
485 struct platform_device
*pdev
= NULL
;
487 if ((pm_data
.standby_mode
!= AT91_PM_BACKUP
) &&
488 (pm_data
.suspend_mode
!= AT91_PM_BACKUP
))
493 np
= of_find_compatible_node(NULL
, NULL
, "atmel,sama5d2-shdwc");
495 pr_warn("%s: failed to find shdwc!\n", __func__
);
499 pm_data
.shdwc
= of_iomap(np
, 0);
502 np
= of_find_compatible_node(NULL
, NULL
, "atmel,sama5d2-sfrbu");
504 pr_warn("%s: failed to find sfrbu!\n", __func__
);
508 pm_data
.sfrbu
= of_iomap(np
, 0);
512 np
= of_find_compatible_node(NULL
, NULL
, "atmel,sama5d2-securam");
516 pdev
= of_find_device_by_node(np
);
519 pr_warn("%s: failed to find securam device!\n", __func__
);
523 sram_pool
= gen_pool_get(&pdev
->dev
, NULL
);
525 pr_warn("%s: securam pool unavailable!\n", __func__
);
529 pm_bu
= (void *)gen_pool_alloc(sram_pool
, sizeof(struct at91_pm_bu
));
531 pr_warn("%s: unable to alloc securam!\n", __func__
);
535 pm_bu
->suspended
= 0;
536 pm_bu
->canary
= __pa_symbol(&canary
);
537 pm_bu
->resume
= __pa_symbol(cpu_resume
);
542 iounmap(pm_data
.shdwc
);
543 pm_data
.shdwc
= NULL
;
545 iounmap(pm_data
.sfrbu
);
546 pm_data
.sfrbu
= NULL
;
548 if (pm_data
.standby_mode
== AT91_PM_BACKUP
)
549 pm_data
.standby_mode
= AT91_PM_SLOW_CLOCK
;
550 if (pm_data
.suspend_mode
== AT91_PM_BACKUP
)
551 pm_data
.suspend_mode
= AT91_PM_SLOW_CLOCK
;
555 unsigned long uhp_udp_mask
;
558 static const struct pmc_info pmc_infos
[] __initconst
= {
559 { .uhp_udp_mask
= AT91RM9200_PMC_UHP
| AT91RM9200_PMC_UDP
},
560 { .uhp_udp_mask
= AT91SAM926x_PMC_UHP
| AT91SAM926x_PMC_UDP
},
561 { .uhp_udp_mask
= AT91SAM926x_PMC_UHP
},
564 static const struct of_device_id atmel_pmc_ids
[] __initconst
= {
565 { .compatible
= "atmel,at91rm9200-pmc", .data
= &pmc_infos
[0] },
566 { .compatible
= "atmel,at91sam9260-pmc", .data
= &pmc_infos
[1] },
567 { .compatible
= "atmel,at91sam9g45-pmc", .data
= &pmc_infos
[2] },
568 { .compatible
= "atmel,at91sam9n12-pmc", .data
= &pmc_infos
[1] },
569 { .compatible
= "atmel,at91sam9x5-pmc", .data
= &pmc_infos
[1] },
570 { .compatible
= "atmel,sama5d3-pmc", .data
= &pmc_infos
[1] },
571 { .compatible
= "atmel,sama5d2-pmc", .data
= &pmc_infos
[1] },
575 static void __init
at91_pm_init(void (*pm_idle
)(void))
577 struct device_node
*pmc_np
;
578 const struct of_device_id
*of_id
;
579 const struct pmc_info
*pmc
;
581 if (at91_cpuidle_device
.dev
.platform_data
)
582 platform_device_register(&at91_cpuidle_device
);
584 pmc_np
= of_find_matching_node_and_match(NULL
, atmel_pmc_ids
, &of_id
);
585 pm_data
.pmc
= of_iomap(pmc_np
, 0);
587 pr_err("AT91: PM not supported, PMC not found\n");
592 pm_data
.uhp_udp_mask
= pmc
->uhp_udp_mask
;
595 arm_pm_idle
= pm_idle
;
599 if (at91_suspend_sram_fn
) {
600 suspend_set_ops(&at91_pm_ops
);
601 pr_info("AT91: PM: standby: %s, suspend: %s\n",
602 pm_modes
[pm_data
.standby_mode
].pattern
,
603 pm_modes
[pm_data
.suspend_mode
].pattern
);
605 pr_info("AT91: PM not supported, due to no SRAM allocated\n");
609 void __init
at91rm9200_pm_init(void)
611 if (!IS_ENABLED(CONFIG_SOC_AT91RM9200
))
617 * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
619 at91_ramc_write(0, AT91_MC_SDRAMC_LPR
, 0);
621 at91_pm_init(at91rm9200_idle
);
624 void __init
at91sam9_pm_init(void)
626 if (!IS_ENABLED(CONFIG_SOC_AT91SAM9
))
630 at91_pm_init(at91sam9_idle
);
633 void __init
sama5_pm_init(void)
635 if (!IS_ENABLED(CONFIG_SOC_SAMA5
))
642 void __init
sama5d2_pm_init(void)
644 if (!IS_ENABLED(CONFIG_SOC_SAMA5D2
))
647 at91_pm_backup_init();
651 static int __init
at91_pm_modes_select(char *str
)
654 substring_t args
[MAX_OPT_ARGS
];
655 int standby
, suspend
;
660 s
= strsep(&str
, ",");
661 standby
= match_token(s
, pm_modes
, args
);
665 suspend
= match_token(str
, pm_modes
, args
);
669 pm_data
.standby_mode
= standby
;
670 pm_data
.suspend_mode
= suspend
;
674 early_param("atmel.pm_modes", at91_pm_modes_select
);