it87_wdt.c 8.8 KB

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