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