start.S 9.8 KB

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