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, IT8659, 17 * IT8665, IT8686, IT8702, IT8712, IT8716, IT8718, IT8720, IT8721, 18 * IT8726, IT8728, IT8772, IT8783, IT8784 and IT8786. 19 */ 20 21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22 23 #include <linux/bits.h> 24 #include <linux/dmi.h> 25 #include <linux/errno.h> 26 #include <linux/init.h> 27 #include <linux/io.h> 28 #include <linux/ioport.h> 29 #include <linux/module.h> 30 #include <linux/moduleparam.h> 31 #include <linux/printk.h> 32 #include <linux/types.h> 33 #include <linux/watchdog.h> 34 35 #define WATCHDOG_NAME "IT87 WDT" 36 37 /* Defaults for Module Parameter */ 38 #define DEFAULT_TIMEOUT 60 39 #define DEFAULT_TESTMODE 0 40 #define DEFAULT_NOWAYOUT WATCHDOG_NOWAYOUT 41 42 /* IO Ports */ 43 #define REG 0x2e 44 #define VAL 0x2f 45 46 /* Logical device Numbers LDN */ 47 #define EC 0x04 48 #define GPIO 0x07 49 50 /* Configuration Registers and Functions */ 51 #define LDNREG 0x07 52 #define CHIPID 0x20 53 #define CHIPREV 0x22 54 55 /* Chip Id numbers */ 56 #define NO_DEV_ID 0xffff 57 #define IT8607_ID 0x8607 58 #define IT8613_ID 0x8613 59 #define IT8620_ID 0x8620 60 #define IT8622_ID 0x8622 61 #define IT8625_ID 0x8625 62 #define IT8628_ID 0x8628 63 #define IT8655_ID 0x8655 64 #define IT8659_ID 0x8659 65 #define IT8665_ID 0x8665 66 #define IT8686_ID 0x8686 67 #define IT8702_ID 0x8702 68 #define IT8705_ID 0x8705 69 #define IT8712_ID 0x8712 70 #define IT8716_ID 0x8716 71 #define IT8718_ID 0x8718 72 #define IT8720_ID 0x8720 73 #define IT8721_ID 0x8721 74 #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */ 75 #define IT8728_ID 0x8728 76 #define IT8772_ID 0x8772 77 #define IT8783_ID 0x8783 78 #define IT8784_ID 0x8784 79 #define IT8786_ID 0x8786 80 81 /* Environment Controller Configuration Registers LDN=0x04 */ 82 #define SCR1 0xfa 83 84 /* Environment Controller Bits SCR1 */ 85 #define WDT_PWRGD 0x20 86 87 /* GPIO Configuration Registers LDN=0x07 */ 88 #define WDTCTRL 0x71 89 #define WDTCFG 0x72 90 #define WDTVALLSB 0x73 91 #define WDTVALMSB 0x74 92 93 /* GPIO Bits WDTCFG */ 94 #define WDT_TOV1 0x80 95 #define WDT_KRST 0x40 96 #define WDT_TOVE 0x20 97 #define WDT_PWROK 0x10 /* not in it8721 */ 98 #define WDT_INT_MASK 0x0f 99 100 static unsigned int max_units, chip_type; 101 102 static unsigned int timeout = DEFAULT_TIMEOUT; 103 static int testmode = DEFAULT_TESTMODE; 104 static bool nowayout = DEFAULT_NOWAYOUT; 105 106 module_param(timeout, int, 0); 107 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default=" 108 __MODULE_STRING(DEFAULT_TIMEOUT)); 109 module_param(testmode, int, 0); 110 MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default=" 111 __MODULE_STRING(DEFAULT_TESTMODE)); 112 module_param(nowayout, bool, 0); 113 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default=" 114 __MODULE_STRING(WATCHDOG_NOWAYOUT)); 115 116 /* Superio Chip */ 117 118 static inline int superio_enter(void) 119 { 120 /* 121 * Try to reserve REG and REG + 1 for exclusive access. 122 */ 123 if (!request_muxed_region(REG, 2, WATCHDOG_NAME)) 124 return -EBUSY; 125 126 outb(0x87, REG); 127 outb(0x01, REG); 128 outb(0x55, REG); 129 outb(0x55, REG); 130 return 0; 131 } 132 133 static inline void superio_exit(void) 134 { 135 outb(0x02, REG); 136 outb(0x02, VAL); 137 release_region(REG, 2); 138 } 139 140 static inline void superio_select(int ldn) 141 { 142 outb(LDNREG, REG); 143 outb(ldn, VAL); 144 } 145 146 static inline int superio_inb(int reg) 147 { 148 outb(reg, REG); 149 return inb(VAL); 150 } 151 152 static inline void superio_outb(int val, int reg) 153 { 154 outb(reg, REG); 155 outb(val, VAL); 156 } 157 158 static inline int superio_inw(int reg) 159 { 160 int val; 161 162 outb(reg++, REG); 163 val = inb(VAL) << 8; 164 outb(reg, REG); 165 val |= inb(VAL); 166 return val; 167 } 168 169 /* Internal function, should be called after superio_select(GPIO) */ 170 static void _wdt_update_timeout(unsigned int t) 171 { 172 unsigned char cfg = WDT_KRST; 173 174 if (testmode) 175 cfg = 0; 176 177 if (t <= max_units) 178 cfg |= WDT_TOV1; 179 else 180 t /= 60; 181 182 if (chip_type != IT8721_ID) 183 cfg |= WDT_PWROK; 184 185 superio_outb(cfg, WDTCFG); 186 superio_outb(t, WDTVALLSB); 187 if (max_units > 255) 188 superio_outb(t >> 8, WDTVALMSB); 189 } 190 191 /* Internal function, should be called after superio_select(GPIO) */ 192 static bool _wdt_running(void) 193 { 194 return superio_inb(WDTVALLSB) || (max_units > 255 && superio_inb(WDTVALMSB)); 195 } 196 197 static int wdt_update_timeout(unsigned int t) 198 { 199 int ret; 200 201 ret = superio_enter(); 202 if (ret) 203 return ret; 204 205 superio_select(GPIO); 206 _wdt_update_timeout(t); 207 superio_exit(); 208 209 return 0; 210 } 211 212 static int wdt_round_time(int t) 213 { 214 t += 59; 215 t -= t % 60; 216 return t; 217 } 218 219 /* watchdog timer handling */ 220 221 static int wdt_start(struct watchdog_device *wdd) 222 { 223 return wdt_update_timeout(wdd->timeout); 224 } 225 226 static int wdt_stop(struct watchdog_device *wdd) 227 { 228 return wdt_update_timeout(0); 229 } 230 231 /** 232 * wdt_set_timeout - set a new timeout value with watchdog ioctl 233 * @wdd: pointer to the watchdog_device structure 234 * @t: timeout value in seconds 235 * 236 * The hardware device has a 8 or 16 bit watchdog timer (depends on 237 * chip version) that can be configured to count seconds or minutes. 238 * 239 * Used within WDIOC_SETTIMEOUT watchdog device ioctl. 240 * 241 * Return: 0 if the timeout was set successfully, or a negative error code on 242 * failure. 243 */ 244 245 static int wdt_set_timeout(struct watchdog_device *wdd, unsigned int t) 246 { 247 int ret = 0; 248 249 if (t > max_units) 250 t = wdt_round_time(t); 251 252 wdd->timeout = t; 253 254 if (watchdog_hw_running(wdd)) 255 ret = wdt_update_timeout(t); 256 257 return ret; 258 } 259 260 enum { 261 IT87_WDT_OUTPUT_THROUGH_PWRGD = BIT(0), 262 }; 263 264 static const struct dmi_system_id it87_quirks[] = { 265 { 266 /* Qotom Q30900P (IT8786) */ 267 .matches = { 268 DMI_EXACT_MATCH(DMI_BOARD_NAME, "QCML04"), 269 }, 270 .driver_data = (void *)IT87_WDT_OUTPUT_THROUGH_PWRGD, 271 }, 272 {} 273 }; 274 275 static const struct watchdog_info ident = { 276 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, 277 .firmware_version = 1, 278 .identity = WATCHDOG_NAME, 279 }; 280 281 static const struct watchdog_ops wdt_ops = { 282 .owner = THIS_MODULE, 283 .start = wdt_start, 284 .stop = wdt_stop, 285 .set_timeout = wdt_set_timeout, 286 }; 287 288 static struct watchdog_device wdt_dev = { 289 .info = &ident, 290 .ops = &wdt_ops, 291 .min_timeout = 1, 292 }; 293 294 static int __init it87_wdt_init(void) 295 { 296 const struct dmi_system_id *dmi_id; 297 u8 chip_rev; 298 u8 ctrl; 299 int quirks = 0; 300 int rc; 301 302 rc = superio_enter(); 303 if (rc) 304 return rc; 305 306 chip_type = superio_inw(CHIPID); 307 chip_rev = superio_inb(CHIPREV) & 0x0f; 308 superio_exit(); 309 310 dmi_id = dmi_first_match(it87_quirks); 311 if (dmi_id) 312 quirks = (long)dmi_id->driver_data; 313 314 switch (chip_type) { 315 case IT8702_ID: 316 max_units = 255; 317 break; 318 case IT8712_ID: 319 max_units = (chip_rev < 8) ? 255 : 65535; 320 break; 321 case IT8607_ID: 322 case IT8613_ID: 323 case IT8620_ID: 324 case IT8622_ID: 325 case IT8625_ID: 326 case IT8628_ID: 327 case IT8655_ID: 328 case IT8659_ID: 329 case IT8665_ID: 330 case IT8686_ID: 331 case IT8716_ID: 332 case IT8718_ID: 333 case IT8720_ID: 334 case IT8721_ID: 335 case IT8726_ID: 336 case IT8728_ID: 337 case IT8772_ID: 338 case IT8783_ID: 339 case IT8784_ID: 340 case IT8786_ID: 341 max_units = 65535; 342 break; 343 case IT8705_ID: 344 pr_err("Unsupported Chip found, Chip %04x Revision %02x\n", 345 chip_type, chip_rev); 346 return -ENODEV; 347 case NO_DEV_ID: 348 pr_err("no device\n"); 349 return -ENODEV; 350 default: 351 pr_err("Unknown Chip found, Chip %04x Revision %04x\n", 352 chip_type, chip_rev); 353 return -ENODEV; 354 } 355 356 rc = superio_enter(); 357 if (rc) 358 return rc; 359 360 superio_select(GPIO); 361 superio_outb(WDT_TOV1, WDTCFG); 362 363 switch (chip_type) { 364 case IT8784_ID: 365 case IT8786_ID: 366 ctrl = superio_inb(WDTCTRL); 367 ctrl &= 0x08; 368 superio_outb(ctrl, WDTCTRL); 369 break; 370 default: 371 superio_outb(0x00, WDTCTRL); 372 } 373 374 if (quirks & IT87_WDT_OUTPUT_THROUGH_PWRGD) { 375 superio_select(EC); 376 ctrl = superio_inb(SCR1); 377 if (!(ctrl & WDT_PWRGD)) { 378 ctrl |= WDT_PWRGD; 379 superio_outb(ctrl, SCR1); 380 } 381 } 382 383 /* wdt already left running by firmware? */ 384 if (_wdt_running()) { 385 pr_info("Left running by firmware.\n"); 386 set_bit(WDOG_HW_RUNNING, &wdt_dev.status); 387 } 388 389 superio_exit(); 390 391 if (timeout < 1 || timeout > max_units * 60) { 392 timeout = DEFAULT_TIMEOUT; 393 pr_warn("Timeout value out of range, use default %d sec\n", 394 DEFAULT_TIMEOUT); 395 } 396 397 if (timeout > max_units) 398 timeout = wdt_round_time(timeout); 399 400 wdt_dev.timeout = timeout; 401 wdt_dev.max_timeout = max_units * 60; 402 403 watchdog_stop_on_reboot(&wdt_dev); 404 rc = watchdog_register_device(&wdt_dev); 405 if (rc) 406 return rc; 407 408 pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n", 409 chip_type, chip_rev, timeout, nowayout, testmode); 410 411 return 0; 412 } 413 414 static void __exit it87_wdt_exit(void) 415 { 416 watchdog_unregister_device(&wdt_dev); 417 } 418 419 module_init(it87_wdt_init); 420 module_exit(it87_wdt_exit); 421 422 MODULE_AUTHOR("Oliver Schuster"); 423 MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O"); 424 MODULE_LICENSE("GPL"); 425