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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "m8260.h"
#include "../port/error.h"
enum{
IRQ0 = 18,
Level = 0,
Edge = 1,
};
enum{
Qdir,
Qirq1,
Qirq2,
Qirq3,
Qirq4,
Qirq5,
Qirq6,
Qirq7,
Qmstimer,
Qfpgareset,
NIRQ,
};
static Dirtab irqdir[]={
".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
"irq1", {Qirq1}, 0, 0666,
"irq2", {Qirq2}, 0, 0666,
"irq3", {Qirq1}, 0, 0666,
"irq4", {Qirq1}, 0, 0666,
"irq5", {Qirq1}, 0, 0666,
"irq6", {Qirq1}, 0, 0666,
"irq7", {Qirq1}, 0, 0666,
"mstimer", {Qmstimer}, 0, 0666,
"fpgareset", {Qfpgareset}, 0, 0222,
};
enum
{
CMinterrupt,
CMmode,
CMreset,
CMwait,
CMdebug,
};
Cmdtab irqmsg[] =
{
CMinterrupt, "interrupt", 2,
CMmode, "mode", 2,
CMreset, "reset", 1,
CMwait, "wait", 1,
CMdebug, "debug", 1,
};
typedef struct Irqconfig Irqconfig;
struct Irqconfig {
int intenable; /* Interrupts are enabled */
int mode; /* level == 0; edge == 1 */
ulong interrupts; /* Count interrupts */
ulong sleepints; /* interrupt count when waiting */
Rendez r; /* Rendez-vous point for interrupt waiting */
Irqconfig *next;
Timer;
};
Irqconfig *irqconfig[NIRQ]; /* irqconfig[0] is not used */
Lock irqlock;
static void interrupt(Ureg*, void*);
void dumpvno(void);
static void
ticmstimer(Ureg*, Timer *t)
{
Irqconfig *ic;
ic = t->ta;
ic->interrupts++;
wakeup(&ic->r);
}
void
irqenable(Irqconfig *ic, int irq)
{
/* call with ilock(&irqlock) held */
if (ic->intenable)
return;
if (irq == Qmstimer){
if (ic->tnext == nil)
ic->tns = MS2NS(ic->mode);
ic->tmode = Tperiodic;
timeradd(&ic->Timer);
}else{
if (irqconfig[irq]){
ic->next = irqconfig[irq];
irqconfig[irq] = ic;
}else{
ic->next = nil;
irqconfig[irq] = ic;
intrenable(IRQ0 + irq, interrupt, &irqconfig[irq], irqdir[irq].name);
}
}
ic->intenable = 1;
}
void
irqdisable(Irqconfig *ic, int irq)
{
Irqconfig **pic;
/* call with ilock(&irqlock) held */
if (ic->intenable == 0)
return;
if (irq == Qmstimer){
timerdel(&ic->Timer);
}else{
for(pic = &irqconfig[irq]; *pic != ic; pic = &(*pic)->next)
assert(*pic);
*pic = (*pic)->next;
if (irqconfig[irq] == nil)
intrdisable(IRQ0 + irq, interrupt, &irqconfig[irq], irqdir[irq].name);
}
ic->intenable = 0;
}
static Chan*
irqattach(char *spec)
{
return devattach('b', spec);
}
static Walkqid*
irqwalk(Chan *c, Chan *nc, char **name, int nname)
{
return devwalk(c, nc, name,nname, irqdir, nelem(irqdir), devgen);
}
static int
irqstat(Chan *c, uchar *dp, int n)
{
return devstat(c, dp, n, irqdir, nelem(irqdir), devgen);
}
static Chan*
irqopen(Chan *c, int omode)
{
Irqconfig *ic;
int irq;
irq = (ulong)c->qid.path;
if(irq != Qdir){
ic = mallocz(sizeof(Irqconfig), 1);
ic->tf = ticmstimer;
ic->ta = ic;
if (irq == Qmstimer)
ic->mode = 1000;
c->aux = ic;
}
return devopen(c, omode, irqdir, nelem(irqdir), devgen);
}
static void
irqclose(Chan *c)
{
int irq;
Irqconfig *ic;
irq = (ulong)c->qid.path;
if(irq == Qdir)
return;
ic = c->aux;
if (irq > Qmstimer)
return;
ilock(&irqlock);
irqdisable(ic, irq);
iunlock(&irqlock);
free(ic);
}
static int
irqtfn(void *arg)
{
Irqconfig *ic;
ic = arg;
return ic->sleepints != ic->interrupts;
}
static long
irqread(Chan *c, void *buf, long n, vlong)
{
int irq;
Irqconfig *ic;
char tmp[24];
if(n <= 0)
return n;
irq = (ulong)c->qid.path;
if(irq == Qdir)
return devdirread(c, buf, n, irqdir, nelem(irqdir), devgen);
if(irq > Qmstimer){
print("irqread 0x%llux\n", c->qid.path);
error(Egreg);
}
ic = c->aux;
if (ic->intenable == 0)
error("disabled");
ic->sleepints = ic->interrupts;
sleep(&ic->r, irqtfn, ic);
if (irq == Qmstimer)
snprint(tmp, sizeof tmp, "%11lud %d", ic->interrupts, ic->mode);
else
snprint(tmp, sizeof tmp, "%11lud %s", ic->interrupts, ic->mode ?"edge":"level");
n = readstr(0, buf, n, tmp);
return n;
}
static long
irqwrite(Chan *c, void *a, long n, vlong)
{
int irq;
Irqconfig *ic;
Cmdbuf *cb;
Cmdtab *ct;
if(n <= 0)
return n;
irq = (ulong)c->qid.path;
if(irq <= 0 || irq >= nelem(irqdir)){
print("irqwrite 0x%llux\n", c->qid.path);
error(Egreg);
}
if (irq == Qfpgareset){
if (strncmp(a, "reset", 5) == 0)
fpgareset();
else
error(Egreg);
return n;
}
ic = c->aux;
cb = parsecmd(a, n);
if(waserror()) {
free(cb);
nexterror();
}
ct = lookupcmd(cb, irqmsg, nelem(irqmsg));
switch(ct->index) {
case CMinterrupt:
/* Turn interrupts on or off */
if (strcmp(cb->f[1], "on") == 0){
ilock(&irqlock);
irqenable(ic, irq);
iomem->siprr = 0x65009770;
iunlock(&irqlock);
}else if (strcmp(cb->f[1], "off") == 0){
ilock(&irqlock);
irqdisable(ic, irq);
iunlock(&irqlock);
}else
error(Ebadarg);
break;
case CMmode:
/* Set mode */
if (irq == Qmstimer){
ic->mode = strtol(cb->f[1], nil, 0);
if (ic->mode <= 0){
ic->tns = MS2NS(1000);
ic->mode = 1000;
error(Ebadarg);
}
ic->tns = MS2NS(ic->mode);
}else if (strcmp(cb->f[1], "level") == 0){
ic->mode = Level;
iomem->siexr &= ~(0x8000 >> irq);
}else if (strcmp(cb->f[1], "edge") == 0){
ic->mode = Edge;
iomem->siexr |= 0x8000 >> irq;
}else
error(Ebadarg);
break;
case CMreset:
ic->interrupts = 0;
break;
case CMwait:
if (ic->intenable == 0)
error("interrupts are off");
ic->sleepints = ic->interrupts;
sleep(&ic->r, irqtfn, ic);
break;
case CMdebug:
print("simr h/l 0x%lux/0x%lux, sipnr h/l 0x%lux/0x%lux, siexr 0x%lux, siprr 0x%lux\n",
iomem->simr_h, iomem->simr_l,
iomem->sipnr_h, iomem->sipnr_l,
iomem->siexr, iomem->siprr);
dumpvno();
}
poperror();
free(cb);
/* Irqi */
return n;
}
static void
interrupt(Ureg*, void *arg)
{
Irqconfig **pic, *ic;
int irq;
pic = arg;
irq = pic - irqconfig;
if (irq <= 0 || irq > nelem(irqdir)){
print("Unexpected interrupt: %d\n", irq);
return;
}
ilock(&irqlock);
if (irq <= Qirq7)
iomem->sipnr_h |= 0x8000 >> irq; /* Clear the interrupt */
for(ic = *pic; ic; ic = ic->next){
ic->interrupts++;
wakeup(&ic->r);
}
iunlock(&irqlock);
}
Dev irqdevtab = {
'b',
"irq",
devreset,
devinit,
devshutdown,
irqattach,
irqwalk,
irqstat,
irqopen,
devcreate,
irqclose,
irqread,
devbread,
irqwrite,
devbwrite,
devremove,
devwstat,
};
|