w83877f_wdt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * W83877F Computer Watchdog Timer driver
  4. *
  5. * Based on acquirewdt.c by Alan Cox,
  6. * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
  7. *
  8. * The authors do NOT admit liability nor provide warranty for
  9. * any of this software. This material is provided "AS-IS" in
  10. * the hope that it may be useful for others.
  11. *
  12. * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net>
  13. *
  14. * 4/19 - 2001 [Initial revision]
  15. * 9/27 - 2001 Added spinlocking
  16. * 4/12 - 2002 [rob@osinvestor.com] Eliminate extra comments
  17. * Eliminate fop_read
  18. * Eliminate extra spin_unlock
  19. * Added KERN_* tags to printks
  20. * add CONFIG_WATCHDOG_NOWAYOUT support
  21. * fix possible wdt_is_open race
  22. * changed watchdog_info to correctly reflect what
  23. * the driver offers
  24. * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
  25. * WDIOC_SETTIMEOUT,
  26. * WDIOC_GETTIMEOUT, and WDIOC_SETOPTIONS ioctls
  27. * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
  28. * added extra printk's for startup problems
  29. * use module_param
  30. * made timeout (the emulated heartbeat) a
  31. * module_param
  32. * made the keepalive ping an internal subroutine
  33. *
  34. * This WDT driver is different from most other Linux WDT
  35. * drivers in that the driver will ping the watchdog by itself,
  36. * because this particular WDT has a very short timeout (1.6
  37. * seconds) and it would be insane to count on any userspace
  38. * daemon always getting scheduled within that time frame.
  39. */
  40. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  41. #include <linux/module.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/types.h>
  44. #include <linux/timer.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/miscdevice.h>
  47. #include <linux/watchdog.h>
  48. #include <linux/fs.h>
  49. #include <linux/ioport.h>
  50. #include <linux/notifier.h>
  51. #include <linux/reboot.h>
  52. #include <linux/init.h>
  53. #include <linux/io.h>
  54. #include <linux/uaccess.h>
  55. #define OUR_NAME "w83877f_wdt"
  56. #define ENABLE_W83877F_PORT 0x3F0
  57. #define ENABLE_W83877F 0x87
  58. #define DISABLE_W83877F 0xAA
  59. #define WDT_PING 0x443
  60. #define WDT_REGISTER 0x14
  61. #define WDT_ENABLE 0x9C
  62. #define WDT_DISABLE 0x8C
  63. /*
  64. * The W83877F seems to be fixed at 1.6s timeout (at least on the
  65. * EMACS PC-104 board I'm using). If we reset the watchdog every
  66. * ~250ms we should be safe. */
  67. #define WDT_INTERVAL (HZ/4+1)
  68. /*
  69. * We must not require too good response from the userspace daemon.
  70. * Here we require the userspace daemon to send us a heartbeat
  71. * char to /dev/watchdog every 30 seconds.
  72. */
  73. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  74. /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
  75. static int timeout = WATCHDOG_TIMEOUT;
  76. module_param(timeout, int, 0);
  77. MODULE_PARM_DESC(timeout,
  78. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  79. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  80. static bool nowayout = WATCHDOG_NOWAYOUT;
  81. module_param(nowayout, bool, 0);
  82. MODULE_PARM_DESC(nowayout,
  83. "Watchdog cannot be stopped once started (default="
  84. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  85. static void wdt_timer_ping(struct timer_list *);
  86. static DEFINE_TIMER(timer, wdt_timer_ping);
  87. static unsigned long next_heartbeat;
  88. static unsigned long wdt_is_open;
  89. static char wdt_expect_close;
  90. static DEFINE_SPINLOCK(wdt_spinlock);
  91. /*
  92. * Whack the dog
  93. */
  94. static void wdt_timer_ping(struct timer_list *unused)
  95. {
  96. /* If we got a heartbeat pulse within the WDT_US_INTERVAL
  97. * we agree to ping the WDT
  98. */
  99. if (time_before(jiffies, next_heartbeat)) {
  100. /* Ping the WDT */
  101. spin_lock(&wdt_spinlock);
  102. /* Ping the WDT by reading from WDT_PING */
  103. inb_p(WDT_PING);
  104. /* Re-set the timer interval */
  105. mod_timer(&timer, jiffies + WDT_INTERVAL);
  106. spin_unlock(&wdt_spinlock);
  107. } else
  108. pr_warn("Heartbeat lost! Will not ping the watchdog\n");
  109. }
  110. /*
  111. * Utility routines
  112. */
  113. static void wdt_change(int writeval)
  114. {
  115. unsigned long flags;
  116. spin_lock_irqsave(&wdt_spinlock, flags);
  117. /* buy some time */
  118. inb_p(WDT_PING);
  119. /* make W83877F available */
  120. outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
  121. outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
  122. /* enable watchdog */
  123. outb_p(WDT_REGISTER, ENABLE_W83877F_PORT);
  124. outb_p(writeval, ENABLE_W83877F_PORT+1);
  125. /* lock the W8387FF away */
  126. outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
  127. spin_unlock_irqrestore(&wdt_spinlock, flags);
  128. }
  129. static void wdt_startup(void)
  130. {
  131. next_heartbeat = jiffies + (timeout * HZ);
  132. /* Start the timer */
  133. mod_timer(&timer, jiffies + WDT_INTERVAL);
  134. wdt_change(WDT_ENABLE);
  135. pr_info("Watchdog timer is now enabled\n");
  136. }
  137. static void wdt_turnoff(void)
  138. {
  139. /* Stop the timer */
  140. del_timer_sync(&timer);
  141. wdt_change(WDT_DISABLE);
  142. pr_info("Watchdog timer is now disabled...\n");
  143. }
  144. static void wdt_keepalive(void)
  145. {
  146. /* user land ping */
  147. next_heartbeat = jiffies + (timeout * HZ);
  148. }
  149. /*
  150. * /dev/watchdog handling
  151. */
  152. static ssize_t fop_write(struct file *file, const char __user *buf,
  153. size_t count, loff_t *ppos)
  154. {
  155. /* See if we got the magic character 'V' and reload the timer */
  156. if (count) {
  157. if (!nowayout) {
  158. size_t ofs;
  159. /* note: just in case someone wrote the magic
  160. character five months ago... */
  161. wdt_expect_close = 0;
  162. /* scan to see whether or not we got the
  163. magic character */
  164. for (ofs = 0; ofs != count; ofs++) {
  165. char c;
  166. if (get_user(c, buf + ofs))
  167. return -EFAULT;
  168. if (c == 'V')
  169. wdt_expect_close = 42;
  170. }
  171. }
  172. /* someone wrote to us, we should restart timer */
  173. wdt_keepalive();
  174. }
  175. return count;
  176. }
  177. static int fop_open(struct inode *inode, struct file *file)
  178. {
  179. /* Just in case we're already talking to someone... */
  180. if (test_and_set_bit(0, &wdt_is_open))
  181. return -EBUSY;
  182. /* Good, fire up the show */
  183. wdt_startup();
  184. return stream_open(inode, file);
  185. }
  186. static int fop_close(struct inode *inode, struct file *file)
  187. {
  188. if (wdt_expect_close == 42)
  189. wdt_turnoff();
  190. else {
  191. del_timer(&timer);
  192. pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
  193. }
  194. clear_bit(0, &wdt_is_open);
  195. wdt_expect_close = 0;
  196. return 0;
  197. }
  198. static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  199. {
  200. void __user *argp = (void __user *)arg;
  201. int __user *p = argp;
  202. static const struct watchdog_info ident = {
  203. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
  204. | WDIOF_MAGICCLOSE,
  205. .firmware_version = 1,
  206. .identity = "W83877F",
  207. };
  208. switch (cmd) {
  209. case WDIOC_GETSUPPORT:
  210. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  211. case WDIOC_GETSTATUS:
  212. case WDIOC_GETBOOTSTATUS:
  213. return put_user(0, p);
  214. case WDIOC_SETOPTIONS:
  215. {
  216. int new_options, retval = -EINVAL;
  217. if (get_user(new_options, p))
  218. return -EFAULT;
  219. if (new_options & WDIOS_DISABLECARD) {
  220. wdt_turnoff();
  221. retval = 0;
  222. }
  223. if (new_options & WDIOS_ENABLECARD) {
  224. wdt_startup();
  225. retval = 0;
  226. }
  227. return retval;
  228. }
  229. case WDIOC_KEEPALIVE:
  230. wdt_keepalive();
  231. return 0;
  232. case WDIOC_SETTIMEOUT:
  233. {
  234. int new_timeout;
  235. if (get_user(new_timeout, p))
  236. return -EFAULT;
  237. /* arbitrary upper limit */
  238. if (new_timeout < 1 || new_timeout > 3600)
  239. return -EINVAL;
  240. timeout = new_timeout;
  241. wdt_keepalive();
  242. }
  243. fallthrough;
  244. case WDIOC_GETTIMEOUT:
  245. return put_user(timeout, p);
  246. default:
  247. return -ENOTTY;
  248. }
  249. }
  250. static const struct file_operations wdt_fops = {
  251. .owner = THIS_MODULE,
  252. .llseek = no_llseek,
  253. .write = fop_write,
  254. .open = fop_open,
  255. .release = fop_close,
  256. .unlocked_ioctl = fop_ioctl,
  257. .compat_ioctl = compat_ptr_ioctl,
  258. };
  259. static struct miscdevice wdt_miscdev = {
  260. .minor = WATCHDOG_MINOR,
  261. .name = "watchdog",
  262. .fops = &wdt_fops,
  263. };
  264. /*
  265. * Notifier for system down
  266. */
  267. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  268. void *unused)
  269. {
  270. if (code == SYS_DOWN || code == SYS_HALT)
  271. wdt_turnoff();
  272. return NOTIFY_DONE;
  273. }
  274. /*
  275. * The WDT needs to learn about soft shutdowns in order to
  276. * turn the timebomb registers off.
  277. */
  278. static struct notifier_block wdt_notifier = {
  279. .notifier_call = wdt_notify_sys,
  280. };
  281. static void __exit w83877f_wdt_unload(void)
  282. {
  283. wdt_turnoff();
  284. /* Deregister */
  285. misc_deregister(&wdt_miscdev);
  286. unregister_reboot_notifier(&wdt_notifier);
  287. release_region(WDT_PING, 1);
  288. release_region(ENABLE_W83877F_PORT, 2);
  289. }
  290. static int __init w83877f_wdt_init(void)
  291. {
  292. int rc = -EBUSY;
  293. if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
  294. timeout = WATCHDOG_TIMEOUT;
  295. pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
  296. timeout);
  297. }
  298. if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT")) {
  299. pr_err("I/O address 0x%04x already in use\n",
  300. ENABLE_W83877F_PORT);
  301. rc = -EIO;
  302. goto err_out;
  303. }
  304. if (!request_region(WDT_PING, 1, "W8387FF WDT")) {
  305. pr_err("I/O address 0x%04x already in use\n", WDT_PING);
  306. rc = -EIO;
  307. goto err_out_region1;
  308. }
  309. rc = register_reboot_notifier(&wdt_notifier);
  310. if (rc) {
  311. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  312. goto err_out_region2;
  313. }
  314. rc = misc_register(&wdt_miscdev);
  315. if (rc) {
  316. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  317. wdt_miscdev.minor, rc);
  318. goto err_out_reboot;
  319. }
  320. pr_info("WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n",
  321. timeout, nowayout);
  322. return 0;
  323. err_out_reboot:
  324. unregister_reboot_notifier(&wdt_notifier);
  325. err_out_region2:
  326. release_region(WDT_PING, 1);
  327. err_out_region1:
  328. release_region(ENABLE_W83877F_PORT, 2);
  329. err_out:
  330. return rc;
  331. }
  332. module_init(w83877f_wdt_init);
  333. module_exit(w83877f_wdt_unload);
  334. MODULE_AUTHOR("Scott and Bill Jennings");
  335. MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
  336. MODULE_LICENSE("GPL");