ib700wdt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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@lxorguk.ukuu.org.uk>,
  15. * All Rights Reserved.
  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@lxorguk.ukuu.org.uk>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/fs.h>
  40. #include <linux/init.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/io.h>
  45. #include <linux/uaccess.h>
  46. static struct platform_device *ibwdt_platform_device;
  47. static unsigned long ibwdt_is_open;
  48. static DEFINE_SPINLOCK(ibwdt_lock);
  49. static char expect_close;
  50. /* Module information */
  51. #define DRV_NAME "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. #define WDT_STOP 0x441
  88. #define WDT_START 0x443
  89. /* Default timeout */
  90. #define WATCHDOG_TIMEOUT 30 /* 30 seconds +/- 20% */
  91. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  92. module_param(timeout, int, 0);
  93. MODULE_PARM_DESC(timeout,
  94. "Watchdog timeout in seconds. 0<= timeout <=30, default="
  95. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  96. static bool nowayout = WATCHDOG_NOWAYOUT;
  97. module_param(nowayout, bool, 0);
  98. MODULE_PARM_DESC(nowayout,
  99. "Watchdog cannot be stopped once started (default="
  100. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  101. /*
  102. * Watchdog Operations
  103. */
  104. static void ibwdt_ping(void)
  105. {
  106. int wd_margin = 15 - ((timeout + 1) / 2);
  107. spin_lock(&ibwdt_lock);
  108. /* Write a watchdog value */
  109. outb_p(wd_margin, WDT_START);
  110. spin_unlock(&ibwdt_lock);
  111. }
  112. static void ibwdt_disable(void)
  113. {
  114. spin_lock(&ibwdt_lock);
  115. outb_p(0, WDT_STOP);
  116. spin_unlock(&ibwdt_lock);
  117. }
  118. static int ibwdt_set_heartbeat(int t)
  119. {
  120. if (t < 0 || t > 30)
  121. return -EINVAL;
  122. timeout = t;
  123. return 0;
  124. }
  125. /*
  126. * /dev/watchdog handling
  127. */
  128. static ssize_t ibwdt_write(struct file *file, const char __user *buf,
  129. size_t count, loff_t *ppos)
  130. {
  131. if (count) {
  132. if (!nowayout) {
  133. size_t i;
  134. /* In case it was set long ago */
  135. expect_close = 0;
  136. for (i = 0; i != count; i++) {
  137. char c;
  138. if (get_user(c, buf + i))
  139. return -EFAULT;
  140. if (c == 'V')
  141. expect_close = 42;
  142. }
  143. }
  144. ibwdt_ping();
  145. }
  146. return count;
  147. }
  148. static long ibwdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  149. {
  150. int new_margin;
  151. void __user *argp = (void __user *)arg;
  152. int __user *p = argp;
  153. static const struct watchdog_info ident = {
  154. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
  155. | WDIOF_MAGICCLOSE,
  156. .firmware_version = 1,
  157. .identity = "IB700 WDT",
  158. };
  159. switch (cmd) {
  160. case WDIOC_GETSUPPORT:
  161. if (copy_to_user(argp, &ident, sizeof(ident)))
  162. return -EFAULT;
  163. break;
  164. case WDIOC_GETSTATUS:
  165. case WDIOC_GETBOOTSTATUS:
  166. return put_user(0, p);
  167. case WDIOC_SETOPTIONS:
  168. {
  169. int options, retval = -EINVAL;
  170. if (get_user(options, p))
  171. return -EFAULT;
  172. if (options & WDIOS_DISABLECARD) {
  173. ibwdt_disable();
  174. retval = 0;
  175. }
  176. if (options & WDIOS_ENABLECARD) {
  177. ibwdt_ping();
  178. retval = 0;
  179. }
  180. return retval;
  181. }
  182. case WDIOC_KEEPALIVE:
  183. ibwdt_ping();
  184. break;
  185. case WDIOC_SETTIMEOUT:
  186. if (get_user(new_margin, p))
  187. return -EFAULT;
  188. if (ibwdt_set_heartbeat(new_margin))
  189. return -EINVAL;
  190. ibwdt_ping();
  191. /* Fall */
  192. case WDIOC_GETTIMEOUT:
  193. return put_user(timeout, p);
  194. default:
  195. return -ENOTTY;
  196. }
  197. return 0;
  198. }
  199. static int ibwdt_open(struct inode *inode, struct file *file)
  200. {
  201. if (test_and_set_bit(0, &ibwdt_is_open))
  202. return -EBUSY;
  203. if (nowayout)
  204. __module_get(THIS_MODULE);
  205. /* Activate */
  206. ibwdt_ping();
  207. return nonseekable_open(inode, file);
  208. }
  209. static int ibwdt_close(struct inode *inode, struct file *file)
  210. {
  211. if (expect_close == 42) {
  212. ibwdt_disable();
  213. } else {
  214. pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
  215. ibwdt_ping();
  216. }
  217. clear_bit(0, &ibwdt_is_open);
  218. expect_close = 0;
  219. return 0;
  220. }
  221. /*
  222. * Kernel Interfaces
  223. */
  224. static const struct file_operations ibwdt_fops = {
  225. .owner = THIS_MODULE,
  226. .llseek = no_llseek,
  227. .write = ibwdt_write,
  228. .unlocked_ioctl = ibwdt_ioctl,
  229. .open = ibwdt_open,
  230. .release = ibwdt_close,
  231. };
  232. static struct miscdevice ibwdt_miscdev = {
  233. .minor = WATCHDOG_MINOR,
  234. .name = "watchdog",
  235. .fops = &ibwdt_fops,
  236. };
  237. /*
  238. * Init & exit routines
  239. */
  240. static int __init ibwdt_probe(struct platform_device *dev)
  241. {
  242. int res;
  243. #if WDT_START != WDT_STOP
  244. if (!request_region(WDT_STOP, 1, "IB700 WDT")) {
  245. pr_err("STOP method I/O %X is not available\n", WDT_STOP);
  246. res = -EIO;
  247. goto out_nostopreg;
  248. }
  249. #endif
  250. if (!request_region(WDT_START, 1, "IB700 WDT")) {
  251. pr_err("START method I/O %X is not available\n", WDT_START);
  252. res = -EIO;
  253. goto out_nostartreg;
  254. }
  255. /* Check that the heartbeat value is within it's range ;
  256. * if not reset to the default */
  257. if (ibwdt_set_heartbeat(timeout)) {
  258. ibwdt_set_heartbeat(WATCHDOG_TIMEOUT);
  259. pr_info("timeout value must be 0<=x<=30, using %d\n", timeout);
  260. }
  261. res = misc_register(&ibwdt_miscdev);
  262. if (res) {
  263. pr_err("failed to register misc device\n");
  264. goto out_nomisc;
  265. }
  266. return 0;
  267. out_nomisc:
  268. release_region(WDT_START, 1);
  269. out_nostartreg:
  270. #if WDT_START != WDT_STOP
  271. release_region(WDT_STOP, 1);
  272. #endif
  273. out_nostopreg:
  274. return res;
  275. }
  276. static int ibwdt_remove(struct platform_device *dev)
  277. {
  278. misc_deregister(&ibwdt_miscdev);
  279. release_region(WDT_START, 1);
  280. #if WDT_START != WDT_STOP
  281. release_region(WDT_STOP, 1);
  282. #endif
  283. return 0;
  284. }
  285. static void ibwdt_shutdown(struct platform_device *dev)
  286. {
  287. /* Turn the WDT off if we have a soft shutdown */
  288. ibwdt_disable();
  289. }
  290. static struct platform_driver ibwdt_driver = {
  291. .remove = ibwdt_remove,
  292. .shutdown = ibwdt_shutdown,
  293. .driver = {
  294. .name = DRV_NAME,
  295. },
  296. };
  297. static int __init ibwdt_init(void)
  298. {
  299. int err;
  300. pr_info("WDT driver for IB700 single board computer initialising\n");
  301. ibwdt_platform_device = platform_device_register_simple(DRV_NAME,
  302. -1, NULL, 0);
  303. if (IS_ERR(ibwdt_platform_device))
  304. return PTR_ERR(ibwdt_platform_device);
  305. err = platform_driver_probe(&ibwdt_driver, ibwdt_probe);
  306. if (err)
  307. goto unreg_platform_device;
  308. return 0;
  309. unreg_platform_device:
  310. platform_device_unregister(ibwdt_platform_device);
  311. return err;
  312. }
  313. static void __exit ibwdt_exit(void)
  314. {
  315. platform_device_unregister(ibwdt_platform_device);
  316. platform_driver_unregister(&ibwdt_driver);
  317. pr_info("Watchdog Module Unloaded\n");
  318. }
  319. module_init(ibwdt_init);
  320. module_exit(ibwdt_exit);
  321. MODULE_AUTHOR("Charles Howes <chowes@vsol.net>");
  322. MODULE_DESCRIPTION("IB700 SBC watchdog driver");
  323. MODULE_LICENSE("GPL");
  324. /* end of ib700wdt.c */