start.S 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 "config.h"
  26. #include "version.h"
  27. /*
  28. *************************************************************************
  29. *
  30. * Jump vector table as in table 3.1 in [1]
  31. *
  32. *************************************************************************
  33. */
  34. .globl _start
  35. _start: b reset
  36. ldr pc, _undefined_instruction
  37. ldr pc, _software_interrupt
  38. ldr pc, _prefetch_abort
  39. ldr pc, _data_abort
  40. ldr pc, _not_used
  41. ldr pc, _irq
  42. ldr pc, _fiq
  43. _undefined_instruction: .word undefined_instruction
  44. _software_interrupt: .word software_interrupt
  45. _prefetch_abort: .word prefetch_abort
  46. _data_abort: .word data_abort
  47. _not_used: .word not_used
  48. _irq: .word irq
  49. _fiq: .word fiq
  50. .balignl 16,0xdeadbeef
  51. /*
  52. *************************************************************************
  53. *
  54. * Startup Code (reset vector)
  55. *
  56. * do important init only if we don't start from memory!
  57. * relocate armboot to ram
  58. * setup stack
  59. * jump to second stage
  60. *
  61. *************************************************************************
  62. */
  63. _TEXT_BASE:
  64. .word TEXT_BASE
  65. .globl _armboot_start
  66. _armboot_start:
  67. .word _start
  68. /*
  69. * These are defined in the board-specific linker script.
  70. */
  71. .globl _bss_start
  72. _bss_start:
  73. .word __bss_start
  74. .globl _bss_end
  75. _bss_end:
  76. .word _end
  77. #ifdef CONFIG_USE_IRQ
  78. /* IRQ stack memory (calculated at run-time) */
  79. .globl IRQ_STACK_START
  80. IRQ_STACK_START:
  81. .word 0x0badc0de
  82. /* IRQ stack memory (calculated at run-time) */
  83. .globl FIQ_STACK_START
  84. FIQ_STACK_START:
  85. .word 0x0badc0de
  86. #endif
  87. /*
  88. * the actual reset code
  89. */
  90. reset:
  91. /*
  92. * set the cpu to SVC32 mode
  93. */
  94. mrs r0,cpsr
  95. bic r0,r0,#0x1f
  96. orr r0,r0,#0xd3 /* was 13 */
  97. msr cpsr,r0
  98. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  99. /* scratch stack */
  100. /**** ldr r1, =0x00204000 ****/
  101. /* Insure word alignment */
  102. /**** bic r1, r1, #3 ****/
  103. /* Init stack SYS */
  104. /**** mov sp, r1 ****/
  105. /*
  106. * This does a lot more than just set up the memory, which
  107. * is why it's called lowlevel_init
  108. */
  109. bl lowlevel_init /* in lowlevel.S */
  110. /*
  111. * Read/modify/write CP15 control register
  112. * disable MMU, enable I-Cache, select Asychronous Clocking Mode
  113. */
  114. mrc p15, 0, r0, c1, c0, 0 @ read cp15 control register (cp15 r1) in r0
  115. bic r0, r0, #0x00002300 @ clear bits 13, 9:8 (--V- --RS)
  116. bic r0, r0, #0x0000008f @ clear bits 7, 3:0 (B--- WCAM)
  117. orr r0, r0, #0x00000002 @ set bit 2 (A) Align
  118. orr r0, r0, #0x00000004 @ set bit 3 (C) D-Cache
  119. orr r0, r0, #0x00001000 @ set bit 12 (I) I-Cache
  120. orr r0, r0, #0xC0000000 @ set bits 31:30 (iA, nF)
  121. mcr p15, 0, r0, c1, c0, 0 @ write r0 in cp15 control register (cp15 r1)
  122. #endif /* CONFIG_SKIP_LOWLEVEL_INIT */
  123. /*
  124. * relocate exeception table
  125. */
  126. ldr r0, =_start
  127. ldr r1, =0x0
  128. mov r2, #16
  129. copyex:
  130. subs r2, r2, #1
  131. ldr r3, [r0], #4
  132. str r3, [r1], #4
  133. bne copyex
  134. /*
  135. * we do sys-critical inits only at reboot,
  136. * not when booting from ram!
  137. */
  138. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  139. bl cpu_init_crit
  140. #endif /* CONFIG_SKIP_LOWLEVEL_INIT */
  141. #ifndef CONFIG_SKIP_RELOCATE_UBOOT
  142. relocate: /* relocate U-Boot to RAM */
  143. adr r0, _start /* r0 <- current position of code */
  144. ldr r1, _TEXT_BASE /* test if we run from flash or RAM */
  145. cmp r0, r1 /* don't reloc during debug */
  146. beq stack_setup
  147. ldr r2, _armboot_start
  148. ldr r3, _bss_start
  149. sub r2, r3, r2 /* r2 <- size of armboot */
  150. add r2, r0, r2 /* r2 <- source end address */
  151. copy_loop:
  152. ldmia r0!, {r3-r10} /* copy from source address [r0] */
  153. stmia r1!, {r3-r10} /* copy to target address [r1] */
  154. cmp r0, r2 /* until source end addreee [r2] */
  155. ble copy_loop
  156. #endif /* CONFIG_SKIP_RELOCATE_UBOOT */
  157. /* Set up the stack */
  158. stack_setup:
  159. ldr r0, _TEXT_BASE /* upper 128 KiB: relocated uboot */
  160. sub r0, r0, #CFG_MALLOC_LEN /* malloc area */
  161. sub r0, r0, #CFG_GBL_DATA_SIZE /* bdinfo */
  162. #ifdef CONFIG_USE_IRQ
  163. sub r0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)
  164. #endif
  165. sub sp, r0, #12 /* leave 3 words for abort-stack */
  166. clear_bss:
  167. ldr r0, _bss_start /* find start of bss segment */
  168. ldr r1, _bss_end /* stop here */
  169. mov r2, #0x00000000 /* clear */
  170. clbss_l:str r2, [r0] /* clear loop... */
  171. add r0, r0, #4
  172. cmp r0, r1
  173. ble clbss_l
  174. ldr pc,_start_armboot
  175. _start_armboot: .word start_armboot
  176. /*
  177. *************************************************************************
  178. *
  179. * CPU_init_critical registers
  180. *
  181. *************************************************************************
  182. */
  183. cpu_init_crit:
  184. /* do nothing for now */
  185. mov pc, lr
  186. /*
  187. *************************************************************************
  188. *
  189. * Interrupt handling
  190. *
  191. *************************************************************************
  192. */
  193. @
  194. @ IRQ stack frame.
  195. @
  196. #define S_FRAME_SIZE 72
  197. #define S_OLD_R0 68
  198. #define S_PSR 64
  199. #define S_PC 60
  200. #define S_LR 56
  201. #define S_SP 52
  202. #define S_IP 48
  203. #define S_FP 44
  204. #define S_R10 40
  205. #define S_R9 36
  206. #define S_R8 32
  207. #define S_R7 28
  208. #define S_R6 24
  209. #define S_R5 20
  210. #define S_R4 16
  211. #define S_R3 12
  212. #define S_R2 8
  213. #define S_R1 4
  214. #define S_R0 0
  215. #define MODE_SVC 0x13
  216. #define I_BIT 0x80
  217. /*
  218. * use bad_save_user_regs for abort/prefetch/undef/swi ...
  219. * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling
  220. */
  221. .macro bad_save_user_regs
  222. sub sp, sp, #S_FRAME_SIZE
  223. stmia sp, {r0 - r12} @ Calling r0-r12
  224. add r8, sp, #S_PC
  225. ldr r2, _armboot_start
  226. sub r2, r2, #(CONFIG_STACKSIZE+CFG_MALLOC_LEN)
  227. sub r2, r2, #(CFG_GBL_DATA_SIZE+8) @ set base 2 words into abort stack
  228. ldmia r2, {r2 - r4} @ get pc, cpsr, old_r0
  229. add r0, sp, #S_FRAME_SIZE @ restore sp_SVC
  230. add r5, sp, #S_SP
  231. mov r1, lr
  232. stmia r5, {r0 - r4} @ save sp_SVC, lr_SVC, pc, cpsr, old_r
  233. mov r0, sp
  234. .endm
  235. .macro irq_save_user_regs
  236. sub sp, sp, #S_FRAME_SIZE
  237. stmia sp, {r0 - r12} @ Calling r0-r12
  238. add r8, sp, #S_PC
  239. stmdb r8, {sp, lr}^ @ Calling SP, LR
  240. str lr, [r8, #0] @ Save calling PC
  241. mrs r6, spsr
  242. str r6, [r8, #4] @ Save CPSR
  243. str r0, [r8, #8] @ Save OLD_R0
  244. mov r0, sp
  245. .endm
  246. .macro irq_restore_user_regs
  247. ldmia sp, {r0 - lr}^ @ Calling r0 - lr
  248. mov r0, r0
  249. ldr lr, [sp, #S_PC] @ Get PC
  250. add sp, sp, #S_FRAME_SIZE
  251. subs pc, lr, #4 @ return & move spsr_svc into cpsr
  252. .endm
  253. .macro get_bad_stack
  254. ldr r13, _armboot_start @ setup our mode stack
  255. sub r13, r13, #(CONFIG_STACKSIZE+CFG_MALLOC_LEN)
  256. sub r13, r13, #(CFG_GBL_DATA_SIZE+8) @ reserved a couple spots in abort stack
  257. str lr, [r13] @ save caller lr / spsr
  258. mrs lr, spsr
  259. str lr, [r13, #4]
  260. mov r13, #MODE_SVC @ prepare SVC-Mode
  261. msr spsr_c, r13
  262. mov lr, pc
  263. movs pc, lr
  264. .endm
  265. .macro get_irq_stack @ setup IRQ stack
  266. ldr sp, IRQ_STACK_START
  267. .endm
  268. .macro get_fiq_stack @ setup FIQ stack
  269. ldr sp, FIQ_STACK_START
  270. .endm
  271. /*
  272. * exception handlers
  273. */
  274. .align 5
  275. undefined_instruction:
  276. get_bad_stack
  277. bad_save_user_regs
  278. bl do_undefined_instruction
  279. .align 5
  280. software_interrupt:
  281. get_bad_stack
  282. bad_save_user_regs
  283. bl do_software_interrupt
  284. .align 5
  285. prefetch_abort:
  286. get_bad_stack
  287. bad_save_user_regs
  288. bl do_prefetch_abort
  289. .align 5
  290. data_abort:
  291. get_bad_stack
  292. bad_save_user_regs
  293. bl do_data_abort
  294. .align 5
  295. not_used:
  296. get_bad_stack
  297. bad_save_user_regs
  298. bl do_not_used
  299. #ifdef CONFIG_USE_IRQ
  300. .align 5
  301. irq:
  302. get_irq_stack
  303. irq_save_user_regs
  304. bl do_irq
  305. irq_restore_user_regs
  306. .align 5
  307. fiq:
  308. get_fiq_stack
  309. /* someone ought to write a more effiction fiq_save_user_regs */
  310. irq_save_user_regs
  311. bl do_fiq
  312. irq_restore_user_regs
  313. #else
  314. .align 5
  315. irq:
  316. get_bad_stack
  317. bad_save_user_regs
  318. bl do_irq
  319. .align 5
  320. fiq:
  321. get_bad_stack
  322. bad_save_user_regs
  323. bl do_fiq
  324. #endif
  325. .align 5
  326. .globl reset_cpu
  327. reset_cpu:
  328. mov pc, r0