2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012, 2013 The FreeBSD Foundation
7 * This software was developed by Oleksandr Rybalko under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/resource.h>
43 #include <sys/watchdog.h>
45 #include <machine/bus.h>
46 #include <machine/intr.h>
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <arm/freescale/imx/imx_machdep.h>
53 #include <arm/freescale/imx/imx_wdogreg.h>
55 struct imx_wdog_softc {
58 struct resource *sc_res[2];
64 static struct resource_spec imx_wdog_spec[] = {
65 { SYS_RES_MEMORY, 0, RF_ACTIVE },
66 { SYS_RES_IRQ, 0, RF_ACTIVE },
73 static struct ofw_compat_data compat_data[] = {
74 {"fsl,imx6sx-wdt", 1},
75 {"fsl,imx6sl-wdt", 1},
87 static inline uint16_t
88 RD2(struct imx_wdog_softc *sc, bus_size_t offs)
91 return (bus_read_2(sc->sc_res[MEMRES], offs));
95 WR2(struct imx_wdog_softc *sc, bus_size_t offs, uint16_t val)
98 bus_write_2(sc->sc_res[MEMRES], offs, val);
102 imx_watchdog(void *arg, u_int cmd, int *error)
104 struct imx_wdog_softc *sc;
109 mtx_lock(&sc->sc_mtx);
112 device_printf(sc->sc_dev, "Can not be disabled.\n");
115 timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U);
116 if (timeout > 1 && timeout < 128) {
117 if (timeout != sc->sc_timeout) {
118 sc->sc_timeout = timeout;
119 reg = RD2(sc, WDOG_CR_REG);
120 reg &= ~WDOG_CR_WT_MASK;
121 reg |= (timeout << (WDOG_CR_WT_SHIFT + 1)) &
123 WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE);
125 /* Refresh counter */
126 WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1);
127 WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2);
128 /* Watchdog active, can disable rom-boot watchdog. */
129 if (sc->sc_pde_enabled) {
130 sc->sc_pde_enabled = false;
131 reg = RD2(sc, WDOG_MCR_REG);
132 WR2(sc, WDOG_MCR_REG, reg & ~WDOG_MCR_PDE);
137 mtx_unlock(&sc->sc_mtx);
141 imx_wdog_intr(void *arg)
143 struct imx_wdog_softc *sc = arg;
146 * When configured for external reset, the actual reset is supposed to
147 * happen when some external device responds to the assertion of the
148 * WDOG_B signal by asserting the POR signal to the chip. This
149 * interrupt handler is a backstop mechanism; it is set up to fire
150 * simultaneously with WDOG_B, and if the external reset happens we'll
151 * never actually make it to here. If we do make it here, just trigger
152 * a software reset. That code will see that external reset is
153 * configured, and it will wait for 1 second for it to take effect, then
154 * it will do a software reset as a fallback.
156 imx_wdog_cpu_reset(BUS_SPACE_PHYSADDR(sc->sc_res[MEMRES], WDOG_CR_REG));
158 return (FILTER_HANDLED); /* unreached */
162 imx_wdog_probe(device_t dev)
165 if (!ofw_bus_status_okay(dev))
168 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
171 device_set_desc(dev, "Freescale i.MX Watchdog");
176 imx_wdog_attach(device_t dev)
178 struct imx_wdog_softc *sc;
180 sc = device_get_softc(dev);
183 if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) {
184 device_printf(dev, "could not allocate resources\n");
188 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF);
191 * If we're configured to assert an external reset signal, set up the
192 * hardware to do so, and install an interrupt handler whose only
193 * purpose is to backstop the external reset. Don't worry if the
194 * interrupt setup fails, since it's only a backstop measure.
196 if (ofw_bus_has_prop(sc->sc_dev, "fsl,ext-reset-output")) {
197 WR2(sc, WDOG_CR_REG, WDOG_CR_WDT | RD2(sc, WDOG_CR_REG));
198 bus_setup_intr(sc->sc_dev, sc->sc_res[IRQRES],
199 INTR_TYPE_MISC | INTR_MPSAFE, imx_wdog_intr, NULL, sc,
201 WR2(sc, WDOG_ICR_REG, WDOG_ICR_WIE); /* Enable, count is 0. */
205 * Note whether the rom-boot so-called "power-down" watchdog is active,
206 * so we can disable it when the regular watchdog is first enabled.
208 if (RD2(sc, WDOG_MCR_REG) & WDOG_MCR_PDE)
209 sc->sc_pde_enabled = true;
211 EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0);
215 static device_method_t imx_wdog_methods[] = {
216 DEVMETHOD(device_probe, imx_wdog_probe),
217 DEVMETHOD(device_attach, imx_wdog_attach),
221 static driver_t imx_wdog_driver = {
224 sizeof(struct imx_wdog_softc),
227 static devclass_t imx_wdog_devclass;
229 DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, imx_wdog_devclass, 0, 0);