imx_wdog.c revision 346522
1285612Sdelphij/*- 2132451Sroberto * Copyright (c) 2012, 2013 The FreeBSD Foundation 354359Sroberto * All rights reserved. 4285612Sdelphij * 5285612Sdelphij * This software was developed by Oleksandr Rybalko under sponsorship 654359Sroberto * from the FreeBSD Foundation. 754359Sroberto * 854359Sroberto * Redistribution and use in source and binary forms, with or without 954359Sroberto * modification, are permitted provided that the following conditions 1054359Sroberto * are met: 1154359Sroberto * 1. Redistributions of source code must retain the above copyright 1254359Sroberto * notice, this list of conditions and the following disclaimer. 1354359Sroberto * 2. Redistributions in binary form must reproduce the above copyright 1454359Sroberto * notice, this list of conditions and the following disclaimer in the 15106163Sroberto * documentation and/or other materials provided with the distribution. 16106163Sroberto * 17182007Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18182007Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19182007Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20182007Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21182007Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2254359Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23285612Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24285612Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25285612Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26285612Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27285612Sdelphij * SUCH DAMAGE. 28285612Sdelphij */ 29285612Sdelphij 30285612Sdelphij#include <sys/cdefs.h> 31285612Sdelphij__FBSDID("$FreeBSD: stable/11/sys/arm/freescale/imx/imx_wdog.c 346522 2019-04-22 04:15:22Z ian $"); 32285612Sdelphij 33285612Sdelphij#include <sys/param.h> 34285612Sdelphij#include <sys/systm.h> 35285612Sdelphij#include <sys/kernel.h> 36285612Sdelphij#include <sys/module.h> 37285612Sdelphij#include <sys/time.h> 38285612Sdelphij#include <sys/bus.h> 39285612Sdelphij#include <sys/resource.h> 40285612Sdelphij#include <sys/rman.h> 41285612Sdelphij#include <sys/watchdog.h> 42285612Sdelphij 43285612Sdelphij#include <machine/bus.h> 44285612Sdelphij#include <machine/intr.h> 45285612Sdelphij 46285612Sdelphij#include <dev/fdt/fdt_common.h> 47285612Sdelphij#include <dev/ofw/openfirm.h> 48285612Sdelphij#include <dev/ofw/ofw_bus.h> 49285612Sdelphij#include <dev/ofw/ofw_bus_subr.h> 50285612Sdelphij 51285612Sdelphij#include <arm/freescale/imx/imx_machdep.h> 52285612Sdelphij#include <arm/freescale/imx/imx_wdogreg.h> 53285612Sdelphij 54285612Sdelphijstruct imx_wdog_softc { 55285612Sdelphij struct mtx sc_mtx; 56285612Sdelphij device_t sc_dev; 57285612Sdelphij struct resource *sc_res[2]; 58285612Sdelphij void *sc_ih; 59285612Sdelphij uint32_t sc_timeout; 60285612Sdelphij bool sc_pde_enabled; 61285612Sdelphij}; 62285612Sdelphij 63285612Sdelphijstatic struct resource_spec imx_wdog_spec[] = { 64285612Sdelphij { SYS_RES_MEMORY, 0, RF_ACTIVE }, 65285612Sdelphij { SYS_RES_IRQ, 0, RF_ACTIVE }, 66285612Sdelphij RESOURCE_SPEC_END 67285612Sdelphij}; 68285612Sdelphij 69285612Sdelphij#define MEMRES 0 70285612Sdelphij#define IRQRES 1 71285612Sdelphij 72285612Sdelphijstatic struct ofw_compat_data compat_data[] = { 73285612Sdelphij {"fsl,imx6sx-wdt", 1}, 74285612Sdelphij {"fsl,imx6sl-wdt", 1}, 75285612Sdelphij {"fsl,imx6q-wdt", 1}, 76285612Sdelphij {"fsl,imx53-wdt", 1}, 77285612Sdelphij {"fsl,imx51-wdt", 1}, 7854359Sroberto {"fsl,imx50-wdt", 1}, 79200576Sroberto {"fsl,imx35-wdt", 1}, 8054359Sroberto {"fsl,imx27-wdt", 1}, 81200576Sroberto {"fsl,imx25-wdt", 1}, 82132451Sroberto {"fsl,imx21-wdt", 1}, 83132451Sroberto {NULL, 0} 84132451Sroberto}; 85132451Sroberto 8682498Srobertostatic inline uint16_t 87132451SrobertoRD2(struct imx_wdog_softc *sc, bus_size_t offs) 8854359Sroberto{ 8954359Sroberto 9054359Sroberto return (bus_read_2(sc->sc_res[MEMRES], offs)); 9154359Sroberto} 9254359Sroberto 9354359Srobertostatic inline void 9454359SrobertoWR2(struct imx_wdog_softc *sc, bus_size_t offs, uint16_t val) 9554359Sroberto{ 96285612Sdelphij 97182007Sroberto bus_write_2(sc->sc_res[MEMRES], offs, val); 98182007Sroberto} 99285612Sdelphij 100285612Sdelphijstatic int 101285612Sdelphijimx_wdog_enable(struct imx_wdog_softc *sc, u_int timeout) 102285612Sdelphij{ 103285612Sdelphij uint16_t reg; 104285612Sdelphij 105285612Sdelphij if (timeout < 1 || timeout > 128) 106285612Sdelphij return (EINVAL); 107285612Sdelphij 108285612Sdelphij mtx_lock(&sc->sc_mtx); 109285612Sdelphij if (timeout != sc->sc_timeout) { 110285612Sdelphij sc->sc_timeout = timeout; 111285612Sdelphij reg = RD2(sc, WDOG_CR_REG); 112285612Sdelphij reg &= ~WDOG_CR_WT_MASK; 113285612Sdelphij reg |= ((2 * timeout - 1) << WDOG_CR_WT_SHIFT); 114285612Sdelphij WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE); 115285612Sdelphij } 116285612Sdelphij /* Refresh counter */ 117285612Sdelphij WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1); 118285612Sdelphij WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2); 119285612Sdelphij /* Watchdog active, can disable rom-boot watchdog. */ 120285612Sdelphij if (sc->sc_pde_enabled) { 121285612Sdelphij sc->sc_pde_enabled = false; 122285612Sdelphij reg = RD2(sc, WDOG_MCR_REG); 123285612Sdelphij WR2(sc, WDOG_MCR_REG, reg & ~WDOG_MCR_PDE); 124285612Sdelphij } 125289999Sglebius mtx_unlock(&sc->sc_mtx); 126285612Sdelphij 127285612Sdelphij return (0); 128285612Sdelphij} 129285612Sdelphij 130285612Sdelphijstatic void 131285612Sdelphijimx_watchdog(void *arg, u_int cmd, int *error) 132285612Sdelphij{ 133285612Sdelphij struct imx_wdog_softc *sc; 134285612Sdelphij u_int timeout; 135285612Sdelphij 136285612Sdelphij sc = arg; 137182007Sroberto if (cmd == 0) { 138182007Sroberto if (bootverbose) 139285612Sdelphij device_printf(sc->sc_dev, "Can not be disabled.\n"); 140285612Sdelphij *error = EOPNOTSUPP; 141182007Sroberto } else { 142182007Sroberto timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U); 143182007Sroberto if (imx_wdog_enable(sc, timeout) == 0) 144200576Sroberto *error = 0; 145285612Sdelphij } 146285612Sdelphij} 147285612Sdelphij 148182007Srobertostatic int 149182007Srobertoimx_wdog_intr(void *arg) 150285612Sdelphij{ 151285612Sdelphij struct imx_wdog_softc *sc = arg; 152285612Sdelphij 153285612Sdelphij /* 154285612Sdelphij * When configured for external reset, the actual reset is supposed to 155285612Sdelphij * happen when some external device responds to the assertion of the 156285612Sdelphij * WDOG_B signal by asserting the POR signal to the chip. This 157285612Sdelphij * interrupt handler is a backstop mechanism; it is set up to fire 158285612Sdelphij * simultaneously with WDOG_B, and if the external reset happens we'll 159285612Sdelphij * never actually make it to here. If we do make it here, just trigger 160285612Sdelphij * a software reset. That code will see that external reset is 161285612Sdelphij * configured, and it will wait for 1 second for it to take effect, then 162285612Sdelphij * it will do a software reset as a fallback. 163285612Sdelphij */ 164285612Sdelphij imx_wdog_cpu_reset(BUS_SPACE_PHYSADDR(sc->sc_res[MEMRES], WDOG_CR_REG)); 165285612Sdelphij 166285612Sdelphij return (FILTER_HANDLED); /* unreached */ 167285612Sdelphij} 168285612Sdelphij 169285612Sdelphijstatic int 170200576Srobertoimx_wdog_probe(device_t dev) 171285612Sdelphij{ 172182007Sroberto 173200576Sroberto if (!ofw_bus_status_okay(dev)) 174182007Sroberto return (ENXIO); 175182007Sroberto 176285612Sdelphij if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 177285612Sdelphij return (ENXIO); 178285612Sdelphij 179285612Sdelphij device_set_desc(dev, "Freescale i.MX Watchdog"); 180285612Sdelphij return (0); 181285612Sdelphij} 182285612Sdelphij 183285612Sdelphijstatic int 184182007Srobertoimx_wdog_attach(device_t dev) 185285612Sdelphij{ 186285612Sdelphij struct imx_wdog_softc *sc; 187285612Sdelphij pcell_t timeout; 188285612Sdelphij 189285612Sdelphij sc = device_get_softc(dev); 190285612Sdelphij sc->sc_dev = dev; 191285612Sdelphij 192285612Sdelphij if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) { 193285612Sdelphij device_printf(dev, "could not allocate resources\n"); 194285612Sdelphij return (ENXIO); 195285612Sdelphij } 196285612Sdelphij 197285612Sdelphij mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF); 198285612Sdelphij 199182007Sroberto /* 200285612Sdelphij * If we're configured to assert an external reset signal, set up the 201285612Sdelphij * hardware to do so, and install an interrupt handler whose only 202285612Sdelphij * purpose is to backstop the external reset. Don't worry if the 203285612Sdelphij * interrupt setup fails, since it's only a backstop measure. 204285612Sdelphij */ 205285612Sdelphij if (ofw_bus_has_prop(sc->sc_dev, "fsl,ext-reset-output")) { 206285612Sdelphij WR2(sc, WDOG_CR_REG, WDOG_CR_WDT | RD2(sc, WDOG_CR_REG)); 207285612Sdelphij bus_setup_intr(sc->sc_dev, sc->sc_res[IRQRES], 208285612Sdelphij INTR_TYPE_MISC | INTR_MPSAFE, imx_wdog_intr, NULL, sc, 209285612Sdelphij &sc->sc_ih); 210285612Sdelphij WR2(sc, WDOG_ICR_REG, WDOG_ICR_WIE); /* Enable, count is 0. */ 211285612Sdelphij } 212285612Sdelphij 213285612Sdelphij /* 214285612Sdelphij * Note whether the rom-boot so-called "power-down" watchdog is active, 215285612Sdelphij * so we can disable it when the regular watchdog is first enabled. 216285612Sdelphij */ 217182007Sroberto if (RD2(sc, WDOG_MCR_REG) & WDOG_MCR_PDE) 218182007Sroberto sc->sc_pde_enabled = true; 219285612Sdelphij 220285612Sdelphij EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0); 221285612Sdelphij 222285612Sdelphij /* If there is a timeout-sec property, activate the watchdog. */ 223182007Sroberto if (OF_getencprop(ofw_bus_get_node(sc->sc_dev), "timeout-sec", 224132451Sroberto &timeout, sizeof(timeout)) == sizeof(timeout)) { 225285612Sdelphij if (timeout < 1 || timeout > 128) { 22654359Sroberto device_printf(sc->sc_dev, "ERROR: bad timeout-sec " 227285612Sdelphij "property value %u, using 128\n", timeout); 228182007Sroberto timeout = 128; 229132451Sroberto } 230132451Sroberto imx_wdog_enable(sc, timeout); 231132451Sroberto device_printf(sc->sc_dev, "watchdog enabled using " 23254359Sroberto "timeout-sec property value %u\n", timeout); 233289999Sglebius } 234285612Sdelphij 235285612Sdelphij /* 236285612Sdelphij * The watchdog hardware cannot be disabled, so there's little point in 237285612Sdelphij * coding up a detach() routine to carefully tear everything down, just 238285612Sdelphij * make the device busy so that detach can't happen. 23954359Sroberto */ 240132451Sroberto device_busy(sc->sc_dev); 24154359Sroberto return (0); 242289999Sglebius} 243285612Sdelphij 24454359Srobertostatic device_method_t imx_wdog_methods[] = { 245285612Sdelphij DEVMETHOD(device_probe, imx_wdog_probe), 24654359Sroberto DEVMETHOD(device_attach, imx_wdog_attach), 247132451Sroberto DEVMETHOD_END 248285612Sdelphij}; 249285612Sdelphij 250285612Sdelphijstatic driver_t imx_wdog_driver = { 251285612Sdelphij "imx_wdog", 252285612Sdelphij imx_wdog_methods, 253285612Sdelphij sizeof(struct imx_wdog_softc), 254132451Sroberto}; 25554359Sroberto 256132451Srobertostatic devclass_t imx_wdog_devclass; 25756746Sroberto 258285612SdelphijEARLY_DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, 259285612Sdelphij imx_wdog_devclass, 0, 0, BUS_PASS_TIMER); 260285612SdelphijSIMPLEBUS_PNP_INFO(compat_data); 261132451Sroberto