xref: /rk3399_ARM-atf/plat/imx/imx8m/imx8mq/imx8mq_bl31_setup.c (revision 613892cfefaba655cfa6548488d086be0a2f3b47)
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>
2952ee8173SLeonard 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  */
mmio_read_8_ldrb(uintptr_t address)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),
553a36f70bSJacky Bai 	MAP_REGION_FLAT(IMX_CAAM_RAM_BASE, IMX_CAAM_RAM_SIZE, MT_MEMORY | MT_RW), /* CAMM RAM */
563a36f70bSJacky Bai 	MAP_REGION_FLAT(IMX_NS_OCRAM_BASE, IMX_NS_OCRAM_SIZE, MT_MEMORY | MT_RW), /* NS OCRAM */
5781136819SBai Ping 	{0},
5881136819SBai Ping };
5981136819SBai Ping 
60ac166f64SJacky Bai static const struct aipstz_cfg aipstz[] = {
61ac166f64SJacky Bai 	{AIPSTZ1_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
62ac166f64SJacky Bai 	{AIPSTZ2_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
63ac166f64SJacky Bai 	{AIPSTZ3_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
64ac166f64SJacky Bai 	{AIPSTZ4_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
65ac166f64SJacky Bai 	{0},
66ac166f64SJacky Bai };
67ac166f64SJacky Bai 
6881136819SBai Ping static entry_point_info_t bl32_image_ep_info;
6981136819SBai Ping static entry_point_info_t bl33_image_ep_info;
7081136819SBai Ping 
7172196cbbSLeonard Crestez static uint32_t imx_soc_revision;
7272196cbbSLeonard Crestez 
imx_soc_info_handler(uint32_t smc_fid,u_register_t x1,u_register_t x2,u_register_t x3)7372196cbbSLeonard Crestez int imx_soc_info_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2,
7472196cbbSLeonard Crestez 				u_register_t x3)
7572196cbbSLeonard Crestez {
7672196cbbSLeonard Crestez 	return imx_soc_revision;
7772196cbbSLeonard Crestez }
7872196cbbSLeonard Crestez 
7972196cbbSLeonard Crestez #define ANAMIX_DIGPROG		0x6c
8072196cbbSLeonard Crestez #define ROM_SOC_INFO_A0		0x800
8172196cbbSLeonard Crestez #define ROM_SOC_INFO_B0		0x83C
8272196cbbSLeonard Crestez #define OCOTP_SOC_INFO_B1	0x40
8372196cbbSLeonard Crestez 
imx8mq_soc_info_init(void)8472196cbbSLeonard Crestez static void imx8mq_soc_info_init(void)
8572196cbbSLeonard Crestez {
8672196cbbSLeonard Crestez 	uint32_t rom_version;
8772196cbbSLeonard Crestez 	uint32_t ocotp_val;
8872196cbbSLeonard Crestez 
8972196cbbSLeonard Crestez 	imx_soc_revision = mmio_read_32(IMX_ANAMIX_BASE + ANAMIX_DIGPROG);
90e75a3b6eSAndre Przywara 	rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_A0);
9172196cbbSLeonard Crestez 	if (rom_version == 0x10)
9272196cbbSLeonard Crestez 		return;
9372196cbbSLeonard Crestez 
94e75a3b6eSAndre Przywara 	rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_B0);
9572196cbbSLeonard Crestez 	if (rom_version == 0x20) {
9672196cbbSLeonard Crestez 		imx_soc_revision &= ~0xff;
9772196cbbSLeonard Crestez 		imx_soc_revision |= rom_version;
9872196cbbSLeonard Crestez 		return;
9972196cbbSLeonard Crestez 	}
10072196cbbSLeonard Crestez 
10172196cbbSLeonard Crestez 	/* 0xff0055aa is magic number for B1 */
10272196cbbSLeonard Crestez 	ocotp_val = mmio_read_32(IMX_OCOTP_BASE + OCOTP_SOC_INFO_B1);
10372196cbbSLeonard Crestez 	if (ocotp_val == 0xff0055aa) {
10472196cbbSLeonard Crestez 		imx_soc_revision &= ~0xff;
10599475c5dSYe Li 		if (rom_version == 0x22) {
10699475c5dSYe Li 			imx_soc_revision |= 0x22;
10799475c5dSYe Li 		} else {
10872196cbbSLeonard Crestez 			imx_soc_revision |= 0x21;
10999475c5dSYe Li 		}
11072196cbbSLeonard Crestez 		return;
11172196cbbSLeonard Crestez 	}
11272196cbbSLeonard Crestez }
11372196cbbSLeonard Crestez 
11481136819SBai Ping /* get SPSR for BL33 entry */
get_spsr_for_bl33_entry(void)11581136819SBai Ping static uint32_t get_spsr_for_bl33_entry(void)
11681136819SBai Ping {
11781136819SBai Ping 	unsigned long el_status;
11881136819SBai Ping 	unsigned long mode;
11981136819SBai Ping 	uint32_t spsr;
12081136819SBai Ping 
12181136819SBai Ping 	/* figure out what mode we enter the non-secure world */
12281136819SBai Ping 	el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
12381136819SBai Ping 	el_status &= ID_AA64PFR0_ELX_MASK;
12481136819SBai Ping 
12581136819SBai Ping 	mode = (el_status) ? MODE_EL2 : MODE_EL1;
12681136819SBai Ping 
12781136819SBai Ping 	spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
12881136819SBai Ping 	return spsr;
12981136819SBai Ping }
13081136819SBai Ping 
bl31_tz380_setup(void)13181136819SBai Ping static void bl31_tz380_setup(void)
13281136819SBai Ping {
13381136819SBai Ping 	unsigned int val;
13481136819SBai Ping 
13581136819SBai Ping 	val = mmio_read_32(IMX_IOMUX_GPR_BASE + IOMUXC_GPR10);
13681136819SBai Ping 	if ((val & GPR_TZASC_EN) != GPR_TZASC_EN)
13781136819SBai Ping 		return;
13881136819SBai Ping 
13981136819SBai Ping 	tzc380_init(IMX_TZASC_BASE);
14081136819SBai Ping 	/*
14181136819SBai Ping 	 * Need to substact offset 0x40000000 from CPU address when
14281136819SBai Ping 	 * programming tzasc region for i.mx8mq. Enable 1G-5G S/NS RW
14381136819SBai Ping 	 */
14481136819SBai Ping 	tzc380_configure_region(0, 0x00000000, TZC_ATTR_REGION_SIZE(TZC_REGION_SIZE_4G) |
14581136819SBai Ping 				TZC_ATTR_REGION_EN_MASK | TZC_ATTR_SP_ALL);
14681136819SBai Ping }
14781136819SBai Ping 
bl31_early_platform_setup2(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)14881136819SBai Ping void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
14981136819SBai Ping 			u_register_t arg2, u_register_t arg3)
15081136819SBai Ping {
15152ee8173SLeonard Göhrs 	unsigned int console_base = IMX_BOOT_UART_BASE;
15236be1086SLucas Stach 	static console_t console;
15381136819SBai Ping 	int i;
15481136819SBai Ping 	/* enable CSU NS access permission */
15581136819SBai Ping 	for (i = 0; i < 64; i++) {
15681136819SBai Ping 		mmio_write_32(IMX_CSU_BASE + i * 4, 0xffffffff);
15781136819SBai Ping 	}
15881136819SBai Ping 
159ac166f64SJacky Bai 	imx_aipstz_init(aipstz);
160ac166f64SJacky Bai 
16152ee8173SLeonard Göhrs 	if (console_base == 0U) {
16252ee8173SLeonard Göhrs 		console_base = imx8m_uart_get_base();
16352ee8173SLeonard Göhrs 	}
16452ee8173SLeonard Göhrs 
16552ee8173SLeonard Göhrs 	console_imx_uart_register(console_base, IMX_BOOT_UART_CLK_IN_HZ,
16681136819SBai Ping 		IMX_CONSOLE_BAUDRATE, &console);
16736be1086SLucas Stach 	/* This console is only used for boot stage */
16836be1086SLucas Stach 	console_set_scope(&console, CONSOLE_FLAG_BOOT);
169901d74b2SAndrey Zhizhikin 
170901d74b2SAndrey Zhizhikin 	imx8m_caam_init();
171901d74b2SAndrey Zhizhikin 
17281136819SBai Ping 	/*
17381136819SBai Ping 	 * tell BL3-1 where the non-secure software image is located
17481136819SBai Ping 	 * and the entry state information.
17581136819SBai Ping 	 */
17681136819SBai Ping 	bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET;
17781136819SBai Ping 	bl33_image_ep_info.spsr = get_spsr_for_bl33_entry();
17881136819SBai Ping 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
17981136819SBai Ping 
180a18e3933SJi Luo #if defined(SPD_opteed) || defined(SPD_trusty)
181abb6fee6SJacky Bai 	/* Populate entry point information for BL32 */
182abb6fee6SJacky Bai 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
183abb6fee6SJacky Bai 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
184abb6fee6SJacky Bai 	bl32_image_ep_info.pc = BL32_BASE;
185abb6fee6SJacky Bai 	bl32_image_ep_info.spsr = 0;
186abb6fee6SJacky Bai 
187abb6fee6SJacky Bai 	/* Pass TEE base and size to bl33 */
188abb6fee6SJacky Bai 	bl33_image_ep_info.args.arg1 = BL32_BASE;
189abb6fee6SJacky Bai 	bl33_image_ep_info.args.arg2 = BL32_SIZE;
190023750c6SSilvano di Ninno 
191023750c6SSilvano di Ninno #ifdef SPD_trusty
192023750c6SSilvano di Ninno 	bl32_image_ep_info.args.arg0 = BL32_SIZE;
193023750c6SSilvano di Ninno 	bl32_image_ep_info.args.arg1 = BL32_BASE;
194023750c6SSilvano di Ninno #else
195023750c6SSilvano di Ninno 	/* Make sure memory is clean */
196023750c6SSilvano di Ninno 	mmio_write_32(BL32_FDT_OVERLAY_ADDR, 0);
197023750c6SSilvano di Ninno 	bl33_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
198023750c6SSilvano di Ninno 	bl32_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
199023750c6SSilvano di Ninno #endif
200abb6fee6SJacky Bai #endif
201abb6fee6SJacky Bai 
20281136819SBai Ping 	bl31_tz380_setup();
20381136819SBai Ping }
20481136819SBai Ping 
bl31_plat_arch_setup(void)20581136819SBai Ping void bl31_plat_arch_setup(void)
20681136819SBai Ping {
207c0fb8874SLucas Stach 	const mmap_region_t bl_regions[] = {
2088cfa94b7SLucas Stach 		MAP_REGION_FLAT(BL31_START, BL31_SIZE,
209c0fb8874SLucas Stach 				MT_MEMORY | MT_RW | MT_SECURE),
210c0fb8874SLucas Stach 		MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
211c0fb8874SLucas Stach 				MT_MEMORY | MT_RO | MT_SECURE),
212*1b65be59SJacky Bai #if SEPARATE_NOBITS_REGION
213*1b65be59SJacky Bai 		MAP_REGION_FLAT(BL_NOBITS_BASE, BL_NOBITS_END - BL_NOBITS_BASE,
214*1b65be59SJacky Bai 				MT_RW_DATA | MT_SECURE),
215*1b65be59SJacky Bai #endif
21681136819SBai Ping #if USE_COHERENT_MEM
217c0fb8874SLucas Stach 		MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
218b05631afSJacky Bai 				BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
219c0fb8874SLucas Stach 				MT_DEVICE | MT_RW | MT_SECURE),
22081136819SBai Ping #endif
221*1b65be59SJacky Bai #if defined(SPD_opteed) || defined(SPD_trusty)
222c0fb8874SLucas Stach 		/* Map TEE memory */
223c0fb8874SLucas Stach 		MAP_REGION_FLAT(BL32_BASE, BL32_SIZE, MT_MEMORY | MT_RW),
224*1b65be59SJacky Bai #endif
225c0fb8874SLucas Stach 		{0},
226c0fb8874SLucas Stach 	};
227c0fb8874SLucas Stach 
228c0fb8874SLucas Stach 	setup_page_tables(bl_regions, imx_mmap);
22981136819SBai Ping 	/* enable the MMU */
23081136819SBai Ping 	enable_mmu_el3(0);
23181136819SBai Ping }
23281136819SBai Ping 
bl31_platform_setup(void)23381136819SBai Ping void bl31_platform_setup(void)
23481136819SBai Ping {
235e8837b0aSJacky Bai 	generic_delay_timer_init();
236e8837b0aSJacky Bai 
23781136819SBai Ping 	/* init the GICv3 cpu and distributor interface */
23881136819SBai Ping 	plat_gic_driver_init();
23981136819SBai Ping 	plat_gic_init();
24081136819SBai Ping 
24172196cbbSLeonard Crestez 	/* determine SOC revision for erratas */
24272196cbbSLeonard Crestez 	imx8mq_soc_info_init();
24372196cbbSLeonard Crestez 
24481136819SBai Ping 	/* gpc init */
24581136819SBai Ping 	imx_gpc_init();
246dd108c3cSJacky Bai 
247dd108c3cSJacky Bai 	dram_info_init(SAVED_DRAM_TIMING_BASE);
24881136819SBai Ping }
24981136819SBai Ping 
bl31_plat_get_next_image_ep_info(unsigned int type)25081136819SBai Ping entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type)
25181136819SBai Ping {
25281136819SBai Ping 	if (type == NON_SECURE)
25381136819SBai Ping 		return &bl33_image_ep_info;
25481136819SBai Ping 	if (type == SECURE)
25581136819SBai Ping 		return &bl32_image_ep_info;
25681136819SBai Ping 
25781136819SBai Ping 	return NULL;
25881136819SBai Ping }
25981136819SBai Ping 
plat_get_syscnt_freq2(void)26081136819SBai Ping unsigned int plat_get_syscnt_freq2(void)
26181136819SBai Ping {
26281136819SBai Ping 	return COUNTER_FREQUENCY;
26381136819SBai Ping }
26481136819SBai Ping 
265a18e3933SJi Luo #ifdef SPD_trusty
plat_trusty_set_boot_args(aapcs64_params_t * args)266a18e3933SJi Luo void plat_trusty_set_boot_args(aapcs64_params_t *args)
267a18e3933SJi Luo {
268a18e3933SJi Luo 	args->arg0 = BL32_SIZE;
269a18e3933SJi Luo 	args->arg1 = BL32_BASE;
270a18e3933SJi Luo 	args->arg2 = TRUSTY_PARAMS_LEN_BYTES;
271a18e3933SJi Luo }
272a18e3933SJi Luo #endif
273