npcm845x_bl31_setup.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c) 2015-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * Copyright (C) 2017-2023 Nuvoton Ltd.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include <assert.h>
  9. #include <arch.h>
  10. #include <arch_helpers.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <drivers/console.h>
  14. #include <drivers/generic_delay_timer.h>
  15. #include <drivers/ti/uart/uart_16550.h>
  16. #include <lib/debugfs.h>
  17. #include <lib/extensions/ras.h>
  18. #include <lib/mmio.h>
  19. #include <lib/xlat_tables/xlat_tables_compat.h>
  20. #include <npcm845x_clock.h>
  21. #include <npcm845x_gcr.h>
  22. #include <npcm845x_lpuart.h>
  23. #include <plat/arm/common/plat_arm.h>
  24. #include <plat/common/platform.h>
  25. #include <plat_npcm845x.h>
  26. #include <platform_def.h>
  27. /*
  28. * Placeholder variables for copying the arguments that have been passed to
  29. * BL31 from BL2.
  30. */
  31. static entry_point_info_t bl32_image_ep_info;
  32. static entry_point_info_t bl33_image_ep_info;
  33. #if !RESET_TO_BL31
  34. /*
  35. * Check that BL31_BASE is above ARM_FW_CONFIG_LIMIT. The reserved page
  36. * is required for SOC_FW_CONFIG/TOS_FW_CONFIG passed from BL2.
  37. */
  38. /* CASSERT(BL31_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl31_base_overflows); */
  39. #endif /* !RESET_TO_BL31 */
  40. #define MAP_BL31_TOTAL MAP_REGION_FLAT( \
  41. BL31_START, \
  42. BL31_END - BL31_START, \
  43. MT_MEMORY | MT_RW | EL3_PAS)
  44. #if RECLAIM_INIT_CODE
  45. IMPORT_SYM(unsigned long, __INIT_CODE_START__, BL_INIT_CODE_BASE);
  46. IMPORT_SYM(unsigned long, __INIT_CODE_END__, BL_CODE_END_UNALIGNED);
  47. #define BL_INIT_CODE_END ((BL_CODE_END_UNALIGNED + PAGE_SIZE - 1) & \
  48. ~(PAGE_SIZE - 1))
  49. #define MAP_BL_INIT_CODE MAP_REGION_FLAT( \
  50. BL_INIT_CODE_BASE, \
  51. BL_INIT_CODE_END - \
  52. BL_INIT_CODE_BASE, \
  53. MT_CODE | MT_SECURE)
  54. #endif /* RECLAIM_INIT_CODE */
  55. #if SEPARATE_NOBITS_REGION
  56. #define MAP_BL31_NOBITS MAP_REGION_FLAT( \
  57. BL31_NOBITS_BASE, \
  58. BL31_NOBITS_LIMIT - \
  59. BL31_NOBITS_BASE, \
  60. MT_MEMORY | MT_RW | EL3_PAS)
  61. #endif /* SEPARATE_NOBITS_REGION */
  62. /******************************************************************************
  63. * Return a pointer to the 'entry_point_info' structure of the next image
  64. * for the security state specified. BL33 corresponds to the non-secure
  65. * image type while BL32 corresponds to the secure image type.
  66. * A NULL pointer is returned if the image does not exist.
  67. *****************************************************************************/
  68. struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type)
  69. {
  70. entry_point_info_t *next_image_info;
  71. assert(sec_state_is_valid(type));
  72. next_image_info = (type == NON_SECURE)
  73. ? &bl33_image_ep_info : &bl32_image_ep_info;
  74. /*
  75. * None of the images on the ARM development platforms can have 0x0
  76. * as the entrypoint
  77. */
  78. if (next_image_info->pc) {
  79. return next_image_info;
  80. } else {
  81. return NULL;
  82. }
  83. }
  84. int board_uart_init(void)
  85. {
  86. unsigned long UART_BASE_ADDR;
  87. static console_t console;
  88. #ifdef CONFIG_TARGET_ARBEL_PALLADIUM
  89. UART_Init(UART0_DEV, UART_MUX_MODE1,
  90. UART_BAUDRATE_115200);
  91. UART_BASE_ADDR = npcm845x_get_base_uart(UART0_DEV);
  92. #else
  93. UART_BASE_ADDR = npcm845x_get_base_uart(UART0_DEV);
  94. #endif /* CONFIG_TARGET_ARBEL_PALLADIUM */
  95. /*
  96. * Register UART w/o initialization -
  97. * A clock rate of zero means to skip the initialisation.
  98. */
  99. console_16550_register((uintptr_t)UART_BASE_ADDR, 0, 0, &console);
  100. return 0;
  101. }
  102. unsigned int plat_get_syscnt_freq2(void)
  103. {
  104. /*
  105. * Do not overwrite the value set by BootBlock
  106. */
  107. return (unsigned int)read_cntfrq_el0();
  108. }
  109. /******************************************************************************
  110. * Perform any BL31 early platform setup common to ARM standard platforms.
  111. * Here is an opportunity to copy parameters passed by the calling EL (S-EL1
  112. * in BL2 & EL3 in BL1) before they are lost (potentially). This needs to be
  113. * done before the MMU is initialized so that the memory layout can be used
  114. * while creating page tables. BL2 has flushed this information to memory,
  115. * so we are guaranteed to pick up good data.
  116. *****************************************************************************/
  117. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  118. u_register_t arg2, u_register_t arg3)
  119. {
  120. arg0 = arg1 = arg2 = arg3 = 0;
  121. #if RESET_TO_BL31
  122. void *from_bl2 = (void *)arg0;
  123. void *plat_params_from_bl2 = (void *)arg3;
  124. if (from_bl2 != NULL) {
  125. assert(from_bl2 == NULL);
  126. }
  127. if (plat_params_from_bl2 != NULL) {
  128. assert(plat_params_from_bl2 == NULL);
  129. }
  130. #endif /* RESET_TO_BL31 */
  131. /* Initialize Delay timer */
  132. generic_delay_timer_init();
  133. /* Do Specific Board/Chip initializations */
  134. board_uart_init();
  135. #if RESET_TO_BL31
  136. /* There are no parameters from BL2 if BL31 is a reset vector */
  137. assert(from_bl2 == NULL);
  138. assert(plat_params_from_bl2 == NULL);
  139. #ifdef BL32_BASE
  140. /* Populate entry point information for BL32 */
  141. SET_PARAM_HEAD(&bl32_image_ep_info,
  142. PARAM_EP,
  143. VERSION_1,
  144. 0);
  145. SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
  146. bl32_image_ep_info.pc = BL32_BASE;
  147. bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
  148. #if defined(SPD_spmd)
  149. /*
  150. * SPM (hafnium in secure world) expects SPM Core manifest base address
  151. * in x0, which in !RESET_TO_BL31 case loaded after base of non shared
  152. * SRAM(after 4KB offset of SRAM). But in RESET_TO_BL31 case all non
  153. * shared SRAM is allocated to BL31, so to avoid overwriting of manifest
  154. * keep it in the last page.
  155. */
  156. bl32_image_ep_info.args.arg0 = ARM_TRUSTED_SRAM_BASE +
  157. PLAT_ARM_TRUSTED_SRAM_SIZE - PAGE_SIZE;
  158. #endif /* SPD_spmd */
  159. #endif /* BL32_BASE */
  160. /* Populate entry point information for BL33 */
  161. SET_PARAM_HEAD(&bl33_image_ep_info,
  162. PARAM_EP,
  163. VERSION_1,
  164. 0);
  165. /*
  166. * Tell BL31 where the non-trusted software image
  167. * is located and the entry state information
  168. */
  169. bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
  170. /* Generic ARM code will switch to EL2, revert to EL1 */
  171. bl33_image_ep_info.spsr = arm_get_spsr_for_bl33_entry();
  172. bl33_image_ep_info.spsr &= ~0x8;
  173. bl33_image_ep_info.spsr |= 0x4;
  174. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, (uint32_t)NON_SECURE);
  175. #if defined(SPD_spmd) && !(ARM_LINUX_KERNEL_AS_BL33)
  176. /*
  177. * Hafnium in normal world expects its manifest address in x0,
  178. * which is loaded at base of DRAM.
  179. */
  180. bl33_image_ep_info.args.arg0 = (u_register_t)ARM_DRAM1_BASE;
  181. #endif /* SPD_spmd && !ARM_LINUX_KERNEL_AS_BL33 */
  182. #if ARM_LINUX_KERNEL_AS_BL33
  183. /*
  184. * According to the file ``Documentation/arm64/booting.txt`` of the
  185. * Linux kernel tree, Linux expects the physical address of the device
  186. * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
  187. * must be 0.
  188. */
  189. bl33_image_ep_info.args.arg0 = (u_register_t)ARM_PRELOADED_DTB_BASE;
  190. bl33_image_ep_info.args.arg1 = 0U;
  191. bl33_image_ep_info.args.arg2 = 0U;
  192. bl33_image_ep_info.args.arg3 = 0U;
  193. #endif /* ARM_LINUX_KERNEL_AS_BL33 */
  194. #else /* RESET_TO_BL31 */
  195. /*
  196. * In debug builds, we pass a special value in 'plat_params_from_bl2'
  197. * to verify platform parameters from BL2 to BL31.
  198. * In release builds, it's not used.
  199. */
  200. assert(((unsigned long long)plat_params_from_bl2) ==
  201. ARM_BL31_PLAT_PARAM_VAL);
  202. /*
  203. * Check params passed from BL2 should not be NULL,
  204. */
  205. bl_params_t *params_from_bl2 = (bl_params_t *)from_bl2;
  206. assert(params_from_bl2 != NULL);
  207. assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
  208. assert(params_from_bl2->h.version >= VERSION_2);
  209. bl_params_node_t *bl_params = params_from_bl2->head;
  210. /*
  211. * Copy BL33 and BL32 (if present), entry point information.
  212. * They are stored in Secure RAM, in BL2's address space.
  213. */
  214. while (bl_params != NULL) {
  215. if (bl_params->image_id == BL32_IMAGE_ID) {
  216. bl32_image_ep_info = *bl_params->ep_info;
  217. }
  218. if (bl_params->image_id == BL33_IMAGE_ID) {
  219. bl33_image_ep_info = *bl_params->ep_info;
  220. }
  221. bl_params = bl_params->next_params_info;
  222. }
  223. if (bl33_image_ep_info.pc == 0U) {
  224. panic();
  225. }
  226. #endif /* RESET_TO_BL31 */
  227. }
  228. /*******************************************************************************
  229. * Perform any BL31 platform setup common to ARM standard platforms
  230. ******************************************************************************/
  231. void bl31_platform_setup(void)
  232. {
  233. /* Initialize the GIC driver, cpu and distributor interfaces */
  234. plat_gic_driver_init();
  235. plat_gic_init();
  236. #if RESET_TO_BL31
  237. #if defined(PLAT_ARM_MEM_PROT_ADDR)
  238. arm_nor_psci_do_dyn_mem_protect();
  239. #endif /* PLAT_ARM_MEM_PROT_ADDR */
  240. #else
  241. /*
  242. * In this soluction, we also do the security initialzation
  243. * even when BL31 is not in the reset vector
  244. */
  245. npcm845x_security_setup();
  246. #endif /* RESET_TO_BL31 */
  247. /* Enable and initialize the System level generic timer */
  248. mmio_write_32(ARM_SYS_CNTCTL_BASE + CNTCR_OFF,
  249. CNTCR_FCREQ(0U) | CNTCR_EN);
  250. /* Initialize power controller before setting up topology */
  251. plat_arm_pwrc_setup();
  252. #if RAS_EXTENSION
  253. ras_init();
  254. #endif
  255. #if USE_DEBUGFS
  256. debugfs_init();
  257. #endif /* USE_DEBUGFS */
  258. }
  259. void arm_console_runtime_init(void)
  260. {
  261. /* Added in order to ignore the original weak function */
  262. }
  263. void plat_arm_program_trusted_mailbox(uintptr_t address)
  264. {
  265. /*
  266. * now we don't use ARM mailbox,
  267. * so that function added to ignore the weak one
  268. */
  269. }
  270. void __init bl31_plat_arch_setup(void)
  271. {
  272. npcm845x_bl31_plat_arch_setup();
  273. }
  274. void __init plat_arm_pwrc_setup(void)
  275. {
  276. /* NPCM850 is always powered so no need for power control */
  277. }
  278. void __init npcm845x_bl31_plat_arch_setup(void)
  279. {
  280. const mmap_region_t bl_regions[] = {
  281. MAP_BL31_TOTAL,
  282. ARM_MAP_BL_RO,
  283. #if USE_COHERENT_MEM
  284. ARM_MAP_BL_COHERENT_RAM,
  285. #endif /* USE_COHERENT_MEM */
  286. ARM_MAP_SHARED_RAM,
  287. {0}
  288. };
  289. setup_page_tables(bl_regions, plat_arm_get_mmap());
  290. enable_mmu_el3(0U);
  291. }