1a2c472e7SAleksandr Rybalko /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
32a2c472e7SAleksandr Rybalko #include <sys/bus.h>
33e2e050c8SConrad Meyer #include <sys/eventhandler.h>
34e2e050c8SConrad Meyer #include <sys/kernel.h>
35e2e050c8SConrad Meyer #include <sys/lock.h>
36e2e050c8SConrad Meyer #include <sys/module.h>
37e2e050c8SConrad Meyer #include <sys/mutex.h>
38a2c472e7SAleksandr Rybalko #include <sys/resource.h>
39a2c472e7SAleksandr Rybalko #include <sys/rman.h>
40e2e050c8SConrad Meyer #include <sys/systm.h>
41e2e050c8SConrad Meyer #include <sys/time.h>
42a2c472e7SAleksandr Rybalko #include <sys/watchdog.h>
43a2c472e7SAleksandr Rybalko
44a2c472e7SAleksandr Rybalko #include <machine/bus.h>
45a2c472e7SAleksandr Rybalko #include <machine/intr.h>
46a2c472e7SAleksandr Rybalko
47a2c472e7SAleksandr Rybalko #include <dev/ofw/openfirm.h>
48a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus.h>
49a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus_subr.h>
50a2c472e7SAleksandr Rybalko
514dbbaf20SIan Lepore #include <arm/freescale/imx/imx_machdep.h>
52a2c472e7SAleksandr Rybalko #include <arm/freescale/imx/imx_wdogreg.h>
53a2c472e7SAleksandr Rybalko
54a2c472e7SAleksandr Rybalko struct imx_wdog_softc {
55a2c472e7SAleksandr Rybalko struct mtx sc_mtx;
56a2c472e7SAleksandr Rybalko device_t sc_dev;
57a2c472e7SAleksandr Rybalko struct resource *sc_res[2];
584dbbaf20SIan Lepore void *sc_ih;
59a2c472e7SAleksandr Rybalko uint32_t sc_timeout;
604dbbaf20SIan Lepore bool sc_pde_enabled;
61a2c472e7SAleksandr Rybalko };
62a2c472e7SAleksandr Rybalko
63a2c472e7SAleksandr Rybalko static struct resource_spec imx_wdog_spec[] = {
64a2c472e7SAleksandr Rybalko { SYS_RES_MEMORY, 0, RF_ACTIVE },
65a2c472e7SAleksandr Rybalko { SYS_RES_IRQ, 0, RF_ACTIVE },
6676ecefceSIan Lepore RESOURCE_SPEC_END
67a2c472e7SAleksandr Rybalko };
68a2c472e7SAleksandr Rybalko
6976ecefceSIan Lepore #define MEMRES 0
7076ecefceSIan Lepore #define IRQRES 1
7176ecefceSIan Lepore
72398c1838SIan Lepore static struct ofw_compat_data compat_data[] = {
73398c1838SIan Lepore {"fsl,imx6sx-wdt", 1},
74398c1838SIan Lepore {"fsl,imx6sl-wdt", 1},
75398c1838SIan Lepore {"fsl,imx6q-wdt", 1},
76398c1838SIan Lepore {"fsl,imx53-wdt", 1},
77398c1838SIan Lepore {"fsl,imx51-wdt", 1},
78398c1838SIan Lepore {"fsl,imx50-wdt", 1},
79398c1838SIan Lepore {"fsl,imx35-wdt", 1},
80398c1838SIan Lepore {"fsl,imx27-wdt", 1},
81398c1838SIan Lepore {"fsl,imx25-wdt", 1},
82398c1838SIan Lepore {"fsl,imx21-wdt", 1},
83398c1838SIan Lepore {NULL, 0}
84398c1838SIan Lepore };
85398c1838SIan Lepore
8676ecefceSIan Lepore static inline uint16_t
RD2(struct imx_wdog_softc * sc,bus_size_t offs)8776ecefceSIan Lepore RD2(struct imx_wdog_softc *sc, bus_size_t offs)
8876ecefceSIan Lepore {
89a2c472e7SAleksandr Rybalko
905b810fe4SIan Lepore return (bus_read_2(sc->sc_res[MEMRES], offs));
9176ecefceSIan Lepore }
92a2c472e7SAleksandr Rybalko
9376ecefceSIan Lepore static inline void
WR2(struct imx_wdog_softc * sc,bus_size_t offs,uint16_t val)9476ecefceSIan Lepore WR2(struct imx_wdog_softc *sc, bus_size_t offs, uint16_t val)
9576ecefceSIan Lepore {
96a2c472e7SAleksandr Rybalko
975b810fe4SIan Lepore bus_write_2(sc->sc_res[MEMRES], offs, val);
9876ecefceSIan Lepore }
99a2c472e7SAleksandr Rybalko
10031ff8defSIan Lepore static int
imx_wdog_enable(struct imx_wdog_softc * sc,u_int timeout)10131ff8defSIan Lepore imx_wdog_enable(struct imx_wdog_softc *sc, u_int timeout)
102a2c472e7SAleksandr Rybalko {
103a2c472e7SAleksandr Rybalko uint16_t reg;
104a2c472e7SAleksandr Rybalko
10531ff8defSIan Lepore if (timeout < 1 || timeout > 128)
10631ff8defSIan Lepore return (EINVAL);
10731ff8defSIan Lepore
108a2c472e7SAleksandr Rybalko mtx_lock(&sc->sc_mtx);
109a2c472e7SAleksandr Rybalko if (timeout != sc->sc_timeout) {
110a2c472e7SAleksandr Rybalko sc->sc_timeout = timeout;
111398c1838SIan Lepore reg = RD2(sc, WDOG_CR_REG);
112a2c472e7SAleksandr Rybalko reg &= ~WDOG_CR_WT_MASK;
113a4db01f7SIan Lepore reg |= ((2 * timeout - 1) << WDOG_CR_WT_SHIFT);
1146da71028SIan Lepore WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE);
1156da71028SIan Lepore }
116a2c472e7SAleksandr Rybalko /* Refresh counter */
117398c1838SIan Lepore WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1);
118398c1838SIan Lepore WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2);
1194dbbaf20SIan Lepore /* Watchdog active, can disable rom-boot watchdog. */
1204dbbaf20SIan Lepore if (sc->sc_pde_enabled) {
1214dbbaf20SIan Lepore sc->sc_pde_enabled = false;
1224dbbaf20SIan Lepore reg = RD2(sc, WDOG_MCR_REG);
1234dbbaf20SIan Lepore WR2(sc, WDOG_MCR_REG, reg & ~WDOG_MCR_PDE);
1244dbbaf20SIan Lepore }
12531ff8defSIan Lepore mtx_unlock(&sc->sc_mtx);
12631ff8defSIan Lepore
12731ff8defSIan Lepore return (0);
12831ff8defSIan Lepore }
12931ff8defSIan Lepore
13031ff8defSIan Lepore static void
imx_watchdog(void * arg,u_int cmd,int * error)13131ff8defSIan Lepore imx_watchdog(void *arg, u_int cmd, int *error)
13231ff8defSIan Lepore {
13331ff8defSIan Lepore struct imx_wdog_softc *sc;
13431ff8defSIan Lepore u_int timeout;
13531ff8defSIan Lepore
13631ff8defSIan Lepore sc = arg;
13731ff8defSIan Lepore if (cmd == 0) {
13831ff8defSIan Lepore if (bootverbose)
13931ff8defSIan Lepore device_printf(sc->sc_dev, "Can not be disabled.\n");
14031ff8defSIan Lepore *error = EOPNOTSUPP;
14131ff8defSIan Lepore } else {
14231ff8defSIan Lepore timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U);
14331ff8defSIan Lepore if (imx_wdog_enable(sc, timeout) == 0)
144a2c472e7SAleksandr Rybalko *error = 0;
145a2c472e7SAleksandr Rybalko }
146a2c472e7SAleksandr Rybalko }
147a2c472e7SAleksandr Rybalko
148a2c472e7SAleksandr Rybalko static int
imx_wdog_intr(void * arg)1494dbbaf20SIan Lepore imx_wdog_intr(void *arg)
1504dbbaf20SIan Lepore {
1514dbbaf20SIan Lepore struct imx_wdog_softc *sc = arg;
1524dbbaf20SIan Lepore
1534dbbaf20SIan Lepore /*
1544dbbaf20SIan Lepore * When configured for external reset, the actual reset is supposed to
1554dbbaf20SIan Lepore * happen when some external device responds to the assertion of the
1564dbbaf20SIan Lepore * WDOG_B signal by asserting the POR signal to the chip. This
1574dbbaf20SIan Lepore * interrupt handler is a backstop mechanism; it is set up to fire
1584dbbaf20SIan Lepore * simultaneously with WDOG_B, and if the external reset happens we'll
1594dbbaf20SIan Lepore * never actually make it to here. If we do make it here, just trigger
1604dbbaf20SIan Lepore * a software reset. That code will see that external reset is
1614dbbaf20SIan Lepore * configured, and it will wait for 1 second for it to take effect, then
1624dbbaf20SIan Lepore * it will do a software reset as a fallback.
1634dbbaf20SIan Lepore */
1644dbbaf20SIan Lepore imx_wdog_cpu_reset(BUS_SPACE_PHYSADDR(sc->sc_res[MEMRES], WDOG_CR_REG));
1654dbbaf20SIan Lepore
1664dbbaf20SIan Lepore return (FILTER_HANDLED); /* unreached */
1674dbbaf20SIan Lepore }
1684dbbaf20SIan Lepore
1694dbbaf20SIan Lepore static int
imx_wdog_probe(device_t dev)170a2c472e7SAleksandr Rybalko imx_wdog_probe(device_t dev)
171a2c472e7SAleksandr Rybalko {
172a2c472e7SAleksandr Rybalko
173add35ed5SIan Lepore if (!ofw_bus_status_okay(dev))
174add35ed5SIan Lepore return (ENXIO);
175add35ed5SIan Lepore
176398c1838SIan Lepore if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
177a2c472e7SAleksandr Rybalko return (ENXIO);
178a2c472e7SAleksandr Rybalko
179398c1838SIan Lepore device_set_desc(dev, "Freescale i.MX Watchdog");
180a2c472e7SAleksandr Rybalko return (0);
181a2c472e7SAleksandr Rybalko }
182a2c472e7SAleksandr Rybalko
183a2c472e7SAleksandr Rybalko static int
imx_wdog_attach(device_t dev)184a2c472e7SAleksandr Rybalko imx_wdog_attach(device_t dev)
185a2c472e7SAleksandr Rybalko {
186a2c472e7SAleksandr Rybalko struct imx_wdog_softc *sc;
18731ff8defSIan Lepore pcell_t timeout;
188a2c472e7SAleksandr Rybalko
189a2c472e7SAleksandr Rybalko sc = device_get_softc(dev);
190a2c472e7SAleksandr Rybalko sc->sc_dev = dev;
191a2c472e7SAleksandr Rybalko
192a2c472e7SAleksandr Rybalko if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) {
193a2c472e7SAleksandr Rybalko device_printf(dev, "could not allocate resources\n");
194a2c472e7SAleksandr Rybalko return (ENXIO);
195a2c472e7SAleksandr Rybalko }
196a2c472e7SAleksandr Rybalko
197a2c472e7SAleksandr Rybalko mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF);
198a2c472e7SAleksandr Rybalko
1994dbbaf20SIan Lepore /*
2004dbbaf20SIan Lepore * If we're configured to assert an external reset signal, set up the
2014dbbaf20SIan Lepore * hardware to do so, and install an interrupt handler whose only
2024dbbaf20SIan Lepore * purpose is to backstop the external reset. Don't worry if the
2034dbbaf20SIan Lepore * interrupt setup fails, since it's only a backstop measure.
2044dbbaf20SIan Lepore */
2054dbbaf20SIan Lepore if (ofw_bus_has_prop(sc->sc_dev, "fsl,ext-reset-output")) {
2064dbbaf20SIan Lepore WR2(sc, WDOG_CR_REG, WDOG_CR_WDT | RD2(sc, WDOG_CR_REG));
2074dbbaf20SIan Lepore bus_setup_intr(sc->sc_dev, sc->sc_res[IRQRES],
2084dbbaf20SIan Lepore INTR_TYPE_MISC | INTR_MPSAFE, imx_wdog_intr, NULL, sc,
2094dbbaf20SIan Lepore &sc->sc_ih);
2104dbbaf20SIan Lepore WR2(sc, WDOG_ICR_REG, WDOG_ICR_WIE); /* Enable, count is 0. */
2114dbbaf20SIan Lepore }
2124dbbaf20SIan Lepore
2134dbbaf20SIan Lepore /*
2144dbbaf20SIan Lepore * Note whether the rom-boot so-called "power-down" watchdog is active,
2154dbbaf20SIan Lepore * so we can disable it when the regular watchdog is first enabled.
2164dbbaf20SIan Lepore */
2174dbbaf20SIan Lepore if (RD2(sc, WDOG_MCR_REG) & WDOG_MCR_PDE)
2184dbbaf20SIan Lepore sc->sc_pde_enabled = true;
219a2c472e7SAleksandr Rybalko
220a2c472e7SAleksandr Rybalko EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0);
2212b352b73SIan Lepore
22231ff8defSIan Lepore /* If there is a timeout-sec property, activate the watchdog. */
22331ff8defSIan Lepore if (OF_getencprop(ofw_bus_get_node(sc->sc_dev), "timeout-sec",
22431ff8defSIan Lepore &timeout, sizeof(timeout)) == sizeof(timeout)) {
22531ff8defSIan Lepore if (timeout < 1 || timeout > 128) {
22631ff8defSIan Lepore device_printf(sc->sc_dev, "ERROR: bad timeout-sec "
22731ff8defSIan Lepore "property value %u, using 128\n", timeout);
22831ff8defSIan Lepore timeout = 128;
22931ff8defSIan Lepore }
23031ff8defSIan Lepore imx_wdog_enable(sc, timeout);
23131ff8defSIan Lepore device_printf(sc->sc_dev, "watchdog enabled using "
23231ff8defSIan Lepore "timeout-sec property value %u\n", timeout);
23331ff8defSIan Lepore }
23431ff8defSIan Lepore
2352b352b73SIan Lepore /*
2362b352b73SIan Lepore * The watchdog hardware cannot be disabled, so there's little point in
2372b352b73SIan Lepore * coding up a detach() routine to carefully tear everything down, just
2382b352b73SIan Lepore * make the device busy so that detach can't happen.
2392b352b73SIan Lepore */
2402b352b73SIan Lepore device_busy(sc->sc_dev);
241a2c472e7SAleksandr Rybalko return (0);
242a2c472e7SAleksandr Rybalko }
24376ecefceSIan Lepore
24476ecefceSIan Lepore static device_method_t imx_wdog_methods[] = {
24576ecefceSIan Lepore DEVMETHOD(device_probe, imx_wdog_probe),
24676ecefceSIan Lepore DEVMETHOD(device_attach, imx_wdog_attach),
24776ecefceSIan Lepore DEVMETHOD_END
24876ecefceSIan Lepore };
24976ecefceSIan Lepore
25076ecefceSIan Lepore static driver_t imx_wdog_driver = {
25176ecefceSIan Lepore "imx_wdog",
25276ecefceSIan Lepore imx_wdog_methods,
25376ecefceSIan Lepore sizeof(struct imx_wdog_softc),
25476ecefceSIan Lepore };
25576ecefceSIan Lepore
256ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, 0, 0, BUS_PASS_TIMER);
2572b352b73SIan Lepore SIMPLEBUS_PNP_INFO(compat_data);
258