it87_wdt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Watchdog Timer Driver
  3. * for ITE IT87xx Environment Control - Low Pin Count Input / Output
  4. *
  5. * (c) Copyright 2007 Oliver Schuster <olivers137@aol.com>
  6. *
  7. * Based on softdog.c by Alan Cox,
  8. * 83977f_wdt.c by Jose Goncalves,
  9. * it87.c by Chris Gauthron, Jean Delvare
  10. *
  11. * Data-sheets: Publicly available at the ITE website
  12. * http://www.ite.com.tw/
  13. *
  14. * Support of the watchdog timers, which are available on
  15. * IT8607, IT8620, IT8622, IT8625, IT8628, IT8655, IT8665, IT8686,
  16. * IT8702, IT8712, IT8716, IT8718, IT8720, IT8721, IT8726, IT8728,
  17. * and IT8783.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version
  22. * 2 of the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/init.h>
  31. #include <linux/io.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/types.h>
  36. #include <linux/watchdog.h>
  37. #define WATCHDOG_NAME "IT87 WDT"
  38. /* Defaults for Module Parameter */
  39. #define DEFAULT_TIMEOUT 60
  40. #define DEFAULT_TESTMODE 0
  41. #define DEFAULT_NOWAYOUT WATCHDOG_NOWAYOUT
  42. /* IO Ports */
  43. #define REG 0x2e
  44. #define VAL 0x2f
  45. /* Logical device Numbers LDN */
  46. #define GPIO 0x07
  47. /* Configuration Registers and Functions */
  48. #define LDNREG 0x07
  49. #define CHIPID 0x20
  50. #define CHIPREV 0x22
  51. /* Chip Id numbers */
  52. #define NO_DEV_ID 0xffff
  53. #define IT8607_ID 0x8607
  54. #define IT8620_ID 0x8620
  55. #define IT8622_ID 0x8622
  56. #define IT8625_ID 0x8625
  57. #define IT8628_ID 0x8628
  58. #define IT8655_ID 0x8655
  59. #define IT8665_ID 0x8665
  60. #define IT8686_ID 0x8686
  61. #define IT8702_ID 0x8702
  62. #define IT8705_ID 0x8705
  63. #define IT8712_ID 0x8712
  64. #define IT8716_ID 0x8716
  65. #define IT8718_ID 0x8718
  66. #define IT8720_ID 0x8720
  67. #define IT8721_ID 0x8721
  68. #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
  69. #define IT8728_ID 0x8728
  70. #define IT8783_ID 0x8783
  71. /* GPIO Configuration Registers LDN=0x07 */
  72. #define WDTCTRL 0x71
  73. #define WDTCFG 0x72
  74. #define WDTVALLSB 0x73
  75. #define WDTVALMSB 0x74
  76. /* GPIO Bits WDTCFG */
  77. #define WDT_TOV1 0x80
  78. #define WDT_KRST 0x40
  79. #define WDT_TOVE 0x20
  80. #define WDT_PWROK 0x10 /* not in it8721 */
  81. #define WDT_INT_MASK 0x0f
  82. static unsigned int max_units, chip_type;
  83. static unsigned int timeout = DEFAULT_TIMEOUT;
  84. static int testmode = DEFAULT_TESTMODE;
  85. static bool nowayout = DEFAULT_NOWAYOUT;
  86. module_param(timeout, int, 0);
  87. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
  88. __MODULE_STRING(DEFAULT_TIMEOUT));
  89. module_param(testmode, int, 0);
  90. MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
  91. __MODULE_STRING(DEFAULT_TESTMODE));
  92. module_param(nowayout, bool, 0);
  93. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
  94. __MODULE_STRING(WATCHDOG_NOWAYOUT));
  95. /* Superio Chip */
  96. static inline int superio_enter(void)
  97. {
  98. /*
  99. * Try to reserve REG and REG + 1 for exclusive access.
  100. */
  101. if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
  102. return -EBUSY;
  103. outb(0x87, REG);
  104. outb(0x01, REG);
  105. outb(0x55, REG);
  106. outb(0x55, REG);
  107. return 0;
  108. }
  109. static inline void superio_exit(void)
  110. {
  111. outb(0x02, REG);
  112. outb(0x02, VAL);
  113. release_region(REG, 2);
  114. }
  115. static inline void superio_select(int ldn)
  116. {
  117. outb(LDNREG, REG);
  118. outb(ldn, VAL);
  119. }
  120. static inline int superio_inb(int reg)
  121. {
  122. outb(reg, REG);
  123. return inb(VAL);
  124. }
  125. static inline void superio_outb(int val, int reg)
  126. {
  127. outb(reg, REG);
  128. outb(val, VAL);
  129. }
  130. static inline int superio_inw(int reg)
  131. {
  132. int val;
  133. outb(reg++, REG);
  134. val = inb(VAL) << 8;
  135. outb(reg, REG);
  136. val |= inb(VAL);
  137. return val;
  138. }
  139. static inline void superio_outw(int val, int reg)
  140. {
  141. outb(reg++, REG);
  142. outb(val >> 8, VAL);
  143. outb(reg, REG);
  144. outb(val, VAL);
  145. }
  146. /* Internal function, should be called after superio_select(GPIO) */
  147. static void _wdt_update_timeout(unsigned int t)
  148. {
  149. unsigned char cfg = WDT_KRST;
  150. if (testmode)
  151. cfg = 0;
  152. if (t <= max_units)
  153. cfg |= WDT_TOV1;
  154. else
  155. t /= 60;
  156. if (chip_type != IT8721_ID)
  157. cfg |= WDT_PWROK;
  158. superio_outb(cfg, WDTCFG);
  159. superio_outb(t, WDTVALLSB);
  160. if (max_units > 255)
  161. superio_outb(t >> 8, WDTVALMSB);
  162. }
  163. static int wdt_update_timeout(unsigned int t)
  164. {
  165. int ret;
  166. ret = superio_enter();
  167. if (ret)
  168. return ret;
  169. superio_select(GPIO);
  170. _wdt_update_timeout(t);
  171. superio_exit();
  172. return 0;
  173. }
  174. static int wdt_round_time(int t)
  175. {
  176. t += 59;
  177. t -= t % 60;
  178. return t;
  179. }
  180. /* watchdog timer handling */
  181. static int wdt_start(struct watchdog_device *wdd)
  182. {
  183. return wdt_update_timeout(wdd->timeout);
  184. }
  185. static int wdt_stop(struct watchdog_device *wdd)
  186. {
  187. return wdt_update_timeout(0);
  188. }
  189. /**
  190. * wdt_set_timeout - set a new timeout value with watchdog ioctl
  191. * @t: timeout value in seconds
  192. *
  193. * The hardware device has a 8 or 16 bit watchdog timer (depends on
  194. * chip version) that can be configured to count seconds or minutes.
  195. *
  196. * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
  197. */
  198. static int wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
  199. {
  200. int ret = 0;
  201. if (t > max_units)
  202. t = wdt_round_time(t);
  203. wdd->timeout = t;
  204. if (watchdog_hw_running(wdd))
  205. ret = wdt_update_timeout(t);
  206. return ret;
  207. }
  208. static const struct watchdog_info ident = {
  209. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  210. .firmware_version = 1,
  211. .identity = WATCHDOG_NAME,
  212. };
  213. static const struct watchdog_ops wdt_ops = {
  214. .owner = THIS_MODULE,
  215. .start = wdt_start,
  216. .stop = wdt_stop,
  217. .set_timeout = wdt_set_timeout,
  218. };
  219. static struct watchdog_device wdt_dev = {
  220. .info = &ident,
  221. .ops = &wdt_ops,
  222. .min_timeout = 1,
  223. };
  224. static int __init it87_wdt_init(void)
  225. {
  226. u8 chip_rev;
  227. int rc;
  228. rc = superio_enter();
  229. if (rc)
  230. return rc;
  231. chip_type = superio_inw(CHIPID);
  232. chip_rev = superio_inb(CHIPREV) & 0x0f;
  233. superio_exit();
  234. switch (chip_type) {
  235. case IT8702_ID:
  236. max_units = 255;
  237. break;
  238. case IT8712_ID:
  239. max_units = (chip_rev < 8) ? 255 : 65535;
  240. break;
  241. case IT8716_ID:
  242. case IT8726_ID:
  243. max_units = 65535;
  244. break;
  245. case IT8607_ID:
  246. case IT8620_ID:
  247. case IT8622_ID:
  248. case IT8625_ID:
  249. case IT8628_ID:
  250. case IT8655_ID:
  251. case IT8665_ID:
  252. case IT8686_ID:
  253. case IT8718_ID:
  254. case IT8720_ID:
  255. case IT8721_ID:
  256. case IT8728_ID:
  257. case IT8783_ID:
  258. max_units = 65535;
  259. break;
  260. case IT8705_ID:
  261. pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
  262. chip_type, chip_rev);
  263. return -ENODEV;
  264. case NO_DEV_ID:
  265. pr_err("no device\n");
  266. return -ENODEV;
  267. default:
  268. pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
  269. chip_type, chip_rev);
  270. return -ENODEV;
  271. }
  272. rc = superio_enter();
  273. if (rc)
  274. return rc;
  275. superio_select(GPIO);
  276. superio_outb(WDT_TOV1, WDTCFG);
  277. superio_outb(0x00, WDTCTRL);
  278. superio_exit();
  279. if (timeout < 1 || timeout > max_units * 60) {
  280. timeout = DEFAULT_TIMEOUT;
  281. pr_warn("Timeout value out of range, use default %d sec\n",
  282. DEFAULT_TIMEOUT);
  283. }
  284. if (timeout > max_units)
  285. timeout = wdt_round_time(timeout);
  286. wdt_dev.timeout = timeout;
  287. wdt_dev.max_timeout = max_units * 60;
  288. watchdog_stop_on_reboot(&wdt_dev);
  289. rc = watchdog_register_device(&wdt_dev);
  290. if (rc) {
  291. pr_err("Cannot register watchdog device (err=%d)\n", rc);
  292. return rc;
  293. }
  294. pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
  295. chip_type, chip_rev, timeout, nowayout, testmode);
  296. return 0;
  297. }
  298. static void __exit it87_wdt_exit(void)
  299. {
  300. watchdog_unregister_device(&wdt_dev);
  301. }
  302. module_init(it87_wdt_init);
  303. module_exit(it87_wdt_exit);
  304. MODULE_AUTHOR("Oliver Schuster");
  305. MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
  306. MODULE_LICENSE("GPL");