xref: /freebsd/sys/dev/wbwd/wbwd.c (revision e716630d4cf89e69ec3f675ebfceee09f1a85e05)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Sandvine Incorporated ULC.
5  * Copyright (c) 2012 iXsystems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 /*
30  * Support for Winbond watchdog.
31  *
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
35  * mode.
36  *
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.
40  */
41 
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 #include <sys/eventhandler.h>
48 #include <sys/module.h>
49 #include <sys/sbuf.h>
50 #include <sys/sysctl.h>
51 #include <sys/watchdog.h>
52 
53 #include <dev/superio/superio.h>
54 
55 #include <machine/bus.h>
56 #include <machine/resource.h>
57 
58 /*
59  * Global registers.
60  */
61 #define	WB_DEVICE_ID_REG	0x20	/* Device ID */
62 #define	WB_DEVICE_REV_REG	0x21	/* Device revision */
63 #define	WB_CR26			0x26	/* Bit6: HEFRAS (base port selector) */
64 
65 /* LDN selection. */
66 #define	WB_LDN_REG		0x07
67 #define	WB_LDN_REG_LDN8		0x08	/* GPIO 2, Watchdog */
68 
69 /*
70  * LDN8 (GPIO 2, Watchdog) specific registers and options.
71  */
72 /* CR30: LDN8 activation control. */
73 #define	WB_LDN8_CR30		0x30
74 #define	WB_LDN8_CR30_ACTIVE	0x01	/* 1: LD active */
75 
76 /* CRF5: Watchdog scale, P20. Mapped to reg_1. */
77 #define	WB_LDN8_CRF5		0xF5
78 #define	WB_LDN8_CRF5_SCALE	0x08	/* 0: 1s, 1: 60s */
79 #define	WB_LDN8_CRF5_KEYB_P20	0x04	/* 1: keyb P20 forces timeout */
80 #define	WB_LDN8_CRF5_KBRST	0x02	/* 1: timeout causes pin60 kbd reset */
81 
82 /* CRF6: Watchdog Timeout (0 == off). Mapped to reg_timeout. */
83 #define	WB_LDN8_CRF6		0xF6
84 
85 /* CRF7: Watchdog mouse, keyb, force, .. Mapped to reg_2. */
86 #define	WB_LDN8_CRF7		0xF7
87 #define	WB_LDN8_CRF7_MOUSE	0x80	/* 1: mouse irq resets wd timer */
88 #define	WB_LDN8_CRF7_KEYB	0x40	/* 1: keyb irq resets wd timer */
89 #define	WB_LDN8_CRF7_FORCE	0x20	/* 1: force timeout (self-clear) */
90 #define	WB_LDN8_CRF7_TS		0x10	/* 0: counting, 1: fired */
91 #define	WB_LDN8_CRF7_IRQS	0x0f	/* irq source for watchdog, 2 == SMI */
92 
93 enum chips { w83627hf, w83627s, w83697hf, w83697ug, w83637hf, w83627thf,
94 	     w83687thf, w83627ehf, w83627dhg, w83627uhg, w83667hg,
95 	     w83627dhg_p, w83667hg_b, nct6775, nct6776, nct6779, nct6791,
96 	     nct6792, nct6793, nct6795, nct6102 };
97 
98 struct wb_softc {
99 	device_t		dev;
100 	eventhandler_tag	ev_tag;
101 	enum chips		chip;
102 	uint8_t			ctl_reg;
103 	uint8_t			time_reg;
104 	uint8_t			csr_reg;
105 	int			debug_verbose;
106 
107 	/*
108 	 * Special feature to let the watchdog fire at a different
109 	 * timeout as set by watchdog(4) but still use that API to
110 	 * re-load it periodically.
111 	 */
112 	unsigned int		timeout_override;
113 
114 	/*
115 	 * Space to save current state temporary and for sysctls.
116 	 * We want to know the timeout value and usually need two
117 	 * additional registers for options. Do not name them by
118 	 * register as these might be different by chip.
119 	 */
120 	uint8_t			reg_timeout;
121 	uint8_t			reg_1;
122 	uint8_t			reg_2;
123 };
124 
125 struct winbond_vendor_device_id {
126 	uint8_t			device_id;
127 	enum chips		chip;
128 	const char *		descr;
129 } wb_devs[] = {
130 	{
131 		.device_id	= 0x52,
132 		.chip		= w83627hf,
133 		.descr		= "Winbond 83627HF/F/HG/G",
134 	},
135 	{
136 		.device_id	= 0x59,
137 		.chip		= w83627s,
138 		.descr		= "Winbond 83627S",
139 	},
140 	{
141 		.device_id	= 0x60,
142 		.chip		= w83697hf,
143 		.descr		= "Winbond 83697HF",
144 	},
145 	{
146 		.device_id	= 0x68,
147 		.chip		= w83697ug,
148 		.descr		= "Winbond 83697UG",
149 	},
150 	{
151 		.device_id	= 0x70,
152 		.chip		= w83637hf,
153 		.descr		= "Winbond 83637HF",
154 	},
155 	{
156 		.device_id	= 0x82,
157 		.chip		= w83627thf,
158 		.descr		= "Winbond 83627THF",
159 	},
160 	{
161 		.device_id	= 0x85,
162 		.chip		= w83687thf,
163 		.descr		= "Winbond 83687THF",
164 	},
165 	{
166 		.device_id	= 0x88,
167 		.chip		= w83627ehf,
168 		.descr		= "Winbond 83627EHF",
169 	},
170 	{
171 		.device_id	= 0xa0,
172 		.chip		= w83627dhg,
173 		.descr		= "Winbond 83627DHG",
174 	},
175 	{
176 		.device_id	= 0xa2,
177 		.chip		= w83627uhg,
178 		.descr		= "Winbond 83627UHG",
179 	},
180 	{
181 		.device_id	= 0xa5,
182 		.chip		= w83667hg,
183 		.descr		= "Winbond 83667HG",
184 	},
185 	{
186 		.device_id	= 0xb0,
187 		.chip		= w83627dhg_p,
188 		.descr		= "Winbond 83627DHG-P",
189 	},
190 	{
191 		.device_id	= 0xb3,
192 		.chip		= w83667hg_b,
193 		.descr		= "Winbond 83667HG-B",
194 	},
195 	{
196 		.device_id	= 0xb4,
197 		.chip		= nct6775,
198 		.descr		= "Nuvoton NCT6775",
199 	},
200 	{
201 		.device_id	= 0xc3,
202 		.chip		= nct6776,
203 		.descr		= "Nuvoton NCT6776",
204 	},
205 	{
206 		.device_id	= 0xc4,
207 		.chip		= nct6102,
208 		.descr		= "Nuvoton NCT6102",
209 	},
210 	{
211 		.device_id	= 0xc5,
212 		.chip		= nct6779,
213 		.descr		= "Nuvoton NCT6779",
214 	},
215 	{
216 		.device_id	= 0xc8,
217 		.chip		= nct6791,
218 		.descr		= "Nuvoton NCT6791",
219 	},
220 	{
221 		.device_id	= 0xc9,
222 		.chip		= nct6792,
223 		.descr		= "Nuvoton NCT6792",
224 	},
225 	{
226 		.device_id	= 0xd1,
227 		.chip		= nct6793,
228 		.descr		= "Nuvoton NCT6793",
229 	},
230 	{
231 		.device_id	= 0xd3,
232 		.chip		= nct6795,
233 		.descr		= "Nuvoton NCT6795",
234 	},
235 };
236 
237 /*
238  * Return the watchdog related registers as we last read them.  This will
239  * usually not give the current timeout or state on whether the watchdog
240  * fired.
241  */
242 static int
243 sysctl_wb_debug(SYSCTL_HANDLER_ARGS)
244 {
245 	struct wb_softc *sc;
246 	struct sbuf sb;
247 	int error;
248 
249 	sc = arg1;
250 
251 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
252 
253 	sbuf_printf(&sb, "LDN8 (GPIO2, Watchdog): ");
254 	sbuf_printf(&sb, "CR%02X 0x%02x ", sc->ctl_reg, sc->reg_1);
255 	sbuf_printf(&sb, "CR%02X 0x%02x ", sc->time_reg, sc->reg_timeout);
256 	sbuf_printf(&sb, "CR%02X 0x%02x", sc->csr_reg, sc->reg_2);
257 
258 	error = sbuf_finish(&sb);
259 	sbuf_delete(&sb);
260 	return (error);
261 }
262 
263 /*
264  * Read the current values before returning them.  Given this might poke
265  * the registers the same time as the watchdog, this sysctl handler should
266  * be marked CTLFLAG_SKIP to not show up by default.
267  */
268 static int
269 sysctl_wb_debug_current(SYSCTL_HANDLER_ARGS)
270 {
271 	struct wb_softc *sc;
272 
273 	sc = arg1;
274 
275 	sc->reg_1 = superio_read(sc->dev, sc->ctl_reg);
276 	sc->reg_timeout = superio_read(sc->dev, sc->time_reg);
277 	sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
278 
279 	return (sysctl_wb_debug(oidp, arg1, arg2, req));
280 }
281 
282 /*
283  * Sysctl handlers to force a watchdog timeout or to test the NMI functionality
284  * works as expetced.
285  * For testing we could set a test_nmi flag in the softc that, in case of NMI, a
286  * callback function from trap.c could check whether we fired and not report the
287  * timeout but clear the flag for the sysctl again.  This is interesting given a
288  * lot of boards have jumpers to change the action on watchdog timeout or
289  * disable the watchdog completely.
290  * XXX-BZ notyet: currently no general infrastructure exists to do this.
291  */
292 static int
293 sysctl_wb_force_test_nmi(SYSCTL_HANDLER_ARGS)
294 {
295 	struct wb_softc *sc;
296 	int error, val;
297 
298 	sc = arg1;
299 
300 #ifdef notyet
301 	val = sc->test_nmi;
302 #else
303 	val = 0;
304 #endif
305 	error = sysctl_handle_int(oidp, &val, 0, req);
306         if (error || !req->newptr)
307                 return (error);
308 
309 #ifdef notyet
310 	int test = arg2;
311 
312 	/* Manually clear the test for a value of 0 and do nothing else. */
313 	if (test && val == 0) {
314 		sc->test_nmi = 0;
315 		return (0);
316 	}
317 
318 	/*
319 	 * If we are testing the NMI functionality, set the flag before
320 	 * forcing the timeout.
321 	 */
322 	if (test)
323 		sc->test_nmi = 1;
324 #endif
325 
326 	/* Force watchdog to fire. */
327 	sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
328 	sc->reg_2 |= WB_LDN8_CRF7_FORCE;
329 	superio_write(sc->dev, sc->csr_reg, sc->reg_2);
330 
331 	return (0);
332 }
333 
334 /*
335  * Print current watchdog state.
336  *
337  * Note: it is the responsibility of the caller to update the registers
338  * upfront.
339  */
340 static void
341 wb_print_state(struct wb_softc *sc, const char *msg)
342 {
343 
344 	device_printf(sc->dev, "%s%sWatchdog %sabled. %s"
345 	    "Scaling by %ds, timer at %d (%s=%ds%s). "
346 	    "CR%02X 0x%02x CR%02X 0x%02x\n",
347 	    (msg != NULL) ? msg : "", (msg != NULL) ? ": " : "",
348 	    (sc->reg_timeout > 0x00) ? "en" : "dis",
349 	    (sc->reg_2 & WB_LDN8_CRF7_TS) ? "Watchdog fired. " : "",
350 	    (sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1,
351 	    sc->reg_timeout,
352 	    (sc->reg_timeout > 0x00) ? "<" : "",
353 	    sc->reg_timeout * ((sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1),
354 	    (sc->reg_timeout > 0x00) ? " left" : "",
355 	    sc->ctl_reg, sc->reg_1, sc->csr_reg, sc->reg_2);
356 }
357 
358 /*
359  * (Re)load the watchdog counter depending on timeout.  A timeout of 0 will
360  * disable the watchdog.
361  */
362 static int
363 wb_set_watchdog(struct wb_softc *sc, unsigned int timeout)
364 {
365 
366 	if (timeout != 0) {
367 		/*
368 		 * In case an override is set, let it override.  It may lead
369 		 * to strange results as we do not check the input of the sysctl.
370 		 */
371 		if (sc->timeout_override > 0)
372 			timeout = sc->timeout_override;
373 
374 		/* Make sure we support the requested timeout. */
375 		if (timeout > 255 * 60)
376 			return (EINVAL);
377 	}
378 
379 	if (sc->debug_verbose)
380 		wb_print_state(sc, "Before watchdog counter (re)load");
381 
382 	if (timeout == 0) {
383 		/* Disable watchdog. */
384 		sc->reg_timeout = 0;
385 		superio_write(sc->dev, sc->time_reg, sc->reg_timeout);
386 
387 	} else {
388 		/* Read current scaling factor. */
389 		sc->reg_1 = superio_read(sc->dev, sc->ctl_reg);
390 
391 		if (timeout > 255) {
392 			/* Set scaling factor to 60s. */
393 			sc->reg_1 |= WB_LDN8_CRF5_SCALE;
394 			sc->reg_timeout = (timeout / 60);
395 			if (timeout % 60)
396 				sc->reg_timeout++;
397 		} else {
398 			/* Set scaling factor to 1s. */
399 			sc->reg_1 &= ~WB_LDN8_CRF5_SCALE;
400 			sc->reg_timeout = timeout;
401 		}
402 
403 		/* In case we fired before we need to clear to fire again. */
404 		sc->reg_2 = superio_read(sc->dev, sc->csr_reg);
405 		if (sc->reg_2 & WB_LDN8_CRF7_TS) {
406 			sc->reg_2 &= ~WB_LDN8_CRF7_TS;
407 			superio_write(sc->dev, sc->csr_reg, sc->reg_2);
408 		}
409 
410 		/* Write back scaling factor. */
411 		superio_write(sc->dev, sc->ctl_reg, sc->reg_1);
412 
413 		/* Set timer and arm/reset the watchdog. */
414 		superio_write(sc->dev, sc->time_reg, sc->reg_timeout);
415 	}
416 
417 	if (sc->debug_verbose)
418 		wb_print_state(sc, "After watchdog counter (re)load");
419 	return (0);
420 }
421 
422 /*
423  * watchdog(9) EVENTHANDLER function implementation to (re)load the counter
424  * with the given timeout or disable the watchdog.
425  */
426 static void
427 wb_watchdog_fn(void *private, u_int cmd, int *error)
428 {
429 	struct wb_softc *sc;
430 	unsigned int timeout;
431 	int e;
432 
433 	sc = private;
434 	KASSERT(sc != NULL, ("%s: watchdog handler function called without "
435 	    "softc.", __func__));
436 
437 	cmd &= WD_INTERVAL;
438 	if (cmd > 0 && cmd <= 63) {
439 		/* Reset (and arm) watchdog. */
440 		timeout = ((uint64_t)1 << cmd) / 1000000000;
441 		if (timeout == 0)
442 			timeout = 1;
443 		e = wb_set_watchdog(sc, timeout);
444 		if (e == 0) {
445 			if (error != NULL)
446 				*error = 0;
447 		} else {
448 			/* On error, try to make sure the WD is disabled. */
449 			wb_set_watchdog(sc, 0);
450 		}
451 
452 	} else {
453 		/* Disable watchdog. */
454 		e = wb_set_watchdog(sc, 0);
455 		if (e != 0 && cmd == 0 && error != NULL) {
456 			/* Failed to disable watchdog. */
457 			*error = EOPNOTSUPP;
458 		}
459 	}
460 }
461 
462 static int
463 wb_probe(device_t dev)
464 {
465 	char buf[128];
466 	struct wb_softc *sc;
467 	int j;
468 	uint8_t devid;
469 	uint8_t revid;
470 
471 	if (superio_vendor(dev) != SUPERIO_VENDOR_NUVOTON)
472 		return (ENXIO);
473 	if (superio_get_type(dev) != SUPERIO_DEV_WDT)
474 		return (ENXIO);
475 
476 	sc = device_get_softc(dev);
477 	devid = superio_devid(dev) >> 8;
478 	revid = superio_revid(dev);
479 	for (j = 0; j < nitems(wb_devs); j++) {
480 		if (wb_devs[j].device_id == devid) {
481 			sc->chip = wb_devs[j].chip;
482 			snprintf(buf, sizeof(buf),
483 			    "%s (0x%02x/0x%02x) Watchdog Timer",
484 			    wb_devs[j].descr, devid, revid);
485 			device_set_desc_copy(dev, buf);
486 			return (BUS_PROBE_SPECIFIC);
487 		}
488 	}
489 	if (bootverbose) {
490 		device_printf(dev,
491 		    "unrecognized chip: devid 0x%02x, revid 0x%02x\n",
492 		    devid, revid);
493 	}
494 	return (ENXIO);
495 }
496 
497 static int
498 wb_attach(device_t dev)
499 {
500 	struct wb_softc *sc;
501 	struct sysctl_ctx_list *sctx;
502 	struct sysctl_oid *soid;
503 	unsigned long timeout;
504 	uint8_t t;
505 
506 	sc = device_get_softc(dev);
507 	sc->dev = dev;
508 
509 	/* Make sure WDT is enabled. */
510 	superio_dev_enable(dev, WB_LDN8_CR30_ACTIVE);
511 
512 	switch (sc->chip) {
513 	case w83697hf:
514 	case w83697ug:
515 		sc->ctl_reg = 0xf3;
516 		sc->time_reg = 0xf4;
517 		sc->csr_reg = 0xf7;
518 		break;
519 	case nct6102:
520 		sc->ctl_reg = 0xf0;
521 		sc->time_reg = 0xf1;
522 		sc->csr_reg = 0xf2;
523 		break;
524 	default:
525 		sc->ctl_reg = 0xf5;
526 		sc->time_reg = 0xf6;
527 		sc->csr_reg = 0xf7;
528 		break;
529 	}
530 
531 	switch (sc->chip) {
532 	case w83627hf:
533 	case w83627s:
534 		t = superio_read(dev, 0x2B) & ~0x10;
535 		superio_write(dev, 0x2B, t); /* set GPIO24 to WDT0 */
536 		break;
537 	case w83697hf:
538 		/* Set pin 119 to WDTO# mode (= CR29, WDT0) */
539 		t = superio_read(dev, 0x29) & ~0x60;
540 		t |= 0x20;
541 		superio_write(dev, 0x29, t);
542 		break;
543 	case w83697ug:
544 		/* Set pin 118 to WDTO# mode */
545 		t = superio_read(dev, 0x2b) & ~0x04;
546 		superio_write(dev, 0x2b, t);
547 		break;
548 	case w83627thf:
549 		t = (superio_read(dev, 0x2B) & ~0x08) | 0x04;
550 		superio_write(dev, 0x2B, t); /* set GPIO3 to WDT0 */
551 		break;
552 	case w83627dhg:
553 	case w83627dhg_p:
554 		t = superio_read(dev, 0x2D) & ~0x01; /* PIN77 -> WDT0# */
555 		superio_write(dev, 0x2D, t); /* set GPIO5 to WDT0 */
556 		t = superio_read(dev, sc->ctl_reg);
557 		t |= 0x02;	/* enable the WDTO# output low pulse
558 				 * to the KBRST# pin */
559 		superio_write(dev, sc->ctl_reg, t);
560 		break;
561 	case w83637hf:
562 		break;
563 	case w83687thf:
564 		t = superio_read(dev, 0x2C) & ~0x80; /* PIN47 -> WDT0# */
565 		superio_write(dev, 0x2C, t);
566 		break;
567 	case w83627ehf:
568 	case w83627uhg:
569 	case w83667hg:
570 	case w83667hg_b:
571 	case nct6775:
572 	case nct6776:
573 	case nct6779:
574 	case nct6791:
575 	case nct6792:
576 	case nct6793:
577 	case nct6795:
578 	case nct6102:
579 		/*
580 		 * These chips have a fixed WDTO# output pin (W83627UHG),
581 		 * or support more than one WDTO# output pin.
582 		 * Don't touch its configuration, and hope the BIOS
583 		 * does the right thing.
584 		 */
585 		t = superio_read(dev, sc->ctl_reg);
586 		t |= 0x02;	/* enable the WDTO# output low pulse
587 				 * to the KBRST# pin */
588 		superio_write(dev, sc->ctl_reg, t);
589 		break;
590 	default:
591 		break;
592 	}
593 
594 	/* Read the current watchdog configuration. */
595 	sc->reg_1 = superio_read(dev, sc->ctl_reg);
596 	sc->reg_timeout = superio_read(dev, sc->time_reg);
597 	sc->reg_2 = superio_read(dev, sc->csr_reg);
598 
599 	/* Print current state if bootverbose or watchdog already enabled. */
600 	if (bootverbose || (sc->reg_timeout > 0x00))
601 		wb_print_state(sc, "Before watchdog attach");
602 
603 	sc->reg_1 &= ~WB_LDN8_CRF5_KEYB_P20;
604 	sc->reg_1 |= WB_LDN8_CRF5_KBRST;
605 	superio_write(dev, sc->ctl_reg, sc->reg_1);
606 
607 	/*
608 	 * Clear a previous watchdog timeout event (if still set).
609 	 * Disable timer reset on mouse interrupts.  Leave reset on keyboard,
610 	 * since one of my boards is getting stuck in reboot without it.
611 	 */
612 	sc->reg_2 &= ~(WB_LDN8_CRF7_MOUSE|WB_LDN8_CRF7_TS);
613 	superio_write(dev, sc->csr_reg, sc->reg_2);
614 
615 	/* Read global timeout override tunable, Add per device sysctls. */
616 	if (TUNABLE_ULONG_FETCH("hw.wbwd.timeout_override", &timeout)) {
617 		if (timeout > 0)
618 			sc->timeout_override = timeout;
619 	}
620 	sctx = device_get_sysctl_ctx(dev);
621 	soid = device_get_sysctl_tree(dev);
622         SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
623 	    "timeout_override", CTLFLAG_RW, &sc->timeout_override, 0,
624             "Timeout in seconds overriding default watchdog timeout");
625         SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
626 	    "debug_verbose", CTLFLAG_RW, &sc->debug_verbose, 0,
627             "Enables extra debugging information");
628         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "debug",
629 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
630 	    sysctl_wb_debug, "A",
631             "Selected register information from last change by driver");
632         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "debug_current",
633 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
634 	    sc, 0, sysctl_wb_debug_current, "A",
635 	     "Selected register information (may interfere)");
636 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "force_timeout",
637 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SKIP | CTLFLAG_MPSAFE, sc, 0,
638 	    sysctl_wb_force_test_nmi, "I", "Enable to force watchdog to fire.");
639 
640 	/* Register watchdog. */
641 	sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, wb_watchdog_fn, sc,
642 	    0);
643 
644 	if (bootverbose)
645 		wb_print_state(sc, "After watchdog attach");
646 
647 	return (0);
648 }
649 
650 static int
651 wb_detach(device_t dev)
652 {
653 	struct wb_softc *sc;
654 
655 	sc = device_get_softc(dev);
656 
657 	/* Unregister and stop the watchdog if running. */
658 	if (sc->ev_tag)
659 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
660 	wb_set_watchdog(sc, 0);
661 
662 	/* Bus subroutines take care of sysctls already. */
663 
664 	return (0);
665 }
666 
667 static device_method_t wb_methods[] = {
668 	/* Device interface */
669 	DEVMETHOD(device_probe,		wb_probe),
670 	DEVMETHOD(device_attach,	wb_attach),
671 	DEVMETHOD(device_detach,	wb_detach),
672 
673 	DEVMETHOD_END
674 };
675 
676 static driver_t wb_driver = {
677 	"wbwd",
678 	wb_methods,
679 	sizeof(struct wb_softc)
680 };
681 
682 DRIVER_MODULE(wb, superio, wb_driver, NULL, NULL);
683 MODULE_DEPEND(wb, superio, 1, 1, 1);
684 MODULE_VERSION(wb, 1);
685