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",
241 * Return the watchdog related registers as we last read them. This will
242 * usually not give the current timeout or state on whether the watchdog
246 sysctl_wb_debug(SYSCTL_HANDLER_ARGS)
254 sbuf_new_for_sysctl(&sb, NULL, 64, req);
256 sbuf_printf(&sb, "LDN8 (GPIO2, Watchdog): ");
257 sbuf_printf(&sb, "CR%02X 0x%02x ", sc->ctl_reg, sc->reg_1);
258 sbuf_printf(&sb, "CR%02X 0x%02x ", sc->time_reg, sc->reg_timeout);
259 sbuf_printf(&sb, "CR%02X 0x%02x", sc->csr_reg, sc->reg_2);
261 error = sbuf_finish(&sb);
267 * Read the current values before returning them. Given this might poke
268 * the registers the same time as the watchdog, this sysctl handler should
269 * be marked CTLFLAG_SKIP to not show up by default.
272 sysctl_wb_debug_current(SYSCTL_HANDLER_ARGS)
278 sc->reg_1 = superio_read(sc->dev, sc->ctl_reg);
279 sc->reg_timeout = superio_read(sc->dev, sc->time_reg);
280 sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
282 return (sysctl_wb_debug(oidp, arg1, arg2, req));
286 * Sysctl handlers to force a watchdog timeout or to test the NMI functionality
288 * For testing we could set a test_nmi flag in the softc that, in case of NMI, a
289 * callback function from trap.c could check whether we fired and not report the
290 * timeout but clear the flag for the sysctl again. This is interesting given a
291 * lot of boards have jumpers to change the action on watchdog timeout or
292 * disable the watchdog completely.
293 * XXX-BZ notyet: currently no general infrastructure exists to do this.
296 sysctl_wb_force_test_nmi(SYSCTL_HANDLER_ARGS)
299 int error, test, val;
309 error = sysctl_handle_int(oidp, &val, 0, req);
310 if (error || !req->newptr)
314 /* Manually clear the test for a value of 0 and do nothing else. */
315 if (test && val == 0) {
321 * If we are testing the NMI functionality, set the flag before
322 * forcing the timeout.
328 /* Force watchdog to fire. */
329 sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
330 sc->reg_2 |= WB_LDN8_CRF7_FORCE;
331 superio_write(sc->dev, sc->csr_reg, sc->reg_2);
337 * Print current watchdog state.
339 * Note: it is the responsibility of the caller to update the registers
343 wb_print_state(struct wb_softc *sc, const char *msg)
346 device_printf(sc->dev, "%s%sWatchdog %sabled. %s"
347 "Scaling by %ds, timer at %d (%s=%ds%s). "
348 "CR%02X 0x%02x CR%02X 0x%02x\n",
349 (msg != NULL) ? msg : "", (msg != NULL) ? ": " : "",
350 (sc->reg_timeout > 0x00) ? "en" : "dis",
351 (sc->reg_2 & WB_LDN8_CRF7_TS) ? "Watchdog fired. " : "",
352 (sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1,
354 (sc->reg_timeout > 0x00) ? "<" : "",
355 sc->reg_timeout * ((sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1),
356 (sc->reg_timeout > 0x00) ? " left" : "",
357 sc->ctl_reg, sc->reg_1, sc->csr_reg, sc->reg_2);
361 * (Re)load the watchdog counter depending on timeout. A timeout of 0 will
362 * disable the watchdog.
365 wb_set_watchdog(struct wb_softc *sc, unsigned int timeout)
370 * In case an override is set, let it override. It may lead
371 * to strange results as we do not check the input of the sysctl.
373 if (sc->timeout_override > 0)
374 timeout = sc->timeout_override;
376 /* Make sure we support the requested timeout. */
377 if (timeout > 255 * 60)
381 if (sc->debug_verbose)
382 wb_print_state(sc, "Before watchdog counter (re)load");
385 /* Disable watchdog. */
387 superio_write(sc->dev, sc->time_reg, sc->reg_timeout);
390 /* Read current scaling factor. */
391 sc->reg_1 = superio_read(sc->dev, sc->ctl_reg);
394 /* Set scaling factor to 60s. */
395 sc->reg_1 |= WB_LDN8_CRF5_SCALE;
396 sc->reg_timeout = (timeout / 60);
400 /* Set scaling factor to 1s. */
401 sc->reg_1 &= ~WB_LDN8_CRF5_SCALE;
402 sc->reg_timeout = timeout;
405 /* In case we fired before we need to clear to fire again. */
406 sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
407 if (sc->reg_2 & WB_LDN8_CRF7_TS) {
408 sc->reg_2 &= ~WB_LDN8_CRF7_TS;
409 superio_write(sc->dev, sc->csr_reg, sc->reg_2);
412 /* Write back scaling factor. */
413 superio_write(sc->dev, sc->ctl_reg, sc->reg_1);
415 /* Set timer and arm/reset the watchdog. */
416 superio_write(sc->dev, sc->time_reg, sc->reg_timeout);
419 if (sc->debug_verbose)
420 wb_print_state(sc, "After watchdog counter (re)load");
425 * watchdog(9) EVENTHANDLER function implementation to (re)load the counter
426 * with the given timeout or disable the watchdog.
429 wb_watchdog_fn(void *private, u_int cmd, int *error)
432 unsigned int timeout;
436 KASSERT(sc != NULL, ("%s: watchdog handler function called without "
437 "softc.", __func__));
440 if (cmd > 0 && cmd <= 63) {
441 /* Reset (and arm) watchdog. */
442 timeout = ((uint64_t)1 << cmd) / 1000000000;
445 e = wb_set_watchdog(sc, timeout);
450 /* On error, try to make sure the WD is disabled. */
451 wb_set_watchdog(sc, 0);
455 /* Disable watchdog. */
456 e = wb_set_watchdog(sc, 0);
457 if (e != 0 && cmd == 0 && error != NULL) {
458 /* Failed to disable watchdog. */
465 wb_probe(device_t dev)
473 if (superio_vendor(dev) != SUPERIO_VENDOR_NUVOTON)
475 if (superio_get_type(dev) != SUPERIO_DEV_WDT)
478 sc = device_get_softc(dev);
479 devid = superio_devid(dev) >> 8;
480 revid = superio_revid(dev);
481 for (j = 0; j < nitems(wb_devs); j++) {
482 if (wb_devs[j].device_id == devid) {
483 sc->chip = wb_devs[j].chip;
484 snprintf(buf, sizeof(buf),
485 "%s (0x%02x/0x%02x) Watchdog Timer",
486 wb_devs[j].descr, devid, revid);
487 device_set_desc_copy(dev, buf);
488 return (BUS_PROBE_SPECIFIC);
493 "unrecognized chip: devid 0x%02x, revid 0x%02x\n",
500 wb_attach(device_t dev)
503 struct sysctl_ctx_list *sctx;
504 struct sysctl_oid *soid;
505 unsigned long timeout;
508 sc = device_get_softc(dev);
511 /* Make sure WDT is enabled. */
512 superio_dev_enable(dev, WB_LDN8_CR30_ACTIVE);
536 t = superio_read(dev, 0x2B) & ~0x10;
537 superio_write(dev, 0x2B, t); /* set GPIO24 to WDT0 */
540 /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
541 t = superio_read(dev, 0x29) & ~0x60;
543 superio_write(dev, 0x29, t);
546 /* Set pin 118 to WDTO# mode */
547 t = superio_read(dev, 0x2b) & ~0x04;
548 superio_write(dev, 0x2b, t);
551 t = (superio_read(dev, 0x2B) & ~0x08) | 0x04;
552 superio_write(dev, 0x2B, t); /* set GPIO3 to WDT0 */
556 t = superio_read(dev, 0x2D) & ~0x01; /* PIN77 -> WDT0# */
557 superio_write(dev, 0x2D, t); /* set GPIO5 to WDT0 */
558 t = superio_read(dev, sc->ctl_reg);
559 t |= 0x02; /* enable the WDTO# output low pulse
560 * to the KBRST# pin */
561 superio_write(dev, sc->ctl_reg, t);
566 t = superio_read(dev, 0x2C) & ~0x80; /* PIN47 -> WDT0# */
567 superio_write(dev, 0x2C, t);
582 * These chips have a fixed WDTO# output pin (W83627UHG),
583 * or support more than one WDTO# output pin.
584 * Don't touch its configuration, and hope the BIOS
585 * does the right thing.
587 t = superio_read(dev, sc->ctl_reg);
588 t |= 0x02; /* enable the WDTO# output low pulse
589 * to the KBRST# pin */
590 superio_write(dev, sc->ctl_reg, t);
596 /* Read the current watchdog configuration. */
597 sc->reg_1 = superio_read(dev, sc->ctl_reg);
598 sc->reg_timeout = superio_read(dev, sc->time_reg);
599 sc->reg_2 = superio_read(dev, sc->csr_reg);
601 /* Print current state if bootverbose or watchdog already enabled. */
602 if (bootverbose || (sc->reg_timeout > 0x00))
603 wb_print_state(sc, "Before watchdog attach");
605 sc->reg_1 &= ~WB_LDN8_CRF5_KEYB_P20;
606 sc->reg_1 |= WB_LDN8_CRF5_KBRST;
607 superio_write(dev, sc->ctl_reg, sc->reg_1);
610 * Clear a previous watchdog timeout event (if still set).
611 * Disable timer reset on mouse interrupts. Leave reset on keyboard,
612 * since one of my boards is getting stuck in reboot without it.
614 sc->reg_2 &= ~(WB_LDN8_CRF7_MOUSE|WB_LDN8_CRF7_TS);
615 superio_write(dev, sc->csr_reg, sc->reg_2);
617 /* Read global timeout override tunable, Add per device sysctls. */
618 if (TUNABLE_ULONG_FETCH("hw.wbwd.timeout_override", &timeout)) {
620 sc->timeout_override = timeout;
622 sctx = device_get_sysctl_ctx(dev);
623 soid = device_get_sysctl_tree(dev);
624 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
625 "timeout_override", CTLFLAG_RW, &sc->timeout_override, 0,
626 "Timeout in seconds overriding default watchdog timeout");
627 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
628 "debug_verbose", CTLFLAG_RW, &sc->debug_verbose, 0,
629 "Enables extra debugging information");
630 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "debug",
631 CTLTYPE_STRING|CTLFLAG_RD, sc, 0, 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, sc, 0,
635 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, 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);