1a2c472e7SAleksandr Rybalko /*- 2af3dc4a7SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3af3dc4a7SPedro F. Giffuni * 494f8d6fdSAleksandr Rybalko * Copyright (c) 2012, 2013 The FreeBSD Foundation 5a2c472e7SAleksandr Rybalko * All rights reserved. 6a2c472e7SAleksandr Rybalko * 7a2c472e7SAleksandr Rybalko * This software was developed by Oleksandr Rybalko under sponsorship 8a2c472e7SAleksandr Rybalko * from the FreeBSD Foundation. 9a2c472e7SAleksandr Rybalko * 10a2c472e7SAleksandr Rybalko * Redistribution and use in source and binary forms, with or without 11a2c472e7SAleksandr Rybalko * modification, are permitted provided that the following conditions 12a2c472e7SAleksandr Rybalko * are met: 13a2c472e7SAleksandr Rybalko * 1. Redistributions of source code must retain the above copyright 14a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer. 15a2c472e7SAleksandr Rybalko * 2. Redistributions in binary form must reproduce the above copyright 16a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer in the 17a2c472e7SAleksandr Rybalko * documentation and/or other materials provided with the distribution. 18a2c472e7SAleksandr Rybalko * 19a2c472e7SAleksandr Rybalko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20a2c472e7SAleksandr Rybalko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21a2c472e7SAleksandr Rybalko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22a2c472e7SAleksandr Rybalko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23a2c472e7SAleksandr Rybalko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24a2c472e7SAleksandr Rybalko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25a2c472e7SAleksandr Rybalko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26a2c472e7SAleksandr Rybalko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27a2c472e7SAleksandr Rybalko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28a2c472e7SAleksandr Rybalko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29a2c472e7SAleksandr Rybalko * SUCH DAMAGE. 30a2c472e7SAleksandr Rybalko */ 31a2c472e7SAleksandr Rybalko 32a2c472e7SAleksandr Rybalko #include <sys/cdefs.h> 33a2c472e7SAleksandr Rybalko __FBSDID("$FreeBSD$"); 34a2c472e7SAleksandr Rybalko 35a2c472e7SAleksandr Rybalko #include <sys/param.h> 36a2c472e7SAleksandr Rybalko #include <sys/systm.h> 37a2c472e7SAleksandr Rybalko #include <sys/kernel.h> 38a2c472e7SAleksandr Rybalko #include <sys/module.h> 39a2c472e7SAleksandr Rybalko #include <sys/time.h> 40a2c472e7SAleksandr Rybalko #include <sys/bus.h> 41a2c472e7SAleksandr Rybalko #include <sys/resource.h> 42a2c472e7SAleksandr Rybalko #include <sys/rman.h> 43a2c472e7SAleksandr Rybalko #include <sys/watchdog.h> 44a2c472e7SAleksandr Rybalko 45a2c472e7SAleksandr Rybalko #include <machine/bus.h> 46a2c472e7SAleksandr Rybalko #include <machine/intr.h> 47a2c472e7SAleksandr Rybalko 48a2c472e7SAleksandr Rybalko #include <dev/ofw/openfirm.h> 49a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus.h> 50a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus_subr.h> 51a2c472e7SAleksandr Rybalko 524dbbaf20SIan Lepore #include <arm/freescale/imx/imx_machdep.h> 53a2c472e7SAleksandr Rybalko #include <arm/freescale/imx/imx_wdogreg.h> 54a2c472e7SAleksandr Rybalko 55a2c472e7SAleksandr Rybalko struct imx_wdog_softc { 56a2c472e7SAleksandr Rybalko struct mtx sc_mtx; 57a2c472e7SAleksandr Rybalko device_t sc_dev; 58a2c472e7SAleksandr Rybalko struct resource *sc_res[2]; 594dbbaf20SIan Lepore void *sc_ih; 60a2c472e7SAleksandr Rybalko uint32_t sc_timeout; 614dbbaf20SIan Lepore bool sc_pde_enabled; 62a2c472e7SAleksandr Rybalko }; 63a2c472e7SAleksandr Rybalko 64a2c472e7SAleksandr Rybalko static struct resource_spec imx_wdog_spec[] = { 65a2c472e7SAleksandr Rybalko { SYS_RES_MEMORY, 0, RF_ACTIVE }, 66a2c472e7SAleksandr Rybalko { SYS_RES_IRQ, 0, RF_ACTIVE }, 6776ecefceSIan Lepore RESOURCE_SPEC_END 68a2c472e7SAleksandr Rybalko }; 69a2c472e7SAleksandr Rybalko 7076ecefceSIan Lepore #define MEMRES 0 7176ecefceSIan Lepore #define IRQRES 1 7276ecefceSIan Lepore 73398c1838SIan Lepore static struct ofw_compat_data compat_data[] = { 74398c1838SIan Lepore {"fsl,imx6sx-wdt", 1}, 75398c1838SIan Lepore {"fsl,imx6sl-wdt", 1}, 76398c1838SIan Lepore {"fsl,imx6q-wdt", 1}, 77398c1838SIan Lepore {"fsl,imx53-wdt", 1}, 78398c1838SIan Lepore {"fsl,imx51-wdt", 1}, 79398c1838SIan Lepore {"fsl,imx50-wdt", 1}, 80398c1838SIan Lepore {"fsl,imx35-wdt", 1}, 81398c1838SIan Lepore {"fsl,imx27-wdt", 1}, 82398c1838SIan Lepore {"fsl,imx25-wdt", 1}, 83398c1838SIan Lepore {"fsl,imx21-wdt", 1}, 84398c1838SIan Lepore {NULL, 0} 85398c1838SIan Lepore }; 86398c1838SIan Lepore 8776ecefceSIan Lepore static inline uint16_t 8876ecefceSIan Lepore RD2(struct imx_wdog_softc *sc, bus_size_t offs) 8976ecefceSIan Lepore { 90a2c472e7SAleksandr Rybalko 915b810fe4SIan Lepore return (bus_read_2(sc->sc_res[MEMRES], offs)); 9276ecefceSIan Lepore } 93a2c472e7SAleksandr Rybalko 9476ecefceSIan Lepore static inline void 9576ecefceSIan Lepore WR2(struct imx_wdog_softc *sc, bus_size_t offs, uint16_t val) 9676ecefceSIan Lepore { 97a2c472e7SAleksandr Rybalko 985b810fe4SIan Lepore bus_write_2(sc->sc_res[MEMRES], offs, val); 9976ecefceSIan Lepore } 100a2c472e7SAleksandr Rybalko 101a2c472e7SAleksandr Rybalko static void 102a2c472e7SAleksandr Rybalko imx_watchdog(void *arg, u_int cmd, int *error) 103a2c472e7SAleksandr Rybalko { 104a2c472e7SAleksandr Rybalko struct imx_wdog_softc *sc; 105a2c472e7SAleksandr Rybalko uint16_t reg; 1066da71028SIan Lepore u_int timeout; 107a2c472e7SAleksandr Rybalko 108a2c472e7SAleksandr Rybalko sc = arg; 109a2c472e7SAleksandr Rybalko mtx_lock(&sc->sc_mtx); 1106da71028SIan Lepore if (cmd == 0) { 1116da71028SIan Lepore if (bootverbose) 1126da71028SIan Lepore device_printf(sc->sc_dev, "Can not be disabled.\n"); 1136da71028SIan Lepore *error = EOPNOTSUPP; 1146da71028SIan Lepore } else { 1156da71028SIan Lepore timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U); 116a2c472e7SAleksandr Rybalko if (timeout > 1 && timeout < 128) { 117a2c472e7SAleksandr Rybalko if (timeout != sc->sc_timeout) { 118a2c472e7SAleksandr Rybalko sc->sc_timeout = timeout; 119398c1838SIan Lepore reg = RD2(sc, WDOG_CR_REG); 120a2c472e7SAleksandr Rybalko reg &= ~WDOG_CR_WT_MASK; 121*a4db01f7SIan Lepore reg |= ((2 * timeout - 1) << WDOG_CR_WT_SHIFT); 1226da71028SIan Lepore WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE); 1236da71028SIan Lepore } 124a2c472e7SAleksandr Rybalko /* Refresh counter */ 125398c1838SIan Lepore WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1); 126398c1838SIan Lepore WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2); 1274dbbaf20SIan Lepore /* Watchdog active, can disable rom-boot watchdog. */ 1284dbbaf20SIan Lepore if (sc->sc_pde_enabled) { 1294dbbaf20SIan Lepore sc->sc_pde_enabled = false; 1304dbbaf20SIan Lepore reg = RD2(sc, WDOG_MCR_REG); 1314dbbaf20SIan Lepore WR2(sc, WDOG_MCR_REG, reg & ~WDOG_MCR_PDE); 1324dbbaf20SIan Lepore } 133a2c472e7SAleksandr Rybalko *error = 0; 134a2c472e7SAleksandr Rybalko } 135a2c472e7SAleksandr Rybalko } 136a2c472e7SAleksandr Rybalko mtx_unlock(&sc->sc_mtx); 137a2c472e7SAleksandr Rybalko } 138a2c472e7SAleksandr Rybalko 139a2c472e7SAleksandr Rybalko static int 1404dbbaf20SIan Lepore imx_wdog_intr(void *arg) 1414dbbaf20SIan Lepore { 1424dbbaf20SIan Lepore struct imx_wdog_softc *sc = arg; 1434dbbaf20SIan Lepore 1444dbbaf20SIan Lepore /* 1454dbbaf20SIan Lepore * When configured for external reset, the actual reset is supposed to 1464dbbaf20SIan Lepore * happen when some external device responds to the assertion of the 1474dbbaf20SIan Lepore * WDOG_B signal by asserting the POR signal to the chip. This 1484dbbaf20SIan Lepore * interrupt handler is a backstop mechanism; it is set up to fire 1494dbbaf20SIan Lepore * simultaneously with WDOG_B, and if the external reset happens we'll 1504dbbaf20SIan Lepore * never actually make it to here. If we do make it here, just trigger 1514dbbaf20SIan Lepore * a software reset. That code will see that external reset is 1524dbbaf20SIan Lepore * configured, and it will wait for 1 second for it to take effect, then 1534dbbaf20SIan Lepore * it will do a software reset as a fallback. 1544dbbaf20SIan Lepore */ 1554dbbaf20SIan Lepore imx_wdog_cpu_reset(BUS_SPACE_PHYSADDR(sc->sc_res[MEMRES], WDOG_CR_REG)); 1564dbbaf20SIan Lepore 1574dbbaf20SIan Lepore return (FILTER_HANDLED); /* unreached */ 1584dbbaf20SIan Lepore } 1594dbbaf20SIan Lepore 1604dbbaf20SIan Lepore static int 161a2c472e7SAleksandr Rybalko imx_wdog_probe(device_t dev) 162a2c472e7SAleksandr Rybalko { 163a2c472e7SAleksandr Rybalko 164add35ed5SIan Lepore if (!ofw_bus_status_okay(dev)) 165add35ed5SIan Lepore return (ENXIO); 166add35ed5SIan Lepore 167398c1838SIan Lepore if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 168a2c472e7SAleksandr Rybalko return (ENXIO); 169a2c472e7SAleksandr Rybalko 170398c1838SIan Lepore device_set_desc(dev, "Freescale i.MX Watchdog"); 171a2c472e7SAleksandr Rybalko return (0); 172a2c472e7SAleksandr Rybalko } 173a2c472e7SAleksandr Rybalko 174a2c472e7SAleksandr Rybalko static int 175a2c472e7SAleksandr Rybalko imx_wdog_attach(device_t dev) 176a2c472e7SAleksandr Rybalko { 177a2c472e7SAleksandr Rybalko struct imx_wdog_softc *sc; 178a2c472e7SAleksandr Rybalko 179a2c472e7SAleksandr Rybalko sc = device_get_softc(dev); 180a2c472e7SAleksandr Rybalko sc->sc_dev = dev; 181a2c472e7SAleksandr Rybalko 182a2c472e7SAleksandr Rybalko if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) { 183a2c472e7SAleksandr Rybalko device_printf(dev, "could not allocate resources\n"); 184a2c472e7SAleksandr Rybalko return (ENXIO); 185a2c472e7SAleksandr Rybalko } 186a2c472e7SAleksandr Rybalko 187a2c472e7SAleksandr Rybalko mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF); 188a2c472e7SAleksandr Rybalko 1894dbbaf20SIan Lepore /* 1904dbbaf20SIan Lepore * If we're configured to assert an external reset signal, set up the 1914dbbaf20SIan Lepore * hardware to do so, and install an interrupt handler whose only 1924dbbaf20SIan Lepore * purpose is to backstop the external reset. Don't worry if the 1934dbbaf20SIan Lepore * interrupt setup fails, since it's only a backstop measure. 1944dbbaf20SIan Lepore */ 1954dbbaf20SIan Lepore if (ofw_bus_has_prop(sc->sc_dev, "fsl,ext-reset-output")) { 1964dbbaf20SIan Lepore WR2(sc, WDOG_CR_REG, WDOG_CR_WDT | RD2(sc, WDOG_CR_REG)); 1974dbbaf20SIan Lepore bus_setup_intr(sc->sc_dev, sc->sc_res[IRQRES], 1984dbbaf20SIan Lepore INTR_TYPE_MISC | INTR_MPSAFE, imx_wdog_intr, NULL, sc, 1994dbbaf20SIan Lepore &sc->sc_ih); 2004dbbaf20SIan Lepore WR2(sc, WDOG_ICR_REG, WDOG_ICR_WIE); /* Enable, count is 0. */ 2014dbbaf20SIan Lepore } 2024dbbaf20SIan Lepore 2034dbbaf20SIan Lepore /* 2044dbbaf20SIan Lepore * Note whether the rom-boot so-called "power-down" watchdog is active, 2054dbbaf20SIan Lepore * so we can disable it when the regular watchdog is first enabled. 2064dbbaf20SIan Lepore */ 2074dbbaf20SIan Lepore if (RD2(sc, WDOG_MCR_REG) & WDOG_MCR_PDE) 2084dbbaf20SIan Lepore sc->sc_pde_enabled = true; 209a2c472e7SAleksandr Rybalko 210a2c472e7SAleksandr Rybalko EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0); 2112b352b73SIan Lepore 2122b352b73SIan Lepore /* 2132b352b73SIan Lepore * The watchdog hardware cannot be disabled, so there's little point in 2142b352b73SIan Lepore * coding up a detach() routine to carefully tear everything down, just 2152b352b73SIan Lepore * make the device busy so that detach can't happen. 2162b352b73SIan Lepore */ 2172b352b73SIan Lepore device_busy(sc->sc_dev); 218a2c472e7SAleksandr Rybalko return (0); 219a2c472e7SAleksandr Rybalko } 22076ecefceSIan Lepore 22176ecefceSIan Lepore static device_method_t imx_wdog_methods[] = { 22276ecefceSIan Lepore DEVMETHOD(device_probe, imx_wdog_probe), 22376ecefceSIan Lepore DEVMETHOD(device_attach, imx_wdog_attach), 22476ecefceSIan Lepore DEVMETHOD_END 22576ecefceSIan Lepore }; 22676ecefceSIan Lepore 22776ecefceSIan Lepore static driver_t imx_wdog_driver = { 22876ecefceSIan Lepore "imx_wdog", 22976ecefceSIan Lepore imx_wdog_methods, 23076ecefceSIan Lepore sizeof(struct imx_wdog_softc), 23176ecefceSIan Lepore }; 23276ecefceSIan Lepore 23376ecefceSIan Lepore static devclass_t imx_wdog_devclass; 23476ecefceSIan Lepore 23576ecefceSIan Lepore DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, imx_wdog_devclass, 0, 0); 2362b352b73SIan Lepore SIMPLEBUS_PNP_INFO(compat_data); 237