ib700wdt.c 7.6 KB

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