xref: /rk3399_ARM-atf/plat/socionext/synquacer/sq_bl31_setup.c (revision 8855e52ec5ba8764280ad6d9a2681f5df2930d23)
1c35d59a3SSumit Garg /*
2*8855e52eSAntonio Nino Diaz  * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
3c35d59a3SSumit Garg  *
4c35d59a3SSumit Garg  * SPDX-License-Identifier: BSD-3-Clause
5c35d59a3SSumit Garg  */
6c35d59a3SSumit Garg 
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz 
909d40e0eSAntonio Nino Diaz #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
11c35d59a3SSumit Garg #include <arch.h>
12c35d59a3SSumit Garg #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1409d40e0eSAntonio Nino Diaz #include <common/debug.h>
1509d40e0eSAntonio Nino Diaz #include <drivers/arm/pl011.h>
1609d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1709d40e0eSAntonio Nino Diaz 
180eb275c9SSumit Garg #include <sq_common.h>
19c35d59a3SSumit Garg 
2067b40070SSumit Garg static console_pl011_t console;
215e5cfc21SSumit Garg static entry_point_info_t bl32_image_ep_info;
225e5cfc21SSumit Garg static entry_point_info_t bl33_image_ep_info;
235e5cfc21SSumit Garg 
24434454a2SArd Biesheuvel IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_START__, SPM_SHIM_EXCEPTIONS_START);
25434454a2SArd Biesheuvel IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_END__,   SPM_SHIM_EXCEPTIONS_END);
26434454a2SArd Biesheuvel IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_LMA__,   SPM_SHIM_EXCEPTIONS_LMA);
27434454a2SArd Biesheuvel 
285e5cfc21SSumit Garg entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
295e5cfc21SSumit Garg {
305e5cfc21SSumit Garg 	assert(sec_state_is_valid(type));
315e5cfc21SSumit Garg 	return type == NON_SECURE ? &bl33_image_ep_info : &bl32_image_ep_info;
325e5cfc21SSumit Garg }
335e5cfc21SSumit Garg 
345e5cfc21SSumit Garg /*******************************************************************************
355e5cfc21SSumit Garg  * Gets SPSR for BL32 entry
365e5cfc21SSumit Garg  ******************************************************************************/
375e5cfc21SSumit Garg uint32_t sq_get_spsr_for_bl32_entry(void)
385e5cfc21SSumit Garg {
395e5cfc21SSumit Garg 	/*
405e5cfc21SSumit Garg 	 * The Secure Payload Dispatcher service is responsible for
415e5cfc21SSumit Garg 	 * setting the SPSR prior to entry into the BL32 image.
425e5cfc21SSumit Garg 	 */
435e5cfc21SSumit Garg 	return 0;
445e5cfc21SSumit Garg }
455e5cfc21SSumit Garg 
465e5cfc21SSumit Garg /*******************************************************************************
475e5cfc21SSumit Garg  * Gets SPSR for BL33 entry
485e5cfc21SSumit Garg  ******************************************************************************/
495e5cfc21SSumit Garg uint32_t sq_get_spsr_for_bl33_entry(void)
505e5cfc21SSumit Garg {
515e5cfc21SSumit Garg 	unsigned long el_status;
525e5cfc21SSumit Garg 	unsigned int mode;
535e5cfc21SSumit Garg 	uint32_t spsr;
545e5cfc21SSumit Garg 
555e5cfc21SSumit Garg 	/* Figure out what mode we enter the non-secure world in */
565e5cfc21SSumit Garg 	el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
575e5cfc21SSumit Garg 	el_status &= ID_AA64PFR0_ELX_MASK;
585e5cfc21SSumit Garg 
595e5cfc21SSumit Garg 	mode = (el_status) ? MODE_EL2 : MODE_EL1;
605e5cfc21SSumit Garg 
615e5cfc21SSumit Garg 	spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
625e5cfc21SSumit Garg 	return spsr;
635e5cfc21SSumit Garg }
6467b40070SSumit Garg 
65ce1f43acSAntonio Nino Diaz void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
66ce1f43acSAntonio Nino Diaz 				u_register_t arg2, u_register_t arg3)
67c35d59a3SSumit Garg {
6867b40070SSumit Garg 	/* Initialize the console to provide early debug support */
6967b40070SSumit Garg 	(void)console_pl011_register(PLAT_SQ_BOOT_UART_BASE,
7067b40070SSumit Garg 			       PLAT_SQ_BOOT_UART_CLK_IN_HZ,
7167b40070SSumit Garg 			       SQ_CONSOLE_BAUDRATE, &console);
7267b40070SSumit Garg 
7367b40070SSumit Garg 	console_set_scope(&console.console, CONSOLE_FLAG_BOOT |
7467b40070SSumit Garg 			  CONSOLE_FLAG_RUNTIME);
7567b40070SSumit Garg 
76c35d59a3SSumit Garg 	/* There are no parameters from BL2 if BL31 is a reset vector */
77ce1f43acSAntonio Nino Diaz 	assert(arg0 == 0U);
78ce1f43acSAntonio Nino Diaz 	assert(arg1 == 0U);
795e5cfc21SSumit Garg 
806cb2a397SSumit Garg 	/* Initialize power controller before setting up topology */
816cb2a397SSumit Garg 	plat_sq_pwrc_setup();
826cb2a397SSumit Garg 
83e373b6a2SArd Biesheuvel #ifdef SPD_opteed
846cb2a397SSumit Garg 	struct draminfo di = {0};
856cb2a397SSumit Garg 
866cb2a397SSumit Garg 	scpi_get_draminfo(&di);
876cb2a397SSumit Garg 
886cb2a397SSumit Garg 	/*
896cb2a397SSumit Garg 	 * Check if OP-TEE has been loaded in Secure RAM allocated
906cb2a397SSumit Garg 	 * from DRAM1 region
916cb2a397SSumit Garg 	 */
926cb2a397SSumit Garg 	if ((di.base1 + di.size1) <= BL32_BASE) {
936cb2a397SSumit Garg 		NOTICE("OP-TEE has been loaded by SCP firmware\n");
945e5cfc21SSumit Garg 		/* Populate entry point information for BL32 */
955e5cfc21SSumit Garg 		SET_PARAM_HEAD(&bl32_image_ep_info,
965e5cfc21SSumit Garg 					PARAM_EP,
975e5cfc21SSumit Garg 					VERSION_1,
985e5cfc21SSumit Garg 					0);
995e5cfc21SSumit Garg 		SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
1005e5cfc21SSumit Garg 		bl32_image_ep_info.pc = BL32_BASE;
1015e5cfc21SSumit Garg 		bl32_image_ep_info.spsr = sq_get_spsr_for_bl32_entry();
1026cb2a397SSumit Garg 	} else {
1036cb2a397SSumit Garg 		NOTICE("OP-TEE has not been loaded by SCP firmware\n");
1046cb2a397SSumit Garg 	}
105e373b6a2SArd Biesheuvel #endif /* SPD_opteed */
1065e5cfc21SSumit Garg 
1075e5cfc21SSumit Garg 	/* Populate entry point information for BL33 */
1085e5cfc21SSumit Garg 	SET_PARAM_HEAD(&bl33_image_ep_info,
1095e5cfc21SSumit Garg 				PARAM_EP,
1105e5cfc21SSumit Garg 				VERSION_1,
1115e5cfc21SSumit Garg 				0);
1125e5cfc21SSumit Garg 	/*
1135e5cfc21SSumit Garg 	 * Tell BL31 where the non-trusted software image
1145e5cfc21SSumit Garg 	 * is located and the entry state information
1155e5cfc21SSumit Garg 	 */
1165e5cfc21SSumit Garg 	bl33_image_ep_info.pc = PRELOADED_BL33_BASE;
1175e5cfc21SSumit Garg 	bl33_image_ep_info.spsr = sq_get_spsr_for_bl33_entry();
1185e5cfc21SSumit Garg 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
119c35d59a3SSumit Garg }
120c35d59a3SSumit Garg 
1215931fdacSSumit Garg static void sq_configure_sys_timer(void)
1225931fdacSSumit Garg {
1235931fdacSSumit Garg 	unsigned int reg_val;
1245931fdacSSumit Garg 
1255931fdacSSumit Garg 	reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT);
1265931fdacSSumit Garg 	reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT);
1275931fdacSSumit Garg 	reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT);
1285931fdacSSumit Garg 	mmio_write_32(SQ_SYS_TIMCTL_BASE +
1295931fdacSSumit Garg 		      CNTACR_BASE(PLAT_SQ_NSTIMER_FRAME_ID), reg_val);
1305931fdacSSumit Garg 
1315931fdacSSumit Garg 	reg_val = (1 << CNTNSAR_NS_SHIFT(PLAT_SQ_NSTIMER_FRAME_ID));
1325931fdacSSumit Garg 	mmio_write_32(SQ_SYS_TIMCTL_BASE + CNTNSAR, reg_val);
1335931fdacSSumit Garg }
1345931fdacSSumit Garg 
135c35d59a3SSumit Garg void bl31_platform_setup(void)
136c35d59a3SSumit Garg {
1370eb275c9SSumit Garg 	/* Initialize the CCN interconnect */
1380eb275c9SSumit Garg 	plat_sq_interconnect_init();
1390eb275c9SSumit Garg 	plat_sq_interconnect_enter_coherency();
140b529799fSSumit Garg 
141b529799fSSumit Garg 	/* Initialize the GIC driver, cpu and distributor interfaces */
142b529799fSSumit Garg 	sq_gic_driver_init();
143b529799fSSumit Garg 	sq_gic_init();
1445931fdacSSumit Garg 
1455931fdacSSumit Garg 	/* Enable and initialize the System level generic timer */
1465931fdacSSumit Garg 	mmio_write_32(SQ_SYS_CNTCTL_BASE + CNTCR_OFF,
147c9512bcaSAntonio Nino Diaz 			CNTCR_FCREQ(0U) | CNTCR_EN);
1485931fdacSSumit Garg 
1495931fdacSSumit Garg 	/* Allow access to the System counter timer module */
1505931fdacSSumit Garg 	sq_configure_sys_timer();
151c35d59a3SSumit Garg }
152c35d59a3SSumit Garg 
153c35d59a3SSumit Garg void bl31_plat_runtime_setup(void)
154c35d59a3SSumit Garg {
155cfe19f85SArd Biesheuvel 	struct draminfo *di = (struct draminfo *)(unsigned long)DRAMINFO_BASE;
156cfe19f85SArd Biesheuvel 
157cfe19f85SArd Biesheuvel 	scpi_get_draminfo(di);
158c35d59a3SSumit Garg }
159c35d59a3SSumit Garg 
160c35d59a3SSumit Garg void bl31_plat_arch_setup(void)
161c35d59a3SSumit Garg {
162434454a2SArd Biesheuvel 	static const mmap_region_t secure_partition_mmap[] = {
163*8855e52eSAntonio Nino Diaz #if ENABLE_SPM && SPM_MM
164434454a2SArd Biesheuvel 		MAP_REGION_FLAT(PLAT_SPM_BUF_BASE,
165434454a2SArd Biesheuvel 				PLAT_SPM_BUF_SIZE,
166434454a2SArd Biesheuvel 				MT_RW_DATA | MT_SECURE),
167434454a2SArd Biesheuvel 		MAP_REGION_FLAT(PLAT_SQ_SP_PRIV_BASE,
168434454a2SArd Biesheuvel 				PLAT_SQ_SP_PRIV_SIZE,
169434454a2SArd Biesheuvel 				MT_RW_DATA | MT_SECURE),
170434454a2SArd Biesheuvel #endif
171434454a2SArd Biesheuvel 		{0},
172434454a2SArd Biesheuvel 	};
173434454a2SArd Biesheuvel 
174434454a2SArd Biesheuvel 	sq_mmap_setup(BL31_BASE, BL31_SIZE, secure_partition_mmap);
1758cd37d7bSSumit Garg 	enable_mmu_el3(XLAT_TABLE_NC);
176434454a2SArd Biesheuvel 
177*8855e52eSAntonio Nino Diaz #if ENABLE_SPM && SPM_MM
178434454a2SArd Biesheuvel 	memcpy((void *)SPM_SHIM_EXCEPTIONS_START,
179434454a2SArd Biesheuvel 	       (void *)SPM_SHIM_EXCEPTIONS_LMA,
180434454a2SArd Biesheuvel 	       (uintptr_t)SPM_SHIM_EXCEPTIONS_END -
181434454a2SArd Biesheuvel 	       (uintptr_t)SPM_SHIM_EXCEPTIONS_START);
182434454a2SArd Biesheuvel #endif
1838cd37d7bSSumit Garg }
1848cd37d7bSSumit Garg 
1858cd37d7bSSumit Garg void bl31_plat_enable_mmu(uint32_t flags)
1868cd37d7bSSumit Garg {
1878cd37d7bSSumit Garg 	enable_mmu_el3(flags | XLAT_TABLE_NC);
188c35d59a3SSumit Garg }
1895931fdacSSumit Garg 
1905931fdacSSumit Garg unsigned int plat_get_syscnt_freq2(void)
1915931fdacSSumit Garg {
1925931fdacSSumit Garg 	unsigned int counter_base_frequency;
1935931fdacSSumit Garg 
1945931fdacSSumit Garg 	/* Read the frequency from Frequency modes table */
1955931fdacSSumit Garg 	counter_base_frequency = mmio_read_32(SQ_SYS_CNTCTL_BASE + CNTFID_OFF);
1965931fdacSSumit Garg 
1975931fdacSSumit Garg 	/* The first entry of the frequency modes table must not be 0 */
1985931fdacSSumit Garg 	if (counter_base_frequency == 0)
1995931fdacSSumit Garg 		panic();
2005931fdacSSumit Garg 
2015931fdacSSumit Garg 	return counter_base_frequency;
2025931fdacSSumit Garg }
203