xref: /rk3399_ARM-atf/plat/imx/imx8ulp/imx8ulp_bl31_setup.c (revision e7b82a7d2fa1fc3f32724e6836b8f6078d20c103)
1 /*
2  * Copyright 2021-2024 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdbool.h>
9 
10 #include <arch_helpers.h>
11 #include <common/bl_common.h>
12 #include <common/debug.h>
13 #include <context.h>
14 #include <drivers/console.h>
15 #include <drivers/generic_delay_timer.h>
16 #include <lib/el3_runtime/context_mgmt.h>
17 #include <lib/mmio.h>
18 #include <lib/xlat_tables/xlat_tables_v2.h>
19 #include <plat/common/platform.h>
20 #include <platform_def.h>
21 
22 #include <imx8_lpuart.h>
23 #include <imx8ulp_caam.h>
24 #include <imx_plat_common.h>
25 #include <plat_imx8.h>
26 #include <upower_api.h>
27 #include <xrdc.h>
28 
29 #define MAP_BL31_TOTAL										   \
30 	MAP_REGION_FLAT(BL31_BASE, BL31_LIMIT - BL31_BASE, MT_MEMORY | MT_RW | MT_SECURE)
31 #define MAP_BL31_RO										   \
32 	MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE, MT_MEMORY | MT_RO | MT_SECURE)
33 #define MAP_BL32_TOTAL MAP_REGION_FLAT(BL32_BASE, BL32_SIZE, MT_MEMORY | MT_RW)
34 #define MAP_COHERENT_MEM									\
35 	MAP_REGION_FLAT(BL_COHERENT_RAM_BASE, (BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE),	\
36 			 MT_DEVICE | MT_RW | MT_SECURE)
37 
38 static const mmap_region_t imx_mmap[] = {
39 	DEVICE0_MAP, DEVICE1_MAP, ELE_MAP,
40 	SEC_SIM_MAP, SRAM0_MAP,
41 	{0}
42 };
43 
44 extern uint32_t upower_init(void);
45 extern void imx8ulp_init_scmi_server(void);
46 
47 static entry_point_info_t bl32_image_ep_info;
48 static entry_point_info_t bl33_image_ep_info;
49 
50 void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
51 				u_register_t arg2, u_register_t arg3)
52 {
53 	static console_t console;
54 
55 	/* config the TPM5 clock */
56 	mmio_write_32(IMX_PCC3_BASE + 0xd0, 0x92000000);
57 	mmio_write_32(IMX_PCC3_BASE + 0xd0, 0xd2000000);
58 
59 	/* enable the GPIO D,E,F non-secure access by default */
60 	mmio_write_32(IMX_PCC4_BASE + 0x78, 0xc0000000);
61 	mmio_write_32(IMX_PCC4_BASE + 0x7c, 0xc0000000);
62 	mmio_write_32(IMX_PCC5_BASE + 0x114, 0xc0000000);
63 
64 	mmio_write_32(IMX_GPIOE_BASE + 0x10, 0xffffffff);
65 	mmio_write_32(IMX_GPIOE_BASE + 0x14, 0x3);
66 	mmio_write_32(IMX_GPIOE_BASE + 0x18, 0xffffffff);
67 	mmio_write_32(IMX_GPIOE_BASE + 0x1c, 0x3);
68 
69 	mmio_write_32(IMX_GPIOF_BASE + 0x10, 0xffffffff);
70 	mmio_write_32(IMX_GPIOF_BASE + 0x14, 0x3);
71 	mmio_write_32(IMX_GPIOF_BASE + 0x18, 0xffffffff);
72 	mmio_write_32(IMX_GPIOF_BASE + 0x1c, 0x3);
73 
74 	mmio_write_32(IMX_GPIOD_BASE + 0x10, 0xffffffff);
75 	mmio_write_32(IMX_GPIOD_BASE + 0x14, 0x3);
76 	mmio_write_32(IMX_GPIOD_BASE + 0x18, 0xffffffff);
77 	mmio_write_32(IMX_GPIOD_BASE + 0x1c, 0x3);
78 
79 	console_lpuart_register(IMX_LPUART_BASE, IMX_BOOT_UART_CLK_IN_HZ,
80 		     IMX_CONSOLE_BAUDRATE, &console);
81 
82 	/* This console is only used for boot stage */
83 	console_set_scope(&console, CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME);
84 
85 	bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET;
86 	bl33_image_ep_info.spsr = plat_get_spsr_for_bl33_entry();
87 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
88 
89 #if defined(SPD_opteed)
90 	/* Populate entry point information for BL32 */
91 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
92 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
93 	bl32_image_ep_info.pc = BL32_BASE;
94 	bl32_image_ep_info.spsr = 0;
95 
96 	/* Pass TEE base and size to bl33 */
97 	bl33_image_ep_info.args.arg1 = BL32_BASE;
98 	bl33_image_ep_info.args.arg2 = BL32_SIZE;
99 
100 	/* Make sure memory is clean */
101 	mmio_write_32(BL32_FDT_OVERLAY_ADDR, 0);
102 	bl33_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
103 	bl32_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
104 #endif
105 }
106 
107 void bl31_plat_arch_setup(void)
108 {
109 	const mmap_region_t bl_regions[] = {
110 		MAP_BL31_TOTAL,
111 		MAP_BL31_RO,
112 #if USE_COHERENT_MEM
113 		MAP_COHERENT_MEM,
114 #endif
115 #if (defined(SPD_opteed)
116 		MAP_BL32_TOTAL,
117 #endif
118 		{0},
119 	};
120 
121 	setup_page_tables(bl_regions, imx_mmap);
122 	enable_mmu_el3(0);
123 
124 	/* TODO: Hack, refine this piece, scmi channel free */
125 	mmio_write_32(SRAM0_BASE + 0x4, 1);
126 
127 	/* Allow M core to reset A core */
128 	mmio_clrbits_32(IMX_MU0B_BASE + 0x10, BIT(2));
129 }
130 
131 void bl31_platform_setup(void)
132 {
133 	/* select the arch timer source */
134 	mmio_setbits_32(IMX_SIM1_BASE + 0x30, 0x8000000);
135 
136 	generic_delay_timer_init();
137 
138 	plat_gic_driver_init();
139 	plat_gic_init();
140 
141 	imx8ulp_init_scmi_server();
142 	upower_init();
143 
144 	xrdc_apply_apd_config();
145 	xrdc_apply_lpav_config();
146 	xrdc_enable();
147 
148 	imx8ulp_caam_init();
149 }
150 
151 entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type)
152 {
153 	if (type == NON_SECURE) {
154 		return &bl33_image_ep_info;
155 	} else {
156 		return &bl32_image_ep_info;
157 	}
158 }
159 
160 unsigned int plat_get_syscnt_freq2(void)
161 {
162 	return COUNTER_FREQUENCY;
163 }
164 
165 void bl31_plat_runtime_setup(void)
166 {
167 }
168