Author: ian
Date: Sat Jul  7 19:10:00 2018
New Revision: 336073
URL: https://svnweb.freebsd.org/changeset/base/336073

Log:
  Add support to the imx watchdog for the FDT "timeout-sec" property, by
  automatically initializing the watchdog using the given value.  Also,
  attach at BUS_PASS_TIMER to extend watchdog protection to more of the
  kernel init process.

Modified:
  head/sys/arm/freescale/imx/imx_wdog.c

Modified: head/sys/arm/freescale/imx/imx_wdog.c
==============================================================================
--- head/sys/arm/freescale/imx/imx_wdog.c       Sat Jul  7 19:03:38 2018        
(r336072)
+++ head/sys/arm/freescale/imx/imx_wdog.c       Sat Jul  7 19:10:00 2018        
(r336073)
@@ -98,42 +98,52 @@ WR2(struct imx_wdog_softc *sc, bus_size_t offs, uint16
        bus_write_2(sc->sc_res[MEMRES], offs, val);
 }
 
+static int
+imx_wdog_enable(struct imx_wdog_softc *sc, u_int timeout)
+{
+       uint16_t reg;
+
+       if (timeout < 1 || timeout > 128)
+               return (EINVAL);
+
+       mtx_lock(&sc->sc_mtx);
+       if (timeout != sc->sc_timeout) {
+               sc->sc_timeout = timeout;
+               reg = RD2(sc, WDOG_CR_REG);
+               reg &= ~WDOG_CR_WT_MASK;
+               reg |= ((2 * timeout - 1) << WDOG_CR_WT_SHIFT);
+               WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE);
+       }
+       /* Refresh counter */
+       WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1);
+       WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2);
+       /* Watchdog active, can disable rom-boot watchdog. */
+       if (sc->sc_pde_enabled) {
+               sc->sc_pde_enabled = false;
+               reg = RD2(sc, WDOG_MCR_REG);
+               WR2(sc, WDOG_MCR_REG, reg & ~WDOG_MCR_PDE);
+       }
+       mtx_unlock(&sc->sc_mtx);
+
+       return (0);
+}
+
 static void
 imx_watchdog(void *arg, u_int cmd, int *error)
 {
        struct imx_wdog_softc *sc;
-       uint16_t reg;
        u_int timeout;
 
        sc = arg;
-       mtx_lock(&sc->sc_mtx);
        if (cmd == 0) {
                if (bootverbose)
                        device_printf(sc->sc_dev, "Can not be disabled.\n");
                *error = EOPNOTSUPP;
        } else {
                timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U);
-               if (timeout > 1 && timeout < 128) {
-                       if (timeout != sc->sc_timeout) {
-                               sc->sc_timeout = timeout;
-                               reg = RD2(sc, WDOG_CR_REG);
-                               reg &= ~WDOG_CR_WT_MASK;
-                               reg |= ((2 * timeout - 1) << WDOG_CR_WT_SHIFT);
-                               WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE);
-                       }
-                       /* Refresh counter */
-                       WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1);
-                       WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2);
-                       /* Watchdog active, can disable rom-boot watchdog. */
-                       if (sc->sc_pde_enabled) {
-                               sc->sc_pde_enabled = false;
-                               reg = RD2(sc, WDOG_MCR_REG);
-                               WR2(sc, WDOG_MCR_REG, reg & ~WDOG_MCR_PDE);
-                       }
+               if (imx_wdog_enable(sc, timeout) == 0)
                        *error = 0;
-               }
        }
-       mtx_unlock(&sc->sc_mtx);
 }
 
 static int
@@ -175,6 +185,7 @@ static int
 imx_wdog_attach(device_t dev)
 {
        struct imx_wdog_softc *sc;
+       pcell_t timeout;
 
        sc = device_get_softc(dev);
        sc->sc_dev = dev;
@@ -209,6 +220,19 @@ imx_wdog_attach(device_t dev)
 
        EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0);
 
+       /* If there is a timeout-sec property, activate the watchdog. */
+       if (OF_getencprop(ofw_bus_get_node(sc->sc_dev), "timeout-sec",
+           &timeout, sizeof(timeout)) == sizeof(timeout)) {
+               if (timeout < 1 || timeout > 128) {
+                       device_printf(sc->sc_dev, "ERROR: bad timeout-sec "
+                           "property value %u, using 128\n", timeout);
+                       timeout = 128;
+               }
+               imx_wdog_enable(sc, timeout);
+               device_printf(sc->sc_dev, "watchdog enabled using "
+                   "timeout-sec property value %u\n", timeout);
+       }
+
        /*
         * The watchdog hardware cannot be disabled, so there's little point in
         * coding up a detach() routine to carefully tear everything down, just
@@ -232,5 +256,6 @@ static driver_t imx_wdog_driver = {
 
 static devclass_t imx_wdog_devclass;
 
-DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, imx_wdog_devclass, 0, 0);
+EARLY_DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver,
+    imx_wdog_devclass, 0, 0, BUS_PASS_TIMER);
 SIMPLEBUS_PNP_INFO(compat_data);
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to