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/param.h>
30e2e050c8SConrad Meyer #include <sys/eventhandler.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 void
viawd_tmr_state(struct viawd_softc * sc,int enable)5961af1d13SFabien Thomas viawd_tmr_state(struct viawd_softc *sc, int enable)
6061af1d13SFabien Thomas {
6161af1d13SFabien Thomas uint32_t reg;
6261af1d13SFabien Thomas
637e1f37e1SFabien Thomas reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
6461af1d13SFabien Thomas if (enable)
6561af1d13SFabien Thomas reg |= VIAWD_MEM_CTRL_TRIGGER | VIAWD_MEM_CTRL_ENABLE;
6661af1d13SFabien Thomas else
6761af1d13SFabien Thomas reg &= ~VIAWD_MEM_CTRL_ENABLE;
687e1f37e1SFabien Thomas viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
6961af1d13SFabien Thomas }
7061af1d13SFabien Thomas
7161af1d13SFabien Thomas static void
viawd_tmr_set(struct viawd_softc * sc,unsigned int timeout)7261af1d13SFabien Thomas viawd_tmr_set(struct viawd_softc *sc, unsigned int timeout)
7361af1d13SFabien Thomas {
7461af1d13SFabien Thomas
7561af1d13SFabien Thomas /* Keep value in range. */
7661af1d13SFabien Thomas if (timeout < VIAWD_MEM_COUNT_MIN)
7761af1d13SFabien Thomas timeout = VIAWD_MEM_COUNT_MIN;
7861af1d13SFabien Thomas else if (timeout > VIAWD_MEM_COUNT_MAX)
7961af1d13SFabien Thomas timeout = VIAWD_MEM_COUNT_MAX;
8061af1d13SFabien Thomas
817e1f37e1SFabien Thomas viawd_write_4(sc, VIAWD_MEM_COUNT, timeout);
8261af1d13SFabien Thomas sc->timeout = timeout;
8361af1d13SFabien Thomas }
8461af1d13SFabien Thomas
8561af1d13SFabien Thomas /*
8661af1d13SFabien Thomas * Watchdog event handler - called by the framework to enable or disable
8761af1d13SFabien Thomas * the watchdog or change the initial timeout value.
8861af1d13SFabien Thomas */
8961af1d13SFabien Thomas static void
viawd_event(void * arg,unsigned int cmd,int * error)9061af1d13SFabien Thomas viawd_event(void *arg, unsigned int cmd, int *error)
9161af1d13SFabien Thomas {
9261af1d13SFabien Thomas struct viawd_softc *sc = arg;
9361af1d13SFabien Thomas unsigned int timeout;
9461af1d13SFabien Thomas
9561af1d13SFabien Thomas /* Convert from power-of-two-ns to second. */
9661af1d13SFabien Thomas cmd &= WD_INTERVAL;
9761af1d13SFabien Thomas timeout = ((uint64_t)1 << cmd) / 1000000000;
9861af1d13SFabien Thomas if (cmd) {
9961af1d13SFabien Thomas if (timeout != sc->timeout)
10061af1d13SFabien Thomas viawd_tmr_set(sc, timeout);
10161af1d13SFabien Thomas viawd_tmr_state(sc, 1);
10261af1d13SFabien Thomas *error = 0;
10361af1d13SFabien Thomas } else
10461af1d13SFabien Thomas viawd_tmr_state(sc, 0);
10561af1d13SFabien Thomas }
10661af1d13SFabien Thomas
1077e1f37e1SFabien Thomas /* Look for a supported VIA south bridge. */
1087e1f37e1SFabien Thomas static struct viawd_device *
viawd_find(device_t dev)1097e1f37e1SFabien Thomas viawd_find(device_t dev)
1107e1f37e1SFabien Thomas {
1117e1f37e1SFabien Thomas struct viawd_device *id;
1127e1f37e1SFabien Thomas
1137e1f37e1SFabien Thomas if (pci_get_vendor(dev) != VENDORID_VIA)
1147e1f37e1SFabien Thomas return (NULL);
1157e1f37e1SFabien Thomas for (id = viawd_devices; id->desc != NULL; id++)
1167e1f37e1SFabien Thomas if (pci_get_device(dev) == id->device)
1177e1f37e1SFabien Thomas return (id);
1187e1f37e1SFabien Thomas return (NULL);
1197e1f37e1SFabien Thomas }
1207e1f37e1SFabien Thomas
12161af1d13SFabien Thomas static void
viawd_identify(driver_t * driver,device_t parent)12261af1d13SFabien Thomas viawd_identify(driver_t *driver, device_t parent)
12361af1d13SFabien Thomas {
12461af1d13SFabien Thomas
1257e1f37e1SFabien Thomas if (viawd_find(parent) == NULL)
12661af1d13SFabien Thomas return;
12761af1d13SFabien Thomas
1287e1f37e1SFabien Thomas if (device_find_child(parent, driver->name, -1) == NULL)
1297e1f37e1SFabien Thomas BUS_ADD_CHILD(parent, 0, driver->name, 0);
13061af1d13SFabien Thomas }
13161af1d13SFabien Thomas
13261af1d13SFabien Thomas static int
viawd_probe(device_t dev)13361af1d13SFabien Thomas viawd_probe(device_t dev)
13461af1d13SFabien Thomas {
1357e1f37e1SFabien Thomas struct viawd_device *id;
13661af1d13SFabien Thomas
1377e1f37e1SFabien Thomas id = viawd_find(device_get_parent(dev));
1387e1f37e1SFabien Thomas KASSERT(id != NULL, ("parent should be a valid VIA SB"));
1397e1f37e1SFabien Thomas device_set_desc(dev, id->desc);
1407e1f37e1SFabien Thomas return (BUS_PROBE_GENERIC);
14161af1d13SFabien Thomas }
14261af1d13SFabien Thomas
14361af1d13SFabien Thomas static int
viawd_attach(device_t dev)14461af1d13SFabien Thomas viawd_attach(device_t dev)
14561af1d13SFabien Thomas {
14661af1d13SFabien Thomas device_t sb_dev;
14761af1d13SFabien Thomas struct viawd_softc *sc;
14861af1d13SFabien Thomas uint32_t pmbase, reg;
14961af1d13SFabien Thomas
15061af1d13SFabien Thomas sc = device_get_softc(dev);
15161af1d13SFabien Thomas sc->dev = dev;
15261af1d13SFabien Thomas
1537e1f37e1SFabien Thomas sb_dev = device_get_parent(dev);
15461af1d13SFabien Thomas if (sb_dev == NULL) {
15561af1d13SFabien Thomas device_printf(dev, "Can not find watchdog device.\n");
15661af1d13SFabien Thomas goto fail;
15761af1d13SFabien Thomas }
15861af1d13SFabien Thomas sc->sb_dev = sb_dev;
15961af1d13SFabien Thomas
16061af1d13SFabien Thomas /* Get watchdog memory base. */
16161af1d13SFabien Thomas pmbase = pci_read_config(sb_dev, VIAWD_CONFIG_BASE, 4);
16261af1d13SFabien Thomas if (pmbase == 0) {
16361af1d13SFabien Thomas device_printf(dev,
16461af1d13SFabien Thomas "Watchdog disabled in BIOS or hardware\n");
16561af1d13SFabien Thomas goto fail;
16661af1d13SFabien Thomas }
16761af1d13SFabien Thomas
16861af1d13SFabien Thomas /* Allocate I/O register space. */
169940853ddSFabien Thomas sc->wd_rid = VIAWD_CONFIG_BASE;
170940853ddSFabien Thomas sc->wd_res = bus_alloc_resource_any(sb_dev, SYS_RES_MEMORY, &sc->wd_rid,
17161af1d13SFabien Thomas RF_ACTIVE | RF_SHAREABLE);
17261af1d13SFabien Thomas if (sc->wd_res == NULL) {
17361af1d13SFabien Thomas device_printf(dev, "Unable to map watchdog memory\n");
17461af1d13SFabien Thomas goto fail;
17561af1d13SFabien Thomas }
176940853ddSFabien Thomas if (rman_get_size(sc->wd_res) < VIAWD_MEM_LEN) {
177940853ddSFabien Thomas device_printf(dev, "Bad size for watchdog memory: %#x\n",
178940853ddSFabien Thomas (unsigned)rman_get_size(sc->wd_res));
179940853ddSFabien Thomas goto fail;
180940853ddSFabien Thomas }
18161af1d13SFabien Thomas
18261af1d13SFabien Thomas /* Check if watchdog fired last boot. */
1837e1f37e1SFabien Thomas reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
18461af1d13SFabien Thomas if (reg & VIAWD_MEM_CTRL_FIRED) {
18561af1d13SFabien Thomas device_printf(dev,
18661af1d13SFabien Thomas "ERROR: watchdog rebooted the system\n");
18761af1d13SFabien Thomas /* Reset bit state. */
1887e1f37e1SFabien Thomas viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
18961af1d13SFabien Thomas }
19061af1d13SFabien Thomas
19161af1d13SFabien Thomas /* Register the watchdog event handler. */
19261af1d13SFabien Thomas sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, viawd_event, sc, 0);
19361af1d13SFabien Thomas
19461af1d13SFabien Thomas return (0);
19561af1d13SFabien Thomas fail:
19661af1d13SFabien Thomas if (sc->wd_res != NULL)
197940853ddSFabien Thomas bus_release_resource(sb_dev, SYS_RES_MEMORY,
19861af1d13SFabien Thomas sc->wd_rid, sc->wd_res);
19961af1d13SFabien Thomas return (ENXIO);
20061af1d13SFabien Thomas }
20161af1d13SFabien Thomas
20261af1d13SFabien Thomas static int
viawd_detach(device_t dev)20361af1d13SFabien Thomas viawd_detach(device_t dev)
20461af1d13SFabien Thomas {
20561af1d13SFabien Thomas struct viawd_softc *sc;
20661af1d13SFabien Thomas uint32_t reg;
20761af1d13SFabien Thomas
20861af1d13SFabien Thomas sc = device_get_softc(dev);
20961af1d13SFabien Thomas
21061af1d13SFabien Thomas /* Deregister event handler. */
21161af1d13SFabien Thomas if (sc->ev_tag != NULL)
21261af1d13SFabien Thomas EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
21361af1d13SFabien Thomas sc->ev_tag = NULL;
21461af1d13SFabien Thomas
21561af1d13SFabien Thomas /*
21661af1d13SFabien Thomas * Do not stop the watchdog on shutdown if active but bump the
21761af1d13SFabien Thomas * timer to avoid spurious reset.
21861af1d13SFabien Thomas */
2197e1f37e1SFabien Thomas reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
22061af1d13SFabien Thomas if (reg & VIAWD_MEM_CTRL_ENABLE) {
22161af1d13SFabien Thomas viawd_tmr_set(sc, VIAWD_TIMEOUT_SHUTDOWN);
22261af1d13SFabien Thomas viawd_tmr_state(sc, 1);
22361af1d13SFabien Thomas device_printf(dev,
2241cba6077SGordon Bergling "Keeping watchdog alive during shutdown for %d seconds\n",
22561af1d13SFabien Thomas VIAWD_TIMEOUT_SHUTDOWN);
22661af1d13SFabien Thomas }
22761af1d13SFabien Thomas
22861af1d13SFabien Thomas if (sc->wd_res != NULL)
229940853ddSFabien Thomas bus_release_resource(sc->sb_dev, SYS_RES_MEMORY,
23061af1d13SFabien Thomas sc->wd_rid, sc->wd_res);
23161af1d13SFabien Thomas
23261af1d13SFabien Thomas return (0);
23361af1d13SFabien Thomas }
23461af1d13SFabien Thomas
23561af1d13SFabien Thomas static device_method_t viawd_methods[] = {
23661af1d13SFabien Thomas DEVMETHOD(device_identify, viawd_identify),
23761af1d13SFabien Thomas DEVMETHOD(device_probe, viawd_probe),
23861af1d13SFabien Thomas DEVMETHOD(device_attach, viawd_attach),
23961af1d13SFabien Thomas DEVMETHOD(device_detach, viawd_detach),
24061af1d13SFabien Thomas DEVMETHOD(device_shutdown, viawd_detach),
24161af1d13SFabien Thomas {0,0}
24261af1d13SFabien Thomas };
24361af1d13SFabien Thomas
24461af1d13SFabien Thomas static driver_t viawd_driver = {
24561af1d13SFabien Thomas "viawd",
24661af1d13SFabien Thomas viawd_methods,
24761af1d13SFabien Thomas sizeof(struct viawd_softc),
24861af1d13SFabien Thomas };
24961af1d13SFabien Thomas
25086dc8398SJohn Baldwin DRIVER_MODULE(viawd, isab, viawd_driver, NULL, NULL);
251