Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / magpie_1_1 / sboot / athos / src / xtos / int-sethandler.c
1 /* int-sethandler.c - register an interrupt handler in XTOS */
2
3 /*
4  * Copyright (c) 1999-2006 Tensilica Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <xtensa/config/core.h>
27 #include <xtensa/config/specreg.h>
28 #include "xtos-internal.h"
29
30
31 #if XCHAL_HAVE_INTERRUPTS
32 /*
33  *  Table of interrupt handlers.
34  *  NOTE:  if the NSA/NSAU instructions are configured, then to save
35  *  a few cycles in the interrupt dispatcher code, the
36  *  _xtos_interrupt_table[] array is filled in reverse.
37  *  IMPORTANT:  Use the MAPINT() macro defined in xtos-internal.h to index entries in this array.
38  */
39 extern XtosIntHandlerEntry      _xtos_interrupt_table[XCHAL_NUM_INTERRUPTS];
40 extern void                     _xtos_unhandled_interrupt();
41 #endif
42
43
44 _xtos_handler _xtos_set_interrupt_handler_arg( int n, _xtos_handler f, void *arg )
45 {
46 #if XCHAL_HAVE_INTERRUPTS
47     XtosIntHandlerEntry *entry;
48     _xtos_handler old;
49
50     if( n < 0 || n >= XCHAL_NUM_INTERRUPTS )
51         return 0;       /* invalid interrupt number */
52     if( Xthal_intlevel[n] > XTOS_LOCKLEVEL )
53         return 0;       /* priority level too high to safely handle in C */
54     entry = _xtos_interrupt_table + MAPINT(n);
55     old = entry->handler;
56     if (f) {
57         entry->handler = f;
58         entry->arg = arg;
59     } else {
60         entry->handler = &_xtos_unhandled_interrupt;
61         entry->arg = (void*)n;
62     }
63     return ((old == &_xtos_unhandled_interrupt) ? 0 : old);
64 #else
65     return 0;
66 #endif
67 }
68
69
70 _xtos_handler _xtos_set_interrupt_handler( int n, _xtos_handler f )
71 {
72     return _xtos_set_interrupt_handler_arg( n, f, (void*) n );
73 }
74
75
76 #if 0
77 /*
78  *  User vector mode exception handler for the LEVEL1_INTERRUPT cause.
79  *  NOTE:  this is now implemented in assembler for performance.
80  *  This C handler is left as an example interrupt dispatcher written in C.
81  *  The actual handler in int-lowpri-dispatcher.S is more fully featured.
82  */
83 UserFrame* _xtos_p_level1int_handler( UserFrame* uf /*, int cause */ )
84 {
85 #if XCHAL_HAVE_INTERRUPTS
86     unsigned int ints;
87     unsigned int index;
88
89     ints = xthal_get_interrupt();
90 # if XTOS_VIRTUAL_INTENABLE
91     ints &= _xtos_enabled;
92 # else
93     ints &= xthal_get_intenable();
94 # endif
95     for( index = 0 ; ints != 0 ; ints >>= 1, index++ )
96     {
97         if( ints & 1 )
98         {
99             void (*f)();
100
101             /*
102              *  Clear interrupt (in case it's edge-triggered or software or write-error).
103              *  This must be done *before* processing the interrupt.
104              */
105             xthal_set_intclear( 1 << index );
106
107             f = _xtos_interrupt_table[MAPINT(index)].handler;
108             if( f )
109                 f( _xtos_interrupt_table[MAPINT(index)].arg /*, uf, index*/ );
110         }
111     }
112 #endif /*XCHAL_HAVE_INTERRUPTS*/
113     return uf;
114 }
115 #endif
116