The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/viawd/viawd.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: e1cef85ee005a593f786b1920968bcc2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.