vserver 1.9.3
[linux-2.6.git] / arch / arm / mach-pxa / pm.c
1 /*
2  * PXA250/210 Power Management Routines
3  *
4  * Original code for the SA11x0:
5  * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com>
6  *
7  * Modified for the PXA250 by Nicolas Pitre:
8  * Copyright (c) 2002 Monta Vista Software, Inc.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License.
12  */
13 #include <linux/config.h>
14 #include <linux/init.h>
15 #include <linux/suspend.h>
16 #include <linux/errno.h>
17 #include <linux/time.h>
18
19 #include <asm/hardware.h>
20 #include <asm/memory.h>
21 #include <asm/system.h>
22 #include <asm/arch/pxa-regs.h>
23 #include <asm/arch/lubbock.h>
24 #include <asm/mach/time.h>
25
26
27 /*
28  * Debug macros
29  */
30 #undef DEBUG
31
32 extern void pxa_cpu_suspend(void);
33 extern void pxa_cpu_resume(void);
34
35 #define SAVE(x)         sleep_save[SLEEP_SAVE_##x] = x
36 #define RESTORE(x)      x = sleep_save[SLEEP_SAVE_##x]
37
38 #define RESTORE_GPLEVEL(n) do { \
39         GPSR##n = sleep_save[SLEEP_SAVE_GPLR##n]; \
40         GPCR##n = ~sleep_save[SLEEP_SAVE_GPLR##n]; \
41 } while (0)
42
43 /*
44  * List of global PXA peripheral registers to preserve.
45  * More ones like CP and general purpose register values are preserved
46  * with the stack pointer in sleep.S.
47  */
48 enum {  SLEEP_SAVE_START = 0,
49
50         SLEEP_SAVE_OIER,
51         SLEEP_SAVE_OSMR0, SLEEP_SAVE_OSMR1, SLEEP_SAVE_OSMR2, SLEEP_SAVE_OSMR3,
52
53         SLEEP_SAVE_GPLR0, SLEEP_SAVE_GPLR1, SLEEP_SAVE_GPLR2,
54         SLEEP_SAVE_GPDR0, SLEEP_SAVE_GPDR1, SLEEP_SAVE_GPDR2,
55         SLEEP_SAVE_GRER0, SLEEP_SAVE_GRER1, SLEEP_SAVE_GRER2,
56         SLEEP_SAVE_GFER0, SLEEP_SAVE_GFER1, SLEEP_SAVE_GFER2,
57         SLEEP_SAVE_GAFR0_L, SLEEP_SAVE_GAFR1_L, SLEEP_SAVE_GAFR2_L,
58         SLEEP_SAVE_GAFR0_U, SLEEP_SAVE_GAFR1_U, SLEEP_SAVE_GAFR2_U,
59
60         SLEEP_SAVE_ICMR,
61         SLEEP_SAVE_CKEN,
62
63         SLEEP_SAVE_CKSUM,
64
65         SLEEP_SAVE_SIZE
66 };
67
68
69 static int pxa_pm_enter(u32 state)
70 {
71         unsigned long sleep_save[SLEEP_SAVE_SIZE];
72         unsigned long checksum = 0;
73         struct timespec delta, rtc;
74         int i;
75
76         if (state != PM_SUSPEND_MEM)
77                 return -EINVAL;
78
79         /* preserve current time */
80         rtc.tv_sec = RCNR;
81         rtc.tv_nsec = 0;
82         save_time_delta(&delta, &rtc);
83
84         /* save vital registers */
85         SAVE(OSMR0);
86         SAVE(OSMR1);
87         SAVE(OSMR2);
88         SAVE(OSMR3);
89         SAVE(OIER);
90
91         SAVE(GPLR0); SAVE(GPLR1); SAVE(GPLR2);
92         SAVE(GPDR0); SAVE(GPDR1); SAVE(GPDR2);
93         SAVE(GRER0); SAVE(GRER1); SAVE(GRER2);
94         SAVE(GFER0); SAVE(GFER1); SAVE(GFER2);
95         SAVE(GAFR0_L); SAVE(GAFR0_U);
96         SAVE(GAFR1_L); SAVE(GAFR1_U);
97         SAVE(GAFR2_L); SAVE(GAFR2_U);
98
99         SAVE(ICMR);
100         ICMR = 0;
101
102         SAVE(CKEN);
103         CKEN = 0;
104
105         /* Note: wake up source are set up in each machine specific files */
106
107         /* clear GPIO transition detect  bits */
108         GEDR0 = GEDR0; GEDR1 = GEDR1; GEDR2 = GEDR2;
109
110         /* Clear sleep reset status */
111         RCSR = RCSR_SMR;
112
113         /* set resume return address */
114         PSPR = virt_to_phys(pxa_cpu_resume);
115
116         /* before sleeping, calculate and save a checksum */
117         for (i = 0; i < SLEEP_SAVE_SIZE - 1; i++)
118                 checksum += sleep_save[i];
119         sleep_save[SLEEP_SAVE_CKSUM] = checksum;
120
121         /* *** go zzz *** */
122         pxa_cpu_suspend();
123
124         /* after sleeping, validate the checksum */
125         checksum = 0;
126         for (i = 0; i < SLEEP_SAVE_SIZE - 1; i++)
127                 checksum += sleep_save[i];
128
129         /* if invalid, display message and wait for a hardware reset */
130         if (checksum != sleep_save[SLEEP_SAVE_CKSUM]) {
131 #ifdef CONFIG_ARCH_LUBBOCK
132                 LUB_HEXLED = 0xbadbadc5;
133 #endif
134                 while (1);
135         }
136
137         /* ensure not to come back here if it wasn't intended */
138         PSPR = 0;
139
140         /* restore registers */
141         RESTORE(GAFR0_L); RESTORE(GAFR0_U);
142         RESTORE(GAFR1_L); RESTORE(GAFR1_U);
143         RESTORE(GAFR2_L); RESTORE(GAFR2_U);
144         RESTORE_GPLEVEL(0); RESTORE_GPLEVEL(1); RESTORE_GPLEVEL(2);
145         RESTORE(GPDR0); RESTORE(GPDR1); RESTORE(GPDR2);
146         RESTORE(GRER0); RESTORE(GRER1); RESTORE(GRER2);
147         RESTORE(GFER0); RESTORE(GFER1); RESTORE(GFER2);
148
149         PSSR = PSSR_RDH | PSSR_PH;
150
151         RESTORE(OSMR0);
152         RESTORE(OSMR1);
153         RESTORE(OSMR2);
154         RESTORE(OSMR3);
155         RESTORE(OIER);
156
157         /* OSMR0 is the system timer: make sure OSCR is sufficiently behind */
158         OSCR = OSMR0 - LATCH;
159
160         RESTORE(CKEN);
161
162         ICLR = 0;
163         ICCR = 1;
164         RESTORE(ICMR);
165
166         /* restore current time */
167         rtc.tv_sec = RCNR;
168         restore_time_delta(&delta, &rtc);
169
170 #ifdef DEBUG
171         printk(KERN_DEBUG "*** made it back from resume\n");
172 #endif
173
174         return 0;
175 }
176
177 unsigned long sleep_phys_sp(void *sp)
178 {
179         return virt_to_phys(sp);
180 }
181
182 /*
183  * Called after processes are frozen, but before we shut down devices.
184  */
185 static int pxa_pm_prepare(u32 state)
186 {
187         return 0;
188 }
189
190 /*
191  * Called after devices are re-setup, but before processes are thawed.
192  */
193 static int pxa_pm_finish(u32 state)
194 {
195         return 0;
196 }
197
198 /*
199  * Set to PM_DISK_FIRMWARE so we can quickly veto suspend-to-disk.
200  */
201 static struct pm_ops pxa_pm_ops = {
202         .pm_disk_mode   = PM_DISK_FIRMWARE,
203         .prepare        = pxa_pm_prepare,
204         .enter          = pxa_pm_enter,
205         .finish         = pxa_pm_finish,
206 };
207
208 static int __init pxa_pm_init(void)
209 {
210         pm_set_ops(&pxa_pm_ops);
211         return 0;
212 }
213
214 late_initcall(pxa_pm_init);