Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2410_intr.c revision 1.6.58.1
      1  1.6.58.1    mjf /* $NetBSD: s3c2410_intr.c,v 1.6.58.1 2008/02/18 21:04:23 mjf 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.6.58.1    mjf __KERNEL_RCSID(0, "$NetBSD: s3c2410_intr.c,v 1.6.58.1 2008/02/18 21:04:23 mjf 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.1    bsh #include <uvm/uvm_extern.h>
     44       1.1    bsh #include <machine/bus.h>
     45       1.1    bsh #include <machine/intr.h>
     46       1.1    bsh #include <arm/cpufunc.h>
     47       1.1    bsh 
     48       1.1    bsh #include <arm/s3c2xx0/s3c2410reg.h>
     49       1.1    bsh #include <arm/s3c2xx0/s3c2410var.h>
     50       1.1    bsh 
     51       1.1    bsh /*
     52       1.1    bsh  * interrupt dispatch table.
     53       1.1    bsh  */
     54       1.1    bsh 
     55       1.1    bsh struct s3c2xx0_intr_dispatch handler[ICU_LEN];
     56       1.1    bsh 
     57       1.1    bsh 
     58       1.6  perry volatile int current_spl_level;
     59       1.6  perry volatile int intr_mask;
     60  1.6.58.1    mjf #ifdef __HAVE_FAST_SOFTINTS
     61  1.6.58.1    mjf volatile int softint_pending;
     62       1.6  perry volatile int soft_intr_mask;
     63  1.6.58.1    mjf #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.6.58.1    mjf #ifdef __HAVE_FAST_SOFTINTS
     70       1.1    bsh int s3c24x0_soft_imask[NIPL];
     71  1.6.58.1    mjf #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.6.58.1    mjf #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.6.58.1    mjf static const int si_to_ipl[] = {
     82  1.6.58.1    mjf 	[SI_SOFTBIO]	= IPL_SOFTBIO,
     83  1.6.58.1    mjf 	[SI_SOFTCLOCK]	= IPL_SOFTCLOCK,
     84  1.6.58.1    mjf 	[SI_SOFTNET]	= IPL_SOFTNET,
     85  1.6.58.1    mjf 	[SI_SOFTSERIAL] = IPL_SOFTSERIAL,
     86       1.1    bsh };
     87  1.6.58.1    mjf #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.1    bsh 	saved_spl_level = current_spl_level;
    103       1.1    bsh 
    104       1.2    bsh #ifdef	DIAGNOSTIC
    105  1.6.58.1    mjf 	if (curcpu()->ci_idepth > 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.6.58.1    mjf #ifdef __HAVE_FAST_SOFTINTS
    144       1.1    bsh 	if (get_pending_softint())
    145       1.1    bsh 		s3c2xx0_do_pending(1);
    146  1.6.58.1    mjf #endif
    147       1.1    bsh }
    148       1.1    bsh 
    149       1.1    bsh /*
    150       1.1    bsh  * Handler for main IRQ of cascaded interrupts.
    151       1.1    bsh  */
    152       1.1    bsh static int
    153       1.1    bsh cascade_irq_handler(void *cookie)
    154       1.1    bsh {
    155       1.2    bsh 	int index = (int)cookie - 1;
    156       1.1    bsh 	uint32_t irqbits;
    157       1.1    bsh 	int irqno, i;
    158       1.1    bsh 	int save = disable_interrupts(I32_bit);
    159       1.1    bsh 
    160       1.2    bsh 	KASSERT(0 <= index && index <= 3);
    161       1.2    bsh 
    162       1.2    bsh 	irqbits = icreg(INTCTL_SUBSRCPND) &
    163       1.2    bsh 	    ~icreg(INTCTL_INTSUBMSK) & (0x07 << (3*index));
    164       1.1    bsh 
    165       1.1    bsh 	for (irqno = 3*index; irqbits; ++irqno) {
    166       1.1    bsh 		if ((irqbits & (1<<irqno)) == 0)
    167       1.1    bsh 			continue;
    168       1.1    bsh 
    169       1.1    bsh 		/* clear pending bit */
    170       1.2    bsh 		irqbits &= ~(1<<irqno);
    171       1.2    bsh 		icreg(INTCTL_SUBSRCPND) = (1 << irqno);
    172       1.1    bsh 
    173       1.1    bsh 		/* allow nested interrupts. SPL is already set
    174       1.1    bsh 		 * correctly by main handler. */
    175       1.1    bsh 		restore_interrupts(save);
    176       1.1    bsh 
    177       1.1    bsh 		i = S3C2410_SUBIRQ_MIN + irqno;
    178       1.1    bsh 		(* handler[i].func)(handler[i].cookie);
    179       1.1    bsh 
    180       1.1    bsh 		disable_interrupts(I32_bit);
    181       1.1    bsh 	}
    182       1.1    bsh 
    183       1.1    bsh 	return 1;
    184       1.1    bsh }
    185       1.1    bsh 
    186       1.1    bsh 
    187       1.2    bsh static const uint8_t subirq_to_main[] = {
    188       1.1    bsh 	S3C2410_INT_UART0,
    189       1.1    bsh 	S3C2410_INT_UART0,
    190       1.1    bsh 	S3C2410_INT_UART0,
    191       1.1    bsh 	S3C2410_INT_UART1,
    192       1.1    bsh 	S3C2410_INT_UART1,
    193       1.1    bsh 	S3C2410_INT_UART1,
    194       1.1    bsh 	S3C2410_INT_UART2,
    195       1.1    bsh 	S3C2410_INT_UART2,
    196       1.1    bsh 	S3C2410_INT_UART2,
    197       1.1    bsh 	S3C24X0_INT_ADCTC,
    198       1.1    bsh 	S3C24X0_INT_ADCTC,
    199       1.1    bsh };
    200       1.1    bsh 
    201       1.1    bsh void *
    202       1.1    bsh s3c24x0_intr_establish(int irqno, int level, int type,
    203       1.1    bsh     int (* func) (void *), void *cookie)
    204       1.1    bsh {
    205       1.1    bsh 	int save;
    206       1.1    bsh 
    207       1.1    bsh 	if (irqno < 0 || irqno >= ICU_LEN ||
    208       1.1    bsh 	    type < IST_NONE || IST_EDGE_BOTH < type)
    209       1.1    bsh 		panic("intr_establish: bogus irq or type");
    210       1.1    bsh 
    211       1.1    bsh 	save = disable_interrupts(I32_bit);
    212       1.1    bsh 
    213       1.1    bsh 	handler[irqno].cookie = cookie;
    214       1.1    bsh 	handler[irqno].func = func;
    215       1.1    bsh 	handler[irqno].level = level;
    216       1.1    bsh 
    217       1.1    bsh 	if (irqno >= S3C2410_SUBIRQ_MIN) {
    218       1.1    bsh 		/* cascaded interrupts. */
    219       1.1    bsh 		int main_irqno;
    220       1.2    bsh 		int i = (irqno - S3C2410_SUBIRQ_MIN);
    221       1.1    bsh 
    222       1.2    bsh 		main_irqno = subirq_to_main[i];
    223       1.1    bsh 
    224       1.2    bsh 		/* establish main irq if first time
    225       1.2    bsh 		 * be careful that cookie shouldn't be 0 */
    226       1.1    bsh 		if (handler[main_irqno].func != cascade_irq_handler)
    227       1.1    bsh 			s3c24x0_intr_establish(main_irqno, level, type,
    228       1.2    bsh 			    cascade_irq_handler, (void *)((i/3) + 1));
    229       1.2    bsh 
    230       1.2    bsh 		/* unmask it in submask register */
    231       1.2    bsh 		icreg(INTCTL_INTSUBMSK) &= ~(1<<i);
    232       1.1    bsh 
    233       1.2    bsh 		restore_interrupts(save);
    234       1.1    bsh 		return &handler[irqno];
    235       1.1    bsh 	}
    236       1.1    bsh 
    237       1.1    bsh 	s3c2xx0_update_intr_masks(irqno, level);
    238       1.1    bsh 
    239       1.2    bsh 	/*
    240       1.2    bsh 	 * set trigger type for external interrupts 0..3
    241       1.2    bsh 	 */
    242       1.1    bsh 	if (irqno <= S3C24X0_INT_EXT(3)) {
    243       1.1    bsh 		/*
    244       1.1    bsh 		 * Update external interrupt control
    245       1.1    bsh 		 */
    246       1.3    bsh 		s3c2410_setup_extint(irqno, type);
    247       1.1    bsh 	}
    248       1.1    bsh 
    249       1.1    bsh 	s3c2xx0_setipl(current_spl_level);
    250       1.1    bsh 
    251       1.1    bsh 	restore_interrupts(save);
    252       1.1    bsh 
    253       1.1    bsh 	return &handler[irqno];
    254       1.1    bsh }
    255       1.1    bsh 
    256       1.1    bsh 
    257       1.1    bsh static void
    258       1.1    bsh init_interrupt_masks(void)
    259       1.1    bsh {
    260       1.1    bsh 	int i;
    261       1.1    bsh 
    262       1.1    bsh 	for (i=0; i < NIPL; ++i)
    263       1.1    bsh 		s3c2xx0_imask[i] = 0;
    264       1.1    bsh 
    265  1.6.58.1    mjf #ifdef __HAVE_FAST_SOFTINTS
    266       1.1    bsh 	s3c24x0_soft_imask[IPL_NONE] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    267       1.1    bsh 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK) |
    268       1.1    bsh 		SI_TO_IRQBIT(SI_SOFT);
    269       1.1    bsh 
    270       1.1    bsh 	s3c24x0_soft_imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    271       1.1    bsh 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK);
    272       1.1    bsh 
    273       1.1    bsh 	/*
    274       1.1    bsh 	 * splsoftclock() is the only interface that users of the
    275       1.1    bsh 	 * generic software interrupt facility have to block their
    276       1.1    bsh 	 * soft intrs, so splsoftclock() must also block IPL_SOFT.
    277       1.1    bsh 	 */
    278       1.1    bsh 	s3c24x0_soft_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    279       1.1    bsh 		SI_TO_IRQBIT(SI_SOFTNET);
    280       1.1    bsh 
    281       1.1    bsh 	/*
    282       1.1    bsh 	 * splsoftnet() must also block splsoftclock(), since we don't
    283       1.1    bsh 	 * want timer-driven network events to occur while we're
    284       1.1    bsh 	 * processing incoming packets.
    285       1.1    bsh 	 */
    286       1.1    bsh 	s3c24x0_soft_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    287       1.1    bsh 
    288       1.1    bsh 	for (i = IPL_BIO; i < IPL_SOFTSERIAL; ++i)
    289       1.1    bsh 		s3c24x0_soft_imask[i] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    290  1.6.58.1    mjf #endif
    291       1.1    bsh }
    292       1.1    bsh 
    293       1.1    bsh void
    294       1.1    bsh s3c2410_intr_init(struct s3c24x0_softc *sc)
    295       1.1    bsh {
    296       1.1    bsh 	intctl_base = (vaddr_t) bus_space_vaddr(sc->sc_sx.sc_iot,
    297       1.1    bsh 	    sc->sc_sx.sc_intctl_ioh);
    298       1.1    bsh 
    299       1.1    bsh 	s3c2xx0_intr_mask_reg = (uint32_t *)(intctl_base + INTCTL_INTMSK);
    300       1.1    bsh 
    301       1.1    bsh 	/* clear all pending interrupt */
    302       1.2    bsh 	icreg(INTCTL_SRCPND) = ~0;
    303       1.2    bsh 	icreg(INTCTL_INTPND) = ~0;
    304       1.2    bsh 
    305       1.2    bsh 	/* mask all sub interrupts */
    306       1.2    bsh 	icreg(INTCTL_INTSUBMSK) = 0x7ff;
    307       1.1    bsh 
    308       1.1    bsh 	init_interrupt_masks();
    309       1.1    bsh 
    310       1.1    bsh 	s3c2xx0_intr_init(handler, ICU_LEN);
    311       1.1    bsh 
    312       1.1    bsh }
    313       1.1    bsh 
    314       1.1    bsh 
    315       1.1    bsh /*
    316       1.1    bsh  * mask/unmask sub interrupts
    317       1.1    bsh  */
    318       1.1    bsh void
    319       1.1    bsh s3c2410_mask_subinterrupts(int bits)
    320       1.1    bsh {
    321       1.4     he 	atomic_set_bit((uint32_t *)__UNVOLATILE(&icreg(INTCTL_INTSUBMSK)),
    322       1.4     he 		bits);
    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.4     he 	atomic_clear_bit((uint32_t *)__UNVOLATILE(&icreg(INTCTL_INTSUBMSK)),
    329       1.4     he 		bits);
    330       1.3    bsh }
    331       1.3    bsh 
    332       1.3    bsh /*
    333       1.3    bsh  * Update external interrupt control
    334       1.3    bsh  */
    335       1.3    bsh static const u_char s3c24x0_ist[] = {
    336       1.3    bsh 	EXTINTR_LOW,		/* NONE */
    337       1.3    bsh 	EXTINTR_FALLING,	/* PULSE */
    338       1.3    bsh 	EXTINTR_FALLING,	/* EDGE */
    339       1.3    bsh 	EXTINTR_LOW,		/* LEVEL */
    340       1.3    bsh 	EXTINTR_HIGH,
    341       1.3    bsh 	EXTINTR_RISING,
    342       1.3    bsh 	EXTINTR_BOTH,
    343       1.3    bsh };
    344       1.3    bsh 
    345       1.3    bsh void
    346       1.3    bsh s3c2410_setup_extint(int extint, int type)
    347       1.3    bsh {
    348       1.3    bsh         uint32_t reg;
    349       1.3    bsh         u_int   trig;
    350       1.3    bsh         int     i = extint % 8;
    351       1.3    bsh         int     regidx = extint/8;      /* GPIO_EXTINT[0:2] */
    352       1.3    bsh 	int	save;
    353       1.3    bsh 
    354       1.3    bsh         trig = s3c24x0_ist[type];
    355       1.3    bsh 
    356       1.3    bsh 	save = disable_interrupts(I32_bit);
    357       1.3    bsh 
    358       1.3    bsh         reg = bus_space_read_4(s3c2xx0_softc->sc_iot,
    359       1.3    bsh             s3c2xx0_softc->sc_gpio_ioh,
    360       1.3    bsh             GPIO_EXTINT(regidx));
    361       1.3    bsh 
    362       1.3    bsh         reg = reg & ~(0x07 << (4*i));
    363       1.3    bsh         reg |= trig << (4*i);
    364       1.3    bsh 
    365       1.3    bsh         bus_space_write_4(s3c2xx0_softc->sc_iot, s3c2xx0_softc->sc_gpio_ioh,
    366       1.3    bsh             GPIO_EXTINT(regidx), reg);
    367       1.3    bsh 
    368       1.3    bsh 	restore_interrupts(save);
    369       1.1    bsh }
    370