2 * NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
4 * This code is based on wdt.c with original copyright.
6 * (C) Copyright 2006 Sven Anders, <anders@anduras.de>
7 * and Marcus Junker, <junker@anduras.de>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Neither Sven Anders, Marcus Junker nor ANDURAS AG
15 * admit liability nor provide warranty for any of this software.
16 * This material is provided "AS-IS" and at no charge.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/ioport.h>
27 #include <linux/delay.h>
28 #include <linux/notifier.h>
30 #include <linux/reboot.h>
31 #include <linux/init.h>
32 #include <linux/spinlock.h>
33 #include <linux/moduleparam.h>
34 #include <linux/version.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
42 #define DEFAULT_TIMEOUT 1 /* 1 minute */
43 #define MAX_TIMEOUT 255
46 #define MODNAME "pc87413 WDT"
47 #define PFX MODNAME ": "
48 #define DPFX MODNAME " - DEBUG: "
50 #define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
51 #define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
53 #define SIOCFG2 0x22 /* Serial IO register */
54 #define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
55 #define WDTO 0x11 /* Watchdog timeout register */
56 #define WDCFG 0x12 /* Watchdog config register */
58 static int io = 0x2E; /* Address used on Portwell Boards */
60 static int timeout = DEFAULT_TIMEOUT; /* timeout value */
61 static unsigned long timer_enabled = 0; /* is the timer enabled? */
63 static char expect_close; /* is the close expected? */
65 static spinlock_t io_lock; /* to guard the watchdog from io races */
67 static int nowayout = WATCHDOG_NOWAYOUT;
69 /* -- Low level function ----------------------------------------*/
71 /* Select pins for Watchdog output */
73 static inline void pc87413_select_wdt_out (void)
75 unsigned int cr_data = 0;
77 /* Step 1: Select multiple pin,pin55,as WDT output */
79 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
81 cr_data = inb (WDT_DATA_IO_PORT);
83 cr_data |= 0x80; /* Set Bit7 to 1*/
84 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
86 outb_p(cr_data, WDT_DATA_IO_PORT);
89 printk(KERN_INFO DPFX "Select multiple pin,pin55,as WDT output:"
90 " Bit7 to 1: %d\n", cr_data);
94 /* Enable SWC functions */
96 static inline void pc87413_enable_swc(void)
98 unsigned int cr_data=0;
100 /* Step 2: Enable SWC functions */
102 outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
103 outb_p(SWC_LDN, WDT_DATA_IO_PORT);
105 outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
106 cr_data = inb(WDT_DATA_IO_PORT);
107 cr_data |= 0x01; /* Set Bit0 to 1 */
108 outb_p(0x30, WDT_INDEX_IO_PORT);
109 outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
112 printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
116 /* Read SWC I/O base address */
118 static inline unsigned int pc87413_get_swc_base(void)
120 unsigned int swc_base_addr = 0;
121 unsigned char addr_l, addr_h = 0;
123 /* Step 3: Read SWC I/O Base Address */
125 outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
126 addr_h = inb(WDT_DATA_IO_PORT);
128 outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
130 addr_l = inb(WDT_DATA_IO_PORT);
132 swc_base_addr = (addr_h << 8) + addr_l;
135 printk(KERN_INFO DPFX "Read SWC I/O Base Address: low %d, high %d,"
136 " res %d\n", addr_l, addr_h, swc_base_addr);
139 return swc_base_addr;
142 /* Select Bank 3 of SWC */
144 static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
146 /* Step 4: Select Bank3 of SWC */
148 outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
151 printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
155 /* Set watchdog timeout to x minutes */
157 static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
160 /* Step 5: Programm WDTO, Twd. */
162 outb_p(pc87413_time, swc_base_addr + WDTO);
165 printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
171 static inline void pc87413_enable_wden(unsigned int swc_base_addr)
173 /* Step 6: Enable WDEN */
175 outb_p(inb (swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
178 printk(KERN_INFO DPFX "Enable WDEN\n");
182 /* Enable SW_WD_TREN */
183 static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
185 /* Enable SW_WD_TREN */
187 outb_p(inb (swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
190 printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
194 /* Disable SW_WD_TREN */
196 static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
198 /* Disable SW_WD_TREN */
200 outb_p(inb (swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
203 printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
207 /* Enable SW_WD_TRG */
209 static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
211 /* Enable SW_WD_TRG */
213 outb_p(inb (swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
216 printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
220 /* Disable SW_WD_TRG */
222 static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
224 /* Disable SW_WD_TRG */
226 outb_p(inb (swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
229 printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
233 /* -- Higher level functions ------------------------------------*/
235 /* Enable the watchdog */
237 static void pc87413_enable(void)
239 unsigned int swc_base_addr;
243 pc87413_select_wdt_out();
244 pc87413_enable_swc();
245 swc_base_addr = pc87413_get_swc_base();
246 pc87413_swc_bank3(swc_base_addr);
247 pc87413_programm_wdto(swc_base_addr, timeout);
248 pc87413_enable_wden(swc_base_addr);
249 pc87413_enable_sw_wd_tren(swc_base_addr);
250 pc87413_enable_sw_wd_trg(swc_base_addr);
252 spin_unlock(&io_lock);
255 /* Disable the watchdog */
257 static void pc87413_disable(void)
259 unsigned int swc_base_addr;
263 pc87413_select_wdt_out();
264 pc87413_enable_swc();
265 swc_base_addr = pc87413_get_swc_base();
266 pc87413_swc_bank3(swc_base_addr);
267 pc87413_disable_sw_wd_tren(swc_base_addr);
268 pc87413_disable_sw_wd_trg(swc_base_addr);
269 pc87413_programm_wdto(swc_base_addr, 0);
271 spin_unlock(&io_lock);
274 /* Refresh the watchdog */
276 static void pc87413_refresh(void)
278 unsigned int swc_base_addr;
282 pc87413_select_wdt_out();
283 pc87413_enable_swc();
284 swc_base_addr = pc87413_get_swc_base();
285 pc87413_swc_bank3(swc_base_addr);
286 pc87413_disable_sw_wd_tren(swc_base_addr);
287 pc87413_disable_sw_wd_trg(swc_base_addr);
288 pc87413_programm_wdto(swc_base_addr, timeout);
289 pc87413_enable_wden(swc_base_addr);
290 pc87413_enable_sw_wd_tren(swc_base_addr);
291 pc87413_enable_sw_wd_trg(swc_base_addr);
293 spin_unlock(&io_lock);
296 /* -- File operations -------------------------------------------*/
300 * @inode: inode of device
301 * @file: file handle to device
305 static int pc87413_open(struct inode *inode, struct file *file)
307 /* /dev/watchdog can only be opened once */
309 if (test_and_set_bit(0, &timer_enabled))
313 __module_get(THIS_MODULE);
315 /* Reload and activate timer */
318 printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to"
319 " %d minute(s).\n", timeout);
321 return nonseekable_open(inode, file);
326 * @inode: inode to board
327 * @file: file handle to board
329 * The watchdog has a configurable API. There is a religious dispute
330 * between people who want their watchdog to be able to shut down and
331 * those who want to be sure if the watchdog manager dies the machine
332 * reboots. In the former case we disable the counters, in the latter
333 * case you have to open it again very soon.
336 static int pc87413_release(struct inode *inode, struct file *file)
338 /* Shut off the timer. */
340 if (expect_close == 42) {
342 printk(KERN_INFO MODNAME "Watchdog disabled,"
343 " sleeping again...\n");
345 printk(KERN_CRIT MODNAME "Unexpected close, not stopping"
350 clear_bit(0, &timer_enabled);
359 * return, if the watchdog is enabled (timeout is set...)
363 static int pc87413_status(void)
365 return 1; /* currently not supported */
370 * @file: file handle to the watchdog
371 * @data: data buffer to write
372 * @len: length in bytes
373 * @ppos: pointer to the position to write. No seeks allowed
375 * A write to a watchdog device is defined as a keepalive signal. Any
376 * write of data will do, as we we don't define content meaning.
379 static ssize_t pc87413_write(struct file *file, const char __user *data,
380 size_t len, loff_t *ppos)
382 /* See if we got the magic character 'V' and reload the timer */
387 /* reset expect flag */
390 /* scan to see whether or not we got the magic character */
391 for (i = 0; i != len; i++) {
393 if (get_user(c, data+i))
400 /* someone wrote to us, we should reload the timer */
408 * @inode: inode of the device
409 * @file: file handle to the device
410 * @cmd: watchdog command
411 * @arg: argument pointer
413 * The watchdog API defines a common set of functions for all watchdogs
414 * according to their available features. We only actually usefully support
415 * querying capabilities and current status.
418 static int pc87413_ioctl(struct inode *inode, struct file *file,
419 unsigned int cmd, unsigned long arg)
424 struct watchdog_info __user *ident;
428 static struct watchdog_info ident = {
429 .options = WDIOF_KEEPALIVEPING |
432 .firmware_version = 1,
433 .identity = "PC87413(HF/F) watchdog"
436 uarg.i = (int __user *)arg;
442 case WDIOC_GETSUPPORT:
443 return copy_to_user(uarg.ident, &ident,
444 sizeof(ident)) ? -EFAULT : 0;
446 case WDIOC_GETSTATUS:
447 return put_user(pc87413_status(), uarg.i);
449 case WDIOC_GETBOOTSTATUS:
450 return put_user(0, uarg.i);
452 case WDIOC_KEEPALIVE:
455 printk(KERN_INFO DPFX "keepalive\n");
459 case WDIOC_SETTIMEOUT:
460 if (get_user(new_timeout, uarg.i))
463 // the API states this is given in secs
466 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
469 timeout = new_timeout;
472 // fall through and return the new timeout...
474 case WDIOC_GETTIMEOUT:
476 new_timeout = timeout * 60;
478 return put_user(new_timeout, uarg.i);
480 case WDIOC_SETOPTIONS:
482 int options, retval = -EINVAL;
484 if (get_user(options, uarg.i))
487 if (options & WDIOS_DISABLECARD) {
492 if (options & WDIOS_ENABLECARD) {
502 /* -- Notifier funtions -----------------------------------------*/
506 * @this: our notifier block
507 * @code: the event being reported
510 * Our notifier is called on system shutdowns. We want to turn the card
511 * off at reboot otherwise the machine will reboot again during memory
512 * test or worse yet during the following fsck. This would suck, in fact
513 * trust me - if it happens it does suck.
516 static int pc87413_notify_sys(struct notifier_block *this,
520 if (code == SYS_DOWN || code == SYS_HALT)
522 /* Turn the card off */
528 /* -- Module's structures ---------------------------------------*/
530 static struct file_operations pc87413_fops = {
531 .owner = THIS_MODULE,
533 .write = pc87413_write,
534 .ioctl = pc87413_ioctl,
535 .open = pc87413_open,
536 .release = pc87413_release,
539 static struct notifier_block pc87413_notifier =
541 .notifier_call = pc87413_notify_sys,
544 static struct miscdevice pc87413_miscdev=
546 .minor = WATCHDOG_MINOR,
548 .fops = &pc87413_fops
551 /* -- Module init functions -------------------------------------*/
554 * pc87413_init: module's "constructor"
556 * Set up the WDT watchdog board. All we have to do is grab the
557 * resources we require and bitch if anyone beat us to them.
558 * The open() function will actually kick the board off.
561 static int __init pc87413_init(void)
565 spin_lock_init(&io_lock);
567 printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n", WDT_INDEX_IO_PORT);
569 /* request_region(io, 2, "pc87413"); */
571 ret = register_reboot_notifier(&pc87413_notifier);
573 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
577 ret = misc_register(&pc87413_miscdev);
580 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
581 WATCHDOG_MINOR, ret);
582 unregister_reboot_notifier(&pc87413_notifier);
586 printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
594 * pc87413_exit: module's "destructor"
596 * Unload the watchdog. You cannot do this with any file handles open.
597 * If your watchdog is set to continue ticking on close and you unload
598 * it, well it keeps ticking. We won't get the interrupt but the board
599 * will not touch PC memory so all is fine. You just have to load a new
600 * module in 60 seconds or reboot.
603 static void __exit pc87413_exit(void)
605 /* Stop the timer before we leave */
609 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
612 misc_deregister(&pc87413_miscdev);
613 unregister_reboot_notifier(&pc87413_notifier);
614 /* release_region(io,2); */
616 printk(MODNAME " watchdog component driver removed.\n");
619 module_init(pc87413_init);
620 module_exit(pc87413_exit);
622 MODULE_AUTHOR("Sven Anders <anders@anduras.de>, Marcus Junker <junker@anduras.de>,");
623 MODULE_DESCRIPTION("PC87413 WDT driver");
624 MODULE_LICENSE("GPL");
626 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
628 module_param(io, int, 0);
629 MODULE_PARM_DESC(wdt_io, MODNAME " I/O port (default: " __MODULE_STRING(io) ").");
631 module_param(timeout, int, 0);
632 MODULE_PARM_DESC(timeout, "Watchdog timeout in minutes (default=" __MODULE_STRING(timeout) ").");
634 module_param(nowayout, int, 0);
635 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");