xref: /arm-trusted-firmware/plat/imx/imx8m/imx8mm/imx8mm_bl31_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2019-2022 ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <stdbool.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <platform_def.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu #include <arch_helpers.h>
13*91f16700Schasinglulu #include <common/bl_common.h>
14*91f16700Schasinglulu #include <common/debug.h>
15*91f16700Schasinglulu #include <context.h>
16*91f16700Schasinglulu #include <drivers/arm/tzc380.h>
17*91f16700Schasinglulu #include <drivers/console.h>
18*91f16700Schasinglulu #include <drivers/generic_delay_timer.h>
19*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
20*91f16700Schasinglulu #include <lib/mmio.h>
21*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_v2.h>
22*91f16700Schasinglulu #include <plat/common/platform.h>
23*91f16700Schasinglulu 
24*91f16700Schasinglulu #include <dram.h>
25*91f16700Schasinglulu #include <gpc.h>
26*91f16700Schasinglulu #include <imx_aipstz.h>
27*91f16700Schasinglulu #include <imx_uart.h>
28*91f16700Schasinglulu #include <imx_rdc.h>
29*91f16700Schasinglulu #include <imx8m_caam.h>
30*91f16700Schasinglulu #include <imx8m_ccm.h>
31*91f16700Schasinglulu #include <imx8m_csu.h>
32*91f16700Schasinglulu #include <imx8m_snvs.h>
33*91f16700Schasinglulu #include <plat_imx8.h>
34*91f16700Schasinglulu 
35*91f16700Schasinglulu #define TRUSTY_PARAMS_LEN_BYTES      (4096*2)
36*91f16700Schasinglulu 
37*91f16700Schasinglulu /*
38*91f16700Schasinglulu  * Note: DRAM region is mapped with entire size available and uses MT_RW
39*91f16700Schasinglulu  * attributes.
40*91f16700Schasinglulu  * See details in docs/plat/imx8m.rst "High Assurance Boot (HABv4)" section
41*91f16700Schasinglulu  * for explanation of this mapping scheme.
42*91f16700Schasinglulu  */
43*91f16700Schasinglulu static const mmap_region_t imx_mmap[] = {
44*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_GIC_BASE, IMX_GIC_SIZE, MT_DEVICE | MT_RW),
45*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_AIPS_BASE, IMX_AIPS_SIZE, MT_DEVICE | MT_RW), /* AIPS map */
46*91f16700Schasinglulu 	MAP_REGION_FLAT(OCRAM_S_BASE, OCRAM_S_SIZE, MT_DEVICE | MT_RW), /* OCRAM_S */
47*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_DDRPHY_BASE, IMX_DDR_IPS_SIZE, MT_DEVICE | MT_RW), /* DDRMIX */
48*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_VPUMIX_BASE, IMX_VPUMIX_SIZE, MT_DEVICE | MT_RW), /* VPUMIX */
49*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_CAAM_RAM_BASE, IMX_CAAM_RAM_SIZE, MT_MEMORY | MT_RW), /* CAMM RAM */
50*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_NS_OCRAM_BASE, IMX_NS_OCRAM_SIZE, MT_MEMORY | MT_RW), /* NS OCRAM */
51*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_ROM_BASE, IMX_ROM_SIZE, MT_MEMORY | MT_RO), /* ROM code */
52*91f16700Schasinglulu 	MAP_REGION_FLAT(IMX_DRAM_BASE, IMX_DRAM_SIZE, MT_MEMORY | MT_RW | MT_NS), /* DRAM */
53*91f16700Schasinglulu 	{0},
54*91f16700Schasinglulu };
55*91f16700Schasinglulu 
56*91f16700Schasinglulu static const struct aipstz_cfg aipstz[] = {
57*91f16700Schasinglulu 	{IMX_AIPSTZ1, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
58*91f16700Schasinglulu 	{IMX_AIPSTZ2, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
59*91f16700Schasinglulu 	{IMX_AIPSTZ3, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
60*91f16700Schasinglulu 	{IMX_AIPSTZ4, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
61*91f16700Schasinglulu 	{0},
62*91f16700Schasinglulu };
63*91f16700Schasinglulu 
64*91f16700Schasinglulu static const struct imx_rdc_cfg rdc[] = {
65*91f16700Schasinglulu 	/* Master domain assignment */
66*91f16700Schasinglulu 	RDC_MDAn(RDC_MDA_M4, DID1),
67*91f16700Schasinglulu 
68*91f16700Schasinglulu 	/* peripherals domain permission */
69*91f16700Schasinglulu 	RDC_PDAPn(RDC_PDAP_UART4, D1R | D1W),
70*91f16700Schasinglulu 	RDC_PDAPn(RDC_PDAP_UART2, D0R | D0W),
71*91f16700Schasinglulu 
72*91f16700Schasinglulu 	/* memory region */
73*91f16700Schasinglulu 
74*91f16700Schasinglulu 	/* Sentinel */
75*91f16700Schasinglulu 	{0},
76*91f16700Schasinglulu };
77*91f16700Schasinglulu 
78*91f16700Schasinglulu static const struct imx_csu_cfg csu_cfg[] = {
79*91f16700Schasinglulu 	/* peripherals csl setting */
80*91f16700Schasinglulu 	CSU_CSLx(0x1, CSU_SEC_LEVEL_0, UNLOCKED),
81*91f16700Schasinglulu 
82*91f16700Schasinglulu 	/* master HP0~1 */
83*91f16700Schasinglulu 
84*91f16700Schasinglulu 	/* SA setting */
85*91f16700Schasinglulu 
86*91f16700Schasinglulu 	/* HP control setting */
87*91f16700Schasinglulu 
88*91f16700Schasinglulu 	/* Sentinel */
89*91f16700Schasinglulu 	{0}
90*91f16700Schasinglulu };
91*91f16700Schasinglulu 
92*91f16700Schasinglulu static entry_point_info_t bl32_image_ep_info;
93*91f16700Schasinglulu static entry_point_info_t bl33_image_ep_info;
94*91f16700Schasinglulu 
95*91f16700Schasinglulu /* get SPSR for BL33 entry */
96*91f16700Schasinglulu static uint32_t get_spsr_for_bl33_entry(void)
97*91f16700Schasinglulu {
98*91f16700Schasinglulu 	unsigned long el_status;
99*91f16700Schasinglulu 	unsigned long mode;
100*91f16700Schasinglulu 	uint32_t spsr;
101*91f16700Schasinglulu 
102*91f16700Schasinglulu 	/* figure out what mode we enter the non-secure world */
103*91f16700Schasinglulu 	el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
104*91f16700Schasinglulu 	el_status &= ID_AA64PFR0_ELX_MASK;
105*91f16700Schasinglulu 
106*91f16700Schasinglulu 	mode = (el_status) ? MODE_EL2 : MODE_EL1;
107*91f16700Schasinglulu 
108*91f16700Schasinglulu 	spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
109*91f16700Schasinglulu 	return spsr;
110*91f16700Schasinglulu }
111*91f16700Schasinglulu 
112*91f16700Schasinglulu void bl31_tzc380_setup(void)
113*91f16700Schasinglulu {
114*91f16700Schasinglulu 	unsigned int val;
115*91f16700Schasinglulu 
116*91f16700Schasinglulu 	val = mmio_read_32(IMX_IOMUX_GPR_BASE + 0x28);
117*91f16700Schasinglulu 	if ((val & GPR_TZASC_EN) != GPR_TZASC_EN)
118*91f16700Schasinglulu 		return;
119*91f16700Schasinglulu 
120*91f16700Schasinglulu 	tzc380_init(IMX_TZASC_BASE);
121*91f16700Schasinglulu 
122*91f16700Schasinglulu 	/*
123*91f16700Schasinglulu 	 * Need to substact offset 0x40000000 from CPU address when
124*91f16700Schasinglulu 	 * programming tzasc region for i.mx8mm.
125*91f16700Schasinglulu 	 */
126*91f16700Schasinglulu 
127*91f16700Schasinglulu 	/* Enable 1G-5G S/NS RW */
128*91f16700Schasinglulu 	tzc380_configure_region(0, 0x00000000, TZC_ATTR_REGION_SIZE(TZC_REGION_SIZE_4G) |
129*91f16700Schasinglulu 		TZC_ATTR_REGION_EN_MASK | TZC_ATTR_SP_ALL);
130*91f16700Schasinglulu }
131*91f16700Schasinglulu 
132*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
133*91f16700Schasinglulu 		u_register_t arg2, u_register_t arg3)
134*91f16700Schasinglulu {
135*91f16700Schasinglulu 	unsigned int console_base = IMX_BOOT_UART_BASE;
136*91f16700Schasinglulu 	static console_t console;
137*91f16700Schasinglulu 	int i;
138*91f16700Schasinglulu 
139*91f16700Schasinglulu 	/* Enable CSU NS access permission */
140*91f16700Schasinglulu 	for (i = 0; i < 64; i++) {
141*91f16700Schasinglulu 		mmio_write_32(IMX_CSU_BASE + i * 4, 0x00ff00ff);
142*91f16700Schasinglulu 	}
143*91f16700Schasinglulu 
144*91f16700Schasinglulu 	imx_aipstz_init(aipstz);
145*91f16700Schasinglulu 
146*91f16700Schasinglulu 	imx_rdc_init(rdc);
147*91f16700Schasinglulu 
148*91f16700Schasinglulu 	imx_csu_init(csu_cfg);
149*91f16700Schasinglulu 
150*91f16700Schasinglulu 	if (console_base == 0U) {
151*91f16700Schasinglulu 		console_base = imx8m_uart_get_base();
152*91f16700Schasinglulu 	}
153*91f16700Schasinglulu 
154*91f16700Schasinglulu 	console_imx_uart_register(console_base, IMX_BOOT_UART_CLK_IN_HZ,
155*91f16700Schasinglulu 		IMX_CONSOLE_BAUDRATE, &console);
156*91f16700Schasinglulu 	/* This console is only used for boot stage */
157*91f16700Schasinglulu 	console_set_scope(&console, CONSOLE_FLAG_BOOT);
158*91f16700Schasinglulu 
159*91f16700Schasinglulu 	imx8m_caam_init();
160*91f16700Schasinglulu 
161*91f16700Schasinglulu 	/*
162*91f16700Schasinglulu 	 * tell BL3-1 where the non-secure software image is located
163*91f16700Schasinglulu 	 * and the entry state information.
164*91f16700Schasinglulu 	 */
165*91f16700Schasinglulu 	bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET;
166*91f16700Schasinglulu 	bl33_image_ep_info.spsr = get_spsr_for_bl33_entry();
167*91f16700Schasinglulu 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
168*91f16700Schasinglulu 
169*91f16700Schasinglulu #if defined(SPD_opteed) || defined(SPD_trusty)
170*91f16700Schasinglulu 	/* Populate entry point information for BL32 */
171*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
172*91f16700Schasinglulu 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
173*91f16700Schasinglulu 	bl32_image_ep_info.pc = BL32_BASE;
174*91f16700Schasinglulu 	bl32_image_ep_info.spsr = 0;
175*91f16700Schasinglulu 
176*91f16700Schasinglulu 	/* Pass TEE base and size to bl33 */
177*91f16700Schasinglulu 	bl33_image_ep_info.args.arg1 = BL32_BASE;
178*91f16700Schasinglulu 	bl33_image_ep_info.args.arg2 = BL32_SIZE;
179*91f16700Schasinglulu 
180*91f16700Schasinglulu #ifdef SPD_trusty
181*91f16700Schasinglulu 	bl32_image_ep_info.args.arg0 = BL32_SIZE;
182*91f16700Schasinglulu 	bl32_image_ep_info.args.arg1 = BL32_BASE;
183*91f16700Schasinglulu #else
184*91f16700Schasinglulu 	/* Make sure memory is clean */
185*91f16700Schasinglulu 	mmio_write_32(BL32_FDT_OVERLAY_ADDR, 0);
186*91f16700Schasinglulu 	bl33_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
187*91f16700Schasinglulu 	bl32_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
188*91f16700Schasinglulu #endif
189*91f16700Schasinglulu #endif
190*91f16700Schasinglulu 
191*91f16700Schasinglulu #if !defined(SPD_opteed) && !defined(SPD_trusty)
192*91f16700Schasinglulu 	enable_snvs_privileged_access();
193*91f16700Schasinglulu #endif
194*91f16700Schasinglulu 
195*91f16700Schasinglulu 	bl31_tzc380_setup();
196*91f16700Schasinglulu }
197*91f16700Schasinglulu 
198*91f16700Schasinglulu #define MAP_BL31_TOTAL										   \
199*91f16700Schasinglulu 	MAP_REGION_FLAT(BL31_START, BL31_SIZE, MT_MEMORY | MT_RW | MT_SECURE)
200*91f16700Schasinglulu #define MAP_BL31_RO										   \
201*91f16700Schasinglulu 	MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE, MT_MEMORY | MT_RO | MT_SECURE)
202*91f16700Schasinglulu #define MAP_COHERENT_MEM									   \
203*91f16700Schasinglulu 	MAP_REGION_FLAT(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,	   \
204*91f16700Schasinglulu 			MT_DEVICE | MT_RW | MT_SECURE)
205*91f16700Schasinglulu #define MAP_BL32_TOTAL										   \
206*91f16700Schasinglulu 	MAP_REGION_FLAT(BL32_BASE, BL32_SIZE, MT_MEMORY | MT_RW)
207*91f16700Schasinglulu 
208*91f16700Schasinglulu void bl31_plat_arch_setup(void)
209*91f16700Schasinglulu {
210*91f16700Schasinglulu 	const mmap_region_t bl_regions[] = {
211*91f16700Schasinglulu 		MAP_BL31_TOTAL,
212*91f16700Schasinglulu 		MAP_BL31_RO,
213*91f16700Schasinglulu #if USE_COHERENT_MEM
214*91f16700Schasinglulu 		MAP_COHERENT_MEM,
215*91f16700Schasinglulu #endif
216*91f16700Schasinglulu #if defined(SPD_opteed) || defined(SPD_trusty)
217*91f16700Schasinglulu 		/* Map TEE memory */
218*91f16700Schasinglulu 		MAP_BL32_TOTAL,
219*91f16700Schasinglulu #endif
220*91f16700Schasinglulu 		{0}
221*91f16700Schasinglulu 	};
222*91f16700Schasinglulu 
223*91f16700Schasinglulu 	setup_page_tables(bl_regions, imx_mmap);
224*91f16700Schasinglulu 	enable_mmu_el3(0);
225*91f16700Schasinglulu }
226*91f16700Schasinglulu 
227*91f16700Schasinglulu void bl31_platform_setup(void)
228*91f16700Schasinglulu {
229*91f16700Schasinglulu 	generic_delay_timer_init();
230*91f16700Schasinglulu 
231*91f16700Schasinglulu 	/* select the CKIL source to 32K OSC */
232*91f16700Schasinglulu 	mmio_write_32(IMX_ANAMIX_BASE + ANAMIX_MISC_CTL, 0x1);
233*91f16700Schasinglulu 
234*91f16700Schasinglulu 	/* Init the dram info */
235*91f16700Schasinglulu 	dram_info_init(SAVED_DRAM_TIMING_BASE);
236*91f16700Schasinglulu 
237*91f16700Schasinglulu 	plat_gic_driver_init();
238*91f16700Schasinglulu 	plat_gic_init();
239*91f16700Schasinglulu 
240*91f16700Schasinglulu 	imx_gpc_init();
241*91f16700Schasinglulu }
242*91f16700Schasinglulu 
243*91f16700Schasinglulu entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type)
244*91f16700Schasinglulu {
245*91f16700Schasinglulu 	if (type == NON_SECURE)
246*91f16700Schasinglulu 		return &bl33_image_ep_info;
247*91f16700Schasinglulu 	if (type == SECURE)
248*91f16700Schasinglulu 		return &bl32_image_ep_info;
249*91f16700Schasinglulu 
250*91f16700Schasinglulu 	return NULL;
251*91f16700Schasinglulu }
252*91f16700Schasinglulu 
253*91f16700Schasinglulu unsigned int plat_get_syscnt_freq2(void)
254*91f16700Schasinglulu {
255*91f16700Schasinglulu 	return COUNTER_FREQUENCY;
256*91f16700Schasinglulu }
257*91f16700Schasinglulu 
258*91f16700Schasinglulu #ifdef SPD_trusty
259*91f16700Schasinglulu void plat_trusty_set_boot_args(aapcs64_params_t *args)
260*91f16700Schasinglulu {
261*91f16700Schasinglulu 	args->arg0 = BL32_SIZE;
262*91f16700Schasinglulu 	args->arg1 = BL32_BASE;
263*91f16700Schasinglulu 	args->arg2 = TRUSTY_PARAMS_LEN_BYTES;
264*91f16700Schasinglulu }
265*91f16700Schasinglulu #endif
266