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/miscdevice.h>
39 #include <linux/watchdog.h>
41 #include <linux/malloc.h>
42 #include <linux/ioport.h>
43 #include <linux/fcntl.h>
45 #include <asm/uaccess.h>
46 #include <asm/system.h>
47 #include <linux/notifier.h>
48 #include <linux/reboot.h>
49 #include <linux/init.h>
51 static int wdt_is_open
=0;
54 * You must set these - there is no sane way to probe for this board.
55 * You can use wdt=x,y to set these now.
61 #define WD_TIMO (100*60) /* 1 minute */
67 * @str: command line string
69 * Setup options. The board isn't really probe-able so we have to
70 * get the user to tell us the configuration. Sane people build it
71 * modular but the others come here.
74 static int __init
wdt_setup(char *str
)
78 str
= get_options (str
, ARRAY_SIZE(ints
), ints
);
90 __setup("wdt=", wdt_setup
);
98 static void wdt_ctr_mode(int ctr
, int mode
)
106 static void wdt_ctr_load(int ctr
, int val
)
108 outb_p(val
&0xFF, WDT_COUNT0
+ctr
);
109 outb_p(val
>>8, WDT_COUNT0
+ctr
);
120 * Extract the status information from a WDT watchdog device. There are
121 * several board variants so we have to know which bits are valid. Some
122 * bits default to one and some to zero in order to be maximally painful.
124 * we then map the bits onto the status ioctl flags.
127 static int wdt_status(void)
130 * Status register to bit flags
134 unsigned char status
=inb_p(WDT_SR
);
136 status
&=~FEATUREMAP2
;
138 if(!(status
&WDC_SR_TGOOD
))
139 flag
|=WDIOF_OVERHEAT
;
140 if(!(status
&WDC_SR_PSUOVER
))
141 flag
|=WDIOF_POWEROVER
;
142 if(!(status
&WDC_SR_PSUUNDR
))
143 flag
|=WDIOF_POWERUNDER
;
144 if(!(status
&WDC_SR_FANGOOD
))
145 flag
|=WDIOF_FANFAULT
;
146 if(status
&WDC_SR_ISOI0
)
148 if(status
&WDC_SR_ISII1
)
155 * @irq: Interrupt number
156 * @dev_id: Unused as we don't allow multiple devices.
159 * Handle an interrupt from the board. These are raised when the status
160 * map changes in what the board considers an interesting way. That means
161 * a failure condition occuring.
164 void wdt_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
167 * Read the status register see what is up and
171 unsigned char status
=inb_p(WDT_SR
);
174 status
&=~FEATUREMAP2
;
176 printk(KERN_CRIT
"WDT status %d\n", status
);
178 if(!(status
&WDC_SR_TGOOD
))
179 printk(KERN_CRIT
"Overheat alarm.(%d)\n",inb_p(WDT_RT
));
180 if(!(status
&WDC_SR_PSUOVER
))
181 printk(KERN_CRIT
"PSU over voltage.\n");
182 if(!(status
&WDC_SR_PSUUNDR
))
183 printk(KERN_CRIT
"PSU under voltage.\n");
184 if(!(status
&WDC_SR_FANGOOD
))
185 printk(KERN_CRIT
"Possible fan fault.\n");
186 if(!(status
&WDC_SR_WCCR
))
187 #ifdef SOFTWARE_REBOOT
189 printk(KERN_CRIT
"Would Reboot.\n");
191 printk(KERN_CRIT
"Initiating system reboot.\n");
192 machine_restart(NULL
);
195 printk(KERN_CRIT
"Reset in 5ms.\n");
200 static long long wdt_llseek(struct file
*file
, long long offset
, int origin
)
208 * Reload counter one with the watchdog timeout. We don't bother reloading
209 * the cascade counter.
212 static void wdt_ping(void)
214 /* Write a watchdog value */
217 wdt_ctr_load(1,WD_TIMO
); /* Timeout */
223 * @file: file handle to the watchdog
224 * @buf: buffer to write (unused as data does not matter here
225 * @count: count of bytes
226 * @ppos: pointer to the position to write. No seeks allowed
228 * A write to a watchdog device is defined as a keepalive signal. Any
229 * write of data will do, as we we don't define content meaning.
232 static ssize_t
wdt_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
234 /* Can't seek (pwrite) on this device */
235 if (ppos
!= &file
->f_pos
)
248 * @file: file handle to the watchdog board
249 * @buf: buffer to write 1 byte into
250 * @count: length of buffer
251 * @ptr: offset (no seek allowed)
253 * Read reports the temperature in degrees Fahrenheit. The API is in
254 * farenheit. It was designed by an imperial measurement luddite.
257 static ssize_t
wdt_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ptr
)
259 unsigned short c
=inb_p(WDT_RT
);
262 /* Can't seek (pread) on this device */
263 if (ptr
!= &file
->f_pos
)
266 switch(MINOR(file
->f_dentry
->d_inode
->i_rdev
))
272 if(copy_to_user(buf
,&cp
,1))
282 * @inode: inode of the device
283 * @file: file handle to the device
284 * @cmd: watchdog command
285 * @arg: argument pointer
287 * The watchdog API defines a common set of functions for all watchdogs
288 * according to their available features. We only actually usefully support
289 * querying capabilities and current status.
292 static int wdt_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
295 static struct watchdog_info ident
=
297 WDIOF_OVERHEAT
|WDIOF_POWERUNDER
|WDIOF_POWEROVER
298 |WDIOF_EXTERN1
|WDIOF_EXTERN2
|WDIOF_FANFAULT
,
303 ident
.options
&=WDT_OPTION_MASK
; /* Mask down to the card we have */
308 case WDIOC_GETSUPPORT
:
309 return copy_to_user((struct watchdog_info
*)arg
, &ident
, sizeof(ident
))?-EFAULT
:0;
311 case WDIOC_GETSTATUS
:
312 return put_user(wdt_status(),(int *)arg
);
313 case WDIOC_GETBOOTSTATUS
:
314 return put_user(0, (int *)arg
);
315 case WDIOC_KEEPALIVE
:
323 * @inode: inode of device
324 * @file: file handle to device
326 * One of our two misc devices has been opened. The watchdog device is
327 * single open and on opening we load the counters. Counter zero is a
328 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
329 * counter triggers counter 2 downcounts the length of the reset pulse
330 * which set set to be as long as possible.
333 static int wdt_open(struct inode
*inode
, struct file
*file
)
335 switch(MINOR(inode
->i_rdev
))
345 inb_p(WDT_DC
); /* Disable */
349 wdt_ctr_load(0, 8948); /* count at 100Hz */
350 wdt_ctr_load(1,WD_TIMO
); /* Timeout 120 seconds */
351 wdt_ctr_load(2,65535);
352 outb_p(0, WDT_DC
); /* Enable */
363 * @inode: inode to board
364 * @file: file handle to board
366 * The watchdog has a configurable API. There is a religious dispute
367 * between people who want their watchdog to be able to shut down and
368 * those who want to be sure if the watchdog manager dies the machine
369 * reboots. In the former case we disable the counters, in the latter
370 * case you have to open it again very soon.
373 static int wdt_release(struct inode
*inode
, struct file
*file
)
375 if(MINOR(inode
->i_rdev
)==WATCHDOG_MINOR
)
377 #ifndef CONFIG_WATCHDOG_NOWAYOUT
378 inb_p(WDT_DC
); /* Disable counters */
379 wdt_ctr_load(2,0); /* 0 length reset pulses now */
388 * @this: our notifier block
389 * @code: the event being reported
392 * Our notifier is called on system shutdowns. We want to turn the card
393 * off at reboot otherwise the machine will reboot again during memory
394 * test or worse yet during the following fsck. This would suck, in fact
395 * trust me - if it happens it does suck.
398 static int wdt_notify_sys(struct notifier_block
*this, unsigned long code
,
401 if(code
==SYS_DOWN
|| code
==SYS_HALT
)
403 /* Turn the card off */
415 static struct file_operations wdt_fops
= {
422 release
: wdt_release
,
425 static struct miscdevice wdt_miscdev
=
432 #ifdef CONFIG_WDT_501
433 static struct miscdevice temp_miscdev
=
442 * The WDT card needs to learn about soft shutdowns in order to
443 * turn the timebomb registers off.
446 static struct notifier_block wdt_notifier
=
455 #define wdt_init init_module
460 * Unload the watchdog. You cannot do this with any file handles open.
461 * If your watchdog is set to continue ticking on close and you unload
462 * it, well it keeps ticking. We won't get the interrupt but the board
463 * will not touch PC memory so all is fine. You just have to load a new
464 * module in 60 seconds or reboot.
467 void cleanup_module(void)
469 misc_deregister(&wdt_miscdev
);
470 #ifdef CONFIG_WDT_501
471 misc_deregister(&temp_miscdev
);
473 unregister_reboot_notifier(&wdt_notifier
);
474 release_region(io
,8);
483 * Set up the WDT watchdog board. All we have to do is grab the
484 * resources we require and bitch if anyone beat us to them.
485 * The open() function will actually kick the board off.
488 int __init
wdt_init(void)
490 printk(KERN_INFO
"WDT500/501-P driver 0.07 at %X (Interrupt %d)\n", io
,irq
);
491 if(request_irq(irq
, wdt_interrupt
, SA_INTERRUPT
, "wdt501p", &wdt_miscdev
))
493 printk(KERN_ERR
"IRQ %d is not free.\n", irq
);
496 misc_register(&wdt_miscdev
);
497 #ifdef CONFIG_WDT_501
498 misc_register(&temp_miscdev
);
500 request_region(io
, 8, "wdt501p");
501 register_reboot_notifier(&wdt_notifier
);