cleanup
[linux-2.4.git] / arch / ppc / platforms / pmac_sleep.S
1 /*
2  * This file contains sleep low-level functions for PowerBook G3.
3  *    Copyright (C) 1999 Benjamin Herrenschmidt (benh@kernel.crashing.org)
4  *    and Paul Mackerras (paulus@samba.org).
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  */
12
13 #include <linux/config.h>
14 #include <asm/processor.h>
15 #include <asm/page.h>
16 #include <asm/ppc_asm.h>
17 #include <asm/cputable.h>
18 #include <asm/cache.h>
19
20 #define MAGIC   0x4c617273      /* 'Lars' */
21
22 /*
23  * Structure for storing CPU registers on the stack.
24  */
25 #define SL_SP           0
26 #define SL_PC           4
27 #define SL_MSR          8
28 #define SL_SDR1         0xc
29 #define SL_SPRG0        0x10    /* 4 sprg's */
30 #define SL_DBAT0        0x20
31 #define SL_IBAT0        0x28
32 #define SL_DBAT1        0x30
33 #define SL_IBAT1        0x38
34 #define SL_DBAT2        0x40
35 #define SL_IBAT2        0x48
36 #define SL_DBAT3        0x50
37 #define SL_IBAT3        0x58
38 #define SL_TB           0x60
39 #define SL_R2           0x68
40 #define SL_CR           0x6c
41 #define SL_R12          0x70    /* r12 to r31 */
42 #define SL_SIZE         (SL_R12 + 80)
43
44         .text
45         .align  5
46
47 #if defined(CONFIG_PMAC_PBOOK)
48
49 /* This gets called by via-pmu.c late during the sleep process.
50  * The PMU was already send the sleep command and will shut us down
51  * soon. We need to save all that is needed and setup the wakeup
52  * vector that will be called by the ROM on wakeup
53  */
54 _GLOBAL(low_sleep_handler)
55         mflr    r0
56         stw     r0,4(r1)
57         stwu    r1,-SL_SIZE(r1)
58         mfcr    r0
59         stw     r0,SL_CR(r1)
60         stw     r2,SL_R2(r1)
61         stmw    r12,SL_R12(r1)
62
63         /* Save MSR & SDR1 */
64         mfmsr   r4
65         stw     r4,SL_MSR(r1)
66         mfsdr1  r4
67         stw     r4,SL_SDR1(r1)
68
69         /* Get a stable timebase and save it */
70 1:      mftbu   r4
71         stw     r4,SL_TB(r1)
72         mftb    r5
73         stw     r5,SL_TB+4(r1)
74         mftbu   r3
75         cmpw    r3,r4
76         bne     1b
77
78         /* Save SPRGs */
79         mfsprg  r4,0
80         stw     r4,SL_SPRG0(r1)
81         mfsprg  r4,1
82         stw     r4,SL_SPRG0+4(r1)
83         mfsprg  r4,2
84         stw     r4,SL_SPRG0+8(r1)
85         mfsprg  r4,3
86         stw     r4,SL_SPRG0+12(r1)
87
88         /* Save BATs */
89         mfdbatu r4,0
90         stw     r4,SL_DBAT0(r1)
91         mfdbatl r4,0
92         stw     r4,SL_DBAT0+4(r1)
93         mfdbatu r4,1
94         stw     r4,SL_DBAT1(r1)
95         mfdbatl r4,1
96         stw     r4,SL_DBAT1+4(r1)
97         mfdbatu r4,2
98         stw     r4,SL_DBAT2(r1)
99         mfdbatl r4,2
100         stw     r4,SL_DBAT2+4(r1)
101         mfdbatu r4,3
102         stw     r4,SL_DBAT3(r1)
103         mfdbatl r4,3
104         stw     r4,SL_DBAT3+4(r1)
105         mfibatu r4,0
106         stw     r4,SL_IBAT0(r1)
107         mfibatl r4,0
108         stw     r4,SL_IBAT0+4(r1)
109         mfibatu r4,1
110         stw     r4,SL_IBAT1(r1)
111         mfibatl r4,1
112         stw     r4,SL_IBAT1+4(r1)
113         mfibatu r4,2
114         stw     r4,SL_IBAT2(r1)
115         mfibatl r4,2
116         stw     r4,SL_IBAT2+4(r1)
117         mfibatu r4,3
118         stw     r4,SL_IBAT3(r1)
119         mfibatl r4,3
120         stw     r4,SL_IBAT3+4(r1)
121
122         /* Backup various CPU config stuffs */
123         bl      __save_cpu_setup
124
125         /* The ROM can wake us up via 2 different vectors:
126          *  - On wallstreet & lombard, we must write a magic
127          *    value 'Lars' at address 4 and a pointer to a
128          *    memory location containing the PC to resume from
129          *    at address 0.
130          *  - On Core99, we must store the wakeup vector at
131          *    address 0x80 and eventually it's parameters
132          *    at address 0x84. I've have some trouble with those
133          *    parameters however and I no longer use them.
134          */
135         lis     r5,grackle_wake_up@ha
136         addi    r5,r5,grackle_wake_up@l
137         tophys(r5,r5)
138         stw     r5,SL_PC(r1)
139         lis     r4,KERNELBASE@h
140         tophys(r5,r1)
141         addi    r5,r5,SL_PC
142         lis     r6,MAGIC@ha
143         addi    r6,r6,MAGIC@l
144         stw     r5,0(r4)
145         stw     r6,4(r4)
146         /* Setup stuffs at 0x80-0x84 for Core99 */
147         lis     r3,core99_wake_up@ha
148         addi    r3,r3,core99_wake_up@l
149         tophys(r3,r3)
150         stw     r3,0x80(r4)
151         stw     r5,0x84(r4)
152         /* Store a pointer to our backup storage into
153          * a kernel global
154          */
155         lis r3,sleep_storage@ha
156         addi r3,r3,sleep_storage@l
157         stw r5,0(r3)
158
159         /* Disable DPM during cache flush */
160         mfspr   r3, SPRN_HID0
161         rlwinm  r3,r3,0,12,10
162         sync
163         mtspr   SPRN_HID0,r3
164         sync
165
166         /* Turn off data relocation. */
167         mfmsr   r3              /* Save MSR in r7 */
168         rlwinm  r3,r3,0,28,26   /* Turn off DR bit */
169         sync
170         mtmsr   r3
171         isync
172
173         /* Flush & disable L1 cache */
174         bl      __flush_disable_L1
175
176 /*
177  * Set the HID0 and MSR for sleep.
178  */
179         mfspr   r2,HID0
180         rlwinm  r2,r2,0,10,7    /* clear doze, nap */
181         oris    r2,r2,HID0_SLEEP@h
182         sync
183         isync
184         mtspr   HID0,r2
185         sync
186
187 /* This loop puts us back to sleep in case we have a spurrious
188  * wakeup so that the host bridge properly stays asleep. The
189  * CPU will be turned off, either after a known time (about 1
190  * second) on wallstreet & lombard, or as soon as the CPU enters
191  * SLEEP mode on core99
192  */
193         mfmsr   r2
194         oris    r2,r2,MSR_POW@h
195 1:      sync
196         mtmsr   r2
197         isync
198         b       1b
199
200 /*
201  * Here is the resume code.
202  */
203
204
205 /*
206  * Core99 machines resume here
207  * r4 has the physical address of SL_PC(sp) (unused)
208  */
209 _GLOBAL(core99_wake_up)
210         /* Make sure HID0 no longer contains any sleep bit */
211         mfspr   r3,HID0
212         rlwinm  r3,r3,0,11,7            /* clear SLEEP, NAP, DOZE bits */
213         mtspr   HID0,r3
214         sync
215         isync
216
217         /* Won't that cause problems on CPU that doesn't support it ? */
218         lis     r3, 0
219         mtspr   SPRN_MMCR0, r3
220
221         /* sanitize MSR */
222         mfmsr   r3
223         ori     r3,r3,MSR_EE|MSR_IP
224         xori    r3,r3,MSR_EE|MSR_IP
225         sync
226         isync
227         mtmsr   r3
228         sync
229         isync
230
231         /* Recover sleep storage */
232         lis     r3,sleep_storage@ha
233         addi    r3,r3,sleep_storage@l
234         tophys(r3,r3)
235         lwz     r1,0(r3)
236
237         /* Pass thru to older resume code ... */
238 /*
239  * Here is the resume code for older machines.
240  * r1 has the physical address of SL_PC(sp).
241  */
242
243 grackle_wake_up:
244         /* Invalidate & enable L1 cache, we don't care about
245          * whatever the ROM may have tried to write to memory
246          */
247         bl      __inval_enable_L1
248
249         /* Restore the kernel's segment registers before
250          * we do any r1 memory access as we are not sure they
251          * are in a sane state above the first 256Mb region
252          */
253         li      r0,16           /* load up segment register values */
254         mtctr   r0              /* for context 0 */
255         lis     r3,0x2000       /* Ku = 1, VSID = 0 */
256         li      r4,0
257 3:      mtsrin  r3,r4
258         addi    r3,r3,0x111     /* increment VSID */
259         addis   r4,r4,0x1000    /* address of next segment */
260         bdnz    3b
261         sync
262         isync
263
264         subi    r1,r1,SL_PC
265
266         /* Restore various CPU config stuffs */
267         bl      __restore_cpu_setup
268
269         /* Restore the BATs, and SDR1.  Then we can turn on the MMU. */
270         lwz     r4,SL_SDR1(r1)
271         mtsdr1  r4
272         lwz     r4,SL_SPRG0(r1)
273         mtsprg  0,r4
274         lwz     r4,SL_SPRG0+4(r1)
275         mtsprg  1,r4
276         lwz     r4,SL_SPRG0+8(r1)
277         mtsprg  2,r4
278         lwz     r4,SL_SPRG0+12(r1)
279         mtsprg  3,r4
280
281         lwz     r4,SL_DBAT0(r1)
282         mtdbatu 0,r4
283         lwz     r4,SL_DBAT0+4(r1)
284         mtdbatl 0,r4
285         lwz     r4,SL_DBAT1(r1)
286         mtdbatu 1,r4
287         lwz     r4,SL_DBAT1+4(r1)
288         mtdbatl 1,r4
289         lwz     r4,SL_DBAT2(r1)
290         mtdbatu 2,r4
291         lwz     r4,SL_DBAT2+4(r1)
292         mtdbatl 2,r4
293         lwz     r4,SL_DBAT3(r1)
294         mtdbatu 3,r4
295         lwz     r4,SL_DBAT3+4(r1)
296         mtdbatl 3,r4
297         lwz     r4,SL_IBAT0(r1)
298         mtibatu 0,r4
299         lwz     r4,SL_IBAT0+4(r1)
300         mtibatl 0,r4
301         lwz     r4,SL_IBAT1(r1)
302         mtibatu 1,r4
303         lwz     r4,SL_IBAT1+4(r1)
304         mtibatl 1,r4
305         lwz     r4,SL_IBAT2(r1)
306         mtibatu 2,r4
307         lwz     r4,SL_IBAT2+4(r1)
308         mtibatl 2,r4
309         lwz     r4,SL_IBAT3(r1)
310         mtibatu 3,r4
311         lwz     r4,SL_IBAT3+4(r1)
312         mtibatl 3,r4
313
314 BEGIN_FTR_SECTION
315         li      r4,0
316         mtspr   SPRN_DBAT4U,r4
317         mtspr   SPRN_DBAT4L,r4
318         mtspr   SPRN_DBAT5U,r4
319         mtspr   SPRN_DBAT5L,r4
320         mtspr   SPRN_DBAT6U,r4
321         mtspr   SPRN_DBAT6L,r4
322         mtspr   SPRN_DBAT7U,r4
323         mtspr   SPRN_DBAT7L,r4
324         mtspr   SPRN_IBAT4U,r4
325         mtspr   SPRN_IBAT4L,r4
326         mtspr   SPRN_IBAT5U,r4
327         mtspr   SPRN_IBAT5L,r4
328         mtspr   SPRN_IBAT6U,r4
329         mtspr   SPRN_IBAT6L,r4
330         mtspr   SPRN_IBAT7U,r4
331         mtspr   SPRN_IBAT7L,r4
332 END_FTR_SECTION_IFSET(CPU_FTR_HAS_HIGH_BATS)
333
334         /* Flush all TLBs */
335         lis     r4,0x1000
336 1:      addic.  r4,r4,-0x1000
337         tlbie   r4
338         blt     1b
339         sync
340
341         /* restore the MSR and turn on the MMU */
342         lwz     r3,SL_MSR(r1)
343         bl      turn_on_mmu
344
345         /* get back the stack pointer */
346         tovirt(r1,r1)
347
348         /* Restore TB */
349         li      r3,0
350         mttbl   r3
351         lwz     r3,SL_TB(r1)
352         lwz     r4,SL_TB+4(r1)
353         mttbu   r3
354         mttbl   r4
355
356         /* Restore the callee-saved registers and return */
357         lwz     r0,SL_CR(r1)
358         mtcr    r0
359         lwz     r2,SL_R2(r1)
360         lmw     r12,SL_R12(r1)
361         addi    r1,r1,SL_SIZE
362         lwz     r0,4(r1)
363         mtlr    r0
364         blr
365
366 turn_on_mmu:
367         mflr    r4
368         tovirt(r4,r4)
369         mtsrr0  r4
370         mtsrr1  r3
371         sync
372         isync
373         rfi
374
375 #endif /* defined(CONFIG_PMAC_PBOOK) */
376
377         .data
378         .balign L1_CACHE_LINE_SIZE
379 sleep_storage:
380         .long 0
381         .balign L1_CACHE_LINE_SIZE, 0
382         .text