vserver 1.9.5.x5
[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_GPLR0, SLEEP_SAVE_GPLR1, SLEEP_SAVE_GPLR2,
51         SLEEP_SAVE_GPDR0, SLEEP_SAVE_GPDR1, SLEEP_SAVE_GPDR2,
52         SLEEP_SAVE_GRER0, SLEEP_SAVE_GRER1, SLEEP_SAVE_GRER2,
53         SLEEP_SAVE_GFER0, SLEEP_SAVE_GFER1, SLEEP_SAVE_GFER2,
54         SLEEP_SAVE_GAFR0_L, SLEEP_SAVE_GAFR1_L, SLEEP_SAVE_GAFR2_L,
55         SLEEP_SAVE_GAFR0_U, SLEEP_SAVE_GAFR1_U, SLEEP_SAVE_GAFR2_U,
56
57         SLEEP_SAVE_ICMR,
58         SLEEP_SAVE_CKEN,
59
60         SLEEP_SAVE_CKSUM,
61
62         SLEEP_SAVE_SIZE
63 };
64
65
66 static int pxa_pm_enter(suspend_state_t state)
67 {
68         unsigned long sleep_save[SLEEP_SAVE_SIZE];
69         unsigned long checksum = 0;
70         struct timespec delta, rtc;
71         int i;
72
73         if (state != PM_SUSPEND_MEM)
74                 return -EINVAL;
75
76         /* preserve current time */
77         rtc.tv_sec = RCNR;
78         rtc.tv_nsec = 0;
79         save_time_delta(&delta, &rtc);
80
81         SAVE(GPLR0); SAVE(GPLR1); SAVE(GPLR2);
82         SAVE(GPDR0); SAVE(GPDR1); SAVE(GPDR2);
83         SAVE(GRER0); SAVE(GRER1); SAVE(GRER2);
84         SAVE(GFER0); SAVE(GFER1); SAVE(GFER2);
85         SAVE(GAFR0_L); SAVE(GAFR0_U);
86         SAVE(GAFR1_L); SAVE(GAFR1_U);
87         SAVE(GAFR2_L); SAVE(GAFR2_U);
88
89         SAVE(ICMR);
90         ICMR = 0;
91
92         SAVE(CKEN);
93         CKEN = 0;
94
95         /* Note: wake up source are set up in each machine specific files */
96
97         /* clear GPIO transition detect  bits */
98         GEDR0 = GEDR0; GEDR1 = GEDR1; GEDR2 = GEDR2;
99
100         /* Clear sleep reset status */
101         RCSR = RCSR_SMR;
102
103         /* set resume return address */
104         PSPR = virt_to_phys(pxa_cpu_resume);
105
106         /* before sleeping, calculate and save a checksum */
107         for (i = 0; i < SLEEP_SAVE_SIZE - 1; i++)
108                 checksum += sleep_save[i];
109         sleep_save[SLEEP_SAVE_CKSUM] = checksum;
110
111         /* *** go zzz *** */
112         pxa_cpu_suspend();
113
114         /* after sleeping, validate the checksum */
115         checksum = 0;
116         for (i = 0; i < SLEEP_SAVE_SIZE - 1; i++)
117                 checksum += sleep_save[i];
118
119         /* if invalid, display message and wait for a hardware reset */
120         if (checksum != sleep_save[SLEEP_SAVE_CKSUM]) {
121 #ifdef CONFIG_ARCH_LUBBOCK
122                 LUB_HEXLED = 0xbadbadc5;
123 #endif
124                 while (1);
125         }
126
127         /* ensure not to come back here if it wasn't intended */
128         PSPR = 0;
129
130         /* restore registers */
131         RESTORE(GAFR0_L); RESTORE(GAFR0_U);
132         RESTORE(GAFR1_L); RESTORE(GAFR1_U);
133         RESTORE(GAFR2_L); RESTORE(GAFR2_U);
134         RESTORE_GPLEVEL(0); RESTORE_GPLEVEL(1); RESTORE_GPLEVEL(2);
135         RESTORE(GPDR0); RESTORE(GPDR1); RESTORE(GPDR2);
136         RESTORE(GRER0); RESTORE(GRER1); RESTORE(GRER2);
137         RESTORE(GFER0); RESTORE(GFER1); RESTORE(GFER2);
138
139         PSSR = PSSR_RDH | PSSR_PH;
140
141         RESTORE(CKEN);
142
143         ICLR = 0;
144         ICCR = 1;
145         RESTORE(ICMR);
146
147         /* restore current time */
148         rtc.tv_sec = RCNR;
149         restore_time_delta(&delta, &rtc);
150
151 #ifdef DEBUG
152         printk(KERN_DEBUG "*** made it back from resume\n");
153 #endif
154
155         return 0;
156 }
157
158 unsigned long sleep_phys_sp(void *sp)
159 {
160         return virt_to_phys(sp);
161 }
162
163 /*
164  * Called after processes are frozen, but before we shut down devices.
165  */
166 static int pxa_pm_prepare(suspend_state_t state)
167 {
168         return 0;
169 }
170
171 /*
172  * Called after devices are re-setup, but before processes are thawed.
173  */
174 static int pxa_pm_finish(suspend_state_t state)
175 {
176         return 0;
177 }
178
179 /*
180  * Set to PM_DISK_FIRMWARE so we can quickly veto suspend-to-disk.
181  */
182 static struct pm_ops pxa_pm_ops = {
183         .pm_disk_mode   = PM_DISK_FIRMWARE,
184         .prepare        = pxa_pm_prepare,
185         .enter          = pxa_pm_enter,
186         .finish         = pxa_pm_finish,
187 };
188
189 static int __init pxa_pm_init(void)
190 {
191         pm_set_ops(&pxa_pm_ops);
192         return 0;
193 }
194
195 late_initcall(pxa_pm_init);