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