Home | History | Annotate | Line # | Download | only in xscale
pxa2x0_intr.c revision 1.1.2.3
      1 /* $NetBSD: pxa2x0_intr.c,v 1.1.2.3 2003/01/03 16:41:12 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project by
     18  *	Genetec Corporation.
     19  * 4. The name of Genetec Corporation may not be used to endorse or
     20  *    promote products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * IRQ handler for the Intel PXA2X0 processor.
     38  * It has integrated interrupt controller.
     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/xscale/pxa2x0reg.h>
     49 #include <arm/xscale/pxa2x0var.h>
     50 #include <arm/sa11x0/sa11x0_var.h>
     51 #include <arm/xscale/pxa2x0_intr.h>
     52 
     53 /*
     54  * interrupt dispatch table.
     55  */
     56 #ifdef MULTIPLE_HANDLERS_ON_ONE_IRQ
     57 struct intrhand {
     58 	TAILQ_ENTRY(intrhand) ih_list;	/* link on intrq list */
     59 	int (*ih_func)(void *);		/* handler */
     60 	void *ih_arg;			/* arg for handler */
     61 };
     62 #endif
     63 
     64 static struct {
     65 #ifdef MULTIPLE_HANDLERS_ON_ONE_IRQ
     66 	TAILQ_HEAD(,intrhand) list;
     67 #else
     68 	pxa2x0_irq_handler_t func;
     69 #endif
     70 	void *cookie;		/* NULL for stackframe */
     71 	/* struct evbnt ev; */
     72 } handler[ICU_LEN];
     73 
     74 __volatile int softint_pending;
     75 
     76 __volatile int current_spl_level;
     77 __volatile int intr_mask;
     78 /* interrupt masks for each level */
     79 int pxa2x0_imask[NIPL];
     80 static int extirq_level[ICU_LEN];
     81 
     82 static __inline void
     83 __raise(int ipl)
     84 {
     85 	if( current_spl_level < ipl ){
     86 		pxa2x0_setipl(ipl);
     87 	}
     88 }
     89 
     90 
     91 /*
     92  * Map a software interrupt queue to an interrupt priority level.
     93  */
     94 static const int si_to_ipl[SI_NQUEUES] = {
     95 	IPL_SOFT,		/* SI_SOFT */
     96 	IPL_SOFTCLOCK,		/* SI_SOFTCLOCK */
     97 	IPL_SOFTNET,		/* SI_SOFTNET */
     98 	IPL_SOFTSERIAL,		/* SI_SOFTSERIAL */
     99 };
    100 
    101 /*
    102  * called from irq_entry.
    103  */
    104 void
    105 pxa2x0_irq_handler(struct clockframe *frame)
    106 {
    107 	uint32_t irqbits;
    108 	int irqno;
    109 	int saved_spl_level;
    110 
    111 	saved_spl_level = current_spl_level;
    112 
    113 	/* get pending IRQs */
    114 	irqbits = read_icu(SAIPIC_IP);
    115 
    116 	while( (irqno = find_first_bit(irqbits)) >= 0 ){
    117 		/* XXX: Shuould we handle IRQs in priority order? */
    118 
    119 		/* raise spl to stop interrupts of lower priorities */
    120 		if( saved_spl_level < extirq_level[irqno] )
    121 			pxa2x0_setipl(extirq_level[irqno]);
    122 
    123 #ifdef notyet
    124 		/* Enable interrupt */
    125 #endif
    126 #ifndef MULTIPLE_HANDLERS_ON_ONE_IRQ
    127 		(* handler[irqno].func)(
    128 			handler[irqno].cookie == 0
    129 			? frame : handler[irqno].cookie );
    130 #else
    131 		/* process all handlers for this interrupt.
    132 		   XXX not yet */
    133 #endif
    134 
    135 #ifdef notyet
    136 		/* Disable interrupt */
    137 #endif
    138 
    139 		irqbits &= ~(1<<irqno);
    140 	}
    141 
    142 	/* restore spl to that was when this interrupt happen */
    143 	pxa2x0_setipl(saved_spl_level);
    144 
    145 	if( softint_pending & intr_mask )
    146 		pxa2x0_do_pending();
    147 }
    148 
    149 static int
    150 stray_interrupt( void *cookie )
    151 {
    152 	int irqno = (int)cookie;
    153 	printf( "stray interrupt %d\n", irqno );
    154 
    155 	if( PXA2X0_IRQ_MIN <= irqno && irqno < ICU_LEN ){
    156 		int save = disable_interrupts(I32_bit);
    157 		write_icu( SAIPIC_MR,
    158 			   read_icu(SAIPIC_MR) & ~(1U<<irqno) );
    159 		restore_interrupts(save);
    160 	}
    161 
    162 	return 0;
    163 }
    164 
    165 
    166 
    167 /*
    168  * Interrupt Mask Handling
    169  */
    170 
    171 void
    172 pxa2x0_update_intr_masks( int irqno, int level )
    173 {
    174 	int mask = 1U<<irqno;
    175 	int psw = disable_interrupts(I32_bit);
    176 	int i;
    177 
    178 
    179 	for( i=IPL_BIO; i < level; ++i )
    180 		pxa2x0_imask[i] |= mask; /* Enable interrupt at lower level */
    181 	for( ; i < NIPL-1; ++i )
    182 		pxa2x0_imask[i] &= ~mask; /* Disable itnerrupt at upper level */
    183 
    184 	/*
    185 	 * Enforce a heirarchy that gives "slow" device (or devices with
    186 	 * limited input buffer space/"real-time" requirements) a better
    187 	 * chance at not dropping data.
    188 	 */
    189 	pxa2x0_imask[IPL_BIO] &= pxa2x0_imask[IPL_SOFTNET];
    190 	pxa2x0_imask[IPL_NET] &= pxa2x0_imask[IPL_BIO];
    191 	pxa2x0_imask[IPL_SOFTSERIAL] &= pxa2x0_imask[IPL_NET];
    192 	pxa2x0_imask[IPL_TTY] &= pxa2x0_imask[IPL_SOFTSERIAL];
    193 
    194 	/*
    195 	 * splvm() blocks all interrupts that use the kernel memory
    196 	 * allocation facilities.
    197 	 */
    198 	pxa2x0_imask[IPL_IMP] &= pxa2x0_imask[IPL_TTY];
    199 
    200 	/*
    201 	 * Audio devices are not allowed to perform memory allocation
    202 	 * in their interrupt routines, and they have fairly "real-time"
    203 	 * requirements, so give them a high interrupt priority.
    204 	 */
    205 	pxa2x0_imask[IPL_AUDIO] &= pxa2x0_imask[IPL_IMP];
    206 
    207 	/*
    208 	 * splclock() must block anything that uses the scheduler.
    209 	 */
    210 	pxa2x0_imask[IPL_CLOCK] &= pxa2x0_imask[IPL_AUDIO];
    211 
    212 	/*
    213 	 * splhigh() must block "everything".
    214 	 */
    215 	pxa2x0_imask[IPL_HIGH] &= pxa2x0_imask[IPL_STATCLOCK];
    216 
    217 	/*
    218 	 * XXX We need serial drivers to run at the absolute highest priority
    219 	 * in order to avoid overruns, so serial > high.
    220 	 */
    221 	pxa2x0_imask[IPL_SERIAL] &= pxa2x0_imask[IPL_HIGH];
    222 
    223 	write_icu( SAIPIC_MR, pxa2x0_imask[current_spl_level] );
    224 
    225 	restore_interrupts(psw);
    226 }
    227 
    228 
    229 static void
    230 init_interrupt_masks(void)
    231 {
    232 	int i;
    233 	pxa2x0_imask[IPL_NONE] = 0xffffffff;
    234 
    235 	for( i = IPL_BIO; i < NIPL; ++i )
    236 		pxa2x0_imask[i] = 0;
    237 
    238 	/*
    239 	 * Initialize the soft interrupt masks to block themselves.
    240 	 */
    241 	pxa2x0_imask[IPL_SOFT] = ~SI_TO_IRQBIT(SI_SOFT);
    242 	pxa2x0_imask[IPL_SOFTCLOCK] = ~SI_TO_IRQBIT(SI_SOFTCLOCK);
    243 	pxa2x0_imask[IPL_SOFTNET] = ~SI_TO_IRQBIT(SI_SOFTNET);
    244 	pxa2x0_imask[IPL_SOFTSERIAL] = ~SI_TO_IRQBIT(SI_SOFTSERIAL);
    245 
    246 	/*
    247 	 * splsoftclock() is the only interface that users of the
    248 	 * generic software interrupt facility have to block their
    249 	 * soft intrs, so splsoftclock() must also block IPL_SOFT.
    250 	 */
    251 	pxa2x0_imask[IPL_SOFTCLOCK] &= pxa2x0_imask[IPL_SOFT];
    252 
    253 	/*
    254 	 * splsoftnet() must also block splsoftclock(), since we don't
    255 	 * want timer-driven network events to occur while we're
    256 	 * processing incoming packets.
    257 	 */
    258 	pxa2x0_imask[IPL_SOFTNET] &= pxa2x0_imask[IPL_SOFTCLOCK];
    259 
    260 }
    261 
    262 void
    263 pxa2x0_do_pending(void)
    264 {
    265 	static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
    266 	int oldirqstate, spl_save;
    267 
    268 	if (__cpu_simple_lock_try(&processing) == 0)
    269 		return;
    270 
    271 	spl_save = current_spl_level;
    272 
    273 	oldirqstate = disable_interrupts(I32_bit);
    274 
    275 #if 1
    276 #define	DO_SOFTINT(si,ipl)						\
    277 	if ((softint_pending & intr_mask) & SI_TO_IRQBIT(si)) {	\
    278 		softint_pending &= ~SI_TO_IRQBIT(si);			\
    279                 __raise(ipl);                                           \
    280 		restore_interrupts(oldirqstate);			\
    281 		softintr_dispatch(si);					\
    282 		oldirqstate = disable_interrupts(I32_bit);		\
    283 		pxa2x0_setipl(spl_save);					\
    284 	}
    285 
    286 	do {
    287 		DO_SOFTINT(SI_SOFTSERIAL,IPL_SOFTSERIAL);
    288 		DO_SOFTINT(SI_SOFTNET, IPL_SOFTNET);
    289 		DO_SOFTINT(SI_SOFTCLOCK, IPL_SOFTCLOCK);
    290 		DO_SOFTINT(SI_SOFT, IPL_SOFT);
    291 	} while( softint_pending & intr_mask );
    292 #else
    293 	while( (si = find_first_bit(softint_pending & intr_mask)) >= 0 ){
    294 		softint_pending &= ~SI_TO_IRQBIT(si);
    295 		__raise(si_to_ipl(si));
    296 		restore_interrupts(oldirqstate);
    297 		softintr_dispatch(si);
    298 		oldirqstate = disable_interrupts(I32_bit);
    299 		pxa2x0_setipl(spl_save);
    300 	}
    301 #endif
    302 
    303 	__cpu_simple_unlock(&processing);
    304 
    305 	restore_interrupts(oldirqstate);
    306 }
    307 
    308 
    309 #undef splx
    310 void
    311 splx(int ipl)
    312 {
    313 	pxa2x0_splx(ipl);
    314 }
    315 
    316 #undef _splraise
    317 int
    318 _splraise(int ipl)
    319 {
    320 	return pxa2x0_splraise(ipl);
    321 }
    322 
    323 #undef _spllower
    324 int
    325 _spllower(int ipl)
    326 {
    327 	return pxa2x0_spllower(ipl);
    328 }
    329 
    330 #undef _setsoftintr
    331 void
    332 _setsoftintr(int si)
    333 {
    334 	return pxa2x0_setsoftintr(si);
    335 }
    336 
    337 
    338 
    339 /*
    340  * Initialize interrupt dispatcher.
    341  */
    342 void
    343 pxa2x0_intr_init(void)
    344 {
    345 	int i;
    346 
    347 	for( i=0; i < sizeof handler / sizeof handler[0]; ++i ){
    348 		handler[i].func = stray_interrupt;
    349 		handler[i].cookie = (void *)(i);
    350 		extirq_level[i] = IPL_SERIAL;
    351 	}
    352 
    353 	init_interrupt_masks();
    354 
    355 	_splraise(IPL_SERIAL);
    356 	enable_interrupts(I32_bit);
    357 }
    358 
    359 void
    360 pxa2x0_set_intcbase( vaddr_t addr )
    361 {
    362 	pxaic_base = addr;
    363 }
    364 
    365 void *
    366 pxa2x0_intr_establish(int irqno, int level,
    367 		      int (*func)(void *), void *cookie)
    368 {
    369 	int psw;
    370 
    371 	if (irqno < PXA2X0_IRQ_MIN || irqno >= ICU_LEN )
    372 		panic("intr_establish: bogus irq number %d", irqno);
    373 
    374 	psw = disable_interrupts(I32_bit);
    375 
    376 	handler[irqno].cookie = cookie;
    377 	handler[irqno].func = func;
    378 	extirq_level[irqno] = level;
    379 	pxa2x0_update_intr_masks( irqno, level );
    380 
    381 	intr_mask = pxa2x0_imask[current_spl_level];
    382 
    383 	restore_interrupts(psw);
    384 
    385 	return ( &handler[irqno] );
    386 }
    387 
    388 
    389 
    390 /*
    391  * Glue for drivers of sa11x0 compatible integrated logics.
    392  */
    393 void *
    394 sa11x0_intr_establish(sa11x0_chipset_tag_t ic, int irq, int type, int level,
    395 		      int (*ih_fun)(void *), void *ih_arg)
    396 {
    397 	return pxa2x0_intr_establish(irq,level,ih_fun,ih_arg);
    398 }
    399 
    400