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/module.h>
22 #include <linux/types.h>
23 #include <linux/miscdevice.h>
24 #include <linux/watchdog.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/notifier.h>
29 #include <linux/reboot.h>
30 #include <linux/init.h>
31 #include <linux/spinlock.h>
32 #include <linux/moduleparam.h>
33 #include <linux/version.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
41 #define DEFAULT_TIMEOUT 1 /* 1 minute */
42 #define MAX_TIMEOUT 255
45 #define MODNAME "pc87413 WDT"
46 #define PFX MODNAME ": "
47 #define DPFX MODNAME " - DEBUG: "
49 #define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
50 #define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
52 #define SIOCFG2 0x22 /* Serial IO register */
53 #define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
54 #define WDTO 0x11 /* Watchdog timeout register */
55 #define WDCFG 0x12 /* Watchdog config register */
57 static int io
= 0x2E; /* Address used on Portwell Boards */
59 static int timeout
= DEFAULT_TIMEOUT
; /* timeout value */
60 static unsigned long timer_enabled
= 0; /* is the timer enabled? */
62 static char expect_close
; /* is the close expected? */
64 static DEFINE_SPINLOCK(io_lock
);/* to guard the watchdog from io races */
66 static int nowayout
= WATCHDOG_NOWAYOUT
;
68 /* -- Low level function ----------------------------------------*/
70 /* Select pins for Watchdog output */
72 static inline void pc87413_select_wdt_out (void)
74 unsigned int cr_data
= 0;
76 /* Step 1: Select multiple pin,pin55,as WDT output */
78 outb_p(SIOCFG2
, WDT_INDEX_IO_PORT
);
80 cr_data
= inb (WDT_DATA_IO_PORT
);
82 cr_data
|= 0x80; /* Set Bit7 to 1*/
83 outb_p(SIOCFG2
, WDT_INDEX_IO_PORT
);
85 outb_p(cr_data
, WDT_DATA_IO_PORT
);
88 printk(KERN_INFO DPFX
"Select multiple pin,pin55,as WDT output:"
89 " Bit7 to 1: %d\n", cr_data
);
93 /* Enable SWC functions */
95 static inline void pc87413_enable_swc(void)
97 unsigned int cr_data
=0;
99 /* Step 2: Enable SWC functions */
101 outb_p(0x07, WDT_INDEX_IO_PORT
); /* Point SWC_LDN (LDN=4) */
102 outb_p(SWC_LDN
, WDT_DATA_IO_PORT
);
104 outb_p(0x30, WDT_INDEX_IO_PORT
); /* Read Index 0x30 First */
105 cr_data
= inb(WDT_DATA_IO_PORT
);
106 cr_data
|= 0x01; /* Set Bit0 to 1 */
107 outb_p(0x30, WDT_INDEX_IO_PORT
);
108 outb_p(cr_data
, WDT_DATA_IO_PORT
); /* Index0x30_bit0P1 */
111 printk(KERN_INFO DPFX
"pc87413 - Enable SWC functions\n");
115 /* Read SWC I/O base address */
117 static inline unsigned int pc87413_get_swc_base(void)
119 unsigned int swc_base_addr
= 0;
120 unsigned char addr_l
, addr_h
= 0;
122 /* Step 3: Read SWC I/O Base Address */
124 outb_p(0x60, WDT_INDEX_IO_PORT
); /* Read Index 0x60 */
125 addr_h
= inb(WDT_DATA_IO_PORT
);
127 outb_p(0x61, WDT_INDEX_IO_PORT
); /* Read Index 0x61 */
129 addr_l
= inb(WDT_DATA_IO_PORT
);
131 swc_base_addr
= (addr_h
<< 8) + addr_l
;
134 printk(KERN_INFO DPFX
"Read SWC I/O Base Address: low %d, high %d,"
135 " res %d\n", addr_l
, addr_h
, swc_base_addr
);
138 return swc_base_addr
;
141 /* Select Bank 3 of SWC */
143 static inline void pc87413_swc_bank3(unsigned int swc_base_addr
)
145 /* Step 4: Select Bank3 of SWC */
147 outb_p(inb(swc_base_addr
+ 0x0f) | 0x03, swc_base_addr
+ 0x0f);
150 printk(KERN_INFO DPFX
"Select Bank3 of SWC\n");
154 /* Set watchdog timeout to x minutes */
156 static inline void pc87413_programm_wdto(unsigned int swc_base_addr
,
159 /* Step 5: Programm WDTO, Twd. */
161 outb_p(pc87413_time
, swc_base_addr
+ WDTO
);
164 printk(KERN_INFO DPFX
"Set WDTO to %d minutes\n", pc87413_time
);
170 static inline void pc87413_enable_wden(unsigned int swc_base_addr
)
172 /* Step 6: Enable WDEN */
174 outb_p(inb (swc_base_addr
+ WDCTL
) | 0x01, swc_base_addr
+ WDCTL
);
177 printk(KERN_INFO DPFX
"Enable WDEN\n");
181 /* Enable SW_WD_TREN */
182 static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr
)
184 /* Enable SW_WD_TREN */
186 outb_p(inb (swc_base_addr
+ WDCFG
) | 0x80, swc_base_addr
+ WDCFG
);
189 printk(KERN_INFO DPFX
"Enable SW_WD_TREN\n");
193 /* Disable SW_WD_TREN */
195 static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr
)
197 /* Disable SW_WD_TREN */
199 outb_p(inb (swc_base_addr
+ WDCFG
) & 0x7f, swc_base_addr
+ WDCFG
);
202 printk(KERN_INFO DPFX
"pc87413 - Disable SW_WD_TREN\n");
206 /* Enable SW_WD_TRG */
208 static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr
)
210 /* Enable SW_WD_TRG */
212 outb_p(inb (swc_base_addr
+ WDCTL
) | 0x80, swc_base_addr
+ WDCTL
);
215 printk(KERN_INFO DPFX
"pc87413 - Enable SW_WD_TRG\n");
219 /* Disable SW_WD_TRG */
221 static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr
)
223 /* Disable SW_WD_TRG */
225 outb_p(inb (swc_base_addr
+ WDCTL
) & 0x7f, swc_base_addr
+ WDCTL
);
228 printk(KERN_INFO DPFX
"Disable SW_WD_TRG\n");
232 /* -- Higher level functions ------------------------------------*/
234 /* Enable the watchdog */
236 static void pc87413_enable(void)
238 unsigned int swc_base_addr
;
242 pc87413_select_wdt_out();
243 pc87413_enable_swc();
244 swc_base_addr
= pc87413_get_swc_base();
245 pc87413_swc_bank3(swc_base_addr
);
246 pc87413_programm_wdto(swc_base_addr
, timeout
);
247 pc87413_enable_wden(swc_base_addr
);
248 pc87413_enable_sw_wd_tren(swc_base_addr
);
249 pc87413_enable_sw_wd_trg(swc_base_addr
);
251 spin_unlock(&io_lock
);
254 /* Disable the watchdog */
256 static void pc87413_disable(void)
258 unsigned int swc_base_addr
;
262 pc87413_select_wdt_out();
263 pc87413_enable_swc();
264 swc_base_addr
= pc87413_get_swc_base();
265 pc87413_swc_bank3(swc_base_addr
);
266 pc87413_disable_sw_wd_tren(swc_base_addr
);
267 pc87413_disable_sw_wd_trg(swc_base_addr
);
268 pc87413_programm_wdto(swc_base_addr
, 0);
270 spin_unlock(&io_lock
);
273 /* Refresh the watchdog */
275 static void pc87413_refresh(void)
277 unsigned int swc_base_addr
;
281 pc87413_select_wdt_out();
282 pc87413_enable_swc();
283 swc_base_addr
= pc87413_get_swc_base();
284 pc87413_swc_bank3(swc_base_addr
);
285 pc87413_disable_sw_wd_tren(swc_base_addr
);
286 pc87413_disable_sw_wd_trg(swc_base_addr
);
287 pc87413_programm_wdto(swc_base_addr
, timeout
);
288 pc87413_enable_wden(swc_base_addr
);
289 pc87413_enable_sw_wd_tren(swc_base_addr
);
290 pc87413_enable_sw_wd_trg(swc_base_addr
);
292 spin_unlock(&io_lock
);
295 /* -- File operations -------------------------------------------*/
299 * @inode: inode of device
300 * @file: file handle to device
304 static int pc87413_open(struct inode
*inode
, struct file
*file
)
306 /* /dev/watchdog can only be opened once */
308 if (test_and_set_bit(0, &timer_enabled
))
312 __module_get(THIS_MODULE
);
314 /* Reload and activate timer */
317 printk(KERN_INFO MODNAME
"Watchdog enabled. Timeout set to"
318 " %d minute(s).\n", timeout
);
320 return nonseekable_open(inode
, file
);
325 * @inode: inode to board
326 * @file: file handle to board
328 * The watchdog has a configurable API. There is a religious dispute
329 * between people who want their watchdog to be able to shut down and
330 * those who want to be sure if the watchdog manager dies the machine
331 * reboots. In the former case we disable the counters, in the latter
332 * case you have to open it again very soon.
335 static int pc87413_release(struct inode
*inode
, struct file
*file
)
337 /* Shut off the timer. */
339 if (expect_close
== 42) {
341 printk(KERN_INFO MODNAME
"Watchdog disabled,"
342 " sleeping again...\n");
344 printk(KERN_CRIT MODNAME
"Unexpected close, not stopping"
349 clear_bit(0, &timer_enabled
);
358 * return, if the watchdog is enabled (timeout is set...)
362 static int pc87413_status(void)
364 return 0; /* currently not supported */
369 * @file: file handle to the watchdog
370 * @data: data buffer to write
371 * @len: length in bytes
372 * @ppos: pointer to the position to write. No seeks allowed
374 * A write to a watchdog device is defined as a keepalive signal. Any
375 * write of data will do, as we we don't define content meaning.
378 static ssize_t
pc87413_write(struct file
*file
, const char __user
*data
,
379 size_t len
, loff_t
*ppos
)
381 /* See if we got the magic character 'V' and reload the timer */
386 /* reset expect flag */
389 /* scan to see whether or not we got the magic character */
390 for (i
= 0; i
!= len
; i
++) {
392 if (get_user(c
, data
+i
))
399 /* someone wrote to us, we should reload the timer */
407 * @inode: inode of the device
408 * @file: file handle to the device
409 * @cmd: watchdog command
410 * @arg: argument pointer
412 * The watchdog API defines a common set of functions for all watchdogs
413 * according to their available features. We only actually usefully support
414 * querying capabilities and current status.
417 static int pc87413_ioctl(struct inode
*inode
, struct file
*file
,
418 unsigned int cmd
, unsigned long arg
)
423 struct watchdog_info __user
*ident
;
427 static struct watchdog_info ident
= {
428 .options
= WDIOF_KEEPALIVEPING
|
431 .firmware_version
= 1,
432 .identity
= "PC87413(HF/F) watchdog"
435 uarg
.i
= (int __user
*)arg
;
441 case WDIOC_GETSUPPORT
:
442 return copy_to_user(uarg
.ident
, &ident
,
443 sizeof(ident
)) ? -EFAULT
: 0;
445 case WDIOC_GETSTATUS
:
446 return put_user(pc87413_status(), uarg
.i
);
448 case WDIOC_GETBOOTSTATUS
:
449 return put_user(0, uarg
.i
);
451 case WDIOC_KEEPALIVE
:
454 printk(KERN_INFO DPFX
"keepalive\n");
458 case WDIOC_SETTIMEOUT
:
459 if (get_user(new_timeout
, uarg
.i
))
462 // the API states this is given in secs
465 if (new_timeout
< 0 || new_timeout
> MAX_TIMEOUT
)
468 timeout
= new_timeout
;
471 // fall through and return the new timeout...
473 case WDIOC_GETTIMEOUT
:
475 new_timeout
= timeout
* 60;
477 return put_user(new_timeout
, uarg
.i
);
479 case WDIOC_SETOPTIONS
:
481 int options
, retval
= -EINVAL
;
483 if (get_user(options
, uarg
.i
))
486 if (options
& WDIOS_DISABLECARD
) {
491 if (options
& WDIOS_ENABLECARD
) {
501 /* -- Notifier funtions -----------------------------------------*/
505 * @this: our notifier block
506 * @code: the event being reported
509 * Our notifier is called on system shutdowns. We want to turn the card
510 * off at reboot otherwise the machine will reboot again during memory
511 * test or worse yet during the following fsck. This would suck, in fact
512 * trust me - if it happens it does suck.
515 static int pc87413_notify_sys(struct notifier_block
*this,
519 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
521 /* Turn the card off */
527 /* -- Module's structures ---------------------------------------*/
529 static const struct file_operations pc87413_fops
= {
530 .owner
= THIS_MODULE
,
532 .write
= pc87413_write
,
533 .ioctl
= pc87413_ioctl
,
534 .open
= pc87413_open
,
535 .release
= pc87413_release
,
538 static struct notifier_block pc87413_notifier
=
540 .notifier_call
= pc87413_notify_sys
,
543 static struct miscdevice pc87413_miscdev
=
545 .minor
= WATCHDOG_MINOR
,
547 .fops
= &pc87413_fops
550 /* -- Module init functions -------------------------------------*/
553 * pc87413_init: module's "constructor"
555 * Set up the WDT watchdog board. All we have to do is grab the
556 * resources we require and bitch if anyone beat us to them.
557 * The open() function will actually kick the board off.
560 static int __init
pc87413_init(void)
564 printk(KERN_INFO PFX
"Version " VERSION
" at io 0x%X\n", WDT_INDEX_IO_PORT
);
566 /* request_region(io, 2, "pc87413"); */
568 ret
= register_reboot_notifier(&pc87413_notifier
);
570 printk(KERN_ERR PFX
"cannot register reboot notifier (err=%d)\n",
574 ret
= misc_register(&pc87413_miscdev
);
577 printk(KERN_ERR PFX
"cannot register miscdev on minor=%d (err=%d)\n",
578 WATCHDOG_MINOR
, ret
);
579 unregister_reboot_notifier(&pc87413_notifier
);
583 printk(KERN_INFO PFX
"initialized. timeout=%d min \n", timeout
);
591 * pc87413_exit: module's "destructor"
593 * Unload the watchdog. You cannot do this with any file handles open.
594 * If your watchdog is set to continue ticking on close and you unload
595 * it, well it keeps ticking. We won't get the interrupt but the board
596 * will not touch PC memory so all is fine. You just have to load a new
597 * module in 60 seconds or reboot.
600 static void __exit
pc87413_exit(void)
602 /* Stop the timer before we leave */
606 printk(KERN_INFO MODNAME
"Watchdog disabled.\n");
609 misc_deregister(&pc87413_miscdev
);
610 unregister_reboot_notifier(&pc87413_notifier
);
611 /* release_region(io,2); */
613 printk(MODNAME
" watchdog component driver removed.\n");
616 module_init(pc87413_init
);
617 module_exit(pc87413_exit
);
619 MODULE_AUTHOR("Sven Anders <anders@anduras.de>, Marcus Junker <junker@anduras.de>,");
620 MODULE_DESCRIPTION("PC87413 WDT driver");
621 MODULE_LICENSE("GPL");
623 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
625 module_param(io
, int, 0);
626 MODULE_PARM_DESC(io
, MODNAME
" I/O port (default: " __MODULE_STRING(io
) ").");
628 module_param(timeout
, int, 0);
629 MODULE_PARM_DESC(timeout
, "Watchdog timeout in minutes (default=" __MODULE_STRING(timeout
) ").");
631 module_param(nowayout
, int, 0);
632 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");