xref: /rk3399_ARM-atf/plat/imx/imx8m/imx8mq/imx8mq_bl31_setup.c (revision e75a3b6e89c4bce11d1885426f22262def9bd664)
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>
2981136819SBai Ping #include <plat_imx8.h>
3081136819SBai Ping 
31a18e3933SJi Luo #define TRUSTY_PARAMS_LEN_BYTES      (4096*2)
32a18e3933SJi Luo 
33*e75a3b6eSAndre Przywara /*
34*e75a3b6eSAndre Przywara  * Avoid the pointer dereference of the canonical mmio_read_8() implementation.
35*e75a3b6eSAndre Przywara  * This prevents the compiler from mis-interpreting the MMIO access as an
36*e75a3b6eSAndre Przywara  * illegal memory access to a very low address (the IMX ROM is mapped at 0).
37*e75a3b6eSAndre Przywara  */
38*e75a3b6eSAndre Przywara static uint8_t mmio_read_8_ldrb(uintptr_t address)
39*e75a3b6eSAndre Przywara {
40*e75a3b6eSAndre Przywara 	uint8_t reg;
41*e75a3b6eSAndre Przywara 
42*e75a3b6eSAndre Przywara 	__asm__ volatile ("ldrb %w0, [%1]" : "=r" (reg) : "r" (address));
43*e75a3b6eSAndre Przywara 
44*e75a3b6eSAndre Przywara 	return reg;
45*e75a3b6eSAndre Przywara }
46*e75a3b6eSAndre Przywara 
4781136819SBai Ping static const mmap_region_t imx_mmap[] = {
4881136819SBai Ping 	MAP_REGION_FLAT(GPV_BASE, GPV_SIZE, MT_DEVICE | MT_RW), /* GPV map */
4972196cbbSLeonard Crestez 	MAP_REGION_FLAT(IMX_ROM_BASE, IMX_ROM_SIZE, MT_MEMORY | MT_RO), /* ROM map */
5081136819SBai Ping 	MAP_REGION_FLAT(IMX_AIPS_BASE, IMX_AIPS_SIZE, MT_DEVICE | MT_RW), /* AIPS map */
5181136819SBai Ping 	MAP_REGION_FLAT(IMX_GIC_BASE, IMX_GIC_SIZE, MT_DEVICE | MT_RW), /* GIC map */
52dd108c3cSJacky Bai 	MAP_REGION_FLAT(IMX_DDRPHY_BASE, IMX_DDR_IPS_SIZE, MT_DEVICE | MT_RW), /* DDRMIX map */
53dd108c3cSJacky Bai 	MAP_REGION_FLAT(IMX_DRAM_BASE, IMX_DRAM_SIZE, MT_MEMORY | MT_RW | MT_NS),
5481136819SBai Ping 	{0},
5581136819SBai Ping };
5681136819SBai Ping 
57ac166f64SJacky Bai static const struct aipstz_cfg aipstz[] = {
58ac166f64SJacky Bai 	{AIPSTZ1_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
59ac166f64SJacky Bai 	{AIPSTZ2_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
60ac166f64SJacky Bai 	{AIPSTZ3_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
61ac166f64SJacky Bai 	{AIPSTZ4_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
62ac166f64SJacky Bai 	{0},
63ac166f64SJacky Bai };
64ac166f64SJacky Bai 
6581136819SBai Ping static entry_point_info_t bl32_image_ep_info;
6681136819SBai Ping static entry_point_info_t bl33_image_ep_info;
6781136819SBai Ping 
6872196cbbSLeonard Crestez static uint32_t imx_soc_revision;
6972196cbbSLeonard Crestez 
7072196cbbSLeonard Crestez int imx_soc_info_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2,
7172196cbbSLeonard Crestez 				u_register_t x3)
7272196cbbSLeonard Crestez {
7372196cbbSLeonard Crestez 	return imx_soc_revision;
7472196cbbSLeonard Crestez }
7572196cbbSLeonard Crestez 
7672196cbbSLeonard Crestez #define ANAMIX_DIGPROG		0x6c
7772196cbbSLeonard Crestez #define ROM_SOC_INFO_A0		0x800
7872196cbbSLeonard Crestez #define ROM_SOC_INFO_B0		0x83C
7972196cbbSLeonard Crestez #define OCOTP_SOC_INFO_B1	0x40
8072196cbbSLeonard Crestez 
8172196cbbSLeonard Crestez static void imx8mq_soc_info_init(void)
8272196cbbSLeonard Crestez {
8372196cbbSLeonard Crestez 	uint32_t rom_version;
8472196cbbSLeonard Crestez 	uint32_t ocotp_val;
8572196cbbSLeonard Crestez 
8672196cbbSLeonard Crestez 	imx_soc_revision = mmio_read_32(IMX_ANAMIX_BASE + ANAMIX_DIGPROG);
87*e75a3b6eSAndre Przywara 	rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_A0);
8872196cbbSLeonard Crestez 	if (rom_version == 0x10)
8972196cbbSLeonard Crestez 		return;
9072196cbbSLeonard Crestez 
91*e75a3b6eSAndre Przywara 	rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_B0);
9272196cbbSLeonard Crestez 	if (rom_version == 0x20) {
9372196cbbSLeonard Crestez 		imx_soc_revision &= ~0xff;
9472196cbbSLeonard Crestez 		imx_soc_revision |= rom_version;
9572196cbbSLeonard Crestez 		return;
9672196cbbSLeonard Crestez 	}
9772196cbbSLeonard Crestez 
9872196cbbSLeonard Crestez 	/* 0xff0055aa is magic number for B1 */
9972196cbbSLeonard Crestez 	ocotp_val = mmio_read_32(IMX_OCOTP_BASE + OCOTP_SOC_INFO_B1);
10072196cbbSLeonard Crestez 	if (ocotp_val == 0xff0055aa) {
10172196cbbSLeonard Crestez 		imx_soc_revision &= ~0xff;
10299475c5dSYe Li 		if (rom_version == 0x22) {
10399475c5dSYe Li 			imx_soc_revision |= 0x22;
10499475c5dSYe Li 		} else {
10572196cbbSLeonard Crestez 			imx_soc_revision |= 0x21;
10699475c5dSYe Li 		}
10772196cbbSLeonard Crestez 		return;
10872196cbbSLeonard Crestez 	}
10972196cbbSLeonard Crestez }
11072196cbbSLeonard Crestez 
11181136819SBai Ping /* get SPSR for BL33 entry */
11281136819SBai Ping static uint32_t get_spsr_for_bl33_entry(void)
11381136819SBai Ping {
11481136819SBai Ping 	unsigned long el_status;
11581136819SBai Ping 	unsigned long mode;
11681136819SBai Ping 	uint32_t spsr;
11781136819SBai Ping 
11881136819SBai Ping 	/* figure out what mode we enter the non-secure world */
11981136819SBai Ping 	el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
12081136819SBai Ping 	el_status &= ID_AA64PFR0_ELX_MASK;
12181136819SBai Ping 
12281136819SBai Ping 	mode = (el_status) ? MODE_EL2 : MODE_EL1;
12381136819SBai Ping 
12481136819SBai Ping 	spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
12581136819SBai Ping 	return spsr;
12681136819SBai Ping }
12781136819SBai Ping 
12881136819SBai Ping static void bl31_tz380_setup(void)
12981136819SBai Ping {
13081136819SBai Ping 	unsigned int val;
13181136819SBai Ping 
13281136819SBai Ping 	val = mmio_read_32(IMX_IOMUX_GPR_BASE + IOMUXC_GPR10);
13381136819SBai Ping 	if ((val & GPR_TZASC_EN) != GPR_TZASC_EN)
13481136819SBai Ping 		return;
13581136819SBai Ping 
13681136819SBai Ping 	tzc380_init(IMX_TZASC_BASE);
13781136819SBai Ping 	/*
13881136819SBai Ping 	 * Need to substact offset 0x40000000 from CPU address when
13981136819SBai Ping 	 * programming tzasc region for i.mx8mq. Enable 1G-5G S/NS RW
14081136819SBai Ping 	 */
14181136819SBai Ping 	tzc380_configure_region(0, 0x00000000, TZC_ATTR_REGION_SIZE(TZC_REGION_SIZE_4G) |
14281136819SBai Ping 				TZC_ATTR_REGION_EN_MASK | TZC_ATTR_SP_ALL);
14381136819SBai Ping }
14481136819SBai Ping 
14581136819SBai Ping void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
14681136819SBai Ping 			u_register_t arg2, u_register_t arg3)
14781136819SBai Ping {
14836be1086SLucas Stach 	static console_t console;
14981136819SBai Ping 	int i;
15081136819SBai Ping 	/* enable CSU NS access permission */
15181136819SBai Ping 	for (i = 0; i < 64; i++) {
15281136819SBai Ping 		mmio_write_32(IMX_CSU_BASE + i * 4, 0xffffffff);
15381136819SBai Ping 	}
15481136819SBai Ping 
155ac166f64SJacky Bai 	imx_aipstz_init(aipstz);
156ac166f64SJacky Bai 
1572e8ab4f5SAnson Huang 	console_imx_uart_register(IMX_BOOT_UART_BASE, IMX_BOOT_UART_CLK_IN_HZ,
15881136819SBai Ping 		IMX_CONSOLE_BAUDRATE, &console);
15936be1086SLucas Stach 	/* This console is only used for boot stage */
16036be1086SLucas Stach 	console_set_scope(&console, CONSOLE_FLAG_BOOT);
161901d74b2SAndrey Zhizhikin 
162901d74b2SAndrey Zhizhikin 	imx8m_caam_init();
163901d74b2SAndrey Zhizhikin 
16481136819SBai Ping 	/*
16581136819SBai Ping 	 * tell BL3-1 where the non-secure software image is located
16681136819SBai Ping 	 * and the entry state information.
16781136819SBai Ping 	 */
16881136819SBai Ping 	bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET;
16981136819SBai Ping 	bl33_image_ep_info.spsr = get_spsr_for_bl33_entry();
17081136819SBai Ping 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
17181136819SBai Ping 
172a18e3933SJi Luo #if defined(SPD_opteed) || defined(SPD_trusty)
173abb6fee6SJacky Bai 	/* Populate entry point information for BL32 */
174abb6fee6SJacky Bai 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
175abb6fee6SJacky Bai 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
176abb6fee6SJacky Bai 	bl32_image_ep_info.pc = BL32_BASE;
177abb6fee6SJacky Bai 	bl32_image_ep_info.spsr = 0;
178abb6fee6SJacky Bai 
179abb6fee6SJacky Bai 	/* Pass TEE base and size to bl33 */
180abb6fee6SJacky Bai 	bl33_image_ep_info.args.arg1 = BL32_BASE;
181abb6fee6SJacky Bai 	bl33_image_ep_info.args.arg2 = BL32_SIZE;
182023750c6SSilvano di Ninno 
183023750c6SSilvano di Ninno #ifdef SPD_trusty
184023750c6SSilvano di Ninno 	bl32_image_ep_info.args.arg0 = BL32_SIZE;
185023750c6SSilvano di Ninno 	bl32_image_ep_info.args.arg1 = BL32_BASE;
186023750c6SSilvano di Ninno #else
187023750c6SSilvano di Ninno 	/* Make sure memory is clean */
188023750c6SSilvano di Ninno 	mmio_write_32(BL32_FDT_OVERLAY_ADDR, 0);
189023750c6SSilvano di Ninno 	bl33_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
190023750c6SSilvano di Ninno 	bl32_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
191023750c6SSilvano di Ninno #endif
192abb6fee6SJacky Bai #endif
193abb6fee6SJacky Bai 
19481136819SBai Ping 	bl31_tz380_setup();
19581136819SBai Ping }
19681136819SBai Ping 
19781136819SBai Ping void bl31_plat_arch_setup(void)
19881136819SBai Ping {
199c0fb8874SLucas Stach 	const mmap_region_t bl_regions[] = {
2008cfa94b7SLucas Stach 		MAP_REGION_FLAT(BL31_START, BL31_SIZE,
201c0fb8874SLucas Stach 				MT_MEMORY | MT_RW | MT_SECURE),
202c0fb8874SLucas Stach 		MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
203c0fb8874SLucas Stach 				MT_MEMORY | MT_RO | MT_SECURE),
20481136819SBai Ping #if USE_COHERENT_MEM
205c0fb8874SLucas Stach 		MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
206b05631afSJacky Bai 				BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
207c0fb8874SLucas Stach 				MT_DEVICE | MT_RW | MT_SECURE),
20881136819SBai Ping #endif
209c0fb8874SLucas Stach 		/* Map TEE memory */
210c0fb8874SLucas Stach 		MAP_REGION_FLAT(BL32_BASE, BL32_SIZE, MT_MEMORY | MT_RW),
211c0fb8874SLucas Stach 		{0},
212c0fb8874SLucas Stach 	};
213c0fb8874SLucas Stach 
214c0fb8874SLucas Stach 	setup_page_tables(bl_regions, imx_mmap);
21581136819SBai Ping 	/* enable the MMU */
21681136819SBai Ping 	enable_mmu_el3(0);
21781136819SBai Ping }
21881136819SBai Ping 
21981136819SBai Ping void bl31_platform_setup(void)
22081136819SBai Ping {
221e8837b0aSJacky Bai 	generic_delay_timer_init();
222e8837b0aSJacky Bai 
22381136819SBai Ping 	/* init the GICv3 cpu and distributor interface */
22481136819SBai Ping 	plat_gic_driver_init();
22581136819SBai Ping 	plat_gic_init();
22681136819SBai Ping 
22772196cbbSLeonard Crestez 	/* determine SOC revision for erratas */
22872196cbbSLeonard Crestez 	imx8mq_soc_info_init();
22972196cbbSLeonard Crestez 
23081136819SBai Ping 	/* gpc init */
23181136819SBai Ping 	imx_gpc_init();
232dd108c3cSJacky Bai 
233dd108c3cSJacky Bai 	dram_info_init(SAVED_DRAM_TIMING_BASE);
23481136819SBai Ping }
23581136819SBai Ping 
23681136819SBai Ping entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type)
23781136819SBai Ping {
23881136819SBai Ping 	if (type == NON_SECURE)
23981136819SBai Ping 		return &bl33_image_ep_info;
24081136819SBai Ping 	if (type == SECURE)
24181136819SBai Ping 		return &bl32_image_ep_info;
24281136819SBai Ping 
24381136819SBai Ping 	return NULL;
24481136819SBai Ping }
24581136819SBai Ping 
24681136819SBai Ping unsigned int plat_get_syscnt_freq2(void)
24781136819SBai Ping {
24881136819SBai Ping 	return COUNTER_FREQUENCY;
24981136819SBai Ping }
25081136819SBai Ping 
251a18e3933SJi Luo #ifdef SPD_trusty
252a18e3933SJi Luo void plat_trusty_set_boot_args(aapcs64_params_t *args)
253a18e3933SJi Luo {
254a18e3933SJi Luo 	args->arg0 = BL32_SIZE;
255a18e3933SJi Luo 	args->arg1 = BL32_BASE;
256a18e3933SJi Luo 	args->arg2 = TRUSTY_PARAMS_LEN_BYTES;
257a18e3933SJi Luo }
258a18e3933SJi Luo #endif
259