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>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/fdt/simplebus.h>
58 #include <machine/bus.h>
59 #include <machine/resource.h>
62 * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that
63 * hang from the Open Firmware root node and adds them as devices to this bus
64 * (except some special nodes which are excluded) so that drivers can be
70 struct simplebus_softc simplebus_sc;
71 struct rman sc_intr_rman;
72 struct rman sc_mem_rman;
76 static device_identify_t ofwbus_identify;
78 static device_probe_t ofwbus_probe;
79 static device_attach_t ofwbus_attach;
80 static bus_alloc_resource_t ofwbus_alloc_resource;
81 static bus_adjust_resource_t ofwbus_adjust_resource;
82 static bus_release_resource_t ofwbus_release_resource;
84 static device_method_t ofwbus_methods[] = {
85 /* Device interface */
87 DEVMETHOD(device_identify, ofwbus_identify),
89 DEVMETHOD(device_probe, ofwbus_probe),
90 DEVMETHOD(device_attach, ofwbus_attach),
93 DEVMETHOD(bus_alloc_resource, ofwbus_alloc_resource),
94 DEVMETHOD(bus_adjust_resource, ofwbus_adjust_resource),
95 DEVMETHOD(bus_release_resource, ofwbus_release_resource),
100 DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods,
101 sizeof(struct ofwbus_softc), simplebus_driver);
102 static devclass_t ofwbus_devclass;
103 EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, ofwbus_devclass, 0, 0,
104 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
105 MODULE_VERSION(ofwbus, 1);
109 ofwbus_identify(driver_t *driver, device_t parent)
112 /* Check if Open Firmware has been instantiated */
116 if (device_find_child(parent, "ofwbus", -1) == NULL)
117 BUS_ADD_CHILD(parent, 0, "ofwbus", -1);
122 ofwbus_probe(device_t dev)
130 device_set_desc(dev, "Open Firmware Device Tree");
131 return (BUS_PROBE_NOWILDCARD);
135 ofwbus_attach(device_t dev)
137 struct ofwbus_softc *sc;
139 struct ofw_bus_devinfo obd;
141 sc = device_get_softc(dev);
146 * If no Open Firmware, bail early
152 * ofwbus bus starts on unamed node in FDT, so we cannot make
153 * ofw_bus_devinfo from it. Pass node to simplebus_init directly.
155 simplebus_init(dev, node);
156 sc->sc_intr_rman.rm_type = RMAN_ARRAY;
157 sc->sc_intr_rman.rm_descr = "Interrupts";
158 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
159 sc->sc_mem_rman.rm_descr = "Device Memory";
160 if (rman_init(&sc->sc_intr_rman) != 0 ||
161 rman_init(&sc->sc_mem_rman) != 0 ||
162 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 ||
163 rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) != 0)
164 panic("%s: failed to set up rmans.", __func__);
167 * Allow devices to identify.
169 bus_generic_probe(dev);
172 * Now walk the OFW tree and attach top-level devices.
174 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
175 if (ofw_bus_gen_setup_devinfo(&obd, node) != 0)
177 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
179 return (bus_generic_attach(dev));
182 static struct resource *
183 ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
184 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
186 struct ofwbus_softc *sc;
189 struct resource_list_entry *rle;
190 int isdefault, passthrough;
192 isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
193 passthrough = (device_get_parent(child) != bus);
194 sc = device_get_softc(bus);
196 if (!passthrough && isdefault) {
197 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
201 device_printf(bus, "no default resources for "
202 "rid = %d, type = %d\n", *rid, type);
206 count = ummax(count, rle->count);
207 end = ummax(rle->end, start + count - 1);
212 rm = &sc->sc_intr_rman;
215 rm = &sc->sc_mem_rman;
221 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
225 rman_set_rid(rv, *rid);
227 if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type,
229 rman_release_resource(rv);
233 if (!passthrough && rle != NULL) {
235 rle->start = rman_get_start(rv);
236 rle->end = rman_get_end(rv);
237 rle->count = rle->end - rle->start + 1;
244 ofwbus_adjust_resource(device_t bus, device_t child __unused, int type,
245 struct resource *r, rman_res_t start, rman_res_t end)
247 struct ofwbus_softc *sc;
252 while (strcmp(device_get_name(device_get_parent(ofwbus)), "root") != 0)
253 ofwbus = device_get_parent(ofwbus);
254 sc = device_get_softc(ofwbus);
257 rm = &sc->sc_intr_rman;
260 rm = &sc->sc_mem_rman;
267 if (rman_is_region_manager(r, rm) == 0)
269 return (rman_adjust_resource(r, start, end));
273 ofwbus_release_resource(device_t bus, device_t child, int type,
274 int rid, struct resource *r)
276 struct resource_list_entry *rle;
280 passthrough = (device_get_parent(child) != bus);
282 /* Clean resource list entry */
283 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
289 if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
290 error = bus_deactivate_resource(child, type, rid, r);
294 return (rman_release_resource(r));