xref: /freebsd/sys/dev/fdt/simplebus.c (revision dabf006a638fdc44cdcf69731de8ac83959db731)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/rman.h>
36 
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 
41 #include <dev/fdt/simplebus.h>
42 
43 /*
44  * Bus interface.
45  */
46 static int		simplebus_probe(device_t dev);
47 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
48     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
49 static void		simplebus_probe_nomatch(device_t bus, device_t child);
50 static int		simplebus_print_child(device_t bus, device_t child);
51 static device_t		simplebus_add_child(device_t dev, u_int order,
52     const char *name, int unit);
53 static struct resource_list *simplebus_get_resource_list(device_t bus,
54     device_t child);
55 
56 static ssize_t		simplebus_get_property(device_t bus, device_t child,
57     const char *propname, void *propvalue, size_t size,
58     device_property_type_t type);
59 /*
60  * ofw_bus interface
61  */
62 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
63     device_t child);
64 
65 /*
66  * Driver methods.
67  */
68 static device_method_t	simplebus_methods[] = {
69 	/* Device interface */
70 	DEVMETHOD(device_probe,		simplebus_probe),
71 	DEVMETHOD(device_attach,	simplebus_attach),
72 	DEVMETHOD(device_detach,	simplebus_detach),
73 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
74 	DEVMETHOD(device_suspend,	bus_generic_suspend),
75 	DEVMETHOD(device_resume,	bus_generic_resume),
76 
77 	/* Bus interface */
78 	DEVMETHOD(bus_add_child,	simplebus_add_child),
79 	DEVMETHOD(bus_print_child,	simplebus_print_child),
80 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
81 	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
82 	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
83 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
84 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
85 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
86 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
87 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
88 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
89 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
90 	DEVMETHOD(bus_map_resource,	bus_generic_map_resource),
91 	DEVMETHOD(bus_unmap_resource,	bus_generic_unmap_resource),
92 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
93 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
94 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
95 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
96 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
97 	DEVMETHOD(bus_get_property,	simplebus_get_property),
98 	DEVMETHOD(bus_get_device_path,  ofw_bus_gen_get_device_path),
99 
100 	/* ofw_bus interface */
101 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
102 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
103 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
104 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
105 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
106 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
107 
108 	DEVMETHOD_END
109 };
110 
111 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
112     sizeof(struct simplebus_softc));
113 
114 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, 0, 0, BUS_PASS_BUS);
115 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, 0, 0,
116     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 	bus_attach_children(dev);
186 	return (0);
187 }
188 
189 int
190 simplebus_detach(device_t dev)
191 {
192 	struct		simplebus_softc *sc;
193 	int	rv;
194 
195 	rv = bus_generic_detach(dev);
196 	if (rv != 0)
197 		return (rv);
198 
199 	sc = device_get_softc(dev);
200 	if (sc->ranges != NULL)
201 		free(sc->ranges, M_DEVBUF);
202 
203 	return (0);
204 }
205 
206 void
207 simplebus_init(device_t dev, phandle_t node)
208 {
209 	struct simplebus_softc *sc;
210 
211 	sc = device_get_softc(dev);
212 	if (node == 0)
213 		node = ofw_bus_get_node(dev);
214 	sc->dev = dev;
215 	sc->node = node;
216 
217 	/*
218 	 * Some important numbers
219 	 */
220 	sc->acells = 2;
221 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
222 	sc->scells = 1;
223 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
224 }
225 
226 int
227 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
228 {
229 	int host_address_cells;
230 	cell_t *base_ranges;
231 	ssize_t nbase_ranges;
232 	int err;
233 	int i, j, k;
234 
235 	err = OF_searchencprop(OF_parent(node), "#address-cells",
236 	    &host_address_cells, sizeof(host_address_cells));
237 	if (err <= 0)
238 		return (-1);
239 
240 	nbase_ranges = OF_getproplen(node, "ranges");
241 	if (nbase_ranges < 0)
242 		return (-1);
243 	sc->nranges = nbase_ranges / sizeof(cell_t) /
244 	    (sc->acells + host_address_cells + sc->scells);
245 	if (sc->nranges == 0)
246 		return (0);
247 
248 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
249 	    M_DEVBUF, M_WAITOK);
250 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
251 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
252 
253 	for (i = 0, j = 0; i < sc->nranges; i++) {
254 		sc->ranges[i].bus = 0;
255 		for (k = 0; k < sc->acells; k++) {
256 			sc->ranges[i].bus <<= 32;
257 			sc->ranges[i].bus |= base_ranges[j++];
258 		}
259 		sc->ranges[i].host = 0;
260 		for (k = 0; k < host_address_cells; k++) {
261 			sc->ranges[i].host <<= 32;
262 			sc->ranges[i].host |= base_ranges[j++];
263 		}
264 		sc->ranges[i].size = 0;
265 		for (k = 0; k < sc->scells; k++) {
266 			sc->ranges[i].size <<= 32;
267 			sc->ranges[i].size |= base_ranges[j++];
268 		}
269 	}
270 
271 	free(base_ranges, M_DEVBUF);
272 	return (sc->nranges);
273 }
274 
275 struct simplebus_devinfo *
276 simplebus_setup_dinfo(device_t dev, phandle_t node,
277     struct simplebus_devinfo *di)
278 {
279 	struct simplebus_softc *sc;
280 	struct simplebus_devinfo *ndi;
281 
282 	sc = device_get_softc(dev);
283 	if (di == NULL)
284 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
285 	else
286 		ndi = di;
287 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
288 		if (di == NULL)
289 			free(ndi, M_DEVBUF);
290 		return (NULL);
291 	}
292 
293 	resource_list_init(&ndi->rl);
294 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
295 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
296 
297 	return (ndi);
298 }
299 
300 device_t
301 simplebus_add_device(device_t dev, phandle_t node, u_int order,
302     const char *name, int unit, struct simplebus_devinfo *di)
303 {
304 	struct simplebus_devinfo *ndi;
305 	device_t cdev;
306 
307 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
308 		return (NULL);
309 	cdev = device_add_child_ordered(dev, order, name, unit);
310 	if (cdev == NULL) {
311 		device_printf(dev, "<%s>: device_add_child failed\n",
312 		    ndi->obdinfo.obd_name);
313 		resource_list_free(&ndi->rl);
314 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
315 		if (di == NULL)
316 			free(ndi, M_DEVBUF);
317 		return (NULL);
318 	}
319 	device_set_ivars(cdev, ndi);
320 
321 	return(cdev);
322 }
323 
324 static device_t
325 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
326 {
327 	device_t cdev;
328 	struct simplebus_devinfo *ndi;
329 
330 	cdev = device_add_child_ordered(dev, order, name, unit);
331 	if (cdev == NULL)
332 		return (NULL);
333 
334 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
335 	ndi->obdinfo.obd_node = -1;
336 	resource_list_init(&ndi->rl);
337 	device_set_ivars(cdev, ndi);
338 
339 	return (cdev);
340 }
341 
342 static const struct ofw_bus_devinfo *
343 simplebus_get_devinfo(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->obdinfo);
351 }
352 
353 static struct resource_list *
354 simplebus_get_resource_list(device_t bus __unused, device_t child)
355 {
356 	struct simplebus_devinfo *ndi;
357 
358 	ndi = device_get_ivars(child);
359 	if (ndi == NULL)
360 		return (NULL);
361 	return (&ndi->rl);
362 }
363 
364 static ssize_t
365 simplebus_get_property(device_t bus, device_t child, const char *propname,
366     void *propvalue, size_t size, device_property_type_t type)
367 {
368 	phandle_t node, xref;
369 	ssize_t ret, i;
370 	uint32_t *buffer;
371 	uint64_t val;
372 
373 	switch (type) {
374 	case DEVICE_PROP_ANY:
375 	case DEVICE_PROP_BUFFER:
376 	case DEVICE_PROP_UINT32:
377 	case DEVICE_PROP_UINT64:
378 	case DEVICE_PROP_HANDLE:
379 		break;
380 	default:
381 		return (-1);
382 	}
383 
384 	node = ofw_bus_get_node(child);
385 	if (propvalue == NULL || size == 0)
386 		return (OF_getproplen(node, propname));
387 
388 	/*
389 	 * Integer values are stored in BE format.
390 	 * If caller declared that the underlying property type is uint32_t
391 	 * we need to do the conversion to match host endianness.
392 	 */
393 	if (type == DEVICE_PROP_UINT32)
394 		return (OF_getencprop(node, propname, propvalue, size));
395 
396 	/*
397 	 * uint64_t also requires endianness handling.
398 	 * In FDT every 8 byte value is stored using two uint32_t variables
399 	 * in BE format. Now, since the upper bits are stored as the first
400 	 * of the pair, both halves require swapping.
401 	 */
402 	 if (type == DEVICE_PROP_UINT64) {
403 		ret = OF_getencprop(node, propname, propvalue, size);
404 		if (ret <= 0) {
405 			return (ret);
406 		}
407 
408 		buffer = (uint32_t *)propvalue;
409 
410 		for (i = 0; i < size / 4; i += 2) {
411 			val = (uint64_t)buffer[i] << 32 | buffer[i + 1];
412 			((uint64_t *)buffer)[i / 2] = val;
413 		}
414 		return (ret);
415 	}
416 
417 	if (type == DEVICE_PROP_HANDLE) {
418 		if (size < sizeof(node))
419 			return (-1);
420 		ret = OF_getencprop(node, propname, &xref, sizeof(xref));
421 		if (ret <= 0)
422 			return (ret);
423 
424 		node = OF_node_from_xref(xref);
425 		if (propvalue != NULL)
426 			*(uint32_t *)propvalue = node;
427 		return (ret);
428 	}
429 
430 	return (OF_getprop(node, propname, propvalue, size));
431 }
432 
433 static struct resource *
434 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
435     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
436 {
437 	struct simplebus_softc *sc;
438 	struct simplebus_devinfo *di;
439 	struct resource_list_entry *rle;
440 	int j;
441 
442 	sc = device_get_softc(bus);
443 
444 	/*
445 	 * Request for the default allocation with a given rid: use resource
446 	 * list stored in the local device info.
447 	 */
448 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
449 		if ((di = device_get_ivars(child)) == NULL)
450 			return (NULL);
451 
452 		rle = resource_list_find(&di->rl, type, *rid);
453 		if (rle == NULL) {
454 			if (bootverbose)
455 				device_printf(bus, "no default resources for "
456 				    "rid = %d, type = %d\n", *rid, type);
457 			return (NULL);
458 		}
459 		start = rle->start;
460 		end = rle->end;
461 		count = rle->count;
462         }
463 
464 	if (type == SYS_RES_IOPORT)
465 		type = SYS_RES_MEMORY;
466 
467 	if (type == SYS_RES_MEMORY) {
468 		/* Remap through ranges property */
469 		for (j = 0; j < sc->nranges; j++) {
470 			if (start >= sc->ranges[j].bus && end <
471 			    sc->ranges[j].bus + sc->ranges[j].size) {
472 				start -= sc->ranges[j].bus;
473 				start += sc->ranges[j].host;
474 				end -= sc->ranges[j].bus;
475 				end += sc->ranges[j].host;
476 				break;
477 			}
478 		}
479 		if (j == sc->nranges && sc->nranges != 0) {
480 			if (bootverbose)
481 				device_printf(bus, "Could not map resource "
482 				    "%#jx-%#jx\n", start, end);
483 
484 			return (NULL);
485 		}
486 	}
487 
488 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
489 	    count, flags));
490 }
491 
492 static int
493 simplebus_print_res(struct simplebus_devinfo *di)
494 {
495 	int rv;
496 
497 	if (di == NULL)
498 		return (0);
499 	rv = 0;
500 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
501 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
502 	return (rv);
503 }
504 
505 static void
506 simplebus_probe_nomatch(device_t bus, device_t child)
507 {
508 	const char *name, *type, *compat;
509 
510 	if (!bootverbose)
511 		return;
512 
513 	compat = ofw_bus_get_compat(child);
514 	if (compat == NULL)
515 		return;
516 	name = ofw_bus_get_name(child);
517 	type = ofw_bus_get_type(child);
518 
519 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
520 	simplebus_print_res(device_get_ivars(child));
521 	if (!ofw_bus_status_okay(child))
522 		printf(" disabled");
523 	if (type)
524 		printf(" type %s", type);
525 	printf(" compat %s (no driver attached)\n", compat);
526 }
527 
528 static int
529 simplebus_print_child(device_t bus, device_t child)
530 {
531 	int rv;
532 
533 	rv = bus_print_child_header(bus, child);
534 	rv += simplebus_print_res(device_get_ivars(child));
535 	if (!ofw_bus_status_okay(child))
536 		rv += printf(" disabled");
537 	rv += bus_print_child_footer(bus, child);
538 	return (rv);
539 }
540