2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
5 * Copyright (c) 2013 SRI International
8 * This software was developed by SRI International and the University of
9 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
10 * ("CTSRD"), as part of the DARPA CRASH research programme.
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.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_platform.h"
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
46 #include <sys/systm.h>
49 #include <machine/bus.h>
50 #include <machine/intr.h>
53 #include <mips/beri/beri_mp.h>
56 #include <dev/fdt/fdt_common.h>
57 #include <dev/ofw/openfirm.h>
58 #include <dev/ofw/ofw_bus.h>
59 #include <dev/ofw/ofw_bus_subr.h>
63 #define BP_NUM_HARD_IRQS 5
64 #define BP_NUM_IRQS 32
65 /* We use hard irqs 15-31 as soft */
66 #define BP_FIRST_SOFT 16
68 #define BP_CFG_IRQ_S 0
69 #define BP_CFG_IRQ_M (0xf << BP_CFG_IRQ_S)
70 #define BP_CFG_TID_S 8
71 #define BP_CFG_TID_M (0x7FFFFF << BP_CFG_TID_S)
72 #define BP_CFG_ENABLE (1 << 31)
83 struct beri_pic_isrc {
84 struct intr_irqsrc isrc;
86 uint32_t mips_hard_irq;
91 struct beripic_softc *sc;
94 struct beripic_softc {
97 struct beri_pic_isrc irqs[BP_NUM_IRQS];
98 struct resource *res[4 + BP_NUM_HARD_IRQS];
99 void *ih[BP_NUM_HARD_IRQS];
100 struct hirq hirq[BP_NUM_HARD_IRQS];
101 uint8_t mips_hard_irq_idx;
104 static struct resource_spec beri_pic_spec[] = {
105 { SYS_RES_MEMORY, 0, RF_ACTIVE },
106 { SYS_RES_MEMORY, 1, RF_ACTIVE },
107 { SYS_RES_MEMORY, 2, RF_ACTIVE },
108 { SYS_RES_MEMORY, 3, RF_ACTIVE },
109 { SYS_RES_IRQ, 0, RF_ACTIVE },
110 { SYS_RES_IRQ, 1, RF_ACTIVE },
111 { SYS_RES_IRQ, 2, RF_ACTIVE },
112 { SYS_RES_IRQ, 3, RF_ACTIVE },
113 { SYS_RES_IRQ, 4, RF_ACTIVE },
118 beri_pic_intr(void *arg)
120 struct beripic_softc *sc;
121 struct intr_irqsrc *isrc;
130 intr = bus_read_8(sc->res[BP_IP_READ], 0);
131 while ((i = fls(intr)) != 0) {
135 isrc = &sc->irqs[i].isrc;
137 reg = bus_read_8(sc->res[BP_CFG], i * 8);
138 if ((reg & BP_CFG_IRQ_M) != h->irq) {
141 if ((reg & (BP_CFG_ENABLE)) == 0) {
145 if (intr_isrc_dispatch(isrc, curthread->td_intr_frame) != 0) {
146 device_printf(sc->dev, "Stray interrupt %u detected\n", i);
149 bus_write_8(sc->res[BP_IP_CLEAR], 0, (1 << i));
152 return (FILTER_HANDLED);
156 beripic_probe(device_t dev)
159 if (!ofw_bus_status_okay(dev))
162 if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-pic"))
165 device_set_desc(dev, "BERI Programmable Interrupt Controller");
167 return (BUS_PROBE_DEFAULT);
171 beripic_attach(device_t dev)
173 struct beripic_softc *sc;
174 struct beri_pic_isrc *pic_isrc;
176 struct intr_irqsrc *isrc;
182 sc = device_get_softc(dev);
185 if (bus_alloc_resources(dev, beri_pic_spec, sc->res)) {
186 device_printf(dev, "could not allocate resources\n");
190 xref = OF_xref_from_node(ofw_bus_get_node(dev));
191 name = device_get_nameunit(dev);
192 unit = device_get_unit(dev);
193 sc->nirqs = BP_NUM_IRQS;
195 for (i = 0; i < sc->nirqs; i++) {
197 isrc = &sc->irqs[i].isrc;
199 /* Assign mips hard irq number. */
200 pic_isrc = (struct beri_pic_isrc *)isrc;
201 pic_isrc->mips_hard_irq = sc->mips_hard_irq_idx++;
202 /* Last IRQ is used for IPIs. */
203 if (sc->mips_hard_irq_idx >= (BP_NUM_HARD_IRQS - 1)) {
204 sc->mips_hard_irq_idx = 0;
207 err = intr_isrc_register(isrc, sc->dev,
208 0, "pic%d,%d", unit, i);
209 bus_write_8(sc->res[BP_CFG], i * 8, 0);
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 /* Last IRQ is used for IPIs. */
222 for (i = 0; i < (BP_NUM_HARD_IRQS - 1); i++) {
225 if (bus_setup_intr(dev, sc->res[4+i], INTR_TYPE_CLK,
226 beri_pic_intr, NULL, &sc->hirq[i], sc->ih[i])) {
227 device_printf(dev, "could not setup irq handler\n");
228 intr_pic_deregister(dev, xref);
237 beri_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
239 struct beri_pic_isrc *pic_isrc;
240 struct beripic_softc *sc;
243 sc = device_get_softc(dev);
244 pic_isrc = (struct beri_pic_isrc *)isrc;
247 reg |= (pic_isrc->mips_hard_irq << BP_CFG_IRQ_S);
248 bus_write_8(sc->res[BP_CFG], pic_isrc->irq * 8, reg);
252 beri_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
254 struct beri_pic_isrc *pic_isrc;
255 struct beripic_softc *sc;
258 sc = device_get_softc(dev);
259 pic_isrc = (struct beri_pic_isrc *)isrc;
261 reg = bus_read_8(sc->res[BP_CFG], pic_isrc->irq * 8);
262 reg &= ~BP_CFG_ENABLE;
263 bus_write_8(sc->res[BP_CFG], pic_isrc->irq * 8, reg);
267 beri_pic_map_intr(device_t dev, struct intr_map_data *data,
268 struct intr_irqsrc **isrcp)
270 struct beripic_softc *sc;
271 struct intr_map_data_fdt *daf;
274 sc = device_get_softc(dev);
275 daf = (struct intr_map_data_fdt *)data;
277 if (data == NULL || data->type != INTR_MAP_DATA_FDT ||
278 daf->ncells != 1 || daf->cells[0] >= sc->nirqs)
283 *isrcp = &sc->irqs[irq].isrc;
289 beri_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
292 beri_pic_enable_intr(dev, isrc);
296 beri_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
299 beri_pic_disable_intr(dev, isrc);
304 beripic_setup_ipi(device_t dev, u_int tid, u_int ipi_irq)
306 struct beripic_softc *sc;
309 sc = device_get_softc(dev);
311 reg = (BP_CFG_ENABLE);
312 reg |= (ipi_irq << BP_CFG_IRQ_S);
313 reg |= (tid << BP_CFG_TID_S);
314 bus_write_8(sc->res[BP_CFG], ((BP_FIRST_SOFT + tid) * 8), reg);
318 beripic_send_ipi(device_t dev, u_int tid)
320 struct beripic_softc *sc;
323 sc = device_get_softc(dev);
325 bit = (BP_FIRST_SOFT + tid);
326 KASSERT(bit < BP_NUM_IRQS, ("tid (%d) to large\n", tid));
328 bus_write_8(sc->res[BP_IP_SET], 0x0, (1 << bit));
332 beripic_clear_ipi(device_t dev, u_int tid)
334 struct beripic_softc *sc;
337 sc = device_get_softc(dev);
339 bit = (BP_FIRST_SOFT + tid);
340 KASSERT(bit < BP_NUM_IRQS, ("tid (%d) to large\n", tid));
342 bus_write_8(sc->res[BP_IP_CLEAR], 0x0, (1 << bit));
346 static device_method_t beripic_fdt_methods[] = {
347 /* Device interface */
348 DEVMETHOD(device_probe, beripic_probe),
349 DEVMETHOD(device_attach, beripic_attach),
351 /* Interrupt controller interface */
352 DEVMETHOD(pic_enable_intr, beri_pic_enable_intr),
353 DEVMETHOD(pic_disable_intr, beri_pic_disable_intr),
354 DEVMETHOD(pic_map_intr, beri_pic_map_intr),
355 DEVMETHOD(pic_post_ithread, beri_pic_post_ithread),
356 DEVMETHOD(pic_pre_ithread, beri_pic_pre_ithread),
361 devclass_t beripic_devclass;
363 static driver_t beripic_driver = {
366 sizeof(struct beripic_softc)
369 EARLY_DRIVER_MODULE(beripic, ofwbus, beripic_driver, beripic_devclass, 0, 0,
370 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);