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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <riscv_io.h>
#include "board.h"
#include "ioremap.h"
#include "sysctl_clk.h"
#include "drv_wdt.h"
#include <rtdbg.h>
#include <sys/ioctl.h>
struct k230_wdt_dev
{
struct rt_watchdog_device device;
const char *name;
rt_ubase_t base;
size_t size;
sysctl_clk_node_e clk; /* clock source */
};
/* There are sixteen TOPs (timeout periods) that can be set in the watchdog. */
static const rt_uint32_t *tops;
static const rt_uint32_t k230_wdt_fix_tops[KD_WDT_NUM_TOPS] = {
KD_WDT_FIX_TOP(0), KD_WDT_FIX_TOP(1), KD_WDT_FIX_TOP(2),
KD_WDT_FIX_TOP(3), KD_WDT_FIX_TOP(4), KD_WDT_FIX_TOP(5),
KD_WDT_FIX_TOP(6), KD_WDT_FIX_TOP(7), KD_WDT_FIX_TOP(8),
KD_WDT_FIX_TOP(9), KD_WDT_FIX_TOP(10), KD_WDT_FIX_TOP(11),
KD_WDT_FIX_TOP(12), KD_WDT_FIX_TOP(13), KD_WDT_FIX_TOP(14),
KD_WDT_FIX_TOP(15)
};
static struct k230_wdt_timeout timeouts[KD_WDT_NUM_TOPS];
static char rmod; /* wdt reset mode, */
static void k230_wdt_timeouts_init(struct k230_wdt_dev *dev)
{
rt_uint32_t wdt_clk;
rt_uint32_t time_value;
rt_uint32_t i, t;
rt_uint64_t msec;
struct k230_wdt_timeout tout, *dst;
/* caculate timeout value */
wdt_clk = sysctl_clk_get_leaf_freq(dev->clk);
for (i = 0; i < KD_WDT_NUM_TOPS; ++i)
{
tout.top_val = i;
tout.sec = tops[i] / wdt_clk;
msec = (rt_uint64_t)tops[i] * (rt_uint64_t)1000L;
msec = msec / wdt_clk;
tout.msec = msec - (rt_uint64_t)tout.sec * (rt_uint64_t)1000L;
for (t = 0; t < i; ++t)
{
dst = &timeouts[t];
if (tout.sec > dst->sec || (tout.sec == dst->sec &&
tout.msec >= dst->msec))
continue;
else
swap(*dst, tout);
}
timeouts[i] = tout;
}
rt_kprintf("watchdog timeout table init OK!\n");
}
static rt_err_t k230_wdt_feed(struct k230_wdt_dev *dev)
{
k230_wdt_t *reg = (k230_wdt_t *)dev->base;
reg->crr = 0x76;
return RT_EOK;
}
static rt_err_t k230_wdt_enable(struct k230_wdt_dev *dev)
{
k230_wdt_t *reg = (k230_wdt_t *)dev->base;
reg->crr = 0x76;
reg->cr |= 0x1;
return RT_EOK;
}
static rt_err_t k230_wdt_disable(struct k230_wdt_dev *dev)
{
k230_wdt_t *reg = (k230_wdt_t *)dev->base;
reg->crr = 0x76;
reg->cr &= ~0x1;
return RT_EOK;
}
static rt_err_t k230_wdt_set_timeout(struct k230_wdt_dev *dev, rt_uint64_t timeout)
{
k230_wdt_t *reg = (k230_wdt_t *)dev->base;
rt_uint32_t top_val;
rt_uint32_t i;
rt_uint32_t time = (timeout + rmod -1) / rmod;
for (i = 0; i < KD_WDT_NUM_TOPS; ++i)
{
if (timeouts[i].sec >= time)
break;
}
if (i == KD_WDT_NUM_TOPS)
--i;
top_val = timeouts[i].top_val;
reg->torr = (top_val << 4) | (top_val << 0);
return RT_EOK;
}
static rt_err_t k230_wdt_get_timeout(struct k230_wdt_dev *dev, void *timeout)
{
rt_uint64_t top_val;
rt_uint32_t i;
k230_wdt_t *reg = (k230_wdt_t *)dev->base;
top_val = ((reg->torr) & 0xf);
for (i = 0; i < KD_WDT_NUM_TOPS; ++i)
{
if (timeouts[i].top_val == top_val)
break;
}
*((rt_uint64_t *)timeout) = timeouts[i].sec * rmod;
return RT_EOK;
}
static rt_err_t k230_wdt_init(rt_watchdog_t *wdt)
{
RT_ASSERT(wdt != NULL);
struct k230_wdt_dev *dev = rt_container_of(wdt, struct k230_wdt_dev, device);
k230_wdt_t *reg = (k230_wdt_t *)dev->base;
reg->cr &= ~(0x01 << 1);
reg->cr |= (0x0 << 1); /* default set wdt reset mode */
rmod = KD_WDT_RMOD_RESET;
if (reg->comp_param_1 & (1 << 6))
{
tops = k230_wdt_fix_tops;
}
k230_wdt_timeouts_init(dev);
if (!timeouts[KD_WDT_NUM_TOPS - 1].sec)
{
rt_kprintf("No any valid Timeout period detected\n");
return -RT_EINVAL;
}
return RT_EOK;
}
static rt_err_t k230_wdt_control(rt_watchdog_t *wdt, int cmd, void *args)
{
RT_ASSERT(wdt != NULL);
struct k230_wdt_dev *dev = rt_container_of(wdt, struct k230_wdt_dev, device);
switch(cmd)
{
case KD_DEVICE_CTRL_WDT_GET_TIMEOUT:
k230_wdt_get_timeout(dev, args);
break;
case KD_DEVICE_CTRL_WDT_SET_TIMEOUT:
k230_wdt_set_timeout(dev, *((rt_uint32_t*)args));
break;
case KD_DEVICE_CTRL_WDT_KEEPALIVE:
k230_wdt_feed(dev);
break;
case KD_DEVICE_CTRL_WDT_START:
k230_wdt_enable(dev);
break;
case RT_DEVICE_CTRL_WDT_STOP:
case KD_DEVICE_CTRL_WDT_STOP:
k230_wdt_disable(dev);
break;
default:
return -RT_EINVAL;
}
return RT_EOK;
}
static struct rt_watchdog_ops k230_wdt_ops =
{
.init = k230_wdt_init,
.control = k230_wdt_control,
};
static struct k230_wdt_dev wdt_devices[] =
{
#ifdef BSP_USING_WDT0
{
.name = "wdt0",
.base = WDT0_BASE_ADDR,
.size = WDT0_IO_SIZE,
.clk = SYSCTL_CLK_WDT0,
},
#endif /* BSP_USING_WDT0 */
#ifdef BSP_USING_WDT1
{
.name = "wdt1",
.base = WDT1_BASE_ADDR,
.size = WDT1_IO_SIZE,
.clk = SYSCTL_CLK_WDT1,
},
#endif /* BSP_USING_WDT1 */
#if !defined(BSP_USING_WDT0) && !defined(BSP_USING_WDT1)
#error "No watchdog device defined!"
#endif
};
int rt_hw_wdt_init(void)
{
rt_uint8_t i;
for (i = 0; i < sizeof(wdt_devices) / sizeof(struct k230_wdt_dev); i++)
{
wdt_devices[i].device.ops = &k230_wdt_ops;
wdt_devices[i].base = (rt_ubase_t)rt_ioremap((void *)wdt_devices[i].base, wdt_devices[i].size);
if (rt_hw_watchdog_register(&wdt_devices[i].device, wdt_devices[i].name, RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
{
LOG_E("%s register failed!", wdt_devices[i].name);
return -RT_ERROR;
}
LOG_D("%s register OK!", wdt_devices[i].name);
}
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_wdt_init);
|