ib700wdt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * IB700 Single Board Computer WDT driver
  3. *
  4. * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
  5. *
  6. * Based on advantechwdt.c which is based on acquirewdt.c which
  7. * is based on wdt.c.
  8. *
  9. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  10. *
  11. * Based on acquirewdt.c which is based on wdt.c.
  12. * Original copyright messages:
  13. *
  14. * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
  15. * http://www.redhat.com
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. *
  22. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  23. * warranty for any of this software. This material is provided
  24. * "AS-IS" and at no charge.
  25. *
  26. * (c) Copyright 1995 Alan Cox <alan@redhat.com>
  27. *
  28. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  29. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  30. * Added timeout module option to override default
  31. *
  32. */
  33. #include <linux/config.h>
  34. #include <linux/module.h>
  35. #include <linux/types.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/watchdog.h>
  38. #include <linux/ioport.h>
  39. #include <linux/notifier.h>
  40. #include <linux/fs.h>
  41. #include <linux/reboot.h>
  42. #include <linux/init.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/moduleparam.h>
  45. #include <asm/io.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/system.h>
  48. static unsigned long ibwdt_is_open;
  49. static spinlock_t ibwdt_lock;
  50. static char expect_close;
  51. #define PFX "ib700wdt: "
  52. /*
  53. *
  54. * Watchdog Timer Configuration
  55. *
  56. * The function of the watchdog timer is to reset the system
  57. * automatically and is defined at I/O port 0443H. To enable the
  58. * watchdog timer and allow the system to reset, write I/O port 0443H.
  59. * To disable the timer, write I/O port 0441H for the system to stop the
  60. * watchdog function. The timer has a tolerance of 20% for its
  61. * intervals.
  62. *
  63. * The following describes how the timer should be programmed.
  64. *
  65. * Enabling Watchdog:
  66. * MOV AX,000FH (Choose the values from 0 to F)
  67. * MOV DX,0443H
  68. * OUT DX,AX
  69. *
  70. * Disabling Watchdog:
  71. * MOV AX,000FH (Any value is fine.)
  72. * MOV DX,0441H
  73. * OUT DX,AX
  74. *
  75. * Watchdog timer control table:
  76. * Level Value Time/sec | Level Value Time/sec
  77. * 1 F 0 | 9 7 16
  78. * 2 E 2 | 10 6 18
  79. * 3 D 4 | 11 5 20
  80. * 4 C 6 | 12 4 22
  81. * 5 B 8 | 13 3 24
  82. * 6 A 10 | 14 2 26
  83. * 7 9 12 | 15 1 28
  84. * 8 8 14 | 16 0 30
  85. *
  86. */
  87. static int wd_times[] = {
  88. 30, /* 0x0 */
  89. 28, /* 0x1 */
  90. 26, /* 0x2 */
  91. 24, /* 0x3 */
  92. 22, /* 0x4 */
  93. 20, /* 0x5 */
  94. 18, /* 0x6 */
  95. 16, /* 0x7 */
  96. 14, /* 0x8 */
  97. 12, /* 0x9 */
  98. 10, /* 0xA */
  99. 8, /* 0xB */
  100. 6, /* 0xC */
  101. 4, /* 0xD */
  102. 2, /* 0xE */
  103. 0, /* 0xF */
  104. };
  105. #define WDT_STOP 0x441
  106. #define WDT_START 0x443
  107. /* Default timeout */
  108. #define WD_TIMO 0 /* 30 seconds +/- 20%, from table */
  109. static int wd_margin = WD_TIMO;
  110. static int nowayout = WATCHDOG_NOWAYOUT;
  111. module_param(nowayout, int, 0);
  112. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  113. /*
  114. * Kernel methods.
  115. */
  116. static void
  117. ibwdt_ping(void)
  118. {
  119. /* Write a watchdog value */
  120. outb_p(wd_margin, WDT_START);
  121. }
  122. static ssize_t
  123. ibwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  124. {
  125. if (count) {
  126. if (!nowayout) {
  127. size_t i;
  128. /* In case it was set long ago */
  129. expect_close = 0;
  130. for (i = 0; i != count; i++) {
  131. char c;
  132. if (get_user(c, buf + i))
  133. return -EFAULT;
  134. if (c == 'V')
  135. expect_close = 42;
  136. }
  137. }
  138. ibwdt_ping();
  139. }
  140. return count;
  141. }
  142. static int
  143. ibwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  144. unsigned long arg)
  145. {
  146. int i, new_margin;
  147. void __user *argp = (void __user *)arg;
  148. int __user *p = argp;
  149. static struct watchdog_info ident = {
  150. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  151. .firmware_version = 1,
  152. .identity = "IB700 WDT",
  153. };
  154. switch (cmd) {
  155. case WDIOC_GETSUPPORT:
  156. if (copy_to_user(argp, &ident, sizeof(ident)))
  157. return -EFAULT;
  158. break;
  159. case WDIOC_GETSTATUS:
  160. return put_user(0, p);
  161. case WDIOC_KEEPALIVE:
  162. ibwdt_ping();
  163. break;
  164. case WDIOC_SETTIMEOUT:
  165. if (get_user(new_margin, p))
  166. return -EFAULT;
  167. if ((new_margin < 0) || (new_margin > 30))
  168. return -EINVAL;
  169. for (i = 0x0F; i > -1; i--)
  170. if (wd_times[i] > new_margin)
  171. break;
  172. wd_margin = i;
  173. ibwdt_ping();
  174. /* Fall */
  175. case WDIOC_GETTIMEOUT:
  176. return put_user(wd_times[wd_margin], p);
  177. break;
  178. default:
  179. return -ENOIOCTLCMD;
  180. }
  181. return 0;
  182. }
  183. static int
  184. ibwdt_open(struct inode *inode, struct file *file)
  185. {
  186. spin_lock(&ibwdt_lock);
  187. if (test_and_set_bit(0, &ibwdt_is_open)) {
  188. spin_unlock(&ibwdt_lock);
  189. return -EBUSY;
  190. }
  191. if (nowayout)
  192. __module_get(THIS_MODULE);
  193. /* Activate */
  194. ibwdt_ping();
  195. spin_unlock(&ibwdt_lock);
  196. return nonseekable_open(inode, file);
  197. }
  198. static int
  199. ibwdt_close(struct inode *inode, struct file *file)
  200. {
  201. spin_lock(&ibwdt_lock);
  202. if (expect_close == 42)
  203. outb_p(0, WDT_STOP);
  204. else
  205. printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n");
  206. clear_bit(0, &ibwdt_is_open);
  207. expect_close = 0;
  208. spin_unlock(&ibwdt_lock);
  209. return 0;
  210. }
  211. /*
  212. * Notifier for system down
  213. */
  214. static int
  215. ibwdt_notify_sys(struct notifier_block *this, unsigned long code,
  216. void *unused)
  217. {
  218. if (code == SYS_DOWN || code == SYS_HALT) {
  219. /* Turn the WDT off */
  220. outb_p(0, WDT_STOP);
  221. }
  222. return NOTIFY_DONE;
  223. }
  224. /*
  225. * Kernel Interfaces
  226. */
  227. static struct file_operations ibwdt_fops = {
  228. .owner = THIS_MODULE,
  229. .llseek = no_llseek,
  230. .write = ibwdt_write,
  231. .ioctl = ibwdt_ioctl,
  232. .open = ibwdt_open,
  233. .release = ibwdt_close,
  234. };
  235. static struct miscdevice ibwdt_miscdev = {
  236. .minor = WATCHDOG_MINOR,
  237. .name = "watchdog",
  238. .fops = &ibwdt_fops,
  239. };
  240. /*
  241. * The WDT needs to learn about soft shutdowns in order to
  242. * turn the timebomb registers off.
  243. */
  244. static struct notifier_block ibwdt_notifier = {
  245. .notifier_call = ibwdt_notify_sys,
  246. };
  247. static int __init ibwdt_init(void)
  248. {
  249. int res;
  250. printk(KERN_INFO PFX "WDT driver for IB700 single board computer initialising.\n");
  251. spin_lock_init(&ibwdt_lock);
  252. res = misc_register(&ibwdt_miscdev);
  253. if (res) {
  254. printk (KERN_ERR PFX "failed to register misc device\n");
  255. goto out_nomisc;
  256. }
  257. #if WDT_START != WDT_STOP
  258. if (!request_region(WDT_STOP, 1, "IB700 WDT")) {
  259. printk (KERN_ERR PFX "STOP method I/O %X is not available.\n", WDT_STOP);
  260. res = -EIO;
  261. goto out_nostopreg;
  262. }
  263. #endif
  264. if (!request_region(WDT_START, 1, "IB700 WDT")) {
  265. printk (KERN_ERR PFX "START method I/O %X is not available.\n", WDT_START);
  266. res = -EIO;
  267. goto out_nostartreg;
  268. }
  269. res = register_reboot_notifier(&ibwdt_notifier);
  270. if (res) {
  271. printk (KERN_ERR PFX "Failed to register reboot notifier.\n");
  272. goto out_noreboot;
  273. }
  274. return 0;
  275. out_noreboot:
  276. release_region(WDT_START, 1);
  277. out_nostartreg:
  278. #if WDT_START != WDT_STOP
  279. release_region(WDT_STOP, 1);
  280. #endif
  281. out_nostopreg:
  282. misc_deregister(&ibwdt_miscdev);
  283. out_nomisc:
  284. return res;
  285. }
  286. static void __exit
  287. ibwdt_exit(void)
  288. {
  289. misc_deregister(&ibwdt_miscdev);
  290. unregister_reboot_notifier(&ibwdt_notifier);
  291. #if WDT_START != WDT_STOP
  292. release_region(WDT_STOP,1);
  293. #endif
  294. release_region(WDT_START,1);
  295. }
  296. module_init(ibwdt_init);
  297. module_exit(ibwdt_exit);
  298. MODULE_AUTHOR("Charles Howes <chowes@vsol.net>");
  299. MODULE_DESCRIPTION("IB700 SBC watchdog driver");
  300. MODULE_LICENSE("GPL");
  301. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  302. /* end of ib700wdt.c */