w83627hf_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 <[email protected]>
  9. * added support for W83627THF.
  10. *
  11. * (c) Copyright 2003,2007 Pádraig Brady <[email protected]>
  12. *
  13. * Based on advantechwdt.c which is based on wdt.c.
  14. * Original copyright messages:
  15. *
  16. * (c) Copyright 2000-2001 Marek Michalkiewicz <[email protected]>
  17. *
  18. * (c) Copyright 1996 Alan Cox <[email protected]>,
  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 <[email protected]>
  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, nct6116 };
  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 NCT6116_ID 0xd2
  86. #define NCT6779_ID 0xc5
  87. #define NCT6791_ID 0xc8
  88. #define NCT6792_ID 0xc9
  89. #define NCT6793_ID 0xd1
  90. #define NCT6795_ID 0xd3
  91. #define NCT6796_ID 0xd4 /* also NCT9697D, NCT9698D */
  92. #define W83627HF_WDT_TIMEOUT 0xf6
  93. #define W83697HF_WDT_TIMEOUT 0xf4
  94. #define NCT6102D_WDT_TIMEOUT 0xf1
  95. #define W83627HF_WDT_CONTROL 0xf5
  96. #define W83697HF_WDT_CONTROL 0xf3
  97. #define NCT6102D_WDT_CONTROL 0xf0
  98. #define W836X7HF_WDT_CSR 0xf7
  99. #define NCT6102D_WDT_CSR 0xf2
  100. #define WDT_CSR_STATUS 0x10
  101. #define WDT_CSR_KBD 0x40
  102. #define WDT_CSR_MOUSE 0x80
  103. static void superio_outb(int reg, int val)
  104. {
  105. outb(reg, WDT_EFER);
  106. outb(val, WDT_EFDR);
  107. }
  108. static inline int superio_inb(int reg)
  109. {
  110. outb(reg, WDT_EFER);
  111. return inb(WDT_EFDR);
  112. }
  113. static int superio_enter(void)
  114. {
  115. if (!request_muxed_region(wdt_io, 2, WATCHDOG_NAME))
  116. return -EBUSY;
  117. outb_p(wdt_cfg_enter, WDT_EFER); /* Enter extended function mode */
  118. outb_p(wdt_cfg_enter, WDT_EFER); /* Again according to manual */
  119. return 0;
  120. }
  121. static void superio_select(int ld)
  122. {
  123. superio_outb(0x07, ld);
  124. }
  125. static void superio_exit(void)
  126. {
  127. outb_p(wdt_cfg_leave, WDT_EFER); /* Leave extended function mode */
  128. release_region(wdt_io, 2);
  129. }
  130. static int w83627hf_init(struct watchdog_device *wdog, enum chips chip)
  131. {
  132. int ret;
  133. unsigned char t;
  134. ret = superio_enter();
  135. if (ret)
  136. return ret;
  137. superio_select(W83627HF_LD_WDT);
  138. /* set CR30 bit 0 to activate GPIO2 */
  139. t = superio_inb(0x30);
  140. if (!(t & 0x01))
  141. superio_outb(0x30, t | 0x01);
  142. switch (chip) {
  143. case w83627hf:
  144. case w83627s:
  145. t = superio_inb(0x2B) & ~0x10;
  146. superio_outb(0x2B, t); /* set GPIO24 to WDT0 */
  147. break;
  148. case w83697hf:
  149. /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
  150. t = superio_inb(0x29) & ~0x60;
  151. t |= 0x20;
  152. superio_outb(0x29, t);
  153. break;
  154. case w83697ug:
  155. /* Set pin 118 to WDTO# mode */
  156. t = superio_inb(0x2b) & ~0x04;
  157. superio_outb(0x2b, t);
  158. break;
  159. case w83627thf:
  160. t = (superio_inb(0x2B) & ~0x08) | 0x04;
  161. superio_outb(0x2B, t); /* set GPIO3 to WDT0 */
  162. break;
  163. case w83627dhg:
  164. case w83627dhg_p:
  165. t = superio_inb(0x2D) & ~0x01; /* PIN77 -> WDT0# */
  166. superio_outb(0x2D, t); /* set GPIO5 to WDT0 */
  167. t = superio_inb(cr_wdt_control);
  168. t |= 0x02; /* enable the WDTO# output low pulse
  169. * to the KBRST# pin */
  170. superio_outb(cr_wdt_control, t);
  171. break;
  172. case w83637hf:
  173. break;
  174. case w83687thf:
  175. t = superio_inb(0x2C) & ~0x80; /* PIN47 -> WDT0# */
  176. superio_outb(0x2C, t);
  177. break;
  178. case w83627ehf:
  179. case w83627uhg:
  180. case w83667hg:
  181. case w83667hg_b:
  182. case nct6775:
  183. case nct6776:
  184. case nct6779:
  185. case nct6791:
  186. case nct6792:
  187. case nct6793:
  188. case nct6795:
  189. case nct6796:
  190. case nct6102:
  191. case nct6116:
  192. /*
  193. * These chips have a fixed WDTO# output pin (W83627UHG),
  194. * or support more than one WDTO# output pin.
  195. * Don't touch its configuration, and hope the BIOS
  196. * does the right thing.
  197. */
  198. t = superio_inb(cr_wdt_control);
  199. t |= 0x02; /* enable the WDTO# output low pulse
  200. * to the KBRST# pin */
  201. superio_outb(cr_wdt_control, t);
  202. break;
  203. default:
  204. break;
  205. }
  206. t = superio_inb(cr_wdt_timeout);
  207. if (t != 0) {
  208. if (early_disable) {
  209. pr_warn("Stopping previously enabled watchdog until userland kicks in\n");
  210. superio_outb(cr_wdt_timeout, 0);
  211. } else {
  212. pr_info("Watchdog already running. Resetting timeout to %d sec\n",
  213. wdog->timeout);
  214. superio_outb(cr_wdt_timeout, wdog->timeout);
  215. }
  216. }
  217. /* set second mode & disable keyboard turning off watchdog */
  218. t = superio_inb(cr_wdt_control) & ~0x0C;
  219. superio_outb(cr_wdt_control, t);
  220. t = superio_inb(cr_wdt_csr);
  221. if (t & WDT_CSR_STATUS)
  222. wdog->bootstatus |= WDIOF_CARDRESET;
  223. /* reset status, disable keyboard & mouse turning off watchdog */
  224. t &= ~(WDT_CSR_STATUS | WDT_CSR_KBD | WDT_CSR_MOUSE);
  225. superio_outb(cr_wdt_csr, t);
  226. superio_exit();
  227. return 0;
  228. }
  229. static int wdt_set_time(unsigned int timeout)
  230. {
  231. int ret;
  232. ret = superio_enter();
  233. if (ret)
  234. return ret;
  235. superio_select(W83627HF_LD_WDT);
  236. superio_outb(cr_wdt_timeout, timeout);
  237. superio_exit();
  238. return 0;
  239. }
  240. static int wdt_start(struct watchdog_device *wdog)
  241. {
  242. return wdt_set_time(wdog->timeout);
  243. }
  244. static int wdt_stop(struct watchdog_device *wdog)
  245. {
  246. return wdt_set_time(0);
  247. }
  248. static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
  249. {
  250. wdog->timeout = timeout;
  251. return 0;
  252. }
  253. static unsigned int wdt_get_time(struct watchdog_device *wdog)
  254. {
  255. unsigned int timeleft;
  256. int ret;
  257. ret = superio_enter();
  258. if (ret)
  259. return 0;
  260. superio_select(W83627HF_LD_WDT);
  261. timeleft = superio_inb(cr_wdt_timeout);
  262. superio_exit();
  263. return timeleft;
  264. }
  265. /*
  266. * Kernel Interfaces
  267. */
  268. static const struct watchdog_info wdt_info = {
  269. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  270. .identity = "W83627HF Watchdog",
  271. };
  272. static const struct watchdog_ops wdt_ops = {
  273. .owner = THIS_MODULE,
  274. .start = wdt_start,
  275. .stop = wdt_stop,
  276. .set_timeout = wdt_set_timeout,
  277. .get_timeleft = wdt_get_time,
  278. };
  279. static struct watchdog_device wdt_dev = {
  280. .info = &wdt_info,
  281. .ops = &wdt_ops,
  282. .timeout = WATCHDOG_TIMEOUT,
  283. .min_timeout = 1,
  284. .max_timeout = 255,
  285. };
  286. /*
  287. * The WDT needs to learn about soft shutdowns in order to
  288. * turn the timebomb registers off.
  289. */
  290. static int wdt_find(int addr)
  291. {
  292. u8 val;
  293. int ret;
  294. cr_wdt_timeout = W83627HF_WDT_TIMEOUT;
  295. cr_wdt_control = W83627HF_WDT_CONTROL;
  296. cr_wdt_csr = W836X7HF_WDT_CSR;
  297. ret = superio_enter();
  298. if (ret)
  299. return ret;
  300. superio_select(W83627HF_LD_WDT);
  301. val = superio_inb(0x20);
  302. switch (val) {
  303. case W83627HF_ID:
  304. ret = w83627hf;
  305. break;
  306. case W83627S_ID:
  307. ret = w83627s;
  308. break;
  309. case W83697HF_ID:
  310. ret = w83697hf;
  311. cr_wdt_timeout = W83697HF_WDT_TIMEOUT;
  312. cr_wdt_control = W83697HF_WDT_CONTROL;
  313. break;
  314. case W83697UG_ID:
  315. ret = w83697ug;
  316. cr_wdt_timeout = W83697HF_WDT_TIMEOUT;
  317. cr_wdt_control = W83697HF_WDT_CONTROL;
  318. break;
  319. case W83637HF_ID:
  320. ret = w83637hf;
  321. break;
  322. case W83627THF_ID:
  323. ret = w83627thf;
  324. break;
  325. case W83687THF_ID:
  326. ret = w83687thf;
  327. break;
  328. case W83627EHF_ID:
  329. ret = w83627ehf;
  330. break;
  331. case W83627DHG_ID:
  332. ret = w83627dhg;
  333. break;
  334. case W83627DHG_P_ID:
  335. ret = w83627dhg_p;
  336. break;
  337. case W83627UHG_ID:
  338. ret = w83627uhg;
  339. break;
  340. case W83667HG_ID:
  341. ret = w83667hg;
  342. break;
  343. case W83667HG_B_ID:
  344. ret = w83667hg_b;
  345. break;
  346. case NCT6775_ID:
  347. ret = nct6775;
  348. break;
  349. case NCT6776_ID:
  350. ret = nct6776;
  351. break;
  352. case NCT6779_ID:
  353. ret = nct6779;
  354. break;
  355. case NCT6791_ID:
  356. ret = nct6791;
  357. break;
  358. case NCT6792_ID:
  359. ret = nct6792;
  360. break;
  361. case NCT6793_ID:
  362. ret = nct6793;
  363. break;
  364. case NCT6795_ID:
  365. ret = nct6795;
  366. break;
  367. case NCT6796_ID:
  368. ret = nct6796;
  369. break;
  370. case NCT6102_ID:
  371. ret = nct6102;
  372. cr_wdt_timeout = NCT6102D_WDT_TIMEOUT;
  373. cr_wdt_control = NCT6102D_WDT_CONTROL;
  374. cr_wdt_csr = NCT6102D_WDT_CSR;
  375. break;
  376. case NCT6116_ID:
  377. ret = nct6116;
  378. cr_wdt_timeout = NCT6102D_WDT_TIMEOUT;
  379. cr_wdt_control = NCT6102D_WDT_CONTROL;
  380. cr_wdt_csr = NCT6102D_WDT_CSR;
  381. break;
  382. case 0xff:
  383. ret = -ENODEV;
  384. break;
  385. default:
  386. ret = -ENODEV;
  387. pr_err("Unsupported chip ID: 0x%02x\n", val);
  388. break;
  389. }
  390. superio_exit();
  391. return ret;
  392. }
  393. /*
  394. * On some systems, the NCT6791D comes with a companion chip and the
  395. * watchdog function is in this companion chip. We must use a different
  396. * unlocking sequence to access the companion chip.
  397. */
  398. static int __init wdt_use_alt_key(const struct dmi_system_id *d)
  399. {
  400. wdt_cfg_enter = 0x88;
  401. wdt_cfg_leave = 0xBB;
  402. return 0;
  403. }
  404. static const struct dmi_system_id wdt_dmi_table[] __initconst = {
  405. {
  406. .matches = {
  407. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "INVES"),
  408. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "CTS"),
  409. DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "INVES"),
  410. DMI_EXACT_MATCH(DMI_BOARD_NAME, "SHARKBAY"),
  411. },
  412. .callback = wdt_use_alt_key,
  413. },
  414. {}
  415. };
  416. static int __init wdt_init(void)
  417. {
  418. int ret;
  419. int chip;
  420. static const char * const chip_name[] = {
  421. "W83627HF",
  422. "W83627S",
  423. "W83697HF",
  424. "W83697UG",
  425. "W83637HF",
  426. "W83627THF",
  427. "W83687THF",
  428. "W83627EHF",
  429. "W83627DHG",
  430. "W83627UHG",
  431. "W83667HG",
  432. "W83667DHG-P",
  433. "W83667HG-B",
  434. "NCT6775",
  435. "NCT6776",
  436. "NCT6779",
  437. "NCT6791",
  438. "NCT6792",
  439. "NCT6793",
  440. "NCT6795",
  441. "NCT6796",
  442. "NCT6102",
  443. "NCT6116",
  444. };
  445. /* Apply system-specific quirks */
  446. dmi_check_system(wdt_dmi_table);
  447. wdt_io = 0x2e;
  448. chip = wdt_find(0x2e);
  449. if (chip < 0) {
  450. wdt_io = 0x4e;
  451. chip = wdt_find(0x4e);
  452. if (chip < 0)
  453. return chip;
  454. }
  455. pr_info("WDT driver for %s Super I/O chip initialising\n",
  456. chip_name[chip]);
  457. watchdog_init_timeout(&wdt_dev, timeout, NULL);
  458. watchdog_set_nowayout(&wdt_dev, nowayout);
  459. watchdog_stop_on_reboot(&wdt_dev);
  460. ret = w83627hf_init(&wdt_dev, chip);
  461. if (ret) {
  462. pr_err("failed to initialize watchdog (err=%d)\n", ret);
  463. return ret;
  464. }
  465. ret = watchdog_register_device(&wdt_dev);
  466. if (ret)
  467. return ret;
  468. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  469. wdt_dev.timeout, nowayout);
  470. return ret;
  471. }
  472. static void __exit wdt_exit(void)
  473. {
  474. watchdog_unregister_device(&wdt_dev);
  475. }
  476. module_init(wdt_init);
  477. module_exit(wdt_exit);
  478. MODULE_LICENSE("GPL");
  479. MODULE_AUTHOR("Pádraig Brady <[email protected]>");
  480. MODULE_DESCRIPTION("w83627hf/thf WDT driver");