-
Notifications
You must be signed in to change notification settings - Fork 8
/
omap_GPIO.c
executable file
·492 lines (393 loc) · 11 KB
/
omap_GPIO.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include "omap_IC.h"
#include "soc_GPIO.h"
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "mem.h"
#define OMAP_SHARED_GPIO_BASE 0xFFFCE000UL
#define OMAP_SHARED_GPIO_SIZE 256
#define OMAP_MPUIO_BASE 0xFFFB5000UL
#define OMAP_MPUIO_SIZE 256
//we treat keyboard scanner as gpios since our keyboard code already works this way
//columns OUT (open collector) are 32..49. rows in (force-input with pullup) are 40..47
struct OmapGpioBank {
uint8_t ofst;
uint8_t irqNo;
//all
uint16_t inputs, latches, dir;
//shared & mpuio
uint16_t intMask, intSta, prevVals;
//shared gpio only
uint16_t intCtl, pinCtl;
//mpuio only
uint16_t eventLatch;
uint8_t eventConfig;
uint16_t unknown_0x0c; //suspect to be like intCtl is for shared
GpioChangedNotifF notifF[16];
void *notifD[16];
//for edge triggering
uint16_t prevIntSta;
bool irqIsEdge;
};
struct SocGpio {
struct SocIc *ic;
struct OmapGpioBank shared;
struct OmapGpioBank mpuio;
struct OmapGpioBank kbd;
uint16_t mpuioDebounce;
GpioDirsChangedF dirNotifF;
void *dirNotifD;
};
static void socGpioPrvRecalc(struct SocGpio *gpio, struct OmapGpioBank *bank)
{
uint_fast16_t diff, wentHi, wentLo, desiredChange = 0, vals = ((bank->inputs & bank->dir) | (bank->latches &~ bank->dir)) & bank->pinCtl, intCause;
bool irq = false;
uint_fast8_t i;
diff = bank->prevVals ^ vals;
wentHi = diff & vals;
wentLo = diff &~ wentHi;
//calc omap changes (what went hi? what went lo?)
desiredChange |= wentHi & bank->intCtl & ~bank->intMask;
desiredChange |= wentLo & (~bank->intCtl) & ~bank->intMask;
bank->intSta |= desiredChange;
intCause = bank->intSta & ~bank->intMask;
if (bank->irqIsEdge) {
if (intCause &~ bank->prevIntSta) {
//edge
socIcInt(gpio->ic, bank->irqNo, true);
socIcInt(gpio->ic, bank->irqNo, false);
}
bank->prevIntSta = bank->intSta;
}
else {
//level
socIcInt(gpio->ic, bank->irqNo, !!intCause);
}
//notify emulator users
for (i = 0; i < 16; i++, diff >>= 1) {
if (!(diff & 1))
continue;
if (!bank->notifF[i])
continue;
bank->notifF[i](bank->notifD[i], bank->ofst + i, !!(bank->prevVals & (1UL << i)), !!(vals & (1UL << i)));
}
//handle "event mode"
if ((bank->eventConfig & 1) && (desiredChange & (1 << (bank->eventConfig >> 1))))
bank->eventLatch = vals;
bank->prevVals = vals;
}
static bool socGpioPrvMpuioMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct SocGpio* gpio = (struct SocGpio*)userData;
uint_fast16_t val = 0, old;
if ((size != 2 && size != 4) || (pa & 3)) {
fprintf(stderr, "%s: Unexpected %s of %u bytes to 0x%08lx\n", __func__, write ? "write" : "read", size, (unsigned long)pa);
return false;
}
pa = (pa - OMAP_MPUIO_BASE) >> 2;
if (write)
val = (size == 2) ? *(uint16_t*)buf : *(uint32_t*)buf;
switch (pa) {
case 0x00 / 4:
if (write)
return false; //not allowed
else
val = gpio->mpuio.inputs;
break;
case 0x04 / 4:
if (write) {
old = gpio->mpuio.latches;
gpio->mpuio.latches = val;
if (gpio->mpuio.latches != old)
socGpioPrvRecalc(gpio, &gpio->mpuio);
}
else
val = gpio->mpuio.latches;
break;
case 0x0c / 4:
if (write)
gpio->mpuio.unknown_0x0c = val;
else
val = gpio->mpuio.unknown_0x0c;
break;
case 0x08 / 4:
if (write) {
old = gpio->mpuio.dir;
gpio->mpuio.dir |= val;
if (gpio->mpuio.dir != old) {
socGpioPrvRecalc(gpio, &gpio->mpuio);
if (gpio->dirNotifF)
gpio->dirNotifF(gpio->dirNotifD);
}
}
else
val = gpio->mpuio.dir;
break;
case 0x10 / 4:
if (write)
; //not allowed as per docs - ignored in hw
else
val = ((gpio->kbd.inputs >> 8) & 0x1f) | 0xffe0; //all highs are expected on all other bits
break;
case 0x14 / 4:
if (write) {
old = gpio->kbd.dir;
gpio->kbd.dir &= 0xff00;
gpio->kbd.dir |= val & 0x00ff;
if (gpio->kbd.dir != old) {
socGpioPrvRecalc(gpio, &gpio->kbd);
if (gpio->dirNotifF)
gpio->dirNotifF(gpio->dirNotifD);
}
}
else
val = gpio->kbd.dir & 0x00ff;
break;
case 0x18 / 4:
if (write)
gpio->mpuio.eventConfig = val & 0x3f;
else
val = gpio->mpuio.eventConfig;
break;
case 0x1c / 4:
if (write) {
gpio->mpuio.intCtl = val;
socGpioPrvRecalc(gpio, &gpio->mpuio);
}
else
val = gpio->mpuio.intCtl;
break;
case 0x20 / 4:
if (write)
; //ignored
else
val = ((gpio->kbd.inputs >> 8) & 0x1f) == 0x1f;
break;
case 0x24 / 4:
if (write)
; //not allowed as per docs - ignored in hw
else {
val = gpio->mpuio.intSta &~ gpio->mpuio.intMask;
gpio->mpuio.intSta = 0;
}
break;
case 0x28 / 4:
if (write)
gpio->kbd.intMask = (val & 1) ? 0xffff : 0x00ff;
else
val = gpio->kbd.intMask >> 15;
break;
case 0x2c / 4:
if (write) {
gpio->mpuio.intMask = val;
socGpioPrvRecalc(gpio, &gpio->mpuio);
}
else
val = gpio->mpuio.intMask;
break;
case 0x30 / 4:
if (write)
gpio->mpuioDebounce = val & 0x1ff;
else
val = gpio->mpuioDebounce;
break;
case 0x34 / 4:
if (write)
; //not allowed as per docs - ignored in hw
else
val = gpio->mpuio.eventLatch;
break;
default:
return false;
}
if (!write) {
if (size == 2)
*(uint16_t*)buf = val;
else
*(uint32_t*)buf = val;
}
return true;
}
static bool socGpioPrvSharedMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct SocGpio* gpio = (struct SocGpio*)userData;
uint_fast16_t val = 0, old;
if ((size != 2 && size != 4) || (pa & 3)) {
fprintf(stderr, "%s: Unexpected %s of %u bytes to 0x%08lx\n", __func__, write ? "write" : "read", size, (unsigned long)pa);
return false;
}
pa = (pa - OMAP_SHARED_GPIO_BASE) >> 2;
if (write)
val = (size == 2) ? *(uint16_t*)buf : *(uint32_t*)buf;
switch (pa) {
case 0x00 / 4:
if (write)
return false; //not allowed
else
val = gpio->shared.inputs & gpio->shared.pinCtl;
break;
case 0x04 / 4:
if (write) {
old = gpio->shared.latches;
gpio->shared.latches &=~ gpio->shared.pinCtl;
gpio->shared.latches |= val & gpio->shared.pinCtl;
if (gpio->shared.latches != old)
socGpioPrvRecalc(gpio, &gpio->shared);
}
else
val = gpio->shared.latches & gpio->shared.pinCtl;
break;
case 0x08 / 4:
if (write) {
old = gpio->shared.dir;
gpio->shared.dir &=~ gpio->shared.pinCtl;
gpio->shared.dir |= val & gpio->shared.pinCtl;
if (gpio->shared.dir != old) {
socGpioPrvRecalc(gpio, &gpio->shared);
if (gpio->dirNotifF)
gpio->dirNotifF(gpio->dirNotifD);
}
}
else
val = gpio->shared.dir & gpio->shared.pinCtl;
break;
case 0x0c / 4:
if (write) {
gpio->shared.intCtl &=~ gpio->shared.pinCtl;
gpio->shared.intCtl |= val & gpio->shared.pinCtl;
socGpioPrvRecalc(gpio, &gpio->shared);
}
else
val = gpio->shared.intCtl & gpio->shared.pinCtl;
break;
case 0x10 / 4:
if (write) {
gpio->shared.intMask &=~ gpio->shared.pinCtl;
gpio->shared.intMask |= val & gpio->shared.pinCtl;
socGpioPrvRecalc(gpio, &gpio->shared);
}
else
val = gpio->shared.intMask & gpio->shared.pinCtl;
break;
case 0x14 / 4:
if (write) {
gpio->shared.intSta &=~ (val & gpio->shared.pinCtl);
socGpioPrvRecalc(gpio, &gpio->shared);
}
else
val = gpio->shared.intSta & gpio->shared.pinCtl;
break;
case 0x18 / 4:
if (write) {
gpio->shared.pinCtl = val;
socGpioPrvRecalc(gpio, &gpio->shared);
}
else
val = gpio->shared.pinCtl;
break;
default:
return false;
}
if (!write) {
if (size == 2)
*(uint16_t*)buf = val;
else
*(uint32_t*)buf = val;
}
return true;
}
struct SocGpio* socGpioInit(struct ArmMem *physMem, struct SocIc *ic, uint_fast8_t socRev)
{
struct SocGpio *gpio = (struct SocGpio*)malloc(sizeof(*gpio));
if (!gpio)
ERR("cannot alloc GPIO");
memset(gpio, 0, sizeof (*gpio));
gpio->ic = ic;
//shared
gpio->shared.latches = 0xffff;
gpio->shared.dir = 0xffff;
gpio->shared.intCtl = 0xffff;
gpio->shared.intMask = 0xffff;
gpio->shared.pinCtl = 0xffff;
gpio->shared.irqNo = OMAP_I_GPIO;
//mpuio
gpio->mpuio.dir = 0xffff;
gpio->mpuio.pinCtl = 0xffff; //all of these permanently belong to the MPU
gpio->mpuio.irqNo = OMAP_I_MPUIO;
gpio->mpuio.ofst = 16;
//keyboard
gpio->kbd.dir = 0x00ff; //columns all out, rows all in
gpio->kbd.irqNo = OMAP_I_KEYBOARD;
gpio->kbd.intMask = 0xff00;
gpio->kbd.ofst = 32;
gpio->kbd.irqIsEdge = true;
if (!memRegionAdd(physMem, OMAP_SHARED_GPIO_BASE, OMAP_SHARED_GPIO_SIZE, socGpioPrvSharedMemAccessF, gpio))
ERR("cannot add GPIO to MEM\n");
if (!memRegionAdd(physMem, OMAP_MPUIO_BASE, OMAP_MPUIO_SIZE, socGpioPrvMpuioMemAccessF, gpio))
ERR("cannot add MPUIO to MEM\n");
socGpioPrvRecalc(gpio, &gpio->shared);
socGpioPrvRecalc(gpio, &gpio->mpuio);
socGpioPrvRecalc(gpio, &gpio->kbd);
return gpio;
}
void socGpioSetState(struct SocGpio *gpio, uint_fast8_t gpioNum, bool on)
{
struct OmapGpioBank *bank;
uint_fast16_t old;
if (gpioNum < 16)
bank = &gpio->shared;
else if (gpioNum < 32)
bank = &gpio->mpuio;
else if (gpioNum < 48)
bank = &gpio->kbd;
else
return;
gpioNum -= bank->ofst;
old = bank->inputs;
if (on)
bank->inputs |= 1UL << gpioNum;
else
bank->inputs &=~ (1UL << gpioNum);
if (bank->inputs != old)
socGpioPrvRecalc(gpio, bank);
}
enum SocGpioState socGpioGetState(struct SocGpio *gpio, uint_fast8_t gpioNum)
{
struct OmapGpioBank *bank;
uint_fast16_t old;
if (gpioNum < 16)
bank = &gpio->shared;
else if (gpioNum < 32)
bank = &gpio->mpuio;
else if (gpioNum < 48)
bank = &gpio->kbd;
else
return SocGpioStateNoSuchGpio;
gpioNum -= bank->ofst;
if (!(bank->pinCtl & (1UL << gpioNum))) //pins given to the DSP report as AFR
return SocGpioStateAFR1;
if (bank->dir & (1UL << gpioNum))
return SocGpioStateHiZ;
return (bank->latches & (1UL << gpioNum)) ? SocGpioStateHigh : SocGpioStateLow;
}
void socGpioSetNotif(struct SocGpio *gpio, uint_fast8_t gpioNum, GpioChangedNotifF notifF, void* userData)
{
struct OmapGpioBank *bank;
if (gpioNum < 16)
bank = &gpio->shared;
else if (gpioNum < 32)
bank = &gpio->mpuio;
else if (gpioNum < 48)
bank = &gpio->kbd;
else
return;
gpioNum -= bank->ofst;
bank->notifF[gpioNum] = notifF;
bank->notifD[gpioNum] = userData;
}
void socGpioSetDirsChangedNotif(struct SocGpio *gpio, GpioDirsChangedF notifF, void *userData)
{
gpio->dirNotifF = notifF;
gpio->dirNotifD = userData;
}