161af1d13SFabien Thomas /*- 2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*718cf2ccSPedro 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> 3361af1d13SFabien Thomas #include <sys/kernel.h> 3461af1d13SFabien Thomas #include <sys/module.h> 3561af1d13SFabien Thomas #include <sys/systm.h> 3661af1d13SFabien Thomas #include <sys/bus.h> 3761af1d13SFabien Thomas #include <machine/bus.h> 3861af1d13SFabien Thomas #include <sys/rman.h> 3961af1d13SFabien Thomas #include <machine/resource.h> 4061af1d13SFabien Thomas #include <sys/watchdog.h> 4161af1d13SFabien Thomas 4261af1d13SFabien Thomas #include <isa/isavar.h> 4361af1d13SFabien Thomas #include <dev/pci/pcivar.h> 4461af1d13SFabien Thomas 4561af1d13SFabien Thomas #include "viawd.h" 4661af1d13SFabien Thomas 477e1f37e1SFabien Thomas #define viawd_read_4(sc, off) bus_read_4((sc)->wd_res, (off)) 487e1f37e1SFabien Thomas #define viawd_write_4(sc, off, val) \ 497e1f37e1SFabien Thomas bus_write_4((sc)->wd_res, (off), (val)) 5061af1d13SFabien Thomas 5161af1d13SFabien Thomas static struct viawd_device viawd_devices[] = { 5261af1d13SFabien Thomas { DEVICEID_VT8251, "VIA VT8251 watchdog timer" }, 5361af1d13SFabien Thomas { DEVICEID_CX700, "VIA CX700 watchdog timer" }, 5461af1d13SFabien Thomas { DEVICEID_VX800, "VIA VX800 watchdog timer" }, 5561af1d13SFabien Thomas { DEVICEID_VX855, "VIA VX855 watchdog timer" }, 5661af1d13SFabien Thomas { DEVICEID_VX900, "VIA VX900 watchdog timer" }, 5761af1d13SFabien Thomas { 0, NULL }, 5861af1d13SFabien Thomas }; 5961af1d13SFabien Thomas 6061af1d13SFabien Thomas static devclass_t viawd_devclass; 6161af1d13SFabien Thomas 6261af1d13SFabien Thomas static void 6361af1d13SFabien Thomas viawd_tmr_state(struct viawd_softc *sc, int enable) 6461af1d13SFabien Thomas { 6561af1d13SFabien Thomas uint32_t reg; 6661af1d13SFabien Thomas 677e1f37e1SFabien Thomas reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 6861af1d13SFabien Thomas if (enable) 6961af1d13SFabien Thomas reg |= VIAWD_MEM_CTRL_TRIGGER | VIAWD_MEM_CTRL_ENABLE; 7061af1d13SFabien Thomas else 7161af1d13SFabien Thomas reg &= ~VIAWD_MEM_CTRL_ENABLE; 727e1f37e1SFabien Thomas viawd_write_4(sc, VIAWD_MEM_CTRL, reg); 7361af1d13SFabien Thomas } 7461af1d13SFabien Thomas 7561af1d13SFabien Thomas static void 7661af1d13SFabien Thomas viawd_tmr_set(struct viawd_softc *sc, unsigned int timeout) 7761af1d13SFabien Thomas { 7861af1d13SFabien Thomas 7961af1d13SFabien Thomas /* Keep value in range. */ 8061af1d13SFabien Thomas if (timeout < VIAWD_MEM_COUNT_MIN) 8161af1d13SFabien Thomas timeout = VIAWD_MEM_COUNT_MIN; 8261af1d13SFabien Thomas else if (timeout > VIAWD_MEM_COUNT_MAX) 8361af1d13SFabien Thomas timeout = VIAWD_MEM_COUNT_MAX; 8461af1d13SFabien Thomas 857e1f37e1SFabien Thomas viawd_write_4(sc, VIAWD_MEM_COUNT, timeout); 8661af1d13SFabien Thomas sc->timeout = timeout; 8761af1d13SFabien Thomas } 8861af1d13SFabien Thomas 8961af1d13SFabien Thomas /* 9061af1d13SFabien Thomas * Watchdog event handler - called by the framework to enable or disable 9161af1d13SFabien Thomas * the watchdog or change the initial timeout value. 9261af1d13SFabien Thomas */ 9361af1d13SFabien Thomas static void 9461af1d13SFabien Thomas viawd_event(void *arg, unsigned int cmd, int *error) 9561af1d13SFabien Thomas { 9661af1d13SFabien Thomas struct viawd_softc *sc = arg; 9761af1d13SFabien Thomas unsigned int timeout; 9861af1d13SFabien Thomas 9961af1d13SFabien Thomas /* Convert from power-of-two-ns to second. */ 10061af1d13SFabien Thomas cmd &= WD_INTERVAL; 10161af1d13SFabien Thomas timeout = ((uint64_t)1 << cmd) / 1000000000; 10261af1d13SFabien Thomas if (cmd) { 10361af1d13SFabien Thomas if (timeout != sc->timeout) 10461af1d13SFabien Thomas viawd_tmr_set(sc, timeout); 10561af1d13SFabien Thomas viawd_tmr_state(sc, 1); 10661af1d13SFabien Thomas *error = 0; 10761af1d13SFabien Thomas } else 10861af1d13SFabien Thomas viawd_tmr_state(sc, 0); 10961af1d13SFabien Thomas } 11061af1d13SFabien Thomas 1117e1f37e1SFabien Thomas /* Look for a supported VIA south bridge. */ 1127e1f37e1SFabien Thomas static struct viawd_device * 1137e1f37e1SFabien Thomas viawd_find(device_t dev) 1147e1f37e1SFabien Thomas { 1157e1f37e1SFabien Thomas struct viawd_device *id; 1167e1f37e1SFabien Thomas 1177e1f37e1SFabien Thomas if (pci_get_vendor(dev) != VENDORID_VIA) 1187e1f37e1SFabien Thomas return (NULL); 1197e1f37e1SFabien Thomas for (id = viawd_devices; id->desc != NULL; id++) 1207e1f37e1SFabien Thomas if (pci_get_device(dev) == id->device) 1217e1f37e1SFabien Thomas return (id); 1227e1f37e1SFabien Thomas return (NULL); 1237e1f37e1SFabien Thomas } 1247e1f37e1SFabien Thomas 12561af1d13SFabien Thomas static void 12661af1d13SFabien Thomas viawd_identify(driver_t *driver, device_t parent) 12761af1d13SFabien Thomas { 12861af1d13SFabien Thomas 1297e1f37e1SFabien Thomas if (viawd_find(parent) == NULL) 13061af1d13SFabien Thomas return; 13161af1d13SFabien Thomas 1327e1f37e1SFabien Thomas if (device_find_child(parent, driver->name, -1) == NULL) 1337e1f37e1SFabien Thomas BUS_ADD_CHILD(parent, 0, driver->name, 0); 13461af1d13SFabien Thomas } 13561af1d13SFabien Thomas 13661af1d13SFabien Thomas static int 13761af1d13SFabien Thomas viawd_probe(device_t dev) 13861af1d13SFabien Thomas { 1397e1f37e1SFabien Thomas struct viawd_device *id; 14061af1d13SFabien Thomas 1417e1f37e1SFabien Thomas id = viawd_find(device_get_parent(dev)); 1427e1f37e1SFabien Thomas KASSERT(id != NULL, ("parent should be a valid VIA SB")); 1437e1f37e1SFabien Thomas device_set_desc(dev, id->desc); 1447e1f37e1SFabien Thomas return (BUS_PROBE_GENERIC); 14561af1d13SFabien Thomas } 14661af1d13SFabien Thomas 14761af1d13SFabien Thomas static int 14861af1d13SFabien Thomas viawd_attach(device_t dev) 14961af1d13SFabien Thomas { 15061af1d13SFabien Thomas device_t sb_dev; 15161af1d13SFabien Thomas struct viawd_softc *sc; 15261af1d13SFabien Thomas uint32_t pmbase, reg; 15361af1d13SFabien Thomas 15461af1d13SFabien Thomas sc = device_get_softc(dev); 15561af1d13SFabien Thomas sc->dev = dev; 15661af1d13SFabien Thomas 1577e1f37e1SFabien Thomas sb_dev = device_get_parent(dev); 15861af1d13SFabien Thomas if (sb_dev == NULL) { 15961af1d13SFabien Thomas device_printf(dev, "Can not find watchdog device.\n"); 16061af1d13SFabien Thomas goto fail; 16161af1d13SFabien Thomas } 16261af1d13SFabien Thomas sc->sb_dev = sb_dev; 16361af1d13SFabien Thomas 16461af1d13SFabien Thomas /* Get watchdog memory base. */ 16561af1d13SFabien Thomas pmbase = pci_read_config(sb_dev, VIAWD_CONFIG_BASE, 4); 16661af1d13SFabien Thomas if (pmbase == 0) { 16761af1d13SFabien Thomas device_printf(dev, 16861af1d13SFabien Thomas "Watchdog disabled in BIOS or hardware\n"); 16961af1d13SFabien Thomas goto fail; 17061af1d13SFabien Thomas } 17161af1d13SFabien Thomas 17261af1d13SFabien Thomas /* Allocate I/O register space. */ 173940853ddSFabien Thomas sc->wd_rid = VIAWD_CONFIG_BASE; 174940853ddSFabien Thomas sc->wd_res = bus_alloc_resource_any(sb_dev, SYS_RES_MEMORY, &sc->wd_rid, 17561af1d13SFabien Thomas RF_ACTIVE | RF_SHAREABLE); 17661af1d13SFabien Thomas if (sc->wd_res == NULL) { 17761af1d13SFabien Thomas device_printf(dev, "Unable to map watchdog memory\n"); 17861af1d13SFabien Thomas goto fail; 17961af1d13SFabien Thomas } 180940853ddSFabien Thomas if (rman_get_size(sc->wd_res) < VIAWD_MEM_LEN) { 181940853ddSFabien Thomas device_printf(dev, "Bad size for watchdog memory: %#x\n", 182940853ddSFabien Thomas (unsigned)rman_get_size(sc->wd_res)); 183940853ddSFabien Thomas goto fail; 184940853ddSFabien Thomas } 18561af1d13SFabien Thomas 18661af1d13SFabien Thomas /* Check if watchdog fired last boot. */ 1877e1f37e1SFabien Thomas reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 18861af1d13SFabien Thomas if (reg & VIAWD_MEM_CTRL_FIRED) { 18961af1d13SFabien Thomas device_printf(dev, 19061af1d13SFabien Thomas "ERROR: watchdog rebooted the system\n"); 19161af1d13SFabien Thomas /* Reset bit state. */ 1927e1f37e1SFabien Thomas viawd_write_4(sc, VIAWD_MEM_CTRL, reg); 19361af1d13SFabien Thomas } 19461af1d13SFabien Thomas 19561af1d13SFabien Thomas /* Register the watchdog event handler. */ 19661af1d13SFabien Thomas sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, viawd_event, sc, 0); 19761af1d13SFabien Thomas 19861af1d13SFabien Thomas return (0); 19961af1d13SFabien Thomas fail: 20061af1d13SFabien Thomas if (sc->wd_res != NULL) 201940853ddSFabien Thomas bus_release_resource(sb_dev, SYS_RES_MEMORY, 20261af1d13SFabien Thomas sc->wd_rid, sc->wd_res); 20361af1d13SFabien Thomas return (ENXIO); 20461af1d13SFabien Thomas } 20561af1d13SFabien Thomas 20661af1d13SFabien Thomas static int 20761af1d13SFabien Thomas viawd_detach(device_t dev) 20861af1d13SFabien Thomas { 20961af1d13SFabien Thomas struct viawd_softc *sc; 21061af1d13SFabien Thomas uint32_t reg; 21161af1d13SFabien Thomas 21261af1d13SFabien Thomas sc = device_get_softc(dev); 21361af1d13SFabien Thomas 21461af1d13SFabien Thomas /* Deregister event handler. */ 21561af1d13SFabien Thomas if (sc->ev_tag != NULL) 21661af1d13SFabien Thomas EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag); 21761af1d13SFabien Thomas sc->ev_tag = NULL; 21861af1d13SFabien Thomas 21961af1d13SFabien Thomas /* 22061af1d13SFabien Thomas * Do not stop the watchdog on shutdown if active but bump the 22161af1d13SFabien Thomas * timer to avoid spurious reset. 22261af1d13SFabien Thomas */ 2237e1f37e1SFabien Thomas reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 22461af1d13SFabien Thomas if (reg & VIAWD_MEM_CTRL_ENABLE) { 22561af1d13SFabien Thomas viawd_tmr_set(sc, VIAWD_TIMEOUT_SHUTDOWN); 22661af1d13SFabien Thomas viawd_tmr_state(sc, 1); 22761af1d13SFabien Thomas device_printf(dev, 22861af1d13SFabien Thomas "Keeping watchog alive during shutdown for %d seconds\n", 22961af1d13SFabien Thomas VIAWD_TIMEOUT_SHUTDOWN); 23061af1d13SFabien Thomas } 23161af1d13SFabien Thomas 23261af1d13SFabien Thomas if (sc->wd_res != NULL) 233940853ddSFabien Thomas bus_release_resource(sc->sb_dev, SYS_RES_MEMORY, 23461af1d13SFabien Thomas sc->wd_rid, sc->wd_res); 23561af1d13SFabien Thomas 23661af1d13SFabien Thomas return (0); 23761af1d13SFabien Thomas } 23861af1d13SFabien Thomas 23961af1d13SFabien Thomas static device_method_t viawd_methods[] = { 24061af1d13SFabien Thomas DEVMETHOD(device_identify, viawd_identify), 24161af1d13SFabien Thomas DEVMETHOD(device_probe, viawd_probe), 24261af1d13SFabien Thomas DEVMETHOD(device_attach, viawd_attach), 24361af1d13SFabien Thomas DEVMETHOD(device_detach, viawd_detach), 24461af1d13SFabien Thomas DEVMETHOD(device_shutdown, viawd_detach), 24561af1d13SFabien Thomas {0,0} 24661af1d13SFabien Thomas }; 24761af1d13SFabien Thomas 24861af1d13SFabien Thomas static driver_t viawd_driver = { 24961af1d13SFabien Thomas "viawd", 25061af1d13SFabien Thomas viawd_methods, 25161af1d13SFabien Thomas sizeof(struct viawd_softc), 25261af1d13SFabien Thomas }; 25361af1d13SFabien Thomas 2547e1f37e1SFabien Thomas DRIVER_MODULE(viawd, isab, viawd_driver, viawd_devclass, NULL, NULL); 255