it8712f_wdt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IT8712F "Smart Guardian" Watchdog support
  4. *
  5. * Copyright (c) 2006-2007 Jorge Boncompte - DTI2 <jorge@dti2.net>
  6. *
  7. * Based on info and code taken from:
  8. *
  9. * drivers/char/watchdog/scx200_wdt.c
  10. * drivers/hwmon/it87.c
  11. * IT8712F EC-LPC I/O Preliminary Specification 0.8.2
  12. * IT8712F EC-LPC I/O Preliminary Specification 0.9.3
  13. *
  14. * The author(s) of this software shall not be held liable for damages
  15. * of any nature resulting due to the use of this software. This
  16. * software is provided AS-IS with no warranties.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/init.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/notifier.h>
  25. #include <linux/reboot.h>
  26. #include <linux/fs.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/io.h>
  30. #include <linux/ioport.h>
  31. #define DEBUG
  32. #define NAME "it8712f_wdt"
  33. MODULE_AUTHOR("Jorge Boncompte - DTI2 <jorge@dti2.net>");
  34. MODULE_DESCRIPTION("IT8712F Watchdog Driver");
  35. MODULE_LICENSE("GPL");
  36. static int max_units = 255;
  37. static int margin = 60; /* in seconds */
  38. module_param(margin, int, 0);
  39. MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
  40. static bool nowayout = WATCHDOG_NOWAYOUT;
  41. module_param(nowayout, bool, 0);
  42. MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
  43. static unsigned long wdt_open;
  44. static unsigned expect_close;
  45. static unsigned char revision;
  46. /* Dog Food address - We use the game port address */
  47. static unsigned short address;
  48. #define REG 0x2e /* The register to read/write */
  49. #define VAL 0x2f /* The value to read/write */
  50. #define LDN 0x07 /* Register: Logical device select */
  51. #define DEVID 0x20 /* Register: Device ID */
  52. #define DEVREV 0x22 /* Register: Device Revision */
  53. #define ACT_REG 0x30 /* LDN Register: Activation */
  54. #define BASE_REG 0x60 /* LDN Register: Base address */
  55. #define IT8712F_DEVID 0x8712
  56. #define LDN_GPIO 0x07 /* GPIO and Watch Dog Timer */
  57. #define LDN_GAME 0x09 /* Game Port */
  58. #define WDT_CONTROL 0x71 /* WDT Register: Control */
  59. #define WDT_CONFIG 0x72 /* WDT Register: Configuration */
  60. #define WDT_TIMEOUT 0x73 /* WDT Register: Timeout Value */
  61. #define WDT_RESET_GAME 0x10 /* Reset timer on read or write to game port */
  62. #define WDT_RESET_KBD 0x20 /* Reset timer on keyboard interrupt */
  63. #define WDT_RESET_MOUSE 0x40 /* Reset timer on mouse interrupt */
  64. #define WDT_RESET_CIR 0x80 /* Reset timer on consumer IR interrupt */
  65. #define WDT_UNIT_SEC 0x80 /* If 0 in MINUTES */
  66. #define WDT_OUT_PWROK 0x10 /* Pulse PWROK on timeout */
  67. #define WDT_OUT_KRST 0x40 /* Pulse reset on timeout */
  68. static int wdt_control_reg = WDT_RESET_GAME;
  69. module_param(wdt_control_reg, int, 0);
  70. MODULE_PARM_DESC(wdt_control_reg, "Value to write to watchdog control "
  71. "register. The default WDT_RESET_GAME resets the timer on "
  72. "game port reads that this driver generates. You can also "
  73. "use KBD, MOUSE or CIR if you have some external way to "
  74. "generate those interrupts.");
  75. static int superio_inb(int reg)
  76. {
  77. outb(reg, REG);
  78. return inb(VAL);
  79. }
  80. static void superio_outb(int val, int reg)
  81. {
  82. outb(reg, REG);
  83. outb(val, VAL);
  84. }
  85. static int superio_inw(int reg)
  86. {
  87. int val;
  88. outb(reg++, REG);
  89. val = inb(VAL) << 8;
  90. outb(reg, REG);
  91. val |= inb(VAL);
  92. return val;
  93. }
  94. static inline void superio_select(int ldn)
  95. {
  96. outb(LDN, REG);
  97. outb(ldn, VAL);
  98. }
  99. static inline int superio_enter(void)
  100. {
  101. /*
  102. * Try to reserve REG and REG + 1 for exclusive access.
  103. */
  104. if (!request_muxed_region(REG, 2, NAME))
  105. return -EBUSY;
  106. outb(0x87, REG);
  107. outb(0x01, REG);
  108. outb(0x55, REG);
  109. outb(0x55, REG);
  110. return 0;
  111. }
  112. static inline void superio_exit(void)
  113. {
  114. outb(0x02, REG);
  115. outb(0x02, VAL);
  116. release_region(REG, 2);
  117. }
  118. static inline void it8712f_wdt_ping(void)
  119. {
  120. if (wdt_control_reg & WDT_RESET_GAME)
  121. inb(address);
  122. }
  123. static void it8712f_wdt_update_margin(void)
  124. {
  125. int config = WDT_OUT_KRST | WDT_OUT_PWROK;
  126. int units = margin;
  127. /* Switch to minutes precision if the configured margin
  128. * value does not fit within the register width.
  129. */
  130. if (units <= max_units) {
  131. config |= WDT_UNIT_SEC; /* else UNIT is MINUTES */
  132. pr_info("timer margin %d seconds\n", units);
  133. } else {
  134. units /= 60;
  135. pr_info("timer margin %d minutes\n", units);
  136. }
  137. superio_outb(config, WDT_CONFIG);
  138. if (revision >= 0x08)
  139. superio_outb(units >> 8, WDT_TIMEOUT + 1);
  140. superio_outb(units, WDT_TIMEOUT);
  141. }
  142. static int it8712f_wdt_get_status(void)
  143. {
  144. if (superio_inb(WDT_CONTROL) & 0x01)
  145. return WDIOF_CARDRESET;
  146. else
  147. return 0;
  148. }
  149. static int it8712f_wdt_enable(void)
  150. {
  151. int ret = superio_enter();
  152. if (ret)
  153. return ret;
  154. pr_debug("enabling watchdog timer\n");
  155. superio_select(LDN_GPIO);
  156. superio_outb(wdt_control_reg, WDT_CONTROL);
  157. it8712f_wdt_update_margin();
  158. superio_exit();
  159. it8712f_wdt_ping();
  160. return 0;
  161. }
  162. static int it8712f_wdt_disable(void)
  163. {
  164. int ret = superio_enter();
  165. if (ret)
  166. return ret;
  167. pr_debug("disabling watchdog timer\n");
  168. superio_select(LDN_GPIO);
  169. superio_outb(0, WDT_CONFIG);
  170. superio_outb(0, WDT_CONTROL);
  171. if (revision >= 0x08)
  172. superio_outb(0, WDT_TIMEOUT + 1);
  173. superio_outb(0, WDT_TIMEOUT);
  174. superio_exit();
  175. return 0;
  176. }
  177. static int it8712f_wdt_notify(struct notifier_block *this,
  178. unsigned long code, void *unused)
  179. {
  180. if (code == SYS_HALT || code == SYS_POWER_OFF)
  181. if (!nowayout)
  182. it8712f_wdt_disable();
  183. return NOTIFY_DONE;
  184. }
  185. static struct notifier_block it8712f_wdt_notifier = {
  186. .notifier_call = it8712f_wdt_notify,
  187. };
  188. static ssize_t it8712f_wdt_write(struct file *file, const char __user *data,
  189. size_t len, loff_t *ppos)
  190. {
  191. /* check for a magic close character */
  192. if (len) {
  193. size_t i;
  194. it8712f_wdt_ping();
  195. expect_close = 0;
  196. for (i = 0; i < len; ++i) {
  197. char c;
  198. if (get_user(c, data + i))
  199. return -EFAULT;
  200. if (c == 'V')
  201. expect_close = 42;
  202. }
  203. }
  204. return len;
  205. }
  206. static long it8712f_wdt_ioctl(struct file *file, unsigned int cmd,
  207. unsigned long arg)
  208. {
  209. void __user *argp = (void __user *)arg;
  210. int __user *p = argp;
  211. static const struct watchdog_info ident = {
  212. .identity = "IT8712F Watchdog",
  213. .firmware_version = 1,
  214. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  215. WDIOF_MAGICCLOSE,
  216. };
  217. int value;
  218. int ret;
  219. switch (cmd) {
  220. case WDIOC_GETSUPPORT:
  221. if (copy_to_user(argp, &ident, sizeof(ident)))
  222. return -EFAULT;
  223. return 0;
  224. case WDIOC_GETSTATUS:
  225. ret = superio_enter();
  226. if (ret)
  227. return ret;
  228. superio_select(LDN_GPIO);
  229. value = it8712f_wdt_get_status();
  230. superio_exit();
  231. return put_user(value, p);
  232. case WDIOC_GETBOOTSTATUS:
  233. return put_user(0, p);
  234. case WDIOC_KEEPALIVE:
  235. it8712f_wdt_ping();
  236. return 0;
  237. case WDIOC_SETTIMEOUT:
  238. if (get_user(value, p))
  239. return -EFAULT;
  240. if (value < 1)
  241. return -EINVAL;
  242. if (value > (max_units * 60))
  243. return -EINVAL;
  244. margin = value;
  245. ret = superio_enter();
  246. if (ret)
  247. return ret;
  248. superio_select(LDN_GPIO);
  249. it8712f_wdt_update_margin();
  250. superio_exit();
  251. it8712f_wdt_ping();
  252. /* Fall through */
  253. case WDIOC_GETTIMEOUT:
  254. if (put_user(margin, p))
  255. return -EFAULT;
  256. return 0;
  257. default:
  258. return -ENOTTY;
  259. }
  260. }
  261. static int it8712f_wdt_open(struct inode *inode, struct file *file)
  262. {
  263. int ret;
  264. /* only allow one at a time */
  265. if (test_and_set_bit(0, &wdt_open))
  266. return -EBUSY;
  267. ret = it8712f_wdt_enable();
  268. if (ret)
  269. return ret;
  270. return stream_open(inode, file);
  271. }
  272. static int it8712f_wdt_release(struct inode *inode, struct file *file)
  273. {
  274. if (expect_close != 42) {
  275. pr_warn("watchdog device closed unexpectedly, will not disable the watchdog timer\n");
  276. } else if (!nowayout) {
  277. if (it8712f_wdt_disable())
  278. pr_warn("Watchdog disable failed\n");
  279. }
  280. expect_close = 0;
  281. clear_bit(0, &wdt_open);
  282. return 0;
  283. }
  284. static const struct file_operations it8712f_wdt_fops = {
  285. .owner = THIS_MODULE,
  286. .llseek = no_llseek,
  287. .write = it8712f_wdt_write,
  288. .unlocked_ioctl = it8712f_wdt_ioctl,
  289. .open = it8712f_wdt_open,
  290. .release = it8712f_wdt_release,
  291. };
  292. static struct miscdevice it8712f_wdt_miscdev = {
  293. .minor = WATCHDOG_MINOR,
  294. .name = "watchdog",
  295. .fops = &it8712f_wdt_fops,
  296. };
  297. static int __init it8712f_wdt_find(unsigned short *address)
  298. {
  299. int err = -ENODEV;
  300. int chip_type;
  301. int ret = superio_enter();
  302. if (ret)
  303. return ret;
  304. chip_type = superio_inw(DEVID);
  305. if (chip_type != IT8712F_DEVID)
  306. goto exit;
  307. superio_select(LDN_GAME);
  308. superio_outb(1, ACT_REG);
  309. if (!(superio_inb(ACT_REG) & 0x01)) {
  310. pr_err("Device not activated, skipping\n");
  311. goto exit;
  312. }
  313. *address = superio_inw(BASE_REG);
  314. if (*address == 0) {
  315. pr_err("Base address not set, skipping\n");
  316. goto exit;
  317. }
  318. err = 0;
  319. revision = superio_inb(DEVREV) & 0x0f;
  320. /* Later revisions have 16-bit values per datasheet 0.9.1 */
  321. if (revision >= 0x08)
  322. max_units = 65535;
  323. if (margin > (max_units * 60))
  324. margin = (max_units * 60);
  325. pr_info("Found IT%04xF chip revision %d - using DogFood address 0x%x\n",
  326. chip_type, revision, *address);
  327. exit:
  328. superio_exit();
  329. return err;
  330. }
  331. static int __init it8712f_wdt_init(void)
  332. {
  333. int err = 0;
  334. if (it8712f_wdt_find(&address))
  335. return -ENODEV;
  336. if (!request_region(address, 1, "IT8712F Watchdog")) {
  337. pr_warn("watchdog I/O region busy\n");
  338. return -EBUSY;
  339. }
  340. err = it8712f_wdt_disable();
  341. if (err) {
  342. pr_err("unable to disable watchdog timer\n");
  343. goto out;
  344. }
  345. err = register_reboot_notifier(&it8712f_wdt_notifier);
  346. if (err) {
  347. pr_err("unable to register reboot notifier\n");
  348. goto out;
  349. }
  350. err = misc_register(&it8712f_wdt_miscdev);
  351. if (err) {
  352. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  353. WATCHDOG_MINOR, err);
  354. goto reboot_out;
  355. }
  356. return 0;
  357. reboot_out:
  358. unregister_reboot_notifier(&it8712f_wdt_notifier);
  359. out:
  360. release_region(address, 1);
  361. return err;
  362. }
  363. static void __exit it8712f_wdt_exit(void)
  364. {
  365. misc_deregister(&it8712f_wdt_miscdev);
  366. unregister_reboot_notifier(&it8712f_wdt_notifier);
  367. release_region(address, 1);
  368. }
  369. module_init(it8712f_wdt_init);
  370. module_exit(it8712f_wdt_exit);