sbc8360.c 10 KB

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