blob: 667fddac38561eff3f25788ccbf9e05ac8214d84 [file] [log] [blame]
Andrew Victor907d6de2006-06-20 19:30:19 +01001/*
Andrew Victor9d041262007-02-05 11:42:07 +01002 * arch/arm/mach-at91/pm.c
Andrew Victor907d6de2006-06-20 19:30:19 +01003 * 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
Alexandre Bellonid2e46792015-01-15 15:59:25 +010013#include <linux/genalloc.h>
Alexandre Belloni9824c442017-01-31 13:49:24 +010014#include <linux/io.h>
15#include <linux/of_address.h>
Alexandre Bellonif5598d32015-01-15 15:59:24 +010016#include <linux/of.h>
Alexandre Bellonid2e46792015-01-15 15:59:25 +010017#include <linux/of_platform.h>
Alexandre Belloni7693e182017-04-26 16:31:03 +020018#include <linux/parser.h>
Alexandre Belloni9824c442017-01-31 13:49:24 +010019#include <linux/suspend.h>
20
Boris BREZILLON2edb90a2013-10-11 09:37:45 +020021#include <linux/clk/at91_pmc.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010022
Wenyou Yang385acc02015-03-09 11:54:26 +080023#include <asm/cacheflush.h>
Alexandre Belloni9824c442017-01-31 13:49:24 +010024#include <asm/fncpy.h>
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +020025#include <asm/system_misc.h>
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +020026#include <asm/suspend.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010027
Andrew Victor907d6de2006-06-20 19:30:19 +010028#include "generic.h"
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010029#include "pm.h"
Andrew Victor907d6de2006-06-20 19:30:19 +010030
Alexandre Belloni23b84082015-03-13 22:57:23 +010031/*
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 */
Ludovic Desroches84235362015-12-01 11:44:40 +010037#ifdef CONFIG_PINCTRL_AT91
Alexandre Belloni23b84082015-03-13 22:57:23 +010038extern void at91_pinctrl_gpio_suspend(void);
39extern void at91_pinctrl_gpio_resume(void);
Ludovic Desroches84235362015-12-01 11:44:40 +010040#endif
Alexandre Belloni23b84082015-03-13 22:57:23 +010041
Alexandre Belloni7693e182017-04-26 16:31:03 +020042static const match_table_t pm_modes __initconst = {
43 { 0, "standby" },
44 { AT91_PM_SLOW_CLOCK, "ulp0" },
45 { AT91_PM_BACKUP, "backup" },
46 { -1, NULL },
47};
48
49static struct at91_pm_data pm_data = {
50 .standby_mode = 0,
51 .suspend_mode = AT91_PM_SLOW_CLOCK,
52};
Alexandre Bellonif5598d32015-01-15 15:59:24 +010053
Alexandre Belloni4d767bc2017-01-31 14:08:47 +010054#define at91_ramc_read(id, field) \
Alexandre Belloni65cc1a52017-01-31 18:12:57 +010055 __raw_readl(pm_data.ramc[id] + field)
Alexandre Belloni4d767bc2017-01-31 14:08:47 +010056
57#define at91_ramc_write(id, field, value) \
Alexandre Belloni65cc1a52017-01-31 18:12:57 +010058 __raw_writel(value, pm_data.ramc[id] + field)
Daniel Lezcano5ad945e2013-09-22 22:29:57 +020059
Andrew Victor907d6de2006-06-20 19:30:19 +010060static int at91_pm_valid_state(suspend_state_t state)
61{
62 switch (state) {
63 case PM_SUSPEND_ON:
64 case PM_SUSPEND_STANDBY:
65 case PM_SUSPEND_MEM:
66 return 1;
67
68 default:
69 return 0;
70 }
71}
72
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +020073static int canary = 0xA5A5A5A5;
Andrew Victor907d6de2006-06-20 19:30:19 +010074
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +020075static struct at91_pm_bu {
76 int suspended;
77 unsigned long reserved;
78 phys_addr_t canary;
79 phys_addr_t resume;
80} *pm_bu;
Andrew Victor907d6de2006-06-20 19:30:19 +010081
82/*
83 * Called after processes are frozen, but before we shutdown devices.
84 */
Rafael J. Wysockic697eec2008-01-08 00:04:17 +010085static int at91_pm_begin(suspend_state_t state)
Andrew Victor907d6de2006-06-20 19:30:19 +010086{
Alexandre Belloni7693e182017-04-26 16:31:03 +020087 switch (state) {
88 case PM_SUSPEND_MEM:
89 pm_data.mode = pm_data.suspend_mode;
90 break;
91
92 case PM_SUSPEND_STANDBY:
93 pm_data.mode = pm_data.standby_mode;
94 break;
95
96 default:
97 pm_data.mode = -1;
98 }
99
Andrew Victor907d6de2006-06-20 19:30:19 +0100100 return 0;
101}
102
103/*
104 * Verify that all the clocks are correct before entering
105 * slow-clock mode.
106 */
107static int at91_pm_verify_clocks(void)
108{
109 unsigned long scsr;
110 int i;
111
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100112 scsr = readl(pm_data.pmc + AT91_PMC_SCSR);
Andrew Victor907d6de2006-06-20 19:30:19 +0100113
114 /* USB must not be using PLLB */
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100115 if ((scsr & pm_data.uhp_udp_mask) != 0) {
Alexandre Bellonif5598d32015-01-15 15:59:24 +0100116 pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
117 return 0;
Andrew Victor907d6de2006-06-20 19:30:19 +0100118 }
119
Andrew Victor907d6de2006-06-20 19:30:19 +0100120 /* PCK0..PCK3 must be disabled, or configured to use clk32k */
121 for (i = 0; i < 4; i++) {
122 u32 css;
123
124 if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
125 continue;
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100126 css = readl(pm_data.pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
Andrew Victor907d6de2006-06-20 19:30:19 +0100127 if (css != AT91_PMC_CSS_SLOW) {
Ryan Mallon7f96b1c2009-04-01 20:33:30 +0100128 pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
Andrew Victor907d6de2006-06-20 19:30:19 +0100129 return 0;
130 }
131 }
Andrew Victor907d6de2006-06-20 19:30:19 +0100132
133 return 1;
134}
135
136/*
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.
141 *
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.
145 */
146int at91_suspend_entering_slow_clock(void)
147{
Alexandre Belloni7693e182017-04-26 16:31:03 +0200148 return (pm_data.mode >= AT91_PM_SLOW_CLOCK);
Andrew Victor907d6de2006-06-20 19:30:19 +0100149}
150EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
151
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100152static void (*at91_suspend_sram_fn)(struct at91_pm_data *);
153extern void at91_pm_suspend_in_sram(struct at91_pm_data *pm_data);
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800154extern u32 at91_pm_suspend_in_sram_sz;
Andrew Victorf5d0f452008-04-02 21:50:16 +0100155
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200156static int at91_suspend_finish(unsigned long val)
Wenyou Yang23be4be2015-03-09 11:49:46 +0800157{
Wenyou Yang385acc02015-03-09 11:54:26 +0800158 flush_cache_all();
159 outer_disable();
160
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100161 at91_suspend_sram_fn(&pm_data);
Wenyou Yang385acc02015-03-09 11:54:26 +0800162
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200163 return 0;
164}
165
166static void at91_pm_suspend(suspend_state_t state)
167{
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200168 if (pm_data.mode == AT91_PM_BACKUP) {
169 pm_bu->suspended = 1;
170
171 cpu_suspend(0, at91_suspend_finish);
172
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);
177 } else {
178 at91_suspend_finish(0);
179 }
180
Wenyou Yang385acc02015-03-09 11:54:26 +0800181 outer_resume();
Wenyou Yang23be4be2015-03-09 11:49:46 +0800182}
183
Alexandre Belloni7693e182017-04-26 16:31:03 +0200184/*
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.
188 *
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
192 *
193 * AT91_PM_BACKUP turns off the whole SoC after placing the DDR in self refresh
194 */
Andrew Victor907d6de2006-06-20 19:30:19 +0100195static int at91_pm_enter(suspend_state_t state)
196{
Ludovic Desroches84235362015-12-01 11:44:40 +0100197#ifdef CONFIG_PINCTRL_AT91
Arnd Bergmann85c4b312014-12-02 12:08:27 +0100198 at91_pinctrl_gpio_suspend();
Ludovic Desroches84235362015-12-01 11:44:40 +0100199#endif
Alexandre Belloni7693e182017-04-26 16:31:03 +0200200
Andrew Victor907d6de2006-06-20 19:30:19 +0100201 switch (state) {
Wenyou Yang23be4be2015-03-09 11:49:46 +0800202 case PM_SUSPEND_MEM:
Alexandre Belloni7693e182017-04-26 16:31:03 +0200203 case PM_SUSPEND_STANDBY:
Andrew Victor907d6de2006-06-20 19:30:19 +0100204 /*
Wenyou Yang23be4be2015-03-09 11:49:46 +0800205 * Ensure that clocks are in a valid state.
Andrew Victor907d6de2006-06-20 19:30:19 +0100206 */
Alexandre Belloni7693e182017-04-26 16:31:03 +0200207 if ((pm_data.mode >= AT91_PM_SLOW_CLOCK) &&
208 !at91_pm_verify_clocks())
Andrew Victor907d6de2006-06-20 19:30:19 +0100209 goto error;
Wenyou Yang23be4be2015-03-09 11:49:46 +0800210
211 at91_pm_suspend(state);
212
213 break;
214
Wenyou Yang23be4be2015-03-09 11:49:46 +0800215 case PM_SUSPEND_ON:
216 cpu_do_idle();
217 break;
218
219 default:
220 pr_debug("AT91: PM - bogus suspend state %d\n", state);
221 goto error;
Andrew Victor907d6de2006-06-20 19:30:19 +0100222 }
223
Andrew Victor907d6de2006-06-20 19:30:19 +0100224error:
Ludovic Desroches84235362015-12-01 11:44:40 +0100225#ifdef CONFIG_PINCTRL_AT91
Arnd Bergmann85c4b312014-12-02 12:08:27 +0100226 at91_pinctrl_gpio_resume();
Ludovic Desroches84235362015-12-01 11:44:40 +0100227#endif
Andrew Victor907d6de2006-06-20 19:30:19 +0100228 return 0;
229}
230
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100231/*
232 * Called right prior to thawing processes.
233 */
234static void at91_pm_end(void)
235{
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100236}
237
Andrew Victor907d6de2006-06-20 19:30:19 +0100238
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100239static const struct platform_suspend_ops at91_pm_ops = {
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100240 .valid = at91_pm_valid_state,
241 .begin = at91_pm_begin,
242 .enter = at91_pm_enter,
243 .end = at91_pm_end,
Andrew Victor907d6de2006-06-20 19:30:19 +0100244};
245
Daniel Lezcano5ad945e2013-09-22 22:29:57 +0200246static struct platform_device at91_cpuidle_device = {
247 .name = "cpuidle-at91",
248};
249
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100250/*
251 * The AT91RM9200 goes into self-refresh mode with this command, and will
252 * terminate self-refresh automatically on the next SDRAM access.
253 *
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.
258 */
259static void at91rm9200_standby(void)
260{
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100261 asm volatile(
262 "b 1f\n\t"
263 ".align 5\n\t"
264 "1: mcr p15, 0, %0, c7, c10, 4\n\t"
Alexandre Belloni5a2d4f02017-02-01 21:32:43 +0100265 " str %2, [%1, %3]\n\t"
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100266 " mcr p15, 0, %0, c7, c0, 4\n\t"
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100267 :
Alexandre Belloni5a2d4f02017-02-01 21:32:43 +0100268 : "r" (0), "r" (pm_data.ramc[0]),
269 "r" (1), "r" (AT91_MC_SDRAMC_SRR));
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100270}
271
272/* We manage both DDRAM/SDRAM controllers, we need more than one value to
273 * remember.
274 */
275static void at91_ddr_standby(void)
276{
277 /* Those two values allow us to delay self-refresh activation
278 * to the maximum. */
279 u32 lpr0, lpr1 = 0;
Alexandre Belloni56387632017-02-01 22:10:34 +0100280 u32 mdr, saved_mdr0, saved_mdr1 = 0;
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100281 u32 saved_lpr0, saved_lpr1 = 0;
282
Alexandre Belloni56387632017-02-01 22:10:34 +0100283 /* 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);
289 }
290
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100291 if (pm_data.ramc[1]) {
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100292 saved_lpr1 = at91_ramc_read(1, AT91_DDRSDRC_LPR);
293 lpr1 = saved_lpr1 & ~AT91_DDRSDRC_LPCB;
294 lpr1 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
Alexandre Belloni56387632017-02-01 22:10:34 +0100295 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);
300 }
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100301 }
302
303 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
304 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
305 lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
306
307 /* self-refresh mode now */
308 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100309 if (pm_data.ramc[1])
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100310 at91_ramc_write(1, AT91_DDRSDRC_LPR, lpr1);
311
312 cpu_do_idle();
313
Alexandre Belloni56387632017-02-01 22:10:34 +0100314 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr0);
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100315 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
Alexandre Belloni56387632017-02-01 22:10:34 +0100316 if (pm_data.ramc[1]) {
317 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr1);
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100318 at91_ramc_write(1, AT91_DDRSDRC_LPR, saved_lpr1);
Alexandre Belloni56387632017-02-01 22:10:34 +0100319 }
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100320}
321
Nicolas Ferre60b89f12017-03-14 09:38:04 +0100322static void sama5d3_ddr_standby(void)
323{
324 u32 lpr0;
325 u32 saved_lpr0;
326
327 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
328 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
329 lpr0 |= AT91_DDRSDRC_LPCB_POWER_DOWN;
330
331 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
332
333 cpu_do_idle();
334
335 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
336}
337
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100338/* We manage both DDRAM/SDRAM controllers, we need more than one value to
339 * remember.
340 */
341static void at91sam9_sdram_standby(void)
342{
343 u32 lpr0, lpr1 = 0;
344 u32 saved_lpr0, saved_lpr1 = 0;
345
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100346 if (pm_data.ramc[1]) {
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100347 saved_lpr1 = at91_ramc_read(1, AT91_SDRAMC_LPR);
348 lpr1 = saved_lpr1 & ~AT91_SDRAMC_LPCB;
349 lpr1 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
350 }
351
352 saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
353 lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
354 lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
355
356 /* self-refresh mode now */
357 at91_ramc_write(0, AT91_SDRAMC_LPR, lpr0);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100358 if (pm_data.ramc[1])
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100359 at91_ramc_write(1, AT91_SDRAMC_LPR, lpr1);
360
361 cpu_do_idle();
362
363 at91_ramc_write(0, AT91_SDRAMC_LPR, saved_lpr0);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100364 if (pm_data.ramc[1])
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100365 at91_ramc_write(1, AT91_SDRAMC_LPR, saved_lpr1);
366}
367
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100368struct ramc_info {
369 void (*idle)(void);
370 unsigned int memctrl;
371};
372
373static 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},
378};
379
Arnd Bergmann05278732017-05-11 13:50:16 +0200380static const struct of_device_id ramc_ids[] __initconst = {
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100381 { .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] },
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100385 { /*sentinel*/ }
386};
387
Uwe Kleine-König444d2d32015-02-18 21:19:56 +0100388static __init void at91_dt_ramc(void)
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100389{
390 struct device_node *np;
391 const struct of_device_id *of_id;
392 int idx = 0;
Alexandre Bellonie56d75a2017-02-01 23:55:10 +0100393 void *standby = NULL;
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100394 const struct ramc_info *ramc;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100395
396 for_each_matching_node_and_match(np, ramc_ids, &of_id) {
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100397 pm_data.ramc[idx] = of_iomap(np, 0);
398 if (!pm_data.ramc[idx])
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100399 panic(pr_fmt("unable to map ramc[%d] cpu registers\n"), idx);
400
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100401 ramc = of_id->data;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100402 if (!standby)
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100403 standby = ramc->idle;
404 pm_data.memctrl = ramc->memctrl;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100405
406 idx++;
407 }
408
409 if (!idx)
410 panic(pr_fmt("unable to find compatible ram controller node in dtb\n"));
411
412 if (!standby) {
413 pr_warn("ramc no standby function available\n");
414 return;
415 }
416
Alexandre Bellonie56d75a2017-02-01 23:55:10 +0100417 at91_cpuidle_device.dev.platform_data = standby;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100418}
419
Ben Dooksab6778e2016-06-17 16:34:18 +0100420static void at91rm9200_idle(void)
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200421{
422 /*
423 * Disable the processor clock. The processor will be automatically
424 * re-enabled by an interrupt or by a reset.
425 */
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100426 writel(AT91_PMC_PCK, pm_data.pmc + AT91_PMC_SCDR);
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200427}
428
Ben Dooksab6778e2016-06-17 16:34:18 +0100429static void at91sam9_idle(void)
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200430{
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100431 writel(AT91_PMC_PCK, pm_data.pmc + AT91_PMC_SCDR);
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200432 cpu_do_idle();
433}
434
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100435static void __init at91_pm_sram_init(void)
436{
437 struct gen_pool *sram_pool;
438 phys_addr_t sram_pbase;
439 unsigned long sram_base;
440 struct device_node *node;
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100441 struct platform_device *pdev = NULL;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100442
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100443 for_each_compatible_node(node, NULL, "mmio-sram") {
444 pdev = of_find_device_by_node(node);
445 if (pdev) {
446 of_node_put(node);
447 break;
448 }
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100449 }
450
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100451 if (!pdev) {
452 pr_warn("%s: failed to find sram device!\n", __func__);
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100453 return;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100454 }
455
Vladimir Zapolskiy73858172015-09-04 15:47:43 -0700456 sram_pool = gen_pool_get(&pdev->dev, NULL);
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100457 if (!sram_pool) {
458 pr_warn("%s: sram pool unavailable!\n", __func__);
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100459 return;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100460 }
461
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800462 sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100463 if (!sram_base) {
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800464 pr_warn("%s: unable to alloc sram!\n", __func__);
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100465 return;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100466 }
467
468 sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800469 at91_suspend_sram_fn = __arm_ioremap_exec(sram_pbase,
470 at91_pm_suspend_in_sram_sz, false);
471 if (!at91_suspend_sram_fn) {
Wenyou Yangd94e6882015-03-09 11:49:01 +0800472 pr_warn("SRAM: Could not map\n");
473 return;
474 }
475
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800476 /* 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);
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100479}
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100480
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200481static void __init at91_pm_backup_init(void)
482{
483 struct gen_pool *sram_pool;
484 struct device_node *np;
485 struct platform_device *pdev = NULL;
486
Alexandre Belloni7693e182017-04-26 16:31:03 +0200487 if ((pm_data.standby_mode != AT91_PM_BACKUP) &&
488 (pm_data.suspend_mode != AT91_PM_BACKUP))
489 return;
490
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200491 pm_bu = NULL;
492
493 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-shdwc");
494 if (!np) {
495 pr_warn("%s: failed to find shdwc!\n", __func__);
496 return;
497 }
498
499 pm_data.shdwc = of_iomap(np, 0);
500 of_node_put(np);
501
502 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-sfrbu");
503 if (!np) {
504 pr_warn("%s: failed to find sfrbu!\n", __func__);
505 goto sfrbu_fail;
506 }
507
508 pm_data.sfrbu = of_iomap(np, 0);
509 of_node_put(np);
510 pm_bu = NULL;
511
512 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
513 if (!np)
514 goto securam_fail;
515
516 pdev = of_find_device_by_node(np);
517 of_node_put(np);
518 if (!pdev) {
519 pr_warn("%s: failed to find securam device!\n", __func__);
520 goto securam_fail;
521 }
522
523 sram_pool = gen_pool_get(&pdev->dev, NULL);
524 if (!sram_pool) {
525 pr_warn("%s: securam pool unavailable!\n", __func__);
526 goto securam_fail;
527 }
528
529 pm_bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
530 if (!pm_bu) {
531 pr_warn("%s: unable to alloc securam!\n", __func__);
532 goto securam_fail;
533 }
534
535 pm_bu->suspended = 0;
536 pm_bu->canary = virt_to_phys(&canary);
537 pm_bu->resume = virt_to_phys(cpu_resume);
538
539 return;
540
541sfrbu_fail:
542 iounmap(pm_data.shdwc);
543 pm_data.shdwc = NULL;
544securam_fail:
545 iounmap(pm_data.sfrbu);
546 pm_data.sfrbu = NULL;
Alexandre Belloni28732232017-04-26 16:34:24 +0200547
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;
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200552}
553
Alexandre Belloni13f16012017-02-01 23:35:18 +0100554struct pmc_info {
555 unsigned long uhp_udp_mask;
556};
557
558static 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 },
562};
563
Alexandre Belloni5737b732015-09-30 01:31:34 +0200564static const struct of_device_id atmel_pmc_ids[] __initconst = {
Alexandre Belloni13f16012017-02-01 23:35:18 +0100565 { .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] },
Alexandre Belloni5737b732015-09-30 01:31:34 +0200572 { /* sentinel */ },
573};
574
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200575static void __init at91_pm_init(void (*pm_idle)(void))
Andrew Victor907d6de2006-06-20 19:30:19 +0100576{
Alexandre Belloni5737b732015-09-30 01:31:34 +0200577 struct device_node *pmc_np;
Alexandre Belloni13f16012017-02-01 23:35:18 +0100578 const struct of_device_id *of_id;
579 const struct pmc_info *pmc;
Andrew Victorf5d0f452008-04-02 21:50:16 +0100580
Daniel Lezcano5ad945e2013-09-22 22:29:57 +0200581 if (at91_cpuidle_device.dev.platform_data)
582 platform_device_register(&at91_cpuidle_device);
Andrew Victor907d6de2006-06-20 19:30:19 +0100583
Alexandre Belloni13f16012017-02-01 23:35:18 +0100584 pmc_np = of_find_matching_node_and_match(NULL, atmel_pmc_ids, &of_id);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100585 pm_data.pmc = of_iomap(pmc_np, 0);
586 if (!pm_data.pmc) {
Alexandre Belloni5737b732015-09-30 01:31:34 +0200587 pr_err("AT91: PM not supported, PMC not found\n");
588 return;
589 }
590
Alexandre Belloni13f16012017-02-01 23:35:18 +0100591 pmc = of_id->data;
592 pm_data.uhp_udp_mask = pmc->uhp_udp_mask;
593
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200594 if (pm_idle)
595 arm_pm_idle = pm_idle;
596
Alexandre Belloni5737b732015-09-30 01:31:34 +0200597 at91_pm_sram_init();
598
Alexandre Belloni7693e182017-04-26 16:31:03 +0200599 if (at91_suspend_sram_fn) {
Wenyou Yangd94e6882015-03-09 11:49:01 +0800600 suspend_set_ops(&at91_pm_ops);
Alexandre Belloni7693e182017-04-26 16:31:03 +0200601 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);
604 } else {
Wenyou Yangd94e6882015-03-09 11:49:01 +0800605 pr_info("AT91: PM not supported, due to no SRAM allocated\n");
Alexandre Belloni7693e182017-04-26 16:31:03 +0200606 }
Andrew Victor907d6de2006-06-20 19:30:19 +0100607}
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100608
Nicolas Ferread3fc3e2015-01-27 18:41:33 +0100609void __init at91rm9200_pm_init(void)
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100610{
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100611 at91_dt_ramc();
612
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100613 /*
614 * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
615 */
Alexandre Bellonid7d45f22015-03-16 15:14:50 +0100616 at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100617
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200618 at91_pm_init(at91rm9200_idle);
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100619}
620
Alexandre Belloni13469192017-02-01 23:43:03 +0100621void __init at91sam9_pm_init(void)
Nicolas Ferrebf022802015-01-22 16:54:50 +0100622{
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100623 at91_dt_ramc();
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200624 at91_pm_init(at91sam9_idle);
625}
626
627void __init sama5_pm_init(void)
628{
629 at91_dt_ramc();
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200630 at91_pm_init(NULL);
Nicolas Ferrebf022802015-01-22 16:54:50 +0100631}
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200632
633void __init sama5d2_pm_init(void)
634{
635 at91_pm_backup_init();
636 sama5_pm_init();
637}
Alexandre Belloni7693e182017-04-26 16:31:03 +0200638
639static int __init at91_pm_modes_select(char *str)
640{
641 char *s;
642 substring_t args[MAX_OPT_ARGS];
643 int standby, suspend;
644
645 if (!str)
646 return 0;
647
648 s = strsep(&str, ",");
649 standby = match_token(s, pm_modes, args);
650 if (standby < 0)
651 return 0;
652
653 suspend = match_token(str, pm_modes, args);
654 if (suspend < 0)
655 return 0;
656
657 pm_data.standby_mode = standby;
658 pm_data.suspend_mode = suspend;
659
660 return 0;
661}
662early_param("atmel.pm_modes", at91_pm_modes_select);