aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/wdt.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/watchdog/wdt.c')
-rw-r--r--drivers/watchdog/wdt.c640
1 files changed, 640 insertions, 0 deletions
diff --git a/drivers/watchdog/wdt.c b/drivers/watchdog/wdt.c
new file mode 100644
index 000000000000..0a3de6a02442
--- /dev/null
+++ b/drivers/watchdog/wdt.c
@@ -0,0 +1,640 @@
1/*
2 * Industrial Computer Source WDT500/501 driver
3 *
4 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
15 *
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
17 *
18 * Release 0.10.
19 *
20 * Fixes
21 * Dave Gregorich : Modularisation and minor bugs
22 * Alan Cox : Added the watchdog ioctl() stuff
23 * Alan Cox : Fixed the reboot problem (as noted by
24 * Matt Crocker).
25 * Alan Cox : Added wdt= boot option
26 * Alan Cox : Cleaned up copy/user stuff
27 * Tim Hockin : Added insmod parameters, comment cleanup
28 * Parameterized timeout
29 * Tigran Aivazian : Restructured wdt_init() to handle failures
30 * Joel Becker : Added WDIOC_GET/SETTIMEOUT
31 * Matt Domsch : Added nowayout module option
32 */
33
34#include <linux/interrupt.h>
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/types.h>
38#include <linux/miscdevice.h>
39#include <linux/watchdog.h>
40#include <linux/fs.h>
41#include <linux/ioport.h>
42#include <linux/notifier.h>
43#include <linux/reboot.h>
44#include <linux/init.h>
45
46#include <asm/io.h>
47#include <asm/uaccess.h>
48#include <asm/system.h>
49#include "wd501p.h"
50
51static unsigned long wdt_is_open;
52static char expect_close;
53
54/*
55 * Module parameters
56 */
57
58#define WD_TIMO 60 /* Default heartbeat = 60 seconds */
59
60static int heartbeat = WD_TIMO;
61static int wd_heartbeat;
62module_param(heartbeat, int, 0);
63MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WD_TIMO) ")");
64
65static int nowayout = WATCHDOG_NOWAYOUT;
66module_param(nowayout, int, 0);
67MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
68
69/* You must set these - there is no sane way to probe for this board. */
70static int io=0x240;
71static int irq=11;
72
73module_param(io, int, 0);
74MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
75module_param(irq, int, 0);
76MODULE_PARM_DESC(irq, "WDT irq (default=11)");
77
78#ifdef CONFIG_WDT_501
79/* Support for the Fan Tachometer on the WDT501-P */
80static int tachometer;
81
82module_param(tachometer, int, 0);
83MODULE_PARM_DESC(tachometer, "WDT501-P Fan Tachometer support (0=disable, default=0)");
84#endif /* CONFIG_WDT_501 */
85
86/*
87 * Programming support
88 */
89
90static void wdt_ctr_mode(int ctr, int mode)
91{
92 ctr<<=6;
93 ctr|=0x30;
94 ctr|=(mode<<1);
95 outb_p(ctr, WDT_CR);
96}
97
98static void wdt_ctr_load(int ctr, int val)
99{
100 outb_p(val&0xFF, WDT_COUNT0+ctr);
101 outb_p(val>>8, WDT_COUNT0+ctr);
102}
103
104/**
105 * wdt_start:
106 *
107 * Start the watchdog driver.
108 */
109
110static int wdt_start(void)
111{
112 inb_p(WDT_DC); /* Disable watchdog */
113 wdt_ctr_mode(0,3); /* Program CTR0 for Mode 3: Square Wave Generator */
114 wdt_ctr_mode(1,2); /* Program CTR1 for Mode 2: Rate Generator */
115 wdt_ctr_mode(2,0); /* Program CTR2 for Mode 0: Pulse on Terminal Count */
116 wdt_ctr_load(0, 8948); /* Count at 100Hz */
117 wdt_ctr_load(1,wd_heartbeat); /* Heartbeat */
118 wdt_ctr_load(2,65535); /* Length of reset pulse */
119 outb_p(0, WDT_DC); /* Enable watchdog */
120 return 0;
121}
122
123/**
124 * wdt_stop:
125 *
126 * Stop the watchdog driver.
127 */
128
129static int wdt_stop (void)
130{
131 /* Turn the card off */
132 inb_p(WDT_DC); /* Disable watchdog */
133 wdt_ctr_load(2,0); /* 0 length reset pulses now */
134 return 0;
135}
136
137/**
138 * wdt_ping:
139 *
140 * Reload counter one with the watchdog heartbeat. We don't bother reloading
141 * the cascade counter.
142 */
143
144static int wdt_ping(void)
145{
146 /* Write a watchdog value */
147 inb_p(WDT_DC); /* Disable watchdog */
148 wdt_ctr_mode(1,2); /* Re-Program CTR1 for Mode 2: Rate Generator */
149 wdt_ctr_load(1,wd_heartbeat); /* Heartbeat */
150 outb_p(0, WDT_DC); /* Enable watchdog */
151 return 0;
152}
153
154/**
155 * wdt_set_heartbeat:
156 * @t: the new heartbeat value that needs to be set.
157 *
158 * Set a new heartbeat value for the watchdog device. If the heartbeat value is
159 * incorrect we keep the old value and return -EINVAL. If successfull we
160 * return 0.
161 */
162static int wdt_set_heartbeat(int t)
163{
164 if ((t < 1) || (t > 65535))
165 return -EINVAL;
166
167 heartbeat = t;
168 wd_heartbeat = t * 100;
169 return 0;
170}
171
172/**
173 * wdt_get_status:
174 * @status: the new status.
175 *
176 * Extract the status information from a WDT watchdog device. There are
177 * several board variants so we have to know which bits are valid. Some
178 * bits default to one and some to zero in order to be maximally painful.
179 *
180 * we then map the bits onto the status ioctl flags.
181 */
182
183static int wdt_get_status(int *status)
184{
185 unsigned char new_status=inb_p(WDT_SR);
186
187 *status=0;
188 if (new_status & WDC_SR_ISOI0)
189 *status |= WDIOF_EXTERN1;
190 if (new_status & WDC_SR_ISII1)
191 *status |= WDIOF_EXTERN2;
192#ifdef CONFIG_WDT_501
193 if (!(new_status & WDC_SR_TGOOD))
194 *status |= WDIOF_OVERHEAT;
195 if (!(new_status & WDC_SR_PSUOVER))
196 *status |= WDIOF_POWEROVER;
197 if (!(new_status & WDC_SR_PSUUNDR))
198 *status |= WDIOF_POWERUNDER;
199 if (tachometer) {
200 if (!(new_status & WDC_SR_FANGOOD))
201 *status |= WDIOF_FANFAULT;
202 }
203#endif /* CONFIG_WDT_501 */
204 return 0;
205}
206
207#ifdef CONFIG_WDT_501
208/**
209 * wdt_get_temperature:
210 *
211 * Reports the temperature in degrees Fahrenheit. The API is in
212 * farenheit. It was designed by an imperial measurement luddite.
213 */
214
215static int wdt_get_temperature(int *temperature)
216{
217 unsigned short c=inb_p(WDT_RT);
218
219 *temperature = (c * 11 / 15) + 7;
220 return 0;
221}
222#endif /* CONFIG_WDT_501 */
223
224/**
225 * wdt_interrupt:
226 * @irq: Interrupt number
227 * @dev_id: Unused as we don't allow multiple devices.
228 *
229 * Handle an interrupt from the board. These are raised when the status
230 * map changes in what the board considers an interesting way. That means
231 * a failure condition occurring.
232 */
233
234static irqreturn_t wdt_interrupt(int irq, void *dev_id)
235{
236 /*
237 * Read the status register see what is up and
238 * then printk it.
239 */
240 unsigned char status=inb_p(WDT_SR);
241
242 printk(KERN_CRIT "WDT status %d\n", status);
243
244#ifdef CONFIG_WDT_501
245 if (!(status & WDC_SR_TGOOD))
246 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
247 if (!(status & WDC_SR_PSUOVER))
248 printk(KERN_CRIT "PSU over voltage.\n");
249 if (!(status & WDC_SR_PSUUNDR))
250 printk(KERN_CRIT "PSU under voltage.\n");
251 if (tachometer) {
252 if (!(status & WDC_SR_FANGOOD))
253 printk(KERN_CRIT "Possible fan fault.\n");
254 }
255#endif /* CONFIG_WDT_501 */
256 if (!(status & WDC_SR_WCCR))
257#ifdef SOFTWARE_REBOOT
258#ifdef ONLY_TESTING
259 printk(KERN_CRIT "Would Reboot.\n");
260#else
261 printk(KERN_CRIT "Initiating system reboot.\n");
262 emergency_restart();
263#endif
264#else
265 printk(KERN_CRIT "Reset in 5ms.\n");
266#endif
267 return IRQ_HANDLED;
268}
269
270
271/**
272 * wdt_write:
273 * @file: file handle to the watchdog
274 * @buf: buffer to write (unused as data does not matter here
275 * @count: count of bytes
276 * @ppos: pointer to the position to write. No seeks allowed
277 *
278 * A write to a watchdog device is defined as a keepalive signal. Any
279 * write of data will do, as we we don't define content meaning.
280 */
281
282static ssize_t wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
283{
284 if(count) {
285 if (!nowayout) {
286 size_t i;
287
288 /* In case it was set long ago */
289 expect_close = 0;
290
291 for (i = 0; i != count; i++) {
292 char c;
293 if (get_user(c, buf + i))
294 return -EFAULT;
295 if (c == 'V')
296 expect_close = 42;
297 }
298 }
299 wdt_ping();
300 }
301 return count;
302}
303
304/**
305 * wdt_ioctl:
306 * @inode: inode of the device
307 * @file: file handle to the device
308 * @cmd: watchdog command
309 * @arg: argument pointer
310 *
311 * The watchdog API defines a common set of functions for all watchdogs
312 * according to their available features. We only actually usefully support
313 * querying capabilities and current status.
314 */
315
316static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
317 unsigned long arg)
318{
319 void __user *argp = (void __user *)arg;
320 int __user *p = argp;
321 int new_heartbeat;
322 int status;
323
324 static struct watchdog_info ident = {
325 .options = WDIOF_SETTIMEOUT|
326 WDIOF_MAGICCLOSE|
327 WDIOF_KEEPALIVEPING,
328 .firmware_version = 1,
329 .identity = "WDT500/501",
330 };
331
332 /* Add options according to the card we have */
333 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
334#ifdef CONFIG_WDT_501
335 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER);
336 if (tachometer)
337 ident.options |= WDIOF_FANFAULT;
338#endif /* CONFIG_WDT_501 */
339
340 switch(cmd)
341 {
342 default:
343 return -ENOTTY;
344 case WDIOC_GETSUPPORT:
345 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
346
347 case WDIOC_GETSTATUS:
348 wdt_get_status(&status);
349 return put_user(status, p);
350 case WDIOC_GETBOOTSTATUS:
351 return put_user(0, p);
352 case WDIOC_KEEPALIVE:
353 wdt_ping();
354 return 0;
355 case WDIOC_SETTIMEOUT:
356 if (get_user(new_heartbeat, p))
357 return -EFAULT;
358
359 if (wdt_set_heartbeat(new_heartbeat))
360 return -EINVAL;
361
362 wdt_ping();
363 /* Fall */
364 case WDIOC_GETTIMEOUT:
365 return put_user(heartbeat, p);
366 }
367}
368
369/**
370 * wdt_open:
371 * @inode: inode of device
372 * @file: file handle to device
373 *
374 * The watchdog device has been opened. The watchdog device is single
375 * open and on opening we load the counters. Counter zero is a 100Hz
376 * cascade, into counter 1 which downcounts to reboot. When the counter
377 * triggers counter 2 downcounts the length of the reset pulse which
378 * set set to be as long as possible.
379 */
380
381static int wdt_open(struct inode *inode, struct file *file)
382{
383 if(test_and_set_bit(0, &wdt_is_open))
384 return -EBUSY;
385 /*
386 * Activate
387 */
388 wdt_start();
389 return nonseekable_open(inode, file);
390}
391
392/**
393 * wdt_release:
394 * @inode: inode to board
395 * @file: file handle to board
396 *
397 * The watchdog has a configurable API. There is a religious dispute
398 * between people who want their watchdog to be able to shut down and
399 * those who want to be sure if the watchdog manager dies the machine
400 * reboots. In the former case we disable the counters, in the latter
401 * case you have to open it again very soon.
402 */
403
404static int wdt_release(struct inode *inode, struct file *file)
405{
406 if (expect_close == 42) {
407 wdt_stop();
408 clear_bit(0, &wdt_is_open);
409 } else {
410 printk(KERN_CRIT "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
411 wdt_ping();
412 }
413 expect_close = 0;
414 return 0;
415}
416
417#ifdef CONFIG_WDT_501
418/**
419 * wdt_temp_read:
420 * @file: file handle to the watchdog board
421 * @buf: buffer to write 1 byte into
422 * @count: length of buffer
423 * @ptr: offset (no seek allowed)
424 *
425 * Temp_read reports the temperature in degrees Fahrenheit. The API is in
426 * farenheit. It was designed by an imperial measurement luddite.
427 */
428
429static ssize_t wdt_temp_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
430{
431 int temperature;
432
433 if (wdt_get_temperature(&temperature))
434 return -EFAULT;
435
436 if (copy_to_user (buf, &temperature, 1))
437 return -EFAULT;
438
439 return 1;
440}
441
442/**
443 * wdt_temp_open:
444 * @inode: inode of device
445 * @file: file handle to device
446 *
447 * The temperature device has been opened.
448 */
449
450static int wdt_temp_open(struct inode *inode, struct file *file)
451{
452 return nonseekable_open(inode, file);
453}
454
455/**
456 * wdt_temp_release:
457 * @inode: inode to board
458 * @file: file handle to board
459 *
460 * The temperature device has been closed.
461 */
462
463static int wdt_temp_release(struct inode *inode, struct file *file)
464{
465 return 0;
466}
467#endif /* CONFIG_WDT_501 */
468
469/**
470 * notify_sys:
471 * @this: our notifier block
472 * @code: the event being reported
473 * @unused: unused
474 *
475 * Our notifier is called on system shutdowns. We want to turn the card
476 * off at reboot otherwise the machine will reboot again during memory
477 * test or worse yet during the following fsck. This would suck, in fact
478 * trust me - if it happens it does suck.
479 */
480
481static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
482 void *unused)
483{
484 if(code==SYS_DOWN || code==SYS_HALT) {
485 /* Turn the card off */
486 wdt_stop();
487 }
488 return NOTIFY_DONE;
489}
490
491/*
492 * Kernel Interfaces
493 */
494
495
496static const struct file_operations wdt_fops = {
497 .owner = THIS_MODULE,
498 .llseek = no_llseek,
499 .write = wdt_write,
500 .ioctl = wdt_ioctl,
501 .open = wdt_open,
502 .release = wdt_release,
503};
504
505static struct miscdevice wdt_miscdev = {
506 .minor = WATCHDOG_MINOR,
507 .name = "watchdog",
508 .fops = &wdt_fops,
509};
510
511#ifdef CONFIG_WDT_501
512static const struct file_operations wdt_temp_fops = {
513 .owner = THIS_MODULE,
514 .llseek = no_llseek,
515 .read = wdt_temp_read,
516 .open = wdt_temp_open,
517 .release = wdt_temp_release,
518};
519
520static struct miscdevice temp_miscdev = {
521 .minor = TEMP_MINOR,
522 .name = "temperature",
523 .fops = &wdt_temp_fops,
524};
525#endif /* CONFIG_WDT_501 */
526
527/*
528 * The WDT card needs to learn about soft shutdowns in order to
529 * turn the timebomb registers off.
530 */
531
532static struct notifier_block wdt_notifier = {
533 .notifier_call = wdt_notify_sys,
534};
535
536/**
537 * cleanup_module:
538 *
539 * Unload the watchdog. You cannot do this with any file handles open.
540 * If your watchdog is set to continue ticking on close and you unload
541 * it, well it keeps ticking. We won't get the interrupt but the board
542 * will not touch PC memory so all is fine. You just have to load a new
543 * module in 60 seconds or reboot.
544 */
545
546static void __exit wdt_exit(void)
547{
548 misc_deregister(&wdt_miscdev);
549#ifdef CONFIG_WDT_501
550 misc_deregister(&temp_miscdev);
551#endif /* CONFIG_WDT_501 */
552 unregister_reboot_notifier(&wdt_notifier);
553 free_irq(irq, NULL);
554 release_region(io,8);
555}
556
557/**
558 * wdt_init:
559 *
560 * Set up the WDT watchdog board. All we have to do is grab the
561 * resources we require and bitch if anyone beat us to them.
562 * The open() function will actually kick the board off.
563 */
564
565static int __init wdt_init(void)
566{
567 int ret;
568
569 /* Check that the heartbeat value is within it's range ; if not reset to the default */
570 if (wdt_set_heartbeat(heartbeat)) {
571 wdt_set_heartbeat(WD_TIMO);
572 printk(KERN_INFO "wdt: heartbeat value must be 0<heartbeat<65536, using %d\n",
573 WD_TIMO);
574 }
575
576 if (!request_region(io, 8, "wdt501p")) {
577 printk(KERN_ERR "wdt: I/O address 0x%04x already in use\n", io);
578 ret = -EBUSY;
579 goto out;
580 }
581
582 ret = request_irq(irq, wdt_interrupt, IRQF_DISABLED, "wdt501p", NULL);
583 if(ret) {
584 printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
585 goto outreg;
586 }
587
588 ret = register_reboot_notifier(&wdt_notifier);
589 if(ret) {
590 printk(KERN_ERR "wdt: cannot register reboot notifier (err=%d)\n", ret);
591 goto outirq;
592 }
593
594#ifdef CONFIG_WDT_501
595 ret = misc_register(&temp_miscdev);
596 if (ret) {
597 printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
598 TEMP_MINOR, ret);
599 goto outrbt;
600 }
601#endif /* CONFIG_WDT_501 */
602
603 ret = misc_register(&wdt_miscdev);
604 if (ret) {
605 printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
606 WATCHDOG_MINOR, ret);
607 goto outmisc;
608 }
609
610 ret = 0;
611 printk(KERN_INFO "WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
612 io, irq, heartbeat, nowayout);
613#ifdef CONFIG_WDT_501
614 printk(KERN_INFO "wdt: Fan Tachometer is %s\n", (tachometer ? "Enabled" : "Disabled"));
615#endif /* CONFIG_WDT_501 */
616
617out:
618 return ret;
619
620outmisc:
621#ifdef CONFIG_WDT_501
622 misc_deregister(&temp_miscdev);
623outrbt:
624#endif /* CONFIG_WDT_501 */
625 unregister_reboot_notifier(&wdt_notifier);
626outirq:
627 free_irq(irq, NULL);
628outreg:
629 release_region(io,8);
630 goto out;
631}
632
633module_init(wdt_init);
634module_exit(wdt_exit);
635
636MODULE_AUTHOR("Alan Cox");
637MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
638MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
639MODULE_ALIAS_MISCDEV(TEMP_MINOR);
640MODULE_LICENSE("GPL");