start.S 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Startup Code for MIPS32 XBURST CPU-core
  3. *
  4. * Copyright (c) 2010 Xiangfu Liu <xiangfu@sharism.cc>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <config.h>
  25. #include <version.h>
  26. #include <asm/regdef.h>
  27. #include <asm/mipsregs.h>
  28. #include <asm/addrspace.h>
  29. #include <asm/cacheops.h>
  30. .set noreorder
  31. .globl _start
  32. .text
  33. _start:
  34. /* Initialize $gp */
  35. bal 1f
  36. nop
  37. .word _gp
  38. 1:
  39. lw gp, 0(ra)
  40. /* Set up temporary stack */
  41. li sp, CONFIG_SYS_SDRAM_BASE + CONFIG_SYS_INIT_SP_OFFSET
  42. la t9, board_init_f
  43. jr t9
  44. nop
  45. /*
  46. * void relocate_code (addr_sp, gd, addr_moni)
  47. *
  48. * This "function" does not return, instead it continues in RAM
  49. * after relocating the monitor code.
  50. *
  51. * a0 = addr_sp
  52. * a1 = gd
  53. * a2 = destination address
  54. */
  55. .globl relocate_code
  56. .ent relocate_code
  57. relocate_code:
  58. move sp, a0 # set new stack pointer
  59. li t0, CONFIG_SYS_MONITOR_BASE
  60. sub t6, a2, t0 # t6 <-- relocation offset
  61. la t3, in_ram
  62. lw t2, -12(t3) # t2 <-- __image_copy_end
  63. move t1, a2
  64. add gp, t6 # adjust gp
  65. /*
  66. * t0 = source address
  67. * t1 = target address
  68. * t2 = source end address
  69. */
  70. 1:
  71. lw t3, 0(t0)
  72. sw t3, 0(t1)
  73. addu t0, 4
  74. blt t0, t2, 1b
  75. addu t1, 4
  76. /* If caches were enabled, we would have to flush them here. */
  77. /* flush d-cache */
  78. li t0, KSEG0
  79. addi t1, t0, CONFIG_SYS_DCACHE_SIZE
  80. 2:
  81. cache INDEX_WRITEBACK_INV_D, 0(t0)
  82. bne t0, t1, 2b
  83. addi t0, CONFIG_SYS_CACHELINE_SIZE
  84. sync
  85. /* flush i-cache */
  86. li t0, KSEG0
  87. addi t1, t0, CONFIG_SYS_ICACHE_SIZE
  88. 3:
  89. cache INDEX_INVALIDATE_I, 0(t0)
  90. bne t0, t1, 3b
  91. addi t0, CONFIG_SYS_CACHELINE_SIZE
  92. /* Invalidate BTB */
  93. mfc0 t0, CP0_CONFIG, 7
  94. nop
  95. ori t0, 2
  96. mtc0 t0, CP0_CONFIG, 7
  97. nop
  98. /* Jump to where we've relocated ourselves */
  99. addi t0, a2, in_ram - _start
  100. jr t0
  101. nop
  102. .word __rel_dyn_end
  103. .word __rel_dyn_start
  104. .word __image_copy_end
  105. .word _GLOBAL_OFFSET_TABLE_
  106. .word num_got_entries
  107. in_ram:
  108. /*
  109. * Now we want to update GOT.
  110. *
  111. * GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
  112. * generated by GNU ld. Skip these reserved entries from relocation.
  113. */
  114. lw t3, -4(t0) # t3 <-- num_got_entries
  115. lw t4, -8(t0) # t4 <-- _GLOBAL_OFFSET_TABLE_
  116. add t4, t6 # t4 now holds relocated _G_O_T_
  117. addi t4, t4, 8 # skipping first two entries
  118. li t2, 2
  119. 1:
  120. lw t1, 0(t4)
  121. beqz t1, 2f
  122. add t1, t6
  123. sw t1, 0(t4)
  124. 2:
  125. addi t2, 1
  126. blt t2, t3, 1b
  127. addi t4, 4
  128. /* Update dynamic relocations */
  129. lw t1, -16(t0) # t1 <-- __rel_dyn_start
  130. lw t2, -20(t0) # t2 <-- __rel_dyn_end
  131. b 2f # skip first reserved entry
  132. addi t1, 8
  133. 1:
  134. lw t3, -4(t1) # t3 <-- relocation info
  135. sub t3, 3
  136. bnez t3, 2f # skip non R_MIPS_REL32 entries
  137. nop
  138. lw t3, -8(t1) # t3 <-- location to fix up in FLASH
  139. lw t4, 0(t3) # t4 <-- original pointer
  140. add t4, t6 # t4 <-- adjusted pointer
  141. add t3, t6 # t3 <-- location to fix up in RAM
  142. sw t4, 0(t3)
  143. 2:
  144. blt t1, t2, 1b
  145. addi t1, 8 # each rel.dyn entry is 8 bytes
  146. /*
  147. * Clear BSS
  148. *
  149. * GOT is now relocated. Thus __bss_start and __bss_end can be
  150. * accessed directly via $gp.
  151. */
  152. la t1, __bss_start # t1 <-- __bss_start
  153. la t2, __bss_end # t2 <-- __bss_end
  154. 1:
  155. sw zero, 0(t1)
  156. blt t1, t2, 1b
  157. addi t1, 4
  158. move a0, a1 # a0 <-- gd
  159. la t9, board_init_r
  160. jr t9
  161. move a1, a2
  162. .end relocate_code