2 * Industrial Computer Source WDT500/501 driver for Linux 2.1.x
4 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
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.
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.
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
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
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
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/smp_lock.h>
40 #include <linux/miscdevice.h>
41 #include <linux/watchdog.h>
43 #include <linux/malloc.h>
44 #include <linux/ioport.h>
45 #include <linux/fcntl.h>
47 #include <asm/uaccess.h>
48 #include <asm/system.h>
49 #include <linux/notifier.h>
50 #include <linux/reboot.h>
51 #include <linux/init.h>
53 static int wdt_is_open
;
56 * You must set these - there is no sane way to probe for this board.
57 * You can use wdt=x,y to set these now.
63 #define WD_TIMO (100*60) /* 1 minute */
69 * @str: command line string
71 * Setup options. The board isn't really probe-able so we have to
72 * get the user to tell us the configuration. Sane people build it
73 * modular but the others come here.
76 static int __init
wdt_setup(char *str
)
80 str
= get_options (str
, ARRAY_SIZE(ints
), ints
);
92 __setup("wdt=", wdt_setup
);
97 MODULE_PARM_DESC(io
, "WDT io port (default=0x240)");
98 MODULE_PARM(irq
, "i");
99 MODULE_PARM_DESC(irq
, "WDT irq (default=11)");
102 * Programming support
105 static void wdt_ctr_mode(int ctr
, int mode
)
113 static void wdt_ctr_load(int ctr
, int val
)
115 outb_p(val
&0xFF, WDT_COUNT0
+ctr
);
116 outb_p(val
>>8, WDT_COUNT0
+ctr
);
127 * Extract the status information from a WDT watchdog device. There are
128 * several board variants so we have to know which bits are valid. Some
129 * bits default to one and some to zero in order to be maximally painful.
131 * we then map the bits onto the status ioctl flags.
134 static int wdt_status(void)
137 * Status register to bit flags
141 unsigned char status
=inb_p(WDT_SR
);
143 status
&=~FEATUREMAP2
;
145 if(!(status
&WDC_SR_TGOOD
))
146 flag
|=WDIOF_OVERHEAT
;
147 if(!(status
&WDC_SR_PSUOVER
))
148 flag
|=WDIOF_POWEROVER
;
149 if(!(status
&WDC_SR_PSUUNDR
))
150 flag
|=WDIOF_POWERUNDER
;
151 if(!(status
&WDC_SR_FANGOOD
))
152 flag
|=WDIOF_FANFAULT
;
153 if(status
&WDC_SR_ISOI0
)
155 if(status
&WDC_SR_ISII1
)
162 * @irq: Interrupt number
163 * @dev_id: Unused as we don't allow multiple devices.
166 * Handle an interrupt from the board. These are raised when the status
167 * map changes in what the board considers an interesting way. That means
168 * a failure condition occuring.
171 void wdt_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
174 * Read the status register see what is up and
178 unsigned char status
=inb_p(WDT_SR
);
181 status
&=~FEATUREMAP2
;
183 printk(KERN_CRIT
"WDT status %d\n", status
);
185 if(!(status
&WDC_SR_TGOOD
))
186 printk(KERN_CRIT
"Overheat alarm.(%d)\n",inb_p(WDT_RT
));
187 if(!(status
&WDC_SR_PSUOVER
))
188 printk(KERN_CRIT
"PSU over voltage.\n");
189 if(!(status
&WDC_SR_PSUUNDR
))
190 printk(KERN_CRIT
"PSU under voltage.\n");
191 if(!(status
&WDC_SR_FANGOOD
))
192 printk(KERN_CRIT
"Possible fan fault.\n");
193 if(!(status
&WDC_SR_WCCR
))
194 #ifdef SOFTWARE_REBOOT
196 printk(KERN_CRIT
"Would Reboot.\n");
198 printk(KERN_CRIT
"Initiating system reboot.\n");
199 machine_restart(NULL
);
202 printk(KERN_CRIT
"Reset in 5ms.\n");
207 static long long wdt_llseek(struct file
*file
, long long offset
, int origin
)
215 * Reload counter one with the watchdog timeout. We don't bother reloading
216 * the cascade counter.
219 static void wdt_ping(void)
221 /* Write a watchdog value */
224 wdt_ctr_load(1,WD_TIMO
); /* Timeout */
230 * @file: file handle to the watchdog
231 * @buf: buffer to write (unused as data does not matter here
232 * @count: count of bytes
233 * @ppos: pointer to the position to write. No seeks allowed
235 * A write to a watchdog device is defined as a keepalive signal. Any
236 * write of data will do, as we we don't define content meaning.
239 static ssize_t
wdt_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
241 /* Can't seek (pwrite) on this device */
242 if (ppos
!= &file
->f_pos
)
255 * @file: file handle to the watchdog board
256 * @buf: buffer to write 1 byte into
257 * @count: length of buffer
258 * @ptr: offset (no seek allowed)
260 * Read reports the temperature in degrees Fahrenheit. The API is in
261 * farenheit. It was designed by an imperial measurement luddite.
264 static ssize_t
wdt_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ptr
)
266 unsigned short c
=inb_p(WDT_RT
);
269 /* Can't seek (pread) on this device */
270 if (ptr
!= &file
->f_pos
)
273 switch(MINOR(file
->f_dentry
->d_inode
->i_rdev
))
279 if(copy_to_user(buf
,&cp
,1))
289 * @inode: inode of the device
290 * @file: file handle to the device
291 * @cmd: watchdog command
292 * @arg: argument pointer
294 * The watchdog API defines a common set of functions for all watchdogs
295 * according to their available features. We only actually usefully support
296 * querying capabilities and current status.
299 static int wdt_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
302 static struct watchdog_info ident
=
304 WDIOF_OVERHEAT
|WDIOF_POWERUNDER
|WDIOF_POWEROVER
305 |WDIOF_EXTERN1
|WDIOF_EXTERN2
|WDIOF_FANFAULT
,
310 ident
.options
&=WDT_OPTION_MASK
; /* Mask down to the card we have */
315 case WDIOC_GETSUPPORT
:
316 return copy_to_user((struct watchdog_info
*)arg
, &ident
, sizeof(ident
))?-EFAULT
:0;
318 case WDIOC_GETSTATUS
:
319 return put_user(wdt_status(),(int *)arg
);
320 case WDIOC_GETBOOTSTATUS
:
321 return put_user(0, (int *)arg
);
322 case WDIOC_KEEPALIVE
:
330 * @inode: inode of device
331 * @file: file handle to device
333 * One of our two misc devices has been opened. The watchdog device is
334 * single open and on opening we load the counters. Counter zero is a
335 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
336 * counter triggers counter 2 downcounts the length of the reset pulse
337 * which set set to be as long as possible.
340 static int wdt_open(struct inode
*inode
, struct file
*file
)
342 switch(MINOR(inode
->i_rdev
))
352 inb_p(WDT_DC
); /* Disable */
356 wdt_ctr_load(0, 8948); /* count at 100Hz */
357 wdt_ctr_load(1,WD_TIMO
); /* Timeout 120 seconds */
358 wdt_ctr_load(2,65535);
359 outb_p(0, WDT_DC
); /* Enable */
370 * @inode: inode to board
371 * @file: file handle to board
373 * The watchdog has a configurable API. There is a religious dispute
374 * between people who want their watchdog to be able to shut down and
375 * those who want to be sure if the watchdog manager dies the machine
376 * reboots. In the former case we disable the counters, in the latter
377 * case you have to open it again very soon.
380 static int wdt_release(struct inode
*inode
, struct file
*file
)
383 if(MINOR(inode
->i_rdev
)==WATCHDOG_MINOR
)
385 #ifndef CONFIG_WATCHDOG_NOWAYOUT
386 inb_p(WDT_DC
); /* Disable counters */
387 wdt_ctr_load(2,0); /* 0 length reset pulses now */
397 * @this: our notifier block
398 * @code: the event being reported
401 * Our notifier is called on system shutdowns. We want to turn the card
402 * off at reboot otherwise the machine will reboot again during memory
403 * test or worse yet during the following fsck. This would suck, in fact
404 * trust me - if it happens it does suck.
407 static int wdt_notify_sys(struct notifier_block
*this, unsigned long code
,
410 if(code
==SYS_DOWN
|| code
==SYS_HALT
)
412 /* Turn the card off */
424 static struct file_operations wdt_fops
= {
431 release
: wdt_release
,
434 static struct miscdevice wdt_miscdev
=
441 #ifdef CONFIG_WDT_501
442 static struct miscdevice temp_miscdev
=
451 * The WDT card needs to learn about soft shutdowns in order to
452 * turn the timebomb registers off.
455 static struct notifier_block wdt_notifier
=
464 #define wdt_init init_module
469 * Unload the watchdog. You cannot do this with any file handles open.
470 * If your watchdog is set to continue ticking on close and you unload
471 * it, well it keeps ticking. We won't get the interrupt but the board
472 * will not touch PC memory so all is fine. You just have to load a new
473 * module in 60 seconds or reboot.
476 void cleanup_module(void)
478 misc_deregister(&wdt_miscdev
);
479 #ifdef CONFIG_WDT_501
480 misc_deregister(&temp_miscdev
);
482 unregister_reboot_notifier(&wdt_notifier
);
483 release_region(io
,8);
492 * Set up the WDT watchdog board. All we have to do is grab the
493 * resources we require and bitch if anyone beat us to them.
494 * The open() function will actually kick the board off.
497 int __init
wdt_init(void)
501 ret
= misc_register(&wdt_miscdev
);
503 printk(KERN_ERR
"wdt: can't misc_register on minor=%d\n", WATCHDOG_MINOR
);
506 ret
= request_irq(irq
, wdt_interrupt
, SA_INTERRUPT
, "wdt501p", NULL
);
508 printk(KERN_ERR
"wdt: IRQ %d is not free.\n", irq
);
511 if (!request_region(io
, 8, "wdt501p")) {
512 printk(KERN_ERR
"wdt: IO %X is not free.\n", io
);
516 ret
= register_reboot_notifier(&wdt_notifier
);
518 printk(KERN_ERR
"wdt: can't register reboot notifier (err=%d)\n", ret
);
522 #ifdef CONFIG_WDT_501
523 ret
= misc_register(&temp_miscdev
);
525 printk(KERN_ERR
"wdt: can't misc_register (temp) on minor=%d\n", TEMP_MINOR
);
531 printk(KERN_INFO
"WDT500/501-P driver 0.07 at %X (Interrupt %d)\n", io
, irq
);
535 #ifdef CONFIG_WDT_501
537 unregister_reboot_notifier(&wdt_notifier
);
541 release_region(io
,8);
545 misc_deregister(&wdt_miscdev
);