blob: 76abbaa27368d5b14ebea23aeccd00d63004de52 [file] [log] [blame]
wdenkc0218802003-03-27 12:09:35 +00001/*
2 * Startup Code for MIPS32 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 later 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 PARTICULAR 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 Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
Wolfgang Denk25ddd1f2010-10-26 14:34:52 +020025#include <asm-offsets.h>
wdenkc0218802003-03-27 12:09:35 +000026#include <config.h>
wdenkc0218802003-03-27 12:09:35 +000027#include <asm/regdef.h>
28#include <asm/mipsregs.h>
29
Daniel Schwierzeckab2a98b2011-07-27 13:22:38 +020030#ifndef CONFIG_SYS_MIPS_CACHE_MODE
31#define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
32#endif
33
Shinya Kuribayashidecaba62008-03-25 21:30:07 +090034 /*
35 * For the moment disable interrupts, mark the kernel mode and
36 * set ST0_KX so that the CPU does not spit fire when using
37 * 64-bit addresses.
38 */
39 .macro setup_c0_status set clr
40 .set push
41 mfc0 t0, CP0_STATUS
42 or t0, ST0_CU0 | \set | 0x1f | \clr
43 xor t0, 0x1f | \clr
44 mtc0 t0, CP0_STATUS
45 .set noreorder
46 sll zero, 3 # ehb
47 .set pop
48 .endm
49
wdenkc0218802003-03-27 12:09:35 +000050 .set noreorder
51
52 .globl _start
53 .text
54_start:
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010055 /* U-boot entry point */
56 b reset
57 nop
58
59 .org 0x10
Daniel Schwierzeck7185adb2011-07-27 13:22:37 +020060#ifdef CONFIG_SYS_XWAY_EBU_BOOTCFG
61 /*
62 * Almost all Lantiq XWAY SoC devices have an external bus unit (EBU) to
63 * access external NOR flashes. If the board boots from NOR flash the
64 * internal BootROM does a blind read at address 0xB0000010 to read the
65 * initial configuration for that EBU in order to access the flash
66 * device with correct parameters. This config option is board-specific.
67 */
68 .word CONFIG_SYS_XWAY_EBU_BOOTCFG
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010069 .word 0x0
wdenkc0218802003-03-27 12:09:35 +000070#endif
wdenk8bde7f72003-06-27 21:31:46 +000071
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010072 .org 0x200
73 /* TLB refill, 32 bit task */
741: b 1b
75 nop
76
77 .org 0x280
78 /* XTLB refill, 64 bit task */
791: b 1b
80 nop
81
82 .org 0x300
83 /* Cache error exception */
841: b 1b
85 nop
86
87 .org 0x380
88 /* General exception */
891: b 1b
90 nop
91
92 .org 0x400
93 /* Catch interrupt exceptions */
941: b 1b
95 nop
96
97 .org 0x480
98 /* EJTAG debug exception */
991: b 1b
100 nop
101
wdenkc0218802003-03-27 12:09:35 +0000102 .align 4
103reset:
104
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900105 /* Clear watch registers */
wdenkc0218802003-03-27 12:09:35 +0000106 mtc0 zero, CP0_WATCHLO
107 mtc0 zero, CP0_WATCHHI
108
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900109 /* WP(Watch Pending), SW0/1 should be cleared */
Shinya Kuribayashid43d43e2008-03-25 21:30:07 +0900110 mtc0 zero, CP0_CAUSE
111
Daniel Schwierzeck4dc74122013-02-12 22:22:12 +0100112 setup_c0_status 0 0
wdenkc0218802003-03-27 12:09:35 +0000113
wdenkc0218802003-03-27 12:09:35 +0000114 /* Init Timer */
115 mtc0 zero, CP0_COUNT
116 mtc0 zero, CP0_COMPARE
117
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900118#ifndef CONFIG_SKIP_LOWLEVEL_INIT
wdenkc0218802003-03-27 12:09:35 +0000119 /* CONFIG0 register */
120 li t0, CONF_CM_UNCACHED
121 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900122#endif
wdenkc0218802003-03-27 12:09:35 +0000123
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900124 /* Initialize $gp */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900125 bal 1f
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900126 nop
Shinya Kuribayashi22069212007-10-21 10:55:36 +0900127 .word _gp
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09001281:
Shinya Kuribayashi16664f72007-11-17 20:05:26 +0900129 lw gp, 0(ra)
Wolfgang Denkc75eba32005-12-01 02:15:07 +0100130
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900131#ifndef CONFIG_SKIP_LOWLEVEL_INIT
132 /* Initialize any external memory */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900133 la t9, lowlevel_init
134 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900135 nop
wdenkc0218802003-03-27 12:09:35 +0000136
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900137 /* Initialize caches... */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900138 la t9, mips_cache_reset
139 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900140 nop
wdenkc0218802003-03-27 12:09:35 +0000141
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900142 /* ... and enable them */
Daniel Schwierzeckab2a98b2011-07-27 13:22:38 +0200143 li t0, CONFIG_SYS_MIPS_CACHE_MODE
wdenkc0218802003-03-27 12:09:35 +0000144 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900145#endif
wdenkc0218802003-03-27 12:09:35 +0000146
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900147 /* Set up temporary stack */
Gabor Juhosf321b0f2013-01-24 06:27:52 +0000148 li sp, CONFIG_SYS_SDRAM_BASE + CONFIG_SYS_INIT_SP_OFFSET
wdenkc0218802003-03-27 12:09:35 +0000149
wdenkc0218802003-03-27 12:09:35 +0000150 la t9, board_init_f
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900151 jr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900152 nop
wdenkc0218802003-03-27 12:09:35 +0000153
wdenkc0218802003-03-27 12:09:35 +0000154/*
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
166relocate_code:
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900167 move sp, a0 # set new stack pointer
wdenkc0218802003-03-27 12:09:35 +0000168
Gabor Juhosb2fe86f2013-01-24 06:27:53 +0000169 move s0, a1 # save gd in s0
170 move s2, a2 # save destination address in s2
171
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200172 li t0, CONFIG_SYS_MONITOR_BASE
Gabor Juhos248fe032013-01-24 06:27:54 +0000173 sub s1, s2, t0 # s1 <-- relocation offset
174
wdenk27b207f2003-07-24 23:38:38 +0000175 la t3, in_ram
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100176 lw t2, -12(t3) # t2 <-- __image_copy_end
wdenk27b207f2003-07-24 23:38:38 +0000177 move t1, a2
178
Gabor Juhos248fe032013-01-24 06:27:54 +0000179 add gp, s1 # adjust gp
wdenk8bde7f72003-06-27 21:31:46 +0000180
wdenkc0218802003-03-27 12:09:35 +0000181 /*
182 * t0 = source address
183 * t1 = target address
184 * t2 = source end address
185 */
1861:
187 lw t3, 0(t0)
188 sw t3, 0(t1)
189 addu t0, 4
Gabor Juhos5b7dd812013-01-24 06:27:51 +0000190 blt t0, t2, 1b
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900191 addu t1, 4
wdenkc0218802003-03-27 12:09:35 +0000192
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900193 /* If caches were enabled, we would have to flush them here. */
Gabor Juhos67d80c92013-01-24 06:27:55 +0000194 sub a1, t1, s2 # a1 <-- size
Stefan Roese71fa0712008-11-18 16:36:12 +0100195 la t9, flush_cache
196 jalr t9
Gabor Juhos67d80c92013-01-24 06:27:55 +0000197 move a0, s2 # a0 <-- destination address
Stefan Roese71fa0712008-11-18 16:36:12 +0100198
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900199 /* Jump to where we've relocated ourselves */
Stefan Roese71fa0712008-11-18 16:36:12 +0100200 addi t0, s2, in_ram - _start
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900201 jr t0
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900202 nop
wdenkc0218802003-03-27 12:09:35 +0000203
Gabor Juhos04380c62013-02-12 22:22:13 +0100204 .word __rel_dyn_end
205 .word __rel_dyn_start
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100206 .word __image_copy_end
Vlad Lungu0f8c62a2008-05-05 14:04:00 +0300207 .word _GLOBAL_OFFSET_TABLE_
wdenkc0218802003-03-27 12:09:35 +0000208 .word num_got_entries
209
210in_ram:
Shinya Kuribayashi22069212007-10-21 10:55:36 +0900211 /*
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.
wdenkc0218802003-03-27 12:09:35 +0000216 */
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900217 lw t3, -4(t0) # t3 <-- num_got_entries
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100218 lw t4, -8(t0) # t4 <-- _GLOBAL_OFFSET_TABLE_
Gabor Juhos025f2b32013-01-30 04:56:37 +0000219 add t4, s1 # t4 now holds relocated _G_O_T_
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900220 addi t4, t4, 8 # skipping first two entries
wdenkc0218802003-03-27 12:09:35 +0000221 li t2, 2
2221:
223 lw t1, 0(t4)
224 beqz t1, 2f
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900225 add t1, s1
wdenkc0218802003-03-27 12:09:35 +0000226 sw t1, 0(t4)
2272:
228 addi t2, 1
229 blt t2, t3, 1b
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900230 addi t4, 4
wdenkc0218802003-03-27 12:09:35 +0000231
Gabor Juhos04380c62013-02-12 22:22:13 +0100232 /* Update dynamic relocations */
233 lw t1, -16(t0) # t1 <-- __rel_dyn_start
234 lw t2, -20(t0) # t2 <-- __rel_dyn_end
235
236 b 2f # skip first reserved entry
237 addi t1, 8
238
2391:
240 lw t3, -4(t1) # t3 <-- relocation info
241
242 sub t3, 3
243 bnez t3, 2f # skip non R_MIPS_REL32 entries
244 nop
245
246 lw t3, -8(t1) # t3 <-- location to fix up in FLASH
247
248 lw t4, 0(t3) # t4 <-- original pointer
249 add t4, s1 # t4 <-- adjusted pointer
250
251 add t3, s1 # t3 <-- location to fix up in RAM
252 sw t4, 0(t3)
253
2542:
255 blt t1, t2, 1b
256 addi t1, 8 # each rel.dyn entry is 8 bytes
257
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100258 /*
259 * Clear BSS
260 *
261 * GOT is now relocated. Thus __bss_start and __bss_end can be
262 * accessed directly via $gp.
263 */
264 la t1, __bss_start # t1 <-- __bss_start
265 la t2, __bss_end # t2 <-- __bss_end
wdenkc0218802003-03-27 12:09:35 +0000266
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09002671:
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100268 sw zero, 0(t1)
269 blt t1, t2, 1b
270 addi t1, 4
wdenk8bde7f72003-06-27 21:31:46 +0000271
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900272 move a0, s0 # a0 <-- gd
wdenkc0218802003-03-27 12:09:35 +0000273 la t9, board_init_r
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900274 jr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900275 move a1, s2
wdenkc0218802003-03-27 12:09:35 +0000276
277 .end relocate_code