watchdog.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 0x4e
  35. #define VAL 0x4f
  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(0xaa, 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. /* Internal function, should be called after superio_select(GPIO) */
  134. static void _wdt_update_timeout(unsigned int t)
  135. {
  136. unsigned char cfg = WDT_KRST;
  137. if (testmode)
  138. cfg = 0;
  139. if (t <= max_units)
  140. cfg |= WDT_TOV1;
  141. else
  142. t /= 60;
  143. if (chip_type != IT8721_ID)
  144. cfg |= WDT_PWROK;
  145. superio_outb(cfg, WDTCFG);
  146. superio_outb(t, WDTVALLSB);
  147. if (max_units > 255)
  148. superio_outb(t >> 8, WDTVALMSB);
  149. else
  150. superio_outb(0, WDTVALMSB);
  151. }
  152. static int wdt_update_timeout(unsigned int t)
  153. {
  154. int ret;
  155. ret = superio_enter();
  156. if (ret)
  157. return ret;
  158. superio_select(GPIO);
  159. _wdt_update_timeout(t);
  160. superio_exit();
  161. return 0;
  162. }
  163. static int wdt_round_time(int t)
  164. {
  165. t += 59;
  166. t -= t % 60;
  167. return t;
  168. }
  169. /* watchdog timer handling */
  170. static int wdt_start(struct watchdog_device *wdd)
  171. {
  172. return wdt_update_timeout(wdd->timeout);
  173. }
  174. static int wdt_stop(struct watchdog_device *wdd)
  175. {
  176. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  177. return wdt_update_timeout(0);
  178. }
  179. /**
  180. * wdt_set_timeout - set a new timeout value with watchdog ioctl
  181. * @t: timeout value in seconds
  182. *
  183. * The hardware device has a 8 or 16 bit watchdog timer (depends on
  184. * chip version) that can be configured to count seconds or minutes.
  185. *
  186. * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
  187. */
  188. static int wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
  189. {
  190. int ret = 0;
  191. if (t > max_units)
  192. t = wdt_round_time(t);
  193. wdd->timeout = t;
  194. if (watchdog_hw_running(wdd))
  195. ret = wdt_update_timeout(t);
  196. return ret;
  197. }
  198. static const struct watchdog_info ident = {
  199. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  200. .firmware_version = 1,
  201. .identity = WATCHDOG_NAME,
  202. };
  203. static const struct watchdog_ops wdt_ops = {
  204. .owner = THIS_MODULE,
  205. .start = wdt_start,
  206. .stop = wdt_stop,
  207. .set_timeout = wdt_set_timeout,
  208. };
  209. static struct watchdog_device wdt_dev = {
  210. .info = &ident,
  211. .ops = &wdt_ops,
  212. .min_timeout = 1,
  213. };
  214. int watchdog_init(void)
  215. {
  216. u8 chip_rev;
  217. int rc;
  218. rc = superio_enter();
  219. if (rc)
  220. return rc;
  221. chip_type = superio_inw(CHIPID);
  222. // chip_type = 0x8786;
  223. chip_rev = superio_inb(CHIPREV) & 0x0f;
  224. superio_exit();
  225. switch (chip_type) {
  226. case IT8702_ID:
  227. max_units = 255;
  228. break;
  229. case IT8712_ID:
  230. max_units = (chip_rev < 8) ? 255 : 65535;
  231. break;
  232. case IT8716_ID:
  233. case IT8726_ID:
  234. max_units = 65535;
  235. break;
  236. case IT8607_ID:
  237. case IT8620_ID:
  238. case IT8622_ID:
  239. case IT8625_ID:
  240. case IT8628_ID:
  241. case IT8655_ID:
  242. case IT8665_ID:
  243. case IT8686_ID:
  244. case IT8718_ID:
  245. case IT8720_ID:
  246. case IT8721_ID:
  247. case IT8728_ID:
  248. case IT8772_ID:
  249. case IT8783_ID:
  250. case IT8784_ID:
  251. case IT8786_ID:
  252. max_units = 65535;
  253. break;
  254. case IT8705_ID:
  255. pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
  256. chip_type, chip_rev);
  257. return -ENODEV;
  258. case NO_DEV_ID:
  259. pr_err("no device\n");
  260. return -ENODEV;
  261. default:
  262. pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
  263. chip_type, chip_rev);
  264. return -ENODEV;
  265. }
  266. rc = superio_enter();
  267. if (rc)
  268. return rc;
  269. superio_select(GPIO);
  270. superio_outb(WDT_TOV1, WDTCFG);
  271. superio_outb(0x00, WDTCTRL);
  272. superio_exit();
  273. if (timeout < 1 || timeout > max_units * 60) {
  274. timeout = DEFAULT_TIMEOUT;
  275. pr_warn("Timeout value out of range, use default %d sec\n",
  276. DEFAULT_TIMEOUT);
  277. }
  278. if (timeout > max_units)
  279. timeout = wdt_round_time(timeout);
  280. wdt_dev.timeout = timeout;
  281. wdt_dev.max_timeout = max_units * 60;
  282. watchdog_stop_on_reboot(&wdt_dev);
  283. rc = watchdog_register_device(&wdt_dev);
  284. if (rc) {
  285. pr_err("Cannot register watchdog device (err=%d)\n", rc);
  286. return rc;
  287. }
  288. pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
  289. chip_type, chip_rev, timeout, nowayout, testmode);
  290. return 0;
  291. }
  292. void watchdog_exit(void)
  293. {
  294. watchdog_unregister_device(&wdt_dev);
  295. }