1 /*
2  * Copyright (c) 2022 Google Inc
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT st_stm32_rcc_rctl
8 
9 #include <zephyr/arch/cpu.h>
10 #include <zephyr/device.h>
11 #include <zephyr/devicetree.h>
12 #include <zephyr/drivers/reset.h>
13 
14 #define STM32_RESET_CLR_OFFSET(id) (((id) >> 17U) & 0xFFFU)
15 #define STM32_RESET_SET_OFFSET(id) (((id) >> 5U) & 0xFFFU)
16 #define STM32_RESET_REG_BIT(id)	   ((id)&0x1FU)
17 
18 struct reset_stm32_config {
19 	uintptr_t base;
20 };
21 
reset_stm32_status(const struct device * dev,uint32_t id,uint8_t * status)22 static int reset_stm32_status(const struct device *dev, uint32_t id,
23 			      uint8_t *status)
24 {
25 	const struct reset_stm32_config *config = dev->config;
26 
27 	*status = !!sys_test_bit(config->base + STM32_RESET_SET_OFFSET(id),
28 				 STM32_RESET_REG_BIT(id));
29 
30 	return 0;
31 }
32 
reset_stm32_line_assert(const struct device * dev,uint32_t id)33 static int reset_stm32_line_assert(const struct device *dev, uint32_t id)
34 {
35 	const struct reset_stm32_config *config = dev->config;
36 
37 	sys_set_bit(config->base + STM32_RESET_SET_OFFSET(id),
38 		    STM32_RESET_REG_BIT(id));
39 
40 	return 0;
41 }
42 
reset_stm32_line_deassert(const struct device * dev,uint32_t id)43 static int reset_stm32_line_deassert(const struct device *dev, uint32_t id)
44 {
45 	const struct reset_stm32_config *config = dev->config;
46 
47 #if DT_INST_PROP(0, set_bit_to_deassert)
48 	sys_set_bit(config->base + STM32_RESET_CLR_OFFSET(id),
49 		    STM32_RESET_REG_BIT(id));
50 #else
51 	sys_clear_bit(config->base + STM32_RESET_SET_OFFSET(id),
52 		      STM32_RESET_REG_BIT(id));
53 #endif
54 
55 	return 0;
56 }
57 
reset_stm32_line_toggle(const struct device * dev,uint32_t id)58 static int reset_stm32_line_toggle(const struct device *dev, uint32_t id)
59 {
60 	reset_stm32_line_assert(dev, id);
61 	reset_stm32_line_deassert(dev, id);
62 
63 	return 0;
64 }
65 
66 static const struct reset_driver_api reset_stm32_driver_api = {
67 	.status = reset_stm32_status,
68 	.line_assert = reset_stm32_line_assert,
69 	.line_deassert = reset_stm32_line_deassert,
70 	.line_toggle = reset_stm32_line_toggle,
71 };
72 
73 static const struct reset_stm32_config reset_stm32_config = {
74 	.base = DT_REG_ADDR(DT_INST_PARENT(0)),
75 };
76 
77 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL, &reset_stm32_config, PRE_KERNEL_1,
78 		      CONFIG_RESET_INIT_PRIORITY, &reset_stm32_driver_api);
79