stm32mp1_reset.c 1.47 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2019, STMicroelectronics - All Rights Reserved
3
4
5
6
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

7
#include <errno.h>
8
#include <limits.h>
9

10
#include <platform_def.h>
11
12
13

#include <common/bl_common.h>
#include <common/debug.h>
14
#include <drivers/delay_timer.h>
15
#include <drivers/st/stm32mp_reset.h>
16
17
#include <lib/mmio.h>
#include <lib/utils_def.h>
18

19
20
21
22
23
24
25
26
27
static uint32_t id2reg_offset(unsigned int reset_id)
{
	return ((reset_id & GENMASK(31, 5)) >> 5) * sizeof(uint32_t);
}

static uint8_t id2reg_bit_pos(unsigned int reset_id)
{
	return (uint8_t)(reset_id & GENMASK(4, 0));
}
28

29
int stm32mp_reset_assert(uint32_t id, unsigned int to_us)
30
{
31
32
	uint32_t offset = id2reg_offset(id);
	uint32_t bitmsk = BIT(id2reg_bit_pos(id));
33
	uintptr_t rcc_base = stm32mp_rcc_base();
34

35
36
	mmio_write_32(rcc_base + offset, bitmsk);

37
38
39
40
41
42
43
	if (to_us != 0U) {
		uint64_t timeout_ref = timeout_init_us(to_us);

		while ((mmio_read_32(rcc_base + offset) & bitmsk) == 0U) {
			if (timeout_elapsed(timeout_ref)) {
				return -ETIMEDOUT;
			}
44
		}
45
	}
46
47

	return 0;
48
49
}

50
int stm32mp_reset_deassert(uint32_t id, unsigned int to_us)
51
{
52
53
	uint32_t offset = id2reg_offset(id) + RCC_RSTCLRR_OFFSET;
	uint32_t bitmsk = BIT(id2reg_bit_pos(id));
54
	uintptr_t rcc_base = stm32mp_rcc_base();
55

56
57
	mmio_write_32(rcc_base + offset, bitmsk);

58
59
60
61
62
63
64
	if (to_us != 0U) {
		uint64_t timeout_ref = timeout_init_us(to_us);

		while ((mmio_read_32(rcc_base + offset) & bitmsk) != 0U) {
			if (timeout_elapsed(timeout_ref)) {
				return -ETIMEDOUT;
			}
65
		}
66
	}
67
68

	return 0;
69
}