blob: 79aa793d1f00569fa56dc642319510df67063910 [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
Russell King2f8163b2011-07-26 10:53:52 +010013#include <linux/gpio.h>
Rafael J. Wysocki95d9ffb2007-10-18 03:04:39 -070014#include <linux/suspend.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010015#include <linux/sched.h>
16#include <linux/proc_fs.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010017#include <linux/interrupt.h>
18#include <linux/sysfs.h>
19#include <linux/module.h>
Alexandre Bellonif5598d32015-01-15 15:59:24 +010020#include <linux/of.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010021#include <linux/platform_device.h>
Russell Kingfced80c2008-09-06 12:10:45 +010022#include <linux/io.h>
Boris BREZILLON2edb90a2013-10-11 09:37:45 +020023#include <linux/clk/at91_pmc.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010024
Andrew Victor907d6de2006-06-20 19:30:19 +010025#include <asm/irq.h>
Arun Sharma600634972011-07-26 16:09:06 -070026#include <linux/atomic.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010027#include <asm/mach/time.h>
28#include <asm/mach/irq.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010029
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/cpu.h>
Uwe Kleine-Königac11a1d2013-11-14 10:49:19 +010031#include <mach/hardware.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010032
33#include "generic.h"
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010034#include "pm.h"
Andrew Victor907d6de2006-06-20 19:30:19 +010035
Alexandre Bellonif5598d32015-01-15 15:59:24 +010036static struct {
37 unsigned long uhp_udp_mask;
38 int memctrl;
39} at91_pm_data;
40
Daniel Lezcano5ad945e2013-09-22 22:29:57 +020041static void (*at91_pm_standby)(void);
42
Andrew Victor907d6de2006-06-20 19:30:19 +010043static int at91_pm_valid_state(suspend_state_t state)
44{
45 switch (state) {
46 case PM_SUSPEND_ON:
47 case PM_SUSPEND_STANDBY:
48 case PM_SUSPEND_MEM:
49 return 1;
50
51 default:
52 return 0;
53 }
54}
55
56
57static suspend_state_t target_state;
58
59/*
60 * Called after processes are frozen, but before we shutdown devices.
61 */
Rafael J. Wysockic697eec2008-01-08 00:04:17 +010062static int at91_pm_begin(suspend_state_t state)
Andrew Victor907d6de2006-06-20 19:30:19 +010063{
64 target_state = state;
65 return 0;
66}
67
68/*
69 * Verify that all the clocks are correct before entering
70 * slow-clock mode.
71 */
72static int at91_pm_verify_clocks(void)
73{
74 unsigned long scsr;
75 int i;
76
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +080077 scsr = at91_pmc_read(AT91_PMC_SCSR);
Andrew Victor907d6de2006-06-20 19:30:19 +010078
79 /* USB must not be using PLLB */
Alexandre Bellonif5598d32015-01-15 15:59:24 +010080 if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
81 pr_err("AT91: PM - Suspend-to-RAM with USB still active\n");
82 return 0;
Andrew Victor907d6de2006-06-20 19:30:19 +010083 }
84
Andrew Victor907d6de2006-06-20 19:30:19 +010085 /* PCK0..PCK3 must be disabled, or configured to use clk32k */
86 for (i = 0; i < 4; i++) {
87 u32 css;
88
89 if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
90 continue;
91
Jean-Christophe PLAGNIOL-VILLARDb5514952011-11-25 09:59:46 +080092 css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
Andrew Victor907d6de2006-06-20 19:30:19 +010093 if (css != AT91_PMC_CSS_SLOW) {
Ryan Mallon7f96b1c2009-04-01 20:33:30 +010094 pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
Andrew Victor907d6de2006-06-20 19:30:19 +010095 return 0;
96 }
97 }
Andrew Victor907d6de2006-06-20 19:30:19 +010098
99 return 1;
100}
101
102/*
103 * Call this from platform driver suspend() to see how deeply to suspend.
104 * For example, some controllers (like OHCI) need one of the PLL clocks
105 * in order to act as a wakeup source, and those are not available when
106 * going into slow clock mode.
107 *
108 * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
109 * the very same problem (but not using at91 main_clk), and it'd be better
110 * to add one generic API rather than lots of platform-specific ones.
111 */
112int at91_suspend_entering_slow_clock(void)
113{
114 return (target_state == PM_SUSPEND_MEM);
115}
116EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
117
118
Jean-Christophe PLAGNIOL-VILLARDfb7e1972012-02-22 17:50:55 +0100119static void (*slow_clock)(void __iomem *pmc, void __iomem *ramc0,
120 void __iomem *ramc1, int memctrl);
Andrew Victor907d6de2006-06-20 19:30:19 +0100121
Andrew Victorf5d0f452008-04-02 21:50:16 +0100122#ifdef CONFIG_AT91_SLOW_CLOCK
Jean-Christophe PLAGNIOL-VILLARDfb7e1972012-02-22 17:50:55 +0100123extern void at91_slow_clock(void __iomem *pmc, void __iomem *ramc0,
124 void __iomem *ramc1, int memctrl);
Andrew Victorf5d0f452008-04-02 21:50:16 +0100125extern u32 at91_slow_clock_sz;
126#endif
127
Andrew Victor907d6de2006-06-20 19:30:19 +0100128static int at91_pm_enter(suspend_state_t state)
129{
Arnd Bergmann85c4b312014-12-02 12:08:27 +0100130 at91_pinctrl_gpio_suspend();
Andrew Victor907d6de2006-06-20 19:30:19 +0100131
Andrew Victor907d6de2006-06-20 19:30:19 +0100132 switch (state) {
133 /*
134 * Suspend-to-RAM is like STANDBY plus slow clock mode, so
135 * drivers must suspend more deeply: only the master clock
136 * controller may be using the main oscillator.
137 */
138 case PM_SUSPEND_MEM:
139 /*
140 * Ensure that clocks are in a valid state.
141 */
142 if (!at91_pm_verify_clocks())
143 goto error;
144
145 /*
146 * Enter slow clock mode by switching over to clk32k and
147 * turning off the main oscillator; reverse on wakeup.
148 */
149 if (slow_clock) {
Andrew Victorf5d0f452008-04-02 21:50:16 +0100150#ifdef CONFIG_AT91_SLOW_CLOCK
151 /* copy slow_clock handler to SRAM, and call it */
152 memcpy(slow_clock, at91_slow_clock, at91_slow_clock_sz);
153#endif
Jean-Christophe PLAGNIOL-VILLARDfb7e1972012-02-22 17:50:55 +0100154 slow_clock(at91_pmc_base, at91_ramc_base[0],
Alexandre Bellonif5598d32015-01-15 15:59:24 +0100155 at91_ramc_base[1],
156 at91_pm_data.memctrl);
Andrew Victor907d6de2006-06-20 19:30:19 +0100157 break;
158 } else {
Andrew Victorf5d0f452008-04-02 21:50:16 +0100159 pr_info("AT91: PM - no slow clock mode enabled ...\n");
Andrew Victor907d6de2006-06-20 19:30:19 +0100160 /* FALLTHROUGH leaving master clock alone */
161 }
162
163 /*
164 * STANDBY mode has *all* drivers suspended; ignores irqs not
165 * marked as 'wakeup' event sources; and reduces DRAM power.
166 * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
167 * nothing fancy done with main or cpu clocks.
168 */
169 case PM_SUSPEND_STANDBY:
170 /*
171 * NOTE: the Wait-for-Interrupt instruction needs to be
Andrew Victorf5d0f452008-04-02 21:50:16 +0100172 * in icache so no SDRAM accesses are needed until the
173 * wakeup IRQ occurs and self-refresh is terminated.
Nicolas Ferre8aeeda82010-10-22 17:53:39 +0200174 * For ARM 926 based chips, this requirement is weaker
175 * as at91sam9 can access a RAM in self-refresh mode.
Andrew Victor907d6de2006-06-20 19:30:19 +0100176 */
Daniel Lezcano5ad945e2013-09-22 22:29:57 +0200177 if (at91_pm_standby)
178 at91_pm_standby();
Andrew Victorf5d0f452008-04-02 21:50:16 +0100179 break;
Andrew Victor907d6de2006-06-20 19:30:19 +0100180
181 case PM_SUSPEND_ON:
Nicolas Ferre8aeeda82010-10-22 17:53:39 +0200182 cpu_do_idle();
Andrew Victor907d6de2006-06-20 19:30:19 +0100183 break;
184
185 default:
186 pr_debug("AT91: PM - bogus suspend state %d\n", state);
187 goto error;
188 }
189
Andrew Victor907d6de2006-06-20 19:30:19 +0100190error:
191 target_state = PM_SUSPEND_ON;
Boris BREZILLON07192602014-07-10 19:14:20 +0200192
Arnd Bergmann85c4b312014-12-02 12:08:27 +0100193 at91_pinctrl_gpio_resume();
Andrew Victor907d6de2006-06-20 19:30:19 +0100194 return 0;
195}
196
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100197/*
198 * Called right prior to thawing processes.
199 */
200static void at91_pm_end(void)
201{
202 target_state = PM_SUSPEND_ON;
203}
204
Andrew Victor907d6de2006-06-20 19:30:19 +0100205
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100206static const struct platform_suspend_ops at91_pm_ops = {
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100207 .valid = at91_pm_valid_state,
208 .begin = at91_pm_begin,
209 .enter = at91_pm_enter,
210 .end = at91_pm_end,
Andrew Victor907d6de2006-06-20 19:30:19 +0100211};
212
Daniel Lezcano5ad945e2013-09-22 22:29:57 +0200213static struct platform_device at91_cpuidle_device = {
214 .name = "cpuidle-at91",
215};
216
217void at91_pm_set_standby(void (*at91_standby)(void))
218{
219 if (at91_standby) {
220 at91_cpuidle_device.dev.platform_data = at91_standby;
221 at91_pm_standby = at91_standby;
222 }
223}
224
Andrew Victor907d6de2006-06-20 19:30:19 +0100225static int __init at91_pm_init(void)
226{
Andrew Victorf5d0f452008-04-02 21:50:16 +0100227#ifdef CONFIG_AT91_SLOW_CLOCK
228 slow_clock = (void *) (AT91_IO_VIRT_BASE - at91_slow_clock_sz);
Andrew Victor907d6de2006-06-20 19:30:19 +0100229#endif
230
Andrew Victorf5d0f452008-04-02 21:50:16 +0100231 pr_info("AT91: Power Management%s\n", (slow_clock ? " (with slow clock mode)" : ""));
232
Alexandre Bellonif5598d32015-01-15 15:59:24 +0100233 at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
234
235 if (of_machine_is_compatible("atmel,at91rm9200")) {
236 /*
237 * AT91RM9200 SDRAM low-power mode cannot be used with
238 * self-refresh.
239 */
Jean-Christophe PLAGNIOL-VILLARDefd09162012-02-13 14:58:30 +0800240 at91_ramc_write(0, AT91RM9200_SDRAMC_LPR, 0);
Alexandre Bellonif5598d32015-01-15 15:59:24 +0100241
242 at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP |
243 AT91RM9200_PMC_UDP;
244 at91_pm_data.memctrl = AT91_MEMCTRL_MC;
245 } else if (of_machine_is_compatible("atmel,at91sam9260") ||
246 of_machine_is_compatible("atmel,at91sam9g20") ||
247 of_machine_is_compatible("atmel,at91sam9261") ||
248 of_machine_is_compatible("atmel,at91sam9g10") ||
249 of_machine_is_compatible("atmel,at91sam9263")) {
250 at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP |
251 AT91SAM926x_PMC_UDP;
252 } else if (of_machine_is_compatible("atmel,at91sam9g45")) {
253 at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
254 }
255
Daniel Lezcano5ad945e2013-09-22 22:29:57 +0200256 if (at91_cpuidle_device.dev.platform_data)
257 platform_device_register(&at91_cpuidle_device);
Andrew Victor907d6de2006-06-20 19:30:19 +0100258
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700259 suspend_set_ops(&at91_pm_ops);
Andrew Victor907d6de2006-06-20 19:30:19 +0100260
Andrew Victor907d6de2006-06-20 19:30:19 +0100261 return 0;
262}
263arch_initcall(at91_pm_init);