Home | History | Annotate | Line # | Download | only in iomd
iomd_irqhandler.c revision 1.1
      1  1.1  reinoud /*	$NetBSD: iomd_irqhandler.c,v 1.1 2001/10/05 22:27:41 reinoud Exp $	*/
      2  1.1  reinoud 
      3  1.1  reinoud /*
      4  1.1  reinoud  * Copyright (c) 1994-1998 Mark Brinicombe.
      5  1.1  reinoud  * Copyright (c) 1994 Brini.
      6  1.1  reinoud  * All rights reserved.
      7  1.1  reinoud  *
      8  1.1  reinoud  * This code is derived from software written for Brini by Mark Brinicombe
      9  1.1  reinoud  *
     10  1.1  reinoud  * Redistribution and use in source and binary forms, with or without
     11  1.1  reinoud  * modification, are permitted provided that the following conditions
     12  1.1  reinoud  * are met:
     13  1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     14  1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     15  1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     18  1.1  reinoud  * 3. All advertising materials mentioning features or use of this software
     19  1.1  reinoud  *    must display the following acknowledgement:
     20  1.1  reinoud  *	This product includes software developed by Mark Brinicombe
     21  1.1  reinoud  *	for the NetBSD Project.
     22  1.1  reinoud  * 4. The name of the company nor the name of the author may be used to
     23  1.1  reinoud  *    endorse or promote products derived from this software without specific
     24  1.1  reinoud  *    prior written permission.
     25  1.1  reinoud  *
     26  1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27  1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28  1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30  1.1  reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31  1.1  reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32  1.1  reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33  1.1  reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34  1.1  reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35  1.1  reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36  1.1  reinoud  *
     37  1.1  reinoud  * IRQ/FIQ initialisation, claim, release and handler routines
     38  1.1  reinoud  *
     39  1.1  reinoud  *	from: irqhandler.c,v 1.14 1997/04/02 21:52:19 christos Exp $
     40  1.1  reinoud  */
     41  1.1  reinoud 
     42  1.1  reinoud #include "opt_irqstats.h"
     43  1.1  reinoud 
     44  1.1  reinoud #include <sys/param.h>
     45  1.1  reinoud #include <sys/systm.h>
     46  1.1  reinoud #include <sys/syslog.h>
     47  1.1  reinoud #include <sys/malloc.h>
     48  1.1  reinoud #include <uvm/uvm_extern.h>
     49  1.1  reinoud 
     50  1.1  reinoud #include <arm/iomd/iomdreg.h>
     51  1.1  reinoud #include <arm/iomd/iomdvar.h>
     52  1.1  reinoud 
     53  1.1  reinoud #include <machine/irqhandler.h>
     54  1.1  reinoud #include <machine/cpu.h>
     55  1.1  reinoud #include <machine/katelib.h>
     56  1.1  reinoud 
     57  1.1  reinoud irqhandler_t *irqhandlers[NIRQS];
     58  1.1  reinoud fiqhandler_t *fiqhandlers;
     59  1.1  reinoud 
     60  1.1  reinoud int current_intr_depth;
     61  1.1  reinoud u_int current_mask;
     62  1.1  reinoud u_int actual_mask;
     63  1.1  reinoud u_int disabled_mask;
     64  1.1  reinoud u_int spl_mask;
     65  1.1  reinoud u_int irqmasks[IPL_LEVELS];
     66  1.1  reinoud u_int irqblock[NIRQS];
     67  1.1  reinoud 
     68  1.1  reinoud extern u_int soft_interrupts;	/* Only so we can initialise it */
     69  1.1  reinoud 
     70  1.1  reinoud extern char *_intrnames;
     71  1.1  reinoud 
     72  1.1  reinoud /* Prototypes */
     73  1.1  reinoud 
     74  1.1  reinoud extern void zero_page_readonly	__P((void));
     75  1.1  reinoud extern void zero_page_readwrite	__P((void));
     76  1.1  reinoud extern int fiq_setregs		__P((fiqhandler_t *));
     77  1.1  reinoud extern int fiq_getregs		__P((fiqhandler_t *));
     78  1.1  reinoud extern void set_spl_masks	__P((void));
     79  1.1  reinoud 
     80  1.1  reinoud /*
     81  1.1  reinoud  * void irq_init(void)
     82  1.1  reinoud  *
     83  1.1  reinoud  * Initialise the IRQ/FIQ sub system
     84  1.1  reinoud  */
     85  1.1  reinoud 
     86  1.1  reinoud void
     87  1.1  reinoud irq_init()
     88  1.1  reinoud {
     89  1.1  reinoud 	int loop;
     90  1.1  reinoud 
     91  1.1  reinoud 	/* Clear all the IRQ handlers and the irq block masks */
     92  1.1  reinoud 	for (loop = 0; loop < NIRQS; ++loop) {
     93  1.1  reinoud 		irqhandlers[loop] = NULL;
     94  1.1  reinoud 		irqblock[loop] = 0;
     95  1.1  reinoud 	}
     96  1.1  reinoud 
     97  1.1  reinoud 	/* Clear the FIQ handler */
     98  1.1  reinoud 	fiqhandlers = NULL;
     99  1.1  reinoud 
    100  1.1  reinoud 	/* Clear the IRQ/FIQ masks in the IOMD */
    101  1.1  reinoud 	IOMD_WRITE_BYTE(IOMD_IRQMSKA, 0x00);
    102  1.1  reinoud 	IOMD_WRITE_BYTE(IOMD_IRQMSKB, 0x00);
    103  1.1  reinoud 
    104  1.1  reinoud 	switch (IOMD_ID) {
    105  1.1  reinoud 	case RPC600_IOMD_ID:
    106  1.1  reinoud 		break;
    107  1.1  reinoud 	case ARM7500_IOC_ID:
    108  1.1  reinoud 	case ARM7500FE_IOC_ID:
    109  1.1  reinoud 		IOMD_WRITE_BYTE(IOMD_IRQMSKC, 0x00);
    110  1.1  reinoud 		IOMD_WRITE_BYTE(IOMD_IRQMSKD, 0x00);
    111  1.1  reinoud 		break;
    112  1.1  reinoud 	default:
    113  1.1  reinoud 		printf("Unknown IOMD id (%d) found in irq_init()\n", IOMD_ID);
    114  1.1  reinoud 	};
    115  1.1  reinoud 
    116  1.1  reinoud 	IOMD_WRITE_BYTE(IOMD_FIQMSK, 0x00);
    117  1.1  reinoud 	IOMD_WRITE_BYTE(IOMD_DMAMSK, 0x00);
    118  1.1  reinoud 
    119  1.1  reinoud 	/*
    120  1.1  reinoud 	 * Setup the irqmasks for the different Interrupt Priority Levels
    121  1.1  reinoud 	 * We will start with no bits set and these will be updated as handlers
    122  1.1  reinoud 	 * are installed at different IPL's.
    123  1.1  reinoud 	 */
    124  1.1  reinoud 	for (loop = 0; loop < IPL_LEVELS; ++loop)
    125  1.1  reinoud 		irqmasks[loop] = 0;
    126  1.1  reinoud 
    127  1.1  reinoud 	current_intr_depth = 0;
    128  1.1  reinoud 	current_mask = 0x00000000;
    129  1.1  reinoud 	disabled_mask = 0x00000000;
    130  1.1  reinoud 	actual_mask = 0x00000000;
    131  1.1  reinoud 	spl_mask = 0x00000000;
    132  1.1  reinoud 	soft_interrupts = 0x00000000;
    133  1.1  reinoud 
    134  1.1  reinoud 	set_spl_masks();
    135  1.1  reinoud 
    136  1.1  reinoud 	/* Enable IRQ's and FIQ's */
    137  1.1  reinoud 	enable_interrupts(I32_bit | F32_bit);
    138  1.1  reinoud }
    139  1.1  reinoud 
    140  1.1  reinoud 
    141  1.1  reinoud /*
    142  1.1  reinoud  * int irq_claim(int irq, irqhandler_t *handler)
    143  1.1  reinoud  *
    144  1.1  reinoud  * Enable an IRQ and install a handler for it.
    145  1.1  reinoud  */
    146  1.1  reinoud 
    147  1.1  reinoud int
    148  1.1  reinoud irq_claim(irq, handler)
    149  1.1  reinoud 	int irq;
    150  1.1  reinoud 	irqhandler_t *handler;
    151  1.1  reinoud {
    152  1.1  reinoud 	int level;
    153  1.1  reinoud 	int loop;
    154  1.1  reinoud 
    155  1.1  reinoud #ifdef DIAGNOSTIC
    156  1.1  reinoud 	/* Sanity check */
    157  1.1  reinoud 	if (handler == NULL)
    158  1.1  reinoud 		panic("NULL interrupt handler\n");
    159  1.1  reinoud 	if (handler->ih_func == NULL)
    160  1.1  reinoud 		panic("Interrupt handler does not have a function\n");
    161  1.1  reinoud #endif	/* DIAGNOSTIC */
    162  1.1  reinoud 
    163  1.1  reinoud 	/*
    164  1.1  reinoud 	 * IRQ_INSTRUCT indicates that we should get the irq number
    165  1.1  reinoud 	 * from the irq structure
    166  1.1  reinoud 	 */
    167  1.1  reinoud 	if (irq == IRQ_INSTRUCT)
    168  1.1  reinoud 		irq = handler->ih_num;
    169  1.1  reinoud 
    170  1.1  reinoud 	/* Make sure the irq number is valid */
    171  1.1  reinoud 	if (irq < 0 || irq >= NIRQS)
    172  1.1  reinoud 		return(-1);
    173  1.1  reinoud 
    174  1.1  reinoud 	/* Make sure the level is valid */
    175  1.1  reinoud 	if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
    176  1.1  reinoud     	        return(-1);
    177  1.1  reinoud 
    178  1.1  reinoud 	/* Attach handler at top of chain */
    179  1.1  reinoud 	handler->ih_next = irqhandlers[irq];
    180  1.1  reinoud 	irqhandlers[irq] = handler;
    181  1.1  reinoud 
    182  1.1  reinoud 	/*
    183  1.1  reinoud 	 * Reset the flags for this handler.
    184  1.1  reinoud 	 * As the handler is now in the chain mark it as active.
    185  1.1  reinoud 	 */
    186  1.1  reinoud 	handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
    187  1.1  reinoud 
    188  1.1  reinoud 	/*
    189  1.1  reinoud 	 * Record the interrupt number for accounting.
    190  1.1  reinoud 	 * Done here as the accounting number may not be the same as the
    191  1.1  reinoud 	 * IRQ number though for the moment they are
    192  1.1  reinoud 	 */
    193  1.1  reinoud 	handler->ih_num = irq;
    194  1.1  reinoud 
    195  1.1  reinoud #ifdef IRQSTATS
    196  1.1  reinoud 	/* Get the interrupt name from the head of the list */
    197  1.1  reinoud 	if (handler->ih_name) {
    198  1.1  reinoud 		char *ptr = _intrnames + (irq * 14);
    199  1.1  reinoud 		strcpy(ptr, "             ");
    200  1.1  reinoud 		strncpy(ptr, handler->ih_name,
    201  1.1  reinoud 		    min(strlen(handler->ih_name), 13));
    202  1.1  reinoud 	} else {
    203  1.1  reinoud 		char *ptr = _intrnames + (irq * 14);
    204  1.1  reinoud 		sprintf(ptr, "irq %2d     ", irq);
    205  1.1  reinoud 	}
    206  1.1  reinoud #endif	/* IRQSTATS */
    207  1.1  reinoud 
    208  1.1  reinoud 	/*
    209  1.1  reinoud 	 * Update the irq masks.
    210  1.1  reinoud 	 * Find the lowest interrupt priority on the irq chain.
    211  1.1  reinoud 	 * Interrupt is allowable at priorities lower than this.
    212  1.1  reinoud 	 * If ih_level is out of range then don't bother to update
    213  1.1  reinoud 	 * the masks.
    214  1.1  reinoud 	 */
    215  1.1  reinoud 	if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
    216  1.1  reinoud 		irqhandler_t *ptr;
    217  1.1  reinoud 
    218  1.1  reinoud 		/*
    219  1.1  reinoud 		 * Find the lowest interrupt priority on the irq chain.
    220  1.1  reinoud 		 * Interrupt is allowable at priorities lower than this.
    221  1.1  reinoud 		 */
    222  1.1  reinoud 		ptr = irqhandlers[irq];
    223  1.1  reinoud 		if (ptr) {
    224  1.1  reinoud 			int max_level;
    225  1.1  reinoud 
    226  1.1  reinoud 			level = ptr->ih_level - 1;
    227  1.1  reinoud 			max_level = ptr->ih_level - 1;
    228  1.1  reinoud 			while (ptr) {
    229  1.1  reinoud 				if (ptr->ih_level - 1 < level)
    230  1.1  reinoud 					level = ptr->ih_level - 1;
    231  1.1  reinoud 				else if (ptr->ih_level - 1 > max_level)
    232  1.1  reinoud 					max_level = ptr->ih_level - 1;
    233  1.1  reinoud 				ptr = ptr->ih_next;
    234  1.1  reinoud 			}
    235  1.1  reinoud 			/* Clear out any levels that we cannot now allow */
    236  1.1  reinoud 			while (max_level >=0 && max_level > level) {
    237  1.1  reinoud 				irqmasks[max_level] &= ~(1 << irq);
    238  1.1  reinoud 				--max_level;
    239  1.1  reinoud 			}
    240  1.1  reinoud 			while (level >= 0) {
    241  1.1  reinoud 				irqmasks[level] |= (1 << irq);
    242  1.1  reinoud 				--level;
    243  1.1  reinoud 			}
    244  1.1  reinoud 		}
    245  1.1  reinoud 
    246  1.1  reinoud #include "sl.h"
    247  1.1  reinoud #include "ppp.h"
    248  1.1  reinoud #if NSL > 0 || NPPP > 0
    249  1.1  reinoud 		/* In the presence of SLIP or PPP, splimp > spltty. */
    250  1.1  reinoud 		irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
    251  1.1  reinoud #endif
    252  1.1  reinoud 	}
    253  1.1  reinoud 
    254  1.1  reinoud 	/*
    255  1.1  reinoud 	 * We now need to update the irqblock array. This array indicates
    256  1.1  reinoud 	 * what other interrupts should be blocked when interrupt is asserted
    257  1.1  reinoud 	 * This basically emulates hardware interrupt priorities e.g. by
    258  1.1  reinoud 	 * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
    259  1.1  reinoud 	 * is asserted. For each interrupt we find the highest IPL and set
    260  1.1  reinoud 	 * the block mask to the interrupt mask for that level.
    261  1.1  reinoud 	 */
    262  1.1  reinoud 	for (loop = 0; loop < NIRQS; ++loop) {
    263  1.1  reinoud 		irqhandler_t *ptr;
    264  1.1  reinoud 
    265  1.1  reinoud 		ptr = irqhandlers[loop];
    266  1.1  reinoud 		if (ptr) {
    267  1.1  reinoud 			/* There is at least 1 handler so scan the chain */
    268  1.1  reinoud 			level = ptr->ih_level;
    269  1.1  reinoud 			while (ptr) {
    270  1.1  reinoud 				if (ptr->ih_level > level)
    271  1.1  reinoud 					level = ptr->ih_level;
    272  1.1  reinoud 				ptr = ptr->ih_next;
    273  1.1  reinoud 			}
    274  1.1  reinoud 			irqblock[loop] = ~irqmasks[level];
    275  1.1  reinoud 		} else
    276  1.1  reinoud 			/* No handlers for this irq so nothing to block */
    277  1.1  reinoud 			irqblock[loop] = 0;
    278  1.1  reinoud 	}
    279  1.1  reinoud 
    280  1.1  reinoud 	enable_irq(irq);
    281  1.1  reinoud 	set_spl_masks();
    282  1.1  reinoud 
    283  1.1  reinoud 	return(0);
    284  1.1  reinoud }
    285  1.1  reinoud 
    286  1.1  reinoud 
    287  1.1  reinoud /*
    288  1.1  reinoud  * int irq_release(int irq, irqhandler_t *handler)
    289  1.1  reinoud  *
    290  1.1  reinoud  * Disable an IRQ and remove a handler for it.
    291  1.1  reinoud  */
    292  1.1  reinoud 
    293  1.1  reinoud int
    294  1.1  reinoud irq_release(irq, handler)
    295  1.1  reinoud 	int irq;
    296  1.1  reinoud 	irqhandler_t *handler;
    297  1.1  reinoud {
    298  1.1  reinoud 	int level;
    299  1.1  reinoud 	int loop;
    300  1.1  reinoud 	irqhandler_t *irqhand;
    301  1.1  reinoud 	irqhandler_t **prehand;
    302  1.1  reinoud #ifdef IRQSTATS
    303  1.1  reinoud 	extern char *_intrnames;
    304  1.1  reinoud #endif
    305  1.1  reinoud 
    306  1.1  reinoud 	/*
    307  1.1  reinoud 	 * IRQ_INSTRUCT indicates that we should get the irq number
    308  1.1  reinoud 	 * from the irq structure
    309  1.1  reinoud 	 */
    310  1.1  reinoud 	if (irq == IRQ_INSTRUCT)
    311  1.1  reinoud 		irq = handler->ih_num;
    312  1.1  reinoud 
    313  1.1  reinoud 	/* Make sure the irq number is valid */
    314  1.1  reinoud 	if (irq < 0 || irq >= NIRQS)
    315  1.1  reinoud 		return(-1);
    316  1.1  reinoud 
    317  1.1  reinoud 	/* Locate the handler */
    318  1.1  reinoud 	irqhand = irqhandlers[irq];
    319  1.1  reinoud 	prehand = &irqhandlers[irq];
    320  1.1  reinoud 
    321  1.1  reinoud 	while (irqhand && handler != irqhand) {
    322  1.1  reinoud 		prehand = &irqhand->ih_next;
    323  1.1  reinoud 		irqhand = irqhand->ih_next;
    324  1.1  reinoud 	}
    325  1.1  reinoud 
    326  1.1  reinoud 	/* Remove the handler if located */
    327  1.1  reinoud 	if (irqhand)
    328  1.1  reinoud 		*prehand = irqhand->ih_next;
    329  1.1  reinoud 	else
    330  1.1  reinoud 		return(-1);
    331  1.1  reinoud 
    332  1.1  reinoud 	/* Now the handler has been removed from the chain mark is as inactive */
    333  1.1  reinoud 	irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
    334  1.1  reinoud 
    335  1.1  reinoud 	/* Make sure the head of the handler list is active */
    336  1.1  reinoud 	if (irqhandlers[irq])
    337  1.1  reinoud 		irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
    338  1.1  reinoud 
    339  1.1  reinoud #ifdef IRQSTATS
    340  1.1  reinoud 	/* Get the interrupt name from the head of the list */
    341  1.1  reinoud 	if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
    342  1.1  reinoud 		char *ptr = _intrnames + (irq * 14);
    343  1.1  reinoud 		strcpy(ptr, "             ");
    344  1.1  reinoud 		strncpy(ptr, irqhandlers[irq]->ih_name,
    345  1.1  reinoud 		    min(strlen(irqhandlers[irq]->ih_name), 13));
    346  1.1  reinoud 	} else {
    347  1.1  reinoud 		char *ptr = _intrnames + (irq * 14);
    348  1.1  reinoud 		sprintf(ptr, "irq %2d     ", irq);
    349  1.1  reinoud 	}
    350  1.1  reinoud #endif	/* IRQSTATS */
    351  1.1  reinoud 
    352  1.1  reinoud 	/*
    353  1.1  reinoud 	 * Update the irq masks.
    354  1.1  reinoud 	 * If ih_level is out of range then don't bother to update
    355  1.1  reinoud 	 * the masks.
    356  1.1  reinoud 	 */
    357  1.1  reinoud 	if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
    358  1.1  reinoud 		irqhandler_t *ptr;
    359  1.1  reinoud 
    360  1.1  reinoud 		/* Clean the bit from all the masks */
    361  1.1  reinoud 		for (level = 0; level < IPL_LEVELS; ++level)
    362  1.1  reinoud 			irqmasks[level] &= ~(1 << irq);
    363  1.1  reinoud 
    364  1.1  reinoud 		/*
    365  1.1  reinoud 		 * Find the lowest interrupt priority on the irq chain.
    366  1.1  reinoud 		 * Interrupt is allowable at priorities lower than this.
    367  1.1  reinoud 		 */
    368  1.1  reinoud 		ptr = irqhandlers[irq];
    369  1.1  reinoud 		if (ptr) {
    370  1.1  reinoud 			level = ptr->ih_level - 1;
    371  1.1  reinoud 			while (ptr) {
    372  1.1  reinoud 				if (ptr->ih_level - 1 < level)
    373  1.1  reinoud 					level = ptr->ih_level - 1;
    374  1.1  reinoud 				ptr = ptr->ih_next;
    375  1.1  reinoud 			}
    376  1.1  reinoud 			while (level >= 0) {
    377  1.1  reinoud 				irqmasks[level] |= (1 << irq);
    378  1.1  reinoud 				--level;
    379  1.1  reinoud 			}
    380  1.1  reinoud 		}
    381  1.1  reinoud 	}
    382  1.1  reinoud 
    383  1.1  reinoud 	/*
    384  1.1  reinoud 	 * We now need to update the irqblock array. This array indicates
    385  1.1  reinoud 	 * what other interrupts should be blocked when interrupt is asserted
    386  1.1  reinoud 	 * This basically emulates hardware interrupt priorities e.g. by
    387  1.1  reinoud 	 * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
    388  1.1  reinoud 	 * is asserted. For each interrupt we find the highest IPL and set
    389  1.1  reinoud 	 * the block mask to the interrupt mask for that level.
    390  1.1  reinoud 	 */
    391  1.1  reinoud 	for (loop = 0; loop < NIRQS; ++loop) {
    392  1.1  reinoud 		irqhandler_t *ptr;
    393  1.1  reinoud 
    394  1.1  reinoud 		ptr = irqhandlers[loop];
    395  1.1  reinoud 		if (ptr) {
    396  1.1  reinoud 			/* There is at least 1 handler so scan the chain */
    397  1.1  reinoud 			level = ptr->ih_level;
    398  1.1  reinoud 			while (ptr) {
    399  1.1  reinoud 				if (ptr->ih_level > level)
    400  1.1  reinoud 					level = ptr->ih_level;
    401  1.1  reinoud 				ptr = ptr->ih_next;
    402  1.1  reinoud 			}
    403  1.1  reinoud 			irqblock[loop] = ~irqmasks[level];
    404  1.1  reinoud 		} else
    405  1.1  reinoud 			/* No handlers for this irq so nothing to block */
    406  1.1  reinoud 			irqblock[loop] = 0;
    407  1.1  reinoud 	}
    408  1.1  reinoud 
    409  1.1  reinoud 	/*
    410  1.1  reinoud 	 * Disable the appropriate mask bit if there are no handlers left for
    411  1.1  reinoud 	 * this IRQ.
    412  1.1  reinoud 	 */
    413  1.1  reinoud 	if (irqhandlers[irq] == NULL)
    414  1.1  reinoud 		disable_irq(irq);
    415  1.1  reinoud 
    416  1.1  reinoud 	set_spl_masks();
    417  1.1  reinoud 
    418  1.1  reinoud 	return(0);
    419  1.1  reinoud }
    420  1.1  reinoud 
    421  1.1  reinoud 
    422  1.1  reinoud void *
    423  1.1  reinoud intr_claim(irq, level, name, ih_func, ih_arg)
    424  1.1  reinoud 	int irq;
    425  1.1  reinoud 	int level;
    426  1.1  reinoud 	const char *name;
    427  1.1  reinoud 	int (*ih_func) __P((void *));
    428  1.1  reinoud 	void *ih_arg;
    429  1.1  reinoud {
    430  1.1  reinoud 	irqhandler_t *ih;
    431  1.1  reinoud 
    432  1.1  reinoud 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    433  1.1  reinoud 	if (!ih)
    434  1.1  reinoud 		panic("intr_claim(): Cannot malloc handler memory\n");
    435  1.1  reinoud 
    436  1.1  reinoud 	ih->ih_level = level;
    437  1.1  reinoud 	ih->ih_name = name;
    438  1.1  reinoud 	ih->ih_func = ih_func;
    439  1.1  reinoud 	ih->ih_arg = ih_arg;
    440  1.1  reinoud 	ih->ih_flags = 0;
    441  1.1  reinoud 
    442  1.1  reinoud 	if (irq_claim(irq, ih) != 0)
    443  1.1  reinoud 		return(NULL);
    444  1.1  reinoud 	return(ih);
    445  1.1  reinoud }
    446  1.1  reinoud 
    447  1.1  reinoud 
    448  1.1  reinoud int
    449  1.1  reinoud intr_release(arg)
    450  1.1  reinoud 	void *arg;
    451  1.1  reinoud {
    452  1.1  reinoud 	irqhandler_t *ih = (irqhandler_t *)arg;
    453  1.1  reinoud 
    454  1.1  reinoud 	if (irq_release(ih->ih_num, ih) == 0) {
    455  1.1  reinoud 		free(ih, M_DEVBUF);
    456  1.1  reinoud 		return(0);
    457  1.1  reinoud 	}
    458  1.1  reinoud 	return(1);
    459  1.1  reinoud }
    460  1.1  reinoud 
    461  1.1  reinoud #if 0
    462  1.1  reinoud u_int
    463  1.1  reinoud disable_interrupts(mask)
    464  1.1  reinoud 	u_int mask;
    465  1.1  reinoud {
    466  1.1  reinoud 	u_int cpsr;
    467  1.1  reinoud 
    468  1.1  reinoud 	cpsr = SetCPSR(mask, mask);
    469  1.1  reinoud 	return(cpsr);
    470  1.1  reinoud }
    471  1.1  reinoud 
    472  1.1  reinoud 
    473  1.1  reinoud u_int
    474  1.1  reinoud restore_interrupts(old_cpsr)
    475  1.1  reinoud 	u_int old_cpsr;
    476  1.1  reinoud {
    477  1.1  reinoud 	int mask = I32_bit | F32_bit;
    478  1.1  reinoud 	return(SetCPSR(mask, old_cpsr & mask));
    479  1.1  reinoud }
    480  1.1  reinoud 
    481  1.1  reinoud 
    482  1.1  reinoud u_int
    483  1.1  reinoud enable_interrupts(mask)
    484  1.1  reinoud 	u_int mask;
    485  1.1  reinoud {
    486  1.1  reinoud 	return(SetCPSR(mask, 0));
    487  1.1  reinoud }
    488  1.1  reinoud #endif
    489  1.1  reinoud 
    490  1.1  reinoud /*
    491  1.1  reinoud  * void disable_irq(int irq)
    492  1.1  reinoud  *
    493  1.1  reinoud  * Disables a specific irq. The irq is removed from the master irq mask
    494  1.1  reinoud  */
    495  1.1  reinoud 
    496  1.1  reinoud void
    497  1.1  reinoud disable_irq(irq)
    498  1.1  reinoud 	int irq;
    499  1.1  reinoud {
    500  1.1  reinoud 	u_int oldirqstate;
    501  1.1  reinoud 
    502  1.1  reinoud 	oldirqstate = disable_interrupts(I32_bit);
    503  1.1  reinoud 	current_mask &= ~(1 << irq);
    504  1.1  reinoud 	irq_setmasks();
    505  1.1  reinoud 	restore_interrupts(oldirqstate);
    506  1.1  reinoud }
    507  1.1  reinoud 
    508  1.1  reinoud 
    509  1.1  reinoud /*
    510  1.1  reinoud  * void enable_irq(int irq)
    511  1.1  reinoud  *
    512  1.1  reinoud  * Enables a specific irq. The irq is added to the master irq mask
    513  1.1  reinoud  * This routine should be used with caution. A handler should already
    514  1.1  reinoud  * be installed.
    515  1.1  reinoud  */
    516  1.1  reinoud 
    517  1.1  reinoud void
    518  1.1  reinoud enable_irq(irq)
    519  1.1  reinoud 	int irq;
    520  1.1  reinoud {
    521  1.1  reinoud 	u_int oldirqstate;
    522  1.1  reinoud 
    523  1.1  reinoud 	oldirqstate = disable_interrupts(I32_bit);
    524  1.1  reinoud 	current_mask |= (1 << irq);
    525  1.1  reinoud 	irq_setmasks();
    526  1.1  reinoud 	restore_interrupts(oldirqstate);
    527  1.1  reinoud }
    528  1.1  reinoud 
    529  1.1  reinoud 
    530  1.1  reinoud /*
    531  1.1  reinoud  * void stray_irqhandler(u_int mask)
    532  1.1  reinoud  *
    533  1.1  reinoud  * Handler for stray interrupts. This gets called if a handler cannot be
    534  1.1  reinoud  * found for an interrupt.
    535  1.1  reinoud  */
    536  1.1  reinoud 
    537  1.1  reinoud void
    538  1.1  reinoud stray_irqhandler(mask)
    539  1.1  reinoud 	u_int mask;
    540  1.1  reinoud {
    541  1.1  reinoud 	static u_int stray_irqs = 0;
    542  1.1  reinoud 
    543  1.1  reinoud 	if (++stray_irqs <= 8)
    544  1.1  reinoud 		log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
    545  1.1  reinoud 		    stray_irqs >= 8 ? ": stopped logging" : "");
    546  1.1  reinoud }
    547  1.1  reinoud 
    548  1.1  reinoud 
    549  1.1  reinoud /*
    550  1.1  reinoud  * int fiq_claim(fiqhandler_t *handler)
    551  1.1  reinoud  *
    552  1.1  reinoud  * Claim FIQ's and install a handler for them.
    553  1.1  reinoud  */
    554  1.1  reinoud 
    555  1.1  reinoud int
    556  1.1  reinoud fiq_claim(handler)
    557  1.1  reinoud 	fiqhandler_t *handler;
    558  1.1  reinoud {
    559  1.1  reinoud 	/* Fail if the FIQ's are already claimed */
    560  1.1  reinoud 	if (fiqhandlers)
    561  1.1  reinoud 		return(-1);
    562  1.1  reinoud 
    563  1.1  reinoud 	if (handler->fh_size > 0xc0)
    564  1.1  reinoud 		return(-1);
    565  1.1  reinoud 
    566  1.1  reinoud 	/* Install the handler */
    567  1.1  reinoud 	fiqhandlers = handler;
    568  1.1  reinoud 
    569  1.1  reinoud 	/* Now we have to actually install the FIQ handler */
    570  1.1  reinoud 
    571  1.1  reinoud 	/* Eventually we will copy this down but for the moment ... */
    572  1.1  reinoud 	zero_page_readwrite();
    573  1.1  reinoud 
    574  1.1  reinoud 	WriteWord(0x0000003c, (u_int) handler->fh_func);
    575  1.1  reinoud 
    576  1.1  reinoud 	zero_page_readonly();
    577  1.1  reinoud 
    578  1.1  reinoud 	/* We must now set up the FIQ registers */
    579  1.1  reinoud 	fiq_setregs(handler);
    580  1.1  reinoud 
    581  1.1  reinoud 	/* Set up the FIQ mask */
    582  1.1  reinoud 	IOMD_WRITE_BYTE(IOMD_FIQMSK, handler->fh_mask);
    583  1.1  reinoud 
    584  1.1  reinoud 	/* Make sure that the FIQ's are enabled */
    585  1.1  reinoud 	enable_interrupts(F32_bit);
    586  1.1  reinoud 	return(0);
    587  1.1  reinoud }
    588  1.1  reinoud 
    589  1.1  reinoud 
    590  1.1  reinoud /*
    591  1.1  reinoud  * int fiq_release(fiqhandler_t *handler)
    592  1.1  reinoud  *
    593  1.1  reinoud  * Release FIQ's and remove a handler for them.
    594  1.1  reinoud  */
    595  1.1  reinoud 
    596  1.1  reinoud int
    597  1.1  reinoud fiq_release(handler)
    598  1.1  reinoud 	fiqhandler_t *handler;
    599  1.1  reinoud {
    600  1.1  reinoud 	/* Fail if the handler is wrong */
    601  1.1  reinoud 	if (fiqhandlers != handler)
    602  1.1  reinoud 		return(-1);
    603  1.1  reinoud 
    604  1.1  reinoud 	/* Disable FIQ interrupts */
    605  1.1  reinoud 	disable_interrupts(F32_bit);
    606  1.1  reinoud 
    607  1.1  reinoud 	/* Clear up the FIQ mask */
    608  1.1  reinoud 	IOMD_WRITE_BYTE(IOMD_FIQMSK, 0x00);
    609  1.1  reinoud 
    610  1.1  reinoud 	/* Retrieve the FIQ registers */
    611  1.1  reinoud 	fiq_getregs(handler);
    612  1.1  reinoud 
    613  1.1  reinoud 	/* Remove the handler */
    614  1.1  reinoud 	fiqhandlers = NULL;
    615  1.1  reinoud 	return(0);
    616  1.1  reinoud }
    617  1.1  reinoud 
    618  1.1  reinoud /* End of irqhandler.c */
    619