start.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 sp, CONFIG_SYS_INIT_SP_ADDR
  114. move fp, sp
  115. dla t9, board_init_f
  116. jr t9
  117. move ra, zero
  118. /*
  119. * void relocate_code (addr_sp, gd, addr_moni)
  120. *
  121. * This "function" does not return, instead it continues in RAM
  122. * after relocating the monitor code.
  123. *
  124. * a0 = addr_sp
  125. * a1 = gd
  126. * a2 = destination address
  127. */
  128. .globl relocate_code
  129. .ent relocate_code
  130. relocate_code:
  131. move sp, a0 # set new stack pointer
  132. move fp, sp
  133. move s0, a1 # save gd in s0
  134. move s2, a2 # save destination address in s2
  135. dli t0, CONFIG_SYS_MONITOR_BASE
  136. dsub s1, s2, t0 # s1 <-- relocation offset
  137. dla t3, in_ram
  138. ld t2, -24(t3) # t2 <-- __image_copy_end
  139. move t1, a2
  140. dadd gp, s1 # adjust gp
  141. /*
  142. * t0 = source address
  143. * t1 = target address
  144. * t2 = source end address
  145. */
  146. 1:
  147. lw t3, 0(t0)
  148. sw t3, 0(t1)
  149. daddu t0, 4
  150. blt t0, t2, 1b
  151. daddu t1, 4
  152. /* If caches were enabled, we would have to flush them here. */
  153. dsub a1, t1, s2 # a1 <-- size
  154. dla t9, flush_cache
  155. jalr t9
  156. move a0, s2 # a0 <-- destination address
  157. /* Jump to where we've relocated ourselves */
  158. daddi t0, s2, in_ram - _start
  159. jr t0
  160. nop
  161. .dword __rel_dyn_end
  162. .dword __rel_dyn_start
  163. .dword __image_copy_end
  164. .dword _GLOBAL_OFFSET_TABLE_
  165. .dword num_got_entries
  166. in_ram:
  167. /*
  168. * Now we want to update GOT.
  169. *
  170. * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
  171. * generated by GNU ld. Skip these reserved entries from relocation.
  172. */
  173. ld t3, -8(t0) # t3 <-- num_got_entries
  174. ld t8, -16(t0) # t8 <-- _GLOBAL_OFFSET_TABLE_
  175. dadd t8, s1 # t8 now holds relocated _G_O_T_
  176. daddi t8, t8, 16 # skipping first two entries
  177. dli t2, 2
  178. 1:
  179. ld t1, 0(t8)
  180. beqz t1, 2f
  181. dadd t1, s1
  182. sd t1, 0(t8)
  183. 2:
  184. daddi t2, 1
  185. blt t2, t3, 1b
  186. daddi t8, 8
  187. /* Update dynamic relocations */
  188. ld t1, -32(t0) # t1 <-- __rel_dyn_start
  189. ld t2, -40(t0) # t2 <-- __rel_dyn_end
  190. b 2f # skip first reserved entry
  191. daddi t1, 16
  192. 1:
  193. lw t8, -4(t1) # t8 <-- relocation info
  194. dli t3, MIPS64_R_INFO(0x00, 0x00, 0x12, 0x03)
  195. bne t8, t3, 2f # skip non R_MIPS_REL32 entries
  196. nop
  197. ld t3, -16(t1) # t3 <-- location to fix up in FLASH
  198. ld t8, 0(t3) # t8 <-- original pointer
  199. dadd t8, s1 # t8 <-- adjusted pointer
  200. dadd t3, s1 # t3 <-- location to fix up in RAM
  201. sd t8, 0(t3)
  202. 2:
  203. blt t1, t2, 1b
  204. daddi t1, 16 # each rel.dyn entry is 16 bytes
  205. /*
  206. * Clear BSS
  207. *
  208. * GOT is now relocated. Thus __bss_start and __bss_end can be
  209. * accessed directly via $gp.
  210. */
  211. dla t1, __bss_start # t1 <-- __bss_start
  212. dla t2, __bss_end # t2 <-- __bss_end
  213. 1:
  214. sd zero, 0(t1)
  215. blt t1, t2, 1b
  216. daddi t1, 8
  217. move a0, s0 # a0 <-- gd
  218. move a1, s2
  219. dla t9, board_init_r
  220. jr t9
  221. move ra, zero
  222. .end relocate_code