-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimanager_wdt.c
300 lines (244 loc) · 6.92 KB
/
imanager_wdt.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Advantech iManager Watchdog driver
*
* Copyright (C) 2016-2017 Advantech Co., Ltd.
* Author: Richard Vidal-Dorsch <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/bitops.h>
#include <linux/byteorder/generic.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <linux/watchdog.h>
#include "compat.h"
#include "imanager.h"
#define WDT_DEFAULT_TIMEOUT 30 /* seconds */
#define WDT_FREQ 10 /* Hz */
struct imanager_wdt_data {
struct imanager_device_data *imgr;
struct watchdog_device wdt;
ulong last_updated;
uint timeout;
};
static uint timeout = WDT_DEFAULT_TIMEOUT;
module_param(timeout, uint, 0444);
MODULE_PARM_DESC(timeout,
"Watchdog timeout in seconds. 1 <= timeout <= 65534, default="
__MODULE_STRING(WDT_DEFAULT_TIMEOUT) ".");
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0444);
MODULE_PARM_DESC(nowayout,
"Watchdog cannot be stopped once started (default="
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
#endif
enum wdt_ctrl {
START = 1, STOP, RESET, GET_TIMEOUT, SET_TIMEOUT, STOPBOOT = 8
};
enum imanager_wdt_event {
WDT_EVT_NONE,
WDT_EVT_DELAY,
WDT_EVT_PWRBTN,
WDT_EVT_NMI,
WDT_EVT_RESET,
WDT_EVT_WDPIN,
WDT_EVT_SCI,
};
struct event_delay {
u16 delay,
pwrbtn,
nmi,
reset,
wdpin,
sci,
dummy;
} __attribute__((__packed__));
static int imanager_wdt_ctrl(struct imanager_device_data *imgr, int ctrl,
int event_type, uint timeout)
{
struct imanager_ec_message msg = {
IMANAGER_MSG(0, 0, ctrl, EC_CMD_WDT_CTRL),
};
u8 *fevent = &msg.u.data[0];
struct event_delay *event = (struct event_delay *)&msg.u.data[1];
int val;
if (ctrl == SET_TIMEOUT) {
memset(event, 0xff, sizeof(*event));
msg.wlen = sizeof(*event);
*fevent = 0;
val = (!timeout) ? 0xffff : cpu_to_be16(timeout * WDT_FREQ);
switch (event_type) {
case WDT_EVT_DELAY:
event->delay = val;
break;
case WDT_EVT_PWRBTN:
event->pwrbtn = val;
break;
case WDT_EVT_NMI:
event->nmi = val;
break;
case WDT_EVT_RESET:
event->reset = val;
break;
case WDT_EVT_WDPIN:
event->wdpin = val;
break;
case WDT_EVT_SCI:
event->sci = val;
break;
default:
return -EINVAL;
}
}
return imanager_write(imgr, &msg);
}
static inline int imanager_wdt_disable_all(struct imanager_wdt_data *data)
{
return (imanager_wdt_ctrl(data->imgr, STOP, WDT_EVT_NONE, 0) ||
imanager_wdt_ctrl(data->imgr, STOPBOOT, WDT_EVT_NONE, 0));
}
static int imanager_wdt_set_timeout(struct watchdog_device *wdt, uint timeout)
{
struct imanager_wdt_data *data = watchdog_get_drvdata(wdt);
struct imanager_device_data *imgr = data->imgr;
int ret;
if (time_before(jiffies, data->last_updated + HZ + HZ / 2))
return 0;
if (data->timeout == timeout)
return 0;
ret = imanager_wdt_ctrl(imgr, SET_TIMEOUT, WDT_EVT_PWRBTN, timeout);
if (ret < 0)
return ret;
data->timeout = timeout;
data->last_updated = jiffies;
return 0;
}
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
static uint imanager_wdt_get_timeleft(struct watchdog_device *wdt)
{
struct imanager_wdt_data *data = watchdog_get_drvdata(wdt);
uint timeleft = 0;
ulong time_diff = ((jiffies - data->last_updated) / HZ);
if (data->last_updated && (data->timeout > time_diff))
timeleft = data->timeout - time_diff;
return timeleft;
}
#endif
static int imanager_wdt_start(struct watchdog_device *wdt)
{
struct imanager_wdt_data *data = watchdog_get_drvdata(wdt);
int ret;
ret = imanager_wdt_ctrl(data->imgr, START, WDT_EVT_NONE, 0);
if (ret < 0)
return ret;
data->last_updated = jiffies;
return 0;
}
static int imanager_wdt_stop(struct watchdog_device *wdt)
{
struct imanager_wdt_data *data = watchdog_get_drvdata(wdt);
int ret;
ret = imanager_wdt_ctrl(data->imgr, STOP, WDT_EVT_NONE, 0);
if (ret < 0)
return ret;
data->last_updated = 0;
return 0;
}
static int imanager_wdt_ping(struct watchdog_device *wdt)
{
struct imanager_wdt_data *data = watchdog_get_drvdata(wdt);
int ret;
ret = imanager_wdt_ctrl(data->imgr, RESET, WDT_EVT_NONE, 0);
if (ret < 0)
return ret;
data->last_updated = jiffies;
return 0;
}
static const struct watchdog_info imanager_wdt_info = {
.options = WDIOF_SETTIMEOUT |
WDIOF_KEEPALIVEPING |
WDIOF_MAGICCLOSE,
.firmware_version = 0,
.identity = "imanager-wdt",
};
static const struct watchdog_ops imanager_wdt_ops = {
.owner = THIS_MODULE,
.start = imanager_wdt_start,
.stop = imanager_wdt_stop,
.ping = imanager_wdt_ping,
.set_timeout = imanager_wdt_set_timeout,
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
.get_timeleft = imanager_wdt_get_timeleft,
#endif
};
static int imanager_wdt_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct imanager_device_data *imgr = dev_get_drvdata(dev->parent);
struct imanager_wdt_data *data;
struct watchdog_device *wdt_dev;
int ret;
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
data->imgr = imgr;
wdt_dev = &data->wdt;
wdt_dev->info = &imanager_wdt_info;
wdt_dev->ops = &imanager_wdt_ops;
wdt_dev->timeout = WDT_DEFAULT_TIMEOUT;
wdt_dev->min_timeout = 1;
wdt_dev->max_timeout = 0xfffe;
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
watchdog_set_nowayout(wdt_dev, nowayout);
#endif
watchdog_set_drvdata(wdt_dev, data);
ret = watchdog_register_device(wdt_dev);
if (ret) {
dev_err(dev, "Could not register watchdog device\n");
return ret;
}
platform_set_drvdata(pdev, data);
imanager_wdt_disable_all(data);
imanager_wdt_set_timeout(wdt_dev, timeout);
dev_info(dev, "Driver loaded (timeout=%d seconds)\n", timeout);
return 0;
}
static int imanager_wdt_remove(struct platform_device *pdev)
{
struct imanager_wdt_data *data = platform_get_drvdata(pdev);
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
if (!nowayout)
imanager_wdt_disable_all(data);
#endif
watchdog_unregister_device(&data->wdt);
platform_set_drvdata(pdev, NULL);
return 0;
}
static void imanager_wdt_shutdown(struct platform_device *pdev)
{
struct imanager_device_data *imgr = dev_get_drvdata(pdev->dev.parent);
imanager_wdt_ctrl(imgr, STOP, WDT_EVT_NONE, 0);
}
static struct platform_driver imanager_wdt_driver = {
.driver = {
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 19, 0)
.owner = THIS_MODULE,
#endif
.name = "imanager-wdt",
},
.probe = imanager_wdt_probe,
.remove = imanager_wdt_remove,
.shutdown = imanager_wdt_shutdown,
};
module_platform_driver(imanager_wdt_driver);
MODULE_DESCRIPTION("Advantech iManager Watchdog Driver");
MODULE_AUTHOR("Richard Vidal-Dorsch <richard.dorsch at advantech.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:imanager-wdt");