w83627hf_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. t = inb(WDT_EFDR);
  96. if (!(t & 0x01))
  97. outb_p(t | 0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
  98. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  99. t = inb_p(WDT_EFDR); /* read CRF6 */
  100. if (t != 0) {
  101. pr_info("Watchdog already running. Resetting timeout to %d sec\n",
  102. wdog->timeout);
  103. outb_p(wdog->timeout, WDT_EFDR); /* Write back to CRF6 */
  104. }
  105. outb_p(0xF5, WDT_EFER); /* Select CRF5 */
  106. t = inb_p(WDT_EFDR); /* read CRF5 */
  107. t &= ~0x0C; /* set second mode & disable keyboard
  108. turning off watchdog */
  109. t |= 0x02; /* enable the WDTO# output low pulse
  110. to the KBRST# pin (PIN60) */
  111. outb_p(t, WDT_EFDR); /* Write back to CRF5 */
  112. outb_p(0xF7, WDT_EFER); /* Select CRF7 */
  113. t = inb_p(WDT_EFDR); /* read CRF7 */
  114. t &= ~0xC0; /* disable keyboard & mouse turning off
  115. watchdog */
  116. outb_p(t, WDT_EFDR); /* Write back to CRF7 */
  117. w83627hf_unselect_wd_register();
  118. }
  119. static int wdt_set_time(unsigned int timeout)
  120. {
  121. w83627hf_select_wd_register();
  122. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  123. outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF6 */
  124. w83627hf_unselect_wd_register();
  125. return 0;
  126. }
  127. static int wdt_start(struct watchdog_device *wdog)
  128. {
  129. return wdt_set_time(wdog->timeout);
  130. }
  131. static int wdt_stop(struct watchdog_device *wdog)
  132. {
  133. return wdt_set_time(0);
  134. }
  135. static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
  136. {
  137. wdog->timeout = timeout;
  138. return 0;
  139. }
  140. static unsigned int wdt_get_time(struct watchdog_device *wdog)
  141. {
  142. unsigned int timeleft;
  143. w83627hf_select_wd_register();
  144. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  145. timeleft = inb_p(WDT_EFDR); /* Read Timeout counter to CRF6 */
  146. w83627hf_unselect_wd_register();
  147. return timeleft;
  148. }
  149. /*
  150. * Notifier for system down
  151. */
  152. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  153. void *unused)
  154. {
  155. if (code == SYS_DOWN || code == SYS_HALT)
  156. wdt_set_time(0); /* Turn the WDT off */
  157. return NOTIFY_DONE;
  158. }
  159. /*
  160. * Kernel Interfaces
  161. */
  162. static struct watchdog_info wdt_info = {
  163. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  164. .identity = "W83627HF Watchdog",
  165. };
  166. static struct watchdog_ops wdt_ops = {
  167. .owner = THIS_MODULE,
  168. .start = wdt_start,
  169. .stop = wdt_stop,
  170. .set_timeout = wdt_set_timeout,
  171. .get_timeleft = wdt_get_time,
  172. };
  173. static struct watchdog_device wdt_dev = {
  174. .info = &wdt_info,
  175. .ops = &wdt_ops,
  176. .timeout = WATCHDOG_TIMEOUT,
  177. .min_timeout = 1,
  178. .max_timeout = 255,
  179. };
  180. /*
  181. * The WDT needs to learn about soft shutdowns in order to
  182. * turn the timebomb registers off.
  183. */
  184. static struct notifier_block wdt_notifier = {
  185. .notifier_call = wdt_notify_sys,
  186. };
  187. static int __init wdt_init(void)
  188. {
  189. int ret;
  190. pr_info("WDT driver for the Winbond(TM) W83627HF/THF/HG/DHG Super I/O chip initialising\n");
  191. if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
  192. pr_err("I/O address 0x%04x already in use\n", wdt_io);
  193. return -EIO;
  194. }
  195. watchdog_init_timeout(&wdt_dev, timeout, NULL);
  196. watchdog_set_nowayout(&wdt_dev, nowayout);
  197. w83627hf_init(&wdt_dev);
  198. ret = register_reboot_notifier(&wdt_notifier);
  199. if (ret != 0) {
  200. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  201. goto unreg_regions;
  202. }
  203. ret = watchdog_register_device(&wdt_dev);
  204. if (ret)
  205. goto unreg_reboot;
  206. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  207. wdt_dev.timeout, nowayout);
  208. return ret;
  209. unreg_reboot:
  210. unregister_reboot_notifier(&wdt_notifier);
  211. unreg_regions:
  212. release_region(wdt_io, 1);
  213. return ret;
  214. }
  215. static void __exit wdt_exit(void)
  216. {
  217. watchdog_unregister_device(&wdt_dev);
  218. unregister_reboot_notifier(&wdt_notifier);
  219. release_region(wdt_io, 1);
  220. }
  221. module_init(wdt_init);
  222. module_exit(wdt_exit);
  223. MODULE_LICENSE("GPL");
  224. MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
  225. MODULE_DESCRIPTION("w83627hf/thf WDT driver");