2 * Copyright (c) 2015 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>
62 #define MTK_IRQ0STAT 0x009c
63 #define MTK_IRQ1STAT 0x00a0
64 #define MTK_INTTYPE 0x0000
65 #define MTK_INTRAW 0x00a4
66 #define MTK_INTENA 0x0080
67 #define MTK_INTDIS 0x0078
69 static int mtk_pic_intr(void *);
71 struct mtk_pic_irqsrc {
72 struct intr_irqsrc isrc;
76 struct mtk_pic_softc {
79 struct resource * pic_res[2];
80 struct mtk_pic_irqsrc pic_irqs[MTK_NIRQS];
85 #define PIC_INTR_ISRC(sc, irq) (&(sc)->pic_irqs[(irq)].isrc)
87 static struct resource_spec mtk_pic_spec[] = {
88 { SYS_RES_MEMORY, 0, RF_ACTIVE }, /* Registers */
89 { SYS_RES_IRQ, 0, RF_ACTIVE }, /* Parent interrupt 1 */
90 // { SYS_RES_IRQ, 1, RF_ACTIVE }, /* Parent interrupt 2 */
94 static struct ofw_compat_data compat_data[] = {
95 { "ralink,mt7628an-intc", 1 },
99 #define READ4(_sc, _reg) bus_read_4((_sc)->pic_res[0], _reg)
100 #define WRITE4(_sc, _reg, _val) bus_write_4((_sc)->pic_res[0], _reg, _val)
103 mtk_pic_probe(device_t dev)
106 if (!ofw_bus_status_okay(dev))
109 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
112 device_set_desc(dev, "MTK Interrupt Controller (v2)");
113 return (BUS_PROBE_DEFAULT);
117 pic_irq_unmask(struct mtk_pic_softc *sc, u_int irq)
120 WRITE4(sc, MTK_INTENA, (1u << (irq)));
124 pic_irq_mask(struct mtk_pic_softc *sc, u_int irq)
127 WRITE4(sc, MTK_INTDIS, (1u << (irq)));
130 static inline intptr_t
131 pic_xref(device_t dev)
133 return (OF_xref_from_node(ofw_bus_get_node(dev)));
137 mtk_pic_register_isrcs(struct mtk_pic_softc *sc)
141 struct intr_irqsrc *isrc;
144 name = device_get_nameunit(sc->pic_dev);
145 for (irq = 0; irq < sc->nirqs; irq++) {
146 sc->pic_irqs[irq].irq = irq;
147 isrc = PIC_INTR_ISRC(sc, irq);
148 error = intr_isrc_register(isrc, sc->pic_dev, 0, "%s", name);
150 /* XXX call intr_isrc_deregister */
151 device_printf(sc->pic_dev, "%s failed", __func__);
160 mtk_pic_attach(device_t dev)
162 struct mtk_pic_softc *sc;
163 intptr_t xref = pic_xref(dev);
165 sc = device_get_softc(dev);
167 if (bus_alloc_resources(dev, mtk_pic_spec, sc->pic_res)) {
168 device_printf(dev, "could not allocate resources\n");
174 /* Initialize mutex */
175 mtx_init(&sc->mutex, "PIC lock", "", MTX_SPIN);
177 /* Set the number of interrupts */
178 sc->nirqs = nitems(sc->pic_irqs);
180 /* Mask all interrupts */
181 WRITE4(sc, MTK_INTDIS, 0xFFFFFFFF);
183 /* But enable interrupt generation/masking */
184 WRITE4(sc, MTK_INTENA, 0x00000000);
186 /* Set all interrupts to type 0 */
187 WRITE4(sc, MTK_INTTYPE, 0xFFFFFFFF);
189 /* Register the interrupts */
190 if (mtk_pic_register_isrcs(sc) != 0) {
191 device_printf(dev, "could not register PIC ISRCs\n");
196 * Now, when everything is initialized, it's right time to
197 * register interrupt controller to interrupt framefork.
199 if (intr_pic_register(dev, xref) != 0) {
200 device_printf(dev, "could not register PIC\n");
204 if (bus_setup_intr(dev, sc->pic_res[1], INTR_TYPE_CLK,
205 mtk_pic_intr, NULL, sc, &sc->pic_intrhand)) {
206 device_printf(dev, "could not setup irq handler\n");
207 intr_pic_deregister(dev, xref);
213 bus_release_resources(dev, mtk_pic_spec, sc->pic_res);
218 mtk_pic_intr(void *arg)
220 struct mtk_pic_softc *sc = arg;
225 /* Workaround: do not inflate intr nesting level */
226 td->td_intr_nesting_level--;
229 intr = READ4(sc, MTK_IRQ1STAT);
230 while ((i = fls(intr)) != 0) {
234 if (intr_isrc_dispatch(PIC_INTR_ISRC(sc, i),
235 curthread->td_intr_frame) != 0) {
236 device_printf(sc->pic_dev,
237 "Stray interrupt %u detected\n", i);
243 KASSERT(i == 0, ("all interrupts handled"));
246 intr = READ4(sc, MTK_IRQ0STAT);
248 while ((i = fls(intr)) != 0) {
252 if (intr_isrc_dispatch(PIC_INTR_ISRC(sc, i),
253 curthread->td_intr_frame) != 0) {
254 device_printf(sc->pic_dev,
255 "Stray interrupt %u detected\n", i);
261 KASSERT(i == 0, ("all interrupts handled"));
263 td->td_intr_nesting_level++;
265 return (FILTER_HANDLED);
269 mtk_pic_map_intr(device_t dev, struct intr_map_data *data,
270 struct intr_irqsrc **isrcp)
273 struct mtk_pic_softc *sc;
275 sc = device_get_softc(dev);
277 if (data == NULL || data->type != INTR_MAP_DATA_FDT ||
278 data->fdt.ncells != 1 || data->fdt.cells[0] >= sc->nirqs)
281 *isrcp = PIC_INTR_ISRC(sc, data->fdt.cells[0]);
289 mtk_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
293 irq = ((struct mtk_pic_irqsrc *)isrc)->irq;
294 pic_irq_unmask(device_get_softc(dev), irq);
298 mtk_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
302 irq = ((struct mtk_pic_irqsrc *)isrc)->irq;
303 pic_irq_mask(device_get_softc(dev), irq);
307 mtk_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
310 mtk_pic_disable_intr(dev, isrc);
314 mtk_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
317 mtk_pic_enable_intr(dev, isrc);
321 mtk_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
325 static device_method_t mtk_pic_methods[] = {
326 /* Device interface */
327 DEVMETHOD(device_probe, mtk_pic_probe),
328 DEVMETHOD(device_attach, mtk_pic_attach),
329 /* Interrupt controller interface */
330 DEVMETHOD(pic_disable_intr, mtk_pic_disable_intr),
331 DEVMETHOD(pic_enable_intr, mtk_pic_enable_intr),
332 DEVMETHOD(pic_map_intr, mtk_pic_map_intr),
333 DEVMETHOD(pic_post_filter, mtk_pic_post_filter),
334 DEVMETHOD(pic_post_ithread, mtk_pic_post_ithread),
335 DEVMETHOD(pic_pre_ithread, mtk_pic_pre_ithread),
339 static driver_t mtk_pic_driver = {
342 sizeof(struct mtk_pic_softc),
345 static devclass_t mtk_pic_devclass;
347 EARLY_DRIVER_MODULE(intc_v2, simplebus, mtk_pic_driver, mtk_pic_devclass, 0, 0,
348 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);