Home | History | Annotate | Line # | Download | only in adm5120
adm5120_intr.c revision 1.1
      1  1.1  dyoung /*	$NetBSD: adm5120_intr.c,v 1.1 2007/03/20 08:52:03 dyoung 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  * 3. All advertising materials mentioning features or use of this software
     49  1.1  dyoung  *    must display the following acknowledgement:
     50  1.1  dyoung  *	This product includes software developed by the NetBSD
     51  1.1  dyoung  *	Foundation, Inc. and its contributors.
     52  1.1  dyoung  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  1.1  dyoung  *    contributors may be used to endorse or promote products derived
     54  1.1  dyoung  *    from this software without specific prior written permission.
     55  1.1  dyoung  *
     56  1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  1.1  dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  1.1  dyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  1.1  dyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  1.1  dyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  1.1  dyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  1.1  dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  1.1  dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  1.1  dyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  1.1  dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  1.1  dyoung  * POSSIBILITY OF SUCH DAMAGE.
     67  1.1  dyoung  */
     68  1.1  dyoung 
     69  1.1  dyoung /*
     70  1.1  dyoung  * Platform-specific interrupt support for the Alchemy Semiconductor Pb1000.
     71  1.1  dyoung  *
     72  1.1  dyoung  * The Alchemy Semiconductor Pb1000's interrupts are wired to two internal
     73  1.1  dyoung  * interrupt controllers.
     74  1.1  dyoung  */
     75  1.1  dyoung 
     76  1.1  dyoung #include <sys/cdefs.h>
     77  1.1  dyoung __KERNEL_RCSID(0, "$NetBSD: adm5120_intr.c,v 1.1 2007/03/20 08:52:03 dyoung Exp $");
     78  1.1  dyoung 
     79  1.1  dyoung #include "opt_ddb.h"
     80  1.1  dyoung 
     81  1.1  dyoung #include <sys/param.h>
     82  1.1  dyoung #include <sys/queue.h>
     83  1.1  dyoung #include <sys/malloc.h>
     84  1.1  dyoung #include <sys/systm.h>
     85  1.1  dyoung #include <sys/device.h>
     86  1.1  dyoung #include <sys/kernel.h>
     87  1.1  dyoung 
     88  1.1  dyoung #include <machine/bus.h>
     89  1.1  dyoung #include <machine/intr.h>
     90  1.1  dyoung 
     91  1.1  dyoung #include <mips/locore.h>
     92  1.1  dyoung #include <mips/adm5120/include/adm5120reg.h>
     93  1.1  dyoung #include <mips/adm5120/include/adm5120var.h>
     94  1.1  dyoung 
     95  1.1  dyoung #include <dev/pci/pcireg.h>
     96  1.1  dyoung #include <dev/pci/pcivar.h>
     97  1.1  dyoung 
     98  1.1  dyoung /*
     99  1.1  dyoung  * This is a mask of bits to clear in the SR when we go to a
    100  1.1  dyoung  * given hardware interrupt priority level.
    101  1.1  dyoung  */
    102  1.1  dyoung const uint32_t ipl_sr_bits[_IPL_N] = {
    103  1.1  dyoung 	0,					/*  0: IPL_NONE */
    104  1.1  dyoung 
    105  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/*  1: IPL_SOFT */
    106  1.1  dyoung 
    107  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/*  2: IPL_SOFTCLOCK */
    108  1.1  dyoung 
    109  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/*  3: IPL_SOFTNET */
    110  1.1  dyoung 
    111  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/*  4: IPL_SOFTSERIAL */
    112  1.1  dyoung 
    113  1.1  dyoung 	MIPS_SOFT_INT_MASK_0|
    114  1.1  dyoung 	MIPS_SOFT_INT_MASK_1|
    115  1.1  dyoung 	MIPS_INT_MASK_0,			/*  5: IPL_BIO */
    116  1.1  dyoung 
    117  1.1  dyoung 	MIPS_SOFT_INT_MASK_0|
    118  1.1  dyoung 	MIPS_SOFT_INT_MASK_1|
    119  1.1  dyoung 	MIPS_INT_MASK_0,			/*  6: IPL_NET */
    120  1.1  dyoung 
    121  1.1  dyoung 	MIPS_SOFT_INT_MASK_0|
    122  1.1  dyoung 	MIPS_SOFT_INT_MASK_1|
    123  1.1  dyoung 	MIPS_INT_MASK_0|
    124  1.1  dyoung 	MIPS_INT_MASK_1,			/*  7: IPL_{SERIAL,TTY} */
    125  1.1  dyoung 
    126  1.1  dyoung 	MIPS_SOFT_INT_MASK_0|
    127  1.1  dyoung 	MIPS_SOFT_INT_MASK_1|
    128  1.1  dyoung 	MIPS_INT_MASK_0|
    129  1.1  dyoung 	MIPS_INT_MASK_1|
    130  1.1  dyoung 	MIPS_INT_MASK_2|
    131  1.1  dyoung 	MIPS_INT_MASK_3|
    132  1.1  dyoung 	MIPS_INT_MASK_4|
    133  1.1  dyoung 	MIPS_INT_MASK_5,			/*  8: IPL_{CLOCK,HIGH} */
    134  1.1  dyoung };
    135  1.1  dyoung 
    136  1.1  dyoung /*
    137  1.1  dyoung  * This is a mask of bits to clear in the SR when we go to a
    138  1.1  dyoung  * given software interrupt priority level.
    139  1.1  dyoung  * Hardware ipls are port/board specific.
    140  1.1  dyoung  */
    141  1.1  dyoung const uint32_t mips_ipl_si_to_sr[SI_NQUEUES] = {
    142  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFT */
    143  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTCLOCK */
    144  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTNET */
    145  1.1  dyoung 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTSERIAL */
    146  1.1  dyoung };
    147  1.1  dyoung 
    148  1.1  dyoung #define	NIRQS		32
    149  1.1  dyoung const char *adm5120_intrnames[NIRQS] = {
    150  1.1  dyoung 	"timer", /*  0 */
    151  1.1  dyoung 	"uart0", /*  1 */
    152  1.1  dyoung 	"uart1", /*  2 */
    153  1.1  dyoung 	"usb",   /*  3 */
    154  1.1  dyoung 	"intx0/gpio2", /*  4 */
    155  1.1  dyoung 	"intx1/gpio4", /*  5 */
    156  1.1  dyoung 	"pci0",  /*  6 */
    157  1.1  dyoung 	"pci1",  /*  7 */
    158  1.1  dyoung 	"pci2",  /*  8 */
    159  1.1  dyoung 	"switch",/*  9 */
    160  1.1  dyoung 	"res10", /* 10 */
    161  1.1  dyoung 	"res11", /* 11 */
    162  1.1  dyoung 	"res12", /* 12 */
    163  1.1  dyoung 	"res13", /* 13 */
    164  1.1  dyoung 	"res14", /* 14 */
    165  1.1  dyoung 	"res15", /* 15 */
    166  1.1  dyoung 	"res16", /* 16 */
    167  1.1  dyoung 	"res17", /* 17 */
    168  1.1  dyoung 	"res18", /* 18 */
    169  1.1  dyoung 	"res19", /* 19 */
    170  1.1  dyoung 	"res20", /* 20 */
    171  1.1  dyoung 	"res21", /* 21 */
    172  1.1  dyoung 	"res22", /* 22 */
    173  1.1  dyoung 	"res23", /* 23 */
    174  1.1  dyoung 	"res24", /* 24 */
    175  1.1  dyoung 	"res25", /* 25 */
    176  1.1  dyoung 	"res26", /* 26 */
    177  1.1  dyoung 	"res27", /* 27 */
    178  1.1  dyoung 	"res28", /* 28 */
    179  1.1  dyoung 	"res29", /* 29 */
    180  1.1  dyoung 	"res30", /* 30 */
    181  1.1  dyoung 	"res31", /* 31 */
    182  1.1  dyoung };
    183  1.1  dyoung 
    184  1.1  dyoung struct adm5120_intrhead {
    185  1.1  dyoung 	struct evcnt intr_count;
    186  1.1  dyoung 	int intr_refcnt;
    187  1.1  dyoung };
    188  1.1  dyoung struct adm5120_intrhead adm5120_intrtab[NIRQS];
    189  1.1  dyoung 
    190  1.1  dyoung 
    191  1.1  dyoung #define	NINTRS			2	/* MIPS INT0 - INT1 */
    192  1.1  dyoung struct adm5120_cpuintr {
    193  1.1  dyoung 	LIST_HEAD(, evbmips_intrhand) cintr_list;
    194  1.1  dyoung 	struct evcnt cintr_count;
    195  1.1  dyoung };
    196  1.1  dyoung struct adm5120_cpuintr adm5120_cpuintrs[NINTRS];
    197  1.1  dyoung 
    198  1.1  dyoung const char *adm5120_cpuintrnames[NINTRS] = {
    199  1.1  dyoung 	"int 0 (irq)",
    200  1.1  dyoung 	"int 1 (fiq)",
    201  1.1  dyoung };
    202  1.1  dyoung 
    203  1.1  dyoung #define REG_READ(o) *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(ADM5120_BASE_ICU + (o)))
    204  1.1  dyoung #define REG_WRITE(o,v) (REG_READ(o)) = (v)
    205  1.1  dyoung 
    206  1.1  dyoung void
    207  1.1  dyoung evbmips_intr_init(void)
    208  1.1  dyoung {
    209  1.1  dyoung 	int i;
    210  1.1  dyoung 
    211  1.1  dyoung 	for (i = 0; i < NINTRS; i++) {
    212  1.1  dyoung 		LIST_INIT(&adm5120_cpuintrs[i].cintr_list);
    213  1.1  dyoung 		evcnt_attach_dynamic(&adm5120_cpuintrs[i].cintr_count,
    214  1.1  dyoung 		    EVCNT_TYPE_INTR, NULL, "mips", adm5120_cpuintrnames[i]);
    215  1.1  dyoung 	}
    216  1.1  dyoung 
    217  1.1  dyoung 	for (i = 0; i < NIRQS; i++) {
    218  1.1  dyoung 		/* XXX steering - use an irqmap array? */
    219  1.1  dyoung 
    220  1.1  dyoung 		adm5120_intrtab[i].intr_refcnt = 0;
    221  1.1  dyoung 		evcnt_attach_dynamic(&adm5120_intrtab[i].intr_count,
    222  1.1  dyoung 		    EVCNT_TYPE_INTR, NULL, "adm5120", adm5120_intrnames[i]);
    223  1.1  dyoung 	}
    224  1.1  dyoung 
    225  1.1  dyoung 	/* disable all interrupts */
    226  1.1  dyoung 	REG_WRITE(ICU_DISABLE_REG, ICU_INT_MASK);
    227  1.1  dyoung }
    228  1.1  dyoung 
    229  1.1  dyoung void *
    230  1.1  dyoung adm5120_intr_establish(int irq, int priority, int (*func)(void *), void *arg)
    231  1.1  dyoung {
    232  1.1  dyoung 	struct evbmips_intrhand *ih;
    233  1.1  dyoung 	uint32_t irqmask;
    234  1.1  dyoung 	int	cpu_int, s;
    235  1.1  dyoung 
    236  1.1  dyoung 	if (irq < 0 || irq >= NIRQS)
    237  1.1  dyoung 		panic("adm5120_intr_establish: bogus IRQ %d", irq);
    238  1.1  dyoung 
    239  1.1  dyoung 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    240  1.1  dyoung 	if (ih == NULL)
    241  1.1  dyoung 		return NULL;
    242  1.1  dyoung 
    243  1.1  dyoung 	ih->ih_func = func;
    244  1.1  dyoung 	ih->ih_arg = arg;
    245  1.1  dyoung 	ih->ih_irq = irq;
    246  1.1  dyoung 
    247  1.1  dyoung 	s = splhigh();
    248  1.1  dyoung 
    249  1.1  dyoung 	/*
    250  1.1  dyoung 	 * First, link it into the tables.
    251  1.1  dyoung 	 * XXX do we want a separate list (really, should only be one item, not
    252  1.1  dyoung 	 *     a list anyway) per irq, not per CPU interrupt?
    253  1.1  dyoung 	 */
    254  1.1  dyoung 
    255  1.1  dyoung 	cpu_int = (priority == INTR_FIQ) ? 1 : 0;
    256  1.1  dyoung 
    257  1.1  dyoung 	LIST_INSERT_HEAD(&adm5120_cpuintrs[cpu_int].cintr_list, ih, ih_q);
    258  1.1  dyoung 
    259  1.1  dyoung 	/*
    260  1.1  dyoung 	 * Now enable it.
    261  1.1  dyoung 	 */
    262  1.1  dyoung 	if (adm5120_intrtab[irq].intr_refcnt++ == 0) {
    263  1.1  dyoung 		irqmask = 1 << irq;
    264  1.1  dyoung 
    265  1.1  dyoung 		/* configure as IRQ or FIQ */
    266  1.1  dyoung 		if (priority == INTR_FIQ) {
    267  1.1  dyoung 			REG_WRITE(ICU_MODE_REG,
    268  1.1  dyoung 			    REG_READ(ICU_MODE_REG) | irqmask);
    269  1.1  dyoung 		} else {
    270  1.1  dyoung 			REG_WRITE(ICU_MODE_REG,
    271  1.1  dyoung 			    REG_READ(ICU_MODE_REG) & ~irqmask);
    272  1.1  dyoung 		}
    273  1.1  dyoung 		/* enable */
    274  1.1  dyoung 		REG_WRITE(ICU_ENABLE_REG, irqmask);
    275  1.1  dyoung 	}
    276  1.1  dyoung 	splx(s);
    277  1.1  dyoung 
    278  1.1  dyoung 	return ih;
    279  1.1  dyoung }
    280  1.1  dyoung 
    281  1.1  dyoung void
    282  1.1  dyoung adm5120_intr_disestablish(void *cookie)
    283  1.1  dyoung {
    284  1.1  dyoung 	struct evbmips_intrhand *ih = cookie;
    285  1.1  dyoung 	int irq, s;
    286  1.1  dyoung 	uint32_t irqmask;
    287  1.1  dyoung 
    288  1.1  dyoung 	irq = ih->ih_irq;
    289  1.1  dyoung 
    290  1.1  dyoung 	s = splhigh();
    291  1.1  dyoung 
    292  1.1  dyoung 	/*
    293  1.1  dyoung 	 * First, remove it from the table.
    294  1.1  dyoung 	 */
    295  1.1  dyoung 	LIST_REMOVE(ih, ih_q);
    296  1.1  dyoung 
    297  1.1  dyoung 	/*
    298  1.1  dyoung 	 * Now, disable it, if there is nothing remaining on the
    299  1.1  dyoung 	 * list.
    300  1.1  dyoung 	 */
    301  1.1  dyoung 	if (adm5120_intrtab[irq].intr_refcnt-- == 1) {
    302  1.1  dyoung 		irqmask = 1 << irq;	/* only used as a mask from here on */
    303  1.1  dyoung 
    304  1.1  dyoung 		/* disable this irq in HW */
    305  1.1  dyoung 		REG_WRITE(ICU_DISABLE_REG, irqmask);
    306  1.1  dyoung 	}
    307  1.1  dyoung 
    308  1.1  dyoung 	splx(s);
    309  1.1  dyoung 
    310  1.1  dyoung 	free(ih, M_DEVBUF);
    311  1.1  dyoung }
    312  1.1  dyoung void
    313  1.1  dyoung evbmips_iointr(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipending)
    314  1.1  dyoung {
    315  1.1  dyoung 	struct evbmips_intrhand *ih;
    316  1.1  dyoung 	int level;
    317  1.1  dyoung 	uint32_t irqmask, irqstat;
    318  1.1  dyoung 
    319  1.1  dyoung 	for (level = NINTRS - 1; level >= 0; level--) {
    320  1.1  dyoung 		if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
    321  1.1  dyoung 			continue;
    322  1.1  dyoung 
    323  1.1  dyoung 		if (level)
    324  1.1  dyoung 			irqstat = REG_READ(ICU_FIQ_STATUS_REG);
    325  1.1  dyoung 		else
    326  1.1  dyoung 			irqstat = REG_READ(ICU_STATUS_REG);
    327  1.1  dyoung 
    328  1.1  dyoung 		adm5120_cpuintrs[level].cintr_count.ev_count++;
    329  1.1  dyoung 		LIST_FOREACH(ih, &adm5120_cpuintrs[level].cintr_list, ih_q) {
    330  1.1  dyoung 			irqmask = 1 << ih->ih_irq;
    331  1.1  dyoung 			if (irqmask & irqstat) {
    332  1.1  dyoung 				adm5120_intrtab[ih->ih_irq].intr_count.ev_count++;
    333  1.1  dyoung 				(*ih->ih_func)(ih->ih_arg);
    334  1.1  dyoung 			}
    335  1.1  dyoung 		}
    336  1.1  dyoung 		cause &= ~(MIPS_INT_MASK_0 << level);
    337  1.1  dyoung 	}
    338  1.1  dyoung 
    339  1.1  dyoung 	/* Re-enable anything that we have processed. */
    340  1.1  dyoung 	_splset(MIPS_SR_INT_IE | ((status & ~cause) & MIPS_HARD_INT_MASK));
    341  1.1  dyoung 
    342  1.1  dyoung 	return;
    343  1.1  dyoung }
    344