4 * backported from w83627hf_wdt.c kernel 2.6.20.1
5 * (c) Copyright 2007 Orpak Systems Ltd. (Tal Kelrich <tal@orpak.com>)
7 * (c) Copyright 2003 Padraig Brady <P@draigBrady.com>
9 * Based on advantechwdt.c which is based on wdt.c.
10 * Original copyright messages:
12 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
14 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
15 * http://www.redhat.com
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
22 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
23 * warranty for any of this software. This material is provided
24 * "AS-IS" and at no charge.
26 * (c) Copyright 1995 Alan Cox <alan@redhat.com>
29 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/miscdevice.h>
32 #include <linux/watchdog.h>
34 #include <linux/ioport.h>
35 #include <linux/notifier.h>
36 #include <linux/reboot.h>
37 #include <linux/init.h>
38 #include <linux/spinlock.h>
39 #include <linux/proc_fs.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
46 #define WATCHDOG_NAME "W83627HF WDT"
47 #define PFX WATCHDOG_NAME ": "
48 #define WATCHDOG_TIMEOUT 120 /* 120 sec default timeout */
50 #ifdef CONFIG_WATCHDOG_NOWAYOUT
51 #define WATCHDOG_NOWAYOUT 1
53 #define WATCHDOG_NOWAYOUT 0
56 static unsigned long wdt_is_open;
57 static char expect_close;
58 static spinlock_t io_lock;
60 /* You must set this - there is no sane way to probe for this board. */
61 static int wdt_io = 0x2E;
62 MODULE_PARM(wdt_io, "i");
63 MODULE_PARM_DESC(wdt_io, "w83627hf WDT io port (default 0x2E)");
65 static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
66 MODULE_PARM(timeout, "i");
67 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=255, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
69 static int nowayout = WATCHDOG_NOWAYOUT;
70 MODULE_PARM(nowayout, "i");
71 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(CONFIG_WATCHDOG_NOWAYOUT)")");
77 #define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
78 #define WDT_EFIR (wdt_io+0) /* Extended Function Index Register (same as EFER) */
79 #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
81 /* Non standard proc bits, added by request, wanted some feedback */
84 w83627hf_select_wd_register(void)
86 outb_p(0x87, WDT_EFER); /* Enter extended function mode */
87 outb_p(0x87, WDT_EFER); /* Again according to manual */
89 outb_p(0x07, WDT_EFER); /* point to logical device number reg */
90 outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
91 outb_p(0x30, WDT_EFER); /* select CR30 */
92 outb_p(0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
96 w83627hf_unselect_wd_register(void)
98 outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
101 /* tyan motherboards seem to set F5 to 0x4C ?
102 * So explicitly init to appropriate value. */
108 w83627hf_select_wd_register();
110 outb_p(0xF6, WDT_EFER); /* Select CRF6 */
111 outb_p(0, WDT_EFDR); /* set to 0 initially */
112 outb_p(0xF5, WDT_EFER); /* Select CRF5 */
113 t=inb_p(WDT_EFDR); /* read CRF5 */
114 t&=~0x0C; /* set second mode & disable keyboard turning off watchdog */
115 outb_p(t, WDT_EFDR); /* Write back to CRF5 */
117 w83627hf_unselect_wd_register();
120 static int wdt_readproc(char *page, char **start, off_t off, int count,
121 int *eof, void *data)
124 unsigned char remaining;
127 w83627hf_select_wd_register();
128 outb_p(0xF6, WDT_EFIR);/* get current timer val */
129 remaining=inb_p(WDT_EFDR);
130 outb_p(0xF7, WDT_EFIR);
131 fired=inb_p(WDT_EFDR);
132 /* clear that bit (bit 4) */
133 outb_p(fired&(~0x10),WDT_EFDR);
134 w83627hf_unselect_wd_register();
135 spin_unlock(&io_lock);
136 fired=(fired&0x10)!=0;
137 len=snprintf(page,PAGE_SIZE,
145 wdt_is_open,wdt_io,nowayout,timeout,remaining,fired);
151 wdt_ctrl(int timeout)
155 w83627hf_select_wd_register();
157 outb_p(0xF6, WDT_EFER); /* Select CRF6 */
158 outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF6 */
160 w83627hf_unselect_wd_register();
162 spin_unlock(&io_lock);
180 wdt_set_heartbeat(int t)
182 if ((t < 1) || (t > 255))
190 wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
192 /* Can't seek (pwrite) on this device */
193 if (ppos != &file->f_pos)
201 for (i = 0; i != count; i++) {
203 if (get_user(c, buf+i))
215 wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
218 void *argp = (void *)arg;
221 static struct watchdog_info ident = {
222 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
223 .firmware_version = 1,
224 .identity = "W83627HF WDT",
228 case WDIOC_GETSUPPORT:
229 if (copy_to_user(argp, &ident, sizeof(ident)))
233 case WDIOC_GETSTATUS:
234 case WDIOC_GETBOOTSTATUS:
235 return put_user(0, p);
237 case WDIOC_KEEPALIVE:
241 case WDIOC_SETTIMEOUT:
242 if (get_user(new_timeout, p))
244 if (wdt_set_heartbeat(new_timeout))
249 case WDIOC_GETTIMEOUT:
250 return put_user(timeout, p);
252 case WDIOC_SETOPTIONS:
254 int options, retval = -EINVAL;
256 if (get_user(options, p))
259 if (options & WDIOS_DISABLECARD) {
264 if (options & WDIOS_ENABLECARD) {
279 wdt_open(struct inode *inode, struct file *file)
281 if (test_and_set_bit(0, &wdt_is_open))
292 wdt_close(struct inode *inode, struct file *file)
294 if (expect_close == 42) {
297 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
301 clear_bit(0, &wdt_is_open);
306 * Notifier for system down
310 wdt_notify_sys(struct notifier_block *this, unsigned long code,
313 if (code == SYS_DOWN || code == SYS_HALT) {
314 /* Turn the WDT off */
324 static const struct file_operations wdt_fops = {
325 .owner = THIS_MODULE,
329 .release = wdt_close,
332 static struct miscdevice wdt_miscdev = {
333 .minor = WATCHDOG_MINOR,
339 * The WDT needs to learn about soft shutdowns in order to
340 * turn the timebomb registers off.
343 static struct notifier_block wdt_notifier = {
344 .notifier_call = wdt_notify_sys,
352 spin_lock_init(&io_lock);
354 printk(KERN_INFO "WDT driver for the Winbond(TM) W83627HF Super I/O chip initialising.\n");
356 if (wdt_set_heartbeat(timeout)) {
357 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
358 printk (KERN_INFO PFX "timeout value must be 1<=timeout<=63, using %d\n",
361 /* on the board I was working on, wdt_io consistently coincided with things already
362 * being used, hence the continuing here.
363 * there's probably a better solution to this */
364 if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
365 printk (KERN_WARNING PFX "I/O address 0x%04x already in use, continuing anyway\n",
368 if(!create_proc_read_entry("watchdog",0,NULL,&wdt_readproc,NULL))
373 ret = register_reboot_notifier(&wdt_notifier);
375 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
380 ret = misc_register(&wdt_miscdev);
382 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
383 WATCHDOG_MINOR, ret);
387 printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
393 unregister_reboot_notifier(&wdt_notifier);
395 remove_proc_entry("watchdog",NULL);
397 release_region(wdt_io, 1);
404 remove_proc_entry("watchdog",NULL);
405 misc_deregister(&wdt_miscdev);
406 unregister_reboot_notifier(&wdt_notifier);
407 release_region(wdt_io,1);
408 /* we might not have gotten it... is this safe? */
411 module_init(wdt_init);
412 module_exit(wdt_exit);
414 MODULE_LICENSE("GPL");
415 MODULE_AUTHOR("Padraig Brady <P@draigBrady.com>");
416 MODULE_DESCRIPTION("w83627hf WDT driver");