blob: 227af6d9a3d988b87f6f9ba04a0b171b06002453 [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>
Paul Burtona39b1cb2015-01-29 10:04:08 +000011#include <asm/asm.h>
wdenkc0218802003-03-27 12:09:35 +000012#include <asm/regdef.h>
13#include <asm/mipsregs.h>
14
Daniel Schwierzeckab2a98b2011-07-27 13:22:38 +020015#ifndef CONFIG_SYS_MIPS_CACHE_MODE
16#define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
17#endif
18
Daniel Schwierzeckdd821282015-01-18 22:18:38 +010019#ifndef CONFIG_SYS_INIT_SP_ADDR
20#define CONFIG_SYS_INIT_SP_ADDR (CONFIG_SYS_SDRAM_BASE + \
21 CONFIG_SYS_INIT_SP_OFFSET)
22#endif
23
Shinya Kuribayashidecaba62008-03-25 21:30:07 +090024 /*
25 * For the moment disable interrupts, mark the kernel mode and
26 * set ST0_KX so that the CPU does not spit fire when using
27 * 64-bit addresses.
28 */
29 .macro setup_c0_status set clr
30 .set push
31 mfc0 t0, CP0_STATUS
32 or t0, ST0_CU0 | \set | 0x1f | \clr
33 xor t0, 0x1f | \clr
34 mtc0 t0, CP0_STATUS
35 .set noreorder
36 sll zero, 3 # ehb
37 .set pop
38 .endm
39
wdenkc0218802003-03-27 12:09:35 +000040 .set noreorder
41
42 .globl _start
43 .text
44_start:
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010045 /* U-boot entry point */
46 b reset
47 nop
48
49 .org 0x10
Gabor Juhos843a76b2013-05-22 03:57:46 +000050#if defined(CONFIG_SYS_XWAY_EBU_BOOTCFG)
Daniel Schwierzeck7185adb2011-07-27 13:22:37 +020051 /*
52 * Almost all Lantiq XWAY SoC devices have an external bus unit (EBU) to
53 * access external NOR flashes. If the board boots from NOR flash the
54 * internal BootROM does a blind read at address 0xB0000010 to read the
55 * initial configuration for that EBU in order to access the flash
56 * device with correct parameters. This config option is board-specific.
57 */
58 .word CONFIG_SYS_XWAY_EBU_BOOTCFG
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010059 .word 0x0
Paul Burton7a9d1092013-11-09 10:22:08 +000060#elif defined(CONFIG_MALTA)
Gabor Juhos843a76b2013-05-22 03:57:46 +000061 /*
62 * Linux expects the Board ID here.
63 */
64 .word 0x00000420 # 0x420 (Malta Board with CoreLV)
65 .word 0x00000000
wdenkc0218802003-03-27 12:09:35 +000066#endif
wdenk8bde7f72003-06-27 21:31:46 +000067
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010068 .org 0x200
69 /* TLB refill, 32 bit task */
701: b 1b
71 nop
72
73 .org 0x280
74 /* XTLB refill, 64 bit task */
751: b 1b
76 nop
77
78 .org 0x300
79 /* Cache error exception */
801: b 1b
81 nop
82
83 .org 0x380
84 /* General exception */
851: b 1b
86 nop
87
88 .org 0x400
89 /* Catch interrupt exceptions */
901: b 1b
91 nop
92
93 .org 0x480
94 /* EJTAG debug exception */
951: b 1b
96 nop
97
wdenkc0218802003-03-27 12:09:35 +000098 .align 4
99reset:
100
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900101 /* Clear watch registers */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000102 MTC0 zero, CP0_WATCHLO
103 MTC0 zero, CP0_WATCHHI
wdenkc0218802003-03-27 12:09:35 +0000104
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900105 /* WP(Watch Pending), SW0/1 should be cleared */
Shinya Kuribayashid43d43e2008-03-25 21:30:07 +0900106 mtc0 zero, CP0_CAUSE
107
Daniel Schwierzeck4dc74122013-02-12 22:22:12 +0100108 setup_c0_status 0 0
wdenkc0218802003-03-27 12:09:35 +0000109
wdenkc0218802003-03-27 12:09:35 +0000110 /* Init Timer */
111 mtc0 zero, CP0_COUNT
112 mtc0 zero, CP0_COMPARE
113
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900114#ifndef CONFIG_SKIP_LOWLEVEL_INIT
wdenkc0218802003-03-27 12:09:35 +0000115 /* CONFIG0 register */
116 li t0, CONF_CM_UNCACHED
117 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900118#endif
wdenkc0218802003-03-27 12:09:35 +0000119
Paul Burtona39b1cb2015-01-29 10:04:08 +0000120 /*
121 * Initialize $gp, force pointer sized alignment of bal instruction to
122 * forbid the compiler to put nop's between bal and _gp. This is
123 * required to keep _gp and ra aligned to 8 byte.
124 */
125 .align PTRLOG
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900126 bal 1f
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900127 nop
Paul Burtona39b1cb2015-01-29 10:04:08 +0000128 PTR _gp
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09001291:
Paul Burtona39b1cb2015-01-29 10:04:08 +0000130 PTR_L gp, 0(ra)
Wolfgang Denkc75eba32005-12-01 02:15:07 +0100131
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900132#ifndef CONFIG_SKIP_LOWLEVEL_INIT
133 /* Initialize any external memory */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000134 PTR_LA t9, lowlevel_init
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900135 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900136 nop
wdenkc0218802003-03-27 12:09:35 +0000137
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900138 /* Initialize caches... */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000139 PTR_LA t9, mips_cache_reset
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900140 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900141 nop
wdenkc0218802003-03-27 12:09:35 +0000142
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900143 /* ... and enable them */
Daniel Schwierzeckab2a98b2011-07-27 13:22:38 +0200144 li t0, CONFIG_SYS_MIPS_CACHE_MODE
wdenkc0218802003-03-27 12:09:35 +0000145 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900146#endif
wdenkc0218802003-03-27 12:09:35 +0000147
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900148 /* Set up temporary stack */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000149 PTR_LI t0, -16
150 PTR_LI t1, CONFIG_SYS_INIT_SP_ADDR
Daniel Schwierzecke5200232015-01-18 22:18:39 +0100151 and sp, t1, t0 # force 16 byte alignment
Paul Burtona39b1cb2015-01-29 10:04:08 +0000152 PTR_SUB sp, sp, GD_SIZE # reserve space for gd
Daniel Schwierzecke5200232015-01-18 22:18:39 +0100153 and sp, sp, t0 # force 16 byte alignment
154 move k0, sp # save gd pointer
155#ifdef CONFIG_SYS_MALLOC_F_LEN
Paul Burtona39b1cb2015-01-29 10:04:08 +0000156 PTR_LI t2, CONFIG_SYS_MALLOC_F_LEN
157 PTR_SUB sp, sp, t2 # reserve space for early malloc
Daniel Schwierzecke5200232015-01-18 22:18:39 +0100158 and sp, sp, t0 # force 16 byte alignment
159#endif
Daniel Schwierzeck6d08e222014-11-20 23:55:32 +0100160 move fp, sp
wdenkc0218802003-03-27 12:09:35 +0000161
Daniel Schwierzecke5200232015-01-18 22:18:39 +0100162 /* Clear gd */
163 move t0, k0
1641:
165 sw zero, 0(t0)
166 blt t0, t1, 1b
Paul Burtona39b1cb2015-01-29 10:04:08 +0000167 PTR_ADDI t0, 4
Daniel Schwierzecke5200232015-01-18 22:18:39 +0100168
169#ifdef CONFIG_SYS_MALLOC_F_LEN
Paul Burtona39b1cb2015-01-29 10:04:08 +0000170 PTR_ADDU t0, k0, GD_MALLOC_BASE # gd->malloc_base offset
Daniel Schwierzecke5200232015-01-18 22:18:39 +0100171 sw sp, 0(t0)
172#endif
173
Paul Burtona39b1cb2015-01-29 10:04:08 +0000174 PTR_LA t9, board_init_f
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900175 jr t9
Daniel Schwierzeck6d08e222014-11-20 23:55:32 +0100176 move ra, zero
wdenkc0218802003-03-27 12:09:35 +0000177
wdenkc0218802003-03-27 12:09:35 +0000178/*
179 * void relocate_code (addr_sp, gd, addr_moni)
180 *
181 * This "function" does not return, instead it continues in RAM
182 * after relocating the monitor code.
183 *
184 * a0 = addr_sp
185 * a1 = gd
186 * a2 = destination address
187 */
188 .globl relocate_code
189 .ent relocate_code
190relocate_code:
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900191 move sp, a0 # set new stack pointer
Daniel Schwierzeck6d08e222014-11-20 23:55:32 +0100192 move fp, sp
wdenkc0218802003-03-27 12:09:35 +0000193
Gabor Juhosb2fe86f2013-01-24 06:27:53 +0000194 move s0, a1 # save gd in s0
195 move s2, a2 # save destination address in s2
196
Paul Burtona39b1cb2015-01-29 10:04:08 +0000197 PTR_LI t0, CONFIG_SYS_MONITOR_BASE
198 PTR_SUB s1, s2, t0 # s1 <-- relocation offset
Gabor Juhos248fe032013-01-24 06:27:54 +0000199
Paul Burtona39b1cb2015-01-29 10:04:08 +0000200 PTR_LA t3, in_ram
201 PTR_L t2, -(3 * PTRSIZE)(t3) # t2 <-- __image_copy_end
wdenk27b207f2003-07-24 23:38:38 +0000202 move t1, a2
203
Paul Burtona39b1cb2015-01-29 10:04:08 +0000204 PTR_ADD gp, s1 # adjust gp
wdenk8bde7f72003-06-27 21:31:46 +0000205
wdenkc0218802003-03-27 12:09:35 +0000206 /*
207 * t0 = source address
208 * t1 = target address
209 * t2 = source end address
210 */
2111:
212 lw t3, 0(t0)
213 sw t3, 0(t1)
Paul Burtona39b1cb2015-01-29 10:04:08 +0000214 PTR_ADDU t0, 4
Gabor Juhos5b7dd812013-01-24 06:27:51 +0000215 blt t0, t2, 1b
Paul Burtona39b1cb2015-01-29 10:04:08 +0000216 PTR_ADDU t1, 4
wdenkc0218802003-03-27 12:09:35 +0000217
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900218 /* If caches were enabled, we would have to flush them here. */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000219 PTR_SUB a1, t1, s2 # a1 <-- size
220 PTR_LA t9, flush_cache
Stefan Roese71fa0712008-11-18 16:36:12 +0100221 jalr t9
Gabor Juhos67d80c92013-01-24 06:27:55 +0000222 move a0, s2 # a0 <-- destination address
Stefan Roese71fa0712008-11-18 16:36:12 +0100223
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900224 /* Jump to where we've relocated ourselves */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000225 PTR_ADDI t0, s2, in_ram - _start
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900226 jr t0
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900227 nop
wdenkc0218802003-03-27 12:09:35 +0000228
Paul Burtona39b1cb2015-01-29 10:04:08 +0000229 PTR __rel_dyn_end
230 PTR __rel_dyn_start
231 PTR __image_copy_end
232 PTR _GLOBAL_OFFSET_TABLE_
233 PTR num_got_entries
wdenkc0218802003-03-27 12:09:35 +0000234
235in_ram:
Shinya Kuribayashi22069212007-10-21 10:55:36 +0900236 /*
237 * Now we want to update GOT.
238 *
239 * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
240 * generated by GNU ld. Skip these reserved entries from relocation.
wdenkc0218802003-03-27 12:09:35 +0000241 */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000242 PTR_L t3, -(1 * PTRSIZE)(t0) # t3 <-- num_got_entries
243 PTR_L t8, -(2 * PTRSIZE)(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_
244 PTR_ADD t8, s1 # t8 now holds relocated _G_O_T_
245 PTR_ADDI t8, t8, 2 * PTRSIZE # skipping first two entries
246 PTR_LI t2, 2
wdenkc0218802003-03-27 12:09:35 +00002471:
Paul Burtona39b1cb2015-01-29 10:04:08 +0000248 PTR_L t1, 0(t8)
wdenkc0218802003-03-27 12:09:35 +0000249 beqz t1, 2f
Paul Burtona39b1cb2015-01-29 10:04:08 +0000250 PTR_ADD t1, s1
251 PTR_S t1, 0(t8)
wdenkc0218802003-03-27 12:09:35 +00002522:
Paul Burtona39b1cb2015-01-29 10:04:08 +0000253 PTR_ADDI t2, 1
wdenkc0218802003-03-27 12:09:35 +0000254 blt t2, t3, 1b
Paul Burtona39b1cb2015-01-29 10:04:08 +0000255 PTR_ADDI t8, PTRSIZE
wdenkc0218802003-03-27 12:09:35 +0000256
Gabor Juhos04380c62013-02-12 22:22:13 +0100257 /* Update dynamic relocations */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000258 PTR_L t1, -(4 * PTRSIZE)(t0) # t1 <-- __rel_dyn_start
259 PTR_L t2, -(5 * PTRSIZE)(t0) # t2 <-- __rel_dyn_end
Gabor Juhos04380c62013-02-12 22:22:13 +0100260
261 b 2f # skip first reserved entry
Paul Burtona39b1cb2015-01-29 10:04:08 +0000262 PTR_ADDI t1, 2 * PTRSIZE
Gabor Juhos04380c62013-02-12 22:22:13 +0100263
2641:
Gabor Juhos691995f2013-06-13 12:59:28 +0200265 lw t8, -4(t1) # t8 <-- relocation info
Gabor Juhos04380c62013-02-12 22:22:13 +0100266
Paul Burtona39b1cb2015-01-29 10:04:08 +0000267 PTR_LI t3, 3
Gabor Juhos691995f2013-06-13 12:59:28 +0200268 bne t8, t3, 2f # skip non R_MIPS_REL32 entries
Gabor Juhos04380c62013-02-12 22:22:13 +0100269 nop
270
Paul Burtona39b1cb2015-01-29 10:04:08 +0000271 PTR_L t3, -(2 * PTRSIZE)(t1) # t3 <-- location to fix up in FLASH
Gabor Juhos04380c62013-02-12 22:22:13 +0100272
Paul Burtona39b1cb2015-01-29 10:04:08 +0000273 PTR_L t8, 0(t3) # t8 <-- original pointer
274 PTR_ADD t8, s1 # t8 <-- adjusted pointer
Gabor Juhos04380c62013-02-12 22:22:13 +0100275
Paul Burtona39b1cb2015-01-29 10:04:08 +0000276 PTR_ADD t3, s1 # t3 <-- location to fix up in RAM
277 PTR_S t8, 0(t3)
Gabor Juhos04380c62013-02-12 22:22:13 +0100278
2792:
280 blt t1, t2, 1b
Paul Burtona39b1cb2015-01-29 10:04:08 +0000281 PTR_ADDI t1, 2 * PTRSIZE # each rel.dyn entry is 2*PTRSIZE bytes
Gabor Juhos04380c62013-02-12 22:22:13 +0100282
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100283 /*
284 * Clear BSS
285 *
286 * GOT is now relocated. Thus __bss_start and __bss_end can be
287 * accessed directly via $gp.
288 */
Paul Burtona39b1cb2015-01-29 10:04:08 +0000289 PTR_LA t1, __bss_start # t1 <-- __bss_start
290 PTR_LA t2, __bss_end # t2 <-- __bss_end
wdenkc0218802003-03-27 12:09:35 +0000291
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09002921:
Paul Burtona39b1cb2015-01-29 10:04:08 +0000293 PTR_S zero, 0(t1)
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100294 blt t1, t2, 1b
Paul Burtona39b1cb2015-01-29 10:04:08 +0000295 PTR_ADDI t1, PTRSIZE
wdenk8bde7f72003-06-27 21:31:46 +0000296
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900297 move a0, s0 # a0 <-- gd
Daniel Schwierzeck6d08e222014-11-20 23:55:32 +0100298 move a1, s2
Paul Burtona39b1cb2015-01-29 10:04:08 +0000299 PTR_LA t9, board_init_r
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900300 jr t9
Daniel Schwierzeck6d08e222014-11-20 23:55:32 +0100301 move ra, zero
wdenkc0218802003-03-27 12:09:35 +0000302
303 .end relocate_code