1*aed05100Sjmcneill /* $NetBSD: sunxi_intc.c,v 1.8 2022/06/25 12:41:56 jmcneill Exp $ */
269b44ac7Sjmcneill
369b44ac7Sjmcneill /*-
469b44ac7Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
569b44ac7Sjmcneill * All rights reserved.
669b44ac7Sjmcneill *
769b44ac7Sjmcneill * Redistribution and use in source and binary forms, with or without
869b44ac7Sjmcneill * modification, are permitted provided that the following conditions
969b44ac7Sjmcneill * are met:
1069b44ac7Sjmcneill * 1. Redistributions of source code must retain the above copyright
1169b44ac7Sjmcneill * notice, this list of conditions and the following disclaimer.
1269b44ac7Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1369b44ac7Sjmcneill * notice, this list of conditions and the following disclaimer in the
1469b44ac7Sjmcneill * documentation and/or other materials provided with the distribution.
1569b44ac7Sjmcneill *
1669b44ac7Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1769b44ac7Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1869b44ac7Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1969b44ac7Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2069b44ac7Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2169b44ac7Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2269b44ac7Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2369b44ac7Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2469b44ac7Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2569b44ac7Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2669b44ac7Sjmcneill * SUCH DAMAGE.
2769b44ac7Sjmcneill */
2869b44ac7Sjmcneill
2969b44ac7Sjmcneill #define _INTR_PRIVATE
3069b44ac7Sjmcneill
3169b44ac7Sjmcneill #include <sys/cdefs.h>
32*aed05100Sjmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_intc.c,v 1.8 2022/06/25 12:41:56 jmcneill Exp $");
3369b44ac7Sjmcneill
3469b44ac7Sjmcneill #include <sys/param.h>
3569b44ac7Sjmcneill #include <sys/bus.h>
3669b44ac7Sjmcneill #include <sys/device.h>
3769b44ac7Sjmcneill #include <sys/intr.h>
38f8a0ef89Sskrll #include <sys/kernel.h>
3958bc9390Sskrll #include <sys/lwp.h>
4069b44ac7Sjmcneill #include <sys/systm.h>
4169b44ac7Sjmcneill
4269b44ac7Sjmcneill #include <dev/fdt/fdtvar.h>
4369b44ac7Sjmcneill
4469b44ac7Sjmcneill #include <arm/cpu.h>
4569b44ac7Sjmcneill #include <arm/pic/picvar.h>
4669b44ac7Sjmcneill #include <arm/fdt/arm_fdtvar.h>
4769b44ac7Sjmcneill
4869b44ac7Sjmcneill #define INTC_MAX_SOURCES 96
4969b44ac7Sjmcneill #define INTC_MAX_GROUPS 3
5069b44ac7Sjmcneill
5169b44ac7Sjmcneill #define INTC_VECTOR_REG 0x00
5269b44ac7Sjmcneill #define INTC_BASE_ADDR_REG 0x04
5369b44ac7Sjmcneill #define INTC_PROTECT_REG 0x08
5469b44ac7Sjmcneill #define INTC_PROTECT_EN __BIT(0)
5569b44ac7Sjmcneill #define INTC_NMII_CTRL_REG 0x0c
5669b44ac7Sjmcneill #define INTC_IRQ_PEND_REG(n) (0x10 + ((n) * 4))
5769b44ac7Sjmcneill #define INTC_FIQ_PEND_REG(n) (0x20 + ((n) * 4))
5869b44ac7Sjmcneill #define INTC_SEL_REG(n) (0x30 + ((n) * 4))
5969b44ac7Sjmcneill #define INTC_EN_REG(n) (0x40 + ((n) * 4))
6069b44ac7Sjmcneill #define INTC_MASK_REG(n) (0x50 + ((n) * 4))
6169b44ac7Sjmcneill #define INTC_RESP_REG(n) (0x60 + ((n) * 4))
6269b44ac7Sjmcneill #define INTC_FORCE_REG(n) (0x70 + ((n) * 4))
6369b44ac7Sjmcneill #define INTC_SRC_PRIO_REG(n) (0x80 + ((n) * 4))
6469b44ac7Sjmcneill
656e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
666e54367aSthorpej { .compat = "allwinner,sun4i-a10-ic" },
676e54367aSthorpej DEVICE_COMPAT_EOL
6869b44ac7Sjmcneill };
6969b44ac7Sjmcneill
7069b44ac7Sjmcneill struct sunxi_intc_softc {
7169b44ac7Sjmcneill device_t sc_dev;
7269b44ac7Sjmcneill bus_space_tag_t sc_bst;
7369b44ac7Sjmcneill bus_space_handle_t sc_bsh;
7469b44ac7Sjmcneill int sc_phandle;
7569b44ac7Sjmcneill
7669b44ac7Sjmcneill uint32_t sc_enabled_irqs[INTC_MAX_GROUPS];
7769b44ac7Sjmcneill
7869b44ac7Sjmcneill struct pic_softc sc_pic;
7969b44ac7Sjmcneill };
8069b44ac7Sjmcneill
8169b44ac7Sjmcneill static struct sunxi_intc_softc *intc_softc;
8269b44ac7Sjmcneill
8369b44ac7Sjmcneill #define PICTOSOFTC(pic) \
8469b44ac7Sjmcneill ((void *)((uintptr_t)(pic) - offsetof(struct sunxi_intc_softc, sc_pic)))
8569b44ac7Sjmcneill
8669b44ac7Sjmcneill #define INTC_READ(sc, reg) \
8769b44ac7Sjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
8869b44ac7Sjmcneill #define INTC_WRITE(sc, reg, val) \
8969b44ac7Sjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
9069b44ac7Sjmcneill
9169b44ac7Sjmcneill static void
sunxi_intc_unblock_irqs(struct pic_softc * pic,size_t irqbase,uint32_t mask)9269b44ac7Sjmcneill sunxi_intc_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
9369b44ac7Sjmcneill {
9469b44ac7Sjmcneill struct sunxi_intc_softc * const sc = PICTOSOFTC(pic);
9569b44ac7Sjmcneill const u_int group = irqbase / 32;
9669b44ac7Sjmcneill
9769b44ac7Sjmcneill KASSERT((mask & sc->sc_enabled_irqs[group]) == 0);
9869b44ac7Sjmcneill sc->sc_enabled_irqs[group] |= mask;
9969b44ac7Sjmcneill INTC_WRITE(sc, INTC_EN_REG(group), sc->sc_enabled_irqs[group]);
1007befd5c4Sjmcneill INTC_WRITE(sc, INTC_MASK_REG(group), ~sc->sc_enabled_irqs[group]);
10169b44ac7Sjmcneill }
10269b44ac7Sjmcneill
10369b44ac7Sjmcneill static void
sunxi_intc_block_irqs(struct pic_softc * pic,size_t irqbase,uint32_t mask)10469b44ac7Sjmcneill sunxi_intc_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
10569b44ac7Sjmcneill {
10669b44ac7Sjmcneill struct sunxi_intc_softc * const sc = PICTOSOFTC(pic);
10769b44ac7Sjmcneill const u_int group = irqbase / 32;
10869b44ac7Sjmcneill
10969b44ac7Sjmcneill sc->sc_enabled_irqs[group] &= ~mask;
11069b44ac7Sjmcneill INTC_WRITE(sc, INTC_EN_REG(group), sc->sc_enabled_irqs[group]);
1117befd5c4Sjmcneill INTC_WRITE(sc, INTC_MASK_REG(group), ~sc->sc_enabled_irqs[group]);
11269b44ac7Sjmcneill }
11369b44ac7Sjmcneill
11469b44ac7Sjmcneill static void
sunxi_intc_establish_irq(struct pic_softc * pic,struct intrsource * is)11569b44ac7Sjmcneill sunxi_intc_establish_irq(struct pic_softc *pic, struct intrsource *is)
11669b44ac7Sjmcneill {
11769b44ac7Sjmcneill KASSERT(is->is_irq < INTC_MAX_SOURCES);
11869b44ac7Sjmcneill KASSERT(is->is_type == IST_LEVEL);
11969b44ac7Sjmcneill }
12069b44ac7Sjmcneill
12169b44ac7Sjmcneill static void
sunxi_intc_set_priority(struct pic_softc * pic,int ipl)12269b44ac7Sjmcneill sunxi_intc_set_priority(struct pic_softc *pic, int ipl)
12369b44ac7Sjmcneill {
124*aed05100Sjmcneill curcpu()->ci_cpl = ipl;
12569b44ac7Sjmcneill }
12669b44ac7Sjmcneill
12769b44ac7Sjmcneill static const struct pic_ops sunxi_intc_picops = {
12869b44ac7Sjmcneill .pic_unblock_irqs = sunxi_intc_unblock_irqs,
12969b44ac7Sjmcneill .pic_block_irqs = sunxi_intc_block_irqs,
13069b44ac7Sjmcneill .pic_establish_irq = sunxi_intc_establish_irq,
13169b44ac7Sjmcneill .pic_set_priority = sunxi_intc_set_priority,
13269b44ac7Sjmcneill };
13369b44ac7Sjmcneill
13469b44ac7Sjmcneill static void *
sunxi_intc_fdt_establish(device_t dev,u_int * specifier,int ipl,int flags,int (* func)(void *),void * arg,const char * xname)13569b44ac7Sjmcneill sunxi_intc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
13659ad346dSjmcneill int (*func)(void *), void *arg, const char *xname)
13769b44ac7Sjmcneill {
13869b44ac7Sjmcneill /* 1st cell is the interrupt number */
13969b44ac7Sjmcneill const u_int irq = be32toh(specifier[0]);
14069b44ac7Sjmcneill
14169b44ac7Sjmcneill if (irq >= INTC_MAX_SOURCES) {
14269b44ac7Sjmcneill #ifdef DIAGNOSTIC
14369b44ac7Sjmcneill device_printf(dev, "IRQ %u is invalid\n", irq);
14469b44ac7Sjmcneill #endif
14569b44ac7Sjmcneill return NULL;
14669b44ac7Sjmcneill }
14769b44ac7Sjmcneill
14869b44ac7Sjmcneill const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
14969b44ac7Sjmcneill
15059ad346dSjmcneill return intr_establish_xname(irq, ipl, IST_LEVEL | mpsafe, func, arg,
15159ad346dSjmcneill xname);
15269b44ac7Sjmcneill }
15369b44ac7Sjmcneill
15469b44ac7Sjmcneill static void
sunxi_intc_fdt_disestablish(device_t dev,void * ih)15569b44ac7Sjmcneill sunxi_intc_fdt_disestablish(device_t dev, void *ih)
15669b44ac7Sjmcneill {
15769b44ac7Sjmcneill intr_disestablish(ih);
15869b44ac7Sjmcneill }
15969b44ac7Sjmcneill
16069b44ac7Sjmcneill static bool
sunxi_intc_fdt_intrstr(device_t dev,u_int * specifier,char * buf,size_t buflen)16169b44ac7Sjmcneill sunxi_intc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
16269b44ac7Sjmcneill {
16369b44ac7Sjmcneill /* 1st cell is the interrupt number */
16469b44ac7Sjmcneill if (!specifier)
16569b44ac7Sjmcneill return false;
16669b44ac7Sjmcneill const u_int irq = be32toh(specifier[0]);
16769b44ac7Sjmcneill
16869b44ac7Sjmcneill snprintf(buf, buflen, "INTC irq %d", irq);
16969b44ac7Sjmcneill
17069b44ac7Sjmcneill return true;
17169b44ac7Sjmcneill }
17269b44ac7Sjmcneill
17369b44ac7Sjmcneill static const struct fdtbus_interrupt_controller_func sunxi_intc_fdt_funcs = {
17469b44ac7Sjmcneill .establish = sunxi_intc_fdt_establish,
17569b44ac7Sjmcneill .disestablish = sunxi_intc_fdt_disestablish,
17669b44ac7Sjmcneill .intrstr = sunxi_intc_fdt_intrstr,
17769b44ac7Sjmcneill };
17869b44ac7Sjmcneill
17969b44ac7Sjmcneill static int
sunxi_intc_find_pending_irqs(struct sunxi_intc_softc * sc,u_int group)18069b44ac7Sjmcneill sunxi_intc_find_pending_irqs(struct sunxi_intc_softc *sc, u_int group)
18169b44ac7Sjmcneill {
18269b44ac7Sjmcneill uint32_t pend;
18369b44ac7Sjmcneill
18469b44ac7Sjmcneill pend = INTC_READ(sc, INTC_IRQ_PEND_REG(group));
185d42f38d4Sjmcneill pend &= sc->sc_enabled_irqs[group];
18669b44ac7Sjmcneill
18769b44ac7Sjmcneill if (pend == 0)
18869b44ac7Sjmcneill return 0;
18969b44ac7Sjmcneill
190d42f38d4Sjmcneill INTC_WRITE(sc, INTC_IRQ_PEND_REG(group), pend);
191d42f38d4Sjmcneill
19269b44ac7Sjmcneill return pic_mark_pending_sources(&sc->sc_pic, group * 32, pend);
19369b44ac7Sjmcneill }
19469b44ac7Sjmcneill
19569b44ac7Sjmcneill static void
sunxi_intc_irq_handler(void * frame)19669b44ac7Sjmcneill sunxi_intc_irq_handler(void *frame)
19769b44ac7Sjmcneill {
19869b44ac7Sjmcneill struct cpu_info * const ci = curcpu();
19969b44ac7Sjmcneill struct sunxi_intc_softc * const sc = intc_softc;
20069b44ac7Sjmcneill const int oldipl = ci->ci_cpl;
20169b44ac7Sjmcneill const uint32_t oldipl_mask = __BIT(oldipl);
20269b44ac7Sjmcneill int ipl_mask = 0;
20369b44ac7Sjmcneill
20469b44ac7Sjmcneill ci->ci_data.cpu_nintr++;
20569b44ac7Sjmcneill
20669b44ac7Sjmcneill if (sc->sc_enabled_irqs[0])
20769b44ac7Sjmcneill ipl_mask |= sunxi_intc_find_pending_irqs(sc, 0);
20869b44ac7Sjmcneill if (sc->sc_enabled_irqs[1])
20969b44ac7Sjmcneill ipl_mask |= sunxi_intc_find_pending_irqs(sc, 1);
21069b44ac7Sjmcneill if (sc->sc_enabled_irqs[2])
21169b44ac7Sjmcneill ipl_mask |= sunxi_intc_find_pending_irqs(sc, 2);
21269b44ac7Sjmcneill
21369b44ac7Sjmcneill if ((ipl_mask & ~oldipl_mask) > oldipl_mask)
21469b44ac7Sjmcneill pic_do_pending_ints(I32_bit, oldipl, frame);
21569b44ac7Sjmcneill }
21669b44ac7Sjmcneill
21769b44ac7Sjmcneill static int
sunxi_intc_match(device_t parent,cfdata_t cf,void * aux)21869b44ac7Sjmcneill sunxi_intc_match(device_t parent, cfdata_t cf, void *aux)
21969b44ac7Sjmcneill {
22069b44ac7Sjmcneill struct fdt_attach_args * const faa = aux;
22169b44ac7Sjmcneill
2226e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
22369b44ac7Sjmcneill }
22469b44ac7Sjmcneill
22569b44ac7Sjmcneill static void
sunxi_intc_attach(device_t parent,device_t self,void * aux)22669b44ac7Sjmcneill sunxi_intc_attach(device_t parent, device_t self, void *aux)
22769b44ac7Sjmcneill {
22869b44ac7Sjmcneill struct sunxi_intc_softc * const sc = device_private(self);
22969b44ac7Sjmcneill struct fdt_attach_args * const faa = aux;
23069b44ac7Sjmcneill const int phandle = faa->faa_phandle;
23169b44ac7Sjmcneill bus_addr_t addr;
23269b44ac7Sjmcneill bus_size_t size;
23369b44ac7Sjmcneill int error, i;
23469b44ac7Sjmcneill
23569b44ac7Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
23669b44ac7Sjmcneill aprint_error(": couldn't get registers\n");
23769b44ac7Sjmcneill return;
23869b44ac7Sjmcneill }
23969b44ac7Sjmcneill
24069b44ac7Sjmcneill sc->sc_dev = self;
24169b44ac7Sjmcneill sc->sc_phandle = phandle;
24269b44ac7Sjmcneill sc->sc_bst = faa->faa_bst;
24369b44ac7Sjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
24469b44ac7Sjmcneill aprint_error(": couldn't map registers\n");
24569b44ac7Sjmcneill return;
24669b44ac7Sjmcneill }
24769b44ac7Sjmcneill
24869b44ac7Sjmcneill aprint_naive("\n");
24969b44ac7Sjmcneill aprint_normal(": Interrupt Controller\n");
25069b44ac7Sjmcneill
25169b44ac7Sjmcneill /* Disable IRQs */
25269b44ac7Sjmcneill for (i = 0; i < INTC_MAX_GROUPS; i++) {
25369b44ac7Sjmcneill INTC_WRITE(sc, INTC_EN_REG(i), 0);
2547befd5c4Sjmcneill INTC_WRITE(sc, INTC_MASK_REG(i), ~0U);
255d42f38d4Sjmcneill INTC_WRITE(sc, INTC_IRQ_PEND_REG(i),
256d42f38d4Sjmcneill INTC_READ(sc, INTC_IRQ_PEND_REG(i)));
25769b44ac7Sjmcneill }
25869b44ac7Sjmcneill /* Disable user mode access to intc registers */
25969b44ac7Sjmcneill INTC_WRITE(sc, INTC_PROTECT_REG, INTC_PROTECT_EN);
26069b44ac7Sjmcneill
26169b44ac7Sjmcneill sc->sc_pic.pic_ops = &sunxi_intc_picops;
26269b44ac7Sjmcneill sc->sc_pic.pic_maxsources = INTC_MAX_SOURCES;
26369b44ac7Sjmcneill snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "intc");
26469b44ac7Sjmcneill pic_add(&sc->sc_pic, 0);
26569b44ac7Sjmcneill
26669b44ac7Sjmcneill error = fdtbus_register_interrupt_controller(self, phandle,
26769b44ac7Sjmcneill &sunxi_intc_fdt_funcs);
26869b44ac7Sjmcneill if (error) {
26969b44ac7Sjmcneill aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
27069b44ac7Sjmcneill error);
27169b44ac7Sjmcneill return;
27269b44ac7Sjmcneill }
27369b44ac7Sjmcneill
27469b44ac7Sjmcneill KASSERT(intc_softc == NULL);
27569b44ac7Sjmcneill intc_softc = sc;
27669b44ac7Sjmcneill arm_fdt_irq_set_handler(sunxi_intc_irq_handler);
27769b44ac7Sjmcneill }
27869b44ac7Sjmcneill
27969b44ac7Sjmcneill CFATTACH_DECL_NEW(sunxi_intc, sizeof(struct sunxi_intc_softc),
28069b44ac7Sjmcneill sunxi_intc_match, sunxi_intc_attach, NULL, NULL);
281