xref: /freebsd/sys/dev/viawd/viawd.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
161af1d13SFabien Thomas /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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>
33e2e050c8SConrad 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 void
6261af1d13SFabien Thomas viawd_tmr_state(struct viawd_softc *sc, int enable)
6361af1d13SFabien Thomas {
6461af1d13SFabien Thomas 	uint32_t reg;
6561af1d13SFabien Thomas 
667e1f37e1SFabien Thomas 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
6761af1d13SFabien Thomas 	if (enable)
6861af1d13SFabien Thomas 		reg |= VIAWD_MEM_CTRL_TRIGGER | VIAWD_MEM_CTRL_ENABLE;
6961af1d13SFabien Thomas 	else
7061af1d13SFabien Thomas 		reg &= ~VIAWD_MEM_CTRL_ENABLE;
717e1f37e1SFabien Thomas 	viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
7261af1d13SFabien Thomas }
7361af1d13SFabien Thomas 
7461af1d13SFabien Thomas static void
7561af1d13SFabien Thomas viawd_tmr_set(struct viawd_softc *sc, unsigned int timeout)
7661af1d13SFabien Thomas {
7761af1d13SFabien Thomas 
7861af1d13SFabien Thomas 	/* Keep value in range. */
7961af1d13SFabien Thomas 	if (timeout < VIAWD_MEM_COUNT_MIN)
8061af1d13SFabien Thomas 		timeout = VIAWD_MEM_COUNT_MIN;
8161af1d13SFabien Thomas 	else if (timeout > VIAWD_MEM_COUNT_MAX)
8261af1d13SFabien Thomas 		timeout = VIAWD_MEM_COUNT_MAX;
8361af1d13SFabien Thomas 
847e1f37e1SFabien Thomas 	viawd_write_4(sc, VIAWD_MEM_COUNT, timeout);
8561af1d13SFabien Thomas 	sc->timeout = timeout;
8661af1d13SFabien Thomas }
8761af1d13SFabien Thomas 
8861af1d13SFabien Thomas /*
8961af1d13SFabien Thomas  * Watchdog event handler - called by the framework to enable or disable
9061af1d13SFabien Thomas  * the watchdog or change the initial timeout value.
9161af1d13SFabien Thomas  */
9261af1d13SFabien Thomas static void
9361af1d13SFabien Thomas viawd_event(void *arg, unsigned int cmd, int *error)
9461af1d13SFabien Thomas {
9561af1d13SFabien Thomas 	struct viawd_softc *sc = arg;
9661af1d13SFabien Thomas 	unsigned int timeout;
9761af1d13SFabien Thomas 
9861af1d13SFabien Thomas 	/* Convert from power-of-two-ns to second. */
9961af1d13SFabien Thomas 	cmd &= WD_INTERVAL;
10061af1d13SFabien Thomas 	timeout = ((uint64_t)1 << cmd) / 1000000000;
10161af1d13SFabien Thomas 	if (cmd) {
10261af1d13SFabien Thomas 		if (timeout != sc->timeout)
10361af1d13SFabien Thomas 			viawd_tmr_set(sc, timeout);
10461af1d13SFabien Thomas 		viawd_tmr_state(sc, 1);
10561af1d13SFabien Thomas 		*error = 0;
10661af1d13SFabien Thomas 	} else
10761af1d13SFabien Thomas 		viawd_tmr_state(sc, 0);
10861af1d13SFabien Thomas }
10961af1d13SFabien Thomas 
1107e1f37e1SFabien Thomas /* Look for a supported VIA south bridge. */
1117e1f37e1SFabien Thomas static struct viawd_device *
1127e1f37e1SFabien Thomas viawd_find(device_t dev)
1137e1f37e1SFabien Thomas {
1147e1f37e1SFabien Thomas 	struct viawd_device *id;
1157e1f37e1SFabien Thomas 
1167e1f37e1SFabien Thomas 	if (pci_get_vendor(dev) != VENDORID_VIA)
1177e1f37e1SFabien Thomas 		return (NULL);
1187e1f37e1SFabien Thomas 	for (id = viawd_devices; id->desc != NULL; id++)
1197e1f37e1SFabien Thomas 		if (pci_get_device(dev) == id->device)
1207e1f37e1SFabien Thomas 			return (id);
1217e1f37e1SFabien Thomas 	return (NULL);
1227e1f37e1SFabien Thomas }
1237e1f37e1SFabien Thomas 
12461af1d13SFabien Thomas static void
12561af1d13SFabien Thomas viawd_identify(driver_t *driver, device_t parent)
12661af1d13SFabien Thomas {
12761af1d13SFabien Thomas 
1287e1f37e1SFabien Thomas 	if (viawd_find(parent) == NULL)
12961af1d13SFabien Thomas 		return;
13061af1d13SFabien Thomas 
1317e1f37e1SFabien Thomas 	if (device_find_child(parent, driver->name, -1) == NULL)
1327e1f37e1SFabien Thomas 		BUS_ADD_CHILD(parent, 0, driver->name, 0);
13361af1d13SFabien Thomas }
13461af1d13SFabien Thomas 
13561af1d13SFabien Thomas static int
13661af1d13SFabien Thomas viawd_probe(device_t dev)
13761af1d13SFabien Thomas {
1387e1f37e1SFabien Thomas 	struct viawd_device *id;
13961af1d13SFabien Thomas 
1407e1f37e1SFabien Thomas 	id = viawd_find(device_get_parent(dev));
1417e1f37e1SFabien Thomas 	KASSERT(id != NULL, ("parent should be a valid VIA SB"));
1427e1f37e1SFabien Thomas 	device_set_desc(dev, id->desc);
1437e1f37e1SFabien Thomas 	return (BUS_PROBE_GENERIC);
14461af1d13SFabien Thomas }
14561af1d13SFabien Thomas 
14661af1d13SFabien Thomas static int
14761af1d13SFabien Thomas viawd_attach(device_t dev)
14861af1d13SFabien Thomas {
14961af1d13SFabien Thomas 	device_t sb_dev;
15061af1d13SFabien Thomas 	struct viawd_softc *sc;
15161af1d13SFabien Thomas 	uint32_t pmbase, reg;
15261af1d13SFabien Thomas 
15361af1d13SFabien Thomas 	sc = device_get_softc(dev);
15461af1d13SFabien Thomas 	sc->dev = dev;
15561af1d13SFabien Thomas 
1567e1f37e1SFabien Thomas 	sb_dev = device_get_parent(dev);
15761af1d13SFabien Thomas 	if (sb_dev == NULL) {
15861af1d13SFabien Thomas 		device_printf(dev, "Can not find watchdog device.\n");
15961af1d13SFabien Thomas 		goto fail;
16061af1d13SFabien Thomas 	}
16161af1d13SFabien Thomas 	sc->sb_dev = sb_dev;
16261af1d13SFabien Thomas 
16361af1d13SFabien Thomas 	/* Get watchdog memory base. */
16461af1d13SFabien Thomas 	pmbase = pci_read_config(sb_dev, VIAWD_CONFIG_BASE, 4);
16561af1d13SFabien Thomas 	if (pmbase == 0) {
16661af1d13SFabien Thomas 		device_printf(dev,
16761af1d13SFabien Thomas 		    "Watchdog disabled in BIOS or hardware\n");
16861af1d13SFabien Thomas 		goto fail;
16961af1d13SFabien Thomas 	}
17061af1d13SFabien Thomas 
17161af1d13SFabien Thomas 	/* Allocate I/O register space. */
172940853ddSFabien Thomas 	sc->wd_rid = VIAWD_CONFIG_BASE;
173940853ddSFabien Thomas 	sc->wd_res = bus_alloc_resource_any(sb_dev, SYS_RES_MEMORY, &sc->wd_rid,
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 	}
179940853ddSFabien Thomas 	if (rman_get_size(sc->wd_res) < VIAWD_MEM_LEN) {
180940853ddSFabien Thomas 		device_printf(dev, "Bad size for watchdog memory: %#x\n",
181940853ddSFabien Thomas 		    (unsigned)rman_get_size(sc->wd_res));
182940853ddSFabien Thomas 		goto fail;
183940853ddSFabien Thomas 	}
18461af1d13SFabien Thomas 
18561af1d13SFabien Thomas 	/* Check if watchdog fired last boot. */
1867e1f37e1SFabien Thomas 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
18761af1d13SFabien Thomas 	if (reg & VIAWD_MEM_CTRL_FIRED) {
18861af1d13SFabien Thomas 		device_printf(dev,
18961af1d13SFabien Thomas 		    "ERROR: watchdog rebooted the system\n");
19061af1d13SFabien Thomas 		/* Reset bit state. */
1917e1f37e1SFabien Thomas 		viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
19261af1d13SFabien Thomas 	}
19361af1d13SFabien Thomas 
19461af1d13SFabien Thomas 	/* Register the watchdog event handler. */
19561af1d13SFabien Thomas 	sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, viawd_event, sc, 0);
19661af1d13SFabien Thomas 
19761af1d13SFabien Thomas 	return (0);
19861af1d13SFabien Thomas fail:
19961af1d13SFabien Thomas 	if (sc->wd_res != NULL)
200940853ddSFabien Thomas 		bus_release_resource(sb_dev, SYS_RES_MEMORY,
20161af1d13SFabien Thomas 		    sc->wd_rid, sc->wd_res);
20261af1d13SFabien Thomas 	return (ENXIO);
20361af1d13SFabien Thomas }
20461af1d13SFabien Thomas 
20561af1d13SFabien Thomas static int
20661af1d13SFabien Thomas viawd_detach(device_t dev)
20761af1d13SFabien Thomas {
20861af1d13SFabien Thomas 	struct viawd_softc *sc;
20961af1d13SFabien Thomas 	uint32_t reg;
21061af1d13SFabien Thomas 
21161af1d13SFabien Thomas 	sc = device_get_softc(dev);
21261af1d13SFabien Thomas 
21361af1d13SFabien Thomas 	/* Deregister event handler. */
21461af1d13SFabien Thomas 	if (sc->ev_tag != NULL)
21561af1d13SFabien Thomas 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
21661af1d13SFabien Thomas 	sc->ev_tag = NULL;
21761af1d13SFabien Thomas 
21861af1d13SFabien Thomas 	/*
21961af1d13SFabien Thomas 	 * Do not stop the watchdog on shutdown if active but bump the
22061af1d13SFabien Thomas 	 * timer to avoid spurious reset.
22161af1d13SFabien Thomas 	 */
2227e1f37e1SFabien Thomas 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
22361af1d13SFabien Thomas 	if (reg & VIAWD_MEM_CTRL_ENABLE) {
22461af1d13SFabien Thomas 		viawd_tmr_set(sc, VIAWD_TIMEOUT_SHUTDOWN);
22561af1d13SFabien Thomas 		viawd_tmr_state(sc, 1);
22661af1d13SFabien Thomas 		device_printf(dev,
2271cba6077SGordon Bergling 		    "Keeping watchdog alive during shutdown for %d seconds\n",
22861af1d13SFabien Thomas 		    VIAWD_TIMEOUT_SHUTDOWN);
22961af1d13SFabien Thomas 	}
23061af1d13SFabien Thomas 
23161af1d13SFabien Thomas 	if (sc->wd_res != NULL)
232940853ddSFabien Thomas 		bus_release_resource(sc->sb_dev, SYS_RES_MEMORY,
23361af1d13SFabien Thomas 		    sc->wd_rid, sc->wd_res);
23461af1d13SFabien Thomas 
23561af1d13SFabien Thomas 	return (0);
23661af1d13SFabien Thomas }
23761af1d13SFabien Thomas 
23861af1d13SFabien Thomas static device_method_t viawd_methods[] = {
23961af1d13SFabien Thomas 	DEVMETHOD(device_identify, viawd_identify),
24061af1d13SFabien Thomas 	DEVMETHOD(device_probe,	viawd_probe),
24161af1d13SFabien Thomas 	DEVMETHOD(device_attach, viawd_attach),
24261af1d13SFabien Thomas 	DEVMETHOD(device_detach, viawd_detach),
24361af1d13SFabien Thomas 	DEVMETHOD(device_shutdown, viawd_detach),
24461af1d13SFabien Thomas 	{0,0}
24561af1d13SFabien Thomas };
24661af1d13SFabien Thomas 
24761af1d13SFabien Thomas static driver_t viawd_driver = {
24861af1d13SFabien Thomas 	"viawd",
24961af1d13SFabien Thomas 	viawd_methods,
25061af1d13SFabien Thomas 	sizeof(struct viawd_softc),
25161af1d13SFabien Thomas };
25261af1d13SFabien Thomas 
25386dc8398SJohn Baldwin DRIVER_MODULE(viawd, isab, viawd_driver, NULL, NULL);
254