ib700wdt.c 8.4 KB

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