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