xref: /freebsd/sys/dev/fdt/simplebus.c (revision c1d255d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/rman.h>
38 
39 #include <dev/ofw/openfirm.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 
43 #include <dev/fdt/simplebus.h>
44 
45 /*
46  * Bus interface.
47  */
48 static int		simplebus_probe(device_t dev);
49 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
50     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
51 static void		simplebus_probe_nomatch(device_t bus, device_t child);
52 static int		simplebus_print_child(device_t bus, device_t child);
53 static device_t		simplebus_add_child(device_t dev, u_int order,
54     const char *name, int unit);
55 static struct resource_list *simplebus_get_resource_list(device_t bus,
56     device_t child);
57 /*
58  * ofw_bus interface
59  */
60 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
61     device_t child);
62 
63 /*
64  * Driver methods.
65  */
66 static device_method_t	simplebus_methods[] = {
67 	/* Device interface */
68 	DEVMETHOD(device_probe,		simplebus_probe),
69 	DEVMETHOD(device_attach,	simplebus_attach),
70 	DEVMETHOD(device_detach,	simplebus_detach),
71 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
72 	DEVMETHOD(device_suspend,	bus_generic_suspend),
73 	DEVMETHOD(device_resume,	bus_generic_resume),
74 
75 	/* Bus interface */
76 	DEVMETHOD(bus_add_child,	simplebus_add_child),
77 	DEVMETHOD(bus_print_child,	simplebus_print_child),
78 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
79 	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
80 	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
81 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
82 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
83 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
84 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
85 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
86 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
87 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
88 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
89 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
90 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
91 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
92 
93 	/* ofw_bus interface */
94 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
95 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
96 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
97 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
98 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
99 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
100 
101 	DEVMETHOD_END
102 };
103 
104 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
105     sizeof(struct simplebus_softc));
106 
107 static devclass_t simplebus_devclass;
108 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
109     0, 0, BUS_PASS_BUS);
110 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
111     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
112 
113 static int
114 simplebus_probe(device_t dev)
115 {
116 
117 	if (!ofw_bus_status_okay(dev))
118 		return (ENXIO);
119 	/*
120 	 * XXX We should attach only to pure' compatible = "simple-bus"',
121 	 * without any other compatible string.
122 	 * For now, filter only know cases:
123 	 * "syscon", "simple-bus"; is handled by fdt/syscon driver
124 	 * "simple-mfd", "simple-bus"; is handled by fdt/simple-mfd driver
125 	 */
126 	if (ofw_bus_is_compatible(dev, "syscon") ||
127 	    ofw_bus_is_compatible(dev, "simple-mfd"))
128 		return (ENXIO);
129 
130 	/*
131 	 * FDT data puts a "simple-bus" compatible string on many things that
132 	 * have children but aren't really buses in our world.  Without a
133 	 * ranges property we will fail to attach, so just fail to probe too.
134 	 */
135 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
136 	    ofw_bus_has_prop(dev, "ranges")) &&
137 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
138 	     "soc") != 0))
139 		return (ENXIO);
140 
141 	device_set_desc(dev, "Flattened device tree simple bus");
142 
143 	return (BUS_PROBE_GENERIC);
144 }
145 
146 int
147 simplebus_attach_impl(device_t dev)
148 {
149 	struct		simplebus_softc *sc;
150 	phandle_t	node;
151 
152 	sc = device_get_softc(dev);
153 	simplebus_init(dev, 0);
154 	if ((sc->flags & SB_FLAG_NO_RANGES) == 0 &&
155 	    simplebus_fill_ranges(sc->node, sc) < 0) {
156 		device_printf(dev, "could not get ranges\n");
157 		return (ENXIO);
158 	}
159 
160 	/*
161 	 * In principle, simplebus could have an interrupt map, but ignore that
162 	 * for now
163 	 */
164 
165 	for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
166 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
167 
168 	return (0);
169 }
170 
171 int
172 simplebus_attach(device_t dev)
173 {
174 	int	rv;
175 
176 	rv = simplebus_attach_impl(dev);
177 	if (rv != 0)
178 		return (rv);
179 
180 	return (bus_generic_attach(dev));
181 }
182 
183 int
184 simplebus_detach(device_t dev)
185 {
186 	struct		simplebus_softc *sc;
187 
188 	sc = device_get_softc(dev);
189 	if (sc->ranges != NULL)
190 		free(sc->ranges, M_DEVBUF);
191 
192 	return (bus_generic_detach(dev));
193 }
194 
195 void
196 simplebus_init(device_t dev, phandle_t node)
197 {
198 	struct simplebus_softc *sc;
199 
200 	sc = device_get_softc(dev);
201 	if (node == 0)
202 		node = ofw_bus_get_node(dev);
203 	sc->dev = dev;
204 	sc->node = node;
205 
206 	/*
207 	 * Some important numbers
208 	 */
209 	sc->acells = 2;
210 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
211 	sc->scells = 1;
212 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
213 }
214 
215 int
216 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
217 {
218 	int host_address_cells;
219 	cell_t *base_ranges;
220 	ssize_t nbase_ranges;
221 	int err;
222 	int i, j, k;
223 
224 	err = OF_searchencprop(OF_parent(node), "#address-cells",
225 	    &host_address_cells, sizeof(host_address_cells));
226 	if (err <= 0)
227 		return (-1);
228 
229 	nbase_ranges = OF_getproplen(node, "ranges");
230 	if (nbase_ranges < 0)
231 		return (-1);
232 	sc->nranges = nbase_ranges / sizeof(cell_t) /
233 	    (sc->acells + host_address_cells + sc->scells);
234 	if (sc->nranges == 0)
235 		return (0);
236 
237 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
238 	    M_DEVBUF, M_WAITOK);
239 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
240 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
241 
242 	for (i = 0, j = 0; i < sc->nranges; i++) {
243 		sc->ranges[i].bus = 0;
244 		for (k = 0; k < sc->acells; k++) {
245 			sc->ranges[i].bus <<= 32;
246 			sc->ranges[i].bus |= base_ranges[j++];
247 		}
248 		sc->ranges[i].host = 0;
249 		for (k = 0; k < host_address_cells; k++) {
250 			sc->ranges[i].host <<= 32;
251 			sc->ranges[i].host |= base_ranges[j++];
252 		}
253 		sc->ranges[i].size = 0;
254 		for (k = 0; k < sc->scells; k++) {
255 			sc->ranges[i].size <<= 32;
256 			sc->ranges[i].size |= base_ranges[j++];
257 		}
258 	}
259 
260 	free(base_ranges, M_DEVBUF);
261 	return (sc->nranges);
262 }
263 
264 struct simplebus_devinfo *
265 simplebus_setup_dinfo(device_t dev, phandle_t node,
266     struct simplebus_devinfo *di)
267 {
268 	struct simplebus_softc *sc;
269 	struct simplebus_devinfo *ndi;
270 
271 	sc = device_get_softc(dev);
272 	if (di == NULL)
273 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
274 	else
275 		ndi = di;
276 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
277 		if (di == NULL)
278 			free(ndi, M_DEVBUF);
279 		return (NULL);
280 	}
281 
282 	resource_list_init(&ndi->rl);
283 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
284 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
285 
286 	return (ndi);
287 }
288 
289 device_t
290 simplebus_add_device(device_t dev, phandle_t node, u_int order,
291     const char *name, int unit, struct simplebus_devinfo *di)
292 {
293 	struct simplebus_devinfo *ndi;
294 	device_t cdev;
295 
296 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
297 		return (NULL);
298 	cdev = device_add_child_ordered(dev, order, name, unit);
299 	if (cdev == NULL) {
300 		device_printf(dev, "<%s>: device_add_child failed\n",
301 		    ndi->obdinfo.obd_name);
302 		resource_list_free(&ndi->rl);
303 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
304 		if (di == NULL)
305 			free(ndi, M_DEVBUF);
306 		return (NULL);
307 	}
308 	device_set_ivars(cdev, ndi);
309 
310 	return(cdev);
311 }
312 
313 static device_t
314 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
315 {
316 	device_t cdev;
317 	struct simplebus_devinfo *ndi;
318 
319 	cdev = device_add_child_ordered(dev, order, name, unit);
320 	if (cdev == NULL)
321 		return (NULL);
322 
323 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
324 	ndi->obdinfo.obd_node = -1;
325 	resource_list_init(&ndi->rl);
326 	device_set_ivars(cdev, ndi);
327 
328 	return (cdev);
329 }
330 
331 static const struct ofw_bus_devinfo *
332 simplebus_get_devinfo(device_t bus __unused, device_t child)
333 {
334         struct simplebus_devinfo *ndi;
335 
336         ndi = device_get_ivars(child);
337 	if (ndi == NULL)
338 		return (NULL);
339         return (&ndi->obdinfo);
340 }
341 
342 static struct resource_list *
343 simplebus_get_resource_list(device_t bus __unused, device_t child)
344 {
345 	struct simplebus_devinfo *ndi;
346 
347 	ndi = device_get_ivars(child);
348 	if (ndi == NULL)
349 		return (NULL);
350 	return (&ndi->rl);
351 }
352 
353 static struct resource *
354 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
355     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
356 {
357 	struct simplebus_softc *sc;
358 	struct simplebus_devinfo *di;
359 	struct resource_list_entry *rle;
360 	int j;
361 
362 	sc = device_get_softc(bus);
363 
364 	/*
365 	 * Request for the default allocation with a given rid: use resource
366 	 * list stored in the local device info.
367 	 */
368 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
369 		if ((di = device_get_ivars(child)) == NULL)
370 			return (NULL);
371 
372 		if (type == SYS_RES_IOPORT)
373 			type = SYS_RES_MEMORY;
374 
375 		rle = resource_list_find(&di->rl, type, *rid);
376 		if (rle == NULL) {
377 			if (bootverbose)
378 				device_printf(bus, "no default resources for "
379 				    "rid = %d, type = %d\n", *rid, type);
380 			return (NULL);
381 		}
382 		start = rle->start;
383 		end = rle->end;
384 		count = rle->count;
385         }
386 
387 	if (type == SYS_RES_MEMORY) {
388 		/* Remap through ranges property */
389 		for (j = 0; j < sc->nranges; j++) {
390 			if (start >= sc->ranges[j].bus && end <
391 			    sc->ranges[j].bus + sc->ranges[j].size) {
392 				start -= sc->ranges[j].bus;
393 				start += sc->ranges[j].host;
394 				end -= sc->ranges[j].bus;
395 				end += sc->ranges[j].host;
396 				break;
397 			}
398 		}
399 		if (j == sc->nranges && sc->nranges != 0) {
400 			if (bootverbose)
401 				device_printf(bus, "Could not map resource "
402 				    "%#jx-%#jx\n", start, end);
403 
404 			return (NULL);
405 		}
406 	}
407 
408 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
409 	    count, flags));
410 }
411 
412 static int
413 simplebus_print_res(struct simplebus_devinfo *di)
414 {
415 	int rv;
416 
417 	if (di == NULL)
418 		return (0);
419 	rv = 0;
420 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
421 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
422 	return (rv);
423 }
424 
425 static void
426 simplebus_probe_nomatch(device_t bus, device_t child)
427 {
428 	const char *name, *type, *compat;
429 
430 	if (!bootverbose)
431 		return;
432 
433 	compat = ofw_bus_get_compat(child);
434 	if (compat == NULL)
435 		return;
436 	name = ofw_bus_get_name(child);
437 	type = ofw_bus_get_type(child);
438 
439 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
440 	simplebus_print_res(device_get_ivars(child));
441 	if (!ofw_bus_status_okay(child))
442 		printf(" disabled");
443 	if (type)
444 		printf(" type %s", type);
445 	printf(" compat %s (no driver attached)\n", compat);
446 }
447 
448 static int
449 simplebus_print_child(device_t bus, device_t child)
450 {
451 	int rv;
452 
453 	rv = bus_print_child_header(bus, child);
454 	rv += simplebus_print_res(device_get_ivars(child));
455 	if (!ofw_bus_status_okay(child))
456 		rv += printf(" disabled");
457 	rv += bus_print_child_footer(bus, child);
458 	return (rv);
459 }
460