Refactor to use new protocol module and sipo.
[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  *
34  * \author Francesco Sacchi <batt@develer.com>
35  *
36  * \brief System IRQ handler for Atmel AT91 ARM7 processors.
37  *
38  * In Atmel AT91 ARM7TDMI processors, there are various
39  * peripheral interrupt sources.
40  * In general, every source has its own interrupt vector, so it
41  * is possible to assign a specific handler for each interrupt
42  * independently.
43  * However, there are a few sources called "system sources" that
44  * share a common IRQ line and vector, called "system IRQ".
45  * So a unique system IRQ dispatcher is implemented here.
46  * This module also contains an interface to manage every source
47  * independently. It is possible to assign to every system IRQ
48  * a specific IRQ handler.
49  *
50  * \see sysirq_setHandler
51  * \see sysirq_setEnable
52  */
53
54 #include "sysirq_at91.h"
55 #include <io/arm.h>
56 #include <cpu/irq.h>
57 #include <cpu/types.h>
58 #include <cfg/module.h>
59 #include <cfg/macros.h>
60
61 /**
62  * Enable/disable the Periodic Interrupt Timer
63  * interrupt.
64  */
65 INLINE void pit_setEnable(bool enable)
66 {
67         if (enable)
68                 PIT_MR |= BV(PITIEN);
69         else
70                 PIT_MR &= ~BV(PITIEN);
71 }
72
73 /**
74  * Table containing all system irqs.
75  */
76 static SysIrq sysirq_tab[] =
77 {
78         /* PIT, Periodic Interval Timer (System timer)*/
79         {
80                 .enabled = false,
81                 .setEnable = pit_setEnable,
82                 .handler = NULL,
83         },
84         /* TODO: add other system sources here */
85 };
86
87 STATIC_ASSERT(countof(sysirq_tab) == SYSIRQ_CNT);
88
89 /**
90  * System IRQ dispatcher.
91  * This is the entry point for all system IRQs in AT91.
92  * This function checks for interrupt enable state of
93  * various sources (system timer, etc..) and calls
94  * the corresponding handler.
95  *
96  * \note On AT91SAM7, all system IRQs (timer included) are handled
97  * by the sysirq_dispatcher, so we can't differentiate between
98  * context-switch and non-context-switch ISR inside this
99  * class of IRQs.
100  */
101 static DECLARE_ISR_CONTEXT_SWITCH(sysirq_dispatcher)
102 {
103         unsigned int i;
104
105         for (i = 0; i < countof(sysirq_tab); i++)
106         {
107                 if (sysirq_tab[i].enabled
108                  && sysirq_tab[i].handler)
109                         sysirq_tab[i].handler();
110         }
111
112         /* Inform hw that we have served the IRQ */
113         AIC_EOICR = 0;
114 }
115
116 #define SYSIRQ_PRIORITY 0 ///< default priority for system irqs.
117
118
119 MOD_DEFINE(sysirq);
120
121 /**
122  * Init system IRQ handling.
123  * \note all system interrupts are disabled.
124  */
125 void sysirq_init(void)
126 {
127         cpu_flags_t flags;
128         IRQ_SAVE_DISABLE(flags);
129
130         /* Disable all system interrupts */
131         for (unsigned i = 0; i < countof(sysirq_tab); i++)
132                 sysirq_tab[i].setEnable(false);
133
134         /* Set the vector. */
135         AIC_SVR(SYSC_ID) = sysirq_dispatcher;
136         /* Initialize to edge triggered with defined priority. */
137         AIC_SMR(SYSC_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SYSIRQ_PRIORITY;
138         /* Clear pending interrupt */
139         AIC_ICCR = BV(SYSC_ID);
140         /* Enable the system IRQ */
141         AIC_IECR = BV(SYSC_ID);
142
143         IRQ_RESTORE(flags);
144         MOD_INIT(sysirq);
145 }
146
147
148 /**
149  * Helper function used to set handler for system IRQ \a irq.
150  */
151 void sysirq_setHandler(sysirq_t irq, sysirq_handler_t handler)
152 {
153         ASSERT(irq < SYSIRQ_CNT);
154         sysirq_tab[irq].handler = handler;
155 }
156
157 /**
158  * Helper function used to enable/disable system IRQ \a irq.
159  */
160 void sysirq_setEnable(sysirq_t irq, bool enable)
161 {
162         ASSERT(irq < SYSIRQ_CNT);
163
164         sysirq_tab[irq].setEnable(enable);
165         sysirq_tab[irq].enabled = enable;
166 }
167
168 /**
169  * Helper function used to get system IRQ \a irq state.
170  */
171 bool sysirq_enabled(sysirq_t irq)
172 {
173         ASSERT(irq < SYSIRQ_CNT);
174
175         return sysirq_tab[irq].enabled;
176 }