xref: /freebsd/sys/dev/fdt/simple_mfd.c (revision 725a9f47324d42037db93c27ceb40d4956872f3e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/rman.h>
37 
38 #include <machine/bus.h>
39 
40 #include <dev/fdt/simplebus.h>
41 
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 #include <dev/fdt/simple_mfd.h>
46 
47 device_t simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
48     const char *name, int unit, struct simplebus_devinfo *di);
49 struct simplebus_devinfo *simple_mfd_setup_dinfo(device_t dev, phandle_t node,
50     struct simplebus_devinfo *di);
51 
52 #include "syscon_if.h"
53 #include <dev/syscon/syscon.h>
54 
55 MALLOC_DECLARE(M_SYSCON);
56 
57 static uint32_t simple_mfd_syscon_read_4(struct syscon *syscon,
58     bus_size_t offset);
59 static int simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset,
60     uint32_t val);
61 static int simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset,
62     uint32_t clear_bits, uint32_t set_bits);
63 
64 #define	SYSCON_LOCK(_sc)		mtx_lock_spin(&(_sc)->mtx)
65 #define	SYSCON_UNLOCK(_sc)		mtx_unlock_spin(&(_sc)->mtx)
66 #define	SYSCON_LOCK_INIT(_sc)		mtx_init(&(_sc)->mtx,	\
67     device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN)
68 #define	SYSCON_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->mtx);
69 #define	SYSCON_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_OWNED);
70 #define	SYSCON_ASSERT_UNLOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_NOTOWNED);
71 
72 static syscon_method_t simple_mfd_syscon_methods[] = {
73 	SYSCONMETHOD(syscon_unlocked_read_4,	simple_mfd_syscon_read_4),
74 	SYSCONMETHOD(syscon_unlocked_write_4,	simple_mfd_syscon_write_4),
75 	SYSCONMETHOD(syscon_unlocked_modify_4,	simple_mfd_syscon_modify_4),
76 
77 	SYSCONMETHOD_END
78 };
79 DEFINE_CLASS_1(simple_mfd_syscon, simple_mfd_syscon_class,
80     simple_mfd_syscon_methods, 0, syscon_class);
81 
82 static uint32_t
83 simple_mfd_syscon_read_4(struct syscon *syscon, bus_size_t offset)
84 {
85 	struct simple_mfd_softc *sc;
86 	uint32_t val;
87 
88 	sc = device_get_softc(syscon->pdev);
89 	SYSCON_ASSERT_LOCKED(sc);
90 	val = bus_read_4(sc->mem_res, offset);
91 	return (val);
92 }
93 
94 static int
95 simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset,
96     uint32_t val)
97 {
98 	struct simple_mfd_softc *sc;
99 
100 	sc = device_get_softc(syscon->pdev);
101 	SYSCON_ASSERT_LOCKED(sc);
102 	bus_write_4(sc->mem_res, offset, val);
103 	return (0);
104 }
105 
106 static int
107 simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset,
108     uint32_t clear_bits, uint32_t set_bits)
109 {
110 	struct simple_mfd_softc *sc;
111 	uint32_t val;
112 
113 	sc = device_get_softc(syscon->pdev);
114 	SYSCON_ASSERT_LOCKED(sc);
115 	val = bus_read_4(sc->mem_res, offset);
116 	val &= ~clear_bits;
117 	val |= set_bits;
118 	bus_write_4(sc->mem_res, offset, val);
119 	return (0);
120 }
121 
122 static int
123 simple_mfd_syscon_get_handle(device_t dev, struct syscon **syscon)
124 {
125 	struct simple_mfd_softc *sc;
126 
127 	sc = device_get_softc(dev);
128 	*syscon = sc->syscon;
129 	if (*syscon == NULL)
130 		return (ENODEV);
131 	return (0);
132 }
133 
134 static void
135 simple_mfd_syscon_lock(device_t dev)
136 {
137 	struct simple_mfd_softc *sc;
138 
139 	sc = device_get_softc(dev);
140 	SYSCON_LOCK(sc);
141 }
142 
143 static void
144 simple_mfd_syscon_unlock(device_t dev)
145 {
146 	struct simple_mfd_softc *sc;
147 
148 	sc = device_get_softc(dev);
149 	SYSCON_UNLOCK(sc);
150 }
151 
152 static int
153 simple_mfd_probe(device_t dev)
154 {
155 
156 	if (!ofw_bus_status_okay(dev))
157 		return (ENXIO);
158 	if (!ofw_bus_is_compatible(dev, "simple-mfd"))
159 		return (ENXIO);
160 
161 	device_set_desc(dev, "Simple MFD (Multi-Functions Device)");
162 
163 	return (BUS_PROBE_GENERIC);
164 }
165 
166 static int
167 simple_mfd_attach(device_t dev)
168 {
169 	struct simple_mfd_softc *sc;
170 	phandle_t node, child;
171 	int rid;
172 
173 	sc = device_get_softc(dev);
174 	node = ofw_bus_get_node(dev);
175 
176 	sc->dev = dev;
177 	rid = 0;
178 
179 	/* Parse address-cells and size-cells from the parent node as a fallback */
180 	if (OF_getencprop(node, "#address-cells", &sc->sc.acells,
181 	    sizeof(sc->sc.acells)) == -1) {
182 		if (OF_getencprop(OF_parent(node), "#address-cells", &sc->sc.acells,
183 		    sizeof(sc->sc.acells)) == -1) {
184 			sc->sc.acells = 2;
185 		}
186 	}
187 	if (OF_getencprop(node, "#size-cells", &sc->sc.scells,
188 	    sizeof(sc->sc.scells)) == -1) {
189 		if (OF_getencprop(OF_parent(node), "#size-cells", &sc->sc.scells,
190 		    sizeof(sc->sc.scells)) == -1) {
191 			sc->sc.scells = 1;
192 		}
193 	}
194 
195 	/* If the node has a ranges prop, parse it so children mapping will be done correctly */
196 	if (OF_hasprop(node, "ranges")) {
197 		if (simplebus_fill_ranges(node, &sc->sc) < 0) {
198 			device_printf(dev, "could not get ranges\n");
199 			return (ENXIO);
200 		}
201 	}
202 
203 	/* Attach child devices */
204 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
205 		(void)simple_mfd_add_device(dev, child, 0, NULL, -1, NULL);
206 	}
207 
208 	if (ofw_bus_is_compatible(dev, "syscon")) {
209 		sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
210 		    RF_ACTIVE);
211 		if (sc->mem_res == NULL) {
212 			device_printf(dev,
213 			    "Cannot allocate memory resource\n");
214 			return (ENXIO);
215 		}
216 
217 		SYSCON_LOCK_INIT(sc);
218 		sc->syscon = syscon_create_ofw_node(dev,
219 		    &simple_mfd_syscon_class, ofw_bus_get_node(dev));
220 		if (sc->syscon == NULL) {
221 			device_printf(dev,
222 			    "Failed to create/register syscon\n");
223 			return (ENXIO);
224 		}
225 	}
226 	return (bus_generic_attach(dev));
227 }
228 
229 static int
230 simple_mfd_detach(device_t dev)
231 {
232 	struct simple_mfd_softc *sc;
233 
234 	sc = device_get_softc(dev);
235 	if (ofw_bus_is_compatible(dev, "syscon")) {
236 		if (sc->syscon != NULL) {
237 			syscon_unregister(sc->syscon);
238 			free(sc->syscon, M_SYSCON);
239 		}
240 
241 		SYSCON_LOCK_DESTROY(sc);
242 
243 		if (sc->mem_res != NULL)
244 			bus_release_resource(dev, SYS_RES_MEMORY, 0,
245 			    sc->mem_res);
246 	}
247 	return (0);
248 }
249 
250 struct simplebus_devinfo *
251 simple_mfd_setup_dinfo(device_t dev, phandle_t node,
252     struct simplebus_devinfo *di)
253 {
254 	struct simplebus_softc *sc;
255 	struct simplebus_devinfo *ndi;
256 
257 	sc = device_get_softc(dev);
258 	if (di == NULL)
259 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
260 	else
261 		ndi = di;
262 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
263 		if (di == NULL)
264 			free(ndi, M_DEVBUF);
265 		return (NULL);
266 	}
267 
268 	/* reg resources is from the parent but interrupts is on the node itself */
269 	resource_list_init(&ndi->rl);
270 	ofw_bus_reg_to_rl(dev, OF_parent(node), sc->acells, sc->scells, &ndi->rl);
271 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
272 
273 	return (ndi);
274 }
275 
276 device_t
277 simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
278     const char *name, int unit, struct simplebus_devinfo *di)
279 {
280 	struct simplebus_devinfo *ndi;
281 	device_t cdev;
282 
283 	if ((ndi = simple_mfd_setup_dinfo(dev, node, di)) == NULL)
284 		return (NULL);
285 	cdev = device_add_child_ordered(dev, order, name, unit);
286 	if (cdev == NULL) {
287 		device_printf(dev, "<%s>: device_add_child failed\n",
288 		    ndi->obdinfo.obd_name);
289 		resource_list_free(&ndi->rl);
290 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
291 		if (di == NULL)
292 			free(ndi, M_DEVBUF);
293 		return (NULL);
294 	}
295 	device_set_ivars(cdev, ndi);
296 
297 	return(cdev);
298 }
299 
300 static device_method_t simple_mfd_methods[] = {
301 	/* syscon interface */
302 	DEVMETHOD(syscon_get_handle,	simple_mfd_syscon_get_handle),
303 	DEVMETHOD(syscon_device_lock,	simple_mfd_syscon_lock),
304 	DEVMETHOD(syscon_device_unlock,	simple_mfd_syscon_unlock),
305 
306 	/* Device interface */
307 	DEVMETHOD(device_probe,		simple_mfd_probe),
308 	DEVMETHOD(device_attach,	simple_mfd_attach),
309 	DEVMETHOD(device_detach,	simple_mfd_detach),
310 
311 	DEVMETHOD_END
312 };
313 
314 DEFINE_CLASS_1(simple_mfd, simple_mfd_driver, simple_mfd_methods,
315   sizeof(struct simple_mfd_softc), simplebus_driver);
316 
317 EARLY_DRIVER_MODULE(simple_mfd, simplebus, simple_mfd_driver, 0, 0,
318     BUS_PASS_BUS + BUS_PASS_ORDER_LATE);
319 MODULE_VERSION(simple_mfd, 1);
320