start.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
  3. * Scott McNutt <smcnutt@psyent.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <asm-offsets.h>
  8. #include <config.h>
  9. #include <version.h>
  10. /*************************************************************************
  11. * RESTART
  12. ************************************************************************/
  13. .text
  14. .global _start
  15. _start:
  16. wrctl status, r0 /* Disable interrupts */
  17. /* ICACHE INIT -- only the icache line at the reset address
  18. * is invalidated at reset. So the init must stay within
  19. * the cache line size (8 words). If GERMS is used, we'll
  20. * just be invalidating the cache a second time. If cache
  21. * is not implemented initi behaves as nop.
  22. */
  23. ori r4, r0, %lo(CONFIG_SYS_ICACHELINE_SIZE)
  24. movhi r5, %hi(CONFIG_SYS_ICACHE_SIZE)
  25. ori r5, r5, %lo(CONFIG_SYS_ICACHE_SIZE)
  26. 0: initi r5
  27. sub r5, r5, r4
  28. bgt r5, r0, 0b
  29. br _except_end /* Skip the tramp */
  30. /* EXCEPTION TRAMPOLINE -- the following gets copied
  31. * to the exception address (below), but is otherwise at the
  32. * default exception vector offset (0x0020).
  33. */
  34. _except_start:
  35. movhi et, %hi(_exception)
  36. ori et, et, %lo(_exception)
  37. jmp et
  38. _except_end:
  39. /* INTERRUPTS -- for now, all interrupts masked and globally
  40. * disabled.
  41. */
  42. wrctl ienable, r0 /* All disabled */
  43. /* DCACHE INIT -- if dcache not implemented, initd behaves as
  44. * nop.
  45. */
  46. movhi r4, %hi(CONFIG_SYS_DCACHELINE_SIZE)
  47. ori r4, r4, %lo(CONFIG_SYS_DCACHELINE_SIZE)
  48. movhi r5, %hi(CONFIG_SYS_DCACHE_SIZE)
  49. ori r5, r5, %lo(CONFIG_SYS_DCACHE_SIZE)
  50. mov r6, r0
  51. 1: initd 0(r6)
  52. add r6, r6, r4
  53. bltu r6, r5, 1b
  54. /* RELOCATE CODE, DATA & COMMAND TABLE -- the following code
  55. * assumes code, data and the command table are all
  56. * contiguous. This lets us relocate everything as a single
  57. * block. Make sure the linker script matches this ;-)
  58. */
  59. nextpc r4
  60. _cur: movhi r5, %hi(_cur - _start)
  61. ori r5, r5, %lo(_cur - _start)
  62. sub r4, r4, r5 /* r4 <- cur _start */
  63. mov r8, r4
  64. movhi r5, %hi(_start)
  65. ori r5, r5, %lo(_start) /* r5 <- linked _start */
  66. beq r4, r5, 3f
  67. movhi r6, %hi(_edata)
  68. ori r6, r6, %lo(_edata)
  69. 2: ldwio r7, 0(r4)
  70. addi r4, r4, 4
  71. stwio r7, 0(r5)
  72. addi r5, r5, 4
  73. bne r5, r6, 2b
  74. 3:
  75. /* ZERO BSS/SBSS -- bss and sbss are assumed to be adjacent
  76. * and between __bss_start and __bss_end.
  77. */
  78. movhi r5, %hi(__bss_start)
  79. ori r5, r5, %lo(__bss_start)
  80. movhi r6, %hi(__bss_end)
  81. ori r6, r6, %lo(__bss_end)
  82. beq r5, r6, 5f
  83. 4: stwio r0, 0(r5)
  84. addi r5, r5, 4
  85. bne r5, r6, 4b
  86. 5:
  87. /* JUMP TO RELOC ADDR */
  88. movhi r4, %hi(_reloc)
  89. ori r4, r4, %lo(_reloc)
  90. jmp r4
  91. _reloc:
  92. /* COPY EXCEPTION TRAMPOLINE -- copy the tramp to the
  93. * exception address. Define CONFIG_ROM_STUBS to prevent
  94. * the copy (e.g. exception in flash or in other
  95. * softare/firmware component).
  96. */
  97. #if !defined(CONFIG_ROM_STUBS)
  98. movhi r4, %hi(_except_start)
  99. ori r4, r4, %lo(_except_start)
  100. movhi r5, %hi(_except_end)
  101. ori r5, r5, %lo(_except_end)
  102. movhi r6, %hi(CONFIG_SYS_EXCEPTION_ADDR)
  103. ori r6, r6, %lo(CONFIG_SYS_EXCEPTION_ADDR)
  104. beq r4, r6, 7f /* Skip if at proper addr */
  105. 6: ldwio r7, 0(r4)
  106. stwio r7, 0(r6)
  107. addi r4, r4, 4
  108. addi r6, r6, 4
  109. bne r4, r5, 6b
  110. 7:
  111. #endif
  112. /* STACK INIT -- zero top two words for call back chain.
  113. */
  114. movhi sp, %hi(CONFIG_SYS_INIT_SP)
  115. ori sp, sp, %lo(CONFIG_SYS_INIT_SP)
  116. addi sp, sp, -8
  117. stw r0, 0(sp)
  118. stw r0, 4(sp)
  119. mov fp, sp
  120. /*
  121. * Call board_init_f -- never returns
  122. */
  123. mov r4, r0
  124. movhi r2, %hi(board_init_f@h)
  125. ori r2, r2, %lo(board_init_f@h)
  126. callr r2
  127. /* NEVER RETURNS -- but branch to the _start just
  128. * in case ;-)
  129. */
  130. br _start
  131. /*
  132. * relocate_code -- Nios2 handles the relocation above. But
  133. * the generic board code monkeys with the heap, stack, etc.
  134. * (it makes some assumptions that may not be appropriate
  135. * for Nios). Nevertheless, we capitulate here.
  136. *
  137. * We'll call the board_init_r from here since this isn't
  138. * supposed to return.
  139. *
  140. * void relocate_code (ulong sp, gd_t *global_data,
  141. * ulong reloc_addr)
  142. * __attribute__ ((noreturn));
  143. */
  144. .text
  145. .global relocate_code
  146. relocate_code:
  147. mov sp, r4 /* Set the new sp */
  148. mov r4, r5
  149. movhi r8, %hi(board_init_r@h)
  150. ori r8, r8, %lo(board_init_r@h)
  151. callr r8
  152. ret
  153. /*
  154. * dly_clks -- Nios2 (like Nios1) doesn't have a timebase in
  155. * the core. For simple delay loops, we do our best by counting
  156. * instruction cycles.
  157. *
  158. * Instruction performance varies based on the core. For cores
  159. * with icache and static/dynamic branch prediction (II/f, II/s):
  160. *
  161. * Normal ALU (e.g. add, cmp, etc): 1 cycle
  162. * Branch (correctly predicted, taken): 2 cycles
  163. * Negative offset is predicted (II/s).
  164. *
  165. * For cores without icache and no branch prediction (II/e):
  166. *
  167. * Normal ALU (e.g. add, cmp, etc): 6 cycles
  168. * Branch (no prediction): 6 cycles
  169. *
  170. * For simplicity, if an instruction cache is implemented we
  171. * assume II/f or II/s. Otherwise, we use the II/e.
  172. *
  173. */
  174. .globl dly_clks
  175. dly_clks:
  176. #if (CONFIG_SYS_ICACHE_SIZE > 0)
  177. subi r4, r4, 3 /* 3 clocks/loop */
  178. #else
  179. subi r4, r4, 12 /* 12 clocks/loop */
  180. #endif
  181. bge r4, r0, dly_clks
  182. ret
  183. .data
  184. .globl version_string
  185. version_string:
  186. .ascii U_BOOT_VERSION_STRING, "\0"