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