xref: /freebsd/sys/arm/freescale/imx/imx_wdog.c (revision 76ecefce9d8153933108b52d211bce75a4cc75ef)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012, 2013 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Oleksandr Rybalko under sponsorship
8  * from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/time.h>
40 #include <sys/bus.h>
41 #include <sys/resource.h>
42 #include <sys/rman.h>
43 #include <sys/watchdog.h>
44 
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47 
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include <arm/freescale/imx/imx_wdogreg.h>
53 
54 struct imx_wdog_softc {
55 	struct mtx		sc_mtx;
56 	device_t		sc_dev;
57 	struct resource		*sc_res[2];
58 	uint32_t		sc_timeout;
59 };
60 
61 static struct resource_spec imx_wdog_spec[] = {
62 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
63 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
64 	RESOURCE_SPEC_END
65 };
66 
67 #define	MEMRES	0
68 #define	IRQRES	1
69 
70 static struct ofw_compat_data compat_data[] = {
71 	{"fsl,imx6sx-wdt", 1},
72 	{"fsl,imx6sl-wdt", 1},
73 	{"fsl,imx6q-wdt",  1},
74 	{"fsl,imx53-wdt",  1},
75 	{"fsl,imx51-wdt",  1},
76 	{"fsl,imx50-wdt",  1},
77 	{"fsl,imx35-wdt",  1},
78 	{"fsl,imx27-wdt",  1},
79 	{"fsl,imx25-wdt",  1},
80 	{"fsl,imx21-wdt",  1},
81 	{NULL,             0}
82 };
83 
84 static inline uint16_t
85 RD2(struct imx_wdog_softc *sc, bus_size_t offs)
86 {
87 
88 	return bus_read_2(sc->sc_res[MEMRES], offs);
89 }
90 
91 static inline void
92 WR2(struct imx_wdog_softc *sc, bus_size_t offs, uint16_t val)
93 {
94 
95 	return bus_write_2(sc->sc_res[MEMRES], offs, val);
96 }
97 
98 static void
99 imx_watchdog(void *arg, u_int cmd, int *error)
100 {
101 	struct imx_wdog_softc *sc;
102 	uint16_t reg;
103 	u_int timeout;
104 
105 	sc = arg;
106 	mtx_lock(&sc->sc_mtx);
107 	if (cmd == 0) {
108 		if (bootverbose)
109 			device_printf(sc->sc_dev, "Can not be disabled.\n");
110 		*error = EOPNOTSUPP;
111 	} else {
112 		timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U);
113 		if (timeout > 1 && timeout < 128) {
114 			if (timeout != sc->sc_timeout) {
115 				sc->sc_timeout = timeout;
116 				reg = RD2(sc, WDOG_CR_REG);
117 				reg &= ~WDOG_CR_WT_MASK;
118 				reg |= (timeout << (WDOG_CR_WT_SHIFT + 1)) &
119 				    WDOG_CR_WT_MASK;
120 				WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE);
121 			}
122 			/* Refresh counter */
123 			WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1);
124 			WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2);
125 			*error = 0;
126 		}
127 	}
128 	mtx_unlock(&sc->sc_mtx);
129 }
130 
131 static int
132 imx_wdog_probe(device_t dev)
133 {
134 
135 	if (!ofw_bus_status_okay(dev))
136 		return (ENXIO);
137 
138 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
139 		return (ENXIO);
140 
141 	device_set_desc(dev, "Freescale i.MX Watchdog");
142 	return (0);
143 }
144 
145 static int
146 imx_wdog_attach(device_t dev)
147 {
148 	struct imx_wdog_softc *sc;
149 
150 	sc = device_get_softc(dev);
151 	sc->sc_dev = dev;
152 
153 	if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) {
154 		device_printf(dev, "could not allocate resources\n");
155 		return (ENXIO);
156 	}
157 
158 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF);
159 
160 	/* TODO: handle interrupt */
161 
162 	EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0);
163 	return (0);
164 }
165 
166 static device_method_t imx_wdog_methods[] = {
167 	DEVMETHOD(device_probe,		imx_wdog_probe),
168 	DEVMETHOD(device_attach,	imx_wdog_attach),
169 	DEVMETHOD_END
170 };
171 
172 static driver_t imx_wdog_driver = {
173 	"imx_wdog",
174 	imx_wdog_methods,
175 	sizeof(struct imx_wdog_softc),
176 };
177 
178 static devclass_t imx_wdog_devclass;
179 
180 DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, imx_wdog_devclass, 0, 0);
181