xref: /rk3399_ARM-atf/plat/mediatek/common/mtk_bl31_setup.c (revision 9edf08b17707a43f5d627fb1255e0bf2067f33e6)
1394b9208SLeon Chen /*
2*9edf08b1SSalman Nabi  * Copyright (c) 2022-2024, MediaTek Inc. All rights reserved.
3394b9208SLeon Chen  *
4394b9208SLeon Chen  * SPDX-License-Identifier: BSD-3-Clause
5394b9208SLeon Chen  */
6394b9208SLeon Chen 
7394b9208SLeon Chen #include <assert.h>
8394b9208SLeon Chen #include <arch.h>
9394b9208SLeon Chen #include <common/bl_common.h>
10394b9208SLeon Chen #include <common/debug.h>
11394b9208SLeon Chen #include <drivers/delay_timer.h>
12394b9208SLeon Chen #include <drivers/generic_delay_timer.h>
13394b9208SLeon Chen #if XLAT_TABLES_LIB_V2 && PLAT_XLAT_TABLES_DYNAMIC
14394b9208SLeon Chen #include <lib/xlat_tables/xlat_tables_v2.h>
15394b9208SLeon Chen #endif
16394b9208SLeon Chen #include <plat/common/platform.h>
17394b9208SLeon Chen 
18ef988aedSRex-BC Chen #if COREBOOT
19ef988aedSRex-BC Chen #include <common/desc_image_load.h>
20ef988aedSRex-BC Chen 
21ef988aedSRex-BC Chen #include <drivers/ti/uart/uart_16550.h>
22ef988aedSRex-BC Chen #include <lib/coreboot.h>
23ef988aedSRex-BC Chen #include <plat_params.h>
24ef988aedSRex-BC Chen #endif
25ef988aedSRex-BC Chen 
26394b9208SLeon Chen /* MTK headers */
27394b9208SLeon Chen #if MTK_SIP_KERNEL_BOOT_ENABLE
28394b9208SLeon Chen #include <cold_boot.h>
29394b9208SLeon Chen #endif
30394b9208SLeon Chen #include <lib/mtk_init/mtk_init.h>
31394b9208SLeon Chen #include <mtk_mmap_pool.h>
32394b9208SLeon Chen 
33394b9208SLeon Chen IMPORT_SYM(uintptr_t, __RW_START__, RW_START);
34394b9208SLeon Chen IMPORT_SYM(uintptr_t, __DATA_START__, DATA_START);
35ef988aedSRex-BC Chen 
36ef988aedSRex-BC Chen #if COREBOOT
37ef988aedSRex-BC Chen static entry_point_info_t bl32_ep_info;
38ef988aedSRex-BC Chen static entry_point_info_t bl33_ep_info;
39ef988aedSRex-BC Chen 
40ef988aedSRex-BC Chen /*******************************************************************************
41ef988aedSRex-BC Chen  * Return a pointer to the 'entry_point_info' structure of the next image for
42ef988aedSRex-BC Chen  * the security state specified. BL33 corresponds to the non-secure image type
43ef988aedSRex-BC Chen  * while BL32 corresponds to the secure image type. A NULL pointer is returned
44ef988aedSRex-BC Chen  * if the image does not exist.
45ef988aedSRex-BC Chen  ******************************************************************************/
46ef988aedSRex-BC Chen entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
47ef988aedSRex-BC Chen {
48ef988aedSRex-BC Chen 	entry_point_info_t *next_image_info;
49ef988aedSRex-BC Chen 
50ef988aedSRex-BC Chen 	next_image_info = (type == NON_SECURE) ? &bl33_ep_info : &bl32_ep_info;
51ef988aedSRex-BC Chen 	assert(next_image_info->h.type == PARAM_EP);
52ef988aedSRex-BC Chen 
53ef988aedSRex-BC Chen 	/* None of the images on this platform can have 0x0 as the entrypoint */
54ef988aedSRex-BC Chen 	if (next_image_info->pc) {
55ef988aedSRex-BC Chen 		return next_image_info;
56ef988aedSRex-BC Chen 	} else {
57ef988aedSRex-BC Chen 		return NULL;
58ef988aedSRex-BC Chen 	}
59ef988aedSRex-BC Chen }
60ef988aedSRex-BC Chen #else
61394b9208SLeon Chen #ifndef MTK_BL31_AS_BL2
62394b9208SLeon Chen static struct mtk_bl31_fw_config bl31_fw_config;
63394b9208SLeon Chen #else
64394b9208SLeon Chen struct mtk_bl31_fw_config bl31_fw_config;
65394b9208SLeon Chen #endif
66394b9208SLeon Chen /* In order to be accessed after MMU enable */
67394b9208SLeon Chen static struct mtk_bl_param_t bl_param_clone;
68394b9208SLeon Chen 
69394b9208SLeon Chen void *get_mtk_bl31_fw_config(int index)
70394b9208SLeon Chen {
71394b9208SLeon Chen 	void *arg = NULL;
72394b9208SLeon Chen 
73394b9208SLeon Chen 	switch (index) {
74394b9208SLeon Chen 	case BOOT_ARG_FROM_BL2:
75394b9208SLeon Chen 		arg = bl31_fw_config.from_bl2;
76394b9208SLeon Chen 		break;
77394b9208SLeon Chen 	case BOOT_ARG_SOC_FW_CONFIG:
78394b9208SLeon Chen 		arg = bl31_fw_config.soc_fw_config;
79394b9208SLeon Chen 		break;
80394b9208SLeon Chen 	case BOOT_ARG_HW_CONFIG:
81394b9208SLeon Chen 		arg = bl31_fw_config.hw_config;
82394b9208SLeon Chen 		break;
83394b9208SLeon Chen 	case BOOT_ARG_RESERVED:
84394b9208SLeon Chen 		arg = bl31_fw_config.reserved;
85394b9208SLeon Chen 		break;
86394b9208SLeon Chen 	default:
87394b9208SLeon Chen 		WARN("Fail to get boot arg, index:%d", index);
88394b9208SLeon Chen 		break;
89394b9208SLeon Chen 	}
90394b9208SLeon Chen 	return arg;
91394b9208SLeon Chen }
92ef988aedSRex-BC Chen #endif
93394b9208SLeon Chen /*****************************************************************************
94394b9208SLeon Chen  * Perform the very early platform specific architectural setup shared between
95394b9208SLeon Chen  * ARM standard platforms. This only does basic initialization. Later
96394b9208SLeon Chen  * architectural setup (bl31_arch_setup()) does not do anything platform
97394b9208SLeon Chen  * specific.
98394b9208SLeon Chen  ******************************************************************************/
99394b9208SLeon Chen void bl31_early_platform_setup2(u_register_t from_bl2,
100394b9208SLeon Chen 				u_register_t soc_fw_config,
101394b9208SLeon Chen 				u_register_t hw_config, u_register_t plat_params_from_bl2)
102394b9208SLeon Chen 
103394b9208SLeon Chen {
104ef988aedSRex-BC Chen #if COREBOOT
105ef988aedSRex-BC Chen 	static console_t console;
106ef988aedSRex-BC Chen 
107ef988aedSRex-BC Chen 	params_early_setup(soc_fw_config);
108ef988aedSRex-BC Chen 	if (coreboot_serial.type) {
109ef988aedSRex-BC Chen 		console_16550_register(coreboot_serial.baseaddr,
110ef988aedSRex-BC Chen 				       coreboot_serial.input_hertz,
111ef988aedSRex-BC Chen 				       coreboot_serial.baud,
112ef988aedSRex-BC Chen 				       &console);
113ef988aedSRex-BC Chen 	}
114ef988aedSRex-BC Chen 	bl31_params_parse_helper(from_bl2, &bl32_ep_info, &bl33_ep_info);
115ef988aedSRex-BC Chen #else
116394b9208SLeon Chen 	struct mtk_bl_param_t *p_mtk_bl_param = (struct mtk_bl_param_t *)from_bl2;
117394b9208SLeon Chen 
118394b9208SLeon Chen 	if (p_mtk_bl_param == NULL) {
119394b9208SLeon Chen 		ERROR("from_bl2 should not be NULL\n");
120394b9208SLeon Chen 		panic();
121394b9208SLeon Chen 	}
122394b9208SLeon Chen 	memcpy(&bl_param_clone, p_mtk_bl_param, sizeof(struct mtk_bl_param_t));
123394b9208SLeon Chen 	bl31_fw_config.from_bl2 = (void *)&bl_param_clone;
124394b9208SLeon Chen 	bl31_fw_config.soc_fw_config = (void *)soc_fw_config;
125394b9208SLeon Chen 	bl31_fw_config.hw_config = (void *)hw_config;
126394b9208SLeon Chen 	bl31_fw_config.reserved = (void *)plat_params_from_bl2;
127ef988aedSRex-BC Chen #endif
128394b9208SLeon Chen 
129394b9208SLeon Chen 	INFO("MTK BL31 start\n");
130394b9208SLeon Chen 	/* Init delay function */
131394b9208SLeon Chen 	generic_delay_timer_init();
132394b9208SLeon Chen 	/* Initialize module initcall */
133394b9208SLeon Chen 	mtk_init_one_level(MTK_INIT_LVL_EARLY_PLAT);
134394b9208SLeon Chen }
135394b9208SLeon Chen 
136394b9208SLeon Chen void bl31_plat_arch_setup(void)
137394b9208SLeon Chen {
138394b9208SLeon Chen 	const mmap_region_t bl_regions[] = {
139394b9208SLeon Chen 		MAP_BL_RO,
140394b9208SLeon Chen 		MAP_BL_RW,
141394b9208SLeon Chen #if USE_COHERENT_MEM
142394b9208SLeon Chen 		MAP_BL_COHERENT_RAM,
143394b9208SLeon Chen #endif
144394b9208SLeon Chen 		{0},
145394b9208SLeon Chen 	};
146394b9208SLeon Chen 
147394b9208SLeon Chen 	mtk_xlat_init(bl_regions);
148394b9208SLeon Chen 	/* Initialize module initcall */
149394b9208SLeon Chen 	mtk_init_one_level(MTK_INIT_LVL_ARCH);
150394b9208SLeon Chen }
151394b9208SLeon Chen 
152394b9208SLeon Chen /*****************************************************************************
153394b9208SLeon Chen  * Perform any BL31 platform setup common to ARM standard platforms
154394b9208SLeon Chen  ******************************************************************************/
155394b9208SLeon Chen 
156394b9208SLeon Chen void bl31_platform_setup(void)
157394b9208SLeon Chen {
158394b9208SLeon Chen 	mtk_init_one_level(MTK_INIT_LVL_PLAT_SETUP_0);
159394b9208SLeon Chen 	mtk_init_one_level(MTK_INIT_LVL_PLAT_SETUP_1);
160394b9208SLeon Chen }
161394b9208SLeon Chen 
162394b9208SLeon Chen /*******************************************************************************
163394b9208SLeon Chen  * Operations before cold CPU leave BL31.
164394b9208SLeon Chen  * Switch console to runtime state.
165394b9208SLeon Chen  ******************************************************************************/
166394b9208SLeon Chen void bl31_plat_runtime_setup(void)
167394b9208SLeon Chen {
168394b9208SLeon Chen 	mtk_init_one_level(MTK_INIT_LVL_PLAT_RUNTIME);
169*9edf08b1SSalman Nabi 
170*9edf08b1SSalman Nabi 	console_flush();
171fcf4dd9fSRex-BC Chen 	console_switch_state(CONSOLE_FLAG_RUNTIME);
172394b9208SLeon Chen }
173394b9208SLeon Chen 
174394b9208SLeon Chen unsigned int plat_get_syscnt_freq2(void)
175394b9208SLeon Chen {
176394b9208SLeon Chen 	return SYS_COUNTER_FREQ_IN_HZ;
177394b9208SLeon Chen }
178