start.S 9.9 KB

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