Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2410_intr.c revision 1.11.8.1
      1  1.11.8.1   rmind /* $NetBSD: s3c2410_intr.c,v 1.11.8.1 2011/03/05 20:49:39 rmind Exp $ */
      2       1.1     bsh 
      3       1.1     bsh /*
      4       1.1     bsh  * Copyright (c) 2003  Genetec corporation.  All rights reserved.
      5       1.1     bsh  * Written by Hiroyuki Bessho for Genetec corporation.
      6       1.1     bsh  *
      7       1.1     bsh  * Redistribution and use in source and binary forms, with or without
      8       1.1     bsh  * modification, are permitted provided that the following conditions
      9       1.1     bsh  * are met:
     10       1.1     bsh  * 1. Redistributions of source code must retain the above copyright
     11       1.1     bsh  *    notice, this list of conditions and the following disclaimer.
     12       1.1     bsh  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1     bsh  *    notice, this list of conditions and the following disclaimer in the
     14       1.1     bsh  *    documentation and/or other materials provided with the distribution.
     15       1.1     bsh  * 3. The name of Genetec corporation may not be used to endorse
     16       1.1     bsh  *    or promote products derived from this software without specific prior
     17       1.1     bsh  *    written permission.
     18       1.1     bsh  *
     19       1.1     bsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
     20       1.1     bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORP.
     23       1.1     bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     bsh  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     bsh  */
     31       1.1     bsh 
     32       1.1     bsh /*
     33       1.1     bsh  * IRQ handler for Samsung S3C2410 processor.
     34       1.1     bsh  * It has integrated interrupt controller.
     35       1.1     bsh  */
     36       1.1     bsh 
     37       1.1     bsh #include <sys/cdefs.h>
     38  1.11.8.1   rmind __KERNEL_RCSID(0, "$NetBSD: s3c2410_intr.c,v 1.11.8.1 2011/03/05 20:49:39 rmind Exp $");
     39       1.1     bsh 
     40       1.1     bsh #include <sys/param.h>
     41       1.1     bsh #include <sys/systm.h>
     42       1.1     bsh #include <sys/malloc.h>
     43  1.11.8.1   rmind 
     44       1.1     bsh #include <machine/bus.h>
     45       1.1     bsh #include <machine/intr.h>
     46  1.11.8.1   rmind 
     47       1.1     bsh #include <arm/cpufunc.h>
     48       1.1     bsh 
     49       1.1     bsh #include <arm/s3c2xx0/s3c2410reg.h>
     50       1.1     bsh #include <arm/s3c2xx0/s3c2410var.h>
     51       1.1     bsh 
     52       1.1     bsh /*
     53       1.1     bsh  * interrupt dispatch table.
     54       1.1     bsh  */
     55       1.1     bsh 
     56       1.1     bsh struct s3c2xx0_intr_dispatch handler[ICU_LEN];
     57       1.1     bsh 
     58       1.1     bsh 
     59       1.6   perry volatile int intr_mask;
     60       1.7    matt #ifdef __HAVE_FAST_SOFTINTS
     61       1.7    matt volatile int softint_pending;
     62       1.6   perry volatile int soft_intr_mask;
     63       1.7    matt #endif
     64       1.6   perry volatile int global_intr_mask = 0; /* mask some interrupts at all spl level */
     65       1.1     bsh 
     66       1.1     bsh /* interrupt masks for each level */
     67       1.1     bsh int s3c2xx0_imask[NIPL];
     68       1.1     bsh int s3c2xx0_ilevel[ICU_LEN];
     69       1.7    matt #ifdef __HAVE_FAST_SOFTINTS
     70       1.1     bsh int s3c24x0_soft_imask[NIPL];
     71       1.7    matt #endif
     72       1.1     bsh 
     73       1.1     bsh vaddr_t intctl_base;		/* interrupt controller registers */
     74       1.1     bsh #define icreg(offset) \
     75       1.1     bsh 	(*(volatile uint32_t *)(intctl_base+(offset)))
     76       1.1     bsh 
     77       1.7    matt #ifdef __HAVE_FAST_SOFTINTS
     78       1.1     bsh /*
     79       1.1     bsh  * Map a software interrupt queue to an interrupt priority level.
     80       1.1     bsh  */
     81       1.7    matt static const int si_to_ipl[] = {
     82       1.7    matt 	[SI_SOFTBIO]	= IPL_SOFTBIO,
     83       1.7    matt 	[SI_SOFTCLOCK]	= IPL_SOFTCLOCK,
     84       1.7    matt 	[SI_SOFTNET]	= IPL_SOFTNET,
     85       1.7    matt 	[SI_SOFTSERIAL] = IPL_SOFTSERIAL,
     86       1.1     bsh };
     87       1.7    matt #endif
     88       1.1     bsh 
     89       1.2     bsh #define PENDING_CLEAR_MASK	(~0)
     90       1.1     bsh 
     91       1.1     bsh /*
     92       1.1     bsh  * called from irq_entry.
     93       1.1     bsh  */
     94       1.1     bsh void s3c2410_irq_handler(struct clockframe *);
     95       1.1     bsh void
     96       1.1     bsh s3c2410_irq_handler(struct clockframe *frame)
     97       1.1     bsh {
     98       1.1     bsh 	uint32_t irqbits;
     99       1.1     bsh 	int irqno;
    100       1.1     bsh 	int saved_spl_level;
    101       1.1     bsh 
    102       1.9    matt 	saved_spl_level = curcpl();
    103       1.1     bsh 
    104       1.2     bsh #ifdef	DIAGNOSTIC
    105       1.9    matt 	if (curcpu()->ci_intr_depth > 10)
    106       1.2     bsh 		panic("nested intr too deep");
    107       1.2     bsh #endif
    108       1.2     bsh 
    109       1.1     bsh 	while ((irqbits = icreg(INTCTL_INTPND)) != 0) {
    110       1.1     bsh 
    111       1.2     bsh 		/* Note: Only one bit in INTPND register is set */
    112       1.1     bsh 
    113       1.2     bsh 		irqno = icreg(INTCTL_INTOFFSET);
    114       1.1     bsh 
    115       1.2     bsh #ifdef	DIAGNOSTIC
    116       1.2     bsh 		if (__predict_false((irqbits & (1<<irqno)) == 0)) {
    117       1.2     bsh 			/* This shouldn't happen */
    118       1.2     bsh 			printf("INTOFFSET=%d, INTPND=%x\n", irqno, irqbits);
    119       1.1     bsh 			break;
    120       1.2     bsh 		}
    121       1.2     bsh #endif
    122       1.1     bsh 		/* raise spl to stop interrupts of lower priorities */
    123       1.1     bsh 		if (saved_spl_level < handler[irqno].level)
    124       1.1     bsh 			s3c2xx0_setipl(handler[irqno].level);
    125       1.1     bsh 
    126       1.1     bsh 		/* clear pending bit */
    127       1.1     bsh 		icreg(INTCTL_SRCPND) = PENDING_CLEAR_MASK & (1 << irqno);
    128       1.2     bsh 		icreg(INTCTL_INTPND) = PENDING_CLEAR_MASK & (1 << irqno);
    129       1.1     bsh 
    130       1.1     bsh 		enable_interrupts(I32_bit); /* allow nested interrupts */
    131       1.1     bsh 
    132       1.1     bsh 		(*handler[irqno].func) (
    133       1.1     bsh 		    handler[irqno].cookie == 0
    134       1.1     bsh 		    ? frame : handler[irqno].cookie);
    135       1.1     bsh 
    136       1.1     bsh 		disable_interrupts(I32_bit);
    137       1.1     bsh 
    138       1.1     bsh 		/* restore spl to that was when this interrupt happen */
    139       1.1     bsh 		s3c2xx0_setipl(saved_spl_level);
    140       1.2     bsh 
    141       1.1     bsh 	}
    142       1.1     bsh 
    143       1.7    matt #ifdef __HAVE_FAST_SOFTINTS
    144       1.9    matt 	cpu_dosoftints();
    145       1.7    matt #endif
    146       1.1     bsh }
    147       1.1     bsh 
    148       1.1     bsh /*
    149       1.1     bsh  * Handler for main IRQ of cascaded interrupts.
    150       1.1     bsh  */
    151       1.1     bsh static int
    152       1.1     bsh cascade_irq_handler(void *cookie)
    153       1.1     bsh {
    154       1.2     bsh 	int index = (int)cookie - 1;
    155       1.1     bsh 	uint32_t irqbits;
    156       1.1     bsh 	int irqno, i;
    157       1.1     bsh 	int save = disable_interrupts(I32_bit);
    158       1.1     bsh 
    159       1.2     bsh 	KASSERT(0 <= index && index <= 3);
    160       1.2     bsh 
    161       1.2     bsh 	irqbits = icreg(INTCTL_SUBSRCPND) &
    162       1.2     bsh 	    ~icreg(INTCTL_INTSUBMSK) & (0x07 << (3*index));
    163       1.1     bsh 
    164       1.1     bsh 	for (irqno = 3*index; irqbits; ++irqno) {
    165       1.1     bsh 		if ((irqbits & (1<<irqno)) == 0)
    166       1.1     bsh 			continue;
    167       1.1     bsh 
    168       1.1     bsh 		/* clear pending bit */
    169       1.2     bsh 		irqbits &= ~(1<<irqno);
    170       1.2     bsh 		icreg(INTCTL_SUBSRCPND) = (1 << irqno);
    171       1.1     bsh 
    172       1.1     bsh 		/* allow nested interrupts. SPL is already set
    173       1.1     bsh 		 * correctly by main handler. */
    174       1.1     bsh 		restore_interrupts(save);
    175       1.1     bsh 
    176       1.1     bsh 		i = S3C2410_SUBIRQ_MIN + irqno;
    177       1.1     bsh 		(* handler[i].func)(handler[i].cookie);
    178       1.1     bsh 
    179       1.1     bsh 		disable_interrupts(I32_bit);
    180       1.1     bsh 	}
    181       1.1     bsh 
    182       1.1     bsh 	return 1;
    183       1.1     bsh }
    184       1.1     bsh 
    185       1.1     bsh 
    186       1.2     bsh static const uint8_t subirq_to_main[] = {
    187       1.1     bsh 	S3C2410_INT_UART0,
    188       1.1     bsh 	S3C2410_INT_UART0,
    189       1.1     bsh 	S3C2410_INT_UART0,
    190       1.1     bsh 	S3C2410_INT_UART1,
    191       1.1     bsh 	S3C2410_INT_UART1,
    192       1.1     bsh 	S3C2410_INT_UART1,
    193       1.1     bsh 	S3C2410_INT_UART2,
    194       1.1     bsh 	S3C2410_INT_UART2,
    195       1.1     bsh 	S3C2410_INT_UART2,
    196       1.1     bsh 	S3C24X0_INT_ADCTC,
    197       1.1     bsh 	S3C24X0_INT_ADCTC,
    198       1.1     bsh };
    199       1.1     bsh 
    200       1.1     bsh void *
    201       1.1     bsh s3c24x0_intr_establish(int irqno, int level, int type,
    202       1.1     bsh     int (* func) (void *), void *cookie)
    203       1.1     bsh {
    204       1.1     bsh 	int save;
    205       1.1     bsh 
    206       1.1     bsh 	if (irqno < 0 || irqno >= ICU_LEN ||
    207       1.1     bsh 	    type < IST_NONE || IST_EDGE_BOTH < type)
    208       1.1     bsh 		panic("intr_establish: bogus irq or type");
    209       1.1     bsh 
    210       1.1     bsh 	save = disable_interrupts(I32_bit);
    211       1.1     bsh 
    212       1.1     bsh 	handler[irqno].cookie = cookie;
    213       1.1     bsh 	handler[irqno].func = func;
    214       1.1     bsh 	handler[irqno].level = level;
    215       1.1     bsh 
    216       1.1     bsh 	if (irqno >= S3C2410_SUBIRQ_MIN) {
    217       1.1     bsh 		/* cascaded interrupts. */
    218       1.1     bsh 		int main_irqno;
    219       1.2     bsh 		int i = (irqno - S3C2410_SUBIRQ_MIN);
    220       1.1     bsh 
    221       1.2     bsh 		main_irqno = subirq_to_main[i];
    222       1.1     bsh 
    223       1.2     bsh 		/* establish main irq if first time
    224       1.2     bsh 		 * be careful that cookie shouldn't be 0 */
    225       1.1     bsh 		if (handler[main_irqno].func != cascade_irq_handler)
    226       1.1     bsh 			s3c24x0_intr_establish(main_irqno, level, type,
    227       1.2     bsh 			    cascade_irq_handler, (void *)((i/3) + 1));
    228       1.2     bsh 
    229       1.2     bsh 		/* unmask it in submask register */
    230       1.2     bsh 		icreg(INTCTL_INTSUBMSK) &= ~(1<<i);
    231       1.1     bsh 
    232       1.2     bsh 		restore_interrupts(save);
    233       1.1     bsh 		return &handler[irqno];
    234       1.1     bsh 	}
    235       1.1     bsh 
    236       1.1     bsh 	s3c2xx0_update_intr_masks(irqno, level);
    237       1.1     bsh 
    238       1.2     bsh 	/*
    239       1.2     bsh 	 * set trigger type for external interrupts 0..3
    240       1.2     bsh 	 */
    241       1.1     bsh 	if (irqno <= S3C24X0_INT_EXT(3)) {
    242       1.1     bsh 		/*
    243       1.1     bsh 		 * Update external interrupt control
    244       1.1     bsh 		 */
    245       1.3     bsh 		s3c2410_setup_extint(irqno, type);
    246       1.1     bsh 	}
    247       1.1     bsh 
    248       1.9    matt 	s3c2xx0_setipl(curcpl());
    249       1.1     bsh 
    250       1.1     bsh 	restore_interrupts(save);
    251       1.1     bsh 
    252       1.1     bsh 	return &handler[irqno];
    253       1.1     bsh }
    254       1.1     bsh 
    255       1.1     bsh 
    256       1.1     bsh static void
    257       1.1     bsh init_interrupt_masks(void)
    258       1.1     bsh {
    259       1.1     bsh 	int i;
    260       1.1     bsh 
    261       1.1     bsh 	for (i=0; i < NIPL; ++i)
    262       1.1     bsh 		s3c2xx0_imask[i] = 0;
    263       1.1     bsh 
    264       1.7    matt #ifdef __HAVE_FAST_SOFTINTS
    265       1.1     bsh 	s3c24x0_soft_imask[IPL_NONE] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    266       1.1     bsh 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK) |
    267       1.1     bsh 		SI_TO_IRQBIT(SI_SOFT);
    268       1.1     bsh 
    269       1.1     bsh 	s3c24x0_soft_imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    270       1.1     bsh 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK);
    271       1.1     bsh 
    272       1.1     bsh 	/*
    273       1.1     bsh 	 * splsoftclock() is the only interface that users of the
    274       1.1     bsh 	 * generic software interrupt facility have to block their
    275       1.1     bsh 	 * soft intrs, so splsoftclock() must also block IPL_SOFT.
    276       1.1     bsh 	 */
    277       1.1     bsh 	s3c24x0_soft_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    278       1.1     bsh 		SI_TO_IRQBIT(SI_SOFTNET);
    279       1.1     bsh 
    280       1.1     bsh 	/*
    281       1.1     bsh 	 * splsoftnet() must also block splsoftclock(), since we don't
    282       1.1     bsh 	 * want timer-driven network events to occur while we're
    283       1.1     bsh 	 * processing incoming packets.
    284       1.1     bsh 	 */
    285       1.1     bsh 	s3c24x0_soft_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    286       1.1     bsh 
    287       1.1     bsh 	for (i = IPL_BIO; i < IPL_SOFTSERIAL; ++i)
    288       1.1     bsh 		s3c24x0_soft_imask[i] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    289       1.7    matt #endif
    290       1.1     bsh }
    291       1.1     bsh 
    292       1.1     bsh void
    293       1.1     bsh s3c2410_intr_init(struct s3c24x0_softc *sc)
    294       1.1     bsh {
    295       1.1     bsh 	intctl_base = (vaddr_t) bus_space_vaddr(sc->sc_sx.sc_iot,
    296       1.1     bsh 	    sc->sc_sx.sc_intctl_ioh);
    297       1.1     bsh 
    298       1.1     bsh 	s3c2xx0_intr_mask_reg = (uint32_t *)(intctl_base + INTCTL_INTMSK);
    299       1.1     bsh 
    300       1.1     bsh 	/* clear all pending interrupt */
    301       1.2     bsh 	icreg(INTCTL_SRCPND) = ~0;
    302       1.2     bsh 	icreg(INTCTL_INTPND) = ~0;
    303       1.2     bsh 
    304       1.2     bsh 	/* mask all sub interrupts */
    305       1.2     bsh 	icreg(INTCTL_INTSUBMSK) = 0x7ff;
    306       1.1     bsh 
    307       1.1     bsh 	init_interrupt_masks();
    308       1.1     bsh 
    309       1.1     bsh 	s3c2xx0_intr_init(handler, ICU_LEN);
    310       1.1     bsh 
    311       1.1     bsh }
    312       1.1     bsh 
    313       1.1     bsh 
    314       1.1     bsh /*
    315       1.1     bsh  * mask/unmask sub interrupts
    316       1.1     bsh  */
    317       1.1     bsh void
    318       1.1     bsh s3c2410_mask_subinterrupts(int bits)
    319       1.1     bsh {
    320      1.10    matt 	int psw = disable_interrupts(IF32_bits);
    321      1.11  dogcow 	icreg(INTCTL_INTSUBMSK) |= bits;
    322      1.10    matt 	restore_interrupts(psw);
    323       1.1     bsh }
    324       1.1     bsh 
    325       1.1     bsh void
    326       1.1     bsh s3c2410_unmask_subinterrupts(int bits)
    327       1.1     bsh {
    328      1.10    matt 	int psw = disable_interrupts(IF32_bits);
    329      1.10    matt 	icreg(INTCTL_INTSUBMSK) &= ~bits;
    330      1.10    matt 	restore_interrupts(psw);
    331       1.3     bsh }
    332       1.3     bsh 
    333       1.3     bsh /*
    334       1.3     bsh  * Update external interrupt control
    335       1.3     bsh  */
    336       1.3     bsh static const u_char s3c24x0_ist[] = {
    337       1.3     bsh 	EXTINTR_LOW,		/* NONE */
    338       1.3     bsh 	EXTINTR_FALLING,	/* PULSE */
    339       1.3     bsh 	EXTINTR_FALLING,	/* EDGE */
    340       1.3     bsh 	EXTINTR_LOW,		/* LEVEL */
    341       1.3     bsh 	EXTINTR_HIGH,
    342       1.3     bsh 	EXTINTR_RISING,
    343       1.3     bsh 	EXTINTR_BOTH,
    344       1.3     bsh };
    345       1.3     bsh 
    346       1.3     bsh void
    347       1.3     bsh s3c2410_setup_extint(int extint, int type)
    348       1.3     bsh {
    349       1.3     bsh         uint32_t reg;
    350       1.3     bsh         u_int   trig;
    351       1.3     bsh         int     i = extint % 8;
    352       1.3     bsh         int     regidx = extint/8;      /* GPIO_EXTINT[0:2] */
    353       1.3     bsh 	int	save;
    354       1.3     bsh 
    355       1.3     bsh         trig = s3c24x0_ist[type];
    356       1.3     bsh 
    357       1.3     bsh 	save = disable_interrupts(I32_bit);
    358       1.3     bsh 
    359       1.3     bsh         reg = bus_space_read_4(s3c2xx0_softc->sc_iot,
    360       1.3     bsh             s3c2xx0_softc->sc_gpio_ioh,
    361       1.3     bsh             GPIO_EXTINT(regidx));
    362       1.3     bsh 
    363       1.3     bsh         reg = reg & ~(0x07 << (4*i));
    364       1.3     bsh         reg |= trig << (4*i);
    365       1.3     bsh 
    366       1.3     bsh         bus_space_write_4(s3c2xx0_softc->sc_iot, s3c2xx0_softc->sc_gpio_ioh,
    367       1.3     bsh             GPIO_EXTINT(regidx), reg);
    368       1.3     bsh 
    369       1.3     bsh 	restore_interrupts(save);
    370       1.1     bsh }
    371