xref: /freebsd/sys/dev/fdt/simplebus.c (revision 06763f5e55540ddfba295f8e6a40b4f11de90e4f)
158158742SRafal Jaworowski /*-
2*06763f5eSNathan Whitehorn  * Copyright (c) 2013 Nathan Whitehorn
358158742SRafal Jaworowski  * All rights reserved.
458158742SRafal Jaworowski  *
558158742SRafal Jaworowski  * Redistribution and use in source and binary forms, with or without
658158742SRafal Jaworowski  * modification, are permitted provided that the following conditions
758158742SRafal Jaworowski  * are met:
858158742SRafal Jaworowski  * 1. Redistributions of source code must retain the above copyright
958158742SRafal Jaworowski  *    notice, this list of conditions and the following disclaimer.
1058158742SRafal Jaworowski  * 2. Redistributions in binary form must reproduce the above copyright
1158158742SRafal Jaworowski  *    notice, this list of conditions and the following disclaimer in the
1258158742SRafal Jaworowski  *    documentation and/or other materials provided with the distribution.
1358158742SRafal Jaworowski  *
1458158742SRafal Jaworowski  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1558158742SRafal Jaworowski  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1658158742SRafal Jaworowski  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1758158742SRafal Jaworowski  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1858158742SRafal Jaworowski  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1958158742SRafal Jaworowski  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2058158742SRafal Jaworowski  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2158158742SRafal Jaworowski  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2258158742SRafal Jaworowski  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2358158742SRafal Jaworowski  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2458158742SRafal Jaworowski  * SUCH DAMAGE.
2558158742SRafal Jaworowski  */
2658158742SRafal Jaworowski 
2758158742SRafal Jaworowski #include <sys/cdefs.h>
2858158742SRafal Jaworowski __FBSDID("$FreeBSD$");
2958158742SRafal Jaworowski #include <sys/param.h>
3058158742SRafal Jaworowski #include <sys/systm.h>
3158158742SRafal Jaworowski #include <sys/module.h>
3258158742SRafal Jaworowski #include <sys/bus.h>
33*06763f5eSNathan Whitehorn #include <sys/conf.h>
34*06763f5eSNathan Whitehorn #include <sys/kernel.h>
3558158742SRafal Jaworowski #include <sys/rman.h>
3658158742SRafal Jaworowski 
37*06763f5eSNathan Whitehorn #include <dev/ofw/openfirm.h>
3858158742SRafal Jaworowski #include <dev/ofw/ofw_bus.h>
3958158742SRafal Jaworowski #include <dev/ofw/ofw_bus_subr.h>
4058158742SRafal Jaworowski 
41*06763f5eSNathan Whitehorn struct simplebus_range {
42*06763f5eSNathan Whitehorn 	uint64_t bus;
43*06763f5eSNathan Whitehorn 	uint64_t host;
44*06763f5eSNathan Whitehorn 	uint64_t size;
45*06763f5eSNathan Whitehorn };
4658158742SRafal Jaworowski 
4758158742SRafal Jaworowski struct simplebus_softc {
48*06763f5eSNathan Whitehorn 	device_t dev;
49*06763f5eSNathan Whitehorn 	phandle_t node;
50*06763f5eSNathan Whitehorn 
51*06763f5eSNathan Whitehorn 	struct simplebus_range *ranges;
52*06763f5eSNathan Whitehorn 	int nranges;
53*06763f5eSNathan Whitehorn 
54*06763f5eSNathan Whitehorn 	pcell_t acells, scells;
5558158742SRafal Jaworowski };
5658158742SRafal Jaworowski 
5758158742SRafal Jaworowski struct simplebus_devinfo {
58*06763f5eSNathan Whitehorn 	struct ofw_bus_devinfo	obdinfo;
59*06763f5eSNathan Whitehorn 	struct resource_list	rl;
6058158742SRafal Jaworowski };
6158158742SRafal Jaworowski 
6258158742SRafal Jaworowski /*
63*06763f5eSNathan Whitehorn  * Bus interface.
6458158742SRafal Jaworowski  */
65*06763f5eSNathan Whitehorn static int		simplebus_probe(device_t dev);
66*06763f5eSNathan Whitehorn static int		simplebus_attach(device_t dev);
6758158742SRafal Jaworowski static struct resource *simplebus_alloc_resource(device_t, device_t, int,
6858158742SRafal Jaworowski     int *, u_long, u_long, u_long, u_int);
69*06763f5eSNathan Whitehorn static void		simplebus_probe_nomatch(device_t bus, device_t child);
70*06763f5eSNathan Whitehorn static int		simplebus_print_child(device_t bus, device_t child);
7158158742SRafal Jaworowski 
7258158742SRafal Jaworowski /*
73*06763f5eSNathan Whitehorn  * ofw_bus interface
74*06763f5eSNathan Whitehorn  */
75*06763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
76*06763f5eSNathan Whitehorn     device_t child);
77*06763f5eSNathan Whitehorn 
78*06763f5eSNathan Whitehorn /*
79*06763f5eSNathan Whitehorn  * local methods
80*06763f5eSNathan Whitehorn  */
81*06763f5eSNathan Whitehorn 
82*06763f5eSNathan Whitehorn static int simplebus_fill_ranges(phandle_t node,
83*06763f5eSNathan Whitehorn     struct simplebus_softc *sc);
84*06763f5eSNathan Whitehorn static struct simplebus_devinfo *simplebus_setup_dinfo(device_t dev,
85*06763f5eSNathan Whitehorn     phandle_t node);
86*06763f5eSNathan Whitehorn 
87*06763f5eSNathan Whitehorn /*
88*06763f5eSNathan Whitehorn  * Driver methods.
8958158742SRafal Jaworowski  */
9058158742SRafal Jaworowski static device_method_t	simplebus_methods[] = {
9158158742SRafal Jaworowski 	/* Device interface */
9258158742SRafal Jaworowski 	DEVMETHOD(device_probe,		simplebus_probe),
9358158742SRafal Jaworowski 	DEVMETHOD(device_attach,	simplebus_attach),
9458158742SRafal Jaworowski 
9558158742SRafal Jaworowski 	/* Bus interface */
9658158742SRafal Jaworowski 	DEVMETHOD(bus_print_child,	simplebus_print_child),
97*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
98*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
99*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
10058158742SRafal Jaworowski 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
101*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
102*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
103*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
104*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
105*06763f5eSNathan Whitehorn 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
10658158742SRafal Jaworowski 
107*06763f5eSNathan Whitehorn 	/* ofw_bus interface */
10858158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
10958158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
11058158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
11158158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
11258158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
11358158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
11458158742SRafal Jaworowski 
115*06763f5eSNathan Whitehorn 	DEVMETHOD_END
11658158742SRafal Jaworowski };
11758158742SRafal Jaworowski 
11858158742SRafal Jaworowski static driver_t simplebus_driver = {
11958158742SRafal Jaworowski 	"simplebus",
12058158742SRafal Jaworowski 	simplebus_methods,
12158158742SRafal Jaworowski 	sizeof(struct simplebus_softc)
12258158742SRafal Jaworowski };
123*06763f5eSNathan Whitehorn static devclass_t simplebus_devclass;
1245cd2b97cSNathan Whitehorn DRIVER_MODULE(simplebus, nexus, simplebus_driver, simplebus_devclass, 0, 0);
12558158742SRafal Jaworowski 
12658158742SRafal Jaworowski static int
12758158742SRafal Jaworowski simplebus_probe(device_t dev)
12858158742SRafal Jaworowski {
12958158742SRafal Jaworowski 
130*06763f5eSNathan Whitehorn 	if (!ofw_bus_is_compatible(dev, "simple-bus") &&
131*06763f5eSNathan Whitehorn 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
132*06763f5eSNathan Whitehorn 	     "soc") != 0))
13358158742SRafal Jaworowski 		return (ENXIO);
13458158742SRafal Jaworowski 
13558158742SRafal Jaworowski 	device_set_desc(dev, "Flattened device tree simple bus");
13658158742SRafal Jaworowski 
1372737a5a9SAleksandr Rybalko 	return (BUS_PROBE_GENERIC);
13858158742SRafal Jaworowski }
13958158742SRafal Jaworowski 
14058158742SRafal Jaworowski static int
14158158742SRafal Jaworowski simplebus_attach(device_t dev)
14258158742SRafal Jaworowski {
14358158742SRafal Jaworowski 	struct		simplebus_softc *sc;
144*06763f5eSNathan Whitehorn 	struct		simplebus_devinfo *di;
145*06763f5eSNathan Whitehorn 	phandle_t	node;
146*06763f5eSNathan Whitehorn 	device_t	cdev;
14758158742SRafal Jaworowski 
148*06763f5eSNathan Whitehorn 	node = ofw_bus_get_node(dev);
14958158742SRafal Jaworowski 	sc = device_get_softc(dev);
15058158742SRafal Jaworowski 
151*06763f5eSNathan Whitehorn 	sc->dev = dev;
152*06763f5eSNathan Whitehorn 	sc->node = node;
153*06763f5eSNathan Whitehorn 
15458158742SRafal Jaworowski 	/*
155*06763f5eSNathan Whitehorn 	 * Some important numbers
15658158742SRafal Jaworowski 	 */
157*06763f5eSNathan Whitehorn 	sc->acells = 2;
158*06763f5eSNathan Whitehorn 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
159*06763f5eSNathan Whitehorn 	sc->scells = 1;
160*06763f5eSNathan Whitehorn 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
16158158742SRafal Jaworowski 
162*06763f5eSNathan Whitehorn 	if (simplebus_fill_ranges(node, sc) < 0) {
163*06763f5eSNathan Whitehorn 		device_printf(dev, "could not get ranges\n");
164*06763f5eSNathan Whitehorn 		return (ENXIO);
16558158742SRafal Jaworowski 	}
16658158742SRafal Jaworowski 
167*06763f5eSNathan Whitehorn 	/*
168*06763f5eSNathan Whitehorn 	 * In principle, simplebus could have an interrupt map, but ignore that
169*06763f5eSNathan Whitehorn 	 * for now
170*06763f5eSNathan Whitehorn 	 */
17158158742SRafal Jaworowski 
172*06763f5eSNathan Whitehorn 	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
173*06763f5eSNathan Whitehorn 		if ((di = simplebus_setup_dinfo(dev, node)) == NULL)
174*06763f5eSNathan Whitehorn 			continue;
175*06763f5eSNathan Whitehorn 		cdev = device_add_child(dev, NULL, -1);
176*06763f5eSNathan Whitehorn 		if (cdev == NULL) {
177*06763f5eSNathan Whitehorn 			device_printf(dev, "<%s>: device_add_child failed\n",
178*06763f5eSNathan Whitehorn 			    di->obdinfo.obd_name);
179*06763f5eSNathan Whitehorn 			resource_list_free(&di->rl);
180*06763f5eSNathan Whitehorn 			ofw_bus_gen_destroy_devinfo(&di->obdinfo);
181*06763f5eSNathan Whitehorn 			free(di, M_DEVBUF);
18258158742SRafal Jaworowski 			continue;
18358158742SRafal Jaworowski 		}
184*06763f5eSNathan Whitehorn 		device_set_ivars(cdev, di);
18558158742SRafal Jaworowski 	}
18658158742SRafal Jaworowski 
18758158742SRafal Jaworowski 	return (bus_generic_attach(dev));
18858158742SRafal Jaworowski }
18958158742SRafal Jaworowski 
19058158742SRafal Jaworowski static int
191*06763f5eSNathan Whitehorn simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
19258158742SRafal Jaworowski {
193*06763f5eSNathan Whitehorn 	int host_address_cells;
194*06763f5eSNathan Whitehorn 	cell_t *base_ranges;
195*06763f5eSNathan Whitehorn 	ssize_t nbase_ranges;
196*06763f5eSNathan Whitehorn 	int err;
197*06763f5eSNathan Whitehorn 	int i, j, k;
19858158742SRafal Jaworowski 
199*06763f5eSNathan Whitehorn 	err = OF_searchencprop(OF_parent(node), "#address-cells",
200*06763f5eSNathan Whitehorn 	    &host_address_cells, sizeof(host_address_cells));
201*06763f5eSNathan Whitehorn 	if (err <= 0)
202*06763f5eSNathan Whitehorn 		return (-1);
20358158742SRafal Jaworowski 
204*06763f5eSNathan Whitehorn 	nbase_ranges = OF_getproplen(node, "ranges");
205*06763f5eSNathan Whitehorn 	if (nbase_ranges < 0)
206*06763f5eSNathan Whitehorn 		return (-1);
207*06763f5eSNathan Whitehorn 	sc->nranges = nbase_ranges / sizeof(cell_t) /
208*06763f5eSNathan Whitehorn 	    (sc->acells + host_address_cells + sc->scells);
209*06763f5eSNathan Whitehorn 	if (sc->nranges == 0)
210*06763f5eSNathan Whitehorn 		return (0);
21158158742SRafal Jaworowski 
212*06763f5eSNathan Whitehorn 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
213*06763f5eSNathan Whitehorn 	    M_DEVBUF, M_WAITOK);
214*06763f5eSNathan Whitehorn 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
215*06763f5eSNathan Whitehorn 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
216*06763f5eSNathan Whitehorn 
217*06763f5eSNathan Whitehorn 	for (i = 0, j = 0; i < sc->nranges; i++) {
218*06763f5eSNathan Whitehorn 		sc->ranges[i].bus = 0;
219*06763f5eSNathan Whitehorn 		for (k = 0; k < sc->acells; k++) {
220*06763f5eSNathan Whitehorn 			sc->ranges[i].bus <<= 32;
221*06763f5eSNathan Whitehorn 			sc->ranges[i].bus |= base_ranges[j++];
222*06763f5eSNathan Whitehorn 		}
223*06763f5eSNathan Whitehorn 		sc->ranges[i].host = 0;
224*06763f5eSNathan Whitehorn 		for (k = 0; k < host_address_cells; k++) {
225*06763f5eSNathan Whitehorn 			sc->ranges[i].host <<= 32;
226*06763f5eSNathan Whitehorn 			sc->ranges[i].host |= base_ranges[j++];
227*06763f5eSNathan Whitehorn 		}
228*06763f5eSNathan Whitehorn 		sc->ranges[i].size = 0;
229*06763f5eSNathan Whitehorn 		for (k = 0; k < sc->scells; k++) {
230*06763f5eSNathan Whitehorn 			sc->ranges[i].size <<= 32;
231*06763f5eSNathan Whitehorn 			sc->ranges[i].size |= base_ranges[j++];
232*06763f5eSNathan Whitehorn 		}
233*06763f5eSNathan Whitehorn 	}
234*06763f5eSNathan Whitehorn 
235*06763f5eSNathan Whitehorn 	free(base_ranges, M_DEVBUF);
236*06763f5eSNathan Whitehorn 	return (sc->nranges);
237*06763f5eSNathan Whitehorn }
238*06763f5eSNathan Whitehorn 
239*06763f5eSNathan Whitehorn static struct simplebus_devinfo *
240*06763f5eSNathan Whitehorn simplebus_setup_dinfo(device_t dev, phandle_t node)
241*06763f5eSNathan Whitehorn {
242*06763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
243*06763f5eSNathan Whitehorn 	struct simplebus_devinfo *ndi;
244*06763f5eSNathan Whitehorn 	uint32_t *reg, *intr, icells;
245*06763f5eSNathan Whitehorn 	uint64_t phys, size;
246*06763f5eSNathan Whitehorn 	phandle_t iparent;
247*06763f5eSNathan Whitehorn 	int i, j, k;
248*06763f5eSNathan Whitehorn 	int nintr;
249*06763f5eSNathan Whitehorn 	int nreg;
250*06763f5eSNathan Whitehorn 
251*06763f5eSNathan Whitehorn 	sc = device_get_softc(dev);
252*06763f5eSNathan Whitehorn 
253*06763f5eSNathan Whitehorn 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
254*06763f5eSNathan Whitehorn 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
255*06763f5eSNathan Whitehorn 		free(ndi, M_DEVBUF);
256*06763f5eSNathan Whitehorn 		return (NULL);
257*06763f5eSNathan Whitehorn 	}
258*06763f5eSNathan Whitehorn 
259*06763f5eSNathan Whitehorn 	resource_list_init(&ndi->rl);
260*06763f5eSNathan Whitehorn 	nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
261*06763f5eSNathan Whitehorn 	if (nreg == -1)
262*06763f5eSNathan Whitehorn 		nreg = 0;
263*06763f5eSNathan Whitehorn 	if (nreg % (sc->acells + sc->scells) != 0) {
264*06763f5eSNathan Whitehorn 		if (bootverbose)
265*06763f5eSNathan Whitehorn 			device_printf(dev, "Malformed reg property on <%s>\n",
266*06763f5eSNathan Whitehorn 			    ndi->obdinfo.obd_name);
267*06763f5eSNathan Whitehorn 		nreg = 0;
268*06763f5eSNathan Whitehorn 	}
269*06763f5eSNathan Whitehorn 
270*06763f5eSNathan Whitehorn 	for (i = 0, k = 0; i < nreg; i += sc->acells + sc->scells, k++) {
271*06763f5eSNathan Whitehorn 		phys = size = 0;
272*06763f5eSNathan Whitehorn 		for (j = 0; j < sc->acells; j++) {
273*06763f5eSNathan Whitehorn 			phys <<= 32;
274*06763f5eSNathan Whitehorn 			phys |= reg[i + j];
275*06763f5eSNathan Whitehorn 		}
276*06763f5eSNathan Whitehorn 		for (j = 0; j < sc->scells; j++) {
277*06763f5eSNathan Whitehorn 			size <<= 32;
278*06763f5eSNathan Whitehorn 			size |= reg[i + sc->acells + j];
279*06763f5eSNathan Whitehorn 		}
280*06763f5eSNathan Whitehorn 
281*06763f5eSNathan Whitehorn 		resource_list_add(&ndi->rl, SYS_RES_MEMORY, k,
282*06763f5eSNathan Whitehorn 		    phys, phys + size - 1, size);
283*06763f5eSNathan Whitehorn 	}
284*06763f5eSNathan Whitehorn 	free(reg, M_OFWPROP);
285*06763f5eSNathan Whitehorn 
286*06763f5eSNathan Whitehorn 	nintr = OF_getencprop_alloc(node, "interrupts",  sizeof(*intr),
287*06763f5eSNathan Whitehorn 	    (void **)&intr);
288*06763f5eSNathan Whitehorn 	if (nintr > 0) {
289*06763f5eSNathan Whitehorn 		iparent = 0;
290*06763f5eSNathan Whitehorn 		OF_searchencprop(node, "interrupt-parent", &iparent,
291*06763f5eSNathan Whitehorn 		    sizeof(iparent));
292*06763f5eSNathan Whitehorn 		OF_searchencprop(OF_xref_phandle(iparent), "#interrupt-cells",
293*06763f5eSNathan Whitehorn 		    &icells, sizeof(icells));
294*06763f5eSNathan Whitehorn 		for (i = 0, k = 0; i < nintr; i += icells, k++) {
295*06763f5eSNathan Whitehorn 			intr[i] = ofw_bus_map_intr(dev, iparent, icells,
296*06763f5eSNathan Whitehorn 			    &intr[i]);
297*06763f5eSNathan Whitehorn 			resource_list_add(&ndi->rl, SYS_RES_IRQ, k, intr[i],
298*06763f5eSNathan Whitehorn 			    intr[i], 1);
299*06763f5eSNathan Whitehorn 		}
300*06763f5eSNathan Whitehorn 		free(intr, M_OFWPROP);
301*06763f5eSNathan Whitehorn 	}
302*06763f5eSNathan Whitehorn 
303*06763f5eSNathan Whitehorn 	return (ndi);
304*06763f5eSNathan Whitehorn }
305*06763f5eSNathan Whitehorn 
306*06763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *
307*06763f5eSNathan Whitehorn simplebus_get_devinfo(device_t bus __unused, device_t child)
308*06763f5eSNathan Whitehorn {
309*06763f5eSNathan Whitehorn         struct simplebus_devinfo *ndi;
310*06763f5eSNathan Whitehorn 
311*06763f5eSNathan Whitehorn         ndi = device_get_ivars(child);
312*06763f5eSNathan Whitehorn         return (&ndi->obdinfo);
31358158742SRafal Jaworowski }
31458158742SRafal Jaworowski 
31558158742SRafal Jaworowski static struct resource *
31658158742SRafal Jaworowski simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
31758158742SRafal Jaworowski     u_long start, u_long end, u_long count, u_int flags)
31858158742SRafal Jaworowski {
319*06763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
32058158742SRafal Jaworowski 	struct simplebus_devinfo *di;
32158158742SRafal Jaworowski 	struct resource_list_entry *rle;
322*06763f5eSNathan Whitehorn 	int j;
323*06763f5eSNathan Whitehorn 
324*06763f5eSNathan Whitehorn 	sc = device_get_softc(bus);
32558158742SRafal Jaworowski 
32658158742SRafal Jaworowski 	/*
32758158742SRafal Jaworowski 	 * Request for the default allocation with a given rid: use resource
32858158742SRafal Jaworowski 	 * list stored in the local device info.
32958158742SRafal Jaworowski 	 */
33058158742SRafal Jaworowski 	if ((start == 0UL) && (end == ~0UL)) {
33158158742SRafal Jaworowski 		if ((di = device_get_ivars(child)) == NULL)
33258158742SRafal Jaworowski 			return (NULL);
33358158742SRafal Jaworowski 
33458158742SRafal Jaworowski 		if (type == SYS_RES_IOPORT)
33558158742SRafal Jaworowski 			type = SYS_RES_MEMORY;
33658158742SRafal Jaworowski 
337*06763f5eSNathan Whitehorn 		rle = resource_list_find(&di->rl, type, *rid);
33858158742SRafal Jaworowski 		if (rle == NULL) {
339089dfb09SAleksandr Rybalko 			if (bootverbose)
34058158742SRafal Jaworowski 				device_printf(bus, "no default resources for "
34158158742SRafal Jaworowski 				    "rid = %d, type = %d\n", *rid, type);
34258158742SRafal Jaworowski 			return (NULL);
34358158742SRafal Jaworowski 		}
34458158742SRafal Jaworowski 		start = rle->start;
34558158742SRafal Jaworowski 		end = rle->end;
34658158742SRafal Jaworowski 		count = rle->count;
34758158742SRafal Jaworowski         }
34858158742SRafal Jaworowski 
349*06763f5eSNathan Whitehorn 	if (type == SYS_RES_MEMORY) {
350*06763f5eSNathan Whitehorn 		/* Remap through ranges property */
351*06763f5eSNathan Whitehorn 		for (j = 0; j < sc->nranges; j++) {
352*06763f5eSNathan Whitehorn 			if (start >= sc->ranges[j].bus && end <
353*06763f5eSNathan Whitehorn 			    sc->ranges[j].bus + sc->ranges[j].size) {
354*06763f5eSNathan Whitehorn 				start -= sc->ranges[j].bus;
355*06763f5eSNathan Whitehorn 				start += sc->ranges[j].host;
356*06763f5eSNathan Whitehorn 				end -= sc->ranges[j].bus;
357*06763f5eSNathan Whitehorn 				end += sc->ranges[j].host;
358*06763f5eSNathan Whitehorn 				break;
359*06763f5eSNathan Whitehorn 			}
360*06763f5eSNathan Whitehorn 		}
361*06763f5eSNathan Whitehorn 		if (j == sc->nranges && sc->nranges != 0) {
362*06763f5eSNathan Whitehorn 			if (bootverbose)
363*06763f5eSNathan Whitehorn 				device_printf(bus, "Could not map resource "
364*06763f5eSNathan Whitehorn 				    "%#lx-%#lx\n", start, end);
365*06763f5eSNathan Whitehorn 
366*06763f5eSNathan Whitehorn 			return (NULL);
367*06763f5eSNathan Whitehorn 		}
368*06763f5eSNathan Whitehorn 	}
3691f40dbc8SBrooks Davis 
37058158742SRafal Jaworowski 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
37158158742SRafal Jaworowski 	    count, flags));
37258158742SRafal Jaworowski }
37358158742SRafal Jaworowski 
3741f40dbc8SBrooks Davis static int
375*06763f5eSNathan Whitehorn simplebus_print_res(struct simplebus_devinfo *di)
3761f40dbc8SBrooks Davis {
377*06763f5eSNathan Whitehorn 	int rv;
3781f40dbc8SBrooks Davis 
379*06763f5eSNathan Whitehorn 	rv = 0;
380*06763f5eSNathan Whitehorn 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#lx");
381*06763f5eSNathan Whitehorn 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%ld");
382*06763f5eSNathan Whitehorn 	return (rv);
383*06763f5eSNathan Whitehorn }
3841f40dbc8SBrooks Davis 
385*06763f5eSNathan Whitehorn static void
386*06763f5eSNathan Whitehorn simplebus_probe_nomatch(device_t bus, device_t child)
387*06763f5eSNathan Whitehorn {
388*06763f5eSNathan Whitehorn 	const char *name, *type;
389*06763f5eSNathan Whitehorn 
390*06763f5eSNathan Whitehorn 	if (!bootverbose)
391*06763f5eSNathan Whitehorn 		return;
392*06763f5eSNathan Whitehorn 
393*06763f5eSNathan Whitehorn 	name = ofw_bus_get_name(child);
394*06763f5eSNathan Whitehorn 	type = ofw_bus_get_type(child);
395*06763f5eSNathan Whitehorn 
396*06763f5eSNathan Whitehorn 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
397*06763f5eSNathan Whitehorn 	simplebus_print_res(device_get_ivars(child));
398*06763f5eSNathan Whitehorn 	printf(" type %s (no driver attached)\n",
399*06763f5eSNathan Whitehorn 	    type != NULL ? type : "unknown");
4001f40dbc8SBrooks Davis }
4011f40dbc8SBrooks Davis 
4021f40dbc8SBrooks Davis static int
403*06763f5eSNathan Whitehorn simplebus_print_child(device_t bus, device_t child)
4041f40dbc8SBrooks Davis {
405*06763f5eSNathan Whitehorn 	int rv;
4061f40dbc8SBrooks Davis 
407*06763f5eSNathan Whitehorn 	rv = bus_print_child_header(bus, child);
408*06763f5eSNathan Whitehorn 	rv += simplebus_print_res(device_get_ivars(child));
409*06763f5eSNathan Whitehorn 	rv += bus_print_child_footer(bus, child);
410*06763f5eSNathan Whitehorn 	return (rv);
4111f40dbc8SBrooks Davis }
4121f40dbc8SBrooks Davis 
413