1*f829d7dfSGabriel Fernandez /* 2*f829d7dfSGabriel Fernandez * Copyright (c) 2024, STMicroelectronics - All Rights Reserved 3*f829d7dfSGabriel Fernandez * 4*f829d7dfSGabriel Fernandez * SPDX-License-Identifier: BSD-3-Clause 5*f829d7dfSGabriel Fernandez */ 6*f829d7dfSGabriel Fernandez 7*f829d7dfSGabriel Fernandez #include <errno.h> 8*f829d7dfSGabriel Fernandez #include <stdbool.h> 9*f829d7dfSGabriel Fernandez 10*f829d7dfSGabriel Fernandez #include <common/debug.h> 11*f829d7dfSGabriel Fernandez #include <drivers/delay_timer.h> 12*f829d7dfSGabriel Fernandez #include <drivers/st/stm32mp_reset.h> 13*f829d7dfSGabriel Fernandez #include <lib/mmio.h> 14*f829d7dfSGabriel Fernandez #include <lib/utils_def.h> 15*f829d7dfSGabriel Fernandez 16*f829d7dfSGabriel Fernandez #include <platform_def.h> 17*f829d7dfSGabriel Fernandez 18*f829d7dfSGabriel Fernandez static uint32_t id2reg_offset(unsigned int reset_id) 19*f829d7dfSGabriel Fernandez { 20*f829d7dfSGabriel Fernandez return ((reset_id & GENMASK(31, 5)) >> 5) * sizeof(uint32_t); 21*f829d7dfSGabriel Fernandez } 22*f829d7dfSGabriel Fernandez 23*f829d7dfSGabriel Fernandez static uint8_t id2reg_bit_pos(unsigned int reset_id) 24*f829d7dfSGabriel Fernandez { 25*f829d7dfSGabriel Fernandez return (uint8_t)(reset_id & GENMASK(4, 0)); 26*f829d7dfSGabriel Fernandez } 27*f829d7dfSGabriel Fernandez 28*f829d7dfSGabriel Fernandez static int reset_toggle(uint32_t id, unsigned int to_us, bool reset_status) 29*f829d7dfSGabriel Fernandez { 30*f829d7dfSGabriel Fernandez uint32_t offset = id2reg_offset(id); 31*f829d7dfSGabriel Fernandez uint32_t bitmsk = BIT(id2reg_bit_pos(id)); 32*f829d7dfSGabriel Fernandez uint32_t bit_check; 33*f829d7dfSGabriel Fernandez uintptr_t rcc_base = stm32mp_rcc_base(); 34*f829d7dfSGabriel Fernandez 35*f829d7dfSGabriel Fernandez if (reset_status) { 36*f829d7dfSGabriel Fernandez mmio_setbits_32(rcc_base + offset, bitmsk); 37*f829d7dfSGabriel Fernandez bit_check = bitmsk; 38*f829d7dfSGabriel Fernandez } else { 39*f829d7dfSGabriel Fernandez mmio_clrbits_32(rcc_base + offset, bitmsk); 40*f829d7dfSGabriel Fernandez bit_check = 0U; 41*f829d7dfSGabriel Fernandez } 42*f829d7dfSGabriel Fernandez 43*f829d7dfSGabriel Fernandez if (to_us != 0U) { 44*f829d7dfSGabriel Fernandez uint64_t timeout_ref = timeout_init_us(to_us); 45*f829d7dfSGabriel Fernandez 46*f829d7dfSGabriel Fernandez while ((mmio_read_32(rcc_base + offset) & bitmsk) != bit_check) { 47*f829d7dfSGabriel Fernandez if (timeout_elapsed(timeout_ref)) { 48*f829d7dfSGabriel Fernandez return -ETIMEDOUT; 49*f829d7dfSGabriel Fernandez } 50*f829d7dfSGabriel Fernandez } 51*f829d7dfSGabriel Fernandez } 52*f829d7dfSGabriel Fernandez 53*f829d7dfSGabriel Fernandez return 0; 54*f829d7dfSGabriel Fernandez } 55*f829d7dfSGabriel Fernandez 56*f829d7dfSGabriel Fernandez int stm32mp_reset_assert(uint32_t id, unsigned int to_us) 57*f829d7dfSGabriel Fernandez { 58*f829d7dfSGabriel Fernandez return reset_toggle(id, to_us, true); 59*f829d7dfSGabriel Fernandez } 60*f829d7dfSGabriel Fernandez 61*f829d7dfSGabriel Fernandez int stm32mp_reset_deassert(uint32_t id, unsigned int to_us) 62*f829d7dfSGabriel Fernandez { 63*f829d7dfSGabriel Fernandez return reset_toggle(id, to_us, false); 64*f829d7dfSGabriel Fernandez } 65*f829d7dfSGabriel Fernandez 66*f829d7dfSGabriel Fernandez void __dead2 stm32mp_system_reset(void) 67*f829d7dfSGabriel Fernandez { 68*f829d7dfSGabriel Fernandez uintptr_t rcc_base = stm32mp_rcc_base(); 69*f829d7dfSGabriel Fernandez 70*f829d7dfSGabriel Fernandez mmio_setbits_32(rcc_base + RCC_GRSTCSETR, RCC_GRSTCSETR_SYSRST); 71*f829d7dfSGabriel Fernandez 72*f829d7dfSGabriel Fernandez /* Loop in case system reset is not immediately caught */ 73*f829d7dfSGabriel Fernandez while (true) { 74*f829d7dfSGabriel Fernandez wfi(); 75*f829d7dfSGabriel Fernandez } 76*f829d7dfSGabriel Fernandez } 77