1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Andesboot - Startup Code for Whitiger core
5 * Copyright (C) 2006 Andes Technology Corporation
6 * Copyright (C) 2006 Shawn Lin <nobuhiro@andestech.com>
7 * Copyright (C) 2011 Macpaul Lin <macpaul@andestech.com>
8 * Greentime Hu <greentime@andestech.com>
13 #include <asm-offsets.h>
16 #include <asm/macro.h>
19 * Jump vector table for EVIC mode
22 #define DIS_DCAC ~ENA_DCAC
23 #define ICAC_MEM_KBF_ISET (0x07) ! I Cache sets per way
24 #define ICAC_MEM_KBF_IWAY (0x07<<3) ! I cache ways
25 #define ICAC_MEM_KBF_ISZ (0x07<<6) ! I cache line size
26 #define DCAC_MEM_KBF_DSET (0x07) ! D Cache sets per way
27 #define DCAC_MEM_KBF_DWAY (0x07<<3) ! D cache ways
28 #define DCAC_MEM_KBF_DSZ (0x07<<6) ! D cache line size
31 #define EIT_INTR_PSW $ir1 ! interruption $PSW
32 #define EIT_PREV_IPSW $ir2 ! previous $IPSW
33 #define EIT_IVB $ir3 ! intr vector base address
34 #define EIT_EVA $ir4 ! MMU related Exception VA reg
35 #define EIT_PREV_EVA $ir5 ! previous $eva
36 #define EIT_ITYPE $ir6 ! interruption type
37 #define EIT_PREV_ITYPE $ir7 ! prev intr type
38 #define EIT_MACH_ERR $ir8 ! machine error log
39 #define EIT_INTR_PC $ir9 ! Interruption PC
40 #define EIT_PREV_IPC $ir10 ! previous $IPC
41 #define EIT_OVL_INTR_PC $ir11 ! overflow interruption PC
42 #define EIT_PREV_P0 $ir12 ! prev $P0
43 #define EIT_PREV_P1 $ir13 ! prev $p1
44 #define CR_ICAC_MEM $cr1 ! I-cache/memory config reg
45 #define CR_DCAC_MEM $cr2 ! D-cache/memory config reg
46 #define MR_CAC_CTL $mr8
59 j internal_interrupt ! H0I
60 j internal_interrupt ! H1I
61 j internal_interrupt ! H2I
62 j internal_interrupt ! H3I
63 j internal_interrupt ! H4I
64 j internal_interrupt ! H5I
65 j software_interrupt ! S0I
70 * Andesboot Startup Code (reset vector)
73 * 1.1 reset - start of u-boot
74 * 1.2 to superuser mode - as is when reset
75 * 1.4 Do lowlevel_init
76 * - (this will jump out to lowlevel_init.S in SoC)
78 * 1.3 Turn off watchdog timer
79 * - (this will jump out to watchdog.S in SoC)
80 * - (turnoff_watchdog)
81 * 2. Do critical init when reboot (not from mem)
82 * 3. Relocate andesboot to ram
84 * 5. Jump to second stage (board_init_r)
87 /* Note: TEXT_BASE is defined by the (board-dependent) linker script */
90 .word CONFIG_SYS_TEXT_BASE
92 /* IRQ stack memory (calculated at run-time) + 8 bytes */
93 .globl IRQ_STACK_START_IN
98 * The bootstrap code of nds32 core
104 * gp = ~0 for burn mode
105 * = ~load_address for load mode
109 sethi $gp, hi20(_GLOBAL_OFFSET_TABLE_-8)
111 ori $gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_-4)
118 /* set IVIC, vector size: 4 bytes, base: 0x0 */
121 * MMU_CTL NTC0 Non-cacheable
132 #if (!defined(CONFIG_SYS_ICACHE_OFF) || !defined(CONFIG_SYS_DCACHE_OFF))
134 * MMU_CTL NTC0 Cacheable/Write-Back
142 #ifndef CONFIG_SYS_DCACHE_OFF
143 #ifdef CONFIG_ARCH_MAP_SYSMEM
145 * MMU_CTL NTC1 Non-cacheable
152 * MMU_CTL NTM1 mapping for partition 0
161 #if !defined(CONFIG_SYS_ICACHE_OFF)
168 #if !defined(CONFIG_SYS_DCACHE_OFF)
177 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
180 * gp = ~VMA for burn mode
181 * = ~load_address for load mode
185 sethi $gp, hi20(_GLOBAL_OFFSET_TABLE_-8)
187 ori $gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_-4)
191 * do critical initializations first (shall be in short time)
192 * do self_relocation ASAP.
196 * Set the N1213 (Whitiger) core to superuser mode
197 * According to spec, it is already when reset
199 #ifndef CONFIG_SKIP_TRUNOFF_WATCHDOG
204 * Set stackpointer in internal RAM to call board_init_f
205 * $sp must be 8-byte alignment for ABI compliance.
208 li $sp, CONFIG_SYS_INIT_SP_ADDR
210 bal board_init_f_alloc_reserve
212 bal board_init_f_init_reserve
213 #ifdef CONFIG_DEBUG_UART
218 #ifdef __NDS32_N1213_43U1H__
219 /* __NDS32_N1213_43U1H__ implies NDS32 V0 ISA */
220 la $r15, board_init_f ! store function address into $r15
223 j board_init_f ! jump to board_init_f() in lib/board.c
226 * void relocate_code (addr_sp, gd, addr_moni)
228 * This "function" does not return, instead it continues in RAM
229 * after relocating the monitor code.
234 * gp = ~RAM_SIZE - TEXT_SIZE for burn/load mode
239 move $r4, $r0 /* save addr_sp */
240 move $r5, $r1 /* save addr of gd */
241 move $r6, $r2 /* save addr of destination */
243 /* Set up the stack */
247 la $r0, _start@GOTOFF
248 beq $r0, $r6, clear_bss /* skip relocation */
251 move $r2, $r6 /* r2 <- scratch for copy_loop */
253 lmw.bim $r11, [$r0], $r18
254 smw.bim $r11, [$r2], $r18
255 blt $r0, $r1, copy_loop
257 * fix relocations related issues
260 l.w $r0, _TEXT_BASE@GOTOFF /* r0 <- Text base */
261 sub $r9, $r6, $r0 /* r9 <- relocation offset */
263 la $r7, __rel_dyn_start@GOTOFF
264 add $r7, $r7, $r9 /* r2 <- rel __got_start in RAM */
265 la $r8, __rel_dyn_end@GOTOFF
266 add $r8, $r8, $r9 /* r2 <- rel __got_start in RAM */
267 li $r3, #0x2a /* R_NDS32_RELATIVE */
269 lmw.bim $r0, [$r7], $r2 /* r0,r1,r2 <- adr,type,addend */
279 la $r0, __bss_start@GOTOFF /* r0 <- rel __bss_start in FLASH */
280 add $r0, $r0, $r9 /* r0 <- rel __bss_start in FLASH */
281 la $r1, __bss_end@GOTOFF /* r1 <- rel __bss_end in RAM */
282 add $r1, $r1, $r9 /* r0 <- rel __bss_end in RAM */
283 li $r2, 0x00000000 /* clear */
286 sw $r2, [$r0] /* clear loop... */
288 bne $r0, $r1, clbss_l
291 * We are done. Do not return, instead branch to second part of board
292 * initialization, now running from RAM.
295 bal invalidate_icache_all
297 la $r0, board_init_r@GOTOFF
298 move $lp, $r0 /* offset of board_init_r() */
299 add $lp, $lp, $r9 /* real address of board_init_r() */
300 /* setup parameters for board_init_r */
301 move $r0, $r5 /* gd_t */
302 move $r1, $r6 /* dest_addr */
305 #ifdef __NDS32_N1213_43U1H__ /* NDS32 V0 ISA */
306 move $r15, $lp /* store function address into $r15 */
311 jr $lp /* jump to board_init_r() */
317 ! read $cr1(I CAC/MEM cfg. reg.) configuration
318 mfsr $t0, CR_ICAC_MEM
321 andi $p0, $t0, ICAC_MEM_KBF_ISZ
323 ! if $p0=0, then no I CAC existed
324 beqz $p0, end_flush_icache
326 ! get $p0 the index of I$ block
329 ! $t1= bit width of I cache line size(ISZ)
333 sll $t5, $t4, $t1 ! get $t5 cache line size
334 andi $p1, $t0, ICAC_MEM_KBF_ISET ! get the ISET field
335 addi $t2, $p1, 6 ! $t2= bit width of ISET
336 andi $p1, $t0, ICAC_MEM_KBF_IWAY ! get bitfield of Iway
338 addi $p1, $p1, 1 ! then $p1 is I way number
339 add $t3, $t2, $t1 ! SHIFT
340 sll $p1, $p1, $t3 ! GET the total cache size
343 cctl $p1, L1I_IX_INVAL
352 ! read $cr2(D CAC/MEM cfg. reg.) configuration
353 mfsr $t0, CR_DCAC_MEM
356 andi $p0, $t0, DCAC_MEM_KBF_DSZ
358 ! if $p0=0, then no D CAC existed
359 beqz $p0, end_flush_dcache
361 ! get $p0 the index of D$ block
364 ! $t1= bit width of D cache line size(DSZ)
368 sll $t5, $t4, $t1 ! get $t5 cache line size
369 andi $p1, $t0, DCAC_MEM_KBF_DSET ! get the DSET field
370 addi $t2, $p1, 6 ! $t2= bit width of DSET
371 andi $p1, $t0, DCAC_MEM_KBF_DWAY ! get bitfield of D way
373 addi $p1, $p1, 1 ! then $p1 is D way number
374 add $t3, $t2, $t1 ! SHIFT
375 sll $p1, $p1, $t3 ! GET the total cache size
378 cctl $p1, L1D_IX_INVAL
393 ! FIXME: Other way to get PC?
394 ! FIXME: Update according to the newest spec!!
398 mfsr $r28, PSW ! $PSW
400 mfsr $r28, EIT_EVA ! $ir1 $EVA
402 mfsr $r28, EIT_ITYPE ! $ir2 $ITYPE
404 mfsr $r28, EIT_MACH_ERR ! $ir3 Mach Error
406 mfsr $r28, EIT_INTR_PSW ! $ir5 $IPSW
408 mfsr $r28, EIT_PREV_IPSW ! $ir6 prev $IPSW
410 mfsr $r28, EIT_PREV_EVA ! $ir7 prev $EVA
412 mfsr $r28, EIT_PREV_ITYPE ! $ir8 prev $ITYPE
414 mfsr $r28, EIT_INTR_PC ! $ir9 Interruption PC
416 mfsr $r28, EIT_PREV_IPC ! $ir10 prev INTR_PC
418 mfsr $r28, EIT_OVL_INTR_PC ! $ir11 Overflowed INTR_PC
428 pushm $r0, $r30 ! store $sp-$r31, ra-$r30, $gp-$r29, $r28-$fp
429 addi $sp, $sp, -4 ! make room for implicit pt_regs parameters
435 move $r0, $sp ! To get the kernel stack
436 li $r1, 1 ! Determine interruption type
442 move $r0, $sp ! To get the kernel stack
443 li $r1, 2 ! Determine interruption type
449 move $r0, $sp ! To get the kernel stack
450 li $r1, 3 ! Determine interruption type
456 move $r0, $sp ! To get the kernel stack
457 li $r1, 4 ! Determine interruption type
463 move $r0, $sp ! To get the kernel stack
464 li $r1, 5 ! Determine interruption type
470 move $r0, $sp ! To get the kernel stack
471 li $r1, 6 ! Determine interruption type
477 move $r0, $sp ! To get the kernel stack
478 li $r1, 7 ! Determine interruption type
484 move $r0, $sp ! To get the kernel stack
485 li $r1, 8 ! Determine interruption type
491 move $r0, $sp ! To get the kernel stack
492 li $r1, 9 ! Determine interruption type
498 move $r0, $sp ! To get the kernel stack
499 li $r1, 10 ! Determine interruption type
505 * void reset_cpu(ulong addr);
506 * $r0: input address to jump to
510 /* No need to disable MMU because we never enable it */
515 andi $p0, $p0, 0x3 ! MMPS
516 li $p1, 0x2 ! TLB MMU
518 tlbop flushall ! Flush TLB
520 mfsr $p0, MR_CAC_CTL ! Get the $CACHE_CTL reg
522 and $p0, $p0, $p1 ! Clear the DC_EN bit
523 mtsr $p0, MR_CAC_CTL ! Write back the $CACHE_CTL reg
524 br $r0 ! Jump to the input address