1 /*
2  * Copyright 2024 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_lpc_syscon_reset
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 LPC_RESET_OFFSET(id) (id >> 16)
16 #define LPC_RESET_BIT(id) (BIT(id & 0xFFFF))
17 
reset_nxp_syscon_status(const struct device * dev,uint32_t id,uint8_t * status)18 static int reset_nxp_syscon_status(const struct device *dev, uint32_t id, uint8_t *status)
19 {
20 	const volatile uint32_t *ctrl_reg = ((uint32_t *)dev->config)+(LPC_RESET_OFFSET(id));
21 	*status = (uint8_t)FIELD_GET((uint32_t)LPC_RESET_BIT(id), *ctrl_reg);
22 
23 	return 0;
24 }
25 
reset_nxp_syscon_line_assert(const struct device * dev,uint32_t id)26 static int reset_nxp_syscon_line_assert(const struct device *dev, uint32_t id)
27 {
28 	SYSCON->PRESETCTRLSET[LPC_RESET_OFFSET(id)] = FIELD_PREP(LPC_RESET_BIT(id), 0b1);
29 
30 	return 0;
31 }
32 
reset_nxp_syscon_line_deassert(const struct device * dev,uint32_t id)33 static int reset_nxp_syscon_line_deassert(const struct device *dev, uint32_t id)
34 {
35 	SYSCON->PRESETCTRLCLR[LPC_RESET_OFFSET(id)] = FIELD_PREP(LPC_RESET_BIT(id), 0b1);
36 
37 	return 0;
38 }
39 
reset_nxp_syscon_line_toggle(const struct device * dev,uint32_t id)40 static int reset_nxp_syscon_line_toggle(const struct device *dev, uint32_t id)
41 {
42 	uint8_t status = 0;
43 
44 	reset_nxp_syscon_line_assert(dev, id);
45 
46 	do {
47 		reset_nxp_syscon_status(dev, id, &status);
48 	} while (status != 0b1);
49 
50 	reset_nxp_syscon_line_deassert(dev, id);
51 
52 	return 0;
53 }
54 
55 static const struct reset_driver_api reset_nxp_syscon_driver_api = {
56 	.status = reset_nxp_syscon_status,
57 	.line_assert = reset_nxp_syscon_line_assert,
58 	.line_deassert = reset_nxp_syscon_line_deassert,
59 	.line_toggle = reset_nxp_syscon_line_toggle,
60 };
61 
62 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL,
63 			(void *)(DT_REG_ADDR(DT_INST_PARENT(0)) + 0x100),
64 			PRE_KERNEL_1, CONFIG_RESET_INIT_PRIORITY,
65 			&reset_nxp_syscon_driver_api);
66