start.S 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. .set noreorder
  33. .macro init_wr sel
  34. MTC0 zero, CP0_WATCHLO,\sel
  35. mtc0 t1, CP0_WATCHHI,\sel
  36. mfc0 t0, CP0_WATCHHI,\sel
  37. bgez t0, wr_done
  38. nop
  39. .endm
  40. .macro uhi_mips_exception
  41. move k0, t9 # preserve t9 in k0
  42. move k1, a0 # preserve a0 in k1
  43. li t9, 15 # UHI exception operation
  44. li a0, 0 # Use hard register context
  45. sdbbp 1 # Invoke UHI operation
  46. .endm
  47. .macro setup_stack_gd
  48. li t0, -16
  49. PTR_LI t1, CONFIG_SYS_INIT_SP_ADDR
  50. and sp, t1, t0 # force 16 byte alignment
  51. PTR_SUBU \
  52. sp, sp, GD_SIZE # reserve space for gd
  53. and sp, sp, t0 # force 16 byte alignment
  54. move k0, sp # save gd pointer
  55. #ifdef CONFIG_SYS_MALLOC_F_LEN
  56. li t2, CONFIG_SYS_MALLOC_F_LEN
  57. PTR_SUBU \
  58. sp, sp, t2 # reserve space for early malloc
  59. and sp, sp, t0 # force 16 byte alignment
  60. #endif
  61. move fp, sp
  62. /* Clear gd */
  63. move t0, k0
  64. 1:
  65. PTR_S zero, 0(t0)
  66. blt t0, t1, 1b
  67. PTR_ADDIU t0, PTRSIZE
  68. #ifdef CONFIG_SYS_MALLOC_F_LEN
  69. PTR_S sp, GD_MALLOC_BASE(k0) # gd->malloc_base offset
  70. #endif
  71. .endm
  72. ENTRY(_start)
  73. /* U-Boot entry point */
  74. b reset
  75. mtc0 zero, CP0_COUNT # clear cp0 count for most accurate boot timing
  76. #if defined(CONFIG_SYS_XWAY_EBU_BOOTCFG)
  77. /*
  78. * Almost all Lantiq XWAY SoC devices have an external bus unit (EBU) to
  79. * access external NOR flashes. If the board boots from NOR flash the
  80. * internal BootROM does a blind read at address 0xB0000010 to read the
  81. * initial configuration for that EBU in order to access the flash
  82. * device with correct parameters. This config option is board-specific.
  83. */
  84. .org 0x10
  85. .word CONFIG_SYS_XWAY_EBU_BOOTCFG
  86. .word 0x0
  87. #endif
  88. #if defined(CONFIG_MALTA)
  89. /*
  90. * Linux expects the Board ID here.
  91. */
  92. .org 0x10
  93. .word 0x00000420 # 0x420 (Malta Board with CoreLV)
  94. .word 0x00000000
  95. #endif
  96. #if defined(CONFIG_ROM_EXCEPTION_VECTORS)
  97. /*
  98. * Exception vector entry points. When running from ROM, an exception
  99. * cannot be handled. Halt execution and transfer control to debugger,
  100. * if one is attached.
  101. */
  102. .org 0x200
  103. /* TLB refill, 32 bit task */
  104. uhi_mips_exception
  105. .org 0x280
  106. /* XTLB refill, 64 bit task */
  107. uhi_mips_exception
  108. .org 0x300
  109. /* Cache error exception */
  110. uhi_mips_exception
  111. .org 0x380
  112. /* General exception */
  113. uhi_mips_exception
  114. .org 0x400
  115. /* Catch interrupt exceptions */
  116. uhi_mips_exception
  117. .org 0x480
  118. /* EJTAG debug exception */
  119. 1: b 1b
  120. nop
  121. .org 0x500
  122. #endif
  123. reset:
  124. #if __mips_isa_rev >= 6
  125. mfc0 t0, CP0_CONFIG, 5
  126. and t0, t0, MIPS_CONF5_VP
  127. beqz t0, 1f
  128. nop
  129. b 2f
  130. mfc0 t0, CP0_GLOBALNUMBER
  131. #endif
  132. 1: mfc0 t0, CP0_EBASE
  133. and t0, t0, EBASE_CPUNUM
  134. /* Hang if this isn't the first CPU in the system */
  135. 2: beqz t0, 4f
  136. nop
  137. 3: wait
  138. b 3b
  139. nop
  140. /* Init CP0 Status */
  141. 4: mfc0 t0, CP0_STATUS
  142. and t0, ST0_IMPL
  143. or t0, ST0_BEV | ST0_ERL | STATUS_SET
  144. mtc0 t0, CP0_STATUS
  145. /*
  146. * Check whether CP0 Config1 is implemented. If not continue
  147. * with legacy Watch register initialization.
  148. */
  149. mfc0 t0, CP0_CONFIG
  150. bgez t0, wr_legacy
  151. nop
  152. /*
  153. * Check WR bit in CP0 Config1 to determine if Watch registers
  154. * are implemented.
  155. */
  156. mfc0 t0, CP0_CONFIG, 1
  157. andi t0, (1 << 3)
  158. beqz t0, wr_done
  159. nop
  160. /* Clear Watch Status bits and disable watch exceptions */
  161. li t1, 0x7 # Clear I, R and W conditions
  162. init_wr 0
  163. init_wr 1
  164. init_wr 2
  165. init_wr 3
  166. init_wr 4
  167. init_wr 5
  168. init_wr 6
  169. init_wr 7
  170. b wr_done
  171. nop
  172. wr_legacy:
  173. MTC0 zero, CP0_WATCHLO
  174. mtc0 zero, CP0_WATCHHI
  175. wr_done:
  176. /* Clear WP, IV and SW interrupts */
  177. mtc0 zero, CP0_CAUSE
  178. /* Clear timer interrupt (CP0_COUNT cleared on branch to 'reset') */
  179. mtc0 zero, CP0_COMPARE
  180. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  181. mfc0 t0, CP0_CONFIG
  182. and t0, t0, MIPS_CONF_IMPL
  183. or t0, t0, CONF_CM_UNCACHED
  184. mtc0 t0, CP0_CONFIG
  185. ehb
  186. #endif
  187. /*
  188. * Initialize $gp, force pointer sized alignment of bal instruction to
  189. * forbid the compiler to put nop's between bal and _gp. This is
  190. * required to keep _gp and ra aligned to 8 byte.
  191. */
  192. .align PTRLOG
  193. bal 1f
  194. nop
  195. PTR _gp
  196. 1:
  197. PTR_L gp, 0(ra)
  198. #ifdef CONFIG_MIPS_CM
  199. PTR_LA t9, mips_cm_map
  200. jalr t9
  201. nop
  202. #endif
  203. #ifdef CONFIG_MIPS_INIT_STACK_IN_SRAM
  204. /* Set up initial stack and global data */
  205. setup_stack_gd
  206. #endif
  207. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  208. # ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
  209. /* Initialize any external memory */
  210. PTR_LA t9, lowlevel_init
  211. jalr t9
  212. nop
  213. # endif
  214. /* Initialize caches... */
  215. PTR_LA t9, mips_cache_reset
  216. jalr t9
  217. nop
  218. # ifndef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
  219. /* Initialize any external memory */
  220. PTR_LA t9, lowlevel_init
  221. jalr t9
  222. nop
  223. # endif
  224. #endif
  225. #ifndef CONFIG_MIPS_INIT_STACK_IN_SRAM
  226. /* Set up initial stack and global data */
  227. setup_stack_gd
  228. #endif
  229. move a0, zero # a0 <-- boot_flags = 0
  230. PTR_LA t9, board_init_f
  231. jr t9
  232. move ra, zero
  233. END(_start)
  234. /*
  235. * void relocate_code (addr_sp, gd, addr_moni)
  236. *
  237. * This "function" does not return, instead it continues in RAM
  238. * after relocating the monitor code.
  239. *
  240. * a0 = addr_sp
  241. * a1 = gd
  242. * a2 = destination address
  243. */
  244. ENTRY(relocate_code)
  245. move sp, a0 # set new stack pointer
  246. move fp, sp
  247. move s0, a1 # save gd in s0
  248. move s2, a2 # save destination address in s2
  249. PTR_LI t0, CONFIG_SYS_MONITOR_BASE
  250. PTR_SUB s1, s2, t0 # s1 <-- relocation offset
  251. PTR_LA t2, __image_copy_end
  252. move t1, a2
  253. /*
  254. * t0 = source address
  255. * t1 = target address
  256. * t2 = source end address
  257. */
  258. 1:
  259. PTR_L t3, 0(t0)
  260. PTR_S t3, 0(t1)
  261. PTR_ADDU t0, PTRSIZE
  262. blt t0, t2, 1b
  263. PTR_ADDU t1, PTRSIZE
  264. /*
  265. * Now we want to update GOT.
  266. *
  267. * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
  268. * generated by GNU ld. Skip these reserved entries from relocation.
  269. */
  270. PTR_LA t3, num_got_entries
  271. PTR_LA t8, _GLOBAL_OFFSET_TABLE_
  272. PTR_ADD t8, s1 # t8 now holds relocated _G_O_T_
  273. PTR_ADDIU t8, t8, 2 * PTRSIZE # skipping first two entries
  274. PTR_LI t2, 2
  275. 1:
  276. PTR_L t1, 0(t8)
  277. beqz t1, 2f
  278. PTR_ADD t1, s1
  279. PTR_S t1, 0(t8)
  280. 2:
  281. PTR_ADDIU t2, 1
  282. blt t2, t3, 1b
  283. PTR_ADDIU t8, PTRSIZE
  284. /* Update dynamic relocations */
  285. PTR_LA t1, __rel_dyn_start
  286. PTR_LA t2, __rel_dyn_end
  287. b 2f # skip first reserved entry
  288. PTR_ADDIU t1, 2 * PTRSIZE
  289. 1:
  290. lw t8, -4(t1) # t8 <-- relocation info
  291. PTR_LI t3, MIPS_RELOC
  292. bne t8, t3, 2f # skip non-MIPS_RELOC entries
  293. nop
  294. PTR_L t3, -(2 * PTRSIZE)(t1) # t3 <-- location to fix up in FLASH
  295. PTR_L t8, 0(t3) # t8 <-- original pointer
  296. PTR_ADD t8, s1 # t8 <-- adjusted pointer
  297. PTR_ADD t3, s1 # t3 <-- location to fix up in RAM
  298. PTR_S t8, 0(t3)
  299. 2:
  300. blt t1, t2, 1b
  301. PTR_ADDIU t1, 2 * PTRSIZE # each rel.dyn entry is 2*PTRSIZE bytes
  302. /*
  303. * Flush caches to ensure our newly modified instructions are visible
  304. * to the instruction cache. We're still running with the old GOT, so
  305. * apply the reloc offset to the start address.
  306. */
  307. PTR_LA a0, __text_start
  308. PTR_LA a1, __text_end
  309. PTR_SUB a1, a1, a0
  310. PTR_LA t9, flush_cache
  311. jalr t9
  312. PTR_ADD a0, s1
  313. PTR_ADD gp, s1 # adjust gp
  314. /*
  315. * Clear BSS
  316. *
  317. * GOT is now relocated. Thus __bss_start and __bss_end can be
  318. * accessed directly via $gp.
  319. */
  320. PTR_LA t1, __bss_start # t1 <-- __bss_start
  321. PTR_LA t2, __bss_end # t2 <-- __bss_end
  322. 1:
  323. PTR_S zero, 0(t1)
  324. blt t1, t2, 1b
  325. PTR_ADDIU t1, PTRSIZE
  326. move a0, s0 # a0 <-- gd
  327. move a1, s2
  328. PTR_LA t9, board_init_r
  329. jr t9
  330. move ra, zero
  331. END(relocate_code)