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