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