/
wdt_wwdgt_gd32.c
225 lines (181 loc) · 5.23 KB
/
wdt_wwdgt_gd32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2022 TOKITA Hiroshi
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT gd_gd32_wwdgt
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/clock_control/gd32.h>
#include <zephyr/drivers/reset.h>
#include <zephyr/drivers/watchdog.h>
#include <zephyr/irq.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys_clock.h>
#include <gd32_wwdgt.h>
LOG_MODULE_REGISTER(wdt_wwdgt_gd32, CONFIG_WDT_LOG_LEVEL);
#define WWDGT_PRESCALER_EXP_MAX (3U)
#define WWDGT_COUNTER_MIN (0x40U)
#define WWDGT_COUNTER_MAX (0x7fU)
#define WWDGT_INTERNAL_DIVIDER (4096ULL)
struct gd32_wwdgt_config {
uint16_t clkid;
struct reset_dt_spec reset;
};
/* mutable driver data */
struct gd32_wwdgt_data {
/* counter update value*/
uint8_t counter;
/* user defined callback */
wdt_callback_t callback;
};
static void gd32_wwdgt_irq_config(const struct device *dev);
/**
* @param timeout Timeout value in milliseconds.
* @param exp exponent part of prescaler
*
* @return ticks count calculated by this formula.
*
* timeout = pclk * INTERNAL_DIVIDER * (2^prescaler_exp) * (count + 1)
* transform as
* count = (timeout * pclk / INTERNAL_DIVIDER * (2^prescaler_exp) ) - 1
*
* and add WWDGT_COUNTER_MIN to this as a offset value.
*/
static inline uint32_t gd32_wwdgt_calc_ticks(const struct device *dev,
uint32_t timeout, uint32_t exp)
{
const struct gd32_wwdgt_config *config = dev->config;
uint32_t pclk;
(void)clock_control_get_rate(GD32_CLOCK_CONTROLLER,
(clock_control_subsys_t)&config->clkid,
&pclk);
return ((timeout * pclk)
/ (WWDGT_INTERNAL_DIVIDER * (1 << exp) * MSEC_PER_SEC) - 1)
+ WWDGT_COUNTER_MIN;
}
/**
* @brief Calculates WWDGT config value from timeout window.
*
* @param win Pointer to timeout window struct.
* @param counter Pointer to the storage of counter value.
* @param wval Pointer to the storage of window value.
* @param prescaler Pointer to the storage of prescaler value.
*
* @return 0 on success, -EINVAL if the window-max is out of range
*/
static int gd32_wwdgt_calc_window(const struct device *dev,
const struct wdt_window *win,
uint32_t *counter, uint32_t *wval,
uint32_t *prescaler)
{
for (uint32_t shift = 0U; shift <= WWDGT_PRESCALER_EXP_MAX; shift++) {
uint32_t max_count = gd32_wwdgt_calc_ticks(dev, win->max, shift);
if (max_count <= WWDGT_COUNTER_MAX) {
*counter = max_count;
*prescaler = CFG_PSC(shift);
if (win->min == 0U) {
*wval = max_count;
} else {
*wval = gd32_wwdgt_calc_ticks(dev, win->min, shift);
}
return 0;
}
}
return -EINVAL;
}
static int gd32_wwdgt_setup(const struct device *dev, uint8_t options)
{
ARG_UNUSED(dev);
if (options & WDT_OPT_PAUSE_HALTED_BY_DBG) {
#if CONFIG_GD32_DBG_SUPPORT
dbg_periph_enable(DBG_WWDGT_HOLD);
#else
LOG_ERR("Debug support not enabled");
return -ENOTSUP;
#endif
}
if (options & WDT_OPT_PAUSE_IN_SLEEP) {
LOG_ERR("WDT_OPT_PAUSE_IN_SLEEP not supported");
return -ENOTSUP;
}
wwdgt_enable();
wwdgt_flag_clear();
wwdgt_interrupt_enable();
return 0;
}
static int gd32_wwdgt_disable(const struct device *dev)
{
/* watchdog cannot be stopped once started */
ARG_UNUSED(dev);
return -EPERM;
}
static int gd32_wwdgt_install_timeout(const struct device *dev,
const struct wdt_timeout_cfg *config)
{
uint32_t prescaler = 0U;
uint32_t counter = 0U;
uint32_t window = 0U;
struct gd32_wwdgt_data *data = dev->data;
if (config->window.max == 0U) {
LOG_ERR("window.max must be non-zero");
return -EINVAL;
}
if (gd32_wwdgt_calc_window(dev, &config->window, &counter, &window,
&prescaler) != 0) {
LOG_ERR("window.max in out of range");
return -EINVAL;
}
data->callback = config->callback;
data->counter = counter;
wwdgt_config(counter, window, prescaler);
return 0;
}
static int gd32_wwdgt_feed(const struct device *dev, int channel_id)
{
struct gd32_wwdgt_data *data = dev->data;
ARG_UNUSED(channel_id);
wwdgt_counter_update(data->counter);
return 0;
}
static void gd32_wwdgt_isr(const struct device *dev)
{
struct gd32_wwdgt_data *data = dev->data;
if (wwdgt_flag_get() != 0) {
wwdgt_flag_clear();
if (data->callback != NULL) {
data->callback(dev, 0);
}
}
}
static void gd32_wwdgt_irq_config(const struct device *dev)
{
ARG_UNUSED(dev);
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority), gd32_wwdgt_isr,
DEVICE_DT_INST_GET(0), 0);
irq_enable(DT_INST_IRQN(0));
}
static const struct wdt_driver_api wwdgt_gd32_api = {
.setup = gd32_wwdgt_setup,
.disable = gd32_wwdgt_disable,
.install_timeout = gd32_wwdgt_install_timeout,
.feed = gd32_wwdgt_feed,
};
static int gd32_wwdgt_init(const struct device *dev)
{
const struct gd32_wwdgt_config *config = dev->config;
(void)clock_control_on(GD32_CLOCK_CONTROLLER,
(clock_control_subsys_t)&config->clkid);
(void)reset_line_toggle_dt(&config->reset);
gd32_wwdgt_irq_config(dev);
return 0;
}
static const struct gd32_wwdgt_config wwdgt_cfg = {
.clkid = DT_INST_CLOCKS_CELL(0, id),
.reset = RESET_DT_SPEC_INST_GET(0),
};
static struct gd32_wwdgt_data wwdgt_data = {
.counter = WWDGT_COUNTER_MIN,
.callback = NULL
};
DEVICE_DT_INST_DEFINE(0, gd32_wwdgt_init, NULL, &wwdgt_data, &wwdgt_cfg, POST_KERNEL,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &wwdgt_gd32_api);