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 * 6a2c472e7SAleksandr Rybalko * This software was developed by Oleksandr Rybalko under sponsorship 7a2c472e7SAleksandr Rybalko * from the FreeBSD Foundation. 8a2c472e7SAleksandr Rybalko * 9a2c472e7SAleksandr Rybalko * Redistribution and use in source and binary forms, with or without 10a2c472e7SAleksandr Rybalko * modification, are permitted provided that the following conditions 11a2c472e7SAleksandr Rybalko * are met: 12a2c472e7SAleksandr Rybalko * 1. Redistributions of source code must retain the above copyright 13a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer. 14a2c472e7SAleksandr Rybalko * 2. Redistributions in binary form must reproduce the above copyright 15a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer in the 16a2c472e7SAleksandr Rybalko * documentation and/or other materials provided with the distribution. 17a2c472e7SAleksandr Rybalko * 18a2c472e7SAleksandr Rybalko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19a2c472e7SAleksandr Rybalko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20a2c472e7SAleksandr Rybalko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21a2c472e7SAleksandr Rybalko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22a2c472e7SAleksandr Rybalko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23a2c472e7SAleksandr Rybalko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24a2c472e7SAleksandr Rybalko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25a2c472e7SAleksandr Rybalko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26a2c472e7SAleksandr Rybalko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27a2c472e7SAleksandr Rybalko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28a2c472e7SAleksandr Rybalko * SUCH DAMAGE. 29a2c472e7SAleksandr Rybalko */ 30a2c472e7SAleksandr Rybalko 31a2c472e7SAleksandr Rybalko #include <sys/cdefs.h> 32a2c472e7SAleksandr Rybalko __FBSDID("$FreeBSD$"); 33a2c472e7SAleksandr Rybalko 34a2c472e7SAleksandr Rybalko #include <sys/param.h> 35a2c472e7SAleksandr Rybalko #include <sys/bus.h> 36e2e050c8SConrad Meyer #include <sys/eventhandler.h> 37e2e050c8SConrad Meyer #include <sys/kernel.h> 38e2e050c8SConrad Meyer #include <sys/lock.h> 39e2e050c8SConrad Meyer #include <sys/module.h> 40e2e050c8SConrad Meyer #include <sys/mutex.h> 41a2c472e7SAleksandr Rybalko #include <sys/resource.h> 42a2c472e7SAleksandr Rybalko #include <sys/rman.h> 43e2e050c8SConrad Meyer #include <sys/systm.h> 44e2e050c8SConrad Meyer #include <sys/time.h> 45a2c472e7SAleksandr Rybalko #include <sys/watchdog.h> 46a2c472e7SAleksandr Rybalko 47a2c472e7SAleksandr Rybalko #include <machine/bus.h> 48a2c472e7SAleksandr Rybalko #include <machine/intr.h> 49a2c472e7SAleksandr Rybalko 50a2c472e7SAleksandr Rybalko #include <dev/ofw/openfirm.h> 51a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus.h> 52a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus_subr.h> 53a2c472e7SAleksandr Rybalko 544dbbaf20SIan Lepore #include <arm/freescale/imx/imx_machdep.h> 55a2c472e7SAleksandr Rybalko #include <arm/freescale/imx/imx_wdogreg.h> 56a2c472e7SAleksandr Rybalko 57a2c472e7SAleksandr Rybalko struct imx_wdog_softc { 58a2c472e7SAleksandr Rybalko struct mtx sc_mtx; 59a2c472e7SAleksandr Rybalko device_t sc_dev; 60a2c472e7SAleksandr Rybalko struct resource *sc_res[2]; 614dbbaf20SIan Lepore void *sc_ih; 62a2c472e7SAleksandr Rybalko uint32_t sc_timeout; 634dbbaf20SIan Lepore bool sc_pde_enabled; 64a2c472e7SAleksandr Rybalko }; 65a2c472e7SAleksandr Rybalko 66a2c472e7SAleksandr Rybalko static struct resource_spec imx_wdog_spec[] = { 67a2c472e7SAleksandr Rybalko { SYS_RES_MEMORY, 0, RF_ACTIVE }, 68a2c472e7SAleksandr Rybalko { SYS_RES_IRQ, 0, RF_ACTIVE }, 6976ecefceSIan Lepore RESOURCE_SPEC_END 70a2c472e7SAleksandr Rybalko }; 71a2c472e7SAleksandr Rybalko 7276ecefceSIan Lepore #define MEMRES 0 7376ecefceSIan Lepore #define IRQRES 1 7476ecefceSIan Lepore 75398c1838SIan Lepore static struct ofw_compat_data compat_data[] = { 76398c1838SIan Lepore {"fsl,imx6sx-wdt", 1}, 77398c1838SIan Lepore {"fsl,imx6sl-wdt", 1}, 78398c1838SIan Lepore {"fsl,imx6q-wdt", 1}, 79398c1838SIan Lepore {"fsl,imx53-wdt", 1}, 80398c1838SIan Lepore {"fsl,imx51-wdt", 1}, 81398c1838SIan Lepore {"fsl,imx50-wdt", 1}, 82398c1838SIan Lepore {"fsl,imx35-wdt", 1}, 83398c1838SIan Lepore {"fsl,imx27-wdt", 1}, 84398c1838SIan Lepore {"fsl,imx25-wdt", 1}, 85398c1838SIan Lepore {"fsl,imx21-wdt", 1}, 86398c1838SIan Lepore {NULL, 0} 87398c1838SIan Lepore }; 88398c1838SIan Lepore 8976ecefceSIan Lepore static inline uint16_t 9076ecefceSIan Lepore RD2(struct imx_wdog_softc *sc, bus_size_t offs) 9176ecefceSIan Lepore { 92a2c472e7SAleksandr Rybalko 935b810fe4SIan Lepore return (bus_read_2(sc->sc_res[MEMRES], offs)); 9476ecefceSIan Lepore } 95a2c472e7SAleksandr Rybalko 9676ecefceSIan Lepore static inline void 9776ecefceSIan Lepore WR2(struct imx_wdog_softc *sc, bus_size_t offs, uint16_t val) 9876ecefceSIan Lepore { 99a2c472e7SAleksandr Rybalko 1005b810fe4SIan Lepore bus_write_2(sc->sc_res[MEMRES], offs, val); 10176ecefceSIan Lepore } 102a2c472e7SAleksandr Rybalko 10331ff8defSIan Lepore static int 10431ff8defSIan Lepore imx_wdog_enable(struct imx_wdog_softc *sc, u_int timeout) 105a2c472e7SAleksandr Rybalko { 106a2c472e7SAleksandr Rybalko uint16_t reg; 107a2c472e7SAleksandr Rybalko 10831ff8defSIan Lepore if (timeout < 1 || timeout > 128) 10931ff8defSIan Lepore return (EINVAL); 11031ff8defSIan Lepore 111a2c472e7SAleksandr Rybalko mtx_lock(&sc->sc_mtx); 112a2c472e7SAleksandr Rybalko if (timeout != sc->sc_timeout) { 113a2c472e7SAleksandr Rybalko sc->sc_timeout = timeout; 114398c1838SIan Lepore reg = RD2(sc, WDOG_CR_REG); 115a2c472e7SAleksandr Rybalko reg &= ~WDOG_CR_WT_MASK; 116a4db01f7SIan Lepore reg |= ((2 * timeout - 1) << WDOG_CR_WT_SHIFT); 1176da71028SIan Lepore WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE); 1186da71028SIan Lepore } 119a2c472e7SAleksandr Rybalko /* Refresh counter */ 120398c1838SIan Lepore WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1); 121398c1838SIan Lepore WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2); 1224dbbaf20SIan Lepore /* Watchdog active, can disable rom-boot watchdog. */ 1234dbbaf20SIan Lepore if (sc->sc_pde_enabled) { 1244dbbaf20SIan Lepore sc->sc_pde_enabled = false; 1254dbbaf20SIan Lepore reg = RD2(sc, WDOG_MCR_REG); 1264dbbaf20SIan Lepore WR2(sc, WDOG_MCR_REG, reg & ~WDOG_MCR_PDE); 1274dbbaf20SIan Lepore } 12831ff8defSIan Lepore mtx_unlock(&sc->sc_mtx); 12931ff8defSIan Lepore 13031ff8defSIan Lepore return (0); 13131ff8defSIan Lepore } 13231ff8defSIan Lepore 13331ff8defSIan Lepore static void 13431ff8defSIan Lepore imx_watchdog(void *arg, u_int cmd, int *error) 13531ff8defSIan Lepore { 13631ff8defSIan Lepore struct imx_wdog_softc *sc; 13731ff8defSIan Lepore u_int timeout; 13831ff8defSIan Lepore 13931ff8defSIan Lepore sc = arg; 14031ff8defSIan Lepore if (cmd == 0) { 14131ff8defSIan Lepore if (bootverbose) 14231ff8defSIan Lepore device_printf(sc->sc_dev, "Can not be disabled.\n"); 14331ff8defSIan Lepore *error = EOPNOTSUPP; 14431ff8defSIan Lepore } else { 14531ff8defSIan Lepore timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U); 14631ff8defSIan Lepore if (imx_wdog_enable(sc, timeout) == 0) 147a2c472e7SAleksandr Rybalko *error = 0; 148a2c472e7SAleksandr Rybalko } 149a2c472e7SAleksandr Rybalko } 150a2c472e7SAleksandr Rybalko 151a2c472e7SAleksandr Rybalko static int 1524dbbaf20SIan Lepore imx_wdog_intr(void *arg) 1534dbbaf20SIan Lepore { 1544dbbaf20SIan Lepore struct imx_wdog_softc *sc = arg; 1554dbbaf20SIan Lepore 1564dbbaf20SIan Lepore /* 1574dbbaf20SIan Lepore * When configured for external reset, the actual reset is supposed to 1584dbbaf20SIan Lepore * happen when some external device responds to the assertion of the 1594dbbaf20SIan Lepore * WDOG_B signal by asserting the POR signal to the chip. This 1604dbbaf20SIan Lepore * interrupt handler is a backstop mechanism; it is set up to fire 1614dbbaf20SIan Lepore * simultaneously with WDOG_B, and if the external reset happens we'll 1624dbbaf20SIan Lepore * never actually make it to here. If we do make it here, just trigger 1634dbbaf20SIan Lepore * a software reset. That code will see that external reset is 1644dbbaf20SIan Lepore * configured, and it will wait for 1 second for it to take effect, then 1654dbbaf20SIan Lepore * it will do a software reset as a fallback. 1664dbbaf20SIan Lepore */ 1674dbbaf20SIan Lepore imx_wdog_cpu_reset(BUS_SPACE_PHYSADDR(sc->sc_res[MEMRES], WDOG_CR_REG)); 1684dbbaf20SIan Lepore 1694dbbaf20SIan Lepore return (FILTER_HANDLED); /* unreached */ 1704dbbaf20SIan Lepore } 1714dbbaf20SIan Lepore 1724dbbaf20SIan Lepore static int 173a2c472e7SAleksandr Rybalko imx_wdog_probe(device_t dev) 174a2c472e7SAleksandr Rybalko { 175a2c472e7SAleksandr Rybalko 176add35ed5SIan Lepore if (!ofw_bus_status_okay(dev)) 177add35ed5SIan Lepore return (ENXIO); 178add35ed5SIan Lepore 179398c1838SIan Lepore if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 180a2c472e7SAleksandr Rybalko return (ENXIO); 181a2c472e7SAleksandr Rybalko 182398c1838SIan Lepore device_set_desc(dev, "Freescale i.MX Watchdog"); 183a2c472e7SAleksandr Rybalko return (0); 184a2c472e7SAleksandr Rybalko } 185a2c472e7SAleksandr Rybalko 186a2c472e7SAleksandr Rybalko static int 187a2c472e7SAleksandr Rybalko imx_wdog_attach(device_t dev) 188a2c472e7SAleksandr Rybalko { 189a2c472e7SAleksandr Rybalko struct imx_wdog_softc *sc; 19031ff8defSIan Lepore pcell_t timeout; 191a2c472e7SAleksandr Rybalko 192a2c472e7SAleksandr Rybalko sc = device_get_softc(dev); 193a2c472e7SAleksandr Rybalko sc->sc_dev = dev; 194a2c472e7SAleksandr Rybalko 195a2c472e7SAleksandr Rybalko if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) { 196a2c472e7SAleksandr Rybalko device_printf(dev, "could not allocate resources\n"); 197a2c472e7SAleksandr Rybalko return (ENXIO); 198a2c472e7SAleksandr Rybalko } 199a2c472e7SAleksandr Rybalko 200a2c472e7SAleksandr Rybalko mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF); 201a2c472e7SAleksandr Rybalko 2024dbbaf20SIan Lepore /* 2034dbbaf20SIan Lepore * If we're configured to assert an external reset signal, set up the 2044dbbaf20SIan Lepore * hardware to do so, and install an interrupt handler whose only 2054dbbaf20SIan Lepore * purpose is to backstop the external reset. Don't worry if the 2064dbbaf20SIan Lepore * interrupt setup fails, since it's only a backstop measure. 2074dbbaf20SIan Lepore */ 2084dbbaf20SIan Lepore if (ofw_bus_has_prop(sc->sc_dev, "fsl,ext-reset-output")) { 2094dbbaf20SIan Lepore WR2(sc, WDOG_CR_REG, WDOG_CR_WDT | RD2(sc, WDOG_CR_REG)); 2104dbbaf20SIan Lepore bus_setup_intr(sc->sc_dev, sc->sc_res[IRQRES], 2114dbbaf20SIan Lepore INTR_TYPE_MISC | INTR_MPSAFE, imx_wdog_intr, NULL, sc, 2124dbbaf20SIan Lepore &sc->sc_ih); 2134dbbaf20SIan Lepore WR2(sc, WDOG_ICR_REG, WDOG_ICR_WIE); /* Enable, count is 0. */ 2144dbbaf20SIan Lepore } 2154dbbaf20SIan Lepore 2164dbbaf20SIan Lepore /* 2174dbbaf20SIan Lepore * Note whether the rom-boot so-called "power-down" watchdog is active, 2184dbbaf20SIan Lepore * so we can disable it when the regular watchdog is first enabled. 2194dbbaf20SIan Lepore */ 2204dbbaf20SIan Lepore if (RD2(sc, WDOG_MCR_REG) & WDOG_MCR_PDE) 2214dbbaf20SIan Lepore sc->sc_pde_enabled = true; 222a2c472e7SAleksandr Rybalko 223a2c472e7SAleksandr Rybalko EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0); 2242b352b73SIan Lepore 22531ff8defSIan Lepore /* If there is a timeout-sec property, activate the watchdog. */ 22631ff8defSIan Lepore if (OF_getencprop(ofw_bus_get_node(sc->sc_dev), "timeout-sec", 22731ff8defSIan Lepore &timeout, sizeof(timeout)) == sizeof(timeout)) { 22831ff8defSIan Lepore if (timeout < 1 || timeout > 128) { 22931ff8defSIan Lepore device_printf(sc->sc_dev, "ERROR: bad timeout-sec " 23031ff8defSIan Lepore "property value %u, using 128\n", timeout); 23131ff8defSIan Lepore timeout = 128; 23231ff8defSIan Lepore } 23331ff8defSIan Lepore imx_wdog_enable(sc, timeout); 23431ff8defSIan Lepore device_printf(sc->sc_dev, "watchdog enabled using " 23531ff8defSIan Lepore "timeout-sec property value %u\n", timeout); 23631ff8defSIan Lepore } 23731ff8defSIan Lepore 2382b352b73SIan Lepore /* 2392b352b73SIan Lepore * The watchdog hardware cannot be disabled, so there's little point in 2402b352b73SIan Lepore * coding up a detach() routine to carefully tear everything down, just 2412b352b73SIan Lepore * make the device busy so that detach can't happen. 2422b352b73SIan Lepore */ 2432b352b73SIan Lepore device_busy(sc->sc_dev); 244a2c472e7SAleksandr Rybalko return (0); 245a2c472e7SAleksandr Rybalko } 24676ecefceSIan Lepore 24776ecefceSIan Lepore static device_method_t imx_wdog_methods[] = { 24876ecefceSIan Lepore DEVMETHOD(device_probe, imx_wdog_probe), 24976ecefceSIan Lepore DEVMETHOD(device_attach, imx_wdog_attach), 25076ecefceSIan Lepore DEVMETHOD_END 25176ecefceSIan Lepore }; 25276ecefceSIan Lepore 25376ecefceSIan Lepore static driver_t imx_wdog_driver = { 25476ecefceSIan Lepore "imx_wdog", 25576ecefceSIan Lepore imx_wdog_methods, 25676ecefceSIan Lepore sizeof(struct imx_wdog_softc), 25776ecefceSIan Lepore }; 25876ecefceSIan Lepore 259*ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, 0, 0, BUS_PASS_TIMER); 2602b352b73SIan Lepore SIMPLEBUS_PNP_INFO(compat_data); 261