Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2410_intr.c revision 1.8.6.1
      1  1.8.6.1    mjf /* $NetBSD: s3c2410_intr.c,v 1.8.6.1 2008/06/02 13:21:55 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.8.6.1    mjf __KERNEL_RCSID(0, "$NetBSD: s3c2410_intr.c,v 1.8.6.1 2008/06/02 13:21:55 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 intr_mask;
     59      1.7   matt #ifdef __HAVE_FAST_SOFTINTS
     60      1.7   matt volatile int softint_pending;
     61      1.6  perry volatile int soft_intr_mask;
     62      1.7   matt #endif
     63      1.6  perry volatile int global_intr_mask = 0; /* mask some interrupts at all spl level */
     64      1.1    bsh 
     65      1.1    bsh /* interrupt masks for each level */
     66      1.1    bsh int s3c2xx0_imask[NIPL];
     67      1.1    bsh int s3c2xx0_ilevel[ICU_LEN];
     68      1.7   matt #ifdef __HAVE_FAST_SOFTINTS
     69      1.1    bsh int s3c24x0_soft_imask[NIPL];
     70      1.7   matt #endif
     71      1.1    bsh 
     72      1.1    bsh vaddr_t intctl_base;		/* interrupt controller registers */
     73      1.1    bsh #define icreg(offset) \
     74      1.1    bsh 	(*(volatile uint32_t *)(intctl_base+(offset)))
     75      1.1    bsh 
     76      1.7   matt #ifdef __HAVE_FAST_SOFTINTS
     77      1.1    bsh /*
     78      1.1    bsh  * Map a software interrupt queue to an interrupt priority level.
     79      1.1    bsh  */
     80      1.7   matt static const int si_to_ipl[] = {
     81      1.7   matt 	[SI_SOFTBIO]	= IPL_SOFTBIO,
     82      1.7   matt 	[SI_SOFTCLOCK]	= IPL_SOFTCLOCK,
     83      1.7   matt 	[SI_SOFTNET]	= IPL_SOFTNET,
     84      1.7   matt 	[SI_SOFTSERIAL] = IPL_SOFTSERIAL,
     85      1.1    bsh };
     86      1.7   matt #endif
     87      1.1    bsh 
     88      1.2    bsh #define PENDING_CLEAR_MASK	(~0)
     89      1.1    bsh 
     90      1.1    bsh /*
     91      1.1    bsh  * called from irq_entry.
     92      1.1    bsh  */
     93      1.1    bsh void s3c2410_irq_handler(struct clockframe *);
     94      1.1    bsh void
     95      1.1    bsh s3c2410_irq_handler(struct clockframe *frame)
     96      1.1    bsh {
     97      1.1    bsh 	uint32_t irqbits;
     98      1.1    bsh 	int irqno;
     99      1.1    bsh 	int saved_spl_level;
    100      1.1    bsh 
    101  1.8.6.1    mjf 	saved_spl_level = curcpl();
    102      1.1    bsh 
    103      1.2    bsh #ifdef	DIAGNOSTIC
    104  1.8.6.1    mjf 	if (curcpu()->ci_intr_depth > 10)
    105      1.2    bsh 		panic("nested intr too deep");
    106      1.2    bsh #endif
    107      1.2    bsh 
    108      1.1    bsh 	while ((irqbits = icreg(INTCTL_INTPND)) != 0) {
    109      1.1    bsh 
    110      1.2    bsh 		/* Note: Only one bit in INTPND register is set */
    111      1.1    bsh 
    112      1.2    bsh 		irqno = icreg(INTCTL_INTOFFSET);
    113      1.1    bsh 
    114      1.2    bsh #ifdef	DIAGNOSTIC
    115      1.2    bsh 		if (__predict_false((irqbits & (1<<irqno)) == 0)) {
    116      1.2    bsh 			/* This shouldn't happen */
    117      1.2    bsh 			printf("INTOFFSET=%d, INTPND=%x\n", irqno, irqbits);
    118      1.1    bsh 			break;
    119      1.2    bsh 		}
    120      1.2    bsh #endif
    121      1.1    bsh 		/* raise spl to stop interrupts of lower priorities */
    122      1.1    bsh 		if (saved_spl_level < handler[irqno].level)
    123      1.1    bsh 			s3c2xx0_setipl(handler[irqno].level);
    124      1.1    bsh 
    125      1.1    bsh 		/* clear pending bit */
    126      1.1    bsh 		icreg(INTCTL_SRCPND) = PENDING_CLEAR_MASK & (1 << irqno);
    127      1.2    bsh 		icreg(INTCTL_INTPND) = PENDING_CLEAR_MASK & (1 << irqno);
    128      1.1    bsh 
    129      1.1    bsh 		enable_interrupts(I32_bit); /* allow nested interrupts */
    130      1.1    bsh 
    131      1.1    bsh 		(*handler[irqno].func) (
    132      1.1    bsh 		    handler[irqno].cookie == 0
    133      1.1    bsh 		    ? frame : handler[irqno].cookie);
    134      1.1    bsh 
    135      1.1    bsh 		disable_interrupts(I32_bit);
    136      1.1    bsh 
    137      1.1    bsh 		/* restore spl to that was when this interrupt happen */
    138      1.1    bsh 		s3c2xx0_setipl(saved_spl_level);
    139      1.2    bsh 
    140      1.1    bsh 	}
    141      1.1    bsh 
    142      1.7   matt #ifdef __HAVE_FAST_SOFTINTS
    143  1.8.6.1    mjf 	cpu_dosoftints();
    144      1.7   matt #endif
    145      1.1    bsh }
    146      1.1    bsh 
    147      1.1    bsh /*
    148      1.1    bsh  * Handler for main IRQ of cascaded interrupts.
    149      1.1    bsh  */
    150      1.1    bsh static int
    151      1.1    bsh cascade_irq_handler(void *cookie)
    152      1.1    bsh {
    153      1.2    bsh 	int index = (int)cookie - 1;
    154      1.1    bsh 	uint32_t irqbits;
    155      1.1    bsh 	int irqno, i;
    156      1.1    bsh 	int save = disable_interrupts(I32_bit);
    157      1.1    bsh 
    158      1.2    bsh 	KASSERT(0 <= index && index <= 3);
    159      1.2    bsh 
    160      1.2    bsh 	irqbits = icreg(INTCTL_SUBSRCPND) &
    161      1.2    bsh 	    ~icreg(INTCTL_INTSUBMSK) & (0x07 << (3*index));
    162      1.1    bsh 
    163      1.1    bsh 	for (irqno = 3*index; irqbits; ++irqno) {
    164      1.1    bsh 		if ((irqbits & (1<<irqno)) == 0)
    165      1.1    bsh 			continue;
    166      1.1    bsh 
    167      1.1    bsh 		/* clear pending bit */
    168      1.2    bsh 		irqbits &= ~(1<<irqno);
    169      1.2    bsh 		icreg(INTCTL_SUBSRCPND) = (1 << irqno);
    170      1.1    bsh 
    171      1.1    bsh 		/* allow nested interrupts. SPL is already set
    172      1.1    bsh 		 * correctly by main handler. */
    173      1.1    bsh 		restore_interrupts(save);
    174      1.1    bsh 
    175      1.1    bsh 		i = S3C2410_SUBIRQ_MIN + irqno;
    176      1.1    bsh 		(* handler[i].func)(handler[i].cookie);
    177      1.1    bsh 
    178      1.1    bsh 		disable_interrupts(I32_bit);
    179      1.1    bsh 	}
    180      1.1    bsh 
    181      1.1    bsh 	return 1;
    182      1.1    bsh }
    183      1.1    bsh 
    184      1.1    bsh 
    185      1.2    bsh static const uint8_t subirq_to_main[] = {
    186      1.1    bsh 	S3C2410_INT_UART0,
    187      1.1    bsh 	S3C2410_INT_UART0,
    188      1.1    bsh 	S3C2410_INT_UART0,
    189      1.1    bsh 	S3C2410_INT_UART1,
    190      1.1    bsh 	S3C2410_INT_UART1,
    191      1.1    bsh 	S3C2410_INT_UART1,
    192      1.1    bsh 	S3C2410_INT_UART2,
    193      1.1    bsh 	S3C2410_INT_UART2,
    194      1.1    bsh 	S3C2410_INT_UART2,
    195      1.1    bsh 	S3C24X0_INT_ADCTC,
    196      1.1    bsh 	S3C24X0_INT_ADCTC,
    197      1.1    bsh };
    198      1.1    bsh 
    199      1.1    bsh void *
    200      1.1    bsh s3c24x0_intr_establish(int irqno, int level, int type,
    201      1.1    bsh     int (* func) (void *), void *cookie)
    202      1.1    bsh {
    203      1.1    bsh 	int save;
    204      1.1    bsh 
    205      1.1    bsh 	if (irqno < 0 || irqno >= ICU_LEN ||
    206      1.1    bsh 	    type < IST_NONE || IST_EDGE_BOTH < type)
    207      1.1    bsh 		panic("intr_establish: bogus irq or type");
    208      1.1    bsh 
    209      1.1    bsh 	save = disable_interrupts(I32_bit);
    210      1.1    bsh 
    211      1.1    bsh 	handler[irqno].cookie = cookie;
    212      1.1    bsh 	handler[irqno].func = func;
    213      1.1    bsh 	handler[irqno].level = level;
    214      1.1    bsh 
    215      1.1    bsh 	if (irqno >= S3C2410_SUBIRQ_MIN) {
    216      1.1    bsh 		/* cascaded interrupts. */
    217      1.1    bsh 		int main_irqno;
    218      1.2    bsh 		int i = (irqno - S3C2410_SUBIRQ_MIN);
    219      1.1    bsh 
    220      1.2    bsh 		main_irqno = subirq_to_main[i];
    221      1.1    bsh 
    222      1.2    bsh 		/* establish main irq if first time
    223      1.2    bsh 		 * be careful that cookie shouldn't be 0 */
    224      1.1    bsh 		if (handler[main_irqno].func != cascade_irq_handler)
    225      1.1    bsh 			s3c24x0_intr_establish(main_irqno, level, type,
    226      1.2    bsh 			    cascade_irq_handler, (void *)((i/3) + 1));
    227      1.2    bsh 
    228      1.2    bsh 		/* unmask it in submask register */
    229      1.2    bsh 		icreg(INTCTL_INTSUBMSK) &= ~(1<<i);
    230      1.1    bsh 
    231      1.2    bsh 		restore_interrupts(save);
    232      1.1    bsh 		return &handler[irqno];
    233      1.1    bsh 	}
    234      1.1    bsh 
    235      1.1    bsh 	s3c2xx0_update_intr_masks(irqno, level);
    236      1.1    bsh 
    237      1.2    bsh 	/*
    238      1.2    bsh 	 * set trigger type for external interrupts 0..3
    239      1.2    bsh 	 */
    240      1.1    bsh 	if (irqno <= S3C24X0_INT_EXT(3)) {
    241      1.1    bsh 		/*
    242      1.1    bsh 		 * Update external interrupt control
    243      1.1    bsh 		 */
    244      1.3    bsh 		s3c2410_setup_extint(irqno, type);
    245      1.1    bsh 	}
    246      1.1    bsh 
    247  1.8.6.1    mjf 	s3c2xx0_setipl(curcpl());
    248      1.1    bsh 
    249      1.1    bsh 	restore_interrupts(save);
    250      1.1    bsh 
    251      1.1    bsh 	return &handler[irqno];
    252      1.1    bsh }
    253      1.1    bsh 
    254      1.1    bsh 
    255      1.1    bsh static void
    256      1.1    bsh init_interrupt_masks(void)
    257      1.1    bsh {
    258      1.1    bsh 	int i;
    259      1.1    bsh 
    260      1.1    bsh 	for (i=0; i < NIPL; ++i)
    261      1.1    bsh 		s3c2xx0_imask[i] = 0;
    262      1.1    bsh 
    263      1.7   matt #ifdef __HAVE_FAST_SOFTINTS
    264      1.1    bsh 	s3c24x0_soft_imask[IPL_NONE] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    265      1.1    bsh 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK) |
    266      1.1    bsh 		SI_TO_IRQBIT(SI_SOFT);
    267      1.1    bsh 
    268      1.1    bsh 	s3c24x0_soft_imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    269      1.1    bsh 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK);
    270      1.1    bsh 
    271      1.1    bsh 	/*
    272      1.1    bsh 	 * splsoftclock() is the only interface that users of the
    273      1.1    bsh 	 * generic software interrupt facility have to block their
    274      1.1    bsh 	 * soft intrs, so splsoftclock() must also block IPL_SOFT.
    275      1.1    bsh 	 */
    276      1.1    bsh 	s3c24x0_soft_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    277      1.1    bsh 		SI_TO_IRQBIT(SI_SOFTNET);
    278      1.1    bsh 
    279      1.1    bsh 	/*
    280      1.1    bsh 	 * splsoftnet() must also block splsoftclock(), since we don't
    281      1.1    bsh 	 * want timer-driven network events to occur while we're
    282      1.1    bsh 	 * processing incoming packets.
    283      1.1    bsh 	 */
    284      1.1    bsh 	s3c24x0_soft_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    285      1.1    bsh 
    286      1.1    bsh 	for (i = IPL_BIO; i < IPL_SOFTSERIAL; ++i)
    287      1.1    bsh 		s3c24x0_soft_imask[i] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    288      1.7   matt #endif
    289      1.1    bsh }
    290      1.1    bsh 
    291      1.1    bsh void
    292      1.1    bsh s3c2410_intr_init(struct s3c24x0_softc *sc)
    293      1.1    bsh {
    294      1.1    bsh 	intctl_base = (vaddr_t) bus_space_vaddr(sc->sc_sx.sc_iot,
    295      1.1    bsh 	    sc->sc_sx.sc_intctl_ioh);
    296      1.1    bsh 
    297      1.1    bsh 	s3c2xx0_intr_mask_reg = (uint32_t *)(intctl_base + INTCTL_INTMSK);
    298      1.1    bsh 
    299      1.1    bsh 	/* clear all pending interrupt */
    300      1.2    bsh 	icreg(INTCTL_SRCPND) = ~0;
    301      1.2    bsh 	icreg(INTCTL_INTPND) = ~0;
    302      1.2    bsh 
    303      1.2    bsh 	/* mask all sub interrupts */
    304      1.2    bsh 	icreg(INTCTL_INTSUBMSK) = 0x7ff;
    305      1.1    bsh 
    306      1.1    bsh 	init_interrupt_masks();
    307      1.1    bsh 
    308      1.1    bsh 	s3c2xx0_intr_init(handler, ICU_LEN);
    309      1.1    bsh 
    310      1.1    bsh }
    311      1.1    bsh 
    312      1.1    bsh 
    313      1.1    bsh /*
    314      1.1    bsh  * mask/unmask sub interrupts
    315      1.1    bsh  */
    316      1.1    bsh void
    317      1.1    bsh s3c2410_mask_subinterrupts(int bits)
    318      1.1    bsh {
    319      1.4     he 	atomic_set_bit((uint32_t *)__UNVOLATILE(&icreg(INTCTL_INTSUBMSK)),
    320      1.4     he 		bits);
    321      1.1    bsh }
    322      1.1    bsh 
    323      1.1    bsh void
    324      1.1    bsh s3c2410_unmask_subinterrupts(int bits)
    325      1.1    bsh {
    326      1.4     he 	atomic_clear_bit((uint32_t *)__UNVOLATILE(&icreg(INTCTL_INTSUBMSK)),
    327      1.4     he 		bits);
    328      1.3    bsh }
    329      1.3    bsh 
    330      1.3    bsh /*
    331      1.3    bsh  * Update external interrupt control
    332      1.3    bsh  */
    333      1.3    bsh static const u_char s3c24x0_ist[] = {
    334      1.3    bsh 	EXTINTR_LOW,		/* NONE */
    335      1.3    bsh 	EXTINTR_FALLING,	/* PULSE */
    336      1.3    bsh 	EXTINTR_FALLING,	/* EDGE */
    337      1.3    bsh 	EXTINTR_LOW,		/* LEVEL */
    338      1.3    bsh 	EXTINTR_HIGH,
    339      1.3    bsh 	EXTINTR_RISING,
    340      1.3    bsh 	EXTINTR_BOTH,
    341      1.3    bsh };
    342      1.3    bsh 
    343      1.3    bsh void
    344      1.3    bsh s3c2410_setup_extint(int extint, int type)
    345      1.3    bsh {
    346      1.3    bsh         uint32_t reg;
    347      1.3    bsh         u_int   trig;
    348      1.3    bsh         int     i = extint % 8;
    349      1.3    bsh         int     regidx = extint/8;      /* GPIO_EXTINT[0:2] */
    350      1.3    bsh 	int	save;
    351      1.3    bsh 
    352      1.3    bsh         trig = s3c24x0_ist[type];
    353      1.3    bsh 
    354      1.3    bsh 	save = disable_interrupts(I32_bit);
    355      1.3    bsh 
    356      1.3    bsh         reg = bus_space_read_4(s3c2xx0_softc->sc_iot,
    357      1.3    bsh             s3c2xx0_softc->sc_gpio_ioh,
    358      1.3    bsh             GPIO_EXTINT(regidx));
    359      1.3    bsh 
    360      1.3    bsh         reg = reg & ~(0x07 << (4*i));
    361      1.3    bsh         reg |= trig << (4*i);
    362      1.3    bsh 
    363      1.3    bsh         bus_space_write_4(s3c2xx0_softc->sc_iot, s3c2xx0_softc->sc_gpio_ioh,
    364      1.3    bsh             GPIO_EXTINT(regidx), reg);
    365      1.3    bsh 
    366      1.3    bsh 	restore_interrupts(save);
    367      1.1    bsh }
    368