Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 1 | /* |
Yann Gautier | d0714c0 | 2022-01-05 18:02:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Yann Gautier | e97b663 | 2019-04-19 10:48:36 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 9 | |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 10 | #include <arch_helpers.h> |
| 11 | #include <common/debug.h> |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 12 | #include <drivers/clk.h> |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 13 | #include <drivers/delay_timer.h> |
| 14 | #include <drivers/st/stm32_console.h> |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 15 | #include <drivers/st/stm32mp_clkfunc.h> |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 16 | #include <drivers/st/stm32mp_reset.h> |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 17 | #include <lib/smccc.h> |
Yann Gautier | a55169b | 2020-01-10 18:18:59 +0100 | [diff] [blame] | 18 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 19 | #include <plat/common/platform.h> |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 20 | #include <services/arm_arch_svc.h> |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 21 | |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 22 | #include <platform_def.h> |
| 23 | |
Nicolas Le Bayon | dc4bcba | 2019-11-18 17:12:27 +0100 | [diff] [blame] | 24 | #define HEADER_VERSION_MAJOR_MASK GENMASK(23, 16) |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 25 | #define RESET_TIMEOUT_US_1MS 1000U |
| 26 | |
| 27 | static console_t console; |
Nicolas Le Bayon | dc4bcba | 2019-11-18 17:12:27 +0100 | [diff] [blame] | 28 | |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 29 | uintptr_t plat_get_ns_image_entrypoint(void) |
| 30 | { |
| 31 | return BL33_BASE; |
| 32 | } |
| 33 | |
| 34 | unsigned int plat_get_syscnt_freq2(void) |
| 35 | { |
| 36 | return read_cntfrq_el0(); |
| 37 | } |
| 38 | |
| 39 | static uintptr_t boot_ctx_address; |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 40 | static uint16_t boot_itf_selected; |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 41 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 42 | void stm32mp_save_boot_ctx_address(uintptr_t address) |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 43 | { |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 44 | boot_api_context_t *boot_context = (boot_api_context_t *)address; |
| 45 | |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 46 | boot_ctx_address = address; |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 47 | boot_itf_selected = boot_context->boot_interface_selected; |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 50 | uintptr_t stm32mp_get_boot_ctx_address(void) |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 51 | { |
| 52 | return boot_ctx_address; |
| 53 | } |
| 54 | |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 55 | uint16_t stm32mp_get_boot_itf_selected(void) |
| 56 | { |
| 57 | return boot_itf_selected; |
| 58 | } |
| 59 | |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 60 | uintptr_t stm32mp_ddrctrl_base(void) |
| 61 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 62 | return DDRCTRL_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | uintptr_t stm32mp_ddrphyc_base(void) |
| 66 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 67 | return DDRPHYC_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | uintptr_t stm32mp_pwr_base(void) |
| 71 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 72 | return PWR_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | uintptr_t stm32mp_rcc_base(void) |
| 76 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 77 | return RCC_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Yann Gautier | f540a59 | 2019-05-22 19:13:51 +0200 | [diff] [blame] | 80 | bool stm32mp_lock_available(void) |
| 81 | { |
| 82 | const uint32_t c_m_bits = SCTLR_M_BIT | SCTLR_C_BIT; |
| 83 | |
| 84 | /* The spinlocks are used only when MMU and data cache are enabled */ |
| 85 | return (read_sctlr() & c_m_bits) == c_m_bits; |
| 86 | } |
| 87 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 88 | #if STM32MP_USE_STM32IMAGE |
Yann Gautier | e97b663 | 2019-04-19 10:48:36 +0200 | [diff] [blame] | 89 | int stm32mp_check_header(boot_api_image_header_t *header, uintptr_t buffer) |
| 90 | { |
| 91 | uint32_t i; |
| 92 | uint32_t img_checksum = 0U; |
| 93 | |
| 94 | /* |
| 95 | * Check header/payload validity: |
| 96 | * - Header magic |
| 97 | * - Header version |
| 98 | * - Payload checksum |
| 99 | */ |
| 100 | if (header->magic != BOOT_API_IMAGE_HEADER_MAGIC_NB) { |
| 101 | ERROR("Header magic\n"); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
Nicolas Le Bayon | dc4bcba | 2019-11-18 17:12:27 +0100 | [diff] [blame] | 105 | if ((header->header_version & HEADER_VERSION_MAJOR_MASK) != |
| 106 | (BOOT_API_HEADER_VERSION & HEADER_VERSION_MAJOR_MASK)) { |
Yann Gautier | e97b663 | 2019-04-19 10:48:36 +0200 | [diff] [blame] | 107 | ERROR("Header version\n"); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | for (i = 0U; i < header->image_length; i++) { |
| 112 | img_checksum += *(uint8_t *)(buffer + i); |
| 113 | } |
| 114 | |
| 115 | if (header->payload_checksum != img_checksum) { |
| 116 | ERROR("Checksum: 0x%x (awaited: 0x%x)\n", img_checksum, |
| 117 | header->payload_checksum); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 123 | #endif /* STM32MP_USE_STM32IMAGE */ |
Yann Gautier | a55169b | 2020-01-10 18:18:59 +0100 | [diff] [blame] | 124 | |
| 125 | int stm32mp_map_ddr_non_cacheable(void) |
| 126 | { |
| 127 | return mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE, |
| 128 | STM32MP_DDR_MAX_SIZE, |
Yann Gautier | f3bd87e | 2020-09-04 15:55:53 +0200 | [diff] [blame] | 129 | MT_NON_CACHEABLE | MT_RW | MT_SECURE); |
Yann Gautier | a55169b | 2020-01-10 18:18:59 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int stm32mp_unmap_ddr(void) |
| 133 | { |
| 134 | return mmap_remove_dynamic_region(STM32MP_DDR_BASE, |
| 135 | STM32MP_DDR_MAX_SIZE); |
| 136 | } |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 137 | |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 138 | int stm32_get_otp_index(const char *otp_name, uint32_t *otp_idx, |
| 139 | uint32_t *otp_len) |
| 140 | { |
| 141 | assert(otp_name != NULL); |
| 142 | assert(otp_idx != NULL); |
| 143 | |
| 144 | return dt_find_otp_name(otp_name, otp_idx, otp_len); |
| 145 | } |
| 146 | |
| 147 | int stm32_get_otp_value(const char *otp_name, uint32_t *otp_val) |
| 148 | { |
| 149 | uint32_t otp_idx; |
| 150 | |
| 151 | assert(otp_name != NULL); |
| 152 | assert(otp_val != NULL); |
| 153 | |
| 154 | if (stm32_get_otp_index(otp_name, &otp_idx, NULL) != 0) { |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | if (stm32_get_otp_value_from_idx(otp_idx, otp_val) != 0) { |
| 159 | ERROR("BSEC: %s Read Error\n", otp_name); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int stm32_get_otp_value_from_idx(const uint32_t otp_idx, uint32_t *otp_val) |
| 167 | { |
| 168 | uint32_t ret = BSEC_NOT_SUPPORTED; |
| 169 | |
| 170 | assert(otp_val != NULL); |
| 171 | |
| 172 | #if defined(IMAGE_BL2) |
| 173 | ret = bsec_shadow_read_otp(otp_val, otp_idx); |
| 174 | #elif defined(IMAGE_BL32) |
| 175 | ret = bsec_read_otp(otp_val, otp_idx); |
| 176 | #else |
| 177 | #error "Not supported" |
| 178 | #endif |
| 179 | if (ret != BSEC_OK) { |
| 180 | ERROR("BSEC: idx=%u Read Error\n", otp_idx); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 187 | #if defined(IMAGE_BL2) |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 188 | static void reset_uart(uint32_t reset) |
| 189 | { |
| 190 | int ret; |
| 191 | |
| 192 | ret = stm32mp_reset_assert(reset, RESET_TIMEOUT_US_1MS); |
| 193 | if (ret != 0) { |
| 194 | panic(); |
| 195 | } |
| 196 | |
| 197 | udelay(2); |
| 198 | |
| 199 | ret = stm32mp_reset_deassert(reset, RESET_TIMEOUT_US_1MS); |
| 200 | if (ret != 0) { |
| 201 | panic(); |
| 202 | } |
| 203 | |
| 204 | mdelay(1); |
| 205 | } |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 206 | #endif |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 207 | |
Yann Gautier | d143574 | 2021-10-18 10:55:23 +0200 | [diff] [blame] | 208 | static void set_console(uintptr_t base, uint32_t clk_rate) |
| 209 | { |
| 210 | unsigned int console_flags; |
| 211 | |
| 212 | if (console_stm32_register(base, clk_rate, |
Yann Gautier | b02dd49 | 2022-03-02 14:31:55 +0100 | [diff] [blame] | 213 | (uint32_t)STM32MP_UART_BAUDRATE, &console) == 0) { |
Yann Gautier | d143574 | 2021-10-18 10:55:23 +0200 | [diff] [blame] | 214 | panic(); |
| 215 | } |
| 216 | |
| 217 | console_flags = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | |
| 218 | CONSOLE_FLAG_TRANSLATE_CRLF; |
| 219 | #if !defined(IMAGE_BL2) && defined(DEBUG) |
| 220 | console_flags |= CONSOLE_FLAG_RUNTIME; |
| 221 | #endif |
| 222 | |
| 223 | console_set_scope(&console, console_flags); |
| 224 | } |
| 225 | |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 226 | int stm32mp_uart_console_setup(void) |
| 227 | { |
| 228 | struct dt_node_info dt_uart_info; |
Yann Gautier | d0714c0 | 2022-01-05 18:02:46 +0100 | [diff] [blame] | 229 | uint32_t clk_rate = 0U; |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 230 | int result; |
Yann Gautier | 3d8497c | 2021-10-18 16:06:22 +0200 | [diff] [blame] | 231 | uint32_t boot_itf __unused; |
| 232 | uint32_t boot_instance __unused; |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 233 | |
| 234 | result = dt_get_stdout_uart_info(&dt_uart_info); |
| 235 | |
| 236 | if ((result <= 0) || |
Yann Gautier | d0714c0 | 2022-01-05 18:02:46 +0100 | [diff] [blame] | 237 | (dt_uart_info.status == DT_DISABLED)) { |
| 238 | return -ENODEV; |
| 239 | } |
| 240 | |
| 241 | #if defined(IMAGE_BL2) |
| 242 | if ((dt_uart_info.clock < 0) || |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 243 | (dt_uart_info.reset < 0)) { |
| 244 | return -ENODEV; |
| 245 | } |
Yann Gautier | d0714c0 | 2022-01-05 18:02:46 +0100 | [diff] [blame] | 246 | #endif |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 247 | |
Yann Gautier | 3d8497c | 2021-10-18 16:06:22 +0200 | [diff] [blame] | 248 | #if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2) |
| 249 | stm32_get_boot_interface(&boot_itf, &boot_instance); |
| 250 | |
| 251 | if ((boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) && |
| 252 | (get_uart_address(boot_instance) == dt_uart_info.base)) { |
| 253 | return -EACCES; |
| 254 | } |
| 255 | #endif |
| 256 | |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 257 | #if defined(IMAGE_BL2) |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 258 | if (dt_set_stdout_pinctrl() != 0) { |
| 259 | return -ENODEV; |
| 260 | } |
| 261 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 262 | clk_enable((unsigned long)dt_uart_info.clock); |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 263 | |
| 264 | reset_uart((uint32_t)dt_uart_info.reset); |
| 265 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 266 | clk_rate = clk_get_rate((unsigned long)dt_uart_info.clock); |
Yann Gautier | d0714c0 | 2022-01-05 18:02:46 +0100 | [diff] [blame] | 267 | #endif |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 268 | |
Yann Gautier | d143574 | 2021-10-18 10:55:23 +0200 | [diff] [blame] | 269 | set_console(dt_uart_info.base, clk_rate); |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Yann Gautier | d143574 | 2021-10-18 10:55:23 +0200 | [diff] [blame] | 274 | #if STM32MP_EARLY_CONSOLE |
| 275 | void stm32mp_setup_early_console(void) |
| 276 | { |
| 277 | plat_crash_console_init(); |
| 278 | set_console(STM32MP_DEBUG_USART_BASE, STM32MP_DEBUG_USART_CLK_FRQ); |
| 279 | } |
| 280 | #endif /* STM32MP_EARLY_CONSOLE */ |
| 281 | |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 282 | /***************************************************************************** |
| 283 | * plat_is_smccc_feature_available() - This function checks whether SMCCC |
| 284 | * feature is availabile for platform. |
| 285 | * @fid: SMCCC function id |
| 286 | * |
| 287 | * Return SMC_ARCH_CALL_SUCCESS if SMCCC feature is available and |
| 288 | * SMC_ARCH_CALL_NOT_SUPPORTED otherwise. |
| 289 | *****************************************************************************/ |
| 290 | int32_t plat_is_smccc_feature_available(u_register_t fid) |
| 291 | { |
| 292 | switch (fid) { |
| 293 | case SMCCC_ARCH_SOC_ID: |
| 294 | return SMC_ARCH_CALL_SUCCESS; |
| 295 | default: |
| 296 | return SMC_ARCH_CALL_NOT_SUPPORTED; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /* Get SOC version */ |
| 301 | int32_t plat_get_soc_version(void) |
| 302 | { |
| 303 | uint32_t chip_id = stm32mp_get_chip_dev_id(); |
| 304 | uint32_t manfid = SOC_ID_SET_JEP_106(JEDEC_ST_BKID, JEDEC_ST_MFID); |
| 305 | |
| 306 | return (int32_t)(manfid | (chip_id & SOC_ID_IMPL_DEF_MASK)); |
| 307 | } |
| 308 | |
| 309 | /* Get SOC revision */ |
| 310 | int32_t plat_get_soc_revision(void) |
| 311 | { |
| 312 | return (int32_t)(stm32mp_get_chip_version() & SOC_ID_REV_MASK); |
| 313 | } |