xref: /freebsd/sys/dev/fdt/simplebus.c (revision b3b50e64d7e4c28bd0fd6323591ed811633826e4)
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 static ssize_t		simplebus_get_property(device_t bus, device_t child,
59     const char *propname, void *propvalue, size_t size,
60     device_property_type_t type);
61 /*
62  * ofw_bus interface
63  */
64 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
65     device_t child);
66 
67 /*
68  * Driver methods.
69  */
70 static device_method_t	simplebus_methods[] = {
71 	/* Device interface */
72 	DEVMETHOD(device_probe,		simplebus_probe),
73 	DEVMETHOD(device_attach,	simplebus_attach),
74 	DEVMETHOD(device_detach,	simplebus_detach),
75 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
76 	DEVMETHOD(device_suspend,	bus_generic_suspend),
77 	DEVMETHOD(device_resume,	bus_generic_resume),
78 
79 	/* Bus interface */
80 	DEVMETHOD(bus_add_child,	simplebus_add_child),
81 	DEVMETHOD(bus_print_child,	simplebus_print_child),
82 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
83 	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
84 	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
85 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
86 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
87 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
88 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
89 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
90 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
91 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
92 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
93 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
94 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
95 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
96 	DEVMETHOD(bus_get_property,	simplebus_get_property),
97 
98 	/* ofw_bus interface */
99 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
100 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
101 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
102 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
103 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
104 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
105 
106 	DEVMETHOD_END
107 };
108 
109 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
110     sizeof(struct simplebus_softc));
111 
112 static devclass_t simplebus_devclass;
113 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
114     0, 0, BUS_PASS_BUS);
115 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
116     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
117 
118 static int
119 simplebus_probe(device_t dev)
120 {
121 
122 	if (!ofw_bus_status_okay(dev))
123 		return (ENXIO);
124 	/*
125 	 * XXX We should attach only to pure' compatible = "simple-bus"',
126 	 * without any other compatible string.
127 	 * For now, filter only know cases:
128 	 * "syscon", "simple-bus"; is handled by fdt/syscon driver
129 	 * "simple-mfd", "simple-bus"; is handled by fdt/simple-mfd driver
130 	 */
131 	if (ofw_bus_is_compatible(dev, "syscon") ||
132 	    ofw_bus_is_compatible(dev, "simple-mfd"))
133 		return (ENXIO);
134 
135 	/*
136 	 * FDT data puts a "simple-bus" compatible string on many things that
137 	 * have children but aren't really buses in our world.  Without a
138 	 * ranges property we will fail to attach, so just fail to probe too.
139 	 */
140 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
141 	    ofw_bus_has_prop(dev, "ranges")) &&
142 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
143 	     "soc") != 0))
144 		return (ENXIO);
145 
146 	device_set_desc(dev, "Flattened device tree simple bus");
147 
148 	return (BUS_PROBE_GENERIC);
149 }
150 
151 int
152 simplebus_attach_impl(device_t dev)
153 {
154 	struct		simplebus_softc *sc;
155 	phandle_t	node;
156 
157 	sc = device_get_softc(dev);
158 	simplebus_init(dev, 0);
159 	if ((sc->flags & SB_FLAG_NO_RANGES) == 0 &&
160 	    simplebus_fill_ranges(sc->node, sc) < 0) {
161 		device_printf(dev, "could not get ranges\n");
162 		return (ENXIO);
163 	}
164 
165 	/*
166 	 * In principle, simplebus could have an interrupt map, but ignore that
167 	 * for now
168 	 */
169 
170 	for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
171 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
172 
173 	return (0);
174 }
175 
176 int
177 simplebus_attach(device_t dev)
178 {
179 	int	rv;
180 
181 	rv = simplebus_attach_impl(dev);
182 	if (rv != 0)
183 		return (rv);
184 
185 	return (bus_generic_attach(dev));
186 }
187 
188 int
189 simplebus_detach(device_t dev)
190 {
191 	struct		simplebus_softc *sc;
192 
193 	sc = device_get_softc(dev);
194 	if (sc->ranges != NULL)
195 		free(sc->ranges, M_DEVBUF);
196 
197 	return (bus_generic_detach(dev));
198 }
199 
200 void
201 simplebus_init(device_t dev, phandle_t node)
202 {
203 	struct simplebus_softc *sc;
204 
205 	sc = device_get_softc(dev);
206 	if (node == 0)
207 		node = ofw_bus_get_node(dev);
208 	sc->dev = dev;
209 	sc->node = node;
210 
211 	/*
212 	 * Some important numbers
213 	 */
214 	sc->acells = 2;
215 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
216 	sc->scells = 1;
217 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
218 }
219 
220 int
221 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
222 {
223 	int host_address_cells;
224 	cell_t *base_ranges;
225 	ssize_t nbase_ranges;
226 	int err;
227 	int i, j, k;
228 
229 	err = OF_searchencprop(OF_parent(node), "#address-cells",
230 	    &host_address_cells, sizeof(host_address_cells));
231 	if (err <= 0)
232 		return (-1);
233 
234 	nbase_ranges = OF_getproplen(node, "ranges");
235 	if (nbase_ranges < 0)
236 		return (-1);
237 	sc->nranges = nbase_ranges / sizeof(cell_t) /
238 	    (sc->acells + host_address_cells + sc->scells);
239 	if (sc->nranges == 0)
240 		return (0);
241 
242 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
243 	    M_DEVBUF, M_WAITOK);
244 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
245 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
246 
247 	for (i = 0, j = 0; i < sc->nranges; i++) {
248 		sc->ranges[i].bus = 0;
249 		for (k = 0; k < sc->acells; k++) {
250 			sc->ranges[i].bus <<= 32;
251 			sc->ranges[i].bus |= base_ranges[j++];
252 		}
253 		sc->ranges[i].host = 0;
254 		for (k = 0; k < host_address_cells; k++) {
255 			sc->ranges[i].host <<= 32;
256 			sc->ranges[i].host |= base_ranges[j++];
257 		}
258 		sc->ranges[i].size = 0;
259 		for (k = 0; k < sc->scells; k++) {
260 			sc->ranges[i].size <<= 32;
261 			sc->ranges[i].size |= base_ranges[j++];
262 		}
263 	}
264 
265 	free(base_ranges, M_DEVBUF);
266 	return (sc->nranges);
267 }
268 
269 struct simplebus_devinfo *
270 simplebus_setup_dinfo(device_t dev, phandle_t node,
271     struct simplebus_devinfo *di)
272 {
273 	struct simplebus_softc *sc;
274 	struct simplebus_devinfo *ndi;
275 
276 	sc = device_get_softc(dev);
277 	if (di == NULL)
278 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
279 	else
280 		ndi = di;
281 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
282 		if (di == NULL)
283 			free(ndi, M_DEVBUF);
284 		return (NULL);
285 	}
286 
287 	resource_list_init(&ndi->rl);
288 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
289 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
290 
291 	return (ndi);
292 }
293 
294 device_t
295 simplebus_add_device(device_t dev, phandle_t node, u_int order,
296     const char *name, int unit, struct simplebus_devinfo *di)
297 {
298 	struct simplebus_devinfo *ndi;
299 	device_t cdev;
300 
301 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
302 		return (NULL);
303 	cdev = device_add_child_ordered(dev, order, name, unit);
304 	if (cdev == NULL) {
305 		device_printf(dev, "<%s>: device_add_child failed\n",
306 		    ndi->obdinfo.obd_name);
307 		resource_list_free(&ndi->rl);
308 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
309 		if (di == NULL)
310 			free(ndi, M_DEVBUF);
311 		return (NULL);
312 	}
313 	device_set_ivars(cdev, ndi);
314 
315 	return(cdev);
316 }
317 
318 static device_t
319 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
320 {
321 	device_t cdev;
322 	struct simplebus_devinfo *ndi;
323 
324 	cdev = device_add_child_ordered(dev, order, name, unit);
325 	if (cdev == NULL)
326 		return (NULL);
327 
328 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
329 	ndi->obdinfo.obd_node = -1;
330 	resource_list_init(&ndi->rl);
331 	device_set_ivars(cdev, ndi);
332 
333 	return (cdev);
334 }
335 
336 static const struct ofw_bus_devinfo *
337 simplebus_get_devinfo(device_t bus __unused, device_t child)
338 {
339         struct simplebus_devinfo *ndi;
340 
341         ndi = device_get_ivars(child);
342 	if (ndi == NULL)
343 		return (NULL);
344         return (&ndi->obdinfo);
345 }
346 
347 static struct resource_list *
348 simplebus_get_resource_list(device_t bus __unused, device_t child)
349 {
350 	struct simplebus_devinfo *ndi;
351 
352 	ndi = device_get_ivars(child);
353 	if (ndi == NULL)
354 		return (NULL);
355 	return (&ndi->rl);
356 }
357 
358 static ssize_t
359 simplebus_get_property(device_t bus, device_t child, const char *propname,
360     void *propvalue, size_t size, device_property_type_t type)
361 {
362 	phandle_t node = ofw_bus_get_node(child);
363 	ssize_t ret, i;
364 	uint32_t *buffer;
365 	uint64_t val;
366 
367 	switch (type) {
368 	case DEVICE_PROP_ANY:
369 	case DEVICE_PROP_BUFFER:
370 	case DEVICE_PROP_UINT32:
371 	case DEVICE_PROP_UINT64:
372 		break;
373 	default:
374 		return (-1);
375 	}
376 
377 	if (propvalue == NULL || size == 0)
378 		return (OF_getproplen(node, propname));
379 
380 	/*
381 	 * Integer values are stored in BE format.
382 	 * If caller declared that the underlying property type is uint32_t
383 	 * we need to do the conversion to match host endianness.
384 	 */
385 	if (type == DEVICE_PROP_UINT32)
386 		return (OF_getencprop(node, propname, propvalue, size));
387 
388 	/*
389 	 * uint64_t also requires endianness handling.
390 	 * In FDT every 8 byte value is stored using two uint32_t variables
391 	 * in BE format. Now, since the upper bits are stored as the first
392 	 * of the pair, both halves require swapping.
393 	 */
394 	 if (type == DEVICE_PROP_UINT64) {
395 		ret = OF_getencprop(node, propname, propvalue, size);
396 		if (ret <= 0) {
397 			return (ret);
398 		}
399 
400 		buffer = (uint32_t *)propvalue;
401 
402 		for (i = 0; i < size / 4; i += 2) {
403 			val = (uint64_t)buffer[i] << 32 | buffer[i + 1];
404 			((uint64_t *)buffer)[i / 2] = val;
405 		}
406 		return (ret);
407 	 }
408 
409 	return (OF_getprop(node, propname, propvalue, size));
410 }
411 
412 static struct resource *
413 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
414     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
415 {
416 	struct simplebus_softc *sc;
417 	struct simplebus_devinfo *di;
418 	struct resource_list_entry *rle;
419 	int j;
420 
421 	sc = device_get_softc(bus);
422 
423 	/*
424 	 * Request for the default allocation with a given rid: use resource
425 	 * list stored in the local device info.
426 	 */
427 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
428 		if ((di = device_get_ivars(child)) == NULL)
429 			return (NULL);
430 
431 		if (type == SYS_RES_IOPORT)
432 			type = SYS_RES_MEMORY;
433 
434 		rle = resource_list_find(&di->rl, type, *rid);
435 		if (rle == NULL) {
436 			if (bootverbose)
437 				device_printf(bus, "no default resources for "
438 				    "rid = %d, type = %d\n", *rid, type);
439 			return (NULL);
440 		}
441 		start = rle->start;
442 		end = rle->end;
443 		count = rle->count;
444         }
445 
446 	if (type == SYS_RES_MEMORY) {
447 		/* Remap through ranges property */
448 		for (j = 0; j < sc->nranges; j++) {
449 			if (start >= sc->ranges[j].bus && end <
450 			    sc->ranges[j].bus + sc->ranges[j].size) {
451 				start -= sc->ranges[j].bus;
452 				start += sc->ranges[j].host;
453 				end -= sc->ranges[j].bus;
454 				end += sc->ranges[j].host;
455 				break;
456 			}
457 		}
458 		if (j == sc->nranges && sc->nranges != 0) {
459 			if (bootverbose)
460 				device_printf(bus, "Could not map resource "
461 				    "%#jx-%#jx\n", start, end);
462 
463 			return (NULL);
464 		}
465 	}
466 
467 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
468 	    count, flags));
469 }
470 
471 static int
472 simplebus_print_res(struct simplebus_devinfo *di)
473 {
474 	int rv;
475 
476 	if (di == NULL)
477 		return (0);
478 	rv = 0;
479 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
480 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
481 	return (rv);
482 }
483 
484 static void
485 simplebus_probe_nomatch(device_t bus, device_t child)
486 {
487 	const char *name, *type, *compat;
488 
489 	if (!bootverbose)
490 		return;
491 
492 	compat = ofw_bus_get_compat(child);
493 	if (compat == NULL)
494 		return;
495 	name = ofw_bus_get_name(child);
496 	type = ofw_bus_get_type(child);
497 
498 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
499 	simplebus_print_res(device_get_ivars(child));
500 	if (!ofw_bus_status_okay(child))
501 		printf(" disabled");
502 	if (type)
503 		printf(" type %s", type);
504 	printf(" compat %s (no driver attached)\n", compat);
505 }
506 
507 static int
508 simplebus_print_child(device_t bus, device_t child)
509 {
510 	int rv;
511 
512 	rv = bus_print_child_header(bus, child);
513 	rv += simplebus_print_res(device_get_ivars(child));
514 	if (!ofw_bus_status_okay(child))
515 		rv += printf(" disabled");
516 	rv += bus_print_child_footer(bus, child);
517 	return (rv);
518 }
519