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