Home | History | Annotate | Line # | Download | only in ingenic
intr.c revision 1.4
      1 /*	$NetBSD: intr.c,v 1.4 2014/12/26 18:06:52 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.4 2014/12/26 18:06:52 macallan Exp $");
     31 
     32 #define __INTR_PRIVATE
     33 
     34 #include <sys/param.h>
     35 #include <sys/cpu.h>
     36 #include <sys/device.h>
     37 #include <sys/kernel.h>
     38 #include <sys/systm.h>
     39 #include <sys/timetc.h>
     40 #include <sys/bitops.h>
     41 
     42 #include <mips/locore.h>
     43 #include <machine/intr.h>
     44 
     45 #include <mips/ingenic/ingenic_regs.h>
     46 
     47 #include "opt_ingenic.h"
     48 
     49 extern void ingenic_clockintr(uint32_t);
     50 extern void ingenic_puts(const char *);
     51 
     52 /*
     53  * This is a mask of bits to clear in the SR when we go to a
     54  * given hardware interrupt priority level.
     55  */
     56 static const struct ipl_sr_map ingenic_ipl_sr_map = {
     57     .sr_bits = {
     58 	[IPL_NONE] =		0,
     59 	[IPL_SOFTCLOCK] =	MIPS_SOFT_INT_MASK_0,
     60 	[IPL_SOFTNET] =		MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1,
     61 	[IPL_VM] =
     62 	    MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 |
     63 	    MIPS_INT_MASK_0 |
     64 	    MIPS_INT_MASK_3 |
     65 	    MIPS_INT_MASK_4 |
     66 	    MIPS_INT_MASK_5,
     67 	[IPL_SCHED] =
     68 	    MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 |
     69 	    MIPS_INT_MASK_0 |
     70 	    MIPS_INT_MASK_1 |
     71 	    MIPS_INT_MASK_2 |
     72 	    MIPS_INT_MASK_3 |
     73 	    MIPS_INT_MASK_4 |
     74 	    MIPS_INT_MASK_5,
     75 	[IPL_DDB] =		MIPS_INT_MASK,
     76 	[IPL_HIGH] =            MIPS_INT_MASK,
     77     },
     78 };
     79 
     80 #define NINTR 64
     81 
     82 /* some timer channels share interrupts, couldn't find any others */
     83 struct intrhand {
     84 	struct evcnt ih_count;
     85 	char ih_name[16];
     86 	int (*ih_func)(void *);
     87 	void *ih_arg;
     88 	int ih_ipl;
     89 };
     90 
     91 struct intrhand intrs[NINTR];
     92 
     93 void ingenic_irq(int);
     94 
     95 void
     96 evbmips_intr_init(void)
     97 {
     98 	uint32_t reg;
     99 	int i;
    100 
    101 	ipl_sr_map = ingenic_ipl_sr_map;
    102 
    103 	/* zero all handlers */
    104 	for (i = 0; i < NINTR; i++) {
    105 		intrs[i].ih_func = NULL;
    106 		intrs[i].ih_arg = NULL;
    107 		snprintf(intrs[i].ih_name, sizeof(intrs[i].ih_name),
    108 		    "irq %d", i);
    109 		evcnt_attach_dynamic(&intrs[i].ih_count, EVCNT_TYPE_INTR,
    110 		    NULL, "PIC", intrs[i].ih_name);
    111 	}
    112 
    113 	/* mask all peripheral IRQs */
    114 	writereg(JZ_ICMR0, 0xffffffff);
    115 	writereg(JZ_ICMR1, 0xffffffff);
    116 
    117 	/* allow peripheral interrupts to core 0 only */
    118 	reg = MFC0(12, 4);	/* reset entry and interrupts */
    119 	reg &= 0xffff0000;
    120 	reg |= REIM_IRQ0_M | REIM_MIRQ0_M | REIM_MIRQ1_M;
    121 	MTC0(reg, 12, 4);
    122 }
    123 
    124 void
    125 evbmips_iointr(int ipl, vaddr_t pc, uint32_t ipending)
    126 {
    127 	uint32_t id;
    128 #ifdef INGENIC_INTR_DEBUG
    129 	char buffer[256];
    130 
    131 #if 0
    132 	snprintf(buffer, 256, "pending: %08x CR %08x\n", ipending,
    133 	    MFC0(MIPS_COP_0_CAUSE, 0));
    134 	ingenic_puts(buffer);
    135 #endif
    136 #endif
    137 	/* see which core we're on */
    138 	id = MFC0(15, 1) & 7;
    139 
    140 	/*
    141 	 * XXX
    142 	 * the manual counts the softint bits as INT0 and INT1, out headers
    143 	 * don't so everything here looks off by two
    144 	 */
    145 	if (ipending & MIPS_INT_MASK_1) {
    146 		/*
    147 		 * this is a mailbox interrupt / IPI
    148 		 * for now just print the message and clear it
    149 		 */
    150 		uint32_t reg;
    151 
    152 		/* read pending IPIs */
    153 		reg = MFC0(12, 3);
    154 		if (id == 0) {
    155 			if (reg & CS_MIRQ0_P) {
    156 
    157 #ifdef INGENIC_INTR_DEBUG
    158 				snprintf(buffer, 256,
    159 				    "IPI for core 0, msg %08x\n",
    160 				    MFC0(CP0_CORE_MBOX, 0));
    161 				ingenic_puts(buffer);
    162 #endif
    163 				reg &= (~CS_MIRQ0_P);
    164 				/* clear it */
    165 				MTC0(reg, 12, 3);
    166 			}
    167 		} else if (id == 1) {
    168 			if (reg & CS_MIRQ1_P) {
    169 #ifdef INGENIC_INTR_DEBUG
    170 				snprintf(buffer, 256,
    171 				    "IPI for core 1, msg %08x\n",
    172 				    MFC0(CP0_CORE_MBOX, 1));
    173 				ingenic_puts(buffer);
    174 #endif
    175 				reg &= ( 7 - CS_MIRQ1_P);
    176 				/* clear it */
    177 				MTC0(reg, 12, 3);
    178 			}
    179 		}
    180 	}
    181 	if (ipending & MIPS_INT_MASK_2) {
    182 		/* this is a timer interrupt */
    183 		ingenic_clockintr(id);
    184 		ingenic_puts("INT2\n");
    185 	}
    186 	if (ipending & MIPS_INT_MASK_0) {
    187 		/* peripheral interrupt */
    188 
    189 		/*
    190 		 * XXX
    191 		 * OS timer interrupts are supposed to show up as INT2 as well
    192 		 * but I haven't seen them there so for now we just weed them
    193 		 * out right here.
    194 		 * The idea is to allow peripheral interrupts on both cores but
    195 		 * block INT0 on core1 so it would see only timer interrupts
    196 		 * and IPIs. If that doesn't work we'll have to send an IPI to
    197 		 * core1 for each timer tick.
    198 		 */
    199 		if (readreg(JZ_ICPR0) & 0x0c000000) {
    200 			ingenic_clockintr(id);
    201 		}
    202 		ingenic_irq(ipl);
    203 		KASSERT(id == 0);
    204 	}
    205 }
    206 
    207 void
    208 ingenic_irq(int ipl)
    209 {
    210 	uint32_t irql, irqh, mask;
    211 	int bit, idx, bail;
    212 #ifdef INGENIC_INTR_DEBUG
    213 	char buffer[16];
    214 #endif
    215 
    216 	irql = readreg(JZ_ICPR0);
    217 #ifdef INGENIC_INTR_DEBUG
    218 	if (irql != 0) {
    219 		snprintf(buffer, 16, " il%08x", irql);
    220 		ingenic_puts(buffer);
    221 	}
    222 #endif
    223 	bail = 32;
    224 	bit = ffs32(irql);
    225 	while (bit != 0) {
    226 		idx = bit - 1;
    227 		mask = 1 << idx;
    228 		if (intrs[idx].ih_func != NULL) {
    229 			if (intrs[idx].ih_ipl == IPL_VM)
    230 				KERNEL_LOCK(1, NULL);
    231 			intrs[idx].ih_func(intrs[idx].ih_arg);
    232 			if (intrs[idx].ih_ipl == IPL_VM)
    233 				KERNEL_UNLOCK_ONE(NULL);
    234 			intrs[idx].ih_count.ev_count++;
    235 		} else {
    236 			/* spurious interrupt, mask it */
    237 			writereg(JZ_ICMSR0, mask);
    238 		}
    239 		irql &= ~mask;
    240 		bit = ffs32(irql);
    241 		bail--;
    242 		KASSERT(bail > 0);
    243 	}
    244 
    245 	irqh = readreg(JZ_ICPR1);
    246 #ifdef INGENIC_INTR_DEBUG
    247 	if (irqh != 0) {
    248 		snprintf(buffer, 16, " ih%08x", irqh);
    249 		ingenic_puts(buffer);
    250 	}
    251 #endif
    252 	bit = ffs32(irqh);
    253 	while (bit != 0) {
    254 		idx = bit - 1;
    255 		mask = 1 << idx;
    256 		idx += 32;
    257 		if (intrs[idx].ih_func != NULL) {
    258 			if (intrs[idx].ih_ipl == IPL_VM)
    259 				KERNEL_LOCK(1, NULL);
    260 			intrs[idx].ih_func(intrs[idx].ih_arg);
    261 			if (intrs[idx].ih_ipl == IPL_VM)
    262 				KERNEL_UNLOCK_ONE(NULL);
    263 			intrs[idx].ih_count.ev_count++;
    264 		} else {
    265 			/* spurious interrupt, mask it */
    266 			writereg(JZ_ICMSR1, mask);
    267 		}
    268 		irqh &= ~mask;
    269 		bit = ffs32(irqh);
    270 	}
    271 }
    272 
    273 void *
    274 evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
    275 {
    276 	int s;
    277 
    278 	if ((irq < 0) || (irq >= NINTR)) {
    279 		aprint_error("%s: invalid irq %d\n", __func__, irq);
    280 		return NULL;
    281 	}
    282 
    283 	s = splhigh();	/* XXX probably needs a mutex */
    284 	intrs[irq].ih_func = func;
    285 	intrs[irq].ih_arg = arg;
    286 	intrs[irq].ih_ipl = IPL_VM;
    287 
    288 	/* now enable the IRQ */
    289 	if (irq >= 32) {
    290 		writereg(JZ_ICMCR1, 1 << (irq - 32));
    291 	} else
    292 		writereg(JZ_ICMCR0, 1 << irq);
    293 
    294 	splx(s);
    295 
    296 	return ((void *)(irq + 1));
    297 }
    298 
    299 void
    300 evbmips_intr_disestablish(void *cookie)
    301 {
    302 	int irq = ((int)cookie) - 1;
    303 	int s;
    304 
    305 	if ((irq < 0) || (irq >= NINTR)) {
    306 		aprint_error("%s: invalid irq %d\n", __func__, irq);
    307 		return;
    308 	}
    309 
    310 	s = splhigh();
    311 
    312 	/* disable the IRQ */
    313 	if (irq >= 32) {
    314 		writereg(JZ_ICMSR1, 1 << (irq - 32));
    315 	} else
    316 		writereg(JZ_ICMSR0, 1 << irq);
    317 
    318 	intrs[irq].ih_func = NULL;
    319 	intrs[irq].ih_arg = NULL;
    320 	intrs[irq].ih_ipl = 0;
    321 
    322 	splx(s);
    323 }
    324