xref: /freebsd/sys/dev/fdt/simplebus.c (revision ec0e626bafb335b30c499d06066997f54b10c092)
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 *, u_long, u_long, u_long, 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 
53 /*
54  * ofw_bus interface
55  */
56 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
57     device_t child);
58 
59 /*
60  * local methods
61  */
62 
63 static int simplebus_fill_ranges(phandle_t node,
64     struct simplebus_softc *sc);
65 static struct simplebus_devinfo *simplebus_setup_dinfo(device_t dev,
66     phandle_t node);
67 
68 /*
69  * Driver methods.
70  */
71 static device_method_t	simplebus_methods[] = {
72 	/* Device interface */
73 	DEVMETHOD(device_probe,		simplebus_probe),
74 	DEVMETHOD(device_attach,	simplebus_attach),
75 
76 	/* Bus interface */
77 	DEVMETHOD(bus_print_child,	simplebus_print_child),
78 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
79 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
80 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
81 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
82 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
83 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
84 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
85 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
86 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
87 
88 	/* ofw_bus interface */
89 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
90 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
91 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
92 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
93 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
94 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
95 
96 	DEVMETHOD_END
97 };
98 
99 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
100     sizeof(struct simplebus_softc));
101 
102 static devclass_t simplebus_devclass;
103 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, simplebus_devclass,
104     0, 0, BUS_PASS_BUS);
105 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, simplebus_devclass,
106     0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
107 
108 static int
109 simplebus_probe(device_t dev)
110 {
111 
112 	if (!ofw_bus_status_okay(dev))
113 		return (ENXIO);
114 
115 	/*
116 	 * FDT data puts a "simple-bus" compatible string on many things that
117 	 * have children but aren't really busses in our world.  Without a
118 	 * ranges property we will fail to attach, so just fail to probe too.
119 	 */
120 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
121 	    ofw_bus_has_prop(dev, "ranges")) &&
122 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
123 	     "soc") != 0))
124 		return (ENXIO);
125 
126 	device_set_desc(dev, "Flattened device tree simple bus");
127 
128 	return (BUS_PROBE_GENERIC);
129 }
130 
131 static int
132 simplebus_attach(device_t dev)
133 {
134 	struct		simplebus_softc *sc;
135 	struct		simplebus_devinfo *di;
136 	phandle_t	node;
137 	device_t	cdev;
138 
139 	node = ofw_bus_get_node(dev);
140 	sc = device_get_softc(dev);
141 
142 	sc->dev = dev;
143 	sc->node = node;
144 
145 	/*
146 	 * Some important numbers
147 	 */
148 	sc->acells = 2;
149 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
150 	sc->scells = 1;
151 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
152 
153 	if (simplebus_fill_ranges(node, sc) < 0) {
154 		device_printf(dev, "could not get ranges\n");
155 		return (ENXIO);
156 	}
157 
158 	/*
159 	 * In principle, simplebus could have an interrupt map, but ignore that
160 	 * for now
161 	 */
162 
163 	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
164 		if ((di = simplebus_setup_dinfo(dev, node)) == NULL)
165 			continue;
166 		cdev = device_add_child(dev, NULL, -1);
167 		if (cdev == NULL) {
168 			device_printf(dev, "<%s>: device_add_child failed\n",
169 			    di->obdinfo.obd_name);
170 			resource_list_free(&di->rl);
171 			ofw_bus_gen_destroy_devinfo(&di->obdinfo);
172 			free(di, M_DEVBUF);
173 			continue;
174 		}
175 		device_set_ivars(cdev, di);
176 	}
177 
178 	return (bus_generic_attach(dev));
179 }
180 
181 static int
182 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
183 {
184 	int host_address_cells;
185 	cell_t *base_ranges;
186 	ssize_t nbase_ranges;
187 	int err;
188 	int i, j, k;
189 
190 	err = OF_searchencprop(OF_parent(node), "#address-cells",
191 	    &host_address_cells, sizeof(host_address_cells));
192 	if (err <= 0)
193 		return (-1);
194 
195 	nbase_ranges = OF_getproplen(node, "ranges");
196 	if (nbase_ranges < 0)
197 		return (-1);
198 	sc->nranges = nbase_ranges / sizeof(cell_t) /
199 	    (sc->acells + host_address_cells + sc->scells);
200 	if (sc->nranges == 0)
201 		return (0);
202 
203 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
204 	    M_DEVBUF, M_WAITOK);
205 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
206 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
207 
208 	for (i = 0, j = 0; i < sc->nranges; i++) {
209 		sc->ranges[i].bus = 0;
210 		for (k = 0; k < sc->acells; k++) {
211 			sc->ranges[i].bus <<= 32;
212 			sc->ranges[i].bus |= base_ranges[j++];
213 		}
214 		sc->ranges[i].host = 0;
215 		for (k = 0; k < host_address_cells; k++) {
216 			sc->ranges[i].host <<= 32;
217 			sc->ranges[i].host |= base_ranges[j++];
218 		}
219 		sc->ranges[i].size = 0;
220 		for (k = 0; k < sc->scells; k++) {
221 			sc->ranges[i].size <<= 32;
222 			sc->ranges[i].size |= base_ranges[j++];
223 		}
224 	}
225 
226 	free(base_ranges, M_DEVBUF);
227 	return (sc->nranges);
228 }
229 
230 static struct simplebus_devinfo *
231 simplebus_setup_dinfo(device_t dev, phandle_t node)
232 {
233 	struct simplebus_softc *sc;
234 	struct simplebus_devinfo *ndi;
235 
236 	sc = device_get_softc(dev);
237 
238 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
239 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
240 		free(ndi, M_DEVBUF);
241 		return (NULL);
242 	}
243 
244 	resource_list_init(&ndi->rl);
245 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
246 	ofw_bus_intr_to_rl(dev, node, &ndi->rl);
247 
248 	return (ndi);
249 }
250 
251 static const struct ofw_bus_devinfo *
252 simplebus_get_devinfo(device_t bus __unused, device_t child)
253 {
254         struct simplebus_devinfo *ndi;
255 
256         ndi = device_get_ivars(child);
257         return (&ndi->obdinfo);
258 }
259 
260 static struct resource *
261 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
262     u_long start, u_long end, u_long count, u_int flags)
263 {
264 	struct simplebus_softc *sc;
265 	struct simplebus_devinfo *di;
266 	struct resource_list_entry *rle;
267 	int j;
268 
269 	sc = device_get_softc(bus);
270 
271 	/*
272 	 * Request for the default allocation with a given rid: use resource
273 	 * list stored in the local device info.
274 	 */
275 	if ((start == 0UL) && (end == ~0UL)) {
276 		if ((di = device_get_ivars(child)) == NULL)
277 			return (NULL);
278 
279 		if (type == SYS_RES_IOPORT)
280 			type = SYS_RES_MEMORY;
281 
282 		rle = resource_list_find(&di->rl, type, *rid);
283 		if (rle == NULL) {
284 			if (bootverbose)
285 				device_printf(bus, "no default resources for "
286 				    "rid = %d, type = %d\n", *rid, type);
287 			return (NULL);
288 		}
289 		start = rle->start;
290 		end = rle->end;
291 		count = rle->count;
292         }
293 
294 	if (type == SYS_RES_MEMORY) {
295 		/* Remap through ranges property */
296 		for (j = 0; j < sc->nranges; j++) {
297 			if (start >= sc->ranges[j].bus && end <
298 			    sc->ranges[j].bus + sc->ranges[j].size) {
299 				start -= sc->ranges[j].bus;
300 				start += sc->ranges[j].host;
301 				end -= sc->ranges[j].bus;
302 				end += sc->ranges[j].host;
303 				break;
304 			}
305 		}
306 		if (j == sc->nranges && sc->nranges != 0) {
307 			if (bootverbose)
308 				device_printf(bus, "Could not map resource "
309 				    "%#lx-%#lx\n", start, end);
310 
311 			return (NULL);
312 		}
313 	}
314 
315 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
316 	    count, flags));
317 }
318 
319 static int
320 simplebus_print_res(struct simplebus_devinfo *di)
321 {
322 	int rv;
323 
324 	rv = 0;
325 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#lx");
326 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%ld");
327 	return (rv);
328 }
329 
330 static void
331 simplebus_probe_nomatch(device_t bus, device_t child)
332 {
333 	const char *name, *type, *compat;
334 
335 	if (!bootverbose)
336 		return;
337 
338 	name = ofw_bus_get_name(child);
339 	type = ofw_bus_get_type(child);
340 	compat = ofw_bus_get_compat(child);
341 
342 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
343 	simplebus_print_res(device_get_ivars(child));
344 	if (!ofw_bus_status_okay(child))
345 		printf(" disabled");
346 	if (type)
347 		printf(" type %s", type);
348 	if (compat)
349 		printf(" compat %s", compat);
350 	printf(" (no driver attached)\n");
351 }
352 
353 static int
354 simplebus_print_child(device_t bus, device_t child)
355 {
356 	int rv;
357 
358 	rv = bus_print_child_header(bus, child);
359 	rv += simplebus_print_res(device_get_ivars(child));
360 	if (!ofw_bus_status_okay(child))
361 		rv += printf(" disabled");
362 	rv += bus_print_child_footer(bus, child);
363 	return (rv);
364 }
365