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