2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3 * Copyright (c) 2016 Emmanuel Vadot <manu@freebsd.org>
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 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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
19 * FOR 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_platform.h"
33 #include <sys/types.h>
35 #include <sys/cpuset.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sched.h>
47 #include <machine/bus.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>
57 * Interrupt controller registers
60 #define SW_INT_VECTOR_REG 0x00
61 #define SW_INT_BASE_ADR_REG 0x04
62 #define SW_INT_PROTECTION_REG 0x08
63 #define SW_INT_NMI_CTRL_REG 0x0c
65 #define SW_INT_IRQ_PENDING_REG0 0x10
66 #define SW_INT_IRQ_PENDING_REG1 0x14
67 #define SW_INT_IRQ_PENDING_REG2 0x18
69 #define SW_INT_FIQ_PENDING_REG0 0x20
70 #define SW_INT_FIQ_PENDING_REG1 0x24
71 #define SW_INT_FIQ_PENDING_REG2 0x28
73 #define SW_INT_SELECT_REG0 0x30
74 #define SW_INT_SELECT_REG1 0x34
75 #define SW_INT_SELECT_REG2 0x38
77 #define SW_INT_ENABLE_REG0 0x40
78 #define SW_INT_ENABLE_REG1 0x44
79 #define SW_INT_ENABLE_REG2 0x48
81 #define SW_INT_MASK_REG0 0x50
82 #define SW_INT_MASK_REG1 0x54
83 #define SW_INT_MASK_REG2 0x58
85 #define SW_INT_IRQNO_ENMI 0
87 #define A10_INTR_MAX_NIRQS 81
89 #define SW_INT_IRQ_PENDING_REG(_b) (0x10 + ((_b) * 4))
90 #define SW_INT_FIQ_PENDING_REG(_b) (0x20 + ((_b) * 4))
91 #define SW_INT_SELECT_REG(_b) (0x30 + ((_b) * 4))
92 #define SW_INT_ENABLE_REG(_b) (0x40 + ((_b) * 4))
93 #define SW_INT_MASK_REG(_b) (0x50 + ((_b) * 4))
95 struct a10_intr_irqsrc {
96 struct intr_irqsrc isrc;
100 struct a10_aintc_softc {
102 struct resource * aintc_res;
103 bus_space_tag_t aintc_bst;
104 bus_space_handle_t aintc_bsh;
106 struct a10_intr_irqsrc isrcs[A10_INTR_MAX_NIRQS];
109 #define aintc_read_4(sc, reg) \
110 bus_space_read_4(sc->aintc_bst, sc->aintc_bsh, reg)
111 #define aintc_write_4(sc, reg, val) \
112 bus_space_write_4(sc->aintc_bst, sc->aintc_bsh, reg, val)
115 a10_intr_eoi(struct a10_aintc_softc *sc, u_int irq)
118 if (irq != SW_INT_IRQNO_ENMI)
120 mtx_lock_spin(&sc->mtx);
121 aintc_write_4(sc, SW_INT_IRQ_PENDING_REG(0),
122 (1 << SW_INT_IRQNO_ENMI));
123 mtx_unlock_spin(&sc->mtx);
127 a10_intr_unmask(struct a10_aintc_softc *sc, u_int irq)
129 uint32_t bit, block, value;
134 mtx_lock_spin(&sc->mtx);
135 value = aintc_read_4(sc, SW_INT_ENABLE_REG(block));
137 aintc_write_4(sc, SW_INT_ENABLE_REG(block), value);
139 value = aintc_read_4(sc, SW_INT_MASK_REG(block));
140 value &= ~(1 << bit);
141 aintc_write_4(sc, SW_INT_MASK_REG(block), value);
142 mtx_unlock_spin(&sc->mtx);
146 a10_intr_mask(struct a10_aintc_softc *sc, u_int irq)
148 uint32_t bit, block, value;
153 mtx_lock_spin(&sc->mtx);
154 value = aintc_read_4(sc, SW_INT_ENABLE_REG(block));
155 value &= ~(1 << bit);
156 aintc_write_4(sc, SW_INT_ENABLE_REG(block), value);
158 value = aintc_read_4(sc, SW_INT_MASK_REG(block));
160 aintc_write_4(sc, SW_INT_MASK_REG(block), value);
161 mtx_unlock_spin(&sc->mtx);
165 a10_pending_irq(struct a10_aintc_softc *sc)
170 for (i = 0; i < 3; i++) {
171 value = aintc_read_4(sc, SW_INT_IRQ_PENDING_REG(i));
174 for (b = 0; b < 32; b++)
175 if (value & (1 << b)) {
186 struct a10_aintc_softc *sc = arg;
189 irq = a10_pending_irq(sc);
190 if (irq == -1 || irq > A10_INTR_MAX_NIRQS) {
191 device_printf(sc->sc_dev, "Spurious interrupt %d\n", irq);
192 return (FILTER_HANDLED);
196 if (irq > A10_INTR_MAX_NIRQS) {
197 device_printf(sc->sc_dev, "Spurious interrupt %d\n",
199 return (FILTER_HANDLED);
201 if (intr_isrc_dispatch(&sc->isrcs[irq].isrc,
202 curthread->td_intr_frame) != 0) {
203 a10_intr_mask(sc, irq);
204 a10_intr_eoi(sc, irq);
205 device_printf(sc->sc_dev,
206 "Stray interrupt %d disabled\n", irq);
209 arm_irq_memory_barrier(irq);
210 irq = a10_pending_irq(sc);
213 return (FILTER_HANDLED);
217 a10_intr_pic_attach(struct a10_aintc_softc *sc)
219 struct intr_pic *pic;
225 name = device_get_nameunit(sc->sc_dev);
226 for (irq = 0; irq < A10_INTR_MAX_NIRQS; irq++) {
227 sc->isrcs[irq].irq = irq;
229 error = intr_isrc_register(&sc->isrcs[irq].isrc,
230 sc->sc_dev, 0, "%s,%u", name, irq);
235 xref = OF_xref_from_node(ofw_bus_get_node(sc->sc_dev));
236 pic = intr_pic_register(sc->sc_dev, xref);
240 return (intr_pic_claim_root(sc->sc_dev, xref, a10_intr, sc, 0));
244 a10_intr_enable_intr(device_t dev, struct intr_irqsrc *isrc)
246 struct a10_aintc_softc *sc;
247 u_int irq = ((struct a10_intr_irqsrc *)isrc)->irq;
249 sc = device_get_softc(dev);
250 arm_irq_memory_barrier(irq);
251 a10_intr_unmask(sc, irq);
255 a10_intr_disable_intr(device_t dev, struct intr_irqsrc *isrc)
257 struct a10_aintc_softc *sc;
258 u_int irq = ((struct a10_intr_irqsrc *)isrc)->irq;
260 sc = device_get_softc(dev);
261 a10_intr_mask(sc, irq);
265 a10_intr_map_intr(device_t dev, struct intr_map_data *data,
266 struct intr_irqsrc **isrcp)
268 struct intr_map_data_fdt *daf;
269 struct a10_aintc_softc *sc;
271 if (data->type != INTR_MAP_DATA_FDT)
274 daf = (struct intr_map_data_fdt *)data;
275 if (daf->ncells != 1 || daf->cells[0] >= A10_INTR_MAX_NIRQS)
278 sc = device_get_softc(dev);
279 *isrcp = &sc->isrcs[daf->cells[0]].isrc;
284 a10_intr_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
286 struct a10_aintc_softc *sc = device_get_softc(dev);
287 u_int irq = ((struct a10_intr_irqsrc *)isrc)->irq;
289 a10_intr_mask(sc, irq);
290 a10_intr_eoi(sc, irq);
294 a10_intr_post_ithread(device_t dev, struct intr_irqsrc *isrc)
297 a10_intr_enable_intr(dev, isrc);
301 a10_intr_post_filter(device_t dev, struct intr_irqsrc *isrc)
303 struct a10_aintc_softc *sc = device_get_softc(dev);
304 u_int irq = ((struct a10_intr_irqsrc *)isrc)->irq;
306 a10_intr_eoi(sc, irq);
310 a10_aintc_probe(device_t dev)
313 if (!ofw_bus_status_okay(dev))
316 if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-ic"))
318 device_set_desc(dev, "A10 AINTC Interrupt Controller");
319 return (BUS_PROBE_DEFAULT);
323 a10_aintc_attach(device_t dev)
325 struct a10_aintc_softc *sc = device_get_softc(dev);
330 sc->aintc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
332 if (!sc->aintc_res) {
333 device_printf(dev, "could not allocate resource\n");
337 sc->aintc_bst = rman_get_bustag(sc->aintc_res);
338 sc->aintc_bsh = rman_get_bushandle(sc->aintc_res);
340 mtx_init(&sc->mtx, "A10 AINTC lock", "", MTX_SPIN);
342 /* Disable & clear all interrupts */
343 for (i = 0; i < 3; i++) {
344 aintc_write_4(sc, SW_INT_ENABLE_REG(i), 0);
345 aintc_write_4(sc, SW_INT_MASK_REG(i), 0xffffffff);
347 /* enable protection mode*/
348 aintc_write_4(sc, SW_INT_PROTECTION_REG, 0x01);
350 /* config the external interrupt source type*/
351 aintc_write_4(sc, SW_INT_NMI_CTRL_REG, 0x00);
353 if (a10_intr_pic_attach(sc) != 0) {
354 device_printf(dev, "could not attach PIC\n");
361 bus_release_resource(dev, SYS_RES_MEMORY, rid,
366 static device_method_t a10_aintc_methods[] = {
367 DEVMETHOD(device_probe, a10_aintc_probe),
368 DEVMETHOD(device_attach, a10_aintc_attach),
370 /* Interrupt controller interface */
371 DEVMETHOD(pic_disable_intr, a10_intr_disable_intr),
372 DEVMETHOD(pic_enable_intr, a10_intr_enable_intr),
373 DEVMETHOD(pic_map_intr, a10_intr_map_intr),
374 DEVMETHOD(pic_post_filter, a10_intr_post_filter),
375 DEVMETHOD(pic_post_ithread, a10_intr_post_ithread),
376 DEVMETHOD(pic_pre_ithread, a10_intr_pre_ithread),
381 static driver_t a10_aintc_driver = {
384 sizeof(struct a10_aintc_softc),
387 static devclass_t a10_aintc_devclass;
389 EARLY_DRIVER_MODULE(aintc, simplebus, a10_aintc_driver, a10_aintc_devclass, 0, 0,
390 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_FIRST);