start.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Andesboot - Startup Code for Whitiger core
  3. *
  4. * Copyright (C) 2006 Andes Technology Corporation
  5. * Copyright (C) 2006 Shawn Lin <nobuhiro@andestech.com>
  6. * Copyright (C) 2011 Macpaul Lin <macpaul@andestech.com>
  7. * Greentime Hu <greentime@andestech.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. .pic
  12. #include <asm-offsets.h>
  13. #include <config.h>
  14. #include <common.h>
  15. #include <asm/macro.h>
  16. /*
  17. * Jump vector table for EVIC mode
  18. */
  19. #define ENA_DCAC 2UL
  20. #define DIS_DCAC ~ENA_DCAC
  21. #define ICAC_MEM_KBF_ISET (0x07) ! I Cache sets per way
  22. #define ICAC_MEM_KBF_IWAY (0x07<<3) ! I cache ways
  23. #define ICAC_MEM_KBF_ISZ (0x07<<6) ! I cache line size
  24. #define DCAC_MEM_KBF_DSET (0x07) ! D Cache sets per way
  25. #define DCAC_MEM_KBF_DWAY (0x07<<3) ! D cache ways
  26. #define DCAC_MEM_KBF_DSZ (0x07<<6) ! D cache line size
  27. #define PSW $ir0
  28. #define EIT_INTR_PSW $ir1 ! interruption $PSW
  29. #define EIT_PREV_IPSW $ir2 ! previous $IPSW
  30. #define EIT_IVB $ir3 ! intr vector base address
  31. #define EIT_EVA $ir4 ! MMU related Exception VA reg
  32. #define EIT_PREV_EVA $ir5 ! previous $eva
  33. #define EIT_ITYPE $ir6 ! interruption type
  34. #define EIT_PREV_ITYPE $ir7 ! prev intr type
  35. #define EIT_MACH_ERR $ir8 ! machine error log
  36. #define EIT_INTR_PC $ir9 ! Interruption PC
  37. #define EIT_PREV_IPC $ir10 ! previous $IPC
  38. #define EIT_OVL_INTR_PC $ir11 ! overflow interruption PC
  39. #define EIT_PREV_P0 $ir12 ! prev $P0
  40. #define EIT_PREV_P1 $ir13 ! prev $p1
  41. #define CR_ICAC_MEM $cr1 ! I-cache/memory config reg
  42. #define CR_DCAC_MEM $cr2 ! D-cache/memory config reg
  43. #define MR_CAC_CTL $mr8
  44. .globl _start
  45. _start: j reset
  46. j tlb_fill
  47. j tlb_not_present
  48. j tlb_misc
  49. j tlb_vlpt_miss
  50. j machine_error
  51. j debug
  52. j general_exception
  53. j syscall
  54. j internal_interrupt ! H0I
  55. j internal_interrupt ! H1I
  56. j internal_interrupt ! H2I
  57. j internal_interrupt ! H3I
  58. j internal_interrupt ! H4I
  59. j internal_interrupt ! H5I
  60. j software_interrupt ! S0I
  61. .balign 16
  62. /*
  63. * Andesboot Startup Code (reset vector)
  64. *
  65. * 1. bootstrap
  66. * 1.1 reset - start of u-boot
  67. * 1.2 to superuser mode - as is when reset
  68. * 1.4 Do lowlevel_init
  69. * - (this will jump out to lowlevel_init.S in SoC)
  70. * - (lowlevel_init)
  71. * 1.3 Turn off watchdog timer
  72. * - (this will jump out to watchdog.S in SoC)
  73. * - (turnoff_watchdog)
  74. * 2. Do critical init when reboot (not from mem)
  75. * 3. Relocate andesboot to ram
  76. * 4. Setup stack
  77. * 5. Jump to second stage (board_init_r)
  78. */
  79. /* Note: TEXT_BASE is defined by the (board-dependent) linker script */
  80. .globl _TEXT_BASE
  81. _TEXT_BASE:
  82. .word CONFIG_SYS_TEXT_BASE
  83. /* IRQ stack memory (calculated at run-time) + 8 bytes */
  84. .globl IRQ_STACK_START_IN
  85. IRQ_STACK_START_IN:
  86. .word 0x0badc0de
  87. /*
  88. * The bootstrap code of nds32 core
  89. */
  90. reset:
  91. /*
  92. * gp = ~0 for burn mode
  93. * = ~load_address for load mode
  94. */
  95. reset_gp:
  96. .relax_hint 0
  97. sethi $gp, hi20(_GLOBAL_OFFSET_TABLE_-8)
  98. .relax_hint 0
  99. ori $gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_-4)
  100. add5.pc $gp
  101. set_ivb:
  102. li $r0, 0x0
  103. /* turn on BTB */
  104. mtsr $r0, $misc_ctl
  105. /* set IVIC, vector size: 4 bytes, base: 0x0 */
  106. mtsr $r0, $ivb
  107. /*
  108. * MMU_CTL NTC0 Cacheable/Write-Back
  109. */
  110. li $r0, ~0x3
  111. mfsr $r1, $mr8
  112. and $r1, $r1, $r0
  113. mtsr $r1, $mr8
  114. #if (!defined(CONFIG_SYS_ICACHE_OFF) || !defined(CONFIG_SYS_DCACHE_OFF))
  115. li $r0, 0x4
  116. mfsr $r1, $mr0
  117. or $r1, $r1, $r0
  118. mtsr $r1, $mr0
  119. #endif
  120. #if !defined(CONFIG_SYS_ICACHE_OFF)
  121. li $r0, 0x1
  122. mfsr $r1, $mr8
  123. or $r1, $r1, $r0
  124. mtsr $r1, $mr8
  125. #endif
  126. #if !defined(CONFIG_SYS_DCACHE_OFF)
  127. li $r0, 0x2
  128. mfsr $r1, $mr8
  129. or $r1, $r1, $r0
  130. mtsr $r1, $mr8
  131. #endif
  132. jal mem_init
  133. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  134. jal lowlevel_init
  135. /*
  136. * gp = ~VMA for burn mode
  137. * = ~load_address for load mode
  138. */
  139. update_gp:
  140. .relax_hint 0
  141. sethi $gp, hi20(_GLOBAL_OFFSET_TABLE_-8)
  142. .relax_hint 0
  143. ori $gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_-4)
  144. add5.pc $gp
  145. #endif
  146. /*
  147. * do critical initializations first (shall be in short time)
  148. * do self_relocation ASAP.
  149. */
  150. /*
  151. * Set the N1213 (Whitiger) core to superuser mode
  152. * According to spec, it is already when reset
  153. */
  154. #ifndef CONFIG_SKIP_TRUNOFF_WATCHDOG
  155. jal turnoff_watchdog
  156. #endif
  157. /*
  158. * Do CPU critical regs init only at reboot,
  159. * not when booting from ram
  160. */
  161. #ifdef CONFIG_INIT_CRITICAL
  162. jal cpu_init_crit ! Do CPU critical regs init
  163. #endif
  164. /*
  165. * Set stackpointer in internal RAM to call board_init_f
  166. * $sp must be 8-byte alignment for ABI compliance.
  167. */
  168. call_board_init_f:
  169. li $sp, CONFIG_SYS_INIT_SP_ADDR
  170. move $r0, $sp
  171. bal board_init_f_alloc_reserve
  172. move $sp, $r0
  173. bal board_init_f_init_reserve
  174. #ifdef CONFIG_DEBUG_UART
  175. bal debug_uart_init
  176. #endif
  177. li $r0, 0x00000000
  178. #ifdef __PIC__
  179. #ifdef __NDS32_N1213_43U1H__
  180. /* __NDS32_N1213_43U1H__ implies NDS32 V0 ISA */
  181. la $r15, board_init_f ! store function address into $r15
  182. #endif
  183. #endif
  184. j board_init_f ! jump to board_init_f() in lib/board.c
  185. /*
  186. * void relocate_code (addr_sp, gd, addr_moni)
  187. *
  188. * This "function" does not return, instead it continues in RAM
  189. * after relocating the monitor code.
  190. *
  191. */
  192. /*
  193. * gp = ~RAM_SIZE - TEXT_SIZE for burn/load mode
  194. */
  195. .globl relocate_code
  196. relocate_code:
  197. move $r4, $r0 /* save addr_sp */
  198. move $r5, $r1 /* save addr of gd */
  199. move $r6, $r2 /* save addr of destination */
  200. /* Set up the stack */
  201. stack_setup:
  202. move $sp, $r4
  203. la $r0, _start@GOTOFF
  204. beq $r0, $r6, clear_bss /* skip relocation */
  205. la $r1, _end@GOTOFF
  206. move $r2, $r6 /* r2 <- scratch for copy_loop */
  207. copy_loop:
  208. lmw.bim $r11, [$r0], $r18
  209. smw.bim $r11, [$r2], $r18
  210. blt $r0, $r1, copy_loop
  211. /*
  212. * fix relocations related issues
  213. */
  214. fix_relocations:
  215. l.w $r0, _TEXT_BASE@GOTOFF /* r0 <- Text base */
  216. sub $r9, $r6, $r0 /* r9 <- relocation offset */
  217. la $r7, __rel_dyn_start@GOTOFF
  218. add $r7, $r7, $r9 /* r2 <- rel __got_start in RAM */
  219. la $r8, __rel_dyn_end@GOTOFF
  220. add $r8, $r8, $r9 /* r2 <- rel __got_start in RAM */
  221. li $r3, #0x2a /* R_NDS32_RELATIVE */
  222. 1:
  223. lmw.bim $r0, [$r7], $r2 /* r0,r1,r2 <- adr,type,addend */
  224. bne $r1, $r3, 2f
  225. add $r0, $r0, $r9
  226. add $r2, $r2, $r9
  227. sw $r2, [$r0]
  228. 2:
  229. blt $r7, $r8, 1b
  230. clear_bss:
  231. la $r0, __bss_start@GOTOFF /* r0 <- rel __bss_start in FLASH */
  232. add $r0, $r0, $r9 /* r0 <- rel __bss_start in FLASH */
  233. la $r1, __bss_end@GOTOFF /* r1 <- rel __bss_end in RAM */
  234. add $r1, $r1, $r9 /* r0 <- rel __bss_end in RAM */
  235. li $r2, 0x00000000 /* clear */
  236. clbss_l:
  237. sw $r2, [$r0] /* clear loop... */
  238. addi $r0, $r0, #4
  239. bne $r0, $r1, clbss_l
  240. /*
  241. * We are done. Do not return, instead branch to second part of board
  242. * initialization, now running from RAM.
  243. */
  244. call_board_init_r:
  245. bal invalidate_icache_all
  246. bal flush_dcache_all
  247. la $r0, board_init_r@GOTOFF
  248. move $lp, $r0 /* offset of board_init_r() */
  249. add $lp, $lp, $r9 /* real address of board_init_r() */
  250. /* setup parameters for board_init_r */
  251. move $r0, $r5 /* gd_t */
  252. move $r1, $r6 /* dest_addr */
  253. #ifdef __PIC__
  254. #ifdef __NDS32_N1213_43U1H__ /* NDS32 V0 ISA */
  255. move $r15, $lp /* store function address into $r15 */
  256. #endif
  257. #endif
  258. /* jump to it ... */
  259. jr $lp /* jump to board_init_r() */
  260. /*
  261. * Initialize CPU critical registers
  262. *
  263. * 1. Setup control registers
  264. * 1.1 Mask all IRQs
  265. * 1.2 Flush cache and TLB
  266. * 1.3 Disable MMU and cache
  267. * 2. Setup memory timing
  268. */
  269. cpu_init_crit:
  270. move $r0, $lp /* push ra */
  271. /* Disable Interrupts by clear GIE in $PSW reg */
  272. setgie.d
  273. /* Flush caches and TLB */
  274. /* Invalidate caches */
  275. jal invalidate_icac
  276. jal invalidate_dcac
  277. /* Flush TLB */
  278. mfsr $p0, $MMU_CFG
  279. andi $p0, $p0, 0x3 ! MMPS
  280. li $p1, 0x2 ! TLB MMU
  281. bne $p0, $p1, 1f
  282. tlbop flushall ! Flush TLB
  283. 1:
  284. ! Disable MMU, Dcache
  285. ! Whitiger is MMU disabled when reset
  286. ! Disable the D$
  287. mfsr $p0, MR_CAC_CTL ! Get the $CACHE_CTL reg
  288. li $p1, DIS_DCAC
  289. and $p0, $p0, $p1 ! Set DC_EN bit
  290. mtsr $p0, MR_CAC_CTL ! write back the $CACHE_CTL reg
  291. isb
  292. move $lp, $r0
  293. 2:
  294. ret
  295. /*
  296. * Invalidate I$
  297. */
  298. invalidate_icac:
  299. ! read $cr1(I CAC/MEM cfg. reg.) configuration
  300. mfsr $t0, CR_ICAC_MEM
  301. ! Get the ISZ field
  302. andi $p0, $t0, ICAC_MEM_KBF_ISZ
  303. ! if $p0=0, then no I CAC existed
  304. beqz $p0, end_flush_icache
  305. ! get $p0 the index of I$ block
  306. srli $p0, $p0, 6
  307. ! $t1= bit width of I cache line size(ISZ)
  308. addi $t1, $p0, 2
  309. li $t4, 1
  310. sll $t5, $t4, $t1 ! get $t5 cache line size
  311. andi $p1, $t0, ICAC_MEM_KBF_ISET ! get the ISET field
  312. addi $t2, $p1, 6 ! $t2= bit width of ISET
  313. andi $p1, $t0, ICAC_MEM_KBF_IWAY ! get bitfield of Iway
  314. srli $p1, $p1, 3
  315. addi $p1, $p1, 1 ! then $p1 is I way number
  316. add $t3, $t2, $t1 ! SHIFT
  317. sll $p1, $p1, $t3 ! GET the total cache size
  318. ICAC_LOOP:
  319. sub $p1, $p1, $t5
  320. cctl $p1, L1I_IX_INVAL
  321. bnez $p1, ICAC_LOOP
  322. end_flush_icache:
  323. ret
  324. /*
  325. * Invalidate D$
  326. */
  327. invalidate_dcac:
  328. ! read $cr2(D CAC/MEM cfg. reg.) configuration
  329. mfsr $t0, CR_DCAC_MEM
  330. ! Get the DSZ field
  331. andi $p0, $t0, DCAC_MEM_KBF_DSZ
  332. ! if $p0=0, then no D CAC existed
  333. beqz $p0, end_flush_dcache
  334. ! get $p0 the index of D$ block
  335. srli $p0, $p0, 6
  336. ! $t1= bit width of D cache line size(DSZ)
  337. addi $t1, $p0, 2
  338. li $t4, 1
  339. sll $t5, $t4, $t1 ! get $t5 cache line size
  340. andi $p1, $t0, DCAC_MEM_KBF_DSET ! get the DSET field
  341. addi $t2, $p1, 6 ! $t2= bit width of DSET
  342. andi $p1, $t0, DCAC_MEM_KBF_DWAY ! get bitfield of D way
  343. srli $p1, $p1, 3
  344. addi $p1, $p1, 1 ! then $p1 is D way number
  345. add $t3, $t2, $t1 ! SHIFT
  346. sll $p1, $p1, $t3 ! GET the total cache size
  347. DCAC_LOOP:
  348. sub $p1, $p1, $t5
  349. cctl $p1, L1D_IX_INVAL
  350. bnez $p1, DCAC_LOOP
  351. end_flush_dcache:
  352. ret
  353. /*
  354. * Interrupt handling
  355. */
  356. /*
  357. * exception handlers
  358. */
  359. .align 5
  360. .macro SAVE_ALL
  361. ! FIXME: Other way to get PC?
  362. ! FIXME: Update according to the newest spec!!
  363. 1:
  364. li $r28, 1
  365. push $r28
  366. mfsr $r28, PSW ! $PSW
  367. push $r28
  368. mfsr $r28, EIT_EVA ! $ir1 $EVA
  369. push $r28
  370. mfsr $r28, EIT_ITYPE ! $ir2 $ITYPE
  371. push $r28
  372. mfsr $r28, EIT_MACH_ERR ! $ir3 Mach Error
  373. push $r28
  374. mfsr $r28, EIT_INTR_PSW ! $ir5 $IPSW
  375. push $r28
  376. mfsr $r28, EIT_PREV_IPSW ! $ir6 prev $IPSW
  377. push $r28
  378. mfsr $r28, EIT_PREV_EVA ! $ir7 prev $EVA
  379. push $r28
  380. mfsr $r28, EIT_PREV_ITYPE ! $ir8 prev $ITYPE
  381. push $r28
  382. mfsr $r28, EIT_INTR_PC ! $ir9 Interruption PC
  383. push $r28
  384. mfsr $r28, EIT_PREV_IPC ! $ir10 prev INTR_PC
  385. push $r28
  386. mfsr $r28, EIT_OVL_INTR_PC ! $ir11 Overflowed INTR_PC
  387. push $r28
  388. mfusr $r28, $d1.lo
  389. push $r28
  390. mfusr $r28, $d1.hi
  391. push $r28
  392. mfusr $r28, $d0.lo
  393. push $r28
  394. mfusr $r28, $d0.hi
  395. push $r28
  396. pushm $r0, $r30 ! store $sp-$r31, ra-$r30, $gp-$r29, $r28-$fp
  397. addi $sp, $sp, -4 ! make room for implicit pt_regs parameters
  398. .endm
  399. .align 5
  400. tlb_fill:
  401. SAVE_ALL
  402. move $r0, $sp ! To get the kernel stack
  403. li $r1, 1 ! Determine interruption type
  404. bal do_interruption
  405. .align 5
  406. tlb_not_present:
  407. SAVE_ALL
  408. move $r0, $sp ! To get the kernel stack
  409. li $r1, 2 ! Determine interruption type
  410. bal do_interruption
  411. .align 5
  412. tlb_misc:
  413. SAVE_ALL
  414. move $r0, $sp ! To get the kernel stack
  415. li $r1, 3 ! Determine interruption type
  416. bal do_interruption
  417. .align 5
  418. tlb_vlpt_miss:
  419. SAVE_ALL
  420. move $r0, $sp ! To get the kernel stack
  421. li $r1, 4 ! Determine interruption type
  422. bal do_interruption
  423. .align 5
  424. machine_error:
  425. SAVE_ALL
  426. move $r0, $sp ! To get the kernel stack
  427. li $r1, 5 ! Determine interruption type
  428. bal do_interruption
  429. .align 5
  430. debug:
  431. SAVE_ALL
  432. move $r0, $sp ! To get the kernel stack
  433. li $r1, 6 ! Determine interruption type
  434. bal do_interruption
  435. .align 5
  436. general_exception:
  437. SAVE_ALL
  438. move $r0, $sp ! To get the kernel stack
  439. li $r1, 7 ! Determine interruption type
  440. bal do_interruption
  441. .align 5
  442. syscall:
  443. SAVE_ALL
  444. move $r0, $sp ! To get the kernel stack
  445. li $r1, 8 ! Determine interruption type
  446. bal do_interruption
  447. .align 5
  448. internal_interrupt:
  449. SAVE_ALL
  450. move $r0, $sp ! To get the kernel stack
  451. li $r1, 9 ! Determine interruption type
  452. bal do_interruption
  453. .align 5
  454. software_interrupt:
  455. SAVE_ALL
  456. move $r0, $sp ! To get the kernel stack
  457. li $r1, 10 ! Determine interruption type
  458. bal do_interruption
  459. .align 5
  460. /*
  461. * void reset_cpu(ulong addr);
  462. * $r0: input address to jump to
  463. */
  464. .globl reset_cpu
  465. reset_cpu:
  466. /* No need to disable MMU because we never enable it */
  467. bal invalidate_icac
  468. bal invalidate_dcac
  469. mfsr $p0, $MMU_CFG
  470. andi $p0, $p0, 0x3 ! MMPS
  471. li $p1, 0x2 ! TLB MMU
  472. bne $p0, $p1, 1f
  473. tlbop flushall ! Flush TLB
  474. 1:
  475. mfsr $p0, MR_CAC_CTL ! Get the $CACHE_CTL reg
  476. li $p1, DIS_DCAC
  477. and $p0, $p0, $p1 ! Clear the DC_EN bit
  478. mtsr $p0, MR_CAC_CTL ! Write back the $CACHE_CTL reg
  479. br $r0 ! Jump to the input address