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