start.S 13 KB

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