start.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * armboot - Startup Code for ARM720 CPU-core
  3. *
  4. * Copyright (c) 2001 Marius Gröger <mag@sysgo.de>
  5. * Copyright (c) 2002 Alex Züpke <azu@sysgo.de>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <asm-offsets.h>
  26. #include <config.h>
  27. #include <version.h>
  28. #include <asm/hardware.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. #ifdef CONFIG_LPC2292
  43. .word 0xB4405F76 /* 2's complement of the checksum of the vectors */
  44. #else
  45. ldr pc, _not_used
  46. #endif
  47. ldr pc, _irq
  48. ldr pc, _fiq
  49. _undefined_instruction: .word undefined_instruction
  50. _software_interrupt: .word software_interrupt
  51. _prefetch_abort: .word prefetch_abort
  52. _data_abort: .word data_abort
  53. _not_used: .word not_used
  54. _irq: .word irq
  55. _fiq: .word fiq
  56. .balignl 16,0xdeadbeef
  57. /*
  58. *************************************************************************
  59. *
  60. * Startup Code (reset vector)
  61. *
  62. * do important init only if we don't start from RAM!
  63. * relocate armboot to ram
  64. * setup stack
  65. * jump to second stage
  66. *
  67. *************************************************************************
  68. */
  69. .globl _TEXT_BASE
  70. _TEXT_BASE:
  71. .word CONFIG_SYS_TEXT_BASE
  72. /*
  73. * These are defined in the board-specific linker script.
  74. * Subtracting _start from them lets the linker put their
  75. * relative position in the executable instead of leaving
  76. * them null.
  77. */
  78. .globl _bss_start_ofs
  79. _bss_start_ofs:
  80. .word __bss_start - _start
  81. .globl _bss_end_ofs
  82. _bss_end_ofs:
  83. .word __bss_end__ - _start
  84. .globl _end_ofs
  85. _end_ofs:
  86. .word _end - _start
  87. #ifdef CONFIG_USE_IRQ
  88. /* IRQ stack memory (calculated at run-time) */
  89. .globl IRQ_STACK_START
  90. IRQ_STACK_START:
  91. .word 0x0badc0de
  92. /* IRQ stack memory (calculated at run-time) */
  93. .globl FIQ_STACK_START
  94. FIQ_STACK_START:
  95. .word 0x0badc0de
  96. #endif
  97. /* IRQ stack memory (calculated at run-time) + 8 bytes */
  98. .globl IRQ_STACK_START_IN
  99. IRQ_STACK_START_IN:
  100. .word 0x0badc0de
  101. /*
  102. * the actual reset code
  103. */
  104. reset:
  105. /*
  106. * set the cpu to SVC32 mode
  107. */
  108. mrs r0,cpsr
  109. bic r0,r0,#0x1f
  110. orr r0,r0,#0xd3
  111. msr cpsr,r0
  112. /*
  113. * we do sys-critical inits only at reboot,
  114. * not when booting from ram!
  115. */
  116. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  117. bl cpu_init_crit
  118. #endif
  119. #ifdef CONFIG_LPC2292
  120. bl lowlevel_init
  121. #endif
  122. /* Set stackpointer in internal RAM to call board_init_f */
  123. call_board_init_f:
  124. ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
  125. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  126. ldr r0,=0x00000000
  127. bl board_init_f
  128. /*------------------------------------------------------------------------------*/
  129. /*
  130. * void relocate_code (addr_sp, gd, addr_moni)
  131. *
  132. * This "function" does not return, instead it continues in RAM
  133. * after relocating the monitor code.
  134. *
  135. */
  136. .globl relocate_code
  137. relocate_code:
  138. mov r4, r0 /* save addr_sp */
  139. mov r5, r1 /* save addr of gd */
  140. mov r6, r2 /* save addr of destination */
  141. /* Set up the stack */
  142. stack_setup:
  143. mov sp, r4
  144. adr r0, _start
  145. cmp r0, r6
  146. beq clear_bss /* skip relocation */
  147. mov r1, r6 /* r1 <- scratch for copy_loop */
  148. ldr r3, _bss_start_ofs
  149. add r2, r0, r3 /* r2 <- source end address */
  150. copy_loop:
  151. ldmia r0!, {r9-r10} /* copy from source address [r0] */
  152. stmia r1!, {r9-r10} /* copy to target address [r1] */
  153. cmp r0, r2 /* until source end address [r2] */
  154. blo copy_loop
  155. #ifndef CONFIG_SPL_BUILD
  156. /*
  157. * fix .rel.dyn relocations
  158. */
  159. ldr r0, _TEXT_BASE /* r0 <- Text base */
  160. sub r9, r6, r0 /* r9 <- relocation offset */
  161. ldr r10, _dynsym_start_ofs /* r10 <- sym table ofs */
  162. add r10, r10, r0 /* r10 <- sym table in FLASH */
  163. ldr r2, _rel_dyn_start_ofs /* r2 <- rel dyn start ofs */
  164. add r2, r2, r0 /* r2 <- rel dyn start in FLASH */
  165. ldr r3, _rel_dyn_end_ofs /* r3 <- rel dyn end ofs */
  166. add r3, r3, r0 /* r3 <- rel dyn end in FLASH */
  167. fixloop:
  168. ldr r0, [r2] /* r0 <- location to fix up, IN FLASH! */
  169. add r0, r0, r9 /* r0 <- location to fix up in RAM */
  170. ldr r1, [r2, #4]
  171. and r7, r1, #0xff
  172. cmp r7, #23 /* relative fixup? */
  173. beq fixrel
  174. cmp r7, #2 /* absolute fixup? */
  175. beq fixabs
  176. /* ignore unknown type of fixup */
  177. b fixnext
  178. fixabs:
  179. /* absolute fix: set location to (offset) symbol value */
  180. mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */
  181. add r1, r10, r1 /* r1 <- address of symbol in table */
  182. ldr r1, [r1, #4] /* r1 <- symbol value */
  183. add r1, r1, r9 /* r1 <- relocated sym addr */
  184. b fixnext
  185. fixrel:
  186. /* relative fix: increase location by offset */
  187. ldr r1, [r0]
  188. add r1, r1, r9
  189. fixnext:
  190. str r1, [r0]
  191. add r2, r2, #8 /* each rel.dyn entry is 8 bytes */
  192. cmp r2, r3
  193. blo fixloop
  194. #endif
  195. clear_bss:
  196. #ifndef CONFIG_SPL_BUILD
  197. ldr r0, _bss_start_ofs
  198. ldr r1, _bss_end_ofs
  199. mov r4, r6 /* reloc addr */
  200. add r0, r0, r4
  201. add r1, r1, r4
  202. mov r2, #0x00000000 /* clear */
  203. clbss_l:cmp r0, r1 /* clear loop... */
  204. bhs clbss_e /* if reached end of bss, exit */
  205. str r2, [r0]
  206. add r0, r0, #4
  207. b clbss_l
  208. clbss_e:
  209. bl coloured_LED_init
  210. bl red_led_on
  211. #endif
  212. /*
  213. * We are done. Do not return, instead branch to second part of board
  214. * initialization, now running from RAM.
  215. */
  216. ldr r0, _board_init_r_ofs
  217. adr r1, _start
  218. add lr, r0, r1
  219. add lr, lr, r9
  220. /* setup parameters for board_init_r */
  221. mov r0, r5 /* gd_t */
  222. mov r1, r6 /* dest_addr */
  223. /* jump to it ... */
  224. mov pc, lr
  225. _board_init_r_ofs:
  226. .word board_init_r - _start
  227. _rel_dyn_start_ofs:
  228. .word __rel_dyn_start - _start
  229. _rel_dyn_end_ofs:
  230. .word __rel_dyn_end - _start
  231. _dynsym_start_ofs:
  232. .word __dynsym_start - _start
  233. /*
  234. *************************************************************************
  235. *
  236. * CPU_init_critical registers
  237. *
  238. * setup important registers
  239. * setup memory timing
  240. *
  241. *************************************************************************
  242. */
  243. #if defined(CONFIG_LPC2292)
  244. PLLCFG_ADR: .word PLLCFG
  245. PLLFEED_ADR: .word PLLFEED
  246. PLLCON_ADR: .word PLLCON
  247. PLLSTAT_ADR: .word PLLSTAT
  248. VPBDIV_ADR: .word VPBDIV
  249. MEMMAP_ADR: .word MEMMAP
  250. #endif
  251. cpu_init_crit:
  252. #if defined(CONFIG_NETARM)
  253. /*
  254. * prior to software reset : need to set pin PORTC4 to be *HRESET
  255. */
  256. ldr r0, =NETARM_GEN_MODULE_BASE
  257. ldr r1, =(NETARM_GEN_PORT_MODE(0x10) | \
  258. NETARM_GEN_PORT_DIR(0x10))
  259. str r1, [r0, #+NETARM_GEN_PORTC]
  260. /*
  261. * software reset : see HW Ref. Guide 8.2.4 : Software Service register
  262. * for an explanation of this process
  263. */
  264. ldr r0, =NETARM_GEN_MODULE_BASE
  265. ldr r1, =NETARM_GEN_SW_SVC_RESETA
  266. str r1, [r0, #+NETARM_GEN_SOFTWARE_SERVICE]
  267. ldr r1, =NETARM_GEN_SW_SVC_RESETB
  268. str r1, [r0, #+NETARM_GEN_SOFTWARE_SERVICE]
  269. ldr r1, =NETARM_GEN_SW_SVC_RESETA
  270. str r1, [r0, #+NETARM_GEN_SOFTWARE_SERVICE]
  271. ldr r1, =NETARM_GEN_SW_SVC_RESETB
  272. str r1, [r0, #+NETARM_GEN_SOFTWARE_SERVICE]
  273. /*
  274. * setup PLL and System Config
  275. */
  276. ldr r0, =NETARM_GEN_MODULE_BASE
  277. ldr r1, =( NETARM_GEN_SYS_CFG_LENDIAN | \
  278. NETARM_GEN_SYS_CFG_BUSFULL | \
  279. NETARM_GEN_SYS_CFG_USER_EN | \
  280. NETARM_GEN_SYS_CFG_ALIGN_ABORT | \
  281. NETARM_GEN_SYS_CFG_BUSARB_INT | \
  282. NETARM_GEN_SYS_CFG_BUSMON_EN )
  283. str r1, [r0, #+NETARM_GEN_SYSTEM_CONTROL]
  284. #ifndef CONFIG_NETARM_PLL_BYPASS
  285. ldr r1, =( NETARM_GEN_PLL_CTL_PLLCNT(NETARM_PLL_COUNT_VAL) | \
  286. NETARM_GEN_PLL_CTL_POLTST_DEF | \
  287. NETARM_GEN_PLL_CTL_INDIV(1) | \
  288. NETARM_GEN_PLL_CTL_ICP_DEF | \
  289. NETARM_GEN_PLL_CTL_OUTDIV(2) )
  290. str r1, [r0, #+NETARM_GEN_PLL_CONTROL]
  291. #endif
  292. /*
  293. * mask all IRQs by clearing all bits in the INTMRs
  294. */
  295. mov r1, #0
  296. ldr r0, =NETARM_GEN_MODULE_BASE
  297. str r1, [r0, #+NETARM_GEN_INTR_ENABLE]
  298. #elif defined(CONFIG_S3C4510B)
  299. /*
  300. * Mask off all IRQ sources
  301. */
  302. ldr r1, =REG_INTMASK
  303. ldr r0, =0x3FFFFF
  304. str r0, [r1]
  305. /*
  306. * Disable Cache
  307. */
  308. ldr r0, =REG_SYSCFG
  309. ldr r1, =0x83ffffa0 /* cache-disabled */
  310. str r1, [r0]
  311. #elif defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_INTEGRATOR)
  312. /* No specific initialisation for IntegratorAP/CM720T as yet */
  313. #elif defined(CONFIG_LPC2292)
  314. /* Set-up PLL */
  315. mov r3, #0xAA
  316. mov r4, #0x55
  317. /* First disconnect and disable the PLL */
  318. ldr r0, PLLCON_ADR
  319. mov r1, #0x00
  320. str r1, [r0]
  321. ldr r0, PLLFEED_ADR /* start feed sequence */
  322. str r3, [r0]
  323. str r4, [r0] /* feed sequence done */
  324. /* Set new M and P values */
  325. ldr r0, PLLCFG_ADR
  326. mov r1, #0x23 /* M=4 and P=2 */
  327. str r1, [r0]
  328. ldr r0, PLLFEED_ADR /* start feed sequence */
  329. str r3, [r0]
  330. str r4, [r0] /* feed sequence done */
  331. /* Then enable the PLL */
  332. ldr r0, PLLCON_ADR
  333. mov r1, #0x01 /* PLL enable bit */
  334. str r1, [r0]
  335. ldr r0, PLLFEED_ADR /* start feed sequence */
  336. str r3, [r0]
  337. str r4, [r0] /* feed sequence done */
  338. /* Wait for the lock */
  339. ldr r0, PLLSTAT_ADR
  340. mov r1, #0x400 /* lock bit */
  341. lock_loop:
  342. ldr r2, [r0]
  343. and r2, r1, r2
  344. cmp r2, #0
  345. beq lock_loop
  346. /* And finally connect the PLL */
  347. ldr r0, PLLCON_ADR
  348. mov r1, #0x03 /* PLL enable bit and connect bit */
  349. str r1, [r0]
  350. ldr r0, PLLFEED_ADR /* start feed sequence */
  351. str r3, [r0]
  352. str r4, [r0] /* feed sequence done */
  353. /* Set-up VPBDIV register */
  354. ldr r0, VPBDIV_ADR
  355. mov r1, #0x01 /* VPB clock is same as process clock */
  356. str r1, [r0]
  357. #else
  358. #error No cpu_init_crit() defined for current CPU type
  359. #endif
  360. #ifdef CONFIG_ARM7_REVD
  361. /* set clock speed */
  362. /* !!! we run @ 36 MHz due to a hardware flaw in Rev. D processors */
  363. /* !!! not doing DRAM refresh properly! */
  364. ldr r0, SYSCON3
  365. ldr r1, [r0]
  366. bic r1, r1, #CLKCTL
  367. orr r1, r1, #CLKCTL_36
  368. str r1, [r0]
  369. #endif
  370. #ifndef CONFIG_LPC2292
  371. mov ip, lr
  372. /*
  373. * before relocating, we have to setup RAM timing
  374. * because memory timing is board-dependent, you will
  375. * find a lowlevel_init.S in your board directory.
  376. */
  377. bl lowlevel_init
  378. mov lr, ip
  379. #endif
  380. mov pc, lr
  381. /*
  382. *************************************************************************
  383. *
  384. * Interrupt handling
  385. *
  386. *************************************************************************
  387. */
  388. @
  389. @ IRQ stack frame.
  390. @
  391. #define S_FRAME_SIZE 72
  392. #define S_OLD_R0 68
  393. #define S_PSR 64
  394. #define S_PC 60
  395. #define S_LR 56
  396. #define S_SP 52
  397. #define S_IP 48
  398. #define S_FP 44
  399. #define S_R10 40
  400. #define S_R9 36
  401. #define S_R8 32
  402. #define S_R7 28
  403. #define S_R6 24
  404. #define S_R5 20
  405. #define S_R4 16
  406. #define S_R3 12
  407. #define S_R2 8
  408. #define S_R1 4
  409. #define S_R0 0
  410. #define MODE_SVC 0x13
  411. #define I_BIT 0x80
  412. /*
  413. * use bad_save_user_regs for abort/prefetch/undef/swi ...
  414. * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling
  415. */
  416. .macro bad_save_user_regs
  417. sub sp, sp, #S_FRAME_SIZE
  418. stmia sp, {r0 - r12} @ Calling r0-r12
  419. add r8, sp, #S_PC
  420. ldr r2, IRQ_STACK_START_IN
  421. ldmia r2, {r2 - r4} @ get pc, cpsr, old_r0
  422. add r0, sp, #S_FRAME_SIZE @ restore sp_SVC
  423. add r5, sp, #S_SP
  424. mov r1, lr
  425. stmia r5, {r0 - r4} @ save sp_SVC, lr_SVC, pc, cpsr, old_r
  426. mov r0, sp
  427. .endm
  428. .macro irq_save_user_regs
  429. sub sp, sp, #S_FRAME_SIZE
  430. stmia sp, {r0 - r12} @ Calling r0-r12
  431. add r8, sp, #S_PC
  432. stmdb r8, {sp, lr}^ @ Calling SP, LR
  433. str lr, [r8, #0] @ Save calling PC
  434. mrs r6, spsr
  435. str r6, [r8, #4] @ Save CPSR
  436. str r0, [r8, #8] @ Save OLD_R0
  437. mov r0, sp
  438. .endm
  439. .macro irq_restore_user_regs
  440. ldmia sp, {r0 - lr}^ @ Calling r0 - lr
  441. mov r0, r0
  442. ldr lr, [sp, #S_PC] @ Get PC
  443. add sp, sp, #S_FRAME_SIZE
  444. subs pc, lr, #4 @ return & move spsr_svc into cpsr
  445. .endm
  446. .macro get_bad_stack
  447. ldr r13, IRQ_STACK_START_IN @ setup our mode stack
  448. str lr, [r13] @ save caller lr / spsr
  449. mrs lr, spsr
  450. str lr, [r13, #4]
  451. mov r13, #MODE_SVC @ prepare SVC-Mode
  452. msr spsr_c, r13
  453. mov lr, pc
  454. movs pc, lr
  455. .endm
  456. .macro get_irq_stack @ setup IRQ stack
  457. ldr sp, IRQ_STACK_START
  458. .endm
  459. .macro get_fiq_stack @ setup FIQ stack
  460. ldr sp, FIQ_STACK_START
  461. .endm
  462. /*
  463. * exception handlers
  464. */
  465. .align 5
  466. undefined_instruction:
  467. get_bad_stack
  468. bad_save_user_regs
  469. bl do_undefined_instruction
  470. .align 5
  471. software_interrupt:
  472. get_bad_stack
  473. bad_save_user_regs
  474. bl do_software_interrupt
  475. .align 5
  476. prefetch_abort:
  477. get_bad_stack
  478. bad_save_user_regs
  479. bl do_prefetch_abort
  480. .align 5
  481. data_abort:
  482. get_bad_stack
  483. bad_save_user_regs
  484. bl do_data_abort
  485. .align 5
  486. not_used:
  487. get_bad_stack
  488. bad_save_user_regs
  489. bl do_not_used
  490. #ifdef CONFIG_USE_IRQ
  491. .align 5
  492. irq:
  493. get_irq_stack
  494. irq_save_user_regs
  495. bl do_irq
  496. irq_restore_user_regs
  497. .align 5
  498. fiq:
  499. get_fiq_stack
  500. /* someone ought to write a more effiction fiq_save_user_regs */
  501. irq_save_user_regs
  502. bl do_fiq
  503. irq_restore_user_regs
  504. #else
  505. .align 5
  506. irq:
  507. get_bad_stack
  508. bad_save_user_regs
  509. bl do_irq
  510. .align 5
  511. fiq:
  512. get_bad_stack
  513. bad_save_user_regs
  514. bl do_fiq
  515. #endif
  516. #if defined(CONFIG_NETARM)
  517. .align 5
  518. .globl reset_cpu
  519. reset_cpu:
  520. ldr r1, =NETARM_MEM_MODULE_BASE
  521. ldr r0, [r1, #+NETARM_MEM_CS0_BASE_ADDR]
  522. ldr r1, =0xFFFFF000
  523. and r0, r1, r0
  524. ldr r1, =(relocate-CONFIG_SYS_TEXT_BASE)
  525. add r0, r1, r0
  526. ldr r4, =NETARM_GEN_MODULE_BASE
  527. ldr r1, =NETARM_GEN_SW_SVC_RESETA
  528. str r1, [r4, #+NETARM_GEN_SOFTWARE_SERVICE]
  529. ldr r1, =NETARM_GEN_SW_SVC_RESETB
  530. str r1, [r4, #+NETARM_GEN_SOFTWARE_SERVICE]
  531. ldr r1, =NETARM_GEN_SW_SVC_RESETA
  532. str r1, [r4, #+NETARM_GEN_SOFTWARE_SERVICE]
  533. ldr r1, =NETARM_GEN_SW_SVC_RESETB
  534. str r1, [r4, #+NETARM_GEN_SOFTWARE_SERVICE]
  535. mov pc, r0
  536. #elif defined(CONFIG_S3C4510B)
  537. /* Nothing done here as reseting the CPU is board specific, depending
  538. * on external peripherals such as watchdog timers, etc. */
  539. #elif defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_INTEGRATOR)
  540. /* No specific reset actions for IntegratorAP/CM720T as yet */
  541. #elif defined(CONFIG_LPC2292)
  542. .align 5
  543. .globl reset_cpu
  544. reset_cpu:
  545. mov pc, r0
  546. #else
  547. #error No reset_cpu() defined for current CPU type
  548. #endif