sbc60xxwdt.c 9.9 KB

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