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/param.h> 30 #include <sys/eventhandler.h> 31 #include <sys/kernel.h> 32 #include <sys/module.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <machine/bus.h> 36 #include <sys/rman.h> 37 #include <machine/resource.h> 38 #include <sys/watchdog.h> 39 40 #include <isa/isavar.h> 41 #include <dev/pci/pcivar.h> 42 43 #include "viawd.h" 44 45 #define viawd_read_4(sc, off) bus_read_4((sc)->wd_res, (off)) 46 #define viawd_write_4(sc, off, val) \ 47 bus_write_4((sc)->wd_res, (off), (val)) 48 49 static struct viawd_device viawd_devices[] = { 50 { DEVICEID_VT8251, "VIA VT8251 watchdog timer" }, 51 { DEVICEID_CX700, "VIA CX700 watchdog timer" }, 52 { DEVICEID_VX800, "VIA VX800 watchdog timer" }, 53 { DEVICEID_VX855, "VIA VX855 watchdog timer" }, 54 { DEVICEID_VX900, "VIA VX900 watchdog timer" }, 55 { 0, NULL }, 56 }; 57 58 static void 59 viawd_tmr_state(struct viawd_softc *sc, int enable) 60 { 61 uint32_t reg; 62 63 reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 64 if (enable) 65 reg |= VIAWD_MEM_CTRL_TRIGGER | VIAWD_MEM_CTRL_ENABLE; 66 else 67 reg &= ~VIAWD_MEM_CTRL_ENABLE; 68 viawd_write_4(sc, VIAWD_MEM_CTRL, reg); 69 } 70 71 static void 72 viawd_tmr_set(struct viawd_softc *sc, unsigned int timeout) 73 { 74 75 /* Keep value in range. */ 76 if (timeout < VIAWD_MEM_COUNT_MIN) 77 timeout = VIAWD_MEM_COUNT_MIN; 78 else if (timeout > VIAWD_MEM_COUNT_MAX) 79 timeout = VIAWD_MEM_COUNT_MAX; 80 81 viawd_write_4(sc, VIAWD_MEM_COUNT, timeout); 82 sc->timeout = timeout; 83 } 84 85 /* 86 * Watchdog event handler - called by the framework to enable or disable 87 * the watchdog or change the initial timeout value. 88 */ 89 static void 90 viawd_event(void *arg, unsigned int cmd, int *error) 91 { 92 struct viawd_softc *sc = arg; 93 unsigned int timeout; 94 95 /* Convert from power-of-two-ns to second. */ 96 cmd &= WD_INTERVAL; 97 timeout = ((uint64_t)1 << cmd) / 1000000000; 98 if (cmd) { 99 if (timeout != sc->timeout) 100 viawd_tmr_set(sc, timeout); 101 viawd_tmr_state(sc, 1); 102 *error = 0; 103 } else 104 viawd_tmr_state(sc, 0); 105 } 106 107 /* Look for a supported VIA south bridge. */ 108 static struct viawd_device * 109 viawd_find(device_t dev) 110 { 111 struct viawd_device *id; 112 113 if (pci_get_vendor(dev) != VENDORID_VIA) 114 return (NULL); 115 for (id = viawd_devices; id->desc != NULL; id++) 116 if (pci_get_device(dev) == id->device) 117 return (id); 118 return (NULL); 119 } 120 121 static void 122 viawd_identify(driver_t *driver, device_t parent) 123 { 124 125 if (viawd_find(parent) == NULL) 126 return; 127 128 if (device_find_child(parent, driver->name, -1) == NULL) 129 BUS_ADD_CHILD(parent, 0, driver->name, 0); 130 } 131 132 static int 133 viawd_probe(device_t dev) 134 { 135 struct viawd_device *id; 136 137 id = viawd_find(device_get_parent(dev)); 138 KASSERT(id != NULL, ("parent should be a valid VIA SB")); 139 device_set_desc(dev, id->desc); 140 return (BUS_PROBE_GENERIC); 141 } 142 143 static int 144 viawd_attach(device_t dev) 145 { 146 device_t sb_dev; 147 struct viawd_softc *sc; 148 uint32_t pmbase, reg; 149 150 sc = device_get_softc(dev); 151 sc->dev = dev; 152 153 sb_dev = device_get_parent(dev); 154 if (sb_dev == NULL) { 155 device_printf(dev, "Can not find watchdog device.\n"); 156 goto fail; 157 } 158 sc->sb_dev = sb_dev; 159 160 /* Get watchdog memory base. */ 161 pmbase = pci_read_config(sb_dev, VIAWD_CONFIG_BASE, 4); 162 if (pmbase == 0) { 163 device_printf(dev, 164 "Watchdog disabled in BIOS or hardware\n"); 165 goto fail; 166 } 167 168 /* Allocate I/O register space. */ 169 sc->wd_rid = VIAWD_CONFIG_BASE; 170 sc->wd_res = bus_alloc_resource_any(sb_dev, SYS_RES_MEMORY, &sc->wd_rid, 171 RF_ACTIVE | RF_SHAREABLE); 172 if (sc->wd_res == NULL) { 173 device_printf(dev, "Unable to map watchdog memory\n"); 174 goto fail; 175 } 176 if (rman_get_size(sc->wd_res) < VIAWD_MEM_LEN) { 177 device_printf(dev, "Bad size for watchdog memory: %#x\n", 178 (unsigned)rman_get_size(sc->wd_res)); 179 goto fail; 180 } 181 182 /* Check if watchdog fired last boot. */ 183 reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 184 if (reg & VIAWD_MEM_CTRL_FIRED) { 185 device_printf(dev, 186 "ERROR: watchdog rebooted the system\n"); 187 /* Reset bit state. */ 188 viawd_write_4(sc, VIAWD_MEM_CTRL, reg); 189 } 190 191 /* Register the watchdog event handler. */ 192 sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, viawd_event, sc, 0); 193 194 return (0); 195 fail: 196 if (sc->wd_res != NULL) 197 bus_release_resource(sb_dev, SYS_RES_MEMORY, 198 sc->wd_rid, sc->wd_res); 199 return (ENXIO); 200 } 201 202 static int 203 viawd_detach(device_t dev) 204 { 205 struct viawd_softc *sc; 206 uint32_t reg; 207 208 sc = device_get_softc(dev); 209 210 /* Deregister event handler. */ 211 if (sc->ev_tag != NULL) 212 EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag); 213 sc->ev_tag = NULL; 214 215 /* 216 * Do not stop the watchdog on shutdown if active but bump the 217 * timer to avoid spurious reset. 218 */ 219 reg = viawd_read_4(sc, VIAWD_MEM_CTRL); 220 if (reg & VIAWD_MEM_CTRL_ENABLE) { 221 viawd_tmr_set(sc, VIAWD_TIMEOUT_SHUTDOWN); 222 viawd_tmr_state(sc, 1); 223 device_printf(dev, 224 "Keeping watchdog alive during shutdown for %d seconds\n", 225 VIAWD_TIMEOUT_SHUTDOWN); 226 } 227 228 if (sc->wd_res != NULL) 229 bus_release_resource(sc->sb_dev, SYS_RES_MEMORY, 230 sc->wd_rid, sc->wd_res); 231 232 return (0); 233 } 234 235 static device_method_t viawd_methods[] = { 236 DEVMETHOD(device_identify, viawd_identify), 237 DEVMETHOD(device_probe, viawd_probe), 238 DEVMETHOD(device_attach, viawd_attach), 239 DEVMETHOD(device_detach, viawd_detach), 240 DEVMETHOD(device_shutdown, viawd_detach), 241 {0,0} 242 }; 243 244 static driver_t viawd_driver = { 245 "viawd", 246 viawd_methods, 247 sizeof(struct viawd_softc), 248 }; 249 250 DRIVER_MODULE(viawd, isab, viawd_driver, NULL, NULL); 251