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