1 1.9 thorpej /* $NetBSD: adm5120_intr.c,v 1.9 2021/01/04 18:11:26 thorpej Exp $ */ 2 1.1 dyoung 3 1.1 dyoung /*- 4 1.1 dyoung * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko. 5 1.1 dyoung * All rights reserved. 6 1.1 dyoung * 7 1.1 dyoung * Redistribution and use in source and binary forms, with or 8 1.1 dyoung * without modification, are permitted provided that the following 9 1.1 dyoung * conditions are met: 10 1.1 dyoung * 1. Redistributions of source code must retain the above copyright 11 1.1 dyoung * notice, this list of conditions and the following disclaimer. 12 1.1 dyoung * 2. Redistributions in binary form must reproduce the above 13 1.1 dyoung * copyright notice, this list of conditions and the following 14 1.1 dyoung * disclaimer in the documentation and/or other materials provided 15 1.1 dyoung * with the distribution. 16 1.1 dyoung * 3. The names of the authors may not be used to endorse or promote 17 1.1 dyoung * products derived from this software without specific prior 18 1.1 dyoung * written permission. 19 1.1 dyoung * 20 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY 21 1.1 dyoung * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 1.1 dyoung * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 1.1 dyoung * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS 24 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 25 1.1 dyoung * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 1.1 dyoung * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 27 1.1 dyoung * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 1.1 dyoung * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 29 1.1 dyoung * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 1.1 dyoung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 1.1 dyoung * OF SUCH DAMAGE. 32 1.1 dyoung */ 33 1.1 dyoung /*- 34 1.1 dyoung * Copyright (c) 2001 The NetBSD Foundation, Inc. 35 1.1 dyoung * All rights reserved. 36 1.1 dyoung * 37 1.1 dyoung * This code is derived from software contributed to The NetBSD Foundation 38 1.1 dyoung * by Jason R. Thorpe. 39 1.1 dyoung * 40 1.1 dyoung * Redistribution and use in source and binary forms, with or without 41 1.1 dyoung * modification, are permitted provided that the following conditions 42 1.1 dyoung * are met: 43 1.1 dyoung * 1. Redistributions of source code must retain the above copyright 44 1.1 dyoung * notice, this list of conditions and the following disclaimer. 45 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright 46 1.1 dyoung * notice, this list of conditions and the following disclaimer in the 47 1.1 dyoung * documentation and/or other materials provided with the distribution. 48 1.1 dyoung * 49 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 1.1 dyoung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 1.1 dyoung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 1.1 dyoung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 1.1 dyoung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 1.1 dyoung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 1.1 dyoung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 1.1 dyoung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 1.1 dyoung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 1.1 dyoung * POSSIBILITY OF SUCH DAMAGE. 60 1.1 dyoung */ 61 1.1 dyoung 62 1.1 dyoung /* 63 1.1 dyoung * Platform-specific interrupt support for the Alchemy Semiconductor Pb1000. 64 1.1 dyoung * 65 1.1 dyoung * The Alchemy Semiconductor Pb1000's interrupts are wired to two internal 66 1.1 dyoung * interrupt controllers. 67 1.1 dyoung */ 68 1.1 dyoung 69 1.1 dyoung #include <sys/cdefs.h> 70 1.9 thorpej __KERNEL_RCSID(0, "$NetBSD: adm5120_intr.c,v 1.9 2021/01/04 18:11:26 thorpej Exp $"); 71 1.1 dyoung 72 1.1 dyoung #include "opt_ddb.h" 73 1.4 matt #define __INTR_PRIVATE 74 1.1 dyoung 75 1.1 dyoung #include <sys/param.h> 76 1.6 matt #include <sys/intr.h> 77 1.9 thorpej #include <sys/kmem.h> 78 1.1 dyoung 79 1.1 dyoung #include <mips/locore.h> 80 1.1 dyoung #include <mips/adm5120/include/adm5120reg.h> 81 1.1 dyoung #include <mips/adm5120/include/adm5120var.h> 82 1.1 dyoung 83 1.1 dyoung #include <dev/pci/pcireg.h> 84 1.1 dyoung #include <dev/pci/pcivar.h> 85 1.1 dyoung 86 1.1 dyoung /* 87 1.1 dyoung * This is a mask of bits to clear in the SR when we go to a 88 1.1 dyoung * given hardware interrupt priority level. 89 1.1 dyoung */ 90 1.4 matt static const struct ipl_sr_map adm5120_ipl_sr_map = { 91 1.4 matt .sr_bits = { 92 1.4 matt [IPL_NONE] = 0, 93 1.4 matt [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0, 94 1.4 matt [IPL_SOFTBIO] = MIPS_SOFT_INT_MASK_0, 95 1.4 matt [IPL_SOFTNET] = MIPS_SOFT_INT_MASK, 96 1.4 matt [IPL_SOFTSERIAL] = MIPS_SOFT_INT_MASK, 97 1.4 matt [IPL_VM] = MIPS_SOFT_INT_MASK|MIPS_INT_MASK_0, 98 1.4 matt [IPL_SCHED] = MIPS_INT_MASK, 99 1.4 matt [IPL_HIGH] = MIPS_INT_MASK, 100 1.4 matt }, 101 1.1 dyoung }; 102 1.1 dyoung 103 1.1 dyoung #define NIRQS 32 104 1.4 matt const char * const adm5120_intrnames[NIRQS] = { 105 1.1 dyoung "timer", /* 0 */ 106 1.1 dyoung "uart0", /* 1 */ 107 1.1 dyoung "uart1", /* 2 */ 108 1.1 dyoung "usb", /* 3 */ 109 1.1 dyoung "intx0/gpio2", /* 4 */ 110 1.1 dyoung "intx1/gpio4", /* 5 */ 111 1.1 dyoung "pci0", /* 6 */ 112 1.1 dyoung "pci1", /* 7 */ 113 1.1 dyoung "pci2", /* 8 */ 114 1.1 dyoung "switch",/* 9 */ 115 1.1 dyoung "res10", /* 10 */ 116 1.1 dyoung "res11", /* 11 */ 117 1.1 dyoung "res12", /* 12 */ 118 1.1 dyoung "res13", /* 13 */ 119 1.1 dyoung "res14", /* 14 */ 120 1.1 dyoung "res15", /* 15 */ 121 1.1 dyoung "res16", /* 16 */ 122 1.1 dyoung "res17", /* 17 */ 123 1.1 dyoung "res18", /* 18 */ 124 1.1 dyoung "res19", /* 19 */ 125 1.1 dyoung "res20", /* 20 */ 126 1.1 dyoung "res21", /* 21 */ 127 1.1 dyoung "res22", /* 22 */ 128 1.1 dyoung "res23", /* 23 */ 129 1.1 dyoung "res24", /* 24 */ 130 1.1 dyoung "res25", /* 25 */ 131 1.1 dyoung "res26", /* 26 */ 132 1.1 dyoung "res27", /* 27 */ 133 1.1 dyoung "res28", /* 28 */ 134 1.1 dyoung "res29", /* 29 */ 135 1.1 dyoung "res30", /* 30 */ 136 1.1 dyoung "res31", /* 31 */ 137 1.1 dyoung }; 138 1.1 dyoung 139 1.1 dyoung struct adm5120_intrhead { 140 1.1 dyoung struct evcnt intr_count; 141 1.1 dyoung int intr_refcnt; 142 1.1 dyoung }; 143 1.1 dyoung struct adm5120_intrhead adm5120_intrtab[NIRQS]; 144 1.1 dyoung 145 1.1 dyoung 146 1.1 dyoung #define NINTRS 2 /* MIPS INT0 - INT1 */ 147 1.1 dyoung struct adm5120_cpuintr { 148 1.1 dyoung LIST_HEAD(, evbmips_intrhand) cintr_list; 149 1.1 dyoung struct evcnt cintr_count; 150 1.1 dyoung }; 151 1.1 dyoung struct adm5120_cpuintr adm5120_cpuintrs[NINTRS]; 152 1.1 dyoung 153 1.4 matt const char * const adm5120_cpuintrnames[NINTRS] = { 154 1.1 dyoung "int 0 (irq)", 155 1.1 dyoung "int 1 (fiq)", 156 1.1 dyoung }; 157 1.1 dyoung 158 1.1 dyoung #define REG_READ(o) *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(ADM5120_BASE_ICU + (o))) 159 1.1 dyoung #define REG_WRITE(o,v) (REG_READ(o)) = (v) 160 1.1 dyoung 161 1.1 dyoung void 162 1.1 dyoung evbmips_intr_init(void) 163 1.1 dyoung { 164 1.4 matt ipl_sr_map = adm5120_ipl_sr_map; 165 1.1 dyoung 166 1.4 matt for (size_t i = 0; i < NINTRS; i++) { 167 1.1 dyoung LIST_INIT(&adm5120_cpuintrs[i].cintr_list); 168 1.1 dyoung evcnt_attach_dynamic(&adm5120_cpuintrs[i].cintr_count, 169 1.1 dyoung EVCNT_TYPE_INTR, NULL, "mips", adm5120_cpuintrnames[i]); 170 1.1 dyoung } 171 1.1 dyoung 172 1.4 matt for (size_t i = 0; i < NIRQS; i++) { 173 1.1 dyoung /* XXX steering - use an irqmap array? */ 174 1.1 dyoung 175 1.1 dyoung adm5120_intrtab[i].intr_refcnt = 0; 176 1.1 dyoung evcnt_attach_dynamic(&adm5120_intrtab[i].intr_count, 177 1.1 dyoung EVCNT_TYPE_INTR, NULL, "adm5120", adm5120_intrnames[i]); 178 1.1 dyoung } 179 1.1 dyoung 180 1.1 dyoung /* disable all interrupts */ 181 1.1 dyoung REG_WRITE(ICU_DISABLE_REG, ICU_INT_MASK); 182 1.1 dyoung } 183 1.1 dyoung 184 1.1 dyoung void * 185 1.1 dyoung adm5120_intr_establish(int irq, int priority, int (*func)(void *), void *arg) 186 1.1 dyoung { 187 1.1 dyoung struct evbmips_intrhand *ih; 188 1.1 dyoung uint32_t irqmask; 189 1.1 dyoung int cpu_int, s; 190 1.1 dyoung 191 1.1 dyoung if (irq < 0 || irq >= NIRQS) 192 1.1 dyoung panic("adm5120_intr_establish: bogus IRQ %d", irq); 193 1.1 dyoung 194 1.9 thorpej ih = kmem_alloc(sizeof(*ih), KM_SLEEP); 195 1.1 dyoung ih->ih_func = func; 196 1.1 dyoung ih->ih_arg = arg; 197 1.1 dyoung ih->ih_irq = irq; 198 1.1 dyoung 199 1.1 dyoung s = splhigh(); 200 1.1 dyoung 201 1.1 dyoung /* 202 1.1 dyoung * First, link it into the tables. 203 1.1 dyoung * XXX do we want a separate list (really, should only be one item, not 204 1.1 dyoung * a list anyway) per irq, not per CPU interrupt? 205 1.1 dyoung */ 206 1.1 dyoung 207 1.1 dyoung cpu_int = (priority == INTR_FIQ) ? 1 : 0; 208 1.1 dyoung 209 1.1 dyoung LIST_INSERT_HEAD(&adm5120_cpuintrs[cpu_int].cintr_list, ih, ih_q); 210 1.1 dyoung 211 1.1 dyoung /* 212 1.1 dyoung * Now enable it. 213 1.1 dyoung */ 214 1.1 dyoung if (adm5120_intrtab[irq].intr_refcnt++ == 0) { 215 1.1 dyoung irqmask = 1 << irq; 216 1.1 dyoung 217 1.1 dyoung /* configure as IRQ or FIQ */ 218 1.1 dyoung if (priority == INTR_FIQ) { 219 1.1 dyoung REG_WRITE(ICU_MODE_REG, 220 1.1 dyoung REG_READ(ICU_MODE_REG) | irqmask); 221 1.1 dyoung } else { 222 1.1 dyoung REG_WRITE(ICU_MODE_REG, 223 1.1 dyoung REG_READ(ICU_MODE_REG) & ~irqmask); 224 1.1 dyoung } 225 1.1 dyoung /* enable */ 226 1.1 dyoung REG_WRITE(ICU_ENABLE_REG, irqmask); 227 1.1 dyoung } 228 1.1 dyoung splx(s); 229 1.1 dyoung 230 1.1 dyoung return ih; 231 1.1 dyoung } 232 1.1 dyoung 233 1.1 dyoung void 234 1.1 dyoung adm5120_intr_disestablish(void *cookie) 235 1.1 dyoung { 236 1.1 dyoung struct evbmips_intrhand *ih = cookie; 237 1.1 dyoung int irq, s; 238 1.1 dyoung uint32_t irqmask; 239 1.1 dyoung 240 1.1 dyoung irq = ih->ih_irq; 241 1.1 dyoung 242 1.1 dyoung s = splhigh(); 243 1.1 dyoung 244 1.1 dyoung /* 245 1.1 dyoung * First, remove it from the table. 246 1.1 dyoung */ 247 1.1 dyoung LIST_REMOVE(ih, ih_q); 248 1.1 dyoung 249 1.1 dyoung /* 250 1.1 dyoung * Now, disable it, if there is nothing remaining on the 251 1.1 dyoung * list. 252 1.1 dyoung */ 253 1.1 dyoung if (adm5120_intrtab[irq].intr_refcnt-- == 1) { 254 1.1 dyoung irqmask = 1 << irq; /* only used as a mask from here on */ 255 1.1 dyoung 256 1.1 dyoung /* disable this irq in HW */ 257 1.1 dyoung REG_WRITE(ICU_DISABLE_REG, irqmask); 258 1.1 dyoung } 259 1.1 dyoung 260 1.1 dyoung splx(s); 261 1.1 dyoung 262 1.9 thorpej kmem_free(ih, sizeof(*ih)); 263 1.1 dyoung } 264 1.1 dyoung void 265 1.7 skrll evbmips_iointr(int ipl, uint32_t ipending, struct clockframe *cf) 266 1.1 dyoung { 267 1.1 dyoung struct evbmips_intrhand *ih; 268 1.1 dyoung uint32_t irqmask, irqstat; 269 1.1 dyoung 270 1.4 matt for (int level = NINTRS - 1; level >= 0; level--) { 271 1.1 dyoung if ((ipending & (MIPS_INT_MASK_0 << level)) == 0) 272 1.1 dyoung continue; 273 1.1 dyoung 274 1.1 dyoung if (level) 275 1.1 dyoung irqstat = REG_READ(ICU_FIQ_STATUS_REG); 276 1.1 dyoung else 277 1.1 dyoung irqstat = REG_READ(ICU_STATUS_REG); 278 1.1 dyoung 279 1.1 dyoung adm5120_cpuintrs[level].cintr_count.ev_count++; 280 1.1 dyoung LIST_FOREACH(ih, &adm5120_cpuintrs[level].cintr_list, ih_q) { 281 1.1 dyoung irqmask = 1 << ih->ih_irq; 282 1.1 dyoung if (irqmask & irqstat) { 283 1.1 dyoung adm5120_intrtab[ih->ih_irq].intr_count.ev_count++; 284 1.1 dyoung (*ih->ih_func)(ih->ih_arg); 285 1.1 dyoung } 286 1.1 dyoung } 287 1.1 dyoung } 288 1.1 dyoung } 289