start.S 12 KB

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