blob: f3bf3ea50e39f6fcd362bd9668377ddb1759c324 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Oliver Schustere1fee942008-03-05 16:48:45 +01002/*
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
Guenter Roeckcddda072017-06-10 21:04:36 -070016 * IT8607, IT8620, IT8622, IT8625, IT8628, IT8655, IT8665, IT8686,
17 * IT8702, IT8712, IT8716, IT8718, IT8720, IT8721, IT8726, IT8728,
18 * and IT8783.
Oliver Schustere1fee942008-03-05 16:48:45 +010019 */
20
Joe Perches27c766a2012-02-15 15:06:19 -080021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Guenter Roeck1123c512017-06-10 21:04:35 -070023#include <linux/init.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
Oliver Schustere1fee942008-03-05 16:48:45 +010026#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/types.h>
Oliver Schustere1fee942008-03-05 16:48:45 +010029#include <linux/watchdog.h>
Oliver Schustere1fee942008-03-05 16:48:45 +010030
Oliver Schustere1fee942008-03-05 16:48:45 +010031#define WATCHDOG_NAME "IT87 WDT"
Oliver Schustere1fee942008-03-05 16:48:45 +010032
33/* Defaults for Module Parameter */
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +000034#define DEFAULT_TIMEOUT 60
Oliver Schustere1fee942008-03-05 16:48:45 +010035#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
Oliver Schustere1fee942008-03-05 16:48:45 +010044
45/* Configuration Registers and Functions */
46#define LDNREG 0x07
47#define CHIPID 0x20
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +000048#define CHIPREV 0x22
Oliver Schustere1fee942008-03-05 16:48:45 +010049
50/* Chip Id numbers */
51#define NO_DEV_ID 0xffff
Guenter Roeckcddda072017-06-10 21:04:36 -070052#define IT8607_ID 0x8607
Maciej S. Szmigiero06716122016-12-15 23:52:36 +010053#define IT8620_ID 0x8620
Guenter Roeckcddda072017-06-10 21:04:36 -070054#define IT8622_ID 0x8622
55#define IT8625_ID 0x8625
56#define IT8628_ID 0x8628
57#define IT8655_ID 0x8655
58#define IT8665_ID 0x8665
59#define IT8686_ID 0x8686
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +020060#define IT8702_ID 0x8702
Oliver Schustere1fee942008-03-05 16:48:45 +010061#define IT8705_ID 0x8705
62#define IT8712_ID 0x8712
63#define IT8716_ID 0x8716
64#define IT8718_ID 0x8718
Ondrej Zajicekee3e9652010-09-14 02:47:28 +020065#define IT8720_ID 0x8720
Huaro Tomita4bc30272011-01-21 07:37:51 +090066#define IT8721_ID 0x8721
Oliver Schustere1fee942008-03-05 16:48:45 +010067#define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
Diego Elio Pettenò198ca012012-03-14 20:49:04 +010068#define IT8728_ID 0x8728
Paolo Tetif83918f2014-10-19 21:39:33 +020069#define IT8783_ID 0x8783
Vincent Prince6ae58ee2020-01-23 15:05:44 +010070#define IT8786_ID 0x8786
Oliver Schustere1fee942008-03-05 16:48:45 +010071
72/* GPIO Configuration Registers LDN=0x07 */
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +000073#define WDTCTRL 0x71
Oliver Schustere1fee942008-03-05 16:48:45 +010074#define WDTCFG 0x72
75#define WDTVALLSB 0x73
76#define WDTVALMSB 0x74
77
Oliver Schustere1fee942008-03-05 16:48:45 +010078/* GPIO Bits WDTCFG */
79#define WDT_TOV1 0x80
80#define WDT_KRST 0x40
81#define WDT_TOVE 0x20
Huaro Tomita4bc30272011-01-21 07:37:51 +090082#define WDT_PWROK 0x10 /* not in it8721 */
Oliver Schustere1fee942008-03-05 16:48:45 +010083#define WDT_INT_MASK 0x0f
84
Guenter Roeck893dc8b2017-06-10 21:04:34 -070085static unsigned int max_units, chip_type;
Oliver Schustere1fee942008-03-05 16:48:45 +010086
Guenter Roeck1d7b8032017-06-10 21:04:33 -070087static unsigned int timeout = DEFAULT_TIMEOUT;
Guenter Roeck893dc8b2017-06-10 21:04:34 -070088static int testmode = DEFAULT_TESTMODE;
89static bool nowayout = DEFAULT_NOWAYOUT;
Oliver Schustere1fee942008-03-05 16:48:45 +010090
Oliver Schustere1fee942008-03-05 16:48:45 +010091module_param(timeout, int, 0);
92MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
93 __MODULE_STRING(DEFAULT_TIMEOUT));
94module_param(testmode, int, 0);
95MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
96 __MODULE_STRING(DEFAULT_TESTMODE));
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010097module_param(nowayout, bool, 0);
Oliver Schustere1fee942008-03-05 16:48:45 +010098MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
99 __MODULE_STRING(WATCHDOG_NOWAYOUT));
100
101/* Superio Chip */
102
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700103static inline int superio_enter(void)
Oliver Schustere1fee942008-03-05 16:48:45 +0100104{
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700105 /*
106 * Try to reserve REG and REG + 1 for exclusive access.
107 */
108 if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
109 return -EBUSY;
110
Oliver Schustere1fee942008-03-05 16:48:45 +0100111 outb(0x87, REG);
112 outb(0x01, REG);
113 outb(0x55, REG);
114 outb(0x55, REG);
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700115 return 0;
Oliver Schustere1fee942008-03-05 16:48:45 +0100116}
117
118static inline void superio_exit(void)
119{
120 outb(0x02, REG);
121 outb(0x02, VAL);
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700122 release_region(REG, 2);
Oliver Schustere1fee942008-03-05 16:48:45 +0100123}
124
125static inline void superio_select(int ldn)
126{
127 outb(LDNREG, REG);
128 outb(ldn, VAL);
129}
130
131static inline int superio_inb(int reg)
132{
133 outb(reg, REG);
134 return inb(VAL);
135}
136
137static inline void superio_outb(int val, int reg)
138{
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000139 outb(reg, REG);
140 outb(val, VAL);
Oliver Schustere1fee942008-03-05 16:48:45 +0100141}
142
143static inline int superio_inw(int reg)
144{
145 int val;
146 outb(reg++, REG);
147 val = inb(VAL) << 8;
148 outb(reg, REG);
149 val |= inb(VAL);
150 return val;
151}
152
153static inline void superio_outw(int val, int reg)
154{
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000155 outb(reg++, REG);
156 outb(val >> 8, VAL);
157 outb(reg, REG);
158 outb(val, VAL);
Oliver Schustere1fee942008-03-05 16:48:45 +0100159}
160
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200161/* Internal function, should be called after superio_select(GPIO) */
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700162static void _wdt_update_timeout(unsigned int t)
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200163{
Huaro Tomita4bc30272011-01-21 07:37:51 +0900164 unsigned char cfg = WDT_KRST;
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200165
166 if (testmode)
167 cfg = 0;
168
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700169 if (t <= max_units)
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200170 cfg |= WDT_TOV1;
171 else
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700172 t /= 60;
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200173
Huaro Tomita4bc30272011-01-21 07:37:51 +0900174 if (chip_type != IT8721_ID)
175 cfg |= WDT_PWROK;
176
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200177 superio_outb(cfg, WDTCFG);
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700178 superio_outb(t, WDTVALLSB);
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200179 if (max_units > 255)
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700180 superio_outb(t >> 8, WDTVALMSB);
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200181}
182
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700183static int wdt_update_timeout(unsigned int t)
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700184{
185 int ret;
186
187 ret = superio_enter();
188 if (ret)
189 return ret;
190
191 superio_select(GPIO);
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700192 _wdt_update_timeout(t);
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700193 superio_exit();
194
195 return 0;
196}
197
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200198static int wdt_round_time(int t)
199{
200 t += 59;
201 t -= t % 60;
202 return t;
203}
204
Oliver Schustere1fee942008-03-05 16:48:45 +0100205/* watchdog timer handling */
206
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700207static int wdt_start(struct watchdog_device *wdd)
Oliver Schustere1fee942008-03-05 16:48:45 +0100208{
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700209 return wdt_update_timeout(wdd->timeout);
Oliver Schustere1fee942008-03-05 16:48:45 +0100210}
211
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700212static int wdt_stop(struct watchdog_device *wdd)
Oliver Schustere1fee942008-03-05 16:48:45 +0100213{
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700214 return wdt_update_timeout(0);
Oliver Schustere1fee942008-03-05 16:48:45 +0100215}
216
217/**
218 * wdt_set_timeout - set a new timeout value with watchdog ioctl
219 * @t: timeout value in seconds
220 *
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200221 * The hardware device has a 8 or 16 bit watchdog timer (depends on
222 * chip version) that can be configured to count seconds or minutes.
Oliver Schustere1fee942008-03-05 16:48:45 +0100223 *
224 * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
225 */
226
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700227static int wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
Oliver Schustere1fee942008-03-05 16:48:45 +0100228{
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700229 int ret = 0;
Oliver Schustere1fee942008-03-05 16:48:45 +0100230
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200231 if (t > max_units)
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700232 t = wdt_round_time(t);
Oliver Schustere1fee942008-03-05 16:48:45 +0100233
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700234 wdd->timeout = t;
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700235
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700236 if (watchdog_hw_running(wdd))
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700237 ret = wdt_update_timeout(t);
Oliver Schustere1fee942008-03-05 16:48:45 +0100238
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700239 return ret;
Oliver Schustere1fee942008-03-05 16:48:45 +0100240}
241
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000242static const struct watchdog_info ident = {
Oliver Schustere1fee942008-03-05 16:48:45 +0100243 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700244 .firmware_version = 1,
Oliver Schustere1fee942008-03-05 16:48:45 +0100245 .identity = WATCHDOG_NAME,
246};
247
Gustavo A. R. Silva2211a8d2017-07-07 19:23:21 -0500248static const struct watchdog_ops wdt_ops = {
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700249 .owner = THIS_MODULE,
250 .start = wdt_start,
251 .stop = wdt_stop,
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700252 .set_timeout = wdt_set_timeout,
253};
Oliver Schustere1fee942008-03-05 16:48:45 +0100254
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700255static struct watchdog_device wdt_dev = {
256 .info = &ident,
257 .ops = &wdt_ops,
258 .min_timeout = 1,
259};
Oliver Schustere1fee942008-03-05 16:48:45 +0100260
Oliver Schustere1fee942008-03-05 16:48:45 +0100261static int __init it87_wdt_init(void)
262{
Oliver Schustere1fee942008-03-05 16:48:45 +0100263 u8 chip_rev;
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700264 int rc;
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200265
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700266 rc = superio_enter();
267 if (rc)
268 return rc;
269
Oliver Schustere1fee942008-03-05 16:48:45 +0100270 chip_type = superio_inw(CHIPID);
271 chip_rev = superio_inb(CHIPREV) & 0x0f;
272 superio_exit();
Oliver Schustere1fee942008-03-05 16:48:45 +0100273
274 switch (chip_type) {
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200275 case IT8702_ID:
276 max_units = 255;
277 break;
278 case IT8712_ID:
279 max_units = (chip_rev < 8) ? 255 : 65535;
280 break;
Oliver Schustere1fee942008-03-05 16:48:45 +0100281 case IT8716_ID:
Oliver Schustere1fee942008-03-05 16:48:45 +0100282 case IT8726_ID:
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200283 max_units = 65535;
Oliver Schustere1fee942008-03-05 16:48:45 +0100284 break;
Guenter Roeckcddda072017-06-10 21:04:36 -0700285 case IT8607_ID:
Maciej S. Szmigiero06716122016-12-15 23:52:36 +0100286 case IT8620_ID:
Guenter Roeckcddda072017-06-10 21:04:36 -0700287 case IT8622_ID:
288 case IT8625_ID:
289 case IT8628_ID:
290 case IT8655_ID:
291 case IT8665_ID:
292 case IT8686_ID:
Ondrej Zajicekee3e9652010-09-14 02:47:28 +0200293 case IT8718_ID:
294 case IT8720_ID:
Huaro Tomita4bc30272011-01-21 07:37:51 +0900295 case IT8721_ID:
Diego Elio Pettenò198ca012012-03-14 20:49:04 +0100296 case IT8728_ID:
Paolo Tetif83918f2014-10-19 21:39:33 +0200297 case IT8783_ID:
Vincent Prince6ae58ee2020-01-23 15:05:44 +0100298 case IT8786_ID:
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200299 max_units = 65535;
Ondrej Zajicekee3e9652010-09-14 02:47:28 +0200300 break;
Oliver Schustere1fee942008-03-05 16:48:45 +0100301 case IT8705_ID:
Joe Perches27c766a2012-02-15 15:06:19 -0800302 pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
Oliver Schustere1fee942008-03-05 16:48:45 +0100303 chip_type, chip_rev);
304 return -ENODEV;
305 case NO_DEV_ID:
Joe Perches27c766a2012-02-15 15:06:19 -0800306 pr_err("no device\n");
Oliver Schustere1fee942008-03-05 16:48:45 +0100307 return -ENODEV;
308 default:
Joe Perches27c766a2012-02-15 15:06:19 -0800309 pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
Oliver Schustere1fee942008-03-05 16:48:45 +0100310 chip_type, chip_rev);
311 return -ENODEV;
312 }
313
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700314 rc = superio_enter();
315 if (rc)
316 return rc;
Oliver Schustere1fee942008-03-05 16:48:45 +0100317
318 superio_select(GPIO);
319 superio_outb(WDT_TOV1, WDTCFG);
320 superio_outb(0x00, WDTCTRL);
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700321 superio_exit();
Oliver Schustere1fee942008-03-05 16:48:45 +0100322
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200323 if (timeout < 1 || timeout > max_units * 60) {
Oliver Schustere1fee942008-03-05 16:48:45 +0100324 timeout = DEFAULT_TIMEOUT;
Joe Perches27c766a2012-02-15 15:06:19 -0800325 pr_warn("Timeout value out of range, use default %d sec\n",
326 DEFAULT_TIMEOUT);
Oliver Schustere1fee942008-03-05 16:48:45 +0100327 }
328
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200329 if (timeout > max_units)
330 timeout = wdt_round_time(timeout);
331
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700332 wdt_dev.timeout = timeout;
333 wdt_dev.max_timeout = max_units * 60;
334
Guenter Roeck1123c512017-06-10 21:04:35 -0700335 watchdog_stop_on_reboot(&wdt_dev);
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700336 rc = watchdog_register_device(&wdt_dev);
337 if (rc) {
338 pr_err("Cannot register watchdog device (err=%d)\n", rc);
Guenter Roeck1123c512017-06-10 21:04:35 -0700339 return rc;
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700340 }
341
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700342 pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
343 chip_type, chip_rev, timeout, nowayout, testmode);
Oliver Schustere1fee942008-03-05 16:48:45 +0100344
345 return 0;
Oliver Schustere1fee942008-03-05 16:48:45 +0100346}
347
348static void __exit it87_wdt_exit(void)
349{
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700350 watchdog_unregister_device(&wdt_dev);
Oliver Schustere1fee942008-03-05 16:48:45 +0100351}
352
353module_init(it87_wdt_init);
354module_exit(it87_wdt_exit);
355
356MODULE_AUTHOR("Oliver Schuster");
357MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
358MODULE_LICENSE("GPL");