2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012, 2013 The FreeBSD Foundation
7 * This software was developed by Oleksandr Rybalko under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
43 #include <sys/mutex.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>
52 #include <arm/freescale/imx/imx51_tzicreg.h>
56 #define TZIC_NIRQS 128
59 struct intr_irqsrc isrc;
65 struct resource *tzicregs;
66 struct tzic_irqsrc isrcs[TZIC_NIRQS];
69 static struct tzic_softc *tzic_sc;
71 static inline uint32_t
72 tzic_read_4(struct tzic_softc *sc, int reg)
75 return (bus_read_4(sc->tzicregs, reg));
79 tzic_write_4(struct tzic_softc *sc, int reg, uint32_t val)
82 bus_write_4(sc->tzicregs, reg, val);
86 tzic_irq_eoi(struct tzic_softc *sc)
89 tzic_write_4(sc, TZIC_PRIOMASK, 0xff);
93 tzic_irq_mask(struct tzic_softc *sc, u_int irq)
96 tzic_write_4(sc, TZIC_ENCLEAR(irq >> 5), (1u << (irq & 0x1f)));
100 tzic_irq_unmask(struct tzic_softc *sc, u_int irq)
103 tzic_write_4(sc, TZIC_ENSET(irq >> 5), (1u << (irq & 0x1f)));
109 struct tzic_softc *sc = arg;
113 /* Get active interrupt */
114 for (i = 0; i < TZIC_NIRQS / 32; ++i) {
115 pending = tzic_read_4(sc, TZIC_PND(i));
116 if ((b = 31 - __builtin_clz(pending)) < 0)
119 tzic_write_4(sc, TZIC_PRIOMASK, 0);
120 if (intr_isrc_dispatch(&sc->isrcs[irq].isrc,
121 curthread->td_intr_frame) != 0) {
122 tzic_irq_mask(sc, irq);
124 arm_irq_memory_barrier(irq);
126 device_printf(sc->dev,
127 "Stray irq %u disabled\n", irq);
130 return (FILTER_HANDLED);
134 device_printf(sc->dev, "Spurious interrupt detected\n");
136 return (FILTER_HANDLED);
140 tzic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
142 u_int irq = ((struct tzic_irqsrc *)isrc)->irq;
143 struct tzic_softc *sc = device_get_softc(dev);
145 arm_irq_memory_barrier(irq);
146 tzic_irq_unmask(sc, irq);
150 tzic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
152 u_int irq = ((struct tzic_irqsrc *)isrc)->irq;
153 struct tzic_softc *sc = device_get_softc(dev);
155 tzic_irq_mask(sc, irq);
159 tzic_map_intr(device_t dev, struct intr_map_data *data,
160 struct intr_irqsrc **isrcp)
162 struct intr_map_data_fdt *daf;
163 struct tzic_softc *sc;
165 if (data->type != INTR_MAP_DATA_FDT)
168 daf = (struct intr_map_data_fdt *)data;
169 if (daf->ncells != 1 || daf->cells[0] >= TZIC_NIRQS)
172 sc = device_get_softc(dev);
173 *isrcp = &sc->isrcs[daf->cells[0]].isrc;
179 tzic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
181 struct tzic_softc *sc = device_get_softc(dev);
183 tzic_irq_mask(sc, ((struct tzic_irqsrc *)isrc)->irq);
188 tzic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
191 tzic_enable_intr(dev, isrc);
195 tzic_post_filter(device_t dev, struct intr_irqsrc *isrc)
198 tzic_irq_eoi(device_get_softc(dev));
202 tzic_pic_attach(struct tzic_softc *sc)
204 struct intr_pic *pic;
210 name = device_get_nameunit(sc->dev);
211 for (irq = 0; irq < TZIC_NIRQS; irq++) {
212 sc->isrcs[irq].irq = irq;
213 error = intr_isrc_register(&sc->isrcs[irq].isrc,
214 sc->dev, 0, "%s,%u", name, irq);
219 xref = OF_xref_from_node(ofw_bus_get_node(sc->dev));
220 pic = intr_pic_register(sc->dev, xref);
224 return (intr_pic_claim_root(sc->dev, xref, tzic_intr, sc, 0));
228 tzic_probe(device_t dev)
231 if (!ofw_bus_status_okay(dev))
234 if (ofw_bus_is_compatible(dev, "fsl,tzic")) {
235 device_set_desc(dev, "TrustZone Interrupt Controller");
236 return (BUS_PROBE_DEFAULT);
242 tzic_attach(device_t dev)
244 struct tzic_softc *sc = device_get_softc(dev);
253 sc->tzicregs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
255 if (sc->tzicregs == NULL) {
256 device_printf(dev, "could not allocate resources\n");
260 /* route all interrupts to IRQ. secure interrupts are for FIQ */
261 for (i = 0; i < 4; i++)
262 tzic_write_4(sc, TZIC_INTSEC(i), 0xffffffff);
264 /* disable all interrupts */
265 for (i = 0; i < 4; i++)
266 tzic_write_4(sc, TZIC_ENCLEAR(i), 0xffffffff);
268 /* Set all interrupts to priority 0 (max). */
269 for (i = 0; i < 128 / 4; ++i)
270 tzic_write_4(sc, TZIC_PRIORITY(i), 0);
273 * Set priority mask to lowest (unmasked) prio, set synchronizer to
274 * low-latency mode (as opposed to low-power), enable the controller.
276 tzic_write_4(sc, TZIC_PRIOMASK, 0xff);
277 tzic_write_4(sc, TZIC_SYNCCTRL, 0);
278 tzic_write_4(sc, TZIC_INTCNTL, INTCNTL_NSEN_MASK|INTCNTL_NSEN|INTCNTL_EN);
280 /* Register as a root pic. */
281 if (tzic_pic_attach(sc) != 0) {
282 device_printf(dev, "could not attach PIC\n");
289 static device_method_t tzic_methods[] = {
290 DEVMETHOD(device_probe, tzic_probe),
291 DEVMETHOD(device_attach, tzic_attach),
293 DEVMETHOD(pic_disable_intr, tzic_disable_intr),
294 DEVMETHOD(pic_enable_intr, tzic_enable_intr),
295 DEVMETHOD(pic_map_intr, tzic_map_intr),
296 DEVMETHOD(pic_post_filter, tzic_post_filter),
297 DEVMETHOD(pic_post_ithread, tzic_post_ithread),
298 DEVMETHOD(pic_pre_ithread, tzic_pre_ithread),
303 static driver_t tzic_driver = {
306 sizeof(struct tzic_softc),
309 static devclass_t tzic_devclass;
311 EARLY_DRIVER_MODULE(tzic, ofwbus, tzic_driver, tzic_devclass, 0, 0,