plat_helpers.S 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2015-2023, Renesas Electronics Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <arch.h>
  8. #include <asm_macros.S>
  9. #include <common/bl_common.h>
  10. #include <common/runtime_svc.h>
  11. #include <cortex_a57.h>
  12. #include <platform_def.h>
  13. #include "rcar_def.h"
  14. .globl plat_get_my_entrypoint
  15. .extern plat_set_my_stack
  16. .globl platform_mem_init
  17. .globl plat_crash_console_init
  18. .globl plat_crash_console_putc
  19. .globl plat_crash_console_flush
  20. .globl plat_invalidate_icache
  21. .globl plat_report_exception
  22. .globl plat_secondary_reset
  23. .globl plat_reset_handler
  24. .globl plat_my_core_pos
  25. .extern rcar_log_init
  26. .extern console_rcar_init
  27. .extern console_rcar_putc
  28. .extern console_rcar_flush
  29. #if IMAGE_BL2
  30. #define INT_ID_MASK (0x3ff)
  31. .extern bl2_interrupt_error_type
  32. .extern bl2_interrupt_error_id
  33. .globl bl2_enter_bl31
  34. .extern gicv2_acknowledge_interrupt
  35. .extern rcar_swdt_exec
  36. #endif
  37. /* -----------------------------------------------------
  38. * void platform_get_core_pos (mpidr)
  39. * -----------------------------------------------------
  40. */
  41. func platform_get_core_pos
  42. and x1, x0, #MPIDR_CPU_MASK
  43. and x0, x0, #MPIDR_CLUSTER_MASK
  44. add x0, x1, x0, LSR #6
  45. ret
  46. endfunc platform_get_core_pos
  47. /* -----------------------------------------------------
  48. * void platform_my_core_pos
  49. * -----------------------------------------------------
  50. */
  51. func plat_my_core_pos
  52. mrs x0, mpidr_el1
  53. b platform_get_core_pos
  54. endfunc plat_my_core_pos
  55. /* -----------------------------------------------------
  56. * void platform_get_my_entrypoint (unsigned int mpid);
  57. *
  58. * Main job of this routine is to distinguish between
  59. * a cold and warm boot.
  60. * On a cold boot the secondaries first wait for the
  61. * platform to be initialized after which they are
  62. * hotplugged in. The primary proceeds to perform the
  63. * platform initialization.
  64. * On a warm boot, each cpu jumps to the address in its
  65. * mailbox.
  66. *
  67. * TODO: Not a good idea to save lr in a temp reg
  68. * -----------------------------------------------------
  69. */
  70. func plat_get_my_entrypoint
  71. mrs x0, mpidr_el1
  72. mov x9, x30 /* lr */
  73. #if defined(IMAGE_BL2)
  74. /* always cold boot on bl2 */
  75. mov x0, #0
  76. ret x9
  77. #else
  78. ldr x1, =BOOT_KIND_BASE
  79. ldr x21, [x1]
  80. /* Check the reset info */
  81. and x1, x21, #0x000c
  82. cmp x1, #0x0008
  83. beq el3_panic
  84. cmp x1, #0x000c
  85. beq el3_panic
  86. /* Check the boot kind */
  87. and x1, x21, #0x0003
  88. cmp x1, #0x0002
  89. beq el3_panic
  90. cmp x1, #0x0003
  91. beq el3_panic
  92. /* warm boot or cold boot */
  93. and x1, x21, #1
  94. cmp x1, #0
  95. bne warm_reset
  96. /* Cold boot */
  97. mov x0, #0
  98. b exit
  99. warm_reset:
  100. /* --------------------------------------------------------------------
  101. * A per-cpu mailbox is maintained in the trusted SDRAM. Its flushed out
  102. * of the caches after every update using normal memory so its safe to
  103. * read it here with SO attributes
  104. * ---------------------------------------------------------------------
  105. */
  106. ldr x10, =MBOX_BASE
  107. bl platform_get_core_pos
  108. lsl x0, x0, #CACHE_WRITEBACK_SHIFT
  109. ldr x0, [x10, x0]
  110. cbz x0, _panic
  111. exit:
  112. ret x9
  113. _panic:
  114. b el3_panic
  115. #endif
  116. endfunc plat_get_my_entrypoint
  117. /* ---------------------------------------------
  118. * plat_secondary_reset
  119. *
  120. * ---------------------------------------------
  121. */
  122. func plat_secondary_reset
  123. mrs x0, sctlr_el3
  124. bic x0, x0, #SCTLR_EE_BIT
  125. msr sctlr_el3, x0
  126. isb
  127. mrs x0, cptr_el3
  128. bic w0, w0, #TCPAC_BIT
  129. bic w0, w0, #TTA_BIT
  130. bic w0, w0, #TFP_BIT
  131. msr cptr_el3, x0
  132. mov_imm x0, PARAMS_BASE
  133. mov_imm x2, BL31_BASE
  134. ldr x3, =BOOT_KIND_BASE
  135. mov x1, #0x1
  136. str x1, [x3]
  137. br x2 /* jump to BL31 */
  138. nop
  139. nop
  140. nop
  141. endfunc plat_secondary_reset
  142. /* ---------------------------------------------
  143. * plat_enter_bl31
  144. *
  145. * ---------------------------------------------
  146. */
  147. func bl2_enter_bl31
  148. mov x20, x0
  149. /*
  150. * MMU needs to be disabled because both BL2 and BL31 execute
  151. * in EL3, and therefore share the same address space.
  152. * BL31 will initialize the address space according to its
  153. * own requirement.
  154. */
  155. /* Disable mmu and data cache */
  156. bl disable_mmu_el3
  157. #if RCAR_BL2_DCACHE == 1
  158. /* Data cache clean and invalidate */
  159. mov x0, #DCCISW
  160. bl dcsw_op_all
  161. #endif /* RCAR_BL2_DCACHE == 1 */
  162. /* TLB invalidate all, EL3 */
  163. tlbi alle3
  164. bl disable_mmu_icache_el3
  165. /* Invalidate instruction cache */
  166. ic iallu
  167. dsb sy
  168. isb
  169. ldp x0, x1, [x20, #ENTRY_POINT_INFO_PC_OFFSET]
  170. msr elr_el3, x0
  171. msr spsr_el3, x1
  172. exception_return
  173. endfunc bl2_enter_bl31
  174. /* -----------------------------------------------------
  175. * void platform_mem_init (void);
  176. *
  177. * Zero out the mailbox registers in the shared memory
  178. * and set the rcar_boot_kind_flag.
  179. * The mmu is turned off right now and only the primary can
  180. * ever execute this code. Secondaries will read the
  181. * mailboxes using SO accesses.
  182. * -----------------------------------------------------
  183. */
  184. func platform_mem_init
  185. #if !IMAGE_BL2
  186. ldr x0, =MBOX_BASE
  187. mov w1, #PLATFORM_CORE_COUNT
  188. loop:
  189. str xzr, [x0], #CACHE_WRITEBACK_GRANULE
  190. subs w1, w1, #1
  191. b.gt loop
  192. #endif
  193. ret
  194. endfunc platform_mem_init
  195. /* ---------------------------------------------
  196. * void plat_report_exception(unsigned int type)
  197. * Function to report an unhandled exception
  198. * with platform-specific means.
  199. * ---------------------------------------------
  200. */
  201. func plat_report_exception
  202. /* Switch to SP_EL0 */
  203. msr spsel, #0
  204. #if IMAGE_BL2
  205. mov w1, #FIQ_SP_EL0
  206. cmp w0, w1
  207. beq rep_exec_fiq_elx
  208. b rep_exec_panic_type
  209. rep_exec_fiq_elx:
  210. bl gicv2_acknowledge_interrupt
  211. mov x2, #INT_ID_MASK
  212. and x0, x0, x2
  213. mov x1, #ARM_IRQ_SEC_WDT
  214. cmp x0, x1
  215. bne rep_exec_panic_id
  216. mrs x0, ELR_EL3
  217. b rcar_swdt_exec
  218. rep_exec_panic_type:
  219. /* x0 is interrupt TYPE */
  220. b bl2_interrupt_error_type
  221. rep_exec_panic_id:
  222. /* x0 is interrupt ID */
  223. b bl2_interrupt_error_id
  224. rep_exec_end:
  225. #endif
  226. ret
  227. endfunc plat_report_exception
  228. /* ---------------------------------------------
  229. * int plat_crash_console_init(void)
  230. * Function to initialize log area
  231. * ---------------------------------------------
  232. */
  233. func plat_crash_console_init
  234. #if IMAGE_BL2
  235. mov x0, #0
  236. #else
  237. mov x1, sp
  238. mov_imm x2, RCAR_CRASH_STACK
  239. mov sp, x2
  240. str x1, [sp, #-16]!
  241. str x30, [sp, #-16]!
  242. bl console_rcar_init
  243. ldr x30, [sp], #16
  244. ldr x1, [sp], #16
  245. mov sp, x1
  246. #endif
  247. ret
  248. endfunc plat_crash_console_init
  249. /* ---------------------------------------------
  250. * int plat_crash_console_putc(int c)
  251. * Function to store a character to log area
  252. * ---------------------------------------------
  253. */
  254. func plat_crash_console_putc
  255. mov x1, sp
  256. mov_imm x2, RCAR_CRASH_STACK
  257. mov sp, x2
  258. str x1, [sp, #-16]!
  259. str x30, [sp, #-16]!
  260. str x3, [sp, #-16]!
  261. str x4, [sp, #-16]!
  262. str x5, [sp, #-16]!
  263. str x6, [sp, #-16]!
  264. str x7, [sp, #-16]!
  265. bl console_rcar_putc
  266. ldr x7, [sp], #16
  267. ldr x6, [sp], #16
  268. ldr x5, [sp], #16
  269. ldr x4, [sp], #16
  270. ldr x3, [sp], #16
  271. ldr x30, [sp], #16
  272. ldr x1, [sp], #16
  273. mov sp, x1
  274. ret
  275. endfunc plat_crash_console_putc
  276. /* ---------------------------------------------
  277. * void plat_crash_console_flush()
  278. * ---------------------------------------------
  279. */
  280. func plat_crash_console_flush
  281. b console_rcar_flush
  282. endfunc plat_crash_console_flush
  283. /* --------------------------------------------------------------------
  284. * void plat_reset_handler(void);
  285. *
  286. * Before adding code in this function, refer to the guidelines in
  287. * docs/firmware-design.md to determine whether the code should reside
  288. * within the FIRST_RESET_HANDLER_CALL block or not.
  289. *
  290. * For R-Car H3:
  291. * - Set the L2 Tag RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
  292. * - Set the L2 Data setup latency to 1 (i.e. 1 cycles) for Cortex-A57
  293. * - Set the L2 Data RAM latency to 3 (i.e. 4 cycles) for Cortex-A57
  294. * For R-Car M3/M3N:
  295. * - Set the L2 Tag RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
  296. * - Set the L2 Data setup latency to 0 (i.e. 0 cycles) for Cortex-A57
  297. * - Set the L2 Data RAM latency to 3 (i.e. 4 cycles) for Cortex-A57
  298. *
  299. * --------------------------------------------------------------------
  300. */
  301. func plat_reset_handler
  302. /*
  303. * On R-Car H3 : x2 := 0
  304. * On R-Car M3/M3N: x2 := 1
  305. */
  306. /* read PRR */
  307. ldr x0, =0xFFF00044
  308. ldr w0, [x0]
  309. ubfx w0, w0, 8, 8
  310. /* H3? */
  311. cmp w0, #0x4F
  312. b.eq RCARH3
  313. /* set R-Car M3/M3N */
  314. mov x2, #1
  315. b CHK_A5x
  316. RCARH3:
  317. /* set R-Car H3 */
  318. mov x2, #0
  319. /* --------------------------------------------------------------------
  320. * Determine whether this code is executed on a Cortex-A53 or on a
  321. * Cortex-A57 core.
  322. * --------------------------------------------------------------------
  323. */
  324. CHK_A5x:
  325. mrs x0, midr_el1
  326. ubfx x1, x0, MIDR_PN_SHIFT, #12
  327. cmp w1, #((CORTEX_A57_MIDR >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
  328. b.eq A57
  329. ret
  330. A57:
  331. /* Get data from CORTEX_A57_L2CTLR_EL1 */
  332. mrs x0, CORTEX_A57_L2CTLR_EL1
  333. /*
  334. * On R-Car H3/M3/M3N
  335. *
  336. * L2 Tag RAM latency is bit8-6 of CORTEX_A57_L2CTLR_EL1
  337. * L2 Data RAM setup is bit5 of CORTEX_A57_L2CTLR_EL1
  338. * L2 Data RAM latency is bit2-0 of CORTEX_A57_L2CTLR_EL1
  339. */
  340. /* clear bit of L2 RAM */
  341. /* ~(0x1e7) -> x1 */
  342. mov x1, #0x1e7
  343. neg x1, x1
  344. /* clear bit of L2 RAM -> x0 */
  345. and x0, x0, x1
  346. /* L2 Tag RAM latency (3 cycles) */
  347. orr x0, x0, #0x2 << 6
  348. /* If M3/M3N then L2 RAM setup is 0 */
  349. cbnz x2, M3_L2
  350. /* L2 Data RAM setup (1 cycle) */
  351. orr x0, x0, #0x1 << 5
  352. M3_L2:
  353. /* L2 Data RAM latency (4 cycles) */
  354. orr x0, x0, #0x3
  355. /* Store data to L2CTLR_EL1 */
  356. msr CORTEX_A57_L2CTLR_EL1, x0
  357. apply_l2_ram_latencies:
  358. ret
  359. endfunc plat_reset_handler
  360. /* ---------------------------------------------
  361. * void plat_invalidate_icache(void)
  362. * Instruction Cache Invalidate All to PoU
  363. * ---------------------------------------------
  364. */
  365. func plat_invalidate_icache
  366. ic iallu
  367. ret
  368. endfunc plat_invalidate_icache