1840831b2SStephan Gerhold /* 2840831b2SStephan Gerhold * Copyright (c) 2021-2023, Stephan Gerhold <stephan@gerhold.net> 3840831b2SStephan Gerhold * 4840831b2SStephan Gerhold * SPDX-License-Identifier: BSD-3-Clause 5840831b2SStephan Gerhold */ 6840831b2SStephan Gerhold 7840831b2SStephan Gerhold #include <assert.h> 8840831b2SStephan Gerhold 9840831b2SStephan Gerhold #include <arch.h> 10840831b2SStephan Gerhold #include <lib/mmio.h> 11840831b2SStephan Gerhold 12840831b2SStephan Gerhold #include "msm8916_config.h" 13840831b2SStephan Gerhold #include "msm8916_gicv2.h" 14840831b2SStephan Gerhold #include <msm8916_mmap.h> 15840831b2SStephan Gerhold #include <platform_def.h> 16840831b2SStephan Gerhold 17840831b2SStephan Gerhold static void msm8916_configure_timer(void) 18840831b2SStephan Gerhold { 19840831b2SStephan Gerhold /* Set timer frequency */ 20840831b2SStephan Gerhold mmio_write_32(APCS_QTMR + CNTCTLBASE_CNTFRQ, PLAT_SYSCNT_FREQ); 21840831b2SStephan Gerhold 22840831b2SStephan Gerhold /* Make all timer frames available to non-secure world */ 23840831b2SStephan Gerhold mmio_write_32(APCS_QTMR + CNTNSAR, GENMASK_32(7, 0)); 24840831b2SStephan Gerhold } 25840831b2SStephan Gerhold 26840831b2SStephan Gerhold /* 27840831b2SStephan Gerhold * The APCS register regions always start with a SECURE register that should 28840831b2SStephan Gerhold * be cleared to 0 to only allow secure access. Since BL31 handles most of 29840831b2SStephan Gerhold * the CPU power management, most of them can be cleared to secure access only. 30840831b2SStephan Gerhold */ 31840831b2SStephan Gerhold #define APCS_GLB_SECURE_STS_NS BIT_32(0) 32840831b2SStephan Gerhold #define APCS_GLB_SECURE_PWR_NS BIT_32(1) 33840831b2SStephan Gerhold #define APCS_BOOT_START_ADDR_SEC (APCS_CFG + 0x04) 34840831b2SStephan Gerhold #define REMAP_EN BIT_32(0) 35840831b2SStephan Gerhold #define APCS_AA64NAA32_REG (APCS_CFG + 0x0c) 36840831b2SStephan Gerhold 37840831b2SStephan Gerhold static void msm8916_configure_cpu_pm(void) 38840831b2SStephan Gerhold { 39840831b2SStephan Gerhold unsigned int cpu; 40840831b2SStephan Gerhold 41840831b2SStephan Gerhold /* Disallow non-secure access to boot remapper / TCM registers */ 42840831b2SStephan Gerhold mmio_write_32(APCS_CFG, 0); 43840831b2SStephan Gerhold 44840831b2SStephan Gerhold /* 45840831b2SStephan Gerhold * Disallow non-secure access to power management registers. 46840831b2SStephan Gerhold * However, allow STS and PWR since those also seem to control access 47840831b2SStephan Gerhold * to CPU frequency related registers (e.g. APCS_CMD_RCGR). If these 48840831b2SStephan Gerhold * bits are not set, CPU frequency control fails in the non-secure world. 49840831b2SStephan Gerhold */ 50840831b2SStephan Gerhold mmio_write_32(APCS_GLB, APCS_GLB_SECURE_STS_NS | APCS_GLB_SECURE_PWR_NS); 51840831b2SStephan Gerhold 52840831b2SStephan Gerhold /* Disallow non-secure access to L2 SAW2 */ 53840831b2SStephan Gerhold mmio_write_32(APCS_L2_SAW2, 0); 54840831b2SStephan Gerhold 55840831b2SStephan Gerhold /* Disallow non-secure access to CPU ACS and SAW2 */ 56840831b2SStephan Gerhold for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++) { 57840831b2SStephan Gerhold mmio_write_32(APCS_ALIAS_ACS(cpu), 0); 58840831b2SStephan Gerhold mmio_write_32(APCS_ALIAS_SAW2(cpu), 0); 59840831b2SStephan Gerhold } 60840831b2SStephan Gerhold 6145b2bd0aSStephan Gerhold #ifdef __aarch64__ 62840831b2SStephan Gerhold /* Make sure all further warm boots end up in BL31 and aarch64 state */ 63840831b2SStephan Gerhold CASSERT((BL31_BASE & 0xffff) == 0, assert_bl31_base_64k_aligned); 64840831b2SStephan Gerhold mmio_write_32(APCS_BOOT_START_ADDR_SEC, BL31_BASE | REMAP_EN); 65840831b2SStephan Gerhold mmio_write_32(APCS_AA64NAA32_REG, 1); 6645b2bd0aSStephan Gerhold #else 6745b2bd0aSStephan Gerhold /* Make sure all further warm boots end up in BL32 */ 6845b2bd0aSStephan Gerhold CASSERT((BL32_BASE & 0xffff) == 0, assert_bl32_base_64k_aligned); 6945b2bd0aSStephan Gerhold mmio_write_32(APCS_BOOT_START_ADDR_SEC, BL32_BASE | REMAP_EN); 7045b2bd0aSStephan Gerhold #endif 71840831b2SStephan Gerhold } 72840831b2SStephan Gerhold 73840831b2SStephan Gerhold /* 74840831b2SStephan Gerhold * MSM8916 has a special "interrupt aggregation logic" in the APPS SMMU, 75840831b2SStephan Gerhold * which allows routing context bank interrupts to one of 3 interrupt numbers 76840831b2SStephan Gerhold * ("TZ/HYP/NS"). Route all interrupts to the non-secure interrupt number 77840831b2SStephan Gerhold * by default to avoid special setup on the non-secure side. 78840831b2SStephan Gerhold */ 79840831b2SStephan Gerhold #define CLK_OFF BIT_32(31) 80840831b2SStephan Gerhold #define GCC_SMMU_CFG_CBCR (GCC_BASE + 0x12038) 81840831b2SStephan Gerhold #define GCC_APCS_SMMU_CLOCK_BRANCH_ENA_VOTE (GCC_BASE + 0x4500c) 82840831b2SStephan Gerhold #define SMMU_CFG_CLK_ENA BIT_32(12) 83840831b2SStephan Gerhold #define APPS_SMMU_INTR_SEL_NS (APPS_SMMU_QCOM + 0x2000) 84840831b2SStephan Gerhold #define APPS_SMMU_INTR_SEL_NS_EN_ALL U(0xffffffff) 85840831b2SStephan Gerhold 86840831b2SStephan Gerhold static void msm8916_configure_smmu(void) 87840831b2SStephan Gerhold { 88840831b2SStephan Gerhold /* Enable SMMU configuration clock to enable register access */ 89840831b2SStephan Gerhold mmio_setbits_32(GCC_APCS_SMMU_CLOCK_BRANCH_ENA_VOTE, SMMU_CFG_CLK_ENA); 90*b9072a34SStephan Gerhold while (mmio_read_32(GCC_SMMU_CFG_CBCR) & CLK_OFF) { 91*b9072a34SStephan Gerhold } 92840831b2SStephan Gerhold 93840831b2SStephan Gerhold /* Route all context bank interrupts to non-secure interrupt */ 94840831b2SStephan Gerhold mmio_write_32(APPS_SMMU_INTR_SEL_NS, APPS_SMMU_INTR_SEL_NS_EN_ALL); 95840831b2SStephan Gerhold 96840831b2SStephan Gerhold /* Disable configuration clock again */ 97840831b2SStephan Gerhold mmio_clrbits_32(GCC_APCS_SMMU_CLOCK_BRANCH_ENA_VOTE, SMMU_CFG_CLK_ENA); 98840831b2SStephan Gerhold } 99840831b2SStephan Gerhold 100840831b2SStephan Gerhold void msm8916_configure(void) 101840831b2SStephan Gerhold { 102840831b2SStephan Gerhold msm8916_gicv2_configure(); 103840831b2SStephan Gerhold msm8916_configure_timer(); 104840831b2SStephan Gerhold msm8916_configure_cpu_pm(); 105840831b2SStephan Gerhold msm8916_configure_smmu(); 106840831b2SStephan Gerhold } 107