1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  *
4  *  Copyright (c) 2023 Nuvoton Technology Corporation.
5  */
6 
7 #define DT_DRV_COMPAT nuvoton_numaker_rst
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 /* Reset controller module IPRST offset */
15 #define NUMAKER_RESET_IPRST0_OFFSET (8UL)
16 #define NUMAKER_RESET_IP_OFFSET(id) (NUMAKER_RESET_IPRST0_OFFSET + (((id) >> 24UL) & 0xffUL))
17 /* Reset controller module configuration bit */
18 #define NUMAKER_RESET_IP_BIT(id)    (id & 0x00ffffffUL)
19 
20 struct reset_numaker_config {
21 	uint32_t base;
22 };
23 
reset_numaker_status(const struct device * dev,uint32_t id,uint8_t * status)24 static int reset_numaker_status(const struct device *dev, uint32_t id, uint8_t *status)
25 {
26 	const struct reset_numaker_config *config = dev->config;
27 
28 	*status = !!sys_test_bit(config->base + NUMAKER_RESET_IP_OFFSET(id),
29 				 NUMAKER_RESET_IP_BIT(id));
30 
31 	return 0;
32 }
33 
reset_numaker_line_assert(const struct device * dev,uint32_t id)34 static int reset_numaker_line_assert(const struct device *dev, uint32_t id)
35 {
36 	const struct reset_numaker_config *config = dev->config;
37 
38 	/* Generate reset signal to the corresponding module */
39 	sys_set_bit(config->base + NUMAKER_RESET_IP_OFFSET(id), NUMAKER_RESET_IP_BIT(id));
40 
41 	return 0;
42 }
43 
reset_numaker_line_deassert(const struct device * dev,uint32_t id)44 static int reset_numaker_line_deassert(const struct device *dev, uint32_t id)
45 {
46 	const struct reset_numaker_config *config = dev->config;
47 
48 	/* Release corresponding module from reset state */
49 	sys_clear_bit(config->base + NUMAKER_RESET_IP_OFFSET(id), NUMAKER_RESET_IP_BIT(id));
50 
51 	return 0;
52 }
53 
reset_numaker_line_toggle(const struct device * dev,uint32_t id)54 static int reset_numaker_line_toggle(const struct device *dev, uint32_t id)
55 {
56 	(void)reset_numaker_line_assert(dev, id);
57 	(void)reset_numaker_line_deassert(dev, id);
58 
59 	return 0;
60 }
61 
62 static const struct reset_driver_api reset_numaker_driver_api = {
63 	.status = reset_numaker_status,
64 	.line_assert = reset_numaker_line_assert,
65 	.line_deassert = reset_numaker_line_deassert,
66 	.line_toggle = reset_numaker_line_toggle,
67 };
68 
69 static const struct reset_numaker_config config = {
70 	.base = (uint32_t)DT_INST_REG_ADDR(0),
71 };
72 
73 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL, &config, PRE_KERNEL_1,
74 		      CONFIG_RESET_INIT_PRIORITY, &reset_numaker_driver_api);
75