xref: /freebsd/sys/dev/fdt/simplebus.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
158158742SRafal Jaworowski /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro F. Giffuni  *
406763f5eSNathan Whitehorn  * Copyright (c) 2013 Nathan Whitehorn
558158742SRafal Jaworowski  * All rights reserved.
658158742SRafal Jaworowski  *
758158742SRafal Jaworowski  * Redistribution and use in source and binary forms, with or without
858158742SRafal Jaworowski  * modification, are permitted provided that the following conditions
958158742SRafal Jaworowski  * are met:
1058158742SRafal Jaworowski  * 1. Redistributions of source code must retain the above copyright
1158158742SRafal Jaworowski  *    notice, this list of conditions and the following disclaimer.
1258158742SRafal Jaworowski  * 2. Redistributions in binary form must reproduce the above copyright
1358158742SRafal Jaworowski  *    notice, this list of conditions and the following disclaimer in the
1458158742SRafal Jaworowski  *    documentation and/or other materials provided with the distribution.
1558158742SRafal Jaworowski  *
1658158742SRafal Jaworowski  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1758158742SRafal Jaworowski  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858158742SRafal Jaworowski  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1958158742SRafal Jaworowski  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2058158742SRafal Jaworowski  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158158742SRafal Jaworowski  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258158742SRafal Jaworowski  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358158742SRafal Jaworowski  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458158742SRafal Jaworowski  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558158742SRafal Jaworowski  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658158742SRafal Jaworowski  * SUCH DAMAGE.
2758158742SRafal Jaworowski  */
2858158742SRafal Jaworowski 
2958158742SRafal Jaworowski #include <sys/cdefs.h>
3058158742SRafal Jaworowski __FBSDID("$FreeBSD$");
3158158742SRafal Jaworowski #include <sys/param.h>
3258158742SRafal Jaworowski #include <sys/systm.h>
3358158742SRafal Jaworowski #include <sys/module.h>
3458158742SRafal Jaworowski #include <sys/bus.h>
3506763f5eSNathan Whitehorn #include <sys/conf.h>
3606763f5eSNathan Whitehorn #include <sys/kernel.h>
3758158742SRafal Jaworowski #include <sys/rman.h>
3858158742SRafal Jaworowski 
3906763f5eSNathan Whitehorn #include <dev/ofw/openfirm.h>
4058158742SRafal Jaworowski #include <dev/ofw/ofw_bus.h>
4158158742SRafal Jaworowski #include <dev/ofw/ofw_bus_subr.h>
4258158742SRafal Jaworowski 
432cc1ad9cSJayachandran C. #include <dev/fdt/simplebus.h>
4458158742SRafal Jaworowski 
4558158742SRafal Jaworowski /*
4606763f5eSNathan Whitehorn  * Bus interface.
4758158742SRafal Jaworowski  */
4806763f5eSNathan Whitehorn static int		simplebus_probe(device_t dev);
4906763f5eSNathan Whitehorn static int		simplebus_attach(device_t dev);
5058158742SRafal Jaworowski static struct resource *simplebus_alloc_resource(device_t, device_t, int,
512dd1bdf1SJustin Hibbits     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
5206763f5eSNathan Whitehorn static void		simplebus_probe_nomatch(device_t bus, device_t child);
5306763f5eSNathan Whitehorn static int		simplebus_print_child(device_t bus, device_t child);
54ecaecbc7SIan Lepore static device_t		simplebus_add_child(device_t dev, u_int order,
55ecaecbc7SIan Lepore     const char *name, int unit);
56ecaecbc7SIan Lepore static struct resource_list *simplebus_get_resource_list(device_t bus,
57ecaecbc7SIan Lepore     device_t child);
5858158742SRafal Jaworowski /*
5906763f5eSNathan Whitehorn  * ofw_bus interface
6006763f5eSNathan Whitehorn  */
6106763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
6206763f5eSNathan Whitehorn     device_t child);
6306763f5eSNathan Whitehorn 
6406763f5eSNathan Whitehorn /*
6506763f5eSNathan Whitehorn  * local methods
6606763f5eSNathan Whitehorn  */
6706763f5eSNathan Whitehorn 
6806763f5eSNathan Whitehorn static int simplebus_fill_ranges(phandle_t node,
6906763f5eSNathan Whitehorn     struct simplebus_softc *sc);
7006763f5eSNathan Whitehorn 
7106763f5eSNathan Whitehorn /*
7206763f5eSNathan Whitehorn  * Driver methods.
7358158742SRafal Jaworowski  */
7458158742SRafal Jaworowski static device_method_t	simplebus_methods[] = {
7558158742SRafal Jaworowski 	/* Device interface */
7658158742SRafal Jaworowski 	DEVMETHOD(device_probe,		simplebus_probe),
7758158742SRafal Jaworowski 	DEVMETHOD(device_attach,	simplebus_attach),
78ecaecbc7SIan Lepore 	DEVMETHOD(device_detach,	bus_generic_detach),
79ecaecbc7SIan Lepore 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
80ecaecbc7SIan Lepore 	DEVMETHOD(device_suspend,	bus_generic_suspend),
81ecaecbc7SIan Lepore 	DEVMETHOD(device_resume,	bus_generic_resume),
8258158742SRafal Jaworowski 
8358158742SRafal Jaworowski 	/* Bus interface */
84ecaecbc7SIan Lepore 	DEVMETHOD(bus_add_child,	simplebus_add_child),
8558158742SRafal Jaworowski 	DEVMETHOD(bus_print_child,	simplebus_print_child),
8606763f5eSNathan Whitehorn 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
87ecaecbc7SIan Lepore 	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
88ecaecbc7SIan Lepore 	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
8906763f5eSNathan Whitehorn 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
9006763f5eSNathan Whitehorn 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
9158158742SRafal Jaworowski 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
9206763f5eSNathan Whitehorn 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
9306763f5eSNathan Whitehorn 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
9406763f5eSNathan Whitehorn 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
9506763f5eSNathan Whitehorn 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
96ecaecbc7SIan Lepore 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
97ecaecbc7SIan Lepore 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
9806763f5eSNathan Whitehorn 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
99ecaecbc7SIan Lepore 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
10058158742SRafal Jaworowski 
10106763f5eSNathan Whitehorn 	/* ofw_bus interface */
10258158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
10358158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
10458158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
10558158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
10658158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
10758158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
10858158742SRafal Jaworowski 
10906763f5eSNathan Whitehorn 	DEVMETHOD_END
11058158742SRafal Jaworowski };
11158158742SRafal Jaworowski 
1122cc1ad9cSJayachandran C. DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
1132cc1ad9cSJayachandran C.     sizeof(struct simplebus_softc));
1142cc1ad9cSJayachandran C. 
11506763f5eSNathan Whitehorn static devclass_t simplebus_devclass;
1162d12d35cSIan Lepore EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
1172d12d35cSIan Lepore     0, 0, BUS_PASS_BUS);
1182d12d35cSIan Lepore EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
119633dbf2eSIan Lepore     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
12058158742SRafal Jaworowski 
12158158742SRafal Jaworowski static int
12258158742SRafal Jaworowski simplebus_probe(device_t dev)
12358158742SRafal Jaworowski {
12458158742SRafal Jaworowski 
125add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
126add35ed5SIan Lepore 		return (ENXIO);
127add35ed5SIan Lepore 
1282a74fe2cSIan Lepore 	/*
1292a74fe2cSIan Lepore 	 * FDT data puts a "simple-bus" compatible string on many things that
130db4fcadfSConrad Meyer 	 * have children but aren't really buses in our world.  Without a
1312a74fe2cSIan Lepore 	 * ranges property we will fail to attach, so just fail to probe too.
1322a74fe2cSIan Lepore 	 */
1332a74fe2cSIan Lepore 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
1342a74fe2cSIan Lepore 	    ofw_bus_has_prop(dev, "ranges")) &&
13506763f5eSNathan Whitehorn 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
13606763f5eSNathan Whitehorn 	     "soc") != 0))
13758158742SRafal Jaworowski 		return (ENXIO);
13858158742SRafal Jaworowski 
13958158742SRafal Jaworowski 	device_set_desc(dev, "Flattened device tree simple bus");
14058158742SRafal Jaworowski 
1412737a5a9SAleksandr Rybalko 	return (BUS_PROBE_GENERIC);
14258158742SRafal Jaworowski }
14358158742SRafal Jaworowski 
14458158742SRafal Jaworowski static int
14558158742SRafal Jaworowski simplebus_attach(device_t dev)
14658158742SRafal Jaworowski {
14758158742SRafal Jaworowski 	struct		simplebus_softc *sc;
14806763f5eSNathan Whitehorn 	phandle_t	node;
14958158742SRafal Jaworowski 
15058158742SRafal Jaworowski 	sc = device_get_softc(dev);
151ecaecbc7SIan Lepore 	simplebus_init(dev, 0);
152ecaecbc7SIan Lepore 	if (simplebus_fill_ranges(sc->node, sc) < 0) {
153ecaecbc7SIan Lepore 		device_printf(dev, "could not get ranges\n");
154ecaecbc7SIan Lepore 		return (ENXIO);
155ecaecbc7SIan Lepore 	}
15658158742SRafal Jaworowski 
157ecaecbc7SIan Lepore 	/*
158ecaecbc7SIan Lepore 	 * In principle, simplebus could have an interrupt map, but ignore that
159ecaecbc7SIan Lepore 	 * for now
160ecaecbc7SIan Lepore 	 */
161ecaecbc7SIan Lepore 
162ecaecbc7SIan Lepore 	for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
163ecaecbc7SIan Lepore 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
164ecaecbc7SIan Lepore 	return (bus_generic_attach(dev));
165ecaecbc7SIan Lepore }
166ecaecbc7SIan Lepore 
167ecaecbc7SIan Lepore void
168ecaecbc7SIan Lepore simplebus_init(device_t dev, phandle_t node)
169ecaecbc7SIan Lepore {
170ecaecbc7SIan Lepore 	struct simplebus_softc *sc;
171ecaecbc7SIan Lepore 
172ecaecbc7SIan Lepore 	sc = device_get_softc(dev);
173ecaecbc7SIan Lepore 	if (node == 0)
174ecaecbc7SIan Lepore 		node = ofw_bus_get_node(dev);
17506763f5eSNathan Whitehorn 	sc->dev = dev;
17606763f5eSNathan Whitehorn 	sc->node = node;
17706763f5eSNathan Whitehorn 
17858158742SRafal Jaworowski 	/*
17906763f5eSNathan Whitehorn 	 * Some important numbers
18058158742SRafal Jaworowski 	 */
18106763f5eSNathan Whitehorn 	sc->acells = 2;
18206763f5eSNathan Whitehorn 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
18306763f5eSNathan Whitehorn 	sc->scells = 1;
18406763f5eSNathan Whitehorn 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
18558158742SRafal Jaworowski }
18658158742SRafal Jaworowski 
18758158742SRafal Jaworowski static int
18806763f5eSNathan Whitehorn simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
18958158742SRafal Jaworowski {
19006763f5eSNathan Whitehorn 	int host_address_cells;
19106763f5eSNathan Whitehorn 	cell_t *base_ranges;
19206763f5eSNathan Whitehorn 	ssize_t nbase_ranges;
19306763f5eSNathan Whitehorn 	int err;
19406763f5eSNathan Whitehorn 	int i, j, k;
19558158742SRafal Jaworowski 
19606763f5eSNathan Whitehorn 	err = OF_searchencprop(OF_parent(node), "#address-cells",
19706763f5eSNathan Whitehorn 	    &host_address_cells, sizeof(host_address_cells));
19806763f5eSNathan Whitehorn 	if (err <= 0)
19906763f5eSNathan Whitehorn 		return (-1);
20058158742SRafal Jaworowski 
20106763f5eSNathan Whitehorn 	nbase_ranges = OF_getproplen(node, "ranges");
20206763f5eSNathan Whitehorn 	if (nbase_ranges < 0)
20306763f5eSNathan Whitehorn 		return (-1);
20406763f5eSNathan Whitehorn 	sc->nranges = nbase_ranges / sizeof(cell_t) /
20506763f5eSNathan Whitehorn 	    (sc->acells + host_address_cells + sc->scells);
20606763f5eSNathan Whitehorn 	if (sc->nranges == 0)
20706763f5eSNathan Whitehorn 		return (0);
20858158742SRafal Jaworowski 
20906763f5eSNathan Whitehorn 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
21006763f5eSNathan Whitehorn 	    M_DEVBUF, M_WAITOK);
21106763f5eSNathan Whitehorn 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
21206763f5eSNathan Whitehorn 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
21306763f5eSNathan Whitehorn 
21406763f5eSNathan Whitehorn 	for (i = 0, j = 0; i < sc->nranges; i++) {
21506763f5eSNathan Whitehorn 		sc->ranges[i].bus = 0;
21606763f5eSNathan Whitehorn 		for (k = 0; k < sc->acells; k++) {
21706763f5eSNathan Whitehorn 			sc->ranges[i].bus <<= 32;
21806763f5eSNathan Whitehorn 			sc->ranges[i].bus |= base_ranges[j++];
21906763f5eSNathan Whitehorn 		}
22006763f5eSNathan Whitehorn 		sc->ranges[i].host = 0;
22106763f5eSNathan Whitehorn 		for (k = 0; k < host_address_cells; k++) {
22206763f5eSNathan Whitehorn 			sc->ranges[i].host <<= 32;
22306763f5eSNathan Whitehorn 			sc->ranges[i].host |= base_ranges[j++];
22406763f5eSNathan Whitehorn 		}
22506763f5eSNathan Whitehorn 		sc->ranges[i].size = 0;
22606763f5eSNathan Whitehorn 		for (k = 0; k < sc->scells; k++) {
22706763f5eSNathan Whitehorn 			sc->ranges[i].size <<= 32;
22806763f5eSNathan Whitehorn 			sc->ranges[i].size |= base_ranges[j++];
22906763f5eSNathan Whitehorn 		}
23006763f5eSNathan Whitehorn 	}
23106763f5eSNathan Whitehorn 
23206763f5eSNathan Whitehorn 	free(base_ranges, M_DEVBUF);
23306763f5eSNathan Whitehorn 	return (sc->nranges);
23406763f5eSNathan Whitehorn }
23506763f5eSNathan Whitehorn 
236ecaecbc7SIan Lepore struct simplebus_devinfo *
237ecaecbc7SIan Lepore simplebus_setup_dinfo(device_t dev, phandle_t node,
238ecaecbc7SIan Lepore     struct simplebus_devinfo *di)
23906763f5eSNathan Whitehorn {
24006763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
24106763f5eSNathan Whitehorn 	struct simplebus_devinfo *ndi;
24206763f5eSNathan Whitehorn 
24306763f5eSNathan Whitehorn 	sc = device_get_softc(dev);
244ecaecbc7SIan Lepore 	if (di == NULL)
24506763f5eSNathan Whitehorn 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
246ecaecbc7SIan Lepore 	else
247ecaecbc7SIan Lepore 		ndi = di;
24806763f5eSNathan Whitehorn 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
249ecaecbc7SIan Lepore 		if (di == NULL)
25006763f5eSNathan Whitehorn 			free(ndi, M_DEVBUF);
25106763f5eSNathan Whitehorn 		return (NULL);
25206763f5eSNathan Whitehorn 	}
25306763f5eSNathan Whitehorn 
25406763f5eSNathan Whitehorn 	resource_list_init(&ndi->rl);
2554b3d9160SZbigniew Bodek 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
256a8c5ea04SRuslan Bukin 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
25706763f5eSNathan Whitehorn 
25806763f5eSNathan Whitehorn 	return (ndi);
25906763f5eSNathan Whitehorn }
26006763f5eSNathan Whitehorn 
261ecaecbc7SIan Lepore device_t
262ecaecbc7SIan Lepore simplebus_add_device(device_t dev, phandle_t node, u_int order,
263ecaecbc7SIan Lepore     const char *name, int unit, struct simplebus_devinfo *di)
264ecaecbc7SIan Lepore {
265ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
266ecaecbc7SIan Lepore 	device_t cdev;
267ecaecbc7SIan Lepore 
268ecaecbc7SIan Lepore 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
269ecaecbc7SIan Lepore 		return (NULL);
270ecaecbc7SIan Lepore 	cdev = device_add_child_ordered(dev, order, name, unit);
271ecaecbc7SIan Lepore 	if (cdev == NULL) {
272ecaecbc7SIan Lepore 		device_printf(dev, "<%s>: device_add_child failed\n",
273ecaecbc7SIan Lepore 		    ndi->obdinfo.obd_name);
274ecaecbc7SIan Lepore 		resource_list_free(&ndi->rl);
275ecaecbc7SIan Lepore 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
276ecaecbc7SIan Lepore 		if (di == NULL)
277ecaecbc7SIan Lepore 			free(ndi, M_DEVBUF);
278ecaecbc7SIan Lepore 		return (NULL);
279ecaecbc7SIan Lepore 	}
280ecaecbc7SIan Lepore 	device_set_ivars(cdev, ndi);
281ecaecbc7SIan Lepore 
282ecaecbc7SIan Lepore 	return(cdev);
283ecaecbc7SIan Lepore }
284ecaecbc7SIan Lepore 
285ecaecbc7SIan Lepore static device_t
286ecaecbc7SIan Lepore simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
287ecaecbc7SIan Lepore {
288ecaecbc7SIan Lepore 	device_t cdev;
289ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
290ecaecbc7SIan Lepore 
291ecaecbc7SIan Lepore 	cdev = device_add_child_ordered(dev, order, name, unit);
292ecaecbc7SIan Lepore 	if (cdev == NULL)
293ecaecbc7SIan Lepore 		return (NULL);
294ecaecbc7SIan Lepore 
295ecaecbc7SIan Lepore 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
296ecaecbc7SIan Lepore 	ndi->obdinfo.obd_node = -1;
297ecaecbc7SIan Lepore 	resource_list_init(&ndi->rl);
298ecaecbc7SIan Lepore 	device_set_ivars(cdev, ndi);
299ecaecbc7SIan Lepore 
300ecaecbc7SIan Lepore 	return (cdev);
301ecaecbc7SIan Lepore }
302ecaecbc7SIan Lepore 
30306763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *
30406763f5eSNathan Whitehorn simplebus_get_devinfo(device_t bus __unused, device_t child)
30506763f5eSNathan Whitehorn {
30606763f5eSNathan Whitehorn         struct simplebus_devinfo *ndi;
30706763f5eSNathan Whitehorn 
30806763f5eSNathan Whitehorn         ndi = device_get_ivars(child);
3096fc608bfSMichal Meloun 	if (ndi == NULL)
3106fc608bfSMichal Meloun 		return (NULL);
31106763f5eSNathan Whitehorn         return (&ndi->obdinfo);
31258158742SRafal Jaworowski }
31358158742SRafal Jaworowski 
314ecaecbc7SIan Lepore static struct resource_list *
315ecaecbc7SIan Lepore simplebus_get_resource_list(device_t bus __unused, device_t child)
316ecaecbc7SIan Lepore {
317ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
318ecaecbc7SIan Lepore 
319ecaecbc7SIan Lepore 	ndi = device_get_ivars(child);
3206fc608bfSMichal Meloun 	if (ndi == NULL)
3216fc608bfSMichal Meloun 		return (NULL);
322ecaecbc7SIan Lepore 	return (&ndi->rl);
323ecaecbc7SIan Lepore }
324ecaecbc7SIan Lepore 
32558158742SRafal Jaworowski static struct resource *
32658158742SRafal Jaworowski simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
3272dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
32858158742SRafal Jaworowski {
32906763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
33058158742SRafal Jaworowski 	struct simplebus_devinfo *di;
33158158742SRafal Jaworowski 	struct resource_list_entry *rle;
33206763f5eSNathan Whitehorn 	int j;
33306763f5eSNathan Whitehorn 
33406763f5eSNathan Whitehorn 	sc = device_get_softc(bus);
33558158742SRafal Jaworowski 
33658158742SRafal Jaworowski 	/*
33758158742SRafal Jaworowski 	 * Request for the default allocation with a given rid: use resource
33858158742SRafal Jaworowski 	 * list stored in the local device info.
33958158742SRafal Jaworowski 	 */
3407915adb5SJustin Hibbits 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
34158158742SRafal Jaworowski 		if ((di = device_get_ivars(child)) == NULL)
34258158742SRafal Jaworowski 			return (NULL);
34358158742SRafal Jaworowski 
34458158742SRafal Jaworowski 		if (type == SYS_RES_IOPORT)
34558158742SRafal Jaworowski 			type = SYS_RES_MEMORY;
34658158742SRafal Jaworowski 
34706763f5eSNathan Whitehorn 		rle = resource_list_find(&di->rl, type, *rid);
34858158742SRafal Jaworowski 		if (rle == NULL) {
349089dfb09SAleksandr Rybalko 			if (bootverbose)
35058158742SRafal Jaworowski 				device_printf(bus, "no default resources for "
35158158742SRafal Jaworowski 				    "rid = %d, type = %d\n", *rid, type);
35258158742SRafal Jaworowski 			return (NULL);
35358158742SRafal Jaworowski 		}
35458158742SRafal Jaworowski 		start = rle->start;
35558158742SRafal Jaworowski 		end = rle->end;
35658158742SRafal Jaworowski 		count = rle->count;
35758158742SRafal Jaworowski         }
35858158742SRafal Jaworowski 
35906763f5eSNathan Whitehorn 	if (type == SYS_RES_MEMORY) {
36006763f5eSNathan Whitehorn 		/* Remap through ranges property */
36106763f5eSNathan Whitehorn 		for (j = 0; j < sc->nranges; j++) {
36206763f5eSNathan Whitehorn 			if (start >= sc->ranges[j].bus && end <
36306763f5eSNathan Whitehorn 			    sc->ranges[j].bus + sc->ranges[j].size) {
36406763f5eSNathan Whitehorn 				start -= sc->ranges[j].bus;
36506763f5eSNathan Whitehorn 				start += sc->ranges[j].host;
36606763f5eSNathan Whitehorn 				end -= sc->ranges[j].bus;
36706763f5eSNathan Whitehorn 				end += sc->ranges[j].host;
36806763f5eSNathan Whitehorn 				break;
36906763f5eSNathan Whitehorn 			}
37006763f5eSNathan Whitehorn 		}
37106763f5eSNathan Whitehorn 		if (j == sc->nranges && sc->nranges != 0) {
37206763f5eSNathan Whitehorn 			if (bootverbose)
37306763f5eSNathan Whitehorn 				device_printf(bus, "Could not map resource "
374da1b038aSJustin Hibbits 				    "%#jx-%#jx\n", start, end);
37506763f5eSNathan Whitehorn 
37606763f5eSNathan Whitehorn 			return (NULL);
37706763f5eSNathan Whitehorn 		}
37806763f5eSNathan Whitehorn 	}
3791f40dbc8SBrooks Davis 
38058158742SRafal Jaworowski 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
38158158742SRafal Jaworowski 	    count, flags));
38258158742SRafal Jaworowski }
38358158742SRafal Jaworowski 
3841f40dbc8SBrooks Davis static int
38506763f5eSNathan Whitehorn simplebus_print_res(struct simplebus_devinfo *di)
3861f40dbc8SBrooks Davis {
38706763f5eSNathan Whitehorn 	int rv;
3881f40dbc8SBrooks Davis 
3896fc608bfSMichal Meloun 	if (di == NULL)
3906fc608bfSMichal Meloun 		return (0);
39106763f5eSNathan Whitehorn 	rv = 0;
392da1b038aSJustin Hibbits 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
393da1b038aSJustin Hibbits 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
39406763f5eSNathan Whitehorn 	return (rv);
39506763f5eSNathan Whitehorn }
3961f40dbc8SBrooks Davis 
39706763f5eSNathan Whitehorn static void
39806763f5eSNathan Whitehorn simplebus_probe_nomatch(device_t bus, device_t child)
39906763f5eSNathan Whitehorn {
400cca75397SWarner Losh 	const char *name, *type, *compat;
40106763f5eSNathan Whitehorn 
40206763f5eSNathan Whitehorn 	if (!bootverbose)
40306763f5eSNathan Whitehorn 		return;
40406763f5eSNathan Whitehorn 
405ecaecbc7SIan Lepore 	compat = ofw_bus_get_compat(child);
406ecaecbc7SIan Lepore 	if (compat == NULL)
407ecaecbc7SIan Lepore 		return;
40806763f5eSNathan Whitehorn 	name = ofw_bus_get_name(child);
40906763f5eSNathan Whitehorn 	type = ofw_bus_get_type(child);
41006763f5eSNathan Whitehorn 
41106763f5eSNathan Whitehorn 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
41206763f5eSNathan Whitehorn 	simplebus_print_res(device_get_ivars(child));
413cca75397SWarner Losh 	if (!ofw_bus_status_okay(child))
414cca75397SWarner Losh 		printf(" disabled");
415cca75397SWarner Losh 	if (type)
416cca75397SWarner Losh 		printf(" type %s", type);
417ecaecbc7SIan Lepore 	printf(" compat %s (no driver attached)\n", compat);
4181f40dbc8SBrooks Davis }
4191f40dbc8SBrooks Davis 
4201f40dbc8SBrooks Davis static int
42106763f5eSNathan Whitehorn simplebus_print_child(device_t bus, device_t child)
4221f40dbc8SBrooks Davis {
42306763f5eSNathan Whitehorn 	int rv;
4241f40dbc8SBrooks Davis 
42506763f5eSNathan Whitehorn 	rv = bus_print_child_header(bus, child);
42606763f5eSNathan Whitehorn 	rv += simplebus_print_res(device_get_ivars(child));
427cca75397SWarner Losh 	if (!ofw_bus_status_okay(child))
428cca75397SWarner Losh 		rv += printf(" disabled");
42906763f5eSNathan Whitehorn 	rv += bus_print_child_footer(bus, child);
43006763f5eSNathan Whitehorn 	return (rv);
4311f40dbc8SBrooks Davis }
432