xref: /linux/drivers/watchdog/sbc_fitpc2_wdt.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Watchdog driver for SBC-FITPC2 board
4  *
5  * Author: Denis Turischev <denis@compulab.co.il>
6  *
7  * Adapted from the IXP2000 watchdog driver by Deepak Saxena.
8  *
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME " WATCHDOG: " fmt
12 
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/miscdevice.h>
16 #include <linux/watchdog.h>
17 #include <linux/ioport.h>
18 #include <linux/delay.h>
19 #include <linux/fs.h>
20 #include <linux/init.h>
21 #include <linux/moduleparam.h>
22 #include <linux/dmi.h>
23 #include <linux/io.h>
24 #include <linux/uaccess.h>
25 
26 
27 static bool nowayout = WATCHDOG_NOWAYOUT;
28 static unsigned int margin = 60;	/* (secs) Default is 1 minute */
29 static unsigned long wdt_status;
30 static DEFINE_MUTEX(wdt_lock);
31 
32 #define WDT_IN_USE		0
33 #define WDT_OK_TO_CLOSE		1
34 
35 #define COMMAND_PORT		0x4c
36 #define DATA_PORT		0x48
37 
38 #define IFACE_ON_COMMAND	1
39 #define REBOOT_COMMAND		2
40 
41 #define WATCHDOG_NAME		"SBC-FITPC2 Watchdog"
42 
43 static void wdt_send_data(unsigned char command, unsigned char data)
44 {
45 	outb(data, DATA_PORT);
46 	msleep(200);
47 	outb(command, COMMAND_PORT);
48 	msleep(100);
49 }
50 
51 static void wdt_enable(void)
52 {
53 	mutex_lock(&wdt_lock);
54 	wdt_send_data(IFACE_ON_COMMAND, 1);
55 	wdt_send_data(REBOOT_COMMAND, margin);
56 	mutex_unlock(&wdt_lock);
57 }
58 
59 static void wdt_disable(void)
60 {
61 	mutex_lock(&wdt_lock);
62 	wdt_send_data(IFACE_ON_COMMAND, 0);
63 	wdt_send_data(REBOOT_COMMAND, 0);
64 	mutex_unlock(&wdt_lock);
65 }
66 
67 static int fitpc2_wdt_open(struct inode *inode, struct file *file)
68 {
69 	if (test_and_set_bit(WDT_IN_USE, &wdt_status))
70 		return -EBUSY;
71 
72 	clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
73 
74 	wdt_enable();
75 
76 	return stream_open(inode, file);
77 }
78 
79 static ssize_t fitpc2_wdt_write(struct file *file, const char __user *data,
80 						size_t len, loff_t *ppos)
81 {
82 	size_t i;
83 
84 	if (!len)
85 		return 0;
86 
87 	if (nowayout) {
88 		len = 0;
89 		goto out;
90 	}
91 
92 	clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
93 
94 	for (i = 0; i != len; i++) {
95 		char c;
96 
97 		if (get_user(c, data + i))
98 			return -EFAULT;
99 
100 		if (c == 'V')
101 			set_bit(WDT_OK_TO_CLOSE, &wdt_status);
102 	}
103 
104 out:
105 	wdt_enable();
106 
107 	return len;
108 }
109 
110 
111 static const struct watchdog_info ident = {
112 	.options	= WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
113 				WDIOF_KEEPALIVEPING,
114 	.identity	= WATCHDOG_NAME,
115 };
116 
117 
118 static long fitpc2_wdt_ioctl(struct file *file, unsigned int cmd,
119 							unsigned long arg)
120 {
121 	int ret = -ENOTTY;
122 	int time;
123 
124 	switch (cmd) {
125 	case WDIOC_GETSUPPORT:
126 		ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
127 				   sizeof(ident)) ? -EFAULT : 0;
128 		break;
129 
130 	case WDIOC_GETSTATUS:
131 		ret = put_user(0, (int __user *)arg);
132 		break;
133 
134 	case WDIOC_GETBOOTSTATUS:
135 		ret = put_user(0, (int __user *)arg);
136 		break;
137 
138 	case WDIOC_KEEPALIVE:
139 		wdt_enable();
140 		ret = 0;
141 		break;
142 
143 	case WDIOC_SETTIMEOUT:
144 		ret = get_user(time, (int __user *)arg);
145 		if (ret)
146 			break;
147 
148 		if (time < 31 || time > 255) {
149 			ret = -EINVAL;
150 			break;
151 		}
152 
153 		margin = time;
154 		wdt_enable();
155 		fallthrough;
156 
157 	case WDIOC_GETTIMEOUT:
158 		ret = put_user(margin, (int __user *)arg);
159 		break;
160 	}
161 
162 	return ret;
163 }
164 
165 static int fitpc2_wdt_release(struct inode *inode, struct file *file)
166 {
167 	if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
168 		wdt_disable();
169 		pr_info("Device disabled\n");
170 	} else {
171 		pr_warn("Device closed unexpectedly - timer will not stop\n");
172 		wdt_enable();
173 	}
174 
175 	clear_bit(WDT_IN_USE, &wdt_status);
176 	clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
177 
178 	return 0;
179 }
180 
181 
182 static const struct file_operations fitpc2_wdt_fops = {
183 	.owner		= THIS_MODULE,
184 	.llseek		= no_llseek,
185 	.write		= fitpc2_wdt_write,
186 	.unlocked_ioctl	= fitpc2_wdt_ioctl,
187 	.compat_ioctl	= compat_ptr_ioctl,
188 	.open		= fitpc2_wdt_open,
189 	.release	= fitpc2_wdt_release,
190 };
191 
192 static struct miscdevice fitpc2_wdt_miscdev = {
193 	.minor		= WATCHDOG_MINOR,
194 	.name		= "watchdog",
195 	.fops		= &fitpc2_wdt_fops,
196 };
197 
198 static int __init fitpc2_wdt_init(void)
199 {
200 	int err;
201 	const char *brd_name;
202 
203 	brd_name = dmi_get_system_info(DMI_BOARD_NAME);
204 
205 	if (!brd_name || !strstr(brd_name, "SBC-FITPC2"))
206 		return -ENODEV;
207 
208 	pr_info("%s found\n", brd_name);
209 
210 	if (!request_region(COMMAND_PORT, 1, WATCHDOG_NAME)) {
211 		pr_err("I/O address 0x%04x already in use\n", COMMAND_PORT);
212 		return -EIO;
213 	}
214 
215 	if (!request_region(DATA_PORT, 1, WATCHDOG_NAME)) {
216 		pr_err("I/O address 0x%04x already in use\n", DATA_PORT);
217 		err = -EIO;
218 		goto err_data_port;
219 	}
220 
221 	if (margin < 31 || margin > 255) {
222 		pr_err("margin must be in range 31 - 255 seconds, you tried to set %d\n",
223 		       margin);
224 		err = -EINVAL;
225 		goto err_margin;
226 	}
227 
228 	err = misc_register(&fitpc2_wdt_miscdev);
229 	if (err) {
230 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
231 		       WATCHDOG_MINOR, err);
232 		goto err_margin;
233 	}
234 
235 	return 0;
236 
237 err_margin:
238 	release_region(DATA_PORT, 1);
239 err_data_port:
240 	release_region(COMMAND_PORT, 1);
241 
242 	return err;
243 }
244 
245 static void __exit fitpc2_wdt_exit(void)
246 {
247 	misc_deregister(&fitpc2_wdt_miscdev);
248 	release_region(DATA_PORT, 1);
249 	release_region(COMMAND_PORT, 1);
250 }
251 
252 module_init(fitpc2_wdt_init);
253 module_exit(fitpc2_wdt_exit);
254 
255 MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
256 MODULE_DESCRIPTION("SBC-FITPC2 Watchdog");
257 
258 module_param(margin, int, 0);
259 MODULE_PARM_DESC(margin, "Watchdog margin in seconds (default 60s)");
260 
261 module_param(nowayout, bool, 0);
262 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
263 
264 MODULE_LICENSE("GPL");
265