xref: /freebsd/sys/dev/fdt/simplebus.c (revision d77f2092ceebaba115e6be53410428f6f5f6ae83)
158158742SRafal Jaworowski /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
3058158742SRafal Jaworowski #include <sys/systm.h>
3158158742SRafal Jaworowski #include <sys/module.h>
3258158742SRafal Jaworowski #include <sys/bus.h>
3306763f5eSNathan Whitehorn #include <sys/conf.h>
3406763f5eSNathan Whitehorn #include <sys/kernel.h>
3558158742SRafal Jaworowski #include <sys/rman.h>
3658158742SRafal Jaworowski 
3706763f5eSNathan 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 
412cc1ad9cSJayachandran C. #include <dev/fdt/simplebus.h>
4258158742SRafal Jaworowski 
4358158742SRafal Jaworowski /*
4406763f5eSNathan Whitehorn  * Bus interface.
4558158742SRafal Jaworowski  */
4606763f5eSNathan Whitehorn static int		simplebus_probe(device_t dev);
4758158742SRafal Jaworowski static struct resource *simplebus_alloc_resource(device_t, device_t, int,
482dd1bdf1SJustin Hibbits     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
493cf55328SJohn Baldwin static int		simplebus_release_resource(device_t bus, device_t child,
503cf55328SJohn Baldwin     int type, int rid, struct resource *r);
513cf55328SJohn Baldwin static int		simplebus_activate_resource(device_t bus,
523cf55328SJohn Baldwin     device_t child, int type, int rid, struct resource *r);
533cf55328SJohn Baldwin static int		simplebus_deactivate_resource(device_t bus,
543cf55328SJohn Baldwin     device_t child, int type, int rid, struct resource *r);
5506763f5eSNathan Whitehorn static void		simplebus_probe_nomatch(device_t bus, device_t child);
5606763f5eSNathan Whitehorn static int		simplebus_print_child(device_t bus, device_t child);
57ecaecbc7SIan Lepore static device_t		simplebus_add_child(device_t dev, u_int order,
58ecaecbc7SIan Lepore     const char *name, int unit);
59ecaecbc7SIan Lepore static struct resource_list *simplebus_get_resource_list(device_t bus,
60ecaecbc7SIan Lepore     device_t child);
613f9a00e3SBartlomiej Grzesik 
623f9a00e3SBartlomiej Grzesik static ssize_t		simplebus_get_property(device_t bus, device_t child,
63b344de4dSKornel Duleba     const char *propname, void *propvalue, size_t size,
64b344de4dSKornel Duleba     device_property_type_t type);
6558158742SRafal Jaworowski /*
6606763f5eSNathan Whitehorn  * ofw_bus interface
6706763f5eSNathan Whitehorn  */
6806763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
6906763f5eSNathan Whitehorn     device_t child);
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),
78b95a8021SMichal Meloun 	DEVMETHOD(device_detach,	simplebus_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),
923cf55328SJohn Baldwin 	DEVMETHOD(bus_release_resource,	simplebus_release_resource),
933cf55328SJohn Baldwin 	DEVMETHOD(bus_activate_resource, simplebus_activate_resource),
943cf55328SJohn Baldwin 	DEVMETHOD(bus_deactivate_resource, simplebus_deactivate_resource),
95fef01f04SJohn Baldwin 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
96*d77f2092SJohn Baldwin 	DEVMETHOD(bus_map_resource,	bus_generic_map_resource),
97*d77f2092SJohn Baldwin 	DEVMETHOD(bus_unmap_resource,	bus_generic_unmap_resource),
98ecaecbc7SIan Lepore 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
99ecaecbc7SIan Lepore 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
100e89d0785SJohn Baldwin 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
101ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
102ecaecbc7SIan Lepore 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
1033f9a00e3SBartlomiej Grzesik 	DEVMETHOD(bus_get_property,	simplebus_get_property),
1047b5d62bbSTakanori Watanabe 	DEVMETHOD(bus_get_device_path,  ofw_bus_gen_get_device_path),
10558158742SRafal Jaworowski 
10606763f5eSNathan Whitehorn 	/* ofw_bus interface */
10758158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
10858158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
10958158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
11058158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
11158158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
11258158742SRafal Jaworowski 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
11358158742SRafal Jaworowski 
11406763f5eSNathan Whitehorn 	DEVMETHOD_END
11558158742SRafal Jaworowski };
11658158742SRafal Jaworowski 
1172cc1ad9cSJayachandran C. DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
1182cc1ad9cSJayachandran C.     sizeof(struct simplebus_softc));
1192cc1ad9cSJayachandran C. 
1200a73fdb9SJohn Baldwin EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, 0, 0, BUS_PASS_BUS);
1210a73fdb9SJohn Baldwin EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, 0, 0,
1220a73fdb9SJohn Baldwin     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
12358158742SRafal Jaworowski 
12458158742SRafal Jaworowski static int
12558158742SRafal Jaworowski simplebus_probe(device_t dev)
12658158742SRafal Jaworowski {
12758158742SRafal Jaworowski 
128add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
129add35ed5SIan Lepore 		return (ENXIO);
1308dc348a4SMichal Meloun 	/*
1318dc348a4SMichal Meloun 	 * XXX We should attach only to pure' compatible = "simple-bus"',
1328dc348a4SMichal Meloun 	 * without any other compatible string.
1338dc348a4SMichal Meloun 	 * For now, filter only know cases:
1348dc348a4SMichal Meloun 	 * "syscon", "simple-bus"; is handled by fdt/syscon driver
1358dc348a4SMichal Meloun 	 * "simple-mfd", "simple-bus"; is handled by fdt/simple-mfd driver
1368dc348a4SMichal Meloun 	 */
1378dc348a4SMichal Meloun 	if (ofw_bus_is_compatible(dev, "syscon") ||
1388dc348a4SMichal Meloun 	    ofw_bus_is_compatible(dev, "simple-mfd"))
1398dc348a4SMichal Meloun 		return (ENXIO);
140add35ed5SIan Lepore 
1412a74fe2cSIan Lepore 	/*
1422a74fe2cSIan Lepore 	 * FDT data puts a "simple-bus" compatible string on many things that
143db4fcadfSConrad Meyer 	 * have children but aren't really buses in our world.  Without a
1442a74fe2cSIan Lepore 	 * ranges property we will fail to attach, so just fail to probe too.
1452a74fe2cSIan Lepore 	 */
1462a74fe2cSIan Lepore 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
1472a74fe2cSIan Lepore 	    ofw_bus_has_prop(dev, "ranges")) &&
14806763f5eSNathan Whitehorn 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
14906763f5eSNathan Whitehorn 	     "soc") != 0))
15058158742SRafal Jaworowski 		return (ENXIO);
15158158742SRafal Jaworowski 
15258158742SRafal Jaworowski 	device_set_desc(dev, "Flattened device tree simple bus");
15358158742SRafal Jaworowski 
1542737a5a9SAleksandr Rybalko 	return (BUS_PROBE_GENERIC);
15558158742SRafal Jaworowski }
15658158742SRafal Jaworowski 
157bc9b178cSAndrew Turner int
158b95a8021SMichal Meloun simplebus_attach_impl(device_t dev)
15958158742SRafal Jaworowski {
16058158742SRafal Jaworowski 	struct		simplebus_softc *sc;
16106763f5eSNathan Whitehorn 	phandle_t	node;
16258158742SRafal Jaworowski 
16358158742SRafal Jaworowski 	sc = device_get_softc(dev);
164ecaecbc7SIan Lepore 	simplebus_init(dev, 0);
165bc9b178cSAndrew Turner 	if ((sc->flags & SB_FLAG_NO_RANGES) == 0 &&
166bc9b178cSAndrew Turner 	    simplebus_fill_ranges(sc->node, sc) < 0) {
167ecaecbc7SIan Lepore 		device_printf(dev, "could not get ranges\n");
168ecaecbc7SIan Lepore 		return (ENXIO);
169ecaecbc7SIan Lepore 	}
17058158742SRafal Jaworowski 
171ecaecbc7SIan Lepore 	/*
172ecaecbc7SIan Lepore 	 * In principle, simplebus could have an interrupt map, but ignore that
173ecaecbc7SIan Lepore 	 * for now
174ecaecbc7SIan Lepore 	 */
175ecaecbc7SIan Lepore 
176ecaecbc7SIan Lepore 	for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
177ecaecbc7SIan Lepore 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
178b95a8021SMichal Meloun 
179b95a8021SMichal Meloun 	return (0);
180b95a8021SMichal Meloun }
181b95a8021SMichal Meloun 
182b95a8021SMichal Meloun int
183b95a8021SMichal Meloun simplebus_attach(device_t dev)
184b95a8021SMichal Meloun {
185b95a8021SMichal Meloun 	int	rv;
186b95a8021SMichal Meloun 
187b95a8021SMichal Meloun 	rv = simplebus_attach_impl(dev);
188b95a8021SMichal Meloun 	if (rv != 0)
189b95a8021SMichal Meloun 		return (rv);
190b95a8021SMichal Meloun 
191ecaecbc7SIan Lepore 	return (bus_generic_attach(dev));
192ecaecbc7SIan Lepore }
193ecaecbc7SIan Lepore 
194b95a8021SMichal Meloun int
195b95a8021SMichal Meloun simplebus_detach(device_t dev)
196b95a8021SMichal Meloun {
197b95a8021SMichal Meloun 	struct		simplebus_softc *sc;
198b95a8021SMichal Meloun 
199b95a8021SMichal Meloun 	sc = device_get_softc(dev);
200b95a8021SMichal Meloun 	if (sc->ranges != NULL)
201b95a8021SMichal Meloun 		free(sc->ranges, M_DEVBUF);
202b95a8021SMichal Meloun 
203b95a8021SMichal Meloun 	return (bus_generic_detach(dev));
204b95a8021SMichal Meloun }
205b95a8021SMichal Meloun 
206ecaecbc7SIan Lepore void
207ecaecbc7SIan Lepore simplebus_init(device_t dev, phandle_t node)
208ecaecbc7SIan Lepore {
209ecaecbc7SIan Lepore 	struct simplebus_softc *sc;
210ecaecbc7SIan Lepore 
211ecaecbc7SIan Lepore 	sc = device_get_softc(dev);
212ecaecbc7SIan Lepore 	if (node == 0)
213ecaecbc7SIan Lepore 		node = ofw_bus_get_node(dev);
21406763f5eSNathan Whitehorn 	sc->dev = dev;
21506763f5eSNathan Whitehorn 	sc->node = node;
21606763f5eSNathan Whitehorn 
21758158742SRafal Jaworowski 	/*
21806763f5eSNathan Whitehorn 	 * Some important numbers
21958158742SRafal Jaworowski 	 */
22006763f5eSNathan Whitehorn 	sc->acells = 2;
22106763f5eSNathan Whitehorn 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
22206763f5eSNathan Whitehorn 	sc->scells = 1;
22306763f5eSNathan Whitehorn 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
22458158742SRafal Jaworowski }
22558158742SRafal Jaworowski 
2262091650bSEmmanuel Vadot int
22706763f5eSNathan Whitehorn simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
22858158742SRafal Jaworowski {
22906763f5eSNathan Whitehorn 	int host_address_cells;
23006763f5eSNathan Whitehorn 	cell_t *base_ranges;
23106763f5eSNathan Whitehorn 	ssize_t nbase_ranges;
23206763f5eSNathan Whitehorn 	int err;
23306763f5eSNathan Whitehorn 	int i, j, k;
23458158742SRafal Jaworowski 
23506763f5eSNathan Whitehorn 	err = OF_searchencprop(OF_parent(node), "#address-cells",
23606763f5eSNathan Whitehorn 	    &host_address_cells, sizeof(host_address_cells));
23706763f5eSNathan Whitehorn 	if (err <= 0)
23806763f5eSNathan Whitehorn 		return (-1);
23958158742SRafal Jaworowski 
24006763f5eSNathan Whitehorn 	nbase_ranges = OF_getproplen(node, "ranges");
24106763f5eSNathan Whitehorn 	if (nbase_ranges < 0)
24206763f5eSNathan Whitehorn 		return (-1);
24306763f5eSNathan Whitehorn 	sc->nranges = nbase_ranges / sizeof(cell_t) /
24406763f5eSNathan Whitehorn 	    (sc->acells + host_address_cells + sc->scells);
24506763f5eSNathan Whitehorn 	if (sc->nranges == 0)
24606763f5eSNathan Whitehorn 		return (0);
24758158742SRafal Jaworowski 
24806763f5eSNathan Whitehorn 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
24906763f5eSNathan Whitehorn 	    M_DEVBUF, M_WAITOK);
25006763f5eSNathan Whitehorn 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
25106763f5eSNathan Whitehorn 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
25206763f5eSNathan Whitehorn 
25306763f5eSNathan Whitehorn 	for (i = 0, j = 0; i < sc->nranges; i++) {
25406763f5eSNathan Whitehorn 		sc->ranges[i].bus = 0;
25506763f5eSNathan Whitehorn 		for (k = 0; k < sc->acells; k++) {
25606763f5eSNathan Whitehorn 			sc->ranges[i].bus <<= 32;
25706763f5eSNathan Whitehorn 			sc->ranges[i].bus |= base_ranges[j++];
25806763f5eSNathan Whitehorn 		}
25906763f5eSNathan Whitehorn 		sc->ranges[i].host = 0;
26006763f5eSNathan Whitehorn 		for (k = 0; k < host_address_cells; k++) {
26106763f5eSNathan Whitehorn 			sc->ranges[i].host <<= 32;
26206763f5eSNathan Whitehorn 			sc->ranges[i].host |= base_ranges[j++];
26306763f5eSNathan Whitehorn 		}
26406763f5eSNathan Whitehorn 		sc->ranges[i].size = 0;
26506763f5eSNathan Whitehorn 		for (k = 0; k < sc->scells; k++) {
26606763f5eSNathan Whitehorn 			sc->ranges[i].size <<= 32;
26706763f5eSNathan Whitehorn 			sc->ranges[i].size |= base_ranges[j++];
26806763f5eSNathan Whitehorn 		}
26906763f5eSNathan Whitehorn 	}
27006763f5eSNathan Whitehorn 
27106763f5eSNathan Whitehorn 	free(base_ranges, M_DEVBUF);
27206763f5eSNathan Whitehorn 	return (sc->nranges);
27306763f5eSNathan Whitehorn }
27406763f5eSNathan Whitehorn 
275ecaecbc7SIan Lepore struct simplebus_devinfo *
276ecaecbc7SIan Lepore simplebus_setup_dinfo(device_t dev, phandle_t node,
277ecaecbc7SIan Lepore     struct simplebus_devinfo *di)
27806763f5eSNathan Whitehorn {
27906763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
28006763f5eSNathan Whitehorn 	struct simplebus_devinfo *ndi;
28106763f5eSNathan Whitehorn 
28206763f5eSNathan Whitehorn 	sc = device_get_softc(dev);
283ecaecbc7SIan Lepore 	if (di == NULL)
28406763f5eSNathan Whitehorn 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
285ecaecbc7SIan Lepore 	else
286ecaecbc7SIan Lepore 		ndi = di;
28706763f5eSNathan Whitehorn 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
288ecaecbc7SIan Lepore 		if (di == NULL)
28906763f5eSNathan Whitehorn 			free(ndi, M_DEVBUF);
29006763f5eSNathan Whitehorn 		return (NULL);
29106763f5eSNathan Whitehorn 	}
29206763f5eSNathan Whitehorn 
29306763f5eSNathan Whitehorn 	resource_list_init(&ndi->rl);
2944b3d9160SZbigniew Bodek 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
295a8c5ea04SRuslan Bukin 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
29606763f5eSNathan Whitehorn 
29706763f5eSNathan Whitehorn 	return (ndi);
29806763f5eSNathan Whitehorn }
29906763f5eSNathan Whitehorn 
300ecaecbc7SIan Lepore device_t
301ecaecbc7SIan Lepore simplebus_add_device(device_t dev, phandle_t node, u_int order,
302ecaecbc7SIan Lepore     const char *name, int unit, struct simplebus_devinfo *di)
303ecaecbc7SIan Lepore {
304ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
305ecaecbc7SIan Lepore 	device_t cdev;
306ecaecbc7SIan Lepore 
307ecaecbc7SIan Lepore 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
308ecaecbc7SIan Lepore 		return (NULL);
309ecaecbc7SIan Lepore 	cdev = device_add_child_ordered(dev, order, name, unit);
310ecaecbc7SIan Lepore 	if (cdev == NULL) {
311ecaecbc7SIan Lepore 		device_printf(dev, "<%s>: device_add_child failed\n",
312ecaecbc7SIan Lepore 		    ndi->obdinfo.obd_name);
313ecaecbc7SIan Lepore 		resource_list_free(&ndi->rl);
314ecaecbc7SIan Lepore 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
315ecaecbc7SIan Lepore 		if (di == NULL)
316ecaecbc7SIan Lepore 			free(ndi, M_DEVBUF);
317ecaecbc7SIan Lepore 		return (NULL);
318ecaecbc7SIan Lepore 	}
319ecaecbc7SIan Lepore 	device_set_ivars(cdev, ndi);
320ecaecbc7SIan Lepore 
321ecaecbc7SIan Lepore 	return(cdev);
322ecaecbc7SIan Lepore }
323ecaecbc7SIan Lepore 
324ecaecbc7SIan Lepore static device_t
325ecaecbc7SIan Lepore simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
326ecaecbc7SIan Lepore {
327ecaecbc7SIan Lepore 	device_t cdev;
328ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
329ecaecbc7SIan Lepore 
330ecaecbc7SIan Lepore 	cdev = device_add_child_ordered(dev, order, name, unit);
331ecaecbc7SIan Lepore 	if (cdev == NULL)
332ecaecbc7SIan Lepore 		return (NULL);
333ecaecbc7SIan Lepore 
334ecaecbc7SIan Lepore 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
335ecaecbc7SIan Lepore 	ndi->obdinfo.obd_node = -1;
336ecaecbc7SIan Lepore 	resource_list_init(&ndi->rl);
337ecaecbc7SIan Lepore 	device_set_ivars(cdev, ndi);
338ecaecbc7SIan Lepore 
339ecaecbc7SIan Lepore 	return (cdev);
340ecaecbc7SIan Lepore }
341ecaecbc7SIan Lepore 
34206763f5eSNathan Whitehorn static const struct ofw_bus_devinfo *
34306763f5eSNathan Whitehorn simplebus_get_devinfo(device_t bus __unused, device_t child)
34406763f5eSNathan Whitehorn {
34506763f5eSNathan Whitehorn         struct simplebus_devinfo *ndi;
34606763f5eSNathan Whitehorn 
34706763f5eSNathan Whitehorn         ndi = device_get_ivars(child);
3486fc608bfSMichal Meloun 	if (ndi == NULL)
3496fc608bfSMichal Meloun 		return (NULL);
35006763f5eSNathan Whitehorn         return (&ndi->obdinfo);
35158158742SRafal Jaworowski }
35258158742SRafal Jaworowski 
353ecaecbc7SIan Lepore static struct resource_list *
354ecaecbc7SIan Lepore simplebus_get_resource_list(device_t bus __unused, device_t child)
355ecaecbc7SIan Lepore {
356ecaecbc7SIan Lepore 	struct simplebus_devinfo *ndi;
357ecaecbc7SIan Lepore 
358ecaecbc7SIan Lepore 	ndi = device_get_ivars(child);
3596fc608bfSMichal Meloun 	if (ndi == NULL)
3606fc608bfSMichal Meloun 		return (NULL);
361ecaecbc7SIan Lepore 	return (&ndi->rl);
362ecaecbc7SIan Lepore }
363ecaecbc7SIan Lepore 
3643f9a00e3SBartlomiej Grzesik static ssize_t
3653f9a00e3SBartlomiej Grzesik simplebus_get_property(device_t bus, device_t child, const char *propname,
366b344de4dSKornel Duleba     void *propvalue, size_t size, device_property_type_t type)
3673f9a00e3SBartlomiej Grzesik {
36899e6980fSBjoern A. Zeeb 	phandle_t node, xref;
369b344de4dSKornel Duleba 	ssize_t ret, i;
370b344de4dSKornel Duleba 	uint32_t *buffer;
371b344de4dSKornel Duleba 	uint64_t val;
372b344de4dSKornel Duleba 
373b344de4dSKornel Duleba 	switch (type) {
374b344de4dSKornel Duleba 	case DEVICE_PROP_ANY:
375b344de4dSKornel Duleba 	case DEVICE_PROP_BUFFER:
376b344de4dSKornel Duleba 	case DEVICE_PROP_UINT32:
377b344de4dSKornel Duleba 	case DEVICE_PROP_UINT64:
37899e6980fSBjoern A. Zeeb 	case DEVICE_PROP_HANDLE:
379b344de4dSKornel Duleba 		break;
380b344de4dSKornel Duleba 	default:
381b344de4dSKornel Duleba 		return (-1);
382b344de4dSKornel Duleba 	}
3833f9a00e3SBartlomiej Grzesik 
38499e6980fSBjoern A. Zeeb 	node = ofw_bus_get_node(child);
3853f9a00e3SBartlomiej Grzesik 	if (propvalue == NULL || size == 0)
3863f9a00e3SBartlomiej Grzesik 		return (OF_getproplen(node, propname));
3873f9a00e3SBartlomiej Grzesik 
388b344de4dSKornel Duleba 	/*
389b344de4dSKornel Duleba 	 * Integer values are stored in BE format.
390b344de4dSKornel Duleba 	 * If caller declared that the underlying property type is uint32_t
391b344de4dSKornel Duleba 	 * we need to do the conversion to match host endianness.
392b344de4dSKornel Duleba 	 */
393b344de4dSKornel Duleba 	if (type == DEVICE_PROP_UINT32)
3943f9a00e3SBartlomiej Grzesik 		return (OF_getencprop(node, propname, propvalue, size));
395b344de4dSKornel Duleba 
396b344de4dSKornel Duleba 	/*
397b344de4dSKornel Duleba 	 * uint64_t also requires endianness handling.
398b344de4dSKornel Duleba 	 * In FDT every 8 byte value is stored using two uint32_t variables
399b344de4dSKornel Duleba 	 * in BE format. Now, since the upper bits are stored as the first
400b344de4dSKornel Duleba 	 * of the pair, both halves require swapping.
401b344de4dSKornel Duleba 	 */
402b344de4dSKornel Duleba 	 if (type == DEVICE_PROP_UINT64) {
403b344de4dSKornel Duleba 		ret = OF_getencprop(node, propname, propvalue, size);
404b344de4dSKornel Duleba 		if (ret <= 0) {
405b344de4dSKornel Duleba 			return (ret);
406b344de4dSKornel Duleba 		}
407b344de4dSKornel Duleba 
408b344de4dSKornel Duleba 		buffer = (uint32_t *)propvalue;
409b344de4dSKornel Duleba 
410b344de4dSKornel Duleba 		for (i = 0; i < size / 4; i += 2) {
411b344de4dSKornel Duleba 			val = (uint64_t)buffer[i] << 32 | buffer[i + 1];
412b344de4dSKornel Duleba 			((uint64_t *)buffer)[i / 2] = val;
413b344de4dSKornel Duleba 		}
414b344de4dSKornel Duleba 		return (ret);
415b344de4dSKornel Duleba 	}
416b344de4dSKornel Duleba 
41799e6980fSBjoern A. Zeeb 	if (type == DEVICE_PROP_HANDLE) {
41899e6980fSBjoern A. Zeeb 		if (size < sizeof(node))
41999e6980fSBjoern A. Zeeb 			return (-1);
42099e6980fSBjoern A. Zeeb 		ret = OF_getencprop(node, propname, &xref, sizeof(xref));
42199e6980fSBjoern A. Zeeb 		if (ret <= 0)
42299e6980fSBjoern A. Zeeb 			return (ret);
42399e6980fSBjoern A. Zeeb 
42499e6980fSBjoern A. Zeeb 		node = OF_node_from_xref(xref);
42599e6980fSBjoern A. Zeeb 		if (propvalue != NULL)
42699e6980fSBjoern A. Zeeb 			*(uint32_t *)propvalue = node;
42799e6980fSBjoern A. Zeeb 		return (ret);
42899e6980fSBjoern A. Zeeb 	}
42999e6980fSBjoern A. Zeeb 
430b344de4dSKornel Duleba 	return (OF_getprop(node, propname, propvalue, size));
4313f9a00e3SBartlomiej Grzesik }
4323f9a00e3SBartlomiej Grzesik 
43358158742SRafal Jaworowski static struct resource *
43458158742SRafal Jaworowski simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
4352dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
43658158742SRafal Jaworowski {
43706763f5eSNathan Whitehorn 	struct simplebus_softc *sc;
43858158742SRafal Jaworowski 	struct simplebus_devinfo *di;
43958158742SRafal Jaworowski 	struct resource_list_entry *rle;
44006763f5eSNathan Whitehorn 	int j;
44106763f5eSNathan Whitehorn 
44206763f5eSNathan Whitehorn 	sc = device_get_softc(bus);
44358158742SRafal Jaworowski 
44458158742SRafal Jaworowski 	/*
44558158742SRafal Jaworowski 	 * Request for the default allocation with a given rid: use resource
44658158742SRafal Jaworowski 	 * list stored in the local device info.
44758158742SRafal Jaworowski 	 */
4487915adb5SJustin Hibbits 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
44958158742SRafal Jaworowski 		if ((di = device_get_ivars(child)) == NULL)
45058158742SRafal Jaworowski 			return (NULL);
45158158742SRafal Jaworowski 
45206763f5eSNathan Whitehorn 		rle = resource_list_find(&di->rl, type, *rid);
45358158742SRafal Jaworowski 		if (rle == NULL) {
454089dfb09SAleksandr Rybalko 			if (bootverbose)
45558158742SRafal Jaworowski 				device_printf(bus, "no default resources for "
45658158742SRafal Jaworowski 				    "rid = %d, type = %d\n", *rid, type);
45758158742SRafal Jaworowski 			return (NULL);
45858158742SRafal Jaworowski 		}
45958158742SRafal Jaworowski 		start = rle->start;
46058158742SRafal Jaworowski 		end = rle->end;
46158158742SRafal Jaworowski 		count = rle->count;
46258158742SRafal Jaworowski         }
46358158742SRafal Jaworowski 
4644505c892SJohn Baldwin 	if (type == SYS_RES_IOPORT)
4654505c892SJohn Baldwin 		type = SYS_RES_MEMORY;
4664505c892SJohn Baldwin 
46706763f5eSNathan Whitehorn 	if (type == SYS_RES_MEMORY) {
46806763f5eSNathan Whitehorn 		/* Remap through ranges property */
46906763f5eSNathan Whitehorn 		for (j = 0; j < sc->nranges; j++) {
47006763f5eSNathan Whitehorn 			if (start >= sc->ranges[j].bus && end <
47106763f5eSNathan Whitehorn 			    sc->ranges[j].bus + sc->ranges[j].size) {
47206763f5eSNathan Whitehorn 				start -= sc->ranges[j].bus;
47306763f5eSNathan Whitehorn 				start += sc->ranges[j].host;
47406763f5eSNathan Whitehorn 				end -= sc->ranges[j].bus;
47506763f5eSNathan Whitehorn 				end += sc->ranges[j].host;
47606763f5eSNathan Whitehorn 				break;
47706763f5eSNathan Whitehorn 			}
47806763f5eSNathan Whitehorn 		}
47906763f5eSNathan Whitehorn 		if (j == sc->nranges && sc->nranges != 0) {
48006763f5eSNathan Whitehorn 			if (bootverbose)
48106763f5eSNathan Whitehorn 				device_printf(bus, "Could not map resource "
482da1b038aSJustin Hibbits 				    "%#jx-%#jx\n", start, end);
48306763f5eSNathan Whitehorn 
48406763f5eSNathan Whitehorn 			return (NULL);
48506763f5eSNathan Whitehorn 		}
48606763f5eSNathan Whitehorn 	}
4871f40dbc8SBrooks Davis 
48858158742SRafal Jaworowski 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
48958158742SRafal Jaworowski 	    count, flags));
49058158742SRafal Jaworowski }
49158158742SRafal Jaworowski 
4921f40dbc8SBrooks Davis static int
4933cf55328SJohn Baldwin simplebus_release_resource(device_t bus, device_t child, int type, int rid,
4943cf55328SJohn Baldwin     struct resource *r)
4953cf55328SJohn Baldwin {
4963cf55328SJohn Baldwin 
4973cf55328SJohn Baldwin 	if (type == SYS_RES_IOPORT)
4983cf55328SJohn Baldwin 		type = SYS_RES_MEMORY;
4993cf55328SJohn Baldwin 	return (bus_generic_release_resource(bus, child, type, rid, r));
5003cf55328SJohn Baldwin }
5013cf55328SJohn Baldwin 
5023cf55328SJohn Baldwin static int
5033cf55328SJohn Baldwin simplebus_activate_resource(device_t bus, device_t child, int type, int rid,
5043cf55328SJohn Baldwin     struct resource *r)
5053cf55328SJohn Baldwin {
5063cf55328SJohn Baldwin 
5073cf55328SJohn Baldwin 	if (type == SYS_RES_IOPORT)
5083cf55328SJohn Baldwin 		type = SYS_RES_MEMORY;
5093cf55328SJohn Baldwin 	return (bus_generic_activate_resource(bus, child, type, rid, r));
5103cf55328SJohn Baldwin }
5113cf55328SJohn Baldwin 
5123cf55328SJohn Baldwin static int
5133cf55328SJohn Baldwin simplebus_deactivate_resource(device_t bus, device_t child, int type, int rid,
5143cf55328SJohn Baldwin     struct resource *r)
5153cf55328SJohn Baldwin {
5163cf55328SJohn Baldwin 
5173cf55328SJohn Baldwin 	if (type == SYS_RES_IOPORT)
5183cf55328SJohn Baldwin 		type = SYS_RES_MEMORY;
5193cf55328SJohn Baldwin 	return (bus_generic_deactivate_resource(bus, child, type, rid, r));
5203cf55328SJohn Baldwin }
5213cf55328SJohn Baldwin 
5223cf55328SJohn Baldwin static int
52306763f5eSNathan Whitehorn simplebus_print_res(struct simplebus_devinfo *di)
5241f40dbc8SBrooks Davis {
52506763f5eSNathan Whitehorn 	int rv;
5261f40dbc8SBrooks Davis 
5276fc608bfSMichal Meloun 	if (di == NULL)
5286fc608bfSMichal Meloun 		return (0);
52906763f5eSNathan Whitehorn 	rv = 0;
530da1b038aSJustin Hibbits 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
531da1b038aSJustin Hibbits 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
53206763f5eSNathan Whitehorn 	return (rv);
53306763f5eSNathan Whitehorn }
5341f40dbc8SBrooks Davis 
53506763f5eSNathan Whitehorn static void
53606763f5eSNathan Whitehorn simplebus_probe_nomatch(device_t bus, device_t child)
53706763f5eSNathan Whitehorn {
538cca75397SWarner Losh 	const char *name, *type, *compat;
53906763f5eSNathan Whitehorn 
54006763f5eSNathan Whitehorn 	if (!bootverbose)
54106763f5eSNathan Whitehorn 		return;
54206763f5eSNathan Whitehorn 
543ecaecbc7SIan Lepore 	compat = ofw_bus_get_compat(child);
544ecaecbc7SIan Lepore 	if (compat == NULL)
545ecaecbc7SIan Lepore 		return;
54606763f5eSNathan Whitehorn 	name = ofw_bus_get_name(child);
54706763f5eSNathan Whitehorn 	type = ofw_bus_get_type(child);
54806763f5eSNathan Whitehorn 
54906763f5eSNathan Whitehorn 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
55006763f5eSNathan Whitehorn 	simplebus_print_res(device_get_ivars(child));
551cca75397SWarner Losh 	if (!ofw_bus_status_okay(child))
552cca75397SWarner Losh 		printf(" disabled");
553cca75397SWarner Losh 	if (type)
554cca75397SWarner Losh 		printf(" type %s", type);
555ecaecbc7SIan Lepore 	printf(" compat %s (no driver attached)\n", compat);
5561f40dbc8SBrooks Davis }
5571f40dbc8SBrooks Davis 
5581f40dbc8SBrooks Davis static int
55906763f5eSNathan Whitehorn simplebus_print_child(device_t bus, device_t child)
5601f40dbc8SBrooks Davis {
56106763f5eSNathan Whitehorn 	int rv;
5621f40dbc8SBrooks Davis 
56306763f5eSNathan Whitehorn 	rv = bus_print_child_header(bus, child);
56406763f5eSNathan Whitehorn 	rv += simplebus_print_res(device_get_ivars(child));
565cca75397SWarner Losh 	if (!ofw_bus_status_okay(child))
566cca75397SWarner Losh 		rv += printf(" disabled");
56706763f5eSNathan Whitehorn 	rv += bus_print_child_footer(bus, child);
56806763f5eSNathan Whitehorn 	return (rv);
5691f40dbc8SBrooks Davis }
570