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) 60eef88dfbSDaniel Schwierzeck /* 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 118eef88dfbSDaniel 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 */ 164eef88dfbSDaniel Schwierzeck PTR_LI t0, -16 165eef88dfbSDaniel Schwierzeck PTR_LI t1, CONFIG_SYS_INIT_SP_ADDR 166eef88dfbSDaniel Schwierzeck and sp, t1, t0 # force 16 byte alignment 167eef88dfbSDaniel Schwierzeck PTR_SUB sp, sp, GD_SIZE # reserve space for gd 168eef88dfbSDaniel Schwierzeck and sp, sp, t0 # force 16 byte alignment 169eef88dfbSDaniel Schwierzeck move k0, sp # save gd pointer 170eef88dfbSDaniel Schwierzeck#ifdef CONFIG_SYS_MALLOC_F_LEN 171eef88dfbSDaniel Schwierzeck PTR_LI t2, CONFIG_SYS_MALLOC_F_LEN 172eef88dfbSDaniel Schwierzeck PTR_SUB sp, sp, t2 # reserve space for early malloc 173eef88dfbSDaniel Schwierzeck and sp, sp, t0 # force 16 byte alignment 174eef88dfbSDaniel Schwierzeck#endif 175eef88dfbSDaniel Schwierzeck move fp, sp 176eef88dfbSDaniel Schwierzeck 177eef88dfbSDaniel Schwierzeck /* Clear gd */ 178eef88dfbSDaniel Schwierzeck move t0, k0 179eef88dfbSDaniel Schwierzeck1: 180eef88dfbSDaniel Schwierzeck sw zero, 0(t0) 181eef88dfbSDaniel Schwierzeck blt t0, t1, 1b 182eef88dfbSDaniel Schwierzeck PTR_ADDI t0, 4 183eef88dfbSDaniel Schwierzeck 184eef88dfbSDaniel Schwierzeck#ifdef CONFIG_SYS_MALLOC_F_LEN 185eef88dfbSDaniel Schwierzeck PTR_ADDU t0, k0, GD_MALLOC_BASE # gd->malloc_base offset 186eef88dfbSDaniel Schwierzeck sw sp, 0(t0) 187eef88dfbSDaniel Schwierzeck#endif 188*a6279099SPurna Chandra Mandal move a0, zero # a0 <-- boot_flags = 0 189eef88dfbSDaniel Schwierzeck PTR_LA t9, board_init_f 190eef88dfbSDaniel Schwierzeck jr t9 191eef88dfbSDaniel Schwierzeck move ra, zero 192eef88dfbSDaniel Schwierzeck 19311349298SDaniel Schwierzeck END(_start) 19411349298SDaniel Schwierzeck 195eef88dfbSDaniel Schwierzeck/* 196eef88dfbSDaniel Schwierzeck * void relocate_code (addr_sp, gd, addr_moni) 197eef88dfbSDaniel Schwierzeck * 198eef88dfbSDaniel Schwierzeck * This "function" does not return, instead it continues in RAM 199eef88dfbSDaniel Schwierzeck * after relocating the monitor code. 200eef88dfbSDaniel Schwierzeck * 201eef88dfbSDaniel Schwierzeck * a0 = addr_sp 202eef88dfbSDaniel Schwierzeck * a1 = gd 203eef88dfbSDaniel Schwierzeck * a2 = destination address 204eef88dfbSDaniel Schwierzeck */ 20511349298SDaniel SchwierzeckENTRY(relocate_code) 206eef88dfbSDaniel Schwierzeck move sp, a0 # set new stack pointer 207eef88dfbSDaniel Schwierzeck move fp, sp 208eef88dfbSDaniel Schwierzeck 209eef88dfbSDaniel Schwierzeck move s0, a1 # save gd in s0 210eef88dfbSDaniel Schwierzeck move s2, a2 # save destination address in s2 211eef88dfbSDaniel Schwierzeck 212eef88dfbSDaniel Schwierzeck PTR_LI t0, CONFIG_SYS_MONITOR_BASE 213eef88dfbSDaniel Schwierzeck PTR_SUB s1, s2, t0 # s1 <-- relocation offset 214eef88dfbSDaniel Schwierzeck 215eef88dfbSDaniel Schwierzeck PTR_LA t3, in_ram 216eef88dfbSDaniel Schwierzeck PTR_L t2, -(3 * PTRSIZE)(t3) # t2 <-- __image_copy_end 217eef88dfbSDaniel Schwierzeck move t1, a2 218eef88dfbSDaniel Schwierzeck 219eef88dfbSDaniel Schwierzeck PTR_ADD gp, s1 # adjust gp 220eef88dfbSDaniel Schwierzeck 221eef88dfbSDaniel Schwierzeck /* 222eef88dfbSDaniel Schwierzeck * t0 = source address 223eef88dfbSDaniel Schwierzeck * t1 = target address 224eef88dfbSDaniel Schwierzeck * t2 = source end address 225eef88dfbSDaniel Schwierzeck */ 226eef88dfbSDaniel Schwierzeck1: 227eef88dfbSDaniel Schwierzeck lw t3, 0(t0) 228eef88dfbSDaniel Schwierzeck sw t3, 0(t1) 229eef88dfbSDaniel Schwierzeck PTR_ADDU t0, 4 230eef88dfbSDaniel Schwierzeck blt t0, t2, 1b 231eef88dfbSDaniel Schwierzeck PTR_ADDU t1, 4 232eef88dfbSDaniel Schwierzeck 233eef88dfbSDaniel Schwierzeck /* If caches were enabled, we would have to flush them here. */ 234eef88dfbSDaniel Schwierzeck PTR_SUB a1, t1, s2 # a1 <-- size 235eef88dfbSDaniel Schwierzeck PTR_LA t9, flush_cache 236eef88dfbSDaniel Schwierzeck jalr t9 237eef88dfbSDaniel Schwierzeck move a0, s2 # a0 <-- destination address 238eef88dfbSDaniel Schwierzeck 239eef88dfbSDaniel Schwierzeck /* Jump to where we've relocated ourselves */ 240eef88dfbSDaniel Schwierzeck PTR_ADDI t0, s2, in_ram - _start 241eef88dfbSDaniel Schwierzeck jr t0 242eef88dfbSDaniel Schwierzeck nop 243eef88dfbSDaniel Schwierzeck 244eef88dfbSDaniel Schwierzeck PTR __rel_dyn_end 245eef88dfbSDaniel Schwierzeck PTR __rel_dyn_start 246eef88dfbSDaniel Schwierzeck PTR __image_copy_end 247eef88dfbSDaniel Schwierzeck PTR _GLOBAL_OFFSET_TABLE_ 248eef88dfbSDaniel Schwierzeck PTR num_got_entries 249eef88dfbSDaniel Schwierzeck 250eef88dfbSDaniel Schwierzeckin_ram: 251eef88dfbSDaniel Schwierzeck /* 252eef88dfbSDaniel Schwierzeck * Now we want to update GOT. 253eef88dfbSDaniel Schwierzeck * 254eef88dfbSDaniel Schwierzeck * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object 255eef88dfbSDaniel Schwierzeck * generated by GNU ld. Skip these reserved entries from relocation. 256eef88dfbSDaniel Schwierzeck */ 257eef88dfbSDaniel Schwierzeck PTR_L t3, -(1 * PTRSIZE)(t0) # t3 <-- num_got_entries 258eef88dfbSDaniel Schwierzeck PTR_L t8, -(2 * PTRSIZE)(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_ 259eef88dfbSDaniel Schwierzeck PTR_ADD t8, s1 # t8 now holds relocated _G_O_T_ 260eef88dfbSDaniel Schwierzeck PTR_ADDI t8, t8, 2 * PTRSIZE # skipping first two entries 261eef88dfbSDaniel Schwierzeck PTR_LI t2, 2 262eef88dfbSDaniel Schwierzeck1: 263eef88dfbSDaniel Schwierzeck PTR_L t1, 0(t8) 264eef88dfbSDaniel Schwierzeck beqz t1, 2f 265eef88dfbSDaniel Schwierzeck PTR_ADD t1, s1 266eef88dfbSDaniel Schwierzeck PTR_S t1, 0(t8) 267eef88dfbSDaniel Schwierzeck2: 268eef88dfbSDaniel Schwierzeck PTR_ADDI t2, 1 269eef88dfbSDaniel Schwierzeck blt t2, t3, 1b 270eef88dfbSDaniel Schwierzeck PTR_ADDI t8, PTRSIZE 271eef88dfbSDaniel Schwierzeck 272eef88dfbSDaniel Schwierzeck /* Update dynamic relocations */ 273eef88dfbSDaniel Schwierzeck PTR_L t1, -(4 * PTRSIZE)(t0) # t1 <-- __rel_dyn_start 274eef88dfbSDaniel Schwierzeck PTR_L t2, -(5 * PTRSIZE)(t0) # t2 <-- __rel_dyn_end 275eef88dfbSDaniel Schwierzeck 276eef88dfbSDaniel Schwierzeck b 2f # skip first reserved entry 277eef88dfbSDaniel Schwierzeck PTR_ADDI t1, 2 * PTRSIZE 278eef88dfbSDaniel Schwierzeck 279eef88dfbSDaniel Schwierzeck1: 280eef88dfbSDaniel Schwierzeck lw t8, -4(t1) # t8 <-- relocation info 281eef88dfbSDaniel Schwierzeck 282eef88dfbSDaniel Schwierzeck PTR_LI t3, MIPS_RELOC 283eef88dfbSDaniel Schwierzeck bne t8, t3, 2f # skip non-MIPS_RELOC entries 284eef88dfbSDaniel Schwierzeck nop 285eef88dfbSDaniel Schwierzeck 286eef88dfbSDaniel Schwierzeck PTR_L t3, -(2 * PTRSIZE)(t1) # t3 <-- location to fix up in FLASH 287eef88dfbSDaniel Schwierzeck 288eef88dfbSDaniel Schwierzeck PTR_L t8, 0(t3) # t8 <-- original pointer 289eef88dfbSDaniel Schwierzeck PTR_ADD t8, s1 # t8 <-- adjusted pointer 290eef88dfbSDaniel Schwierzeck 291eef88dfbSDaniel Schwierzeck PTR_ADD t3, s1 # t3 <-- location to fix up in RAM 292eef88dfbSDaniel Schwierzeck PTR_S t8, 0(t3) 293eef88dfbSDaniel Schwierzeck 294eef88dfbSDaniel Schwierzeck2: 295eef88dfbSDaniel Schwierzeck blt t1, t2, 1b 296eef88dfbSDaniel Schwierzeck PTR_ADDI t1, 2 * PTRSIZE # each rel.dyn entry is 2*PTRSIZE bytes 297eef88dfbSDaniel Schwierzeck 298eef88dfbSDaniel Schwierzeck /* 299eef88dfbSDaniel Schwierzeck * Clear BSS 300eef88dfbSDaniel Schwierzeck * 301eef88dfbSDaniel Schwierzeck * GOT is now relocated. Thus __bss_start and __bss_end can be 302eef88dfbSDaniel Schwierzeck * accessed directly via $gp. 303eef88dfbSDaniel Schwierzeck */ 304eef88dfbSDaniel Schwierzeck PTR_LA t1, __bss_start # t1 <-- __bss_start 305eef88dfbSDaniel Schwierzeck PTR_LA t2, __bss_end # t2 <-- __bss_end 306eef88dfbSDaniel Schwierzeck 307eef88dfbSDaniel Schwierzeck1: 308eef88dfbSDaniel Schwierzeck PTR_S zero, 0(t1) 309eef88dfbSDaniel Schwierzeck blt t1, t2, 1b 310eef88dfbSDaniel Schwierzeck PTR_ADDI t1, PTRSIZE 311eef88dfbSDaniel Schwierzeck 312eef88dfbSDaniel Schwierzeck move a0, s0 # a0 <-- gd 313eef88dfbSDaniel Schwierzeck move a1, s2 314eef88dfbSDaniel Schwierzeck PTR_LA t9, board_init_r 315eef88dfbSDaniel Schwierzeck jr t9 316eef88dfbSDaniel Schwierzeck move ra, zero 317eef88dfbSDaniel Schwierzeck 31811349298SDaniel Schwierzeck END(relocate_code) 319