xref: /rk3399_ARM-atf/plat/arm/css/common/css_bl2_setup.c (revision b10d44995eb652675863c2cc6a7726683613da0d)
1 /*
2  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <bl_common.h>
32 #include <css_def.h>
33 #include <debug.h>
34 #include <mmio.h>
35 #include <plat_arm.h>
36 #include <string.h>
37 #include <utils.h>
38 #include "css_scp_bootloader.h"
39 
40 /* Weak definition may be overridden in specific CSS based platform */
41 #if LOAD_IMAGE_V2
42 #pragma weak plat_arm_bl2_handle_scp_bl2
43 #else
44 #pragma weak bl2_plat_handle_scp_bl2
45 #endif
46 
47 /*******************************************************************************
48  * Transfer SCP_BL2 from Trusted RAM using the SCP Download protocol.
49  * Return 0 on success, -1 otherwise.
50  ******************************************************************************/
51 #if LOAD_IMAGE_V2
52 int plat_arm_bl2_handle_scp_bl2(image_info_t *scp_bl2_image_info)
53 #else
54 int bl2_plat_handle_scp_bl2(image_info_t *scp_bl2_image_info)
55 #endif
56 {
57 	int ret;
58 
59 	INFO("BL2: Initiating SCP_BL2 transfer to SCP\n");
60 
61 	ret = scp_bootloader_transfer((void *)scp_bl2_image_info->image_base,
62 		scp_bl2_image_info->image_size);
63 
64 	if (ret == 0)
65 		INFO("BL2: SCP_BL2 transferred to SCP\n");
66 	else
67 		ERROR("BL2: SCP_BL2 transfer failure\n");
68 
69 	return ret;
70 }
71 
72 #ifdef EL3_PAYLOAD_BASE
73 /*
74  * We need to override some of the platform functions when booting an EL3
75  * payload.
76  */
77 
78 static unsigned int scp_boot_config;
79 
80 void bl2_early_platform_setup(meminfo_t *mem_layout)
81 {
82 	arm_bl2_early_platform_setup(mem_layout);
83 
84 	/* Save SCP Boot config before it gets overwritten by SCP_BL2 loading */
85 	scp_boot_config = mmio_read_32(SCP_BOOT_CFG_ADDR);
86 	VERBOSE("BL2: Saved SCP Boot config = 0x%x\n", scp_boot_config);
87 }
88 
89 void bl2_platform_setup(void)
90 {
91 	arm_bl2_platform_setup();
92 
93 	/*
94 	 * Before releasing the AP cores out of reset, the SCP writes some data
95 	 * at the beginning of the Trusted SRAM. It is is overwritten before
96 	 * reaching this function. We need to restore this data, as if the
97 	 * target had just come out of reset. This implies:
98 	 *  - zeroing the first 128 bytes of Trusted SRAM;
99 	 *  - restoring the SCP boot configuration.
100 	 */
101 	VERBOSE("BL2: Restoring SCP reset data in Trusted SRAM\n");
102 	zero_normalmem((void *)ARM_TRUSTED_SRAM_BASE, 128);
103 	mmio_write_32(SCP_BOOT_CFG_ADDR, scp_boot_config);
104 }
105 #endif /* EL3_PAYLOAD_BASE */
106