1 /*
2  * Copyright 2024 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_rstctl
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/reset.h>
11 #include <zephyr/sys/util.h>
12 
13 #include <fsl_device_registers.h>
14 
15 #define NXP_RSTCTL_OFFSET(id) ((id >> 16) * sizeof(uint32_t))
16 #define NXP_RSTCTL_BIT(id) (BIT(id & 0xFFFF))
17 #define NXP_RSTCTL_CTL(id) (NXP_RSTCTL_OFFSET(id) + 0x10)
18 #define NXP_RSTCTL_SET(id) (NXP_RSTCTL_OFFSET(id) + 0x40)
19 #define NXP_RSTCTL_CLR(id) (NXP_RSTCTL_OFFSET(id) + 0x70)
20 
reset_nxp_rstctl_status(const struct device * dev,uint32_t id,uint8_t * status)21 static int reset_nxp_rstctl_status(const struct device *dev, uint32_t id, uint8_t *status)
22 {
23 	const uint32_t *base = dev->config;
24 	volatile const uint32_t *ctl_reg = base+(NXP_RSTCTL_CTL(id)/sizeof(uint32_t));
25 	uint32_t val = *ctl_reg;
26 
27 	*status = (uint8_t)FIELD_GET(NXP_RSTCTL_BIT(id), val);
28 
29 	return 0;
30 }
31 
reset_nxp_rstctl_line_assert(const struct device * dev,uint32_t id)32 static int reset_nxp_rstctl_line_assert(const struct device *dev, uint32_t id)
33 {
34 	const uint32_t *base = dev->config;
35 	volatile uint32_t *set_reg = (uint32_t *)base+(NXP_RSTCTL_SET(id)/sizeof(uint32_t));
36 
37 	*set_reg = FIELD_PREP(NXP_RSTCTL_BIT(id), 0b1);
38 
39 	return 0;
40 }
41 
reset_nxp_rstctl_line_deassert(const struct device * dev,uint32_t id)42 static int reset_nxp_rstctl_line_deassert(const struct device *dev, uint32_t id)
43 {
44 	const uint32_t *base = dev->config;
45 	volatile uint32_t *clr_reg = (uint32_t *)base+(NXP_RSTCTL_CLR(id)/sizeof(uint32_t));
46 
47 	*clr_reg = FIELD_PREP(NXP_RSTCTL_BIT(id), 0b1);
48 
49 	return 0;
50 }
51 
reset_nxp_rstctl_line_toggle(const struct device * dev,uint32_t id)52 static int reset_nxp_rstctl_line_toggle(const struct device *dev, uint32_t id)
53 {
54 	uint8_t status = 0;
55 
56 	reset_nxp_rstctl_line_assert(dev, id);
57 
58 	do {
59 		reset_nxp_rstctl_status(dev, id, &status);
60 	} while (status != 0b1);
61 
62 	reset_nxp_rstctl_line_deassert(dev, id);
63 
64 	return 0;
65 }
66 
67 static DEVICE_API(reset, reset_nxp_rstctl_driver_api) = {
68 	.status = reset_nxp_rstctl_status,
69 	.line_assert = reset_nxp_rstctl_line_assert,
70 	.line_deassert = reset_nxp_rstctl_line_deassert,
71 	.line_toggle = reset_nxp_rstctl_line_toggle,
72 };
73 
74 #define NXP_RSTCTL_INIT(n)						\
75 	DEVICE_DT_INST_DEFINE(n, NULL, NULL, NULL,			\
76 			      (void *)DT_INST_REG_ADDR(n),		\
77 			      PRE_KERNEL_1, CONFIG_RESET_INIT_PRIORITY,	\
78 			      &reset_nxp_rstctl_driver_api);
79 
80 DT_INST_FOREACH_STATUS_OKAY(NXP_RSTCTL_INIT)
81