simplebus.c revision 261410
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: head/sys/dev/fdt/simplebus.c 261410 2014-02-02 19:17:28Z ian $");
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
41struct simplebus_range {
42	uint64_t bus;
43	uint64_t host;
44	uint64_t size;
45};
46
47struct simplebus_softc {
48	device_t dev;
49	phandle_t node;
50
51	struct simplebus_range *ranges;
52	int nranges;
53
54	pcell_t acells, scells;
55};
56
57struct simplebus_devinfo {
58	struct ofw_bus_devinfo	obdinfo;
59	struct resource_list	rl;
60};
61
62/*
63 * Bus interface.
64 */
65static int		simplebus_probe(device_t dev);
66static int		simplebus_attach(device_t dev);
67static struct resource *simplebus_alloc_resource(device_t, device_t, int,
68    int *, u_long, u_long, u_long, u_int);
69static void		simplebus_probe_nomatch(device_t bus, device_t child);
70static int		simplebus_print_child(device_t bus, device_t child);
71
72/*
73 * ofw_bus interface
74 */
75static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
76    device_t child);
77
78/*
79 * local methods
80 */
81
82static int simplebus_fill_ranges(phandle_t node,
83    struct simplebus_softc *sc);
84static struct simplebus_devinfo *simplebus_setup_dinfo(device_t dev,
85    phandle_t node);
86
87/*
88 * Driver methods.
89 */
90static device_method_t	simplebus_methods[] = {
91	/* Device interface */
92	DEVMETHOD(device_probe,		simplebus_probe),
93	DEVMETHOD(device_attach,	simplebus_attach),
94
95	/* Bus interface */
96	DEVMETHOD(bus_print_child,	simplebus_print_child),
97	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
98	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
99	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
100	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
101	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
102	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
103	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
104	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
105	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
106
107	/* ofw_bus interface */
108	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
109	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
110	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
111	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
112	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
113	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
114
115	DEVMETHOD_END
116};
117
118static driver_t simplebus_driver = {
119	"simplebus",
120	simplebus_methods,
121	sizeof(struct simplebus_softc)
122};
123static devclass_t simplebus_devclass;
124DRIVER_MODULE(simplebus, nexus, simplebus_driver, simplebus_devclass, 0, 0);
125DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass, 0, 0);
126
127static int
128simplebus_probe(device_t dev)
129{
130
131	if (!ofw_bus_status_okay(dev))
132		return (ENXIO);
133
134	if (!ofw_bus_is_compatible(dev, "simple-bus") &&
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
144static int
145simplebus_attach(device_t dev)
146{
147	struct		simplebus_softc *sc;
148	struct		simplebus_devinfo *di;
149	phandle_t	node;
150	device_t	cdev;
151
152	node = ofw_bus_get_node(dev);
153	sc = device_get_softc(dev);
154
155	sc->dev = dev;
156	sc->node = node;
157
158	/*
159	 * Some important numbers
160	 */
161	sc->acells = 2;
162	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
163	sc->scells = 1;
164	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
165
166	if (simplebus_fill_ranges(node, sc) < 0) {
167		device_printf(dev, "could not get ranges\n");
168		return (ENXIO);
169	}
170
171	/*
172	 * In principle, simplebus could have an interrupt map, but ignore that
173	 * for now
174	 */
175
176	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
177		if ((di = simplebus_setup_dinfo(dev, node)) == NULL)
178			continue;
179		cdev = device_add_child(dev, NULL, -1);
180		if (cdev == NULL) {
181			device_printf(dev, "<%s>: device_add_child failed\n",
182			    di->obdinfo.obd_name);
183			resource_list_free(&di->rl);
184			ofw_bus_gen_destroy_devinfo(&di->obdinfo);
185			free(di, M_DEVBUF);
186			continue;
187		}
188		device_set_ivars(cdev, di);
189	}
190
191	return (bus_generic_attach(dev));
192}
193
194static int
195simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
196{
197	int host_address_cells;
198	cell_t *base_ranges;
199	ssize_t nbase_ranges;
200	int err;
201	int i, j, k;
202
203	err = OF_searchencprop(OF_parent(node), "#address-cells",
204	    &host_address_cells, sizeof(host_address_cells));
205	if (err <= 0)
206		return (-1);
207
208	nbase_ranges = OF_getproplen(node, "ranges");
209	if (nbase_ranges < 0)
210		return (-1);
211	sc->nranges = nbase_ranges / sizeof(cell_t) /
212	    (sc->acells + host_address_cells + sc->scells);
213	if (sc->nranges == 0)
214		return (0);
215
216	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
217	    M_DEVBUF, M_WAITOK);
218	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
219	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
220
221	for (i = 0, j = 0; i < sc->nranges; i++) {
222		sc->ranges[i].bus = 0;
223		for (k = 0; k < sc->acells; k++) {
224			sc->ranges[i].bus <<= 32;
225			sc->ranges[i].bus |= base_ranges[j++];
226		}
227		sc->ranges[i].host = 0;
228		for (k = 0; k < host_address_cells; k++) {
229			sc->ranges[i].host <<= 32;
230			sc->ranges[i].host |= base_ranges[j++];
231		}
232		sc->ranges[i].size = 0;
233		for (k = 0; k < sc->scells; k++) {
234			sc->ranges[i].size <<= 32;
235			sc->ranges[i].size |= base_ranges[j++];
236		}
237	}
238
239	free(base_ranges, M_DEVBUF);
240	return (sc->nranges);
241}
242
243static struct simplebus_devinfo *
244simplebus_setup_dinfo(device_t dev, phandle_t node)
245{
246	struct simplebus_softc *sc;
247	struct simplebus_devinfo *ndi;
248	uint32_t *reg, *intr, icells;
249	uint64_t phys, size;
250	phandle_t iparent;
251	int i, j, k;
252	int nintr;
253	int nreg;
254
255	sc = device_get_softc(dev);
256
257	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
258	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
259		free(ndi, M_DEVBUF);
260		return (NULL);
261	}
262
263	resource_list_init(&ndi->rl);
264	nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
265	if (nreg == -1)
266		nreg = 0;
267	if (nreg % (sc->acells + sc->scells) != 0) {
268		if (bootverbose)
269			device_printf(dev, "Malformed reg property on <%s>\n",
270			    ndi->obdinfo.obd_name);
271		nreg = 0;
272	}
273
274	for (i = 0, k = 0; i < nreg; i += sc->acells + sc->scells, k++) {
275		phys = size = 0;
276		for (j = 0; j < sc->acells; j++) {
277			phys <<= 32;
278			phys |= reg[i + j];
279		}
280		for (j = 0; j < sc->scells; j++) {
281			size <<= 32;
282			size |= reg[i + sc->acells + j];
283		}
284
285		resource_list_add(&ndi->rl, SYS_RES_MEMORY, k,
286		    phys, phys + size - 1, size);
287	}
288	free(reg, M_OFWPROP);
289
290	nintr = OF_getencprop_alloc(node, "interrupts",  sizeof(*intr),
291	    (void **)&intr);
292	if (nintr > 0) {
293		if (OF_searchencprop(node, "interrupt-parent", &iparent,
294		    sizeof(iparent)) == -1) {
295			device_printf(dev, "No interrupt-parent found, "
296			    "assuming direct parent\n");
297			iparent = OF_parent(node);
298		}
299		if (OF_searchencprop(OF_xref_phandle(iparent),
300		    "#interrupt-cells", &icells, sizeof(icells)) == -1) {
301			device_printf(dev, "Missing #interrupt-cells property, "
302			    "assuming <1>\n");
303			icells = 1;
304		}
305		if (icells < 1 || icells > nintr) {
306			device_printf(dev, "Invalid #interrupt-cells property "
307			    "value <%d>, assuming <1>\n", icells);
308			icells = 1;
309		}
310		for (i = 0, k = 0; i < nintr; i += icells, k++) {
311			intr[i] = ofw_bus_map_intr(dev, iparent, icells,
312			    &intr[i]);
313			resource_list_add(&ndi->rl, SYS_RES_IRQ, k, intr[i],
314			    intr[i], 1);
315		}
316		free(intr, M_OFWPROP);
317	}
318
319	return (ndi);
320}
321
322static const struct ofw_bus_devinfo *
323simplebus_get_devinfo(device_t bus __unused, device_t child)
324{
325        struct simplebus_devinfo *ndi;
326
327        ndi = device_get_ivars(child);
328        return (&ndi->obdinfo);
329}
330
331static struct resource *
332simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
333    u_long start, u_long end, u_long count, u_int flags)
334{
335	struct simplebus_softc *sc;
336	struct simplebus_devinfo *di;
337	struct resource_list_entry *rle;
338	int j;
339
340	sc = device_get_softc(bus);
341
342	/*
343	 * Request for the default allocation with a given rid: use resource
344	 * list stored in the local device info.
345	 */
346	if ((start == 0UL) && (end == ~0UL)) {
347		if ((di = device_get_ivars(child)) == NULL)
348			return (NULL);
349
350		if (type == SYS_RES_IOPORT)
351			type = SYS_RES_MEMORY;
352
353		rle = resource_list_find(&di->rl, type, *rid);
354		if (rle == NULL) {
355			if (bootverbose)
356				device_printf(bus, "no default resources for "
357				    "rid = %d, type = %d\n", *rid, type);
358			return (NULL);
359		}
360		start = rle->start;
361		end = rle->end;
362		count = rle->count;
363        }
364
365	if (type == SYS_RES_MEMORY) {
366		/* Remap through ranges property */
367		for (j = 0; j < sc->nranges; j++) {
368			if (start >= sc->ranges[j].bus && end <
369			    sc->ranges[j].bus + sc->ranges[j].size) {
370				start -= sc->ranges[j].bus;
371				start += sc->ranges[j].host;
372				end -= sc->ranges[j].bus;
373				end += sc->ranges[j].host;
374				break;
375			}
376		}
377		if (j == sc->nranges && sc->nranges != 0) {
378			if (bootverbose)
379				device_printf(bus, "Could not map resource "
380				    "%#lx-%#lx\n", start, end);
381
382			return (NULL);
383		}
384	}
385
386	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
387	    count, flags));
388}
389
390static int
391simplebus_print_res(struct simplebus_devinfo *di)
392{
393	int rv;
394
395	rv = 0;
396	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#lx");
397	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%ld");
398	return (rv);
399}
400
401static void
402simplebus_probe_nomatch(device_t bus, device_t child)
403{
404	const char *name, *type;
405
406	if (!bootverbose)
407		return;
408
409	name = ofw_bus_get_name(child);
410	type = ofw_bus_get_type(child);
411
412	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
413	simplebus_print_res(device_get_ivars(child));
414	printf(" type %s (no driver attached)\n",
415	    type != NULL ? type : "unknown");
416}
417
418static int
419simplebus_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	rv += bus_print_child_footer(bus, child);
426	return (rv);
427}
428
429