1eef88dfbSDaniel Schwierzeck/* 2eef88dfbSDaniel Schwierzeck * Startup Code for MIPS32 CPU-core 3eef88dfbSDaniel Schwierzeck * 4eef88dfbSDaniel Schwierzeck * Copyright (c) 2003 Wolfgang Denk <wd@denx.de> 5eef88dfbSDaniel Schwierzeck * 6eef88dfbSDaniel Schwierzeck * SPDX-License-Identifier: GPL-2.0+ 7eef88dfbSDaniel Schwierzeck */ 8eef88dfbSDaniel Schwierzeck 9eef88dfbSDaniel Schwierzeck#include <asm-offsets.h> 10eef88dfbSDaniel Schwierzeck#include <config.h> 11eef88dfbSDaniel Schwierzeck#include <asm/asm.h> 12eef88dfbSDaniel Schwierzeck#include <asm/regdef.h> 13eef88dfbSDaniel Schwierzeck#include <asm/mipsregs.h> 14eef88dfbSDaniel Schwierzeck 15eef88dfbSDaniel Schwierzeck#ifndef CONFIG_SYS_MIPS_CACHE_MODE 16eef88dfbSDaniel Schwierzeck#define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT 17eef88dfbSDaniel Schwierzeck#endif 18eef88dfbSDaniel Schwierzeck 19eef88dfbSDaniel Schwierzeck#ifndef CONFIG_SYS_INIT_SP_ADDR 20eef88dfbSDaniel Schwierzeck#define CONFIG_SYS_INIT_SP_ADDR (CONFIG_SYS_SDRAM_BASE + \ 21eef88dfbSDaniel Schwierzeck CONFIG_SYS_INIT_SP_OFFSET) 22eef88dfbSDaniel Schwierzeck#endif 23eef88dfbSDaniel Schwierzeck 24eef88dfbSDaniel Schwierzeck#ifdef CONFIG_32BIT 25eef88dfbSDaniel Schwierzeck# define MIPS_RELOC 3 26eef88dfbSDaniel Schwierzeck# define STATUS_SET 0 27eef88dfbSDaniel Schwierzeck#endif 28eef88dfbSDaniel Schwierzeck 29eef88dfbSDaniel Schwierzeck#ifdef CONFIG_64BIT 30eef88dfbSDaniel Schwierzeck# ifdef CONFIG_SYS_LITTLE_ENDIAN 31eef88dfbSDaniel Schwierzeck# define MIPS64_R_INFO(ssym, r_type3, r_type2, r_type) \ 32eef88dfbSDaniel Schwierzeck (((r_type) << 24) | ((r_type2) << 16) | ((r_type3) << 8) | (ssym)) 33eef88dfbSDaniel Schwierzeck# else 34eef88dfbSDaniel Schwierzeck# define MIPS64_R_INFO(ssym, r_type3, r_type2, r_type) \ 35eef88dfbSDaniel Schwierzeck ((r_type) | ((r_type2) << 8) | ((r_type3) << 16) | (ssym) << 24) 36eef88dfbSDaniel Schwierzeck# endif 37eef88dfbSDaniel Schwierzeck# define MIPS_RELOC MIPS64_R_INFO(0x00, 0x00, 0x12, 0x03) 38eef88dfbSDaniel Schwierzeck# define STATUS_SET ST0_KX 39eef88dfbSDaniel Schwierzeck#endif 40eef88dfbSDaniel Schwierzeck 41eef88dfbSDaniel Schwierzeck /* 42eef88dfbSDaniel Schwierzeck * For the moment disable interrupts, mark the kernel mode and 43eef88dfbSDaniel Schwierzeck * set ST0_KX so that the CPU does not spit fire when using 44eef88dfbSDaniel Schwierzeck * 64-bit addresses. 45eef88dfbSDaniel Schwierzeck */ 46eef88dfbSDaniel Schwierzeck .macro setup_c0_status set clr 47eef88dfbSDaniel Schwierzeck .set push 48eef88dfbSDaniel Schwierzeck mfc0 t0, CP0_STATUS 49eef88dfbSDaniel Schwierzeck or t0, ST0_CU0 | \set | 0x1f | \clr 50eef88dfbSDaniel Schwierzeck xor t0, 0x1f | \clr 51eef88dfbSDaniel Schwierzeck mtc0 t0, CP0_STATUS 52eef88dfbSDaniel Schwierzeck .set noreorder 53eef88dfbSDaniel Schwierzeck sll zero, 3 # ehb 54eef88dfbSDaniel Schwierzeck .set pop 55eef88dfbSDaniel Schwierzeck .endm 56eef88dfbSDaniel Schwierzeck 57eef88dfbSDaniel Schwierzeck .set noreorder 58eef88dfbSDaniel Schwierzeck 5911349298SDaniel SchwierzeckENTRY(_start) 60a187559eSBin Meng /* U-Boot entry point */ 61eef88dfbSDaniel Schwierzeck b reset 62eef88dfbSDaniel Schwierzeck nop 63eef88dfbSDaniel Schwierzeck 64eef88dfbSDaniel Schwierzeck .org 0x10 65eef88dfbSDaniel Schwierzeck#if defined(CONFIG_SYS_XWAY_EBU_BOOTCFG) 66eef88dfbSDaniel Schwierzeck /* 67eef88dfbSDaniel Schwierzeck * Almost all Lantiq XWAY SoC devices have an external bus unit (EBU) to 68eef88dfbSDaniel Schwierzeck * access external NOR flashes. If the board boots from NOR flash the 69eef88dfbSDaniel Schwierzeck * internal BootROM does a blind read at address 0xB0000010 to read the 70eef88dfbSDaniel Schwierzeck * initial configuration for that EBU in order to access the flash 71eef88dfbSDaniel Schwierzeck * device with correct parameters. This config option is board-specific. 72eef88dfbSDaniel Schwierzeck */ 73eef88dfbSDaniel Schwierzeck .word CONFIG_SYS_XWAY_EBU_BOOTCFG 74eef88dfbSDaniel Schwierzeck .word 0x0 75eef88dfbSDaniel Schwierzeck#elif defined(CONFIG_MALTA) 76eef88dfbSDaniel Schwierzeck /* 77eef88dfbSDaniel Schwierzeck * Linux expects the Board ID here. 78eef88dfbSDaniel Schwierzeck */ 79eef88dfbSDaniel Schwierzeck .word 0x00000420 # 0x420 (Malta Board with CoreLV) 80eef88dfbSDaniel Schwierzeck .word 0x00000000 81eef88dfbSDaniel Schwierzeck#endif 82eef88dfbSDaniel Schwierzeck 83eef88dfbSDaniel Schwierzeck .org 0x200 84eef88dfbSDaniel Schwierzeck /* TLB refill, 32 bit task */ 85eef88dfbSDaniel Schwierzeck1: b 1b 86eef88dfbSDaniel Schwierzeck nop 87eef88dfbSDaniel Schwierzeck 88eef88dfbSDaniel Schwierzeck .org 0x280 89eef88dfbSDaniel Schwierzeck /* XTLB refill, 64 bit task */ 90eef88dfbSDaniel Schwierzeck1: b 1b 91eef88dfbSDaniel Schwierzeck nop 92eef88dfbSDaniel Schwierzeck 93eef88dfbSDaniel Schwierzeck .org 0x300 94eef88dfbSDaniel Schwierzeck /* Cache error exception */ 95eef88dfbSDaniel Schwierzeck1: b 1b 96eef88dfbSDaniel Schwierzeck nop 97eef88dfbSDaniel Schwierzeck 98eef88dfbSDaniel Schwierzeck .org 0x380 99eef88dfbSDaniel Schwierzeck /* General exception */ 100eef88dfbSDaniel Schwierzeck1: b 1b 101eef88dfbSDaniel Schwierzeck nop 102eef88dfbSDaniel Schwierzeck 103eef88dfbSDaniel Schwierzeck .org 0x400 104eef88dfbSDaniel Schwierzeck /* Catch interrupt exceptions */ 105eef88dfbSDaniel Schwierzeck1: b 1b 106eef88dfbSDaniel Schwierzeck nop 107eef88dfbSDaniel Schwierzeck 108eef88dfbSDaniel Schwierzeck .org 0x480 109eef88dfbSDaniel Schwierzeck /* EJTAG debug exception */ 110eef88dfbSDaniel Schwierzeck1: b 1b 111eef88dfbSDaniel Schwierzeck nop 112eef88dfbSDaniel Schwierzeck 113eef88dfbSDaniel Schwierzeck .align 4 114eef88dfbSDaniel Schwierzeckreset: 115eef88dfbSDaniel Schwierzeck 116eef88dfbSDaniel Schwierzeck /* Clear watch registers */ 117eef88dfbSDaniel Schwierzeck MTC0 zero, CP0_WATCHLO 118e26e8dc8SDaniel Schwierzeck mtc0 zero, CP0_WATCHHI 119eef88dfbSDaniel Schwierzeck 120eef88dfbSDaniel Schwierzeck /* WP(Watch Pending), SW0/1 should be cleared */ 121eef88dfbSDaniel Schwierzeck mtc0 zero, CP0_CAUSE 122eef88dfbSDaniel Schwierzeck 123eef88dfbSDaniel Schwierzeck setup_c0_status STATUS_SET 0 124eef88dfbSDaniel Schwierzeck 125eef88dfbSDaniel Schwierzeck /* Init Timer */ 126eef88dfbSDaniel Schwierzeck mtc0 zero, CP0_COUNT 127eef88dfbSDaniel Schwierzeck mtc0 zero, CP0_COMPARE 128eef88dfbSDaniel Schwierzeck 129eef88dfbSDaniel Schwierzeck#ifndef CONFIG_SKIP_LOWLEVEL_INIT 130eef88dfbSDaniel Schwierzeck /* CONFIG0 register */ 131eef88dfbSDaniel Schwierzeck li t0, CONF_CM_UNCACHED 132eef88dfbSDaniel Schwierzeck mtc0 t0, CP0_CONFIG 133eef88dfbSDaniel Schwierzeck#endif 134eef88dfbSDaniel Schwierzeck 135eef88dfbSDaniel Schwierzeck /* 136eef88dfbSDaniel Schwierzeck * Initialize $gp, force pointer sized alignment of bal instruction to 137eef88dfbSDaniel Schwierzeck * forbid the compiler to put nop's between bal and _gp. This is 138eef88dfbSDaniel Schwierzeck * required to keep _gp and ra aligned to 8 byte. 139eef88dfbSDaniel Schwierzeck */ 140eef88dfbSDaniel Schwierzeck .align PTRLOG 141eef88dfbSDaniel Schwierzeck bal 1f 142eef88dfbSDaniel Schwierzeck nop 143eef88dfbSDaniel Schwierzeck PTR _gp 144eef88dfbSDaniel Schwierzeck1: 145eef88dfbSDaniel Schwierzeck PTR_L gp, 0(ra) 146eef88dfbSDaniel Schwierzeck 147eef88dfbSDaniel Schwierzeck#ifndef CONFIG_SKIP_LOWLEVEL_INIT 148eef88dfbSDaniel Schwierzeck /* Initialize any external memory */ 149eef88dfbSDaniel Schwierzeck PTR_LA t9, lowlevel_init 150eef88dfbSDaniel Schwierzeck jalr t9 151eef88dfbSDaniel Schwierzeck nop 152eef88dfbSDaniel Schwierzeck 153eef88dfbSDaniel Schwierzeck /* Initialize caches... */ 154eef88dfbSDaniel Schwierzeck PTR_LA t9, mips_cache_reset 155eef88dfbSDaniel Schwierzeck jalr t9 156eef88dfbSDaniel Schwierzeck nop 157eef88dfbSDaniel Schwierzeck 158eef88dfbSDaniel Schwierzeck /* ... and enable them */ 159eef88dfbSDaniel Schwierzeck li t0, CONFIG_SYS_MIPS_CACHE_MODE 160eef88dfbSDaniel Schwierzeck mtc0 t0, CP0_CONFIG 161eef88dfbSDaniel Schwierzeck#endif 162eef88dfbSDaniel Schwierzeck 163eef88dfbSDaniel Schwierzeck /* Set up temporary stack */ 164e26e8dc8SDaniel Schwierzeck li t0, -16 165eef88dfbSDaniel Schwierzeck PTR_LI t1, CONFIG_SYS_INIT_SP_ADDR 166eef88dfbSDaniel Schwierzeck and sp, t1, t0 # force 16 byte alignment 167*9f8ac824SPaul Burton PTR_SUBU \ 168*9f8ac824SPaul Burton sp, sp, GD_SIZE # reserve space for gd 169eef88dfbSDaniel Schwierzeck and sp, sp, t0 # force 16 byte alignment 170eef88dfbSDaniel Schwierzeck move k0, sp # save gd pointer 171eef88dfbSDaniel Schwierzeck#ifdef CONFIG_SYS_MALLOC_F_LEN 172e26e8dc8SDaniel Schwierzeck li t2, CONFIG_SYS_MALLOC_F_LEN 173*9f8ac824SPaul Burton PTR_SUBU \ 174*9f8ac824SPaul Burton sp, sp, t2 # reserve space for early malloc 175eef88dfbSDaniel Schwierzeck and sp, sp, t0 # force 16 byte alignment 176eef88dfbSDaniel Schwierzeck#endif 177eef88dfbSDaniel Schwierzeck move fp, sp 178eef88dfbSDaniel Schwierzeck 179eef88dfbSDaniel Schwierzeck /* Clear gd */ 180eef88dfbSDaniel Schwierzeck move t0, k0 181eef88dfbSDaniel Schwierzeck1: 182e26e8dc8SDaniel Schwierzeck PTR_S zero, 0(t0) 183eef88dfbSDaniel Schwierzeck blt t0, t1, 1b 184*9f8ac824SPaul Burton PTR_ADDIU t0, PTRSIZE 185eef88dfbSDaniel Schwierzeck 186eef88dfbSDaniel Schwierzeck#ifdef CONFIG_SYS_MALLOC_F_LEN 187e26e8dc8SDaniel Schwierzeck PTR_S sp, GD_MALLOC_BASE(k0) # gd->malloc_base offset 188eef88dfbSDaniel Schwierzeck#endif 189e26e8dc8SDaniel Schwierzeck 190a6279099SPurna Chandra Mandal move a0, zero # a0 <-- boot_flags = 0 191eef88dfbSDaniel Schwierzeck PTR_LA t9, board_init_f 192eef88dfbSDaniel Schwierzeck jr t9 193eef88dfbSDaniel Schwierzeck move ra, zero 194eef88dfbSDaniel Schwierzeck 19511349298SDaniel Schwierzeck END(_start) 19611349298SDaniel Schwierzeck 197eef88dfbSDaniel Schwierzeck/* 198eef88dfbSDaniel Schwierzeck * void relocate_code (addr_sp, gd, addr_moni) 199eef88dfbSDaniel Schwierzeck * 200eef88dfbSDaniel Schwierzeck * This "function" does not return, instead it continues in RAM 201eef88dfbSDaniel Schwierzeck * after relocating the monitor code. 202eef88dfbSDaniel Schwierzeck * 203eef88dfbSDaniel Schwierzeck * a0 = addr_sp 204eef88dfbSDaniel Schwierzeck * a1 = gd 205eef88dfbSDaniel Schwierzeck * a2 = destination address 206eef88dfbSDaniel Schwierzeck */ 20711349298SDaniel SchwierzeckENTRY(relocate_code) 208eef88dfbSDaniel Schwierzeck move sp, a0 # set new stack pointer 209eef88dfbSDaniel Schwierzeck move fp, sp 210eef88dfbSDaniel Schwierzeck 211eef88dfbSDaniel Schwierzeck move s0, a1 # save gd in s0 212eef88dfbSDaniel Schwierzeck move s2, a2 # save destination address in s2 213eef88dfbSDaniel Schwierzeck 214eef88dfbSDaniel Schwierzeck PTR_LI t0, CONFIG_SYS_MONITOR_BASE 215eef88dfbSDaniel Schwierzeck PTR_SUB s1, s2, t0 # s1 <-- relocation offset 216eef88dfbSDaniel Schwierzeck 217eef88dfbSDaniel Schwierzeck PTR_LA t3, in_ram 218eef88dfbSDaniel Schwierzeck PTR_L t2, -(3 * PTRSIZE)(t3) # t2 <-- __image_copy_end 219eef88dfbSDaniel Schwierzeck move t1, a2 220eef88dfbSDaniel Schwierzeck 221eef88dfbSDaniel Schwierzeck PTR_ADD gp, s1 # adjust gp 222eef88dfbSDaniel Schwierzeck 223eef88dfbSDaniel Schwierzeck /* 224eef88dfbSDaniel Schwierzeck * t0 = source address 225eef88dfbSDaniel Schwierzeck * t1 = target address 226eef88dfbSDaniel Schwierzeck * t2 = source end address 227eef88dfbSDaniel Schwierzeck */ 228eef88dfbSDaniel Schwierzeck1: 229e26e8dc8SDaniel Schwierzeck PTR_L t3, 0(t0) 230e26e8dc8SDaniel Schwierzeck PTR_S t3, 0(t1) 231e26e8dc8SDaniel Schwierzeck PTR_ADDU t0, PTRSIZE 232eef88dfbSDaniel Schwierzeck blt t0, t2, 1b 233e26e8dc8SDaniel Schwierzeck PTR_ADDU t1, PTRSIZE 234eef88dfbSDaniel Schwierzeck 235eef88dfbSDaniel Schwierzeck /* If caches were enabled, we would have to flush them here. */ 236eef88dfbSDaniel Schwierzeck PTR_SUB a1, t1, s2 # a1 <-- size 237eef88dfbSDaniel Schwierzeck PTR_LA t9, flush_cache 238eef88dfbSDaniel Schwierzeck jalr t9 239eef88dfbSDaniel Schwierzeck move a0, s2 # a0 <-- destination address 240eef88dfbSDaniel Schwierzeck 241eef88dfbSDaniel Schwierzeck /* Jump to where we've relocated ourselves */ 242*9f8ac824SPaul Burton PTR_ADDIU t0, s2, in_ram - _start 243eef88dfbSDaniel Schwierzeck jr t0 244eef88dfbSDaniel Schwierzeck nop 245eef88dfbSDaniel Schwierzeck 246eef88dfbSDaniel Schwierzeck PTR __rel_dyn_end 247eef88dfbSDaniel Schwierzeck PTR __rel_dyn_start 248eef88dfbSDaniel Schwierzeck PTR __image_copy_end 249eef88dfbSDaniel Schwierzeck PTR _GLOBAL_OFFSET_TABLE_ 250eef88dfbSDaniel Schwierzeck PTR num_got_entries 251eef88dfbSDaniel Schwierzeck 252eef88dfbSDaniel Schwierzeckin_ram: 253eef88dfbSDaniel Schwierzeck /* 254eef88dfbSDaniel Schwierzeck * Now we want to update GOT. 255eef88dfbSDaniel Schwierzeck * 256eef88dfbSDaniel Schwierzeck * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object 257eef88dfbSDaniel Schwierzeck * generated by GNU ld. Skip these reserved entries from relocation. 258eef88dfbSDaniel Schwierzeck */ 259eef88dfbSDaniel Schwierzeck PTR_L t3, -(1 * PTRSIZE)(t0) # t3 <-- num_got_entries 260eef88dfbSDaniel Schwierzeck PTR_L t8, -(2 * PTRSIZE)(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_ 261eef88dfbSDaniel Schwierzeck PTR_ADD t8, s1 # t8 now holds relocated _G_O_T_ 262*9f8ac824SPaul Burton PTR_ADDIU t8, t8, 2 * PTRSIZE # skipping first two entries 263eef88dfbSDaniel Schwierzeck PTR_LI t2, 2 264eef88dfbSDaniel Schwierzeck1: 265eef88dfbSDaniel Schwierzeck PTR_L t1, 0(t8) 266eef88dfbSDaniel Schwierzeck beqz t1, 2f 267eef88dfbSDaniel Schwierzeck PTR_ADD t1, s1 268eef88dfbSDaniel Schwierzeck PTR_S t1, 0(t8) 269eef88dfbSDaniel Schwierzeck2: 270*9f8ac824SPaul Burton PTR_ADDIU t2, 1 271eef88dfbSDaniel Schwierzeck blt t2, t3, 1b 272*9f8ac824SPaul Burton PTR_ADDIU t8, PTRSIZE 273eef88dfbSDaniel Schwierzeck 274eef88dfbSDaniel Schwierzeck /* Update dynamic relocations */ 275eef88dfbSDaniel Schwierzeck PTR_L t1, -(4 * PTRSIZE)(t0) # t1 <-- __rel_dyn_start 276eef88dfbSDaniel Schwierzeck PTR_L t2, -(5 * PTRSIZE)(t0) # t2 <-- __rel_dyn_end 277eef88dfbSDaniel Schwierzeck 278eef88dfbSDaniel Schwierzeck b 2f # skip first reserved entry 279*9f8ac824SPaul Burton PTR_ADDIU t1, 2 * PTRSIZE 280eef88dfbSDaniel Schwierzeck 281eef88dfbSDaniel Schwierzeck1: 282eef88dfbSDaniel Schwierzeck lw t8, -4(t1) # t8 <-- relocation info 283eef88dfbSDaniel Schwierzeck 284eef88dfbSDaniel Schwierzeck PTR_LI t3, MIPS_RELOC 285eef88dfbSDaniel Schwierzeck bne t8, t3, 2f # skip non-MIPS_RELOC entries 286eef88dfbSDaniel Schwierzeck nop 287eef88dfbSDaniel Schwierzeck 288eef88dfbSDaniel Schwierzeck PTR_L t3, -(2 * PTRSIZE)(t1) # t3 <-- location to fix up in FLASH 289eef88dfbSDaniel Schwierzeck 290eef88dfbSDaniel Schwierzeck PTR_L t8, 0(t3) # t8 <-- original pointer 291eef88dfbSDaniel Schwierzeck PTR_ADD t8, s1 # t8 <-- adjusted pointer 292eef88dfbSDaniel Schwierzeck 293eef88dfbSDaniel Schwierzeck PTR_ADD t3, s1 # t3 <-- location to fix up in RAM 294eef88dfbSDaniel Schwierzeck PTR_S t8, 0(t3) 295eef88dfbSDaniel Schwierzeck 296eef88dfbSDaniel Schwierzeck2: 297eef88dfbSDaniel Schwierzeck blt t1, t2, 1b 298*9f8ac824SPaul Burton PTR_ADDIU t1, 2 * PTRSIZE # each rel.dyn entry is 2*PTRSIZE bytes 299eef88dfbSDaniel Schwierzeck 300eef88dfbSDaniel Schwierzeck /* 301eef88dfbSDaniel Schwierzeck * Clear BSS 302eef88dfbSDaniel Schwierzeck * 303eef88dfbSDaniel Schwierzeck * GOT is now relocated. Thus __bss_start and __bss_end can be 304eef88dfbSDaniel Schwierzeck * accessed directly via $gp. 305eef88dfbSDaniel Schwierzeck */ 306eef88dfbSDaniel Schwierzeck PTR_LA t1, __bss_start # t1 <-- __bss_start 307eef88dfbSDaniel Schwierzeck PTR_LA t2, __bss_end # t2 <-- __bss_end 308eef88dfbSDaniel Schwierzeck 309eef88dfbSDaniel Schwierzeck1: 310eef88dfbSDaniel Schwierzeck PTR_S zero, 0(t1) 311eef88dfbSDaniel Schwierzeck blt t1, t2, 1b 312*9f8ac824SPaul Burton PTR_ADDIU t1, PTRSIZE 313eef88dfbSDaniel Schwierzeck 314eef88dfbSDaniel Schwierzeck move a0, s0 # a0 <-- gd 315eef88dfbSDaniel Schwierzeck move a1, s2 316eef88dfbSDaniel Schwierzeck PTR_LA t9, board_init_r 317eef88dfbSDaniel Schwierzeck jr t9 318eef88dfbSDaniel Schwierzeck move ra, zero 319eef88dfbSDaniel Schwierzeck 32011349298SDaniel Schwierzeck END(relocate_code) 321