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