2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
4 * SPDX-License-Identifier: BSD-3-Clause
7 #include <arch_helpers.h>
12 #include <desc_image_load.h>
18 #include <hisi_sram_map.h>
22 #include <optee_utils.h>
25 #include <platform_def.h>
26 #include <sp804_delay_timer.h>
29 #include "hikey_def.h"
30 #include "hikey_private.h"
33 * The next 2 constants identify the extents of the code & RO data region.
34 * These addresses are used by the MMU setup code and therefore they must be
35 * page-aligned. It is the responsibility of the linker script to ensure that
36 * __RO_START__ and __RO_END__ linker symbols refer to page-aligned addresses.
38 #define BL2_RO_BASE (unsigned long)(&__RO_START__)
39 #define BL2_RO_LIMIT (unsigned long)(&__RO_END__)
42 * The next 2 constants identify the extents of the coherent memory region.
43 * These addresses are used by the MMU setup code and therefore they must be
44 * page-aligned. It is the responsibility of the linker script to ensure that
45 * __COHERENT_RAM_START__ and __COHERENT_RAM_END__ linker symbols refer to
46 * page-aligned addresses.
48 #define BL2_COHERENT_RAM_BASE (unsigned long)(&__COHERENT_RAM_START__)
49 #define BL2_COHERENT_RAM_LIMIT (unsigned long)(&__COHERENT_RAM_END__)
51 static meminfo_t bl2_tzram_layout
__aligned(CACHE_WRITEBACK_GRANULE
);
55 /*******************************************************************************
56 * This structure represents the superset of information that is passed to
57 * BL31, e.g. while passing control to it from BL2, bl31_params
58 * and other platform specific params
59 ******************************************************************************/
60 typedef struct bl2_to_bl31_params_mem
{
61 bl31_params_t bl31_params
;
62 image_info_t bl31_image_info
;
63 image_info_t bl32_image_info
;
64 image_info_t bl33_image_info
;
65 entry_point_info_t bl33_ep_info
;
66 entry_point_info_t bl32_ep_info
;
67 entry_point_info_t bl31_ep_info
;
68 } bl2_to_bl31_params_mem_t
;
70 static bl2_to_bl31_params_mem_t bl31_params_mem
;
72 meminfo_t
*bl2_plat_sec_mem_layout(void)
74 return &bl2_tzram_layout
;
77 void bl2_plat_get_scp_bl2_meminfo(meminfo_t
*scp_bl2_meminfo
)
79 scp_bl2_meminfo
->total_base
= SCP_BL2_BASE
;
80 scp_bl2_meminfo
->total_size
= SCP_BL2_SIZE
;
81 scp_bl2_meminfo
->free_base
= SCP_BL2_BASE
;
82 scp_bl2_meminfo
->free_size
= SCP_BL2_SIZE
;
84 #endif /* LOAD_IMAGE_V2 */
86 /*******************************************************************************
87 * Transfer SCP_BL2 from Trusted RAM using the SCP Download protocol.
88 * Return 0 on success, -1 otherwise.
89 ******************************************************************************/
91 int plat_hikey_bl2_handle_scp_bl2(image_info_t
*scp_bl2_image_info
)
93 int bl2_plat_handle_scp_bl2(struct image_info
*scp_bl2_image_info
)
97 hisi_mcu_enable_sram();
99 /* Load MCU binary into SRAM */
100 hisi_mcu_load_image(scp_bl2_image_info
->image_base
,
101 scp_bl2_image_info
->image_size
);
102 /* Let MCU running */
103 hisi_mcu_start_run();
105 INFO("%s: MCU PC is at 0x%x\n",
106 __func__
, mmio_read_32(AO_SC_MCU_SUBSYS_STAT2
));
107 INFO("%s: AO_SC_PERIPH_CLKSTAT4 is 0x%x\n",
108 __func__
, mmio_read_32(AO_SC_PERIPH_CLKSTAT4
));
112 /*******************************************************************************
113 * Gets SPSR for BL32 entry
114 ******************************************************************************/
115 uint32_t hikey_get_spsr_for_bl32_entry(void)
118 * The Secure Payload Dispatcher service is responsible for
119 * setting the SPSR prior to entry into the BL3-2 image.
124 /*******************************************************************************
125 * Gets SPSR for BL33 entry
126 ******************************************************************************/
128 uint32_t hikey_get_spsr_for_bl33_entry(void)
133 /* Figure out what mode we enter the non-secure world in */
134 mode
= EL_IMPLEMENTED(2) ? MODE_EL2
: MODE_EL1
;
137 * TODO: Consider the possibility of specifying the SPSR in
138 * the FIP ToC and allowing the platform to have a say as
141 spsr
= SPSR_64(mode
, MODE_SP_ELX
, DISABLE_ALL_EXCEPTIONS
);
145 uint32_t hikey_get_spsr_for_bl33_entry(void)
147 unsigned int hyp_status
, mode
, spsr
;
149 hyp_status
= GET_VIRT_EXT(read_id_pfr1());
151 mode
= (hyp_status
) ? MODE32_hyp
: MODE32_svc
;
154 * TODO: Consider the possibility of specifying the SPSR in
155 * the FIP ToC and allowing the platform to have a say as
158 spsr
= SPSR_MODE32(mode
, plat_get_ns_image_entrypoint() & 0x1,
159 SPSR_E_LITTLE
, DISABLE_ALL_EXCEPTIONS
);
165 int hikey_bl2_handle_post_image_load(unsigned int image_id
)
168 bl_mem_params_node_t
*bl_mem_params
= get_bl_mem_params_node(image_id
);
170 bl_mem_params_node_t
*pager_mem_params
= NULL
;
171 bl_mem_params_node_t
*paged_mem_params
= NULL
;
173 assert(bl_mem_params
);
179 pager_mem_params
= get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID
);
180 assert(pager_mem_params
);
182 paged_mem_params
= get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID
);
183 assert(paged_mem_params
);
185 err
= parse_optee_header(&bl_mem_params
->ep_info
,
186 &pager_mem_params
->image_info
,
187 &paged_mem_params
->image_info
);
189 WARN("OPTEE header parse error.\n");
192 bl_mem_params
->ep_info
.spsr
= hikey_get_spsr_for_bl32_entry();
197 /* BL33 expects to receive the primary CPU MPID (through r0) */
198 bl_mem_params
->ep_info
.args
.arg0
= 0xffff & read_mpidr();
199 bl_mem_params
->ep_info
.spsr
= hikey_get_spsr_for_bl33_entry();
203 case SCP_BL2_IMAGE_ID
:
204 /* The subsequent handling of SCP_BL2 is platform specific */
205 err
= plat_hikey_bl2_handle_scp_bl2(&bl_mem_params
->image_info
);
207 WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
216 /*******************************************************************************
217 * This function can be used by the platforms to update/use image
218 * information for given `image_id`.
219 ******************************************************************************/
220 int bl2_plat_handle_post_image_load(unsigned int image_id
)
222 return hikey_bl2_handle_post_image_load(image_id
);
225 #else /* LOAD_IMAGE_V2 */
227 bl31_params_t
*bl2_plat_get_bl31_params(void)
229 bl31_params_t
*bl2_to_bl31_params
= NULL
;
232 * Initialise the memory for all the arguments that needs to
235 memset(&bl31_params_mem
, 0, sizeof(bl2_to_bl31_params_mem_t
));
237 /* Assign memory for TF related information */
238 bl2_to_bl31_params
= &bl31_params_mem
.bl31_params
;
239 SET_PARAM_HEAD(bl2_to_bl31_params
, PARAM_BL31
, VERSION_1
, 0);
241 /* Fill BL3-1 related information */
242 bl2_to_bl31_params
->bl31_image_info
= &bl31_params_mem
.bl31_image_info
;
243 SET_PARAM_HEAD(bl2_to_bl31_params
->bl31_image_info
, PARAM_IMAGE_BINARY
,
246 /* Fill BL3-2 related information if it exists */
248 bl2_to_bl31_params
->bl32_ep_info
= &bl31_params_mem
.bl32_ep_info
;
249 SET_PARAM_HEAD(bl2_to_bl31_params
->bl32_ep_info
, PARAM_EP
,
251 bl2_to_bl31_params
->bl32_image_info
= &bl31_params_mem
.bl32_image_info
;
252 SET_PARAM_HEAD(bl2_to_bl31_params
->bl32_image_info
, PARAM_IMAGE_BINARY
,
256 /* Fill BL3-3 related information */
257 bl2_to_bl31_params
->bl33_ep_info
= &bl31_params_mem
.bl33_ep_info
;
258 SET_PARAM_HEAD(bl2_to_bl31_params
->bl33_ep_info
,
259 PARAM_EP
, VERSION_1
, 0);
261 /* BL3-3 expects to receive the primary CPU MPID (through x0) */
262 bl2_to_bl31_params
->bl33_ep_info
->args
.arg0
= 0xffff & read_mpidr();
264 bl2_to_bl31_params
->bl33_image_info
= &bl31_params_mem
.bl33_image_info
;
265 SET_PARAM_HEAD(bl2_to_bl31_params
->bl33_image_info
, PARAM_IMAGE_BINARY
,
268 return bl2_to_bl31_params
;
271 struct entry_point_info
*bl2_plat_get_bl31_ep_info(void)
274 bl31_params_mem
.bl31_ep_info
.args
.arg1
= HIKEY_BL31_PLAT_PARAM_VAL
;
277 return &bl31_params_mem
.bl31_ep_info
;
280 void bl2_plat_set_bl31_ep_info(image_info_t
*image
,
281 entry_point_info_t
*bl31_ep_info
)
283 SET_SECURITY_STATE(bl31_ep_info
->h
.attr
, SECURE
);
284 bl31_ep_info
->spsr
= SPSR_64(MODE_EL3
, MODE_SP_ELX
,
285 DISABLE_ALL_EXCEPTIONS
);
288 /*******************************************************************************
289 * Before calling this function BL32 is loaded in memory and its entrypoint
290 * is set by load_image. This is a placeholder for the platform to change
291 * the entrypoint of BL32 and set SPSR and security state.
292 * On Hikey we only set the security state of the entrypoint
293 ******************************************************************************/
295 void bl2_plat_set_bl32_ep_info(image_info_t
*bl32_image_info
,
296 entry_point_info_t
*bl32_ep_info
)
298 SET_SECURITY_STATE(bl32_ep_info
->h
.attr
, SECURE
);
300 * The Secure Payload Dispatcher service is responsible for
301 * setting the SPSR prior to entry into the BL32 image.
303 bl32_ep_info
->spsr
= 0;
306 /*******************************************************************************
307 * Populate the extents of memory available for loading BL32
308 ******************************************************************************/
309 void bl2_plat_get_bl32_meminfo(meminfo_t
*bl32_meminfo
)
312 * Populate the extents of memory available for loading BL32.
314 bl32_meminfo
->total_base
= BL32_BASE
;
315 bl32_meminfo
->free_base
= BL32_BASE
;
316 bl32_meminfo
->total_size
=
317 (TSP_SEC_MEM_BASE
+ TSP_SEC_MEM_SIZE
) - BL32_BASE
;
318 bl32_meminfo
->free_size
=
319 (TSP_SEC_MEM_BASE
+ TSP_SEC_MEM_SIZE
) - BL32_BASE
;
321 #endif /* BL32_BASE */
323 void bl2_plat_set_bl33_ep_info(image_info_t
*image
,
324 entry_point_info_t
*bl33_ep_info
)
326 unsigned long el_status
;
329 /* Figure out what mode we enter the non-secure world in */
330 el_status
= read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT
;
331 el_status
&= ID_AA64PFR0_ELX_MASK
;
339 * TODO: Consider the possibility of specifying the SPSR in
340 * the FIP ToC and allowing the platform to have a say as
343 bl33_ep_info
->spsr
= SPSR_64(mode
, MODE_SP_ELX
,
344 DISABLE_ALL_EXCEPTIONS
);
345 SET_SECURITY_STATE(bl33_ep_info
->h
.attr
, NON_SECURE
);
348 void bl2_plat_flush_bl31_params(void)
350 flush_dcache_range((unsigned long)&bl31_params_mem
,
351 sizeof(bl2_to_bl31_params_mem_t
));
354 void bl2_plat_get_bl33_meminfo(meminfo_t
*bl33_meminfo
)
356 bl33_meminfo
->total_base
= DDR_BASE
;
357 bl33_meminfo
->total_size
= DDR_SIZE
;
358 bl33_meminfo
->free_base
= DDR_BASE
;
359 bl33_meminfo
->free_size
= DDR_SIZE
;
361 #endif /* LOAD_IMAGE_V2 */
363 static void reset_dwmmc_clk(void)
367 /* disable mmc0 bus clock */
368 mmio_write_32(PERI_SC_PERIPH_CLKDIS0
, PERI_CLK0_MMC0
);
370 data
= mmio_read_32(PERI_SC_PERIPH_CLKSTAT0
);
371 } while (data
& PERI_CLK0_MMC0
);
372 /* enable mmc0 bus clock */
373 mmio_write_32(PERI_SC_PERIPH_CLKEN0
, PERI_CLK0_MMC0
);
375 data
= mmio_read_32(PERI_SC_PERIPH_CLKSTAT0
);
376 } while (!(data
& PERI_CLK0_MMC0
));
377 /* reset mmc0 clock domain */
378 mmio_write_32(PERI_SC_PERIPH_RSTEN0
, PERI_RST0_MMC0
);
380 /* bypass mmc0 clock phase */
381 data
= mmio_read_32(PERI_SC_PERIPH_CTRL2
);
383 mmio_write_32(PERI_SC_PERIPH_CTRL2
, data
);
385 /* disable low power */
386 data
= mmio_read_32(PERI_SC_PERIPH_CTRL13
);
388 mmio_write_32(PERI_SC_PERIPH_CTRL13
, data
);
390 data
= mmio_read_32(PERI_SC_PERIPH_RSTSTAT0
);
391 } while (!(data
& PERI_RST0_MMC0
));
393 /* unreset mmc0 clock domain */
394 mmio_write_32(PERI_SC_PERIPH_RSTDIS0
, PERI_RST0_MMC0
);
396 data
= mmio_read_32(PERI_SC_PERIPH_RSTSTAT0
);
397 } while (data
& PERI_RST0_MMC0
);
400 static void hikey_boardid_init(void)
405 mmio_write_32(MEMORY_AXI_CHIP_ADDR
, midr
);
406 INFO("[BDID] [%x] midr: 0x%x\n", MEMORY_AXI_CHIP_ADDR
,
409 mmio_write_32(MEMORY_AXI_BOARD_TYPE_ADDR
, 0);
410 mmio_write_32(MEMORY_AXI_BOARD_ID_ADDR
, 0x2b);
412 mmio_write_32(ACPU_ARM64_FLAGA
, 0x1234);
413 mmio_write_32(ACPU_ARM64_FLAGB
, 0x5678);
416 static void hikey_sd_init(void)
418 /* switch pinmux to SD */
419 mmio_write_32(IOMG_SD_CLK
, IOMG_MUX_FUNC0
);
420 mmio_write_32(IOMG_SD_CMD
, IOMG_MUX_FUNC0
);
421 mmio_write_32(IOMG_SD_DATA0
, IOMG_MUX_FUNC0
);
422 mmio_write_32(IOMG_SD_DATA1
, IOMG_MUX_FUNC0
);
423 mmio_write_32(IOMG_SD_DATA2
, IOMG_MUX_FUNC0
);
424 mmio_write_32(IOMG_SD_DATA3
, IOMG_MUX_FUNC0
);
426 mmio_write_32(IOCG_SD_CLK
, IOCG_INPUT_16MA
);
427 mmio_write_32(IOCG_SD_CMD
, IOCG_INPUT_12MA
);
428 mmio_write_32(IOCG_SD_DATA0
, IOCG_INPUT_12MA
);
429 mmio_write_32(IOCG_SD_DATA1
, IOCG_INPUT_12MA
);
430 mmio_write_32(IOCG_SD_DATA2
, IOCG_INPUT_12MA
);
431 mmio_write_32(IOCG_SD_DATA3
, IOCG_INPUT_12MA
);
433 /* set SD Card detect as nopull */
434 mmio_write_32(IOCG_GPIO8
, 0);
437 static void hikey_jumper_init(void)
439 /* set jumper detect as nopull */
440 mmio_write_32(IOCG_GPIO24
, 0);
441 /* set jumper detect as GPIO */
442 mmio_write_32(IOMG_GPIO24
, IOMG_MUX_FUNC0
);
445 void bl2_early_platform_setup(meminfo_t
*mem_layout
)
447 dw_mmc_params_t params
;
449 /* Initialize the console to provide early debug support */
450 console_init(CONSOLE_BASE
, PL011_UART_CLK_IN_HZ
, PL011_BAUDRATE
);
452 /* Setup the BL2 memory layout */
453 bl2_tzram_layout
= *mem_layout
;
455 /* Clear SRAM since it'll be used by MCU right now. */
456 memset((void *)SRAM_BASE
, 0, SRAM_SIZE
);
458 sp804_timer_init(SP804_TIMER0_BASE
, 10, 192);
462 hikey_boardid_init();
468 memset(¶ms
, 0, sizeof(dw_mmc_params_t
));
469 params
.reg_base
= DWMMC0_BASE
;
470 params
.desc_base
= HIKEY_MMC_DESC_BASE
;
471 params
.desc_size
= 1 << 20;
472 params
.clk_rate
= 24 * 1000 * 1000;
473 params
.bus_width
= EMMC_BUS_WIDTH_8
;
474 params
.flags
= EMMC_FLAG_CMD23
;
475 dw_mmc_init(¶ms
);
480 void bl2_plat_arch_setup(void)
482 hikey_init_mmu_el1(bl2_tzram_layout
.total_base
,
483 bl2_tzram_layout
.total_size
,
486 BL2_COHERENT_RAM_BASE
,
487 BL2_COHERENT_RAM_LIMIT
);
490 void bl2_platform_setup(void)
492 hikey_security_setup();