1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 Fabien Thomas <fabient@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/eventhandler.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <machine/bus.h> 37 #include <sys/rman.h> 38 #include <machine/resource.h> 39 #include <sys/watchdog.h> 40 41 #include <isa/isavar.h> 42 #include <dev/pci/pcivar.h> 43 44 #include "viawd.h" 45 46 #define viawd_read_4(sc, off) bus_read_4((sc)->wd_res, (off)) 47 #define viawd_write_4(sc, off, val) \ 48 bus_write_4((sc)->wd_res, (off), (val)) 49 50 static struct viawd_device viawd_devices[] = { 51 { DEVICEID_VT8251, "VIA VT8251 watchdog timer" }, 52 { DEVICEID_CX700, "VIA CX700 watchdog timer" }, 53 { DEVICEID_VX800, "VIA VX800 watchdog timer" }, 54 { DEVICEID_VX855, "VIA VX855 watchdog timer" }, 55 { DEVICEID_VX900, "VIA VX900 watchdog timer" }, 56 { 0, NULL }, 57 }; 58 59 static void 60 viawd_tmr_state(struct viawd_softc *sc, int enable) 61 { 62 uint32_t reg; 63 64 reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 65 if (enable) 66 reg |= VIAWD_MEM_CTRL_TRIGGER | VIAWD_MEM_CTRL_ENABLE; 67 else 68 reg &= ~VIAWD_MEM_CTRL_ENABLE; 69 viawd_write_4(sc, VIAWD_MEM_CTRL, reg); 70 } 71 72 static void 73 viawd_tmr_set(struct viawd_softc *sc, unsigned int timeout) 74 { 75 76 /* Keep value in range. */ 77 if (timeout < VIAWD_MEM_COUNT_MIN) 78 timeout = VIAWD_MEM_COUNT_MIN; 79 else if (timeout > VIAWD_MEM_COUNT_MAX) 80 timeout = VIAWD_MEM_COUNT_MAX; 81 82 viawd_write_4(sc, VIAWD_MEM_COUNT, timeout); 83 sc->timeout = timeout; 84 } 85 86 /* 87 * Watchdog event handler - called by the framework to enable or disable 88 * the watchdog or change the initial timeout value. 89 */ 90 static void 91 viawd_event(void *arg, unsigned int cmd, int *error) 92 { 93 struct viawd_softc *sc = arg; 94 unsigned int timeout; 95 96 /* Convert from power-of-two-ns to second. */ 97 cmd &= WD_INTERVAL; 98 timeout = ((uint64_t)1 << cmd) / 1000000000; 99 if (cmd) { 100 if (timeout != sc->timeout) 101 viawd_tmr_set(sc, timeout); 102 viawd_tmr_state(sc, 1); 103 *error = 0; 104 } else 105 viawd_tmr_state(sc, 0); 106 } 107 108 /* Look for a supported VIA south bridge. */ 109 static struct viawd_device * 110 viawd_find(device_t dev) 111 { 112 struct viawd_device *id; 113 114 if (pci_get_vendor(dev) != VENDORID_VIA) 115 return (NULL); 116 for (id = viawd_devices; id->desc != NULL; id++) 117 if (pci_get_device(dev) == id->device) 118 return (id); 119 return (NULL); 120 } 121 122 static void 123 viawd_identify(driver_t *driver, device_t parent) 124 { 125 126 if (viawd_find(parent) == NULL) 127 return; 128 129 if (device_find_child(parent, driver->name, -1) == NULL) 130 BUS_ADD_CHILD(parent, 0, driver->name, 0); 131 } 132 133 static int 134 viawd_probe(device_t dev) 135 { 136 struct viawd_device *id; 137 138 id = viawd_find(device_get_parent(dev)); 139 KASSERT(id != NULL, ("parent should be a valid VIA SB")); 140 device_set_desc(dev, id->desc); 141 return (BUS_PROBE_GENERIC); 142 } 143 144 static int 145 viawd_attach(device_t dev) 146 { 147 device_t sb_dev; 148 struct viawd_softc *sc; 149 uint32_t pmbase, reg; 150 151 sc = device_get_softc(dev); 152 sc->dev = dev; 153 154 sb_dev = device_get_parent(dev); 155 if (sb_dev == NULL) { 156 device_printf(dev, "Can not find watchdog device.\n"); 157 goto fail; 158 } 159 sc->sb_dev = sb_dev; 160 161 /* Get watchdog memory base. */ 162 pmbase = pci_read_config(sb_dev, VIAWD_CONFIG_BASE, 4); 163 if (pmbase == 0) { 164 device_printf(dev, 165 "Watchdog disabled in BIOS or hardware\n"); 166 goto fail; 167 } 168 169 /* Allocate I/O register space. */ 170 sc->wd_rid = VIAWD_CONFIG_BASE; 171 sc->wd_res = bus_alloc_resource_any(sb_dev, SYS_RES_MEMORY, &sc->wd_rid, 172 RF_ACTIVE | RF_SHAREABLE); 173 if (sc->wd_res == NULL) { 174 device_printf(dev, "Unable to map watchdog memory\n"); 175 goto fail; 176 } 177 if (rman_get_size(sc->wd_res) < VIAWD_MEM_LEN) { 178 device_printf(dev, "Bad size for watchdog memory: %#x\n", 179 (unsigned)rman_get_size(sc->wd_res)); 180 goto fail; 181 } 182 183 /* Check if watchdog fired last boot. */ 184 reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 185 if (reg & VIAWD_MEM_CTRL_FIRED) { 186 device_printf(dev, 187 "ERROR: watchdog rebooted the system\n"); 188 /* Reset bit state. */ 189 viawd_write_4(sc, VIAWD_MEM_CTRL, reg); 190 } 191 192 /* Register the watchdog event handler. */ 193 sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, viawd_event, sc, 0); 194 195 return (0); 196 fail: 197 if (sc->wd_res != NULL) 198 bus_release_resource(sb_dev, SYS_RES_MEMORY, 199 sc->wd_rid, sc->wd_res); 200 return (ENXIO); 201 } 202 203 static int 204 viawd_detach(device_t dev) 205 { 206 struct viawd_softc *sc; 207 uint32_t reg; 208 209 sc = device_get_softc(dev); 210 211 /* Deregister event handler. */ 212 if (sc->ev_tag != NULL) 213 EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag); 214 sc->ev_tag = NULL; 215 216 /* 217 * Do not stop the watchdog on shutdown if active but bump the 218 * timer to avoid spurious reset. 219 */ 220 reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 221 if (reg & VIAWD_MEM_CTRL_ENABLE) { 222 viawd_tmr_set(sc, VIAWD_TIMEOUT_SHUTDOWN); 223 viawd_tmr_state(sc, 1); 224 device_printf(dev, 225 "Keeping watchdog alive during shutdown for %d seconds\n", 226 VIAWD_TIMEOUT_SHUTDOWN); 227 } 228 229 if (sc->wd_res != NULL) 230 bus_release_resource(sc->sb_dev, SYS_RES_MEMORY, 231 sc->wd_rid, sc->wd_res); 232 233 return (0); 234 } 235 236 static device_method_t viawd_methods[] = { 237 DEVMETHOD(device_identify, viawd_identify), 238 DEVMETHOD(device_probe, viawd_probe), 239 DEVMETHOD(device_attach, viawd_attach), 240 DEVMETHOD(device_detach, viawd_detach), 241 DEVMETHOD(device_shutdown, viawd_detach), 242 {0,0} 243 }; 244 245 static driver_t viawd_driver = { 246 "viawd", 247 viawd_methods, 248 sizeof(struct viawd_softc), 249 }; 250 251 DRIVER_MODULE(viawd, isab, viawd_driver, NULL, NULL); 252