1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IT8712F "Smart Guardian" Watchdog support
4  *
5  *	Copyright (c) 2006-2007 Jorge Boncompte - DTI2 <jorge@dti2.net>
6  *
7  *	Based on info and code taken from:
8  *
9  *	drivers/char/watchdog/scx200_wdt.c
10  *	drivers/hwmon/it87.c
11  *	IT8712F EC-LPC I/O Preliminary Specification 0.8.2
12  *	IT8712F EC-LPC I/O Preliminary Specification 0.9.3
13  *
14  *	The author(s) of this software shall not be held liable for damages
15  *	of any nature resulting due to the use of this software. This
16  *	software is provided AS-IS with no warranties.
17  */
18 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/notifier.h>
27 #include <linux/reboot.h>
28 #include <linux/fs.h>
29 #include <linux/spinlock.h>
30 #include <linux/uaccess.h>
31 #include <linux/io.h>
32 #include <linux/ioport.h>
33 
34 #define DEBUG
35 #define NAME "it8712f_wdt"
36 
37 MODULE_AUTHOR("Jorge Boncompte - DTI2 <jorge@dti2.net>");
38 MODULE_DESCRIPTION("IT8712F Watchdog Driver");
39 MODULE_LICENSE("GPL");
40 
41 static int max_units = 255;
42 static int margin = 60;		/* in seconds */
43 module_param(margin, int, 0);
44 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
45 
46 static bool nowayout = WATCHDOG_NOWAYOUT;
47 module_param(nowayout, bool, 0);
48 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
49 
50 static unsigned long wdt_open;
51 static unsigned expect_close;
52 static unsigned char revision;
53 
54 /* Dog Food address - We use the game port address */
55 static unsigned short address;
56 
57 #define	REG		0x2e	/* The register to read/write */
58 #define	VAL		0x2f	/* The value to read/write */
59 
60 #define	LDN		0x07	/* Register: Logical device select */
61 #define	DEVID		0x20	/* Register: Device ID */
62 #define	DEVREV		0x22	/* Register: Device Revision */
63 #define ACT_REG		0x30	/* LDN Register: Activation */
64 #define BASE_REG	0x60	/* LDN Register: Base address */
65 
66 #define IT8712F_DEVID	0x8712
67 
68 #define LDN_GPIO	0x07	/* GPIO and Watch Dog Timer */
69 #define LDN_GAME	0x09	/* Game Port */
70 
71 #define WDT_CONTROL	0x71	/* WDT Register: Control */
72 #define WDT_CONFIG	0x72	/* WDT Register: Configuration */
73 #define WDT_TIMEOUT	0x73	/* WDT Register: Timeout Value */
74 
75 #define WDT_RESET_GAME	0x10	/* Reset timer on read or write to game port */
76 #define WDT_RESET_KBD	0x20	/* Reset timer on keyboard interrupt */
77 #define WDT_RESET_MOUSE	0x40	/* Reset timer on mouse interrupt */
78 #define WDT_RESET_CIR	0x80	/* Reset timer on consumer IR interrupt */
79 
80 #define WDT_UNIT_SEC	0x80	/* If 0 in MINUTES */
81 
82 #define WDT_OUT_PWROK	0x10	/* Pulse PWROK on timeout */
83 #define WDT_OUT_KRST	0x40	/* Pulse reset on timeout */
84 
85 static int wdt_control_reg = WDT_RESET_GAME;
86 module_param(wdt_control_reg, int, 0);
87 MODULE_PARM_DESC(wdt_control_reg, "Value to write to watchdog control "
88 		"register. The default WDT_RESET_GAME resets the timer on "
89 		"game port reads that this driver generates. You can also "
90 		"use KBD, MOUSE or CIR if you have some external way to "
91 		"generate those interrupts.");
92 
93 static int superio_inb(int reg)
94 {
95 	outb(reg, REG);
96 	return inb(VAL);
97 }
98 
99 static void superio_outb(int val, int reg)
100 {
101 	outb(reg, REG);
102 	outb(val, VAL);
103 }
104 
105 static int superio_inw(int reg)
106 {
107 	int val;
108 	outb(reg++, REG);
109 	val = inb(VAL) << 8;
110 	outb(reg, REG);
111 	val |= inb(VAL);
112 	return val;
113 }
114 
115 static inline void superio_select(int ldn)
116 {
117 	outb(LDN, REG);
118 	outb(ldn, VAL);
119 }
120 
121 static inline int superio_enter(void)
122 {
123 	/*
124 	 * Try to reserve REG and REG + 1 for exclusive access.
125 	 */
126 	if (!request_muxed_region(REG, 2, NAME))
127 		return -EBUSY;
128 
129 	outb(0x87, REG);
130 	outb(0x01, REG);
131 	outb(0x55, REG);
132 	outb(0x55, REG);
133 	return 0;
134 }
135 
136 static inline void superio_exit(void)
137 {
138 	outb(0x02, REG);
139 	outb(0x02, VAL);
140 	release_region(REG, 2);
141 }
142 
143 static inline void it8712f_wdt_ping(void)
144 {
145 	if (wdt_control_reg & WDT_RESET_GAME)
146 		inb(address);
147 }
148 
149 static void it8712f_wdt_update_margin(void)
150 {
151 	int config = WDT_OUT_KRST | WDT_OUT_PWROK;
152 	int units = margin;
153 
154 	/* Switch to minutes precision if the configured margin
155 	 * value does not fit within the register width.
156 	 */
157 	if (units <= max_units) {
158 		config |= WDT_UNIT_SEC; /* else UNIT is MINUTES */
159 		pr_info("timer margin %d seconds\n", units);
160 	} else {
161 		units /= 60;
162 		pr_info("timer margin %d minutes\n", units);
163 	}
164 	superio_outb(config, WDT_CONFIG);
165 
166 	if (revision >= 0x08)
167 		superio_outb(units >> 8, WDT_TIMEOUT + 1);
168 	superio_outb(units, WDT_TIMEOUT);
169 }
170 
171 static int it8712f_wdt_get_status(void)
172 {
173 	if (superio_inb(WDT_CONTROL) & 0x01)
174 		return WDIOF_CARDRESET;
175 	else
176 		return 0;
177 }
178 
179 static int it8712f_wdt_enable(void)
180 {
181 	int ret = superio_enter();
182 	if (ret)
183 		return ret;
184 
185 	pr_debug("enabling watchdog timer\n");
186 	superio_select(LDN_GPIO);
187 
188 	superio_outb(wdt_control_reg, WDT_CONTROL);
189 
190 	it8712f_wdt_update_margin();
191 
192 	superio_exit();
193 
194 	it8712f_wdt_ping();
195 
196 	return 0;
197 }
198 
199 static int it8712f_wdt_disable(void)
200 {
201 	int ret = superio_enter();
202 	if (ret)
203 		return ret;
204 
205 	pr_debug("disabling watchdog timer\n");
206 	superio_select(LDN_GPIO);
207 
208 	superio_outb(0, WDT_CONFIG);
209 	superio_outb(0, WDT_CONTROL);
210 	if (revision >= 0x08)
211 		superio_outb(0, WDT_TIMEOUT + 1);
212 	superio_outb(0, WDT_TIMEOUT);
213 
214 	superio_exit();
215 	return 0;
216 }
217 
218 static int it8712f_wdt_notify(struct notifier_block *this,
219 		    unsigned long code, void *unused)
220 {
221 	if (code == SYS_HALT || code == SYS_POWER_OFF)
222 		if (!nowayout)
223 			it8712f_wdt_disable();
224 
225 	return NOTIFY_DONE;
226 }
227 
228 static struct notifier_block it8712f_wdt_notifier = {
229 	.notifier_call = it8712f_wdt_notify,
230 };
231 
232 static ssize_t it8712f_wdt_write(struct file *file, const char __user *data,
233 					size_t len, loff_t *ppos)
234 {
235 	/* check for a magic close character */
236 	if (len) {
237 		size_t i;
238 
239 		it8712f_wdt_ping();
240 
241 		expect_close = 0;
242 		for (i = 0; i < len; ++i) {
243 			char c;
244 			if (get_user(c, data + i))
245 				return -EFAULT;
246 			if (c == 'V')
247 				expect_close = 42;
248 		}
249 	}
250 
251 	return len;
252 }
253 
254 static long it8712f_wdt_ioctl(struct file *file, unsigned int cmd,
255 							unsigned long arg)
256 {
257 	void __user *argp = (void __user *)arg;
258 	int __user *p = argp;
259 	static const struct watchdog_info ident = {
260 		.identity = "IT8712F Watchdog",
261 		.firmware_version = 1,
262 		.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
263 						WDIOF_MAGICCLOSE,
264 	};
265 	int value;
266 	int ret;
267 
268 	switch (cmd) {
269 	case WDIOC_GETSUPPORT:
270 		if (copy_to_user(argp, &ident, sizeof(ident)))
271 			return -EFAULT;
272 		return 0;
273 	case WDIOC_GETSTATUS:
274 		ret = superio_enter();
275 		if (ret)
276 			return ret;
277 		superio_select(LDN_GPIO);
278 
279 		value = it8712f_wdt_get_status();
280 
281 		superio_exit();
282 
283 		return put_user(value, p);
284 	case WDIOC_GETBOOTSTATUS:
285 		return put_user(0, p);
286 	case WDIOC_KEEPALIVE:
287 		it8712f_wdt_ping();
288 		return 0;
289 	case WDIOC_SETTIMEOUT:
290 		if (get_user(value, p))
291 			return -EFAULT;
292 		if (value < 1)
293 			return -EINVAL;
294 		if (value > (max_units * 60))
295 			return -EINVAL;
296 		margin = value;
297 		ret = superio_enter();
298 		if (ret)
299 			return ret;
300 		superio_select(LDN_GPIO);
301 
302 		it8712f_wdt_update_margin();
303 
304 		superio_exit();
305 		it8712f_wdt_ping();
306 		fallthrough;
307 	case WDIOC_GETTIMEOUT:
308 		if (put_user(margin, p))
309 			return -EFAULT;
310 		return 0;
311 	default:
312 		return -ENOTTY;
313 	}
314 }
315 
316 static int it8712f_wdt_open(struct inode *inode, struct file *file)
317 {
318 	int ret;
319 	/* only allow one at a time */
320 	if (test_and_set_bit(0, &wdt_open))
321 		return -EBUSY;
322 
323 	ret = it8712f_wdt_enable();
324 	if (ret)
325 		return ret;
326 	return stream_open(inode, file);
327 }
328 
329 static int it8712f_wdt_release(struct inode *inode, struct file *file)
330 {
331 	if (expect_close != 42) {
332 		pr_warn("watchdog device closed unexpectedly, will not disable the watchdog timer\n");
333 	} else if (!nowayout) {
334 		if (it8712f_wdt_disable())
335 			pr_warn("Watchdog disable failed\n");
336 	}
337 	expect_close = 0;
338 	clear_bit(0, &wdt_open);
339 
340 	return 0;
341 }
342 
343 static const struct file_operations it8712f_wdt_fops = {
344 	.owner = THIS_MODULE,
345 	.llseek = no_llseek,
346 	.write = it8712f_wdt_write,
347 	.unlocked_ioctl = it8712f_wdt_ioctl,
348 	.compat_ioctl = compat_ptr_ioctl,
349 	.open = it8712f_wdt_open,
350 	.release = it8712f_wdt_release,
351 };
352 
353 static struct miscdevice it8712f_wdt_miscdev = {
354 	.minor = WATCHDOG_MINOR,
355 	.name = "watchdog",
356 	.fops = &it8712f_wdt_fops,
357 };
358 
359 static int __init it8712f_wdt_find(unsigned short *address)
360 {
361 	int err = -ENODEV;
362 	int chip_type;
363 	int ret = superio_enter();
364 	if (ret)
365 		return ret;
366 
367 	chip_type = superio_inw(DEVID);
368 	if (chip_type != IT8712F_DEVID)
369 		goto exit;
370 
371 	superio_select(LDN_GAME);
372 	superio_outb(1, ACT_REG);
373 	if (!(superio_inb(ACT_REG) & 0x01)) {
374 		pr_err("Device not activated, skipping\n");
375 		goto exit;
376 	}
377 
378 	*address = superio_inw(BASE_REG);
379 	if (*address == 0) {
380 		pr_err("Base address not set, skipping\n");
381 		goto exit;
382 	}
383 
384 	err = 0;
385 	revision = superio_inb(DEVREV) & 0x0f;
386 
387 	/* Later revisions have 16-bit values per datasheet 0.9.1 */
388 	if (revision >= 0x08)
389 		max_units = 65535;
390 
391 	if (margin > (max_units * 60))
392 		margin = (max_units * 60);
393 
394 	pr_info("Found IT%04xF chip revision %d - using DogFood address 0x%x\n",
395 		chip_type, revision, *address);
396 
397 exit:
398 	superio_exit();
399 	return err;
400 }
401 
402 static int __init it8712f_wdt_init(void)
403 {
404 	int err = 0;
405 
406 	if (it8712f_wdt_find(&address))
407 		return -ENODEV;
408 
409 	if (!request_region(address, 1, "IT8712F Watchdog")) {
410 		pr_warn("watchdog I/O region busy\n");
411 		return -EBUSY;
412 	}
413 
414 	err = it8712f_wdt_disable();
415 	if (err) {
416 		pr_err("unable to disable watchdog timer\n");
417 		goto out;
418 	}
419 
420 	err = register_reboot_notifier(&it8712f_wdt_notifier);
421 	if (err) {
422 		pr_err("unable to register reboot notifier\n");
423 		goto out;
424 	}
425 
426 	err = misc_register(&it8712f_wdt_miscdev);
427 	if (err) {
428 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
429 		       WATCHDOG_MINOR, err);
430 		goto reboot_out;
431 	}
432 
433 	return 0;
434 
435 
436 reboot_out:
437 	unregister_reboot_notifier(&it8712f_wdt_notifier);
438 out:
439 	release_region(address, 1);
440 	return err;
441 }
442 
443 static void __exit it8712f_wdt_exit(void)
444 {
445 	misc_deregister(&it8712f_wdt_miscdev);
446 	unregister_reboot_notifier(&it8712f_wdt_notifier);
447 	release_region(address, 1);
448 }
449 
450 module_init(it8712f_wdt_init);
451 module_exit(it8712f_wdt_exit);
452