2 * Copyright (c) 2011,2016 The FreeBSD Foundation
5 * This software was developed by Andrew Turner under
6 * sponsorship from the FreeBSD Foundation.
8 * Developed by Damjan Marion <damjan.marion@gmail.com>
10 * Based on OMAP4 GIC code by Ben Gray
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the company nor the name of the author may be used to
21 * endorse or promote products derived from this software without specific
22 * prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "opt_platform.h"
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <machine/intr.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
54 #include <arm/arm/gic.h>
57 struct arm_gic_devinfo {
58 struct ofw_bus_devinfo obdinfo;
59 struct resource_list rl;
63 static device_probe_t gic_fdt_probe;
64 static device_attach_t gic_fdt_attach;
65 static ofw_bus_get_devinfo_t gic_ofw_get_devinfo;
67 static bus_get_resource_list_t gic_fdt_get_resource_list;
68 static bool arm_gic_add_children(device_t);
71 static struct ofw_compat_data compat_data[] = {
72 {"arm,gic", true}, /* Non-standard, used in FreeBSD dts. */
73 {"arm,gic-400", true},
74 {"arm,cortex-a15-gic", true},
75 {"arm,cortex-a9-gic", true},
76 {"arm,cortex-a7-gic", true},
77 {"arm,arm11mp-gic", true},
78 {"brcm,brahma-b15-gic", true},
79 {"qcom,msm-qgic2", true},
83 static device_method_t gic_fdt_methods[] = {
84 /* Device interface */
85 DEVMETHOD(device_probe, gic_fdt_probe),
86 DEVMETHOD(device_attach, gic_fdt_attach),
90 DEVMETHOD(bus_get_resource_list,gic_fdt_get_resource_list),
92 /* ofw_bus interface */
93 DEVMETHOD(ofw_bus_get_devinfo, gic_ofw_get_devinfo),
94 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
95 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
96 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
97 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
98 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
104 DEFINE_CLASS_1(gic, gic_fdt_driver, gic_fdt_methods,
105 sizeof(struct arm_gic_softc), arm_gic_driver);
107 static devclass_t gic_fdt_devclass;
109 EARLY_DRIVER_MODULE(gic, simplebus, gic_fdt_driver, gic_fdt_devclass, 0, 0,
110 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
111 EARLY_DRIVER_MODULE(gic, ofwbus, gic_fdt_driver, gic_fdt_devclass, 0, 0,
112 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
115 gic_fdt_probe(device_t dev)
118 if (!ofw_bus_status_okay(dev))
121 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
123 device_set_desc(dev, "ARM Generic Interrupt Controller");
124 return (BUS_PROBE_DEFAULT);
128 gic_fdt_attach(device_t dev)
131 struct arm_gic_softc *sc = device_get_softc(dev);
137 err = arm_gic_attach(dev);
142 xref = OF_xref_from_node(ofw_bus_get_node(dev));
145 * Now, when everything is initialized, it's right time to
146 * register interrupt controller to interrupt framefork.
148 if (intr_pic_register(dev, xref) == NULL) {
149 device_printf(dev, "could not register PIC\n");
154 * Controller is root if:
155 * - doesn't have interrupt parent
156 * - his interrupt parent is this controller
158 pxref = ofw_bus_find_iparent(ofw_bus_get_node(dev));
159 if (pxref == 0 || xref == pxref) {
160 if (intr_pic_claim_root(dev, xref, arm_gic_intr, sc,
161 GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
162 device_printf(dev, "could not set PIC as a root\n");
163 intr_pic_deregister(dev, xref);
167 if (sc->gic_res[2] == NULL) {
169 "not root PIC must have defined interrupt\n");
170 intr_pic_deregister(dev, xref);
173 if (bus_setup_intr(dev, sc->gic_res[2], INTR_TYPE_CLK,
174 arm_gic_intr, NULL, sc, &sc->gic_intrhand)) {
175 device_printf(dev, "could not setup irq handler\n");
176 intr_pic_deregister(dev, xref);
181 OF_device_register_xref(xref, dev);
183 /* If we have children probe and attach them */
184 if (arm_gic_add_children(dev)) {
185 bus_generic_probe(dev);
186 return (bus_generic_attach(dev));
200 static struct resource_list *
201 gic_fdt_get_resource_list(device_t bus, device_t child)
203 struct arm_gic_devinfo *di;
205 di = device_get_ivars(child);
206 KASSERT(di != NULL, ("gic_fdt_get_resource_list: No devinfo"));
212 arm_gic_fill_ranges(phandle_t node, struct arm_gic_softc *sc)
216 ssize_t nbase_ranges;
220 OF_getencprop(OF_parent(node), "#address-cells", &host_cells,
223 OF_getencprop(node, "#address-cells", &sc->addr_cells,
224 sizeof(sc->addr_cells));
226 OF_getencprop(node, "#size-cells", &sc->size_cells,
227 sizeof(sc->size_cells));
229 nbase_ranges = OF_getproplen(node, "ranges");
230 if (nbase_ranges < 0)
232 sc->nranges = nbase_ranges / sizeof(cell_t) /
233 (sc->addr_cells + host_cells + sc->size_cells);
234 if (sc->nranges == 0)
237 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
239 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
240 OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
242 for (i = 0, j = 0; i < sc->nranges; i++) {
243 sc->ranges[i].bus = 0;
244 for (k = 0; k < sc->addr_cells; k++) {
245 sc->ranges[i].bus <<= 32;
246 sc->ranges[i].bus |= base_ranges[j++];
248 sc->ranges[i].host = 0;
249 for (k = 0; k < host_cells; k++) {
250 sc->ranges[i].host <<= 32;
251 sc->ranges[i].host |= base_ranges[j++];
253 sc->ranges[i].size = 0;
254 for (k = 0; k < sc->size_cells; k++) {
255 sc->ranges[i].size <<= 32;
256 sc->ranges[i].size |= base_ranges[j++];
260 free(base_ranges, M_DEVBUF);
261 return (sc->nranges);
265 arm_gic_add_children(device_t dev)
267 struct arm_gic_softc *sc;
268 struct arm_gic_devinfo *dinfo;
269 phandle_t child, node;
272 sc = device_get_softc(dev);
273 node = ofw_bus_get_node(dev);
275 /* If we have no children don't probe for them */
276 child = OF_child(node);
280 if (arm_gic_fill_ranges(node, sc) < 0) {
281 device_printf(dev, "Have a child, but no ranges\n");
285 for (; child != 0; child = OF_peer(child)) {
286 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
288 if (ofw_bus_gen_setup_devinfo(&dinfo->obdinfo, child) != 0) {
289 free(dinfo, M_DEVBUF);
293 resource_list_init(&dinfo->rl);
294 ofw_bus_reg_to_rl(dev, child, sc->addr_cells,
295 sc->size_cells, &dinfo->rl);
297 cdev = device_add_child(dev, NULL, -1);
299 device_printf(dev, "<%s>: device_add_child failed\n",
300 dinfo->obdinfo.obd_name);
301 resource_list_free(&dinfo->rl);
302 ofw_bus_gen_destroy_devinfo(&dinfo->obdinfo);
303 free(dinfo, M_DEVBUF);
306 device_set_ivars(cdev, dinfo);
312 static const struct ofw_bus_devinfo *
313 gic_ofw_get_devinfo(device_t bus __unused, device_t child)
315 struct arm_gic_devinfo *di;
317 di = device_get_ivars(child);
319 return (&di->obdinfo);
322 static struct ofw_compat_data gicv2m_compat_data[] = {
323 {"arm,gic-v2m-frame", true},
328 arm_gicv2m_fdt_probe(device_t dev)
331 if (!ofw_bus_status_okay(dev))
334 if (!ofw_bus_search_compatible(dev, gicv2m_compat_data)->ocd_data)
337 device_set_desc(dev, "ARM Generic Interrupt Controller MSI/MSIX");
338 return (BUS_PROBE_DEFAULT);
342 arm_gicv2m_fdt_attach(device_t dev)
344 struct arm_gicv2m_softc *sc;
346 sc = device_get_softc(dev);
347 sc->sc_xref = OF_xref_from_node(ofw_bus_get_node(dev));
349 return (arm_gicv2m_attach(dev));
352 static device_method_t arm_gicv2m_fdt_methods[] = {
353 /* Device interface */
354 DEVMETHOD(device_probe, arm_gicv2m_fdt_probe),
355 DEVMETHOD(device_attach, arm_gicv2m_fdt_attach),
361 DEFINE_CLASS_1(gicv2m, arm_gicv2m_fdt_driver, arm_gicv2m_fdt_methods,
362 sizeof(struct arm_gicv2m_softc), arm_gicv2m_driver);
364 static devclass_t arm_gicv2m_fdt_devclass;
366 EARLY_DRIVER_MODULE(gicv2m, gic, arm_gicv2m_fdt_driver,
367 arm_gicv2m_fdt_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);