blob: 5b775fac1c6b56fac9aa97fe988daadcf2fc0462 [file] [log] [blame]
Andrew Victor907d6de2006-06-20 19:30:19 +01001/*
2 * arch/arm/mach-at91rm9200/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/pm.h>
14#include <linux/sched.h>
15#include <linux/proc_fs.h>
16#include <linux/pm.h>
17#include <linux/interrupt.h>
18#include <linux/sysfs.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21
22#include <asm/io.h>
23#include <asm/irq.h>
24#include <asm/atomic.h>
25#include <asm/mach/time.h>
26#include <asm/mach/irq.h>
27#include <asm/mach-types.h>
28
Andrew Victor55d8bae2006-11-30 17:16:43 +010029#include <asm/arch/at91_pmc.h>
30#include <asm/arch/at91rm9200_mc.h>
Andrew Victor907d6de2006-06-20 19:30:19 +010031#include <asm/arch/gpio.h>
32
33#include "generic.h"
34
35
36static int at91_pm_valid_state(suspend_state_t state)
37{
38 switch (state) {
39 case PM_SUSPEND_ON:
40 case PM_SUSPEND_STANDBY:
41 case PM_SUSPEND_MEM:
42 return 1;
43
44 default:
45 return 0;
46 }
47}
48
49
50static suspend_state_t target_state;
51
52/*
53 * Called after processes are frozen, but before we shutdown devices.
54 */
55static int at91_pm_prepare(suspend_state_t state)
56{
57 target_state = state;
58 return 0;
59}
60
61/*
62 * Verify that all the clocks are correct before entering
63 * slow-clock mode.
64 */
65static int at91_pm_verify_clocks(void)
66{
67 unsigned long scsr;
68 int i;
69
70 scsr = at91_sys_read(AT91_PMC_SCSR);
71
72 /* USB must not be using PLLB */
73 if ((scsr & (AT91_PMC_UHP | AT91_PMC_UDP)) != 0) {
74 pr_debug("AT91: PM - Suspend-to-RAM with USB still active\n");
75 return 0;
76 }
77
78#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
79 /* PCK0..PCK3 must be disabled, or configured to use clk32k */
80 for (i = 0; i < 4; i++) {
81 u32 css;
82
83 if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
84 continue;
85
86 css = at91_sys_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
87 if (css != AT91_PMC_CSS_SLOW) {
88 pr_debug("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
89 return 0;
90 }
91 }
92#endif
93
94 return 1;
95}
96
97/*
98 * Call this from platform driver suspend() to see how deeply to suspend.
99 * For example, some controllers (like OHCI) need one of the PLL clocks
100 * in order to act as a wakeup source, and those are not available when
101 * going into slow clock mode.
102 *
103 * REVISIT: generalize as clk_will_be_available(clk)? Other platforms have
104 * the very same problem (but not using at91 main_clk), and it'd be better
105 * to add one generic API rather than lots of platform-specific ones.
106 */
107int at91_suspend_entering_slow_clock(void)
108{
109 return (target_state == PM_SUSPEND_MEM);
110}
111EXPORT_SYMBOL(at91_suspend_entering_slow_clock);
112
113
114static void (*slow_clock)(void);
115
116
Andrew Victor907d6de2006-06-20 19:30:19 +0100117static int at91_pm_enter(suspend_state_t state)
118{
119 at91_gpio_suspend();
120 at91_irq_suspend();
121
122 pr_debug("AT91: PM - wake mask %08x, pm state %d\n",
123 /* remember all the always-wake irqs */
124 (at91_sys_read(AT91_PMC_PCSR)
125 | (1 << AT91_ID_FIQ)
126 | (1 << AT91_ID_SYS)
Andrew Victor1f4fd0a2006-11-30 10:01:47 +0100127 | (at91_extern_irq))
Andrew Victor907d6de2006-06-20 19:30:19 +0100128 & at91_sys_read(AT91_AIC_IMR),
129 state);
130
131 switch (state) {
132 /*
133 * Suspend-to-RAM is like STANDBY plus slow clock mode, so
134 * drivers must suspend more deeply: only the master clock
135 * controller may be using the main oscillator.
136 */
137 case PM_SUSPEND_MEM:
138 /*
139 * Ensure that clocks are in a valid state.
140 */
141 if (!at91_pm_verify_clocks())
142 goto error;
143
144 /*
145 * Enter slow clock mode by switching over to clk32k and
146 * turning off the main oscillator; reverse on wakeup.
147 */
148 if (slow_clock) {
149 slow_clock();
150 break;
151 } else {
152 /* DEVELOPMENT ONLY */
153 pr_info("AT91: PM - no slow clock mode yet ...\n");
154 /* FALLTHROUGH leaving master clock alone */
155 }
156
157 /*
158 * STANDBY mode has *all* drivers suspended; ignores irqs not
159 * marked as 'wakeup' event sources; and reduces DRAM power.
160 * But otherwise it's identical to PM_SUSPEND_ON: cpu idle, and
161 * nothing fancy done with main or cpu clocks.
162 */
163 case PM_SUSPEND_STANDBY:
164 /*
165 * NOTE: the Wait-for-Interrupt instruction needs to be
166 * in icache so the SDRAM stays in self-refresh mode until
167 * the wakeup IRQ occurs.
168 */
169 asm("b 1f; .align 5; 1:");
170 asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
171 at91_sys_write(AT91_SDRAMC_SRR, 1); /* self-refresh mode */
172 /* fall though to next state */
173
174 case PM_SUSPEND_ON:
175 asm("mcr p15, 0, r0, c7, c0, 4"); /* wait for interrupt */
176 break;
177
178 default:
179 pr_debug("AT91: PM - bogus suspend state %d\n", state);
180 goto error;
181 }
182
183 pr_debug("AT91: PM - wakeup %08x\n",
184 at91_sys_read(AT91_AIC_IPR) & at91_sys_read(AT91_AIC_IMR));
185
186error:
187 target_state = PM_SUSPEND_ON;
188 at91_irq_resume();
189 at91_gpio_resume();
190 return 0;
191}
192
193
194static struct pm_ops at91_pm_ops ={
195 .pm_disk_mode = 0,
196 .valid = at91_pm_valid_state,
197 .prepare = at91_pm_prepare,
198 .enter = at91_pm_enter,
199};
200
201static int __init at91_pm_init(void)
202{
203 printk("AT91: Power Management\n");
204
205#ifdef CONFIG_AT91_PM_SLOW_CLOCK
206 /* REVISIT allocations of SRAM should be dynamically managed.
207 * FIQ handlers and other components will want SRAM/TCM too...
208 */
209 slow_clock = (void *) (AT91_VA_BASE_SRAM + (3 * SZ_4K));
210 memcpy(slow_clock, at91rm9200_slow_clock, at91rm9200_slow_clock_sz);
211#endif
212
213 /* Disable SDRAM low-power mode. Cannot be used with self-refresh. */
214 at91_sys_write(AT91_SDRAMC_LPR, 0);
215
216 pm_set_ops(&at91_pm_ops);
217
218 return 0;
219}
220arch_initcall(at91_pm_init);