xref: /freebsd/sys/dev/fdt/simple_mfd.c (revision dcc4d2939f789a6d1f272ffeab2068ba2b7525ea)
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/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/rman.h>
38 
39 #include <machine/bus.h>
40 
41 #include <dev/fdt/simplebus.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <dev/fdt/simple_mfd.h>
47 
48 device_t simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
49     const char *name, int unit, struct simplebus_devinfo *di);
50 struct simplebus_devinfo *simple_mfd_setup_dinfo(device_t dev, phandle_t node,
51     struct simplebus_devinfo *di);
52 
53 #include "syscon_if.h"
54 #include <dev/extres/syscon/syscon.h>
55 
56 MALLOC_DECLARE(M_SYSCON);
57 
58 static uint32_t simple_mfd_syscon_read_4(struct syscon *syscon,
59     bus_size_t offset);
60 static int simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset,
61     uint32_t val);
62 static int simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset,
63     uint32_t clear_bits, uint32_t set_bits);
64 
65 #define	SYSCON_LOCK(_sc)		mtx_lock_spin(&(_sc)->mtx)
66 #define	SYSCON_UNLOCK(_sc)		mtx_unlock_spin(&(_sc)->mtx)
67 #define	SYSCON_LOCK_INIT(_sc)		mtx_init(&(_sc)->mtx,	\
68     device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN)
69 #define	SYSCON_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->mtx);
70 #define	SYSCON_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_OWNED);
71 #define	SYSCON_ASSERT_UNLOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_NOTOWNED);
72 
73 static syscon_method_t simple_mfd_syscon_methods[] = {
74 	SYSCONMETHOD(syscon_unlocked_read_4,	simple_mfd_syscon_read_4),
75 	SYSCONMETHOD(syscon_unlocked_write_4,	simple_mfd_syscon_write_4),
76 	SYSCONMETHOD(syscon_unlocked_modify_4,	simple_mfd_syscon_modify_4),
77 
78 	SYSCONMETHOD_END
79 };
80 DEFINE_CLASS_1(simple_mfd_syscon, simple_mfd_syscon_class,
81     simple_mfd_syscon_methods, 0, syscon_class);
82 
83 static uint32_t
84 simple_mfd_syscon_read_4(struct syscon *syscon, bus_size_t offset)
85 {
86 	struct simple_mfd_softc *sc;
87 	uint32_t val;
88 
89 	sc = device_get_softc(syscon->pdev);
90 	SYSCON_ASSERT_LOCKED(sc);
91 	val = bus_read_4(sc->mem_res, offset);
92 	return (val);
93 }
94 
95 static int
96 simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset,
97     uint32_t val)
98 {
99 	struct simple_mfd_softc *sc;
100 
101 	sc = device_get_softc(syscon->pdev);
102 	SYSCON_ASSERT_LOCKED(sc);
103 	bus_write_4(sc->mem_res, offset, val);
104 	return (0);
105 }
106 
107 static int
108 simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset,
109     uint32_t clear_bits, uint32_t set_bits)
110 {
111 	struct simple_mfd_softc *sc;
112 	uint32_t val;
113 
114 	sc = device_get_softc(syscon->pdev);
115 	SYSCON_ASSERT_LOCKED(sc);
116 	val = bus_read_4(sc->mem_res, offset);
117 	val &= ~clear_bits;
118 	val |= set_bits;
119 	bus_write_4(sc->mem_res, offset, val);
120 	return (0);
121 }
122 
123 static int
124 simple_mfd_syscon_get_handle(device_t dev, struct syscon **syscon)
125 {
126 	struct simple_mfd_softc *sc;
127 
128 	sc = device_get_softc(dev);
129 	*syscon = sc->syscon;
130 	if (*syscon == NULL)
131 		return (ENODEV);
132 	return (0);
133 }
134 
135 static void
136 simple_mfd_syscon_lock(device_t dev)
137 {
138 	struct simple_mfd_softc *sc;
139 
140 	sc = device_get_softc(dev);
141 	SYSCON_LOCK(sc);
142 }
143 
144 static void
145 simple_mfd_syscon_unlock(device_t dev)
146 {
147 	struct simple_mfd_softc *sc;
148 
149 	sc = device_get_softc(dev);
150 	SYSCON_UNLOCK(sc);
151 }
152 
153 static int
154 simple_mfd_probe(device_t dev)
155 {
156 
157 	if (!ofw_bus_status_okay(dev))
158 		return (ENXIO);
159 	if (!ofw_bus_is_compatible(dev, "simple-mfd"))
160 		return (ENXIO);
161 
162 	device_set_desc(dev, "Simple MFD (Multi-Functions Device)");
163 
164 	return (BUS_PROBE_GENERIC);
165 }
166 
167 static int
168 simple_mfd_attach(device_t dev)
169 {
170 	struct simple_mfd_softc *sc;
171 	phandle_t node, child;
172 	device_t cdev;
173 	int rid;
174 
175 	sc = device_get_softc(dev);
176 	node = ofw_bus_get_node(dev);
177 
178 	sc->dev = dev;
179 	rid = 0;
180 
181 	/* Parse address-cells and size-cells from the parent node as a fallback */
182 	if (OF_getencprop(node, "#address-cells", &sc->sc.acells,
183 	    sizeof(sc->sc.acells)) == -1) {
184 		if (OF_getencprop(OF_parent(node), "#address-cells", &sc->sc.acells,
185 		    sizeof(sc->sc.acells)) == -1) {
186 			sc->sc.acells = 2;
187 		}
188 	}
189 	if (OF_getencprop(node, "#size-cells", &sc->sc.scells,
190 	    sizeof(sc->sc.scells)) == -1) {
191 		if (OF_getencprop(OF_parent(node), "#size-cells", &sc->sc.scells,
192 		    sizeof(sc->sc.scells)) == -1) {
193 			sc->sc.scells = 1;
194 		}
195 	}
196 
197 	/* If the node has a ranges prop, parse it so children mapping will be done correctly */
198 	if (OF_hasprop(node, "ranges")) {
199 		if (simplebus_fill_ranges(node, &sc->sc) < 0) {
200 			device_printf(dev, "could not get ranges\n");
201 			return (ENXIO);
202 		}
203 	}
204 
205 	/* Attach child devices */
206 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
207 		cdev = simple_mfd_add_device(dev, child, 0, NULL, -1, NULL);
208 		if (cdev != NULL)
209 			device_probe_and_attach(cdev);
210 	}
211 
212 	if (ofw_bus_is_compatible(dev, "syscon")) {
213 		sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
214 		    RF_ACTIVE);
215 		if (sc->mem_res == NULL) {
216 			device_printf(dev,
217 			    "Cannot allocate memory resource\n");
218 			return (ENXIO);
219 		}
220 
221 		SYSCON_LOCK_INIT(sc);
222 		sc->syscon = syscon_create_ofw_node(dev,
223 		    &simple_mfd_syscon_class, ofw_bus_get_node(dev));
224 		if (sc->syscon == NULL) {
225 			device_printf(dev,
226 			    "Failed to create/register syscon\n");
227 			return (ENXIO);
228 		}
229 	}
230 	return (bus_generic_attach(dev));
231 }
232 
233 static int
234 simple_mfd_detach(device_t dev)
235 {
236 	struct simple_mfd_softc *sc;
237 
238 	sc = device_get_softc(dev);
239 	if (ofw_bus_is_compatible(dev, "syscon")) {
240 		if (sc->syscon != NULL) {
241 			syscon_unregister(sc->syscon);
242 			free(sc->syscon, M_SYSCON);
243 		}
244 
245 		SYSCON_LOCK_DESTROY(sc);
246 
247 		if (sc->mem_res != NULL)
248 			bus_release_resource(dev, SYS_RES_MEMORY, 0,
249 			    sc->mem_res);
250 	}
251 	return (0);
252 }
253 
254 struct simplebus_devinfo *
255 simple_mfd_setup_dinfo(device_t dev, phandle_t node,
256     struct simplebus_devinfo *di)
257 {
258 	struct simplebus_softc *sc;
259 	struct simplebus_devinfo *ndi;
260 
261 	sc = device_get_softc(dev);
262 	if (di == NULL)
263 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
264 	else
265 		ndi = di;
266 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
267 		if (di == NULL)
268 			free(ndi, M_DEVBUF);
269 		return (NULL);
270 	}
271 
272 	/* reg resources is from the parent but interrupts is on the node itself */
273 	resource_list_init(&ndi->rl);
274 	ofw_bus_reg_to_rl(dev, OF_parent(node), sc->acells, sc->scells, &ndi->rl);
275 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
276 
277 	return (ndi);
278 }
279 
280 device_t
281 simple_mfd_add_device(device_t dev, phandle_t node, u_int order,
282     const char *name, int unit, struct simplebus_devinfo *di)
283 {
284 	struct simplebus_devinfo *ndi;
285 	device_t cdev;
286 
287 	if ((ndi = simple_mfd_setup_dinfo(dev, node, di)) == NULL)
288 		return (NULL);
289 	cdev = device_add_child_ordered(dev, order, name, unit);
290 	if (cdev == NULL) {
291 		device_printf(dev, "<%s>: device_add_child failed\n",
292 		    ndi->obdinfo.obd_name);
293 		resource_list_free(&ndi->rl);
294 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
295 		if (di == NULL)
296 			free(ndi, M_DEVBUF);
297 		return (NULL);
298 	}
299 	device_set_ivars(cdev, ndi);
300 
301 	return(cdev);
302 }
303 
304 static device_method_t simple_mfd_methods[] = {
305 	/* syscon interface */
306 	DEVMETHOD(syscon_get_handle,	simple_mfd_syscon_get_handle),
307 	DEVMETHOD(syscon_device_lock,	simple_mfd_syscon_lock),
308 	DEVMETHOD(syscon_device_unlock,	simple_mfd_syscon_unlock),
309 
310 	/* Device interface */
311 	DEVMETHOD(device_probe,		simple_mfd_probe),
312 	DEVMETHOD(device_attach,	simple_mfd_attach),
313 	DEVMETHOD(device_detach,	simple_mfd_detach),
314 
315 	DEVMETHOD_END
316 };
317 
318 DEFINE_CLASS_1(simple_mfd, simple_mfd_driver, simple_mfd_methods,
319   sizeof(struct simple_mfd_softc), simplebus_driver);
320 
321 EARLY_DRIVER_MODULE(simple_mfd, simplebus, simple_mfd_driver, 0, 0,
322     BUS_PASS_BUS + BUS_PASS_ORDER_LATE);
323 MODULE_VERSION(simple_mfd, 1);
324