Move kfile interface to the io/ directory.
[bertos.git] / bertos / cpu / arm / drv / sysirq_at91.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * \brief System IRQ handler for Atmel AT91 ARM7 processors.
38  *
39  * In Atmel AT91 ARM7TDMI processors, there are various
40  * peripheral interrupt sources.
41  * In general, every source has its own interrupt vector, so it
42  * is possible to assign a specific handler for each interrupt
43  * independently.
44  * However, there are a few sources called "system sources" that
45  * share a common IRQ line and vector, called "system IRQ".
46  * So a unique system IRQ dispatcher is implemented here.
47  * This module also contains an interface to manage every source
48  * independently. It is possible to assign to every system IRQ
49  * a specific IRQ handler.
50  *
51  * \see sysirq_setHandler
52  * \see sysirq_setEnable
53  */
54
55 #include "sysirq_at91.h"
56 #include <io/arm.h>
57 #include <cpu/irq.h>
58 #include <cpu/types.h>
59 #include <cfg/module.h>
60 #include <cfg/macros.h>
61
62 /**
63  * Enable/disable the Periodic Interrupt Timer
64  * interrupt.
65  */
66 INLINE void pit_setEnable(bool enable)
67 {
68         if (enable)
69                 PIT_MR |= BV(PITIEN);
70         else
71                 PIT_MR &= ~BV(PITIEN);
72 }
73
74 /**
75  * Table containing all system irqs.
76  */
77 static SysIrq sysirq_tab[] =
78 {
79         /* PIT, Periodic Interval Timer (System timer)*/
80         {
81                 .enabled = false,
82                 .setEnable = pit_setEnable,
83                 .handler = NULL,
84         },
85         /* TODO: add other system sources here */
86 };
87
88 STATIC_ASSERT(countof(sysirq_tab) == SYSIRQ_CNT);
89
90 /**
91  * System IRQ dispatcher.
92  * This is the entry point for all system IRQs in AT91.
93  * This function checks for interrupt enable state of
94  * various sources (system timer, etc..) and calls
95  * the corresponding handler.
96  *
97  * \note On AT91SAM7, all system IRQs (timer included) are handled
98  * by the sysirq_dispatcher, so we can't differentiate between
99  * context-switch and non-context-switch ISR inside this
100  * class of IRQs.
101  */
102 static DECLARE_ISR_CONTEXT_SWITCH(sysirq_dispatcher)
103 {
104         unsigned int i;
105
106         for (i = 0; i < countof(sysirq_tab); i++)
107         {
108                 if (sysirq_tab[i].enabled
109                  && sysirq_tab[i].handler)
110                         sysirq_tab[i].handler();
111         }
112
113         /* Inform hw that we have served the IRQ */
114         AIC_EOICR = 0;
115 }
116
117 #define SYSIRQ_PRIORITY 0 ///< default priority for system irqs.
118
119
120 MOD_DEFINE(sysirq);
121
122 /**
123  * Init system IRQ handling.
124  * \note all system interrupts are disabled.
125  */
126 void sysirq_init(void)
127 {
128         cpu_flags_t flags;
129         IRQ_SAVE_DISABLE(flags);
130
131         /* Disable all system interrupts */
132         for (unsigned i = 0; i < countof(sysirq_tab); i++)
133                 sysirq_tab[i].setEnable(false);
134
135         /* Set the vector. */
136         AIC_SVR(SYSC_ID) = sysirq_dispatcher;
137         /* Initialize to edge triggered with defined priority. */
138         AIC_SMR(SYSC_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SYSIRQ_PRIORITY;
139         /* Clear pending interrupt */
140         AIC_ICCR = BV(SYSC_ID);
141         /* Enable the system IRQ */
142         AIC_IECR = BV(SYSC_ID);
143
144         IRQ_RESTORE(flags);
145         MOD_INIT(sysirq);
146 }
147
148
149 /**
150  * Helper function used to set handler for system IRQ \a irq.
151  */
152 void sysirq_setHandler(sysirq_t irq, sysirq_handler_t handler)
153 {
154         ASSERT(irq < SYSIRQ_CNT);
155         sysirq_tab[irq].handler = handler;
156 }
157
158 /**
159  * Helper function used to enable/disable system IRQ \a irq.
160  */
161 void sysirq_setEnable(sysirq_t irq, bool enable)
162 {
163         ASSERT(irq < SYSIRQ_CNT);
164
165         sysirq_tab[irq].setEnable(enable);
166         sysirq_tab[irq].enabled = enable;
167 }
168
169 /**
170  * Helper function used to get system IRQ \a irq state.
171  */
172 bool sysirq_enabled(sysirq_t irq)
173 {
174         ASSERT(irq < SYSIRQ_CNT);
175
176         return sysirq_tab[irq].enabled;
177 }