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 * JP Nollmann : Added support for PCI wdt501p
30 * Alan Cox : Split ISA and PCI cards into two drivers
31 * Jeff Garzik : PCI cleanups
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/version.h>
37 #include <linux/types.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/miscdevice.h>
42 #include <linux/watchdog.h>
45 #include <linux/malloc.h>
46 #include <linux/ioport.h>
47 #include <linux/fcntl.h>
49 #include <asm/uaccess.h>
50 #include <asm/system.h>
51 #include <linux/notifier.h>
52 #include <linux/reboot.h>
53 #include <linux/init.h>
55 #include <linux/pci.h>
57 #define PFX "wdt_pci: "
60 * Until Access I/O gets their application for a PCI vendor ID approved,
61 * I don't think that it's appropriate to move these constants into the
62 * regular pci_ids.h file. -- JPN 2000/01/18
65 #ifndef PCI_VENDOR_ID_ACCESSIO
66 #define PCI_VENDOR_ID_ACCESSIO 0x494f
68 #ifndef PCI_DEVICE_ID_WDG_CSM
69 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
72 static int wdt_is_open
=0;
75 * You must set these - there is no sane way to probe for this board.
76 * You can use wdt=x,y to set these now.
82 #define WD_TIMO (100*60) /* 1 minute */
88 * @str: command line string
90 * Setup options. The board isn't really probe-able so we have to
91 * get the user to tell us the configuration. Sane people build it
92 * modular but the others come here.
95 static int __init
wdtpci_setup(char *str
)
99 str
= get_options (str
, ARRAY_SIZE(ints
), ints
);
111 __setup("wdt=", wdtpci_setup
);
116 * Programming support
119 static void wdtpci_ctr_mode(int ctr
, int mode
)
127 static void wdtpci_ctr_load(int ctr
, int val
)
129 outb_p(val
&0xFF, WDT_COUNT0
+ctr
);
130 outb_p(val
>>8, WDT_COUNT0
+ctr
);
141 * Extract the status information from a WDT watchdog device. There are
142 * several board variants so we have to know which bits are valid. Some
143 * bits default to one and some to zero in order to be maximally painful.
145 * we then map the bits onto the status ioctl flags.
148 static int wdtpci_status(void)
151 * Status register to bit flags
155 unsigned char status
=inb_p(WDT_SR
);
157 status
&=~FEATUREMAP2
;
159 if(!(status
&WDC_SR_TGOOD
))
160 flag
|=WDIOF_OVERHEAT
;
161 if(!(status
&WDC_SR_PSUOVER
))
162 flag
|=WDIOF_POWEROVER
;
163 if(!(status
&WDC_SR_PSUUNDR
))
164 flag
|=WDIOF_POWERUNDER
;
165 if(!(status
&WDC_SR_FANGOOD
))
166 flag
|=WDIOF_FANFAULT
;
167 if(status
&WDC_SR_ISOI0
)
169 if(status
&WDC_SR_ISII1
)
176 * @irq: Interrupt number
177 * @dev_id: Unused as we don't allow multiple devices.
180 * Handle an interrupt from the board. These are raised when the status
181 * map changes in what the board considers an interesting way. That means
182 * a failure condition occuring.
185 static void wdtpci_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
188 * Read the status register see what is up and
192 unsigned char status
=inb_p(WDT_SR
);
195 status
&=~FEATUREMAP2
;
197 printk(KERN_CRIT
"WDT status %d\n", status
);
199 if(!(status
&WDC_SR_TGOOD
))
200 printk(KERN_CRIT
"Overheat alarm.(%d)\n",inb_p(WDT_RT
));
201 if(!(status
&WDC_SR_PSUOVER
))
202 printk(KERN_CRIT
"PSU over voltage.\n");
203 if(!(status
&WDC_SR_PSUUNDR
))
204 printk(KERN_CRIT
"PSU under voltage.\n");
205 if(!(status
&WDC_SR_FANGOOD
))
206 printk(KERN_CRIT
"Possible fan fault.\n");
207 if(!(status
&WDC_SR_WCCR
))
208 #ifdef SOFTWARE_REBOOT
210 printk(KERN_CRIT
"Would Reboot.\n");
212 printk(KERN_CRIT
"Initiating system reboot.\n");
213 machine_restart(NULL
);
216 printk(KERN_CRIT
"Reset in 5ms.\n");
221 static long long wdtpci_llseek(struct file
*file
, long long offset
, int origin
)
229 * Reload counter one with the watchdog timeout. We don't bother reloading
230 * the cascade counter.
233 static void wdtpci_ping(void)
235 /* Write a watchdog value */
237 wdtpci_ctr_mode(1,2);
238 wdtpci_ctr_load(1,WD_TIMO
); /* Timeout */
244 * @file: file handle to the watchdog
245 * @buf: buffer to write (unused as data does not matter here
246 * @count: count of bytes
247 * @ppos: pointer to the position to write. No seeks allowed
249 * A write to a watchdog device is defined as a keepalive signal. Any
250 * write of data will do, as we we don't define content meaning.
253 static ssize_t
wdtpci_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
255 /* Can't seek (pwrite) on this device */
256 if (ppos
!= &file
->f_pos
)
269 * @file: file handle to the watchdog board
270 * @buf: buffer to write 1 byte into
271 * @count: length of buffer
272 * @ptr: offset (no seek allowed)
274 * Read reports the temperature in degrees Fahrenheit. The API is in
275 * farenheit. It was designed by an imperial measurement luddite.
278 static ssize_t
wdtpci_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ptr
)
280 unsigned short c
=inb_p(WDT_RT
);
283 /* Can't seek (pread) on this device */
284 if (ptr
!= &file
->f_pos
)
287 switch(MINOR(file
->f_dentry
->d_inode
->i_rdev
))
293 if(copy_to_user(buf
,&cp
,1))
303 * @inode: inode of the device
304 * @file: file handle to the device
305 * @cmd: watchdog command
306 * @arg: argument pointer
308 * The watchdog API defines a common set of functions for all watchdogs
309 * according to their available features. We only actually usefully support
310 * querying capabilities and current status.
313 static int wdtpci_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
316 static struct watchdog_info ident
=
318 WDIOF_OVERHEAT
|WDIOF_POWERUNDER
|WDIOF_POWEROVER
319 |WDIOF_EXTERN1
|WDIOF_EXTERN2
|WDIOF_FANFAULT
,
324 ident
.options
&=WDT_OPTION_MASK
; /* Mask down to the card we have */
329 case WDIOC_GETSUPPORT
:
330 return copy_to_user((struct watchdog_info
*)arg
, &ident
, sizeof(ident
))?-EFAULT
:0;
332 case WDIOC_GETSTATUS
:
333 return put_user(wdtpci_status(),(int *)arg
);
334 case WDIOC_GETBOOTSTATUS
:
335 return put_user(0, (int *)arg
);
336 case WDIOC_KEEPALIVE
:
344 * @inode: inode of device
345 * @file: file handle to device
347 * One of our two misc devices has been opened. The watchdog device is
348 * single open and on opening we load the counters. Counter zero is a
349 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
350 * counter triggers counter 2 downcounts the length of the reset pulse
351 * which set set to be as long as possible.
354 static int wdtpci_open(struct inode
*inode
, struct file
*file
)
356 switch(MINOR(inode
->i_rdev
))
368 inb_p(WDT_DC
); /* Disable */
371 * "pet" the watchdog, as Access says.
372 * This resets the clock outputs.
375 wdtpci_ctr_mode(2,0);
380 outb_p(0, WDT_CLOCK
); /* 2.0833MHz clock */
381 inb_p(WDT_BUZZER
); /* disable */
382 inb_p(WDT_OPTONOTRST
); /* disable */
383 inb_p(WDT_OPTORST
); /* disable */
384 inb_p(WDT_PROGOUT
); /* disable */
385 wdtpci_ctr_mode(0,3);
386 wdtpci_ctr_mode(1,2);
387 wdtpci_ctr_mode(2,1);
388 wdtpci_ctr_load(0,20833); /* count at 100Hz */
389 wdtpci_ctr_load(1,WD_TIMO
);/* Timeout 60 seconds */
390 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
391 outb_p(0, WDT_DC
); /* Enable */
403 * @inode: inode to board
404 * @file: file handle to board
406 * The watchdog has a configurable API. There is a religious dispute
407 * between people who want their watchdog to be able to shut down and
408 * those who want to be sure if the watchdog manager dies the machine
409 * reboots. In the former case we disable the counters, in the latter
410 * case you have to open it again very soon.
413 static int wdtpci_release(struct inode
*inode
, struct file
*file
)
415 if(MINOR(inode
->i_rdev
)==WATCHDOG_MINOR
)
417 #ifndef CONFIG_WATCHDOG_NOWAYOUT
418 inb_p(WDT_DC
); /* Disable counters */
419 wdtpci_ctr_load(2,0); /* 0 length reset pulses now */
429 * @this: our notifier block
430 * @code: the event being reported
433 * Our notifier is called on system shutdowns. We want to turn the card
434 * off at reboot otherwise the machine will reboot again during memory
435 * test or worse yet during the following fsck. This would suck, in fact
436 * trust me - if it happens it does suck.
439 static int wdtpci_notify_sys(struct notifier_block
*this, unsigned long code
,
442 if(code
==SYS_DOWN
|| code
==SYS_HALT
)
444 /* Turn the card off */
446 wdtpci_ctr_load(2,0);
456 static struct file_operations wdtpci_fops
= {
457 llseek
: wdtpci_llseek
,
462 release
: wdtpci_release
,
465 static struct miscdevice wdtpci_miscdev
=
472 #ifdef CONFIG_WDT_501
473 static struct miscdevice temp_miscdev
=
482 * The WDT card needs to learn about soft shutdowns in order to
483 * turn the timebomb registers off.
486 static struct notifier_block wdtpci_notifier
=
494 static int __init
wdtpci_init_one (struct pci_dev
*dev
,
495 const struct pci_device_id
*ent
)
497 static int dev_count
= 0;
502 "this driver only supports 1 device\n");
507 io
= pci_resource_start (dev
, 2);
508 printk ("WDT501-P(PCI-WDG-CSM) driver 0.07 at %X "
509 "(Interrupt %d)\n", io
, irq
);
511 if (pci_enable_device (dev
))
514 if (request_region (io
, 16, "wdt-pci") == NULL
) {
515 printk (KERN_ERR PFX
"I/O %d is not free.\n", io
);
519 if (request_irq (irq
, wdtpci_interrupt
, SA_INTERRUPT
| SA_SHIRQ
,
520 "wdt-pci", &wdtpci_miscdev
)) {
521 printk (KERN_ERR PFX
"IRQ %d is not free.\n", irq
);
522 goto err_out_free_res
;
525 misc_register (&wdtpci_miscdev
);
527 #ifdef CONFIG_WDT_501
528 misc_register (&temp_miscdev
);
531 register_reboot_notifier (&wdtpci_notifier
);
536 release_region (io
, 16);
542 static void __exit
wdtpci_remove_one (struct pci_dev
*pdev
)
544 /* here we assume only one device will ever have
545 * been picked up and registered by probe function */
546 unregister_reboot_notifier(&wdtpci_notifier
);
547 #ifdef CONFIG_WDT_501_PCI
548 misc_deregister(&temp_miscdev
);
550 misc_deregister(&wdtpci_miscdev
);
551 free_irq(irq
, &wdtpci_miscdev
);
552 release_region(io
, 16);
556 static struct pci_device_id wdtpci_pci_tbl
[] __initdata
= {
557 { PCI_VENDOR_ID_ACCESSIO
, PCI_DEVICE_ID_WDG_CSM
, PCI_ANY_ID
, PCI_ANY_ID
, },
558 { 0, }, /* terminate list */
560 MODULE_DEVICE_TABLE(pci
, wdtpci_pci_tbl
);
563 static struct pci_driver wdtpci_driver
= {
565 id_table
: wdtpci_pci_tbl
,
566 probe
: wdtpci_init_one
,
567 remove
: wdtpci_remove_one
,
574 * Unload the watchdog. You cannot do this with any file handles open.
575 * If your watchdog is set to continue ticking on close and you unload
576 * it, well it keeps ticking. We won't get the interrupt but the board
577 * will not touch PC memory so all is fine. You just have to load a new
578 * module in 60 seconds or reboot.
581 static void __exit
wdtpci_cleanup(void)
583 pci_unregister_driver (&wdtpci_driver
);
590 * Set up the WDT watchdog board. All we have to do is grab the
591 * resources we require and bitch if anyone beat us to them.
592 * The open() function will actually kick the board off.
595 static int __init
wdtpci_init(void)
597 int rc
= pci_register_driver (&wdtpci_driver
);
606 module_init(wdtpci_init
);
607 module_exit(wdtpci_cleanup
);