]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/standalone_v6_6/src/boot.S
Update Zynq, MPSoc Cortex-A53 and MPSoc Cortex-R5 demo projects to build with the...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / standalone_v6_6 / src / boot.S
1 /******************************************************************************
2 *
3 * Copyright (C) 2014 - 2018 Xilinx, Inc. All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 * @file boot.S
35 *
36 * @addtogroup a53_64_boot_code Cortex A53 64bit Processor Boot Code
37 * @{
38 * <h2> boot.S </h2>
39 *
40 * The boot code performs minimum configuration which is required for an
41 * application. Cortex-A53 starts by checking current exception level. If the
42 * current exception level is EL3 and BSP is built for EL3, it will do
43 * initialization required for application execution at EL3. Below is a
44 * sequence illustrating what all configuration is performed before control
45 * reaches to main function for EL3 execution.
46 *
47 * 1. Program vector table base for exception handling
48 * 2. Set reset vector table base address
49 * 3. Program stack pointer for EL3
50 * 4. Routing of interrupts to EL3
51 * 5. Enable ECC protection
52 * 6. Program generic counter frequency
53 * 7. Invalidate instruction cache, data cache and TLBs
54 * 8. Configure MMU registers and program base address of translation table
55 * 9. Transfer control to _start which clears BSS sections and runs global
56 *    constructor before jumping to main application
57 *
58 * If the current exception level is EL1 and BSP is also built for EL1_NONSECURE
59 * it will perform initialization required for application execution at EL1
60 * non-secure. For all other combination, the execution will go into infinite
61 * loop. Below is a sequence illustrating what all configuration is performed
62 * before control reaches to main function for EL1 execution.
63 *
64 * 1. Program vector table base for exception handling
65 * 2. Program stack pointer for EL1
66 * 3. Invalidate instruction cache, data cache and TLBs
67 * 4. Configure MMU registers and program base address of translation table
68 * 5. Transfer control to _start which clears BSS sections and runs global
69 *    constructor before jumping to main application
70 *
71 * <pre>
72 * MODIFICATION HISTORY:
73 *
74 * Ver   Who     Date     Changes
75 * ----- ------- -------- ---------------------------------------------------
76 * 5.00  pkp     05/21/14 Initial version
77 * 6.00  pkp     07/25/16 Program the counter frequency
78 * 6.02  pkp     01/22/17 Added support for EL1 non-secure
79 * 6.02  pkp     01/24/17 Clearing status of FPUStatus variable to ensure it
80 *                        holds correct value.
81 * 6.3   mus 04/20/17 CPU Cache protection bit in the L2CTLR_EL1 will be in
82 *                    set state on reset. So, setting that bit through boot
83 *                    code is redundant, hence removed the code which sets
84 *                    CPU cache protection bit.
85 * 6.4   mus      08/11/17 Implemented ARM erratum 855873.It fixes
86 *                         CR#982209.
87 * 6.6   mus      01/19/18 Added isb after writing to the cpacr_el1/cptr_el3,
88 *                         to ensure floating-point unit is disabled, before
89 *                         any subsequent instruction.
90 *
91 *
92 ******************************************************************************/
93
94 #include "xparameters.h"
95 #include "bspconfig.h"
96 #include "xil_errata.h"
97
98 .globl MMUTableL0
99 .globl MMUTableL1
100 .globl MMUTableL2
101 .global _prestart
102 .global _boot
103
104 .global __el3_stack
105 .global __el2_stack
106 .global __el1_stack
107 .global __el0_stack
108 .global _vector_table
109
110 .set EL3_stack,         __el3_stack
111 .set EL2_stack,         __el2_stack
112 .set EL1_stack,         __el1_stack
113 .set EL0_stack,         __el0_stack
114
115 .set TT_S1_FAULT,       0x0
116 .set TT_S1_TABLE,       0x3
117
118 .set L0Table,   MMUTableL0
119 .set L1Table,   MMUTableL1
120 .set L2Table,   MMUTableL2
121 .set vector_base,       _vector_table
122 .set rvbar_base,        0xFD5C0040
123
124 .set counterfreq,       XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ
125 .set MODE_EL1, 0x5
126 .set DAIF_BIT,  0x1C0
127
128 .section .boot,"ax"
129
130
131 /* this initializes the various processor modes */
132
133 _prestart:
134 _boot:
135         mov      x0, #0
136         mov      x1, #0
137         mov      x2, #0
138         mov      x3, #0
139         mov      x4, #0
140         mov      x5, #0
141         mov      x6, #0
142         mov      x7, #0
143         mov      x8, #0
144         mov      x9, #0
145         mov      x10, #0
146         mov      x11, #0
147         mov      x12, #0
148         mov      x13, #0
149         mov      x14, #0
150         mov      x15, #0
151         mov      x16, #0
152         mov      x17, #0
153         mov      x18, #0
154         mov      x19, #0
155         mov      x20, #0
156         mov      x21, #0
157         mov      x22, #0
158         mov      x23, #0
159         mov      x24, #0
160         mov      x25, #0
161         mov      x26, #0
162         mov      x27, #0
163         mov      x28, #0
164         mov      x29, #0
165         mov      x30, #0
166 #if 0 //dont put other a53 cpus in wfi
167    //Which core am I
168    // ----------------
169         mrs      x0, MPIDR_EL1
170         and      x0, x0, #0xFF                        //Mask off to leave Aff0
171         cbz      x0, OKToRun                          //If core 0, run the primary init code
172 EndlessLoop0:
173         wfi
174         b        EndlessLoop0
175 #endif
176 OKToRun:
177
178         mrs     x0, currentEL
179         cmp     x0, #0xC
180         beq     InitEL3
181
182         cmp     x0, #0x4
183         beq     InitEL1
184
185         b       error                   // go to error if current exception level is neither EL3 nor EL1
186 InitEL3:
187 .if (EL3 == 1)
188         /*Set vector table base address*/
189         ldr     x1, =vector_base
190         msr     VBAR_EL3,x1
191
192         /* Set reset vector address */
193         /* Get the cpu ID */
194         mrs  x0, MPIDR_EL1
195         and  x0, x0, #0xFF
196         mov  w0, w0
197         ldr      w2, =rvbar_base
198         /* calculate the rvbar base address for particular CPU core */
199         mov      w3, #0x8
200         mul      w0, w0, w3
201         add      w2, w2, w0
202         /* store vector base address to RVBAR */
203         str  x1, [x2]
204
205         /*Define stack pointer for current exception level*/
206         ldr      x2,=EL3_stack
207         mov      sp,x2
208
209         /* Enable Trapping of SIMD/FPU register for standalone BSP */
210         mov      x0, #0
211 #ifndef FREERTOS_BSP
212         orr      x0, x0, #(0x1 << 10)
213 #endif
214         msr      CPTR_EL3, x0
215         isb
216
217         /*
218          * Clear FPUStatus variable to make sure that it contains current
219          * status of FPU i.e. disabled. In case of a warm restart execution
220          * when bss sections are not cleared, it may contain previously updated
221          * value which does not hold true now.
222          */
223 #ifndef FREERTOS_BSP
224          ldr x0,=FPUStatus
225          str xzr, [x0]
226 #endif
227         /* Configure SCR_EL3 */
228         mov      w1, #0                 //; Initial value of register is unknown
229         orr      w1, w1, #(1 << 11)     //; Set ST bit (Secure EL1 can access CNTPS_TVAL_EL1, CNTPS_CTL_EL1 & CNTPS_CVAL_EL1)
230         orr      w1, w1, #(1 << 10)     //; Set RW bit (EL1 is AArch64, as this is the Secure world)
231         orr      w1, w1, #(1 << 3)      //; Set EA bit (SError routed to EL3)
232         orr      w1, w1, #(1 << 2)      //; Set FIQ bit (FIQs routed to EL3)
233         orr      w1, w1, #(1 << 1)      //; Set IRQ bit (IRQs routed to EL3)
234         msr      SCR_EL3, x1
235
236         /*configure cpu auxiliary control register EL1 */
237         ldr     x0,=0x80CA000           // L1 Data prefetch control - 5, Enable device split throttle, 2 independent data prefetch streams
238 #if CONFIG_ARM_ERRATA_855873
239         /*
240          *  Set ENDCCASCI bit in CPUACTLR_EL1 register, to execute data
241          *  cache clean operations as data cache clean and invalidate
242          *
243          */
244         orr     x0, x0, #(1 << 44)      //; Set ENDCCASCI bit
245 #endif
246         msr     S3_1_C15_C2_0, x0       //CPUACTLR_EL1
247
248         /* program the counter frequency */
249         ldr     x0,=counterfreq
250         msr     CNTFRQ_EL0, x0
251
252         /*Enable hardware coherency between cores*/
253         mrs      x0, S3_1_c15_c2_1      //Read EL1 CPU Extended Control Register
254         orr      x0, x0, #(1 << 6)      //Set the SMPEN bit
255         msr      S3_1_c15_c2_1, x0      //Write EL1 CPU Extended Control Register
256         isb
257
258         tlbi    ALLE3
259         ic      IALLU                   //; Invalidate I cache to PoU
260         bl      invalidate_dcaches
261         dsb      sy
262         isb
263
264         ldr      x1, =L0Table           //; Get address of level 0 for TTBR0_EL3
265         msr      TTBR0_EL3, x1          //; Set TTBR0_EL3
266
267         /**********************************************
268         * Set up memory attributes
269         * This equates to:
270         * 0 = b01000100 = Normal, Inner/Outer Non-Cacheable
271         * 1 = b11111111 = Normal, Inner/Outer WB/WA/RA
272         * 2 = b00000000 = Device-nGnRnE
273         * 3 = b00000100 = Device-nGnRE
274         * 4 = b10111011 = Normal, Inner/Outer WT/WA/RA
275         **********************************************/
276         ldr      x1, =0x000000BB0400FF44
277         msr      MAIR_EL3, x1
278
279         /**********************************************
280          * Set up TCR_EL3
281          * Physical Address Size PS =  010 -> 40bits 1TB
282          * Granual Size TG0 = 00 -> 4KB
283          * size offset of the memory region T0SZ = 24 -> (region size 2^(64-24) = 2^40)
284          ***************************************************/
285         ldr     x1,=0x80823518
286         msr     TCR_EL3, x1
287         isb
288
289         /* Enable SError Exception for asynchronous abort */
290         mrs     x1,DAIF
291         bic     x1,x1,#(0x1<<8)
292         msr     DAIF,x1
293
294         /* Configure SCTLR_EL3 */
295         mov      x1, #0                //Most of the SCTLR_EL3 bits are unknown at reset
296         orr      x1, x1, #(1 << 12)     //Enable I cache
297         orr      x1, x1, #(1 << 3)      //Enable SP alignment check
298         orr      x1, x1, #(1 << 2)      //Enable caches
299         orr      x1, x1, #(1 << 0)      //Enable MMU
300         msr      SCTLR_EL3, x1
301         dsb      sy
302         isb
303
304         b        _startup               //jump to start
305 .else
306         b       error                   // present exception level and selected exception level mismatch
307 .endif
308
309 InitEL1:
310 .if (EL1_NONSECURE == 1)
311         /*Set vector table base address*/
312         ldr     x1, =vector_base
313         msr     VBAR_EL1,x1
314
315         mrs     x0, CPACR_EL1
316         bic     x0, x0, #(0x3 << 0x20)
317         msr     CPACR_EL1, x0
318         isb
319
320         /*
321          * Clear FPUStatus variable to make sure that it contains current
322          * status of FPU i.e. disabled. In case of a warm restart execution
323          * when bss sections are not cleared, it may contain previously updated
324          * value which does not hold true now.
325          */
326 #ifndef FREERTOS_BSP
327          ldr x0,=FPUStatus
328          str xzr, [x0]
329 #endif
330         /*Define stack pointer for current exception level*/
331         ldr      x2,=EL1_stack
332         mov      sp,x2
333
334         /* Disable MMU first */
335         mov     x1,#0x0
336         msr     SCTLR_EL1, x1
337         isb
338
339         TLBI    VMALLE1
340
341         ic      IALLU                   //; Invalidate I cache to PoU
342         bl      invalidate_dcaches
343         dsb      sy
344         isb
345
346         ldr      x1, =L0Table           //; Get address of level 0 for TTBR0_EL1
347         msr      TTBR0_EL1, x1          //; Set TTBR0_EL1
348
349         /**********************************************
350         * Set up memory attributes
351         * This equates to:
352         * 0 = b01000100 = Normal, Inner/Outer Non-Cacheable
353         * 1 = b11111111 = Normal, Inner/Outer WB/WA/RA
354         * 2 = b00000000 = Device-nGnRnE
355         * 3 = b00000100 = Device-nGnRE
356         * 4 = b10111011 = Normal, Inner/Outer WT/WA/RA
357         **********************************************/
358         ldr      x1, =0x000000BB0400FF44
359         msr      MAIR_EL1, x1
360
361         /**********************************************
362         * Set up TCR_EL1
363         * Physical Address Size PS =  010 -> 40bits 1TB
364         * Granual Size TG0 = 00 -> 4KB
365         * size offset of the memory region T0SZ = 24 -> (region size 2^(64-24) = 2^40)
366         ***************************************************/
367         ldr     x1,=0x285800518
368         msr     TCR_EL1, x1
369         isb
370         /* Enable SError Exception for asynchronous abort */
371         mrs     x1,DAIF
372         bic     x1,x1,#(0x1<<8)
373         msr     DAIF,x1
374
375         //; Enable MMU
376         mov     x1,#0x0
377         orr     x1, x1, #(1 << 18)    // ; Set WFE non trapping
378         orr     x1, x1, #(1 << 17)    // ; Set WFI non trapping
379         orr     x1, x1, #(1 << 5)    // ; Set CP15 barrier enabled
380         orr     x1, x1, #(1 << 12)    // ; Set I bit
381         orr     x1, x1, #(1 << 2)    // ; Set C bit
382         orr     x1, x1, #(1 << 0)    // ; Set M bit
383         msr     SCTLR_EL1, x1
384         isb
385
386         bl       _startup               //jump to start
387 .else
388         b       error                   // present exception level and selected exception level mismatch
389 .endif
390
391 error:  b       error
392
393
394 invalidate_dcaches:
395
396         dmb     ISH
397         mrs     x0, CLIDR_EL1          //; x0 = CLIDR
398         ubfx    w2, w0, #24, #3        //; w2 = CLIDR.LoC
399         cmp     w2, #0                 //; LoC is 0?
400         b.eq    invalidateCaches_end   //; No cleaning required and enable MMU
401         mov     w1, #0                 //; w1 = level iterator
402
403 invalidateCaches_flush_level:
404         add     w3, w1, w1, lsl #1     //; w3 = w1 * 3 (right-shift for cache type)
405         lsr     w3, w0, w3             //; w3 = w0 >> w3
406         ubfx    w3, w3, #0, #3         //; w3 = cache type of this level
407         cmp     w3, #2                 //; No cache at this level?
408         b.lt    invalidateCaches_next_level
409
410         lsl     w4, w1, #1
411         msr     CSSELR_EL1, x4         //; Select current cache level in CSSELR
412         isb                            //; ISB required to reflect new CSIDR
413         mrs     x4, CCSIDR_EL1         //; w4 = CSIDR
414
415         ubfx    w3, w4, #0, #3
416         add     w3, w3, #2             //; w3 = log2(line size)
417         ubfx    w5, w4, #13, #15
418         ubfx    w4, w4, #3, #10        //; w4 = Way number
419         clz     w6, w4                 //; w6 = 32 - log2(number of ways)
420
421 invalidateCaches_flush_set:
422         mov     w8, w4                 //; w8 = Way number
423 invalidateCaches_flush_way:
424         lsl     w7, w1, #1             //; Fill level field
425         lsl     w9, w5, w3
426         orr     w7, w7, w9             //; Fill index field
427         lsl     w9, w8, w6
428         orr     w7, w7, w9             //; Fill way field
429         dc      CISW, x7               //; Invalidate by set/way to point of coherency
430         subs    w8, w8, #1             //; Decrement way
431         b.ge    invalidateCaches_flush_way
432         subs    w5, w5, #1             //; Descrement set
433         b.ge    invalidateCaches_flush_set
434
435 invalidateCaches_next_level:
436         add     w1, w1, #1             //; Next level
437         cmp     w2, w1
438         b.gt    invalidateCaches_flush_level
439
440 invalidateCaches_end:
441         ret
442
443 .end
444 /**
445 * @} End of "addtogroup a53_64_boot_code".
446 */