Update to new tree.
[bertos.git] / 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/at91sam7s.h>
57 #include <cpu/cpu.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  * \brief Interrupt entry.
91  */
92 #define IRQ_ENTRY() \
93     asm volatile("sub   lr, lr,#4"          "\n\t"  /* Adjust LR */ \
94                  "stmfd sp!,{r0-r12,lr}"    "\n\t"  /* Save registers on IRQ stack. */ \
95                  "mrs   r1, spsr"           "\n\t"  /* Save SPSR */ \
96                  "stmfd sp!,{r1}"           "\n\t")     /* */
97
98 /*!
99  * \brief Interrupt exit.
100  */
101 #define IRQ_EXIT() \
102     asm volatile("ldmfd sp!, {r1}"          "\n\t"  /* Restore SPSR */ \
103                  "msr   spsr_c, r1"         "\n\t"  /* */ \
104                  "ldr   r0, =0xFFFFF000"    "\n\t"  /* End of interrupt. */ \
105                  "str   r0, [r0, #0x130]"   "\n\t"  /* */ \
106                  "ldmfd sp!, {r0-r12, pc}^" "\n\t")     /* Restore registers and return. */
107
108
109 /**
110  * System IRQ dispatcher.
111  * This is the entry point for all system IRQs in AT91.
112  * This function checks for interrupt enable state of
113  * various sources (system timer, etc..) and calls
114  * the corresponding handler.
115  */
116 static void sysirq_dispatcher(void) __attribute__ ((naked));
117 static void sysirq_dispatcher(void)
118 {
119         IRQ_ENTRY();
120         for (unsigned i = 0; i < countof(sysirq_tab); i++)
121         {
122                 if (sysirq_tab[i].enabled
123                  && sysirq_tab[i].handler)
124                         sysirq_tab[i].handler();
125         }
126
127         IRQ_EXIT();
128 }
129
130 #define SYSIRQ_PRIORITY 0 ///< default priority for system irqs.
131
132
133 MOD_DEFINE(sysirq);
134
135 /**
136  * Init system IRQ handling.
137  * \note all system interrupts are disabled.
138  */
139 void sysirq_init(void)
140 {
141         cpuflags_t flags;
142         IRQ_SAVE_DISABLE(flags);
143
144         /* Disable all system interrupts */
145         for (unsigned i = 0; i < countof(sysirq_tab); i++)
146                 sysirq_tab[i].setEnable(false);
147
148         /* Set the vector. */
149         AIC_SVR(SYSC_ID) = sysirq_dispatcher;
150         /* Initialize to edge triggered with defined priority. */
151         AIC_SMR(SYSC_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SYSIRQ_PRIORITY;
152         /* Clear pending interrupt */
153         AIC_ICCR = BV(SYSC_ID);
154         /* Enable the system IRQ */
155         AIC_IECR = BV(SYSC_ID);
156
157         IRQ_RESTORE(flags);
158         MOD_INIT(sysirq);
159 }
160
161
162 /**
163  * Helper function used to set handler for system IRQ \a irq.
164  */
165 void sysirq_setHandler(sysirq_t irq, sysirq_handler_t handler)
166 {
167         ASSERT(irq >= 0);
168         ASSERT(irq < SYSIRQ_CNT);
169         sysirq_tab[irq].handler = handler;
170 }
171
172 /**
173  * Helper function used to enable/disable system IRQ \a irq.
174  */
175 void sysirq_setEnable(sysirq_t irq, bool enable)
176 {
177         ASSERT(irq >= 0);
178         ASSERT(irq < SYSIRQ_CNT);
179
180         sysirq_tab[irq].setEnable(enable);
181         sysirq_tab[irq].enabled = enable;
182 }
183
184 /**
185  * Helper function used to get system IRQ \a irq state.
186  */
187 bool sysirq_enabled(sysirq_t irq)
188 {
189         ASSERT(irq >= 0);
190         ASSERT(irq < SYSIRQ_CNT);
191
192         return sysirq_tab[irq].enabled;
193 }