start.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Startup Code for MIPS64 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/regdef.h>
  11. #include <asm/mipsregs.h>
  12. #ifndef CONFIG_SYS_MIPS_CACHE_MODE
  13. #define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
  14. #endif
  15. #ifndef CONFIG_SYS_INIT_SP_ADDR
  16. #define CONFIG_SYS_INIT_SP_ADDR (CONFIG_SYS_SDRAM_BASE + \
  17. CONFIG_SYS_INIT_SP_OFFSET)
  18. #endif
  19. #ifdef CONFIG_SYS_LITTLE_ENDIAN
  20. #define MIPS64_R_INFO(ssym, r_type3, r_type2, r_type) \
  21. (((r_type) << 24) | ((r_type2) << 16) | ((r_type3) << 8) | (ssym))
  22. #else
  23. #define MIPS64_R_INFO(ssym, r_type3, r_type2, r_type) \
  24. ((r_type) | ((r_type2) << 8) | ((r_type3) << 16) | (ssym) << 24)
  25. #endif
  26. /*
  27. * For the moment disable interrupts, mark the kernel mode and
  28. * set ST0_KX so that the CPU does not spit fire when using
  29. * 64-bit addresses.
  30. */
  31. .macro setup_c0_status set clr
  32. .set push
  33. mfc0 t0, CP0_STATUS
  34. or t0, ST0_CU0 | \set | 0x1f | \clr
  35. xor t0, 0x1f | \clr
  36. mtc0 t0, CP0_STATUS
  37. .set noreorder
  38. sll zero, 3 # ehb
  39. .set pop
  40. .endm
  41. .set noreorder
  42. .globl _start
  43. .text
  44. _start:
  45. /* U-boot entry point */
  46. b reset
  47. nop
  48. .org 0x200
  49. /* TLB refill, 32 bit task */
  50. 1: b 1b
  51. nop
  52. .org 0x280
  53. /* XTLB refill, 64 bit task */
  54. 1: b 1b
  55. nop
  56. .org 0x300
  57. /* Cache error exception */
  58. 1: b 1b
  59. nop
  60. .org 0x380
  61. /* General exception */
  62. 1: b 1b
  63. nop
  64. .org 0x400
  65. /* Catch interrupt exceptions */
  66. 1: b 1b
  67. nop
  68. .org 0x480
  69. /* EJTAG debug exception */
  70. 1: b 1b
  71. nop
  72. .align 4
  73. reset:
  74. /* Clear watch registers */
  75. dmtc0 zero, CP0_WATCHLO
  76. dmtc0 zero, CP0_WATCHHI
  77. /* WP(Watch Pending), SW0/1 should be cleared */
  78. mtc0 zero, CP0_CAUSE
  79. setup_c0_status ST0_KX 0
  80. /* Init Timer */
  81. mtc0 zero, CP0_COUNT
  82. mtc0 zero, CP0_COMPARE
  83. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  84. /* CONFIG0 register */
  85. dli t0, CONF_CM_UNCACHED
  86. mtc0 t0, CP0_CONFIG
  87. #endif
  88. /*
  89. * Initialize $gp, force 8 byte alignment of bal instruction to forbid
  90. * the compiler to put nop's between bal and _gp. This is required to
  91. * keep _gp and ra aligned to 8 byte.
  92. */
  93. .align 3
  94. bal 1f
  95. nop
  96. .dword _gp
  97. 1:
  98. ld gp, 0(ra)
  99. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  100. /* Initialize any external memory */
  101. dla t9, lowlevel_init
  102. jalr t9
  103. nop
  104. /* Initialize caches... */
  105. dla t9, mips_cache_reset
  106. jalr t9
  107. nop
  108. /* ... and enable them */
  109. dli t0, CONFIG_SYS_MIPS_CACHE_MODE
  110. mtc0 t0, CP0_CONFIG
  111. #endif
  112. /* Set up temporary stack */
  113. dli t0, -16
  114. dli t1, CONFIG_SYS_INIT_SP_ADDR
  115. and sp, t1, t0 # force 16 byte alignment
  116. dsub sp, sp, GD_SIZE # reserve space for gd
  117. and sp, sp, t0 # force 16 byte alignment
  118. move k0, sp # save gd pointer
  119. #ifdef CONFIG_SYS_MALLOC_F_LEN
  120. dli t2, CONFIG_SYS_MALLOC_F_LEN
  121. dsub sp, sp, t2 # reserve space for early malloc
  122. and sp, sp, t0 # force 16 byte alignment
  123. #endif
  124. move fp, sp
  125. /* Clear gd */
  126. move t0, k0
  127. 1:
  128. sw zero, 0(t0)
  129. blt t0, t1, 1b
  130. daddi t0, 4
  131. #ifdef CONFIG_SYS_MALLOC_F_LEN
  132. daddu t0, k0, GD_MALLOC_BASE # gd->malloc_base offset
  133. sw sp, 0(t0)
  134. #endif
  135. dla t9, board_init_f
  136. jr t9
  137. move ra, zero
  138. /*
  139. * void relocate_code (addr_sp, gd, addr_moni)
  140. *
  141. * This "function" does not return, instead it continues in RAM
  142. * after relocating the monitor code.
  143. *
  144. * a0 = addr_sp
  145. * a1 = gd
  146. * a2 = destination address
  147. */
  148. .globl relocate_code
  149. .ent relocate_code
  150. relocate_code:
  151. move sp, a0 # set new stack pointer
  152. move fp, sp
  153. move s0, a1 # save gd in s0
  154. move s2, a2 # save destination address in s2
  155. dli t0, CONFIG_SYS_MONITOR_BASE
  156. dsub s1, s2, t0 # s1 <-- relocation offset
  157. dla t3, in_ram
  158. ld t2, -24(t3) # t2 <-- __image_copy_end
  159. move t1, a2
  160. dadd gp, s1 # adjust gp
  161. /*
  162. * t0 = source address
  163. * t1 = target address
  164. * t2 = source end address
  165. */
  166. 1:
  167. lw t3, 0(t0)
  168. sw t3, 0(t1)
  169. daddu t0, 4
  170. blt t0, t2, 1b
  171. daddu t1, 4
  172. /* If caches were enabled, we would have to flush them here. */
  173. dsub a1, t1, s2 # a1 <-- size
  174. dla t9, flush_cache
  175. jalr t9
  176. move a0, s2 # a0 <-- destination address
  177. /* Jump to where we've relocated ourselves */
  178. daddi t0, s2, in_ram - _start
  179. jr t0
  180. nop
  181. .dword __rel_dyn_end
  182. .dword __rel_dyn_start
  183. .dword __image_copy_end
  184. .dword _GLOBAL_OFFSET_TABLE_
  185. .dword num_got_entries
  186. in_ram:
  187. /*
  188. * Now we want to update GOT.
  189. *
  190. * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
  191. * generated by GNU ld. Skip these reserved entries from relocation.
  192. */
  193. ld t3, -8(t0) # t3 <-- num_got_entries
  194. ld t8, -16(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_
  195. dadd t8, s1 # t8 now holds relocated _G_O_T_
  196. daddi t8, t8, 16 # skipping first two entries
  197. dli t2, 2
  198. 1:
  199. ld t1, 0(t8)
  200. beqz t1, 2f
  201. dadd t1, s1
  202. sd t1, 0(t8)
  203. 2:
  204. daddi t2, 1
  205. blt t2, t3, 1b
  206. daddi t8, 8
  207. /* Update dynamic relocations */
  208. ld t1, -32(t0) # t1 <-- __rel_dyn_start
  209. ld t2, -40(t0) # t2 <-- __rel_dyn_end
  210. b 2f # skip first reserved entry
  211. daddi t1, 16
  212. 1:
  213. lw t8, -4(t1) # t8 <-- relocation info
  214. dli t3, MIPS64_R_INFO(0x00, 0x00, 0x12, 0x03)
  215. bne t8, t3, 2f # skip non R_MIPS_REL32 entries
  216. nop
  217. ld t3, -16(t1) # t3 <-- location to fix up in FLASH
  218. ld t8, 0(t3) # t8 <-- original pointer
  219. dadd t8, s1 # t8 <-- adjusted pointer
  220. dadd t3, s1 # t3 <-- location to fix up in RAM
  221. sd t8, 0(t3)
  222. 2:
  223. blt t1, t2, 1b
  224. daddi t1, 16 # each rel.dyn entry is 16 bytes
  225. /*
  226. * Clear BSS
  227. *
  228. * GOT is now relocated. Thus __bss_start and __bss_end can be
  229. * accessed directly via $gp.
  230. */
  231. dla t1, __bss_start # t1 <-- __bss_start
  232. dla t2, __bss_end # t2 <-- __bss_end
  233. 1:
  234. sd zero, 0(t1)
  235. blt t1, t2, 1b
  236. daddi t1, 8
  237. move a0, s0 # a0 <-- gd
  238. move a1, s2
  239. dla t9, board_init_r
  240. jr t9
  241. move ra, zero
  242. .end relocate_code