1 /*
2  * Copyright (c) 2020, Jiri Kerestes
3  *
4  * Based on wdt_mcux_wdog32.c, which is:
5  * Copyright (c) 2019 Vestas Wind Systems A/S
6  * Copyright (c) 2018, NXP
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #define DT_DRV_COMPAT nxp_lpc_wwdt
12 
13 #include <zephyr/drivers/watchdog.h>
14 #include <zephyr/irq.h>
15 #include <zephyr/sys_clock.h>
16 #include <fsl_wwdt.h>
17 #include <fsl_clock.h>
18 
19 #define LOG_LEVEL CONFIG_WDT_LOG_LEVEL
20 #include <zephyr/logging/log.h>
21 LOG_MODULE_REGISTER(wdt_mcux_wwdt);
22 
23 #define MIN_TIMEOUT 0xFF
24 
25 struct mcux_wwdt_config {
26 	WWDT_Type *base;
27 	uint8_t clk_divider;
28 	void (*irq_config_func)(const struct device *dev);
29 };
30 
31 struct mcux_wwdt_data {
32 	wdt_callback_t callback;
33 	wwdt_config_t wwdt_config;
34 	bool timeout_valid;
35 };
36 
mcux_wwdt_setup(const struct device * dev,uint8_t options)37 static int mcux_wwdt_setup(const struct device *dev, uint8_t options)
38 {
39 	const struct mcux_wwdt_config *config = dev->config;
40 	struct mcux_wwdt_data *data = dev->data;
41 	WWDT_Type *base = config->base;
42 
43 	if (!data->timeout_valid) {
44 		LOG_ERR("No valid timeouts installed");
45 		return -EINVAL;
46 	}
47 
48 	WWDT_Init(base, &data->wwdt_config);
49 	LOG_DBG("Setup the watchdog");
50 
51 	return 0;
52 }
53 
mcux_wwdt_disable(const struct device * dev)54 static int mcux_wwdt_disable(const struct device *dev)
55 {
56 	const struct mcux_wwdt_config *config = dev->config;
57 	struct mcux_wwdt_data *data = dev->data;
58 	WWDT_Type *base = config->base;
59 
60 	WWDT_Deinit(base);
61 	data->timeout_valid = false;
62 	LOG_DBG("Disabled the watchdog");
63 
64 	return 0;
65 }
66 
67 /*
68  * LPC55xxx WWDT has a fixed divide-by-4 clock prescaler.
69  * This prescaler is different from the clock divider specified in Device Tree.
70  */
71 #define MSEC_TO_WWDT_TICKS(clock_freq, msec) \
72 	((uint32_t)((clock_freq / MSEC_PER_SEC) * msec) / 4)
73 
mcux_wwdt_install_timeout(const struct device * dev,const struct wdt_timeout_cfg * cfg)74 static int mcux_wwdt_install_timeout(const struct device *dev,
75 				     const struct wdt_timeout_cfg *cfg)
76 {
77 	struct mcux_wwdt_data *data = dev->data;
78 	uint32_t clock_freq;
79 
80 	if (data->timeout_valid) {
81 		LOG_ERR("No more timeouts can be installed");
82 		return -ENOMEM;
83 	}
84 
85 #if defined(CONFIG_SOC_MIMXRT685S_CM33) || defined(CONFIG_SOC_MIMXRT595S_CM33) \
86 	|| defined(CONFIG_SOC_SERIES_MCXN)
87 	clock_freq = CLOCK_GetWdtClkFreq(0);
88 #elif defined(CONFIG_SOC_SERIES_RW6XX)
89 	clock_freq = CLOCK_GetWdtClkFreq();
90 #else
91 	const struct mcux_wwdt_config *config = dev->config;
92 
93 	CLOCK_SetClkDiv(kCLOCK_DivWdtClk, config->clk_divider, true);
94 	clock_freq = CLOCK_GetWdtClkFreq();
95 #endif
96 
97 	WWDT_GetDefaultConfig(&data->wwdt_config);
98 
99 	data->wwdt_config.clockFreq_Hz = clock_freq;
100 
101 	data->wwdt_config.timeoutValue =
102 		MSEC_TO_WWDT_TICKS(clock_freq, cfg->window.max);
103 
104 	if (cfg->window.min) {
105 		data->wwdt_config.windowValue =
106 			MSEC_TO_WWDT_TICKS(clock_freq, cfg->window.min);
107 	}
108 
109 	if ((data->wwdt_config.timeoutValue < MIN_TIMEOUT) ||
110 	    ((data->wwdt_config.windowValue != 0xFFFFFFU) &&
111 	     (data->wwdt_config.timeoutValue <
112 	      data->wwdt_config.windowValue))) {
113 		return -EINVAL;
114 	}
115 
116 	if (cfg->flags & WDT_FLAG_RESET_SOC) {
117 		data->wwdt_config.enableWatchdogReset = true;
118 		LOG_DBG("Enabling SoC reset");
119 	}
120 
121 	if (cfg->callback && (CONFIG_WDT_MCUX_WWDT_WARNING_INTERRUPT_CFG > 0)) {
122 		data->callback = cfg->callback;
123 		data->wwdt_config.warningValue = CONFIG_WDT_MCUX_WWDT_WARNING_INTERRUPT_CFG;
124 	} else if (cfg->callback) {
125 		return -ENOTSUP;
126 	}
127 
128 
129 	data->timeout_valid = true;
130 	LOG_DBG("Installed timeout (timeoutValue = %d)",
131 		data->wwdt_config.timeoutValue);
132 
133 	return 0;
134 }
135 
mcux_wwdt_feed(const struct device * dev,int channel_id)136 static int mcux_wwdt_feed(const struct device *dev, int channel_id)
137 {
138 	const struct mcux_wwdt_config *config = dev->config;
139 	WWDT_Type *base = config->base;
140 
141 	if (channel_id != 0) {
142 		LOG_ERR("Invalid channel id");
143 		return -EINVAL;
144 	}
145 
146 	WWDT_Refresh(base);
147 	LOG_DBG("Fed the watchdog");
148 
149 	return 0;
150 }
151 
mcux_wwdt_isr(const struct device * dev)152 static void mcux_wwdt_isr(const struct device *dev)
153 {
154 	const struct mcux_wwdt_config *config = dev->config;
155 	struct mcux_wwdt_data *data = dev->data;
156 	WWDT_Type *base = config->base;
157 	uint32_t flags;
158 
159 	flags = WWDT_GetStatusFlags(base);
160 	WWDT_ClearStatusFlags(base, flags);
161 
162 	if (data->callback) {
163 		data->callback(dev, 0);
164 	}
165 }
166 
mcux_wwdt_init(const struct device * dev)167 static int mcux_wwdt_init(const struct device *dev)
168 {
169 	const struct mcux_wwdt_config *config = dev->config;
170 
171 	config->irq_config_func(dev);
172 
173 	return 0;
174 }
175 
176 static DEVICE_API(wdt, mcux_wwdt_api) = {
177 	.setup = mcux_wwdt_setup,
178 	.disable = mcux_wwdt_disable,
179 	.install_timeout = mcux_wwdt_install_timeout,
180 	.feed = mcux_wwdt_feed,
181 };
182 
183 static void mcux_wwdt_config_func_0(const struct device *dev);
184 
185 static const struct mcux_wwdt_config mcux_wwdt_config_0 = {
186 	.base = (WWDT_Type *) DT_INST_REG_ADDR(0),
187 	.clk_divider =
188 		DT_INST_PROP(0, clk_divider),
189 	.irq_config_func = mcux_wwdt_config_func_0,
190 };
191 
192 static struct mcux_wwdt_data mcux_wwdt_data_0;
193 
194 DEVICE_DT_INST_DEFINE(0, &mcux_wwdt_init,
195 		    NULL, &mcux_wwdt_data_0,
196 		    &mcux_wwdt_config_0, POST_KERNEL,
197 		    CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
198 		    &mcux_wwdt_api);
199 
mcux_wwdt_config_func_0(const struct device * dev)200 static void mcux_wwdt_config_func_0(const struct device *dev)
201 {
202 	IRQ_CONNECT(DT_INST_IRQN(0),
203 		    DT_INST_IRQ(0, priority),
204 		    mcux_wwdt_isr, DEVICE_DT_INST_GET(0), 0);
205 
206 	irq_enable(DT_INST_IRQN(0));
207 }
208