1 #ifndef __ALT_IRQ_H__
2 #define __ALT_IRQ_H__
3 
4 /******************************************************************************
5 *                                                                             *
6 * License Agreement                                                           *
7 *                                                                             *
8 * Copyright (c) 2009 Altera Corporation, San Jose, California, USA.           *
9 * All rights reserved.                                                        *
10 *                                                                             *
11 * Permission is hereby granted, free of charge, to any person obtaining a     *
12 * copy of this software and associated documentation files (the "Software"),  *
13 * to deal in the Software without restriction, including without limitation   *
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
15 * and/or sell copies of the Software, and to permit persons to whom the       *
16 * Software is furnished to do so, subject to the following conditions:        *
17 *                                                                             *
18 * The above copyright notice and this permission notice shall be included in  *
19 * all copies or substantial portions of the Software.                         *
20 *                                                                             *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
27 * DEALINGS IN THE SOFTWARE.                                                   *
28 *                                                                             *
29 *                                                                             *
30 ******************************************************************************/
31 
32 /*
33  * alt_irq.h is the Nios II specific implementation of the interrupt controller
34  * interface.
35  *
36  * Nios II includes optional support for an external interrupt controller.
37  * When an external controller is present, the "Enhanced" interrupt API
38  * must be used to manage individual interrupts. The enhanced API also
39  * supports the processor's internal interrupt controller. Certain API
40  * members are accessible from either the "legacy" or "enhanced" interrpt
41  * API.
42  *
43  * Regardless of which API is in use, this file should be included by
44  * application code and device drivers that register ISRs or manage interrpts.
45  */
46 #include <errno.h>
47 
48 #include "nios2.h"
49 #include "alt_types.h"
50 #include "system.h"
51 
52 #ifdef __cplusplus
53 extern "C"
54 {
55 #endif /* __cplusplus */
56 
57 /*
58  * Macros used by alt_irq_enabled
59  */
60 #define ALT_IRQ_ENABLED  1
61 #define ALT_IRQ_DISABLED 0
62 
63 /*
64  * Number of available interrupts in internal interrupt controller.
65  */
66 #define ALT_NIRQ NIOS2_NIRQ
67 
68 /*
69  * Used by alt_irq_disable_all() and alt_irq_enable_all().
70  */
71 typedef int alt_irq_context;
72 
73 /* ISR Prototype */
74 #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
75 typedef void (*alt_isr_func)(void* isr_context);
76 #else
77 typedef void (*alt_isr_func)(void* isr_context, alt_u32 id);
78 #endif
79 
80 /*
81  * The following protypes and routines are supported by both
82  * the enhanced and legacy interrupt APIs
83  */
84 
85 /*
86  * alt_irq_enabled can be called to determine if the processor's global
87  * interrupt enable is asserted. The return value is zero if interrupts
88  * are disabled, and non-zero otherwise.
89  *
90  * Whether the internal or external interrupt controller is present,
91  * individual interrupts may still be disabled. Use the other API to query
92  * a specific interrupt.
93  */
alt_irq_enabled(void)94 static ALT_INLINE int ALT_ALWAYS_INLINE alt_irq_enabled (void)
95 {
96   int status;
97 
98   NIOS2_READ_STATUS (status);
99 
100   return status & NIOS2_STATUS_PIE_MSK;
101 }
102 
103 /*
104  * alt_irq_disable_all()
105  *
106  * This routine inhibits all interrupts by negating the status register PIE
107  * bit. It returns the previous contents of the CPU status register (IRQ
108  * context) which can be used to restore the status register PIE bit to its
109  * state before this routine was called.
110  */
111 static ALT_INLINE alt_irq_context ALT_ALWAYS_INLINE
alt_irq_disable_all(void)112        alt_irq_disable_all (void)
113 {
114   alt_irq_context context;
115 
116   NIOS2_READ_STATUS (context);
117 
118   NIOS2_WRITE_STATUS (context & ~NIOS2_STATUS_PIE_MSK);
119 
120   return context;
121 }
122 
123 /*
124  * alt_irq_enable_all()
125  *
126  * Enable all interrupts that were previously disabled by alt_irq_disable_all()
127  *
128  * This routine accepts a context to restore the CPU status register PIE bit
129  * to the state prior to a call to alt_irq_disable_all().
130 
131  * In the case of nested calls to alt_irq_disable_all()/alt_irq_enable_all(),
132  * this means that alt_irq_enable_all() does not necessarily re-enable
133  * interrupts.
134  *
135  * This routine will perform a read-modify-write sequence to restore only
136  * status.PIE if the processor is configured with options that add additional
137  * writeable status register bits. These include the MMU, MPU, the enhanced
138  * interrupt controller port, and shadow registers. Otherwise, as a performance
139  * enhancement, status is overwritten with the prior context.
140  */
141 static ALT_INLINE void ALT_ALWAYS_INLINE
alt_irq_enable_all(alt_irq_context context)142        alt_irq_enable_all (alt_irq_context context)
143 {
144 #if (NIOS2_NUM_OF_SHADOW_REG_SETS > 0) || (defined NIOS2_EIC_PRESENT) || \
145     (defined NIOS2_MMU_PRESENT) || (defined NIOS2_MPU_PRESENT)
146   alt_irq_context status;
147 
148   NIOS2_READ_STATUS (status);
149 
150   status &= ~NIOS2_STATUS_PIE_MSK;
151   status |= (context & NIOS2_STATUS_PIE_MSK);
152 
153   NIOS2_WRITE_STATUS (status);
154 #else
155   NIOS2_WRITE_STATUS (context);
156 #endif
157 }
158 
159 /*
160  * The function alt_irq_init() is defined within the auto-generated file
161  * alt_sys_init.c. This function calls the initilization macros for all
162  * interrupt controllers in the system at config time, before any other
163  * non-interrupt controller driver is initialized.
164  *
165  * The "base" parameter is ignored and only present for backwards-compatibility.
166  * It is recommended that NULL is passed in for the "base" parameter.
167  */
168 extern void alt_irq_init (const void* base);
169 
170 /*
171  * alt_irq_cpu_enable_interrupts() enables the CPU to start taking interrupts.
172  */
173 static ALT_INLINE void ALT_ALWAYS_INLINE
alt_irq_cpu_enable_interrupts(void)174        alt_irq_cpu_enable_interrupts (void)
175 {
176     NIOS2_WRITE_STATUS(NIOS2_STATUS_PIE_MSK
177 #if defined(NIOS2_EIC_PRESENT) && (NIOS2_NUM_OF_SHADOW_REG_SETS > 0)
178     | NIOS2_STATUS_RSIE_MSK
179 #endif
180       );
181 }
182 
183 
184 /*
185  * Prototypes for the enhanced interrupt API.
186  */
187 #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
188 /*
189  * alt_ic_isr_register() can be used to register an interrupt handler. If the
190  * function is succesful, then the requested interrupt will be enabled upon
191  * return.
192  */
193 extern int alt_ic_isr_register(alt_u32 ic_id,
194                         alt_u32 irq,
195                         alt_isr_func isr,
196                         void *isr_context,
197                         void *flags);
198 
199 /*
200  * alt_ic_irq_enable() and alt_ic_irq_disable() enable/disable a specific
201  * interrupt by using IRQ port and interrupt controller instance.
202  */
203 int alt_ic_irq_enable (alt_u32 ic_id, alt_u32 irq);
204 int alt_ic_irq_disable(alt_u32 ic_id, alt_u32 irq);
205 
206  /*
207  * alt_ic_irq_enabled() indicates whether a specific interrupt, as
208  * specified by IRQ port and interrupt controller instance is enabled.
209  */
210 alt_u32 alt_ic_irq_enabled(alt_u32 ic_id, alt_u32 irq);
211 
212 #else
213 /*
214  * Prototypes for the legacy interrupt API.
215  */
216 #include "priv/alt_legacy_irq.h"
217 #endif
218 
219 
220 /*
221  * alt_irq_pending() returns a bit list of the current pending interrupts.
222  * This is used by alt_irq_handler() to determine which registered interrupt
223  * handlers should be called.
224  *
225  * This routine is only available for the Nios II internal interrupt
226  * controller.
227  */
228 #ifndef NIOS2_EIC_PRESENT
alt_irq_pending(void)229 static ALT_INLINE alt_u32 ALT_ALWAYS_INLINE alt_irq_pending (void)
230 {
231   alt_u32 active;
232 
233   NIOS2_READ_IPENDING (active);
234 
235   return active;
236 }
237 #endif
238 
239 #ifdef __cplusplus
240 }
241 #endif /* __cplusplus */
242 
243 #endif /* __ALT_IRQ_H__ */
244