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