blob: 2dfd27e90f5b34467dd884ce5976e092aab6c1a4 [file] [log] [blame]
Xiangfu Liu80421fc2011-10-12 12:24:06 +08001/*
2 * Startup Code for MIPS32 XBURST CPU-core
3 *
4 * Copyright (c) 2010 Xiangfu Liu <xiangfu@sharism.cc>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Xiangfu Liu80421fc2011-10-12 12:24:06 +08007 */
8
9#include <config.h>
10#include <version.h>
11#include <asm/regdef.h>
12#include <asm/mipsregs.h>
13#include <asm/addrspace.h>
14#include <asm/cacheops.h>
15
16 .set noreorder
17
18 .globl _start
19 .text
20_start:
21 /* Initialize $gp */
22 bal 1f
23 nop
24 .word _gp
251:
26 lw gp, 0(ra)
27
28 /* Set up temporary stack */
29 li sp, CONFIG_SYS_SDRAM_BASE + CONFIG_SYS_INIT_SP_OFFSET
30
31 la t9, board_init_f
32 jr t9
33 nop
34
35/*
36 * void relocate_code (addr_sp, gd, addr_moni)
37 *
38 * This "function" does not return, instead it continues in RAM
39 * after relocating the monitor code.
40 *
41 * a0 = addr_sp
42 * a1 = gd
43 * a2 = destination address
44 */
45 .globl relocate_code
46 .ent relocate_code
47relocate_code:
48 move sp, a0 # set new stack pointer
49
Gabor Juhos9a28e0d2013-06-13 12:59:29 +020050 move s2, a2 # save destination address in s2
51
Xiangfu Liu80421fc2011-10-12 12:24:06 +080052 li t0, CONFIG_SYS_MONITOR_BASE
Gabor Juhosba9cf072013-06-13 12:59:30 +020053 sub s1, s2, t0 # s1 <-- relocation offset
Gabor Juhos6d862272013-01-30 04:51:06 +000054
Xiangfu Liu80421fc2011-10-12 12:24:06 +080055 la t3, in_ram
Daniel Schwierzeck28875e22013-02-12 22:22:13 +010056 lw t2, -12(t3) # t2 <-- __image_copy_end
Xiangfu Liu80421fc2011-10-12 12:24:06 +080057 move t1, a2
58
Gabor Juhosba9cf072013-06-13 12:59:30 +020059 add gp, s1 # adjust gp
Xiangfu Liu80421fc2011-10-12 12:24:06 +080060
61 /*
62 * t0 = source address
63 * t1 = target address
64 * t2 = source end address
65 */
661:
67 lw t3, 0(t0)
68 sw t3, 0(t1)
69 addu t0, 4
Gabor Juhos5b7dd812013-01-24 06:27:51 +000070 blt t0, t2, 1b
Xiangfu Liu80421fc2011-10-12 12:24:06 +080071 addu t1, 4
72
73 /* If caches were enabled, we would have to flush them here. */
74
75 /* flush d-cache */
76 li t0, KSEG0
77 addi t1, t0, CONFIG_SYS_DCACHE_SIZE
782:
Zhi-zhou Zhangcb0a6a12012-10-16 15:02:08 +020079 cache INDEX_WRITEBACK_INV_D, 0(t0)
Xiangfu Liu80421fc2011-10-12 12:24:06 +080080 bne t0, t1, 2b
81 addi t0, CONFIG_SYS_CACHELINE_SIZE
82
83 sync
84
85 /* flush i-cache */
86 li t0, KSEG0
87 addi t1, t0, CONFIG_SYS_ICACHE_SIZE
883:
Zhi-zhou Zhangcb0a6a12012-10-16 15:02:08 +020089 cache INDEX_INVALIDATE_I, 0(t0)
Xiangfu Liu80421fc2011-10-12 12:24:06 +080090 bne t0, t1, 3b
91 addi t0, CONFIG_SYS_CACHELINE_SIZE
92
93 /* Invalidate BTB */
94 mfc0 t0, CP0_CONFIG, 7
95 nop
96 ori t0, 2
97 mtc0 t0, CP0_CONFIG, 7
98 nop
99
100 /* Jump to where we've relocated ourselves */
Gabor Juhos9a28e0d2013-06-13 12:59:29 +0200101 addi t0, s2, in_ram - _start
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800102 jr t0
103 nop
104
Gabor Juhos04380c62013-02-12 22:22:13 +0100105 .word __rel_dyn_end
106 .word __rel_dyn_start
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100107 .word __image_copy_end
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800108 .word _GLOBAL_OFFSET_TABLE_
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800109 .word num_got_entries
110
111in_ram:
112 /*
113 * Now we want to update GOT.
114 *
115 * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
116 * generated by GNU ld. Skip these reserved entries from relocation.
117 */
118 lw t3, -4(t0) # t3 <-- num_got_entries
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100119 lw t4, -8(t0) # t4 <-- _GLOBAL_OFFSET_TABLE_
Gabor Juhosba9cf072013-06-13 12:59:30 +0200120 add t4, s1 # t4 now holds relocated _G_O_T_
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800121 addi t4, t4, 8 # skipping first two entries
122 li t2, 2
1231:
124 lw t1, 0(t4)
125 beqz t1, 2f
Gabor Juhosba9cf072013-06-13 12:59:30 +0200126 add t1, s1
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800127 sw t1, 0(t4)
1282:
129 addi t2, 1
130 blt t2, t3, 1b
131 addi t4, 4
132
Gabor Juhos04380c62013-02-12 22:22:13 +0100133 /* Update dynamic relocations */
134 lw t1, -16(t0) # t1 <-- __rel_dyn_start
135 lw t2, -20(t0) # t2 <-- __rel_dyn_end
136
137 b 2f # skip first reserved entry
138 addi t1, 8
139
1401:
141 lw t3, -4(t1) # t3 <-- relocation info
142
143 sub t3, 3
144 bnez t3, 2f # skip non R_MIPS_REL32 entries
145 nop
146
147 lw t3, -8(t1) # t3 <-- location to fix up in FLASH
148
149 lw t4, 0(t3) # t4 <-- original pointer
Gabor Juhosba9cf072013-06-13 12:59:30 +0200150 add t4, s1 # t4 <-- adjusted pointer
Gabor Juhos04380c62013-02-12 22:22:13 +0100151
Gabor Juhosba9cf072013-06-13 12:59:30 +0200152 add t3, s1 # t3 <-- location to fix up in RAM
Gabor Juhos04380c62013-02-12 22:22:13 +0100153 sw t4, 0(t3)
154
1552:
156 blt t1, t2, 1b
157 addi t1, 8 # each rel.dyn entry is 8 bytes
158
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100159 /*
160 * Clear BSS
161 *
162 * GOT is now relocated. Thus __bss_start and __bss_end can be
163 * accessed directly via $gp.
164 */
165 la t1, __bss_start # t1 <-- __bss_start
166 la t2, __bss_end # t2 <-- __bss_end
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800167
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +01001681:
169 sw zero, 0(t1)
170 blt t1, t2, 1b
171 addi t1, 4
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800172
173 move a0, a1 # a0 <-- gd
174 la t9, board_init_r
175 jr t9
Gabor Juhos9a28e0d2013-06-13 12:59:29 +0200176 move a1, s2
Xiangfu Liu80421fc2011-10-12 12:24:06 +0800177
178 .end relocate_code