Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <bl_common.h> |
| 10 | #include <console.h> |
| 11 | #include <debug.h> |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 12 | #include <desc_image_load.h> |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 13 | #include <dw_mmc.h> |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <generic_delay_timer.h> |
Haojian Zhuang | 3eff409 | 2018-08-04 18:07:26 +0800 | [diff] [blame] | 16 | #include <mmc.h> |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 17 | #include <mmio.h> |
Victor Chong | aa03347 | 2018-02-01 00:35:39 +0900 | [diff] [blame] | 18 | #include <optee_utils.h> |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 19 | #include <partition/partition.h> |
| 20 | #include <platform.h> |
| 21 | #include <string.h> |
| 22 | #include "hi3798cv200.h" |
| 23 | #include "plat_private.h" |
| 24 | |
| 25 | /* Memory ranges for code and read only data sections */ |
| 26 | #define BL2_RO_BASE (unsigned long)(&__RO_START__) |
| 27 | #define BL2_RO_LIMIT (unsigned long)(&__RO_END__) |
| 28 | |
| 29 | /* Memory ranges for coherent memory section */ |
| 30 | #define BL2_COHERENT_RAM_BASE (unsigned long)(&__COHERENT_RAM_START__) |
| 31 | #define BL2_COHERENT_RAM_LIMIT (unsigned long)(&__COHERENT_RAM_END__) |
| 32 | |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 33 | static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE); |
| 34 | |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 35 | /******************************************************************************* |
| 36 | * Transfer SCP_BL2 from Trusted RAM using the SCP Download protocol. |
| 37 | * Return 0 on success, -1 otherwise. |
| 38 | ******************************************************************************/ |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 39 | int plat_poplar_bl2_handle_scp_bl2(image_info_t *scp_bl2_image_info) |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 40 | { |
| 41 | /* |
| 42 | * This platform has no SCP_BL2 yet |
| 43 | */ |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /******************************************************************************* |
| 48 | * Gets SPSR for BL32 entry |
| 49 | ******************************************************************************/ |
| 50 | uint32_t poplar_get_spsr_for_bl32_entry(void) |
| 51 | { |
| 52 | /* |
| 53 | * The Secure Payload Dispatcher service is responsible for |
| 54 | * setting the SPSR prior to entry into the BL3-2 image. |
| 55 | */ |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /******************************************************************************* |
| 60 | * Gets SPSR for BL33 entry |
| 61 | ******************************************************************************/ |
| 62 | #ifndef AARCH32 |
| 63 | uint32_t poplar_get_spsr_for_bl33_entry(void) |
| 64 | { |
| 65 | unsigned long el_status; |
| 66 | unsigned int mode; |
| 67 | uint32_t spsr; |
| 68 | |
| 69 | /* Figure out what mode we enter the non-secure world in */ |
| 70 | el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT; |
| 71 | el_status &= ID_AA64PFR0_ELX_MASK; |
| 72 | |
| 73 | mode = (el_status) ? MODE_EL2 : MODE_EL1; |
| 74 | |
| 75 | /* |
| 76 | * TODO: Consider the possibility of specifying the SPSR in |
| 77 | * the FIP ToC and allowing the platform to have a say as |
| 78 | * well. |
| 79 | */ |
| 80 | spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
| 81 | return spsr; |
| 82 | } |
| 83 | #else |
| 84 | uint32_t poplar_get_spsr_for_bl33_entry(void) |
| 85 | { |
| 86 | unsigned int hyp_status, mode, spsr; |
| 87 | |
| 88 | hyp_status = GET_VIRT_EXT(read_id_pfr1()); |
| 89 | |
| 90 | mode = (hyp_status) ? MODE32_hyp : MODE32_svc; |
| 91 | |
| 92 | /* |
| 93 | * TODO: Consider the possibility of specifying the SPSR in |
| 94 | * the FIP ToC and allowing the platform to have a say as |
| 95 | * well. |
| 96 | */ |
| 97 | spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1, |
| 98 | SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); |
| 99 | return spsr; |
| 100 | } |
| 101 | #endif /* AARCH32 */ |
| 102 | |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 103 | int poplar_bl2_handle_post_image_load(unsigned int image_id) |
| 104 | { |
| 105 | int err = 0; |
| 106 | bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); |
Victor Chong | aa03347 | 2018-02-01 00:35:39 +0900 | [diff] [blame] | 107 | #ifdef SPD_opteed |
| 108 | bl_mem_params_node_t *pager_mem_params = NULL; |
| 109 | bl_mem_params_node_t *paged_mem_params = NULL; |
| 110 | #endif |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 111 | |
| 112 | assert(bl_mem_params); |
| 113 | |
| 114 | switch (image_id) { |
| 115 | #ifdef AARCH64 |
| 116 | case BL32_IMAGE_ID: |
Victor Chong | aa03347 | 2018-02-01 00:35:39 +0900 | [diff] [blame] | 117 | #ifdef SPD_opteed |
| 118 | pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID); |
| 119 | assert(pager_mem_params); |
| 120 | |
| 121 | paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID); |
| 122 | assert(paged_mem_params); |
| 123 | |
| 124 | err = parse_optee_header(&bl_mem_params->ep_info, |
| 125 | &pager_mem_params->image_info, |
| 126 | &paged_mem_params->image_info); |
| 127 | if (err != 0) { |
| 128 | WARN("OPTEE header parse error.\n"); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * OP-TEE expect to receive DTB address in x2. |
| 133 | * This will be copied into x2 by dispatcher. |
| 134 | * Set this (arg3) if necessary |
| 135 | */ |
| 136 | /* bl_mem_params->ep_info.args.arg3 = PLAT_HIKEY_DT_BASE; */ |
| 137 | #endif |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 138 | bl_mem_params->ep_info.spsr = poplar_get_spsr_for_bl32_entry(); |
| 139 | break; |
| 140 | #endif |
| 141 | |
| 142 | case BL33_IMAGE_ID: |
| 143 | /* BL33 expects to receive the primary CPU MPID (through r0) */ |
| 144 | bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr(); |
| 145 | bl_mem_params->ep_info.spsr = poplar_get_spsr_for_bl33_entry(); |
| 146 | break; |
| 147 | |
| 148 | #ifdef SCP_BL2_BASE |
| 149 | case SCP_BL2_IMAGE_ID: |
| 150 | /* The subsequent handling of SCP_BL2 is platform specific */ |
| 151 | err = plat_poplar_bl2_handle_scp_bl2(&bl_mem_params->image_info); |
| 152 | if (err) { |
| 153 | WARN("Failure in platform-specific handling of SCP_BL2 image.\n"); |
| 154 | } |
| 155 | break; |
| 156 | #endif |
Jonathan Wright | ff957ed | 2018-03-14 15:24:00 +0000 | [diff] [blame] | 157 | default: |
| 158 | /* Do nothing in default case */ |
| 159 | break; |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | /******************************************************************************* |
| 166 | * This function can be used by the platforms to update/use image |
| 167 | * information for given `image_id`. |
| 168 | ******************************************************************************/ |
| 169 | int bl2_plat_handle_post_image_load(unsigned int image_id) |
| 170 | { |
| 171 | return poplar_bl2_handle_post_image_load(image_id); |
| 172 | } |
| 173 | |
Antonio Nino Diaz | e93cde1 | 2018-09-24 17:15:15 +0100 | [diff] [blame] | 174 | void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, |
| 175 | u_register_t arg2, u_register_t arg3) |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 176 | { |
Antonio Nino Diaz | e93cde1 | 2018-09-24 17:15:15 +0100 | [diff] [blame] | 177 | struct meminfo *mem_layout = (struct meminfo *)arg1; |
Shawn Guo | d793ff0 | 2018-09-27 16:48:00 +0800 | [diff] [blame] | 178 | #if !POPLAR_RECOVERY |
Haojian Zhuang | 3eff409 | 2018-08-04 18:07:26 +0800 | [diff] [blame] | 179 | struct mmc_device_info info; |
| 180 | |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 181 | dw_mmc_params_t params = EMMC_INIT_PARAMS(POPLAR_EMMC_DESC_BASE); |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 182 | #endif |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 183 | |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 184 | console_init(PL011_UART0_BASE, PL011_UART0_CLK_IN_HZ, PL011_BAUDRATE); |
| 185 | |
| 186 | /* Enable arch timer */ |
| 187 | generic_delay_timer_init(); |
| 188 | |
| 189 | bl2_tzram_layout = *mem_layout; |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 190 | |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 191 | #if !POPLAR_RECOVERY |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 192 | /* SoC-specific emmc register are initialized/configured by bootrom */ |
| 193 | INFO("BL2: initializing emmc\n"); |
Haojian Zhuang | 3eff409 | 2018-08-04 18:07:26 +0800 | [diff] [blame] | 194 | info.mmc_dev_type = MMC_IS_EMMC; |
| 195 | dw_mmc_init(¶ms, &info); |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 196 | #endif |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 197 | |
| 198 | plat_io_setup(); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void bl2_plat_arch_setup(void) |
| 202 | { |
| 203 | plat_configure_mmu_el1(bl2_tzram_layout.total_base, |
| 204 | bl2_tzram_layout.total_size, |
| 205 | BL2_RO_BASE, |
| 206 | BL2_RO_LIMIT, |
| 207 | BL2_COHERENT_RAM_BASE, |
| 208 | BL2_COHERENT_RAM_LIMIT); |
| 209 | } |
| 210 | |
| 211 | void bl2_platform_setup(void) |
| 212 | { |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 215 | uintptr_t plat_get_ns_image_entrypoint(void) |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 216 | { |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 217 | #ifdef PRELOADED_BL33_BASE |
| 218 | return PRELOADED_BL33_BASE; |
| 219 | #else |
Victor Chong | 6df271c | 2017-10-27 00:09:14 +0900 | [diff] [blame] | 220 | return PLAT_POPLAR_NS_IMAGE_OFFSET; |
Victor Chong | 175dd8a | 2018-02-01 00:35:22 +0900 | [diff] [blame] | 221 | #endif |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 222 | } |