xref: /rk3399_ARM-atf/plat/st/stm32mp1/bl2_plat_setup.c (revision cce37d44a9436f73ef8ceda294f290689151712b)
14353bb20SYann Gautier /*
24353bb20SYann Gautier  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
34353bb20SYann Gautier  *
44353bb20SYann Gautier  * SPDX-License-Identifier: BSD-3-Clause
54353bb20SYann Gautier  */
64353bb20SYann Gautier 
74353bb20SYann Gautier #include <arch_helpers.h>
84353bb20SYann Gautier #include <assert.h>
94353bb20SYann Gautier #include <bl_common.h>
104353bb20SYann Gautier #include <boot_api.h>
114353bb20SYann Gautier #include <debug.h>
124353bb20SYann Gautier #include <delay_timer.h>
134353bb20SYann Gautier #include <desc_image_load.h>
144353bb20SYann Gautier #include <generic_delay_timer.h>
154353bb20SYann Gautier #include <mmio.h>
164353bb20SYann Gautier #include <platform.h>
174353bb20SYann Gautier #include <platform_def.h>
18*cce37d44SYann Gautier #include <stm32_console.h>
197839a050SYann Gautier #include <stm32mp1_clk.h>
20*cce37d44SYann Gautier #include <stm32mp1_context.h>
217839a050SYann Gautier #include <stm32mp1_dt.h>
22e4f559ffSYann Gautier #include <stm32mp1_pmic.h>
234353bb20SYann Gautier #include <stm32mp1_private.h>
244353bb20SYann Gautier #include <stm32mp1_pwr.h>
2510a511ceSYann Gautier #include <stm32mp1_ram.h>
264353bb20SYann Gautier #include <stm32mp1_rcc.h>
27278c34dfSYann Gautier #include <stm32mp1_reset.h>
284353bb20SYann Gautier #include <string.h>
294353bb20SYann Gautier #include <xlat_tables_v2.h>
304353bb20SYann Gautier 
31*cce37d44SYann Gautier static struct console_stm32 console;
32*cce37d44SYann Gautier 
334353bb20SYann Gautier void bl2_el3_early_platform_setup(u_register_t arg0, u_register_t arg1,
344353bb20SYann Gautier 				  u_register_t arg2, u_register_t arg3)
354353bb20SYann Gautier {
364353bb20SYann Gautier 	stm32mp1_save_boot_ctx_address(arg0);
374353bb20SYann Gautier }
384353bb20SYann Gautier 
394353bb20SYann Gautier void bl2_platform_setup(void)
404353bb20SYann Gautier {
4110a511ceSYann Gautier 	int ret;
4210a511ceSYann Gautier 
43e4f559ffSYann Gautier 	if (dt_check_pmic()) {
44e4f559ffSYann Gautier 		initialize_pmic();
45e4f559ffSYann Gautier 	}
46e4f559ffSYann Gautier 
4710a511ceSYann Gautier 	ret = stm32mp1_ddr_probe();
4810a511ceSYann Gautier 	if (ret < 0) {
4910a511ceSYann Gautier 		ERROR("Invalid DDR init: error %d\n", ret);
5010a511ceSYann Gautier 		panic();
5110a511ceSYann Gautier 	}
5210a511ceSYann Gautier 
534353bb20SYann Gautier 	INFO("BL2 runs SP_MIN setup\n");
544353bb20SYann Gautier }
554353bb20SYann Gautier 
564353bb20SYann Gautier void bl2_el3_plat_arch_setup(void)
574353bb20SYann Gautier {
58278c34dfSYann Gautier 	int32_t result;
59278c34dfSYann Gautier 	struct dt_node_info dt_dev_info;
60278c34dfSYann Gautier 	const char *board_model;
61e58a53fbSYann Gautier 	boot_api_context_t *boot_context =
62e58a53fbSYann Gautier 		(boot_api_context_t *)stm32mp1_get_boot_ctx_address();
63278c34dfSYann Gautier 	uint32_t clk_rate;
64e58a53fbSYann Gautier 
654353bb20SYann Gautier 	/*
664353bb20SYann Gautier 	 * Disable the backup domain write protection.
674353bb20SYann Gautier 	 * The protection is enable at each reset by hardware
684353bb20SYann Gautier 	 * and must be disabled by software.
694353bb20SYann Gautier 	 */
704353bb20SYann Gautier 	mmio_setbits_32(PWR_BASE + PWR_CR1, PWR_CR1_DBP);
714353bb20SYann Gautier 
724353bb20SYann Gautier 	while ((mmio_read_32(PWR_BASE + PWR_CR1) & PWR_CR1_DBP) == 0U) {
734353bb20SYann Gautier 		;
744353bb20SYann Gautier 	}
754353bb20SYann Gautier 
764353bb20SYann Gautier 	/* Reset backup domain on cold boot cases */
774353bb20SYann Gautier 	if ((mmio_read_32(RCC_BASE + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
784353bb20SYann Gautier 		mmio_setbits_32(RCC_BASE + RCC_BDCR, RCC_BDCR_VSWRST);
794353bb20SYann Gautier 
804353bb20SYann Gautier 		while ((mmio_read_32(RCC_BASE + RCC_BDCR) & RCC_BDCR_VSWRST) ==
814353bb20SYann Gautier 		       0U) {
824353bb20SYann Gautier 			;
834353bb20SYann Gautier 		}
844353bb20SYann Gautier 
854353bb20SYann Gautier 		mmio_clrbits_32(RCC_BASE + RCC_BDCR, RCC_BDCR_VSWRST);
864353bb20SYann Gautier 	}
874353bb20SYann Gautier 
884353bb20SYann Gautier 	mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
894353bb20SYann Gautier 			BL_CODE_END - BL_CODE_BASE,
904353bb20SYann Gautier 			MT_CODE | MT_SECURE);
914353bb20SYann Gautier 
924353bb20SYann Gautier 	/* Prevent corruption of preloaded BL32 */
934353bb20SYann Gautier 	mmap_add_region(BL32_BASE, BL32_BASE,
944353bb20SYann Gautier 			BL32_LIMIT - BL32_BASE,
954353bb20SYann Gautier 			MT_MEMORY | MT_RO | MT_SECURE);
964353bb20SYann Gautier 
974353bb20SYann Gautier 	/* Prevent corruption of preloaded Device Tree */
984353bb20SYann Gautier 	mmap_add_region(DTB_BASE, DTB_BASE,
994353bb20SYann Gautier 			DTB_LIMIT - DTB_BASE,
1004353bb20SYann Gautier 			MT_MEMORY | MT_RO | MT_SECURE);
1014353bb20SYann Gautier 
1024353bb20SYann Gautier 	configure_mmu();
1034353bb20SYann Gautier 
1044353bb20SYann Gautier 	generic_delay_timer_init();
1054353bb20SYann Gautier 
1067839a050SYann Gautier 	if (dt_open_and_check() < 0) {
1077839a050SYann Gautier 		panic();
1087839a050SYann Gautier 	}
1097839a050SYann Gautier 
1107839a050SYann Gautier 	if (stm32mp1_clk_probe() < 0) {
1117839a050SYann Gautier 		panic();
1127839a050SYann Gautier 	}
1137839a050SYann Gautier 
1147839a050SYann Gautier 	if (stm32mp1_clk_init() < 0) {
1157839a050SYann Gautier 		panic();
1167839a050SYann Gautier 	}
1177839a050SYann Gautier 
118278c34dfSYann Gautier 	result = dt_get_stdout_uart_info(&dt_dev_info);
119278c34dfSYann Gautier 
120278c34dfSYann Gautier 	if ((result <= 0) ||
121278c34dfSYann Gautier 	    (dt_dev_info.status == 0U) ||
122278c34dfSYann Gautier 	    (dt_dev_info.clock < 0) ||
123278c34dfSYann Gautier 	    (dt_dev_info.reset < 0)) {
124278c34dfSYann Gautier 		goto skip_console_init;
125278c34dfSYann Gautier 	}
126278c34dfSYann Gautier 
127278c34dfSYann Gautier 	if (dt_set_stdout_pinctrl() != 0) {
128278c34dfSYann Gautier 		goto skip_console_init;
129278c34dfSYann Gautier 	}
130278c34dfSYann Gautier 
131278c34dfSYann Gautier 	if (stm32mp1_clk_enable((unsigned long)dt_dev_info.clock) != 0) {
132278c34dfSYann Gautier 		goto skip_console_init;
133278c34dfSYann Gautier 	}
134278c34dfSYann Gautier 
135278c34dfSYann Gautier 	stm32mp1_reset_assert((uint32_t)dt_dev_info.reset);
136278c34dfSYann Gautier 	udelay(2);
137278c34dfSYann Gautier 	stm32mp1_reset_deassert((uint32_t)dt_dev_info.reset);
138278c34dfSYann Gautier 	mdelay(1);
139278c34dfSYann Gautier 
140278c34dfSYann Gautier 	clk_rate = stm32mp1_clk_get_rate((unsigned long)dt_dev_info.clock);
141278c34dfSYann Gautier 
142*cce37d44SYann Gautier 	if (console_stm32_register(dt_dev_info.base, clk_rate,
143*cce37d44SYann Gautier 				   STM32MP1_UART_BAUDRATE, &console) == 0) {
144278c34dfSYann Gautier 		panic();
145278c34dfSYann Gautier 	}
146278c34dfSYann Gautier 
147278c34dfSYann Gautier 	board_model = dt_get_board_model();
148278c34dfSYann Gautier 	if (board_model != NULL) {
149278c34dfSYann Gautier 		NOTICE("%s\n", board_model);
150278c34dfSYann Gautier 	}
151278c34dfSYann Gautier 
152278c34dfSYann Gautier skip_console_init:
153278c34dfSYann Gautier 
154e58a53fbSYann Gautier 	if (stm32_save_boot_interface(boot_context->boot_interface_selected,
155e58a53fbSYann Gautier 				      boot_context->boot_interface_instance) !=
156e58a53fbSYann Gautier 	    0) {
157e58a53fbSYann Gautier 		ERROR("Cannot save boot interface\n");
158e58a53fbSYann Gautier 	}
159e58a53fbSYann Gautier 
16010a511ceSYann Gautier 	stm32mp1_arch_security_setup();
16110a511ceSYann Gautier 
1624353bb20SYann Gautier 	stm32mp1_io_setup();
1634353bb20SYann Gautier }
164