1 /*
2 * Copyright (c) 2022 Teslabs Engineering S.L.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT gd_gd32_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 /** RCU offset (from id field) */
15 #define GD32_RESET_ID_OFFSET(id) (((id) >> 6U) & 0xFFU)
16 /** RCU configuration bit (from id field) */
17 #define GD32_RESET_ID_BIT(id) ((id) & 0x1FU)
18
19 struct reset_gd32_config {
20 uint32_t base;
21 };
22
reset_gd32_status(const struct device * dev,uint32_t id,uint8_t * status)23 static int reset_gd32_status(const struct device *dev, uint32_t id,
24 uint8_t *status)
25 {
26 const struct reset_gd32_config *config = dev->config;
27
28 *status = !!sys_test_bit(config->base + GD32_RESET_ID_OFFSET(id),
29 GD32_RESET_ID_BIT(id));
30
31 return 0;
32 }
33
reset_gd32_line_assert(const struct device * dev,uint32_t id)34 static int reset_gd32_line_assert(const struct device *dev, uint32_t id)
35 {
36 const struct reset_gd32_config *config = dev->config;
37
38 sys_set_bit(config->base + GD32_RESET_ID_OFFSET(id),
39 GD32_RESET_ID_BIT(id));
40
41 return 0;
42 }
43
reset_gd32_line_deassert(const struct device * dev,uint32_t id)44 static int reset_gd32_line_deassert(const struct device *dev, uint32_t id)
45 {
46 const struct reset_gd32_config *config = dev->config;
47
48 sys_clear_bit(config->base + GD32_RESET_ID_OFFSET(id),
49 GD32_RESET_ID_BIT(id));
50
51 return 0;
52 }
53
reset_gd32_line_toggle(const struct device * dev,uint32_t id)54 static int reset_gd32_line_toggle(const struct device *dev, uint32_t id)
55 {
56 (void)reset_gd32_line_assert(dev, id);
57 (void)reset_gd32_line_deassert(dev, id);
58
59 return 0;
60 }
61
62 static const struct reset_driver_api reset_gd32_driver_api = {
63 .status = reset_gd32_status,
64 .line_assert = reset_gd32_line_assert,
65 .line_deassert = reset_gd32_line_deassert,
66 .line_toggle = reset_gd32_line_toggle,
67 };
68
69 static const struct reset_gd32_config config = {
70 .base = DT_REG_ADDR(DT_INST_PARENT(0)),
71 };
72
73 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL, &config, PRE_KERNEL_1,
74 CONFIG_RESET_INIT_PRIORITY, &reset_gd32_driver_api);
75