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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52
53#include <linux/module.h>
54#include <linux/moduleparam.h>
55#include <linux/types.h>
56#include <linux/timer.h>
57#include <linux/miscdevice.h>
58#include <linux/watchdog.h>
59#include <linux/fs.h>
60#include <linux/ioport.h>
61#include <linux/notifier.h>
62#include <linux/reboot.h>
63#include <linux/init.h>
64#include <linux/jiffies.h>
65#include <linux/io.h>
66#include <linux/uaccess.h>
67
68
69
70
71
72
73
74
75
76
77
78
79#define WDT_INTERVAL (HZ/4+1)
80
81
82
83
84
85
86
87#define WATCHDOG_TIMEOUT 30
88
89static int timeout = WATCHDOG_TIMEOUT;
90module_param(timeout, int, 0);
91MODULE_PARM_DESC(timeout,
92 "Watchdog timeout in seconds. (1 <= timeout <= 3600, default="
93 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
94
95static bool nowayout = WATCHDOG_NOWAYOUT;
96module_param(nowayout, bool, 0);
97MODULE_PARM_DESC(nowayout,
98 "Watchdog cannot be stopped once started (default="
99 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
100
101
102
103
104#define MMCR_BASE 0xfffef000
105#define OFFS_WDTMRCTL 0xCB0
106
107
108#define WDT_EXP_SEL_01 0x0001
109#define WDT_EXP_SEL_02 0x0002
110#define WDT_EXP_SEL_03 0x0004
111#define WDT_EXP_SEL_04 0x0008
112#define WDT_EXP_SEL_05 0x0010
113#define WDT_EXP_SEL_06 0x0020
114#define WDT_EXP_SEL_07 0x0040
115#define WDT_EXP_SEL_08 0x0080
116#define WDT_IRQ_FLG 0x1000
117#define WDT_WRST_ENB 0x4000
118#define WDT_ENB 0x8000
119
120static __u16 __iomem *wdtmrctl;
121
122static void wdt_timer_ping(struct timer_list *);
123static DEFINE_TIMER(timer, wdt_timer_ping);
124static unsigned long next_heartbeat;
125static unsigned long wdt_is_open;
126static char wdt_expect_close;
127static DEFINE_SPINLOCK(wdt_spinlock);
128
129
130
131
132
133static void wdt_timer_ping(struct timer_list *unused)
134{
135
136
137
138 if (time_before(jiffies, next_heartbeat)) {
139
140 spin_lock(&wdt_spinlock);
141 writew(0xAAAA, wdtmrctl);
142 writew(0x5555, wdtmrctl);
143 spin_unlock(&wdt_spinlock);
144
145
146 mod_timer(&timer, jiffies + WDT_INTERVAL);
147 } else
148 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
149}
150
151
152
153
154
155static void wdt_config(int writeval)
156{
157 unsigned long flags;
158
159
160 spin_lock_irqsave(&wdt_spinlock, flags);
161 readw(wdtmrctl);
162 writew(0xAAAA, wdtmrctl);
163 writew(0x5555, wdtmrctl);
164
165 writew(0x3333, wdtmrctl);
166 writew(0xCCCC, wdtmrctl);
167
168 writew(writeval, wdtmrctl);
169 spin_unlock_irqrestore(&wdt_spinlock, flags);
170}
171
172static int wdt_startup(void)
173{
174 next_heartbeat = jiffies + (timeout * HZ);
175
176
177 mod_timer(&timer, jiffies + WDT_INTERVAL);
178
179
180 wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04);
181
182 pr_info("Watchdog timer is now enabled\n");
183 return 0;
184}
185
186static int wdt_turnoff(void)
187{
188
189 del_timer_sync(&timer);
190
191
192 wdt_config(0);
193
194 pr_info("Watchdog timer is now disabled...\n");
195 return 0;
196}
197
198static int wdt_keepalive(void)
199{
200
201 next_heartbeat = jiffies + (timeout * HZ);
202 return 0;
203}
204
205static int wdt_set_heartbeat(int t)
206{
207 if ((t < 1) || (t > 3600))
208 return -EINVAL;
209
210 timeout = t;
211 return 0;
212}
213
214
215
216
217
218static ssize_t fop_write(struct file *file, const char __user *buf,
219 size_t count, loff_t *ppos)
220{
221
222 if (count) {
223 if (!nowayout) {
224 size_t ofs;
225
226
227
228 wdt_expect_close = 0;
229
230
231 for (ofs = 0; ofs != count; ofs++) {
232 char c;
233 if (get_user(c, buf + ofs))
234 return -EFAULT;
235 if (c == 'V')
236 wdt_expect_close = 42;
237 }
238 }
239
240
241
242 wdt_keepalive();
243 }
244 return count;
245}
246
247static int fop_open(struct inode *inode, struct file *file)
248{
249
250 if (test_and_set_bit(0, &wdt_is_open))
251 return -EBUSY;
252 if (nowayout)
253 __module_get(THIS_MODULE);
254
255
256 wdt_startup();
257 return stream_open(inode, file);
258}
259
260static int fop_close(struct inode *inode, struct file *file)
261{
262 if (wdt_expect_close == 42)
263 wdt_turnoff();
264 else {
265 pr_crit("Unexpected close, not stopping watchdog!\n");
266 wdt_keepalive();
267 }
268 clear_bit(0, &wdt_is_open);
269 wdt_expect_close = 0;
270 return 0;
271}
272
273static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
274{
275 void __user *argp = (void __user *)arg;
276 int __user *p = argp;
277 static const struct watchdog_info ident = {
278 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
279 | WDIOF_MAGICCLOSE,
280 .firmware_version = 1,
281 .identity = "SC520",
282 };
283
284 switch (cmd) {
285 case WDIOC_GETSUPPORT:
286 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
287 case WDIOC_GETSTATUS:
288 case WDIOC_GETBOOTSTATUS:
289 return put_user(0, p);
290 case WDIOC_SETOPTIONS:
291 {
292 int new_options, retval = -EINVAL;
293
294 if (get_user(new_options, p))
295 return -EFAULT;
296
297 if (new_options & WDIOS_DISABLECARD) {
298 wdt_turnoff();
299 retval = 0;
300 }
301
302 if (new_options & WDIOS_ENABLECARD) {
303 wdt_startup();
304 retval = 0;
305 }
306
307 return retval;
308 }
309 case WDIOC_KEEPALIVE:
310 wdt_keepalive();
311 return 0;
312 case WDIOC_SETTIMEOUT:
313 {
314 int new_timeout;
315
316 if (get_user(new_timeout, p))
317 return -EFAULT;
318
319 if (wdt_set_heartbeat(new_timeout))
320 return -EINVAL;
321
322 wdt_keepalive();
323 }
324 fallthrough;
325 case WDIOC_GETTIMEOUT:
326 return put_user(timeout, p);
327 default:
328 return -ENOTTY;
329 }
330}
331
332static const struct file_operations wdt_fops = {
333 .owner = THIS_MODULE,
334 .llseek = no_llseek,
335 .write = fop_write,
336 .open = fop_open,
337 .release = fop_close,
338 .unlocked_ioctl = fop_ioctl,
339 .compat_ioctl = compat_ptr_ioctl,
340};
341
342static struct miscdevice wdt_miscdev = {
343 .minor = WATCHDOG_MINOR,
344 .name = "watchdog",
345 .fops = &wdt_fops,
346};
347
348
349
350
351
352static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
353 void *unused)
354{
355 if (code == SYS_DOWN || code == SYS_HALT)
356 wdt_turnoff();
357 return NOTIFY_DONE;
358}
359
360
361
362
363
364
365static struct notifier_block wdt_notifier = {
366 .notifier_call = wdt_notify_sys,
367};
368
369static void __exit sc520_wdt_unload(void)
370{
371 if (!nowayout)
372 wdt_turnoff();
373
374
375 misc_deregister(&wdt_miscdev);
376 unregister_reboot_notifier(&wdt_notifier);
377 iounmap(wdtmrctl);
378}
379
380static int __init sc520_wdt_init(void)
381{
382 int rc = -EBUSY;
383
384
385
386 if (wdt_set_heartbeat(timeout)) {
387 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
388 pr_info("timeout value must be 1 <= timeout <= 3600, using %d\n",
389 WATCHDOG_TIMEOUT);
390 }
391
392 wdtmrctl = ioremap(MMCR_BASE + OFFS_WDTMRCTL, 2);
393 if (!wdtmrctl) {
394 pr_err("Unable to remap memory\n");
395 rc = -ENOMEM;
396 goto err_out_region2;
397 }
398
399 rc = register_reboot_notifier(&wdt_notifier);
400 if (rc) {
401 pr_err("cannot register reboot notifier (err=%d)\n", rc);
402 goto err_out_ioremap;
403 }
404
405 rc = misc_register(&wdt_miscdev);
406 if (rc) {
407 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
408 WATCHDOG_MINOR, rc);
409 goto err_out_notifier;
410 }
411
412 pr_info("WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n",
413 timeout, nowayout);
414
415 return 0;
416
417err_out_notifier:
418 unregister_reboot_notifier(&wdt_notifier);
419err_out_ioremap:
420 iounmap(wdtmrctl);
421err_out_region2:
422 return rc;
423}
424
425module_init(sc520_wdt_init);
426module_exit(sc520_wdt_unload);
427
428MODULE_AUTHOR("Scott and Bill Jennings");
429MODULE_DESCRIPTION(
430 "Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor");
431MODULE_LICENSE("GPL");
432