start.S 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_TEXT_BASE)
  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 _image_copy_end_ofs
  94. _image_copy_end_ofs:
  95. .word __image_copy_end - _start
  96. .globl _bss_end_ofs
  97. _bss_end_ofs:
  98. .word __bss_end - _start
  99. .globl _end_ofs
  100. _end_ofs:
  101. .word _end - _start
  102. #ifdef CONFIG_USE_IRQ
  103. /* IRQ stack memory (calculated at run-time) */
  104. .globl IRQ_STACK_START
  105. IRQ_STACK_START:
  106. .word 0x0badc0de
  107. /* IRQ stack memory (calculated at run-time) */
  108. .globl FIQ_STACK_START
  109. FIQ_STACK_START:
  110. .word 0x0badc0de
  111. #endif
  112. /* IRQ stack memory (calculated at run-time) + 8 bytes */
  113. .globl IRQ_STACK_START_IN
  114. IRQ_STACK_START_IN:
  115. .word 0x0badc0de
  116. /*
  117. * the actual reset code
  118. */
  119. reset:
  120. /*
  121. * set the cpu to SVC32 mode
  122. */
  123. mrs r0,cpsr
  124. bic r0,r0,#0x1f
  125. orr r0,r0,#0xd3
  126. msr cpsr,r0
  127. /*
  128. * we do sys-critical inits only at reboot,
  129. * not when booting from ram!
  130. */
  131. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  132. bl cpu_init_crit
  133. #endif
  134. bl _main
  135. /*------------------------------------------------------------------------------*/
  136. /*
  137. * void relocate_code(addr_moni)
  138. *
  139. * This function relocates the monitor code.
  140. */
  141. .globl relocate_code
  142. relocate_code:
  143. mov r6, r0 /* save addr of destination */
  144. adr r0, _start
  145. subs r9, r6, r0 /* r9 <- relocation offset */
  146. beq relocate_done /* skip relocation */
  147. mov r1, r6 /* r1 <- scratch for copy_loop */
  148. ldr r3, _image_copy_end_ofs
  149. add r2, r0, r3 /* r2 <- source end address */
  150. copy_loop:
  151. ldmia r0!, {r10-r11} /* copy from source address [r0] */
  152. stmia r1!, {r10-r11} /* copy to target address [r1] */
  153. cmp r0, r2 /* until source end address [r2] */
  154. blo copy_loop
  155. #ifndef CONFIG_SPL_BUILD
  156. /*
  157. * fix .rel.dyn relocations
  158. */
  159. ldr r0, _TEXT_BASE /* r0 <- Text base */
  160. ldr r10, _dynsym_start_ofs /* r10 <- sym table ofs */
  161. add r10, r10, r0 /* r10 <- sym table in FLASH */
  162. ldr r2, _rel_dyn_start_ofs /* r2 <- rel dyn start ofs */
  163. add r2, r2, r0 /* r2 <- rel dyn start in FLASH */
  164. ldr r3, _rel_dyn_end_ofs /* r3 <- rel dyn end ofs */
  165. add r3, r3, r0 /* r3 <- rel dyn end in FLASH */
  166. fixloop:
  167. ldr r0, [r2] /* r0 <- location to fix up, IN FLASH! */
  168. add r0, r0, r9 /* r0 <- location to fix up in RAM */
  169. ldr r1, [r2, #4]
  170. and r7, r1, #0xff
  171. cmp r7, #23 /* relative fixup? */
  172. beq fixrel
  173. cmp r7, #2 /* absolute fixup? */
  174. beq fixabs
  175. /* ignore unknown type of fixup */
  176. b fixnext
  177. fixabs:
  178. /* absolute fix: set location to (offset) symbol value */
  179. mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */
  180. add r1, r10, r1 /* r1 <- address of symbol in table */
  181. ldr r1, [r1, #4] /* r1 <- symbol value */
  182. add r1, r1, r9 /* r1 <- relocated sym addr */
  183. b fixnext
  184. fixrel:
  185. /* relative fix: increase location by offset */
  186. ldr r1, [r0]
  187. add r1, r1, r9
  188. fixnext:
  189. str r1, [r0]
  190. add r2, r2, #8 /* each rel.dyn entry is 8 bytes */
  191. cmp r2, r3
  192. blo fixloop
  193. #endif
  194. relocate_done:
  195. mov pc, lr
  196. _rel_dyn_start_ofs:
  197. .word __rel_dyn_start - _start
  198. _rel_dyn_end_ofs:
  199. .word __rel_dyn_end - _start
  200. _dynsym_start_ofs:
  201. .word __dynsym_start - _start
  202. .globl c_runtime_cpu_setup
  203. c_runtime_cpu_setup:
  204. mov pc, lr
  205. /*
  206. *************************************************************************
  207. *
  208. * CPU_init_critical registers
  209. *
  210. * setup important registers
  211. * setup memory timing
  212. *
  213. *************************************************************************
  214. */
  215. cpu_init_crit:
  216. #if !defined(CONFIG_TEGRA)
  217. mov ip, lr
  218. /*
  219. * before relocating, we have to setup RAM timing
  220. * because memory timing is board-dependent, you will
  221. * find a lowlevel_init.S in your board directory.
  222. */
  223. bl lowlevel_init
  224. mov lr, ip
  225. #endif
  226. mov pc, lr
  227. #ifndef CONFIG_SPL_BUILD
  228. /*
  229. *************************************************************************
  230. *
  231. * Interrupt handling
  232. *
  233. *************************************************************************
  234. */
  235. @
  236. @ IRQ stack frame.
  237. @
  238. #define S_FRAME_SIZE 72
  239. #define S_OLD_R0 68
  240. #define S_PSR 64
  241. #define S_PC 60
  242. #define S_LR 56
  243. #define S_SP 52
  244. #define S_IP 48
  245. #define S_FP 44
  246. #define S_R10 40
  247. #define S_R9 36
  248. #define S_R8 32
  249. #define S_R7 28
  250. #define S_R6 24
  251. #define S_R5 20
  252. #define S_R4 16
  253. #define S_R3 12
  254. #define S_R2 8
  255. #define S_R1 4
  256. #define S_R0 0
  257. #define MODE_SVC 0x13
  258. #define I_BIT 0x80
  259. /*
  260. * use bad_save_user_regs for abort/prefetch/undef/swi ...
  261. * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling
  262. */
  263. .macro bad_save_user_regs
  264. sub sp, sp, #S_FRAME_SIZE
  265. stmia sp, {r0 - r12} @ Calling r0-r12
  266. add r8, sp, #S_PC
  267. ldr r2, IRQ_STACK_START_IN
  268. ldmia r2, {r2 - r4} @ get pc, cpsr, old_r0
  269. add r0, sp, #S_FRAME_SIZE @ restore sp_SVC
  270. add r5, sp, #S_SP
  271. mov r1, lr
  272. stmia r5, {r0 - r4} @ save sp_SVC, lr_SVC, pc, cpsr, old_r
  273. mov r0, sp
  274. .endm
  275. .macro irq_save_user_regs
  276. sub sp, sp, #S_FRAME_SIZE
  277. stmia sp, {r0 - r12} @ Calling r0-r12
  278. add r8, sp, #S_PC
  279. stmdb r8, {sp, lr}^ @ Calling SP, LR
  280. str lr, [r8, #0] @ Save calling PC
  281. mrs r6, spsr
  282. str r6, [r8, #4] @ Save CPSR
  283. str r0, [r8, #8] @ Save OLD_R0
  284. mov r0, sp
  285. .endm
  286. .macro irq_restore_user_regs
  287. ldmia sp, {r0 - lr}^ @ Calling r0 - lr
  288. mov r0, r0
  289. ldr lr, [sp, #S_PC] @ Get PC
  290. add sp, sp, #S_FRAME_SIZE
  291. subs pc, lr, #4 @ return & move spsr_svc into cpsr
  292. .endm
  293. .macro get_bad_stack
  294. ldr r13, IRQ_STACK_START_IN @ setup our mode stack
  295. str lr, [r13] @ save caller lr / spsr
  296. mrs lr, spsr
  297. str lr, [r13, #4]
  298. mov r13, #MODE_SVC @ prepare SVC-Mode
  299. msr spsr_c, r13
  300. mov lr, pc
  301. movs pc, lr
  302. .endm
  303. .macro get_irq_stack @ setup IRQ stack
  304. ldr sp, IRQ_STACK_START
  305. .endm
  306. .macro get_fiq_stack @ setup FIQ stack
  307. ldr sp, FIQ_STACK_START
  308. .endm
  309. /*
  310. * exception handlers
  311. */
  312. .align 5
  313. undefined_instruction:
  314. get_bad_stack
  315. bad_save_user_regs
  316. bl do_undefined_instruction
  317. .align 5
  318. software_interrupt:
  319. get_bad_stack
  320. bad_save_user_regs
  321. bl do_software_interrupt
  322. .align 5
  323. prefetch_abort:
  324. get_bad_stack
  325. bad_save_user_regs
  326. bl do_prefetch_abort
  327. .align 5
  328. data_abort:
  329. get_bad_stack
  330. bad_save_user_regs
  331. bl do_data_abort
  332. .align 5
  333. not_used:
  334. get_bad_stack
  335. bad_save_user_regs
  336. bl do_not_used
  337. #ifdef CONFIG_USE_IRQ
  338. .align 5
  339. irq:
  340. get_irq_stack
  341. irq_save_user_regs
  342. bl do_irq
  343. irq_restore_user_regs
  344. .align 5
  345. fiq:
  346. get_fiq_stack
  347. /* someone ought to write a more effiction fiq_save_user_regs */
  348. irq_save_user_regs
  349. bl do_fiq
  350. irq_restore_user_regs
  351. #else
  352. .align 5
  353. irq:
  354. get_bad_stack
  355. bad_save_user_regs
  356. bl do_irq
  357. .align 5
  358. fiq:
  359. get_bad_stack
  360. bad_save_user_regs
  361. bl do_fiq
  362. #endif
  363. #endif /* CONFIG_SPL_BUILD */