sbc8360.c 9.8 KB

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