-
Notifications
You must be signed in to change notification settings - Fork 8
/
omap_McBSP.c
executable file
·610 lines (472 loc) · 12.6 KB
/
omap_McBSP.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include "omap_McBSP.h"
#include <string.h>
#include <stdlib.h>
#include "util.h"
enum OmapMcBspPipeStateType {
OmapMcBspPipeInReset,
OmapMcBspPipeWaitForFrame,
OmapMcBspPipePostFrameDelay,
OmapMcBspPipePhase1,
OmapMcBspPipePhase2,
};
struct OmapMcBspPipeState {
enum OmapMcBspPipeStateType state;
uint_fast8_t wordIdx;
uint_fast8_t bitIdx;
};
struct OmapMcBsp {
//emu info
struct SocDma *dma;
struct SocIc *ic;
uint32_t base;
uint8_t irqNoTx, irqNoRx, dmaNoTx, dmaNoRx;
//data holding regs
uint32_t drr, rbr, rsr, dxr, xsr;
//configs
uint16_t spcr1, spcr2, rcr1, rcr2, xcr1, xcr2, srgr1, srgr2, mcr1, mcr2, pcr;
uint16_t rcer[8], xcer[8];
//state
uint32_t rbrFull : 1;
uint32_t drrFull : 1;
uint32_t rsrBits : 5; ///xxx: set
uint32_t xsrBits : 5; ///xxx: set
uint32_t curTxWordBitLen : 6; ///xxx: set
uint32_t curRxWordBitLen : 6; ///xxx: set
struct OmapMcBspPipeState rxState;
struct OmapMcBspPipeState txState;
};
//XXX: XEMPTY Bit reset state shoudl be ???
#define OMAP_McBSP_SIZE 0x40
static uint_fast8_t omapMcBspPrvBitRev8(uint_fast8_t val)
{
val = ((val & 0x55) << 1) | ((val >> 1) & 0x55);
val = ((val & 0x33) << 2) | ((val >> 2) & 0x33);
val = ((val & 0x0f) << 4) | ((val >> 4) & 0x0f);
return val;
}
static uint32_t omapMcBspPrvCompandCompress(struct OmapMcBsp *sp, uint32_t val, uint_fast8_t txBitLen)
{
uint32_t ret = 0, chord;
int16_t cmp, val16 = val;
if (txBitLen != 8)
return val;
switch ((sp->xcr2 >> 3) & 3) { //XCOMPAND
case 0: //nothing
ret = val;
break;
case 1: //8bit, bitreversed
ret = omapMcBspPrvBitRev8(val);
break;
case 2: //uLaw
if (val16 >= 0)
ret |= 0x80;
else
val16 = -val16;
val16 >>= 3; //12 significant bits of now-unsigned data please
for (chord = 0, cmp = 0x40; ; chord++, cmp <<= 1) {
if (val16 >= cmp)
continue;
ret |= chord << 4;
val16 >>= chord + 1;
ret |= val16 & 0x0f;
break;
}
break;
case 3: //aLaw
if (val16 < 0) {
ret |= 0x80;
val16 = -val16;
}
val16 >>= 3; //12 significant bits of now-unsigned data please
for (chord = 0, cmp = 0x20; ; chord++, cmp <<= 1) {
if (val16 >= cmp)
continue;
ret |= chord << 4;
val16 >>= chord ? chord : 1;
ret |= val16 & 0x0f;
break;
}
break;
default:
__builtin_unreachable();
ret = 0;
}
return ret;
}
static uint32_t omapMcBspPrvCompandExpand(struct OmapMcBsp *sp, uint32_t val, uint_fast8_t rxBitLen)
{
uint32_t ret = 0;
if (rxBitLen != 8)
return val;
switch ((sp->rcr2 >> 3) & 3) { //RCOMPAND
case 0: //nothing
ret = val;
break;
case 1: //8bit, bitreversed
ret = omapMcBspPrvBitRev8(val);
break;
case 2: //uLaw
ret = val & 0x0f;
ret = ret * 2 + 0x21;
ret <<= ((val >> 4) & 7);
if (!(val & 0x80))
ret = -ret;
break;
case 3: //aLaw
ret = val & 0x0f;
if (val & 0x70) {
ret = ret * 2 + 0x21;
ret <<= ((val >> 4) & 7) - 1;
}
else
ret = ret * 2 + 1;
if (val & 0x80)
ret = -ret;
break;
default:
__builtin_unreachable();
ret = 0;
}
return ret;
}
static void omapMcBspPrvUpdateIrqs(struct OmapMcBsp *sp)
{
}
static uint_fast8_t omapMcBspPrvBitLenUnpack(struct OmapMcBsp *sp, uint_fast8_t val)
{
if (val < 5)
return 4 * (val + 2);
else if (val == 4)
return 32;
ERR("bit length %u not decodeable\n", val);
return 0;
}
static uint_fast8_t omapMcBspPrvTxGoToDataPhase(struct OmapMcBsp *sp, struct OmapMcBspPipeState *ps, bool second)
{
ps->state = second ? OmapMcBspPipePhase2 : OmapMcBspPipePhase1;
ps->wordIdx = 0;
return omapMcBspPrvBitLenUnpack(sp, ((second ? sp->xcr2 : sp->xcr1) >> 5) & 7);
}
//XXX: xempty needs to be adjusted when data written to buffer
static void omapMcBspPrvGetXsrIfNeeded(struct OmapMcBsp *sp)
{
uint_fast8_t wordsThisPhase;
if (sp->xsrBits)
return;
//sort out if we are about to go to a new phase
switch (sp->txState.state) {
case OmapMcBspPipePhase1:
wordsThisPhase = ((sp->xcr1 >> 8) & 0x7f) + 1;
break;
case OmapMcBspPipePhase2:
wordsThisPhase = ((sp->xcr2 >> 8) & 0x7f) + 1;
break;
default:
ERR("phase %u does not transmit\n", sp->txState.state);
break;
}
sp->txState.wordIdx++;
if (sp->txState.wordIdx > wordsThisPhase)
ERR("word index overflow\n");
else if (sp->txState.wordIdx == wordsThisPhase) {
if (sp->txState.state == OmapMcBspPipePhase2 || !(sp->xcr2 & 0x8000)) {
sp->txState.state = OmapMcBspPipeWaitForFrame;
return;
}
sp->curTxWordBitLen = omapMcBspPrvTxGoToDataPhase(sp, &sp->txState, true);
}
//we got here because we just shifted out the last bit in XSR
if (sp->spcr2 & 2) //transmitter was ready for data, but none arrived
sp->spcr2 &=~ 4; //we have run empty
else {
sp->spcr2 &=~ 2;
sp->xsr = omapMcBspPrvCompandCompress(sp, sp->dxr, sp->curTxWordBitLen);
}
if (!(sp->spcr2 & 4)) { //we are empty
sp->xsr = 0;
fprintf(stderr, "McBsp@0x%08lx: TX underrun\n", (unsigned long)sp->base);
}
sp->xsr <<= 32 - sp->curTxWordBitLen; //move data to top bits for transmission
sp->xsrBits = sp->curTxWordBitLen;
omapMcBspPrvUpdateIrqs(sp);
}
static bool omapMcBspPrvCalcTxBit(struct OmapMcBsp *sp)
{
bool ret;
//xxx check for transmitter reset
omapMcBspPrvGetXsrIfNeeded(sp);
ret = !!(sp->xsr >> 31);
sp->xsr <<= 1;
sp->xsrBits--;
omapMcBspPrvGetXsrIfNeeded(sp);
return ret;
}
static void omapMcBspPrvSetRbrIfNeeded(struct OmapMcBsp *sp)
{
if (sp->rsrBits != sp->curRxWordBitLen)
return;
//TODO: copy to rbr
}
static void omapMcBspPrvAcceptRxBit(struct OmapMcBsp *sp, bool hi)
{
//xxx check for receiver reset
sp->rsr <<= 1;
if (hi)
sp->rsr++;
sp->rsrBits++;
omapMcBspPrvSetRbrIfNeeded(sp);
}
static void omapMcBspPrvDoBit(struct OmapMcBsp *sp)
{
bool tx = omapMcBspPrvCalcTxBit(sp);
bool rx = false; //for now
omapMcBspPrvAcceptRxBit(sp, rx);
omapMcBspPrvUpdateIrqs(sp);
}
void omapMcBspPeriodic(struct OmapMcBsp *sp)
{
uint_fast8_t i;
//XXX: consider phases
// for (i = 0; i < 4; i++) //all word sizes are multiples of 4
// omapMcBspPrvDoBit(sp);
//for now, request DMA forever
if (!(sp->spcr1 & 0x0001))
socDmaExternalReq(sp->dma, sp->dmaNoRx, true);
if (!(sp->spcr2 & 0x0001))
socDmaExternalReq(sp->dma, sp->dmaNoTx, true);
}
static bool omapMcBspPrvMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct OmapMcBsp *sp = (struct OmapMcBsp*)userData;
uint_fast16_t val = 0;
//XXX: scary hack
if (size == 4 && write)
size = 2;
if (size != 2) {
fprintf(stderr, "%s: Unexpected %s of %u bytes to 0x%08lx\n", __func__, write ? "write" : "read", size, (unsigned long)pa);
return false;
}
pa = (pa - sp->base) >> 1;
if (write)
val = *(uint16_t*)buf;
switch (pa) {
case 0x00 / 2: //DRR2
if (write)
return false;
else
val = sp->drr >> 16;
break;
case 0x02 / 2: //DRR1
if (write)
return false;
else {
val = sp->drr & 0xffff;
if (sp->rbrFull) {
sp->drr = omapMcBspPrvCompandExpand(sp, sp->rbr, sp->curRxWordBitLen);
sp->rbrFull = false;
}
else
sp->drrFull = false;
omapMcBspPrvUpdateIrqs(sp);
}
break;
case 0x04 / 2: //DXR2
if (write)
sp->dxr = (sp->dxr & 0xffff) + (((uint32_t)val) << 16);
else
return false;
break;
case 0x06 / 2: //DXR1
if (write) {
sp->dxr = (sp->dxr & 0xffff0000ul) + val;
sp->spcr2 |= 4; //no longer empty
sp->spcr2 &=~ 2; //no longer ready for more data
omapMcBspPrvUpdateIrqs(sp);
}
else
return false;
break;
case 0x08 / 2: //SPCR2
if (write) {
if ((sp->spcr2 & 0x80) && !(val & 0x80)) { //frame sync logic reset
//todo
}
else if (!(sp->spcr2 & 0x80) && (val & 0x80)) { //frame sync logic remove from reset
sp->rxState.state = OmapMcBspPipeWaitForFrame;
//todo
}
if ((sp->spcr2 & 0x40) && !(val & 0x40)) { //sample rate generator reset
//todo
}
else if (!(sp->spcr2 & 0x40) && (val & 0x40)) { //sample rate generator remove from reset
sp->rxState.state = OmapMcBspPipeWaitForFrame;
//todo
}
sp->spcr2 &= 0x0006;
sp->spcr2 = val & 0x03f9;
omapMcBspPrvUpdateIrqs(sp);
}
else
val = sp->spcr2;
break;
case 0x0a / 2: //SPCR1
if (write) {
if ((sp->spcr1 & 1) && !(val & 1)) //receiver reset
sp->rxState.state = OmapMcBspPipeInReset;
else if (!(sp->spcr1 & 1) && (val & 1)) { //receiver remove from reset
sp->rxState.state = OmapMcBspPipeWaitForFrame;
sp->spcr1 &=~ 0x000e;
sp->rbrFull = 0;
sp->drrFull = 0;
sp->rsrBits = 0;
}
sp->spcr1 &= 0x0006;
sp->spcr1 |= val & 0xf8b9;
if (val & 0x8000)
ERR("loopback not supported\n");
omapMcBspPrvUpdateIrqs(sp);
}
else
val = sp->spcr1;
break;
case 0x0c / 2: //RCR2
if (write)
sp->rcr2 = val;
else
val = sp->rcr2;
break;
case 0x0e / 2: //RCR1
if (write)
sp->rcr1 = val & 0x7fe0;
else
val = sp->rcr1;
break;
case 0x10 / 2: //XCR2
if (write)
sp->xcr2 = val;
else
val = sp->xcr2;
break;
case 0x12 / 2: //XCR1
if (write)
sp->xcr1 = val & 0x7fe0;
else
val = sp->xcr1;
break;
case 0x14 / 2: //SRGR2
if (write)
sp->srgr2 = val;
else
val = sp->srgr2;
break;
case 0x16 / 2: //SRGR1
if (write)
sp->srgr1 = val;
else
val = sp->srgr1;
break;
case 0x18 / 2: //MCR2
if (write) {
sp->mcr2 = val & 0x03fd;
if (val & 3)
ERR("multichannel TX mode not supported\n");
}
else
val = sp->mcr2;
break;
case 0x1a / 2: //MCR1
if (write) {
sp->mcr1 = val & 0x03fd;
if (val & 1)
ERR("multichannel RX mode not supported\n");
}
else
val = sp->mcr1;
break;
case 0x1c / 2: //RCERA
case 0x1e / 2: //RCERB
if (write)
sp->rcer[pa - 0x1c / 2 + 0] = val;
else
val = sp->rcer[pa - 0x1c / 2 + 0];
break;
case 0x20 / 2: //XCERA
case 0x22 / 2: //XCERB
if (write)
sp->xcer[pa - 0x20 / 2 + 0] = val;
else
val = sp->xcer[pa - 0x20 / 2 + 0];
break;
case 0x24 / 2: //PCR0
if (write)
sp->pcr = val & 0x7fff;
else
val = sp->pcr;
break;
case 0x26 / 2: //RCERC
case 0x28 / 2: //RCERD
if (write)
sp->rcer[pa - 0x26 / 2 + 2] = val;
else
val = sp->rcer[pa - 0x26 / 2 + 2];
break;
case 0x2a / 2: //XCERC
case 0x2c / 2: //XCERD
if (write)
sp->xcer[pa - 0x2a / 2 + 2] = val;
else
val = sp->xcer[pa - 0x2a / 2 + 2];
break;
case 0x2e / 2: //RCERE
case 0x30 / 2: //RCERF
if (write)
sp->rcer[pa - 0x2e / 2 + 4] = val;
else
val = sp->rcer[pa - 0x2e / 2 + 4];
break;
case 0x32 / 2: //XCERE
case 0x34 / 2: //XCERF
if (write)
sp->xcer[pa - 0x32 / 2 + 4] = val;
else
val = sp->xcer[pa - 0x32 / 2 + 4];
break;
case 0x36 / 2: //RCERG
case 0x38 / 2: //RCERH
if (write)
sp->rcer[pa - 0x36 / 2 + 6] = val;
else
val = sp->rcer[pa - 0x36 / 2 + 6];
break;
case 0x3a / 2: //XCERG
case 0x3c / 2: //XCERH
if (write)
sp->xcer[pa - 0x3a / 2 + 6] = val;
else
val = sp->xcer[pa - 0x3a / 2 + 6];
break;
default:
return false;
}
if (!write)
*(uint16_t*)buf = val;
return true;
}
struct OmapMcBsp* omapMcBspInit(struct ArmMem *physMem, struct SocIc *ic, struct SocDma *dma, uint32_t base, uint8_t irqNoTx, uint8_t irqNoRx, uint8_t dmaNoTx, uint8_t dmaNoRx)
{
struct OmapMcBsp *sp = (struct OmapMcBsp*)malloc(sizeof(*sp));
if (!sp)
ERR("cannot alloc McBSP @ 0x%08lx", (unsigned long)base);
memset(sp, 0, sizeof (*sp));
sp->dma = dma;
sp->ic = ic;
sp->irqNoTx = irqNoTx;
sp->irqNoRx = irqNoRx;
sp->dmaNoTx = dmaNoTx;
sp->dmaNoRx = dmaNoRx;
sp->base = base;
if (!memRegionAdd(physMem, base, OMAP_McBSP_SIZE, omapMcBspPrvMemAccessF, sp))
ERR("cannot add McBSP @ 0x%08lx to MEM\n", (unsigned long)base);
return sp;
}