start.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* vi: set ts=8 sw=8 noet: */
  2. /*
  3. * u-boot - Startup Code for XScale IXP
  4. *
  5. * Copyright (C) 2003 Kyle Harris <kharris@nexus-tech.net>
  6. *
  7. * Based on startup code example contained in the
  8. * Intel IXP4xx Programmer's Guide and past u-boot Start.S
  9. * samples.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <asm-offsets.h>
  30. #include <config.h>
  31. #include <version.h>
  32. #include <asm/arch/ixp425.h>
  33. #define MMU_Control_M 0x001 /* Enable MMU */
  34. #define MMU_Control_A 0x002 /* Enable address alignment faults */
  35. #define MMU_Control_C 0x004 /* Enable cache */
  36. #define MMU_Control_W 0x008 /* Enable write-buffer */
  37. #define MMU_Control_P 0x010 /* Compatability: 32 bit code */
  38. #define MMU_Control_D 0x020 /* Compatability: 32 bit data */
  39. #define MMU_Control_L 0x040 /* Compatability: */
  40. #define MMU_Control_B 0x080 /* Enable Big-Endian */
  41. #define MMU_Control_S 0x100 /* Enable system protection */
  42. #define MMU_Control_R 0x200 /* Enable ROM protection */
  43. #define MMU_Control_I 0x1000 /* Enable Instruction cache */
  44. #define MMU_Control_X 0x2000 /* Set interrupt vectors at 0xFFFF0000 */
  45. #define MMU_Control_Init (MMU_Control_P|MMU_Control_D|MMU_Control_L)
  46. /*
  47. * Macro definitions
  48. */
  49. /* Delay a bit */
  50. .macro DELAY_FOR cycles, reg0
  51. ldr \reg0, =\cycles
  52. subs \reg0, \reg0, #1
  53. subne pc, pc, #0xc
  54. .endm
  55. /* wait for coprocessor write complete */
  56. .macro CPWAIT reg
  57. mrc p15,0,\reg,c2,c0,0
  58. mov \reg,\reg
  59. sub pc,pc,#4
  60. .endm
  61. .globl _start
  62. _start: b reset
  63. ldr pc, _undefined_instruction
  64. ldr pc, _software_interrupt
  65. ldr pc, _prefetch_abort
  66. ldr pc, _data_abort
  67. ldr pc, _not_used
  68. ldr pc, _irq
  69. ldr pc, _fiq
  70. _undefined_instruction: .word undefined_instruction
  71. _software_interrupt: .word software_interrupt
  72. _prefetch_abort: .word prefetch_abort
  73. _data_abort: .word data_abort
  74. _not_used: .word not_used
  75. _irq: .word irq
  76. _fiq: .word fiq
  77. .balignl 16,0xdeadbeef
  78. /*
  79. * Startup Code (reset vector)
  80. *
  81. * do important init only if we don't start from memory!
  82. * - relocate armboot to ram
  83. * - setup stack
  84. * - jump to second stage
  85. */
  86. .globl _TEXT_BASE
  87. _TEXT_BASE:
  88. .word CONFIG_SYS_TEXT_BASE
  89. /*
  90. * These are defined in the board-specific linker script.
  91. */
  92. .globl _bss_start
  93. _bss_start:
  94. .word __bss_start
  95. .globl _bss_end
  96. _bss_end:
  97. .word _end
  98. #ifdef CONFIG_USE_IRQ
  99. /* IRQ stack memory (calculated at run-time) */
  100. .globl IRQ_STACK_START
  101. IRQ_STACK_START:
  102. .word 0x0badc0de
  103. /* IRQ stack memory (calculated at run-time) */
  104. .globl FIQ_STACK_START
  105. FIQ_STACK_START:
  106. .word 0x0badc0de
  107. #endif
  108. /* IRQ stack memory (calculated at run-time) + 8 bytes */
  109. .globl IRQ_STACK_START_IN
  110. IRQ_STACK_START_IN:
  111. .word 0x0badc0de
  112. .globl _datarel_start
  113. _datarel_start:
  114. .word __datarel_start
  115. .globl _datarelrolocal_start
  116. _datarelrolocal_start:
  117. .word __datarelrolocal_start
  118. .globl _datarellocal_start
  119. _datarellocal_start:
  120. .word __datarellocal_start
  121. .globl _datarelro_start
  122. _datarelro_start:
  123. .word __datarelro_start
  124. .globl _got_start
  125. _got_start:
  126. .word __got_start
  127. .globl _got_end
  128. _got_end:
  129. .word __got_end
  130. /*
  131. * the actual reset code
  132. */
  133. reset:
  134. /* disable mmu, set big-endian */
  135. mov r0, #0xf8
  136. mcr p15, 0, r0, c1, c0, 0
  137. CPWAIT r0
  138. /* invalidate I & D caches & BTB */
  139. mcr p15, 0, r0, c7, c7, 0
  140. CPWAIT r0
  141. /* invalidate I & Data TLB */
  142. mcr p15, 0, r0, c8, c7, 0
  143. CPWAIT r0
  144. /* drain write and fill buffers */
  145. mcr p15, 0, r0, c7, c10, 4
  146. CPWAIT r0
  147. /* disable write buffer coalescing */
  148. mrc p15, 0, r0, c1, c0, 1
  149. orr r0, r0, #1
  150. mcr p15, 0, r0, c1, c0, 1
  151. CPWAIT r0
  152. /* set EXP CS0 to the optimum timing */
  153. ldr r1, =CONFIG_SYS_EXP_CS0
  154. ldr r2, =IXP425_EXP_CS0
  155. str r1, [r2]
  156. /* make sure flash is visible at 0 */
  157. #if 0
  158. ldr r2, =IXP425_EXP_CFG0
  159. ldr r1, [r2]
  160. orr r1, r1, #0x80000000
  161. str r1, [r2]
  162. #endif
  163. mov r1, #CONFIG_SYS_SDR_CONFIG
  164. ldr r2, =IXP425_SDR_CONFIG
  165. str r1, [r2]
  166. /* disable refresh cycles */
  167. mov r1, #0
  168. ldr r3, =IXP425_SDR_REFRESH
  169. str r1, [r3]
  170. /* send nop command */
  171. mov r1, #3
  172. ldr r4, =IXP425_SDR_IR
  173. str r1, [r4]
  174. DELAY_FOR 0x4000, r0
  175. /* set SDRAM internal refresh val */
  176. ldr r1, =CONFIG_SYS_SDRAM_REFRESH_CNT
  177. str r1, [r3]
  178. DELAY_FOR 0x4000, r0
  179. /* send precharge-all command to close all open banks */
  180. mov r1, #2
  181. str r1, [r4]
  182. DELAY_FOR 0x4000, r0
  183. /* provide 8 auto-refresh cycles */
  184. mov r1, #4
  185. mov r5, #8
  186. 111: str r1, [r4]
  187. DELAY_FOR 0x100, r0
  188. subs r5, r5, #1
  189. bne 111b
  190. /* set mode register in sdram */
  191. mov r1, #CONFIG_SYS_SDR_MODE_CONFIG
  192. str r1, [r4]
  193. DELAY_FOR 0x4000, r0
  194. /* send normal operation command */
  195. mov r1, #6
  196. str r1, [r4]
  197. DELAY_FOR 0x4000, r0
  198. /* copy */
  199. mov r0, #0
  200. mov r4, r0
  201. add r2, r0, #CONFIG_SYS_MONITOR_LEN
  202. mov r1, #0x10000000
  203. mov r5, r1
  204. 30:
  205. ldr r3, [r0], #4
  206. str r3, [r1], #4
  207. cmp r0, r2
  208. bne 30b
  209. /* invalidate I & D caches & BTB */
  210. mcr p15, 0, r0, c7, c7, 0
  211. CPWAIT r0
  212. /* invalidate I & Data TLB */
  213. mcr p15, 0, r0, c8, c7, 0
  214. CPWAIT r0
  215. /* drain write and fill buffers */
  216. mcr p15, 0, r0, c7, c10, 4
  217. CPWAIT r0
  218. /* move flash to 0x50000000 */
  219. ldr r2, =IXP425_EXP_CFG0
  220. ldr r1, [r2]
  221. bic r1, r1, #0x80000000
  222. str r1, [r2]
  223. nop
  224. nop
  225. nop
  226. nop
  227. nop
  228. nop
  229. /* invalidate I & Data TLB */
  230. mcr p15, 0, r0, c8, c7, 0
  231. CPWAIT r0
  232. /* enable I cache */
  233. mrc p15, 0, r0, c1, c0, 0
  234. orr r0, r0, #MMU_Control_I
  235. mcr p15, 0, r0, c1, c0, 0
  236. CPWAIT r0
  237. mrs r0,cpsr /* set the cpu to SVC32 mode */
  238. bic r0,r0,#0x1f /* (superviser mode, M=10011) */
  239. orr r0,r0,#0x13
  240. msr cpsr,r0
  241. /* Set stackpointer in internal RAM to call board_init_f */
  242. call_board_init_f:
  243. ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
  244. ldr r0,=0x00000000
  245. bl board_init_f
  246. /*------------------------------------------------------------------------------*/
  247. /*
  248. * void relocate_code (addr_sp, gd, addr_moni)
  249. *
  250. * This "function" does not return, instead it continues in RAM
  251. * after relocating the monitor code.
  252. *
  253. */
  254. .globl relocate_code
  255. relocate_code:
  256. mov r4, r0 /* save addr_sp */
  257. mov r5, r1 /* save addr of gd */
  258. mov r6, r2 /* save addr of destination */
  259. mov r7, r2 /* save addr of destination */
  260. /* Set up the stack */
  261. stack_setup:
  262. mov sp, r4
  263. adr r0, _start
  264. ldr r2, _TEXT_BASE
  265. ldr r3, _bss_start
  266. sub r2, r3, r2 /* r2 <- size of armboot */
  267. add r2, r0, r2 /* r2 <- source end address */
  268. cmp r0, r6
  269. beq clear_bss
  270. copy_loop:
  271. ldmia r0!, {r9-r10} /* copy from source address [r0] */
  272. stmia r6!, {r9-r10} /* copy to target address [r1] */
  273. cmp r0, r2 /* until source end address [r2] */
  274. blo copy_loop
  275. #ifndef CONFIG_PRELOADER
  276. /* fix got entries */
  277. ldr r1, _TEXT_BASE /* Text base */
  278. mov r0, r7 /* reloc addr */
  279. ldr r2, _got_start /* addr in Flash */
  280. ldr r3, _got_end /* addr in Flash */
  281. sub r3, r3, r1
  282. add r3, r3, r0
  283. sub r2, r2, r1
  284. add r2, r2, r0
  285. fixloop:
  286. ldr r4, [r2]
  287. sub r4, r4, r1
  288. add r4, r4, r0
  289. str r4, [r2]
  290. add r2, r2, #4
  291. cmp r2, r3
  292. blo fixloop
  293. #endif
  294. clear_bss:
  295. #ifndef CONFIG_PRELOADER
  296. ldr r0, _bss_start
  297. ldr r1, _bss_end
  298. ldr r3, _TEXT_BASE /* Text base */
  299. mov r4, r7 /* reloc addr */
  300. sub r0, r0, r3
  301. add r0, r0, r4
  302. sub r1, r1, r3
  303. add r1, r1, r4
  304. mov r2, #0x00000000 /* clear */
  305. clbss_l:str r2, [r0] /* clear loop... */
  306. add r0, r0, #4
  307. cmp r0, r1
  308. bne clbss_l
  309. bl coloured_LED_init
  310. bl red_LED_on
  311. #endif
  312. /*
  313. * We are done. Do not return, instead branch to second part of board
  314. * initialization, now running from RAM.
  315. */
  316. ldr r0, _TEXT_BASE
  317. ldr r2, _board_init_r
  318. sub r2, r2, r0
  319. add r2, r2, r7 /* position from board_init_r in RAM */
  320. /* setup parameters for board_init_r */
  321. mov r0, r5 /* gd_t */
  322. mov r1, r7 /* dest_addr */
  323. /* jump to it ... */
  324. mov lr, r2
  325. mov pc, lr
  326. _board_init_r: .word board_init_r
  327. /****************************************************************************/
  328. /* */
  329. /* Interrupt handling */
  330. /* */
  331. /****************************************************************************/
  332. /* IRQ stack frame */
  333. #define S_FRAME_SIZE 72
  334. #define S_OLD_R0 68
  335. #define S_PSR 64
  336. #define S_PC 60
  337. #define S_LR 56
  338. #define S_SP 52
  339. #define S_IP 48
  340. #define S_FP 44
  341. #define S_R10 40
  342. #define S_R9 36
  343. #define S_R8 32
  344. #define S_R7 28
  345. #define S_R6 24
  346. #define S_R5 20
  347. #define S_R4 16
  348. #define S_R3 12
  349. #define S_R2 8
  350. #define S_R1 4
  351. #define S_R0 0
  352. #define MODE_SVC 0x13
  353. /* use bad_save_user_regs for abort/prefetch/undef/swi ... */
  354. .macro bad_save_user_regs
  355. sub sp, sp, #S_FRAME_SIZE
  356. stmia sp, {r0 - r12} /* Calling r0-r12 */
  357. add r8, sp, #S_PC
  358. ldr r2, IRQ_STACK_START_IN
  359. ldmia r2, {r2 - r4} /* get pc, cpsr, old_r0 */
  360. add r0, sp, #S_FRAME_SIZE /* restore sp_SVC */
  361. add r5, sp, #S_SP
  362. mov r1, lr
  363. stmia r5, {r0 - r4} /* save sp_SVC, lr_SVC, pc, cpsr, old_r */
  364. mov r0, sp
  365. .endm
  366. /* use irq_save_user_regs / irq_restore_user_regs for */
  367. /* IRQ/FIQ handling */
  368. .macro irq_save_user_regs
  369. sub sp, sp, #S_FRAME_SIZE
  370. stmia sp, {r0 - r12} /* Calling r0-r12 */
  371. add r8, sp, #S_PC
  372. stmdb r8, {sp, lr}^ /* Calling SP, LR */
  373. str lr, [r8, #0] /* Save calling PC */
  374. mrs r6, spsr
  375. str r6, [r8, #4] /* Save CPSR */
  376. str r0, [r8, #8] /* Save OLD_R0 */
  377. mov r0, sp
  378. .endm
  379. .macro irq_restore_user_regs
  380. ldmia sp, {r0 - lr}^ @ Calling r0 - lr
  381. mov r0, r0
  382. ldr lr, [sp, #S_PC] @ Get PC
  383. add sp, sp, #S_FRAME_SIZE
  384. subs pc, lr, #4 @ return & move spsr_svc into cpsr
  385. .endm
  386. .macro get_bad_stack
  387. ldr r13, IRQ_STACK_START_IN @ setup our mode stack
  388. str lr, [r13] @ save caller lr / spsr
  389. mrs lr, spsr
  390. str lr, [r13, #4]
  391. mov r13, #MODE_SVC @ prepare SVC-Mode
  392. msr spsr_c, r13
  393. mov lr, pc
  394. movs pc, lr
  395. .endm
  396. .macro get_irq_stack @ setup IRQ stack
  397. ldr sp, IRQ_STACK_START
  398. .endm
  399. .macro get_fiq_stack @ setup FIQ stack
  400. ldr sp, FIQ_STACK_START
  401. .endm
  402. /****************************************************************************/
  403. /* */
  404. /* exception handlers */
  405. /* */
  406. /****************************************************************************/
  407. .align 5
  408. undefined_instruction:
  409. get_bad_stack
  410. bad_save_user_regs
  411. bl do_undefined_instruction
  412. .align 5
  413. software_interrupt:
  414. get_bad_stack
  415. bad_save_user_regs
  416. bl do_software_interrupt
  417. .align 5
  418. prefetch_abort:
  419. get_bad_stack
  420. bad_save_user_regs
  421. bl do_prefetch_abort
  422. .align 5
  423. data_abort:
  424. get_bad_stack
  425. bad_save_user_regs
  426. bl do_data_abort
  427. .align 5
  428. not_used:
  429. get_bad_stack
  430. bad_save_user_regs
  431. bl do_not_used
  432. #ifdef CONFIG_USE_IRQ
  433. .align 5
  434. irq:
  435. get_irq_stack
  436. irq_save_user_regs
  437. bl do_irq
  438. irq_restore_user_regs
  439. .align 5
  440. fiq:
  441. get_fiq_stack
  442. irq_save_user_regs /* someone ought to write a more */
  443. bl do_fiq /* effiction fiq_save_user_regs */
  444. irq_restore_user_regs
  445. #else
  446. .align 5
  447. irq:
  448. get_bad_stack
  449. bad_save_user_regs
  450. bl do_irq
  451. .align 5
  452. fiq:
  453. get_bad_stack
  454. bad_save_user_regs
  455. bl do_fiq
  456. #endif
  457. /****************************************************************************/
  458. /* */
  459. /* Reset function: Use Watchdog to reset */
  460. /* */
  461. /****************************************************************************/
  462. .align 5
  463. .globl reset_cpu
  464. reset_cpu:
  465. ldr r1, =0x482e
  466. ldr r2, =IXP425_OSWK
  467. str r1, [r2]
  468. ldr r1, =0x0fff
  469. ldr r2, =IXP425_OSWT
  470. str r1, [r2]
  471. ldr r1, =0x5
  472. ldr r2, =IXP425_OSWE
  473. str r1, [r2]
  474. b reset_endless
  475. reset_endless:
  476. b reset_endless
  477. #ifdef CONFIG_USE_IRQ
  478. .LC0: .word loops_per_jiffy
  479. /*
  480. * 0 <= r0 <= 2000
  481. */
  482. .globl __udelay
  483. __udelay:
  484. mov r2, #0x6800
  485. orr r2, r2, #0x00db
  486. mul r0, r2, r0
  487. ldr r2, .LC0
  488. ldr r2, [r2] @ max = 0x0fffffff
  489. mov r0, r0, lsr #11 @ max = 0x00003fff
  490. mov r2, r2, lsr #11 @ max = 0x0003ffff
  491. mul r0, r2, r0 @ max = 2^32-1
  492. movs r0, r0, lsr #6
  493. delay_loop:
  494. subs r0, r0, #1
  495. bne delay_loop
  496. mov pc, lr
  497. #endif /* CONFIG_USE_IRQ */