Home | History | Annotate | Line # | Download | only in ti
ti_omapintc.c revision 1.1
      1  1.1  jakllsch /*	$NetBSD: ti_omapintc.c,v 1.1 2017/10/26 01:16:32 jakllsch Exp $	*/
      2  1.1  jakllsch /*
      3  1.1  jakllsch  * Define the SDP2430 specific information and then include the generic OMAP
      4  1.1  jakllsch  * interrupt header.
      5  1.1  jakllsch  */
      6  1.1  jakllsch 
      7  1.1  jakllsch /*
      8  1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
      9  1.1  jakllsch  * modification, are permitted provided that the following conditions
     10  1.1  jakllsch  * are met:
     11  1.1  jakllsch  * 1. Redistributions of source code must retain this list of conditions
     12  1.1  jakllsch  *    and the following disclaimer.
     13  1.1  jakllsch  * 2. Redistributions in binary form must reproduce this list of conditions
     14  1.1  jakllsch  *    and the following disclaimer in the documentation and/or other materials
     15  1.1  jakllsch  *    provided with the distribution.
     16  1.1  jakllsch  *
     17  1.1  jakllsch  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1  jakllsch  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
     19  1.1  jakllsch  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
     20  1.1  jakllsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  1.1  jakllsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  1.1  jakllsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23  1.1  jakllsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  1.1  jakllsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  1.1  jakllsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26  1.1  jakllsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jakllsch  */
     28  1.1  jakllsch 
     29  1.1  jakllsch #define _INTR_PRIVATE
     30  1.1  jakllsch 
     31  1.1  jakllsch #include <sys/cdefs.h>
     32  1.1  jakllsch __KERNEL_RCSID(0, "$NetBSD: ti_omapintc.c,v 1.1 2017/10/26 01:16:32 jakllsch Exp $");
     33  1.1  jakllsch 
     34  1.1  jakllsch #include <sys/param.h>
     35  1.1  jakllsch #include <sys/evcnt.h>
     36  1.1  jakllsch #include <sys/device.h>
     37  1.1  jakllsch 
     38  1.1  jakllsch #include <uvm/uvm_extern.h>
     39  1.1  jakllsch 
     40  1.1  jakllsch #include <machine/intr.h>
     41  1.1  jakllsch #include <sys/bus.h>
     42  1.1  jakllsch 
     43  1.1  jakllsch #include <arm/cpu.h>
     44  1.1  jakllsch #include <arm/armreg.h>
     45  1.1  jakllsch #include <arm/cpufunc.h>
     46  1.1  jakllsch #include <arm/atomic.h>
     47  1.1  jakllsch 
     48  1.1  jakllsch #include <dev/fdt/fdtvar.h>
     49  1.1  jakllsch 
     50  1.1  jakllsch #define INTC_CONTROL		0x048
     51  1.1  jakllsch #define INTC_CONTROL_NEWIRQAGR	__BIT(0)
     52  1.1  jakllsch #define INTC_ITR		0x080
     53  1.1  jakllsch #define INTC_MIR		0x084
     54  1.1  jakllsch #define INTC_MIR_CLEAR		0x088
     55  1.1  jakllsch #define INTC_MIR_SET		0x08c
     56  1.1  jakllsch #define INTC_PENDING_IRQ	0x098
     57  1.1  jakllsch 
     58  1.1  jakllsch #define INTC_MAX_SOURCES	128
     59  1.1  jakllsch 
     60  1.1  jakllsch 
     61  1.1  jakllsch #define	INTC_READ(sc, g, o)		\
     62  1.1  jakllsch 	bus_space_read_4((sc)->sc_memt, (sc)->sc_memh, (g) * 0x20 + (o))
     63  1.1  jakllsch #define	INTC_WRITE(sc, g, o, v)	\
     64  1.1  jakllsch 	bus_space_write_4((sc)->sc_memt, (sc)->sc_memh, (g) * 0x20 + (o), v)
     65  1.1  jakllsch 
     66  1.1  jakllsch static int omap2icu_match(device_t, cfdata_t, void *);
     67  1.1  jakllsch static void omap2icu_attach(device_t, device_t, void *);
     68  1.1  jakllsch 
     69  1.1  jakllsch static void omap2icu_unblock_irqs(struct pic_softc *, size_t, uint32_t);
     70  1.1  jakllsch static void omap2icu_block_irqs(struct pic_softc *, size_t, uint32_t);
     71  1.1  jakllsch static void omap2icu_establish_irq(struct pic_softc *, struct intrsource *);
     72  1.1  jakllsch static void omap2icu_set_priority(struct pic_softc *, int);
     73  1.1  jakllsch #if 0
     74  1.1  jakllsch static void omap2icu_source_name(struct pic_softc *, int, char *, size_t);
     75  1.1  jakllsch #endif
     76  1.1  jakllsch 
     77  1.1  jakllsch static const struct pic_ops omap2icu_picops = {
     78  1.1  jakllsch 	.pic_unblock_irqs = omap2icu_unblock_irqs,
     79  1.1  jakllsch 	.pic_block_irqs = omap2icu_block_irqs,
     80  1.1  jakllsch 	.pic_establish_irq = omap2icu_establish_irq,
     81  1.1  jakllsch 	.pic_set_priority = omap2icu_set_priority,
     82  1.1  jakllsch #if 0
     83  1.1  jakllsch 	.pic_source_name = omap2icu_source_name,
     84  1.1  jakllsch #endif
     85  1.1  jakllsch };
     86  1.1  jakllsch 
     87  1.1  jakllsch #define	PICTOSOFTC(pic)	\
     88  1.1  jakllsch 	((struct omap2icu_softc *)((uintptr_t)(pic) - offsetof(struct omap2icu_softc, sc_pic)))
     89  1.1  jakllsch 
     90  1.1  jakllsch struct omap2icu_softc {
     91  1.1  jakllsch 	device_t sc_dev;
     92  1.1  jakllsch 	bus_space_tag_t sc_memt;
     93  1.1  jakllsch 	bus_space_handle_t sc_memh;
     94  1.1  jakllsch 	struct pic_softc sc_pic;
     95  1.1  jakllsch 	uint32_t sc_enabled_irqs[howmany(INTC_MAX_SOURCES, 32)];
     96  1.1  jakllsch };
     97  1.1  jakllsch 
     98  1.1  jakllsch static struct omap2icu_softc *intc_softc;
     99  1.1  jakllsch 
    100  1.1  jakllsch static void
    101  1.1  jakllsch omap2icu_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask)
    102  1.1  jakllsch {
    103  1.1  jakllsch 	struct omap2icu_softc * const sc = PICTOSOFTC(pic);
    104  1.1  jakllsch 	const size_t group = irqbase / 32;
    105  1.1  jakllsch 	KASSERT((irq_mask & sc->sc_enabled_irqs[group]) == 0);
    106  1.1  jakllsch 	sc->sc_enabled_irqs[group] |= irq_mask;
    107  1.1  jakllsch 	INTC_WRITE(sc, group, INTC_MIR_CLEAR, irq_mask);
    108  1.1  jakllsch 
    109  1.1  jakllsch 	/* Force INTC to recompute IRQ availability */
    110  1.1  jakllsch 	INTC_WRITE(sc, 0, INTC_CONTROL, INTC_CONTROL_NEWIRQAGR);
    111  1.1  jakllsch }
    112  1.1  jakllsch 
    113  1.1  jakllsch static void
    114  1.1  jakllsch omap2icu_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask)
    115  1.1  jakllsch {
    116  1.1  jakllsch 	struct omap2icu_softc * const sc = PICTOSOFTC(pic);
    117  1.1  jakllsch 	const size_t group = irqbase / 32;
    118  1.1  jakllsch 
    119  1.1  jakllsch 	INTC_WRITE(sc, group, INTC_MIR_SET, irq_mask);
    120  1.1  jakllsch 	sc->sc_enabled_irqs[group] &= ~irq_mask;
    121  1.1  jakllsch }
    122  1.1  jakllsch 
    123  1.1  jakllsch /*
    124  1.1  jakllsch  * Called with interrupts disabled
    125  1.1  jakllsch  */
    126  1.1  jakllsch static int
    127  1.1  jakllsch find_pending_irqs(struct omap2icu_softc *sc, size_t group)
    128  1.1  jakllsch {
    129  1.1  jakllsch 	uint32_t pending = INTC_READ(sc, group, INTC_PENDING_IRQ);
    130  1.1  jakllsch 
    131  1.1  jakllsch 	KASSERT((sc->sc_enabled_irqs[group] & pending) == pending);
    132  1.1  jakllsch 
    133  1.1  jakllsch 	if (pending == 0)
    134  1.1  jakllsch 		return 0;
    135  1.1  jakllsch 
    136  1.1  jakllsch 	return pic_mark_pending_sources(&sc->sc_pic, group * 32, pending);
    137  1.1  jakllsch }
    138  1.1  jakllsch 
    139  1.1  jakllsch static void
    140  1.1  jakllsch omap_irq_handler(void *frame)
    141  1.1  jakllsch {
    142  1.1  jakllsch 	struct cpu_info * const ci = curcpu();
    143  1.1  jakllsch 	struct omap2icu_softc * const sc = intc_softc;
    144  1.1  jakllsch 	const int oldipl = ci->ci_cpl;
    145  1.1  jakllsch 	const uint32_t oldipl_mask = __BIT(oldipl);
    146  1.1  jakllsch 	int ipl_mask = 0;
    147  1.1  jakllsch 
    148  1.1  jakllsch 	ci->ci_data.cpu_nintr++;
    149  1.1  jakllsch 
    150  1.1  jakllsch 	if (sc->sc_enabled_irqs[0])
    151  1.1  jakllsch 		ipl_mask |= find_pending_irqs(sc, 0);
    152  1.1  jakllsch 	if (sc->sc_enabled_irqs[1])
    153  1.1  jakllsch 		ipl_mask |= find_pending_irqs(sc, 1);
    154  1.1  jakllsch 	if (sc->sc_enabled_irqs[2])
    155  1.1  jakllsch 		ipl_mask |= find_pending_irqs(sc, 2);
    156  1.1  jakllsch 	if (sc->sc_enabled_irqs[3])
    157  1.1  jakllsch 		ipl_mask |= find_pending_irqs(sc, 3);
    158  1.1  jakllsch 
    159  1.1  jakllsch 	/* force INTC to recomputq IRQ */
    160  1.1  jakllsch 	INTC_WRITE(sc, 0, INTC_CONTROL, INTC_CONTROL_NEWIRQAGR);
    161  1.1  jakllsch 
    162  1.1  jakllsch 	/*
    163  1.1  jakllsch 	 * Record the pending_ipls and deliver them if we can.
    164  1.1  jakllsch 	 */
    165  1.1  jakllsch 	if ((ipl_mask & ~oldipl_mask) > oldipl_mask)
    166  1.1  jakllsch 		pic_do_pending_ints(I32_bit, oldipl, frame);
    167  1.1  jakllsch }
    168  1.1  jakllsch 
    169  1.1  jakllsch void
    170  1.1  jakllsch omap2icu_establish_irq(struct pic_softc *pic, struct intrsource *is)
    171  1.1  jakllsch {
    172  1.1  jakllsch 	KASSERT(is->is_irq < PICTOSOFTC(pic)->sc_pic.pic_maxsources);
    173  1.1  jakllsch 	KASSERT(is->is_type == IST_LEVEL);
    174  1.1  jakllsch }
    175  1.1  jakllsch 
    176  1.1  jakllsch static void
    177  1.1  jakllsch omap2icu_set_priority(struct pic_softc *pic, int ipl)
    178  1.1  jakllsch {
    179  1.1  jakllsch 	curcpu()->ci_cpl = ipl;
    180  1.1  jakllsch }
    181  1.1  jakllsch 
    182  1.1  jakllsch static void *
    183  1.1  jakllsch omapintc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    184  1.1  jakllsch     int (*func)(void *), void *arg)
    185  1.1  jakllsch {
    186  1.1  jakllsch 	const u_int irq = be32toh(specifier[0]);
    187  1.1  jakllsch 	if (irq >= INTC_MAX_SOURCES) {
    188  1.1  jakllsch 		device_printf(dev, "IRQ %u is invalid\n", irq);
    189  1.1  jakllsch 		return NULL;
    190  1.1  jakllsch 	}
    191  1.1  jakllsch 
    192  1.1  jakllsch 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    193  1.1  jakllsch 	return intr_establish(irq, ipl, IST_LEVEL | mpsafe, func, arg);
    194  1.1  jakllsch }
    195  1.1  jakllsch 
    196  1.1  jakllsch static void
    197  1.1  jakllsch omapintc_fdt_disestablish(device_t dev, void *ih)
    198  1.1  jakllsch {
    199  1.1  jakllsch         intr_disestablish(ih);
    200  1.1  jakllsch }
    201  1.1  jakllsch 
    202  1.1  jakllsch static bool
    203  1.1  jakllsch omapintc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    204  1.1  jakllsch {
    205  1.1  jakllsch 	if (!specifier)
    206  1.1  jakllsch 		return false;
    207  1.1  jakllsch 
    208  1.1  jakllsch 	const u_int irq = be32toh(specifier[0]);
    209  1.1  jakllsch 	snprintf(buf, buflen, "INTC irq %d", irq);
    210  1.1  jakllsch 	return true;
    211  1.1  jakllsch }
    212  1.1  jakllsch 
    213  1.1  jakllsch static const struct fdtbus_interrupt_controller_func omapintc_fdt_funcs = {
    214  1.1  jakllsch 	.establish = omapintc_fdt_establish,
    215  1.1  jakllsch 	.disestablish = omapintc_fdt_disestablish,
    216  1.1  jakllsch 	.intrstr = omapintc_fdt_intrstr,
    217  1.1  jakllsch };
    218  1.1  jakllsch 
    219  1.1  jakllsch int
    220  1.1  jakllsch omap2icu_match(device_t parent, cfdata_t cf, void *aux)
    221  1.1  jakllsch {
    222  1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
    223  1.1  jakllsch 
    224  1.1  jakllsch 	static const char * const compatible[] = {
    225  1.1  jakllsch 		"ti,am33xx-intc",
    226  1.1  jakllsch 		NULL
    227  1.1  jakllsch 	};
    228  1.1  jakllsch 
    229  1.1  jakllsch 	return of_match_compatible(faa->faa_phandle, compatible);
    230  1.1  jakllsch }
    231  1.1  jakllsch 
    232  1.1  jakllsch void
    233  1.1  jakllsch omap2icu_attach(device_t parent, device_t self, void *aux)
    234  1.1  jakllsch {
    235  1.1  jakllsch 	struct omap2icu_softc * const sc = device_private(self);
    236  1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
    237  1.1  jakllsch 	const int phandle = faa->faa_phandle;
    238  1.1  jakllsch 	bus_addr_t addr;
    239  1.1  jakllsch 	bus_size_t size;
    240  1.1  jakllsch 	int error;
    241  1.1  jakllsch 
    242  1.1  jakllsch 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    243  1.1  jakllsch 		aprint_error(": couldn't get registers\n");
    244  1.1  jakllsch 		return;
    245  1.1  jakllsch 	}
    246  1.1  jakllsch 
    247  1.1  jakllsch 	sc->sc_dev = self;
    248  1.1  jakllsch 	sc->sc_memt = faa->faa_bst;
    249  1.1  jakllsch 	if (bus_space_map(sc->sc_memt, addr, size, 0, &sc->sc_memh) != 0) {
    250  1.1  jakllsch 		aprint_error(": couldn't map registers\n");
    251  1.1  jakllsch 		return;
    252  1.1  jakllsch 	}
    253  1.1  jakllsch 
    254  1.1  jakllsch 	aprint_naive("\n");
    255  1.1  jakllsch 	aprint_normal("\n");
    256  1.1  jakllsch 
    257  1.1  jakllsch 	INTC_WRITE(sc, 0, INTC_MIR_SET, 0xffffffff);
    258  1.1  jakllsch 	INTC_WRITE(sc, 1, INTC_MIR_SET, 0xffffffff);
    259  1.1  jakllsch 	INTC_WRITE(sc, 2, INTC_MIR_SET, 0xffffffff);
    260  1.1  jakllsch 
    261  1.1  jakllsch 	sc->sc_dev = self;
    262  1.1  jakllsch 	self->dv_private = sc;
    263  1.1  jakllsch 
    264  1.1  jakllsch 	sc->sc_pic.pic_ops = &omap2icu_picops;
    265  1.1  jakllsch 	sc->sc_pic.pic_maxsources = INTC_MAX_SOURCES;
    266  1.1  jakllsch 	snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "intc");
    267  1.1  jakllsch 	pic_add(&sc->sc_pic, 0);
    268  1.1  jakllsch 	error = fdtbus_register_interrupt_controller(self, phandle,
    269  1.1  jakllsch 		&omapintc_fdt_funcs);
    270  1.1  jakllsch 	if (error) {
    271  1.1  jakllsch 		aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
    272  1.1  jakllsch 		    error);
    273  1.1  jakllsch 		return;
    274  1.1  jakllsch 	}
    275  1.1  jakllsch 
    276  1.1  jakllsch 	KASSERT(intc_softc == NULL);
    277  1.1  jakllsch 	intc_softc = sc;
    278  1.1  jakllsch 	arm_fdt_irq_set_handler(omap_irq_handler);
    279  1.1  jakllsch }
    280  1.1  jakllsch 
    281  1.1  jakllsch CFATTACH_DECL_NEW(omapintc,
    282  1.1  jakllsch     sizeof(struct omap2icu_softc),
    283  1.1  jakllsch     omap2icu_match, omap2icu_attach,
    284  1.1  jakllsch     NULL, NULL);
    285