it87_wdt.c 7.4 KB

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