xref: /freebsd/sys/arm/freescale/imx/imx_wdog.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*-
2  * Copyright (c) 2012, 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Oleksandr Rybalko under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1.	Redistributions of source code must retain the above copyright
12  *	notice, this list of conditions and the following disclaimer.
13  * 2.	Redistributions in binary form must reproduce the above copyright
14  *	notice, this list of conditions and the following disclaimer in the
15  *	documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/time.h>
38 #include <sys/bus.h>
39 #include <sys/resource.h>
40 #include <sys/rman.h>
41 #include <sys/watchdog.h>
42 
43 #include <machine/bus.h>
44 #include <machine/intr.h>
45 
46 #include <dev/ofw/openfirm.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include <arm/freescale/imx/imx_wdogreg.h>
51 
52 struct imx_wdog_softc {
53 	struct mtx		sc_mtx;
54 	device_t		sc_dev;
55 	bus_space_tag_t		sc_bst;
56 	bus_space_handle_t	sc_bsh;
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 	{ -1, 0 }
65 };
66 
67 static struct ofw_compat_data compat_data[] = {
68 	{"fsl,imx6sx-wdt", 1},
69 	{"fsl,imx6sl-wdt", 1},
70 	{"fsl,imx6q-wdt",  1},
71 	{"fsl,imx53-wdt",  1},
72 	{"fsl,imx51-wdt",  1},
73 	{"fsl,imx50-wdt",  1},
74 	{"fsl,imx35-wdt",  1},
75 	{"fsl,imx27-wdt",  1},
76 	{"fsl,imx25-wdt",  1},
77 	{"fsl,imx21-wdt",  1},
78 	{NULL,             0}
79 };
80 
81 static void	imx_watchdog(void *, u_int, int *);
82 static int	imx_wdog_probe(device_t);
83 static int	imx_wdog_attach(device_t);
84 
85 static device_method_t imx_wdog_methods[] = {
86 	DEVMETHOD(device_probe,		imx_wdog_probe),
87 	DEVMETHOD(device_attach,	imx_wdog_attach),
88 	DEVMETHOD_END
89 };
90 
91 static driver_t imx_wdog_driver = {
92 	"imx_wdog",
93 	imx_wdog_methods,
94 	sizeof(struct imx_wdog_softc),
95 };
96 static devclass_t imx_wdog_devclass;
97 DRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, imx_wdog_devclass, 0, 0);
98 
99 #define	RD2(_sc, _r)							\
100 		bus_space_read_2((_sc)->sc_bst, (_sc)->sc_bsh, (_r))
101 #define	WR2(_sc, _r, _v)						\
102 		bus_space_write_2((_sc)->sc_bst, (_sc)->sc_bsh, (_r), (_v))
103 
104 static void
105 imx_watchdog(void *arg, u_int cmd, int *error)
106 {
107 	struct imx_wdog_softc *sc;
108 	uint16_t reg;
109 	u_int timeout;
110 
111 	sc = arg;
112 	mtx_lock(&sc->sc_mtx);
113 	if (cmd == 0) {
114 		if (bootverbose)
115 			device_printf(sc->sc_dev, "Can not be disabled.\n");
116 		*error = EOPNOTSUPP;
117 	} else {
118 		timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U);
119 		if (timeout > 1 && timeout < 128) {
120 			if (timeout != sc->sc_timeout) {
121 				sc->sc_timeout = timeout;
122 				reg = RD2(sc, WDOG_CR_REG);
123 				reg &= ~WDOG_CR_WT_MASK;
124 				reg |= (timeout << (WDOG_CR_WT_SHIFT + 1)) &
125 				    WDOG_CR_WT_MASK;
126 				WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE);
127 			}
128 			/* Refresh counter */
129 			WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1);
130 			WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2);
131 			*error = 0;
132 		}
133 	}
134 	mtx_unlock(&sc->sc_mtx);
135 }
136 
137 static int
138 imx_wdog_probe(device_t dev)
139 {
140 
141 	if (!ofw_bus_status_okay(dev))
142 		return (ENXIO);
143 
144 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
145 		return (ENXIO);
146 
147 	device_set_desc(dev, "Freescale i.MX Watchdog");
148 	return (0);
149 }
150 
151 static int
152 imx_wdog_attach(device_t dev)
153 {
154 	struct imx_wdog_softc *sc;
155 
156 	sc = device_get_softc(dev);
157 	sc->sc_dev = dev;
158 
159 	if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) {
160 		device_printf(dev, "could not allocate resources\n");
161 		return (ENXIO);
162 	}
163 
164 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF);
165 
166 	sc->sc_dev = dev;
167 	sc->sc_bst = rman_get_bustag(sc->sc_res[0]);
168 	sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]);
169 
170 	/* TODO: handle interrupt */
171 
172 	EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0);
173 	return (0);
174 }
175