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