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