start.S 10.0 KB

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