181136819SBai Ping /* 299475c5dSYe Li * Copyright (c) 2018-2023, ARM Limited and Contributors. All rights reserved. 381136819SBai Ping * 481136819SBai Ping * SPDX-License-Identifier: BSD-3-Clause 581136819SBai Ping */ 681136819SBai Ping 781136819SBai Ping #include <assert.h> 809d40e0eSAntonio Nino Diaz #include <stdbool.h> 909d40e0eSAntonio Nino Diaz 1009d40e0eSAntonio Nino Diaz #include <platform_def.h> 1109d40e0eSAntonio Nino Diaz 1209d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1309d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 1409d40e0eSAntonio Nino Diaz #include <common/debug.h> 1581136819SBai Ping #include <context.h> 1609d40e0eSAntonio Nino Diaz #include <drivers/arm/tzc380.h> 1709d40e0eSAntonio Nino Diaz #include <drivers/console.h> 18e8837b0aSJacky Bai #include <drivers/generic_delay_timer.h> 1909d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 2009d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 214f8d5b01SJi Luo #include <lib/xlat_tables/xlat_tables_v2.h> 2209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2309d40e0eSAntonio Nino Diaz 24dd108c3cSJacky Bai #include <dram.h> 2581136819SBai Ping #include <gpc.h> 26ac166f64SJacky Bai #include <imx_aipstz.h> 2781136819SBai Ping #include <imx_uart.h> 282502709fSJacky Bai #include <imx8m_caam.h> 29*52ee8173SLeonard Göhrs #include <imx8m_ccm.h> 3081136819SBai Ping #include <plat_imx8.h> 3181136819SBai Ping 32a18e3933SJi Luo #define TRUSTY_PARAMS_LEN_BYTES (4096*2) 33a18e3933SJi Luo 34e75a3b6eSAndre Przywara /* 35e75a3b6eSAndre Przywara * Avoid the pointer dereference of the canonical mmio_read_8() implementation. 36e75a3b6eSAndre Przywara * This prevents the compiler from mis-interpreting the MMIO access as an 37e75a3b6eSAndre Przywara * illegal memory access to a very low address (the IMX ROM is mapped at 0). 38e75a3b6eSAndre Przywara */ 39e75a3b6eSAndre Przywara static uint8_t mmio_read_8_ldrb(uintptr_t address) 40e75a3b6eSAndre Przywara { 41e75a3b6eSAndre Przywara uint8_t reg; 42e75a3b6eSAndre Przywara 43e75a3b6eSAndre Przywara __asm__ volatile ("ldrb %w0, [%1]" : "=r" (reg) : "r" (address)); 44e75a3b6eSAndre Przywara 45e75a3b6eSAndre Przywara return reg; 46e75a3b6eSAndre Przywara } 47e75a3b6eSAndre Przywara 4881136819SBai Ping static const mmap_region_t imx_mmap[] = { 4981136819SBai Ping MAP_REGION_FLAT(GPV_BASE, GPV_SIZE, MT_DEVICE | MT_RW), /* GPV map */ 5072196cbbSLeonard Crestez MAP_REGION_FLAT(IMX_ROM_BASE, IMX_ROM_SIZE, MT_MEMORY | MT_RO), /* ROM map */ 5181136819SBai Ping MAP_REGION_FLAT(IMX_AIPS_BASE, IMX_AIPS_SIZE, MT_DEVICE | MT_RW), /* AIPS map */ 5281136819SBai Ping MAP_REGION_FLAT(IMX_GIC_BASE, IMX_GIC_SIZE, MT_DEVICE | MT_RW), /* GIC map */ 53dd108c3cSJacky Bai MAP_REGION_FLAT(IMX_DDRPHY_BASE, IMX_DDR_IPS_SIZE, MT_DEVICE | MT_RW), /* DDRMIX map */ 54dd108c3cSJacky Bai MAP_REGION_FLAT(IMX_DRAM_BASE, IMX_DRAM_SIZE, MT_MEMORY | MT_RW | MT_NS), 5581136819SBai Ping {0}, 5681136819SBai Ping }; 5781136819SBai Ping 58ac166f64SJacky Bai static const struct aipstz_cfg aipstz[] = { 59ac166f64SJacky Bai {AIPSTZ1_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, }, 60ac166f64SJacky Bai {AIPSTZ2_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, }, 61ac166f64SJacky Bai {AIPSTZ3_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, }, 62ac166f64SJacky Bai {AIPSTZ4_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, }, 63ac166f64SJacky Bai {0}, 64ac166f64SJacky Bai }; 65ac166f64SJacky Bai 6681136819SBai Ping static entry_point_info_t bl32_image_ep_info; 6781136819SBai Ping static entry_point_info_t bl33_image_ep_info; 6881136819SBai Ping 6972196cbbSLeonard Crestez static uint32_t imx_soc_revision; 7072196cbbSLeonard Crestez 7172196cbbSLeonard Crestez int imx_soc_info_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, 7272196cbbSLeonard Crestez u_register_t x3) 7372196cbbSLeonard Crestez { 7472196cbbSLeonard Crestez return imx_soc_revision; 7572196cbbSLeonard Crestez } 7672196cbbSLeonard Crestez 7772196cbbSLeonard Crestez #define ANAMIX_DIGPROG 0x6c 7872196cbbSLeonard Crestez #define ROM_SOC_INFO_A0 0x800 7972196cbbSLeonard Crestez #define ROM_SOC_INFO_B0 0x83C 8072196cbbSLeonard Crestez #define OCOTP_SOC_INFO_B1 0x40 8172196cbbSLeonard Crestez 8272196cbbSLeonard Crestez static void imx8mq_soc_info_init(void) 8372196cbbSLeonard Crestez { 8472196cbbSLeonard Crestez uint32_t rom_version; 8572196cbbSLeonard Crestez uint32_t ocotp_val; 8672196cbbSLeonard Crestez 8772196cbbSLeonard Crestez imx_soc_revision = mmio_read_32(IMX_ANAMIX_BASE + ANAMIX_DIGPROG); 88e75a3b6eSAndre Przywara rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_A0); 8972196cbbSLeonard Crestez if (rom_version == 0x10) 9072196cbbSLeonard Crestez return; 9172196cbbSLeonard Crestez 92e75a3b6eSAndre Przywara rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_B0); 9372196cbbSLeonard Crestez if (rom_version == 0x20) { 9472196cbbSLeonard Crestez imx_soc_revision &= ~0xff; 9572196cbbSLeonard Crestez imx_soc_revision |= rom_version; 9672196cbbSLeonard Crestez return; 9772196cbbSLeonard Crestez } 9872196cbbSLeonard Crestez 9972196cbbSLeonard Crestez /* 0xff0055aa is magic number for B1 */ 10072196cbbSLeonard Crestez ocotp_val = mmio_read_32(IMX_OCOTP_BASE + OCOTP_SOC_INFO_B1); 10172196cbbSLeonard Crestez if (ocotp_val == 0xff0055aa) { 10272196cbbSLeonard Crestez imx_soc_revision &= ~0xff; 10399475c5dSYe Li if (rom_version == 0x22) { 10499475c5dSYe Li imx_soc_revision |= 0x22; 10599475c5dSYe Li } else { 10672196cbbSLeonard Crestez imx_soc_revision |= 0x21; 10799475c5dSYe Li } 10872196cbbSLeonard Crestez return; 10972196cbbSLeonard Crestez } 11072196cbbSLeonard Crestez } 11172196cbbSLeonard Crestez 11281136819SBai Ping /* get SPSR for BL33 entry */ 11381136819SBai Ping static uint32_t get_spsr_for_bl33_entry(void) 11481136819SBai Ping { 11581136819SBai Ping unsigned long el_status; 11681136819SBai Ping unsigned long mode; 11781136819SBai Ping uint32_t spsr; 11881136819SBai Ping 11981136819SBai Ping /* figure out what mode we enter the non-secure world */ 12081136819SBai Ping el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT; 12181136819SBai Ping el_status &= ID_AA64PFR0_ELX_MASK; 12281136819SBai Ping 12381136819SBai Ping mode = (el_status) ? MODE_EL2 : MODE_EL1; 12481136819SBai Ping 12581136819SBai Ping spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 12681136819SBai Ping return spsr; 12781136819SBai Ping } 12881136819SBai Ping 12981136819SBai Ping static void bl31_tz380_setup(void) 13081136819SBai Ping { 13181136819SBai Ping unsigned int val; 13281136819SBai Ping 13381136819SBai Ping val = mmio_read_32(IMX_IOMUX_GPR_BASE + IOMUXC_GPR10); 13481136819SBai Ping if ((val & GPR_TZASC_EN) != GPR_TZASC_EN) 13581136819SBai Ping return; 13681136819SBai Ping 13781136819SBai Ping tzc380_init(IMX_TZASC_BASE); 13881136819SBai Ping /* 13981136819SBai Ping * Need to substact offset 0x40000000 from CPU address when 14081136819SBai Ping * programming tzasc region for i.mx8mq. Enable 1G-5G S/NS RW 14181136819SBai Ping */ 14281136819SBai Ping tzc380_configure_region(0, 0x00000000, TZC_ATTR_REGION_SIZE(TZC_REGION_SIZE_4G) | 14381136819SBai Ping TZC_ATTR_REGION_EN_MASK | TZC_ATTR_SP_ALL); 14481136819SBai Ping } 14581136819SBai Ping 14681136819SBai Ping void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, 14781136819SBai Ping u_register_t arg2, u_register_t arg3) 14881136819SBai Ping { 149*52ee8173SLeonard Göhrs unsigned int console_base = IMX_BOOT_UART_BASE; 15036be1086SLucas Stach static console_t console; 15181136819SBai Ping int i; 15281136819SBai Ping /* enable CSU NS access permission */ 15381136819SBai Ping for (i = 0; i < 64; i++) { 15481136819SBai Ping mmio_write_32(IMX_CSU_BASE + i * 4, 0xffffffff); 15581136819SBai Ping } 15681136819SBai Ping 157ac166f64SJacky Bai imx_aipstz_init(aipstz); 158ac166f64SJacky Bai 159*52ee8173SLeonard Göhrs if (console_base == 0U) { 160*52ee8173SLeonard Göhrs console_base = imx8m_uart_get_base(); 161*52ee8173SLeonard Göhrs } 162*52ee8173SLeonard Göhrs 163*52ee8173SLeonard Göhrs console_imx_uart_register(console_base, IMX_BOOT_UART_CLK_IN_HZ, 16481136819SBai Ping IMX_CONSOLE_BAUDRATE, &console); 16536be1086SLucas Stach /* This console is only used for boot stage */ 16636be1086SLucas Stach console_set_scope(&console, CONSOLE_FLAG_BOOT); 167901d74b2SAndrey Zhizhikin 168901d74b2SAndrey Zhizhikin imx8m_caam_init(); 169901d74b2SAndrey Zhizhikin 17081136819SBai Ping /* 17181136819SBai Ping * tell BL3-1 where the non-secure software image is located 17281136819SBai Ping * and the entry state information. 17381136819SBai Ping */ 17481136819SBai Ping bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET; 17581136819SBai Ping bl33_image_ep_info.spsr = get_spsr_for_bl33_entry(); 17681136819SBai Ping SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE); 17781136819SBai Ping 178a18e3933SJi Luo #if defined(SPD_opteed) || defined(SPD_trusty) 179abb6fee6SJacky Bai /* Populate entry point information for BL32 */ 180abb6fee6SJacky Bai SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0); 181abb6fee6SJacky Bai SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE); 182abb6fee6SJacky Bai bl32_image_ep_info.pc = BL32_BASE; 183abb6fee6SJacky Bai bl32_image_ep_info.spsr = 0; 184abb6fee6SJacky Bai 185abb6fee6SJacky Bai /* Pass TEE base and size to bl33 */ 186abb6fee6SJacky Bai bl33_image_ep_info.args.arg1 = BL32_BASE; 187abb6fee6SJacky Bai bl33_image_ep_info.args.arg2 = BL32_SIZE; 188023750c6SSilvano di Ninno 189023750c6SSilvano di Ninno #ifdef SPD_trusty 190023750c6SSilvano di Ninno bl32_image_ep_info.args.arg0 = BL32_SIZE; 191023750c6SSilvano di Ninno bl32_image_ep_info.args.arg1 = BL32_BASE; 192023750c6SSilvano di Ninno #else 193023750c6SSilvano di Ninno /* Make sure memory is clean */ 194023750c6SSilvano di Ninno mmio_write_32(BL32_FDT_OVERLAY_ADDR, 0); 195023750c6SSilvano di Ninno bl33_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR; 196023750c6SSilvano di Ninno bl32_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR; 197023750c6SSilvano di Ninno #endif 198abb6fee6SJacky Bai #endif 199abb6fee6SJacky Bai 20081136819SBai Ping bl31_tz380_setup(); 20181136819SBai Ping } 20281136819SBai Ping 20381136819SBai Ping void bl31_plat_arch_setup(void) 20481136819SBai Ping { 205c0fb8874SLucas Stach const mmap_region_t bl_regions[] = { 2068cfa94b7SLucas Stach MAP_REGION_FLAT(BL31_START, BL31_SIZE, 207c0fb8874SLucas Stach MT_MEMORY | MT_RW | MT_SECURE), 208c0fb8874SLucas Stach MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE, 209c0fb8874SLucas Stach MT_MEMORY | MT_RO | MT_SECURE), 21081136819SBai Ping #if USE_COHERENT_MEM 211c0fb8874SLucas Stach MAP_REGION_FLAT(BL_COHERENT_RAM_BASE, 212b05631afSJacky Bai BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE, 213c0fb8874SLucas Stach MT_DEVICE | MT_RW | MT_SECURE), 21481136819SBai Ping #endif 215c0fb8874SLucas Stach /* Map TEE memory */ 216c0fb8874SLucas Stach MAP_REGION_FLAT(BL32_BASE, BL32_SIZE, MT_MEMORY | MT_RW), 217c0fb8874SLucas Stach {0}, 218c0fb8874SLucas Stach }; 219c0fb8874SLucas Stach 220c0fb8874SLucas Stach setup_page_tables(bl_regions, imx_mmap); 22181136819SBai Ping /* enable the MMU */ 22281136819SBai Ping enable_mmu_el3(0); 22381136819SBai Ping } 22481136819SBai Ping 22581136819SBai Ping void bl31_platform_setup(void) 22681136819SBai Ping { 227e8837b0aSJacky Bai generic_delay_timer_init(); 228e8837b0aSJacky Bai 22981136819SBai Ping /* init the GICv3 cpu and distributor interface */ 23081136819SBai Ping plat_gic_driver_init(); 23181136819SBai Ping plat_gic_init(); 23281136819SBai Ping 23372196cbbSLeonard Crestez /* determine SOC revision for erratas */ 23472196cbbSLeonard Crestez imx8mq_soc_info_init(); 23572196cbbSLeonard Crestez 23681136819SBai Ping /* gpc init */ 23781136819SBai Ping imx_gpc_init(); 238dd108c3cSJacky Bai 239dd108c3cSJacky Bai dram_info_init(SAVED_DRAM_TIMING_BASE); 24081136819SBai Ping } 24181136819SBai Ping 24281136819SBai Ping entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type) 24381136819SBai Ping { 24481136819SBai Ping if (type == NON_SECURE) 24581136819SBai Ping return &bl33_image_ep_info; 24681136819SBai Ping if (type == SECURE) 24781136819SBai Ping return &bl32_image_ep_info; 24881136819SBai Ping 24981136819SBai Ping return NULL; 25081136819SBai Ping } 25181136819SBai Ping 25281136819SBai Ping unsigned int plat_get_syscnt_freq2(void) 25381136819SBai Ping { 25481136819SBai Ping return COUNTER_FREQUENCY; 25581136819SBai Ping } 25681136819SBai Ping 257a18e3933SJi Luo #ifdef SPD_trusty 258a18e3933SJi Luo void plat_trusty_set_boot_args(aapcs64_params_t *args) 259a18e3933SJi Luo { 260a18e3933SJi Luo args->arg0 = BL32_SIZE; 261a18e3933SJi Luo args->arg1 = BL32_BASE; 262a18e3933SJi Luo args->arg2 = TRUSTY_PARAMS_LEN_BYTES; 263a18e3933SJi Luo } 264a18e3933SJi Luo #endif 265