Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Startup Code for MIPS64 CPU-core |
| 3 | * |
| 4 | * Copyright (c) 2003 Wolfgang Denk <wd@denx.de> |
| 5 | * |
Tom Rini | 518d438 | 2013-07-24 09:50:52 -0400 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <asm-offsets.h> |
| 10 | #include <config.h> |
| 11 | #include <asm/regdef.h> |
| 12 | #include <asm/mipsregs.h> |
| 13 | |
| 14 | #ifndef CONFIG_SYS_MIPS_CACHE_MODE |
| 15 | #define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT |
| 16 | #endif |
| 17 | |
Gabor Juhos | 04380c6 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 18 | #ifdef CONFIG_SYS_LITTLE_ENDIAN |
| 19 | #define MIPS64_R_INFO(ssym, r_type3, r_type2, r_type) \ |
| 20 | (((r_type) << 24) | ((r_type2) << 16) | ((r_type3) << 8) | (ssym)) |
| 21 | #else |
| 22 | #define MIPS64_R_INFO(ssym, r_type3, r_type2, r_type) \ |
| 23 | ((r_type) | ((r_type2) << 8) | ((r_type3) << 16) | (ssym) << 24) |
| 24 | #endif |
| 25 | |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 26 | /* |
| 27 | * For the moment disable interrupts, mark the kernel mode and |
| 28 | * set ST0_KX so that the CPU does not spit fire when using |
| 29 | * 64-bit addresses. |
| 30 | */ |
| 31 | .macro setup_c0_status set clr |
| 32 | .set push |
| 33 | mfc0 t0, CP0_STATUS |
| 34 | or t0, ST0_CU0 | \set | 0x1f | \clr |
| 35 | xor t0, 0x1f | \clr |
| 36 | mtc0 t0, CP0_STATUS |
| 37 | .set noreorder |
| 38 | sll zero, 3 # ehb |
| 39 | .set pop |
| 40 | .endm |
| 41 | |
| 42 | .set noreorder |
| 43 | |
| 44 | .globl _start |
| 45 | .text |
| 46 | _start: |
Daniel Schwierzeck | 8b1c734 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 47 | /* U-boot entry point */ |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 48 | b reset |
| 49 | nop |
Daniel Schwierzeck | 8b1c734 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 50 | |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 51 | .org 0x200 |
Daniel Schwierzeck | 8b1c734 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 52 | /* TLB refill, 32 bit task */ |
| 53 | 1: b 1b |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 54 | nop |
| 55 | |
Daniel Schwierzeck | 8b1c734 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 56 | .org 0x280 |
| 57 | /* XTLB refill, 64 bit task */ |
| 58 | 1: b 1b |
| 59 | nop |
| 60 | |
| 61 | .org 0x300 |
| 62 | /* Cache error exception */ |
| 63 | 1: b 1b |
| 64 | nop |
| 65 | |
| 66 | .org 0x380 |
| 67 | /* General exception */ |
| 68 | 1: b 1b |
| 69 | nop |
| 70 | |
| 71 | .org 0x400 |
| 72 | /* Catch interrupt exceptions */ |
| 73 | 1: b 1b |
| 74 | nop |
| 75 | |
| 76 | .org 0x480 |
| 77 | /* EJTAG debug exception */ |
| 78 | 1: b 1b |
| 79 | nop |
| 80 | |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 81 | .align 4 |
| 82 | reset: |
| 83 | |
| 84 | /* Clear watch registers */ |
| 85 | dmtc0 zero, CP0_WATCHLO |
| 86 | dmtc0 zero, CP0_WATCHHI |
| 87 | |
| 88 | /* WP(Watch Pending), SW0/1 should be cleared */ |
| 89 | mtc0 zero, CP0_CAUSE |
| 90 | |
| 91 | setup_c0_status ST0_KX 0 |
| 92 | |
| 93 | /* Init Timer */ |
| 94 | mtc0 zero, CP0_COUNT |
| 95 | mtc0 zero, CP0_COMPARE |
| 96 | |
| 97 | #ifndef CONFIG_SKIP_LOWLEVEL_INIT |
| 98 | /* CONFIG0 register */ |
| 99 | dli t0, CONF_CM_UNCACHED |
| 100 | mtc0 t0, CP0_CONFIG |
| 101 | #endif |
| 102 | |
Zhi-zhou Zhang | 0d69d91 | 2012-11-24 05:07:12 +0000 | [diff] [blame] | 103 | /* |
| 104 | * Initialize $gp, force 8 byte alignment of bal instruction to forbid |
| 105 | * the compiler to put nop's between bal and _gp. This is required to |
| 106 | * keep _gp and ra aligned to 8 byte. |
| 107 | */ |
| 108 | .align 3 |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 109 | bal 1f |
| 110 | nop |
| 111 | .dword _gp |
| 112 | 1: |
| 113 | ld gp, 0(ra) |
| 114 | |
| 115 | #ifndef CONFIG_SKIP_LOWLEVEL_INIT |
| 116 | /* Initialize any external memory */ |
| 117 | dla t9, lowlevel_init |
| 118 | jalr t9 |
| 119 | nop |
| 120 | |
| 121 | /* Initialize caches... */ |
| 122 | dla t9, mips_cache_reset |
| 123 | jalr t9 |
| 124 | nop |
| 125 | |
| 126 | /* ... and enable them */ |
| 127 | dli t0, CONFIG_SYS_MIPS_CACHE_MODE |
| 128 | mtc0 t0, CP0_CONFIG |
| 129 | #endif |
| 130 | |
| 131 | /* Set up temporary stack */ |
Gabor Juhos | f321b0f | 2013-01-24 06:27:52 +0000 | [diff] [blame] | 132 | dli sp, CONFIG_SYS_SDRAM_BASE + CONFIG_SYS_INIT_SP_OFFSET |
Daniel Schwierzeck | 6d08e22 | 2014-11-20 23:55:32 +0100 | [diff] [blame] | 133 | move fp, sp |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 134 | |
| 135 | dla t9, board_init_f |
| 136 | jr t9 |
Daniel Schwierzeck | 6d08e22 | 2014-11-20 23:55:32 +0100 | [diff] [blame] | 137 | move ra, zero |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * void relocate_code (addr_sp, gd, addr_moni) |
| 141 | * |
| 142 | * This "function" does not return, instead it continues in RAM |
| 143 | * after relocating the monitor code. |
| 144 | * |
| 145 | * a0 = addr_sp |
| 146 | * a1 = gd |
| 147 | * a2 = destination address |
| 148 | */ |
| 149 | .globl relocate_code |
| 150 | .ent relocate_code |
| 151 | relocate_code: |
| 152 | move sp, a0 # set new stack pointer |
Daniel Schwierzeck | 6d08e22 | 2014-11-20 23:55:32 +0100 | [diff] [blame] | 153 | move fp, sp |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 154 | |
Gabor Juhos | b2fe86f | 2013-01-24 06:27:53 +0000 | [diff] [blame] | 155 | move s0, a1 # save gd in s0 |
| 156 | move s2, a2 # save destination address in s2 |
| 157 | |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 158 | dli t0, CONFIG_SYS_MONITOR_BASE |
Gabor Juhos | 248fe03 | 2013-01-24 06:27:54 +0000 | [diff] [blame] | 159 | dsub s1, s2, t0 # s1 <-- relocation offset |
| 160 | |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 161 | dla t3, in_ram |
Daniel Schwierzeck | 28875e2 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 162 | ld t2, -24(t3) # t2 <-- __image_copy_end |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 163 | move t1, a2 |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 164 | |
Gabor Juhos | 248fe03 | 2013-01-24 06:27:54 +0000 | [diff] [blame] | 165 | dadd gp, s1 # adjust gp |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * t0 = source address |
| 169 | * t1 = target address |
| 170 | * t2 = source end address |
| 171 | */ |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 172 | 1: |
| 173 | lw t3, 0(t0) |
| 174 | sw t3, 0(t1) |
| 175 | daddu t0, 4 |
Gabor Juhos | 5b7dd81 | 2013-01-24 06:27:51 +0000 | [diff] [blame] | 176 | blt t0, t2, 1b |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 177 | daddu t1, 4 |
| 178 | |
| 179 | /* If caches were enabled, we would have to flush them here. */ |
Gabor Juhos | 67d80c9 | 2013-01-24 06:27:55 +0000 | [diff] [blame] | 180 | dsub a1, t1, s2 # a1 <-- size |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 181 | dla t9, flush_cache |
| 182 | jalr t9 |
Gabor Juhos | 67d80c9 | 2013-01-24 06:27:55 +0000 | [diff] [blame] | 183 | move a0, s2 # a0 <-- destination address |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 184 | |
| 185 | /* Jump to where we've relocated ourselves */ |
| 186 | daddi t0, s2, in_ram - _start |
| 187 | jr t0 |
| 188 | nop |
| 189 | |
Gabor Juhos | 04380c6 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 190 | .dword __rel_dyn_end |
| 191 | .dword __rel_dyn_start |
Daniel Schwierzeck | 28875e2 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 192 | .dword __image_copy_end |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 193 | .dword _GLOBAL_OFFSET_TABLE_ |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 194 | .dword num_got_entries |
| 195 | |
| 196 | in_ram: |
| 197 | /* |
| 198 | * Now we want to update GOT. |
| 199 | * |
| 200 | * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object |
| 201 | * generated by GNU ld. Skip these reserved entries from relocation. |
| 202 | */ |
| 203 | ld t3, -8(t0) # t3 <-- num_got_entries |
Daniel Schwierzeck | 28875e2 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 204 | ld t8, -16(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_ |
Gabor Juhos | 025f2b3 | 2013-01-30 04:56:37 +0000 | [diff] [blame] | 205 | dadd t8, s1 # t8 now holds relocated _G_O_T_ |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 206 | daddi t8, t8, 16 # skipping first two entries |
| 207 | dli t2, 2 |
| 208 | 1: |
| 209 | ld t1, 0(t8) |
| 210 | beqz t1, 2f |
| 211 | dadd t1, s1 |
| 212 | sd t1, 0(t8) |
| 213 | 2: |
| 214 | daddi t2, 1 |
| 215 | blt t2, t3, 1b |
| 216 | daddi t8, 8 |
| 217 | |
Gabor Juhos | 04380c6 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 218 | /* Update dynamic relocations */ |
| 219 | ld t1, -32(t0) # t1 <-- __rel_dyn_start |
| 220 | ld t2, -40(t0) # t2 <-- __rel_dyn_end |
| 221 | |
| 222 | b 2f # skip first reserved entry |
| 223 | daddi t1, 16 |
| 224 | |
| 225 | 1: |
| 226 | lw t8, -4(t1) # t8 <-- relocation info |
| 227 | |
| 228 | dli t3, MIPS64_R_INFO(0x00, 0x00, 0x12, 0x03) |
| 229 | bne t8, t3, 2f # skip non R_MIPS_REL32 entries |
| 230 | nop |
| 231 | |
| 232 | ld t3, -16(t1) # t3 <-- location to fix up in FLASH |
| 233 | |
| 234 | ld t8, 0(t3) # t8 <-- original pointer |
| 235 | dadd t8, s1 # t8 <-- adjusted pointer |
| 236 | |
| 237 | dadd t3, s1 # t3 <-- location to fix up in RAM |
| 238 | sd t8, 0(t3) |
| 239 | |
| 240 | 2: |
| 241 | blt t1, t2, 1b |
| 242 | daddi t1, 16 # each rel.dyn entry is 16 bytes |
| 243 | |
Daniel Schwierzeck | 696a3b2 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Clear BSS |
| 246 | * |
| 247 | * GOT is now relocated. Thus __bss_start and __bss_end can be |
| 248 | * accessed directly via $gp. |
| 249 | */ |
| 250 | dla t1, __bss_start # t1 <-- __bss_start |
| 251 | dla t2, __bss_end # t2 <-- __bss_end |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 252 | |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 253 | 1: |
Daniel Schwierzeck | 696a3b2 | 2013-02-12 22:22:13 +0100 | [diff] [blame] | 254 | sd zero, 0(t1) |
| 255 | blt t1, t2, 1b |
| 256 | daddi t1, 8 |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 257 | |
| 258 | move a0, s0 # a0 <-- gd |
Daniel Schwierzeck | 6d08e22 | 2014-11-20 23:55:32 +0100 | [diff] [blame] | 259 | move a1, s2 |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 260 | dla t9, board_init_r |
| 261 | jr t9 |
Daniel Schwierzeck | 6d08e22 | 2014-11-20 23:55:32 +0100 | [diff] [blame] | 262 | move ra, zero |
Zhi-zhou Zhang | 32afad7 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 263 | |
| 264 | .end relocate_code |