1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Watchdog Timer Driver
4 * for ITE IT87xx Environment Control - Low Pin Count Input / Output
6 * (c) Copyright 2007 Oliver Schuster <olivers137@aol.com>
8 * Based on softdog.c by Alan Cox,
9 * 83977f_wdt.c by Jose Goncalves,
10 * it87.c by Chris Gauthron, Jean Delvare
12 * Data-sheets: Publicly available at the ITE website
13 * http://www.ite.com.tw/
15 * Support of the watchdog timers, which are available on
16 * IT8607, IT8613, IT8620, IT8622, IT8625, IT8628, IT8655, IT8659,
17 * IT8665, IT8686, IT8702, IT8712, IT8716, IT8718, IT8720, IT8721,
18 * IT8726, IT8728, IT8772, IT8783, IT8784 and IT8786.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/bits.h>
24 #include <linux/dmi.h>
25 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/types.h>
31 #include <linux/watchdog.h>
33 #define WATCHDOG_NAME "IT87 WDT"
35 /* Defaults for Module Parameter */
36 #define DEFAULT_TIMEOUT 60
37 #define DEFAULT_TESTMODE 0
38 #define DEFAULT_NOWAYOUT WATCHDOG_NOWAYOUT
44 /* Logical device Numbers LDN */
48 /* Configuration Registers and Functions */
54 #define NO_DEV_ID 0xffff
55 #define IT8607_ID 0x8607
56 #define IT8613_ID 0x8613
57 #define IT8620_ID 0x8620
58 #define IT8622_ID 0x8622
59 #define IT8625_ID 0x8625
60 #define IT8628_ID 0x8628
61 #define IT8655_ID 0x8655
62 #define IT8659_ID 0x8659
63 #define IT8665_ID 0x8665
64 #define IT8686_ID 0x8686
65 #define IT8702_ID 0x8702
66 #define IT8705_ID 0x8705
67 #define IT8712_ID 0x8712
68 #define IT8716_ID 0x8716
69 #define IT8718_ID 0x8718
70 #define IT8720_ID 0x8720
71 #define IT8721_ID 0x8721
72 #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
73 #define IT8728_ID 0x8728
74 #define IT8772_ID 0x8772
75 #define IT8783_ID 0x8783
76 #define IT8784_ID 0x8784
77 #define IT8786_ID 0x8786
79 /* Environment Controller Configuration Registers LDN=0x04 */
82 /* Environment Controller Bits SCR1 */
83 #define WDT_PWRGD 0x20
85 /* GPIO Configuration Registers LDN=0x07 */
88 #define WDTVALLSB 0x73
89 #define WDTVALMSB 0x74
91 /* GPIO Bits WDTCFG */
95 #define WDT_PWROK 0x10 /* not in it8721 */
96 #define WDT_INT_MASK 0x0f
98 static unsigned int max_units
, chip_type
;
100 static unsigned int timeout
= DEFAULT_TIMEOUT
;
101 static int testmode
= DEFAULT_TESTMODE
;
102 static bool nowayout
= DEFAULT_NOWAYOUT
;
104 module_param(timeout
, int, 0);
105 MODULE_PARM_DESC(timeout
, "Watchdog timeout in seconds, default="
106 __MODULE_STRING(DEFAULT_TIMEOUT
));
107 module_param(testmode
, int, 0);
108 MODULE_PARM_DESC(testmode
, "Watchdog test mode (1 = no reboot), default="
109 __MODULE_STRING(DEFAULT_TESTMODE
));
110 module_param(nowayout
, bool, 0);
111 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started, default="
112 __MODULE_STRING(WATCHDOG_NOWAYOUT
));
116 static inline int superio_enter(void)
119 * Try to reserve REG and REG + 1 for exclusive access.
121 if (!request_muxed_region(REG
, 2, WATCHDOG_NAME
))
131 static inline void superio_exit(void)
135 release_region(REG
, 2);
138 static inline void superio_select(int ldn
)
144 static inline int superio_inb(int reg
)
150 static inline void superio_outb(int val
, int reg
)
156 static inline int superio_inw(int reg
)
167 /* Internal function, should be called after superio_select(GPIO) */
168 static void _wdt_update_timeout(unsigned int t
)
170 unsigned char cfg
= WDT_KRST
;
180 if (chip_type
!= IT8721_ID
)
183 superio_outb(cfg
, WDTCFG
);
184 superio_outb(t
, WDTVALLSB
);
186 superio_outb(t
>> 8, WDTVALMSB
);
189 static int wdt_update_timeout(unsigned int t
)
193 ret
= superio_enter();
197 superio_select(GPIO
);
198 _wdt_update_timeout(t
);
204 static int wdt_round_time(int t
)
211 /* watchdog timer handling */
213 static int wdt_start(struct watchdog_device
*wdd
)
215 return wdt_update_timeout(wdd
->timeout
);
218 static int wdt_stop(struct watchdog_device
*wdd
)
220 return wdt_update_timeout(0);
224 * wdt_set_timeout - set a new timeout value with watchdog ioctl
225 * @wdd: pointer to the watchdog_device structure
226 * @t: timeout value in seconds
228 * The hardware device has a 8 or 16 bit watchdog timer (depends on
229 * chip version) that can be configured to count seconds or minutes.
231 * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
233 * Return: 0 if the timeout was set successfully, or a negative error code on
237 static int wdt_set_timeout(struct watchdog_device
*wdd
, unsigned int t
)
242 t
= wdt_round_time(t
);
246 if (watchdog_hw_running(wdd
))
247 ret
= wdt_update_timeout(t
);
253 IT87_WDT_OUTPUT_THROUGH_PWRGD
= BIT(0),
256 static const struct dmi_system_id it87_quirks
[] = {
258 /* Qotom Q30900P (IT8786) */
260 DMI_EXACT_MATCH(DMI_BOARD_NAME
, "QCML04"),
262 .driver_data
= (void *)IT87_WDT_OUTPUT_THROUGH_PWRGD
,
267 static const struct watchdog_info ident
= {
268 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
,
269 .firmware_version
= 1,
270 .identity
= WATCHDOG_NAME
,
273 static const struct watchdog_ops wdt_ops
= {
274 .owner
= THIS_MODULE
,
277 .set_timeout
= wdt_set_timeout
,
280 static struct watchdog_device wdt_dev
= {
286 static int __init
it87_wdt_init(void)
288 const struct dmi_system_id
*dmi_id
;
294 rc
= superio_enter();
298 chip_type
= superio_inw(CHIPID
);
299 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
302 dmi_id
= dmi_first_match(it87_quirks
);
304 quirks
= (long)dmi_id
->driver_data
;
311 max_units
= (chip_rev
< 8) ? 255 : 65535;
336 pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
337 chip_type
, chip_rev
);
340 pr_err("no device\n");
343 pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
344 chip_type
, chip_rev
);
348 rc
= superio_enter();
352 superio_select(GPIO
);
353 superio_outb(WDT_TOV1
, WDTCFG
);
358 ctrl
= superio_inb(WDTCTRL
);
360 superio_outb(ctrl
, WDTCTRL
);
363 superio_outb(0x00, WDTCTRL
);
366 if (quirks
& IT87_WDT_OUTPUT_THROUGH_PWRGD
) {
368 ctrl
= superio_inb(SCR1
);
369 if (!(ctrl
& WDT_PWRGD
)) {
371 superio_outb(ctrl
, SCR1
);
377 if (timeout
< 1 || timeout
> max_units
* 60) {
378 timeout
= DEFAULT_TIMEOUT
;
379 pr_warn("Timeout value out of range, use default %d sec\n",
383 if (timeout
> max_units
)
384 timeout
= wdt_round_time(timeout
);
386 wdt_dev
.timeout
= timeout
;
387 wdt_dev
.max_timeout
= max_units
* 60;
389 watchdog_stop_on_reboot(&wdt_dev
);
390 rc
= watchdog_register_device(&wdt_dev
);
394 pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
395 chip_type
, chip_rev
, timeout
, nowayout
, testmode
);
400 static void __exit
it87_wdt_exit(void)
402 watchdog_unregister_device(&wdt_dev
);
405 module_init(it87_wdt_init
);
406 module_exit(it87_wdt_exit
);
408 MODULE_AUTHOR("Oliver Schuster");
409 MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
410 MODULE_LICENSE("GPL");