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
32 * Tigran Aivazian : Restructured wdtpci_init_one() to handle failures
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/version.h>
38 #include <linux/types.h>
39 #include <linux/errno.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/miscdevice.h>
43 #include <linux/watchdog.h>
46 #include <linux/malloc.h>
47 #include <linux/ioport.h>
48 #include <linux/fcntl.h>
50 #include <asm/uaccess.h>
51 #include <asm/system.h>
52 #include <linux/notifier.h>
53 #include <linux/reboot.h>
54 #include <linux/init.h>
55 #include <linux/smp_lock.h>
57 #include <linux/pci.h>
59 #define PFX "wdt_pci: "
62 * Until Access I/O gets their application for a PCI vendor ID approved,
63 * I don't think that it's appropriate to move these constants into the
64 * regular pci_ids.h file. -- JPN 2000/01/18
67 #ifndef PCI_VENDOR_ID_ACCESSIO
68 #define PCI_VENDOR_ID_ACCESSIO 0x494f
70 #ifndef PCI_DEVICE_ID_WDG_CSM
71 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
74 static int wdt_is_open
;
77 * You must set these - there is no sane way to probe for this board.
78 * You can use wdt=x,y to set these now.
84 #define WD_TIMO (100*60) /* 1 minute */
90 * @str: command line string
92 * Setup options. The board isn't really probe-able so we have to
93 * get the user to tell us the configuration. Sane people build it
94 * modular but the others come here.
97 static int __init
wdtpci_setup(char *str
)
101 str
= get_options (str
, ARRAY_SIZE(ints
), ints
);
113 __setup("wdt=", wdtpci_setup
);
118 * Programming support
121 static void wdtpci_ctr_mode(int ctr
, int mode
)
129 static void wdtpci_ctr_load(int ctr
, int val
)
131 outb_p(val
&0xFF, WDT_COUNT0
+ctr
);
132 outb_p(val
>>8, WDT_COUNT0
+ctr
);
143 * Extract the status information from a WDT watchdog device. There are
144 * several board variants so we have to know which bits are valid. Some
145 * bits default to one and some to zero in order to be maximally painful.
147 * we then map the bits onto the status ioctl flags.
150 static int wdtpci_status(void)
153 * Status register to bit flags
157 unsigned char status
=inb_p(WDT_SR
);
159 status
&=~FEATUREMAP2
;
161 if(!(status
&WDC_SR_TGOOD
))
162 flag
|=WDIOF_OVERHEAT
;
163 if(!(status
&WDC_SR_PSUOVER
))
164 flag
|=WDIOF_POWEROVER
;
165 if(!(status
&WDC_SR_PSUUNDR
))
166 flag
|=WDIOF_POWERUNDER
;
167 if(!(status
&WDC_SR_FANGOOD
))
168 flag
|=WDIOF_FANFAULT
;
169 if(status
&WDC_SR_ISOI0
)
171 if(status
&WDC_SR_ISII1
)
178 * @irq: Interrupt number
179 * @dev_id: Unused as we don't allow multiple devices.
182 * Handle an interrupt from the board. These are raised when the status
183 * map changes in what the board considers an interesting way. That means
184 * a failure condition occuring.
187 static void wdtpci_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
190 * Read the status register see what is up and
194 unsigned char status
=inb_p(WDT_SR
);
197 status
&=~FEATUREMAP2
;
199 printk(KERN_CRIT
"WDT status %d\n", status
);
201 if(!(status
&WDC_SR_TGOOD
))
202 printk(KERN_CRIT
"Overheat alarm.(%d)\n",inb_p(WDT_RT
));
203 if(!(status
&WDC_SR_PSUOVER
))
204 printk(KERN_CRIT
"PSU over voltage.\n");
205 if(!(status
&WDC_SR_PSUUNDR
))
206 printk(KERN_CRIT
"PSU under voltage.\n");
207 if(!(status
&WDC_SR_FANGOOD
))
208 printk(KERN_CRIT
"Possible fan fault.\n");
209 if(!(status
&WDC_SR_WCCR
))
210 #ifdef SOFTWARE_REBOOT
212 printk(KERN_CRIT
"Would Reboot.\n");
214 printk(KERN_CRIT
"Initiating system reboot.\n");
215 machine_restart(NULL
);
218 printk(KERN_CRIT
"Reset in 5ms.\n");
223 static long long wdtpci_llseek(struct file
*file
, long long offset
, int origin
)
231 * Reload counter one with the watchdog timeout. We don't bother reloading
232 * the cascade counter.
235 static void wdtpci_ping(void)
237 /* Write a watchdog value */
239 wdtpci_ctr_mode(1,2);
240 wdtpci_ctr_load(1,WD_TIMO
); /* Timeout */
246 * @file: file handle to the watchdog
247 * @buf: buffer to write (unused as data does not matter here
248 * @count: count of bytes
249 * @ppos: pointer to the position to write. No seeks allowed
251 * A write to a watchdog device is defined as a keepalive signal. Any
252 * write of data will do, as we we don't define content meaning.
255 static ssize_t
wdtpci_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
257 /* Can't seek (pwrite) on this device */
258 if (ppos
!= &file
->f_pos
)
271 * @file: file handle to the watchdog board
272 * @buf: buffer to write 1 byte into
273 * @count: length of buffer
274 * @ptr: offset (no seek allowed)
276 * Read reports the temperature in degrees Fahrenheit. The API is in
277 * fahrenheit. It was designed by an imperial measurement luddite.
280 static ssize_t
wdtpci_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ptr
)
282 unsigned short c
=inb_p(WDT_RT
);
285 /* Can't seek (pread) on this device */
286 if (ptr
!= &file
->f_pos
)
289 switch(MINOR(file
->f_dentry
->d_inode
->i_rdev
))
295 if(copy_to_user(buf
,&cp
,1))
305 * @inode: inode of the device
306 * @file: file handle to the device
307 * @cmd: watchdog command
308 * @arg: argument pointer
310 * The watchdog API defines a common set of functions for all watchdogs
311 * according to their available features. We only actually usefully support
312 * querying capabilities and current status.
315 static int wdtpci_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
318 static struct watchdog_info ident
=
320 WDIOF_OVERHEAT
|WDIOF_POWERUNDER
|WDIOF_POWEROVER
321 |WDIOF_EXTERN1
|WDIOF_EXTERN2
|WDIOF_FANFAULT
,
326 ident
.options
&=WDT_OPTION_MASK
; /* Mask down to the card we have */
331 case WDIOC_GETSUPPORT
:
332 return copy_to_user((struct watchdog_info
*)arg
, &ident
, sizeof(ident
))?-EFAULT
:0;
334 case WDIOC_GETSTATUS
:
335 return put_user(wdtpci_status(),(int *)arg
);
336 case WDIOC_GETBOOTSTATUS
:
337 return put_user(0, (int *)arg
);
338 case WDIOC_KEEPALIVE
:
346 * @inode: inode of device
347 * @file: file handle to device
349 * One of our two misc devices has been opened. The watchdog device is
350 * single open and on opening we load the counters. Counter zero is a
351 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
352 * counter triggers counter 2 downcounts the length of the reset pulse
353 * which set set to be as long as possible.
356 static int wdtpci_open(struct inode
*inode
, struct file
*file
)
358 switch(MINOR(inode
->i_rdev
))
363 #ifdef CONFIG_WATCHDOG_NOWAYOUT
372 inb_p(WDT_DC
); /* Disable */
375 * "pet" the watchdog, as Access says.
376 * This resets the clock outputs.
379 wdtpci_ctr_mode(2,0);
384 outb_p(0, WDT_CLOCK
); /* 2.0833MHz clock */
385 inb_p(WDT_BUZZER
); /* disable */
386 inb_p(WDT_OPTONOTRST
); /* disable */
387 inb_p(WDT_OPTORST
); /* disable */
388 inb_p(WDT_PROGOUT
); /* disable */
389 wdtpci_ctr_mode(0,3);
390 wdtpci_ctr_mode(1,2);
391 wdtpci_ctr_mode(2,1);
392 wdtpci_ctr_load(0,20833); /* count at 100Hz */
393 wdtpci_ctr_load(1,WD_TIMO
);/* Timeout 60 seconds */
394 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
395 outb_p(0, WDT_DC
); /* Enable */
406 * @inode: inode to board
407 * @file: file handle to board
409 * The watchdog has a configurable API. There is a religious dispute
410 * between people who want their watchdog to be able to shut down and
411 * those who want to be sure if the watchdog manager dies the machine
412 * reboots. In the former case we disable the counters, in the latter
413 * case you have to open it again very soon.
416 static int wdtpci_release(struct inode
*inode
, struct file
*file
)
418 if(MINOR(inode
->i_rdev
)==WATCHDOG_MINOR
)
421 #ifndef CONFIG_WATCHDOG_NOWAYOUT
422 inb_p(WDT_DC
); /* Disable counters */
423 wdtpci_ctr_load(2,0); /* 0 length reset pulses now */
433 * @this: our notifier block
434 * @code: the event being reported
437 * Our notifier is called on system shutdowns. We want to turn the card
438 * off at reboot otherwise the machine will reboot again during memory
439 * test or worse yet during the following fsck. This would suck, in fact
440 * trust me - if it happens it does suck.
443 static int wdtpci_notify_sys(struct notifier_block
*this, unsigned long code
,
446 if(code
==SYS_DOWN
|| code
==SYS_HALT
)
448 /* Turn the card off */
450 wdtpci_ctr_load(2,0);
460 static struct file_operations wdtpci_fops
= {
462 llseek
: wdtpci_llseek
,
467 release
: wdtpci_release
,
470 static struct miscdevice wdtpci_miscdev
=
477 #ifdef CONFIG_WDT_501
478 static struct miscdevice temp_miscdev
=
487 * The WDT card needs to learn about soft shutdowns in order to
488 * turn the timebomb registers off.
491 static struct notifier_block wdtpci_notifier
=
499 static int __init
wdtpci_init_one (struct pci_dev
*dev
,
500 const struct pci_device_id
*ent
)
502 static int dev_count
= 0;
508 "this driver only supports 1 device\n");
513 io
= pci_resource_start (dev
, 2);
514 printk ("WDT501-P(PCI-WDG-CSM) driver 0.07 at %X "
515 "(Interrupt %d)\n", io
, irq
);
517 if (pci_enable_device (dev
))
520 if (request_region (io
, 16, "wdt-pci") == NULL
) {
521 printk (KERN_ERR PFX
"I/O %d is not free.\n", io
);
525 if (request_irq (irq
, wdtpci_interrupt
, SA_INTERRUPT
| SA_SHIRQ
,
526 "wdt-pci", &wdtpci_miscdev
)) {
527 printk (KERN_ERR PFX
"IRQ %d is not free.\n", irq
);
531 ret
= misc_register (&wdtpci_miscdev
);
533 printk (KERN_ERR PFX
"can't misc_register on minor=%d\n", WATCHDOG_MINOR
);
537 ret
= register_reboot_notifier (&wdtpci_notifier
);
539 printk (KERN_ERR PFX
"can't misc_register on minor=%d\n", WATCHDOG_MINOR
);
542 #ifdef CONFIG_WDT_501
543 ret
= misc_register (&temp_miscdev
);
545 printk (KERN_ERR PFX
"can't misc_register (temp) on minor=%d\n", TEMP_MINOR
);
554 #ifdef CONFIG_WDT_501
556 unregister_reboot_notifier(&wdtpci_notifier
);
559 misc_deregister(&wdtpci_miscdev
);
561 free_irq(irq
, &wdtpci_miscdev
);
563 release_region (io
, 16);
568 static void __exit
wdtpci_remove_one (struct pci_dev
*pdev
)
570 /* here we assume only one device will ever have
571 * been picked up and registered by probe function */
572 unregister_reboot_notifier(&wdtpci_notifier
);
573 #ifdef CONFIG_WDT_501_PCI
574 misc_deregister(&temp_miscdev
);
576 misc_deregister(&wdtpci_miscdev
);
577 free_irq(irq
, &wdtpci_miscdev
);
578 release_region(io
, 16);
582 static struct pci_device_id wdtpci_pci_tbl
[] __initdata
= {
583 { PCI_VENDOR_ID_ACCESSIO
, PCI_DEVICE_ID_WDG_CSM
, PCI_ANY_ID
, PCI_ANY_ID
, },
584 { 0, }, /* terminate list */
586 MODULE_DEVICE_TABLE(pci
, wdtpci_pci_tbl
);
589 static struct pci_driver wdtpci_driver
= {
591 id_table
: wdtpci_pci_tbl
,
592 probe
: wdtpci_init_one
,
593 remove
: wdtpci_remove_one
,
600 * Unload the watchdog. You cannot do this with any file handles open.
601 * If your watchdog is set to continue ticking on close and you unload
602 * it, well it keeps ticking. We won't get the interrupt but the board
603 * will not touch PC memory so all is fine. You just have to load a new
604 * module in 60 seconds or reboot.
607 static void __exit
wdtpci_cleanup(void)
609 pci_unregister_driver (&wdtpci_driver
);
616 * Set up the WDT watchdog board. All we have to do is grab the
617 * resources we require and bitch if anyone beat us to them.
618 * The open() function will actually kick the board off.
621 static int __init
wdtpci_init(void)
623 int rc
= pci_register_driver (&wdtpci_driver
);
632 module_init(wdtpci_init
);
633 module_exit(wdtpci_cleanup
);