2 * SBC EPX C3 0.1 A Hardware Watchdog Device for the Winsystems EPX-C3
3 * single board computer
5 * (c) Copyright 2006 Calin A. Culianu <calin@ajvar.org>, All Rights
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/notifier.h>
27 #include <linux/reboot.h>
28 #include <linux/init.h>
29 #include <linux/ioport.h>
30 #include <linux/uaccess.h>
33 static int epx_c3_alive
;
35 #define WATCHDOG_TIMEOUT 1 /* 1 sec default timeout */
37 static bool nowayout
= WATCHDOG_NOWAYOUT
;
38 module_param(nowayout
, bool, 0);
39 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
42 #define EPXC3_WATCHDOG_CTL_REG 0x1ee /* write 1 to enable, 0 to disable */
43 #define EPXC3_WATCHDOG_PET_REG 0x1ef /* write anything to pet once enabled */
45 static void epx_c3_start(void)
47 outb(1, EPXC3_WATCHDOG_CTL_REG
);
50 static void epx_c3_stop(void)
53 outb(0, EPXC3_WATCHDOG_CTL_REG
);
55 pr_info("Stopped watchdog timer\n");
58 static void epx_c3_pet(void)
60 outb(1, EPXC3_WATCHDOG_PET_REG
);
64 * Allow only one person to hold it open
66 static int epx_c3_open(struct inode
*inode
, struct file
*file
)
72 __module_get(THIS_MODULE
);
79 pr_info("Started watchdog timer\n");
81 return nonseekable_open(inode
, file
);
84 static int epx_c3_release(struct inode
*inode
, struct file
*file
)
86 /* Shut off the timer.
87 * Lock it in if it's a module and we defined ...NOWAYOUT */
89 epx_c3_stop(); /* Turn the WDT off */
96 static ssize_t
epx_c3_write(struct file
*file
, const char __user
*data
,
97 size_t len
, loff_t
*ppos
)
99 /* Refresh the timer. */
105 static long epx_c3_ioctl(struct file
*file
, unsigned int cmd
,
108 int options
, retval
= -EINVAL
;
109 int __user
*argp
= (void __user
*)arg
;
110 static const struct watchdog_info ident
= {
111 .options
= WDIOF_KEEPALIVEPING
,
112 .firmware_version
= 0,
113 .identity
= "Winsystems EPX-C3 H/W Watchdog",
117 case WDIOC_GETSUPPORT
:
118 if (copy_to_user(argp
, &ident
, sizeof(ident
)))
121 case WDIOC_GETSTATUS
:
122 case WDIOC_GETBOOTSTATUS
:
123 return put_user(0, argp
);
124 case WDIOC_SETOPTIONS
:
125 if (get_user(options
, argp
))
128 if (options
& WDIOS_DISABLECARD
) {
133 if (options
& WDIOS_ENABLECARD
) {
139 case WDIOC_KEEPALIVE
:
142 case WDIOC_GETTIMEOUT
:
143 return put_user(WATCHDOG_TIMEOUT
, argp
);
149 static int epx_c3_notify_sys(struct notifier_block
*this, unsigned long code
,
152 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
153 epx_c3_stop(); /* Turn the WDT off */
158 static const struct file_operations epx_c3_fops
= {
159 .owner
= THIS_MODULE
,
161 .write
= epx_c3_write
,
162 .unlocked_ioctl
= epx_c3_ioctl
,
164 .release
= epx_c3_release
,
167 static struct miscdevice epx_c3_miscdev
= {
168 .minor
= WATCHDOG_MINOR
,
170 .fops
= &epx_c3_fops
,
173 static struct notifier_block epx_c3_notifier
= {
174 .notifier_call
= epx_c3_notify_sys
,
177 static int __init
watchdog_init(void)
181 if (!request_region(EPXC3_WATCHDOG_CTL_REG
, 2, "epxc3_watchdog"))
184 ret
= register_reboot_notifier(&epx_c3_notifier
);
186 pr_err("cannot register reboot notifier (err=%d)\n", ret
);
190 ret
= misc_register(&epx_c3_miscdev
);
192 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
193 WATCHDOG_MINOR
, ret
);
194 unregister_reboot_notifier(&epx_c3_notifier
);
198 pr_info("Hardware Watchdog Timer for Winsystems EPX-C3 SBC: 0.1\n");
203 release_region(EPXC3_WATCHDOG_CTL_REG
, 2);
207 static void __exit
watchdog_exit(void)
209 misc_deregister(&epx_c3_miscdev
);
210 unregister_reboot_notifier(&epx_c3_notifier
);
211 release_region(EPXC3_WATCHDOG_CTL_REG
, 2);
214 module_init(watchdog_init
);
215 module_exit(watchdog_exit
);
217 MODULE_AUTHOR("Calin A. Culianu <calin@ajvar.org>");
218 MODULE_DESCRIPTION("Hardware Watchdog Device for Winsystems EPX-C3 SBC. "
219 "Note that there is no way to probe for this device -- "
220 "so only use it if you are *sure* you are running on this specific "
221 "SBC system from Winsystems! It writes to IO ports 0x1ee and 0x1ef!");
222 MODULE_LICENSE("GPL");
223 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);