165d08437SNathan Whitehorn /*- 265d08437SNathan Whitehorn * Copyright 1998 Massachusetts Institute of Technology 365d08437SNathan Whitehorn * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>. 465d08437SNathan Whitehorn * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>. 565d08437SNathan Whitehorn * All rights reserved. 665d08437SNathan Whitehorn * 765d08437SNathan Whitehorn * Permission to use, copy, modify, and distribute this software and 865d08437SNathan Whitehorn * its documentation for any purpose and without fee is hereby 965d08437SNathan Whitehorn * granted, provided that both the above copyright notice and this 1065d08437SNathan Whitehorn * permission notice appear in all copies, that both the above 1165d08437SNathan Whitehorn * copyright notice and this permission notice appear in all 1265d08437SNathan Whitehorn * supporting documentation, and that the name of M.I.T. not be used 1365d08437SNathan Whitehorn * in advertising or publicity pertaining to distribution of the 1465d08437SNathan Whitehorn * software without specific, written prior permission. M.I.T. makes 1565d08437SNathan Whitehorn * no representations about the suitability of this software for any 1665d08437SNathan Whitehorn * purpose. It is provided "as is" without express or implied 1765d08437SNathan Whitehorn * warranty. 1865d08437SNathan Whitehorn * 1965d08437SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 2065d08437SNathan Whitehorn * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 2165d08437SNathan Whitehorn * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 2265d08437SNathan Whitehorn * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 2365d08437SNathan Whitehorn * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2465d08437SNathan Whitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2565d08437SNathan Whitehorn * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2665d08437SNathan Whitehorn * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 2765d08437SNathan Whitehorn * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2865d08437SNathan Whitehorn * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 2965d08437SNathan Whitehorn * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3065d08437SNathan Whitehorn * SUCH DAMAGE. 3165d08437SNathan Whitehorn * 3265d08437SNathan Whitehorn * from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09 3365d08437SNathan Whitehorn */ 3465d08437SNathan Whitehorn 3565d08437SNathan Whitehorn #include <sys/param.h> 3665d08437SNathan Whitehorn #include <sys/systm.h> 3765d08437SNathan Whitehorn #include <sys/bus.h> 3865d08437SNathan Whitehorn #include <sys/kernel.h> 3965d08437SNathan Whitehorn #include <sys/module.h> 4065d08437SNathan Whitehorn #include <sys/rman.h> 4165d08437SNathan Whitehorn 4265d08437SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 4365d08437SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h> 4465d08437SNathan Whitehorn #include <dev/ofw/openfirm.h> 45ecaecbc7SIan Lepore #include <dev/fdt/simplebus.h> 4665d08437SNathan Whitehorn 4765d08437SNathan Whitehorn #include <machine/bus.h> 4865d08437SNathan Whitehorn #include <machine/resource.h> 4965d08437SNathan Whitehorn 5065d08437SNathan Whitehorn /* 5165d08437SNathan Whitehorn * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that 5265d08437SNathan Whitehorn * hang from the Open Firmware root node and adds them as devices to this bus 5365d08437SNathan Whitehorn * (except some special nodes which are excluded) so that drivers can be 5453d5e65eSMitchell Horne * attached to them. There should be only one ofwbus in the system, added 5553d5e65eSMitchell Horne * directly as a child of nexus0. 5665d08437SNathan Whitehorn */ 5765d08437SNathan Whitehorn 5865d08437SNathan Whitehorn static device_probe_t ofwbus_probe; 5965d08437SNathan Whitehorn static device_attach_t ofwbus_attach; 6065d08437SNathan Whitehorn static bus_alloc_resource_t ofwbus_alloc_resource; 6165d08437SNathan Whitehorn static bus_release_resource_t ofwbus_release_resource; 6265d08437SNathan Whitehorn 6365d08437SNathan Whitehorn static device_method_t ofwbus_methods[] = { 6465d08437SNathan Whitehorn /* Device interface */ 6565d08437SNathan Whitehorn DEVMETHOD(device_probe, ofwbus_probe), 6665d08437SNathan Whitehorn DEVMETHOD(device_attach, ofwbus_attach), 6765d08437SNathan Whitehorn 6865d08437SNathan Whitehorn /* Bus interface */ 6965d08437SNathan Whitehorn DEVMETHOD(bus_alloc_resource, ofwbus_alloc_resource), 70f9bdaab9SElliott Mitchell DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 7165d08437SNathan Whitehorn DEVMETHOD(bus_release_resource, ofwbus_release_resource), 7265d08437SNathan Whitehorn 7365d08437SNathan Whitehorn DEVMETHOD_END 7465d08437SNathan Whitehorn }; 7565d08437SNathan Whitehorn 76ecaecbc7SIan Lepore DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods, 77f9bdaab9SElliott Mitchell sizeof(struct simplebus_softc), simplebus_driver); 7803f6459cSJohn Baldwin EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, 0, 0, 79633dbf2eSIan Lepore BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 8065d08437SNathan Whitehorn MODULE_VERSION(ofwbus, 1); 8165d08437SNathan Whitehorn 8265d08437SNathan Whitehorn static int 8365d08437SNathan Whitehorn ofwbus_probe(device_t dev) 8465d08437SNathan Whitehorn { 8565d08437SNathan Whitehorn 862c0d026bSAndrew Turner if (OF_peer(0) == 0) 872c0d026bSAndrew Turner return (ENXIO); 882c0d026bSAndrew Turner 89afca197fSMitchell Horne /* Only one instance of ofwbus. */ 90afca197fSMitchell Horne if (device_get_unit(dev) != 0) 91afca197fSMitchell Horne panic("ofwbus added with non-zero unit number: %d\n", 92afca197fSMitchell Horne device_get_unit(dev)); 93afca197fSMitchell Horne 9465d08437SNathan Whitehorn device_set_desc(dev, "Open Firmware Device Tree"); 9565d08437SNathan Whitehorn return (BUS_PROBE_NOWILDCARD); 9665d08437SNathan Whitehorn } 9765d08437SNathan Whitehorn 9865d08437SNathan Whitehorn static int 9965d08437SNathan Whitehorn ofwbus_attach(device_t dev) 10065d08437SNathan Whitehorn { 10165d08437SNathan Whitehorn phandle_t node; 10265d08437SNathan Whitehorn 10365d08437SNathan Whitehorn node = OF_peer(0); 10465d08437SNathan Whitehorn 10565d08437SNathan Whitehorn /* 10665d08437SNathan Whitehorn * If no Open Firmware, bail early 10765d08437SNathan Whitehorn */ 10865d08437SNathan Whitehorn if (node == -1) 10965d08437SNathan Whitehorn return (ENXIO); 11065d08437SNathan Whitehorn 111ecaecbc7SIan Lepore /* 112ecaecbc7SIan Lepore * ofwbus bus starts on unamed node in FDT, so we cannot make 113ecaecbc7SIan Lepore * ofw_bus_devinfo from it. Pass node to simplebus_init directly. 114ecaecbc7SIan Lepore */ 115ecaecbc7SIan Lepore simplebus_init(dev, node); 11665d08437SNathan Whitehorn 11765d08437SNathan Whitehorn /* 11865d08437SNathan Whitehorn * Allow devices to identify. 11965d08437SNathan Whitehorn */ 120*723da5d9SJohn Baldwin bus_identify_children(dev); 12165d08437SNathan Whitehorn 12265d08437SNathan Whitehorn /* 12365d08437SNathan Whitehorn * Now walk the OFW tree and attach top-level devices. 12465d08437SNathan Whitehorn */ 1259105ba04SChristos Margiolis for (node = OF_child(node); node > 0; node = OF_peer(node)) 126ecaecbc7SIan Lepore simplebus_add_device(dev, node, 0, NULL, -1, NULL); 1279105ba04SChristos Margiolis 12865d08437SNathan Whitehorn return (bus_generic_attach(dev)); 12965d08437SNathan Whitehorn } 13065d08437SNathan Whitehorn 13165d08437SNathan Whitehorn static struct resource * 13265d08437SNathan Whitehorn ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid, 1332dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 13465d08437SNathan Whitehorn { 13565d08437SNathan Whitehorn struct resource *rv; 13665d08437SNathan Whitehorn struct resource_list_entry *rle; 137f9bdaab9SElliott Mitchell bool isdefault, passthrough; 13865d08437SNathan Whitehorn 1397915adb5SJustin Hibbits isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 14065d08437SNathan Whitehorn passthrough = (device_get_parent(child) != bus); 14165d08437SNathan Whitehorn rle = NULL; 14265d08437SNathan Whitehorn if (!passthrough && isdefault) { 14365d08437SNathan Whitehorn rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 14465d08437SNathan Whitehorn type, *rid); 145ecaecbc7SIan Lepore if (rle == NULL) { 146ecaecbc7SIan Lepore if (bootverbose) 147ecaecbc7SIan Lepore device_printf(bus, "no default resources for " 148ecaecbc7SIan Lepore "rid = %d, type = %d\n", *rid, type); 14965d08437SNathan Whitehorn return (NULL); 150ecaecbc7SIan Lepore } 15165d08437SNathan Whitehorn start = rle->start; 152da1b038aSJustin Hibbits count = ummax(count, rle->count); 153da1b038aSJustin Hibbits end = ummax(rle->end, start + count - 1); 15465d08437SNathan Whitehorn } 15565d08437SNathan Whitehorn 156f9bdaab9SElliott Mitchell /* Let nexus handle the allocation. */ 157f9bdaab9SElliott Mitchell rv = bus_generic_alloc_resource(bus, child, type, rid, start, end, 158f9bdaab9SElliott Mitchell count, flags); 15965d08437SNathan Whitehorn if (rv == NULL) 16065d08437SNathan Whitehorn return (NULL); 16165d08437SNathan Whitehorn 16265d08437SNathan Whitehorn if (!passthrough && rle != NULL) { 16365d08437SNathan Whitehorn rle->res = rv; 16465d08437SNathan Whitehorn rle->start = rman_get_start(rv); 16565d08437SNathan Whitehorn rle->end = rman_get_end(rv); 16665d08437SNathan Whitehorn rle->count = rle->end - rle->start + 1; 16765d08437SNathan Whitehorn } 16865d08437SNathan Whitehorn 16965d08437SNathan Whitehorn return (rv); 17065d08437SNathan Whitehorn } 17165d08437SNathan Whitehorn 17265d08437SNathan Whitehorn static int 1739dbf5b0eSJohn Baldwin ofwbus_release_resource(device_t bus, device_t child, struct resource *r) 17465d08437SNathan Whitehorn { 17576a8ef26SZbigniew Bodek struct resource_list_entry *rle; 176f9bdaab9SElliott Mitchell bool passthrough; 17765d08437SNathan Whitehorn 178e2d4f32fSZbigniew Bodek passthrough = (device_get_parent(child) != bus); 179e2d4f32fSZbigniew Bodek if (!passthrough) { 18076a8ef26SZbigniew Bodek /* Clean resource list entry */ 181e2d4f32fSZbigniew Bodek rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 1829dbf5b0eSJohn Baldwin rman_get_type(r), rman_get_rid(r)); 18376a8ef26SZbigniew Bodek if (rle != NULL) 18476a8ef26SZbigniew Bodek rle->res = NULL; 185e2d4f32fSZbigniew Bodek } 18676a8ef26SZbigniew Bodek 187f9bdaab9SElliott Mitchell /* Let nexus handle the release. */ 1889dbf5b0eSJohn Baldwin return (bus_generic_release_resource(bus, child, r)); 18965d08437SNathan Whitehorn } 190