start.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * armboot - Startup Code for OMAP3530/ARM Cortex CPU-core
  3. *
  4. * Copyright (c) 2004 Texas Instruments <r-woodruff2@ti.com>
  5. *
  6. * Copyright (c) 2001 Marius Gröger <mag@sysgo.de>
  7. * Copyright (c) 2002 Alex Züpke <azu@sysgo.de>
  8. * Copyright (c) 2002 Gary Jennejohn <garyj@denx.de>
  9. * Copyright (c) 2003 Richard Woodruff <r-woodruff2@ti.com>
  10. * Copyright (c) 2003 Kshitij <kshitij@ti.com>
  11. * Copyright (c) 2006-2008 Syed Mohammed Khasim <x0khasim@ti.com>
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #include <asm-offsets.h>
  32. #include <config.h>
  33. #include <version.h>
  34. #include <asm/system.h>
  35. #include <linux/linkage.h>
  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. #ifdef CONFIG_SPL_BUILD
  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. _pad: .word 0x12345678 /* now 16*4=64 */
  54. #else
  55. _undefined_instruction: .word undefined_instruction
  56. _software_interrupt: .word software_interrupt
  57. _prefetch_abort: .word prefetch_abort
  58. _data_abort: .word data_abort
  59. _not_used: .word not_used
  60. _irq: .word irq
  61. _fiq: .word fiq
  62. _pad: .word 0x12345678 /* now 16*4=64 */
  63. #endif /* CONFIG_SPL_BUILD */
  64. .global _end_vect
  65. _end_vect:
  66. .balignl 16,0xdeadbeef
  67. /*************************************************************************
  68. *
  69. * Startup Code (reset vector)
  70. *
  71. * do important init only if we don't start from memory!
  72. * setup Memory and board specific bits prior to relocation.
  73. * relocate armboot to ram
  74. * setup stack
  75. *
  76. *************************************************************************/
  77. .globl _TEXT_BASE
  78. _TEXT_BASE:
  79. .word CONFIG_SYS_TEXT_BASE
  80. /*
  81. * These are defined in the board-specific linker script.
  82. */
  83. .globl _bss_start_ofs
  84. _bss_start_ofs:
  85. .word __bss_start - _start
  86. .global _image_copy_end_ofs
  87. _image_copy_end_ofs:
  88. .word __image_copy_end - _start
  89. .globl _bss_end_ofs
  90. _bss_end_ofs:
  91. .word __bss_end__ - _start
  92. .globl _end_ofs
  93. _end_ofs:
  94. .word _end - _start
  95. #ifdef CONFIG_USE_IRQ
  96. /* IRQ stack memory (calculated at run-time) */
  97. .globl IRQ_STACK_START
  98. IRQ_STACK_START:
  99. .word 0x0badc0de
  100. /* IRQ stack memory (calculated at run-time) */
  101. .globl FIQ_STACK_START
  102. FIQ_STACK_START:
  103. .word 0x0badc0de
  104. #endif
  105. /* IRQ stack memory (calculated at run-time) + 8 bytes */
  106. .globl IRQ_STACK_START_IN
  107. IRQ_STACK_START_IN:
  108. .word 0x0badc0de
  109. /*
  110. * the actual reset code
  111. */
  112. reset:
  113. bl save_boot_params
  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. * Setup vector:
  123. * (OMAP4 spl TEXT_BASE is not 32 byte aligned.
  124. * Continue to use ROM code vector only in OMAP4 spl)
  125. */
  126. #if !(defined(CONFIG_OMAP44XX) && defined(CONFIG_SPL_BUILD))
  127. /* Set V=0 in CP15 SCTRL register - for VBAR to point to vector */
  128. mrc p15, 0, r0, c1, c0, 0 @ Read CP15 SCTRL Register
  129. bic r0, #CR_V @ V = 0
  130. mcr p15, 0, r0, c1, c0, 0 @ Write CP15 SCTRL Register
  131. /* Set vector address in CP15 VBAR register */
  132. ldr r0, =_start
  133. mcr p15, 0, r0, c12, c0, 0 @Set VBAR
  134. #endif
  135. /* the mask ROM code should have PLL and others stable */
  136. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  137. bl cpu_init_cp15
  138. bl cpu_init_crit
  139. #endif
  140. /* Set stackpointer in internal RAM to call board_init_f */
  141. call_board_init_f:
  142. ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
  143. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  144. ldr r0,=0x00000000
  145. bl board_init_f
  146. /*------------------------------------------------------------------------------*/
  147. #ifndef CONFIG_SPL_BUILD
  148. /*
  149. * void relocate_code (addr_sp, gd, addr_moni)
  150. *
  151. * This "function" does not return, instead it continues in RAM
  152. * after relocating the monitor code.
  153. *
  154. */
  155. ENTRY(relocate_code)
  156. mov r4, r0 /* save addr_sp */
  157. mov r5, r1 /* save addr of gd */
  158. mov r6, r2 /* save addr of destination */
  159. /* Set up the stack */
  160. stack_setup:
  161. mov sp, r4
  162. adr r0, _start
  163. cmp r0, r6
  164. moveq r9, #0 /* no relocation. relocation offset(r9) = 0 */
  165. beq clear_bss /* skip relocation */
  166. mov r1, r6 /* r1 <- scratch for copy_loop */
  167. ldr r3, _image_copy_end_ofs
  168. add r2, r0, r3 /* r2 <- source end address */
  169. copy_loop:
  170. ldmia r0!, {r9-r10} /* copy from source address [r0] */
  171. stmia r1!, {r9-r10} /* copy to target address [r1] */
  172. cmp r0, r2 /* until source end address [r2] */
  173. blo copy_loop
  174. /*
  175. * fix .rel.dyn relocations
  176. */
  177. ldr r0, _TEXT_BASE /* r0 <- Text base */
  178. sub r9, r6, r0 /* r9 <- relocation offset */
  179. ldr r10, _dynsym_start_ofs /* r10 <- sym table ofs */
  180. add r10, r10, r0 /* r10 <- sym table in FLASH */
  181. ldr r2, _rel_dyn_start_ofs /* r2 <- rel dyn start ofs */
  182. add r2, r2, r0 /* r2 <- rel dyn start in FLASH */
  183. ldr r3, _rel_dyn_end_ofs /* r3 <- rel dyn end ofs */
  184. add r3, r3, r0 /* r3 <- rel dyn end in FLASH */
  185. fixloop:
  186. ldr r0, [r2] /* r0 <- location to fix up, IN FLASH! */
  187. add r0, r0, r9 /* r0 <- location to fix up in RAM */
  188. ldr r1, [r2, #4]
  189. and r7, r1, #0xff
  190. cmp r7, #23 /* relative fixup? */
  191. beq fixrel
  192. cmp r7, #2 /* absolute fixup? */
  193. beq fixabs
  194. /* ignore unknown type of fixup */
  195. b fixnext
  196. fixabs:
  197. /* absolute fix: set location to (offset) symbol value */
  198. mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */
  199. add r1, r10, r1 /* r1 <- address of symbol in table */
  200. ldr r1, [r1, #4] /* r1 <- symbol value */
  201. add r1, r1, r9 /* r1 <- relocated sym addr */
  202. b fixnext
  203. fixrel:
  204. /* relative fix: increase location by offset */
  205. ldr r1, [r0]
  206. add r1, r1, r9
  207. fixnext:
  208. str r1, [r0]
  209. add r2, r2, #8 /* each rel.dyn entry is 8 bytes */
  210. cmp r2, r3
  211. blo fixloop
  212. b clear_bss
  213. _rel_dyn_start_ofs:
  214. .word __rel_dyn_start - _start
  215. _rel_dyn_end_ofs:
  216. .word __rel_dyn_end - _start
  217. _dynsym_start_ofs:
  218. .word __dynsym_start - _start
  219. clear_bss:
  220. ldr r0, _bss_start_ofs
  221. ldr r1, _bss_end_ofs
  222. mov r4, r6 /* reloc addr */
  223. add r0, r0, r4
  224. add r1, r1, r4
  225. mov r2, #0x00000000 /* clear */
  226. clbss_l:cmp r0, r1 /* clear loop... */
  227. bhs clbss_e /* if reached end of bss, exit */
  228. str r2, [r0]
  229. add r0, r0, #4
  230. b clbss_l
  231. clbss_e:
  232. /*
  233. * We are done. Do not return, instead branch to second part of board
  234. * initialization, now running from RAM.
  235. */
  236. jump_2_ram:
  237. /*
  238. * If I-cache is enabled invalidate it
  239. */
  240. #ifndef CONFIG_SYS_ICACHE_OFF
  241. mcr p15, 0, r0, c7, c5, 0 @ invalidate icache
  242. mcr p15, 0, r0, c7, c10, 4 @ DSB
  243. mcr p15, 0, r0, c7, c5, 4 @ ISB
  244. #endif
  245. /*
  246. * Move vector table
  247. */
  248. #if !defined(CONFIG_TEGRA20)
  249. /* Set vector address in CP15 VBAR register */
  250. ldr r0, =_start
  251. add r0, r0, r9
  252. mcr p15, 0, r0, c12, c0, 0 @Set VBAR
  253. #endif /* !Tegra20 */
  254. ldr r0, _board_init_r_ofs
  255. adr r1, _start
  256. add lr, r0, r1
  257. add lr, lr, r9
  258. /* setup parameters for board_init_r */
  259. mov r0, r5 /* gd_t */
  260. mov r1, r6 /* dest_addr */
  261. /* jump to it ... */
  262. mov pc, lr
  263. _board_init_r_ofs:
  264. .word board_init_r - _start
  265. ENDPROC(relocate_code)
  266. #endif
  267. /*************************************************************************
  268. *
  269. * void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3)
  270. * __attribute__((weak));
  271. *
  272. * Stack pointer is not yet initialized at this moment
  273. * Don't save anything to stack even if compiled with -O0
  274. *
  275. *************************************************************************/
  276. ENTRY(save_boot_params)
  277. bx lr @ back to my caller
  278. ENDPROC(save_boot_params)
  279. .weak save_boot_params
  280. /*************************************************************************
  281. *
  282. * cpu_init_cp15
  283. *
  284. * Setup CP15 registers (cache, MMU, TLBs). The I-cache is turned on unless
  285. * CONFIG_SYS_ICACHE_OFF is defined.
  286. *
  287. *************************************************************************/
  288. ENTRY(cpu_init_cp15)
  289. /*
  290. * Invalidate L1 I/D
  291. */
  292. mov r0, #0 @ set up for MCR
  293. mcr p15, 0, r0, c8, c7, 0 @ invalidate TLBs
  294. mcr p15, 0, r0, c7, c5, 0 @ invalidate icache
  295. mcr p15, 0, r0, c7, c5, 6 @ invalidate BP array
  296. mcr p15, 0, r0, c7, c10, 4 @ DSB
  297. mcr p15, 0, r0, c7, c5, 4 @ ISB
  298. /*
  299. * disable MMU stuff and caches
  300. */
  301. mrc p15, 0, r0, c1, c0, 0
  302. bic r0, r0, #0x00002000 @ clear bits 13 (--V-)
  303. bic r0, r0, #0x00000007 @ clear bits 2:0 (-CAM)
  304. orr r0, r0, #0x00000002 @ set bit 1 (--A-) Align
  305. orr r0, r0, #0x00000800 @ set bit 11 (Z---) BTB
  306. #ifdef CONFIG_SYS_ICACHE_OFF
  307. bic r0, r0, #0x00001000 @ clear bit 12 (I) I-cache
  308. #else
  309. orr r0, r0, #0x00001000 @ set bit 12 (I) I-cache
  310. #endif
  311. mcr p15, 0, r0, c1, c0, 0
  312. mov pc, lr @ back to my caller
  313. ENDPROC(cpu_init_cp15)
  314. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  315. /*************************************************************************
  316. *
  317. * CPU_init_critical registers
  318. *
  319. * setup important registers
  320. * setup memory timing
  321. *
  322. *************************************************************************/
  323. ENTRY(cpu_init_crit)
  324. /*
  325. * Jump to board specific initialization...
  326. * The Mask ROM will have already initialized
  327. * basic memory. Go here to bump up clock rate and handle
  328. * wake up conditions.
  329. */
  330. mov ip, lr @ persevere link reg across call
  331. bl lowlevel_init @ go setup pll,mux,memory
  332. mov lr, ip @ restore link
  333. mov pc, lr @ back to my caller
  334. ENDPROC(cpu_init_crit)
  335. #endif
  336. #ifndef CONFIG_SPL_BUILD
  337. /*
  338. *************************************************************************
  339. *
  340. * Interrupt handling
  341. *
  342. *************************************************************************
  343. */
  344. @
  345. @ IRQ stack frame.
  346. @
  347. #define S_FRAME_SIZE 72
  348. #define S_OLD_R0 68
  349. #define S_PSR 64
  350. #define S_PC 60
  351. #define S_LR 56
  352. #define S_SP 52
  353. #define S_IP 48
  354. #define S_FP 44
  355. #define S_R10 40
  356. #define S_R9 36
  357. #define S_R8 32
  358. #define S_R7 28
  359. #define S_R6 24
  360. #define S_R5 20
  361. #define S_R4 16
  362. #define S_R3 12
  363. #define S_R2 8
  364. #define S_R1 4
  365. #define S_R0 0
  366. #define MODE_SVC 0x13
  367. #define I_BIT 0x80
  368. /*
  369. * use bad_save_user_regs for abort/prefetch/undef/swi ...
  370. * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling
  371. */
  372. .macro bad_save_user_regs
  373. sub sp, sp, #S_FRAME_SIZE @ carve out a frame on current
  374. @ user stack
  375. stmia sp, {r0 - r12} @ Save user registers (now in
  376. @ svc mode) r0-r12
  377. ldr r2, IRQ_STACK_START_IN @ set base 2 words into abort
  378. @ stack
  379. ldmia r2, {r2 - r3} @ get values for "aborted" pc
  380. @ and cpsr (into parm regs)
  381. add r0, sp, #S_FRAME_SIZE @ grab pointer to old stack
  382. add r5, sp, #S_SP
  383. mov r1, lr
  384. stmia r5, {r0 - r3} @ save sp_SVC, lr_SVC, pc, cpsr
  385. mov r0, sp @ save current stack into r0
  386. @ (param register)
  387. .endm
  388. .macro irq_save_user_regs
  389. sub sp, sp, #S_FRAME_SIZE
  390. stmia sp, {r0 - r12} @ Calling r0-r12
  391. add r8, sp, #S_PC @ !! R8 NEEDS to be saved !!
  392. @ a reserved stack spot would
  393. @ be good.
  394. stmdb r8, {sp, lr}^ @ Calling SP, LR
  395. str lr, [r8, #0] @ Save calling PC
  396. mrs r6, spsr
  397. str r6, [r8, #4] @ Save CPSR
  398. str r0, [r8, #8] @ Save OLD_R0
  399. mov r0, sp
  400. .endm
  401. .macro irq_restore_user_regs
  402. ldmia sp, {r0 - lr}^ @ Calling r0 - lr
  403. mov r0, r0
  404. ldr lr, [sp, #S_PC] @ Get PC
  405. add sp, sp, #S_FRAME_SIZE
  406. subs pc, lr, #4 @ return & move spsr_svc into
  407. @ cpsr
  408. .endm
  409. .macro get_bad_stack
  410. ldr r13, IRQ_STACK_START_IN @ setup our mode stack (enter
  411. @ in banked mode)
  412. str lr, [r13] @ save caller lr in position 0
  413. @ of saved stack
  414. mrs lr, spsr @ get the spsr
  415. str lr, [r13, #4] @ save spsr in position 1 of
  416. @ saved stack
  417. mov r13, #MODE_SVC @ prepare SVC-Mode
  418. @ msr spsr_c, r13
  419. msr spsr, r13 @ switch modes, make sure
  420. @ moves will execute
  421. mov lr, pc @ capture return pc
  422. movs pc, lr @ jump to next instruction &
  423. @ switch modes.
  424. .endm
  425. .macro get_bad_stack_swi
  426. sub r13, r13, #4 @ space on current stack for
  427. @ scratch reg.
  428. str r0, [r13] @ save R0's value.
  429. ldr r0, IRQ_STACK_START_IN @ get data regions start
  430. @ spots for abort stack
  431. str lr, [r0] @ save caller lr in position 0
  432. @ of saved stack
  433. mrs r0, spsr @ get the spsr
  434. str lr, [r0, #4] @ save spsr in position 1 of
  435. @ saved stack
  436. ldr r0, [r13] @ restore r0
  437. add r13, r13, #4 @ pop stack entry
  438. .endm
  439. .macro get_irq_stack @ setup IRQ stack
  440. ldr sp, IRQ_STACK_START
  441. .endm
  442. .macro get_fiq_stack @ setup FIQ stack
  443. ldr sp, FIQ_STACK_START
  444. .endm
  445. /*
  446. * exception handlers
  447. */
  448. .align 5
  449. undefined_instruction:
  450. get_bad_stack
  451. bad_save_user_regs
  452. bl do_undefined_instruction
  453. .align 5
  454. software_interrupt:
  455. get_bad_stack_swi
  456. bad_save_user_regs
  457. bl do_software_interrupt
  458. .align 5
  459. prefetch_abort:
  460. get_bad_stack
  461. bad_save_user_regs
  462. bl do_prefetch_abort
  463. .align 5
  464. data_abort:
  465. get_bad_stack
  466. bad_save_user_regs
  467. bl do_data_abort
  468. .align 5
  469. not_used:
  470. get_bad_stack
  471. bad_save_user_regs
  472. bl do_not_used
  473. #ifdef CONFIG_USE_IRQ
  474. .align 5
  475. irq:
  476. get_irq_stack
  477. irq_save_user_regs
  478. bl do_irq
  479. irq_restore_user_regs
  480. .align 5
  481. fiq:
  482. get_fiq_stack
  483. /* someone ought to write a more effective fiq_save_user_regs */
  484. irq_save_user_regs
  485. bl do_fiq
  486. irq_restore_user_regs
  487. #else
  488. .align 5
  489. irq:
  490. get_bad_stack
  491. bad_save_user_regs
  492. bl do_irq
  493. .align 5
  494. fiq:
  495. get_bad_stack
  496. bad_save_user_regs
  497. bl do_fiq
  498. #endif /* CONFIG_USE_IRQ */
  499. #endif /* CONFIG_SPL_BUILD */