2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Sandvine Incorporated ULC.
5 * Copyright (c) 2012 iXsystems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Support for Winbond watchdog.
32 * With minor abstractions it might be possible to add support for other
33 * different Winbond Super I/O chips as well. Winbond seems to have four
34 * different types of chips, four different ways to get into extended config
37 * Note: there is no serialization between the debugging sysctl handlers and
38 * the watchdog functions and possibly others poking the registers at the same
39 * time. For that at least possibly interfering sysctls are hidden by default.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
49 #include <sys/eventhandler.h>
50 #include <sys/module.h>
52 #include <sys/sysctl.h>
53 #include <sys/watchdog.h>
55 #include <dev/superio/superio.h>
57 #include <machine/bus.h>
58 #include <machine/resource.h>
63 #define WB_DEVICE_ID_REG 0x20 /* Device ID */
64 #define WB_DEVICE_REV_REG 0x21 /* Device revision */
65 #define WB_CR26 0x26 /* Bit6: HEFRAS (base port selector) */
68 #define WB_LDN_REG 0x07
69 #define WB_LDN_REG_LDN8 0x08 /* GPIO 2, Watchdog */
72 * LDN8 (GPIO 2, Watchdog) specific registers and options.
74 /* CR30: LDN8 activation control. */
75 #define WB_LDN8_CR30 0x30
76 #define WB_LDN8_CR30_ACTIVE 0x01 /* 1: LD active */
78 /* CRF5: Watchdog scale, P20. Mapped to reg_1. */
79 #define WB_LDN8_CRF5 0xF5
80 #define WB_LDN8_CRF5_SCALE 0x08 /* 0: 1s, 1: 60s */
81 #define WB_LDN8_CRF5_KEYB_P20 0x04 /* 1: keyb P20 forces timeout */
82 #define WB_LDN8_CRF5_KBRST 0x02 /* 1: timeout causes pin60 kbd reset */
84 /* CRF6: Watchdog Timeout (0 == off). Mapped to reg_timeout. */
85 #define WB_LDN8_CRF6 0xF6
87 /* CRF7: Watchdog mouse, keyb, force, .. Mapped to reg_2. */
88 #define WB_LDN8_CRF7 0xF7
89 #define WB_LDN8_CRF7_MOUSE 0x80 /* 1: mouse irq resets wd timer */
90 #define WB_LDN8_CRF7_KEYB 0x40 /* 1: keyb irq resets wd timer */
91 #define WB_LDN8_CRF7_FORCE 0x20 /* 1: force timeout (self-clear) */
92 #define WB_LDN8_CRF7_TS 0x10 /* 0: counting, 1: fired */
93 #define WB_LDN8_CRF7_IRQS 0x0f /* irq source for watchdog, 2 == SMI */
95 enum chips { w83627hf, w83627s, w83697hf, w83697ug, w83637hf, w83627thf,
96 w83687thf, w83627ehf, w83627dhg, w83627uhg, w83667hg,
97 w83627dhg_p, w83667hg_b, nct6775, nct6776, nct6779, nct6791,
98 nct6792, nct6793, nct6795, nct6102 };
102 eventhandler_tag ev_tag;
110 * Special feature to let the watchdog fire at a different
111 * timeout as set by watchdog(4) but still use that API to
112 * re-load it periodically.
114 unsigned int timeout_override;
117 * Space to save current state temporary and for sysctls.
118 * We want to know the timeout value and usually need two
119 * additional registers for options. Do not name them by
120 * register as these might be different by chip.
127 struct winbond_vendor_device_id {
135 .descr = "Winbond 83627HF/F/HG/G",
140 .descr = "Winbond 83627S",
145 .descr = "Winbond 83697HF",
150 .descr = "Winbond 83697UG",
155 .descr = "Winbond 83637HF",
160 .descr = "Winbond 83627THF",
165 .descr = "Winbond 83687THF",
170 .descr = "Winbond 83627EHF",
175 .descr = "Winbond 83627DHG",
180 .descr = "Winbond 83627UHG",
185 .descr = "Winbond 83667HG",
190 .descr = "Winbond 83627DHG-P",
195 .descr = "Winbond 83667HG-B",
200 .descr = "Nuvoton NCT6775",
205 .descr = "Nuvoton NCT6776",
210 .descr = "Nuvoton NCT6102",
215 .descr = "Nuvoton NCT6779",
220 .descr = "Nuvoton NCT6791",
225 .descr = "Nuvoton NCT6792",
230 .descr = "Nuvoton NCT6793",
235 .descr = "Nuvoton NCT6795",
240 * Return the watchdog related registers as we last read them. This will
241 * usually not give the current timeout or state on whether the watchdog
245 sysctl_wb_debug(SYSCTL_HANDLER_ARGS)
253 sbuf_new_for_sysctl(&sb, NULL, 64, req);
255 sbuf_printf(&sb, "LDN8 (GPIO2, Watchdog): ");
256 sbuf_printf(&sb, "CR%02X 0x%02x ", sc->ctl_reg, sc->reg_1);
257 sbuf_printf(&sb, "CR%02X 0x%02x ", sc->time_reg, sc->reg_timeout);
258 sbuf_printf(&sb, "CR%02X 0x%02x", sc->csr_reg, sc->reg_2);
260 error = sbuf_finish(&sb);
266 * Read the current values before returning them. Given this might poke
267 * the registers the same time as the watchdog, this sysctl handler should
268 * be marked CTLFLAG_SKIP to not show up by default.
271 sysctl_wb_debug_current(SYSCTL_HANDLER_ARGS)
277 sc->reg_1 = superio_read(sc->dev, sc->ctl_reg);
278 sc->reg_timeout = superio_read(sc->dev, sc->time_reg);
279 sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
281 return (sysctl_wb_debug(oidp, arg1, arg2, req));
285 * Sysctl handlers to force a watchdog timeout or to test the NMI functionality
287 * For testing we could set a test_nmi flag in the softc that, in case of NMI, a
288 * callback function from trap.c could check whether we fired and not report the
289 * timeout but clear the flag for the sysctl again. This is interesting given a
290 * lot of boards have jumpers to change the action on watchdog timeout or
291 * disable the watchdog completely.
292 * XXX-BZ notyet: currently no general infrastructure exists to do this.
295 sysctl_wb_force_test_nmi(SYSCTL_HANDLER_ARGS)
298 int error, test, val;
308 error = sysctl_handle_int(oidp, &val, 0, req);
309 if (error || !req->newptr)
313 /* Manually clear the test for a value of 0 and do nothing else. */
314 if (test && val == 0) {
320 * If we are testing the NMI functionality, set the flag before
321 * forcing the timeout.
327 /* Force watchdog to fire. */
328 sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
329 sc->reg_2 |= WB_LDN8_CRF7_FORCE;
330 superio_write(sc->dev, sc->csr_reg, sc->reg_2);
336 * Print current watchdog state.
338 * Note: it is the responsibility of the caller to update the registers
342 wb_print_state(struct wb_softc *sc, const char *msg)
345 device_printf(sc->dev, "%s%sWatchdog %sabled. %s"
346 "Scaling by %ds, timer at %d (%s=%ds%s). "
347 "CR%02X 0x%02x CR%02X 0x%02x\n",
348 (msg != NULL) ? msg : "", (msg != NULL) ? ": " : "",
349 (sc->reg_timeout > 0x00) ? "en" : "dis",
350 (sc->reg_2 & WB_LDN8_CRF7_TS) ? "Watchdog fired. " : "",
351 (sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1,
353 (sc->reg_timeout > 0x00) ? "<" : "",
354 sc->reg_timeout * ((sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1),
355 (sc->reg_timeout > 0x00) ? " left" : "",
356 sc->ctl_reg, sc->reg_1, sc->csr_reg, sc->reg_2);
360 * (Re)load the watchdog counter depending on timeout. A timeout of 0 will
361 * disable the watchdog.
364 wb_set_watchdog(struct wb_softc *sc, unsigned int timeout)
369 * In case an override is set, let it override. It may lead
370 * to strange results as we do not check the input of the sysctl.
372 if (sc->timeout_override > 0)
373 timeout = sc->timeout_override;
375 /* Make sure we support the requested timeout. */
376 if (timeout > 255 * 60)
380 if (sc->debug_verbose)
381 wb_print_state(sc, "Before watchdog counter (re)load");
384 /* Disable watchdog. */
386 superio_write(sc->dev, sc->time_reg, sc->reg_timeout);
389 /* Read current scaling factor. */
390 sc->reg_1 = superio_read(sc->dev, sc->ctl_reg);
393 /* Set scaling factor to 60s. */
394 sc->reg_1 |= WB_LDN8_CRF5_SCALE;
395 sc->reg_timeout = (timeout / 60);
399 /* Set scaling factor to 1s. */
400 sc->reg_1 &= ~WB_LDN8_CRF5_SCALE;
401 sc->reg_timeout = timeout;
404 /* In case we fired before we need to clear to fire again. */
405 sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
406 if (sc->reg_2 & WB_LDN8_CRF7_TS) {
407 sc->reg_2 &= ~WB_LDN8_CRF7_TS;
408 superio_write(sc->dev, sc->csr_reg, sc->reg_2);
411 /* Write back scaling factor. */
412 superio_write(sc->dev, sc->ctl_reg, sc->reg_1);
414 /* Set timer and arm/reset the watchdog. */
415 superio_write(sc->dev, sc->time_reg, sc->reg_timeout);
418 if (sc->debug_verbose)
419 wb_print_state(sc, "After watchdog counter (re)load");
424 * watchdog(9) EVENTHANDLER function implementation to (re)load the counter
425 * with the given timeout or disable the watchdog.
428 wb_watchdog_fn(void *private, u_int cmd, int *error)
431 unsigned int timeout;
435 KASSERT(sc != NULL, ("%s: watchdog handler function called without "
436 "softc.", __func__));
439 if (cmd > 0 && cmd <= 63) {
440 /* Reset (and arm) watchdog. */
441 timeout = ((uint64_t)1 << cmd) / 1000000000;
444 e = wb_set_watchdog(sc, timeout);
449 /* On error, try to make sure the WD is disabled. */
450 wb_set_watchdog(sc, 0);
454 /* Disable watchdog. */
455 e = wb_set_watchdog(sc, 0);
456 if (e != 0 && cmd == 0 && error != NULL) {
457 /* Failed to disable watchdog. */
464 wb_probe(device_t dev)
472 if (superio_vendor(dev) != SUPERIO_VENDOR_NUVOTON)
474 if (superio_get_type(dev) != SUPERIO_DEV_WDT)
477 sc = device_get_softc(dev);
478 devid = superio_devid(dev) >> 8;
479 revid = superio_revid(dev);
480 for (j = 0; j < nitems(wb_devs); j++) {
481 if (wb_devs[j].device_id == devid) {
482 sc->chip = wb_devs[j].chip;
483 snprintf(buf, sizeof(buf),
484 "%s (0x%02x/0x%02x) Watchdog Timer",
485 wb_devs[j].descr, devid, revid);
486 device_set_desc_copy(dev, buf);
487 return (BUS_PROBE_SPECIFIC);
492 "unrecognized chip: devid 0x%02x, revid 0x%02x\n",
499 wb_attach(device_t dev)
502 struct sysctl_ctx_list *sctx;
503 struct sysctl_oid *soid;
504 unsigned long timeout;
507 sc = device_get_softc(dev);
510 /* Make sure WDT is enabled. */
511 superio_dev_enable(dev, WB_LDN8_CR30_ACTIVE);
535 t = superio_read(dev, 0x2B) & ~0x10;
536 superio_write(dev, 0x2B, t); /* set GPIO24 to WDT0 */
539 /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
540 t = superio_read(dev, 0x29) & ~0x60;
542 superio_write(dev, 0x29, t);
545 /* Set pin 118 to WDTO# mode */
546 t = superio_read(dev, 0x2b) & ~0x04;
547 superio_write(dev, 0x2b, t);
550 t = (superio_read(dev, 0x2B) & ~0x08) | 0x04;
551 superio_write(dev, 0x2B, t); /* set GPIO3 to WDT0 */
555 t = superio_read(dev, 0x2D) & ~0x01; /* PIN77 -> WDT0# */
556 superio_write(dev, 0x2D, t); /* set GPIO5 to WDT0 */
557 t = superio_read(dev, sc->ctl_reg);
558 t |= 0x02; /* enable the WDTO# output low pulse
559 * to the KBRST# pin */
560 superio_write(dev, sc->ctl_reg, t);
565 t = superio_read(dev, 0x2C) & ~0x80; /* PIN47 -> WDT0# */
566 superio_write(dev, 0x2C, t);
581 * These chips have a fixed WDTO# output pin (W83627UHG),
582 * or support more than one WDTO# output pin.
583 * Don't touch its configuration, and hope the BIOS
584 * does the right thing.
586 t = superio_read(dev, sc->ctl_reg);
587 t |= 0x02; /* enable the WDTO# output low pulse
588 * to the KBRST# pin */
589 superio_write(dev, sc->ctl_reg, t);
595 /* Read the current watchdog configuration. */
596 sc->reg_1 = superio_read(dev, sc->ctl_reg);
597 sc->reg_timeout = superio_read(dev, sc->time_reg);
598 sc->reg_2 = superio_read(dev, sc->csr_reg);
600 /* Print current state if bootverbose or watchdog already enabled. */
601 if (bootverbose || (sc->reg_timeout > 0x00))
602 wb_print_state(sc, "Before watchdog attach");
604 sc->reg_1 &= ~WB_LDN8_CRF5_KEYB_P20;
605 sc->reg_1 |= WB_LDN8_CRF5_KBRST;
606 superio_write(dev, sc->ctl_reg, sc->reg_1);
609 * Clear a previous watchdog timeout event (if still set).
610 * Disable timer reset on mouse interrupts. Leave reset on keyboard,
611 * since one of my boards is getting stuck in reboot without it.
613 sc->reg_2 &= ~(WB_LDN8_CRF7_MOUSE|WB_LDN8_CRF7_TS);
614 superio_write(dev, sc->csr_reg, sc->reg_2);
616 /* Read global timeout override tunable, Add per device sysctls. */
617 if (TUNABLE_ULONG_FETCH("hw.wbwd.timeout_override", &timeout)) {
619 sc->timeout_override = timeout;
621 sctx = device_get_sysctl_ctx(dev);
622 soid = device_get_sysctl_tree(dev);
623 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
624 "timeout_override", CTLFLAG_RW, &sc->timeout_override, 0,
625 "Timeout in seconds overriding default watchdog timeout");
626 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
627 "debug_verbose", CTLFLAG_RW, &sc->debug_verbose, 0,
628 "Enables extra debugging information");
629 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "debug",
630 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
631 sysctl_wb_debug, "A",
632 "Selected register information from last change by driver");
633 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "debug_current",
634 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_NEEDGIANT,
635 sc, 0, sysctl_wb_debug_current, "A",
636 "Selected register information (may interfere)");
637 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "force_timeout",
638 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SKIP | CTLFLAG_NEEDGIANT, sc, 0,
639 sysctl_wb_force_test_nmi, "I", "Enable to force watchdog to fire.");
641 /* Register watchdog. */
642 sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, wb_watchdog_fn, sc,
646 wb_print_state(sc, "After watchdog attach");
652 wb_detach(device_t dev)
656 sc = device_get_softc(dev);
658 /* Unregister and stop the watchdog if running. */
660 EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
661 wb_set_watchdog(sc, 0);
663 /* Bus subroutines take care of sysctls already. */
668 static device_method_t wb_methods[] = {
669 /* Device interface */
670 DEVMETHOD(device_probe, wb_probe),
671 DEVMETHOD(device_attach, wb_attach),
672 DEVMETHOD(device_detach, wb_detach),
677 static driver_t wb_driver = {
680 sizeof(struct wb_softc)
683 static devclass_t wb_devclass;
685 DRIVER_MODULE(wb, superio, wb_driver, wb_devclass, NULL, NULL);
686 MODULE_DEPEND(wb, superio, 1, 1, 1);
687 MODULE_VERSION(wb, 1);