xref: /freebsd/sys/dev/fdt/simplebus.c (revision ad30f8e79bd1007cc2476e491bd21b4f5e389e0a)
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_platform.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/ktr.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 #include <sys/malloc.h>
42 
43 #include <machine/fdt.h>
44 
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
48 
49 #include "fdt_common.h"
50 #include "ofw_bus_if.h"
51 
52 #define DEBUG
53 #undef DEBUG
54 
55 #ifdef DEBUG
56 #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
57     printf(fmt,##args); } while (0)
58 #else
59 #define debugf(fmt, args...)
60 #endif
61 
62 static MALLOC_DEFINE(M_SIMPLEBUS, "simplebus", "simplebus devices information");
63 
64 struct simplebus_softc {
65 	int	sc_addr_cells;
66 	int	sc_size_cells;
67 	u_long	sc_start_pa;
68 	u_long	sc_start_va;
69 	u_long	sc_size;
70 };
71 
72 struct simplebus_devinfo {
73 	struct ofw_bus_devinfo	di_ofw;
74 	struct resource_list	di_res;
75 
76 	/* Interrupts sense-level info for this device */
77 	struct fdt_sense_level	di_intr_sl[DI_MAX_INTR_NUM];
78 };
79 
80 /*
81  * Prototypes.
82  */
83 static int simplebus_probe(device_t);
84 static int simplebus_attach(device_t);
85 
86 static int simplebus_print_child(device_t, device_t);
87 static int simplebus_setup_intr(device_t, device_t, struct resource *, int,
88     driver_filter_t *, driver_intr_t *, void *, void **);
89 
90 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
91     int *, u_long, u_long, u_long, u_int);
92 static struct resource_list *simplebus_get_resource_list(device_t, device_t);
93 
94 static ofw_bus_get_devinfo_t simplebus_get_devinfo;
95 
96 /*
97  * Bus interface definition.
98  */
99 static device_method_t simplebus_methods[] = {
100 	/* Device interface */
101 	DEVMETHOD(device_probe,		simplebus_probe),
102 	DEVMETHOD(device_attach,	simplebus_attach),
103 	DEVMETHOD(device_detach,	bus_generic_detach),
104 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
105 	DEVMETHOD(device_suspend,	bus_generic_suspend),
106 	DEVMETHOD(device_resume,	bus_generic_resume),
107 
108 	/* Bus interface */
109 	DEVMETHOD(bus_print_child,	simplebus_print_child),
110 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
111 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
112 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
113 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
114 	DEVMETHOD(bus_setup_intr,	simplebus_setup_intr),
115 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
116 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
117 
118 	/* OFW bus interface */
119 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
120 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
121 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
122 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
123 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
124 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
125 
126 	{ 0, 0 }
127 };
128 
129 static driver_t simplebus_driver = {
130 	"simplebus",
131 	simplebus_methods,
132 	sizeof(struct simplebus_softc)
133 };
134 
135 devclass_t simplebus_devclass;
136 
137 DRIVER_MODULE(simplebus, fdtbus, simplebus_driver, simplebus_devclass, 0, 0);
138 
139 static int
140 simplebus_probe(device_t dev)
141 {
142 
143 	if (!ofw_bus_is_compatible_strict(dev, "simple-bus"))
144 		return (ENXIO);
145 
146 	device_set_desc(dev, "Flattened device tree simple bus");
147 
148 	return (BUS_PROBE_DEFAULT);
149 }
150 
151 static int
152 simplebus_attach(device_t dev)
153 {
154 	device_t dev_child;
155 	struct simplebus_devinfo *di;
156 	struct simplebus_softc *sc;
157 	phandle_t dt_node, dt_child;
158 
159 	sc = device_get_softc(dev);
160 
161 	sc->sc_start_pa = fdt_immr_pa;
162 	sc->sc_start_va = fdt_immr_va;
163 	sc->sc_size = fdt_immr_size;
164 
165 	/*
166 	 * Walk simple-bus and add direct subordinates as our children.
167 	 */
168 	dt_node = ofw_bus_get_node(dev);
169 	for (dt_child = OF_child(dt_node); dt_child != 0;
170 	    dt_child = OF_peer(dt_child)) {
171 
172 		/* Check and process 'status' property. */
173 		if (!(fdt_is_enabled(dt_child)))
174 			continue;
175 
176 		if (!(fdt_pm_is_enabled(dt_child)))
177 			continue;
178 
179 		di = malloc(sizeof(*di), M_SIMPLEBUS, M_WAITOK | M_ZERO);
180 
181 		if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) {
182 			free(di, M_SIMPLEBUS);
183 			device_printf(dev, "could not set up devinfo\n");
184 			continue;
185 		}
186 
187 		resource_list_init(&di->di_res);
188 
189 		if (fdt_reg_to_rl(dt_child, &di->di_res, sc->sc_start_va)) {
190 			device_printf(dev, "%s: could not process 'reg' "
191 			    "property\n", di->di_ofw.obd_name);
192 			ofw_bus_gen_destroy_devinfo(&di->di_ofw);
193 			free(di, M_SIMPLEBUS);
194 			continue;
195 		}
196 
197 		if (fdt_intr_to_rl(dt_child, &di->di_res, di->di_intr_sl)) {
198 			device_printf(dev, "%s: could not process "
199 			    "'interrupts' property\n", di->di_ofw.obd_name);
200 			resource_list_free(&di->di_res);
201 			ofw_bus_gen_destroy_devinfo(&di->di_ofw);
202 			free(di, M_SIMPLEBUS);
203 			continue;
204 		}
205 
206 		/* Add newbus device for this FDT node */
207 		dev_child = device_add_child(dev, NULL, -1);
208 		if (dev_child == NULL) {
209 			device_printf(dev, "could not add child: %s\n",
210 			    di->di_ofw.obd_name);
211 			resource_list_free(&di->di_res);
212 			ofw_bus_gen_destroy_devinfo(&di->di_ofw);
213 			free(di, M_SIMPLEBUS);
214 			continue;
215 		}
216 #ifdef DEBUG
217 		device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name);
218 #endif
219 		device_set_ivars(dev_child, di);
220 	}
221 
222 	return (bus_generic_attach(dev));
223 }
224 
225 static int
226 simplebus_print_child(device_t dev, device_t child)
227 {
228 	struct simplebus_devinfo *di;
229 	struct resource_list *rl;
230 	int rv;
231 
232 	di = device_get_ivars(child);
233 	rl = &di->di_res;
234 
235 	rv = 0;
236 	rv += bus_print_child_header(dev, child);
237 	rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
238 	rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
239 	rv += bus_print_child_footer(dev, child);
240 
241 	return (rv);
242 }
243 
244 static struct resource *
245 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
246     u_long start, u_long end, u_long count, u_int flags)
247 {
248 	struct simplebus_devinfo *di;
249 	struct resource_list_entry *rle;
250 
251 	/*
252 	 * Request for the default allocation with a given rid: use resource
253 	 * list stored in the local device info.
254 	 */
255 	if ((start == 0UL) && (end == ~0UL)) {
256 		if ((di = device_get_ivars(child)) == NULL)
257 			return (NULL);
258 
259 		if (type == SYS_RES_IOPORT)
260 			type = SYS_RES_MEMORY;
261 
262 		rle = resource_list_find(&di->di_res, type, *rid);
263 		if (rle == NULL) {
264 			device_printf(bus, "no default resources for "
265 			    "rid = %d, type = %d\n", *rid, type);
266 			return (NULL);
267 		}
268 		start = rle->start;
269 		end = rle->end;
270 		count = rle->count;
271 	}
272 
273 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
274 	    count, flags));
275 }
276 
277 static struct resource_list *
278 simplebus_get_resource_list(device_t bus, device_t child)
279 {
280 	struct simplebus_devinfo *di;
281 
282 	di = device_get_ivars(child);
283 	return (&di->di_res);
284 }
285 
286 static int
287 simplebus_setup_intr(device_t bus, device_t child, struct resource *res,
288     int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg,
289     void **cookiep)
290 {
291 	struct simplebus_devinfo *di;
292 	enum intr_trigger trig;
293 	enum intr_polarity pol;
294 	int irq, rid;
295 
296 	if (res == NULL)
297 		panic("simplebus_setup_intr: NULL irq resource!");
298 
299 	rid = rman_get_rid(res);
300 	if (rid > DI_MAX_INTR_NUM) {
301 		device_printf(child, "rid out of range rid = %d\n", rid);
302 		return (ERANGE);
303 	}
304 
305 	irq = rman_get_start(res);
306 
307 	if ((di = device_get_ivars(child)) == NULL) {
308 		device_printf(child, "could not retrieve devinfo\n");
309 		return (ENXIO);
310 	}
311 
312 	trig = di->di_intr_sl[rid].trig;
313 	pol = di->di_intr_sl[rid].pol;
314 
315 	debugf("intr config: irq = %d, trig = %d, pol = %d\n", irq, trig, pol);
316 
317 #if defined(__powerpc__)
318 	int err;
319 
320 	err = powerpc_config_intr(irq, trig, pol);
321 	if (err)
322 		return (err);
323 #endif
324 
325 	return (bus_generic_setup_intr(bus, child, res, flags, filter, ihand,
326 	    arg, cookiep));
327 }
328 
329 static const struct ofw_bus_devinfo *
330 simplebus_get_devinfo(device_t bus, device_t child)
331 {
332 	struct simplebus_devinfo *di;
333 
334 	di = device_get_ivars(child);
335 	return (&di->di_ofw);
336 }
337