w83697hf_wdt.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * w83697hf/hg WDT driver
  3. *
  4. * (c) Copyright 2006 Marcus Junker <junker@anduras.de>
  5. *
  6. * Based on w83627hf_wdt.c which is based on advantechwdt.c
  7. * which is based on wdt.c.
  8. * Original copyright messages:
  9. *
  10. * (c) Copyright 2003 Pádraig Brady <P@draigBrady.com>
  11. *
  12. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  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 Marcus Junker nor ANDURAS AG admit liability nor provide
  23. * warranty for any of this software. This material is provided
  24. * "AS-IS" and at no charge.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/types.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/fs.h>
  32. #include <linux/ioport.h>
  33. #include <linux/notifier.h>
  34. #include <linux/reboot.h>
  35. #include <linux/init.h>
  36. #include <linux/spinlock.h>
  37. #include <asm/io.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/system.h>
  40. #define WATCHDOG_NAME "w83697hf/hg WDT"
  41. #define PFX WATCHDOG_NAME ": "
  42. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  43. static unsigned long wdt_is_open;
  44. static char expect_close;
  45. static spinlock_t io_lock;
  46. /* You must set this - there is no sane way to probe for this board. */
  47. static int wdt_io = 0x2e;
  48. module_param(wdt_io, int, 0);
  49. MODULE_PARM_DESC(wdt_io, "w83697hf/hg WDT io port (default 0x2e, 0 = autodetect)");
  50. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  51. module_param(timeout, int, 0);
  52. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=255, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  53. static int nowayout = WATCHDOG_NOWAYOUT;
  54. module_param(nowayout, int, 0);
  55. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  56. /*
  57. * Kernel methods.
  58. */
  59. #define W83697HF_EFER (wdt_io+0) /* Extended Function Enable Register */
  60. #define W83697HF_EFIR (wdt_io+0) /* Extended Function Index Register (same as EFER) */
  61. #define W83697HF_EFDR (wdt_io+1) /* Extended Function Data Register */
  62. static inline void
  63. w83697hf_unlock(void)
  64. {
  65. outb_p(0x87, W83697HF_EFER); /* Enter extended function mode */
  66. outb_p(0x87, W83697HF_EFER); /* Again according to manual */
  67. }
  68. static inline void
  69. w83697hf_lock(void)
  70. {
  71. outb_p(0xAA, W83697HF_EFER); /* Leave extended function mode */
  72. }
  73. /*
  74. * The three functions w83697hf_get_reg(), w83697hf_set_reg() and
  75. * w83697hf_write_timeout() must be called with the device unlocked.
  76. */
  77. static unsigned char
  78. w83697hf_get_reg(unsigned char reg)
  79. {
  80. outb_p(reg, W83697HF_EFIR);
  81. return inb_p(W83697HF_EFDR);
  82. }
  83. static void
  84. w83697hf_set_reg(unsigned char reg, unsigned char data)
  85. {
  86. outb_p(reg, W83697HF_EFIR);
  87. outb_p(data, W83697HF_EFDR);
  88. }
  89. static void
  90. w83697hf_write_timeout(int timeout)
  91. {
  92. w83697hf_set_reg(0xF4, timeout); /* Write Timeout counter to CRF4 */
  93. }
  94. static void
  95. w83697hf_select_wdt(void)
  96. {
  97. w83697hf_unlock();
  98. w83697hf_set_reg(0x07, 0x08); /* Switch to logic device 8 (GPIO2) */
  99. }
  100. static inline void
  101. w83697hf_deselect_wdt(void)
  102. {
  103. w83697hf_lock();
  104. }
  105. static void
  106. w83697hf_select_wd_register(void)
  107. {
  108. w83697hf_unlock();
  109. w83697hf_set_reg(0x29, 0x20); /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
  110. w83697hf_set_reg(0x07, 0x08); /* Switch to logic device 8 (GPIO2) */
  111. w83697hf_set_reg(0x30, 0x01); /* Enable timer/activate GPIO2 via bit 0 */
  112. }
  113. static void
  114. w83697hf_unselect_wd_register(void)
  115. {
  116. w83697hf_lock();
  117. }
  118. static void
  119. w83697hf_init(void)
  120. {
  121. unsigned char t;
  122. w83697hf_select_wd_register();
  123. t = w83697hf_get_reg(0xF3); /* Read CRF3 */
  124. if (t != 0) {
  125. printk (KERN_INFO PFX "Watchdog already running. Resetting timeout to %d sec\n", timeout);
  126. w83697hf_set_reg(0xF3, timeout); /* Write new timeout */
  127. }
  128. t = w83697hf_get_reg(0xF4); /* Read CRF4 */
  129. t&=~0x0C; /* set second mode & disable keyboard turning off watchdog */
  130. w83697hf_set_reg(0xF4, t); /* Write back to CRF4 */
  131. w83697hf_unselect_wd_register();
  132. }
  133. static int
  134. wdt_ping(void)
  135. {
  136. spin_lock(&io_lock);
  137. w83697hf_select_wdt();
  138. w83697hf_write_timeout(timeout);
  139. w83697hf_deselect_wdt();
  140. spin_unlock(&io_lock);
  141. return 0;
  142. }
  143. static int
  144. wdt_enable(void)
  145. {
  146. spin_lock(&io_lock);
  147. w83697hf_select_wdt();
  148. w83697hf_write_timeout(timeout);
  149. w83697hf_set_reg(0x30, 1); /* Enable timer */
  150. w83697hf_deselect_wdt();
  151. spin_unlock(&io_lock);
  152. return 0;
  153. }
  154. static int
  155. wdt_disable(void)
  156. {
  157. spin_lock(&io_lock);
  158. w83697hf_select_wdt();
  159. w83697hf_set_reg(0x30, 0); /* Disable timer */
  160. w83697hf_write_timeout(0);
  161. w83697hf_deselect_wdt();
  162. spin_unlock(&io_lock);
  163. return 0;
  164. }
  165. static int
  166. wdt_set_heartbeat(int t)
  167. {
  168. if ((t < 1) || (t > 255))
  169. return -EINVAL;
  170. timeout = t;
  171. return 0;
  172. }
  173. static ssize_t
  174. wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  175. {
  176. if (count) {
  177. if (!nowayout) {
  178. size_t i;
  179. expect_close = 0;
  180. for (i = 0; i != count; i++) {
  181. char c;
  182. if (get_user(c, buf+i))
  183. return -EFAULT;
  184. if (c == 'V')
  185. expect_close = 42;
  186. }
  187. }
  188. wdt_ping();
  189. }
  190. return count;
  191. }
  192. static int
  193. wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  194. unsigned long arg)
  195. {
  196. void __user *argp = (void __user *)arg;
  197. int __user *p = argp;
  198. int new_timeout;
  199. static struct watchdog_info ident = {
  200. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  201. .firmware_version = 1,
  202. .identity = "W83697HF WDT",
  203. };
  204. switch (cmd) {
  205. case WDIOC_GETSUPPORT:
  206. if (copy_to_user(argp, &ident, sizeof(ident)))
  207. return -EFAULT;
  208. break;
  209. case WDIOC_GETSTATUS:
  210. case WDIOC_GETBOOTSTATUS:
  211. return put_user(0, p);
  212. case WDIOC_KEEPALIVE:
  213. wdt_ping();
  214. break;
  215. case WDIOC_SETTIMEOUT:
  216. if (get_user(new_timeout, p))
  217. return -EFAULT;
  218. if (wdt_set_heartbeat(new_timeout))
  219. return -EINVAL;
  220. wdt_ping();
  221. /* Fall */
  222. case WDIOC_GETTIMEOUT:
  223. return put_user(timeout, p);
  224. case WDIOC_SETOPTIONS:
  225. {
  226. int options, retval = -EINVAL;
  227. if (get_user(options, p))
  228. return -EFAULT;
  229. if (options & WDIOS_DISABLECARD) {
  230. wdt_disable();
  231. retval = 0;
  232. }
  233. if (options & WDIOS_ENABLECARD) {
  234. wdt_enable();
  235. retval = 0;
  236. }
  237. return retval;
  238. }
  239. default:
  240. return -ENOTTY;
  241. }
  242. return 0;
  243. }
  244. static int
  245. wdt_open(struct inode *inode, struct file *file)
  246. {
  247. if (test_and_set_bit(0, &wdt_is_open))
  248. return -EBUSY;
  249. /*
  250. * Activate
  251. */
  252. wdt_enable();
  253. return nonseekable_open(inode, file);
  254. }
  255. static int
  256. wdt_close(struct inode *inode, struct file *file)
  257. {
  258. if (expect_close == 42) {
  259. wdt_disable();
  260. } else {
  261. printk (KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  262. wdt_ping();
  263. }
  264. expect_close = 0;
  265. clear_bit(0, &wdt_is_open);
  266. return 0;
  267. }
  268. /*
  269. * Notifier for system down
  270. */
  271. static int
  272. wdt_notify_sys(struct notifier_block *this, unsigned long code,
  273. void *unused)
  274. {
  275. if (code == SYS_DOWN || code == SYS_HALT) {
  276. /* Turn the WDT off */
  277. wdt_disable();
  278. }
  279. return NOTIFY_DONE;
  280. }
  281. /*
  282. * Kernel Interfaces
  283. */
  284. static struct file_operations wdt_fops = {
  285. .owner = THIS_MODULE,
  286. .llseek = no_llseek,
  287. .write = wdt_write,
  288. .ioctl = wdt_ioctl,
  289. .open = wdt_open,
  290. .release = wdt_close,
  291. };
  292. static struct miscdevice wdt_miscdev = {
  293. .minor = WATCHDOG_MINOR,
  294. .name = "watchdog",
  295. .fops = &wdt_fops,
  296. };
  297. /*
  298. * The WDT needs to learn about soft shutdowns in order to
  299. * turn the timebomb registers off.
  300. */
  301. static struct notifier_block wdt_notifier = {
  302. .notifier_call = wdt_notify_sys,
  303. };
  304. static int
  305. w83697hf_check_wdt(void)
  306. {
  307. if (!request_region(wdt_io, 2, WATCHDOG_NAME)) {
  308. printk (KERN_ERR PFX "I/O address 0x%x already in use\n", wdt_io);
  309. return -EIO;
  310. }
  311. printk (KERN_DEBUG PFX "Looking for watchdog at address 0x%x\n", wdt_io);
  312. w83697hf_unlock();
  313. if (w83697hf_get_reg(0x20) == 0x60) {
  314. printk (KERN_INFO PFX "watchdog found at address 0x%x\n", wdt_io);
  315. w83697hf_lock();
  316. return 0;
  317. }
  318. w83697hf_lock(); /* Reprotect in case it was a compatible device */
  319. printk (KERN_INFO PFX "watchdog not found at address 0x%x\n", wdt_io);
  320. release_region(wdt_io, 2);
  321. return -EIO;
  322. }
  323. static int __init
  324. wdt_init(void)
  325. {
  326. int ret, autodetect;
  327. spin_lock_init(&io_lock);
  328. printk (KERN_INFO PFX "WDT driver for W83697HF/HG initializing\n");
  329. autodetect = wdt_io == 0;
  330. if (autodetect)
  331. wdt_io = 0x2e;
  332. if (!w83697hf_check_wdt())
  333. goto found;
  334. if (autodetect) {
  335. wdt_io = 0x4e;
  336. if (!w83697hf_check_wdt())
  337. goto found;
  338. }
  339. printk (KERN_ERR PFX "No W83697HF/HG could be found\n");
  340. ret = -EIO;
  341. goto out;
  342. found:
  343. if (wdt_set_heartbeat(timeout)) {
  344. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  345. printk (KERN_INFO PFX "timeout value must be 1<=timeout<=255, using %d\n",
  346. WATCHDOG_TIMEOUT);
  347. }
  348. w83697hf_init();
  349. ret = register_reboot_notifier(&wdt_notifier);
  350. if (ret != 0) {
  351. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  352. ret);
  353. goto unreg_regions;
  354. }
  355. ret = misc_register(&wdt_miscdev);
  356. if (ret != 0) {
  357. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  358. WATCHDOG_MINOR, ret);
  359. goto unreg_reboot;
  360. }
  361. printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  362. timeout, nowayout);
  363. out:
  364. return ret;
  365. unreg_reboot:
  366. unregister_reboot_notifier(&wdt_notifier);
  367. unreg_regions:
  368. release_region(wdt_io, 2);
  369. goto out;
  370. }
  371. static void __exit
  372. wdt_exit(void)
  373. {
  374. misc_deregister(&wdt_miscdev);
  375. unregister_reboot_notifier(&wdt_notifier);
  376. release_region(wdt_io, 2);
  377. }
  378. module_init(wdt_init);
  379. module_exit(wdt_exit);
  380. MODULE_LICENSE("GPL");
  381. MODULE_AUTHOR("Marcus Junker <junker@anduras.de>");
  382. MODULE_DESCRIPTION("w83697hf/hg WDT driver");
  383. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);