Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2800_intr.c revision 1.9.66.1
      1 /* $NetBSD: s3c2800_intr.c,v 1.9.66.1 2008/01/08 22:09:31 bouyer Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002 Fujitsu Component Limited
      5  * Copyright (c) 2002 Genetec Corporation
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of The Fujitsu Component Limited nor the name of
     17  *    Genetec corporation may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
     21  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
     25  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * IRQ handler for Samsung S3C2800 processor.
     37  * It has integrated interrupt controller.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: s3c2800_intr.c,v 1.9.66.1 2008/01/08 22:09:31 bouyer Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/malloc.h>
     46 #include <uvm/uvm_extern.h>
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 #include <arm/cpufunc.h>
     50 
     51 #include <arm/s3c2xx0/s3c2800reg.h>
     52 #include <arm/s3c2xx0/s3c2800var.h>
     53 
     54 /*
     55  * interrupt dispatch table.
     56  */
     57 
     58 struct s3c2xx0_intr_dispatch handler[ICU_LEN];
     59 
     60 #ifdef __HAVE_FAST_SOFTINTS
     61 volatile int softint_pending;
     62 #endif
     63 
     64 volatile int current_spl_level;
     65 volatile int intr_mask;    /* XXX: does this need to be volatile? */
     66 volatile int global_intr_mask = 0; /* mask some interrupts at all spl level */
     67 
     68 /* interrupt masks for each level */
     69 int s3c2xx0_imask[NIPL];
     70 int s3c2xx0_ilevel[ICU_LEN];
     71 
     72 vaddr_t intctl_base;		/* interrupt controller registers */
     73 #define icreg(offset) \
     74 	(*(volatile uint32_t *)(intctl_base+(offset)))
     75 
     76 #ifdef __HAVE_FAST_SOFTINTS
     77 /*
     78  * Map a software interrupt queue to an interrupt priority level.
     79  */
     80 static const int si_to_ipl[] = {
     81 	[SI_SOFTBIO]	= IPL_SOFTBIO,
     82 	[SI_SOFTCLOCK]	= IPL_SOFTCLOCK,
     83 	[SI_SOFTNET]	= IPL_SOFTNET,
     84 	[SI_SOFTSERIAL] = IPL_SOFTSERIAL,
     85 };
     86 #endif
     87 
     88 /*
     89  *   Clearing interrupt pending bits affects some built-in
     90  * peripherals.  For example, IIC starts transmitting next data when
     91  * its interrupt pending bit is cleared.
     92  *   We need to leave those bits to peripheral handlers.
     93  */
     94 #define PENDING_CLEAR_MASK	(~((1<<S3C2800_INT_IIC0)|(1<<S3C2800_INT_IIC1)))
     95 
     96 /*
     97  * called from irq_entry.
     98  */
     99 void s3c2800_irq_handler(struct clockframe *);
    100 void
    101 s3c2800_irq_handler(struct clockframe *frame)
    102 {
    103 	uint32_t irqbits;
    104 	int irqno;
    105 	int saved_spl_level;
    106 
    107 	saved_spl_level = current_spl_level;
    108 
    109 	while ((irqbits = icreg(INTCTL_IRQPND) & ICU_INT_HWMASK) != 0) {
    110 
    111 		for (irqno = ICU_LEN-1; irqno >= 0; --irqno)
    112 			if (irqbits & (1<<irqno))
    113 				break;
    114 
    115 		if (irqno < 0)
    116 			break;
    117 
    118 		/* raise spl to stop interrupts of lower priorities */
    119 		if (saved_spl_level < handler[irqno].level)
    120 			s3c2xx0_setipl(handler[irqno].level);
    121 
    122 		/* clear pending bit */
    123 		icreg(INTCTL_SRCPND) = PENDING_CLEAR_MASK & (1 << irqno);
    124 
    125 		enable_interrupts(I32_bit); /* allow nested interrupts */
    126 
    127 		(*handler[irqno].func) (
    128 		    handler[irqno].cookie == 0
    129 		    ? frame : handler[irqno].cookie);
    130 
    131 		disable_interrupts(I32_bit);
    132 
    133 		/* restore spl to that was when this interrupt happen */
    134 		s3c2xx0_setipl(saved_spl_level);
    135 	}
    136 
    137 #ifdef __HAVE_FAST_SOFTINTS
    138 	if (softint_pending & intr_mask)
    139 		s3c2xx0_do_pending(1);
    140 #endif
    141 }
    142 
    143 static const u_char s3c2800_ist[] = {
    144 	EXTINTR_LOW,		/* NONE */
    145 	EXTINTR_FALLING,	/* PULSE */
    146 	EXTINTR_FALLING,	/* EDGE */
    147 	EXTINTR_LOW,		/* LEVEL */
    148 	EXTINTR_HIGH,
    149 	EXTINTR_RISING,
    150 	EXTINTR_BOTH,
    151 };
    152 
    153 void *
    154 s3c2800_intr_establish(int irqno, int level, int type,
    155     int (* func) (void *), void *cookie)
    156 {
    157 	int save;
    158 
    159 	if (irqno < 0 || irqno >= ICU_LEN ||
    160 	    type < IST_NONE || IST_EDGE_BOTH < type)
    161 		panic("intr_establish: bogus irq or type");
    162 
    163 	save = disable_interrupts(I32_bit);
    164 
    165 	handler[irqno].cookie = cookie;
    166 	handler[irqno].func = func;
    167 	handler[irqno].level = level;
    168 
    169 	s3c2xx0_update_intr_masks(irqno, level);
    170 
    171 	if (irqno <= S3C2800_INT_EXT(7)) {
    172 		/*
    173 		 * Update external interrupt control
    174 		 */
    175 		uint32_t reg;
    176 		u_int 	trig;
    177 
    178 		trig = s3c2800_ist[type];
    179 
    180 		reg = bus_space_read_4(s3c2xx0_softc->sc_iot,
    181 				       s3c2xx0_softc->sc_gpio_ioh,
    182 				       GPIO_EXTINTR);
    183 
    184 		reg = reg & ~(0x0f << (4*irqno));
    185 		reg |= trig << (4*irqno);
    186 
    187 		bus_space_write_4(s3c2xx0_softc->sc_iot, s3c2xx0_softc->sc_gpio_ioh,
    188 				  GPIO_EXTINTR, reg);
    189 	}
    190 
    191 	s3c2xx0_setipl(current_spl_level);
    192 
    193 	restore_interrupts(save);
    194 
    195 	return (&handler[irqno]);
    196 }
    197 
    198 
    199 static void
    200 init_interrupt_masks(void)
    201 {
    202 	int i = 0;
    203 
    204 #ifdef __HAVE_FAST_SOFTINTS
    205 	s3c2xx0_imask[IPL_NONE] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    206 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK) |
    207 		SI_TO_IRQBIT(SI_SOFTBIO);
    208 
    209 	s3c2xx0_imask[IPL_SOFTBIO] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    210 		SI_TO_IRQBIT(SI_SOFTNET) | SI_TO_IRQBIT(SI_SOFTCLOCK);
    211 
    212 	/*
    213 	 * splsoftclock() is the only interface that users of the
    214 	 * generic software interrupt facility have to block their
    215 	 * soft intrs, so splsoftclock() must also block IPL_SOFT.
    216 	 */
    217 	s3c2xx0_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTSERIAL) |
    218 		SI_TO_IRQBIT(SI_SOFTNET);
    219 
    220 	/*
    221 	 * splsoftnet() must also block splsoftclock(), since we don't
    222 	 * want timer-driven network events to occur while we're
    223 	 * processing incoming packets.
    224 	 */
    225 	s3c2xx0_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    226 
    227 	for (i = IPL_BIO; i < IPL_SOFTSERIAL; ++i)
    228 		s3c2xx0_imask[i] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    229 #endif
    230 	for (; i < NIPL; ++i)
    231 		s3c2xx0_imask[i] = 0;
    232 }
    233 
    234 void
    235 s3c2800_intr_init(struct s3c2800_softc *sc)
    236 {
    237 	intctl_base = (vaddr_t) bus_space_vaddr(sc->sc_sx.sc_iot,
    238 	    sc->sc_sx.sc_intctl_ioh);
    239 
    240 	s3c2xx0_intr_mask_reg = (uint32_t *)(intctl_base + INTCTL_INTMSK);
    241 
    242 	/* clear all pending interrupt */
    243 	icreg(INTCTL_SRCPND) = 0xffffffff;
    244 
    245 	init_interrupt_masks();
    246 
    247 	s3c2xx0_intr_init(handler, ICU_LEN);
    248 
    249 }
    250