ARM: sunxi: Add sun6i specific PSCI implementation
[oweals/u-boot.git] / arch / arm / cpu / armv7 / sunxi / psci_sun6i.S
1 /*
2  * Copyright (C) 2015 - Chen-Yu Tsai
3  * Author: Chen-Yu Tsai <wens@csie.org>
4  *
5  * Based on psci_sun7i.S by Marc Zyngier <marc.zyngier@arm.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/macro.h>
23 #include <asm/psci.h>
24 #include <asm/arch/cpu.h>
25
26 /*
27  * Memory layout:
28  *
29  * SECURE_RAM to text_end :
30  *      ._secure_text section
31  * text_end to ALIGN_PAGE(text_end):
32  *      nothing
33  * ALIGN_PAGE(text_end) to ALIGN_PAGE(text_end) + 0x1000)
34  *      1kB of stack per CPU (4 CPUs max).
35  */
36
37         .pushsection ._secure.text, "ax"
38
39         .arch_extension sec
40
41 #define ONE_MS                  (CONFIG_TIMER_CLK_FREQ / 1000)
42 #define TEN_MS                  (10 * ONE_MS)
43 #define GICD_BASE               0x1c81000
44 #define GICC_BASE               0x1c82000
45
46 .macro  timer_wait      reg, ticks
47         @ Program CNTP_TVAL
48         movw    \reg, #(\ticks & 0xffff)
49         movt    \reg, #(\ticks >> 16)
50         mcr     p15, 0, \reg, c14, c2, 0
51         isb
52         @ Enable physical timer, mask interrupt
53         mov     \reg, #3
54         mcr     p15, 0, \reg, c14, c2, 1
55         @ Poll physical timer until ISTATUS is on
56 1:      isb
57         mrc     p15, 0, \reg, c14, c2, 1
58         ands    \reg, \reg, #4
59         bne     1b
60         @ Disable timer
61         mov     \reg, #0
62         mcr     p15, 0, \reg, c14, c2, 1
63         isb
64 .endm
65
66 .globl  psci_fiq_enter
67 psci_fiq_enter:
68         push    {r0-r12}
69
70         @ Switch to secure
71         mrc     p15, 0, r7, c1, c1, 0
72         bic     r8, r7, #1
73         mcr     p15, 0, r8, c1, c1, 0
74         isb
75
76         @ Validate reason based on IAR and acknowledge
77         movw    r8, #(GICC_BASE & 0xffff)
78         movt    r8, #(GICC_BASE >> 16)
79         ldr     r9, [r8, #GICC_IAR]
80         movw    r10, #0x3ff
81         movt    r10, #0
82         cmp     r9, r10                 @ skip spurious interrupt 1023
83         beq     out
84         movw    r10, #0x3fe             @ ...and 1022
85         cmp     r9, r10
86         beq     out
87         str     r9, [r8, #GICC_EOIR]    @ acknowledge the interrupt
88         dsb
89
90         @ Compute CPU number
91         lsr     r9, r9, #10
92         and     r9, r9, #0xf
93
94         movw    r8, #(SUN6I_CPUCFG_BASE & 0xffff)
95         movt    r8, #(SUN6I_CPUCFG_BASE >> 16)
96
97         @ Wait for the core to enter WFI
98         lsl     r11, r9, #6             @ x64
99         add     r11, r11, r8
100
101 1:      ldr     r10, [r11, #0x48]
102         tst     r10, #(1 << 2)
103         bne     2f
104         timer_wait r10, ONE_MS
105         b       1b
106
107         @ Reset CPU
108 2:      mov     r10, #0
109         str     r10, [r11, #0x40]
110
111         @ Lock CPU
112         mov     r10, #1
113         lsl     r11, r10, r9            @ r11 is now CPU mask
114         ldr     r10, [r8, #0x1e4]
115         bic     r10, r10, r11
116         str     r10, [r8, #0x1e4]
117
118         movw    r8, #(SUNXI_PRCM_BASE & 0xffff)
119         movt    r8, #(SUNXI_PRCM_BASE >> 16)
120
121         @ Set power gating
122         ldr     r10, [r8, #0x100]
123         orr     r10, r10, r11
124         str     r10, [r8, #0x100]
125         timer_wait r10, ONE_MS
126
127         @ Activate power clamp
128         lsl     r12, r9, #2             @ x4
129         add     r12, r12, r8
130         mov     r10, #0xff
131         str     r10, [r12, #0x140]
132
133         movw    r8, #(SUN6I_CPUCFG_BASE & 0xffff)
134         movt    r8, #(SUN6I_CPUCFG_BASE >> 16)
135
136         @ Unlock CPU
137         ldr     r10, [r8, #0x1e4]
138         orr     r10, r10, r11
139         str     r10, [r8, #0x1e4]
140
141         @ Restore security level
142 out:    mcr     p15, 0, r7, c1, c1, 0
143
144         pop     {r0-r12}
145         subs    pc, lr, #4
146
147         @ r1 = target CPU
148         @ r2 = target PC
149 .globl  psci_cpu_on
150 psci_cpu_on:
151         push    {lr}
152
153         mov     r0, r1
154         bl      psci_get_cpu_stack_top  @ get stack top of target CPU
155         str     r2, [r0]                @ store target PC at stack top
156         dsb
157
158         movw    r0, #(SUN6I_CPUCFG_BASE & 0xffff)
159         movt    r0, #(SUN6I_CPUCFG_BASE >> 16)
160
161         @ CPU mask
162         and     r1, r1, #3      @ only care about first cluster
163         mov     r4, #1
164         lsl     r4, r4, r1
165
166         ldr     r6, =psci_cpu_entry
167         str     r6, [r0, #0x1a4] @ PRIVATE_REG (boot vector)
168
169         @ Assert reset on target CPU
170         mov     r6, #0
171         lsl     r5, r1, #6      @ 64 bytes per CPU
172         add     r5, r5, #0x40   @ Offset from base
173         add     r5, r5, r0      @ CPU control block
174         str     r6, [r5]        @ Reset CPU
175
176         @ l1 invalidate
177         ldr     r6, [r0, #0x184] @ CPUCFG_GEN_CTRL_REG
178         bic     r6, r6, r4
179         str     r6, [r0, #0x184]
180
181         @ Lock CPU (Disable external debug access)
182         ldr     r6, [r0, #0x1e4] @ CPUCFG_DBG_CTL1_REG
183         bic     r6, r6, r4
184         str     r6, [r0, #0x1e4]
185
186         movw    r0, #(SUNXI_PRCM_BASE & 0xffff)
187         movt    r0, #(SUNXI_PRCM_BASE >> 16)
188
189         @ Release power clamp
190         lsl     r5, r1, #2      @ 1 register per CPU
191         add     r5, r5, r0      @ PRCM
192         movw    r6, #0x1ff
193         movt    r6, #0
194 1:      lsrs    r6, r6, #1
195         str     r6, [r5, #0x140] @ CPUx_PWR_CLAMP
196         bne     1b
197
198         timer_wait r6, TEN_MS
199
200         @ Clear power gating
201         ldr     r6, [r0, #0x100] @ CPU_PWROFF_GATING
202         bic     r6, r6, r4
203         str     r6, [r0, #0x100]
204
205         @ re-calculate CPU control register address
206         movw    r0, #(SUN6I_CPUCFG_BASE & 0xffff)
207         movt    r0, #(SUN6I_CPUCFG_BASE >> 16)
208
209         @ Deassert reset on target CPU
210         mov     r6, #3
211         lsl     r5, r1, #6      @ 64 bytes per CPU
212         add     r5, r5, #0x40   @ Offset from base
213         add     r5, r5, r0      @ CPU control block
214         str     r6, [r5]
215
216         @ Unlock CPU (Enable external debug access)
217         ldr     r6, [r0, #0x1e4] @ CPUCFG_DBG_CTL1_REG
218         orr     r6, r6, r4
219         str     r6, [r0, #0x1e4]
220
221         mov     r0, #ARM_PSCI_RET_SUCCESS       @ Return PSCI_RET_SUCCESS
222         pop     {pc}
223
224 .globl  psci_cpu_off
225 psci_cpu_off:
226         bl      psci_cpu_off_common
227
228         @ Ask CPU0 to pull the rug...
229         movw    r0, #(GICD_BASE & 0xffff)
230         movt    r0, #(GICD_BASE >> 16)
231         movw    r1, #15                         @ SGI15
232         movt    r1, #1                          @ Target is CPU0
233         str     r1, [r0, #GICD_SGIR]
234         dsb
235
236 1:      wfi
237         b       1b
238
239 .globl  psci_arch_init
240 psci_arch_init:
241         mov     r6, lr
242
243         movw    r4, #(GICD_BASE & 0xffff)
244         movt    r4, #(GICD_BASE >> 16)
245
246         ldr     r5, [r4, #GICD_IGROUPRn]
247         bic     r5, r5, #(1 << 15)      @ SGI15 as Group-0
248         str     r5, [r4, #GICD_IGROUPRn]
249
250         mov     r5, #0                  @ Set SGI15 priority to 0
251         strb    r5, [r4, #(GICD_IPRIORITYRn + 15)]
252
253         add     r4, r4, #0x1000         @ GICC address
254
255         mov     r5, #0xff
256         str     r5, [r4, #GICC_PMR]     @ Be cool with non-secure
257
258         ldr     r5, [r4, #GICC_CTLR]
259         orr     r5, r5, #(1 << 3)       @ Switch FIQEn on
260         str     r5, [r4, #GICC_CTLR]
261
262         mrc     p15, 0, r5, c1, c1, 0   @ Read SCR
263         orr     r5, r5, #4              @ Enable FIQ in monitor mode
264         bic     r5, r5, #1              @ Secure mode
265         mcr     p15, 0, r5, c1, c1, 0   @ Write SCR
266         isb
267
268         bl      psci_get_cpu_id         @ CPU ID => r0
269         bl      psci_get_cpu_stack_top  @ stack top => r0
270         mov     sp, r0
271
272         bx      r6
273
274         .globl psci_text_end
275 psci_text_end:
276         .popsection