1 /*
2  * Copyright (c) 2018-2019, STMicroelectronics - All Rights Reserved
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <errno.h>
8 #include <limits.h>
9 
10 #include <platform_def.h>
11 
12 #include <common/bl_common.h>
13 #include <common/debug.h>
14 #include <drivers/delay_timer.h>
15 #include <drivers/st/stm32mp_reset.h>
16 #include <lib/mmio.h>
17 #include <lib/utils_def.h>
18 
id2reg_offset(unsigned int reset_id)19 static uint32_t id2reg_offset(unsigned int reset_id)
20 {
21 	return ((reset_id & GENMASK(31, 5)) >> 5) * sizeof(uint32_t);
22 }
23 
id2reg_bit_pos(unsigned int reset_id)24 static uint8_t id2reg_bit_pos(unsigned int reset_id)
25 {
26 	return (uint8_t)(reset_id & GENMASK(4, 0));
27 }
28 
stm32mp_reset_assert(uint32_t id,unsigned int to_us)29 int stm32mp_reset_assert(uint32_t id, unsigned int to_us)
30 {
31 	uint32_t offset = id2reg_offset(id);
32 	uint32_t bitmsk = BIT(id2reg_bit_pos(id));
33 	uintptr_t rcc_base = stm32mp_rcc_base();
34 
35 	mmio_write_32(rcc_base + offset, bitmsk);
36 
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 			}
44 		}
45 	}
46 
47 	return 0;
48 }
49 
stm32mp_reset_deassert(uint32_t id,unsigned int to_us)50 int stm32mp_reset_deassert(uint32_t id, unsigned int to_us)
51 {
52 	uint32_t offset = id2reg_offset(id) + RCC_RSTCLRR_OFFSET;
53 	uint32_t bitmsk = BIT(id2reg_bit_pos(id));
54 	uintptr_t rcc_base = stm32mp_rcc_base();
55 
56 	mmio_write_32(rcc_base + offset, bitmsk);
57 
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 			}
65 		}
66 	}
67 
68 	return 0;
69 }
70