]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/sunxi/psci.S
sun7i: Add support for taking CPUs offline via PSCI
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / sunxi / psci.S
1 /*
2  * Copyright (C) 2013 - ARM Ltd
3  * Author: Marc Zyngier <marc.zyngier@arm.com>
4  *
5  * Based on code by Carl van Schaik <carl@ok-labs.com>.
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 version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <asm/gic.h>
22 #include <asm/psci.h>
23 #include <asm/arch/cpu.h>
24
25 /*
26  * Memory layout:
27  *
28  * SECURE_RAM to text_end :
29  *      ._secure_text section
30  * text_end to ALIGN_PAGE(text_end):
31  *      nothing
32  * ALIGN_PAGE(text_end) to ALIGN_PAGE(text_end) + 0x1000)
33  *      1kB of stack per CPU (4 CPUs max).
34  */
35
36         .pushsection ._secure.text, "ax"
37
38         .arch_extension sec
39
40 #define ONE_MS                  (CONFIG_SYS_CLK_FREQ / 1000)
41 #define TEN_MS                  (10 * ONE_MS)
42 #define GICD_BASE               0x1c81000
43 #define GICC_BASE               0x1c82000
44
45 .macro  timer_wait      reg, ticks
46         @ Program CNTP_TVAL
47         movw    \reg, #(\ticks & 0xffff)
48         movt    \reg, #(\ticks >> 16)
49         mcr     p15, 0, \reg, c14, c2, 0
50         isb
51         @ Enable physical timer, mask interrupt
52         mov     \reg, #3
53         mcr     p15, 0, \reg, c14, c2, 1
54         @ Poll physical timer until ISTATUS is on
55 1:      isb
56         mrc     p15, 0, \reg, c14, c2, 1
57         ands    \reg, \reg, #4
58         bne     1b
59         @ Disable timer
60         mov     \reg, #0
61         mcr     p15, 0, \reg, c14, c2, 1
62         isb
63 .endm
64
65 .globl  psci_arch_init
66 psci_arch_init:
67         movw    r4, #(GICD_BASE & 0xffff)
68         movt    r4, #(GICD_BASE >> 16)
69
70         ldr     r5, [r4, #GICD_IGROUPRn]
71         bic     r5, r5, #(1 << 15)      @ SGI15 as Group-0
72         str     r5, [r4, #GICD_IGROUPRn]
73
74         mov     r5, #0                  @ Set SGI15 priority to 0
75         strb    r5, [r4, #(GICD_IPRIORITYRn + 15)]
76
77         add     r4, r4, #0x1000         @ GICC address
78
79         mov     r5, #0xff
80         str     r5, [r4, #GICC_PMR]     @ Be cool with non-secure
81
82         ldr     r5, [r4, #GICC_CTLR]
83         orr     r5, r5, #(1 << 3)       @ Switch FIQEn on
84         str     r5, [r4, #GICC_CTLR]
85
86         mrc     p15, 0, r5, c1, c1, 0   @ Read SCR
87         orr     r5, r5, #4              @ Enable FIQ in monitor mode
88         bic     r5, r5, #1              @ Secure mode
89         mcr     p15, 0, r5, c1, c1, 0   @ Write SCR
90         isb
91
92         mrc     p15, 0, r4, c0, c0, 5   @ MPIDR
93         and     r4, r4, #3              @ cpu number in cluster
94         mov     r5, #0x400              @ 1kB of stack per CPU
95         mul     r4, r4, r5
96
97         adr     r5, text_end            @ end of text
98         add     r5, r5, #0x2000         @ Skip two pages
99         lsr     r5, r5, #12             @ Align to start of page
100         lsl     r5, r5, #12
101         sub     sp, r5, r4              @ here's our stack!
102
103         bx      lr
104
105 .globl  psci_fiq_enter
106 psci_fiq_enter:
107         push    {r0-r12}
108
109         @ Switch to secure
110         mrc     p15, 0, r7, c1, c1, 0
111         bic     r8, r7, #1
112         mcr     p15, 0, r8, c1, c1, 0
113         isb
114
115         @ Validate reason based on IAR and acknowledge
116         movw    r8, #(GICC_BASE & 0xffff)
117         movt    r8, #(GICC_BASE >> 16)
118         ldr     r9, [r8, #GICC_IAR]
119         movw    r10, #0x3ff
120         movt    r10, #0
121         cmp     r9, r10                 @ skip spurious interrupt 1023
122         beq     out
123         movw    r10, #0x3fe             @ ...and 1022
124         cmp     r9, r10
125         beq     out
126         str     r9, [r8, #GICC_EOIR]    @ acknowledge the interrupt
127         dsb
128
129         @ Compute CPU number
130         lsr     r9, r9, #10
131         and     r9, r9, #0xf
132
133         movw    r8, #(SUN7I_CPUCFG_BASE & 0xffff)
134         movt    r8, #(SUN7I_CPUCFG_BASE >> 16)
135
136         @ Wait for the core to enter WFI
137         lsl     r11, r9, #6             @ x64
138         add     r11, r11, r8
139
140 1:      ldr     r10, [r11, #0x48]
141         tst     r10, #(1 << 2)
142         bne     2f
143         timer_wait r10, ONE_MS
144         b       1b
145
146         @ Reset CPU
147 2:      mov     r10, #0
148         str     r10, [r11, #0x40]
149
150         @ Lock CPU
151         mov     r10, #1
152         lsl     r9, r10, r9             @ r9 is now CPU mask
153         ldr     r10, [r8, #0x1e4]
154         bic     r10, r10, r9
155         str     r10, [r8, #0x1e4]
156
157         @ Set power gating
158         ldr     r10, [r8, #0x1b4]
159         orr     r10, r10, #1
160         str     r10, [r8, #0x1b4]
161         timer_wait r10, ONE_MS
162
163         @ Activate power clamp
164         mov     r10, #1
165 1:      str     r10, [r8, #0x1b0]
166         lsl     r10, r10, #1
167         orr     r10, r10, #1
168         tst     r10, #0x100
169         beq     1b
170
171         @ Restore security level
172 out:    mcr     p15, 0, r7, c1, c1, 0
173
174         pop     {r0-r12}
175         subs    pc, lr, #4
176
177         @ r1 = target CPU
178         @ r2 = target PC
179 .globl  psci_cpu_on
180 psci_cpu_on:
181         adr     r0, _target_pc
182         str     r2, [r0]
183         dsb
184
185         movw    r0, #(SUN7I_CPUCFG_BASE & 0xffff)
186         movt    r0, #(SUN7I_CPUCFG_BASE >> 16)
187
188         @ CPU mask
189         and     r1, r1, #3      @ only care about first cluster
190         mov     r4, #1
191         lsl     r4, r4, r1
192
193         adr     r6, _sunxi_cpu_entry
194         str     r6, [r0, #0x1a4] @ PRIVATE_REG (boot vector)
195
196         @ Assert reset on target CPU
197         mov     r6, #0
198         lsl     r5, r1, #6      @ 64 bytes per CPU
199         add     r5, r5, #0x40   @ Offset from base
200         add     r5, r5, r0      @ CPU control block
201         str     r6, [r5]        @ Reset CPU
202
203         @ l1 invalidate
204         ldr     r6, [r0, #0x184]
205         bic     r6, r6, r4
206         str     r6, [r0, #0x184]
207
208         @ Lock CPU
209         ldr     r6, [r0, #0x1e4]
210         bic     r6, r6, r4
211         str     r6, [r0, #0x1e4]
212
213         @ Release power clamp
214         movw    r6, #0x1ff
215         movt    r6, #0
216 1:      lsrs    r6, r6, #1
217         str     r6, [r0, #0x1b0]
218         bne     1b
219
220         timer_wait r1, TEN_MS
221
222         @ Clear power gating
223         ldr     r6, [r0, #0x1b4]
224         bic     r6, r6, #1
225         str     r6, [r0, #0x1b4]
226
227         @ Deassert reset on target CPU
228         mov     r6, #3
229         str     r6, [r5]
230
231         @ Unlock CPU
232         ldr     r6, [r0, #0x1e4]
233         orr     r6, r6, r4
234         str     r6, [r0, #0x1e4]
235
236         mov     r0, #ARM_PSCI_RET_SUCCESS       @ Return PSCI_RET_SUCCESS
237         mov     pc, lr
238
239 _target_pc:
240         .word   0
241
242 /* Imported from Linux kernel */
243 v7_flush_dcache_all:
244         dmb                                     @ ensure ordering with previous memory accesses
245         mrc     p15, 1, r0, c0, c0, 1           @ read clidr
246         ands    r3, r0, #0x7000000              @ extract loc from clidr
247         mov     r3, r3, lsr #23                 @ left align loc bit field
248         beq     finished                        @ if loc is 0, then no need to clean
249         mov     r10, #0                         @ start clean at cache level 0
250 flush_levels:
251         add     r2, r10, r10, lsr #1            @ work out 3x current cache level
252         mov     r1, r0, lsr r2                  @ extract cache type bits from clidr
253         and     r1, r1, #7                      @ mask of the bits for current cache only
254         cmp     r1, #2                          @ see what cache we have at this level
255         blt     skip                            @ skip if no cache, or just i-cache
256         mrs     r9, cpsr                        @ make cssr&csidr read atomic
257         mcr     p15, 2, r10, c0, c0, 0          @ select current cache level in cssr
258         isb                                     @ isb to sych the new cssr&csidr
259         mrc     p15, 1, r1, c0, c0, 0           @ read the new csidr
260         msr     cpsr_c, r9
261         and     r2, r1, #7                      @ extract the length of the cache lines
262         add     r2, r2, #4                      @ add 4 (line length offset)
263         ldr     r4, =0x3ff
264         ands    r4, r4, r1, lsr #3              @ find maximum number on the way size
265         clz     r5, r4                          @ find bit position of way size increment
266         ldr     r7, =0x7fff
267         ands    r7, r7, r1, lsr #13             @ extract max number of the index size
268 loop1:
269         mov     r9, r7                          @ create working copy of max index
270 loop2:
271         orr     r11, r10, r4, lsl r5            @ factor way and cache number into r11
272         orr     r11, r11, r9, lsl r2            @ factor index number into r11
273         mcr     p15, 0, r11, c7, c14, 2         @ clean & invalidate by set/way
274         subs    r9, r9, #1                      @ decrement the index
275         bge     loop2
276         subs    r4, r4, #1                      @ decrement the way
277         bge     loop1
278 skip:
279         add     r10, r10, #2                    @ increment cache number
280         cmp     r3, r10
281         bgt     flush_levels
282 finished:
283         mov     r10, #0                         @ swith back to cache level 0
284         mcr     p15, 2, r10, c0, c0, 0          @ select current cache level in cssr
285         dsb     st
286         isb
287         bx      lr
288
289 _sunxi_cpu_entry:
290         @ Set SMP bit
291         mrc     p15, 0, r0, c1, c0, 1
292         orr     r0, r0, #0x40
293         mcr     p15, 0, r0, c1, c0, 1
294         isb
295
296         bl      _nonsec_init
297         bl      psci_arch_init
298
299         adr     r0, _target_pc
300         ldr     r0, [r0]
301         b       _do_nonsec_entry
302
303 .globl  psci_cpu_off
304 psci_cpu_off:
305         mrc     p15, 0, r0, c1, c0, 0           @ SCTLR
306         bic     r0, r0, #(1 << 2)               @ Clear C bit
307         mcr     p15, 0, r0, c1, c0, 0           @ SCTLR
308         isb
309         dsb
310
311         bl      v7_flush_dcache_all
312
313         clrex                                   @ Why???
314
315         mrc     p15, 0, r0, c1, c0, 1           @ ACTLR
316         bic     r0, r0, #(1 << 6)               @ Clear SMP bit
317         mcr     p15, 0, r0, c1, c0, 1           @ ACTLR
318         isb
319         dsb
320
321         @ Ask CPU0 to pull the rug...
322         movw    r0, #(GICD_BASE & 0xffff)
323         movt    r0, #(GICD_BASE >> 16)
324         movw    r1, #15                         @ SGI15
325         movt    r1, #1                          @ Target is CPU0
326         str     r1, [r0, #GICD_SGIR]
327         dsb
328
329 1:      wfi
330         b       1b
331
332 text_end:
333         .popsection