start.S 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * armboot - Startup Code for ARM720 CPU-core
  3. *
  4. * Copyright (c) 2001 Marius Gröger <mag@sysgo.de>
  5. * Copyright (c) 2002 Alex Züpke <azu@sysgo.de>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <asm-offsets.h>
  26. #include <config.h>
  27. #include <version.h>
  28. #include <asm/hardware.h>
  29. /*
  30. *************************************************************************
  31. *
  32. * Jump vector table as in table 3.1 in [1]
  33. *
  34. *************************************************************************
  35. */
  36. .globl _start
  37. _start: b reset
  38. ldr pc, _undefined_instruction
  39. ldr pc, _software_interrupt
  40. ldr pc, _prefetch_abort
  41. ldr pc, _data_abort
  42. ldr pc, _not_used
  43. ldr pc, _irq
  44. ldr pc, _fiq
  45. #ifdef CONFIG_SPL_BUILD
  46. _undefined_instruction: .word _undefined_instruction
  47. _software_interrupt: .word _software_interrupt
  48. _prefetch_abort: .word _prefetch_abort
  49. _data_abort: .word _data_abort
  50. _not_used: .word _not_used
  51. _irq: .word _irq
  52. _fiq: .word _fiq
  53. _pad: .word 0x12345678 /* now 16*4=64 */
  54. #else
  55. _undefined_instruction: .word undefined_instruction
  56. _software_interrupt: .word software_interrupt
  57. _prefetch_abort: .word prefetch_abort
  58. _data_abort: .word data_abort
  59. _not_used: .word not_used
  60. _irq: .word irq
  61. _fiq: .word fiq
  62. _pad: .word 0x12345678 /* now 16*4=64 */
  63. #endif /* CONFIG_SPL_BUILD */
  64. .balignl 16,0xdeadbeef
  65. /*
  66. *************************************************************************
  67. *
  68. * Startup Code (reset vector)
  69. *
  70. * do important init only if we don't start from RAM!
  71. * relocate armboot to ram
  72. * setup stack
  73. * jump to second stage
  74. *
  75. *************************************************************************
  76. */
  77. .globl _TEXT_BASE
  78. _TEXT_BASE:
  79. #ifdef CONFIG_SPL_BUILD
  80. .word CONFIG_SPL_TEXT_BASE
  81. #else
  82. .word CONFIG_SYS_TEXT_BASE
  83. #endif
  84. /*
  85. * These are defined in the board-specific linker script.
  86. * Subtracting _start from them lets the linker put their
  87. * relative position in the executable instead of leaving
  88. * them null.
  89. */
  90. .globl _bss_start_ofs
  91. _bss_start_ofs:
  92. .word __bss_start - _start
  93. .globl _bss_end_ofs
  94. _bss_end_ofs:
  95. .word __bss_end__ - _start
  96. .globl _end_ofs
  97. _end_ofs:
  98. .word _end - _start
  99. #ifdef CONFIG_USE_IRQ
  100. /* IRQ stack memory (calculated at run-time) */
  101. .globl IRQ_STACK_START
  102. IRQ_STACK_START:
  103. .word 0x0badc0de
  104. /* IRQ stack memory (calculated at run-time) */
  105. .globl FIQ_STACK_START
  106. FIQ_STACK_START:
  107. .word 0x0badc0de
  108. #endif
  109. /* IRQ stack memory (calculated at run-time) + 8 bytes */
  110. .globl IRQ_STACK_START_IN
  111. IRQ_STACK_START_IN:
  112. .word 0x0badc0de
  113. /*
  114. * the actual reset code
  115. */
  116. reset:
  117. /*
  118. * set the cpu to SVC32 mode
  119. */
  120. mrs r0,cpsr
  121. bic r0,r0,#0x1f
  122. orr r0,r0,#0xd3
  123. msr cpsr,r0
  124. /*
  125. * we do sys-critical inits only at reboot,
  126. * not when booting from ram!
  127. */
  128. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  129. bl cpu_init_crit
  130. #endif
  131. bl _main
  132. /*------------------------------------------------------------------------------*/
  133. /*
  134. * void relocate_code (addr_sp, gd, addr_moni)
  135. *
  136. * This "function" does not return, instead it continues in RAM
  137. * after relocating the monitor code.
  138. *
  139. */
  140. .globl relocate_code
  141. relocate_code:
  142. mov r4, r0 /* save addr_sp */
  143. mov r5, r1 /* save addr of gd */
  144. mov r6, r2 /* save addr of destination */
  145. adr r0, _start
  146. cmp r0, r6
  147. moveq r9, #0 /* no relocation. relocation offset(r9) = 0 */
  148. beq relocate_done /* skip relocation */
  149. mov r1, r6 /* r1 <- scratch for copy_loop */
  150. ldr r3, _bss_start_ofs
  151. add r2, r0, r3 /* r2 <- source end address */
  152. copy_loop:
  153. ldmia r0!, {r9-r10} /* copy from source address [r0] */
  154. stmia r1!, {r9-r10} /* copy to target address [r1] */
  155. cmp r0, r2 /* until source end address [r2] */
  156. blo copy_loop
  157. #ifndef CONFIG_SPL_BUILD
  158. /*
  159. * fix .rel.dyn relocations
  160. */
  161. ldr r0, _TEXT_BASE /* r0 <- Text base */
  162. sub r9, r6, r0 /* r9 <- relocation offset */
  163. ldr r10, _dynsym_start_ofs /* r10 <- sym table ofs */
  164. add r10, r10, r0 /* r10 <- sym table in FLASH */
  165. ldr r2, _rel_dyn_start_ofs /* r2 <- rel dyn start ofs */
  166. add r2, r2, r0 /* r2 <- rel dyn start in FLASH */
  167. ldr r3, _rel_dyn_end_ofs /* r3 <- rel dyn end ofs */
  168. add r3, r3, r0 /* r3 <- rel dyn end in FLASH */
  169. fixloop:
  170. ldr r0, [r2] /* r0 <- location to fix up, IN FLASH! */
  171. add r0, r0, r9 /* r0 <- location to fix up in RAM */
  172. ldr r1, [r2, #4]
  173. and r7, r1, #0xff
  174. cmp r7, #23 /* relative fixup? */
  175. beq fixrel
  176. cmp r7, #2 /* absolute fixup? */
  177. beq fixabs
  178. /* ignore unknown type of fixup */
  179. b fixnext
  180. fixabs:
  181. /* absolute fix: set location to (offset) symbol value */
  182. mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */
  183. add r1, r10, r1 /* r1 <- address of symbol in table */
  184. ldr r1, [r1, #4] /* r1 <- symbol value */
  185. add r1, r1, r9 /* r1 <- relocated sym addr */
  186. b fixnext
  187. fixrel:
  188. /* relative fix: increase location by offset */
  189. ldr r1, [r0]
  190. add r1, r1, r9
  191. fixnext:
  192. str r1, [r0]
  193. add r2, r2, #8 /* each rel.dyn entry is 8 bytes */
  194. cmp r2, r3
  195. blo fixloop
  196. #endif
  197. relocate_done:
  198. mov pc, lr
  199. _rel_dyn_start_ofs:
  200. .word __rel_dyn_start - _start
  201. _rel_dyn_end_ofs:
  202. .word __rel_dyn_end - _start
  203. _dynsym_start_ofs:
  204. .word __dynsym_start - _start
  205. .globl c_runtime_cpu_setup
  206. c_runtime_cpu_setup:
  207. mov pc, lr
  208. /*
  209. *************************************************************************
  210. *
  211. * CPU_init_critical registers
  212. *
  213. * setup important registers
  214. * setup memory timing
  215. *
  216. *************************************************************************
  217. */
  218. cpu_init_crit:
  219. #if !defined(CONFIG_TEGRA)
  220. mov ip, lr
  221. /*
  222. * before relocating, we have to setup RAM timing
  223. * because memory timing is board-dependent, you will
  224. * find a lowlevel_init.S in your board directory.
  225. */
  226. bl lowlevel_init
  227. mov lr, ip
  228. #endif
  229. mov pc, lr
  230. #ifndef CONFIG_SPL_BUILD
  231. /*
  232. *************************************************************************
  233. *
  234. * Interrupt handling
  235. *
  236. *************************************************************************
  237. */
  238. @
  239. @ IRQ stack frame.
  240. @
  241. #define S_FRAME_SIZE 72
  242. #define S_OLD_R0 68
  243. #define S_PSR 64
  244. #define S_PC 60
  245. #define S_LR 56
  246. #define S_SP 52
  247. #define S_IP 48
  248. #define S_FP 44
  249. #define S_R10 40
  250. #define S_R9 36
  251. #define S_R8 32
  252. #define S_R7 28
  253. #define S_R6 24
  254. #define S_R5 20
  255. #define S_R4 16
  256. #define S_R3 12
  257. #define S_R2 8
  258. #define S_R1 4
  259. #define S_R0 0
  260. #define MODE_SVC 0x13
  261. #define I_BIT 0x80
  262. /*
  263. * use bad_save_user_regs for abort/prefetch/undef/swi ...
  264. * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling
  265. */
  266. .macro bad_save_user_regs
  267. sub sp, sp, #S_FRAME_SIZE
  268. stmia sp, {r0 - r12} @ Calling r0-r12
  269. add r8, sp, #S_PC
  270. ldr r2, IRQ_STACK_START_IN
  271. ldmia r2, {r2 - r4} @ get pc, cpsr, old_r0
  272. add r0, sp, #S_FRAME_SIZE @ restore sp_SVC
  273. add r5, sp, #S_SP
  274. mov r1, lr
  275. stmia r5, {r0 - r4} @ save sp_SVC, lr_SVC, pc, cpsr, old_r
  276. mov r0, sp
  277. .endm
  278. .macro irq_save_user_regs
  279. sub sp, sp, #S_FRAME_SIZE
  280. stmia sp, {r0 - r12} @ Calling r0-r12
  281. add r8, sp, #S_PC
  282. stmdb r8, {sp, lr}^ @ Calling SP, LR
  283. str lr, [r8, #0] @ Save calling PC
  284. mrs r6, spsr
  285. str r6, [r8, #4] @ Save CPSR
  286. str r0, [r8, #8] @ Save OLD_R0
  287. mov r0, sp
  288. .endm
  289. .macro irq_restore_user_regs
  290. ldmia sp, {r0 - lr}^ @ Calling r0 - lr
  291. mov r0, r0
  292. ldr lr, [sp, #S_PC] @ Get PC
  293. add sp, sp, #S_FRAME_SIZE
  294. subs pc, lr, #4 @ return & move spsr_svc into cpsr
  295. .endm
  296. .macro get_bad_stack
  297. ldr r13, IRQ_STACK_START_IN @ setup our mode stack
  298. str lr, [r13] @ save caller lr / spsr
  299. mrs lr, spsr
  300. str lr, [r13, #4]
  301. mov r13, #MODE_SVC @ prepare SVC-Mode
  302. msr spsr_c, r13
  303. mov lr, pc
  304. movs pc, lr
  305. .endm
  306. .macro get_irq_stack @ setup IRQ stack
  307. ldr sp, IRQ_STACK_START
  308. .endm
  309. .macro get_fiq_stack @ setup FIQ stack
  310. ldr sp, FIQ_STACK_START
  311. .endm
  312. /*
  313. * exception handlers
  314. */
  315. .align 5
  316. undefined_instruction:
  317. get_bad_stack
  318. bad_save_user_regs
  319. bl do_undefined_instruction
  320. .align 5
  321. software_interrupt:
  322. get_bad_stack
  323. bad_save_user_regs
  324. bl do_software_interrupt
  325. .align 5
  326. prefetch_abort:
  327. get_bad_stack
  328. bad_save_user_regs
  329. bl do_prefetch_abort
  330. .align 5
  331. data_abort:
  332. get_bad_stack
  333. bad_save_user_regs
  334. bl do_data_abort
  335. .align 5
  336. not_used:
  337. get_bad_stack
  338. bad_save_user_regs
  339. bl do_not_used
  340. #ifdef CONFIG_USE_IRQ
  341. .align 5
  342. irq:
  343. get_irq_stack
  344. irq_save_user_regs
  345. bl do_irq
  346. irq_restore_user_regs
  347. .align 5
  348. fiq:
  349. get_fiq_stack
  350. /* someone ought to write a more effiction fiq_save_user_regs */
  351. irq_save_user_regs
  352. bl do_fiq
  353. irq_restore_user_regs
  354. #else
  355. .align 5
  356. irq:
  357. get_bad_stack
  358. bad_save_user_regs
  359. bl do_irq
  360. .align 5
  361. fiq:
  362. get_bad_stack
  363. bad_save_user_regs
  364. bl do_fiq
  365. #endif
  366. #endif /* CONFIG_SPL_BUILD */