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