2 * ICP Wafer 5823 Single Board Computer WDT driver
3 * http://www.icpamerica.com/wafer_5823.php
4 * May also work on other similar models
6 * (c) Copyright 2002 Justin Cormack <justin@street-vision.com>
10 * Based on advantechwdt.c which is based on wdt.c.
11 * Original copyright messages:
13 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
14 * All Rights Reserved.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
21 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
22 * warranty for any of this software. This material is provided
23 * "AS-IS" and at no charge.
25 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/miscdevice.h>
34 #include <linux/watchdog.h>
36 #include <linux/ioport.h>
37 #include <linux/notifier.h>
38 #include <linux/reboot.h>
39 #include <linux/init.h>
40 #include <linux/spinlock.h>
42 #include <linux/uaccess.h>
44 #define WATCHDOG_NAME "Wafer 5823 WDT"
45 #define PFX WATCHDOG_NAME ": "
46 #define WD_TIMO 60 /* 60 sec default timeout */
48 static unsigned long wafwdt_is_open
;
49 static char expect_close
;
50 static DEFINE_SPINLOCK(wafwdt_lock
);
53 * You must set these - there is no sane way to probe for this board.
55 * To enable, write the timeout value in seconds (1 to 255) to I/O
56 * port WDT_START, then read the port to start the watchdog. To pat
57 * the dog, read port WDT_STOP to stop the timer, then read WDT_START
58 * to restart it again.
61 static int wdt_stop
= 0x843;
62 static int wdt_start
= 0x443;
64 static int timeout
= WD_TIMO
; /* in seconds */
65 module_param(timeout
, int, 0);
66 MODULE_PARM_DESC(timeout
,
67 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
68 __MODULE_STRING(WD_TIMO
) ".");
70 static bool nowayout
= WATCHDOG_NOWAYOUT
;
71 module_param(nowayout
, bool, 0);
72 MODULE_PARM_DESC(nowayout
,
73 "Watchdog cannot be stopped once started (default="
74 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
76 static void wafwdt_ping(void)
79 spin_lock(&wafwdt_lock
);
82 spin_unlock(&wafwdt_lock
);
85 static void wafwdt_start(void)
87 /* start up watchdog */
88 outb_p(timeout
, wdt_start
);
92 static void wafwdt_stop(void)
98 static ssize_t
wafwdt_write(struct file
*file
, const char __user
*buf
,
99 size_t count
, loff_t
*ppos
)
101 /* See if we got the magic character 'V' and reload the timer */
106 /* In case it was set long ago */
109 /* scan to see whether or not we got the magic
111 for (i
= 0; i
!= count
; i
++) {
113 if (get_user(c
, buf
+ i
))
119 /* Well, anyhow someone wrote to us, we should
120 return that favour */
126 static long wafwdt_ioctl(struct file
*file
, unsigned int cmd
,
130 void __user
*argp
= (void __user
*)arg
;
131 int __user
*p
= argp
;
132 static const struct watchdog_info ident
= {
133 .options
= WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
|
135 .firmware_version
= 1,
136 .identity
= "Wafer 5823 WDT",
140 case WDIOC_GETSUPPORT
:
141 if (copy_to_user(argp
, &ident
, sizeof(ident
)))
145 case WDIOC_GETSTATUS
:
146 case WDIOC_GETBOOTSTATUS
:
147 return put_user(0, p
);
149 case WDIOC_SETOPTIONS
:
151 int options
, retval
= -EINVAL
;
153 if (get_user(options
, p
))
156 if (options
& WDIOS_DISABLECARD
) {
161 if (options
& WDIOS_ENABLECARD
) {
169 case WDIOC_KEEPALIVE
:
173 case WDIOC_SETTIMEOUT
:
174 if (get_user(new_timeout
, p
))
176 if ((new_timeout
< 1) || (new_timeout
> 255))
178 timeout
= new_timeout
;
182 case WDIOC_GETTIMEOUT
:
183 return put_user(timeout
, p
);
191 static int wafwdt_open(struct inode
*inode
, struct file
*file
)
193 if (test_and_set_bit(0, &wafwdt_is_open
))
200 return nonseekable_open(inode
, file
);
203 static int wafwdt_close(struct inode
*inode
, struct file
*file
)
205 if (expect_close
== 42)
208 pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
211 clear_bit(0, &wafwdt_is_open
);
217 * Notifier for system down
220 static int wafwdt_notify_sys(struct notifier_block
*this, unsigned long code
,
223 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
232 static const struct file_operations wafwdt_fops
= {
233 .owner
= THIS_MODULE
,
235 .write
= wafwdt_write
,
236 .unlocked_ioctl
= wafwdt_ioctl
,
238 .release
= wafwdt_close
,
241 static struct miscdevice wafwdt_miscdev
= {
242 .minor
= WATCHDOG_MINOR
,
244 .fops
= &wafwdt_fops
,
248 * The WDT needs to learn about soft shutdowns in order to
249 * turn the timebomb registers off.
252 static struct notifier_block wafwdt_notifier
= {
253 .notifier_call
= wafwdt_notify_sys
,
256 static int __init
wafwdt_init(void)
260 pr_info("WDT driver for Wafer 5823 single board computer initialising\n");
262 if (timeout
< 1 || timeout
> 255) {
264 pr_info("timeout value must be 1 <= x <= 255, using %d\n",
268 if (wdt_stop
!= wdt_start
) {
269 if (!request_region(wdt_stop
, 1, "Wafer 5823 WDT")) {
270 pr_err("I/O address 0x%04x already in use\n", wdt_stop
);
276 if (!request_region(wdt_start
, 1, "Wafer 5823 WDT")) {
277 pr_err("I/O address 0x%04x already in use\n", wdt_start
);
282 ret
= register_reboot_notifier(&wafwdt_notifier
);
284 pr_err("cannot register reboot notifier (err=%d)\n", ret
);
288 ret
= misc_register(&wafwdt_miscdev
);
290 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
291 WATCHDOG_MINOR
, ret
);
295 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
300 unregister_reboot_notifier(&wafwdt_notifier
);
302 release_region(wdt_start
, 1);
304 if (wdt_stop
!= wdt_start
)
305 release_region(wdt_stop
, 1);
310 static void __exit
wafwdt_exit(void)
312 misc_deregister(&wafwdt_miscdev
);
313 unregister_reboot_notifier(&wafwdt_notifier
);
314 if (wdt_stop
!= wdt_start
)
315 release_region(wdt_stop
, 1);
316 release_region(wdt_start
, 1);
319 module_init(wafwdt_init
);
320 module_exit(wafwdt_exit
);
322 MODULE_AUTHOR("Justin Cormack");
323 MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
324 MODULE_LICENSE("GPL");
325 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
327 /* end of wafer5823wdt.c */