xref: /linux/drivers/watchdog/it87_wdt.c (revision 9f7861c56b51b84d30114e7fea9d744a9d5ba9b7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Watchdog Timer Driver
4  *	   for ITE IT87xx Environment Control - Low Pin Count Input / Output
5  *
6  *	(c) Copyright 2007  Oliver Schuster <olivers137@aol.com>
7  *
8  *	Based on softdog.c	by Alan Cox,
9  *		 83977f_wdt.c	by Jose Goncalves,
10  *		 it87.c		by Chris Gauthron, Jean Delvare
11  *
12  *	Data-sheets: Publicly available at the ITE website
13  *		    http://www.ite.com.tw/
14  *
15  *	Support of the watchdog timers, which are available on
16  *	IT8607, IT8613, IT8620, IT8622, IT8625, IT8628, IT8655, IT8665,
17  *	IT8686, IT8702, IT8712, IT8716, IT8718, IT8720, IT8721, IT8726,
18  *	IT8728, IT8772, IT8783 and IT8784.
19  */
20 
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 
23 #include <linux/init.h>
24 #include <linux/io.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/types.h>
29 #include <linux/watchdog.h>
30 
31 #define WATCHDOG_NAME		"IT87 WDT"
32 
33 /* Defaults for Module Parameter */
34 #define DEFAULT_TIMEOUT		60
35 #define DEFAULT_TESTMODE	0
36 #define DEFAULT_NOWAYOUT	WATCHDOG_NOWAYOUT
37 
38 /* IO Ports */
39 #define REG		0x2e
40 #define VAL		0x2f
41 
42 /* Logical device Numbers LDN */
43 #define GPIO		0x07
44 
45 /* Configuration Registers and Functions */
46 #define LDNREG		0x07
47 #define CHIPID		0x20
48 #define CHIPREV		0x22
49 
50 /* Chip Id numbers */
51 #define NO_DEV_ID	0xffff
52 #define IT8607_ID	0x8607
53 #define IT8613_ID	0x8613
54 #define IT8620_ID	0x8620
55 #define IT8622_ID	0x8622
56 #define IT8625_ID	0x8625
57 #define IT8628_ID	0x8628
58 #define IT8655_ID	0x8655
59 #define IT8665_ID	0x8665
60 #define IT8686_ID	0x8686
61 #define IT8702_ID	0x8702
62 #define IT8705_ID	0x8705
63 #define IT8712_ID	0x8712
64 #define IT8716_ID	0x8716
65 #define IT8718_ID	0x8718
66 #define IT8720_ID	0x8720
67 #define IT8721_ID	0x8721
68 #define IT8726_ID	0x8726	/* the data sheet suggest wrongly 0x8716 */
69 #define IT8728_ID	0x8728
70 #define IT8772_ID	0x8772
71 #define IT8783_ID	0x8783
72 #define IT8784_ID	0x8784
73 #define IT8786_ID	0x8786
74 
75 /* GPIO Configuration Registers LDN=0x07 */
76 #define WDTCTRL		0x71
77 #define WDTCFG		0x72
78 #define WDTVALLSB	0x73
79 #define WDTVALMSB	0x74
80 
81 /* GPIO Bits WDTCFG */
82 #define WDT_TOV1	0x80
83 #define WDT_KRST	0x40
84 #define WDT_TOVE	0x20
85 #define WDT_PWROK	0x10 /* not in it8721 */
86 #define WDT_INT_MASK	0x0f
87 
88 static unsigned int max_units, chip_type;
89 
90 static unsigned int timeout = DEFAULT_TIMEOUT;
91 static int testmode = DEFAULT_TESTMODE;
92 static bool nowayout = DEFAULT_NOWAYOUT;
93 
94 module_param(timeout, int, 0);
95 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
96 		__MODULE_STRING(DEFAULT_TIMEOUT));
97 module_param(testmode, int, 0);
98 MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
99 		__MODULE_STRING(DEFAULT_TESTMODE));
100 module_param(nowayout, bool, 0);
101 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
102 		__MODULE_STRING(WATCHDOG_NOWAYOUT));
103 
104 /* Superio Chip */
105 
106 static inline int superio_enter(void)
107 {
108 	/*
109 	 * Try to reserve REG and REG + 1 for exclusive access.
110 	 */
111 	if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
112 		return -EBUSY;
113 
114 	outb(0x87, REG);
115 	outb(0x01, REG);
116 	outb(0x55, REG);
117 	outb(0x55, REG);
118 	return 0;
119 }
120 
121 static inline void superio_exit(void)
122 {
123 	outb(0x02, REG);
124 	outb(0x02, VAL);
125 	release_region(REG, 2);
126 }
127 
128 static inline void superio_select(int ldn)
129 {
130 	outb(LDNREG, REG);
131 	outb(ldn, VAL);
132 }
133 
134 static inline int superio_inb(int reg)
135 {
136 	outb(reg, REG);
137 	return inb(VAL);
138 }
139 
140 static inline void superio_outb(int val, int reg)
141 {
142 	outb(reg, REG);
143 	outb(val, VAL);
144 }
145 
146 static inline int superio_inw(int reg)
147 {
148 	int val;
149 	outb(reg++, REG);
150 	val = inb(VAL) << 8;
151 	outb(reg, REG);
152 	val |= inb(VAL);
153 	return val;
154 }
155 
156 /* Internal function, should be called after superio_select(GPIO) */
157 static void _wdt_update_timeout(unsigned int t)
158 {
159 	unsigned char cfg = WDT_KRST;
160 
161 	if (testmode)
162 		cfg = 0;
163 
164 	if (t <= max_units)
165 		cfg |= WDT_TOV1;
166 	else
167 		t /= 60;
168 
169 	if (chip_type != IT8721_ID)
170 		cfg |= WDT_PWROK;
171 
172 	superio_outb(cfg, WDTCFG);
173 	superio_outb(t, WDTVALLSB);
174 	if (max_units > 255)
175 		superio_outb(t >> 8, WDTVALMSB);
176 }
177 
178 static int wdt_update_timeout(unsigned int t)
179 {
180 	int ret;
181 
182 	ret = superio_enter();
183 	if (ret)
184 		return ret;
185 
186 	superio_select(GPIO);
187 	_wdt_update_timeout(t);
188 	superio_exit();
189 
190 	return 0;
191 }
192 
193 static int wdt_round_time(int t)
194 {
195 	t += 59;
196 	t -= t % 60;
197 	return t;
198 }
199 
200 /* watchdog timer handling */
201 
202 static int wdt_start(struct watchdog_device *wdd)
203 {
204 	return wdt_update_timeout(wdd->timeout);
205 }
206 
207 static int wdt_stop(struct watchdog_device *wdd)
208 {
209 	return wdt_update_timeout(0);
210 }
211 
212 /**
213  *	wdt_set_timeout - set a new timeout value with watchdog ioctl
214  *	@t: timeout value in seconds
215  *
216  *	The hardware device has a 8 or 16 bit watchdog timer (depends on
217  *	chip version) that can be configured to count seconds or minutes.
218  *
219  *	Used within WDIOC_SETTIMEOUT watchdog device ioctl.
220  */
221 
222 static int wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
223 {
224 	int ret = 0;
225 
226 	if (t > max_units)
227 		t = wdt_round_time(t);
228 
229 	wdd->timeout = t;
230 
231 	if (watchdog_hw_running(wdd))
232 		ret = wdt_update_timeout(t);
233 
234 	return ret;
235 }
236 
237 static const struct watchdog_info ident = {
238 	.options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
239 	.firmware_version = 1,
240 	.identity = WATCHDOG_NAME,
241 };
242 
243 static const struct watchdog_ops wdt_ops = {
244 	.owner = THIS_MODULE,
245 	.start = wdt_start,
246 	.stop = wdt_stop,
247 	.set_timeout = wdt_set_timeout,
248 };
249 
250 static struct watchdog_device wdt_dev = {
251 	.info = &ident,
252 	.ops = &wdt_ops,
253 	.min_timeout = 1,
254 };
255 
256 static int __init it87_wdt_init(void)
257 {
258 	u8  chip_rev;
259 	int rc;
260 
261 	rc = superio_enter();
262 	if (rc)
263 		return rc;
264 
265 	chip_type = superio_inw(CHIPID);
266 	chip_rev  = superio_inb(CHIPREV) & 0x0f;
267 	superio_exit();
268 
269 	switch (chip_type) {
270 	case IT8702_ID:
271 		max_units = 255;
272 		break;
273 	case IT8712_ID:
274 		max_units = (chip_rev < 8) ? 255 : 65535;
275 		break;
276 	case IT8716_ID:
277 	case IT8726_ID:
278 		max_units = 65535;
279 		break;
280 	case IT8607_ID:
281 	case IT8613_ID:
282 	case IT8620_ID:
283 	case IT8622_ID:
284 	case IT8625_ID:
285 	case IT8628_ID:
286 	case IT8655_ID:
287 	case IT8665_ID:
288 	case IT8686_ID:
289 	case IT8718_ID:
290 	case IT8720_ID:
291 	case IT8721_ID:
292 	case IT8728_ID:
293 	case IT8772_ID:
294 	case IT8783_ID:
295 	case IT8784_ID:
296 	case IT8786_ID:
297 		max_units = 65535;
298 		break;
299 	case IT8705_ID:
300 		pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
301 		       chip_type, chip_rev);
302 		return -ENODEV;
303 	case NO_DEV_ID:
304 		pr_err("no device\n");
305 		return -ENODEV;
306 	default:
307 		pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
308 		       chip_type, chip_rev);
309 		return -ENODEV;
310 	}
311 
312 	rc = superio_enter();
313 	if (rc)
314 		return rc;
315 
316 	superio_select(GPIO);
317 	superio_outb(WDT_TOV1, WDTCFG);
318 	superio_outb(0x00, WDTCTRL);
319 	superio_exit();
320 
321 	if (timeout < 1 || timeout > max_units * 60) {
322 		timeout = DEFAULT_TIMEOUT;
323 		pr_warn("Timeout value out of range, use default %d sec\n",
324 			DEFAULT_TIMEOUT);
325 	}
326 
327 	if (timeout > max_units)
328 		timeout = wdt_round_time(timeout);
329 
330 	wdt_dev.timeout = timeout;
331 	wdt_dev.max_timeout = max_units * 60;
332 
333 	watchdog_stop_on_reboot(&wdt_dev);
334 	rc = watchdog_register_device(&wdt_dev);
335 	if (rc) {
336 		pr_err("Cannot register watchdog device (err=%d)\n", rc);
337 		return rc;
338 	}
339 
340 	pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
341 		chip_type, chip_rev, timeout, nowayout, testmode);
342 
343 	return 0;
344 }
345 
346 static void __exit it87_wdt_exit(void)
347 {
348 	watchdog_unregister_device(&wdt_dev);
349 }
350 
351 module_init(it87_wdt_init);
352 module_exit(it87_wdt_exit);
353 
354 MODULE_AUTHOR("Oliver Schuster");
355 MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
356 MODULE_LICENSE("GPL");
357