Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 1 | /* |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2021, 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 | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 12 | #include <drivers/delay_timer.h> |
| 13 | #include <drivers/st/stm32_console.h> |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 14 | #include <drivers/st/stm32mp_clkfunc.h> |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 15 | #include <drivers/st/stm32mp_reset.h> |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 16 | #include <lib/smccc.h> |
Yann Gautier | a55169b | 2020-01-10 18:18:59 +0100 | [diff] [blame] | 17 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 18 | #include <plat/common/platform.h> |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 19 | #include <services/arm_arch_svc.h> |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 20 | |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 21 | #include <platform_def.h> |
| 22 | |
Nicolas Le Bayon | dc4bcba | 2019-11-18 17:12:27 +0100 | [diff] [blame] | 23 | #define HEADER_VERSION_MAJOR_MASK GENMASK(23, 16) |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 24 | #define RESET_TIMEOUT_US_1MS 1000U |
| 25 | |
| 26 | static console_t console; |
Nicolas Le Bayon | dc4bcba | 2019-11-18 17:12:27 +0100 | [diff] [blame] | 27 | |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 28 | uintptr_t plat_get_ns_image_entrypoint(void) |
| 29 | { |
| 30 | return BL33_BASE; |
| 31 | } |
| 32 | |
| 33 | unsigned int plat_get_syscnt_freq2(void) |
| 34 | { |
| 35 | return read_cntfrq_el0(); |
| 36 | } |
| 37 | |
| 38 | static uintptr_t boot_ctx_address; |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 39 | static uint16_t boot_itf_selected; |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 40 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 41 | void stm32mp_save_boot_ctx_address(uintptr_t address) |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 42 | { |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 43 | boot_api_context_t *boot_context = (boot_api_context_t *)address; |
| 44 | |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 45 | boot_ctx_address = address; |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 46 | boot_itf_selected = boot_context->boot_interface_selected; |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 49 | uintptr_t stm32mp_get_boot_ctx_address(void) |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 50 | { |
| 51 | return boot_ctx_address; |
| 52 | } |
| 53 | |
Yann Gautier | cf1360d | 2020-08-27 18:28:57 +0200 | [diff] [blame] | 54 | uint16_t stm32mp_get_boot_itf_selected(void) |
| 55 | { |
| 56 | return boot_itf_selected; |
| 57 | } |
| 58 | |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 59 | uintptr_t stm32mp_ddrctrl_base(void) |
| 60 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 61 | return DDRCTRL_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | uintptr_t stm32mp_ddrphyc_base(void) |
| 65 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 66 | return DDRPHYC_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | uintptr_t stm32mp_pwr_base(void) |
| 70 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 71 | return PWR_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | uintptr_t stm32mp_rcc_base(void) |
| 75 | { |
Yann Gautier | a18f61b | 2020-05-05 17:58:40 +0200 | [diff] [blame] | 76 | return RCC_BASE; |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Yann Gautier | f540a59 | 2019-05-22 19:13:51 +0200 | [diff] [blame] | 79 | bool stm32mp_lock_available(void) |
| 80 | { |
| 81 | const uint32_t c_m_bits = SCTLR_M_BIT | SCTLR_C_BIT; |
| 82 | |
| 83 | /* The spinlocks are used only when MMU and data cache are enabled */ |
| 84 | return (read_sctlr() & c_m_bits) == c_m_bits; |
| 85 | } |
| 86 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 87 | #if STM32MP_USE_STM32IMAGE |
Yann Gautier | e97b663 | 2019-04-19 10:48:36 +0200 | [diff] [blame] | 88 | int stm32mp_check_header(boot_api_image_header_t *header, uintptr_t buffer) |
| 89 | { |
| 90 | uint32_t i; |
| 91 | uint32_t img_checksum = 0U; |
| 92 | |
| 93 | /* |
| 94 | * Check header/payload validity: |
| 95 | * - Header magic |
| 96 | * - Header version |
| 97 | * - Payload checksum |
| 98 | */ |
| 99 | if (header->magic != BOOT_API_IMAGE_HEADER_MAGIC_NB) { |
| 100 | ERROR("Header magic\n"); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
Nicolas Le Bayon | dc4bcba | 2019-11-18 17:12:27 +0100 | [diff] [blame] | 104 | if ((header->header_version & HEADER_VERSION_MAJOR_MASK) != |
| 105 | (BOOT_API_HEADER_VERSION & HEADER_VERSION_MAJOR_MASK)) { |
Yann Gautier | e97b663 | 2019-04-19 10:48:36 +0200 | [diff] [blame] | 106 | ERROR("Header version\n"); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | |
| 110 | for (i = 0U; i < header->image_length; i++) { |
| 111 | img_checksum += *(uint8_t *)(buffer + i); |
| 112 | } |
| 113 | |
| 114 | if (header->payload_checksum != img_checksum) { |
| 115 | ERROR("Checksum: 0x%x (awaited: 0x%x)\n", img_checksum, |
| 116 | header->payload_checksum); |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 122 | #endif /* STM32MP_USE_STM32IMAGE */ |
Yann Gautier | a55169b | 2020-01-10 18:18:59 +0100 | [diff] [blame] | 123 | |
| 124 | int stm32mp_map_ddr_non_cacheable(void) |
| 125 | { |
| 126 | return mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE, |
| 127 | STM32MP_DDR_MAX_SIZE, |
Yann Gautier | f3bd87e | 2020-09-04 15:55:53 +0200 | [diff] [blame] | 128 | MT_NON_CACHEABLE | MT_RW | MT_SECURE); |
Yann Gautier | a55169b | 2020-01-10 18:18:59 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | int stm32mp_unmap_ddr(void) |
| 132 | { |
| 133 | return mmap_remove_dynamic_region(STM32MP_DDR_BASE, |
| 134 | STM32MP_DDR_MAX_SIZE); |
| 135 | } |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 136 | |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 137 | #if defined(IMAGE_BL2) |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 138 | static void reset_uart(uint32_t reset) |
| 139 | { |
| 140 | int ret; |
| 141 | |
| 142 | ret = stm32mp_reset_assert(reset, RESET_TIMEOUT_US_1MS); |
| 143 | if (ret != 0) { |
| 144 | panic(); |
| 145 | } |
| 146 | |
| 147 | udelay(2); |
| 148 | |
| 149 | ret = stm32mp_reset_deassert(reset, RESET_TIMEOUT_US_1MS); |
| 150 | if (ret != 0) { |
| 151 | panic(); |
| 152 | } |
| 153 | |
| 154 | mdelay(1); |
| 155 | } |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 156 | #endif |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 157 | |
| 158 | int stm32mp_uart_console_setup(void) |
| 159 | { |
| 160 | struct dt_node_info dt_uart_info; |
| 161 | unsigned int console_flags; |
| 162 | uint32_t clk_rate; |
| 163 | int result; |
Yann Gautier | 3d8497c | 2021-10-18 16:06:22 +0200 | [diff] [blame] | 164 | uint32_t boot_itf __unused; |
| 165 | uint32_t boot_instance __unused; |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 166 | |
| 167 | result = dt_get_stdout_uart_info(&dt_uart_info); |
| 168 | |
| 169 | if ((result <= 0) || |
| 170 | (dt_uart_info.status == DT_DISABLED) || |
| 171 | (dt_uart_info.clock < 0) || |
| 172 | (dt_uart_info.reset < 0)) { |
| 173 | return -ENODEV; |
| 174 | } |
| 175 | |
Yann Gautier | 3d8497c | 2021-10-18 16:06:22 +0200 | [diff] [blame] | 176 | #if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2) |
| 177 | stm32_get_boot_interface(&boot_itf, &boot_instance); |
| 178 | |
| 179 | if ((boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) && |
| 180 | (get_uart_address(boot_instance) == dt_uart_info.base)) { |
| 181 | return -EACCES; |
| 182 | } |
| 183 | #endif |
| 184 | |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 185 | #if defined(IMAGE_BL2) |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 186 | if (dt_set_stdout_pinctrl() != 0) { |
| 187 | return -ENODEV; |
| 188 | } |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 189 | #endif |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 190 | |
| 191 | stm32mp_clk_enable((unsigned long)dt_uart_info.clock); |
| 192 | |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 193 | #if defined(IMAGE_BL2) |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 194 | reset_uart((uint32_t)dt_uart_info.reset); |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 195 | #endif |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 196 | |
| 197 | clk_rate = stm32mp_clk_get_rate((unsigned long)dt_uart_info.clock); |
| 198 | |
| 199 | if (console_stm32_register(dt_uart_info.base, clk_rate, |
| 200 | STM32MP_UART_BAUDRATE, &console) == 0) { |
| 201 | panic(); |
| 202 | } |
| 203 | |
| 204 | console_flags = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH | |
| 205 | CONSOLE_FLAG_TRANSLATE_CRLF; |
Yann Gautier | 414f17c | 2021-10-18 15:50:05 +0200 | [diff] [blame] | 206 | #if !defined(IMAGE_BL2) && defined(DEBUG) |
| 207 | console_flags |= CONSOLE_FLAG_RUNTIME; |
| 208 | #endif |
Yann Gautier | 7a81912 | 2021-10-18 15:26:33 +0200 | [diff] [blame] | 209 | console_set_scope(&console, console_flags); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Yann Gautier | ed6515d | 2021-03-08 15:03:35 +0100 | [diff] [blame] | 214 | /***************************************************************************** |
| 215 | * plat_is_smccc_feature_available() - This function checks whether SMCCC |
| 216 | * feature is availabile for platform. |
| 217 | * @fid: SMCCC function id |
| 218 | * |
| 219 | * Return SMC_ARCH_CALL_SUCCESS if SMCCC feature is available and |
| 220 | * SMC_ARCH_CALL_NOT_SUPPORTED otherwise. |
| 221 | *****************************************************************************/ |
| 222 | int32_t plat_is_smccc_feature_available(u_register_t fid) |
| 223 | { |
| 224 | switch (fid) { |
| 225 | case SMCCC_ARCH_SOC_ID: |
| 226 | return SMC_ARCH_CALL_SUCCESS; |
| 227 | default: |
| 228 | return SMC_ARCH_CALL_NOT_SUPPORTED; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* Get SOC version */ |
| 233 | int32_t plat_get_soc_version(void) |
| 234 | { |
| 235 | uint32_t chip_id = stm32mp_get_chip_dev_id(); |
| 236 | uint32_t manfid = SOC_ID_SET_JEP_106(JEDEC_ST_BKID, JEDEC_ST_MFID); |
| 237 | |
| 238 | return (int32_t)(manfid | (chip_id & SOC_ID_IMPL_DEF_MASK)); |
| 239 | } |
| 240 | |
| 241 | /* Get SOC revision */ |
| 242 | int32_t plat_get_soc_revision(void) |
| 243 | { |
| 244 | return (int32_t)(stm32mp_get_chip_version() & SOC_ID_REV_MASK); |
| 245 | } |