Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, STMicroelectronics - All Rights Reserved |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Etienne Carriere | f02647a | 2019-12-08 08:14:40 +0100 | [diff] [blame] | 7 | #include <errno.h> |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 8 | #include <limits.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 10 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
| 12 | #include <common/bl_common.h> |
| 13 | #include <common/debug.h> |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 14 | #include <drivers/delay_timer.h> |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 15 | #include <drivers/st/stm32mp_reset.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <lib/mmio.h> |
| 17 | #include <lib/utils_def.h> |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 18 | |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 19 | static uint32_t id2reg_offset(unsigned int reset_id) |
| 20 | { |
| 21 | return ((reset_id & GENMASK(31, 5)) >> 5) * sizeof(uint32_t); |
| 22 | } |
| 23 | |
| 24 | static uint8_t id2reg_bit_pos(unsigned int reset_id) |
| 25 | { |
| 26 | return (uint8_t)(reset_id & GENMASK(4, 0)); |
| 27 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 28 | |
Etienne Carriere | f02647a | 2019-12-08 08:14:40 +0100 | [diff] [blame] | 29 | int stm32mp_reset_assert(uint32_t id, unsigned int to_us) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 30 | { |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 31 | uint32_t offset = id2reg_offset(id); |
| 32 | uint32_t bitmsk = BIT(id2reg_bit_pos(id)); |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 33 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 34 | |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 35 | mmio_write_32(rcc_base + offset, bitmsk); |
| 36 | |
Etienne Carriere | f02647a | 2019-12-08 08:14:40 +0100 | [diff] [blame] | 37 | if (to_us != 0U) { |
| 38 | uint64_t timeout_ref = timeout_init_us(to_us); |
| 39 | |
| 40 | while ((mmio_read_32(rcc_base + offset) & bitmsk) == 0U) { |
| 41 | if (timeout_elapsed(timeout_ref)) { |
| 42 | return -ETIMEDOUT; |
| 43 | } |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 44 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 45 | } |
Etienne Carriere | f02647a | 2019-12-08 08:14:40 +0100 | [diff] [blame] | 46 | |
| 47 | return 0; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Etienne Carriere | f02647a | 2019-12-08 08:14:40 +0100 | [diff] [blame] | 50 | int stm32mp_reset_deassert(uint32_t id, unsigned int to_us) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 51 | { |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 52 | uint32_t offset = id2reg_offset(id) + RCC_RSTCLRR_OFFSET; |
| 53 | uint32_t bitmsk = BIT(id2reg_bit_pos(id)); |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 54 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 55 | |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 56 | mmio_write_32(rcc_base + offset, bitmsk); |
| 57 | |
Etienne Carriere | f02647a | 2019-12-08 08:14:40 +0100 | [diff] [blame] | 58 | if (to_us != 0U) { |
| 59 | uint64_t timeout_ref = timeout_init_us(to_us); |
| 60 | |
| 61 | while ((mmio_read_32(rcc_base + offset) & bitmsk) != 0U) { |
| 62 | if (timeout_elapsed(timeout_ref)) { |
| 63 | return -ETIMEDOUT; |
| 64 | } |
Yann Gautier | 33b1be4 | 2019-02-14 09:17:55 +0100 | [diff] [blame] | 65 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 66 | } |
Etienne Carriere | f02647a | 2019-12-08 08:14:40 +0100 | [diff] [blame] | 67 | |
| 68 | return 0; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 69 | } |