start.S 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #ifdef CONFIG_ARCH_BMIPS
  133. 1: mfc0 t0, CP0_DIAGNOSTIC, 3
  134. and t0, t0, (1 << 31)
  135. #else
  136. 1: mfc0 t0, CP0_EBASE
  137. and t0, t0, EBASE_CPUNUM
  138. #endif
  139. /* Hang if this isn't the first CPU in the system */
  140. 2: beqz t0, 4f
  141. nop
  142. 3: wait
  143. b 3b
  144. nop
  145. /* Init CP0 Status */
  146. 4: mfc0 t0, CP0_STATUS
  147. and t0, ST0_IMPL
  148. or t0, ST0_BEV | ST0_ERL | STATUS_SET
  149. mtc0 t0, CP0_STATUS
  150. /*
  151. * Check whether CP0 Config1 is implemented. If not continue
  152. * with legacy Watch register initialization.
  153. */
  154. mfc0 t0, CP0_CONFIG
  155. bgez t0, wr_legacy
  156. nop
  157. /*
  158. * Check WR bit in CP0 Config1 to determine if Watch registers
  159. * are implemented.
  160. */
  161. mfc0 t0, CP0_CONFIG, 1
  162. andi t0, (1 << 3)
  163. beqz t0, wr_done
  164. nop
  165. /* Clear Watch Status bits and disable watch exceptions */
  166. li t1, 0x7 # Clear I, R and W conditions
  167. init_wr 0
  168. init_wr 1
  169. init_wr 2
  170. init_wr 3
  171. init_wr 4
  172. init_wr 5
  173. init_wr 6
  174. init_wr 7
  175. b wr_done
  176. nop
  177. wr_legacy:
  178. MTC0 zero, CP0_WATCHLO
  179. mtc0 zero, CP0_WATCHHI
  180. wr_done:
  181. /* Clear WP, IV and SW interrupts */
  182. mtc0 zero, CP0_CAUSE
  183. /* Clear timer interrupt (CP0_COUNT cleared on branch to 'reset') */
  184. mtc0 zero, CP0_COMPARE
  185. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  186. mfc0 t0, CP0_CONFIG
  187. and t0, t0, MIPS_CONF_IMPL
  188. or t0, t0, CONF_CM_UNCACHED
  189. mtc0 t0, CP0_CONFIG
  190. ehb
  191. #endif
  192. /*
  193. * Initialize $gp, force pointer sized alignment of bal instruction to
  194. * forbid the compiler to put nop's between bal and _gp. This is
  195. * required to keep _gp and ra aligned to 8 byte.
  196. */
  197. .align PTRLOG
  198. bal 1f
  199. nop
  200. PTR _gp
  201. 1:
  202. PTR_L gp, 0(ra)
  203. #ifdef CONFIG_MIPS_CM
  204. PTR_LA t9, mips_cm_map
  205. jalr t9
  206. nop
  207. #endif
  208. #ifdef CONFIG_MIPS_INIT_STACK_IN_SRAM
  209. /* Set up initial stack and global data */
  210. setup_stack_gd
  211. # ifdef CONFIG_DEBUG_UART
  212. /* Earliest point to set up debug uart */
  213. PTR_LA t9, debug_uart_init
  214. jalr t9
  215. nop
  216. # endif
  217. #endif
  218. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  219. # ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
  220. /* Initialize any external memory */
  221. PTR_LA t9, lowlevel_init
  222. jalr t9
  223. nop
  224. # endif
  225. /* Initialize caches... */
  226. PTR_LA t9, mips_cache_reset
  227. jalr t9
  228. nop
  229. # ifndef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
  230. /* Initialize any external memory */
  231. PTR_LA t9, lowlevel_init
  232. jalr t9
  233. nop
  234. # endif
  235. #endif
  236. #ifndef CONFIG_MIPS_INIT_STACK_IN_SRAM
  237. /* Set up initial stack and global data */
  238. setup_stack_gd
  239. # ifdef CONFIG_DEBUG_UART
  240. /* Earliest point to set up debug uart */
  241. PTR_LA t9, debug_uart_init
  242. jalr t9
  243. nop
  244. # endif
  245. #endif
  246. move a0, zero # a0 <-- boot_flags = 0
  247. PTR_LA t9, board_init_f
  248. jr t9
  249. move ra, zero
  250. END(_start)
  251. /*
  252. * void relocate_code (addr_sp, gd, addr_moni)
  253. *
  254. * This "function" does not return, instead it continues in RAM
  255. * after relocating the monitor code.
  256. *
  257. * a0 = addr_sp
  258. * a1 = gd
  259. * a2 = destination address
  260. */
  261. ENTRY(relocate_code)
  262. move sp, a0 # set new stack pointer
  263. move fp, sp
  264. move s0, a1 # save gd in s0
  265. move s2, a2 # save destination address in s2
  266. PTR_LI t0, CONFIG_SYS_MONITOR_BASE
  267. PTR_SUB s1, s2, t0 # s1 <-- relocation offset
  268. PTR_LA t2, __image_copy_end
  269. move t1, a2
  270. /*
  271. * t0 = source address
  272. * t1 = target address
  273. * t2 = source end address
  274. */
  275. 1:
  276. PTR_L t3, 0(t0)
  277. PTR_S t3, 0(t1)
  278. PTR_ADDU t0, PTRSIZE
  279. blt t0, t2, 1b
  280. PTR_ADDU t1, PTRSIZE
  281. /*
  282. * Now we want to update GOT.
  283. *
  284. * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
  285. * generated by GNU ld. Skip these reserved entries from relocation.
  286. */
  287. PTR_LA t3, num_got_entries
  288. PTR_LA t8, _GLOBAL_OFFSET_TABLE_
  289. PTR_ADD t8, s1 # t8 now holds relocated _G_O_T_
  290. PTR_ADDIU t8, t8, 2 * PTRSIZE # skipping first two entries
  291. PTR_LI t2, 2
  292. 1:
  293. PTR_L t1, 0(t8)
  294. beqz t1, 2f
  295. PTR_ADD t1, s1
  296. PTR_S t1, 0(t8)
  297. 2:
  298. PTR_ADDIU t2, 1
  299. blt t2, t3, 1b
  300. PTR_ADDIU t8, PTRSIZE
  301. /* Update dynamic relocations */
  302. PTR_LA t1, __rel_dyn_start
  303. PTR_LA t2, __rel_dyn_end
  304. b 2f # skip first reserved entry
  305. PTR_ADDIU t1, 2 * PTRSIZE
  306. 1:
  307. lw t8, -4(t1) # t8 <-- relocation info
  308. PTR_LI t3, MIPS_RELOC
  309. bne t8, t3, 2f # skip non-MIPS_RELOC entries
  310. nop
  311. PTR_L t3, -(2 * PTRSIZE)(t1) # t3 <-- location to fix up in FLASH
  312. PTR_L t8, 0(t3) # t8 <-- original pointer
  313. PTR_ADD t8, s1 # t8 <-- adjusted pointer
  314. PTR_ADD t3, s1 # t3 <-- location to fix up in RAM
  315. PTR_S t8, 0(t3)
  316. 2:
  317. blt t1, t2, 1b
  318. PTR_ADDIU t1, 2 * PTRSIZE # each rel.dyn entry is 2*PTRSIZE bytes
  319. /*
  320. * Flush caches to ensure our newly modified instructions are visible
  321. * to the instruction cache. We're still running with the old GOT, so
  322. * apply the reloc offset to the start address.
  323. */
  324. PTR_LA a0, __text_start
  325. PTR_LA a1, __text_end
  326. PTR_SUB a1, a1, a0
  327. PTR_LA t9, flush_cache
  328. jalr t9
  329. PTR_ADD a0, s1
  330. PTR_ADD gp, s1 # adjust gp
  331. /*
  332. * Clear BSS
  333. *
  334. * GOT is now relocated. Thus __bss_start and __bss_end can be
  335. * accessed directly via $gp.
  336. */
  337. PTR_LA t1, __bss_start # t1 <-- __bss_start
  338. PTR_LA t2, __bss_end # t2 <-- __bss_end
  339. 1:
  340. PTR_S zero, 0(t1)
  341. blt t1, t2, 1b
  342. PTR_ADDIU t1, PTRSIZE
  343. move a0, s0 # a0 <-- gd
  344. move a1, s2
  345. PTR_LA t9, board_init_r
  346. jr t9
  347. move ra, zero
  348. END(relocate_code)