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,
67 static device_method_t simplebus_methods[] = {
68 /* Device interface */
69 DEVMETHOD(device_probe, simplebus_probe),
70 DEVMETHOD(device_attach, simplebus_attach),
71 DEVMETHOD(device_detach, bus_generic_detach),
72 DEVMETHOD(device_shutdown, bus_generic_shutdown),
73 DEVMETHOD(device_suspend, bus_generic_suspend),
74 DEVMETHOD(device_resume, bus_generic_resume),
77 DEVMETHOD(bus_add_child, simplebus_add_child),
78 DEVMETHOD(bus_print_child, simplebus_print_child),
79 DEVMETHOD(bus_probe_nomatch, simplebus_probe_nomatch),
80 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
81 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
82 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
83 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
84 DEVMETHOD(bus_alloc_resource, simplebus_alloc_resource),
85 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
86 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
87 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
88 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
89 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
90 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
91 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
92 DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
94 /* ofw_bus interface */
95 DEVMETHOD(ofw_bus_get_devinfo, simplebus_get_devinfo),
96 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
97 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
98 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
99 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
100 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
105 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
106 sizeof(struct simplebus_softc));
108 static devclass_t simplebus_devclass;
109 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
111 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
112 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
115 simplebus_probe(device_t dev)
118 if (!ofw_bus_status_okay(dev))
122 * FDT data puts a "simple-bus" compatible string on many things that
123 * have children but aren't really buses in our world. Without a
124 * ranges property we will fail to attach, so just fail to probe too.
126 if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
127 ofw_bus_has_prop(dev, "ranges")) &&
128 (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
132 device_set_desc(dev, "Flattened device tree simple bus");
134 return (BUS_PROBE_GENERIC);
138 simplebus_attach(device_t dev)
140 struct simplebus_softc *sc;
143 sc = device_get_softc(dev);
144 simplebus_init(dev, 0);
145 if (simplebus_fill_ranges(sc->node, sc) < 0) {
146 device_printf(dev, "could not get ranges\n");
151 * In principle, simplebus could have an interrupt map, but ignore that
155 for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
156 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
157 return (bus_generic_attach(dev));
161 simplebus_init(device_t dev, phandle_t node)
163 struct simplebus_softc *sc;
165 sc = device_get_softc(dev);
167 node = ofw_bus_get_node(dev);
172 * Some important numbers
175 OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
177 OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
181 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
183 int host_address_cells;
185 ssize_t nbase_ranges;
189 err = OF_searchencprop(OF_parent(node), "#address-cells",
190 &host_address_cells, sizeof(host_address_cells));
194 nbase_ranges = OF_getproplen(node, "ranges");
195 if (nbase_ranges < 0)
197 sc->nranges = nbase_ranges / sizeof(cell_t) /
198 (sc->acells + host_address_cells + sc->scells);
199 if (sc->nranges == 0)
202 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
204 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
205 OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
207 for (i = 0, j = 0; i < sc->nranges; i++) {
208 sc->ranges[i].bus = 0;
209 for (k = 0; k < sc->acells; k++) {
210 sc->ranges[i].bus <<= 32;
211 sc->ranges[i].bus |= base_ranges[j++];
213 sc->ranges[i].host = 0;
214 for (k = 0; k < host_address_cells; k++) {
215 sc->ranges[i].host <<= 32;
216 sc->ranges[i].host |= base_ranges[j++];
218 sc->ranges[i].size = 0;
219 for (k = 0; k < sc->scells; k++) {
220 sc->ranges[i].size <<= 32;
221 sc->ranges[i].size |= base_ranges[j++];
225 free(base_ranges, M_DEVBUF);
226 return (sc->nranges);
229 struct simplebus_devinfo *
230 simplebus_setup_dinfo(device_t dev, phandle_t node,
231 struct simplebus_devinfo *di)
233 struct simplebus_softc *sc;
234 struct simplebus_devinfo *ndi;
236 sc = device_get_softc(dev);
238 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
241 if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
247 resource_list_init(&ndi->rl);
248 ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
249 ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
255 simplebus_add_device(device_t dev, phandle_t node, u_int order,
256 const char *name, int unit, struct simplebus_devinfo *di)
258 struct simplebus_devinfo *ndi;
261 if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
263 cdev = device_add_child_ordered(dev, order, name, unit);
265 device_printf(dev, "<%s>: device_add_child failed\n",
266 ndi->obdinfo.obd_name);
267 resource_list_free(&ndi->rl);
268 ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
273 device_set_ivars(cdev, ndi);
279 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
282 struct simplebus_devinfo *ndi;
284 cdev = device_add_child_ordered(dev, order, name, unit);
288 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
289 ndi->obdinfo.obd_node = -1;
290 resource_list_init(&ndi->rl);
291 device_set_ivars(cdev, ndi);
296 static const struct ofw_bus_devinfo *
297 simplebus_get_devinfo(device_t bus __unused, device_t child)
299 struct simplebus_devinfo *ndi;
301 ndi = device_get_ivars(child);
304 return (&ndi->obdinfo);
307 static struct resource_list *
308 simplebus_get_resource_list(device_t bus __unused, device_t child)
310 struct simplebus_devinfo *ndi;
312 ndi = device_get_ivars(child);
318 static struct resource *
319 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
320 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
322 struct simplebus_softc *sc;
323 struct simplebus_devinfo *di;
324 struct resource_list_entry *rle;
327 sc = device_get_softc(bus);
330 * Request for the default allocation with a given rid: use resource
331 * list stored in the local device info.
333 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
334 if ((di = device_get_ivars(child)) == NULL)
337 if (type == SYS_RES_IOPORT)
338 type = SYS_RES_MEMORY;
340 rle = resource_list_find(&di->rl, type, *rid);
343 device_printf(bus, "no default resources for "
344 "rid = %d, type = %d\n", *rid, type);
352 if (type == SYS_RES_MEMORY) {
353 /* Remap through ranges property */
354 for (j = 0; j < sc->nranges; j++) {
355 if (start >= sc->ranges[j].bus && end <
356 sc->ranges[j].bus + sc->ranges[j].size) {
357 start -= sc->ranges[j].bus;
358 start += sc->ranges[j].host;
359 end -= sc->ranges[j].bus;
360 end += sc->ranges[j].host;
364 if (j == sc->nranges && sc->nranges != 0) {
366 device_printf(bus, "Could not map resource "
367 "%#jx-%#jx\n", start, end);
373 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
378 simplebus_print_res(struct simplebus_devinfo *di)
385 rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
386 rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
391 simplebus_probe_nomatch(device_t bus, device_t child)
393 const char *name, *type, *compat;
398 compat = ofw_bus_get_compat(child);
401 name = ofw_bus_get_name(child);
402 type = ofw_bus_get_type(child);
404 device_printf(bus, "<%s>", name != NULL ? name : "unknown");
405 simplebus_print_res(device_get_ivars(child));
406 if (!ofw_bus_status_okay(child))
409 printf(" type %s", type);
410 printf(" compat %s (no driver attached)\n", compat);
414 simplebus_print_child(device_t bus, device_t child)
418 rv = bus_print_child_header(bus, child);
419 rv += simplebus_print_res(device_get_ivars(child));
420 if (!ofw_bus_status_okay(child))
421 rv += printf(" disabled");
422 rv += bus_print_child_footer(bus, child);