blob: d138d19fa9f0e2404b30ef716d807b4cee302d54 [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 Belloni9824c442017-01-31 13:49:24 +010018#include <linux/suspend.h>
19
Boris BREZILLON2edb90a2013-10-11 09:37:45 +020020#include <linux/clk/at91_pmc.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010021
Wenyou Yang385acc02015-03-09 11:54:26 +080022#include <asm/cacheflush.h>
Alexandre Belloni9824c442017-01-31 13:49:24 +010023#include <asm/fncpy.h>
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +020024#include <asm/system_misc.h>
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +020025#include <asm/suspend.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010026
Andrew Victor907d6de2006-06-20 19:30:19 +010027#include "generic.h"
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010028#include "pm.h"
Andrew Victor907d6de2006-06-20 19:30:19 +010029
Alexandre Belloni23b84082015-03-13 22:57:23 +010030/*
31 * FIXME: this is needed to communicate between the pinctrl driver and
32 * the PM implementation in the machine. Possibly part of the PM
33 * implementation should be moved down into the pinctrl driver and get
34 * called as part of the generic suspend/resume path.
35 */
Ludovic Desroches84235362015-12-01 11:44:40 +010036#ifdef CONFIG_PINCTRL_AT91
Alexandre Belloni23b84082015-03-13 22:57:23 +010037extern void at91_pinctrl_gpio_suspend(void);
38extern void at91_pinctrl_gpio_resume(void);
Ludovic Desroches84235362015-12-01 11:44:40 +010039#endif
Alexandre Belloni23b84082015-03-13 22:57:23 +010040
Alexandre Belloni65cc1a52017-01-31 18:12:57 +010041static struct at91_pm_data pm_data;
Alexandre Bellonif5598d32015-01-15 15:59:24 +010042
Alexandre Belloni4d767bc2017-01-31 14:08:47 +010043#define at91_ramc_read(id, field) \
Alexandre Belloni65cc1a52017-01-31 18:12:57 +010044 __raw_readl(pm_data.ramc[id] + field)
Alexandre Belloni4d767bc2017-01-31 14:08:47 +010045
46#define at91_ramc_write(id, field, value) \
Alexandre Belloni65cc1a52017-01-31 18:12:57 +010047 __raw_writel(value, pm_data.ramc[id] + field)
Daniel Lezcano5ad945e2013-09-22 22:29:57 +020048
Andrew Victor907d6de2006-06-20 19:30:19 +010049static int at91_pm_valid_state(suspend_state_t state)
50{
51 switch (state) {
52 case PM_SUSPEND_ON:
53 case PM_SUSPEND_STANDBY:
54 case PM_SUSPEND_MEM:
55 return 1;
56
57 default:
58 return 0;
59 }
60}
61
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +020062static int canary = 0xA5A5A5A5;
63
64static struct at91_pm_bu {
65 int suspended;
66 unsigned long reserved;
67 phys_addr_t canary;
68 phys_addr_t resume;
69} *pm_bu;
Andrew Victor907d6de2006-06-20 19:30:19 +010070
71static suspend_state_t target_state;
72
73/*
74 * Called after processes are frozen, but before we shutdown devices.
75 */
Rafael J. Wysockic697eec2008-01-08 00:04:17 +010076static int at91_pm_begin(suspend_state_t state)
Andrew Victor907d6de2006-06-20 19:30:19 +010077{
78 target_state = state;
79 return 0;
80}
81
82/*
83 * Verify that all the clocks are correct before entering
84 * slow-clock mode.
85 */
86static int at91_pm_verify_clocks(void)
87{
88 unsigned long scsr;
89 int i;
90
Alexandre Belloni65cc1a52017-01-31 18:12:57 +010091 scsr = readl(pm_data.pmc + AT91_PMC_SCSR);
Andrew Victor907d6de2006-06-20 19:30:19 +010092
93 /* USB must not be using PLLB */
Alexandre Belloni65cc1a52017-01-31 18:12:57 +010094 if ((scsr & pm_data.uhp_udp_mask) != 0) {
Alexandre Bellonif5598d32015-01-15 15:59:24 +010095 pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
96 return 0;
Andrew Victor907d6de2006-06-20 19:30:19 +010097 }
98
Andrew Victor907d6de2006-06-20 19:30:19 +010099 /* PCK0..PCK3 must be disabled, or configured to use clk32k */
100 for (i = 0; i < 4; i++) {
101 u32 css;
102
103 if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
104 continue;
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100105 css = readl(pm_data.pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
Andrew Victor907d6de2006-06-20 19:30:19 +0100106 if (css != AT91_PMC_CSS_SLOW) {
Ryan Mallon7f96b1c2009-04-01 20:33:30 +0100107 pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
Andrew Victor907d6de2006-06-20 19:30:19 +0100108 return 0;
109 }
110 }
Andrew Victor907d6de2006-06-20 19:30:19 +0100111
112 return 1;
113}
114
115/*
116 * Call this from platform driver suspend() to see how deeply to suspend.
117 * For example, some controllers (like OHCI) need one of the PLL clocks
118 * in order to act as a wakeup source, and those are not available when
119 * going into slow clock mode.
120 *
121 * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
122 * the very same problem (but not using at91 main_clk), and it'd be better
123 * to add one generic API rather than lots of platform-specific ones.
124 */
125int at91_suspend_entering_slow_clock(void)
126{
127 return (target_state == PM_SUSPEND_MEM);
128}
129EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
130
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100131static void (*at91_suspend_sram_fn)(struct at91_pm_data *);
132extern void at91_pm_suspend_in_sram(struct at91_pm_data *pm_data);
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800133extern u32 at91_pm_suspend_in_sram_sz;
Andrew Victorf5d0f452008-04-02 21:50:16 +0100134
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200135static int at91_suspend_finish(unsigned long val)
Wenyou Yang23be4be2015-03-09 11:49:46 +0800136{
Wenyou Yang385acc02015-03-09 11:54:26 +0800137 flush_cache_all();
138 outer_disable();
139
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100140 at91_suspend_sram_fn(&pm_data);
Wenyou Yang385acc02015-03-09 11:54:26 +0800141
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200142 return 0;
143}
144
145static void at91_pm_suspend(suspend_state_t state)
146{
147 if (pm_data.deepest_state == AT91_PM_BACKUP)
148 if (state == PM_SUSPEND_MEM)
149 pm_data.mode = AT91_PM_BACKUP;
150 else
151 pm_data.mode = AT91_PM_SLOW_CLOCK;
152 else
153 pm_data.mode = (state == PM_SUSPEND_MEM) ? AT91_PM_SLOW_CLOCK : 0;
154
155 if (pm_data.mode == AT91_PM_BACKUP) {
156 pm_bu->suspended = 1;
157
158 cpu_suspend(0, at91_suspend_finish);
159
160 /* The SRAM is lost between suspend cycles */
161 at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
162 &at91_pm_suspend_in_sram,
163 at91_pm_suspend_in_sram_sz);
164 } else {
165 at91_suspend_finish(0);
166 }
167
Wenyou Yang385acc02015-03-09 11:54:26 +0800168 outer_resume();
Wenyou Yang23be4be2015-03-09 11:49:46 +0800169}
170
Andrew Victor907d6de2006-06-20 19:30:19 +0100171static int at91_pm_enter(suspend_state_t state)
172{
Ludovic Desroches84235362015-12-01 11:44:40 +0100173#ifdef CONFIG_PINCTRL_AT91
Arnd Bergmann85c4b312014-12-02 12:08:27 +0100174 at91_pinctrl_gpio_suspend();
Ludovic Desroches84235362015-12-01 11:44:40 +0100175#endif
Andrew Victor907d6de2006-06-20 19:30:19 +0100176 switch (state) {
Wenyou Yang23be4be2015-03-09 11:49:46 +0800177 /*
178 * Suspend-to-RAM is like STANDBY plus slow clock mode, so
179 * drivers must suspend more deeply, the master clock switches
180 * to the clk32k and turns off the main oscillator
181 */
182 case PM_SUSPEND_MEM:
Andrew Victor907d6de2006-06-20 19:30:19 +0100183 /*
Wenyou Yang23be4be2015-03-09 11:49:46 +0800184 * Ensure that clocks are in a valid state.
Andrew Victor907d6de2006-06-20 19:30:19 +0100185 */
Wenyou Yang23be4be2015-03-09 11:49:46 +0800186 if (!at91_pm_verify_clocks())
Andrew Victor907d6de2006-06-20 19:30:19 +0100187 goto error;
Wenyou Yang23be4be2015-03-09 11:49:46 +0800188
189 at91_pm_suspend(state);
190
191 break;
192
193 /*
194 * STANDBY mode has *all* drivers suspended; ignores irqs not
195 * marked as 'wakeup' event sources; and reduces DRAM power.
196 * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
197 * nothing fancy done with main or cpu clocks.
198 */
199 case PM_SUSPEND_STANDBY:
200 at91_pm_suspend(state);
201 break;
202
203 case PM_SUSPEND_ON:
204 cpu_do_idle();
205 break;
206
207 default:
208 pr_debug("AT91: PM - bogus suspend state %d\n", state);
209 goto error;
Andrew Victor907d6de2006-06-20 19:30:19 +0100210 }
211
Andrew Victor907d6de2006-06-20 19:30:19 +0100212error:
213 target_state = PM_SUSPEND_ON;
Boris BREZILLON07192602014-07-10 19:14:20 +0200214
Ludovic Desroches84235362015-12-01 11:44:40 +0100215#ifdef CONFIG_PINCTRL_AT91
Arnd Bergmann85c4b312014-12-02 12:08:27 +0100216 at91_pinctrl_gpio_resume();
Ludovic Desroches84235362015-12-01 11:44:40 +0100217#endif
Andrew Victor907d6de2006-06-20 19:30:19 +0100218 return 0;
219}
220
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100221/*
222 * Called right prior to thawing processes.
223 */
224static void at91_pm_end(void)
225{
226 target_state = PM_SUSPEND_ON;
227}
228
Andrew Victor907d6de2006-06-20 19:30:19 +0100229
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100230static const struct platform_suspend_ops at91_pm_ops = {
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100231 .valid = at91_pm_valid_state,
232 .begin = at91_pm_begin,
233 .enter = at91_pm_enter,
234 .end = at91_pm_end,
Andrew Victor907d6de2006-06-20 19:30:19 +0100235};
236
Daniel Lezcano5ad945e2013-09-22 22:29:57 +0200237static struct platform_device at91_cpuidle_device = {
238 .name = "cpuidle-at91",
239};
240
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100241/*
242 * The AT91RM9200 goes into self-refresh mode with this command, and will
243 * terminate self-refresh automatically on the next SDRAM access.
244 *
245 * Self-refresh mode is exited as soon as a memory access is made, but we don't
246 * know for sure when that happens. However, we need to restore the low-power
247 * mode if it was enabled before going idle. Restoring low-power mode while
248 * still in self-refresh is "not recommended", but seems to work.
249 */
250static void at91rm9200_standby(void)
251{
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100252 asm volatile(
253 "b 1f\n\t"
254 ".align 5\n\t"
255 "1: mcr p15, 0, %0, c7, c10, 4\n\t"
Alexandre Belloni5a2d4f02017-02-01 21:32:43 +0100256 " str %2, [%1, %3]\n\t"
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100257 " mcr p15, 0, %0, c7, c0, 4\n\t"
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100258 :
Alexandre Belloni5a2d4f02017-02-01 21:32:43 +0100259 : "r" (0), "r" (pm_data.ramc[0]),
260 "r" (1), "r" (AT91_MC_SDRAMC_SRR));
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100261}
262
263/* We manage both DDRAM/SDRAM controllers, we need more than one value to
264 * remember.
265 */
266static void at91_ddr_standby(void)
267{
268 /* Those two values allow us to delay self-refresh activation
269 * to the maximum. */
270 u32 lpr0, lpr1 = 0;
Alexandre Belloni56387632017-02-01 22:10:34 +0100271 u32 mdr, saved_mdr0, saved_mdr1 = 0;
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100272 u32 saved_lpr0, saved_lpr1 = 0;
273
Alexandre Belloni56387632017-02-01 22:10:34 +0100274 /* LPDDR1 --> force DDR2 mode during self-refresh */
275 saved_mdr0 = at91_ramc_read(0, AT91_DDRSDRC_MDR);
276 if ((saved_mdr0 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
277 mdr = saved_mdr0 & ~AT91_DDRSDRC_MD;
278 mdr |= AT91_DDRSDRC_MD_DDR2;
279 at91_ramc_write(0, AT91_DDRSDRC_MDR, mdr);
280 }
281
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100282 if (pm_data.ramc[1]) {
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100283 saved_lpr1 = at91_ramc_read(1, AT91_DDRSDRC_LPR);
284 lpr1 = saved_lpr1 & ~AT91_DDRSDRC_LPCB;
285 lpr1 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
Alexandre Belloni56387632017-02-01 22:10:34 +0100286 saved_mdr1 = at91_ramc_read(1, AT91_DDRSDRC_MDR);
287 if ((saved_mdr1 & AT91_DDRSDRC_MD) == AT91_DDRSDRC_MD_LOW_POWER_DDR) {
288 mdr = saved_mdr1 & ~AT91_DDRSDRC_MD;
289 mdr |= AT91_DDRSDRC_MD_DDR2;
290 at91_ramc_write(1, AT91_DDRSDRC_MDR, mdr);
291 }
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100292 }
293
294 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
295 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
296 lpr0 |= AT91_DDRSDRC_LPCB_SELF_REFRESH;
297
298 /* self-refresh mode now */
299 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100300 if (pm_data.ramc[1])
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100301 at91_ramc_write(1, AT91_DDRSDRC_LPR, lpr1);
302
303 cpu_do_idle();
304
Alexandre Belloni56387632017-02-01 22:10:34 +0100305 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr0);
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100306 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
Alexandre Belloni56387632017-02-01 22:10:34 +0100307 if (pm_data.ramc[1]) {
308 at91_ramc_write(0, AT91_DDRSDRC_MDR, saved_mdr1);
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100309 at91_ramc_write(1, AT91_DDRSDRC_LPR, saved_lpr1);
Alexandre Belloni56387632017-02-01 22:10:34 +0100310 }
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100311}
312
Nicolas Ferre60b89f12017-03-14 09:38:04 +0100313static void sama5d3_ddr_standby(void)
314{
315 u32 lpr0;
316 u32 saved_lpr0;
317
318 saved_lpr0 = at91_ramc_read(0, AT91_DDRSDRC_LPR);
319 lpr0 = saved_lpr0 & ~AT91_DDRSDRC_LPCB;
320 lpr0 |= AT91_DDRSDRC_LPCB_POWER_DOWN;
321
322 at91_ramc_write(0, AT91_DDRSDRC_LPR, lpr0);
323
324 cpu_do_idle();
325
326 at91_ramc_write(0, AT91_DDRSDRC_LPR, saved_lpr0);
327}
328
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100329/* We manage both DDRAM/SDRAM controllers, we need more than one value to
330 * remember.
331 */
332static void at91sam9_sdram_standby(void)
333{
334 u32 lpr0, lpr1 = 0;
335 u32 saved_lpr0, saved_lpr1 = 0;
336
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100337 if (pm_data.ramc[1]) {
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100338 saved_lpr1 = at91_ramc_read(1, AT91_SDRAMC_LPR);
339 lpr1 = saved_lpr1 & ~AT91_SDRAMC_LPCB;
340 lpr1 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
341 }
342
343 saved_lpr0 = at91_ramc_read(0, AT91_SDRAMC_LPR);
344 lpr0 = saved_lpr0 & ~AT91_SDRAMC_LPCB;
345 lpr0 |= AT91_SDRAMC_LPCB_SELF_REFRESH;
346
347 /* self-refresh mode now */
348 at91_ramc_write(0, AT91_SDRAMC_LPR, lpr0);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100349 if (pm_data.ramc[1])
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100350 at91_ramc_write(1, AT91_SDRAMC_LPR, lpr1);
351
352 cpu_do_idle();
353
354 at91_ramc_write(0, AT91_SDRAMC_LPR, saved_lpr0);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100355 if (pm_data.ramc[1])
Alexandre Bellonia18d0692015-03-16 23:44:37 +0100356 at91_ramc_write(1, AT91_SDRAMC_LPR, saved_lpr1);
357}
358
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100359struct ramc_info {
360 void (*idle)(void);
361 unsigned int memctrl;
362};
363
364static const struct ramc_info ramc_infos[] __initconst = {
365 { .idle = at91rm9200_standby, .memctrl = AT91_MEMCTRL_MC},
366 { .idle = at91sam9_sdram_standby, .memctrl = AT91_MEMCTRL_SDRAMC},
367 { .idle = at91_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
368 { .idle = sama5d3_ddr_standby, .memctrl = AT91_MEMCTRL_DDRSDR},
369};
370
Nicolas Pitre19c233b2015-07-27 18:27:52 -0400371static const struct of_device_id const ramc_ids[] __initconst = {
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100372 { .compatible = "atmel,at91rm9200-sdramc", .data = &ramc_infos[0] },
373 { .compatible = "atmel,at91sam9260-sdramc", .data = &ramc_infos[1] },
374 { .compatible = "atmel,at91sam9g45-ddramc", .data = &ramc_infos[2] },
375 { .compatible = "atmel,sama5d3-ddramc", .data = &ramc_infos[3] },
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100376 { /*sentinel*/ }
377};
378
Uwe Kleine-König444d2d32015-02-18 21:19:56 +0100379static __init void at91_dt_ramc(void)
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100380{
381 struct device_node *np;
382 const struct of_device_id *of_id;
383 int idx = 0;
Alexandre Bellonie56d75a2017-02-01 23:55:10 +0100384 void *standby = NULL;
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100385 const struct ramc_info *ramc;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100386
387 for_each_matching_node_and_match(np, ramc_ids, &of_id) {
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100388 pm_data.ramc[idx] = of_iomap(np, 0);
389 if (!pm_data.ramc[idx])
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100390 panic(pr_fmt("unable to map ramc[%d] cpu registers\n"), idx);
391
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100392 ramc = of_id->data;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100393 if (!standby)
Alexandre Belloniaab02d62017-02-01 22:41:50 +0100394 standby = ramc->idle;
395 pm_data.memctrl = ramc->memctrl;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100396
397 idx++;
398 }
399
400 if (!idx)
401 panic(pr_fmt("unable to find compatible ram controller node in dtb\n"));
402
403 if (!standby) {
404 pr_warn("ramc no standby function available\n");
405 return;
406 }
407
Alexandre Bellonie56d75a2017-02-01 23:55:10 +0100408 at91_cpuidle_device.dev.platform_data = standby;
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100409}
410
Ben Dooksab6778e2016-06-17 16:34:18 +0100411static void at91rm9200_idle(void)
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200412{
413 /*
414 * Disable the processor clock. The processor will be automatically
415 * re-enabled by an interrupt or by a reset.
416 */
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100417 writel(AT91_PMC_PCK, pm_data.pmc + AT91_PMC_SCDR);
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200418}
419
Ben Dooksab6778e2016-06-17 16:34:18 +0100420static void at91sam9_idle(void)
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200421{
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100422 writel(AT91_PMC_PCK, pm_data.pmc + AT91_PMC_SCDR);
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200423 cpu_do_idle();
424}
425
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100426static void __init at91_pm_sram_init(void)
427{
428 struct gen_pool *sram_pool;
429 phys_addr_t sram_pbase;
430 unsigned long sram_base;
431 struct device_node *node;
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100432 struct platform_device *pdev = NULL;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100433
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100434 for_each_compatible_node(node, NULL, "mmio-sram") {
435 pdev = of_find_device_by_node(node);
436 if (pdev) {
437 of_node_put(node);
438 break;
439 }
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100440 }
441
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100442 if (!pdev) {
443 pr_warn("%s: failed to find sram device!\n", __func__);
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100444 return;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100445 }
446
Vladimir Zapolskiy73858172015-09-04 15:47:43 -0700447 sram_pool = gen_pool_get(&pdev->dev, NULL);
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100448 if (!sram_pool) {
449 pr_warn("%s: sram pool unavailable!\n", __func__);
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100450 return;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100451 }
452
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800453 sram_base = gen_pool_alloc(sram_pool, at91_pm_suspend_in_sram_sz);
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100454 if (!sram_base) {
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800455 pr_warn("%s: unable to alloc sram!\n", __func__);
Alexandre Belloni4a031f72015-03-03 08:38:07 +0100456 return;
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100457 }
458
459 sram_pbase = gen_pool_virt_to_phys(sram_pool, sram_base);
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800460 at91_suspend_sram_fn = __arm_ioremap_exec(sram_pbase,
461 at91_pm_suspend_in_sram_sz, false);
462 if (!at91_suspend_sram_fn) {
Wenyou Yangd94e6882015-03-09 11:49:01 +0800463 pr_warn("SRAM: Could not map\n");
464 return;
465 }
466
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800467 /* Copy the pm suspend handler to SRAM */
468 at91_suspend_sram_fn = fncpy(at91_suspend_sram_fn,
469 &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100470}
Alexandre Bellonid2e46792015-01-15 15:59:25 +0100471
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200472static void __init at91_pm_backup_init(void)
473{
474 struct gen_pool *sram_pool;
475 struct device_node *np;
476 struct platform_device *pdev = NULL;
477
478 pm_bu = NULL;
479
480 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-shdwc");
481 if (!np) {
482 pr_warn("%s: failed to find shdwc!\n", __func__);
483 return;
484 }
485
486 pm_data.shdwc = of_iomap(np, 0);
487 of_node_put(np);
488
489 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-sfrbu");
490 if (!np) {
491 pr_warn("%s: failed to find sfrbu!\n", __func__);
492 goto sfrbu_fail;
493 }
494
495 pm_data.sfrbu = of_iomap(np, 0);
496 of_node_put(np);
497 pm_bu = NULL;
498
499 np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
500 if (!np)
501 goto securam_fail;
502
503 pdev = of_find_device_by_node(np);
504 of_node_put(np);
505 if (!pdev) {
506 pr_warn("%s: failed to find securam device!\n", __func__);
507 goto securam_fail;
508 }
509
510 sram_pool = gen_pool_get(&pdev->dev, NULL);
511 if (!sram_pool) {
512 pr_warn("%s: securam pool unavailable!\n", __func__);
513 goto securam_fail;
514 }
515
516 pm_bu = (void *)gen_pool_alloc(sram_pool, sizeof(struct at91_pm_bu));
517 if (!pm_bu) {
518 pr_warn("%s: unable to alloc securam!\n", __func__);
519 goto securam_fail;
520 }
521
522 pm_bu->suspended = 0;
523 pm_bu->canary = virt_to_phys(&canary);
524 pm_bu->resume = virt_to_phys(cpu_resume);
525
526 return;
527
528sfrbu_fail:
529 iounmap(pm_data.shdwc);
530 pm_data.shdwc = NULL;
531securam_fail:
532 iounmap(pm_data.sfrbu);
533 pm_data.sfrbu = NULL;
534}
535
Alexandre Belloni13f16012017-02-01 23:35:18 +0100536struct pmc_info {
537 unsigned long uhp_udp_mask;
538};
539
540static const struct pmc_info pmc_infos[] __initconst = {
541 { .uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP },
542 { .uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP },
543 { .uhp_udp_mask = AT91SAM926x_PMC_UHP },
544};
545
Alexandre Belloni5737b732015-09-30 01:31:34 +0200546static const struct of_device_id atmel_pmc_ids[] __initconst = {
Alexandre Belloni13f16012017-02-01 23:35:18 +0100547 { .compatible = "atmel,at91rm9200-pmc", .data = &pmc_infos[0] },
548 { .compatible = "atmel,at91sam9260-pmc", .data = &pmc_infos[1] },
549 { .compatible = "atmel,at91sam9g45-pmc", .data = &pmc_infos[2] },
550 { .compatible = "atmel,at91sam9n12-pmc", .data = &pmc_infos[1] },
551 { .compatible = "atmel,at91sam9x5-pmc", .data = &pmc_infos[1] },
552 { .compatible = "atmel,sama5d3-pmc", .data = &pmc_infos[1] },
553 { .compatible = "atmel,sama5d2-pmc", .data = &pmc_infos[1] },
Alexandre Belloni5737b732015-09-30 01:31:34 +0200554 { /* sentinel */ },
555};
556
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200557static void __init at91_pm_init(void (*pm_idle)(void))
Andrew Victor907d6de2006-06-20 19:30:19 +0100558{
Alexandre Belloni5737b732015-09-30 01:31:34 +0200559 struct device_node *pmc_np;
Alexandre Belloni13f16012017-02-01 23:35:18 +0100560 const struct of_device_id *of_id;
561 const struct pmc_info *pmc;
Andrew Victorf5d0f452008-04-02 21:50:16 +0100562
Daniel Lezcano5ad945e2013-09-22 22:29:57 +0200563 if (at91_cpuidle_device.dev.platform_data)
564 platform_device_register(&at91_cpuidle_device);
Andrew Victor907d6de2006-06-20 19:30:19 +0100565
Alexandre Belloni13f16012017-02-01 23:35:18 +0100566 pmc_np = of_find_matching_node_and_match(NULL, atmel_pmc_ids, &of_id);
Alexandre Belloni65cc1a52017-01-31 18:12:57 +0100567 pm_data.pmc = of_iomap(pmc_np, 0);
568 if (!pm_data.pmc) {
Alexandre Belloni5737b732015-09-30 01:31:34 +0200569 pr_err("AT91: PM not supported, PMC not found\n");
570 return;
571 }
572
Alexandre Belloni13f16012017-02-01 23:35:18 +0100573 pmc = of_id->data;
574 pm_data.uhp_udp_mask = pmc->uhp_udp_mask;
575
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200576 if (pm_idle)
577 arm_pm_idle = pm_idle;
578
Alexandre Belloni5737b732015-09-30 01:31:34 +0200579 at91_pm_sram_init();
580
Wenyou Yang5726a8b2015-03-09 11:51:09 +0800581 if (at91_suspend_sram_fn)
Wenyou Yangd94e6882015-03-09 11:49:01 +0800582 suspend_set_ops(&at91_pm_ops);
583 else
584 pr_info("AT91: PM not supported, due to no SRAM allocated\n");
Andrew Victor907d6de2006-06-20 19:30:19 +0100585}
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100586
Nicolas Ferread3fc3e2015-01-27 18:41:33 +0100587void __init at91rm9200_pm_init(void)
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100588{
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100589 at91_dt_ramc();
590
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100591 /*
592 * AT91RM9200 SDRAM low-power mode cannot be used with self-refresh.
593 */
Alexandre Bellonid7d45f22015-03-16 15:14:50 +0100594 at91_ramc_write(0, AT91_MC_SDRAMC_LPR, 0);
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100595
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200596 at91_pm_init(at91rm9200_idle);
Alexandre Belloni4db0ba22015-01-15 15:59:27 +0100597}
598
Alexandre Belloni13469192017-02-01 23:43:03 +0100599void __init at91sam9_pm_init(void)
Nicolas Ferrebf022802015-01-22 16:54:50 +0100600{
Alexandre Belloni827de1f2015-01-27 17:38:46 +0100601 at91_dt_ramc();
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200602 at91_pm_init(at91sam9_idle);
603}
604
605void __init sama5_pm_init(void)
606{
607 at91_dt_ramc();
Alexandre Bellonifbc7edc2015-09-30 01:58:40 +0200608 at91_pm_init(NULL);
Nicolas Ferrebf022802015-01-22 16:54:50 +0100609}
Alexandre Belloni24a0f5c2016-09-27 12:29:50 +0200610
611void __init sama5d2_pm_init(void)
612{
613 at91_pm_backup_init();
614 sama5_pm_init();
615}