2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
36 #include <sys/kernel.h>
39 #include <dev/ofw/openfirm.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/fdt/simplebus.h>
48 static int simplebus_probe(device_t dev);
49 static int simplebus_attach(device_t dev);
50 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
51 int *, rman_res_t, rman_res_t, rman_res_t, u_int);
52 static void simplebus_probe_nomatch(device_t bus, device_t child);
53 static int simplebus_print_child(device_t bus, device_t child);
54 static device_t simplebus_add_child(device_t dev, u_int order,
55 const char *name, int unit);
56 static struct resource_list *simplebus_get_resource_list(device_t bus,
61 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
68 static int simplebus_fill_ranges(phandle_t node,
69 struct simplebus_softc *sc);
74 static device_method_t simplebus_methods[] = {
75 /* Device interface */
76 DEVMETHOD(device_probe, simplebus_probe),
77 DEVMETHOD(device_attach, simplebus_attach),
78 DEVMETHOD(device_detach, bus_generic_detach),
79 DEVMETHOD(device_shutdown, bus_generic_shutdown),
80 DEVMETHOD(device_suspend, bus_generic_suspend),
81 DEVMETHOD(device_resume, bus_generic_resume),
84 DEVMETHOD(bus_add_child, simplebus_add_child),
85 DEVMETHOD(bus_print_child, simplebus_print_child),
86 DEVMETHOD(bus_probe_nomatch, simplebus_probe_nomatch),
87 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
88 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
89 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
90 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
91 DEVMETHOD(bus_alloc_resource, simplebus_alloc_resource),
92 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
93 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
94 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
95 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
96 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
97 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
98 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
99 DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
101 /* ofw_bus interface */
102 DEVMETHOD(ofw_bus_get_devinfo, simplebus_get_devinfo),
103 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
104 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
105 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
106 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
107 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
112 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
113 sizeof(struct simplebus_softc));
115 static devclass_t simplebus_devclass;
116 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
118 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
119 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
122 simplebus_probe(device_t dev)
125 if (!ofw_bus_status_okay(dev))
129 * FDT data puts a "simple-bus" compatible string on many things that
130 * have children but aren't really buses in our world. Without a
131 * ranges property we will fail to attach, so just fail to probe too.
133 if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
134 ofw_bus_has_prop(dev, "ranges")) &&
135 (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
139 device_set_desc(dev, "Flattened device tree simple bus");
141 return (BUS_PROBE_GENERIC);
145 simplebus_attach(device_t dev)
147 struct simplebus_softc *sc;
150 sc = device_get_softc(dev);
151 simplebus_init(dev, 0);
152 if (simplebus_fill_ranges(sc->node, sc) < 0) {
153 device_printf(dev, "could not get ranges\n");
158 * In principle, simplebus could have an interrupt map, but ignore that
162 for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
163 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
164 return (bus_generic_attach(dev));
168 simplebus_init(device_t dev, phandle_t node)
170 struct simplebus_softc *sc;
172 sc = device_get_softc(dev);
174 node = ofw_bus_get_node(dev);
179 * Some important numbers
182 OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
184 OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
188 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
190 int host_address_cells;
192 ssize_t nbase_ranges;
196 err = OF_searchencprop(OF_parent(node), "#address-cells",
197 &host_address_cells, sizeof(host_address_cells));
201 nbase_ranges = OF_getproplen(node, "ranges");
202 if (nbase_ranges < 0)
204 sc->nranges = nbase_ranges / sizeof(cell_t) /
205 (sc->acells + host_address_cells + sc->scells);
206 if (sc->nranges == 0)
209 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
211 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
212 OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
214 for (i = 0, j = 0; i < sc->nranges; i++) {
215 sc->ranges[i].bus = 0;
216 for (k = 0; k < sc->acells; k++) {
217 sc->ranges[i].bus <<= 32;
218 sc->ranges[i].bus |= base_ranges[j++];
220 sc->ranges[i].host = 0;
221 for (k = 0; k < host_address_cells; k++) {
222 sc->ranges[i].host <<= 32;
223 sc->ranges[i].host |= base_ranges[j++];
225 sc->ranges[i].size = 0;
226 for (k = 0; k < sc->scells; k++) {
227 sc->ranges[i].size <<= 32;
228 sc->ranges[i].size |= base_ranges[j++];
232 free(base_ranges, M_DEVBUF);
233 return (sc->nranges);
236 struct simplebus_devinfo *
237 simplebus_setup_dinfo(device_t dev, phandle_t node,
238 struct simplebus_devinfo *di)
240 struct simplebus_softc *sc;
241 struct simplebus_devinfo *ndi;
243 sc = device_get_softc(dev);
245 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
248 if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
254 resource_list_init(&ndi->rl);
255 ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
256 ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
262 simplebus_add_device(device_t dev, phandle_t node, u_int order,
263 const char *name, int unit, struct simplebus_devinfo *di)
265 struct simplebus_devinfo *ndi;
268 if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
270 cdev = device_add_child_ordered(dev, order, name, unit);
272 device_printf(dev, "<%s>: device_add_child failed\n",
273 ndi->obdinfo.obd_name);
274 resource_list_free(&ndi->rl);
275 ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
280 device_set_ivars(cdev, ndi);
286 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
289 struct simplebus_devinfo *ndi;
291 cdev = device_add_child_ordered(dev, order, name, unit);
295 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
296 ndi->obdinfo.obd_node = -1;
297 resource_list_init(&ndi->rl);
298 device_set_ivars(cdev, ndi);
303 static const struct ofw_bus_devinfo *
304 simplebus_get_devinfo(device_t bus __unused, device_t child)
306 struct simplebus_devinfo *ndi;
308 ndi = device_get_ivars(child);
311 return (&ndi->obdinfo);
314 static struct resource_list *
315 simplebus_get_resource_list(device_t bus __unused, device_t child)
317 struct simplebus_devinfo *ndi;
319 ndi = device_get_ivars(child);
325 static struct resource *
326 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
327 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
329 struct simplebus_softc *sc;
330 struct simplebus_devinfo *di;
331 struct resource_list_entry *rle;
334 sc = device_get_softc(bus);
337 * Request for the default allocation with a given rid: use resource
338 * list stored in the local device info.
340 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
341 if ((di = device_get_ivars(child)) == NULL)
344 if (type == SYS_RES_IOPORT)
345 type = SYS_RES_MEMORY;
347 rle = resource_list_find(&di->rl, type, *rid);
350 device_printf(bus, "no default resources for "
351 "rid = %d, type = %d\n", *rid, type);
359 if (type == SYS_RES_MEMORY) {
360 /* Remap through ranges property */
361 for (j = 0; j < sc->nranges; j++) {
362 if (start >= sc->ranges[j].bus && end <
363 sc->ranges[j].bus + sc->ranges[j].size) {
364 start -= sc->ranges[j].bus;
365 start += sc->ranges[j].host;
366 end -= sc->ranges[j].bus;
367 end += sc->ranges[j].host;
371 if (j == sc->nranges && sc->nranges != 0) {
373 device_printf(bus, "Could not map resource "
374 "%#jx-%#jx\n", start, end);
380 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
385 simplebus_print_res(struct simplebus_devinfo *di)
392 rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
393 rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
398 simplebus_probe_nomatch(device_t bus, device_t child)
400 const char *name, *type, *compat;
405 compat = ofw_bus_get_compat(child);
408 name = ofw_bus_get_name(child);
409 type = ofw_bus_get_type(child);
411 device_printf(bus, "<%s>", name != NULL ? name : "unknown");
412 simplebus_print_res(device_get_ivars(child));
413 if (!ofw_bus_status_okay(child))
416 printf(" type %s", type);
417 printf(" compat %s (no driver attached)\n", compat);
421 simplebus_print_child(device_t bus, device_t child)
425 rv = bus_print_child_header(bus, child);
426 rv += simplebus_print_res(device_get_ivars(child));
427 if (!ofw_bus_status_okay(child))
428 rv += printf(" disabled");
429 rv += bus_print_child_footer(bus, child);