xref: /freebsd/sys/dev/viawd/viawd.c (revision a7c3afc33f08d5f29bacd8650b9bc385a3ddbe70)
161af1d13SFabien Thomas /*-
2*a7c3afc3SFabien Thomas  * Copyright (c) 2011 Fabien Thomas <fabient@FreeBSD.org>
361af1d13SFabien Thomas  * All rights reserved.
461af1d13SFabien Thomas  *
561af1d13SFabien Thomas  * Redistribution and use in source and binary forms, with or without
661af1d13SFabien Thomas  * modification, are permitted provided that the following conditions
761af1d13SFabien Thomas  * are met:
861af1d13SFabien Thomas  * 1. Redistributions of source code must retain the above copyright
961af1d13SFabien Thomas  *    notice, this list of conditions and the following disclaimer.
1061af1d13SFabien Thomas  * 2. Redistributions in binary form must reproduce the above copyright
1161af1d13SFabien Thomas  *    notice, this list of conditions and the following disclaimer in the
1261af1d13SFabien Thomas  *    documentation and/or other materials provided with the distribution.
1361af1d13SFabien Thomas  *
1461af1d13SFabien Thomas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1561af1d13SFabien Thomas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1661af1d13SFabien Thomas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1761af1d13SFabien Thomas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1861af1d13SFabien Thomas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1961af1d13SFabien Thomas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2061af1d13SFabien Thomas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2161af1d13SFabien Thomas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2261af1d13SFabien Thomas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2361af1d13SFabien Thomas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2461af1d13SFabien Thomas  * SUCH DAMAGE.
2561af1d13SFabien Thomas  */
2661af1d13SFabien Thomas 
2761af1d13SFabien Thomas #include <sys/cdefs.h>
2861af1d13SFabien Thomas __FBSDID("$FreeBSD$");
2961af1d13SFabien Thomas 
3061af1d13SFabien Thomas #include <sys/param.h>
3161af1d13SFabien Thomas #include <sys/kernel.h>
3261af1d13SFabien Thomas #include <sys/module.h>
3361af1d13SFabien Thomas #include <sys/systm.h>
3461af1d13SFabien Thomas #include <sys/bus.h>
3561af1d13SFabien Thomas #include <machine/bus.h>
3661af1d13SFabien Thomas #include <sys/rman.h>
3761af1d13SFabien Thomas #include <machine/resource.h>
3861af1d13SFabien Thomas #include <sys/watchdog.h>
3961af1d13SFabien Thomas 
4061af1d13SFabien Thomas #include <isa/isavar.h>
4161af1d13SFabien Thomas #include <dev/pci/pcivar.h>
4261af1d13SFabien Thomas 
4361af1d13SFabien Thomas #include "viawd.h"
4461af1d13SFabien Thomas 
457e1f37e1SFabien Thomas #define	viawd_read_4(sc, off)	bus_read_4((sc)->wd_res, (off))
467e1f37e1SFabien Thomas #define	viawd_write_4(sc, off, val)	\
477e1f37e1SFabien Thomas 	bus_write_4((sc)->wd_res, (off), (val))
4861af1d13SFabien Thomas 
4961af1d13SFabien Thomas static struct viawd_device viawd_devices[] = {
5061af1d13SFabien Thomas 	{ DEVICEID_VT8251, "VIA VT8251 watchdog timer" },
5161af1d13SFabien Thomas 	{ DEVICEID_CX700,  "VIA CX700 watchdog timer" },
5261af1d13SFabien Thomas 	{ DEVICEID_VX800,  "VIA VX800 watchdog timer" },
5361af1d13SFabien Thomas 	{ DEVICEID_VX855,  "VIA VX855 watchdog timer" },
5461af1d13SFabien Thomas 	{ DEVICEID_VX900,  "VIA VX900 watchdog timer" },
5561af1d13SFabien Thomas 	{ 0, NULL },
5661af1d13SFabien Thomas };
5761af1d13SFabien Thomas 
5861af1d13SFabien Thomas static devclass_t viawd_devclass;
5961af1d13SFabien Thomas 
6061af1d13SFabien Thomas static void
6161af1d13SFabien Thomas viawd_tmr_state(struct viawd_softc *sc, int enable)
6261af1d13SFabien Thomas {
6361af1d13SFabien Thomas 	uint32_t reg;
6461af1d13SFabien Thomas 
657e1f37e1SFabien Thomas 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
6661af1d13SFabien Thomas 	if (enable)
6761af1d13SFabien Thomas 		reg |= VIAWD_MEM_CTRL_TRIGGER | VIAWD_MEM_CTRL_ENABLE;
6861af1d13SFabien Thomas 	else
6961af1d13SFabien Thomas 		reg &= ~VIAWD_MEM_CTRL_ENABLE;
707e1f37e1SFabien Thomas 	viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
7161af1d13SFabien Thomas }
7261af1d13SFabien Thomas 
7361af1d13SFabien Thomas static void
7461af1d13SFabien Thomas viawd_tmr_set(struct viawd_softc *sc, unsigned int timeout)
7561af1d13SFabien Thomas {
7661af1d13SFabien Thomas 
7761af1d13SFabien Thomas 	/* Keep value in range. */
7861af1d13SFabien Thomas 	if (timeout < VIAWD_MEM_COUNT_MIN)
7961af1d13SFabien Thomas 		timeout = VIAWD_MEM_COUNT_MIN;
8061af1d13SFabien Thomas 	else if (timeout > VIAWD_MEM_COUNT_MAX)
8161af1d13SFabien Thomas 		timeout = VIAWD_MEM_COUNT_MAX;
8261af1d13SFabien Thomas 
837e1f37e1SFabien Thomas 	viawd_write_4(sc, VIAWD_MEM_COUNT, timeout);
8461af1d13SFabien Thomas 	sc->timeout = timeout;
8561af1d13SFabien Thomas }
8661af1d13SFabien Thomas 
8761af1d13SFabien Thomas /*
8861af1d13SFabien Thomas  * Watchdog event handler - called by the framework to enable or disable
8961af1d13SFabien Thomas  * the watchdog or change the initial timeout value.
9061af1d13SFabien Thomas  */
9161af1d13SFabien Thomas static void
9261af1d13SFabien Thomas viawd_event(void *arg, unsigned int cmd, int *error)
9361af1d13SFabien Thomas {
9461af1d13SFabien Thomas 	struct viawd_softc *sc = arg;
9561af1d13SFabien Thomas 	unsigned int timeout;
9661af1d13SFabien Thomas 
9761af1d13SFabien Thomas 	/* Convert from power-of-two-ns to second. */
9861af1d13SFabien Thomas 	cmd &= WD_INTERVAL;
9961af1d13SFabien Thomas 	timeout = ((uint64_t)1 << cmd) / 1000000000;
10061af1d13SFabien Thomas 	if (cmd) {
10161af1d13SFabien Thomas 		if (timeout != sc->timeout)
10261af1d13SFabien Thomas 			viawd_tmr_set(sc, timeout);
10361af1d13SFabien Thomas 		viawd_tmr_state(sc, 1);
10461af1d13SFabien Thomas 		*error = 0;
10561af1d13SFabien Thomas 	} else
10661af1d13SFabien Thomas 		viawd_tmr_state(sc, 0);
10761af1d13SFabien Thomas }
10861af1d13SFabien Thomas 
1097e1f37e1SFabien Thomas /* Look for a supported VIA south bridge. */
1107e1f37e1SFabien Thomas static struct viawd_device *
1117e1f37e1SFabien Thomas viawd_find(device_t dev)
1127e1f37e1SFabien Thomas {
1137e1f37e1SFabien Thomas 	struct viawd_device *id;
1147e1f37e1SFabien Thomas 
1157e1f37e1SFabien Thomas 	if (pci_get_vendor(dev) != VENDORID_VIA)
1167e1f37e1SFabien Thomas 		return (NULL);
1177e1f37e1SFabien Thomas 	for (id = viawd_devices; id->desc != NULL; id++)
1187e1f37e1SFabien Thomas 		if (pci_get_device(dev) == id->device)
1197e1f37e1SFabien Thomas 			return (id);
1207e1f37e1SFabien Thomas 	return (NULL);
1217e1f37e1SFabien Thomas }
1227e1f37e1SFabien Thomas 
12361af1d13SFabien Thomas static void
12461af1d13SFabien Thomas viawd_identify(driver_t *driver, device_t parent)
12561af1d13SFabien Thomas {
12661af1d13SFabien Thomas 
1277e1f37e1SFabien Thomas 	if (viawd_find(parent) == NULL)
12861af1d13SFabien Thomas 		return;
12961af1d13SFabien Thomas 
1307e1f37e1SFabien Thomas 	if (device_find_child(parent, driver->name, -1) == NULL)
1317e1f37e1SFabien Thomas 		BUS_ADD_CHILD(parent, 0, driver->name, 0);
13261af1d13SFabien Thomas }
13361af1d13SFabien Thomas 
13461af1d13SFabien Thomas static int
13561af1d13SFabien Thomas viawd_probe(device_t dev)
13661af1d13SFabien Thomas {
1377e1f37e1SFabien Thomas 	struct viawd_device *id;
13861af1d13SFabien Thomas 
1397e1f37e1SFabien Thomas 	id = viawd_find(device_get_parent(dev));
1407e1f37e1SFabien Thomas 	KASSERT(id != NULL, ("parent should be a valid VIA SB"));
1417e1f37e1SFabien Thomas 	device_set_desc(dev, id->desc);
1427e1f37e1SFabien Thomas 	return (BUS_PROBE_GENERIC);
14361af1d13SFabien Thomas }
14461af1d13SFabien Thomas 
14561af1d13SFabien Thomas static int
14661af1d13SFabien Thomas viawd_attach(device_t dev)
14761af1d13SFabien Thomas {
14861af1d13SFabien Thomas 	device_t sb_dev;
14961af1d13SFabien Thomas 	struct viawd_softc *sc;
15061af1d13SFabien Thomas 	uint32_t pmbase, reg;
15161af1d13SFabien Thomas 
15261af1d13SFabien Thomas 	sc = device_get_softc(dev);
15361af1d13SFabien Thomas 	sc->dev = dev;
15461af1d13SFabien Thomas 
1557e1f37e1SFabien Thomas 	sb_dev = device_get_parent(dev);
15661af1d13SFabien Thomas 	if (sb_dev == NULL) {
15761af1d13SFabien Thomas 		device_printf(dev, "Can not find watchdog device.\n");
15861af1d13SFabien Thomas 		goto fail;
15961af1d13SFabien Thomas 	}
16061af1d13SFabien Thomas 	sc->sb_dev = sb_dev;
16161af1d13SFabien Thomas 
16261af1d13SFabien Thomas 	/* Get watchdog memory base. */
16361af1d13SFabien Thomas 	pmbase = pci_read_config(sb_dev, VIAWD_CONFIG_BASE, 4);
16461af1d13SFabien Thomas 	if (pmbase == 0) {
16561af1d13SFabien Thomas 		device_printf(dev,
16661af1d13SFabien Thomas 		    "Watchdog disabled in BIOS or hardware\n");
16761af1d13SFabien Thomas 		goto fail;
16861af1d13SFabien Thomas 	}
16961af1d13SFabien Thomas 
17061af1d13SFabien Thomas 	/* Allocate I/O register space. */
17161af1d13SFabien Thomas 	sc->wd_rid = 0;
17261af1d13SFabien Thomas 	sc->wd_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->wd_rid,
17361af1d13SFabien Thomas 	    pmbase, pmbase + VIAWD_MEM_LEN - 1, VIAWD_MEM_LEN,
17461af1d13SFabien Thomas 	    RF_ACTIVE | RF_SHAREABLE);
17561af1d13SFabien Thomas 	if (sc->wd_res == NULL) {
17661af1d13SFabien Thomas 		device_printf(dev, "Unable to map watchdog memory\n");
17761af1d13SFabien Thomas 		goto fail;
17861af1d13SFabien Thomas 	}
17961af1d13SFabien Thomas 
18061af1d13SFabien Thomas 	/* Check if watchdog fired last boot. */
1817e1f37e1SFabien Thomas 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
18261af1d13SFabien Thomas 	if (reg & VIAWD_MEM_CTRL_FIRED) {
18361af1d13SFabien Thomas 		device_printf(dev,
18461af1d13SFabien Thomas 		    "ERROR: watchdog rebooted the system\n");
18561af1d13SFabien Thomas 		/* Reset bit state. */
1867e1f37e1SFabien Thomas 		viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
18761af1d13SFabien Thomas 	}
18861af1d13SFabien Thomas 
18961af1d13SFabien Thomas 	/* Register the watchdog event handler. */
19061af1d13SFabien Thomas 	sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, viawd_event, sc, 0);
19161af1d13SFabien Thomas 
19261af1d13SFabien Thomas 	return (0);
19361af1d13SFabien Thomas fail:
19461af1d13SFabien Thomas 	if (sc->wd_res != NULL)
19561af1d13SFabien Thomas 		bus_release_resource(dev, SYS_RES_MEMORY,
19661af1d13SFabien Thomas 		    sc->wd_rid, sc->wd_res);
19761af1d13SFabien Thomas 	return (ENXIO);
19861af1d13SFabien Thomas }
19961af1d13SFabien Thomas 
20061af1d13SFabien Thomas static int
20161af1d13SFabien Thomas viawd_detach(device_t dev)
20261af1d13SFabien Thomas {
20361af1d13SFabien Thomas 	struct viawd_softc *sc;
20461af1d13SFabien Thomas 	uint32_t reg;
20561af1d13SFabien Thomas 
20661af1d13SFabien Thomas 	sc = device_get_softc(dev);
20761af1d13SFabien Thomas 
20861af1d13SFabien Thomas 	/* Deregister event handler. */
20961af1d13SFabien Thomas 	if (sc->ev_tag != NULL)
21061af1d13SFabien Thomas 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
21161af1d13SFabien Thomas 	sc->ev_tag = NULL;
21261af1d13SFabien Thomas 
21361af1d13SFabien Thomas 	/*
21461af1d13SFabien Thomas 	 * Do not stop the watchdog on shutdown if active but bump the
21561af1d13SFabien Thomas 	 * timer to avoid spurious reset.
21661af1d13SFabien Thomas 	 */
2177e1f37e1SFabien Thomas 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
21861af1d13SFabien Thomas 	if (reg & VIAWD_MEM_CTRL_ENABLE) {
21961af1d13SFabien Thomas 		viawd_tmr_set(sc, VIAWD_TIMEOUT_SHUTDOWN);
22061af1d13SFabien Thomas 		viawd_tmr_state(sc, 1);
22161af1d13SFabien Thomas 		device_printf(dev,
22261af1d13SFabien Thomas 		    "Keeping watchog alive during shutdown for %d seconds\n",
22361af1d13SFabien Thomas 		    VIAWD_TIMEOUT_SHUTDOWN);
22461af1d13SFabien Thomas 	}
22561af1d13SFabien Thomas 
22661af1d13SFabien Thomas 	if (sc->wd_res != NULL)
22761af1d13SFabien Thomas 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
22861af1d13SFabien Thomas 		    sc->wd_rid, sc->wd_res);
22961af1d13SFabien Thomas 
23061af1d13SFabien Thomas 	return (0);
23161af1d13SFabien Thomas }
23261af1d13SFabien Thomas 
23361af1d13SFabien Thomas static device_method_t viawd_methods[] = {
23461af1d13SFabien Thomas 	DEVMETHOD(device_identify, viawd_identify),
23561af1d13SFabien Thomas 	DEVMETHOD(device_probe,	viawd_probe),
23661af1d13SFabien Thomas 	DEVMETHOD(device_attach, viawd_attach),
23761af1d13SFabien Thomas 	DEVMETHOD(device_detach, viawd_detach),
23861af1d13SFabien Thomas 	DEVMETHOD(device_shutdown, viawd_detach),
23961af1d13SFabien Thomas 	{0,0}
24061af1d13SFabien Thomas };
24161af1d13SFabien Thomas 
24261af1d13SFabien Thomas static driver_t viawd_driver = {
24361af1d13SFabien Thomas 	"viawd",
24461af1d13SFabien Thomas 	viawd_methods,
24561af1d13SFabien Thomas 	sizeof(struct viawd_softc),
24661af1d13SFabien Thomas };
24761af1d13SFabien Thomas 
2487e1f37e1SFabien Thomas DRIVER_MODULE(viawd, isab, viawd_driver, viawd_devclass, NULL, NULL);
249