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