blob: bb11229093966db7f16a28531fecb83e1a32367a [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,
Hanspeter Portnerc1137392020-09-04 23:16:39 +020018 * IT8772, IT8783 and IT8784.
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
Hanspeter Portnerbeaabe02020-08-27 12:59:40 +020069#define IT8772_ID 0x8772
Paolo Tetif83918f2014-10-19 21:39:33 +020070#define IT8783_ID 0x8783
Hanspeter Portnerc1137392020-09-04 23:16:39 +020071#define IT8784_ID 0x8784
Vincent Prince6ae58ee2020-01-23 15:05:44 +010072#define IT8786_ID 0x8786
Oliver Schustere1fee942008-03-05 16:48:45 +010073
74/* GPIO Configuration Registers LDN=0x07 */
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +000075#define WDTCTRL 0x71
Oliver Schustere1fee942008-03-05 16:48:45 +010076#define WDTCFG 0x72
77#define WDTVALLSB 0x73
78#define WDTVALMSB 0x74
79
Oliver Schustere1fee942008-03-05 16:48:45 +010080/* GPIO Bits WDTCFG */
81#define WDT_TOV1 0x80
82#define WDT_KRST 0x40
83#define WDT_TOVE 0x20
Huaro Tomita4bc30272011-01-21 07:37:51 +090084#define WDT_PWROK 0x10 /* not in it8721 */
Oliver Schustere1fee942008-03-05 16:48:45 +010085#define WDT_INT_MASK 0x0f
86
Guenter Roeck893dc8b2017-06-10 21:04:34 -070087static unsigned int max_units, chip_type;
Oliver Schustere1fee942008-03-05 16:48:45 +010088
Guenter Roeck1d7b8032017-06-10 21:04:33 -070089static unsigned int timeout = DEFAULT_TIMEOUT;
Guenter Roeck893dc8b2017-06-10 21:04:34 -070090static int testmode = DEFAULT_TESTMODE;
91static bool nowayout = DEFAULT_NOWAYOUT;
Oliver Schustere1fee942008-03-05 16:48:45 +010092
Oliver Schustere1fee942008-03-05 16:48:45 +010093module_param(timeout, int, 0);
94MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
95 __MODULE_STRING(DEFAULT_TIMEOUT));
96module_param(testmode, int, 0);
97MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
98 __MODULE_STRING(DEFAULT_TESTMODE));
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010099module_param(nowayout, bool, 0);
Oliver Schustere1fee942008-03-05 16:48:45 +0100100MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
101 __MODULE_STRING(WATCHDOG_NOWAYOUT));
102
103/* Superio Chip */
104
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700105static inline int superio_enter(void)
Oliver Schustere1fee942008-03-05 16:48:45 +0100106{
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700107 /*
108 * Try to reserve REG and REG + 1 for exclusive access.
109 */
110 if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
111 return -EBUSY;
112
Oliver Schustere1fee942008-03-05 16:48:45 +0100113 outb(0x87, REG);
114 outb(0x01, REG);
115 outb(0x55, REG);
116 outb(0x55, REG);
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700117 return 0;
Oliver Schustere1fee942008-03-05 16:48:45 +0100118}
119
120static inline void superio_exit(void)
121{
122 outb(0x02, REG);
123 outb(0x02, VAL);
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700124 release_region(REG, 2);
Oliver Schustere1fee942008-03-05 16:48:45 +0100125}
126
127static inline void superio_select(int ldn)
128{
129 outb(LDNREG, REG);
130 outb(ldn, VAL);
131}
132
133static inline int superio_inb(int reg)
134{
135 outb(reg, REG);
136 return inb(VAL);
137}
138
139static inline void superio_outb(int val, int reg)
140{
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000141 outb(reg, REG);
142 outb(val, VAL);
Oliver Schustere1fee942008-03-05 16:48:45 +0100143}
144
145static inline int superio_inw(int reg)
146{
147 int val;
148 outb(reg++, REG);
149 val = inb(VAL) << 8;
150 outb(reg, REG);
151 val |= inb(VAL);
152 return val;
153}
154
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200155/* Internal function, should be called after superio_select(GPIO) */
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700156static void _wdt_update_timeout(unsigned int t)
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200157{
Huaro Tomita4bc30272011-01-21 07:37:51 +0900158 unsigned char cfg = WDT_KRST;
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200159
160 if (testmode)
161 cfg = 0;
162
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700163 if (t <= max_units)
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200164 cfg |= WDT_TOV1;
165 else
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700166 t /= 60;
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200167
Huaro Tomita4bc30272011-01-21 07:37:51 +0900168 if (chip_type != IT8721_ID)
169 cfg |= WDT_PWROK;
170
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200171 superio_outb(cfg, WDTCFG);
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700172 superio_outb(t, WDTVALLSB);
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200173 if (max_units > 255)
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700174 superio_outb(t >> 8, WDTVALMSB);
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200175}
176
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700177static int wdt_update_timeout(unsigned int t)
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700178{
179 int ret;
180
181 ret = superio_enter();
182 if (ret)
183 return ret;
184
185 superio_select(GPIO);
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700186 _wdt_update_timeout(t);
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700187 superio_exit();
188
189 return 0;
190}
191
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200192static int wdt_round_time(int t)
193{
194 t += 59;
195 t -= t % 60;
196 return t;
197}
198
Oliver Schustere1fee942008-03-05 16:48:45 +0100199/* watchdog timer handling */
200
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700201static int wdt_start(struct watchdog_device *wdd)
Oliver Schustere1fee942008-03-05 16:48:45 +0100202{
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700203 return wdt_update_timeout(wdd->timeout);
Oliver Schustere1fee942008-03-05 16:48:45 +0100204}
205
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700206static int wdt_stop(struct watchdog_device *wdd)
Oliver Schustere1fee942008-03-05 16:48:45 +0100207{
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700208 return wdt_update_timeout(0);
Oliver Schustere1fee942008-03-05 16:48:45 +0100209}
210
211/**
212 * wdt_set_timeout - set a new timeout value with watchdog ioctl
213 * @t: timeout value in seconds
214 *
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200215 * The hardware device has a 8 or 16 bit watchdog timer (depends on
216 * chip version) that can be configured to count seconds or minutes.
Oliver Schustere1fee942008-03-05 16:48:45 +0100217 *
218 * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
219 */
220
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700221static int wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
Oliver Schustere1fee942008-03-05 16:48:45 +0100222{
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700223 int ret = 0;
Oliver Schustere1fee942008-03-05 16:48:45 +0100224
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200225 if (t > max_units)
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700226 t = wdt_round_time(t);
Oliver Schustere1fee942008-03-05 16:48:45 +0100227
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700228 wdd->timeout = t;
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700229
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700230 if (watchdog_hw_running(wdd))
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700231 ret = wdt_update_timeout(t);
Oliver Schustere1fee942008-03-05 16:48:45 +0100232
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700233 return ret;
Oliver Schustere1fee942008-03-05 16:48:45 +0100234}
235
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000236static const struct watchdog_info ident = {
Oliver Schustere1fee942008-03-05 16:48:45 +0100237 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700238 .firmware_version = 1,
Oliver Schustere1fee942008-03-05 16:48:45 +0100239 .identity = WATCHDOG_NAME,
240};
241
Gustavo A. R. Silva2211a8d2017-07-07 19:23:21 -0500242static const struct watchdog_ops wdt_ops = {
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700243 .owner = THIS_MODULE,
244 .start = wdt_start,
245 .stop = wdt_stop,
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700246 .set_timeout = wdt_set_timeout,
247};
Oliver Schustere1fee942008-03-05 16:48:45 +0100248
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700249static struct watchdog_device wdt_dev = {
250 .info = &ident,
251 .ops = &wdt_ops,
252 .min_timeout = 1,
253};
Oliver Schustere1fee942008-03-05 16:48:45 +0100254
Oliver Schustere1fee942008-03-05 16:48:45 +0100255static int __init it87_wdt_init(void)
256{
Oliver Schustere1fee942008-03-05 16:48:45 +0100257 u8 chip_rev;
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700258 int rc;
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200259
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700260 rc = superio_enter();
261 if (rc)
262 return rc;
263
Oliver Schustere1fee942008-03-05 16:48:45 +0100264 chip_type = superio_inw(CHIPID);
265 chip_rev = superio_inb(CHIPREV) & 0x0f;
266 superio_exit();
Oliver Schustere1fee942008-03-05 16:48:45 +0100267
268 switch (chip_type) {
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200269 case IT8702_ID:
270 max_units = 255;
271 break;
272 case IT8712_ID:
273 max_units = (chip_rev < 8) ? 255 : 65535;
274 break;
Oliver Schustere1fee942008-03-05 16:48:45 +0100275 case IT8716_ID:
Oliver Schustere1fee942008-03-05 16:48:45 +0100276 case IT8726_ID:
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200277 max_units = 65535;
Oliver Schustere1fee942008-03-05 16:48:45 +0100278 break;
Guenter Roeckcddda072017-06-10 21:04:36 -0700279 case IT8607_ID:
Maciej S. Szmigiero06716122016-12-15 23:52:36 +0100280 case IT8620_ID:
Guenter Roeckcddda072017-06-10 21:04:36 -0700281 case IT8622_ID:
282 case IT8625_ID:
283 case IT8628_ID:
284 case IT8655_ID:
285 case IT8665_ID:
286 case IT8686_ID:
Ondrej Zajicekee3e9652010-09-14 02:47:28 +0200287 case IT8718_ID:
288 case IT8720_ID:
Huaro Tomita4bc30272011-01-21 07:37:51 +0900289 case IT8721_ID:
Diego Elio Pettenò198ca012012-03-14 20:49:04 +0100290 case IT8728_ID:
Hanspeter Portnerbeaabe02020-08-27 12:59:40 +0200291 case IT8772_ID:
Paolo Tetif83918f2014-10-19 21:39:33 +0200292 case IT8783_ID:
Hanspeter Portnerc1137392020-09-04 23:16:39 +0200293 case IT8784_ID:
Vincent Prince6ae58ee2020-01-23 15:05:44 +0100294 case IT8786_ID:
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200295 max_units = 65535;
Ondrej Zajicekee3e9652010-09-14 02:47:28 +0200296 break;
Oliver Schustere1fee942008-03-05 16:48:45 +0100297 case IT8705_ID:
Joe Perches27c766a2012-02-15 15:06:19 -0800298 pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
Oliver Schustere1fee942008-03-05 16:48:45 +0100299 chip_type, chip_rev);
300 return -ENODEV;
301 case NO_DEV_ID:
Joe Perches27c766a2012-02-15 15:06:19 -0800302 pr_err("no device\n");
Oliver Schustere1fee942008-03-05 16:48:45 +0100303 return -ENODEV;
304 default:
Joe Perches27c766a2012-02-15 15:06:19 -0800305 pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
Oliver Schustere1fee942008-03-05 16:48:45 +0100306 chip_type, chip_rev);
307 return -ENODEV;
308 }
309
Nat Gurumoorthya134b822011-05-09 11:45:07 -0700310 rc = superio_enter();
311 if (rc)
312 return rc;
Oliver Schustere1fee942008-03-05 16:48:45 +0100313
314 superio_select(GPIO);
315 superio_outb(WDT_TOV1, WDTCFG);
316 superio_outb(0x00, WDTCTRL);
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700317 superio_exit();
Oliver Schustere1fee942008-03-05 16:48:45 +0100318
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200319 if (timeout < 1 || timeout > max_units * 60) {
Oliver Schustere1fee942008-03-05 16:48:45 +0100320 timeout = DEFAULT_TIMEOUT;
Joe Perches27c766a2012-02-15 15:06:19 -0800321 pr_warn("Timeout value out of range, use default %d sec\n",
322 DEFAULT_TIMEOUT);
Oliver Schustere1fee942008-03-05 16:48:45 +0100323 }
324
Ondrej Zajicekdfb0b8e2010-09-14 02:54:16 +0200325 if (timeout > max_units)
326 timeout = wdt_round_time(timeout);
327
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700328 wdt_dev.timeout = timeout;
329 wdt_dev.max_timeout = max_units * 60;
330
Guenter Roeck1123c512017-06-10 21:04:35 -0700331 watchdog_stop_on_reboot(&wdt_dev);
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700332 rc = watchdog_register_device(&wdt_dev);
333 if (rc) {
334 pr_err("Cannot register watchdog device (err=%d)\n", rc);
Guenter Roeck1123c512017-06-10 21:04:35 -0700335 return rc;
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700336 }
337
Guenter Roeck893dc8b2017-06-10 21:04:34 -0700338 pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
339 chip_type, chip_rev, timeout, nowayout, testmode);
Oliver Schustere1fee942008-03-05 16:48:45 +0100340
341 return 0;
Oliver Schustere1fee942008-03-05 16:48:45 +0100342}
343
344static void __exit it87_wdt_exit(void)
345{
Guenter Roeck1d7b8032017-06-10 21:04:33 -0700346 watchdog_unregister_device(&wdt_dev);
Oliver Schustere1fee942008-03-05 16:48:45 +0100347}
348
349module_init(it87_wdt_init);
350module_exit(it87_wdt_exit);
351
352MODULE_AUTHOR("Oliver Schuster");
353MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
354MODULE_LICENSE("GPL");