blob: 70ad198cc9d2a54888d447a8641345bc5faa491a [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
Gabor Juhos843a76b2013-05-22 03:57:46 +000044#if defined(CONFIG_SYS_XWAY_EBU_BOOTCFG)
Daniel Schwierzeck7185adb2011-07-27 13:22:37 +020045 /*
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
Gabor Juhos843a76b2013-05-22 03:57:46 +000054#elif defined(CONFIG_QEMU_MALTA)
55 /*
56 * Linux expects the Board ID here.
57 */
58 .word 0x00000420 # 0x420 (Malta Board with CoreLV)
59 .word 0x00000000
wdenkc0218802003-03-27 12:09:35 +000060#endif
wdenk8bde7f72003-06-27 21:31:46 +000061
Daniel Schwierzeck8b1c7342013-02-12 22:22:12 +010062 .org 0x200
63 /* TLB refill, 32 bit task */
641: b 1b
65 nop
66
67 .org 0x280
68 /* XTLB refill, 64 bit task */
691: b 1b
70 nop
71
72 .org 0x300
73 /* Cache error exception */
741: b 1b
75 nop
76
77 .org 0x380
78 /* General exception */
791: b 1b
80 nop
81
82 .org 0x400
83 /* Catch interrupt exceptions */
841: b 1b
85 nop
86
87 .org 0x480
88 /* EJTAG debug exception */
891: b 1b
90 nop
91
wdenkc0218802003-03-27 12:09:35 +000092 .align 4
93reset:
94
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +090095 /* Clear watch registers */
wdenkc0218802003-03-27 12:09:35 +000096 mtc0 zero, CP0_WATCHLO
97 mtc0 zero, CP0_WATCHHI
98
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +090099 /* WP(Watch Pending), SW0/1 should be cleared */
Shinya Kuribayashid43d43e2008-03-25 21:30:07 +0900100 mtc0 zero, CP0_CAUSE
101
Daniel Schwierzeck4dc74122013-02-12 22:22:12 +0100102 setup_c0_status 0 0
wdenkc0218802003-03-27 12:09:35 +0000103
wdenkc0218802003-03-27 12:09:35 +0000104 /* Init Timer */
105 mtc0 zero, CP0_COUNT
106 mtc0 zero, CP0_COMPARE
107
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900108#ifndef CONFIG_SKIP_LOWLEVEL_INIT
wdenkc0218802003-03-27 12:09:35 +0000109 /* CONFIG0 register */
110 li t0, CONF_CM_UNCACHED
111 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900112#endif
wdenkc0218802003-03-27 12:09:35 +0000113
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900114 /* Initialize $gp */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900115 bal 1f
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900116 nop
Shinya Kuribayashi22069212007-10-21 10:55:36 +0900117 .word _gp
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09001181:
Shinya Kuribayashi16664f72007-11-17 20:05:26 +0900119 lw gp, 0(ra)
Wolfgang Denkc75eba32005-12-01 02:15:07 +0100120
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900121#ifndef CONFIG_SKIP_LOWLEVEL_INIT
122 /* Initialize any external memory */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900123 la t9, lowlevel_init
124 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900125 nop
wdenkc0218802003-03-27 12:09:35 +0000126
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900127 /* Initialize caches... */
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900128 la t9, mips_cache_reset
129 jalr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900130 nop
wdenkc0218802003-03-27 12:09:35 +0000131
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900132 /* ... and enable them */
Daniel Schwierzeckab2a98b2011-07-27 13:22:38 +0200133 li t0, CONFIG_SYS_MIPS_CACHE_MODE
wdenkc0218802003-03-27 12:09:35 +0000134 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900135#endif
wdenkc0218802003-03-27 12:09:35 +0000136
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900137 /* Set up temporary stack */
Gabor Juhosf321b0f2013-01-24 06:27:52 +0000138 li sp, CONFIG_SYS_SDRAM_BASE + CONFIG_SYS_INIT_SP_OFFSET
wdenkc0218802003-03-27 12:09:35 +0000139
wdenkc0218802003-03-27 12:09:35 +0000140 la t9, board_init_f
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900141 jr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900142 nop
wdenkc0218802003-03-27 12:09:35 +0000143
wdenkc0218802003-03-27 12:09:35 +0000144/*
145 * void relocate_code (addr_sp, gd, addr_moni)
146 *
147 * This "function" does not return, instead it continues in RAM
148 * after relocating the monitor code.
149 *
150 * a0 = addr_sp
151 * a1 = gd
152 * a2 = destination address
153 */
154 .globl relocate_code
155 .ent relocate_code
156relocate_code:
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900157 move sp, a0 # set new stack pointer
wdenkc0218802003-03-27 12:09:35 +0000158
Gabor Juhosb2fe86f2013-01-24 06:27:53 +0000159 move s0, a1 # save gd in s0
160 move s2, a2 # save destination address in s2
161
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200162 li t0, CONFIG_SYS_MONITOR_BASE
Gabor Juhos248fe032013-01-24 06:27:54 +0000163 sub s1, s2, t0 # s1 <-- relocation offset
164
wdenk27b207f2003-07-24 23:38:38 +0000165 la t3, in_ram
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100166 lw t2, -12(t3) # t2 <-- __image_copy_end
wdenk27b207f2003-07-24 23:38:38 +0000167 move t1, a2
168
Gabor Juhos248fe032013-01-24 06:27:54 +0000169 add gp, s1 # adjust gp
wdenk8bde7f72003-06-27 21:31:46 +0000170
wdenkc0218802003-03-27 12:09:35 +0000171 /*
172 * t0 = source address
173 * t1 = target address
174 * t2 = source end address
175 */
1761:
177 lw t3, 0(t0)
178 sw t3, 0(t1)
179 addu t0, 4
Gabor Juhos5b7dd812013-01-24 06:27:51 +0000180 blt t0, t2, 1b
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900181 addu t1, 4
wdenkc0218802003-03-27 12:09:35 +0000182
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900183 /* If caches were enabled, we would have to flush them here. */
Gabor Juhos67d80c92013-01-24 06:27:55 +0000184 sub a1, t1, s2 # a1 <-- size
Stefan Roese71fa0712008-11-18 16:36:12 +0100185 la t9, flush_cache
186 jalr t9
Gabor Juhos67d80c92013-01-24 06:27:55 +0000187 move a0, s2 # a0 <-- destination address
Stefan Roese71fa0712008-11-18 16:36:12 +0100188
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900189 /* Jump to where we've relocated ourselves */
Stefan Roese71fa0712008-11-18 16:36:12 +0100190 addi t0, s2, in_ram - _start
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900191 jr t0
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900192 nop
wdenkc0218802003-03-27 12:09:35 +0000193
Gabor Juhos04380c62013-02-12 22:22:13 +0100194 .word __rel_dyn_end
195 .word __rel_dyn_start
Daniel Schwierzeck28875e22013-02-12 22:22:13 +0100196 .word __image_copy_end
Vlad Lungu0f8c62a2008-05-05 14:04:00 +0300197 .word _GLOBAL_OFFSET_TABLE_
wdenkc0218802003-03-27 12:09:35 +0000198 .word num_got_entries
199
200in_ram:
Shinya Kuribayashi22069212007-10-21 10:55:36 +0900201 /*
202 * Now we want to update GOT.
203 *
204 * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
205 * generated by GNU ld. Skip these reserved entries from relocation.
wdenkc0218802003-03-27 12:09:35 +0000206 */
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900207 lw t3, -4(t0) # t3 <-- num_got_entries
Gabor Juhos680cb2d2013-06-13 12:59:27 +0200208 lw t8, -8(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_
209 add t8, s1 # t8 now holds relocated _G_O_T_
210 addi t8, t8, 8 # skipping first two entries
wdenkc0218802003-03-27 12:09:35 +0000211 li t2, 2
2121:
Gabor Juhos680cb2d2013-06-13 12:59:27 +0200213 lw t1, 0(t8)
wdenkc0218802003-03-27 12:09:35 +0000214 beqz t1, 2f
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900215 add t1, s1
Gabor Juhos680cb2d2013-06-13 12:59:27 +0200216 sw t1, 0(t8)
wdenkc0218802003-03-27 12:09:35 +00002172:
218 addi t2, 1
219 blt t2, t3, 1b
Gabor Juhos680cb2d2013-06-13 12:59:27 +0200220 addi t8, 4
wdenkc0218802003-03-27 12:09:35 +0000221
Gabor Juhos04380c62013-02-12 22:22:13 +0100222 /* Update dynamic relocations */
223 lw t1, -16(t0) # t1 <-- __rel_dyn_start
224 lw t2, -20(t0) # t2 <-- __rel_dyn_end
225
226 b 2f # skip first reserved entry
227 addi t1, 8
228
2291:
Gabor Juhos691995f2013-06-13 12:59:28 +0200230 lw t8, -4(t1) # t8 <-- relocation info
Gabor Juhos04380c62013-02-12 22:22:13 +0100231
Gabor Juhos691995f2013-06-13 12:59:28 +0200232 li t3, 3
233 bne t8, t3, 2f # skip non R_MIPS_REL32 entries
Gabor Juhos04380c62013-02-12 22:22:13 +0100234 nop
235
236 lw t3, -8(t1) # t3 <-- location to fix up in FLASH
237
Gabor Juhos680cb2d2013-06-13 12:59:27 +0200238 lw t8, 0(t3) # t8 <-- original pointer
239 add t8, s1 # t8 <-- adjusted pointer
Gabor Juhos04380c62013-02-12 22:22:13 +0100240
241 add t3, s1 # t3 <-- location to fix up in RAM
Gabor Juhos680cb2d2013-06-13 12:59:27 +0200242 sw t8, 0(t3)
Gabor Juhos04380c62013-02-12 22:22:13 +0100243
2442:
245 blt t1, t2, 1b
246 addi t1, 8 # each rel.dyn entry is 8 bytes
247
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100248 /*
249 * Clear BSS
250 *
251 * GOT is now relocated. Thus __bss_start and __bss_end can be
252 * accessed directly via $gp.
253 */
254 la t1, __bss_start # t1 <-- __bss_start
255 la t2, __bss_end # t2 <-- __bss_end
wdenkc0218802003-03-27 12:09:35 +0000256
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +09002571:
Daniel Schwierzeck696a3b22013-02-12 22:22:13 +0100258 sw zero, 0(t1)
259 blt t1, t2, 1b
260 addi t1, 4
wdenk8bde7f72003-06-27 21:31:46 +0000261
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900262 move a0, s0 # a0 <-- gd
wdenkc0218802003-03-27 12:09:35 +0000263 la t9, board_init_r
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900264 jr t9
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900265 move a1, s2
wdenkc0218802003-03-27 12:09:35 +0000266
267 .end relocate_code