1 1.2 yurix /* $NetBSD: am18xx_intc.c,v 1.2 2026/07/30 15:08:08 yurix Exp $ */ 2 1.1 yurix 3 1.1 yurix /*- 4 1.1 yurix * Copyright (c) 2025 The NetBSD Foundation, Inc. 5 1.1 yurix * All rights reserved. 6 1.1 yurix * 7 1.1 yurix * This code is derived from software contributed to The NetBSD Foundation 8 1.1 yurix * by Yuri Honegger. 9 1.1 yurix * 10 1.1 yurix * Redistribution and use in source and binary forms, with or without 11 1.1 yurix * modification, are permitted provided that the following conditions 12 1.1 yurix * are met: 13 1.1 yurix * 1. Redistributions of source code must retain the above copyright 14 1.1 yurix * notice, this list of conditions and the following disclaimer. 15 1.1 yurix * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 yurix * notice, this list of conditions and the following disclaimer in the 17 1.1 yurix * documentation and/or other materials provided with the distribution. 18 1.1 yurix * 19 1.1 yurix * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 yurix * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 yurix * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 yurix * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 yurix * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 yurix * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 yurix * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 yurix * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 yurix * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 yurix * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 yurix * POSSIBILITY OF SUCH DAMAGE. 30 1.1 yurix */ 31 1.1 yurix 32 1.1 yurix /* 33 1.1 yurix * Interrupt controller for the TI AM18XX SOC 34 1.1 yurix */ 35 1.1 yurix 36 1.1 yurix #define _INTR_PRIVATE 37 1.1 yurix 38 1.1 yurix #include <sys/cdefs.h> 39 1.1 yurix #include <sys/cpu.h> 40 1.1 yurix #include <sys/device.h> 41 1.1 yurix 42 1.1 yurix #include <dev/fdt/fdtvar.h> 43 1.1 yurix 44 1.1 yurix #include <arm/armreg.h> 45 1.1 yurix #include <arm/pic/picvar.h> 46 1.1 yurix 47 1.1 yurix struct am18xx_intc_softc { 48 1.1 yurix struct pic_softc sc_pic; 49 1.1 yurix bus_space_tag_t sc_bst; 50 1.1 yurix bus_space_handle_t sc_bsh; 51 1.1 yurix int sc_num_irqs; 52 1.1 yurix }; 53 1.1 yurix 54 1.1 yurix static int am18xx_intc_match(device_t, cfdata_t, void *); 55 1.1 yurix static void am18xx_intc_attach(device_t, device_t, void *); 56 1.1 yurix 57 1.1 yurix static void am18xx_intc_unblock_irqs(struct pic_softc *, size_t, uint32_t); 58 1.1 yurix static void am18xx_intc_block_irqs(struct pic_softc *, size_t, uint32_t); 59 1.1 yurix static void am18xx_intc_establish_irq(struct pic_softc *, 60 1.1 yurix struct intrsource *); 61 1.1 yurix static void am18xx_intc_set_priority(struct pic_softc *, int); 62 1.1 yurix static void am18xx_intc_irq_handler(void *); 63 1.1 yurix static void *am18xx_intc_fdt_establish(device_t, u_int *, int, int, 64 1.1 yurix int (*)(void *), void *, 65 1.1 yurix const char *); 66 1.1 yurix static void am18xx_intc_fdt_disestablish(device_t, void *); 67 1.1 yurix static bool am18xx_intc_fdt_intrstr(device_t, u_int *, char *, size_t); 68 1.1 yurix static void am18xx_intc_enable_interrupts(struct am18xx_intc_softc *); 69 1.1 yurix 70 1.1 yurix #define AM18XX_AINTC_CR 0x4 71 1.1 yurix #define AM18XX_AINTC_GER 0x10 72 1.1 yurix #define AM18XX_AINTC_SECR1 0x280 73 1.1 yurix #define AM18XX_AINTC_ESR1 0x300 74 1.1 yurix #define AM18XX_AINTC_ECR1 0x380 75 1.1 yurix #define AM18XX_AINTC_CMR0 0x400 76 1.1 yurix #define AM18XX_AINTC_HIER 0x1500 77 1.1 yurix 78 1.1 yurix #define AM18XX_AINTC_GER_ENABLE 1 79 1.1 yurix #define AM18XX_AINTC_HIER_IRQ 2 80 1.1 yurix #define AM18XX_AINTC_IRQ_CHANNEL 4 /* any number between 2 and 31 works */ 81 1.1 yurix 82 1.1 yurix #define INTC_READ(sc, reg) \ 83 1.1 yurix bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, reg) 84 1.1 yurix #define INTC_WRITE(sc, reg, val) \ 85 1.1 yurix bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, reg, val) 86 1.1 yurix #define PICTOSOFTC(pic) \ 87 1.1 yurix ((struct am18xx_intc_softc *) \ 88 1.1 yurix ((uintptr_t)(pic) - offsetof(struct am18xx_intc_softc, sc_pic))) 89 1.1 yurix 90 1.1 yurix static struct am18xx_intc_softc *intc_softc; 91 1.1 yurix 92 1.1 yurix CFATTACH_DECL_NEW(am18xxintc, sizeof(struct am18xx_intc_softc), 93 1.1 yurix am18xx_intc_match, am18xx_intc_attach, NULL, NULL); 94 1.1 yurix 95 1.1 yurix static const struct device_compatible_entry compat_data[] = { 96 1.1 yurix { .compat = "ti,cp-intc" }, 97 1.1 yurix DEVICE_COMPAT_EOL 98 1.1 yurix }; 99 1.1 yurix 100 1.1 yurix static const struct fdtbus_interrupt_controller_func am18xx_intc_fdt_funcs = { 101 1.1 yurix .establish = am18xx_intc_fdt_establish, 102 1.1 yurix .disestablish = am18xx_intc_fdt_disestablish, 103 1.1 yurix .intrstr = am18xx_intc_fdt_intrstr, 104 1.1 yurix }; 105 1.1 yurix 106 1.1 yurix static const struct pic_ops am18xx_intc_picops = { 107 1.1 yurix .pic_unblock_irqs = am18xx_intc_unblock_irqs, 108 1.1 yurix .pic_block_irqs = am18xx_intc_block_irqs, 109 1.1 yurix .pic_establish_irq = am18xx_intc_establish_irq, 110 1.1 yurix .pic_set_priority = am18xx_intc_set_priority, 111 1.1 yurix }; 112 1.1 yurix 113 1.1 yurix static void * 114 1.1 yurix am18xx_intc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags, 115 1.1 yurix int (*func)(void *), void *arg, const char *xname) 116 1.1 yurix { 117 1.1 yurix struct am18xx_intc_softc * const sc = device_private(dev); 118 1.1 yurix 119 1.1 yurix const u_int irq = be32toh(specifier[0]); 120 1.1 yurix if (irq >= sc->sc_num_irqs) { 121 1.1 yurix device_printf(dev, "IRQ %u is out of range\n", irq); 122 1.1 yurix return NULL; 123 1.1 yurix } 124 1.1 yurix 125 1.1 yurix const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0; 126 1.1 yurix return intr_establish_xname(irq, ipl, IST_LEVEL | mpsafe, func, arg, 127 1.1 yurix xname); 128 1.1 yurix } 129 1.1 yurix 130 1.1 yurix static void 131 1.1 yurix am18xx_intc_fdt_disestablish(device_t dev, void *ih) 132 1.1 yurix { 133 1.1 yurix return intr_disestablish(ih); 134 1.1 yurix } 135 1.1 yurix 136 1.1 yurix static bool 137 1.1 yurix am18xx_intc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, 138 1.1 yurix size_t buflen) 139 1.1 yurix { 140 1.1 yurix const u_int irq = be32toh(specifier[0]); 141 1.1 yurix snprintf(buf, buflen, "irq %d", irq); 142 1.1 yurix return true; 143 1.1 yurix } 144 1.1 yurix 145 1.1 yurix static void 146 1.1 yurix am18xx_intc_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t mask) 147 1.1 yurix { 148 1.1 yurix struct am18xx_intc_softc * const sc = PICTOSOFTC(pic); 149 1.1 yurix 150 1.1 yurix const size_t group = irq_base / 32; 151 1.1 yurix uint32_t esr_reg = AM18XX_AINTC_ESR1 + 4 * group; 152 1.1 yurix INTC_WRITE(sc, esr_reg, mask); 153 1.1 yurix } 154 1.1 yurix 155 1.1 yurix static void 156 1.1 yurix am18xx_intc_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t mask) 157 1.1 yurix { 158 1.1 yurix struct am18xx_intc_softc * const sc = PICTOSOFTC(pic); 159 1.1 yurix 160 1.1 yurix const size_t group = irq_base / 32; 161 1.1 yurix uint32_t ecr_reg = AM18XX_AINTC_ECR1 + 4 * group; 162 1.1 yurix INTC_WRITE(sc, ecr_reg, mask); 163 1.1 yurix } 164 1.1 yurix 165 1.1 yurix static void 166 1.1 yurix am18xx_intc_establish_irq(struct pic_softc *pic, struct intrsource *is) 167 1.1 yurix { 168 1.1 yurix struct am18xx_intc_softc * const sc = PICTOSOFTC(pic); 169 1.1 yurix 170 1.1 yurix /* there is one CMR register per 4 interrupts*/ 171 1.1 yurix uint32_t cmr_reg = AM18XX_AINTC_CMR0 + (is->is_irq & (~0x3)); 172 1.1 yurix uint32_t cmr_shift = (is->is_irq % 4) * 8; 173 1.1 yurix 174 1.1 yurix /* update CMR register with channel */ 175 1.1 yurix uint32_t cmr = INTC_READ(sc, cmr_reg); 176 1.1 yurix cmr = cmr & ~(0xFF<<cmr_shift); 177 1.1 yurix cmr |= AM18XX_AINTC_IRQ_CHANNEL << cmr_shift; 178 1.1 yurix INTC_WRITE(sc, cmr_reg, cmr); 179 1.1 yurix } 180 1.1 yurix 181 1.1 yurix static void 182 1.1 yurix am18xx_intc_set_priority(struct pic_softc *pic, int new_ipl) 183 1.1 yurix { 184 1.1 yurix curcpu()->ci_cpl = new_ipl; 185 1.1 yurix } 186 1.1 yurix 187 1.1 yurix static int 188 1.1 yurix find_pending_irqs(struct am18xx_intc_softc *sc, size_t group) 189 1.1 yurix { 190 1.1 yurix uint32_t reg = AM18XX_AINTC_SECR1 + (4 * group); 191 1.1 yurix uint32_t pending = INTC_READ(sc, reg); 192 1.1 yurix 193 1.1 yurix /* clear interrupts */ 194 1.1 yurix INTC_WRITE(sc, reg, pending); 195 1.1 yurix 196 1.1 yurix if (pending == 0) 197 1.1 yurix return 0; 198 1.1 yurix 199 1.1 yurix return pic_mark_pending_sources(&sc->sc_pic, group * 32, pending); 200 1.1 yurix } 201 1.1 yurix 202 1.1 yurix static void 203 1.1 yurix am18xx_intc_irq_handler(void *frame) 204 1.1 yurix { 205 1.1 yurix struct cpu_info * const ci = curcpu(); 206 1.1 yurix struct am18xx_intc_softc * const sc = intc_softc; 207 1.1 yurix const int old_ipl = ci->ci_cpl; 208 1.1 yurix const uint32_t oldipl_mask = __BIT(old_ipl); 209 1.1 yurix int ipl_mask = 0; 210 1.1 yurix 211 1.1 yurix ci->ci_data.cpu_nintr++; 212 1.1 yurix 213 1.1 yurix for (int group = 0; group <= sc->sc_num_irqs/32; group++) { 214 1.1 yurix ipl_mask |= find_pending_irqs(sc, group); 215 1.1 yurix } 216 1.1 yurix 217 1.1 yurix if ((ipl_mask & ~oldipl_mask) > oldipl_mask) 218 1.1 yurix pic_do_pending_ints(I32_bit, old_ipl, frame); 219 1.1 yurix } 220 1.1 yurix 221 1.1 yurix /* 222 1.1 yurix * Enable interrupt according to the TRM section 11.3.2 223 1.1 yurix */ 224 1.1 yurix static void am18xx_intc_enable_interrupts(struct am18xx_intc_softc *sc) 225 1.1 yurix { 226 1.1 yurix /* disable interrupts */ 227 1.1 yurix INTC_WRITE(sc, AM18XX_AINTC_GER, 0); 228 1.1 yurix INTC_WRITE(sc, AM18XX_AINTC_HIER, 0); 229 1.1 yurix 230 1.1 yurix /* disable nesting and prioritization in CR */ 231 1.1 yurix INTC_WRITE(sc, AM18XX_AINTC_CR, 0); 232 1.1 yurix 233 1.1 yurix /* disable all interrupts, clear their status */ 234 1.1 yurix for (int i = 0; i <= sc->sc_num_irqs / 32; i++) { 235 1.1 yurix INTC_WRITE(sc, AM18XX_AINTC_ECR1 + 4 * i, 0xFFFFFFFF); 236 1.1 yurix INTC_WRITE(sc, AM18XX_AINTC_SECR1 + 4 * i, 0xFFFFFFFF); 237 1.1 yurix } 238 1.1 yurix 239 1.1 yurix /* enable IRQ interrupts */ 240 1.1 yurix INTC_WRITE(sc, AM18XX_AINTC_HIER, AM18XX_AINTC_HIER_IRQ); 241 1.1 yurix 242 1.1 yurix /* set enable bit in GER */ 243 1.1 yurix INTC_WRITE(sc, AM18XX_AINTC_GER, AM18XX_AINTC_GER_ENABLE); 244 1.1 yurix } 245 1.1 yurix 246 1.1 yurix int 247 1.1 yurix am18xx_intc_match(device_t parent, cfdata_t cf, void *aux) 248 1.1 yurix { 249 1.1 yurix struct fdt_attach_args * const faa = aux; 250 1.1 yurix 251 1.1 yurix return of_compatible_match(faa->faa_phandle, compat_data); 252 1.1 yurix } 253 1.1 yurix 254 1.1 yurix void 255 1.1 yurix am18xx_intc_attach(device_t parent, device_t self, void *aux) 256 1.1 yurix { 257 1.1 yurix struct am18xx_intc_softc * const sc = device_private(self); 258 1.1 yurix struct fdt_attach_args * const faa = aux; 259 1.1 yurix const int phandle = faa->faa_phandle; 260 1.1 yurix bus_addr_t addr; 261 1.1 yurix bus_size_t size; 262 1.1 yurix uint32_t maxsources; 263 1.1 yurix 264 1.1 yurix sc->sc_bst = faa->faa_bst; 265 1.1 yurix intc_softc = sc; 266 1.1 yurix 267 1.1 yurix if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 268 1.1 yurix aprint_error(": couldn't get registers\n"); 269 1.1 yurix return; 270 1.1 yurix } 271 1.1 yurix if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 272 1.1 yurix aprint_error(": couldn't map registers\n"); 273 1.1 yurix return; 274 1.1 yurix } 275 1.1 yurix 276 1.1 yurix if (of_getprop_uint32(phandle, "ti,intc-size", &maxsources) != 0) { 277 1.1 yurix aprint_error(": couldn't get max interrupt number\n"); 278 1.1 yurix return; 279 1.1 yurix } 280 1.1 yurix sc->sc_num_irqs = maxsources; 281 1.1 yurix 282 1.1 yurix sc->sc_pic.pic_ops = &am18xx_intc_picops; 283 1.1 yurix sc->sc_pic.pic_maxsources = maxsources; 284 1.1 yurix strlcpy(sc->sc_pic.pic_name, device_xname(self), 285 1.1 yurix sizeof(sc->sc_pic.pic_name)); 286 1.1 yurix 287 1.1 yurix pic_add(&sc->sc_pic, 0); 288 1.1 yurix fdtbus_register_interrupt_controller(self, phandle, 289 1.1 yurix &am18xx_intc_fdt_funcs); 290 1.1 yurix 291 1.1 yurix arm_fdt_irq_set_handler(am18xx_intc_irq_handler); 292 1.1 yurix am18xx_intc_enable_interrupts(sc); 293 1.1 yurix 294 1.1 yurix aprint_normal("\n"); 295 1.1 yurix } 296 1.1 yurix 297