xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_setup.c (revision ba597da7fd23d34e8867342f1dfee7925991300c)
1b4315306SDan Handley /*
2c228956aSSoby Mathew  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3b4315306SDan Handley  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5b4315306SDan Handley  */
6b4315306SDan Handley 
7b4315306SDan Handley #include <arch.h>
8b4315306SDan Handley #include <arm_def.h>
93b211ff5SAntonio Nino Diaz #include <arm_xlat_tables.h>
10d323af9eSDaniel Boulby #include <assert.h>
111af540efSRoberto Vargas #include <bl1.h>
12b4315306SDan Handley #include <bl_common.h>
13b4315306SDan Handley #include <plat_arm.h>
141af540efSRoberto Vargas #include <platform.h>
154adb10c1SIsla Mitchell #include <platform_def.h>
167b4c1405SJuan Castillo #include <sp805.h>
17af419dd6SSandrine Bailleux #include <utils.h>
183ae8a360SSandrine Bailleux #include "../../../bl1/bl1_private.h"
19b4315306SDan Handley 
20b4315306SDan Handley /* Weak definitions may be overridden in specific ARM standard platform */
21b4315306SDan Handley #pragma weak bl1_early_platform_setup
22b4315306SDan Handley #pragma weak bl1_plat_arch_setup
23b4315306SDan Handley #pragma weak bl1_platform_setup
24b4315306SDan Handley #pragma weak bl1_plat_sec_mem_layout
2507570d59SYatharth Kochar #pragma weak bl1_plat_prepare_exit
26b4315306SDan Handley 
27d323af9eSDaniel Boulby #define MAP_BL1_TOTAL		MAP_REGION_FLAT(			\
28d323af9eSDaniel Boulby 					bl1_tzram_layout.total_base,	\
29d323af9eSDaniel Boulby 					bl1_tzram_layout.total_size,	\
30d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
312ecaafd2SDaniel Boulby /*
322ecaafd2SDaniel Boulby  * If SEPARATE_CODE_AND_RODATA=1 we define a region for each section
332ecaafd2SDaniel Boulby  * otherwise one region is defined containing both
342ecaafd2SDaniel Boulby  */
352ecaafd2SDaniel Boulby #if SEPARATE_CODE_AND_RODATA
362ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
37d323af9eSDaniel Boulby 					BL_CODE_BASE,			\
38d323af9eSDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
392ecaafd2SDaniel Boulby 					MT_CODE | MT_SECURE),		\
402ecaafd2SDaniel Boulby 				MAP_REGION_FLAT(			\
41d323af9eSDaniel Boulby 					BL1_RO_DATA_BASE,		\
42d323af9eSDaniel Boulby 					BL1_RO_DATA_END			\
43d323af9eSDaniel Boulby 						- BL_RO_DATA_BASE,	\
44d323af9eSDaniel Boulby 					MT_RO_DATA | MT_SECURE)
452ecaafd2SDaniel Boulby #else
462ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
472ecaafd2SDaniel Boulby 					BL_CODE_BASE,			\
482ecaafd2SDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
492ecaafd2SDaniel Boulby 					MT_CODE | MT_SECURE)
502ecaafd2SDaniel Boulby #endif
51b4315306SDan Handley 
52b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL1*/
53b4315306SDan Handley static meminfo_t bl1_tzram_layout;
54b4315306SDan Handley 
556c77e749SSandrine Bailleux struct meminfo *bl1_plat_sec_mem_layout(void)
56b4315306SDan Handley {
57b4315306SDan Handley 	return &bl1_tzram_layout;
58b4315306SDan Handley }
59b4315306SDan Handley 
60b4315306SDan Handley /*******************************************************************************
61b4315306SDan Handley  * BL1 specific platform actions shared between ARM standard platforms.
62b4315306SDan Handley  ******************************************************************************/
63b4315306SDan Handley void arm_bl1_early_platform_setup(void)
64b4315306SDan Handley {
65b4315306SDan Handley 
667b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
677b4c1405SJuan Castillo 	/* Enable watchdog */
687b4c1405SJuan Castillo 	sp805_start(ARM_SP805_TWDG_BASE, ARM_TWDG_LOAD_VAL);
697b4c1405SJuan Castillo #endif
707b4c1405SJuan Castillo 
71b4315306SDan Handley 	/* Initialize the console to provide early debug support */
7288a0523eSAntonio Nino Diaz 	arm_console_boot_init();
73b4315306SDan Handley 
74b4315306SDan Handley 	/* Allow BL1 to see the whole Trusted RAM */
75b4315306SDan Handley 	bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
76b4315306SDan Handley 	bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
77b4315306SDan Handley 
78a8aa7fecSYatharth Kochar #if !LOAD_IMAGE_V2
79b4315306SDan Handley 	/* Calculate how much RAM BL1 is using and how much remains free */
80b4315306SDan Handley 	bl1_tzram_layout.free_base = ARM_BL_RAM_BASE;
81b4315306SDan Handley 	bl1_tzram_layout.free_size = ARM_BL_RAM_SIZE;
82b4315306SDan Handley 	reserve_mem(&bl1_tzram_layout.free_base,
83b4315306SDan Handley 		    &bl1_tzram_layout.free_size,
84b4315306SDan Handley 		    BL1_RAM_BASE,
85a8aa7fecSYatharth Kochar 		    BL1_RAM_LIMIT - BL1_RAM_BASE);
86a8aa7fecSYatharth Kochar #endif /* LOAD_IMAGE_V2 */
87b4315306SDan Handley }
88b4315306SDan Handley 
89b4315306SDan Handley void bl1_early_platform_setup(void)
90b4315306SDan Handley {
91b4315306SDan Handley 	arm_bl1_early_platform_setup();
92b4315306SDan Handley 
93b4315306SDan Handley 	/*
946355f234SVikram Kanigiri 	 * Initialize Interconnect for this cluster during cold boot.
95b4315306SDan Handley 	 * No need for locks as no other CPU is active.
96b4315306SDan Handley 	 */
976355f234SVikram Kanigiri 	plat_arm_interconnect_init();
98b4315306SDan Handley 	/*
996355f234SVikram Kanigiri 	 * Enable Interconnect coherency for the primary CPU's cluster.
100b4315306SDan Handley 	 */
1016355f234SVikram Kanigiri 	plat_arm_interconnect_enter_coherency();
102b4315306SDan Handley }
103b4315306SDan Handley 
104b4315306SDan Handley /******************************************************************************
105b4315306SDan Handley  * Perform the very early platform specific architecture setup shared between
106b4315306SDan Handley  * ARM standard platforms. This only does basic initialization. Later
107b4315306SDan Handley  * architectural setup (bl1_arch_setup()) does not do anything platform
108b4315306SDan Handley  * specific.
109b4315306SDan Handley  *****************************************************************************/
110b4315306SDan Handley void arm_bl1_plat_arch_setup(void)
111b4315306SDan Handley {
112b4315306SDan Handley #if USE_COHERENT_MEM
113d323af9eSDaniel Boulby 	/* ARM platforms dont use coherent memory in BL1 */
114d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
115b4315306SDan Handley #endif
116d323af9eSDaniel Boulby 
117d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
118d323af9eSDaniel Boulby 		MAP_BL1_TOTAL,
1192ecaafd2SDaniel Boulby 		MAP_BL1_RO,
1201eb735d7SRoberto Vargas #if USE_ROMLIB
1211eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1221eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1231eb735d7SRoberto Vargas  #endif
124d323af9eSDaniel Boulby 		{0}
125d323af9eSDaniel Boulby 	};
126d323af9eSDaniel Boulby 
127d323af9eSDaniel Boulby 	arm_setup_page_tables(bl_regions, plat_arm_get_mmap());
12883fc4a93SYatharth Kochar #ifdef AARCH32
1291e54cbb8SAntonio Nino Diaz 	enable_mmu_svc_mon(0);
13083fc4a93SYatharth Kochar #else
131b5fa6563SSandrine Bailleux 	enable_mmu_el3(0);
13283fc4a93SYatharth Kochar #endif /* AARCH32 */
1331eb735d7SRoberto Vargas 
1341eb735d7SRoberto Vargas 	arm_setup_romlib();
135b4315306SDan Handley }
136b4315306SDan Handley 
137b4315306SDan Handley void bl1_plat_arch_setup(void)
138b4315306SDan Handley {
139b4315306SDan Handley 	arm_bl1_plat_arch_setup();
140b4315306SDan Handley }
141b4315306SDan Handley 
142b4315306SDan Handley /*
143b4315306SDan Handley  * Perform the platform specific architecture setup shared between
144b4315306SDan Handley  * ARM standard platforms.
145b4315306SDan Handley  */
146b4315306SDan Handley void arm_bl1_platform_setup(void)
147b4315306SDan Handley {
148b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
149b4315306SDan Handley 	plat_arm_io_setup();
150c228956aSSoby Mathew #if LOAD_IMAGE_V2
151c228956aSSoby Mathew 	arm_load_tb_fw_config();
152*ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT
153*ba597da7SJohn Tsichritzis 	/* Share the Mbed TLS heap info with other images */
154*ba597da7SJohn Tsichritzis 	arm_bl1_set_mbedtls_heap();
155*ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */
156*ba597da7SJohn Tsichritzis #endif /* LOAD_IMAGE_V2 */
1573208edcdSSoby Mathew 	/*
1583208edcdSSoby Mathew 	 * Allow access to the System counter timer module and program
1593208edcdSSoby Mathew 	 * counter frequency for non secure images during FWU
1603208edcdSSoby Mathew 	 */
1613208edcdSSoby Mathew 	arm_configure_sys_timer();
1623208edcdSSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
163b4315306SDan Handley }
164b4315306SDan Handley 
165b4315306SDan Handley void bl1_platform_setup(void)
166b4315306SDan Handley {
167b4315306SDan Handley 	arm_bl1_platform_setup();
168b4315306SDan Handley }
169b4315306SDan Handley 
1704c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
1714c117f6cSSandrine Bailleux {
1727b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
1737b4c1405SJuan Castillo 	/* Disable watchdog before leaving BL1 */
1747b4c1405SJuan Castillo 	sp805_stop(ARM_SP805_TWDG_BASE);
1757b4c1405SJuan Castillo #endif
1767b4c1405SJuan Castillo 
1774c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
1784c117f6cSSandrine Bailleux 	/*
1794c117f6cSSandrine Bailleux 	 * Program the EL3 payload's entry point address into the CPUs mailbox
1804c117f6cSSandrine Bailleux 	 * in order to release secondary CPUs from their holding pen and make
1814c117f6cSSandrine Bailleux 	 * them jump there.
1824c117f6cSSandrine Bailleux 	 */
1832a246d2eSDimitris Papastamos 	plat_arm_program_trusted_mailbox(ep_info->pc);
1844c117f6cSSandrine Bailleux 	dsbsy();
1854c117f6cSSandrine Bailleux 	sev();
1864c117f6cSSandrine Bailleux #endif
1874c117f6cSSandrine Bailleux }
1887b56928aSSoby Mathew 
1897b56928aSSoby Mathew /*******************************************************************************
1907b56928aSSoby Mathew  * The following function checks if Firmware update is needed,
1917b56928aSSoby Mathew  * by checking if TOC in FIP image is valid or not.
1927b56928aSSoby Mathew  ******************************************************************************/
1937b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void)
1947b56928aSSoby Mathew {
1957b56928aSSoby Mathew 	if (!arm_io_is_toc_valid())
1967b56928aSSoby Mathew 		return NS_BL1U_IMAGE_ID;
1977b56928aSSoby Mathew 
1987b56928aSSoby Mathew 	return BL2_IMAGE_ID;
1997b56928aSSoby Mathew }
200