2 * Copyright 1998 Massachusetts Institute of Technology
3 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.
4 * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>.
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby
9 * granted, provided that both the above copyright notice and this
10 * permission notice appear in all copies, that both the above
11 * copyright notice and this permission notice appear in all
12 * supporting documentation, and that the name of M.I.T. not be used
13 * in advertising or publicity pertaining to distribution of the
14 * software without specific, written prior permission. M.I.T. makes
15 * no representations about the suitability of this software for any
16 * purpose. It is provided "as is" without express or implied
19 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
20 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
23 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/ofw/openfirm.h>
54 #include <machine/bus.h>
55 #include <machine/resource.h>
58 * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that
59 * hang from the Open Firmware root node and adds them as devices to this bus
60 * (except some special nodes which are excluded) so that drivers can be
65 struct ofwbus_devinfo {
66 struct ofw_bus_devinfo ndi_obdinfo;
67 struct resource_list ndi_rl;
71 uint32_t acells, scells;
72 struct rman sc_intr_rman;
73 struct rman sc_mem_rman;
76 static device_identify_t ofwbus_identify;
77 static device_probe_t ofwbus_probe;
78 static device_attach_t ofwbus_attach;
79 static bus_print_child_t ofwbus_print_child;
80 static bus_add_child_t ofwbus_add_child;
81 static bus_probe_nomatch_t ofwbus_probe_nomatch;
82 static bus_alloc_resource_t ofwbus_alloc_resource;
83 static bus_adjust_resource_t ofwbus_adjust_resource;
84 static bus_release_resource_t ofwbus_release_resource;
85 static bus_get_resource_list_t ofwbus_get_resource_list;
86 static ofw_bus_get_devinfo_t ofwbus_get_devinfo;
88 static int ofwbus_inlist(const char *, const char *const *);
89 static struct ofwbus_devinfo * ofwbus_setup_dinfo(device_t, phandle_t);
90 static void ofwbus_destroy_dinfo(struct ofwbus_devinfo *);
91 static int ofwbus_print_res(struct ofwbus_devinfo *);
93 static device_method_t ofwbus_methods[] = {
94 /* Device interface */
95 DEVMETHOD(device_identify, ofwbus_identify),
96 DEVMETHOD(device_probe, ofwbus_probe),
97 DEVMETHOD(device_attach, ofwbus_attach),
98 DEVMETHOD(device_detach, bus_generic_detach),
99 DEVMETHOD(device_shutdown, bus_generic_shutdown),
100 DEVMETHOD(device_suspend, bus_generic_suspend),
101 DEVMETHOD(device_resume, bus_generic_resume),
104 DEVMETHOD(bus_print_child, ofwbus_print_child),
105 DEVMETHOD(bus_probe_nomatch, ofwbus_probe_nomatch),
106 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
107 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
108 DEVMETHOD(bus_add_child, ofwbus_add_child),
109 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
110 DEVMETHOD(bus_alloc_resource, ofwbus_alloc_resource),
111 DEVMETHOD(bus_adjust_resource, ofwbus_adjust_resource),
112 DEVMETHOD(bus_release_resource, ofwbus_release_resource),
113 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
114 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
115 DEVMETHOD(bus_get_resource_list, ofwbus_get_resource_list),
116 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
117 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
118 DEVMETHOD(bus_config_intr, bus_generic_config_intr),
119 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
120 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
122 /* ofw_bus interface */
123 DEVMETHOD(ofw_bus_get_devinfo, ofwbus_get_devinfo),
124 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
125 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
126 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
127 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
128 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
133 static driver_t ofwbus_driver = {
136 sizeof(struct ofwbus_softc)
138 static devclass_t ofwbus_devclass;
139 EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, ofwbus_devclass, 0, 0,
140 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
141 MODULE_VERSION(ofwbus, 1);
143 static const char *const ofwbus_excl_name[] = {
149 "counter-timer", /* No separate device; handled by psycho/sbus */
163 static const char *const ofwbus_excl_type[] = {
170 ofwbus_inlist(const char *name, const char *const *list)
176 for (i = 0; list[i] != NULL; i++)
177 if (strcmp(name, list[i]) == 0)
182 #define OFWBUS_EXCLUDED(name, type) \
183 (ofwbus_inlist((name), ofwbus_excl_name) || \
184 ((type) != NULL && ofwbus_inlist((type), ofwbus_excl_type)))
187 ofwbus_identify(driver_t *driver, device_t parent)
190 /* Check if Open Firmware has been instantiated */
194 if (device_find_child(parent, "ofwbus", -1) == NULL)
195 BUS_ADD_CHILD(parent, 0, "ofwbus", -1);
199 ofwbus_probe(device_t dev)
202 device_set_desc(dev, "Open Firmware Device Tree");
203 return (BUS_PROBE_NOWILDCARD);
207 ofwbus_attach(device_t dev)
209 struct ofwbus_devinfo *ndi;
210 struct ofwbus_softc *sc;
214 sc = device_get_softc(dev);
219 * If no Open Firmware, bail early
224 sc->sc_intr_rman.rm_type = RMAN_ARRAY;
225 sc->sc_intr_rman.rm_descr = "Interrupts";
226 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
227 sc->sc_mem_rman.rm_descr = "Device Memory";
228 if (rman_init(&sc->sc_intr_rman) != 0 ||
229 rman_init(&sc->sc_mem_rman) != 0 ||
230 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 ||
231 rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) != 0)
232 panic("%s: failed to set up rmans.", __func__);
235 * Allow devices to identify.
237 bus_generic_probe(dev);
240 * Some important numbers
243 OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
245 OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
248 * Now walk the OFW tree and attach top-level devices.
250 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
251 if ((ndi = ofwbus_setup_dinfo(dev, node)) == NULL)
253 cdev = device_add_child(dev, NULL, -1);
255 device_printf(dev, "<%s>: device_add_child failed\n",
256 ndi->ndi_obdinfo.obd_name);
257 ofwbus_destroy_dinfo(ndi);
260 device_set_ivars(cdev, ndi);
262 return (bus_generic_attach(dev));
266 ofwbus_add_child(device_t dev, u_int order, const char *name, int unit)
269 struct ofwbus_devinfo *ndi;
271 cdev = device_add_child_ordered(dev, order, name, unit);
275 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
276 ndi->ndi_obdinfo.obd_node = -1;
277 resource_list_init(&ndi->ndi_rl);
278 device_set_ivars(cdev, ndi);
284 ofwbus_print_child(device_t bus, device_t child)
288 rv = bus_print_child_header(bus, child);
289 rv += ofwbus_print_res(device_get_ivars(child));
290 rv += bus_print_child_footer(bus, child);
295 ofwbus_probe_nomatch(device_t bus, device_t child)
297 const char *name, *type;
302 name = ofw_bus_get_name(child);
303 type = ofw_bus_get_type(child);
305 device_printf(bus, "<%s>",
306 name != NULL ? name : "unknown");
307 ofwbus_print_res(device_get_ivars(child));
308 printf(" type %s (no driver attached)\n",
309 type != NULL ? type : "unknown");
312 static struct resource *
313 ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
314 u_long start, u_long end, u_long count, u_int flags)
316 struct ofwbus_softc *sc;
319 struct resource_list_entry *rle;
320 int isdefault, passthrough;
322 isdefault = (start == 0UL && end == ~0UL);
323 passthrough = (device_get_parent(child) != bus);
324 sc = device_get_softc(bus);
327 if (!passthrough && isdefault) {
328 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
332 if (rle->res != NULL)
333 panic("%s: resource entry is busy", __func__);
335 count = ulmax(count, rle->count);
336 end = ulmax(rle->end, start + count - 1);
341 rm = &sc->sc_intr_rman;
344 rm = &sc->sc_mem_rman;
350 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
354 rman_set_rid(rv, *rid);
356 if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type,
358 rman_release_resource(rv);
362 if (!passthrough && rle != NULL) {
364 rle->start = rman_get_start(rv);
365 rle->end = rman_get_end(rv);
366 rle->count = rle->end - rle->start + 1;
373 ofwbus_adjust_resource(device_t bus, device_t child __unused, int type,
374 struct resource *r, u_long start, u_long end)
376 struct ofwbus_softc *sc;
381 while (strcmp(device_get_name(device_get_parent(ofwbus)), "root") != 0)
382 ofwbus = device_get_parent(ofwbus);
383 sc = device_get_softc(ofwbus);
386 rm = &sc->sc_intr_rman;
389 rm = &sc->sc_mem_rman;
396 if (rman_is_region_manager(r, rm) == 0)
398 return (rman_adjust_resource(r, start, end));
402 ofwbus_release_resource(device_t bus __unused, device_t child, int type,
403 int rid, struct resource *r)
407 if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
408 error = bus_deactivate_resource(child, type, rid, r);
412 return (rman_release_resource(r));
415 static struct resource_list *
416 ofwbus_get_resource_list(device_t bus __unused, device_t child)
418 struct ofwbus_devinfo *ndi;
420 ndi = device_get_ivars(child);
421 return (&ndi->ndi_rl);
424 static const struct ofw_bus_devinfo *
425 ofwbus_get_devinfo(device_t bus __unused, device_t child)
427 struct ofwbus_devinfo *ndi;
429 ndi = device_get_ivars(child);
430 return (&ndi->ndi_obdinfo);
433 static struct ofwbus_devinfo *
434 ofwbus_setup_dinfo(device_t dev, phandle_t node)
436 struct ofwbus_softc *sc;
437 struct ofwbus_devinfo *ndi;
438 const char *nodename;
439 uint32_t *reg, *intr, icells;
446 sc = device_get_softc(dev);
448 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
449 if (ofw_bus_gen_setup_devinfo(&ndi->ndi_obdinfo, node) != 0) {
453 nodename = ndi->ndi_obdinfo.obd_name;
454 if (OFWBUS_EXCLUDED(nodename, ndi->ndi_obdinfo.obd_type)) {
455 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
460 resource_list_init(&ndi->ndi_rl);
461 nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®);
464 if (nreg % (sc->acells + sc->scells) != 0) {
466 device_printf(dev, "Malformed reg property on <%s>\n",
471 for (i = 0, rid = 0; i < nreg; i += sc->acells + sc->scells, rid++) {
473 for (j = 0; j < sc->acells; j++) {
477 for (j = 0; j < sc->scells; j++) {
479 size |= reg[i + sc->acells + j];
481 /* Skip the dummy reg property of glue devices like ssm(4). */
483 resource_list_add(&ndi->ndi_rl, SYS_RES_MEMORY, rid,
484 phys, phys + size - 1, size);
486 free(reg, M_OFWPROP);
488 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
491 if (OF_searchencprop(node, "interrupt-parent", &iparent,
492 sizeof(iparent)) == -1) {
493 device_printf(dev, "No interrupt-parent found, "
494 "assuming nexus on <%s>\n", nodename);
495 iparent = 0xffffffff;
497 if (OF_searchencprop(OF_node_from_xref(iparent),
498 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
499 device_printf(dev, "Missing #interrupt-cells property, "
500 "assuming <1> on <%s>\n", nodename);
503 if (icells < 1 || icells > nintr) {
504 device_printf(dev, "Invalid #interrupt-cells property "
505 "value <%d>, assuming <1> on <%s>\n", icells,
509 for (i = 0, rid = 0; i < nintr; i += icells, rid++) {
510 intr[i] = ofw_bus_map_intr(dev, iparent, icells,
512 resource_list_add(&ndi->ndi_rl, SYS_RES_IRQ, rid, intr[i],
515 free(intr, M_OFWPROP);
522 ofwbus_destroy_dinfo(struct ofwbus_devinfo *ndi)
525 resource_list_free(&ndi->ndi_rl);
526 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
531 ofwbus_print_res(struct ofwbus_devinfo *ndi)
536 rv += resource_list_print_type(&ndi->ndi_rl, "mem", SYS_RES_MEMORY,
538 rv += resource_list_print_type(&ndi->ndi_rl, "irq", SYS_RES_IRQ,