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