w83627hf_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * w83627hf/thf WDT driver
  3. *
  4. * (c) Copyright 2013 Guenter Roeck
  5. * converted to watchdog infrastructure
  6. *
  7. * (c) Copyright 2007 Vlad Drukker <vlad@storewiz.com>
  8. * added support for W83627THF.
  9. *
  10. * (c) Copyright 2003,2007 Pádraig Brady <P@draigBrady.com>
  11. *
  12. * Based on advantechwdt.c which is based on wdt.c.
  13. * Original copyright messages:
  14. *
  15. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  16. *
  17. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  18. * All Rights Reserved.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version
  23. * 2 of the License, or (at your option) any later version.
  24. *
  25. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  26. * warranty for any of this software. This material is provided
  27. * "AS-IS" and at no charge.
  28. *
  29. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/types.h>
  35. #include <linux/watchdog.h>
  36. #include <linux/ioport.h>
  37. #include <linux/notifier.h>
  38. #include <linux/reboot.h>
  39. #include <linux/init.h>
  40. #include <linux/io.h>
  41. #define WATCHDOG_NAME "w83627hf/thf/hg/dhg WDT"
  42. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  43. /* You must set this - there is no sane way to probe for this board. */
  44. static int wdt_io = 0x2E;
  45. module_param(wdt_io, int, 0);
  46. MODULE_PARM_DESC(wdt_io, "w83627hf/thf WDT io port (default 0x2E)");
  47. static int timeout; /* in seconds */
  48. module_param(timeout, int, 0);
  49. MODULE_PARM_DESC(timeout,
  50. "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
  51. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  52. static bool nowayout = WATCHDOG_NOWAYOUT;
  53. module_param(nowayout, bool, 0);
  54. MODULE_PARM_DESC(nowayout,
  55. "Watchdog cannot be stopped once started (default="
  56. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  57. /*
  58. * Kernel methods.
  59. */
  60. #define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
  61. #define WDT_EFIR (wdt_io+0) /* Extended Function Index Register
  62. (same as EFER) */
  63. #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
  64. static void w83627hf_select_wd_register(void)
  65. {
  66. outb_p(0x87, WDT_EFER); /* Enter extended function mode */
  67. outb_p(0x87, WDT_EFER); /* Again according to manual */
  68. outb_p(0x07, WDT_EFER); /* point to logical device number reg */
  69. outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
  70. }
  71. static void w83627hf_unselect_wd_register(void)
  72. {
  73. outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
  74. }
  75. /* tyan motherboards seem to set F5 to 0x4C ?
  76. * So explicitly init to appropriate value. */
  77. static void w83627hf_init(struct watchdog_device *wdog)
  78. {
  79. unsigned char t;
  80. w83627hf_select_wd_register();
  81. outb(0x20, WDT_EFER); /* check chip version */
  82. t = inb(WDT_EFDR);
  83. if (t == 0x82) { /* W83627THF */
  84. outb_p(0x2b, WDT_EFER); /* select GPIO3 */
  85. t = ((inb_p(WDT_EFDR) & 0xf7) | 0x04); /* select WDT0 */
  86. outb_p(0x2b, WDT_EFER);
  87. outb_p(t, WDT_EFDR); /* set GPIO3 to WDT0 */
  88. } else if (t == 0x88 || t == 0xa0) { /* W83627EHF / W83627DHG */
  89. outb_p(0x2d, WDT_EFER); /* select GPIO5 */
  90. t = inb_p(WDT_EFDR) & ~0x01; /* PIN77 -> WDT0# */
  91. outb_p(0x2d, WDT_EFER);
  92. outb_p(t, WDT_EFDR); /* set GPIO5 to WDT0 */
  93. }
  94. outb_p(0x30, WDT_EFER); /* select CR30 */
  95. outb_p(0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
  96. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  97. t = inb_p(WDT_EFDR); /* read CRF6 */
  98. if (t != 0) {
  99. pr_info("Watchdog already running. Resetting timeout to %d sec\n",
  100. wdog->timeout);
  101. outb_p(wdog->timeout, WDT_EFDR); /* Write back to CRF6 */
  102. }
  103. outb_p(0xF5, WDT_EFER); /* Select CRF5 */
  104. t = inb_p(WDT_EFDR); /* read CRF5 */
  105. t &= ~0x0C; /* set second mode & disable keyboard
  106. turning off watchdog */
  107. t |= 0x02; /* enable the WDTO# output low pulse
  108. to the KBRST# pin (PIN60) */
  109. outb_p(t, WDT_EFDR); /* Write back to CRF5 */
  110. outb_p(0xF7, WDT_EFER); /* Select CRF7 */
  111. t = inb_p(WDT_EFDR); /* read CRF7 */
  112. t &= ~0xC0; /* disable keyboard & mouse turning off
  113. watchdog */
  114. outb_p(t, WDT_EFDR); /* Write back to CRF7 */
  115. w83627hf_unselect_wd_register();
  116. }
  117. static int wdt_set_time(unsigned int timeout)
  118. {
  119. w83627hf_select_wd_register();
  120. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  121. outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF6 */
  122. w83627hf_unselect_wd_register();
  123. return 0;
  124. }
  125. static int wdt_start(struct watchdog_device *wdog)
  126. {
  127. return wdt_set_time(wdog->timeout);
  128. }
  129. static int wdt_stop(struct watchdog_device *wdog)
  130. {
  131. return wdt_set_time(0);
  132. }
  133. static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
  134. {
  135. wdog->timeout = timeout;
  136. return 0;
  137. }
  138. static unsigned int wdt_get_time(struct watchdog_device *wdog)
  139. {
  140. unsigned int timeleft;
  141. w83627hf_select_wd_register();
  142. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  143. timeleft = inb_p(WDT_EFDR); /* Read Timeout counter to CRF6 */
  144. w83627hf_unselect_wd_register();
  145. return timeleft;
  146. }
  147. /*
  148. * Notifier for system down
  149. */
  150. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  151. void *unused)
  152. {
  153. if (code == SYS_DOWN || code == SYS_HALT)
  154. wdt_set_time(0); /* Turn the WDT off */
  155. return NOTIFY_DONE;
  156. }
  157. /*
  158. * Kernel Interfaces
  159. */
  160. static struct watchdog_info wdt_info = {
  161. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  162. .identity = "W83627HF Watchdog",
  163. };
  164. static struct watchdog_ops wdt_ops = {
  165. .owner = THIS_MODULE,
  166. .start = wdt_start,
  167. .stop = wdt_stop,
  168. .set_timeout = wdt_set_timeout,
  169. .get_timeleft = wdt_get_time,
  170. };
  171. static struct watchdog_device wdt_dev = {
  172. .info = &wdt_info,
  173. .ops = &wdt_ops,
  174. .timeout = WATCHDOG_TIMEOUT,
  175. .min_timeout = 1,
  176. .max_timeout = 255,
  177. };
  178. /*
  179. * The WDT needs to learn about soft shutdowns in order to
  180. * turn the timebomb registers off.
  181. */
  182. static struct notifier_block wdt_notifier = {
  183. .notifier_call = wdt_notify_sys,
  184. };
  185. static int __init wdt_init(void)
  186. {
  187. int ret;
  188. pr_info("WDT driver for the Winbond(TM) W83627HF/THF/HG/DHG Super I/O chip initialising\n");
  189. if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
  190. pr_err("I/O address 0x%04x already in use\n", wdt_io);
  191. return -EIO;
  192. }
  193. watchdog_init_timeout(&wdt_dev, timeout, NULL);
  194. watchdog_set_nowayout(&wdt_dev, nowayout);
  195. w83627hf_init(&wdt_dev);
  196. ret = register_reboot_notifier(&wdt_notifier);
  197. if (ret != 0) {
  198. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  199. goto unreg_regions;
  200. }
  201. ret = watchdog_register_device(&wdt_dev);
  202. if (ret)
  203. goto unreg_reboot;
  204. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  205. wdt_dev.timeout, nowayout);
  206. return ret;
  207. unreg_reboot:
  208. unregister_reboot_notifier(&wdt_notifier);
  209. unreg_regions:
  210. release_region(wdt_io, 1);
  211. return ret;
  212. }
  213. static void __exit wdt_exit(void)
  214. {
  215. watchdog_unregister_device(&wdt_dev);
  216. unregister_reboot_notifier(&wdt_notifier);
  217. release_region(wdt_io, 1);
  218. }
  219. module_init(wdt_init);
  220. module_exit(wdt_exit);
  221. MODULE_LICENSE("GPL");
  222. MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
  223. MODULE_DESCRIPTION("w83627hf/thf WDT driver");