sbc8360.c 10.0 KB

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