start.S 4.9 KB

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