Home | History | Annotate | Line # | Download | only in alchemy
au_icu.c revision 1.16
      1 /*	$NetBSD: au_icu.c,v 1.16 2006/02/10 00:56:41 gdamore Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Garrett D'Amore for Itronix Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*-
     35  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     36  * All rights reserved.
     37  *
     38  * This code is derived from software contributed to The NetBSD Foundation
     39  * by Jason R. Thorpe.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. All advertising materials mentioning features or use of this software
     50  *    must display the following acknowledgement:
     51  *	This product includes software developed by the NetBSD
     52  *	Foundation, Inc. and its contributors.
     53  * 4. Neither the name of The NetBSD Foundation nor the names of its
     54  *    contributors may be used to endorse or promote products derived
     55  *    from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     58  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     59  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     60  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     61  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     62  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     63  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     64  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     65  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     66  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     67  * POSSIBILITY OF SUCH DAMAGE.
     68  */
     69 
     70 /*
     71  * Interrupt support for the Alchemy Semiconductor Au1x00 CPUs.
     72  *
     73  * The Alchemy Semiconductor Au1x00's interrupts are wired to two internal
     74  * interrupt controllers.
     75  */
     76 
     77 #include <sys/cdefs.h>
     78 __KERNEL_RCSID(0, "$NetBSD: au_icu.c,v 1.16 2006/02/10 00:56:41 gdamore Exp $");
     79 
     80 #include "opt_ddb.h"
     81 
     82 #include <sys/param.h>
     83 #include <sys/queue.h>
     84 #include <sys/malloc.h>
     85 #include <sys/systm.h>
     86 #include <sys/device.h>
     87 #include <sys/kernel.h>
     88 
     89 #include <machine/bus.h>
     90 #include <machine/intr.h>
     91 
     92 #include <mips/locore.h>
     93 #include <mips/alchemy/include/aureg.h>
     94 #include <mips/alchemy/include/auvar.h>
     95 
     96 #define	REGVAL(x)	*((volatile uint32_t *)(MIPS_PHYS_TO_KSEG1((x))))
     97 
     98 /*
     99  * This is a mask of bits to clear in the SR when we go to a
    100  * given hardware interrupt priority level.
    101  */
    102 
    103 const uint32_t ipl_sr_bits[_IPL_N] = {
    104 	0,					/*  0: IPL_NONE */
    105 
    106 	MIPS_SOFT_INT_MASK_0,			/*  1: IPL_SOFT */
    107 
    108 	MIPS_SOFT_INT_MASK_0,			/*  2: IPL_SOFTCLOCK */
    109 
    110 	MIPS_SOFT_INT_MASK_0,			/*  3: IPL_SOFTNET */
    111 
    112 	MIPS_SOFT_INT_MASK_0,			/*  4: IPL_SOFTSERIAL */
    113 
    114 	MIPS_SOFT_INT_MASK_0|
    115 		MIPS_SOFT_INT_MASK_1|
    116 		MIPS_INT_MASK_0,		/*  5: IPL_BIO */
    117 
    118 	MIPS_SOFT_INT_MASK_0|
    119 		MIPS_SOFT_INT_MASK_1|
    120 		MIPS_INT_MASK_0,		/*  6: IPL_NET */
    121 
    122 	MIPS_SOFT_INT_MASK_0|
    123 		MIPS_SOFT_INT_MASK_1|
    124 		MIPS_INT_MASK_0,		/*  7: IPL_{SERIAL,TTY} */
    125 
    126 	MIPS_SOFT_INT_MASK_0|
    127 		MIPS_SOFT_INT_MASK_1|
    128 		MIPS_INT_MASK_0|
    129 		MIPS_INT_MASK_1|
    130 		MIPS_INT_MASK_2|
    131 		MIPS_INT_MASK_3|
    132 		MIPS_INT_MASK_4|
    133 		MIPS_INT_MASK_5,		/*  8: IPL_{CLOCK,HIGH} */
    134 };
    135 
    136 /*
    137  * This is a mask of bits to clear in the SR when we go to a
    138  * given software interrupt priority level.
    139  * Hardware ipls are port/board specific.
    140  */
    141 const uint32_t mips_ipl_si_to_sr[_IPL_NSOFT] = {
    142 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFT */
    143 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTCLOCK */
    144 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTNET */
    145 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTSERIAL */
    146 };
    147 
    148 #define	NIRQS		64
    149 
    150 struct au_icu_intrhead {
    151 	struct evcnt intr_count;
    152 	int intr_refcnt;
    153 };
    154 struct au_icu_intrhead au_icu_intrtab[NIRQS];
    155 
    156 #define	NINTRS			4	/* MIPS INT0 - INT3 */
    157 
    158 struct au_intrhand {
    159 	LIST_ENTRY(au_intrhand) ih_q;
    160 	int (*ih_func)(void *);
    161 	void *ih_arg;
    162 	int ih_irq;
    163 };
    164 
    165 struct au_cpuintr {
    166 	LIST_HEAD(, au_intrhand) cintr_list;
    167 	struct evcnt cintr_count;
    168 };
    169 
    170 struct au_cpuintr au_cpuintrs[NINTRS];
    171 const char *au_cpuintrnames[NINTRS] = {
    172 	"icu 0, req 0",
    173 	"icu 0, req 1",
    174 	"icu 1, req 0",
    175 	"icu 1, req 1",
    176 };
    177 
    178 static bus_addr_t ic0_base, ic1_base;
    179 
    180 void
    181 au_intr_init(void)
    182 {
    183 	int			i;
    184 	struct au_chipdep	*chip;
    185 
    186 	for (i = 0; i < NINTRS; i++) {
    187 		LIST_INIT(&au_cpuintrs[i].cintr_list);
    188 		evcnt_attach_dynamic(&au_cpuintrs[i].cintr_count,
    189 		    EVCNT_TYPE_INTR, NULL, "mips", au_cpuintrnames[i]);
    190 	}
    191 
    192 	chip = au_chipdep();
    193 	KASSERT(chip != NULL);
    194 
    195 	ic0_base = chip->icus[0];
    196 	ic1_base = chip->icus[1];
    197 
    198 	for (i = 0; i < NIRQS; i++) {
    199 		au_icu_intrtab[i].intr_refcnt = 0;
    200 		evcnt_attach_dynamic(&au_icu_intrtab[i].intr_count,
    201 		    EVCNT_TYPE_INTR, NULL, chip->name, chip->irqnames[i]);
    202 	}
    203 }
    204 
    205 void *
    206 au_intr_establish(int irq, int req, int level, int type,
    207     int (*func)(void *), void *arg)
    208 {
    209 	struct au_intrhand	*ih;
    210 	uint32_t		icu_base;
    211 	int			cpu_int, s;
    212 	struct au_chipdep	*chip;
    213 
    214 	chip = au_chipdep();
    215 	KASSERT(chip != NULL);
    216 
    217 	if (irq >= NIRQS)
    218 		panic("au_intr_establish: bogus IRQ %d", irq);
    219 	if (req > 1)
    220 		panic("au_intr_establish: bogus request %d", req);
    221 
    222 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    223 	if (ih == NULL)
    224 		return (NULL);
    225 
    226 	ih->ih_func = func;
    227 	ih->ih_arg = arg;
    228 	ih->ih_irq = irq;
    229 
    230 	s = splhigh();
    231 
    232 	/*
    233 	 * First, link it into the tables.
    234 	 * XXX do we want a separate list (really, should only be one item, not
    235 	 *     a list anyway) per irq, not per CPU interrupt?
    236 	 */
    237 	cpu_int = (irq < 32 ? 0 : 2);
    238 	LIST_INSERT_HEAD(&au_cpuintrs[cpu_int].cintr_list, ih, ih_q);
    239 
    240 	/*
    241 	 * Now enable it.
    242 	 */
    243 	if (au_icu_intrtab[irq].intr_refcnt++ == 0) {
    244 		icu_base = (irq < 32) ? ic0_base : ic1_base;
    245 
    246 		irq &= 31;	/* throw away high bit if set */
    247 		irq = 1 << irq;	/* only used as a mask from here on */
    248 
    249 		/* XXX Only level interrupts for now */
    250 		switch (type) {
    251 		case IST_NONE:
    252 		case IST_PULSE:
    253 		case IST_EDGE:
    254 			panic("unsupported irq type %d", type);
    255 			/* NOTREACHED */
    256 		case IST_LEVEL:
    257 		case IST_LEVEL_HIGH:
    258 			REGVAL(icu_base + IC_CONFIG2_SET) = irq;
    259 			REGVAL(icu_base + IC_CONFIG1_CLEAR) = irq;
    260 			REGVAL(icu_base + IC_CONFIG0_SET) = irq;
    261 			break;
    262 		case IST_LEVEL_LOW:
    263 			REGVAL(icu_base + IC_CONFIG2_SET) = irq;
    264 			REGVAL(icu_base + IC_CONFIG1_SET) = irq;
    265 			REGVAL(icu_base + IC_CONFIG0_CLEAR) = irq;
    266 			break;
    267 		}
    268 
    269 		/* XXX handle GPIO interrupts - not done at all yet */
    270 		if (cpu_int & 0x1)
    271 			REGVAL(icu_base + IC_ASSIGN_REQUEST_CLEAR) = irq;
    272 		else
    273 			REGVAL(icu_base + IC_ASSIGN_REQUEST_SET) = irq;
    274 
    275 		/* Associate interrupt with peripheral */
    276 		REGVAL(icu_base + IC_SOURCE_SET) = irq;
    277 
    278 		/* Actually enable the interrupt */
    279 		REGVAL(icu_base + IC_MASK_SET) = irq;
    280 
    281 		/* And allow the interrupt to interrupt idle */
    282 		REGVAL(icu_base + IC_WAKEUP_SET) = irq;
    283 	}
    284 	splx(s);
    285 
    286 	return (ih);
    287 }
    288 
    289 void
    290 au_intr_disestablish(void *cookie)
    291 {
    292 	struct au_intrhand *ih = cookie;
    293 	uint32_t icu_base;
    294 	int irq, s;
    295 
    296 	irq = ih->ih_irq;
    297 
    298 	s = splhigh();
    299 
    300 	/*
    301 	 * First, remove it from the table.
    302 	 */
    303 	LIST_REMOVE(ih, ih_q);
    304 
    305 	/*
    306 	 * Now, disable it, if there is nothing remaining on the
    307 	 * list.
    308 	 */
    309 	if (au_icu_intrtab[irq].intr_refcnt-- == 1) {
    310 		icu_base = (irq < 32) ? ic0_base : ic1_base;
    311 
    312 		irq &= 31;	/* throw away high bit if set */
    313 		irq = 1 << irq;	/* only used as a mask from here on */
    314 
    315 		REGVAL(icu_base + IC_CONFIG2_CLEAR) = irq;
    316 		REGVAL(icu_base + IC_CONFIG1_CLEAR) = irq;
    317 		REGVAL(icu_base + IC_CONFIG0_CLEAR) = irq;
    318 
    319 		/* disable with MASK_CLEAR and WAKEUP_CLEAR */
    320 		REGVAL(icu_base + IC_MASK_CLEAR) = irq;
    321 		REGVAL(icu_base + IC_WAKEUP_CLEAR) = irq;
    322 	}
    323 
    324 	splx(s);
    325 
    326 	free(ih, M_DEVBUF);
    327 }
    328 
    329 void
    330 au_iointr(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipending)
    331 {
    332 	struct au_intrhand *ih;
    333 	int level;
    334 	uint32_t icu_base = 0, irqstat = 0, irqmask = 0; /* XXX gcc */
    335 
    336 	for (level = 3; level >= 0; level--) {
    337 		if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
    338 			continue;
    339 
    340 		/*
    341 		 * XXX	the following may well be slow to execute.
    342 		 *	investigate and possibly speed up.
    343 		 *
    344 		 * is something like:
    345 		 *
    346 		 *    irqmask = REGVAL(
    347 		 *	 (level & 4 == 0) ? IC0_BASE ? IC1_BASE +
    348 		 *	 (level & 2 == 0) ? IC_REQUEST0_INT : IC_REQUEST1_INT);
    349 		 *
    350 		 * be any better?
    351 		 *
    352 		 */
    353 		switch (level) {
    354 		case 0:
    355 			icu_base = ic0_base;
    356 			irqstat = REGVAL(icu_base + IC_REQUEST0_INT);
    357 			break;
    358 		case 1:
    359 			icu_base = ic0_base;
    360 			irqstat = REGVAL(icu_base + IC_REQUEST1_INT);
    361 			break;
    362 		case 2:
    363 			icu_base = ic1_base;
    364 			irqstat = REGVAL(icu_base + IC_REQUEST0_INT);
    365 			break;
    366 		case 3:
    367 			icu_base = ic1_base;
    368 			irqstat = REGVAL(icu_base + IC_REQUEST1_INT);
    369 			break;
    370 		}
    371 		irqmask = REGVAL(icu_base + IC_MASK_READ);
    372 		au_cpuintrs[level].cintr_count.ev_count++;
    373 		LIST_FOREACH(ih, &au_cpuintrs[level].cintr_list, ih_q) {
    374 			int irq = (1 << ih->ih_irq);
    375 
    376 			if ((irq & irqmask) && (irq & irqstat)) {
    377 				au_icu_intrtab[ih->ih_irq].intr_count.ev_count++;
    378 				(*ih->ih_func)(ih->ih_arg);
    379 
    380 				if (REGVAL(icu_base + IC_MASK_READ) & irq) {
    381 					REGVAL(icu_base + IC_MASK_CLEAR) = irq;
    382 					REGVAL(icu_base + IC_MASK_SET) = irq;
    383 				}
    384 			}
    385 		}
    386 		cause &= ~(MIPS_INT_MASK_0 << level);
    387 	}
    388 
    389 	/* Re-enable anything that we have processed. */
    390 	_splset(MIPS_SR_INT_IE | ((status & ~cause) & MIPS_HARD_INT_MASK));
    391 }
    392 
    393 /*
    394  * Some devices (e.g. PCMCIA) want to be able to mask interrupts at
    395  * the ICU, and leave them masked off until some later time
    396  * (e.g. reenabled by a soft interrupt).
    397  */
    398 
    399 void
    400 au_intr_enable(int irq)
    401 {
    402 	int		s;
    403 	uint32_t	icu_base, mask;
    404 
    405 	if (irq >= NIRQS)
    406 		panic("au_intr_enable: bogus IRQ %d", irq);
    407 
    408 	icu_base = (irq < 32) ? ic0_base : ic1_base;
    409 	mask = irq & 31;
    410 	mask = 1 << mask;
    411 
    412 	s = splhigh();
    413 	/* only enable the interrupt if we have a handler */
    414 	if (au_icu_intrtab[irq].intr_refcnt)
    415 		REGVAL(icu_base + IC_MASK_SET) = mask;
    416 	splx(s);
    417 }
    418 
    419 void
    420 au_intr_disable(int irq)
    421 {
    422 	int		s;
    423 	uint32_t	icu_base, mask;
    424 
    425 	icu_base = (irq < 32) ? ic0_base : ic1_base;
    426 	mask = irq & 31;
    427 	mask = 1 << mask;
    428 
    429 	if (irq >= NIRQS)
    430 		panic("au_intr_disable: bogus IRQ %d", irq);
    431 	s = splhigh();
    432 	REGVAL(icu_base + IC_MASK_CLEAR) = mask;
    433 	splx(s);
    434 }
    435