start.S 6.5 KB

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