1 1.8 riastrad /* $NetBSD: imx31_icu.c,v 1.8 2022/02/12 03:24:34 riastradh Exp $ */ 2 1.2 matt /*- 3 1.2 matt * Copyright (c) 2007 The NetBSD Foundation, Inc. 4 1.2 matt * All rights reserved. 5 1.2 matt * 6 1.2 matt * This code is derived from software contributed to The NetBSD Foundation 7 1.2 matt * by Matt Thomas. 8 1.2 matt * 9 1.2 matt * Redistribution and use in source and binary forms, with or without 10 1.2 matt * modification, are permitted provided that the following conditions 11 1.2 matt * are met: 12 1.2 matt * 1. Redistributions of source code must retain the above copyright 13 1.2 matt * notice, this list of conditions and the following disclaimer. 14 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright 15 1.2 matt * notice, this list of conditions and the following disclaimer in the 16 1.2 matt * documentation and/or other materials provided with the distribution. 17 1.2 matt * 18 1.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.2 matt * POSSIBILITY OF SUCH DAMAGE. 29 1.2 matt */ 30 1.2 matt #include <sys/cdefs.h> 31 1.8 riastrad __KERNEL_RCSID(0, "$NetBSD: imx31_icu.c,v 1.8 2022/02/12 03:24:34 riastradh Exp $"); 32 1.2 matt 33 1.2 matt #define _INTR_PRIVATE 34 1.2 matt 35 1.2 matt #include "locators.h" 36 1.2 matt 37 1.2 matt #include <sys/param.h> 38 1.2 matt #include <sys/evcnt.h> 39 1.2 matt #include <sys/device.h> 40 1.5 matt #include <sys/atomic.h> 41 1.2 matt 42 1.2 matt #include <uvm/uvm_extern.h> 43 1.2 matt 44 1.2 matt #include <machine/intr.h> 45 1.2 matt 46 1.2 matt #include <arm/cpu.h> 47 1.2 matt #include <arm/armreg.h> 48 1.2 matt #include <arm/cpufunc.h> 49 1.2 matt 50 1.2 matt #include <machine/autoconf.h> 51 1.6 dyoung #include <sys/bus.h> 52 1.2 matt 53 1.2 matt #include <arm/imx/imx31reg.h> 54 1.2 matt #include <arm/imx/imx31var.h> 55 1.2 matt 56 1.2 matt static void avic_unblock_irqs(struct pic_softc *, size_t, uint32_t); 57 1.2 matt static void avic_block_irqs(struct pic_softc *, size_t, uint32_t); 58 1.2 matt static void avic_establish_irq(struct pic_softc *, struct intrsource *); 59 1.2 matt static void avic_source_name(struct pic_softc *, int, char *, size_t); 60 1.2 matt 61 1.2 matt const struct pic_ops avic_pic_ops = { 62 1.2 matt .pic_unblock_irqs = avic_unblock_irqs, 63 1.2 matt .pic_block_irqs = avic_block_irqs, 64 1.2 matt .pic_establish_irq = avic_establish_irq, 65 1.2 matt .pic_source_name = avic_source_name 66 1.2 matt }; 67 1.2 matt 68 1.2 matt struct avic_softc { 69 1.2 matt struct pic_softc avic_pic; 70 1.2 matt bus_space_tag_t avic_memt; 71 1.2 matt bus_space_handle_t avic_memh; 72 1.2 matt }; 73 1.2 matt 74 1.2 matt extern struct cfdriver avic_cd; 75 1.2 matt 76 1.2 matt #define INTC_READ(avic, reg) \ 77 1.2 matt bus_space_read_4((avic)->avic_memt, (avic)->avic_memh, (reg)) 78 1.2 matt #define INTC_WRITE(avic, reg, val) \ 79 1.2 matt bus_space_write_4((avic)->avic_memt, (avic)->avic_memh, (reg), (val)) 80 1.2 matt #define HW_TO_SW_IPL(ipl) ((ipl) + 1) 81 1.2 matt #define SW_TO_HW_IPL(ipl) ((ipl) - 1) 82 1.2 matt 83 1.2 matt void 84 1.2 matt avic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask) 85 1.2 matt { 86 1.2 matt struct avic_softc * const avic = (void *) pic; 87 1.2 matt #if 0 88 1.2 matt if (irq_base == 0) 89 1.2 matt INTC_WRITE(avic, IMX31_INTENABLEL, irq_mask); 90 1.2 matt else 91 1.2 matt INTC_WRITE(avic, IMX31_INTENABLEH, irq_mask); 92 1.2 matt #else 93 1.2 matt uint32_t irq; 94 1.2 matt while ((irq = ffs(irq_mask)) != 0) { 95 1.2 matt irq--; 96 1.2 matt irq_base += irq; 97 1.2 matt irq_mask >>= irq; 98 1.2 matt INTC_WRITE(avic, IMX31_INTENNUM, irq_base); 99 1.2 matt } 100 1.2 matt #endif 101 1.2 matt } 102 1.2 matt 103 1.2 matt void 104 1.2 matt avic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask) 105 1.2 matt { 106 1.2 matt struct avic_softc * const avic = (void *) pic; 107 1.2 matt #if 0 108 1.2 matt if (irq_base == 0) 109 1.2 matt INTC_WRITE(avic, IMX31_INTDISABLEL, irq_mask); 110 1.2 matt else 111 1.2 matt INTC_WRITE(avic, IMX31_INTDISABLEH, irq_mask); 112 1.2 matt #else 113 1.2 matt uint32_t irq; 114 1.2 matt while ((irq = ffs(irq_mask)) != 0) { 115 1.2 matt irq--; 116 1.2 matt irq_base += irq; 117 1.2 matt irq_mask >>= irq; 118 1.2 matt INTC_WRITE(avic, IMX31_INTDISNUM, irq_base); 119 1.2 matt } 120 1.2 matt #endif 121 1.2 matt } 122 1.2 matt 123 1.2 matt void 124 1.2 matt avic_establish_irq(struct pic_softc *pic, struct intrsource *is) 125 1.2 matt { 126 1.2 matt struct avic_softc * const avic = (void *) pic; 127 1.2 matt bus_addr_t priority_reg; 128 1.2 matt int priority_shift; 129 1.2 matt uint32_t v; 130 1.2 matt 131 1.2 matt KASSERT(is->is_irq < 64); 132 1.2 matt KASSERT(is->is_ipl < 16); 133 1.2 matt 134 1.2 matt priority_reg = IMX31_NIPRIORITY0 - (is->is_irq >> 3); 135 1.2 matt priority_shift = (is->is_irq & 7) * 4; 136 1.2 matt v = INTC_READ(avic, priority_reg); 137 1.2 matt v &= ~(0x0f << priority_shift); 138 1.2 matt v |= SW_TO_HW_IPL(is->is_ipl) << priority_shift; 139 1.2 matt INTC_WRITE(avic, priority_reg, v); 140 1.2 matt 141 1.2 matt KASSERT(is->is_type == IST_LEVEL); 142 1.2 matt } 143 1.2 matt 144 1.2 matt static const char * const avic_intr_source_names[] = AVIC_INTR_SOURCE_NAMES; 145 1.2 matt 146 1.2 matt void 147 1.2 matt avic_source_name(struct pic_softc *pic, int irq, char *buf, size_t len) 148 1.2 matt { 149 1.2 matt strlcpy(buf, avic_intr_source_names[irq], len); 150 1.2 matt } 151 1.2 matt 152 1.2 matt void 153 1.2 matt imx31_irq_handler(void *frame) 154 1.2 matt { 155 1.4 cegger struct avic_softc * const avic = device_lookup_private(&avic_cd, 0); 156 1.2 matt struct pic_softc * const pic = &avic->avic_pic; 157 1.2 matt int32_t saved_nimask; 158 1.2 matt int32_t irq; 159 1.2 matt int ipl, newipl, oldipl; 160 1.2 matt 161 1.2 matt saved_nimask = INTC_READ(avic, IMX31_NIMASK); 162 1.2 matt for (;;) { 163 1.2 matt irq = INTC_READ(avic, IMX31_NIVECSR); 164 1.2 matt if (irq < 0) 165 1.2 matt break; 166 1.2 matt ipl = (int16_t) irq; 167 1.2 matt KASSERT(ipl >= 0); 168 1.2 matt irq >>= 16; 169 1.2 matt KASSERT(irq < 64); 170 1.2 matt KASSERT(pic->pic_sources[irq] != NULL); 171 1.2 matt 172 1.2 matt /* 173 1.2 matt * If this interrupt is not above the current spl, 174 1.2 matt * mark it as pending and try again. 175 1.2 matt */ 176 1.2 matt newipl = HW_TO_SW_IPL(ipl); 177 1.2 matt if (newipl <= curcpu()->ci_cpl) { 178 1.2 matt pic_mark_pending(pic, irq); 179 1.2 matt continue; 180 1.2 matt } 181 1.2 matt 182 1.2 matt /* 183 1.2 matt * Before enabling interrupts, mask out lower priority 184 1.2 matt * interrupts and raise SPL to its equivalent. 185 1.2 matt */ 186 1.2 matt 187 1.2 matt INTC_WRITE(avic, IMX31_NIMASK, ipl); 188 1.2 matt oldipl = _splraise(newipl); 189 1.2 matt cpsie(I32_bit); 190 1.2 matt 191 1.2 matt pic_dispatch(pic->pic_sources[irq], frame); 192 1.2 matt 193 1.2 matt /* 194 1.2 matt * Disable interrupts again. Drop SPL. Restore saved 195 1.2 matt * HW interrupt level. 196 1.2 matt */ 197 1.2 matt cpsid(I32_bit); 198 1.2 matt splx(oldipl); 199 1.2 matt INTC_WRITE(avic, IMX31_NIMASK, saved_nimask); 200 1.2 matt } 201 1.2 matt } 202 1.2 matt 203 1.2 matt static int avic_match(device_t, cfdata_t, void *); 204 1.2 matt static void avic_attach(device_t, device_t, void *); 205 1.2 matt 206 1.7 chs CFATTACH_DECL_NEW(avic, sizeof(struct avic_softc), 207 1.7 chs avic_match, avic_attach, NULL, NULL); 208 1.2 matt 209 1.2 matt int 210 1.2 matt avic_match(device_t parent, cfdata_t self, void *aux) 211 1.2 matt { 212 1.2 matt struct ahb_attach_args * const ahba = aux; 213 1.2 matt 214 1.2 matt if (ahba->ahba_addr != INTC_BASE) 215 1.2 matt return 0; 216 1.2 matt 217 1.2 matt return 1; 218 1.2 matt } 219 1.2 matt 220 1.2 matt void 221 1.2 matt avic_attach(device_t parent, device_t self, void *aux) 222 1.2 matt { 223 1.7 chs struct avic_softc * const avic = device_private(self); 224 1.2 matt struct ahb_attach_args * const ahba = aux; 225 1.2 matt int error; 226 1.2 matt 227 1.2 matt KASSERT(ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT); 228 1.8 riastrad KASSERT(device_unit(self) == 0); 229 1.2 matt 230 1.2 matt if (ahba->ahba_size == AHBCF_SIZE_DEFAULT) 231 1.2 matt ahba->ahba_size = INTC_SIZE; 232 1.2 matt 233 1.2 matt avic->avic_memt = ahba->ahba_memt; 234 1.2 matt error = bus_space_map(avic->avic_memt, ahba->ahba_addr, ahba->ahba_size, 235 1.2 matt 0, &avic->avic_memh); 236 1.2 matt if (error) 237 1.2 matt panic("avic_attach: failed to map register %#lx-%#lx: %d", 238 1.2 matt ahba->ahba_addr, ahba->ahba_addr + ahba->ahba_size - 1, 239 1.2 matt error); 240 1.2 matt 241 1.2 matt avic->avic_pic.pic_ops = &avic_pic_ops; 242 1.2 matt avic->avic_pic.pic_maxsources = 64; 243 1.7 chs strlcpy(avic->avic_pic.pic_name, device_xname(self), 244 1.2 matt sizeof(avic->avic_pic.pic_name)); 245 1.2 matt 246 1.2 matt pic_add(&avic->avic_pic, ahba->ahba_irqbase); 247 1.2 matt aprint_normal(": interrupts %d..%d\n", 248 1.2 matt ahba->ahba_irqbase, ahba->ahba_irqbase + 63); 249 1.2 matt #if 0 250 1.2 matt softintr_init(); 251 1.2 matt #endif 252 1.2 matt } 253