2 * Copyright (c) 2016 Stanislav Galabov
3 * Copyright (c) 2015 Alexander Kabaev
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include "opt_platform.h"
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
44 #include <sys/cpuset.h>
46 #include <sys/mutex.h>
48 #include <sys/sched.h>
49 #include <machine/bus.h>
50 #include <machine/intr.h>
51 #include <machine/smp.h>
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
60 #define MTK_NIRQS 64 /* We'll only use 64 for now */
62 #define MTK_INTPOL 0x0100
63 #define MTK_INTTRIG 0x0180
64 #define MTK_INTDIS 0x0300
65 #define MTK_INTENA 0x0380
66 #define MTK_INTMASK 0x0400
67 #define MTK_INTSTAT 0x0480
68 #define MTK_MAPPIN(_i) (0x0500 + (4 * (_i)))
69 #define MTK_MAPVPE(_i, _v) (0x2000 + (32 * (_i)) + (((_v) / 32) * 4))
71 #define MTK_INTPOL_POS 1
72 #define MTK_INTPOL_NEG 0
73 #define MTK_INTTRIG_EDGE 1
74 #define MTK_INTTRIG_LEVEL 0
75 #define MTK_PIN_BITS(_i) ((1 << 31) | (_i))
76 #define MTK_VPE_BITS(_v) (1 << ((_v) % 32))
78 static int mtk_gic_intr(void *);
80 struct mtk_gic_irqsrc {
81 struct intr_irqsrc isrc;
85 struct mtk_gic_softc {
88 struct resource * gic_res[2];
89 struct mtk_gic_irqsrc gic_irqs[MTK_NIRQS];
94 #define GIC_INTR_ISRC(sc, irq) (&(sc)->gic_irqs[(irq)].isrc)
96 static struct resource_spec mtk_gic_spec[] = {
97 { SYS_RES_MEMORY, 0, RF_ACTIVE }, /* Registers */
101 static struct ofw_compat_data compat_data[] = {
106 #define READ4(_sc, _reg) bus_read_4((_sc)->gic_res[0], (_reg))
107 #define WRITE4(_sc, _reg, _val) bus_write_4((_sc)->gic_res[0], (_reg), (_val))
110 mtk_gic_probe(device_t dev)
113 if (!ofw_bus_status_okay(dev))
116 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
119 device_set_desc(dev, "MTK Interrupt Controller (GIC)");
120 return (BUS_PROBE_DEFAULT);
124 gic_irq_unmask(struct mtk_gic_softc *sc, u_int irq)
127 WRITE4(sc, MTK_INTENA, (1u << (irq)));
131 gic_irq_mask(struct mtk_gic_softc *sc, u_int irq)
134 WRITE4(sc, MTK_INTDIS, (1u << (irq)));
137 static inline intptr_t
138 gic_xref(device_t dev)
141 return (OF_xref_from_node(ofw_bus_get_node(dev)));
145 mtk_gic_register_isrcs(struct mtk_gic_softc *sc)
149 struct intr_irqsrc *isrc;
152 name = device_get_nameunit(sc->gic_dev);
153 for (irq = 0; irq < sc->nirqs; irq++) {
154 sc->gic_irqs[irq].irq = irq;
155 isrc = GIC_INTR_ISRC(sc, irq);
156 error = intr_isrc_register(isrc, sc->gic_dev, 0, "%s", name);
158 /* XXX call intr_isrc_deregister */
159 device_printf(sc->gic_dev, "%s failed", __func__);
168 mtk_gic_attach(device_t dev)
170 struct mtk_gic_softc *sc;
171 intptr_t xref = gic_xref(dev);
174 sc = device_get_softc(dev);
176 if (bus_alloc_resources(dev, mtk_gic_spec, sc->gic_res)) {
177 device_printf(dev, "could not allocate resources\n");
183 /* Initialize mutex */
184 mtx_init(&sc->mutex, "PIC lock", "", MTX_SPIN);
186 /* Set the number of interrupts */
187 sc->nirqs = nitems(sc->gic_irqs);
189 /* Mask all interrupts */
190 WRITE4(sc, MTK_INTDIS, 0xFFFFFFFF);
192 /* All interrupts are of type level */
193 WRITE4(sc, MTK_INTTRIG, 0x00000000);
195 /* All interrupts are of positive polarity */
196 WRITE4(sc, MTK_INTPOL, 0xFFFFFFFF);
199 * Route all interrupts to pin 0 on VPE 0;
201 for (i = 0; i < 32; i++) {
202 WRITE4(sc, MTK_MAPPIN(i), MTK_PIN_BITS(0));
203 WRITE4(sc, MTK_MAPVPE(i, 0), MTK_VPE_BITS(0));
206 /* Register the interrupts */
207 if (mtk_gic_register_isrcs(sc) != 0) {
208 device_printf(dev, "could not register GIC ISRCs\n");
213 * Now, when everything is initialized, it's right time to
214 * register interrupt controller to interrupt framefork.
216 if (intr_pic_register(dev, xref) == NULL) {
217 device_printf(dev, "could not register PIC\n");
221 cpu_establish_hardintr("gic", mtk_gic_intr, NULL, sc, 0, INTR_TYPE_CLK,
227 bus_release_resources(dev, mtk_gic_spec, sc->gic_res);
232 mtk_gic_intr(void *arg)
234 struct mtk_gic_softc *sc = arg;
239 /* Workaround: do not inflate intr nesting level */
240 td->td_intr_nesting_level--;
242 intr = READ4(sc, MTK_INTSTAT) & READ4(sc, MTK_INTMASK);
243 while ((i = fls(intr)) != 0) {
247 if (intr_isrc_dispatch(GIC_INTR_ISRC(sc, i),
248 curthread->td_intr_frame) != 0) {
249 device_printf(sc->gic_dev,
250 "Stray interrupt %u detected\n", i);
256 KASSERT(i == 0, ("all interrupts handled"));
258 td->td_intr_nesting_level++;
260 return (FILTER_HANDLED);
264 mtk_gic_map_intr(device_t dev, struct intr_map_data *data,
265 struct intr_irqsrc **isrcp)
268 struct intr_map_data_fdt *daf;
269 struct mtk_gic_softc *sc;
271 if (data->type != INTR_MAP_DATA_FDT)
274 sc = device_get_softc(dev);
275 daf = (struct intr_map_data_fdt *)data;
277 if (daf->ncells != 3 || daf->cells[1] >= sc->nirqs)
280 *isrcp = GIC_INTR_ISRC(sc, daf->cells[1]);
288 mtk_gic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
292 irq = ((struct mtk_gic_irqsrc *)isrc)->irq;
293 gic_irq_unmask(device_get_softc(dev), irq);
297 mtk_gic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
301 irq = ((struct mtk_gic_irqsrc *)isrc)->irq;
302 gic_irq_mask(device_get_softc(dev), irq);
306 mtk_gic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
309 mtk_gic_disable_intr(dev, isrc);
313 mtk_gic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
316 mtk_gic_enable_intr(dev, isrc);
320 mtk_gic_post_filter(device_t dev, struct intr_irqsrc *isrc)
326 mtk_gic_bind(device_t dev, struct intr_irqsrc *isrc)
332 mtk_gic_init_secondary(device_t dev)
337 mtk_gic_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus)
342 static device_method_t mtk_gic_methods[] = {
343 /* Device interface */
344 DEVMETHOD(device_probe, mtk_gic_probe),
345 DEVMETHOD(device_attach, mtk_gic_attach),
346 /* Interrupt controller interface */
347 DEVMETHOD(pic_disable_intr, mtk_gic_disable_intr),
348 DEVMETHOD(pic_enable_intr, mtk_gic_enable_intr),
349 DEVMETHOD(pic_map_intr, mtk_gic_map_intr),
350 DEVMETHOD(pic_post_filter, mtk_gic_post_filter),
351 DEVMETHOD(pic_post_ithread, mtk_gic_post_ithread),
352 DEVMETHOD(pic_pre_ithread, mtk_gic_pre_ithread),
354 DEVMETHOD(pic_bind, mtk_gic_bind),
355 DEVMETHOD(pic_init_secondary, mtk_gic_init_secondary),
356 DEVMETHOD(pic_ipi_send, mtk_gic_ipi_send),
361 static driver_t mtk_gic_driver = {
364 sizeof(struct mtk_gic_softc),
367 static devclass_t mtk_gic_devclass;
369 EARLY_DRIVER_MODULE(intc_gic, simplebus, mtk_gic_driver, mtk_gic_devclass, 0, 0,
370 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);