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
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/smp_lock.h>
39 #include <linux/miscdevice.h>
40 #include <linux/watchdog.h>
42 #include <linux/malloc.h>
43 #include <linux/ioport.h>
44 #include <linux/fcntl.h>
46 #include <asm/uaccess.h>
47 #include <asm/system.h>
48 #include <linux/notifier.h>
49 #include <linux/reboot.h>
50 #include <linux/init.h>
52 static int wdt_is_open
=0;
55 * You must set these - there is no sane way to probe for this board.
56 * You can use wdt=x,y to set these now.
62 #define WD_TIMO (100*60) /* 1 minute */
68 * @str: command line string
70 * Setup options. The board isn't really probe-able so we have to
71 * get the user to tell us the configuration. Sane people build it
72 * modular but the others come here.
75 static int __init
wdt_setup(char *str
)
79 str
= get_options (str
, ARRAY_SIZE(ints
), ints
);
91 __setup("wdt=", wdt_setup
);
96 MODULE_PARM_DESC(io
, "WDT io port (default=0x240)");
97 MODULE_PARM(irq
, "i");
98 MODULE_PARM_DESC(irq
, "WDT irq (default=11)");
101 * Programming support
104 static void wdt_ctr_mode(int ctr
, int mode
)
112 static void wdt_ctr_load(int ctr
, int val
)
114 outb_p(val
&0xFF, WDT_COUNT0
+ctr
);
115 outb_p(val
>>8, WDT_COUNT0
+ctr
);
126 * Extract the status information from a WDT watchdog device. There are
127 * several board variants so we have to know which bits are valid. Some
128 * bits default to one and some to zero in order to be maximally painful.
130 * we then map the bits onto the status ioctl flags.
133 static int wdt_status(void)
136 * Status register to bit flags
140 unsigned char status
=inb_p(WDT_SR
);
142 status
&=~FEATUREMAP2
;
144 if(!(status
&WDC_SR_TGOOD
))
145 flag
|=WDIOF_OVERHEAT
;
146 if(!(status
&WDC_SR_PSUOVER
))
147 flag
|=WDIOF_POWEROVER
;
148 if(!(status
&WDC_SR_PSUUNDR
))
149 flag
|=WDIOF_POWERUNDER
;
150 if(!(status
&WDC_SR_FANGOOD
))
151 flag
|=WDIOF_FANFAULT
;
152 if(status
&WDC_SR_ISOI0
)
154 if(status
&WDC_SR_ISII1
)
161 * @irq: Interrupt number
162 * @dev_id: Unused as we don't allow multiple devices.
165 * Handle an interrupt from the board. These are raised when the status
166 * map changes in what the board considers an interesting way. That means
167 * a failure condition occuring.
170 void wdt_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
173 * Read the status register see what is up and
177 unsigned char status
=inb_p(WDT_SR
);
180 status
&=~FEATUREMAP2
;
182 printk(KERN_CRIT
"WDT status %d\n", status
);
184 if(!(status
&WDC_SR_TGOOD
))
185 printk(KERN_CRIT
"Overheat alarm.(%d)\n",inb_p(WDT_RT
));
186 if(!(status
&WDC_SR_PSUOVER
))
187 printk(KERN_CRIT
"PSU over voltage.\n");
188 if(!(status
&WDC_SR_PSUUNDR
))
189 printk(KERN_CRIT
"PSU under voltage.\n");
190 if(!(status
&WDC_SR_FANGOOD
))
191 printk(KERN_CRIT
"Possible fan fault.\n");
192 if(!(status
&WDC_SR_WCCR
))
193 #ifdef SOFTWARE_REBOOT
195 printk(KERN_CRIT
"Would Reboot.\n");
197 printk(KERN_CRIT
"Initiating system reboot.\n");
198 machine_restart(NULL
);
201 printk(KERN_CRIT
"Reset in 5ms.\n");
206 static long long wdt_llseek(struct file
*file
, long long offset
, int origin
)
214 * Reload counter one with the watchdog timeout. We don't bother reloading
215 * the cascade counter.
218 static void wdt_ping(void)
220 /* Write a watchdog value */
223 wdt_ctr_load(1,WD_TIMO
); /* Timeout */
229 * @file: file handle to the watchdog
230 * @buf: buffer to write (unused as data does not matter here
231 * @count: count of bytes
232 * @ppos: pointer to the position to write. No seeks allowed
234 * A write to a watchdog device is defined as a keepalive signal. Any
235 * write of data will do, as we we don't define content meaning.
238 static ssize_t
wdt_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
240 /* Can't seek (pwrite) on this device */
241 if (ppos
!= &file
->f_pos
)
254 * @file: file handle to the watchdog board
255 * @buf: buffer to write 1 byte into
256 * @count: length of buffer
257 * @ptr: offset (no seek allowed)
259 * Read reports the temperature in degrees Fahrenheit. The API is in
260 * farenheit. It was designed by an imperial measurement luddite.
263 static ssize_t
wdt_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ptr
)
265 unsigned short c
=inb_p(WDT_RT
);
268 /* Can't seek (pread) on this device */
269 if (ptr
!= &file
->f_pos
)
272 switch(MINOR(file
->f_dentry
->d_inode
->i_rdev
))
278 if(copy_to_user(buf
,&cp
,1))
288 * @inode: inode of the device
289 * @file: file handle to the device
290 * @cmd: watchdog command
291 * @arg: argument pointer
293 * The watchdog API defines a common set of functions for all watchdogs
294 * according to their available features. We only actually usefully support
295 * querying capabilities and current status.
298 static int wdt_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
301 static struct watchdog_info ident
=
303 WDIOF_OVERHEAT
|WDIOF_POWERUNDER
|WDIOF_POWEROVER
304 |WDIOF_EXTERN1
|WDIOF_EXTERN2
|WDIOF_FANFAULT
,
309 ident
.options
&=WDT_OPTION_MASK
; /* Mask down to the card we have */
314 case WDIOC_GETSUPPORT
:
315 return copy_to_user((struct watchdog_info
*)arg
, &ident
, sizeof(ident
))?-EFAULT
:0;
317 case WDIOC_GETSTATUS
:
318 return put_user(wdt_status(),(int *)arg
);
319 case WDIOC_GETBOOTSTATUS
:
320 return put_user(0, (int *)arg
);
321 case WDIOC_KEEPALIVE
:
329 * @inode: inode of device
330 * @file: file handle to device
332 * One of our two misc devices has been opened. The watchdog device is
333 * single open and on opening we load the counters. Counter zero is a
334 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
335 * counter triggers counter 2 downcounts the length of the reset pulse
336 * which set set to be as long as possible.
339 static int wdt_open(struct inode
*inode
, struct file
*file
)
341 switch(MINOR(inode
->i_rdev
))
351 inb_p(WDT_DC
); /* Disable */
355 wdt_ctr_load(0, 8948); /* count at 100Hz */
356 wdt_ctr_load(1,WD_TIMO
); /* Timeout 120 seconds */
357 wdt_ctr_load(2,65535);
358 outb_p(0, WDT_DC
); /* Enable */
369 * @inode: inode to board
370 * @file: file handle to board
372 * The watchdog has a configurable API. There is a religious dispute
373 * between people who want their watchdog to be able to shut down and
374 * those who want to be sure if the watchdog manager dies the machine
375 * reboots. In the former case we disable the counters, in the latter
376 * case you have to open it again very soon.
379 static int wdt_release(struct inode
*inode
, struct file
*file
)
382 if(MINOR(inode
->i_rdev
)==WATCHDOG_MINOR
)
384 #ifndef CONFIG_WATCHDOG_NOWAYOUT
385 inb_p(WDT_DC
); /* Disable counters */
386 wdt_ctr_load(2,0); /* 0 length reset pulses now */
396 * @this: our notifier block
397 * @code: the event being reported
400 * Our notifier is called on system shutdowns. We want to turn the card
401 * off at reboot otherwise the machine will reboot again during memory
402 * test or worse yet during the following fsck. This would suck, in fact
403 * trust me - if it happens it does suck.
406 static int wdt_notify_sys(struct notifier_block
*this, unsigned long code
,
409 if(code
==SYS_DOWN
|| code
==SYS_HALT
)
411 /* Turn the card off */
423 static struct file_operations wdt_fops
= {
430 release
: wdt_release
,
433 static struct miscdevice wdt_miscdev
=
440 #ifdef CONFIG_WDT_501
441 static struct miscdevice temp_miscdev
=
450 * The WDT card needs to learn about soft shutdowns in order to
451 * turn the timebomb registers off.
454 static struct notifier_block wdt_notifier
=
463 #define wdt_init init_module
468 * Unload the watchdog. You cannot do this with any file handles open.
469 * If your watchdog is set to continue ticking on close and you unload
470 * it, well it keeps ticking. We won't get the interrupt but the board
471 * will not touch PC memory so all is fine. You just have to load a new
472 * module in 60 seconds or reboot.
475 void cleanup_module(void)
477 misc_deregister(&wdt_miscdev
);
478 #ifdef CONFIG_WDT_501
479 misc_deregister(&temp_miscdev
);
481 unregister_reboot_notifier(&wdt_notifier
);
482 release_region(io
,8);
491 * Set up the WDT watchdog board. All we have to do is grab the
492 * resources we require and bitch if anyone beat us to them.
493 * The open() function will actually kick the board off.
496 int __init
wdt_init(void)
498 printk(KERN_INFO
"WDT500/501-P driver 0.07 at %X (Interrupt %d)\n", io
,irq
);
499 if(request_irq(irq
, wdt_interrupt
, SA_INTERRUPT
, "wdt501p", &wdt_miscdev
))
501 printk(KERN_ERR
"IRQ %d is not free.\n", irq
);
504 misc_register(&wdt_miscdev
);
505 #ifdef CONFIG_WDT_501
506 misc_register(&temp_miscdev
);
508 request_region(io
, 8, "wdt501p");
509 register_reboot_notifier(&wdt_notifier
);