w83627hf_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * w83627hf/thf WDT driver
  4. *
  5. * (c) Copyright 2013 Guenter Roeck
  6. * converted to watchdog infrastructure
  7. *
  8. * (c) Copyright 2007 Vlad Drukker <vlad@storewiz.com>
  9. * added support for W83627THF.
  10. *
  11. * (c) Copyright 2003,2007 Pádraig Brady <P@draigBrady.com>
  12. *
  13. * Based on advantechwdt.c which is based on wdt.c.
  14. * Original copyright messages:
  15. *
  16. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  17. *
  18. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  19. * All Rights Reserved.
  20. *
  21. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  22. * warranty for any of this software. This material is provided
  23. * "AS-IS" and at no charge.
  24. *
  25. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/types.h>
  31. #include <linux/watchdog.h>
  32. #include <linux/ioport.h>
  33. #include <linux/init.h>
  34. #include <linux/io.h>
  35. #include <linux/dmi.h>
  36. #define WATCHDOG_NAME "w83627hf/thf/hg/dhg WDT"
  37. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  38. static int wdt_io;
  39. static int cr_wdt_timeout; /* WDT timeout register */
  40. static int cr_wdt_control; /* WDT control register */
  41. static int cr_wdt_csr; /* WDT control & status register */
  42. static int wdt_cfg_enter = 0x87;/* key to unlock configuration space */
  43. static int wdt_cfg_leave = 0xAA;/* key to lock configuration space */
  44. enum chips { w83627hf, w83627s, w83697hf, w83697ug, w83637hf, w83627thf,
  45. w83687thf, w83627ehf, w83627dhg, w83627uhg, w83667hg, w83627dhg_p,
  46. w83667hg_b, nct6775, nct6776, nct6779, nct6791, nct6792, nct6793,
  47. nct6795, nct6796, nct6102 };
  48. static int timeout; /* in seconds */
  49. module_param(timeout, int, 0);
  50. MODULE_PARM_DESC(timeout,
  51. "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
  52. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  53. static bool nowayout = WATCHDOG_NOWAYOUT;
  54. module_param(nowayout, bool, 0);
  55. MODULE_PARM_DESC(nowayout,
  56. "Watchdog cannot be stopped once started (default="
  57. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  58. static int early_disable;
  59. module_param(early_disable, int, 0);
  60. MODULE_PARM_DESC(early_disable, "Disable watchdog at boot time (default=0)");
  61. /*
  62. * Kernel methods.
  63. */
  64. #define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
  65. #define WDT_EFIR (wdt_io+0) /* Extended Function Index Register
  66. (same as EFER) */
  67. #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
  68. #define W83627HF_LD_WDT 0x08
  69. #define W83627HF_ID 0x52
  70. #define W83627S_ID 0x59
  71. #define W83697HF_ID 0x60
  72. #define W83697UG_ID 0x68
  73. #define W83637HF_ID 0x70
  74. #define W83627THF_ID 0x82
  75. #define W83687THF_ID 0x85
  76. #define W83627EHF_ID 0x88
  77. #define W83627DHG_ID 0xa0
  78. #define W83627UHG_ID 0xa2
  79. #define W83667HG_ID 0xa5
  80. #define W83627DHG_P_ID 0xb0
  81. #define W83667HG_B_ID 0xb3
  82. #define NCT6775_ID 0xb4
  83. #define NCT6776_ID 0xc3
  84. #define NCT6102_ID 0xc4
  85. #define NCT6779_ID 0xc5
  86. #define NCT6791_ID 0xc8
  87. #define NCT6792_ID 0xc9
  88. #define NCT6793_ID 0xd1
  89. #define NCT6795_ID 0xd3
  90. #define NCT6796_ID 0xd4 /* also NCT9697D, NCT9698D */
  91. #define W83627HF_WDT_TIMEOUT 0xf6
  92. #define W83697HF_WDT_TIMEOUT 0xf4
  93. #define NCT6102D_WDT_TIMEOUT 0xf1
  94. #define W83627HF_WDT_CONTROL 0xf5
  95. #define W83697HF_WDT_CONTROL 0xf3
  96. #define NCT6102D_WDT_CONTROL 0xf0
  97. #define W836X7HF_WDT_CSR 0xf7
  98. #define NCT6102D_WDT_CSR 0xf2
  99. static void superio_outb(int reg, int val)
  100. {
  101. outb(reg, WDT_EFER);
  102. outb(val, WDT_EFDR);
  103. }
  104. static inline int superio_inb(int reg)
  105. {
  106. outb(reg, WDT_EFER);
  107. return inb(WDT_EFDR);
  108. }
  109. static int superio_enter(void)
  110. {
  111. if (!request_muxed_region(wdt_io, 2, WATCHDOG_NAME))
  112. return -EBUSY;
  113. outb_p(wdt_cfg_enter, WDT_EFER); /* Enter extended function mode */
  114. outb_p(wdt_cfg_enter, WDT_EFER); /* Again according to manual */
  115. return 0;
  116. }
  117. static void superio_select(int ld)
  118. {
  119. superio_outb(0x07, ld);
  120. }
  121. static void superio_exit(void)
  122. {
  123. outb_p(wdt_cfg_leave, WDT_EFER); /* Leave extended function mode */
  124. release_region(wdt_io, 2);
  125. }
  126. static int w83627hf_init(struct watchdog_device *wdog, enum chips chip)
  127. {
  128. int ret;
  129. unsigned char t;
  130. ret = superio_enter();
  131. if (ret)
  132. return ret;
  133. superio_select(W83627HF_LD_WDT);
  134. /* set CR30 bit 0 to activate GPIO2 */
  135. t = superio_inb(0x30);
  136. if (!(t & 0x01))
  137. superio_outb(0x30, t | 0x01);
  138. switch (chip) {
  139. case w83627hf:
  140. case w83627s:
  141. t = superio_inb(0x2B) & ~0x10;
  142. superio_outb(0x2B, t); /* set GPIO24 to WDT0 */
  143. break;
  144. case w83697hf:
  145. /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
  146. t = superio_inb(0x29) & ~0x60;
  147. t |= 0x20;
  148. superio_outb(0x29, t);
  149. break;
  150. case w83697ug:
  151. /* Set pin 118 to WDTO# mode */
  152. t = superio_inb(0x2b) & ~0x04;
  153. superio_outb(0x2b, t);
  154. break;
  155. case w83627thf:
  156. t = (superio_inb(0x2B) & ~0x08) | 0x04;
  157. superio_outb(0x2B, t); /* set GPIO3 to WDT0 */
  158. break;
  159. case w83627dhg:
  160. case w83627dhg_p:
  161. t = superio_inb(0x2D) & ~0x01; /* PIN77 -> WDT0# */
  162. superio_outb(0x2D, t); /* set GPIO5 to WDT0 */
  163. t = superio_inb(cr_wdt_control);
  164. t |= 0x02; /* enable the WDTO# output low pulse
  165. * to the KBRST# pin */
  166. superio_outb(cr_wdt_control, t);
  167. break;
  168. case w83637hf:
  169. break;
  170. case w83687thf:
  171. t = superio_inb(0x2C) & ~0x80; /* PIN47 -> WDT0# */
  172. superio_outb(0x2C, t);
  173. break;
  174. case w83627ehf:
  175. case w83627uhg:
  176. case w83667hg:
  177. case w83667hg_b:
  178. case nct6775:
  179. case nct6776:
  180. case nct6779:
  181. case nct6791:
  182. case nct6792:
  183. case nct6793:
  184. case nct6795:
  185. case nct6796:
  186. case nct6102:
  187. /*
  188. * These chips have a fixed WDTO# output pin (W83627UHG),
  189. * or support more than one WDTO# output pin.
  190. * Don't touch its configuration, and hope the BIOS
  191. * does the right thing.
  192. */
  193. t = superio_inb(cr_wdt_control);
  194. t |= 0x02; /* enable the WDTO# output low pulse
  195. * to the KBRST# pin */
  196. superio_outb(cr_wdt_control, t);
  197. break;
  198. default:
  199. break;
  200. }
  201. t = superio_inb(cr_wdt_timeout);
  202. if (t != 0) {
  203. if (early_disable) {
  204. pr_warn("Stopping previously enabled watchdog until userland kicks in\n");
  205. superio_outb(cr_wdt_timeout, 0);
  206. } else {
  207. pr_info("Watchdog already running. Resetting timeout to %d sec\n",
  208. wdog->timeout);
  209. superio_outb(cr_wdt_timeout, wdog->timeout);
  210. }
  211. }
  212. /* set second mode & disable keyboard turning off watchdog */
  213. t = superio_inb(cr_wdt_control) & ~0x0C;
  214. superio_outb(cr_wdt_control, t);
  215. /* reset trigger, disable keyboard & mouse turning off watchdog */
  216. t = superio_inb(cr_wdt_csr) & ~0xD0;
  217. superio_outb(cr_wdt_csr, t);
  218. superio_exit();
  219. return 0;
  220. }
  221. static int wdt_set_time(unsigned int timeout)
  222. {
  223. int ret;
  224. ret = superio_enter();
  225. if (ret)
  226. return ret;
  227. superio_select(W83627HF_LD_WDT);
  228. superio_outb(cr_wdt_timeout, timeout);
  229. superio_exit();
  230. return 0;
  231. }
  232. static int wdt_start(struct watchdog_device *wdog)
  233. {
  234. return wdt_set_time(wdog->timeout);
  235. }
  236. static int wdt_stop(struct watchdog_device *wdog)
  237. {
  238. return wdt_set_time(0);
  239. }
  240. static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
  241. {
  242. wdog->timeout = timeout;
  243. return 0;
  244. }
  245. static unsigned int wdt_get_time(struct watchdog_device *wdog)
  246. {
  247. unsigned int timeleft;
  248. int ret;
  249. ret = superio_enter();
  250. if (ret)
  251. return 0;
  252. superio_select(W83627HF_LD_WDT);
  253. timeleft = superio_inb(cr_wdt_timeout);
  254. superio_exit();
  255. return timeleft;
  256. }
  257. /*
  258. * Kernel Interfaces
  259. */
  260. static const struct watchdog_info wdt_info = {
  261. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  262. .identity = "W83627HF Watchdog",
  263. };
  264. static const struct watchdog_ops wdt_ops = {
  265. .owner = THIS_MODULE,
  266. .start = wdt_start,
  267. .stop = wdt_stop,
  268. .set_timeout = wdt_set_timeout,
  269. .get_timeleft = wdt_get_time,
  270. };
  271. static struct watchdog_device wdt_dev = {
  272. .info = &wdt_info,
  273. .ops = &wdt_ops,
  274. .timeout = WATCHDOG_TIMEOUT,
  275. .min_timeout = 1,
  276. .max_timeout = 255,
  277. };
  278. /*
  279. * The WDT needs to learn about soft shutdowns in order to
  280. * turn the timebomb registers off.
  281. */
  282. static int wdt_find(int addr)
  283. {
  284. u8 val;
  285. int ret;
  286. cr_wdt_timeout = W83627HF_WDT_TIMEOUT;
  287. cr_wdt_control = W83627HF_WDT_CONTROL;
  288. cr_wdt_csr = W836X7HF_WDT_CSR;
  289. ret = superio_enter();
  290. if (ret)
  291. return ret;
  292. superio_select(W83627HF_LD_WDT);
  293. val = superio_inb(0x20);
  294. switch (val) {
  295. case W83627HF_ID:
  296. ret = w83627hf;
  297. break;
  298. case W83627S_ID:
  299. ret = w83627s;
  300. break;
  301. case W83697HF_ID:
  302. ret = w83697hf;
  303. cr_wdt_timeout = W83697HF_WDT_TIMEOUT;
  304. cr_wdt_control = W83697HF_WDT_CONTROL;
  305. break;
  306. case W83697UG_ID:
  307. ret = w83697ug;
  308. cr_wdt_timeout = W83697HF_WDT_TIMEOUT;
  309. cr_wdt_control = W83697HF_WDT_CONTROL;
  310. break;
  311. case W83637HF_ID:
  312. ret = w83637hf;
  313. break;
  314. case W83627THF_ID:
  315. ret = w83627thf;
  316. break;
  317. case W83687THF_ID:
  318. ret = w83687thf;
  319. break;
  320. case W83627EHF_ID:
  321. ret = w83627ehf;
  322. break;
  323. case W83627DHG_ID:
  324. ret = w83627dhg;
  325. break;
  326. case W83627DHG_P_ID:
  327. ret = w83627dhg_p;
  328. break;
  329. case W83627UHG_ID:
  330. ret = w83627uhg;
  331. break;
  332. case W83667HG_ID:
  333. ret = w83667hg;
  334. break;
  335. case W83667HG_B_ID:
  336. ret = w83667hg_b;
  337. break;
  338. case NCT6775_ID:
  339. ret = nct6775;
  340. break;
  341. case NCT6776_ID:
  342. ret = nct6776;
  343. break;
  344. case NCT6779_ID:
  345. ret = nct6779;
  346. break;
  347. case NCT6791_ID:
  348. ret = nct6791;
  349. break;
  350. case NCT6792_ID:
  351. ret = nct6792;
  352. break;
  353. case NCT6793_ID:
  354. ret = nct6793;
  355. break;
  356. case NCT6795_ID:
  357. ret = nct6795;
  358. break;
  359. case NCT6796_ID:
  360. ret = nct6796;
  361. break;
  362. case NCT6102_ID:
  363. ret = nct6102;
  364. cr_wdt_timeout = NCT6102D_WDT_TIMEOUT;
  365. cr_wdt_control = NCT6102D_WDT_CONTROL;
  366. cr_wdt_csr = NCT6102D_WDT_CSR;
  367. break;
  368. case 0xff:
  369. ret = -ENODEV;
  370. break;
  371. default:
  372. ret = -ENODEV;
  373. pr_err("Unsupported chip ID: 0x%02x\n", val);
  374. break;
  375. }
  376. superio_exit();
  377. return ret;
  378. }
  379. /*
  380. * On some systems, the NCT6791D comes with a companion chip and the
  381. * watchdog function is in this companion chip. We must use a different
  382. * unlocking sequence to access the companion chip.
  383. */
  384. static int __init wdt_use_alt_key(const struct dmi_system_id *d)
  385. {
  386. wdt_cfg_enter = 0x88;
  387. wdt_cfg_leave = 0xBB;
  388. return 0;
  389. }
  390. static const struct dmi_system_id wdt_dmi_table[] __initconst = {
  391. {
  392. .matches = {
  393. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "INVES"),
  394. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "CTS"),
  395. DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "INVES"),
  396. DMI_EXACT_MATCH(DMI_BOARD_NAME, "SHARKBAY"),
  397. },
  398. .callback = wdt_use_alt_key,
  399. },
  400. {}
  401. };
  402. static int __init wdt_init(void)
  403. {
  404. int ret;
  405. int chip;
  406. static const char * const chip_name[] = {
  407. "W83627HF",
  408. "W83627S",
  409. "W83697HF",
  410. "W83697UG",
  411. "W83637HF",
  412. "W83627THF",
  413. "W83687THF",
  414. "W83627EHF",
  415. "W83627DHG",
  416. "W83627UHG",
  417. "W83667HG",
  418. "W83667DHG-P",
  419. "W83667HG-B",
  420. "NCT6775",
  421. "NCT6776",
  422. "NCT6779",
  423. "NCT6791",
  424. "NCT6792",
  425. "NCT6793",
  426. "NCT6795",
  427. "NCT6796",
  428. "NCT6102",
  429. };
  430. /* Apply system-specific quirks */
  431. dmi_check_system(wdt_dmi_table);
  432. wdt_io = 0x2e;
  433. chip = wdt_find(0x2e);
  434. if (chip < 0) {
  435. wdt_io = 0x4e;
  436. chip = wdt_find(0x4e);
  437. if (chip < 0)
  438. return chip;
  439. }
  440. pr_info("WDT driver for %s Super I/O chip initialising\n",
  441. chip_name[chip]);
  442. watchdog_init_timeout(&wdt_dev, timeout, NULL);
  443. watchdog_set_nowayout(&wdt_dev, nowayout);
  444. watchdog_stop_on_reboot(&wdt_dev);
  445. ret = w83627hf_init(&wdt_dev, chip);
  446. if (ret) {
  447. pr_err("failed to initialize watchdog (err=%d)\n", ret);
  448. return ret;
  449. }
  450. ret = watchdog_register_device(&wdt_dev);
  451. if (ret)
  452. return ret;
  453. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  454. wdt_dev.timeout, nowayout);
  455. return ret;
  456. }
  457. static void __exit wdt_exit(void)
  458. {
  459. watchdog_unregister_device(&wdt_dev);
  460. }
  461. module_init(wdt_init);
  462. module_exit(wdt_exit);
  463. MODULE_LICENSE("GPL");
  464. MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
  465. MODULE_DESCRIPTION("w83627hf/thf WDT driver");