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