xref: /freebsd/sys/dev/fdt/simplebus.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/rman.h>
38 
39 #include <dev/ofw/openfirm.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 
43 #include <dev/fdt/simplebus.h>
44 
45 /*
46  * Bus interface.
47  */
48 static int		simplebus_probe(device_t dev);
49 static int		simplebus_attach(device_t dev);
50 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
51     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
52 static void		simplebus_probe_nomatch(device_t bus, device_t child);
53 static int		simplebus_print_child(device_t bus, device_t child);
54 static device_t		simplebus_add_child(device_t dev, u_int order,
55     const char *name, int unit);
56 static struct resource_list *simplebus_get_resource_list(device_t bus,
57     device_t child);
58 /*
59  * ofw_bus interface
60  */
61 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
62     device_t child);
63 
64 /*
65  * local methods
66  */
67 
68 static int simplebus_fill_ranges(phandle_t node,
69     struct simplebus_softc *sc);
70 
71 /*
72  * Driver methods.
73  */
74 static device_method_t	simplebus_methods[] = {
75 	/* Device interface */
76 	DEVMETHOD(device_probe,		simplebus_probe),
77 	DEVMETHOD(device_attach,	simplebus_attach),
78 	DEVMETHOD(device_detach,	bus_generic_detach),
79 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
80 	DEVMETHOD(device_suspend,	bus_generic_suspend),
81 	DEVMETHOD(device_resume,	bus_generic_resume),
82 
83 	/* Bus interface */
84 	DEVMETHOD(bus_add_child,	simplebus_add_child),
85 	DEVMETHOD(bus_print_child,	simplebus_print_child),
86 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
87 	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
88 	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
89 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
90 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
91 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
92 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
93 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
94 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
95 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
96 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
97 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
98 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
99 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
100 
101 	/* ofw_bus interface */
102 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
103 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
104 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
105 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
106 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
107 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
108 
109 	DEVMETHOD_END
110 };
111 
112 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
113     sizeof(struct simplebus_softc));
114 
115 static devclass_t simplebus_devclass;
116 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
117     0, 0, BUS_PASS_BUS);
118 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
119     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
120 
121 static int
122 simplebus_probe(device_t dev)
123 {
124 
125 	if (!ofw_bus_status_okay(dev))
126 		return (ENXIO);
127 
128 	/*
129 	 * FDT data puts a "simple-bus" compatible string on many things that
130 	 * have children but aren't really buses in our world.  Without a
131 	 * ranges property we will fail to attach, so just fail to probe too.
132 	 */
133 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
134 	    ofw_bus_has_prop(dev, "ranges")) &&
135 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
136 	     "soc") != 0))
137 		return (ENXIO);
138 
139 	device_set_desc(dev, "Flattened device tree simple bus");
140 
141 	return (BUS_PROBE_GENERIC);
142 }
143 
144 static int
145 simplebus_attach(device_t dev)
146 {
147 	struct		simplebus_softc *sc;
148 	phandle_t	node;
149 
150 	sc = device_get_softc(dev);
151 	simplebus_init(dev, 0);
152 	if (simplebus_fill_ranges(sc->node, sc) < 0) {
153 		device_printf(dev, "could not get ranges\n");
154 		return (ENXIO);
155 	}
156 
157 	/*
158 	 * In principle, simplebus could have an interrupt map, but ignore that
159 	 * for now
160 	 */
161 
162 	for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
163 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
164 	return (bus_generic_attach(dev));
165 }
166 
167 void
168 simplebus_init(device_t dev, phandle_t node)
169 {
170 	struct simplebus_softc *sc;
171 
172 	sc = device_get_softc(dev);
173 	if (node == 0)
174 		node = ofw_bus_get_node(dev);
175 	sc->dev = dev;
176 	sc->node = node;
177 
178 	/*
179 	 * Some important numbers
180 	 */
181 	sc->acells = 2;
182 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
183 	sc->scells = 1;
184 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
185 }
186 
187 static int
188 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
189 {
190 	int host_address_cells;
191 	cell_t *base_ranges;
192 	ssize_t nbase_ranges;
193 	int err;
194 	int i, j, k;
195 
196 	err = OF_searchencprop(OF_parent(node), "#address-cells",
197 	    &host_address_cells, sizeof(host_address_cells));
198 	if (err <= 0)
199 		return (-1);
200 
201 	nbase_ranges = OF_getproplen(node, "ranges");
202 	if (nbase_ranges < 0)
203 		return (-1);
204 	sc->nranges = nbase_ranges / sizeof(cell_t) /
205 	    (sc->acells + host_address_cells + sc->scells);
206 	if (sc->nranges == 0)
207 		return (0);
208 
209 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
210 	    M_DEVBUF, M_WAITOK);
211 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
212 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
213 
214 	for (i = 0, j = 0; i < sc->nranges; i++) {
215 		sc->ranges[i].bus = 0;
216 		for (k = 0; k < sc->acells; k++) {
217 			sc->ranges[i].bus <<= 32;
218 			sc->ranges[i].bus |= base_ranges[j++];
219 		}
220 		sc->ranges[i].host = 0;
221 		for (k = 0; k < host_address_cells; k++) {
222 			sc->ranges[i].host <<= 32;
223 			sc->ranges[i].host |= base_ranges[j++];
224 		}
225 		sc->ranges[i].size = 0;
226 		for (k = 0; k < sc->scells; k++) {
227 			sc->ranges[i].size <<= 32;
228 			sc->ranges[i].size |= base_ranges[j++];
229 		}
230 	}
231 
232 	free(base_ranges, M_DEVBUF);
233 	return (sc->nranges);
234 }
235 
236 struct simplebus_devinfo *
237 simplebus_setup_dinfo(device_t dev, phandle_t node,
238     struct simplebus_devinfo *di)
239 {
240 	struct simplebus_softc *sc;
241 	struct simplebus_devinfo *ndi;
242 
243 	sc = device_get_softc(dev);
244 	if (di == NULL)
245 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
246 	else
247 		ndi = di;
248 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
249 		if (di == NULL)
250 			free(ndi, M_DEVBUF);
251 		return (NULL);
252 	}
253 
254 	resource_list_init(&ndi->rl);
255 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
256 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
257 
258 	return (ndi);
259 }
260 
261 device_t
262 simplebus_add_device(device_t dev, phandle_t node, u_int order,
263     const char *name, int unit, struct simplebus_devinfo *di)
264 {
265 	struct simplebus_devinfo *ndi;
266 	device_t cdev;
267 
268 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
269 		return (NULL);
270 	cdev = device_add_child_ordered(dev, order, name, unit);
271 	if (cdev == NULL) {
272 		device_printf(dev, "<%s>: device_add_child failed\n",
273 		    ndi->obdinfo.obd_name);
274 		resource_list_free(&ndi->rl);
275 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
276 		if (di == NULL)
277 			free(ndi, M_DEVBUF);
278 		return (NULL);
279 	}
280 	device_set_ivars(cdev, ndi);
281 
282 	return(cdev);
283 }
284 
285 static device_t
286 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
287 {
288 	device_t cdev;
289 	struct simplebus_devinfo *ndi;
290 
291 	cdev = device_add_child_ordered(dev, order, name, unit);
292 	if (cdev == NULL)
293 		return (NULL);
294 
295 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
296 	ndi->obdinfo.obd_node = -1;
297 	resource_list_init(&ndi->rl);
298 	device_set_ivars(cdev, ndi);
299 
300 	return (cdev);
301 }
302 
303 static const struct ofw_bus_devinfo *
304 simplebus_get_devinfo(device_t bus __unused, device_t child)
305 {
306         struct simplebus_devinfo *ndi;
307 
308         ndi = device_get_ivars(child);
309 	if (ndi == NULL)
310 		return (NULL);
311         return (&ndi->obdinfo);
312 }
313 
314 static struct resource_list *
315 simplebus_get_resource_list(device_t bus __unused, device_t child)
316 {
317 	struct simplebus_devinfo *ndi;
318 
319 	ndi = device_get_ivars(child);
320 	if (ndi == NULL)
321 		return (NULL);
322 	return (&ndi->rl);
323 }
324 
325 static struct resource *
326 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
327     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
328 {
329 	struct simplebus_softc *sc;
330 	struct simplebus_devinfo *di;
331 	struct resource_list_entry *rle;
332 	int j;
333 
334 	sc = device_get_softc(bus);
335 
336 	/*
337 	 * Request for the default allocation with a given rid: use resource
338 	 * list stored in the local device info.
339 	 */
340 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
341 		if ((di = device_get_ivars(child)) == NULL)
342 			return (NULL);
343 
344 		if (type == SYS_RES_IOPORT)
345 			type = SYS_RES_MEMORY;
346 
347 		rle = resource_list_find(&di->rl, type, *rid);
348 		if (rle == NULL) {
349 			if (bootverbose)
350 				device_printf(bus, "no default resources for "
351 				    "rid = %d, type = %d\n", *rid, type);
352 			return (NULL);
353 		}
354 		start = rle->start;
355 		end = rle->end;
356 		count = rle->count;
357         }
358 
359 	if (type == SYS_RES_MEMORY) {
360 		/* Remap through ranges property */
361 		for (j = 0; j < sc->nranges; j++) {
362 			if (start >= sc->ranges[j].bus && end <
363 			    sc->ranges[j].bus + sc->ranges[j].size) {
364 				start -= sc->ranges[j].bus;
365 				start += sc->ranges[j].host;
366 				end -= sc->ranges[j].bus;
367 				end += sc->ranges[j].host;
368 				break;
369 			}
370 		}
371 		if (j == sc->nranges && sc->nranges != 0) {
372 			if (bootverbose)
373 				device_printf(bus, "Could not map resource "
374 				    "%#jx-%#jx\n", start, end);
375 
376 			return (NULL);
377 		}
378 	}
379 
380 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
381 	    count, flags));
382 }
383 
384 static int
385 simplebus_print_res(struct simplebus_devinfo *di)
386 {
387 	int rv;
388 
389 	if (di == NULL)
390 		return (0);
391 	rv = 0;
392 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
393 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
394 	return (rv);
395 }
396 
397 static void
398 simplebus_probe_nomatch(device_t bus, device_t child)
399 {
400 	const char *name, *type, *compat;
401 
402 	if (!bootverbose)
403 		return;
404 
405 	compat = ofw_bus_get_compat(child);
406 	if (compat == NULL)
407 		return;
408 	name = ofw_bus_get_name(child);
409 	type = ofw_bus_get_type(child);
410 
411 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
412 	simplebus_print_res(device_get_ivars(child));
413 	if (!ofw_bus_status_okay(child))
414 		printf(" disabled");
415 	if (type)
416 		printf(" type %s", type);
417 	printf(" compat %s (no driver attached)\n", compat);
418 }
419 
420 static int
421 simplebus_print_child(device_t bus, device_t child)
422 {
423 	int rv;
424 
425 	rv = bus_print_child_header(bus, child);
426 	rv += simplebus_print_res(device_get_ivars(child));
427 	if (!ofw_bus_status_okay(child))
428 		rv += printf(" disabled");
429 	rv += bus_print_child_footer(bus, child);
430 	return (rv);
431 }
432