blob: 730618f0b149651d11bf6b6f52a7a9f825fd8c48 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <err.h>
24#include <sys/types.h>
25#include <debug.h>
26#include <reg.h>
27#include <kernel/thread.h>
28#include <platform/interrupts.h>
29#include <arch/ops.h>
30#include <arch/arm.h>
31#include "platform_p.h"
32#include <platform/omap5912.h>
33
34struct int_handler_struct {
35 int_handler handler;
36 void *arg;
37};
38
39static struct int_handler_struct int_handler_table[INT_VECTORS];
40
41static const uint32_t icBase[5] = {
42 INTCON0_BASE, INTCON1_BASE, INTCON2_BASE, INTCON3_BASE, INTCON4_BASE };
43
44/* a bitmap of the level triggered interrupt vectors */
45static uint32_t level_trigger[5] = {
46 0xb3fefe8f, // level 1 0-31
47 0xfdb3c1fd, // level 2 0-31
48 0xfffff7ff, // level 2 32-63
49 0xbfffffff, // level 2 64-95
50 0xffffffff // level 2 96-128
51};
52
53static inline volatile uint32_t *ICReg(uint controller, uint reg)
54{
55 return (volatile uint32_t *)(icBase[controller] + reg);
56}
57
58static inline uint32_t readICReg(uint controller, uint reg)
59{
60 return *ICReg(controller, reg);
61}
62static inline void writeICReg(uint controller, uint reg, uint val)
63{
64 *ICReg(controller, reg) = val;
65}
66
67static inline uint vectorToController(uint vector)
68{
69 return vector / 32;
70}
71
72void platform_init_interrupts(void)
73{
74 unsigned int i;
75
76 // mask all interrupts
77 *ICReg(0, INTCON_MIR) = 0xfffffffa;
78 *ICReg(1, INTCON_MIR) = 0xffffffff;
79 *ICReg(2, INTCON_MIR) = 0xffffffff;
80 *ICReg(3, INTCON_MIR) = 0xffffffff;
81 *ICReg(4, INTCON_MIR) = 0xffffffff;
82
83 // set up each of the interrupts
84 for (i = 0; i < INT_VECTORS; i++) {
85 // set each vector up as high priority, IRQ, and default edge/level sensitivity
86 *ICReg(i / 32, INTCON_ILR_BASE + 4*(i%32)) = ((level_trigger[i/32] & (1<<(i%32))) ? (1<<1) : (0<<1)) | 0;
87 }
88
89 // clear any pending interrupts
90 *ICReg(0, INTCON_ITR) = 0;
91 *ICReg(1, INTCON_ITR) = 0;
92 *ICReg(2, INTCON_ITR) = 0;
93 *ICReg(3, INTCON_ITR) = 0;
94 *ICReg(4, INTCON_ITR) = 0;
95
96 // globally unmask interrupts
97 *ICReg(1, INTCON_CONTROL) = 3;
98 *ICReg(0, INTCON_CONTROL) = 3;
99 *ICReg(0, INTCON_GMR) = 0;
100}
101
Travis Geiselbrechtf1cdea32008-09-13 14:13:54 -0700102status_t mask_interrupt(unsigned int vector)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700103{
104 if (vector >= INT_VECTORS)
105 return ERR_INVALID_ARGS;
106
107// dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector);
108
109 enter_critical_section();
110
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700111 volatile uint32_t *mir = ICReg(vectorToController(vector), INTCON_MIR);
112 *mir = *mir | (1<<(vector % 32));
113
114 exit_critical_section();
115
116 return NO_ERROR;
117}
118
Travis Geiselbrechtf1cdea32008-09-13 14:13:54 -0700119status_t unmask_interrupt(unsigned int vector)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700120{
121 if (vector >= INT_VECTORS)
122 return ERR_INVALID_ARGS;
123
124// dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector);
125
126 enter_critical_section();
127
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700128 volatile uint32_t *mir = ICReg(vectorToController(vector), INTCON_MIR);
129 *mir = *mir & ~(1<<(vector % 32));
130
131 exit_critical_section();
132
133 return NO_ERROR;
134}
135
136enum handler_return platform_irq(struct arm_iframe *frame)
137{
138 // get the current vector
139 unsigned int vector;
140
141#if THREAD_STATS
142 thread_stats.interrupts++;
143#endif
144
145 // read from the first level int handler
146 vector = *ICReg(0, INTCON_SIR_IRQ);
147
148 // see if it's coming from the second level handler
149 if (vector == 0) {
150 vector = *ICReg(1, INTCON_SIR_IRQ) + 32;
151 }
152
153// dprintf("platform_irq: spsr 0x%x, pc 0x%x, currthread %p, vector %d\n", frame->spsr, frame->pc, current_thread, vector);
154
155 // deliver the interrupt
156 enum handler_return ret;
157
158 ret = INT_NO_RESCHEDULE;
159 if (int_handler_table[vector].handler)
160 ret = int_handler_table[vector].handler(int_handler_table[vector].arg);
161
162 // ack the interrupt
163 if (vector >= 32) {
164 // interrupt is chained, so ack the second level first, and then the first
165 *ICReg(vector / 32, INTCON_ITR) = ~(1 << (vector % 32));
166 *ICReg(1, INTCON_CONTROL) |= 1;
167 vector = 0; // force the following code to ack the chained first level vector
168 }
169
170 *ICReg(0, INTCON_ITR) = ~(1 << vector);
171 *ICReg(0, INTCON_CONTROL) = 1;
172
173 return ret;
174}
175
176void platform_fiq(struct arm_iframe *frame)
177{
178 PANIC_UNIMPLEMENTED;
179}
180
181void register_int_handler(unsigned int vector, int_handler handler, void *arg)
182{
183 if (vector >= INT_VECTORS)
184 panic("register_int_handler: vector out of range %d\n", vector);
185
186 enter_critical_section();
187
188 int_handler_table[vector].handler = handler;
189 int_handler_table[vector].arg = arg;
190
191 exit_critical_section();
192}
193