xref: /freebsd/sys/dev/wbwd/wbwd.c (revision 9e8bad81)
10566170fSBjoern A. Zeeb /*-
20566170fSBjoern A. Zeeb  * Copyright (c) 2011 Sandvine Incorporated ULC.
31adb4ebdSXin LI  * Copyright (c) 2012 iXsystems, Inc.
40566170fSBjoern A. Zeeb  * All rights reserved.
50566170fSBjoern A. Zeeb  *
60566170fSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
70566170fSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
80566170fSBjoern A. Zeeb  * are met:
90566170fSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
100566170fSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
110566170fSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
120566170fSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
130566170fSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
140566170fSBjoern A. Zeeb  *
150566170fSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
160566170fSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170566170fSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180566170fSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
190566170fSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200566170fSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210566170fSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220566170fSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
230566170fSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240566170fSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250566170fSBjoern A. Zeeb  * SUCH DAMAGE.
260566170fSBjoern A. Zeeb  */
270566170fSBjoern A. Zeeb /*
280566170fSBjoern A. Zeeb  * Support for Winbond watchdog.
290566170fSBjoern A. Zeeb  *
300566170fSBjoern A. Zeeb  * With minor abstractions it might be possible to add support for other
310566170fSBjoern A. Zeeb  * different Winbond Super I/O chips as well.  Winbond seems to have four
320566170fSBjoern A. Zeeb  * different types of chips, four different ways to get into extended config
330566170fSBjoern A. Zeeb  * mode.
340566170fSBjoern A. Zeeb  *
350566170fSBjoern A. Zeeb  * Note: there is no serialization between the debugging sysctl handlers and
360566170fSBjoern A. Zeeb  * the watchdog functions and possibly others poking the registers at the same
370566170fSBjoern A. Zeeb  * time.  For that at least possibly interfering sysctls are hidden by default.
380566170fSBjoern A. Zeeb  */
390566170fSBjoern A. Zeeb 
400566170fSBjoern A. Zeeb #include <sys/cdefs.h>
410566170fSBjoern A. Zeeb __FBSDID("$FreeBSD$");
420566170fSBjoern A. Zeeb 
430566170fSBjoern A. Zeeb #include <sys/param.h>
440566170fSBjoern A. Zeeb #include <sys/kernel.h>
450566170fSBjoern A. Zeeb #include <sys/systm.h>
460566170fSBjoern A. Zeeb #include <sys/bus.h>
470566170fSBjoern A. Zeeb #include <sys/eventhandler.h>
480566170fSBjoern A. Zeeb #include <sys/lock.h>
490566170fSBjoern A. Zeeb #include <sys/module.h>
500566170fSBjoern A. Zeeb #include <sys/rman.h>
510566170fSBjoern A. Zeeb #include <sys/sbuf.h>
520566170fSBjoern A. Zeeb #include <sys/sysctl.h>
530566170fSBjoern A. Zeeb #include <sys/watchdog.h>
540566170fSBjoern A. Zeeb 
550566170fSBjoern A. Zeeb #include <isa/isavar.h>
560566170fSBjoern A. Zeeb 
570566170fSBjoern A. Zeeb #include <machine/bus.h>
580566170fSBjoern A. Zeeb #include <machine/resource.h>
590566170fSBjoern A. Zeeb 
600566170fSBjoern A. Zeeb /*
610566170fSBjoern A. Zeeb  * Global registers.
620566170fSBjoern A. Zeeb  */
630566170fSBjoern A. Zeeb #define	WB_DEVICE_ID_REG	0x20	/* Device ID */
640566170fSBjoern A. Zeeb #define	WB_DEVICE_REV_REG	0x21	/* Device revision */
650566170fSBjoern A. Zeeb #define	WB_CR26			0x26	/* Bit6: HEFRAS (base port selector) */
660566170fSBjoern A. Zeeb 
670566170fSBjoern A. Zeeb /* LDN selection. */
680566170fSBjoern A. Zeeb #define	WB_LDN_REG		0x07
690566170fSBjoern A. Zeeb #define	WB_LDN_REG_LDN8		0x08	/* GPIO 2, Watchdog */
700566170fSBjoern A. Zeeb 
710566170fSBjoern A. Zeeb /*
720566170fSBjoern A. Zeeb  * LDN8 (GPIO 2, Watchdog) specific registers and options.
730566170fSBjoern A. Zeeb  */
740566170fSBjoern A. Zeeb /* CR30: LDN8 activation control. */
750566170fSBjoern A. Zeeb #define	WB_LDN8_CR30		0x30
760566170fSBjoern A. Zeeb #define	WB_LDN8_CR30_ACTIVE	0x01	/* 1: LD active */
770566170fSBjoern A. Zeeb 
780566170fSBjoern A. Zeeb /* CRF5: Watchdog scale, P20. Mapped to reg_1. */
790566170fSBjoern A. Zeeb #define	WB_LDN8_CRF5		0xF5
800566170fSBjoern A. Zeeb #define	WB_LDN8_CRF5_SCALE	0x08	/* 0: 1s, 1: 60s */
810566170fSBjoern A. Zeeb #define	WB_LDN8_CRF5_KEYB_P20	0x04	/* 1: keyb P20 forces timeout */
82cf864f03SRobert Noland #define	WB_LDN8_CRF5_KBRST	0x02	/* 1: timeout causes pin60 kbd reset */
830566170fSBjoern A. Zeeb 
840566170fSBjoern A. Zeeb /* CRF6: Watchdog Timeout (0 == off). Mapped to reg_timeout. */
850566170fSBjoern A. Zeeb #define	WB_LDN8_CRF6		0xF6
860566170fSBjoern A. Zeeb 
870566170fSBjoern A. Zeeb /* CRF7: Watchdog mouse, keyb, force, .. Mapped to reg_2. */
880566170fSBjoern A. Zeeb #define	WB_LDN8_CRF7		0xF7
890566170fSBjoern A. Zeeb #define	WB_LDN8_CRF7_MOUSE	0x80	/* 1: mouse irq resets wd timer */
900566170fSBjoern A. Zeeb #define	WB_LDN8_CRF7_KEYB	0x40	/* 1: keyb irq resets wd timer */
910566170fSBjoern A. Zeeb #define	WB_LDN8_CRF7_FORCE	0x20	/* 1: force timeout (self-clear) */
920566170fSBjoern A. Zeeb #define	WB_LDN8_CRF7_TS		0x10	/* 0: counting, 1: fired */
930566170fSBjoern A. Zeeb #define	WB_LDN8_CRF7_IRQS	0x0f	/* irq source for watchdog, 2 == SMI */
940566170fSBjoern A. Zeeb #define	WB_LDN8_CRF7_CLEAR_MASK	\
950566170fSBjoern A. Zeeb     (WB_LDN8_CRF7_MOUSE|WB_LDN8_CRF7_KEYB|WB_LDN8_CRF7_TS|WB_LDN8_CRF7_IRQS)
960566170fSBjoern A. Zeeb 
970566170fSBjoern A. Zeeb struct wb_softc {
980566170fSBjoern A. Zeeb 	device_t		dev;
990566170fSBjoern A. Zeeb 	struct resource		*portres;
1000566170fSBjoern A. Zeeb 	bus_space_tag_t		bst;
1010566170fSBjoern A. Zeeb 	bus_space_handle_t	bsh;
1020566170fSBjoern A. Zeeb 	int			rid;
1030566170fSBjoern A. Zeeb 	eventhandler_tag	ev_tag;
1041adb4ebdSXin LI 	int			(*ext_cfg_enter_f)(struct wb_softc *, u_short);
1051adb4ebdSXin LI 	void			(*ext_cfg_exit_f)(struct wb_softc *, u_short);
1060566170fSBjoern A. Zeeb 	int			debug_verbose;
1070566170fSBjoern A. Zeeb 
1080566170fSBjoern A. Zeeb 	/*
1090566170fSBjoern A. Zeeb 	 * Special feature to let the watchdog fire at a different
1100566170fSBjoern A. Zeeb 	 * timeout as set by watchdog(4) but still use that API to
1110566170fSBjoern A. Zeeb 	 * re-load it periodically.
1120566170fSBjoern A. Zeeb 	 */
1130566170fSBjoern A. Zeeb 	unsigned int		timeout_override;
1140566170fSBjoern A. Zeeb 
1150566170fSBjoern A. Zeeb 	/*
1160566170fSBjoern A. Zeeb 	 * Space to save current state temporary and for sysctls.
1170566170fSBjoern A. Zeeb 	 * We want to know the timeout value and usually need two
1180566170fSBjoern A. Zeeb 	 * additional registers for options. Do not name them by
1190566170fSBjoern A. Zeeb 	 * register as these might be different by chip.
1200566170fSBjoern A. Zeeb 	 */
1210566170fSBjoern A. Zeeb 	uint8_t			reg_timeout;
1220566170fSBjoern A. Zeeb 	uint8_t			reg_1;
1230566170fSBjoern A. Zeeb 	uint8_t			reg_2;
1240566170fSBjoern A. Zeeb };
1250566170fSBjoern A. Zeeb 
1261adb4ebdSXin LI static int	ext_cfg_enter_0x87_0x87(struct wb_softc *, u_short);
1271adb4ebdSXin LI static void	ext_cfg_exit_0xaa(struct wb_softc *, u_short);
1280566170fSBjoern A. Zeeb 
1290566170fSBjoern A. Zeeb struct winbond_superio_cfg {
1300566170fSBjoern A. Zeeb 	uint8_t			efer;	/* and efir */
1311adb4ebdSXin LI 	int			(*ext_cfg_enter_f)(struct wb_softc *, u_short);
1321adb4ebdSXin LI 	void			(*ext_cfg_exit_f)(struct wb_softc *, u_short);
1330566170fSBjoern A. Zeeb } probe_addrs[] = {
1340566170fSBjoern A. Zeeb 	{
1350566170fSBjoern A. Zeeb 		.efer			= 0x2e,
1360566170fSBjoern A. Zeeb 		.ext_cfg_enter_f	= ext_cfg_enter_0x87_0x87,
1370566170fSBjoern A. Zeeb 		.ext_cfg_exit_f		= ext_cfg_exit_0xaa,
1380566170fSBjoern A. Zeeb 	},
1390566170fSBjoern A. Zeeb 	{
1400566170fSBjoern A. Zeeb 		.efer			= 0x4e,
1410566170fSBjoern A. Zeeb 		.ext_cfg_enter_f	= ext_cfg_enter_0x87_0x87,
1420566170fSBjoern A. Zeeb 		.ext_cfg_exit_f		= ext_cfg_exit_0xaa,
1430566170fSBjoern A. Zeeb 	},
1440566170fSBjoern A. Zeeb };
1450566170fSBjoern A. Zeeb 
1460566170fSBjoern A. Zeeb struct winbond_vendor_device_id {
1470566170fSBjoern A. Zeeb 	uint16_t		vendor_id;
1480566170fSBjoern A. Zeeb 	uint8_t			device_id;
1490566170fSBjoern A. Zeeb 	uint8_t			device_rev;
1500566170fSBjoern A. Zeeb 	const char *		descr;
1510566170fSBjoern A. Zeeb } wb_devs[] = {
1520566170fSBjoern A. Zeeb 	{
1530566170fSBjoern A. Zeeb 		.vendor_id	= 0x5ca3,
1540566170fSBjoern A. Zeeb 		.device_id	= 0x52,
1550566170fSBjoern A. Zeeb 		.device_rev	= 0x17,
1560566170fSBjoern A. Zeeb 		.descr		= "Winbond 83627HF/F/HG/G Rev. G",
1570566170fSBjoern A. Zeeb 	},
1580566170fSBjoern A. Zeeb 	{
1590566170fSBjoern A. Zeeb 		.vendor_id	= 0x5ca3,
1600566170fSBjoern A. Zeeb 		.device_id	= 0x52,
1610566170fSBjoern A. Zeeb 		.device_rev	= 0x3a,
1620566170fSBjoern A. Zeeb 		.descr		= "Winbond 83627HF/F/HG/G Rev. J",
1630566170fSBjoern A. Zeeb 	},
1640566170fSBjoern A. Zeeb 	{
1650566170fSBjoern A. Zeeb 		.vendor_id	= 0x5ca3,
1660566170fSBjoern A. Zeeb 		.device_id	= 0x52,
1670566170fSBjoern A. Zeeb 		.device_rev	= 0x41,
1680566170fSBjoern A. Zeeb 		.descr		= "Winbond 83627HF/F/HG/G Rev. UD-A",
1690566170fSBjoern A. Zeeb 	},
1700566170fSBjoern A. Zeeb 	{
1710566170fSBjoern A. Zeeb 		.vendor_id	= 0x5ca3,
1720566170fSBjoern A. Zeeb 		.device_id	= 0xa0,
1730566170fSBjoern A. Zeeb 		.device_rev	= 0x25,
1740566170fSBjoern A. Zeeb 		.descr		= "Winbond 83627DHG IC ver. 5",
1750566170fSBjoern A. Zeeb 	},
176cf864f03SRobert Noland 	{
177cf864f03SRobert Noland 		.vendor_id	= 0x5ca3,
178cf864f03SRobert Noland 		.device_id	= 0xb0,
179cf864f03SRobert Noland 		.device_rev	= 0x73,
180cf864f03SRobert Noland 		.descr		= "Winbond 83627DHG-P",
181cf864f03SRobert Noland 	},
1829e8bad81SXin LI 	{
1839e8bad81SXin LI 		.vendor_id	= 0x5ca3,
1849e8bad81SXin LI 		.device_id	= 0xc3,
1859e8bad81SXin LI 		.device_rev	= 0x33,
1869e8bad81SXin LI 		.descr		= "Nuvoton WPCM450RA0BX",
1879e8bad81SXin LI 	},
1880566170fSBjoern A. Zeeb };
1890566170fSBjoern A. Zeeb 
1901adb4ebdSXin LI static void
1911adb4ebdSXin LI write_efir_1(struct wb_softc *sc, u_short baseport, uint8_t value)
1921adb4ebdSXin LI {
1931adb4ebdSXin LI 
1941adb4ebdSXin LI 	MPASS(sc != NULL || baseport != 0);
1951adb4ebdSXin LI 	if (sc != NULL)
1961adb4ebdSXin LI 		bus_space_write_1((sc)->bst, (sc)->bsh, 0, (value));
1971adb4ebdSXin LI 	else
1981adb4ebdSXin LI 		outb(baseport, value);
1991adb4ebdSXin LI }
2001adb4ebdSXin LI 
2011adb4ebdSXin LI static uint8_t __unused
2021adb4ebdSXin LI read_efir_1(struct wb_softc *sc, u_short baseport)
2031adb4ebdSXin LI {
2041adb4ebdSXin LI 
2051adb4ebdSXin LI 	MPASS(sc != NULL || baseport != 0);
2061adb4ebdSXin LI 	if (sc != NULL)
2071adb4ebdSXin LI 		return (bus_space_read_1((sc)->bst, (sc)->bsh, 0));
2081adb4ebdSXin LI 	else
2091adb4ebdSXin LI 		return (inb(baseport));
2101adb4ebdSXin LI }
2111adb4ebdSXin LI 
2121adb4ebdSXin LI static void
2131adb4ebdSXin LI write_efdr_1(struct wb_softc *sc, u_short baseport, uint8_t value)
2141adb4ebdSXin LI {
2151adb4ebdSXin LI 
2161adb4ebdSXin LI 	MPASS(sc != NULL || baseport != 0);
2171adb4ebdSXin LI 	if (sc != NULL)
2181adb4ebdSXin LI 		bus_space_write_1((sc)->bst, (sc)->bsh, 1, (value));
2191adb4ebdSXin LI 	else
2201adb4ebdSXin LI 		outb(baseport + 1, value);
2211adb4ebdSXin LI }
2221adb4ebdSXin LI 
2231adb4ebdSXin LI static uint8_t
2241adb4ebdSXin LI read_efdr_1(struct wb_softc *sc, u_short baseport)
2251adb4ebdSXin LI {
2261adb4ebdSXin LI 
2271adb4ebdSXin LI 	MPASS(sc != NULL || baseport != 0);
2281adb4ebdSXin LI 	if (sc != NULL)
2291adb4ebdSXin LI 		return (bus_space_read_1((sc)->bst, (sc)->bsh, 1));
2301adb4ebdSXin LI 	else
2311adb4ebdSXin LI 		return (inb(baseport + 1));
2321adb4ebdSXin LI }
2331adb4ebdSXin LI 
2340566170fSBjoern A. Zeeb /*
2350566170fSBjoern A. Zeeb  * Return the watchdog related registers as we last read them.  This will
2360566170fSBjoern A. Zeeb  * usually not give the current timeout or state on whether the watchdog
2370566170fSBjoern A. Zeeb  * fired.
2380566170fSBjoern A. Zeeb  */
2390566170fSBjoern A. Zeeb static int
2400566170fSBjoern A. Zeeb sysctl_wb_debug(SYSCTL_HANDLER_ARGS)
2410566170fSBjoern A. Zeeb {
2420566170fSBjoern A. Zeeb 	struct wb_softc *sc;
2430566170fSBjoern A. Zeeb 	struct sbuf sb;
2440566170fSBjoern A. Zeeb 	int error;
2450566170fSBjoern A. Zeeb 
2460566170fSBjoern A. Zeeb 	sc = arg1;
2470566170fSBjoern A. Zeeb 
2480566170fSBjoern A. Zeeb 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
2490566170fSBjoern A. Zeeb 
2500566170fSBjoern A. Zeeb 	sbuf_printf(&sb, "LDN8 (GPIO2, Watchdog): ");
2510566170fSBjoern A. Zeeb 	sbuf_printf(&sb, "CRF5 0x%02x ", sc->reg_1);
2520566170fSBjoern A. Zeeb 	sbuf_printf(&sb, "CRF6 0x%02x ", sc->reg_timeout);
2530566170fSBjoern A. Zeeb 	sbuf_printf(&sb, "CRF7 0x%02x", sc->reg_2);
2540566170fSBjoern A. Zeeb 
2550566170fSBjoern A. Zeeb 	error = sbuf_finish(&sb);
2560566170fSBjoern A. Zeeb 	sbuf_delete(&sb);
2570566170fSBjoern A. Zeeb 	return (error);
2580566170fSBjoern A. Zeeb }
2590566170fSBjoern A. Zeeb 
2600566170fSBjoern A. Zeeb /*
2610566170fSBjoern A. Zeeb  * Read the current values before returning them.  Given this might poke
2620566170fSBjoern A. Zeeb  * the registers the same time as the watchdog, this sysctl handler should
2630566170fSBjoern A. Zeeb  * be marked CTLFLAG_SKIP to not show up by default.
2640566170fSBjoern A. Zeeb  */
2650566170fSBjoern A. Zeeb static int
2660566170fSBjoern A. Zeeb sysctl_wb_debug_current(SYSCTL_HANDLER_ARGS)
2670566170fSBjoern A. Zeeb {
2680566170fSBjoern A. Zeeb 	struct wb_softc *sc;
2690566170fSBjoern A. Zeeb 
2700566170fSBjoern A. Zeeb 	sc = arg1;
2710566170fSBjoern A. Zeeb 
2720566170fSBjoern A. Zeeb 	/*
2730566170fSBjoern A. Zeeb 	 * Enter extended function mode in case someone else has been
2740566170fSBjoern A. Zeeb 	 * poking on the registers.  We will not leave it though.
2750566170fSBjoern A. Zeeb 	 */
2761adb4ebdSXin LI 	if ((*sc->ext_cfg_enter_f)(sc, 0) != 0)
2770566170fSBjoern A. Zeeb 		return (ENXIO);
2780566170fSBjoern A. Zeeb 
2790566170fSBjoern A. Zeeb 	/* Watchdog is configured as part of LDN 8 (GPIO Port2, Watchdog). */
2801adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN_REG);
2811adb4ebdSXin LI 	write_efdr_1(sc, 0, WB_LDN_REG_LDN8);
2820566170fSBjoern A. Zeeb 
2831adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF5);
2841adb4ebdSXin LI 	sc->reg_1 = read_efdr_1(sc, 0);
2851adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF6);
2861adb4ebdSXin LI 	sc->reg_timeout = read_efdr_1(sc, 0);
2871adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF7);
2881adb4ebdSXin LI 	sc->reg_2 = read_efdr_1(sc, 0);
2890566170fSBjoern A. Zeeb 
2900566170fSBjoern A. Zeeb 	return (sysctl_wb_debug(oidp, arg1, arg2, req));
2910566170fSBjoern A. Zeeb }
2920566170fSBjoern A. Zeeb 
2930566170fSBjoern A. Zeeb /*
2940566170fSBjoern A. Zeeb  * Sysctl handlers to force a watchdog timeout or to test the NMI functionality
2950566170fSBjoern A. Zeeb  * works as expetced.
2960566170fSBjoern A. Zeeb  * For testing we could set a test_nmi flag in the softc that, in case of NMI, a
2970566170fSBjoern A. Zeeb  * callback function from trap.c could check whether we fired and not report the
2980566170fSBjoern A. Zeeb  * timeout but clear the flag for the sysctl again.  This is interesting given a
2990566170fSBjoern A. Zeeb  * lot of boards have jumpers to change the action on watchdog timeout or
3000566170fSBjoern A. Zeeb  * disable the watchdog completely.
3010566170fSBjoern A. Zeeb  * XXX-BZ notyet: currently no general infrastructure exists to do this.
3020566170fSBjoern A. Zeeb  */
3030566170fSBjoern A. Zeeb static int
3040566170fSBjoern A. Zeeb sysctl_wb_force_test_nmi(SYSCTL_HANDLER_ARGS)
3050566170fSBjoern A. Zeeb {
3060566170fSBjoern A. Zeeb 	struct wb_softc *sc;
3070566170fSBjoern A. Zeeb 	int error, test, val;
3080566170fSBjoern A. Zeeb 
3090566170fSBjoern A. Zeeb 	sc = arg1;
3100566170fSBjoern A. Zeeb 	test = arg2;
3110566170fSBjoern A. Zeeb 
3120566170fSBjoern A. Zeeb #ifdef notyet
3130566170fSBjoern A. Zeeb 	val = sc->test_nmi;
3140566170fSBjoern A. Zeeb #else
3150566170fSBjoern A. Zeeb 	val = 0;
3160566170fSBjoern A. Zeeb #endif
3170566170fSBjoern A. Zeeb 	error = sysctl_handle_int(oidp, &val, 0, req);
3180566170fSBjoern A. Zeeb         if (error || !req->newptr)
3190566170fSBjoern A. Zeeb                 return (error);
3200566170fSBjoern A. Zeeb 
3210566170fSBjoern A. Zeeb #ifdef notyet
3220566170fSBjoern A. Zeeb 	/* Manually clear the test for a value of 0 and do nothing else. */
3230566170fSBjoern A. Zeeb 	if (test && val == 0) {
3240566170fSBjoern A. Zeeb 		sc->test_nmi = 0;
3250566170fSBjoern A. Zeeb 		return (0);
3260566170fSBjoern A. Zeeb 	}
3270566170fSBjoern A. Zeeb #endif
3280566170fSBjoern A. Zeeb 
3290566170fSBjoern A. Zeeb 	/*
3300566170fSBjoern A. Zeeb 	 * Enter extended function mode in case someone else has been
3310566170fSBjoern A. Zeeb 	 * poking on the registers.  We will not leave it though.
3320566170fSBjoern A. Zeeb 	 */
3331adb4ebdSXin LI 	if ((*sc->ext_cfg_enter_f)(sc, 0) != 0)
3340566170fSBjoern A. Zeeb 		return (ENXIO);
3350566170fSBjoern A. Zeeb 
3360566170fSBjoern A. Zeeb #ifdef notyet
3370566170fSBjoern A. Zeeb 	/*
3380566170fSBjoern A. Zeeb 	 * If we are testing the NMI functionality, set the flag before
3390566170fSBjoern A. Zeeb 	 * forcing the timeout.
3400566170fSBjoern A. Zeeb 	 */
3410566170fSBjoern A. Zeeb 	if (test)
3420566170fSBjoern A. Zeeb 		sc->test_nmi = 1;
3430566170fSBjoern A. Zeeb #endif
3440566170fSBjoern A. Zeeb 
3450566170fSBjoern A. Zeeb 	/* Watchdog is configured as part of LDN 8 (GPIO Port2, Watchdog). */
3461adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN_REG);
3471adb4ebdSXin LI 	write_efdr_1(sc, 0, WB_LDN_REG_LDN8);
3480566170fSBjoern A. Zeeb 
3490566170fSBjoern A. Zeeb 	/* Force watchdog to fire. */
3501adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF7);
3511adb4ebdSXin LI 	sc->reg_2 = read_efdr_1(sc, 0);
3520566170fSBjoern A. Zeeb 	sc->reg_2 |= WB_LDN8_CRF7_FORCE;
3530566170fSBjoern A. Zeeb 
3541adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF7);
3551adb4ebdSXin LI 	write_efdr_1(sc, 0, sc->reg_2);
3560566170fSBjoern A. Zeeb 
3570566170fSBjoern A. Zeeb 	return (0);
3580566170fSBjoern A. Zeeb }
3590566170fSBjoern A. Zeeb 
3600566170fSBjoern A. Zeeb /*
3610566170fSBjoern A. Zeeb  * Print current watchdog state.
3620566170fSBjoern A. Zeeb  *
3630566170fSBjoern A. Zeeb  * Note: it is the responsibility of the caller to update the registers
3640566170fSBjoern A. Zeeb  * upfront.
3650566170fSBjoern A. Zeeb  */
3660566170fSBjoern A. Zeeb static void
3670566170fSBjoern A. Zeeb wb_print_state(struct wb_softc *sc, const char *msg)
3680566170fSBjoern A. Zeeb {
3690566170fSBjoern A. Zeeb 
3700566170fSBjoern A. Zeeb 	device_printf(sc->dev, "%s%sWatchdog %sabled. %s"
3710566170fSBjoern A. Zeeb 	    "Scaling by %ds, timer at %d (%s=%ds%s). "
3720566170fSBjoern A. Zeeb 	    "CRF5 0x%02x CRF7 0x%02x\n",
3730566170fSBjoern A. Zeeb 	    (msg != NULL) ? msg : "", (msg != NULL) ? ": " : "",
3740566170fSBjoern A. Zeeb 	    (sc->reg_timeout > 0x00) ? "en" : "dis",
3750566170fSBjoern A. Zeeb 	    (sc->reg_2 & WB_LDN8_CRF7_TS) ? "Watchdog fired. " : "",
3760566170fSBjoern A. Zeeb 	    (sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1,
3770566170fSBjoern A. Zeeb 	    sc->reg_timeout,
3780566170fSBjoern A. Zeeb 	    (sc->reg_timeout > 0x00) ? "<" : "",
3790566170fSBjoern A. Zeeb 	    sc->reg_timeout * ((sc->reg_1 & WB_LDN8_CRF5_SCALE) ? 60 : 1),
3800566170fSBjoern A. Zeeb 	    (sc->reg_timeout > 0x00) ? " left" : "",
3810566170fSBjoern A. Zeeb 	    sc->reg_1, sc->reg_2);
3820566170fSBjoern A. Zeeb }
3830566170fSBjoern A. Zeeb 
3840566170fSBjoern A. Zeeb /*
3850566170fSBjoern A. Zeeb  * Functions to enter and exit extended function mode.  Possibly shared
3860566170fSBjoern A. Zeeb  * between different chips.
3870566170fSBjoern A. Zeeb  */
3880566170fSBjoern A. Zeeb static int
3891adb4ebdSXin LI ext_cfg_enter_0x87_0x87(struct wb_softc *sc, u_short baseport)
3900566170fSBjoern A. Zeeb {
3910566170fSBjoern A. Zeeb 
3920566170fSBjoern A. Zeeb 	/*
3930566170fSBjoern A. Zeeb 	 * Enable extended function mode.
3940566170fSBjoern A. Zeeb 	 * Winbond does not allow us to validate so always return success.
3950566170fSBjoern A. Zeeb 	 */
3961adb4ebdSXin LI 	write_efir_1(sc, baseport, 0x87);
3971adb4ebdSXin LI 	write_efir_1(sc, baseport, 0x87);
3980566170fSBjoern A. Zeeb 
3990566170fSBjoern A. Zeeb 	return (0);
4000566170fSBjoern A. Zeeb }
4010566170fSBjoern A. Zeeb 
4020566170fSBjoern A. Zeeb static void
4031adb4ebdSXin LI ext_cfg_exit_0xaa(struct wb_softc *sc, u_short baseport)
4040566170fSBjoern A. Zeeb {
4050566170fSBjoern A. Zeeb 
4061adb4ebdSXin LI 	write_efir_1(sc, baseport, 0xaa);
4070566170fSBjoern A. Zeeb }
4080566170fSBjoern A. Zeeb 
4090566170fSBjoern A. Zeeb /*
4100566170fSBjoern A. Zeeb  * (Re)load the watchdog counter depending on timeout.  A timeout of 0 will
4110566170fSBjoern A. Zeeb  * disable the watchdog.
4120566170fSBjoern A. Zeeb  */
4130566170fSBjoern A. Zeeb static int
4140566170fSBjoern A. Zeeb wb_set_watchdog(struct wb_softc *sc, unsigned int timeout)
4150566170fSBjoern A. Zeeb {
4160566170fSBjoern A. Zeeb 
4170566170fSBjoern A. Zeeb 	if (sc->debug_verbose)
4180566170fSBjoern A. Zeeb 		wb_print_state(sc, "Before watchdog counter (re)load");
4190566170fSBjoern A. Zeeb 
4200566170fSBjoern A. Zeeb 	/*
4210566170fSBjoern A. Zeeb 	 * Enter extended function mode in case someone else has been
4220566170fSBjoern A. Zeeb 	 * poking on the registers.  We will not leave it though.
4230566170fSBjoern A. Zeeb 	 */
4241adb4ebdSXin LI 	if ((*sc->ext_cfg_enter_f)(sc, 0) != 0)
4250566170fSBjoern A. Zeeb 		return (ENXIO);
4260566170fSBjoern A. Zeeb 
4270566170fSBjoern A. Zeeb 	/* Watchdog is configured as part of LDN 8 (GPIO Port2, Watchdog) */
4281adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN_REG);
4291adb4ebdSXin LI 	write_efdr_1(sc, 0, WB_LDN_REG_LDN8);
4300566170fSBjoern A. Zeeb 
4310566170fSBjoern A. Zeeb 	/* Disable and validate or arm/reset watchdog. */
4320566170fSBjoern A. Zeeb 	if (timeout == 0) {
4330566170fSBjoern A. Zeeb 		/* Disable watchdog. */
4341adb4ebdSXin LI 		write_efir_1(sc, 0, WB_LDN8_CRF6);
4351adb4ebdSXin LI 		write_efdr_1(sc, 0, 0x00);
4360566170fSBjoern A. Zeeb 
4370566170fSBjoern A. Zeeb 		/* Re-check. */
4381adb4ebdSXin LI 		write_efir_1(sc, 0, WB_LDN8_CRF6);
4391adb4ebdSXin LI 		sc->reg_timeout = read_efdr_1(sc, 0);
4400566170fSBjoern A. Zeeb 
4410566170fSBjoern A. Zeeb 		if (sc->reg_timeout != 0x00) {
4420566170fSBjoern A. Zeeb 			device_printf(sc->dev, "Failed to disable watchdog: "
4430566170fSBjoern A. Zeeb 			    "0x%02x.\n", sc->reg_timeout);
4440566170fSBjoern A. Zeeb 			return (EIO);
4450566170fSBjoern A. Zeeb 		}
4460566170fSBjoern A. Zeeb 
4470566170fSBjoern A. Zeeb 	} else {
4480566170fSBjoern A. Zeeb 		/*
4490566170fSBjoern A. Zeeb 		 * In case an override is set, let it override.  It may lead
4500566170fSBjoern A. Zeeb 		 * to strange results as we do not check the input of the sysctl.
4510566170fSBjoern A. Zeeb 		 */
4520566170fSBjoern A. Zeeb 		if (sc->timeout_override > 0)
4530566170fSBjoern A. Zeeb 			timeout = sc->timeout_override;
4540566170fSBjoern A. Zeeb 
4550566170fSBjoern A. Zeeb 		/* Make sure we support the requested timeout. */
4560566170fSBjoern A. Zeeb 		if (timeout > 255 * 60)
4570566170fSBjoern A. Zeeb 			return (EINVAL);
4580566170fSBjoern A. Zeeb 
4590566170fSBjoern A. Zeeb 		/* Read current scaling factor. */
4601adb4ebdSXin LI 		write_efir_1(sc, 0, WB_LDN8_CRF5);
4611adb4ebdSXin LI 		sc->reg_1 = read_efdr_1(sc, 0);
4620566170fSBjoern A. Zeeb 
4630566170fSBjoern A. Zeeb 		if (timeout > 255) {
4640566170fSBjoern A. Zeeb 			/* Set scaling factor to 60s. */
4650566170fSBjoern A. Zeeb 			sc->reg_1 |= WB_LDN8_CRF5_SCALE;
4660566170fSBjoern A. Zeeb 			sc->reg_timeout = (timeout / 60);
4670566170fSBjoern A. Zeeb 			if (timeout % 60)
4680566170fSBjoern A. Zeeb 				sc->reg_timeout++;
4690566170fSBjoern A. Zeeb 		} else {
4700566170fSBjoern A. Zeeb 			/* Set scaling factor to 1s. */
4710566170fSBjoern A. Zeeb 			sc->reg_1 &= ~WB_LDN8_CRF5_SCALE;
4720566170fSBjoern A. Zeeb 			sc->reg_timeout = timeout;
4730566170fSBjoern A. Zeeb 		}
4740566170fSBjoern A. Zeeb 
4750566170fSBjoern A. Zeeb 		/* In case we fired before we need to clear to fire again. */
4761adb4ebdSXin LI 		write_efir_1(sc, 0, WB_LDN8_CRF7);
4771adb4ebdSXin LI 		sc->reg_2 = read_efdr_1(sc, 0);
4780566170fSBjoern A. Zeeb 		if (sc->reg_2 & WB_LDN8_CRF7_TS) {
4790566170fSBjoern A. Zeeb 			sc->reg_2 &= ~WB_LDN8_CRF7_TS;
4801adb4ebdSXin LI 			write_efir_1(sc, 0, WB_LDN8_CRF7);
4811adb4ebdSXin LI 			write_efdr_1(sc, 0, sc->reg_2);
4820566170fSBjoern A. Zeeb 		}
4830566170fSBjoern A. Zeeb 
4840566170fSBjoern A. Zeeb 		/* Write back scaling factor. */
4851adb4ebdSXin LI 		write_efir_1(sc, 0, WB_LDN8_CRF5);
4861adb4ebdSXin LI 		write_efdr_1(sc, 0, sc->reg_1);
4870566170fSBjoern A. Zeeb 
4880566170fSBjoern A. Zeeb 		/* Set timer and arm/reset the watchdog. */
4891adb4ebdSXin LI 		write_efir_1(sc, 0, WB_LDN8_CRF6);
4901adb4ebdSXin LI 		write_efdr_1(sc, 0, sc->reg_timeout);
4910566170fSBjoern A. Zeeb 	}
4920566170fSBjoern A. Zeeb 
4930566170fSBjoern A. Zeeb 	if (sc->debug_verbose)
4940566170fSBjoern A. Zeeb 		wb_print_state(sc, "After watchdog counter (re)load");
4950566170fSBjoern A. Zeeb 
4960566170fSBjoern A. Zeeb 	return (0);
4970566170fSBjoern A. Zeeb }
4980566170fSBjoern A. Zeeb 
4990566170fSBjoern A. Zeeb /*
5000566170fSBjoern A. Zeeb  * watchdog(9) EVENTHANDLER function implementation to (re)load the counter
5010566170fSBjoern A. Zeeb  * with the given timeout or disable the watchdog.
5020566170fSBjoern A. Zeeb  */
5030566170fSBjoern A. Zeeb static void
5040566170fSBjoern A. Zeeb wb_watchdog_fn(void *private, u_int cmd, int *error)
5050566170fSBjoern A. Zeeb {
5060566170fSBjoern A. Zeeb 	struct wb_softc *sc;
5070566170fSBjoern A. Zeeb 	unsigned int timeout;
5080566170fSBjoern A. Zeeb 	int e;
5090566170fSBjoern A. Zeeb 
5100566170fSBjoern A. Zeeb 	sc = private;
5110566170fSBjoern A. Zeeb 	KASSERT(sc != NULL, ("%s: watchdog handler function called without "
5120566170fSBjoern A. Zeeb 	    "softc.", __func__));
5130566170fSBjoern A. Zeeb 
5140566170fSBjoern A. Zeeb 	cmd &= WD_INTERVAL;
5150566170fSBjoern A. Zeeb 	if (cmd > 0 && cmd <= 63) {
5160566170fSBjoern A. Zeeb 		/* Reset (and arm) watchdog. */
5170566170fSBjoern A. Zeeb 		timeout = ((uint64_t)1 << cmd) / 1000000000;
5180566170fSBjoern A. Zeeb 		if (timeout == 0)
5190566170fSBjoern A. Zeeb 			timeout = 1;
5200566170fSBjoern A. Zeeb 		e = wb_set_watchdog(sc, timeout);
5210566170fSBjoern A. Zeeb 		if (e == 0) {
5220566170fSBjoern A. Zeeb 			if (error != NULL)
5230566170fSBjoern A. Zeeb 				*error = 0;
5240566170fSBjoern A. Zeeb 		} else {
5250566170fSBjoern A. Zeeb 			/* On error, try to make sure the WD is disabled. */
5260566170fSBjoern A. Zeeb 			wb_set_watchdog(sc, 0);
5270566170fSBjoern A. Zeeb 		}
5280566170fSBjoern A. Zeeb 
5290566170fSBjoern A. Zeeb 	} else {
5300566170fSBjoern A. Zeeb 		/* Disable watchdog. */
5310566170fSBjoern A. Zeeb 		e = wb_set_watchdog(sc, 0);
5320566170fSBjoern A. Zeeb 		if (e != 0 && cmd == 0 && error != NULL) {
5330566170fSBjoern A. Zeeb 			/* Failed to disable watchdog. */
5340566170fSBjoern A. Zeeb 			*error = EOPNOTSUPP;
5350566170fSBjoern A. Zeeb 		}
5360566170fSBjoern A. Zeeb 	}
5370566170fSBjoern A. Zeeb }
5380566170fSBjoern A. Zeeb 
5390566170fSBjoern A. Zeeb /*
5400566170fSBjoern A. Zeeb  * Probe/attach the Winbond Super I/O chip.
5410566170fSBjoern A. Zeeb  *
5420566170fSBjoern A. Zeeb  * Initial abstraction to possibly support more chips:
5430566170fSBjoern A. Zeeb  * - Iterate over the well known base ports, try to enable extended function
5440566170fSBjoern A. Zeeb  *   mode and read and match the device ID and device revision.  Unfortunately
5450566170fSBjoern A. Zeeb  *   the Vendor ID is in the hardware monitoring section accessible by different
5460566170fSBjoern A. Zeeb  *   base ports only.
5470566170fSBjoern A. Zeeb  * - Also HEFRAS, which would tell use the base port, is only accessible after
5480566170fSBjoern A. Zeeb  *   entering extended function mode, for which the base port is needed.
5490566170fSBjoern A. Zeeb  *   At least check HEFRAS to match the current base port we are probing.
5500566170fSBjoern A. Zeeb  * - On match set the description, remember functions to enter/exit extended
5510566170fSBjoern A. Zeeb  *   function mode as well as the base port.
5520566170fSBjoern A. Zeeb  */
5530566170fSBjoern A. Zeeb static int
5540566170fSBjoern A. Zeeb wb_probe_enable(device_t dev, int probe)
5550566170fSBjoern A. Zeeb {
5560566170fSBjoern A. Zeeb 	struct wb_softc *sc;
5570566170fSBjoern A. Zeeb 	int error, found, i, j;
5580566170fSBjoern A. Zeeb 	uint8_t dev_id, dev_rev, cr26;
5590566170fSBjoern A. Zeeb 
5601adb4ebdSXin LI 	if (dev == NULL)
5611adb4ebdSXin LI 		sc = NULL;
5621adb4ebdSXin LI 	else {
5630566170fSBjoern A. Zeeb 		sc = device_get_softc(dev);
5640566170fSBjoern A. Zeeb 		bzero(sc, sizeof(*sc));
5650566170fSBjoern A. Zeeb 		sc->dev = dev;
5661adb4ebdSXin LI 	}
5670566170fSBjoern A. Zeeb 
5680566170fSBjoern A. Zeeb 	error = ENXIO;
5690566170fSBjoern A. Zeeb 	for (i = 0; i < sizeof(probe_addrs) / sizeof(*probe_addrs); i++) {
5700566170fSBjoern A. Zeeb 
5711adb4ebdSXin LI 		if (sc != NULL) {
5720566170fSBjoern A. Zeeb 			/* Allocate bus resources for IO index/data register access. */
5730566170fSBjoern A. Zeeb 			sc->portres = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->rid,
5740566170fSBjoern A. Zeeb 			    probe_addrs[i].efer, probe_addrs[i].efer + 1, 2, RF_ACTIVE);
5750566170fSBjoern A. Zeeb 			if (sc->portres == NULL)
5760566170fSBjoern A. Zeeb 				continue;
5770566170fSBjoern A. Zeeb 			sc->bst = rman_get_bustag(sc->portres);
5780566170fSBjoern A. Zeeb 			sc->bsh = rman_get_bushandle(sc->portres);
5791adb4ebdSXin LI 		}
5800566170fSBjoern A. Zeeb 
5810566170fSBjoern A. Zeeb 		found = 0;
5821adb4ebdSXin LI 		error = (*probe_addrs[i].ext_cfg_enter_f)(sc, probe_addrs[i].efer);
5830566170fSBjoern A. Zeeb 		if (error != 0)
5840566170fSBjoern A. Zeeb 			goto cleanup;
5850566170fSBjoern A. Zeeb 
5860566170fSBjoern A. Zeeb 		/* Identify the SuperIO chip. */
5871adb4ebdSXin LI 		write_efir_1(sc, probe_addrs[i].efer, WB_DEVICE_ID_REG);
5881adb4ebdSXin LI 		dev_id = read_efdr_1(sc, probe_addrs[i].efer);
5891adb4ebdSXin LI 		write_efir_1(sc, probe_addrs[i].efer, WB_DEVICE_REV_REG);
5901adb4ebdSXin LI 		dev_rev = read_efdr_1(sc, probe_addrs[i].efer);
5911adb4ebdSXin LI 		write_efir_1(sc, probe_addrs[i].efer, WB_CR26);
5921adb4ebdSXin LI 		cr26 = read_efdr_1(sc, probe_addrs[i].efer);
5930566170fSBjoern A. Zeeb 
5940566170fSBjoern A. Zeeb 		/* HEFRAS of 0 means EFER at 0x2e, 1 means EFER at 0x4e. */
5950566170fSBjoern A. Zeeb 		if (((cr26 & 0x40) == 0x00 && probe_addrs[i].efer != 0x2e) ||
5960566170fSBjoern A. Zeeb 		    ((cr26 & 0x40) == 0x40 && probe_addrs[i].efer != 0x4e)) {
5971adb4ebdSXin LI 			if (dev != NULL)
5981adb4ebdSXin LI 				device_printf(dev, "HEFRAS and EFER do not "
5991adb4ebdSXin LI 				    "align: EFER 0x%02x DevID 0x%02x DevRev "
6001adb4ebdSXin LI 				    "0x%02x CR26 0x%02x\n",
6010566170fSBjoern A. Zeeb 				    probe_addrs[i].efer, dev_id, dev_rev, cr26);
6020566170fSBjoern A. Zeeb 			goto cleanup;
6030566170fSBjoern A. Zeeb 		}
6040566170fSBjoern A. Zeeb 
6059e8bad81SXin LI 		if (dev_id == 0xff && dev_rev == 0xff)
6069e8bad81SXin LI 			goto cleanup;
6079e8bad81SXin LI 
6080566170fSBjoern A. Zeeb 		for (j = 0; j < sizeof(wb_devs) / sizeof(*wb_devs); j++) {
6090566170fSBjoern A. Zeeb 			if (wb_devs[j].device_id == dev_id &&
6100566170fSBjoern A. Zeeb 			    wb_devs[j].device_rev == dev_rev) {
6111adb4ebdSXin LI 				if (probe && dev != NULL)
6120566170fSBjoern A. Zeeb 					device_set_desc(dev, wb_devs[j].descr);
6130566170fSBjoern A. Zeeb 				found++;
6140566170fSBjoern A. Zeeb 				break;
6150566170fSBjoern A. Zeeb 			}
6160566170fSBjoern A. Zeeb 		}
6179e8bad81SXin LI 
6189e8bad81SXin LI 		if (!found) {
6199e8bad81SXin LI 			if (probe && dev != NULL) {
6209e8bad81SXin LI 				device_set_desc(dev, "Unknown Winbond/Nuvoton model");
6219e8bad81SXin LI 				device_printf(dev, "DevID 0x%02x DevRev 0x%02x, "
6229e8bad81SXin LI 				    "please report this.\n", dev_id, dev_rev);
6239e8bad81SXin LI 			}
6249e8bad81SXin LI 			found++;
6259e8bad81SXin LI 		}
6269e8bad81SXin LI 
6271adb4ebdSXin LI 		if (probe && found && bootverbose && dev != NULL)
6280566170fSBjoern A. Zeeb 			device_printf(dev, "%s EFER 0x%02x ID 0x%02x Rev 0x%02x"
6290566170fSBjoern A. Zeeb 			     " CR26 0x%02x (probing)\n", device_get_desc(dev),
6300566170fSBjoern A. Zeeb 			     probe_addrs[i].efer, dev_id, dev_rev, cr26);
6310566170fSBjoern A. Zeeb cleanup:
6320566170fSBjoern A. Zeeb 		if (probe || !found) {
6331adb4ebdSXin LI 			(*probe_addrs[i].ext_cfg_exit_f)(sc, probe_addrs[i].efer);
6340566170fSBjoern A. Zeeb 
6351adb4ebdSXin LI 			if (sc != NULL)
6361adb4ebdSXin LI 				(void) bus_release_resource(dev, SYS_RES_IOPORT,
6371adb4ebdSXin LI 				    sc->rid, sc->portres);
6380566170fSBjoern A. Zeeb 		}
6390566170fSBjoern A. Zeeb 
6400566170fSBjoern A. Zeeb 		/*
6410566170fSBjoern A. Zeeb 		 * Stop probing if have successfully identified the SuperIO.
6420566170fSBjoern A. Zeeb 		 * Remember the extended function mode enter/exit functions
6430566170fSBjoern A. Zeeb 		 * for operations.
6440566170fSBjoern A. Zeeb 		 */
6450566170fSBjoern A. Zeeb 		if (found) {
6461adb4ebdSXin LI 			if (sc != NULL) {
6470566170fSBjoern A. Zeeb 				sc->ext_cfg_enter_f = probe_addrs[i].ext_cfg_enter_f;
6480566170fSBjoern A. Zeeb 				sc->ext_cfg_exit_f = probe_addrs[i].ext_cfg_exit_f;
6491adb4ebdSXin LI 			}
6500566170fSBjoern A. Zeeb 			error = BUS_PROBE_DEFAULT;
6510566170fSBjoern A. Zeeb 			break;
6520566170fSBjoern A. Zeeb 		} else
6530566170fSBjoern A. Zeeb 			error = ENXIO;
6540566170fSBjoern A. Zeeb 	}
6550566170fSBjoern A. Zeeb 
6560566170fSBjoern A. Zeeb 	return (error);
6570566170fSBjoern A. Zeeb }
6580566170fSBjoern A. Zeeb 
6591adb4ebdSXin LI static void
6601adb4ebdSXin LI wb_identify(driver_t *driver, device_t parent)
6611adb4ebdSXin LI {
6621adb4ebdSXin LI 	device_t dev;
6631adb4ebdSXin LI 
6641adb4ebdSXin LI 	if ((dev = device_find_child(parent, driver->name, 0)) == NULL) {
6651adb4ebdSXin LI 		if (wb_probe_enable(dev, 1) != BUS_PROBE_DEFAULT) {
6661adb4ebdSXin LI 			if (bootverbose)
6671adb4ebdSXin LI 				device_printf(dev, "can not find compatible Winbond chip.\n");
6681adb4ebdSXin LI 		} else
6691adb4ebdSXin LI 			dev = BUS_ADD_CHILD(parent, 0, driver->name, 0);
6701adb4ebdSXin LI 		return;
6711adb4ebdSXin LI 	}
6721adb4ebdSXin LI }
6731adb4ebdSXin LI 
6740566170fSBjoern A. Zeeb static int
6750566170fSBjoern A. Zeeb wb_probe(device_t dev)
6760566170fSBjoern A. Zeeb {
6770566170fSBjoern A. Zeeb 
6780566170fSBjoern A. Zeeb 	/* Make sure we do not claim some ISA PNP device. */
6790566170fSBjoern A. Zeeb 	if (isa_get_logicalid(dev) != 0)
6800566170fSBjoern A. Zeeb 		return (ENXIO);
6810566170fSBjoern A. Zeeb 
6820566170fSBjoern A. Zeeb 	return (wb_probe_enable(dev, 1));
6830566170fSBjoern A. Zeeb }
6840566170fSBjoern A. Zeeb 
6850566170fSBjoern A. Zeeb static int
6860566170fSBjoern A. Zeeb wb_attach(device_t dev)
6870566170fSBjoern A. Zeeb {
6880566170fSBjoern A. Zeeb 	struct wb_softc *sc;
6890566170fSBjoern A. Zeeb 	struct sysctl_ctx_list *sctx;
6900566170fSBjoern A. Zeeb 	struct sysctl_oid *soid;
6910566170fSBjoern A. Zeeb 	unsigned long timeout;
6920566170fSBjoern A. Zeeb 	int error;
6930566170fSBjoern A. Zeeb 
6940566170fSBjoern A. Zeeb 	error = wb_probe_enable(dev, 0);
6950566170fSBjoern A. Zeeb 	if (error > 0)
6960566170fSBjoern A. Zeeb 		return (ENXIO);
6970566170fSBjoern A. Zeeb 
6980566170fSBjoern A. Zeeb 	sc = device_get_softc(dev);
6990566170fSBjoern A. Zeeb 	KASSERT(sc->ext_cfg_enter_f != NULL && sc->ext_cfg_exit_f != NULL,
7000566170fSBjoern A. Zeeb 	    ("%s: successfull probe result but not setup correctly", __func__));
7010566170fSBjoern A. Zeeb 
7020566170fSBjoern A. Zeeb 	/* Watchdog is configured as part of LDN 8 (GPIO Port2, Watchdog). */
7031adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN_REG);
7041adb4ebdSXin LI 	write_efdr_1(sc, 0, WB_LDN_REG_LDN8);
7050566170fSBjoern A. Zeeb 
7060566170fSBjoern A. Zeeb 	/* Make sure LDN8 is enabled (Do we need to? Also affects GPIO). */
7071adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CR30);
7081adb4ebdSXin LI 	write_efdr_1(sc, 0, WB_LDN8_CR30_ACTIVE);
7090566170fSBjoern A. Zeeb 
7100566170fSBjoern A. Zeeb 	/* Read the current watchdog configuration. */
7111adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF5);
7121adb4ebdSXin LI 	sc->reg_1 = read_efdr_1(sc, 0);
7131adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF6);
7141adb4ebdSXin LI 	sc->reg_timeout = read_efdr_1(sc, 0);
7151adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF7);
7161adb4ebdSXin LI 	sc->reg_2 = read_efdr_1(sc, 0);
7170566170fSBjoern A. Zeeb 
7180566170fSBjoern A. Zeeb 	/* Print current state if bootverbose or watchdog already enabled. */
7190566170fSBjoern A. Zeeb 	if (bootverbose || (sc->reg_timeout > 0x00))
7200566170fSBjoern A. Zeeb 		wb_print_state(sc, "Before watchdog attach");
7210566170fSBjoern A. Zeeb 
7220566170fSBjoern A. Zeeb 	/*
7230566170fSBjoern A. Zeeb 	 * Clear a previous watchdog timeout event (if (still) set).
7240566170fSBjoern A. Zeeb 	 * Disable all all interrupt reset sources (defaults).
7250566170fSBjoern A. Zeeb 	 */
7260566170fSBjoern A. Zeeb 	sc->reg_1 &= ~(WB_LDN8_CRF5_KEYB_P20);
727cf864f03SRobert Noland 	sc->reg_1 |= WB_LDN8_CRF5_KBRST;
7281adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF5);
7291adb4ebdSXin LI 	write_efdr_1(sc, 0, sc->reg_1);
7300566170fSBjoern A. Zeeb 
7310566170fSBjoern A. Zeeb 	sc->reg_2 &= ~WB_LDN8_CRF7_CLEAR_MASK;
7321adb4ebdSXin LI 	write_efir_1(sc, 0, WB_LDN8_CRF7);
7331adb4ebdSXin LI 	write_efdr_1(sc, 0, sc->reg_2);
7340566170fSBjoern A. Zeeb 
7350566170fSBjoern A. Zeeb 	/* Read global timeout override tunable, Add per device sysctls. */
7360566170fSBjoern A. Zeeb 	if (TUNABLE_ULONG_FETCH("hw.wbwd.timeout_override", &timeout)) {
7370566170fSBjoern A. Zeeb 		if (timeout > 0)
7380566170fSBjoern A. Zeeb 			sc->timeout_override = timeout;
7390566170fSBjoern A. Zeeb 	}
7400566170fSBjoern A. Zeeb 	sctx = device_get_sysctl_ctx(dev);
7410566170fSBjoern A. Zeeb 	soid = device_get_sysctl_tree(dev);
7420566170fSBjoern A. Zeeb         SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
7430566170fSBjoern A. Zeeb 	    "timeout_override", CTLFLAG_RW, &sc->timeout_override, 0,
7440566170fSBjoern A. Zeeb             "Timeout in seconds overriding default watchdog timeout");
7450566170fSBjoern A. Zeeb         SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
7460566170fSBjoern A. Zeeb 	    "debug_verbose", CTLFLAG_RW, &sc->debug_verbose, 0,
7470566170fSBjoern A. Zeeb             "Enables extra debugging information");
7480566170fSBjoern A. Zeeb         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "debug",
7490566170fSBjoern A. Zeeb 	    CTLTYPE_STRING|CTLFLAG_RD, sc, 0, sysctl_wb_debug, "A",
7500566170fSBjoern A. Zeeb             "Selected register information from last change by driver");
7510566170fSBjoern A. Zeeb         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "debug_current",
7520566170fSBjoern A. Zeeb 	    CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_SKIP, sc, 0,
7530566170fSBjoern A. Zeeb 	     sysctl_wb_debug_current, "A",
7540566170fSBjoern A. Zeeb 	     "Selected register information (may interfere)");
7550566170fSBjoern A. Zeeb 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "force_timeout",
7560566170fSBjoern A. Zeeb 	    CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_SKIP, sc, 0,
7570566170fSBjoern A. Zeeb 	    sysctl_wb_force_test_nmi, "I", "Enable to force watchdog to fire.");
7580566170fSBjoern A. Zeeb 
7590566170fSBjoern A. Zeeb 	/* Register watchdog. */
7600566170fSBjoern A. Zeeb 	sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, wb_watchdog_fn, sc,
7610566170fSBjoern A. Zeeb 	    0);
7620566170fSBjoern A. Zeeb 
7630566170fSBjoern A. Zeeb 	if (bootverbose)
7640566170fSBjoern A. Zeeb 		wb_print_state(sc, "After watchdog attach");
7650566170fSBjoern A. Zeeb 
7660566170fSBjoern A. Zeeb 	return (0);
7670566170fSBjoern A. Zeeb }
7680566170fSBjoern A. Zeeb 
7690566170fSBjoern A. Zeeb static int
7700566170fSBjoern A. Zeeb wb_detach(device_t dev)
7710566170fSBjoern A. Zeeb {
7720566170fSBjoern A. Zeeb 	struct wb_softc *sc;
7730566170fSBjoern A. Zeeb 
7740566170fSBjoern A. Zeeb 	sc = device_get_softc(dev);
7750566170fSBjoern A. Zeeb 
7760566170fSBjoern A. Zeeb 	/* Unregister and stop the watchdog if running. */
7770566170fSBjoern A. Zeeb 	if (sc->ev_tag)
7780566170fSBjoern A. Zeeb 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
7790566170fSBjoern A. Zeeb 	wb_set_watchdog(sc, 0);
7800566170fSBjoern A. Zeeb 
7810566170fSBjoern A. Zeeb 	/* Disable extended function mode. */
7821adb4ebdSXin LI 	(*sc->ext_cfg_exit_f)(sc, 0);
7830566170fSBjoern A. Zeeb 
7840566170fSBjoern A. Zeeb 	/* Cleanup resources. */
7850566170fSBjoern A. Zeeb 	(void) bus_release_resource(dev, SYS_RES_IOPORT, sc->rid, sc->portres);
7860566170fSBjoern A. Zeeb 
7870566170fSBjoern A. Zeeb 	/* Bus subroutines take care of sysctls already. */
7880566170fSBjoern A. Zeeb 
7890566170fSBjoern A. Zeeb 	return (0);
7900566170fSBjoern A. Zeeb }
7910566170fSBjoern A. Zeeb 
7920566170fSBjoern A. Zeeb static device_method_t wb_methods[] = {
7930566170fSBjoern A. Zeeb 	/* Device interface */
7941adb4ebdSXin LI 	DEVMETHOD(device_identify,	wb_identify),
7950566170fSBjoern A. Zeeb 	DEVMETHOD(device_probe,		wb_probe),
7960566170fSBjoern A. Zeeb 	DEVMETHOD(device_attach,	wb_attach),
7970566170fSBjoern A. Zeeb 	DEVMETHOD(device_detach,	wb_detach),
7980566170fSBjoern A. Zeeb 
799e25fe6cdSXin LI 	DEVMETHOD_END
8000566170fSBjoern A. Zeeb };
8010566170fSBjoern A. Zeeb 
8020566170fSBjoern A. Zeeb static driver_t wb_isa_driver = {
8030566170fSBjoern A. Zeeb 	"wbwd",
8040566170fSBjoern A. Zeeb 	wb_methods,
8050566170fSBjoern A. Zeeb 	sizeof(struct wb_softc)
8060566170fSBjoern A. Zeeb };
8070566170fSBjoern A. Zeeb 
8080566170fSBjoern A. Zeeb static devclass_t wb_devclass;
8090566170fSBjoern A. Zeeb 
8100566170fSBjoern A. Zeeb DRIVER_MODULE(wb, isa, wb_isa_driver, wb_devclass, NULL, NULL);
811