blob: b08a897af0f302df56a3c9463e467902e7e536dd [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkc0218802003-03-27 12:09:35 +00007 */
8
Wolfgang Denk25ddd1f2010-10-26 14:34:52 +02009#include <asm-offsets.h>
wdenkc0218802003-03-27 12:09:35 +000010#include <config.h>
wdenkc0218802003-03-27 12:09:35 +000011#include <asm/regdef.h>
12#include <asm/mipsregs.h>
13
Daniel Schwierzeckab2a98b2011-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
Shinya Kuribayashidecaba62008-03-25 21:30:07 +090018 /*
19 * For the moment disable interrupts, mark the kernel mode and
20 * set ST0_KX so that the CPU does not spit fire when using
21 * 64-bit addresses.
22 */
23 .macro setup_c0_status set clr
24 .set push
25 mfc0 t0, CP0_STATUS
26 or t0, ST0_CU0 | \set | 0x1f | \clr
27 xor t0, 0x1f | \clr
28 mtc0 t0, CP0_STATUS
29 .set noreorder
30 sll zero, 3 # ehb
31 .set pop
32 .endm
33
wdenkc0218802003-03-27 12:09:35 +000034 .set noreorder
35
36 .globl _start
37 .text
38_start:
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010039 /* U-boot entry point */
40 b reset
41 nop
42
43 .org 0x10
Daniel Schwierzeck7185adb2011-07-27 13:22:37 +020044#ifdef CONFIG_SYS_XWAY_EBU_BOOTCFG
45 /*
46 * Almost all Lantiq XWAY SoC devices have an external bus unit (EBU) to
47 * access external NOR flashes. If the board boots from NOR flash the
48 * internal BootROM does a blind read at address 0xB0000010 to read the
49 * initial configuration for that EBU in order to access the flash
50 * device with correct parameters. This config option is board-specific.
51 */
52 .word CONFIG_SYS_XWAY_EBU_BOOTCFG
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010053 .word 0x0
wdenkc0218802003-03-27 12:09:35 +000054#endif
wdenk8bde7f72003-06-27 21:31:46 +000055
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010056 .org 0x200
57 /* TLB refill, 32 bit task */
581: b 1b
59 nop
60
61 .org 0x280
62 /* XTLB refill, 64 bit task */
631: b 1b
64 nop
65
66 .org 0x300
67 /* Cache error exception */
681: b 1b
69 nop
70
71 .org 0x380
72 /* General exception */
731: b 1b
74 nop
75
76 .org 0x400
77 /* Catch interrupt exceptions */
781: b 1b
79 nop
80
81 .org 0x480
82 /* EJTAG debug exception */
831: b 1b
84 nop
85
wdenkc0218802003-03-27 12:09:35 +000086 .align 4
87reset:
88
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +090089 /* Clear watch registers */
wdenkc0218802003-03-27 12:09:35 +000090 mtc0 zero, CP0_WATCHLO
91 mtc0 zero, CP0_WATCHHI
92
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +090093 /* WP(Watch Pending), SW0/1 should be cleared */
Shinya Kuribayashid43d43e2008-03-25 21:30:07 +090094 mtc0 zero, CP0_CAUSE
95
Daniel Schwierzeck4dc74122013-02-12 22:22:12 +010096 setup_c0_status 0 0
wdenkc0218802003-03-27 12:09:35 +000097
wdenkc0218802003-03-27 12:09:35 +000098 /* Init Timer */
99 mtc0 zero, CP0_COUNT
100 mtc0 zero, CP0_COMPARE
101
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900102#ifndef CONFIG_SKIP_LOWLEVEL_INIT
wdenkc0218802003-03-27 12:09:35 +0000103 /* CONFIG0 register */
104 li t0, CONF_CM_UNCACHED
105 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900106#endif
wdenkc0218802003-03-27 12:09:35 +0000107
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900108 /* Initialize $gp */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900109 bal 1f
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900110 nop
Shinya Kuribayashi22069212007-10-21 10:55:36 +0900111 .word _gp
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09001121:
Shinya Kuribayashi16664f72007-11-17 20:05:26 +0900113 lw gp, 0(ra)
Wolfgang Denkc75eba32005-12-01 02:15:07 +0100114
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900115#ifndef CONFIG_SKIP_LOWLEVEL_INIT
116 /* Initialize any external memory */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900117 la t9, lowlevel_init
118 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900119 nop
wdenkc0218802003-03-27 12:09:35 +0000120
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900121 /* Initialize caches... */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900122 la t9, mips_cache_reset
123 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900124 nop
wdenkc0218802003-03-27 12:09:35 +0000125
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900126 /* ... and enable them */
Daniel Schwierzeckab2a98b2011-07-27 13:22:38 +0200127 li t0, CONFIG_SYS_MIPS_CACHE_MODE
wdenkc0218802003-03-27 12:09:35 +0000128 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900129#endif
wdenkc0218802003-03-27 12:09:35 +0000130
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900131 /* Set up temporary stack */
Gabor Juhosf321b0f2013-01-24 06:27:52 +0000132 li sp, CONFIG_SYS_SDRAM_BASE + CONFIG_SYS_INIT_SP_OFFSET
wdenkc0218802003-03-27 12:09:35 +0000133
wdenkc0218802003-03-27 12:09:35 +0000134 la t9, board_init_f
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900135 jr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900136 nop
wdenkc0218802003-03-27 12:09:35 +0000137
wdenkc0218802003-03-27 12:09:35 +0000138/*
139 * void relocate_code (addr_sp, gd, addr_moni)
140 *
141 * This "function" does not return, instead it continues in RAM
142 * after relocating the monitor code.
143 *
144 * a0 = addr_sp
145 * a1 = gd
146 * a2 = destination address
147 */
148 .globl relocate_code
149 .ent relocate_code
150relocate_code:
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900151 move sp, a0 # set new stack pointer
wdenkc0218802003-03-27 12:09:35 +0000152
Gabor Juhosb2fe86f2013-01-24 06:27:53 +0000153 move s0, a1 # save gd in s0
154 move s2, a2 # save destination address in s2
155
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200156 li t0, CONFIG_SYS_MONITOR_BASE
Gabor Juhos248fe032013-01-24 06:27:54 +0000157 sub s1, s2, t0 # s1 <-- relocation offset
158
wdenk27b207f2003-07-24 23:38:38 +0000159 la t3, in_ram
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100160 lw t2, -12(t3) # t2 <-- __image_copy_end
wdenk27b207f2003-07-24 23:38:38 +0000161 move t1, a2
162
Gabor Juhos248fe032013-01-24 06:27:54 +0000163 add gp, s1 # adjust gp
wdenk8bde7f72003-06-27 21:31:46 +0000164
wdenkc0218802003-03-27 12:09:35 +0000165 /*
166 * t0 = source address
167 * t1 = target address
168 * t2 = source end address
169 */
1701:
171 lw t3, 0(t0)
172 sw t3, 0(t1)
173 addu t0, 4
Gabor Juhos5b7dd812013-01-24 06:27:51 +0000174 blt t0, t2, 1b
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900175 addu t1, 4
wdenkc0218802003-03-27 12:09:35 +0000176
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900177 /* If caches were enabled, we would have to flush them here. */
Gabor Juhos67d80c92013-01-24 06:27:55 +0000178 sub a1, t1, s2 # a1 <-- size
Stefan Roese71fa0712008-11-18 16:36:12 +0100179 la t9, flush_cache
180 jalr t9
Gabor Juhos67d80c92013-01-24 06:27:55 +0000181 move a0, s2 # a0 <-- destination address
Stefan Roese71fa0712008-11-18 16:36:12 +0100182
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900183 /* Jump to where we've relocated ourselves */
Stefan Roese71fa0712008-11-18 16:36:12 +0100184 addi t0, s2, in_ram - _start
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900185 jr t0
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900186 nop
wdenkc0218802003-03-27 12:09:35 +0000187
Gabor Juhos04380c62013-02-12 22:22:13 +0100188 .word __rel_dyn_end
189 .word __rel_dyn_start
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100190 .word __image_copy_end
Vlad Lungu0f8c62a2008-05-05 14:04:00 +0300191 .word _GLOBAL_OFFSET_TABLE_
wdenkc0218802003-03-27 12:09:35 +0000192 .word num_got_entries
193
194in_ram:
Shinya Kuribayashi22069212007-10-21 10:55:36 +0900195 /*
196 * Now we want to update GOT.
197 *
198 * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
199 * generated by GNU ld. Skip these reserved entries from relocation.
wdenkc0218802003-03-27 12:09:35 +0000200 */
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900201 lw t3, -4(t0) # t3 <-- num_got_entries
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100202 lw t4, -8(t0) # t4 <-- _GLOBAL_OFFSET_TABLE_
Gabor Juhos025f2b32013-01-30 04:56:37 +0000203 add t4, s1 # t4 now holds relocated _G_O_T_
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900204 addi t4, t4, 8 # skipping first two entries
wdenkc0218802003-03-27 12:09:35 +0000205 li t2, 2
2061:
207 lw t1, 0(t4)
208 beqz t1, 2f
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900209 add t1, s1
wdenkc0218802003-03-27 12:09:35 +0000210 sw t1, 0(t4)
2112:
212 addi t2, 1
213 blt t2, t3, 1b
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900214 addi t4, 4
wdenkc0218802003-03-27 12:09:35 +0000215
Gabor Juhos04380c62013-02-12 22:22:13 +0100216 /* Update dynamic relocations */
217 lw t1, -16(t0) # t1 <-- __rel_dyn_start
218 lw t2, -20(t0) # t2 <-- __rel_dyn_end
219
220 b 2f # skip first reserved entry
221 addi t1, 8
222
2231:
224 lw t3, -4(t1) # t3 <-- relocation info
225
226 sub t3, 3
227 bnez t3, 2f # skip non R_MIPS_REL32 entries
228 nop
229
230 lw t3, -8(t1) # t3 <-- location to fix up in FLASH
231
232 lw t4, 0(t3) # t4 <-- original pointer
233 add t4, s1 # t4 <-- adjusted pointer
234
235 add t3, s1 # t3 <-- location to fix up in RAM
236 sw t4, 0(t3)
237
2382:
239 blt t1, t2, 1b
240 addi t1, 8 # each rel.dyn entry is 8 bytes
241
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100242 /*
243 * Clear BSS
244 *
245 * GOT is now relocated. Thus __bss_start and __bss_end can be
246 * accessed directly via $gp.
247 */
248 la t1, __bss_start # t1 <-- __bss_start
249 la t2, __bss_end # t2 <-- __bss_end
wdenkc0218802003-03-27 12:09:35 +0000250
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09002511:
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100252 sw zero, 0(t1)
253 blt t1, t2, 1b
254 addi t1, 4
wdenk8bde7f72003-06-27 21:31:46 +0000255
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900256 move a0, s0 # a0 <-- gd
wdenkc0218802003-03-27 12:09:35 +0000257 la t9, board_init_r
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900258 jr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900259 move a1, s2
wdenkc0218802003-03-27 12:09:35 +0000260
261 .end relocate_code