-
Notifications
You must be signed in to change notification settings - Fork 138
/
interrupt.c
137 lines (113 loc) · 4.4 KB
/
interrupt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "openiboot.h"
#include "interrupt.h"
#include "hardware/interrupt.h"
#include "util.h"
#include "arm/arm.h"
int interrupt_setup() {
SET_REG(VIC0 + VICINTENCLEAR, 0xFFFFFFFF); // disable all interrupts
SET_REG(VIC1 + VICINTENCLEAR, 0xFFFFFFFF);
SET_REG(VIC2 + VICINTENCLEAR, 0xFFFFFFFF);
SET_REG(VIC3 + VICINTENCLEAR, 0xFFFFFFFF);
SET_REG(VIC0 + VICINTENABLE, 0);
SET_REG(VIC1 + VICINTENABLE, 0);
SET_REG(VIC2 + VICINTENABLE, 0);
SET_REG(VIC3 + VICINTENABLE, 0);
SET_REG(VIC0 + VICINTSELECT, 0); // 0 means to use IRQs, 1 means to use FIQs, so use all IRQs
SET_REG(VIC1 + VICINTSELECT, 0);
SET_REG(VIC2 + VICINTSELECT, 0);
SET_REG(VIC3 + VICINTSELECT, 0);
SET_REG(VIC0 + VICSWPRIORITYMASK, 0xffff); // unmask all 16 interrupt levels
SET_REG(VIC1 + VICSWPRIORITYMASK, 0xffff);
SET_REG(VIC2 + VICSWPRIORITYMASK, 0xffff);
SET_REG(VIC3 + VICSWPRIORITYMASK, 0xffff);
// Set interrupt vector addresses to the interrupt number. This will signal the interrupt handler to consult the handler tables
int i;
for(i = 0; i < VIC_InterruptSeparator; i++) {
SET_REG(VIC0 + VICVECTADDRS + (i * 4), i);
SET_REG(VIC1 + VICVECTADDRS + (i * 4), VIC_InterruptSeparator + i);
SET_REG(VIC2 + VICVECTADDRS + (i * 4), VIC_InterruptSeparator*2 + i);
SET_REG(VIC3 + VICVECTADDRS + (i * 4), VIC_InterruptSeparator*3 + i);
}
memset(InterruptHandlerTable, 0, sizeof(InterruptHandlerTable));
return 0;
}
int interrupt_install(int irq_no, InterruptServiceRoutine handler, uint32_t token) {
if(irq_no >= VIC_MaxInterrupt) {
return -1;
}
EnterCriticalSection();
InterruptHandlerTable[irq_no].handler = handler;
InterruptHandlerTable[irq_no].token = token;
InterruptHandlerTable[irq_no].useEdgeIC = 0;
LeaveCriticalSection();
return 0;
}
int interrupt_enable(int irq_no) {
if(irq_no >= VIC_MaxInterrupt) {
return -1;
}
EnterCriticalSection();
if(irq_no < VIC_InterruptSeparator) {
SET_REG(VIC0 + VICINTENABLE, GET_REG(VIC0 + VICINTENABLE) | (1 << irq_no));
} else if(irq_no < VIC_InterruptSeparator*2) {
SET_REG(VIC1 + VICINTENABLE, GET_REG(VIC1 + VICINTENABLE) | (1 << (irq_no - VIC_InterruptSeparator)));
} else if(irq_no < VIC_InterruptSeparator*3) {
SET_REG(VIC2 + VICINTENABLE, GET_REG(VIC2 + VICINTENABLE) | (1 << (irq_no - VIC_InterruptSeparator*2)));
} else if(irq_no < VIC_InterruptSeparator*4) {
SET_REG(VIC3 + VICINTENABLE, GET_REG(VIC3 + VICINTENABLE) | (1 << (irq_no - VIC_InterruptSeparator*3)));
}
LeaveCriticalSection();
return 0;
}
int interrupt_disable(int irq_no) {
if(irq_no >= VIC_MaxInterrupt) {
return -1;
}
EnterCriticalSection();
if(irq_no < VIC_InterruptSeparator) {
SET_REG(VIC0 + VICINTENABLE, GET_REG(VIC0 + VICINTENABLE) & ~(1 << irq_no));
} else if(irq_no < VIC_InterruptSeparator*2) {
SET_REG(VIC1 + VICINTENABLE, GET_REG(VIC1 + VICINTENABLE) & ~(1 << (irq_no - VIC_InterruptSeparator)));
} else if(irq_no < VIC_InterruptSeparator*3) {
SET_REG(VIC2 + VICINTENABLE, GET_REG(VIC2 + VICINTENABLE) & ~(1 << (irq_no - VIC_InterruptSeparator*2)));
} else if(irq_no < VIC_InterruptSeparator*4) {
SET_REG(VIC3 + VICINTENABLE, GET_REG(VIC3 + VICINTENABLE) & ~(1 << (irq_no - VIC_InterruptSeparator*3)));
}
LeaveCriticalSection();
return 0;
}
int interrupt_set_int_type(int irq_no, uint8_t type) {
if(irq_no >= VIC_MaxInterrupt) {
return -1;
}
int vic = (irq_no >> 5);
int irq = (irq_no & 0x1F);
EnterCriticalSection();
if (type & 1) {
SET_REG(VIC0 + VICINTSELECT + (vic << 16), GET_REG(VIC0 + VICINTSELECT + (vic << 16)) | 1 << irq);
EnableCPUFIQ();
} else {
SET_REG(VIC0 + VICINTSELECT + (vic << 16), GET_REG(VIC0 + VICINTSELECT + (vic << 16)) & ~(1 << irq));
}
LeaveCriticalSection();
if (type & 2)
return -1; // edge not supported
return 0;
}
int interrupt_clear(int irq_no) {
if(irq_no >= VIC_MaxInterrupt) {
return -1;
}
EnterCriticalSection();
if(irq_no < VIC_InterruptSeparator) {
SET_REG(VIC0 + VICINTENCLEAR, GET_REG(VIC0 + VICINTENCLEAR) | (1 << irq_no));
} else if(irq_no < VIC_InterruptSeparator*2) {
SET_REG(VIC1 + VICINTENCLEAR, GET_REG(VIC1 + VICINTENCLEAR) | (1 << (irq_no - VIC_InterruptSeparator)));
} else if(irq_no < VIC_InterruptSeparator*3) {
SET_REG(VIC2 + VICINTENCLEAR, GET_REG(VIC2 + VICINTENCLEAR) | (1 << (irq_no - VIC_InterruptSeparator*2)));
} else if(irq_no < VIC_InterruptSeparator*4) {
SET_REG(VIC3 + VICINTENCLEAR, GET_REG(VIC3 + VICINTENCLEAR) | (1 << (irq_no - VIC_InterruptSeparator*3)));
}
LeaveCriticalSection();
return 0;
}