sbc8360.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SBC8360 Watchdog driver
  4. *
  5. * (c) Copyright 2005 Webcon, Inc.
  6. *
  7. * Based on ib700wdt.c, which is based on advantechwdt.c which is based
  8. * on acquirewdt.c which is based on wdt.c.
  9. *
  10. * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
  11. *
  12. * Based on advantechwdt.c which is based on acquirewdt.c which
  13. * is based on wdt.c.
  14. *
  15. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  16. *
  17. * Based on acquirewdt.c which is based on wdt.c.
  18. * Original copyright messages:
  19. *
  20. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  21. * All Rights Reserved.
  22. *
  23. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  24. * warranty for any of this software. This material is provided
  25. * "AS-IS" and at no charge.
  26. *
  27. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  28. *
  29. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  30. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  31. * Added timeout module option to override default
  32. *
  33. */
  34. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  35. #include <linux/module.h>
  36. #include <linux/types.h>
  37. #include <linux/miscdevice.h>
  38. #include <linux/watchdog.h>
  39. #include <linux/ioport.h>
  40. #include <linux/delay.h>
  41. #include <linux/notifier.h>
  42. #include <linux/fs.h>
  43. #include <linux/reboot.h>
  44. #include <linux/init.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/io.h>
  48. #include <linux/uaccess.h>
  49. static unsigned long sbc8360_is_open;
  50. static char expect_close;
  51. /*
  52. *
  53. * Watchdog Timer Configuration
  54. *
  55. * The function of the watchdog timer is to reset the system automatically
  56. * and is defined at I/O port 0120H and 0121H. To enable the watchdog timer
  57. * and allow the system to reset, write appropriate values from the table
  58. * below to I/O port 0120H and 0121H. To disable the timer, write a zero
  59. * value to I/O port 0121H for the system to stop the watchdog function.
  60. *
  61. * The following describes how the timer should be programmed (according to
  62. * the vendor documentation)
  63. *
  64. * Enabling Watchdog:
  65. * MOV AX,000AH (enable, phase I)
  66. * MOV DX,0120H
  67. * OUT DX,AX
  68. * MOV AX,000BH (enable, phase II)
  69. * MOV DX,0120H
  70. * OUT DX,AX
  71. * MOV AX,000nH (set multiplier n, from 1-4)
  72. * MOV DX,0120H
  73. * OUT DX,AX
  74. * MOV AX,000mH (set base timer m, from 0-F)
  75. * MOV DX,0121H
  76. * OUT DX,AX
  77. *
  78. * Reset timer:
  79. * MOV AX,000mH (same as set base timer, above)
  80. * MOV DX,0121H
  81. * OUT DX,AX
  82. *
  83. * Disabling Watchdog:
  84. * MOV AX,0000H (a zero value)
  85. * MOV DX,0120H
  86. * OUT DX,AX
  87. *
  88. * Watchdog timeout configuration values:
  89. * N
  90. * M | 1 2 3 4
  91. * --|----------------------------------
  92. * 0 | 0.5s 5s 50s 100s
  93. * 1 | 1s 10s 100s 200s
  94. * 2 | 1.5s 15s 150s 300s
  95. * 3 | 2s 20s 200s 400s
  96. * 4 | 2.5s 25s 250s 500s
  97. * 5 | 3s 30s 300s 600s
  98. * 6 | 3.5s 35s 350s 700s
  99. * 7 | 4s 40s 400s 800s
  100. * 8 | 4.5s 45s 450s 900s
  101. * 9 | 5s 50s 500s 1000s
  102. * A | 5.5s 55s 550s 1100s
  103. * B | 6s 60s 600s 1200s
  104. * C | 6.5s 65s 650s 1300s
  105. * D | 7s 70s 700s 1400s
  106. * E | 7.5s 75s 750s 1500s
  107. * F | 8s 80s 800s 1600s
  108. *
  109. * Another way to say the same things is:
  110. * For N=1, Timeout = (M+1) * 0.5s
  111. * For N=2, Timeout = (M+1) * 5s
  112. * For N=3, Timeout = (M+1) * 50s
  113. * For N=4, Timeout = (M+1) * 100s
  114. *
  115. */
  116. static int wd_times[64][2] = {
  117. {0, 1}, /* 0 = 0.5s */
  118. {1, 1}, /* 1 = 1s */
  119. {2, 1}, /* 2 = 1.5s */
  120. {3, 1}, /* 3 = 2s */
  121. {4, 1}, /* 4 = 2.5s */
  122. {5, 1}, /* 5 = 3s */
  123. {6, 1}, /* 6 = 3.5s */
  124. {7, 1}, /* 7 = 4s */
  125. {8, 1}, /* 8 = 4.5s */
  126. {9, 1}, /* 9 = 5s */
  127. {0xA, 1}, /* 10 = 5.5s */
  128. {0xB, 1}, /* 11 = 6s */
  129. {0xC, 1}, /* 12 = 6.5s */
  130. {0xD, 1}, /* 13 = 7s */
  131. {0xE, 1}, /* 14 = 7.5s */
  132. {0xF, 1}, /* 15 = 8s */
  133. {0, 2}, /* 16 = 5s */
  134. {1, 2}, /* 17 = 10s */
  135. {2, 2}, /* 18 = 15s */
  136. {3, 2}, /* 19 = 20s */
  137. {4, 2}, /* 20 = 25s */
  138. {5, 2}, /* 21 = 30s */
  139. {6, 2}, /* 22 = 35s */
  140. {7, 2}, /* 23 = 40s */
  141. {8, 2}, /* 24 = 45s */
  142. {9, 2}, /* 25 = 50s */
  143. {0xA, 2}, /* 26 = 55s */
  144. {0xB, 2}, /* 27 = 60s */
  145. {0xC, 2}, /* 28 = 65s */
  146. {0xD, 2}, /* 29 = 70s */
  147. {0xE, 2}, /* 30 = 75s */
  148. {0xF, 2}, /* 31 = 80s */
  149. {0, 3}, /* 32 = 50s */
  150. {1, 3}, /* 33 = 100s */
  151. {2, 3}, /* 34 = 150s */
  152. {3, 3}, /* 35 = 200s */
  153. {4, 3}, /* 36 = 250s */
  154. {5, 3}, /* 37 = 300s */
  155. {6, 3}, /* 38 = 350s */
  156. {7, 3}, /* 39 = 400s */
  157. {8, 3}, /* 40 = 450s */
  158. {9, 3}, /* 41 = 500s */
  159. {0xA, 3}, /* 42 = 550s */
  160. {0xB, 3}, /* 43 = 600s */
  161. {0xC, 3}, /* 44 = 650s */
  162. {0xD, 3}, /* 45 = 700s */
  163. {0xE, 3}, /* 46 = 750s */
  164. {0xF, 3}, /* 47 = 800s */
  165. {0, 4}, /* 48 = 100s */
  166. {1, 4}, /* 49 = 200s */
  167. {2, 4}, /* 50 = 300s */
  168. {3, 4}, /* 51 = 400s */
  169. {4, 4}, /* 52 = 500s */
  170. {5, 4}, /* 53 = 600s */
  171. {6, 4}, /* 54 = 700s */
  172. {7, 4}, /* 55 = 800s */
  173. {8, 4}, /* 56 = 900s */
  174. {9, 4}, /* 57 = 1000s */
  175. {0xA, 4}, /* 58 = 1100s */
  176. {0xB, 4}, /* 59 = 1200s */
  177. {0xC, 4}, /* 60 = 1300s */
  178. {0xD, 4}, /* 61 = 1400s */
  179. {0xE, 4}, /* 62 = 1500s */
  180. {0xF, 4} /* 63 = 1600s */
  181. };
  182. #define SBC8360_ENABLE 0x120
  183. #define SBC8360_BASETIME 0x121
  184. static int timeout = 27;
  185. static int wd_margin = 0xB;
  186. static int wd_multiplier = 2;
  187. static bool nowayout = WATCHDOG_NOWAYOUT;
  188. module_param(timeout, int, 0);
  189. MODULE_PARM_DESC(timeout, "Index into timeout table (0-63) (default=27 (60s))");
  190. module_param(nowayout, bool, 0);
  191. MODULE_PARM_DESC(nowayout,
  192. "Watchdog cannot be stopped once started (default="
  193. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  194. /*
  195. * Kernel methods.
  196. */
  197. /* Activate and pre-configure watchdog */
  198. static void sbc8360_activate(void)
  199. {
  200. /* Enable the watchdog */
  201. outb(0x0A, SBC8360_ENABLE);
  202. msleep_interruptible(100);
  203. outb(0x0B, SBC8360_ENABLE);
  204. msleep_interruptible(100);
  205. /* Set timeout multiplier */
  206. outb(wd_multiplier, SBC8360_ENABLE);
  207. msleep_interruptible(100);
  208. /* Nothing happens until first sbc8360_ping() */
  209. }
  210. /* Kernel pings watchdog */
  211. static void sbc8360_ping(void)
  212. {
  213. /* Write the base timer register */
  214. outb(wd_margin, SBC8360_BASETIME);
  215. }
  216. /* stop watchdog */
  217. static void sbc8360_stop(void)
  218. {
  219. /* De-activate the watchdog */
  220. outb(0, SBC8360_ENABLE);
  221. }
  222. /* Userspace pings kernel driver, or requests clean close */
  223. static ssize_t sbc8360_write(struct file *file, const char __user *buf,
  224. size_t count, loff_t *ppos)
  225. {
  226. if (count) {
  227. if (!nowayout) {
  228. size_t i;
  229. /* In case it was set long ago */
  230. expect_close = 0;
  231. for (i = 0; i != count; i++) {
  232. char c;
  233. if (get_user(c, buf + i))
  234. return -EFAULT;
  235. if (c == 'V')
  236. expect_close = 42;
  237. }
  238. }
  239. sbc8360_ping();
  240. }
  241. return count;
  242. }
  243. static int sbc8360_open(struct inode *inode, struct file *file)
  244. {
  245. if (test_and_set_bit(0, &sbc8360_is_open))
  246. return -EBUSY;
  247. if (nowayout)
  248. __module_get(THIS_MODULE);
  249. /* Activate and ping once to start the countdown */
  250. sbc8360_activate();
  251. sbc8360_ping();
  252. return stream_open(inode, file);
  253. }
  254. static int sbc8360_close(struct inode *inode, struct file *file)
  255. {
  256. if (expect_close == 42)
  257. sbc8360_stop();
  258. else
  259. pr_crit("SBC8360 device closed unexpectedly. SBC8360 will not stop!\n");
  260. clear_bit(0, &sbc8360_is_open);
  261. expect_close = 0;
  262. return 0;
  263. }
  264. /*
  265. * Notifier for system down
  266. */
  267. static int sbc8360_notify_sys(struct notifier_block *this, unsigned long code,
  268. void *unused)
  269. {
  270. if (code == SYS_DOWN || code == SYS_HALT)
  271. sbc8360_stop(); /* Disable the SBC8360 Watchdog */
  272. return NOTIFY_DONE;
  273. }
  274. /*
  275. * Kernel Interfaces
  276. */
  277. static const struct file_operations sbc8360_fops = {
  278. .owner = THIS_MODULE,
  279. .llseek = no_llseek,
  280. .write = sbc8360_write,
  281. .open = sbc8360_open,
  282. .release = sbc8360_close,
  283. };
  284. static struct miscdevice sbc8360_miscdev = {
  285. .minor = WATCHDOG_MINOR,
  286. .name = "watchdog",
  287. .fops = &sbc8360_fops,
  288. };
  289. /*
  290. * The SBC8360 needs to learn about soft shutdowns in order to
  291. * turn the timebomb registers off.
  292. */
  293. static struct notifier_block sbc8360_notifier = {
  294. .notifier_call = sbc8360_notify_sys,
  295. };
  296. static int __init sbc8360_init(void)
  297. {
  298. int res;
  299. unsigned long int mseconds = 60000;
  300. if (timeout < 0 || timeout > 63) {
  301. pr_err("Invalid timeout index (must be 0-63)\n");
  302. res = -EINVAL;
  303. goto out;
  304. }
  305. if (!request_region(SBC8360_ENABLE, 1, "SBC8360")) {
  306. pr_err("ENABLE method I/O %X is not available\n",
  307. SBC8360_ENABLE);
  308. res = -EIO;
  309. goto out;
  310. }
  311. if (!request_region(SBC8360_BASETIME, 1, "SBC8360")) {
  312. pr_err("BASETIME method I/O %X is not available\n",
  313. SBC8360_BASETIME);
  314. res = -EIO;
  315. goto out_nobasetimereg;
  316. }
  317. res = register_reboot_notifier(&sbc8360_notifier);
  318. if (res) {
  319. pr_err("Failed to register reboot notifier\n");
  320. goto out_noreboot;
  321. }
  322. res = misc_register(&sbc8360_miscdev);
  323. if (res) {
  324. pr_err("failed to register misc device\n");
  325. goto out_nomisc;
  326. }
  327. wd_margin = wd_times[timeout][0];
  328. wd_multiplier = wd_times[timeout][1];
  329. if (wd_multiplier == 1)
  330. mseconds = (wd_margin + 1) * 500;
  331. else if (wd_multiplier == 2)
  332. mseconds = (wd_margin + 1) * 5000;
  333. else if (wd_multiplier == 3)
  334. mseconds = (wd_margin + 1) * 50000;
  335. else if (wd_multiplier == 4)
  336. mseconds = (wd_margin + 1) * 100000;
  337. /* My kingdom for the ability to print "0.5 seconds" in the kernel! */
  338. pr_info("Timeout set at %ld ms\n", mseconds);
  339. return 0;
  340. out_nomisc:
  341. unregister_reboot_notifier(&sbc8360_notifier);
  342. out_noreboot:
  343. release_region(SBC8360_BASETIME, 1);
  344. out_nobasetimereg:
  345. release_region(SBC8360_ENABLE, 1);
  346. out:
  347. return res;
  348. }
  349. static void __exit sbc8360_exit(void)
  350. {
  351. misc_deregister(&sbc8360_miscdev);
  352. unregister_reboot_notifier(&sbc8360_notifier);
  353. release_region(SBC8360_ENABLE, 1);
  354. release_region(SBC8360_BASETIME, 1);
  355. }
  356. module_init(sbc8360_init);
  357. module_exit(sbc8360_exit);
  358. MODULE_AUTHOR("Ian E. Morgan <imorgan@webcon.ca>");
  359. MODULE_DESCRIPTION("SBC8360 watchdog driver");
  360. MODULE_LICENSE("GPL");
  361. MODULE_VERSION("1.01");
  362. /* end of sbc8360.c */