1 /*
2 * Copyright (C) 2020, NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nxp_imx_wdog
8
9 #include <zephyr/drivers/pinctrl.h>
10 #include <zephyr/drivers/watchdog.h>
11 #include <zephyr/sys_clock.h>
12 #include <fsl_wdog.h>
13
14 #define LOG_LEVEL CONFIG_WDT_LOG_LEVEL
15 #include <zephyr/logging/log.h>
16 #include <zephyr/irq.h>
17 LOG_MODULE_REGISTER(wdt_mcux_wdog);
18
19 #define WDOG_TMOUT_SEC(x) (((x * 2) / MSEC_PER_SEC) - 1)
20
21 struct mcux_wdog_config {
22 WDOG_Type *base;
23 void (*irq_config_func)(const struct device *dev);
24 const struct pinctrl_dev_config *pcfg;
25 };
26
27 struct mcux_wdog_data {
28 wdt_callback_t callback;
29 wdog_config_t wdog_config;
30 bool timeout_valid;
31 };
32
mcux_wdog_setup(const struct device * dev,uint8_t options)33 static int mcux_wdog_setup(const struct device *dev, uint8_t options)
34 {
35 const struct mcux_wdog_config *config = dev->config;
36 struct mcux_wdog_data *data = dev->data;
37 WDOG_Type *base = config->base;
38
39 if (!data->timeout_valid) {
40 LOG_ERR("No valid timeouts installed");
41 return -EINVAL;
42 }
43
44 data->wdog_config.workMode.enableStop =
45 (options & WDT_OPT_PAUSE_IN_SLEEP) == 0U;
46
47 data->wdog_config.workMode.enableDebug =
48 (options & WDT_OPT_PAUSE_HALTED_BY_DBG) == 0U;
49
50 WDOG_Init(base, &data->wdog_config);
51 LOG_DBG("Setup the watchdog");
52
53 return 0;
54 }
55
mcux_wdog_disable(const struct device * dev)56 static int mcux_wdog_disable(const struct device *dev)
57 {
58 const struct mcux_wdog_config *config = dev->config;
59 struct mcux_wdog_data *data = dev->data;
60 WDOG_Type *base = config->base;
61
62 WDOG_Deinit(base);
63 data->timeout_valid = false;
64 LOG_DBG("Disabled the watchdog");
65
66 return 0;
67 }
68
mcux_wdog_install_timeout(const struct device * dev,const struct wdt_timeout_cfg * cfg)69 static int mcux_wdog_install_timeout(const struct device *dev,
70 const struct wdt_timeout_cfg *cfg)
71 {
72 struct mcux_wdog_data *data = dev->data;
73
74 if (data->timeout_valid) {
75 LOG_ERR("No more timeouts can be installed");
76 return -ENOMEM;
77 }
78
79 WDOG_GetDefaultConfig(&data->wdog_config);
80 data->wdog_config.interruptTimeValue = 0U;
81
82 if (cfg->window.max < (MSEC_PER_SEC / 2)) {
83 LOG_ERR("Invalid window max, shortest window is 500ms");
84 return -EINVAL;
85 }
86
87 data->wdog_config.timeoutValue =
88 WDOG_TMOUT_SEC(cfg->window.max);
89
90 if (cfg->window.min) {
91 LOG_ERR("Invalid window.min, Do not support window model");
92 return -EINVAL;
93 }
94 if (data->wdog_config.timeoutValue > 128) {
95 LOG_ERR("Invalid timeoutValue, valid (0.5s - 128.0s)");
96 return -EINVAL;
97 }
98
99 data->wdog_config.enableInterrupt = cfg->callback != NULL;
100 data->callback = cfg->callback;
101 data->timeout_valid = true;
102
103 return 0;
104 }
105
mcux_wdog_feed(const struct device * dev,int channel_id)106 static int mcux_wdog_feed(const struct device *dev, int channel_id)
107 {
108 const struct mcux_wdog_config *config = dev->config;
109 WDOG_Type *base = config->base;
110
111 if (channel_id != 0) {
112 LOG_ERR("Invalid channel id");
113 return -EINVAL;
114 }
115
116 WDOG_Refresh(base);
117 LOG_DBG("Fed the watchdog");
118
119 return 0;
120 }
121
mcux_wdog_isr(const struct device * dev)122 static void mcux_wdog_isr(const struct device *dev)
123 {
124 const struct mcux_wdog_config *config = dev->config;
125 struct mcux_wdog_data *data = dev->data;
126 WDOG_Type *base = config->base;
127 uint32_t flags;
128
129 flags = WDOG_GetStatusFlags(base);
130 WDOG_ClearInterruptStatus(base, flags);
131
132 if (data->callback) {
133 data->callback(dev, 0);
134 }
135 }
136
mcux_wdog_init(const struct device * dev)137 static int mcux_wdog_init(const struct device *dev)
138 {
139 const struct mcux_wdog_config *config = dev->config;
140 int ret;
141
142 config->irq_config_func(dev);
143
144 ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
145 if (ret < 0 && ret != -ENOENT) {
146 return ret;
147 }
148
149 return 0;
150 }
151
152 static DEVICE_API(wdt, mcux_wdog_api) = {
153 .setup = mcux_wdog_setup,
154 .disable = mcux_wdog_disable,
155 .install_timeout = mcux_wdog_install_timeout,
156 .feed = mcux_wdog_feed,
157 };
158
159 static void mcux_wdog_config_func(const struct device *dev);
160
161 PINCTRL_DT_INST_DEFINE(0);
162
163 static const struct mcux_wdog_config mcux_wdog_config = {
164 .base = (WDOG_Type *) DT_INST_REG_ADDR(0),
165 .irq_config_func = mcux_wdog_config_func,
166 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0),
167 };
168
169 static struct mcux_wdog_data mcux_wdog_data;
170
171 DEVICE_DT_INST_DEFINE(0,
172 &mcux_wdog_init,
173 NULL,
174 &mcux_wdog_data, &mcux_wdog_config,
175 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
176 &mcux_wdog_api);
177
mcux_wdog_config_func(const struct device * dev)178 static void mcux_wdog_config_func(const struct device *dev)
179 {
180 IRQ_CONNECT(DT_INST_IRQN(0),
181 DT_INST_IRQ(0, priority),
182 mcux_wdog_isr, DEVICE_DT_INST_GET(0), 0);
183
184 irq_enable(DT_INST_IRQN(0));
185 }
186