1 /*- 2 * Copyright (c) 2012, 2013 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Oleksandr Rybalko under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/time.h> 38 #include <sys/bus.h> 39 #include <sys/resource.h> 40 #include <sys/rman.h> 41 #include <sys/watchdog.h> 42 43 #include <machine/bus.h> 44 #include <machine/intr.h> 45 46 #include <dev/fdt/fdt_common.h> 47 #include <dev/ofw/openfirm.h> 48 #include <dev/ofw/ofw_bus.h> 49 #include <dev/ofw/ofw_bus_subr.h> 50 51 #include <arm/freescale/imx/imx_wdogreg.h> 52 53 struct imx_wdog_softc { 54 struct mtx sc_mtx; 55 device_t sc_dev; 56 bus_space_tag_t sc_bst; 57 bus_space_handle_t sc_bsh; 58 struct resource *sc_res[2]; 59 uint32_t sc_timeout; 60 }; 61 62 static struct resource_spec imx_wdog_spec[] = { 63 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 64 { SYS_RES_IRQ, 0, RF_ACTIVE }, 65 { -1, 0 } 66 }; 67 68 static void imx_watchdog(void *, u_int, int *); 69 static int imx_wdog_probe(device_t); 70 static int imx_wdog_attach(device_t); 71 72 static device_method_t imx_wdog_methods[] = { 73 DEVMETHOD(device_probe, imx_wdog_probe), 74 DEVMETHOD(device_attach, imx_wdog_attach), 75 DEVMETHOD_END 76 }; 77 78 static driver_t imx_wdog_driver = { 79 "imx_wdog", 80 imx_wdog_methods, 81 sizeof(struct imx_wdog_softc), 82 }; 83 static devclass_t imx_wdog_devclass; 84 DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, imx_wdog_devclass, 0, 0); 85 86 87 static void 88 imx_watchdog(void *arg, u_int cmd, int *error) 89 { 90 struct imx_wdog_softc *sc; 91 uint16_t reg; 92 int timeout; 93 94 sc = arg; 95 mtx_lock(&sc->sc_mtx); 96 97 /* Refresh counter, since we feels good */ 98 WRITE(sc, WDOG_SR_REG, WDOG_SR_STEP1); 99 WRITE(sc, WDOG_SR_REG, WDOG_SR_STEP2); 100 101 /* We don't require precession, so "-10" (/1024) is ok */ 102 timeout = (1 << ((cmd & WD_INTERVAL) - 10)) / 1000000; 103 if (timeout > 1 && timeout < 128) { 104 if (timeout != sc->sc_timeout) { 105 device_printf(sc->sc_dev, 106 "WARNING: watchdog can't be disabled!!!"); 107 sc->sc_timeout = timeout; 108 reg = READ(sc, WDOG_CR_REG); 109 reg &= ~WDOG_CR_WT_MASK; 110 reg |= (timeout << (WDOG_CR_WT_SHIFT + 1)) & 111 WDOG_CR_WT_MASK; 112 WRITE(sc, WDOG_CR_REG, reg); 113 /* Refresh counter */ 114 WRITE(sc, WDOG_SR_REG, WDOG_SR_STEP1); 115 WRITE(sc, WDOG_SR_REG, WDOG_SR_STEP2); 116 *error = 0; 117 } else { 118 *error = EOPNOTSUPP; 119 } 120 } else { 121 device_printf(sc->sc_dev, "Can not be disabled.\n"); 122 *error = EOPNOTSUPP; 123 } 124 mtx_unlock(&sc->sc_mtx); 125 126 } 127 128 static int 129 imx_wdog_probe(device_t dev) 130 { 131 132 if (!ofw_bus_status_okay(dev)) 133 return (ENXIO); 134 135 if (!ofw_bus_is_compatible(dev, "fsl,imx51-wdt") && 136 !ofw_bus_is_compatible(dev, "fsl,imx53-wdt")) 137 return (ENXIO); 138 139 device_set_desc(dev, "Freescale i.MX5xx Watchdog Timer"); 140 return (0); 141 } 142 143 static int 144 imx_wdog_attach(device_t dev) 145 { 146 struct imx_wdog_softc *sc; 147 148 sc = device_get_softc(dev); 149 sc->sc_dev = dev; 150 151 if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) { 152 device_printf(dev, "could not allocate resources\n"); 153 return (ENXIO); 154 } 155 156 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF); 157 158 sc->sc_dev = dev; 159 sc->sc_bst = rman_get_bustag(sc->sc_res[0]); 160 sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]); 161 162 /* TODO: handle interrupt */ 163 164 EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0); 165 return (0); 166 } 167