xref: /freebsd/sys/dev/fdt/simplebus.c (revision 8dc348a47965722ce5302636f1262d8516fba2c3)
158158742SRafal Jaworowski /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro 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);
4958158742SRafal Jaworowski static struct resource *simplebus_alloc_resource(device_t, device_t, int,
502dd1bdf1SJustin Hibbits     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
5106763f5eSNathan Whitehorn static void		simplebus_probe_nomatch(device_t bus, device_t child);
5206763f5eSNathan Whitehorn static int		simplebus_print_child(device_t bus, device_t child);
53ecaecbc7SIan Lepore static device_t		simplebus_add_child(device_t dev, u_int order,
54ecaecbc7SIan Lepore     const char *name, int unit);
55ecaecbc7SIan Lepore static struct resource_list *simplebus_get_resource_list(device_t bus,
56ecaecbc7SIan Lepore     device_t child);
5758158742SRafal Jaworowski /*
5806763f5eSNathan Whitehorn  * ofw_bus interface
5906763f5eSNathan Whitehorn  */
6006763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
6106763f5eSNathan Whitehorn     device_t child);
6206763f5eSNathan Whitehorn 
6306763f5eSNathan Whitehorn /*
6406763f5eSNathan Whitehorn  * Driver methods.
6558158742SRafal Jaworowski  */
6658158742SRafal Jaworowski static device_method_t	simplebus_methods[] = {
6758158742SRafal Jaworowski 	/* Device interface */
6858158742SRafal Jaworowski 	DEVMETHOD(device_probe,		simplebus_probe),
6958158742SRafal Jaworowski 	DEVMETHOD(device_attach,	simplebus_attach),
70b95a8021SMichal Meloun 	DEVMETHOD(device_detach,	simplebus_detach),
71ecaecbc7SIan Lepore 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
72ecaecbc7SIan Lepore 	DEVMETHOD(device_suspend,	bus_generic_suspend),
73ecaecbc7SIan Lepore 	DEVMETHOD(device_resume,	bus_generic_resume),
7458158742SRafal Jaworowski 
7558158742SRafal Jaworowski 	/* Bus interface */
76ecaecbc7SIan Lepore 	DEVMETHOD(bus_add_child,	simplebus_add_child),
7758158742SRafal Jaworowski 	DEVMETHOD(bus_print_child,	simplebus_print_child),
7806763f5eSNathan Whitehorn 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
79ecaecbc7SIan Lepore 	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
80ecaecbc7SIan Lepore 	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
8106763f5eSNathan Whitehorn 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
8206763f5eSNathan Whitehorn 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
8358158742SRafal Jaworowski 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
8406763f5eSNathan Whitehorn 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
8506763f5eSNathan Whitehorn 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
8606763f5eSNathan Whitehorn 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
8706763f5eSNathan Whitehorn 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
88ecaecbc7SIan Lepore 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
89ecaecbc7SIan Lepore 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
9006763f5eSNathan Whitehorn 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
91ecaecbc7SIan Lepore 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
9258158742SRafal Jaworowski 
9306763f5eSNathan Whitehorn 	/* ofw_bus interface */
9458158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
9558158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
9658158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
9758158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
9858158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
9958158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
10058158742SRafal Jaworowski 
10106763f5eSNathan Whitehorn 	DEVMETHOD_END
10258158742SRafal Jaworowski };
10358158742SRafal Jaworowski 
1042cc1ad9cSJayachandran C. DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
1052cc1ad9cSJayachandran C.     sizeof(struct simplebus_softc));
1062cc1ad9cSJayachandran C. 
10706763f5eSNathan Whitehorn static devclass_t simplebus_devclass;
1082d12d35cSIan Lepore EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
1092d12d35cSIan Lepore     0, 0, BUS_PASS_BUS);
1102d12d35cSIan Lepore EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
111633dbf2eSIan Lepore     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
11258158742SRafal Jaworowski 
11358158742SRafal Jaworowski static int
11458158742SRafal Jaworowski simplebus_probe(device_t dev)
11558158742SRafal Jaworowski {
11658158742SRafal Jaworowski 
117add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
118add35ed5SIan Lepore 		return (ENXIO);
119*8dc348a4SMichal Meloun 	/*
120*8dc348a4SMichal Meloun 	 * XXX We should attach only to pure' compatible = "simple-bus"',
121*8dc348a4SMichal Meloun 	 * without any other compatible string.
122*8dc348a4SMichal Meloun 	 * For now, filter only know cases:
123*8dc348a4SMichal Meloun 	 * "syscon", "simple-bus"; is handled by fdt/syscon driver
124*8dc348a4SMichal Meloun 	 * "simple-mfd", "simple-bus"; is handled by fdt/simple-mfd driver
125*8dc348a4SMichal Meloun 	 */
126*8dc348a4SMichal Meloun 	if (ofw_bus_is_compatible(dev, "syscon") ||
127*8dc348a4SMichal Meloun 	    ofw_bus_is_compatible(dev, "simple-mfd"))
128*8dc348a4SMichal Meloun 		return (ENXIO);
129add35ed5SIan Lepore 
1302a74fe2cSIan Lepore 	/*
1312a74fe2cSIan Lepore 	 * FDT data puts a "simple-bus" compatible string on many things that
132db4fcadfSConrad Meyer 	 * have children but aren't really buses in our world.  Without a
1332a74fe2cSIan Lepore 	 * ranges property we will fail to attach, so just fail to probe too.
1342a74fe2cSIan Lepore 	 */
1352a74fe2cSIan Lepore 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
1362a74fe2cSIan Lepore 	    ofw_bus_has_prop(dev, "ranges")) &&
13706763f5eSNathan Whitehorn 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
13806763f5eSNathan Whitehorn 	     "soc") != 0))
13958158742SRafal Jaworowski 		return (ENXIO);
14058158742SRafal Jaworowski 
14158158742SRafal Jaworowski 	device_set_desc(dev, "Flattened device tree simple bus");
14258158742SRafal Jaworowski 
1432737a5a9SAleksandr Rybalko 	return (BUS_PROBE_GENERIC);
14458158742SRafal Jaworowski }
14558158742SRafal Jaworowski 
146bc9b178cSAndrew Turner int
147b95a8021SMichal Meloun simplebus_attach_impl(device_t dev)
14858158742SRafal Jaworowski {
14958158742SRafal Jaworowski 	struct		simplebus_softc *sc;
15006763f5eSNathan Whitehorn 	phandle_t	node;
15158158742SRafal Jaworowski 
15258158742SRafal Jaworowski 	sc = device_get_softc(dev);
153ecaecbc7SIan Lepore 	simplebus_init(dev, 0);
154bc9b178cSAndrew Turner 	if ((sc->flags & SB_FLAG_NO_RANGES) == 0 &&
155bc9b178cSAndrew Turner 	    simplebus_fill_ranges(sc->node, sc) < 0) {
156ecaecbc7SIan Lepore 		device_printf(dev, "could not get ranges\n");
157ecaecbc7SIan Lepore 		return (ENXIO);
158ecaecbc7SIan Lepore 	}
15958158742SRafal Jaworowski 
160ecaecbc7SIan Lepore 	/*
161ecaecbc7SIan Lepore 	 * In principle, simplebus could have an interrupt map, but ignore that
162ecaecbc7SIan Lepore 	 * for now
163ecaecbc7SIan Lepore 	 */
164ecaecbc7SIan Lepore 
165ecaecbc7SIan Lepore 	for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
166ecaecbc7SIan Lepore 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
167b95a8021SMichal Meloun 
168b95a8021SMichal Meloun 	return (0);
169b95a8021SMichal Meloun }
170b95a8021SMichal Meloun 
171b95a8021SMichal Meloun int
172b95a8021SMichal Meloun simplebus_attach(device_t dev)
173b95a8021SMichal Meloun {
174b95a8021SMichal Meloun 	int	rv;
175b95a8021SMichal Meloun 
176b95a8021SMichal Meloun 	rv = simplebus_attach_impl(dev);
177b95a8021SMichal Meloun 	if (rv != 0)
178b95a8021SMichal Meloun 		return (rv);
179b95a8021SMichal Meloun 
180ecaecbc7SIan Lepore 	return (bus_generic_attach(dev));
181ecaecbc7SIan Lepore }
182ecaecbc7SIan Lepore 
183b95a8021SMichal Meloun int
184b95a8021SMichal Meloun simplebus_detach(device_t dev)
185b95a8021SMichal Meloun {
186b95a8021SMichal Meloun 	struct		simplebus_softc *sc;
187b95a8021SMichal Meloun 
188b95a8021SMichal Meloun 	sc = device_get_softc(dev);
189b95a8021SMichal Meloun 	if (sc->ranges != NULL)
190b95a8021SMichal Meloun 		free(sc->ranges, M_DEVBUF);
191b95a8021SMichal Meloun 
192b95a8021SMichal Meloun 	return (bus_generic_detach(dev));
193b95a8021SMichal Meloun }
194b95a8021SMichal Meloun 
195ecaecbc7SIan Lepore void
196ecaecbc7SIan Lepore simplebus_init(device_t dev, phandle_t node)
197ecaecbc7SIan Lepore {
198ecaecbc7SIan Lepore 	struct simplebus_softc *sc;
199ecaecbc7SIan Lepore 
200ecaecbc7SIan Lepore 	sc = device_get_softc(dev);
201ecaecbc7SIan Lepore 	if (node == 0)
202ecaecbc7SIan Lepore 		node = ofw_bus_get_node(dev);
20306763f5eSNathan Whitehorn 	sc->dev = dev;
20406763f5eSNathan Whitehorn 	sc->node = node;
20506763f5eSNathan Whitehorn 
20658158742SRafal Jaworowski 	/*
20706763f5eSNathan Whitehorn 	 * Some important numbers
20858158742SRafal Jaworowski 	 */
20906763f5eSNathan Whitehorn 	sc->acells = 2;
21006763f5eSNathan Whitehorn 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
21106763f5eSNathan Whitehorn 	sc->scells = 1;
21206763f5eSNathan Whitehorn 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
21358158742SRafal Jaworowski }
21458158742SRafal Jaworowski 
2152091650bSEmmanuel Vadot int
21606763f5eSNathan Whitehorn simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
21758158742SRafal Jaworowski {
21806763f5eSNathan Whitehorn 	int host_address_cells;
21906763f5eSNathan Whitehorn 	cell_t *base_ranges;
22006763f5eSNathan Whitehorn 	ssize_t nbase_ranges;
22106763f5eSNathan Whitehorn 	int err;
22206763f5eSNathan Whitehorn 	int i, j, k;
22358158742SRafal Jaworowski 
22406763f5eSNathan Whitehorn 	err = OF_searchencprop(OF_parent(node), "#address-cells",
22506763f5eSNathan Whitehorn 	    &host_address_cells, sizeof(host_address_cells));
22606763f5eSNathan Whitehorn 	if (err <= 0)
22706763f5eSNathan Whitehorn 		return (-1);
22858158742SRafal Jaworowski 
22906763f5eSNathan Whitehorn 	nbase_ranges = OF_getproplen(node, "ranges");
23006763f5eSNathan Whitehorn 	if (nbase_ranges < 0)
23106763f5eSNathan Whitehorn 		return (-1);
23206763f5eSNathan Whitehorn 	sc->nranges = nbase_ranges / sizeof(cell_t) /
23306763f5eSNathan Whitehorn 	    (sc->acells + host_address_cells + sc->scells);
23406763f5eSNathan Whitehorn 	if (sc->nranges == 0)
23506763f5eSNathan Whitehorn 		return (0);
23658158742SRafal Jaworowski 
23706763f5eSNathan Whitehorn 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
23806763f5eSNathan Whitehorn 	    M_DEVBUF, M_WAITOK);
23906763f5eSNathan Whitehorn 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
24006763f5eSNathan Whitehorn 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
24106763f5eSNathan Whitehorn 
24206763f5eSNathan Whitehorn 	for (i = 0, j = 0; i < sc->nranges; i++) {
24306763f5eSNathan Whitehorn 		sc->ranges[i].bus = 0;
24406763f5eSNathan Whitehorn 		for (k = 0; k < sc->acells; k++) {
24506763f5eSNathan Whitehorn 			sc->ranges[i].bus <<= 32;
24606763f5eSNathan Whitehorn 			sc->ranges[i].bus |= base_ranges[j++];
24706763f5eSNathan Whitehorn 		}
24806763f5eSNathan Whitehorn 		sc->ranges[i].host = 0;
24906763f5eSNathan Whitehorn 		for (k = 0; k < host_address_cells; k++) {
25006763f5eSNathan Whitehorn 			sc->ranges[i].host <<= 32;
25106763f5eSNathan Whitehorn 			sc->ranges[i].host |= base_ranges[j++];
25206763f5eSNathan Whitehorn 		}
25306763f5eSNathan Whitehorn 		sc->ranges[i].size = 0;
25406763f5eSNathan Whitehorn 		for (k = 0; k < sc->scells; k++) {
25506763f5eSNathan Whitehorn 			sc->ranges[i].size <<= 32;
25606763f5eSNathan Whitehorn 			sc->ranges[i].size |= base_ranges[j++];
25706763f5eSNathan Whitehorn 		}
25806763f5eSNathan Whitehorn 	}
25906763f5eSNathan Whitehorn 
26006763f5eSNathan Whitehorn 	free(base_ranges, M_DEVBUF);
26106763f5eSNathan Whitehorn 	return (sc->nranges);
26206763f5eSNathan Whitehorn }
26306763f5eSNathan Whitehorn 
264ecaecbc7SIan Lepore struct simplebus_devinfo *
265ecaecbc7SIan Lepore simplebus_setup_dinfo(device_t dev, phandle_t node,
266ecaecbc7SIan Lepore     struct simplebus_devinfo *di)
26706763f5eSNathan Whitehorn {
26806763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
26906763f5eSNathan Whitehorn 	struct simplebus_devinfo *ndi;
27006763f5eSNathan Whitehorn 
27106763f5eSNathan Whitehorn 	sc = device_get_softc(dev);
272ecaecbc7SIan Lepore 	if (di == NULL)
27306763f5eSNathan Whitehorn 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
274ecaecbc7SIan Lepore 	else
275ecaecbc7SIan Lepore 		ndi = di;
27606763f5eSNathan Whitehorn 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
277ecaecbc7SIan Lepore 		if (di == NULL)
27806763f5eSNathan Whitehorn 			free(ndi, M_DEVBUF);
27906763f5eSNathan Whitehorn 		return (NULL);
28006763f5eSNathan Whitehorn 	}
28106763f5eSNathan Whitehorn 
28206763f5eSNathan Whitehorn 	resource_list_init(&ndi->rl);
2834b3d9160SZbigniew Bodek 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
284a8c5ea04SRuslan Bukin 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
28506763f5eSNathan Whitehorn 
28606763f5eSNathan Whitehorn 	return (ndi);
28706763f5eSNathan Whitehorn }
28806763f5eSNathan Whitehorn 
289ecaecbc7SIan Lepore device_t
290ecaecbc7SIan Lepore simplebus_add_device(device_t dev, phandle_t node, u_int order,
291ecaecbc7SIan Lepore     const char *name, int unit, struct simplebus_devinfo *di)
292ecaecbc7SIan Lepore {
293ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
294ecaecbc7SIan Lepore 	device_t cdev;
295ecaecbc7SIan Lepore 
296ecaecbc7SIan Lepore 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
297ecaecbc7SIan Lepore 		return (NULL);
298ecaecbc7SIan Lepore 	cdev = device_add_child_ordered(dev, order, name, unit);
299ecaecbc7SIan Lepore 	if (cdev == NULL) {
300ecaecbc7SIan Lepore 		device_printf(dev, "<%s>: device_add_child failed\n",
301ecaecbc7SIan Lepore 		    ndi->obdinfo.obd_name);
302ecaecbc7SIan Lepore 		resource_list_free(&ndi->rl);
303ecaecbc7SIan Lepore 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
304ecaecbc7SIan Lepore 		if (di == NULL)
305ecaecbc7SIan Lepore 			free(ndi, M_DEVBUF);
306ecaecbc7SIan Lepore 		return (NULL);
307ecaecbc7SIan Lepore 	}
308ecaecbc7SIan Lepore 	device_set_ivars(cdev, ndi);
309ecaecbc7SIan Lepore 
310ecaecbc7SIan Lepore 	return(cdev);
311ecaecbc7SIan Lepore }
312ecaecbc7SIan Lepore 
313ecaecbc7SIan Lepore static device_t
314ecaecbc7SIan Lepore simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
315ecaecbc7SIan Lepore {
316ecaecbc7SIan Lepore 	device_t cdev;
317ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
318ecaecbc7SIan Lepore 
319ecaecbc7SIan Lepore 	cdev = device_add_child_ordered(dev, order, name, unit);
320ecaecbc7SIan Lepore 	if (cdev == NULL)
321ecaecbc7SIan Lepore 		return (NULL);
322ecaecbc7SIan Lepore 
323ecaecbc7SIan Lepore 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
324ecaecbc7SIan Lepore 	ndi->obdinfo.obd_node = -1;
325ecaecbc7SIan Lepore 	resource_list_init(&ndi->rl);
326ecaecbc7SIan Lepore 	device_set_ivars(cdev, ndi);
327ecaecbc7SIan Lepore 
328ecaecbc7SIan Lepore 	return (cdev);
329ecaecbc7SIan Lepore }
330ecaecbc7SIan Lepore 
33106763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *
33206763f5eSNathan Whitehorn simplebus_get_devinfo(device_t bus __unused, device_t child)
33306763f5eSNathan Whitehorn {
33406763f5eSNathan Whitehorn         struct simplebus_devinfo *ndi;
33506763f5eSNathan Whitehorn 
33606763f5eSNathan Whitehorn         ndi = device_get_ivars(child);
3376fc608bfSMichal Meloun 	if (ndi == NULL)
3386fc608bfSMichal Meloun 		return (NULL);
33906763f5eSNathan Whitehorn         return (&ndi->obdinfo);
34058158742SRafal Jaworowski }
34158158742SRafal Jaworowski 
342ecaecbc7SIan Lepore static struct resource_list *
343ecaecbc7SIan Lepore simplebus_get_resource_list(device_t bus __unused, device_t child)
344ecaecbc7SIan Lepore {
345ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
346ecaecbc7SIan Lepore 
347ecaecbc7SIan Lepore 	ndi = device_get_ivars(child);
3486fc608bfSMichal Meloun 	if (ndi == NULL)
3496fc608bfSMichal Meloun 		return (NULL);
350ecaecbc7SIan Lepore 	return (&ndi->rl);
351ecaecbc7SIan Lepore }
352ecaecbc7SIan Lepore 
35358158742SRafal Jaworowski static struct resource *
35458158742SRafal Jaworowski simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
3552dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
35658158742SRafal Jaworowski {
35706763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
35858158742SRafal Jaworowski 	struct simplebus_devinfo *di;
35958158742SRafal Jaworowski 	struct resource_list_entry *rle;
36006763f5eSNathan Whitehorn 	int j;
36106763f5eSNathan Whitehorn 
36206763f5eSNathan Whitehorn 	sc = device_get_softc(bus);
36358158742SRafal Jaworowski 
36458158742SRafal Jaworowski 	/*
36558158742SRafal Jaworowski 	 * Request for the default allocation with a given rid: use resource
36658158742SRafal Jaworowski 	 * list stored in the local device info.
36758158742SRafal Jaworowski 	 */
3687915adb5SJustin Hibbits 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
36958158742SRafal Jaworowski 		if ((di = device_get_ivars(child)) == NULL)
37058158742SRafal Jaworowski 			return (NULL);
37158158742SRafal Jaworowski 
37258158742SRafal Jaworowski 		if (type == SYS_RES_IOPORT)
37358158742SRafal Jaworowski 			type = SYS_RES_MEMORY;
37458158742SRafal Jaworowski 
37506763f5eSNathan Whitehorn 		rle = resource_list_find(&di->rl, type, *rid);
37658158742SRafal Jaworowski 		if (rle == NULL) {
377089dfb09SAleksandr Rybalko 			if (bootverbose)
37858158742SRafal Jaworowski 				device_printf(bus, "no default resources for "
37958158742SRafal Jaworowski 				    "rid = %d, type = %d\n", *rid, type);
38058158742SRafal Jaworowski 			return (NULL);
38158158742SRafal Jaworowski 		}
38258158742SRafal Jaworowski 		start = rle->start;
38358158742SRafal Jaworowski 		end = rle->end;
38458158742SRafal Jaworowski 		count = rle->count;
38558158742SRafal Jaworowski         }
38658158742SRafal Jaworowski 
38706763f5eSNathan Whitehorn 	if (type == SYS_RES_MEMORY) {
38806763f5eSNathan Whitehorn 		/* Remap through ranges property */
38906763f5eSNathan Whitehorn 		for (j = 0; j < sc->nranges; j++) {
39006763f5eSNathan Whitehorn 			if (start >= sc->ranges[j].bus && end <
39106763f5eSNathan Whitehorn 			    sc->ranges[j].bus + sc->ranges[j].size) {
39206763f5eSNathan Whitehorn 				start -= sc->ranges[j].bus;
39306763f5eSNathan Whitehorn 				start += sc->ranges[j].host;
39406763f5eSNathan Whitehorn 				end -= sc->ranges[j].bus;
39506763f5eSNathan Whitehorn 				end += sc->ranges[j].host;
39606763f5eSNathan Whitehorn 				break;
39706763f5eSNathan Whitehorn 			}
39806763f5eSNathan Whitehorn 		}
39906763f5eSNathan Whitehorn 		if (j == sc->nranges && sc->nranges != 0) {
40006763f5eSNathan Whitehorn 			if (bootverbose)
40106763f5eSNathan Whitehorn 				device_printf(bus, "Could not map resource "
402da1b038aSJustin Hibbits 				    "%#jx-%#jx\n", start, end);
40306763f5eSNathan Whitehorn 
40406763f5eSNathan Whitehorn 			return (NULL);
40506763f5eSNathan Whitehorn 		}
40606763f5eSNathan Whitehorn 	}
4071f40dbc8SBrooks Davis 
40858158742SRafal Jaworowski 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
40958158742SRafal Jaworowski 	    count, flags));
41058158742SRafal Jaworowski }
41158158742SRafal Jaworowski 
4121f40dbc8SBrooks Davis static int
41306763f5eSNathan Whitehorn simplebus_print_res(struct simplebus_devinfo *di)
4141f40dbc8SBrooks Davis {
41506763f5eSNathan Whitehorn 	int rv;
4161f40dbc8SBrooks Davis 
4176fc608bfSMichal Meloun 	if (di == NULL)
4186fc608bfSMichal Meloun 		return (0);
41906763f5eSNathan Whitehorn 	rv = 0;
420da1b038aSJustin Hibbits 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
421da1b038aSJustin Hibbits 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
42206763f5eSNathan Whitehorn 	return (rv);
42306763f5eSNathan Whitehorn }
4241f40dbc8SBrooks Davis 
42506763f5eSNathan Whitehorn static void
42606763f5eSNathan Whitehorn simplebus_probe_nomatch(device_t bus, device_t child)
42706763f5eSNathan Whitehorn {
428cca75397SWarner Losh 	const char *name, *type, *compat;
42906763f5eSNathan Whitehorn 
43006763f5eSNathan Whitehorn 	if (!bootverbose)
43106763f5eSNathan Whitehorn 		return;
43206763f5eSNathan Whitehorn 
433ecaecbc7SIan Lepore 	compat = ofw_bus_get_compat(child);
434ecaecbc7SIan Lepore 	if (compat == NULL)
435ecaecbc7SIan Lepore 		return;
43606763f5eSNathan Whitehorn 	name = ofw_bus_get_name(child);
43706763f5eSNathan Whitehorn 	type = ofw_bus_get_type(child);
43806763f5eSNathan Whitehorn 
43906763f5eSNathan Whitehorn 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
44006763f5eSNathan Whitehorn 	simplebus_print_res(device_get_ivars(child));
441cca75397SWarner Losh 	if (!ofw_bus_status_okay(child))
442cca75397SWarner Losh 		printf(" disabled");
443cca75397SWarner Losh 	if (type)
444cca75397SWarner Losh 		printf(" type %s", type);
445ecaecbc7SIan Lepore 	printf(" compat %s (no driver attached)\n", compat);
4461f40dbc8SBrooks Davis }
4471f40dbc8SBrooks Davis 
4481f40dbc8SBrooks Davis static int
44906763f5eSNathan Whitehorn simplebus_print_child(device_t bus, device_t child)
4501f40dbc8SBrooks Davis {
45106763f5eSNathan Whitehorn 	int rv;
4521f40dbc8SBrooks Davis 
45306763f5eSNathan Whitehorn 	rv = bus_print_child_header(bus, child);
45406763f5eSNathan Whitehorn 	rv += simplebus_print_res(device_get_ivars(child));
455cca75397SWarner Losh 	if (!ofw_bus_status_okay(child))
456cca75397SWarner Losh 		rv += printf(" disabled");
45706763f5eSNathan Whitehorn 	rv += bus_print_child_footer(bus, child);
45806763f5eSNathan Whitehorn 	return (rv);
4591f40dbc8SBrooks Davis }
460