xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_setup.c (revision d323af9e3d903d981b42f954844a95a6bfef91ab)
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>
10*d323af9eSDaniel 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 
27*d323af9eSDaniel Boulby #define MAP_BL1_TOTAL		MAP_REGION_FLAT(			\
28*d323af9eSDaniel Boulby 					bl1_tzram_layout.total_base,	\
29*d323af9eSDaniel Boulby 					bl1_tzram_layout.total_size,	\
30*d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
31*d323af9eSDaniel Boulby #define MAP_BL1_CODE		MAP_REGION_FLAT(			\
32*d323af9eSDaniel Boulby 					BL_CODE_BASE,			\
33*d323af9eSDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
34*d323af9eSDaniel Boulby 					MT_CODE | MT_SECURE)
35*d323af9eSDaniel Boulby #define MAP_BL1_RO_DATA		MAP_REGION_FLAT(			\
36*d323af9eSDaniel Boulby 					BL1_RO_DATA_BASE,		\
37*d323af9eSDaniel Boulby 					BL1_RO_DATA_END			\
38*d323af9eSDaniel Boulby 						- BL_RO_DATA_BASE,	\
39*d323af9eSDaniel Boulby 					MT_RO_DATA | MT_SECURE)
40b4315306SDan Handley 
41b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL1*/
42b4315306SDan Handley static meminfo_t bl1_tzram_layout;
43b4315306SDan Handley 
44b4315306SDan Handley meminfo_t *bl1_plat_sec_mem_layout(void)
45b4315306SDan Handley {
46b4315306SDan Handley 	return &bl1_tzram_layout;
47b4315306SDan Handley }
48b4315306SDan Handley 
49b4315306SDan Handley /*******************************************************************************
50b4315306SDan Handley  * BL1 specific platform actions shared between ARM standard platforms.
51b4315306SDan Handley  ******************************************************************************/
52b4315306SDan Handley void arm_bl1_early_platform_setup(void)
53b4315306SDan Handley {
54b4315306SDan Handley 
557b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
567b4c1405SJuan Castillo 	/* Enable watchdog */
577b4c1405SJuan Castillo 	sp805_start(ARM_SP805_TWDG_BASE, ARM_TWDG_LOAD_VAL);
587b4c1405SJuan Castillo #endif
597b4c1405SJuan Castillo 
60b4315306SDan Handley 	/* Initialize the console to provide early debug support */
6188a0523eSAntonio Nino Diaz 	arm_console_boot_init();
62b4315306SDan Handley 
63b4315306SDan Handley 	/* Allow BL1 to see the whole Trusted RAM */
64b4315306SDan Handley 	bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
65b4315306SDan Handley 	bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
66b4315306SDan Handley 
67a8aa7fecSYatharth Kochar #if !LOAD_IMAGE_V2
68b4315306SDan Handley 	/* Calculate how much RAM BL1 is using and how much remains free */
69b4315306SDan Handley 	bl1_tzram_layout.free_base = ARM_BL_RAM_BASE;
70b4315306SDan Handley 	bl1_tzram_layout.free_size = ARM_BL_RAM_SIZE;
71b4315306SDan Handley 	reserve_mem(&bl1_tzram_layout.free_base,
72b4315306SDan Handley 		    &bl1_tzram_layout.free_size,
73b4315306SDan Handley 		    BL1_RAM_BASE,
74a8aa7fecSYatharth Kochar 		    BL1_RAM_LIMIT - BL1_RAM_BASE);
75a8aa7fecSYatharth Kochar #endif /* LOAD_IMAGE_V2 */
76b4315306SDan Handley }
77b4315306SDan Handley 
78b4315306SDan Handley void bl1_early_platform_setup(void)
79b4315306SDan Handley {
80b4315306SDan Handley 	arm_bl1_early_platform_setup();
81b4315306SDan Handley 
82b4315306SDan Handley 	/*
836355f234SVikram Kanigiri 	 * Initialize Interconnect for this cluster during cold boot.
84b4315306SDan Handley 	 * No need for locks as no other CPU is active.
85b4315306SDan Handley 	 */
866355f234SVikram Kanigiri 	plat_arm_interconnect_init();
87b4315306SDan Handley 	/*
886355f234SVikram Kanigiri 	 * Enable Interconnect coherency for the primary CPU's cluster.
89b4315306SDan Handley 	 */
906355f234SVikram Kanigiri 	plat_arm_interconnect_enter_coherency();
91b4315306SDan Handley }
92b4315306SDan Handley 
93b4315306SDan Handley /******************************************************************************
94b4315306SDan Handley  * Perform the very early platform specific architecture setup shared between
95b4315306SDan Handley  * ARM standard platforms. This only does basic initialization. Later
96b4315306SDan Handley  * architectural setup (bl1_arch_setup()) does not do anything platform
97b4315306SDan Handley  * specific.
98b4315306SDan Handley  *****************************************************************************/
99b4315306SDan Handley void arm_bl1_plat_arch_setup(void)
100b4315306SDan Handley {
101b4315306SDan Handley #if USE_COHERENT_MEM
102*d323af9eSDaniel Boulby 	/* ARM platforms dont use coherent memory in BL1 */
103*d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
104b4315306SDan Handley #endif
105*d323af9eSDaniel Boulby 
106*d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
107*d323af9eSDaniel Boulby 		MAP_BL1_TOTAL,
108*d323af9eSDaniel Boulby 		MAP_BL1_CODE,
109*d323af9eSDaniel Boulby 		MAP_BL1_RO_DATA,
110*d323af9eSDaniel Boulby 		{0}
111*d323af9eSDaniel Boulby 	};
112*d323af9eSDaniel Boulby 
113*d323af9eSDaniel Boulby 	arm_setup_page_tables(bl_regions, plat_arm_get_mmap());
11483fc4a93SYatharth Kochar #ifdef AARCH32
11583fc4a93SYatharth Kochar 	enable_mmu_secure(0);
11683fc4a93SYatharth Kochar #else
117b5fa6563SSandrine Bailleux 	enable_mmu_el3(0);
11883fc4a93SYatharth Kochar #endif /* AARCH32 */
119b4315306SDan Handley }
120b4315306SDan Handley 
121b4315306SDan Handley void bl1_plat_arch_setup(void)
122b4315306SDan Handley {
123b4315306SDan Handley 	arm_bl1_plat_arch_setup();
124b4315306SDan Handley }
125b4315306SDan Handley 
126b4315306SDan Handley /*
127b4315306SDan Handley  * Perform the platform specific architecture setup shared between
128b4315306SDan Handley  * ARM standard platforms.
129b4315306SDan Handley  */
130b4315306SDan Handley void arm_bl1_platform_setup(void)
131b4315306SDan Handley {
132b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
133b4315306SDan Handley 	plat_arm_io_setup();
134c228956aSSoby Mathew #if LOAD_IMAGE_V2
135c228956aSSoby Mathew 	arm_load_tb_fw_config();
136c228956aSSoby Mathew #endif
1373208edcdSSoby Mathew 	/*
1383208edcdSSoby Mathew 	 * Allow access to the System counter timer module and program
1393208edcdSSoby Mathew 	 * counter frequency for non secure images during FWU
1403208edcdSSoby Mathew 	 */
1413208edcdSSoby Mathew 	arm_configure_sys_timer();
1423208edcdSSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
143b4315306SDan Handley }
144b4315306SDan Handley 
145b4315306SDan Handley void bl1_platform_setup(void)
146b4315306SDan Handley {
147b4315306SDan Handley 	arm_bl1_platform_setup();
148b4315306SDan Handley }
149b4315306SDan Handley 
1504c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
1514c117f6cSSandrine Bailleux {
1527b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
1537b4c1405SJuan Castillo 	/* Disable watchdog before leaving BL1 */
1547b4c1405SJuan Castillo 	sp805_stop(ARM_SP805_TWDG_BASE);
1557b4c1405SJuan Castillo #endif
1567b4c1405SJuan Castillo 
1574c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
1584c117f6cSSandrine Bailleux 	/*
1594c117f6cSSandrine Bailleux 	 * Program the EL3 payload's entry point address into the CPUs mailbox
1604c117f6cSSandrine Bailleux 	 * in order to release secondary CPUs from their holding pen and make
1614c117f6cSSandrine Bailleux 	 * them jump there.
1624c117f6cSSandrine Bailleux 	 */
1634c117f6cSSandrine Bailleux 	arm_program_trusted_mailbox(ep_info->pc);
1644c117f6cSSandrine Bailleux 	dsbsy();
1654c117f6cSSandrine Bailleux 	sev();
1664c117f6cSSandrine Bailleux #endif
1674c117f6cSSandrine Bailleux }
1687b56928aSSoby Mathew 
1697b56928aSSoby Mathew /*******************************************************************************
1707b56928aSSoby Mathew  * The following function checks if Firmware update is needed,
1717b56928aSSoby Mathew  * by checking if TOC in FIP image is valid or not.
1727b56928aSSoby Mathew  ******************************************************************************/
1737b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void)
1747b56928aSSoby Mathew {
1757b56928aSSoby Mathew 	if (!arm_io_is_toc_valid())
1767b56928aSSoby Mathew 		return NS_BL1U_IMAGE_ID;
1777b56928aSSoby Mathew 
1787b56928aSSoby Mathew 	return BL2_IMAGE_ID;
1797b56928aSSoby Mathew }
180