2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
7 * Based on OMAP3 INTC code by Ben Gray
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_platform.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <machine/bus.h>
46 #include <machine/intr.h>
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
54 #define INTC_PENDING_BASIC 0x00
55 #define INTC_PENDING_BANK1 0x04
56 #define INTC_PENDING_BANK2 0x08
57 #define INTC_FIQ_CONTROL 0x0C
58 #define INTC_ENABLE_BANK1 0x10
59 #define INTC_ENABLE_BANK2 0x14
60 #define INTC_ENABLE_BASIC 0x18
61 #define INTC_DISABLE_BANK1 0x1C
62 #define INTC_DISABLE_BANK2 0x20
63 #define INTC_DISABLE_BASIC 0x24
65 #define INTC_PENDING_BASIC_ARM 0x0000FF
66 #define INTC_PENDING_BASIC_GPU1_PEND 0x000100
67 #define INTC_PENDING_BASIC_GPU2_PEND 0x000200
68 #define INTC_PENDING_BASIC_GPU1_7 0x000400
69 #define INTC_PENDING_BASIC_GPU1_9 0x000800
70 #define INTC_PENDING_BASIC_GPU1_10 0x001000
71 #define INTC_PENDING_BASIC_GPU1_18 0x002000
72 #define INTC_PENDING_BASIC_GPU1_19 0x004000
73 #define INTC_PENDING_BASIC_GPU2_21 0x008000
74 #define INTC_PENDING_BASIC_GPU2_22 0x010000
75 #define INTC_PENDING_BASIC_GPU2_23 0x020000
76 #define INTC_PENDING_BASIC_GPU2_24 0x040000
77 #define INTC_PENDING_BASIC_GPU2_25 0x080000
78 #define INTC_PENDING_BASIC_GPU2_30 0x100000
79 #define INTC_PENDING_BASIC_MASK 0x1FFFFF
81 #define INTC_PENDING_BASIC_GPU1_MASK (INTC_PENDING_BASIC_GPU1_7 | \
82 INTC_PENDING_BASIC_GPU1_9 | \
83 INTC_PENDING_BASIC_GPU1_10 | \
84 INTC_PENDING_BASIC_GPU1_18 | \
85 INTC_PENDING_BASIC_GPU1_19)
87 #define INTC_PENDING_BASIC_GPU2_MASK (INTC_PENDING_BASIC_GPU2_21 | \
88 INTC_PENDING_BASIC_GPU2_22 | \
89 INTC_PENDING_BASIC_GPU2_23 | \
90 INTC_PENDING_BASIC_GPU2_24 | \
91 INTC_PENDING_BASIC_GPU2_25 | \
92 INTC_PENDING_BASIC_GPU2_30)
94 #define INTC_PENDING_BANK1_MASK (~((1 << 7) | (1 << 9) | (1 << 10) | \
95 (1 << 18) | (1 << 19)))
96 #define INTC_PENDING_BANK2_MASK (~((1 << 21) | (1 << 22) | (1 << 23) | \
97 (1 << 24) | (1 << 25) | (1 << 30)))
100 #define BANK1_END (BANK1_START + 32 - 1)
101 #define BANK2_START (BANK1_START + 32)
102 #define BANK2_END (BANK2_START + 32 - 1)
104 #define IS_IRQ_BASIC(n) (((n) >= 0) && ((n) < BANK1_START))
105 #define IS_IRQ_BANK1(n) (((n) >= BANK1_START) && ((n) <= BANK1_END))
106 #define IS_IRQ_BANK2(n) (((n) >= BANK2_START) && ((n) <= BANK2_END))
107 #define IRQ_BANK1(n) ((n) - BANK1_START)
108 #define IRQ_BANK2(n) ((n) - BANK2_START)
111 #define dprintf(fmt, args...) printf(fmt, ##args)
113 #define dprintf(fmt, args...)
116 #define BCM_INTC_NIRQS 72 /* 8 + 32 + 32 */
118 struct bcm_intc_irqsrc {
119 struct intr_irqsrc bii_isrc;
121 uint16_t bii_disable_reg;
122 uint16_t bii_enable_reg;
126 struct bcm_intc_softc {
128 struct resource * intc_res;
129 bus_space_tag_t intc_bst;
130 bus_space_handle_t intc_bsh;
131 struct resource * intc_irq_res;
133 struct bcm_intc_irqsrc intc_isrcs[BCM_INTC_NIRQS];
136 static struct ofw_compat_data compat_data[] = {
137 {"broadcom,bcm2835-armctrl-ic", 1},
138 {"brcm,bcm2835-armctrl-ic", 1},
139 {"brcm,bcm2836-armctrl-ic", 1},
143 static struct bcm_intc_softc *bcm_intc_sc = NULL;
145 #define intc_read_4(_sc, reg) \
146 bus_space_read_4((_sc)->intc_bst, (_sc)->intc_bsh, (reg))
147 #define intc_write_4(_sc, reg, val) \
148 bus_space_write_4((_sc)->intc_bst, (_sc)->intc_bsh, (reg), (val))
151 bcm_intc_isrc_mask(struct bcm_intc_softc *sc, struct bcm_intc_irqsrc *bii)
154 intc_write_4(sc, bii->bii_disable_reg, bii->bii_mask);
158 bcm_intc_isrc_unmask(struct bcm_intc_softc *sc, struct bcm_intc_irqsrc *bii)
161 intc_write_4(sc, bii->bii_enable_reg, bii->bii_mask);
165 bcm2835_intc_active_intr(struct bcm_intc_softc *sc)
167 uint32_t pending, pending_gpu;
169 pending = intc_read_4(sc, INTC_PENDING_BASIC) & INTC_PENDING_BASIC_MASK;
172 if (pending & INTC_PENDING_BASIC_ARM)
173 return (ffs(pending) - 1);
174 if (pending & INTC_PENDING_BASIC_GPU1_MASK) {
175 if (pending & INTC_PENDING_BASIC_GPU1_7)
176 return (BANK1_START + 7);
177 if (pending & INTC_PENDING_BASIC_GPU1_9)
178 return (BANK1_START + 9);
179 if (pending & INTC_PENDING_BASIC_GPU1_10)
180 return (BANK1_START + 10);
181 if (pending & INTC_PENDING_BASIC_GPU1_18)
182 return (BANK1_START + 18);
183 if (pending & INTC_PENDING_BASIC_GPU1_19)
184 return (BANK1_START + 19);
186 if (pending & INTC_PENDING_BASIC_GPU2_MASK) {
187 if (pending & INTC_PENDING_BASIC_GPU2_21)
188 return (BANK2_START + 21);
189 if (pending & INTC_PENDING_BASIC_GPU2_22)
190 return (BANK2_START + 22);
191 if (pending & INTC_PENDING_BASIC_GPU2_23)
192 return (BANK2_START + 23);
193 if (pending & INTC_PENDING_BASIC_GPU2_24)
194 return (BANK2_START + 24);
195 if (pending & INTC_PENDING_BASIC_GPU2_25)
196 return (BANK2_START + 25);
197 if (pending & INTC_PENDING_BASIC_GPU2_30)
198 return (BANK2_START + 30);
200 if (pending & INTC_PENDING_BASIC_GPU1_PEND) {
201 pending_gpu = intc_read_4(sc, INTC_PENDING_BANK1);
202 pending_gpu &= INTC_PENDING_BANK1_MASK;
203 if (pending_gpu != 0)
204 return (BANK1_START + ffs(pending_gpu) - 1);
206 if (pending & INTC_PENDING_BASIC_GPU2_PEND) {
207 pending_gpu = intc_read_4(sc, INTC_PENDING_BANK2);
208 pending_gpu &= INTC_PENDING_BANK2_MASK;
209 if (pending_gpu != 0)
210 return (BANK2_START + ffs(pending_gpu) - 1);
212 return (-1); /* It shouldn't end here, but it's hardware. */
216 bcm2835_intc_intr(void *arg)
219 struct bcm_intc_softc *sc = arg;
221 for (num = 0; ; num++) {
222 irq = bcm2835_intc_active_intr(sc);
225 if (intr_isrc_dispatch(&sc->intc_isrcs[irq].bii_isrc,
226 curthread->td_intr_frame) != 0) {
227 bcm_intc_isrc_mask(sc, &sc->intc_isrcs[irq]);
228 device_printf(sc->sc_dev, "Stray irq %u disabled\n",
231 arm_irq_memory_barrier(0); /* XXX */
234 device_printf(sc->sc_dev, "Spurious interrupt detected\n");
236 return (FILTER_HANDLED);
240 bcm_intc_enable_intr(device_t dev, struct intr_irqsrc *isrc)
242 struct bcm_intc_irqsrc *bii = (struct bcm_intc_irqsrc *)isrc;
244 arm_irq_memory_barrier(bii->bii_irq);
245 bcm_intc_isrc_unmask(device_get_softc(dev), bii);
249 bcm_intc_disable_intr(device_t dev, struct intr_irqsrc *isrc)
252 bcm_intc_isrc_mask(device_get_softc(dev),
253 (struct bcm_intc_irqsrc *)isrc);
257 bcm_intc_map_intr(device_t dev, struct intr_map_data *data,
258 struct intr_irqsrc **isrcp)
261 struct intr_map_data_fdt *daf;
262 struct bcm_intc_softc *sc;
265 if (data->type != INTR_MAP_DATA_FDT)
268 daf = (struct intr_map_data_fdt *)data;
269 if (daf->ncells == 1)
271 else if (daf->ncells == 2) {
273 switch (daf->cells[0]) {
276 if (irq >= BANK1_START)
280 irq = daf->cells[1] + BANK1_START;
285 irq = daf->cells[1] + BANK2_START;
296 "invalid IRQ config: bank=%d, irq=%d\n",
297 daf->cells[0], daf->cells[1]);
304 if (irq >= BCM_INTC_NIRQS)
307 sc = device_get_softc(dev);
308 *isrcp = &sc->intc_isrcs[irq].bii_isrc;
313 bcm_intc_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
316 bcm_intc_disable_intr(dev, isrc);
320 bcm_intc_post_ithread(device_t dev, struct intr_irqsrc *isrc)
323 bcm_intc_enable_intr(dev, isrc);
327 bcm_intc_post_filter(device_t dev, struct intr_irqsrc *isrc)
332 bcm_intc_pic_register(struct bcm_intc_softc *sc, intptr_t xref)
334 struct bcm_intc_irqsrc *bii;
339 name = device_get_nameunit(sc->sc_dev);
340 for (irq = 0; irq < BCM_INTC_NIRQS; irq++) {
341 bii = &sc->intc_isrcs[irq];
343 if (IS_IRQ_BASIC(irq)) {
344 bii->bii_disable_reg = INTC_DISABLE_BASIC;
345 bii->bii_enable_reg = INTC_ENABLE_BASIC;
346 bii->bii_mask = 1 << irq;
347 } else if (IS_IRQ_BANK1(irq)) {
348 bii->bii_disable_reg = INTC_DISABLE_BANK1;
349 bii->bii_enable_reg = INTC_ENABLE_BANK1;
350 bii->bii_mask = 1 << IRQ_BANK1(irq);
351 } else if (IS_IRQ_BANK2(irq)) {
352 bii->bii_disable_reg = INTC_DISABLE_BANK2;
353 bii->bii_enable_reg = INTC_ENABLE_BANK2;
354 bii->bii_mask = 1 << IRQ_BANK2(irq);
358 error = intr_isrc_register(&bii->bii_isrc, sc->sc_dev, 0,
363 if (intr_pic_register(sc->sc_dev, xref) == NULL)
370 bcm_intc_probe(device_t dev)
373 if (!ofw_bus_status_okay(dev))
376 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
379 device_set_desc(dev, "BCM2835 Interrupt Controller");
380 return (BUS_PROBE_DEFAULT);
384 bcm_intc_attach(device_t dev)
386 struct bcm_intc_softc *sc = device_get_softc(dev);
394 sc->intc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
395 if (sc->intc_res == NULL) {
396 device_printf(dev, "could not allocate memory resource\n");
400 xref = OF_xref_from_node(ofw_bus_get_node(dev));
401 if (bcm_intc_pic_register(sc, xref) != 0) {
402 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->intc_res);
403 device_printf(dev, "could not register PIC\n");
408 sc->intc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
410 if (sc->intc_irq_res == NULL) {
411 if (intr_pic_claim_root(dev, xref, bcm2835_intc_intr, sc, 0) != 0) {
413 device_printf(dev, "could not set PIC as a root\n");
417 if (bus_setup_intr(dev, sc->intc_irq_res, INTR_TYPE_CLK,
418 bcm2835_intc_intr, NULL, sc, &sc->intc_irq_hdl)) {
420 device_printf(dev, "could not setup irq handler\n");
424 sc->intc_bst = rman_get_bustag(sc->intc_res);
425 sc->intc_bsh = rman_get_bushandle(sc->intc_res);
432 static device_method_t bcm_intc_methods[] = {
433 DEVMETHOD(device_probe, bcm_intc_probe),
434 DEVMETHOD(device_attach, bcm_intc_attach),
436 DEVMETHOD(pic_disable_intr, bcm_intc_disable_intr),
437 DEVMETHOD(pic_enable_intr, bcm_intc_enable_intr),
438 DEVMETHOD(pic_map_intr, bcm_intc_map_intr),
439 DEVMETHOD(pic_post_filter, bcm_intc_post_filter),
440 DEVMETHOD(pic_post_ithread, bcm_intc_post_ithread),
441 DEVMETHOD(pic_pre_ithread, bcm_intc_pre_ithread),
446 static driver_t bcm_intc_driver = {
449 sizeof(struct bcm_intc_softc),
452 static devclass_t bcm_intc_devclass;
454 EARLY_DRIVER_MODULE(intc, simplebus, bcm_intc_driver, bcm_intc_devclass,
455 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);