1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017-2022, STMicroelectronics 4 * Copyright (c) 2016-2018, Linaro Limited 5 */ 6 7 #include <boot_api.h> 8 #include <config.h> 9 #include <console.h> 10 #include <drivers/gic.h> 11 #include <drivers/stm32_etzpc.h> 12 #include <drivers/stm32_iwdg.h> 13 #include <drivers/stm32_tamp.h> 14 #include <drivers/stm32_uart.h> 15 #include <drivers/stm32mp1_etzpc.h> 16 #include <drivers/stm32mp_dt_bindings.h> 17 #include <kernel/boot.h> 18 #include <kernel/dt.h> 19 #include <kernel/interrupt.h> 20 #include <kernel/misc.h> 21 #include <kernel/panic.h> 22 #include <kernel/spinlock.h> 23 #include <mm/core_memprot.h> 24 #include <platform_config.h> 25 #include <sm/psci.h> 26 #include <stm32_util.h> 27 #include <trace.h> 28 29 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, APB1_BASE, APB1_SIZE); 30 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, APB2_BASE, APB2_SIZE); 31 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, APB3_BASE, APB3_SIZE); 32 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, APB4_BASE, APB4_SIZE); 33 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, APB5_BASE, APB5_SIZE); 34 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, AHB4_BASE, AHB4_SIZE); 35 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, AHB5_BASE, AHB5_SIZE); 36 37 register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB3_BASE, APB3_SIZE); 38 register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB4_BASE, APB4_SIZE); 39 register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB5_BASE, APB5_SIZE); 40 register_phys_mem_pgdir(MEM_AREA_IO_SEC, AHB4_BASE, AHB4_SIZE); 41 register_phys_mem_pgdir(MEM_AREA_IO_SEC, AHB5_BASE, AHB5_SIZE); 42 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GIC_BASE, GIC_SIZE); 43 44 register_ddr(DDR_BASE, CFG_DRAM_SIZE); 45 46 #define _ID2STR(id) (#id) 47 #define ID2STR(id) _ID2STR(id) 48 49 static TEE_Result platform_banner(void) 50 { 51 #ifdef CFG_EMBED_DTB 52 IMSG("Platform stm32mp1: flavor %s - DT %s", 53 ID2STR(PLATFORM_FLAVOR), 54 ID2STR(CFG_EMBED_DTB_SOURCE_FILE)); 55 #else 56 IMSG("Platform stm32mp1: flavor %s - no device tree", 57 ID2STR(PLATFORM_FLAVOR)); 58 #endif 59 60 return TEE_SUCCESS; 61 } 62 service_init(platform_banner); 63 64 /* 65 * Console 66 * 67 * CFG_STM32_EARLY_CONSOLE_UART specifies the ID of the UART used for 68 * trace console. Value 0 disables the early console. 69 * 70 * We cannot use the generic serial_console support since probing 71 * the console requires the platform clock driver to be already 72 * up and ready which is done only once service_init are completed. 73 */ 74 static struct stm32_uart_pdata console_data; 75 76 void console_init(void) 77 { 78 /* Early console initialization before MMU setup */ 79 struct uart { 80 paddr_t pa; 81 bool secure; 82 } uarts[] = { 83 [0] = { .pa = 0 }, 84 [1] = { .pa = USART1_BASE, .secure = true, }, 85 [2] = { .pa = USART2_BASE, .secure = false, }, 86 [3] = { .pa = USART3_BASE, .secure = false, }, 87 [4] = { .pa = UART4_BASE, .secure = false, }, 88 [5] = { .pa = UART5_BASE, .secure = false, }, 89 [6] = { .pa = USART6_BASE, .secure = false, }, 90 [7] = { .pa = UART7_BASE, .secure = false, }, 91 [8] = { .pa = UART8_BASE, .secure = false, }, 92 }; 93 94 COMPILE_TIME_ASSERT(ARRAY_SIZE(uarts) > CFG_STM32_EARLY_CONSOLE_UART); 95 96 if (!uarts[CFG_STM32_EARLY_CONSOLE_UART].pa) 97 return; 98 99 /* No clock yet bound to the UART console */ 100 console_data.clock = NULL; 101 102 console_data.secure = uarts[CFG_STM32_EARLY_CONSOLE_UART].secure; 103 stm32_uart_init(&console_data, uarts[CFG_STM32_EARLY_CONSOLE_UART].pa); 104 105 register_serial_console(&console_data.chip); 106 107 IMSG("Early console on UART#%u", CFG_STM32_EARLY_CONSOLE_UART); 108 } 109 110 #ifdef CFG_EMBED_DTB 111 static TEE_Result init_console_from_dt(void) 112 { 113 struct stm32_uart_pdata *pd = NULL; 114 void *fdt = NULL; 115 int node = 0; 116 TEE_Result res = TEE_ERROR_GENERIC; 117 118 fdt = get_embedded_dt(); 119 res = get_console_node_from_dt(fdt, &node, NULL, NULL); 120 if (res == TEE_ERROR_ITEM_NOT_FOUND) { 121 fdt = get_external_dt(); 122 res = get_console_node_from_dt(fdt, &node, NULL, NULL); 123 if (res == TEE_ERROR_ITEM_NOT_FOUND) 124 return TEE_SUCCESS; 125 if (res != TEE_SUCCESS) 126 return res; 127 } 128 129 pd = stm32_uart_init_from_dt_node(fdt, node); 130 if (!pd) { 131 IMSG("DTB disables console"); 132 register_serial_console(NULL); 133 return TEE_SUCCESS; 134 } 135 136 /* Replace early console with the new one */ 137 console_flush(); 138 console_data = *pd; 139 free(pd); 140 register_serial_console(&console_data.chip); 141 IMSG("DTB enables console (%ssecure)", pd->secure ? "" : "non-"); 142 143 return TEE_SUCCESS; 144 } 145 146 /* Probe console from DT once clock inits (service init level) are completed */ 147 service_init_late(init_console_from_dt); 148 #endif 149 150 /* 151 * GIC init, used also for primary/secondary boot core wake completion 152 */ 153 static struct gic_data gic_data; 154 155 void itr_core_handler(void) 156 { 157 gic_it_handle(&gic_data); 158 } 159 160 void main_init_gic(void) 161 { 162 gic_init(&gic_data, GIC_BASE + GICC_OFFSET, GIC_BASE + GICD_OFFSET); 163 itr_init(&gic_data.chip); 164 165 stm32mp_register_online_cpu(); 166 } 167 168 void main_secondary_init_gic(void) 169 { 170 gic_cpu_init(&gic_data); 171 172 stm32mp_register_online_cpu(); 173 } 174 175 static TEE_Result init_stm32mp1_drivers(void) 176 { 177 /* Without secure DTB support, some drivers must be inited */ 178 if (!IS_ENABLED(CFG_EMBED_DTB)) 179 stm32_etzpc_init(ETZPC_BASE); 180 181 /* Secure internal memories for the platform, once ETZPC is ready */ 182 etzpc_configure_tzma(0, ETZPC_TZMA_ALL_SECURE); 183 etzpc_lock_tzma(0); 184 185 #ifdef CFG_TZSRAM_START 186 COMPILE_TIME_ASSERT(((SYSRAM_BASE + SYSRAM_SIZE) <= CFG_TZSRAM_START) || 187 ((SYSRAM_BASE <= CFG_TZSRAM_START) && 188 (SYSRAM_SEC_SIZE >= CFG_TZSRAM_SIZE))); 189 #endif /* CFG_TZSRAM_START */ 190 191 etzpc_configure_tzma(1, SYSRAM_SEC_SIZE >> SMALL_PAGE_SHIFT); 192 etzpc_lock_tzma(1); 193 194 return TEE_SUCCESS; 195 } 196 197 service_init_late(init_stm32mp1_drivers); 198 199 static TEE_Result init_late_stm32mp1_drivers(void) 200 { 201 TEE_Result res = TEE_ERROR_GENERIC; 202 203 /* Set access permission to TAM backup registers */ 204 if (IS_ENABLED(CFG_STM32_TAMP)) { 205 struct stm32_bkpregs_conf conf = { 206 .nb_zone1_regs = TAMP_BKP_REGISTER_ZONE1_COUNT, 207 .nb_zone2_regs = TAMP_BKP_REGISTER_ZONE2_COUNT, 208 }; 209 210 res = stm32_tamp_set_secure_bkpregs(&conf); 211 if (res == TEE_ERROR_DEFER_DRIVER_INIT) { 212 /* TAMP driver was not probed if disabled in the DT */ 213 res = TEE_SUCCESS; 214 } 215 if (res) 216 panic(); 217 } 218 219 return TEE_SUCCESS; 220 } 221 222 driver_init_late(init_late_stm32mp1_drivers); 223 224 vaddr_t stm32_rcc_base(void) 225 { 226 static struct io_pa_va base = { .pa = RCC_BASE }; 227 228 return io_pa_or_va_secure(&base, 1); 229 } 230 231 vaddr_t get_gicd_base(void) 232 { 233 struct io_pa_va base = { .pa = GIC_BASE + GICD_OFFSET }; 234 235 return io_pa_or_va_secure(&base, 1); 236 } 237 238 void stm32mp_get_bsec_static_cfg(struct stm32_bsec_static_cfg *cfg) 239 { 240 cfg->base = BSEC_BASE; 241 cfg->upper_start = STM32MP1_UPPER_OTP_START; 242 cfg->max_id = STM32MP1_OTP_MAX_ID; 243 } 244 245 bool stm32mp_is_closed_device(void) 246 { 247 uint32_t otp = 0; 248 TEE_Result result = TEE_ERROR_GENERIC; 249 250 /* Non closed_device platform expects fuse well programmed to 0 */ 251 result = stm32_bsec_shadow_read_otp(&otp, DATA0_OTP); 252 if (!result && !(otp & BIT(DATA0_OTP_SECURED_POS))) 253 return false; 254 255 return true; 256 } 257 258 bool __weak stm32mp_with_pmic(void) 259 { 260 return false; 261 } 262 263 uint32_t may_spin_lock(unsigned int *lock) 264 { 265 if (!lock || !cpu_mmu_enabled()) 266 return 0; 267 268 return cpu_spin_lock_xsave(lock); 269 } 270 271 void may_spin_unlock(unsigned int *lock, uint32_t exceptions) 272 { 273 if (!lock || !cpu_mmu_enabled()) 274 return; 275 276 cpu_spin_unlock_xrestore(lock, exceptions); 277 } 278 279 static vaddr_t stm32_tamp_base(void) 280 { 281 static struct io_pa_va base = { .pa = TAMP_BASE }; 282 283 return io_pa_or_va_secure(&base, 1); 284 } 285 286 static vaddr_t bkpreg_base(void) 287 { 288 return stm32_tamp_base() + TAMP_BKP_REGISTER_OFF; 289 } 290 291 vaddr_t stm32mp_bkpreg(unsigned int idx) 292 { 293 return bkpreg_base() + (idx * sizeof(uint32_t)); 294 } 295 296 static bool __maybe_unused bank_is_valid(unsigned int bank) 297 { 298 if (IS_ENABLED(CFG_STM32MP15)) 299 return bank == GPIO_BANK_Z || bank <= GPIO_BANK_K; 300 301 if (IS_ENABLED(CFG_STM32MP13)) 302 return bank <= GPIO_BANK_I; 303 304 panic(); 305 } 306 307 vaddr_t stm32_get_gpio_bank_base(unsigned int bank) 308 { 309 static struct io_pa_va base = { .pa = GPIOA_BASE }; 310 311 static_assert(GPIO_BANK_A == 0); 312 assert(bank_is_valid(bank)); 313 314 if (IS_ENABLED(CFG_STM32MP15)) { 315 static struct io_pa_va zbase = { .pa = GPIOZ_BASE }; 316 317 /* Get secure mapping address for GPIOZ */ 318 if (bank == GPIO_BANK_Z) 319 return io_pa_or_va_secure(&zbase, GPIO_BANK_OFFSET); 320 321 /* Other are mapped non-secure */ 322 return io_pa_or_va_nsec(&base, (bank + 1) * GPIO_BANK_OFFSET) + 323 (bank * GPIO_BANK_OFFSET); 324 } 325 326 if (IS_ENABLED(CFG_STM32MP13)) 327 return io_pa_or_va_secure(&base, 328 (bank + 1) * GPIO_BANK_OFFSET) + 329 (bank * GPIO_BANK_OFFSET); 330 331 panic(); 332 } 333 334 unsigned int stm32_get_gpio_bank_offset(unsigned int bank) 335 { 336 assert(bank_is_valid(bank)); 337 338 if (bank == GPIO_BANK_Z) 339 return 0; 340 341 return bank * GPIO_BANK_OFFSET; 342 } 343 344 unsigned int stm32_get_gpio_bank_clock(unsigned int bank) 345 { 346 assert(bank_is_valid(bank)); 347 348 #ifdef CFG_STM32MP15 349 if (bank == GPIO_BANK_Z) 350 return GPIOZ; 351 #endif 352 353 return GPIOA + bank; 354 } 355 356 struct clk *stm32_get_gpio_bank_clk(unsigned int bank) 357 { 358 assert(bank_is_valid(bank)); 359 360 if (!IS_ENABLED(CFG_DRIVERS_CLK)) 361 return NULL; 362 363 return stm32mp_rcc_clock_id_to_clk(stm32_get_gpio_bank_clock(bank)); 364 } 365 366 #ifdef CFG_STM32_IWDG 367 TEE_Result stm32_get_iwdg_otp_config(paddr_t pbase, 368 struct stm32_iwdg_otp_data *otp_data) 369 { 370 unsigned int idx = 0; 371 uint32_t otp_value = 0; 372 373 switch (pbase) { 374 case IWDG1_BASE: 375 idx = 0; 376 break; 377 case IWDG2_BASE: 378 idx = 1; 379 break; 380 default: 381 panic(); 382 } 383 384 if (stm32_bsec_read_otp(&otp_value, HW2_OTP)) 385 panic(); 386 387 otp_data->hw_enabled = otp_value & 388 BIT(idx + HW2_OTP_IWDG_HW_ENABLE_SHIFT); 389 otp_data->disable_on_stop = otp_value & 390 BIT(idx + HW2_OTP_IWDG_FZ_STOP_SHIFT); 391 otp_data->disable_on_standby = otp_value & 392 BIT(idx + HW2_OTP_IWDG_FZ_STANDBY_SHIFT); 393 394 return TEE_SUCCESS; 395 } 396 #endif /*CFG_STM32_IWDG*/ 397