1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SBC EPX C3 0.1 A Hardware Watchdog Device for the Winsystems EPX-C3
4 * single board computer
6 * (c) Copyright 2006 Calin A. Culianu <calin@ajvar.org>, All Rights
9 * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
20 #include <linux/miscdevice.h>
21 #include <linux/watchdog.h>
22 #include <linux/notifier.h>
23 #include <linux/reboot.h>
24 #include <linux/init.h>
25 #include <linux/ioport.h>
26 #include <linux/uaccess.h>
29 static int epx_c3_alive
;
31 #define WATCHDOG_TIMEOUT 1 /* 1 sec default timeout */
33 static bool nowayout
= WATCHDOG_NOWAYOUT
;
34 module_param(nowayout
, bool, 0);
35 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
36 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
38 #define EPXC3_WATCHDOG_CTL_REG 0x1ee /* write 1 to enable, 0 to disable */
39 #define EPXC3_WATCHDOG_PET_REG 0x1ef /* write anything to pet once enabled */
41 static void epx_c3_start(void)
43 outb(1, EPXC3_WATCHDOG_CTL_REG
);
46 static void epx_c3_stop(void)
49 outb(0, EPXC3_WATCHDOG_CTL_REG
);
51 pr_info("Stopped watchdog timer\n");
54 static void epx_c3_pet(void)
56 outb(1, EPXC3_WATCHDOG_PET_REG
);
60 * Allow only one person to hold it open
62 static int epx_c3_open(struct inode
*inode
, struct file
*file
)
68 __module_get(THIS_MODULE
);
75 pr_info("Started watchdog timer\n");
77 return stream_open(inode
, file
);
80 static int epx_c3_release(struct inode
*inode
, struct file
*file
)
82 /* Shut off the timer.
83 * Lock it in if it's a module and we defined ...NOWAYOUT */
85 epx_c3_stop(); /* Turn the WDT off */
92 static ssize_t
epx_c3_write(struct file
*file
, const char __user
*data
,
93 size_t len
, loff_t
*ppos
)
95 /* Refresh the timer. */
101 static long epx_c3_ioctl(struct file
*file
, unsigned int cmd
,
104 int options
, retval
= -EINVAL
;
105 int __user
*argp
= (void __user
*)arg
;
106 static const struct watchdog_info ident
= {
107 .options
= WDIOF_KEEPALIVEPING
,
108 .firmware_version
= 0,
109 .identity
= "Winsystems EPX-C3 H/W Watchdog",
113 case WDIOC_GETSUPPORT
:
114 if (copy_to_user(argp
, &ident
, sizeof(ident
)))
117 case WDIOC_GETSTATUS
:
118 case WDIOC_GETBOOTSTATUS
:
119 return put_user(0, argp
);
120 case WDIOC_SETOPTIONS
:
121 if (get_user(options
, argp
))
124 if (options
& WDIOS_DISABLECARD
) {
129 if (options
& WDIOS_ENABLECARD
) {
135 case WDIOC_KEEPALIVE
:
138 case WDIOC_GETTIMEOUT
:
139 return put_user(WATCHDOG_TIMEOUT
, argp
);
145 static int epx_c3_notify_sys(struct notifier_block
*this, unsigned long code
,
148 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
149 epx_c3_stop(); /* Turn the WDT off */
154 static const struct file_operations epx_c3_fops
= {
155 .owner
= THIS_MODULE
,
157 .write
= epx_c3_write
,
158 .unlocked_ioctl
= epx_c3_ioctl
,
159 .compat_ioctl
= compat_ptr_ioctl
,
161 .release
= epx_c3_release
,
164 static struct miscdevice epx_c3_miscdev
= {
165 .minor
= WATCHDOG_MINOR
,
167 .fops
= &epx_c3_fops
,
170 static struct notifier_block epx_c3_notifier
= {
171 .notifier_call
= epx_c3_notify_sys
,
174 static int __init
watchdog_init(void)
178 if (!request_region(EPXC3_WATCHDOG_CTL_REG
, 2, "epxc3_watchdog"))
181 ret
= register_reboot_notifier(&epx_c3_notifier
);
183 pr_err("cannot register reboot notifier (err=%d)\n", ret
);
187 ret
= misc_register(&epx_c3_miscdev
);
189 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
190 WATCHDOG_MINOR
, ret
);
191 unregister_reboot_notifier(&epx_c3_notifier
);
195 pr_info("Hardware Watchdog Timer for Winsystems EPX-C3 SBC: 0.1\n");
200 release_region(EPXC3_WATCHDOG_CTL_REG
, 2);
204 static void __exit
watchdog_exit(void)
206 misc_deregister(&epx_c3_miscdev
);
207 unregister_reboot_notifier(&epx_c3_notifier
);
208 release_region(EPXC3_WATCHDOG_CTL_REG
, 2);
211 module_init(watchdog_init
);
212 module_exit(watchdog_exit
);
214 MODULE_AUTHOR("Calin A. Culianu <calin@ajvar.org>");
215 MODULE_DESCRIPTION("Hardware Watchdog Device for Winsystems EPX-C3 SBC. "
216 "Note that there is no way to probe for this device -- "
217 "so only use it if you are *sure* you are running on this specific "
218 "SBC system from Winsystems! It writes to IO ports 0x1ee and 0x1ef!");
219 MODULE_LICENSE("GPL");