|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Raspberry Pi (Trading) Ltd. |
| 3 | + * Copyright (c) 2016 Stephen Warren <[email protected]> |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * * Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * * Neither the name of the copyright holder nor the names of its contributors |
| 14 | + * may be used to endorse or promote products derived from this software |
| 15 | + * without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +#define BIT(x) (1 << (x)) |
| 31 | + |
| 32 | +#define LOCAL_CONTROL 0xff800000 |
| 33 | +#define LOCAL_PRESCALER 0xff800008 |
| 34 | +#define GIC_DISTB 0xff841000 |
| 35 | +#define GIC_CPUB 0xff842000 |
| 36 | + |
| 37 | +#define OSC_FREQ 54000000 |
| 38 | + |
| 39 | +#define SCR_RW BIT(10) |
| 40 | +#define SCR_HCE BIT(8) |
| 41 | +#define SCR_SMD BIT(7) |
| 42 | +#define SCR_RES1_5 BIT(5) |
| 43 | +#define SCR_RES1_4 BIT(4) |
| 44 | +#define SCR_NS BIT(0) |
| 45 | +#define SCR_VAL \ |
| 46 | + (SCR_RW | SCR_HCE | SCR_SMD | SCR_RES1_5 | SCR_RES1_4 | SCR_NS) |
| 47 | + |
| 48 | +#define CPUECTLR_EL1 S3_1_C15_C2_1 |
| 49 | +#define CPUECTLR_EL1_SMPEN BIT(6) |
| 50 | + |
| 51 | +#define SPSR_EL3_D BIT(9) |
| 52 | +#define SPSR_EL3_A BIT(8) |
| 53 | +#define SPSR_EL3_I BIT(7) |
| 54 | +#define SPSR_EL3_F BIT(6) |
| 55 | +#define SPSR_EL3_MODE_EL2H 9 |
| 56 | +#define SPSR_EL3_VAL \ |
| 57 | + (SPSR_EL3_D | SPSR_EL3_A | SPSR_EL3_I | SPSR_EL3_F | SPSR_EL3_MODE_EL2H) |
| 58 | + |
| 59 | +#define L2CTLR_EL1 S3_1_C11_C0_2 |
| 60 | + |
| 61 | + |
| 62 | +#define GICC_CTRLR 0x0 |
| 63 | +#define GICC_PMR 0x4 |
| 64 | +#define IT_NR 0x8 // Number of interrupt enable registers (256 total irqs) |
| 65 | +#define GICD_CTRLR 0x0 |
| 66 | +#define GICD_IGROUPR 0x80 |
| 67 | + |
| 68 | +.globl _start |
| 69 | +_start: |
| 70 | + /* |
| 71 | + * LOCAL_CONTROL: |
| 72 | + * Bit 9 clear: Increment by 1 (vs. 2). |
| 73 | + * Bit 8 clear: Timer source is 19.2MHz crystal (vs. APB). |
| 74 | + */ |
| 75 | + mov x0, LOCAL_CONTROL |
| 76 | + str wzr, [x0] |
| 77 | + /* LOCAL_PRESCALER; divide-by (0x80000000 / register_val) == 1 */ |
| 78 | + mov w1, 0x80000000 |
| 79 | + str w1, [x0, #(LOCAL_PRESCALER - LOCAL_CONTROL)] |
| 80 | + |
| 81 | + /* Set L2 read/write cache latency to 3 */ |
| 82 | + mrs x0, L2CTLR_EL1 |
| 83 | + mov x1, #0x22 |
| 84 | + orr x0, x0, x1 |
| 85 | + msr L2CTLR_EL1, x0 |
| 86 | + |
| 87 | + /* Set up CNTFRQ_EL0 */ |
| 88 | + ldr x0, =OSC_FREQ |
| 89 | + msr CNTFRQ_EL0, x0 |
| 90 | + |
| 91 | + /* Set up CNTVOFF_EL2 */ |
| 92 | + msr CNTVOFF_EL2, xzr |
| 93 | + |
| 94 | + /* Enable FP/SIMD */ |
| 95 | + /* All set bits below are res1; bit 10 (TFP) is set to 0 */ |
| 96 | + mov x0, #0x33ff |
| 97 | + msr CPTR_EL3, x0 |
| 98 | + |
| 99 | + /* Set up SCR */ |
| 100 | + mov x0, #SCR_VAL |
| 101 | + msr SCR_EL3, x0 |
| 102 | + |
| 103 | + /* Set SMPEN */ |
| 104 | + mov x0, #CPUECTLR_EL1_SMPEN |
| 105 | + msr CPUECTLR_EL1, x0 |
| 106 | + |
| 107 | +#ifdef GIC |
| 108 | + bl setup_gic |
| 109 | +#endif |
| 110 | + /* |
| 111 | + * Set up SCTLR_EL2 |
| 112 | + * All set bits below are res1. LE, no WXN/I/SA/C/A/M |
| 113 | + */ |
| 114 | + ldr x0, =0x30c50830 |
| 115 | + msr SCTLR_EL2, x0 |
| 116 | + |
| 117 | + /* Switch to EL2 */ |
| 118 | + mov x0, #SPSR_EL3_VAL |
| 119 | + msr spsr_el3, x0 |
| 120 | + adr x0, in_el2 |
| 121 | + msr elr_el3, x0 |
| 122 | + eret |
| 123 | +in_el2: |
| 124 | + |
| 125 | + mrs x6, MPIDR_EL1 |
| 126 | + and x6, x6, #0x3 |
| 127 | + cbz x6, primary_cpu |
| 128 | + |
| 129 | + adr x5, spin_cpu0 |
| 130 | +secondary_spin: |
| 131 | + wfe |
| 132 | + ldr x4, [x5, x6, lsl #3] |
| 133 | + cbz x4, secondary_spin |
| 134 | + mov x0, #0 |
| 135 | + b boot_kernel |
| 136 | + |
| 137 | +primary_cpu: |
| 138 | + ldr w4, kernel_entry32 |
| 139 | + ldr w0, dtb_ptr32 |
| 140 | + |
| 141 | +boot_kernel: |
| 142 | + mov x1, #0 |
| 143 | + mov x2, #0 |
| 144 | + mov x3, #0 |
| 145 | + br x4 |
| 146 | + |
| 147 | +.ltorg |
| 148 | + |
| 149 | +.org 0xd8 |
| 150 | +.globl spin_cpu0 |
| 151 | +spin_cpu0: |
| 152 | + .quad 0 |
| 153 | +.org 0xe0 |
| 154 | +.globl spin_cpu1 |
| 155 | +spin_cpu1: |
| 156 | + .quad 0 |
| 157 | +.org 0xe8 |
| 158 | +.globl spin_cpu2 |
| 159 | +spin_cpu2: |
| 160 | + .quad 0 |
| 161 | +.org 0xf0 |
| 162 | +.globl spin_cpu3 |
| 163 | +spin_cpu3: |
| 164 | + # Shared with next two symbols/.word |
| 165 | + # FW clears the next 8 bytes after reading the initial value, leaving |
| 166 | + # the location suitable for use as spin_cpu3 |
| 167 | +.org 0xf0 |
| 168 | +.globl stub_magic |
| 169 | +stub_magic: |
| 170 | + .word 0x5afe570b |
| 171 | +.org 0xf4 |
| 172 | +.globl stub_version |
| 173 | +stub_version: |
| 174 | + .word 0 |
| 175 | +.org 0xf8 |
| 176 | +.globl dtb_ptr32 |
| 177 | +dtb_ptr32: |
| 178 | + .word 0x0 |
| 179 | +.org 0xfc |
| 180 | +.globl kernel_entry32 |
| 181 | +kernel_entry32: |
| 182 | + .word 0x0 |
| 183 | + |
| 184 | +.org 0x100 |
| 185 | + |
| 186 | +#ifdef GIC |
| 187 | + |
| 188 | +setup_gic: // Called from secure mode - set all interrupts to group 1 and enable. |
| 189 | + mrs x0, MPIDR_EL1 |
| 190 | + ldr x2, =GIC_DISTB |
| 191 | + tst x0, #0x3 |
| 192 | + b.eq 2f // primary core |
| 193 | + |
| 194 | + mov w0, #3 // Enable group 0 and 1 IRQs from distributor |
| 195 | + str w0, [x2, #GICD_CTRLR] |
| 196 | +2: |
| 197 | + add x1, x2, #(GIC_CPUB - GIC_DISTB) |
| 198 | + mov w0, #0x1e7 |
| 199 | + str w0, [x1, #GICC_CTRLR] // Enable group 1 IRQs from CPU interface |
| 200 | + mov w0, #0xff |
| 201 | + str w0, [x1, #GICC_PMR] // priority mask |
| 202 | + add x2, x2, #GICD_IGROUPR |
| 203 | + mov x0, #(IT_NR * 4) |
| 204 | + mov w1, #~0 // group 1 all the things |
| 205 | +3: |
| 206 | + subs x0, x0, #4 |
| 207 | + str w1, [x2, x0] |
| 208 | + b.ne 3b |
| 209 | + ret |
| 210 | + |
| 211 | +#endif |
| 212 | + |
| 213 | +.globl dtb_space |
| 214 | +dtb_space: |
0 commit comments