2 * NANO7240 SBC Watchdog device driver
4 * Based on w83877f.c by Scott Jennings,
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation;
10 * Software distributed under the License is distributed on an "AS IS"
11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or
12 * implied. See the License for the specific language governing
13 * rights and limitations under the License.
15 * (c) Copyright 2007 Gilles GIGAN <gilles.gigan@jcu.edu.au>
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/init.h>
23 #include <linux/ioport.h>
24 #include <linux/jiffies.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/miscdevice.h>
28 #include <linux/notifier.h>
29 #include <linux/reboot.h>
30 #include <linux/types.h>
31 #include <linux/watchdog.h>
33 #include <linux/uaccess.h>
34 #include <linux/atomic.h>
35 #include <asm/system.h>
37 #define SBC7240_ENABLE_PORT 0x443
38 #define SBC7240_DISABLE_PORT 0x043
39 #define SBC7240_SET_TIMEOUT_PORT SBC7240_ENABLE_PORT
40 #define SBC7240_MAGIC_CHAR 'V'
42 #define SBC7240_TIMEOUT 30
43 #define SBC7240_MAX_TIMEOUT 255
44 static int timeout = SBC7240_TIMEOUT; /* in seconds */
45 module_param(timeout, int, 0);
46 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<="
47 __MODULE_STRING(SBC7240_MAX_TIMEOUT) ", default="
48 __MODULE_STRING(SBC7240_TIMEOUT) ")");
50 static bool nowayout = WATCHDOG_NOWAYOUT;
51 module_param(nowayout, bool, 0);
52 MODULE_PARM_DESC(nowayout, "Disable watchdog when closing device file");
54 #define SBC7240_OPEN_STATUS_BIT 0
55 #define SBC7240_ENABLED_STATUS_BIT 1
56 #define SBC7240_EXPECT_CLOSE_STATUS_BIT 2
57 static unsigned long wdt_status;
63 static void wdt_disable(void)
65 /* disable the watchdog */
66 if (test_and_clear_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
67 inb_p(SBC7240_DISABLE_PORT);
68 pr_info("Watchdog timer is now disabled\n");
72 static void wdt_enable(void)
74 /* enable the watchdog */
75 if (!test_and_set_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
76 inb_p(SBC7240_ENABLE_PORT);
77 pr_info("Watchdog timer is now enabled\n");
81 static int wdt_set_timeout(int t)
83 if (t < 1 || t > SBC7240_MAX_TIMEOUT) {
84 pr_err("timeout value must be 1<=x<=%d\n", SBC7240_MAX_TIMEOUT);
88 outb_p((unsigned)t, SBC7240_SET_TIMEOUT_PORT);
90 pr_info("timeout set to %d seconds\n", t);
95 static inline void wdt_keepalive(void)
97 if (test_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status))
98 inb_p(SBC7240_ENABLE_PORT);
102 * /dev/watchdog handling
104 static ssize_t fop_write(struct file *file, const char __user *buf,
105 size_t count, loff_t *ppos)
112 clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
115 /* is there a magic char ? */
116 for (i = 0; i != count; i++) {
117 if (get_user(c, buf + i))
119 if (c == SBC7240_MAGIC_CHAR) {
120 set_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
133 static int fop_open(struct inode *inode, struct file *file)
135 if (test_and_set_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status))
140 return nonseekable_open(inode, file);
143 static int fop_close(struct inode *inode, struct file *file)
145 if (test_and_clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT, &wdt_status)
149 pr_crit("Unexpected close, not stopping watchdog!\n");
153 clear_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status);
157 static const struct watchdog_info ident = {
158 .options = WDIOF_KEEPALIVEPING|
161 .firmware_version = 1,
162 .identity = "SBC7240",
166 static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
169 case WDIOC_GETSUPPORT:
170 return copy_to_user((void __user *)arg, &ident, sizeof(ident))
172 case WDIOC_GETSTATUS:
173 case WDIOC_GETBOOTSTATUS:
174 return put_user(0, (int __user *)arg);
175 case WDIOC_SETOPTIONS:
178 int retval = -EINVAL;
180 if (get_user(options, (int __user *)arg))
183 if (options & WDIOS_DISABLECARD) {
188 if (options & WDIOS_ENABLECARD) {
195 case WDIOC_KEEPALIVE:
198 case WDIOC_SETTIMEOUT:
202 if (get_user(new_timeout, (int __user *)arg))
205 if (wdt_set_timeout(new_timeout))
210 case WDIOC_GETTIMEOUT:
211 return put_user(timeout, (int __user *)arg);
217 static const struct file_operations wdt_fops = {
218 .owner = THIS_MODULE,
222 .release = fop_close,
223 .unlocked_ioctl = fop_ioctl,
226 static struct miscdevice wdt_miscdev = {
227 .minor = WATCHDOG_MINOR,
233 * Notifier for system down
236 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
239 if (code == SYS_DOWN || code == SYS_HALT)
244 static struct notifier_block wdt_notifier = {
245 .notifier_call = wdt_notify_sys,
248 static void __exit sbc7240_wdt_unload(void)
250 pr_info("Removing watchdog\n");
251 misc_deregister(&wdt_miscdev);
253 unregister_reboot_notifier(&wdt_notifier);
254 release_region(SBC7240_ENABLE_PORT, 1);
257 static int __init sbc7240_wdt_init(void)
261 if (!request_region(SBC7240_ENABLE_PORT, 1, "SBC7240 WDT")) {
262 pr_err("I/O address 0x%04x already in use\n",
263 SBC7240_ENABLE_PORT);
268 /* The IO port 0x043 used to disable the watchdog
269 * is already claimed by the system timer, so we
270 * can't request_region() it ...*/
272 if (timeout < 1 || timeout > SBC7240_MAX_TIMEOUT) {
273 timeout = SBC7240_TIMEOUT;
274 pr_info("timeout value must be 1<=x<=%d, using %d\n",
275 SBC7240_MAX_TIMEOUT, timeout);
277 wdt_set_timeout(timeout);
280 rc = register_reboot_notifier(&wdt_notifier);
282 pr_err("cannot register reboot notifier (err=%d)\n", rc);
286 rc = misc_register(&wdt_miscdev);
288 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
289 wdt_miscdev.minor, rc);
290 goto err_out_reboot_notifier;
293 pr_info("Watchdog driver for SBC7240 initialised (nowayout=%d)\n",
298 err_out_reboot_notifier:
299 unregister_reboot_notifier(&wdt_notifier);
301 release_region(SBC7240_ENABLE_PORT, 1);
306 module_init(sbc7240_wdt_init);
307 module_exit(sbc7240_wdt_unload);
309 MODULE_AUTHOR("Gilles Gigan");
310 MODULE_DESCRIPTION("Watchdog device driver for single board"
311 " computers EPIC Nano 7240 from iEi");
312 MODULE_LICENSE("GPL");
313 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);