blob: 36b92cc6874d873070784640bacae0aaa16442dd [file] [log] [blame]
wdenkbb1b8262003-03-27 12:09:35 +00001/*
2 * Startup Code for MIPS32 CPU-core
3 *
4 * Copyright (c) 2003 Wolfgang Denk <wd@denx.de>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkbb1b8262003-03-27 12:09:35 +00007 */
8
Wolfgang Denk0191e472010-10-26 14:34:52 +02009#include <asm-offsets.h>
wdenkbb1b8262003-03-27 12:09:35 +000010#include <config.h>
wdenkbb1b8262003-03-27 12:09:35 +000011#include <asm/regdef.h>
12#include <asm/mipsregs.h>
13
Daniel Schwierzeck7dc16be2011-07-27 13:22:38 +020014#ifndef CONFIG_SYS_MIPS_CACHE_MODE
15#define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
16#endif
17
Daniel Schwierzeck28144592015-01-18 22:18:38 +010018#ifndef CONFIG_SYS_INIT_SP_ADDR
19#define CONFIG_SYS_INIT_SP_ADDR (CONFIG_SYS_SDRAM_BASE + \
20 CONFIG_SYS_INIT_SP_OFFSET)
21#endif
22
Shinya Kuribayashi2300af12008-03-25 21:30:07 +090023 /*
24 * For the moment disable interrupts, mark the kernel mode and
25 * set ST0_KX so that the CPU does not spit fire when using
26 * 64-bit addresses.
27 */
28 .macro setup_c0_status set clr
29 .set push
30 mfc0 t0, CP0_STATUS
31 or t0, ST0_CU0 | \set | 0x1f | \clr
32 xor t0, 0x1f | \clr
33 mtc0 t0, CP0_STATUS
34 .set noreorder
35 sll zero, 3 # ehb
36 .set pop
37 .endm
38
wdenkbb1b8262003-03-27 12:09:35 +000039 .set noreorder
40
41 .globl _start
42 .text
43_start:
Daniel Schwierzeckec443162013-02-12 22:22:12 +010044 /* U-boot entry point */
45 b reset
46 nop
47
48 .org 0x10
Gabor Juhosb6be59a2013-05-22 03:57:46 +000049#if defined(CONFIG_SYS_XWAY_EBU_BOOTCFG)
Daniel Schwierzeck6ff8ae02011-07-27 13:22:37 +020050 /*
51 * Almost all Lantiq XWAY SoC devices have an external bus unit (EBU) to
52 * access external NOR flashes. If the board boots from NOR flash the
53 * internal BootROM does a blind read at address 0xB0000010 to read the
54 * initial configuration for that EBU in order to access the flash
55 * device with correct parameters. This config option is board-specific.
56 */
57 .word CONFIG_SYS_XWAY_EBU_BOOTCFG
Daniel Schwierzeckec443162013-02-12 22:22:12 +010058 .word 0x0
Paul Burton10a74b52013-11-09 10:22:08 +000059#elif defined(CONFIG_MALTA)
Gabor Juhosb6be59a2013-05-22 03:57:46 +000060 /*
61 * Linux expects the Board ID here.
62 */
63 .word 0x00000420 # 0x420 (Malta Board with CoreLV)
64 .word 0x00000000
wdenkbb1b8262003-03-27 12:09:35 +000065#endif
wdenk57b2d802003-06-27 21:31:46 +000066
Daniel Schwierzeckec443162013-02-12 22:22:12 +010067 .org 0x200
68 /* TLB refill, 32 bit task */
691: b 1b
70 nop
71
72 .org 0x280
73 /* XTLB refill, 64 bit task */
741: b 1b
75 nop
76
77 .org 0x300
78 /* Cache error exception */
791: b 1b
80 nop
81
82 .org 0x380
83 /* General exception */
841: b 1b
85 nop
86
87 .org 0x400
88 /* Catch interrupt exceptions */
891: b 1b
90 nop
91
92 .org 0x480
93 /* EJTAG debug exception */
941: b 1b
95 nop
96
wdenkbb1b8262003-03-27 12:09:35 +000097 .align 4
98reset:
99
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900100 /* Clear watch registers */
wdenkbb1b8262003-03-27 12:09:35 +0000101 mtc0 zero, CP0_WATCHLO
102 mtc0 zero, CP0_WATCHHI
103
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900104 /* WP(Watch Pending), SW0/1 should be cleared */
Shinya Kuribayashi79727f82008-03-25 21:30:07 +0900105 mtc0 zero, CP0_CAUSE
106
Daniel Schwierzeck7e446142013-02-12 22:22:12 +0100107 setup_c0_status 0 0
wdenkbb1b8262003-03-27 12:09:35 +0000108
wdenkbb1b8262003-03-27 12:09:35 +0000109 /* Init Timer */
110 mtc0 zero, CP0_COUNT
111 mtc0 zero, CP0_COMPARE
112
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900113#ifndef CONFIG_SKIP_LOWLEVEL_INIT
wdenkbb1b8262003-03-27 12:09:35 +0000114 /* CONFIG0 register */
115 li t0, CONF_CM_UNCACHED
116 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900117#endif
wdenkbb1b8262003-03-27 12:09:35 +0000118
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900119 /* Initialize $gp */
Shinya Kuribayashic7faac52007-10-27 15:27:06 +0900120 bal 1f
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900121 nop
Shinya Kuribayashi7cb56762007-10-21 10:55:36 +0900122 .word _gp
Shinya Kuribayashic7faac52007-10-27 15:27:06 +09001231:
Shinya Kuribayashiec655bd2007-11-17 20:05:26 +0900124 lw gp, 0(ra)
Wolfgang Denk117b0b12005-12-01 02:15:07 +0100125
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900126#ifndef CONFIG_SKIP_LOWLEVEL_INIT
127 /* Initialize any external memory */
Shinya Kuribayashic7faac52007-10-27 15:27:06 +0900128 la t9, lowlevel_init
129 jalr t9
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900130 nop
wdenkbb1b8262003-03-27 12:09:35 +0000131
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900132 /* Initialize caches... */
Shinya Kuribayashic7faac52007-10-27 15:27:06 +0900133 la t9, mips_cache_reset
134 jalr t9
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900135 nop
wdenkbb1b8262003-03-27 12:09:35 +0000136
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900137 /* ... and enable them */
Daniel Schwierzeck7dc16be2011-07-27 13:22:38 +0200138 li t0, CONFIG_SYS_MIPS_CACHE_MODE
wdenkbb1b8262003-03-27 12:09:35 +0000139 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900140#endif
wdenkbb1b8262003-03-27 12:09:35 +0000141
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900142 /* Set up temporary stack */
Daniel Schwierzeck01cab272015-01-18 22:18:39 +0100143 li t0, -16
144 li t1, CONFIG_SYS_INIT_SP_ADDR
145 and sp, t1, t0 # force 16 byte alignment
146 sub sp, sp, GD_SIZE # reserve space for gd
147 and sp, sp, t0 # force 16 byte alignment
148 move k0, sp # save gd pointer
149#ifdef CONFIG_SYS_MALLOC_F_LEN
150 li t2, CONFIG_SYS_MALLOC_F_LEN
151 sub sp, sp, t2 # reserve space for early malloc
152 and sp, sp, t0 # force 16 byte alignment
153#endif
Daniel Schwierzeckf224c1a2014-11-20 23:55:32 +0100154 move fp, sp
wdenkbb1b8262003-03-27 12:09:35 +0000155
Daniel Schwierzeck01cab272015-01-18 22:18:39 +0100156 /* Clear gd */
157 move t0, k0
1581:
159 sw zero, 0(t0)
160 blt t0, t1, 1b
161 addi t0, 4
162
163#ifdef CONFIG_SYS_MALLOC_F_LEN
164 addu t0, k0, GD_MALLOC_BASE # gd->malloc_base offset
165 sw sp, 0(t0)
166#endif
167
wdenkbb1b8262003-03-27 12:09:35 +0000168 la t9, board_init_f
Shinya Kuribayashi9dabea12008-04-17 23:35:13 +0900169 jr t9
Daniel Schwierzeckf224c1a2014-11-20 23:55:32 +0100170 move ra, zero
wdenkbb1b8262003-03-27 12:09:35 +0000171
wdenkbb1b8262003-03-27 12:09:35 +0000172/*
173 * void relocate_code (addr_sp, gd, addr_moni)
174 *
175 * This "function" does not return, instead it continues in RAM
176 * after relocating the monitor code.
177 *
178 * a0 = addr_sp
179 * a1 = gd
180 * a2 = destination address
181 */
182 .globl relocate_code
183 .ent relocate_code
184relocate_code:
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900185 move sp, a0 # set new stack pointer
Daniel Schwierzeckf224c1a2014-11-20 23:55:32 +0100186 move fp, sp
wdenkbb1b8262003-03-27 12:09:35 +0000187
Gabor Juhosf902d462013-01-24 06:27:53 +0000188 move s0, a1 # save gd in s0
189 move s2, a2 # save destination address in s2
190
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200191 li t0, CONFIG_SYS_MONITOR_BASE
Gabor Juhosfac2f652013-01-24 06:27:54 +0000192 sub s1, s2, t0 # s1 <-- relocation offset
193
wdenk874ac262003-07-24 23:38:38 +0000194 la t3, in_ram
Daniel Schwierzeck7f2d5402013-02-12 22:22:13 +0100195 lw t2, -12(t3) # t2 <-- __image_copy_end
wdenk874ac262003-07-24 23:38:38 +0000196 move t1, a2
197
Gabor Juhosfac2f652013-01-24 06:27:54 +0000198 add gp, s1 # adjust gp
wdenk57b2d802003-06-27 21:31:46 +0000199
wdenkbb1b8262003-03-27 12:09:35 +0000200 /*
201 * t0 = source address
202 * t1 = target address
203 * t2 = source end address
204 */
2051:
206 lw t3, 0(t0)
207 sw t3, 0(t1)
208 addu t0, 4
Gabor Juhos9a081ab2013-01-24 06:27:51 +0000209 blt t0, t2, 1b
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900210 addu t1, 4
wdenkbb1b8262003-03-27 12:09:35 +0000211
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900212 /* If caches were enabled, we would have to flush them here. */
Gabor Juhoseb590242013-01-24 06:27:55 +0000213 sub a1, t1, s2 # a1 <-- size
Stefan Roeseaed3f502008-11-18 16:36:12 +0100214 la t9, flush_cache
215 jalr t9
Gabor Juhoseb590242013-01-24 06:27:55 +0000216 move a0, s2 # a0 <-- destination address
Stefan Roeseaed3f502008-11-18 16:36:12 +0100217
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900218 /* Jump to where we've relocated ourselves */
Stefan Roeseaed3f502008-11-18 16:36:12 +0100219 addi t0, s2, in_ram - _start
Shinya Kuribayashi9dabea12008-04-17 23:35:13 +0900220 jr t0
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900221 nop
wdenkbb1b8262003-03-27 12:09:35 +0000222
Gabor Juhos84937ab2013-02-12 22:22:13 +0100223 .word __rel_dyn_end
224 .word __rel_dyn_start
Daniel Schwierzeck7f2d5402013-02-12 22:22:13 +0100225 .word __image_copy_end
Vlad Lungu82809fa2008-05-05 14:04:00 +0300226 .word _GLOBAL_OFFSET_TABLE_
wdenkbb1b8262003-03-27 12:09:35 +0000227 .word num_got_entries
228
229in_ram:
Shinya Kuribayashi7cb56762007-10-21 10:55:36 +0900230 /*
231 * Now we want to update GOT.
232 *
233 * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
234 * generated by GNU ld. Skip these reserved entries from relocation.
wdenkbb1b8262003-03-27 12:09:35 +0000235 */
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900236 lw t3, -4(t0) # t3 <-- num_got_entries
Gabor Juhos489c5d72013-06-13 12:59:27 +0200237 lw t8, -8(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_
238 add t8, s1 # t8 now holds relocated _G_O_T_
239 addi t8, t8, 8 # skipping first two entries
wdenkbb1b8262003-03-27 12:09:35 +0000240 li t2, 2
2411:
Gabor Juhos489c5d72013-06-13 12:59:27 +0200242 lw t1, 0(t8)
wdenkbb1b8262003-03-27 12:09:35 +0000243 beqz t1, 2f
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900244 add t1, s1
Gabor Juhos489c5d72013-06-13 12:59:27 +0200245 sw t1, 0(t8)
wdenkbb1b8262003-03-27 12:09:35 +00002462:
247 addi t2, 1
248 blt t2, t3, 1b
Gabor Juhos489c5d72013-06-13 12:59:27 +0200249 addi t8, 4
wdenkbb1b8262003-03-27 12:09:35 +0000250
Gabor Juhos84937ab2013-02-12 22:22:13 +0100251 /* Update dynamic relocations */
252 lw t1, -16(t0) # t1 <-- __rel_dyn_start
253 lw t2, -20(t0) # t2 <-- __rel_dyn_end
254
255 b 2f # skip first reserved entry
256 addi t1, 8
257
2581:
Gabor Juhosb8478792013-06-13 12:59:28 +0200259 lw t8, -4(t1) # t8 <-- relocation info
Gabor Juhos84937ab2013-02-12 22:22:13 +0100260
Gabor Juhosb8478792013-06-13 12:59:28 +0200261 li t3, 3
262 bne t8, t3, 2f # skip non R_MIPS_REL32 entries
Gabor Juhos84937ab2013-02-12 22:22:13 +0100263 nop
264
265 lw t3, -8(t1) # t3 <-- location to fix up in FLASH
266
Gabor Juhos489c5d72013-06-13 12:59:27 +0200267 lw t8, 0(t3) # t8 <-- original pointer
268 add t8, s1 # t8 <-- adjusted pointer
Gabor Juhos84937ab2013-02-12 22:22:13 +0100269
270 add t3, s1 # t3 <-- location to fix up in RAM
Gabor Juhos489c5d72013-06-13 12:59:27 +0200271 sw t8, 0(t3)
Gabor Juhos84937ab2013-02-12 22:22:13 +0100272
2732:
274 blt t1, t2, 1b
275 addi t1, 8 # each rel.dyn entry is 8 bytes
276
Daniel Schwierzeck0de9cc52013-02-12 22:22:13 +0100277 /*
278 * Clear BSS
279 *
280 * GOT is now relocated. Thus __bss_start and __bss_end can be
281 * accessed directly via $gp.
282 */
283 la t1, __bss_start # t1 <-- __bss_start
284 la t2, __bss_end # t2 <-- __bss_end
wdenkbb1b8262003-03-27 12:09:35 +0000285
Shinya Kuribayashic7faac52007-10-27 15:27:06 +09002861:
Daniel Schwierzeck0de9cc52013-02-12 22:22:13 +0100287 sw zero, 0(t1)
288 blt t1, t2, 1b
289 addi t1, 4
wdenk57b2d802003-06-27 21:31:46 +0000290
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900291 move a0, s0 # a0 <-- gd
Daniel Schwierzeckf224c1a2014-11-20 23:55:32 +0100292 move a1, s2
wdenkbb1b8262003-03-27 12:09:35 +0000293 la t9, board_init_r
Shinya Kuribayashi9dabea12008-04-17 23:35:13 +0900294 jr t9
Daniel Schwierzeckf224c1a2014-11-20 23:55:32 +0100295 move ra, zero
wdenkbb1b8262003-03-27 12:09:35 +0000296
297 .end relocate_code