blob: a6925847f76f8a3cfa7838ae81747bb8fd576303 [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ICP Wafer 5823 Single Board Computer WDT driver
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +00004 * http://www.icpamerica.com/wafer_5823.php
5 * May also work on other similar models
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * (c) Copyright 2002 Justin Cormack <justin@street-vision.com>
8 *
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +00009 * Release 0.02
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Based on advantechwdt.c which is based on wdt.c.
12 * Original copyright messages:
13 *
Alan Cox29fa0582008-10-27 15:17:56 +000014 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
15 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
18 * warranty for any of this software. This material is provided
19 * "AS-IS" and at no charge.
20 *
21 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
22 *
23 */
24
Joe Perches27c766a2012-02-15 15:06:19 -080025#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/miscdevice.h>
30#include <linux/watchdog.h>
31#include <linux/fs.h>
32#include <linux/ioport.h>
33#include <linux/notifier.h>
34#include <linux/reboot.h>
35#include <linux/init.h>
36#include <linux/spinlock.h>
Alan Cox694b16b2008-05-19 14:09:40 +010037#include <linux/io.h>
38#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define WATCHDOG_NAME "Wafer 5823 WDT"
41#define PFX WATCHDOG_NAME ": "
42#define WD_TIMO 60 /* 60 sec default timeout */
43
44static unsigned long wafwdt_is_open;
45static char expect_close;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070046static DEFINE_SPINLOCK(wafwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * You must set these - there is no sane way to probe for this board.
50 *
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000051 * To enable, write the timeout value in seconds (1 to 255) to I/O
52 * port WDT_START, then read the port to start the watchdog. To pat
53 * the dog, read port WDT_STOP to stop the timer, then read WDT_START
54 * to restart it again.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
57static int wdt_stop = 0x843;
58static int wdt_start = 0x443;
59
60static int timeout = WD_TIMO; /* in seconds */
61module_param(timeout, int, 0);
Alan Cox694b16b2008-05-19 14:09:40 +010062MODULE_PARM_DESC(timeout,
63 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
64 __MODULE_STRING(WD_TIMO) ".");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010066static bool nowayout = WATCHDOG_NOWAYOUT;
67module_param(nowayout, bool, 0);
Alan Cox694b16b2008-05-19 14:09:40 +010068MODULE_PARM_DESC(nowayout,
69 "Watchdog cannot be stopped once started (default="
70 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static void wafwdt_ping(void)
73{
74 /* pat watchdog */
75 spin_lock(&wafwdt_lock);
76 inb_p(wdt_stop);
77 inb_p(wdt_start);
78 spin_unlock(&wafwdt_lock);
79}
80
81static void wafwdt_start(void)
82{
83 /* start up watchdog */
84 outb_p(timeout, wdt_start);
85 inb_p(wdt_start);
86}
87
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000088static void wafwdt_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 /* stop watchdog */
91 inb_p(wdt_stop);
92}
93
Alan Cox694b16b2008-05-19 14:09:40 +010094static ssize_t wafwdt_write(struct file *file, const char __user *buf,
95 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 /* See if we got the magic character 'V' and reload the timer */
98 if (count) {
99 if (!nowayout) {
100 size_t i;
101
102 /* In case it was set long ago */
103 expect_close = 0;
104
Alan Cox694b16b2008-05-19 14:09:40 +0100105 /* scan to see whether or not we got the magic
106 character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 for (i = 0; i != count; i++) {
108 char c;
109 if (get_user(c, buf + i))
110 return -EFAULT;
111 if (c == 'V')
112 expect_close = 42;
113 }
114 }
Alan Cox694b16b2008-05-19 14:09:40 +0100115 /* Well, anyhow someone wrote to us, we should
116 return that favour */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 wafwdt_ping();
118 }
119 return count;
120}
121
Alan Cox694b16b2008-05-19 14:09:40 +0100122static long wafwdt_ioctl(struct file *file, unsigned int cmd,
123 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int new_timeout;
126 void __user *argp = (void __user *)arg;
127 int __user *p = argp;
Alan Cox694b16b2008-05-19 14:09:40 +0100128 static const struct watchdog_info ident = {
129 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
130 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .firmware_version = 1,
132 .identity = "Wafer 5823 WDT",
133 };
134
135 switch (cmd) {
136 case WDIOC_GETSUPPORT:
Alan Cox694b16b2008-05-19 14:09:40 +0100137 if (copy_to_user(argp, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -EFAULT;
139 break;
140
141 case WDIOC_GETSTATUS:
142 case WDIOC_GETBOOTSTATUS:
143 return put_user(0, p);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 case WDIOC_SETOPTIONS:
146 {
147 int options, retval = -EINVAL;
148
149 if (get_user(options, p))
150 return -EFAULT;
151
152 if (options & WDIOS_DISABLECARD) {
Axel Lin8a062ac2012-01-18 19:26:43 +0800153 wafwdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 retval = 0;
155 }
156
157 if (options & WDIOS_ENABLECARD) {
Axel Lin8a062ac2012-01-18 19:26:43 +0800158 wafwdt_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 retval = 0;
160 }
161
162 return retval;
163 }
164
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000165 case WDIOC_KEEPALIVE:
166 wafwdt_ping();
167 break;
168
169 case WDIOC_SETTIMEOUT:
170 if (get_user(new_timeout, p))
171 return -EFAULT;
172 if ((new_timeout < 1) || (new_timeout > 255))
173 return -EINVAL;
174 timeout = new_timeout;
175 wafwdt_stop();
176 wafwdt_start();
Gustavo A. R. Silvae30d69d2018-03-27 14:33:49 -0500177 /* Fall through */
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000178 case WDIOC_GETTIMEOUT:
179 return put_user(timeout, p);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200182 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 return 0;
185}
186
187static int wafwdt_open(struct inode *inode, struct file *file)
188{
189 if (test_and_set_bit(0, &wafwdt_is_open))
190 return -EBUSY;
191
192 /*
193 * Activate
194 */
195 wafwdt_start();
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300196 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000199static int wafwdt_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Alan Cox694b16b2008-05-19 14:09:40 +0100201 if (expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 wafwdt_stop();
Alan Cox694b16b2008-05-19 14:09:40 +0100203 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800204 pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 wafwdt_ping();
206 }
207 clear_bit(0, &wafwdt_is_open);
208 expect_close = 0;
209 return 0;
210}
211
212/*
213 * Notifier for system down
214 */
215
Alan Cox694b16b2008-05-19 14:09:40 +0100216static int wafwdt_notify_sys(struct notifier_block *this, unsigned long code,
217 void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Alan Cox694b16b2008-05-19 14:09:40 +0100219 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 wafwdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return NOTIFY_DONE;
222}
223
224/*
225 * Kernel Interfaces
226 */
227
Arjan van de Ven62322d22006-07-03 00:24:21 -0700228static const struct file_operations wafwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .owner = THIS_MODULE,
230 .llseek = no_llseek,
231 .write = wafwdt_write,
Alan Cox694b16b2008-05-19 14:09:40 +0100232 .unlocked_ioctl = wafwdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200233 .compat_ioctl = compat_ptr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .open = wafwdt_open,
235 .release = wafwdt_close,
236};
237
238static struct miscdevice wafwdt_miscdev = {
239 .minor = WATCHDOG_MINOR,
240 .name = "watchdog",
241 .fops = &wafwdt_fops,
242};
243
244/*
245 * The WDT needs to learn about soft shutdowns in order to
246 * turn the timebomb registers off.
247 */
248
249static struct notifier_block wafwdt_notifier = {
250 .notifier_call = wafwdt_notify_sys,
251};
252
253static int __init wafwdt_init(void)
254{
255 int ret;
256
Joe Perches27c766a2012-02-15 15:06:19 -0800257 pr_info("WDT driver for Wafer 5823 single board computer initialising\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (timeout < 1 || timeout > 255) {
260 timeout = WD_TIMO;
Joe Perches27c766a2012-02-15 15:06:19 -0800261 pr_info("timeout value must be 1 <= x <= 255, using %d\n",
262 timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 if (wdt_stop != wdt_start) {
Alan Cox694b16b2008-05-19 14:09:40 +0100266 if (!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800267 pr_err("I/O address 0x%04x already in use\n", wdt_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ret = -EIO;
269 goto error;
270 }
271 }
272
Alan Cox694b16b2008-05-19 14:09:40 +0100273 if (!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800274 pr_err("I/O address 0x%04x already in use\n", wdt_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ret = -EIO;
276 goto error2;
277 }
278
279 ret = register_reboot_notifier(&wafwdt_notifier);
280 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800281 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto error3;
283 }
284
285 ret = misc_register(&wafwdt_miscdev);
286 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800287 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
288 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto error4;
290 }
291
Joe Perches27c766a2012-02-15 15:06:19 -0800292 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 timeout, nowayout);
294
295 return ret;
296error4:
297 unregister_reboot_notifier(&wafwdt_notifier);
298error3:
299 release_region(wdt_start, 1);
300error2:
301 if (wdt_stop != wdt_start)
302 release_region(wdt_stop, 1);
303error:
304 return ret;
305}
306
307static void __exit wafwdt_exit(void)
308{
309 misc_deregister(&wafwdt_miscdev);
310 unregister_reboot_notifier(&wafwdt_notifier);
Alan Cox694b16b2008-05-19 14:09:40 +0100311 if (wdt_stop != wdt_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 release_region(wdt_stop, 1);
313 release_region(wdt_start, 1);
314}
315
316module_init(wafwdt_init);
317module_exit(wafwdt_exit);
318
319MODULE_AUTHOR("Justin Cormack");
320MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
321MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/* end of wafer5823wdt.c */