-
Notifications
You must be signed in to change notification settings - Fork 8
/
omap_DMA.c
executable file
·457 lines (359 loc) · 11.3 KB
/
omap_DMA.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
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include "omap_DMA.h"
#include "omap_IC.h"
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include "util.h"
#include "mem.h"
#define OMAP_DMA_BASE 0xFFFEDC00UL
#define OMAP_DMA_SIZE 4
#define OMAP_DMA_CHS_BASE 0xFFFED800UL
#define OMAP_DMA_CHS_SIZE 0x300
#define NUM_CHANNELS 9
struct DmaChannelCfg {
uint32_t src, dst;
uint16_t cen, cfn, cfi, cei;
};
struct DmaChannel {
uint16_t csdp, ccr, csr, cpc;
uint8_t cicr;
//regs config iface accesses
struct DmaChannelCfg programming;
//shadow regs DMA uses
struct DmaChannelCfg active;
//dma state we have
uint16_t curElemIdx; //next elem to xfer in cur frame
uint16_t curFrameIdx; //next frame to xfer in cur block
uint32_t curSrcAddr;
uint32_t curDstAddr; //we cannot modify active.{src,dst} since in case of repetitive xfer we need them as is
bool waitingForEndProg;
//to help with efficiency
uint8_t irqNo;
struct DmaChannel *friendChannel;
};
struct SocDma {
struct SocIc *ic;
struct ArmMem *mem;
uint32_t pendingReqs;
struct DmaChannel ch[NUM_CHANNELS];
uint8_t gcr;
};
static bool socDmaPrvCtrlMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct SocDma *dma = (struct SocDma*)userData;
uint32_t val = 0;
if ((size != 4 && size != 2) || (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_DMA_BASE) >> 2;
if (write)
val = (size == 2) ? *(uint16_t*)buf : *(uint32_t*)buf;
switch (pa) {
case 0x00 / 4:
if (write)
dma->gcr = val & 0x0c;
else
val = dma->gcr;
break;
default:
return false;
}
if (!write) {
if (size == 2)
*(uint16_t*)buf = val;
else
*(uint32_t*)buf = val;
}
return true;
}
static void socDmaPrvChannelInitialize(struct SocDma *dma, struct DmaChannel *ch, bool copyCfg) //also handles re-initializing
{
if (copyCfg)
ch->active = ch->programming;
ch->curElemIdx = 0;
ch->curFrameIdx = 0;
ch->cpc = 0;
ch->waitingForEndProg = false;
ch->curSrcAddr = ch->active.src;
ch->curDstAddr = ch->active.dst;
if (0)
if (ch - dma->ch != 7) {
static const char *modes[] = {"const", "linear", "single index", "double index"};
fprintf(stderr, "DMA start ch %u %u byte x %u x %u from 0x%08lx to 0x%08lx\n",
(unsigned)(ch - dma->ch), 1 << (ch->csdp & 3), ch->active.cen, ch->active.cfn, (unsigned long)ch->active.src, (unsigned long)ch->active.dst);
fprintf(stderr, " src mode is %s, dst mode is %s\n", modes[(ch->ccr >> 12) & 3], modes[(ch->ccr >> 14) & 3]);
fprintf(stderr, " cei = %d, cfi = %d\n", ch->active.cei, ch->active.cfi);
}
}
static bool socDmaPrvChsMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct SocDma *dma = (struct SocDma*)userData;
uint_fast16_t val = 0;
struct DmaChannel *ch;
uint_fast8_t chNo;
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 -= OMAP_DMA_CHS_BASE;
if (write)
val = *(uint16_t*)buf;
chNo = pa / 0x40;
pa %= 0x40;
pa /= 2;
if (chNo >= NUM_CHANNELS)
return false;
ch = &dma->ch[chNo];
switch (pa) {
case 0x00 / 2:
if (write)
ch->csdp = val & 0xffff;
else
val = ch->csdp;
break;
case 0x02 / 2:
if (write) {
ch->ccr &= 0x80; //keep old enabled bits
ch->ccr = val & 0xfb7f; //do not set new enabled bit yet
//sort out enablement
if (!(ch->ccr & 0x0080) && (val & 0x0080)) {
ch->ccr |= 0x0080;
socDmaPrvChannelInitialize(dma, ch, true);
}
//sort out disablement
if ((ch->ccr & 0x0080) && !(val & 0x0080)) {
ch->ccr &=~ 0x0080;
ch->waitingForEndProg = false;
}
//or maybe we're enabled and END_PROG we've been waiting for arrived
if ((val & 0x0800) && ch->waitingForEndProg) {
ch->waitingForEndProg = false;
ch->ccr &=~ 0x0800;
socDmaPrvChannelInitialize(dma, ch, true);
}
}
else
val = ch->ccr;
break;
case 0x04 / 2:
if (write) {
ch->cicr = val & 0x3f;
//xxx: is this accurate? how else would ints be blocked if they get disabled concurrently with happening?
ch->csr &= ch->cicr;
}
else
val = ch->cicr;
break;
case 0x06 / 2:
if (write)
; //write ignored
else {
val = ch->csr & 0x7f;
ch->csr &= 0x40; //other bits clear when read
if (ch->friendChannel) {
val |= ((uint32_t)ch->friendChannel->csr) << 7;
ch->friendChannel->csr &= 0x40; //other bits clear when read
}
}
break;
case 0x08 / 2: //src.lo
if (write)
ch->programming.src = (ch->programming.src & 0xffff0000ul) | val;
else
val = (uint16_t)ch->programming.src;
break;
case 0x0a / 2: //src.hi
if (write)
ch->programming.src = (ch->programming.src & 0xffff) | (((uint32_t)val) << 16);
else
val = ch->programming.src >> 16;
break;
case 0x0c / 2: //dst.lo
if (write)
ch->programming.dst = (ch->programming.dst & 0xffff0000ul) | val;
else
val = (uint16_t)ch->programming.dst;
break;
case 0x0e / 2: //dst.hi
if (write)
ch->programming.dst = (ch->programming.dst & 0xffff) | (((uint32_t)val) << 16);
else
val = ch->programming.dst >> 16;
break;
case 0x10 / 2:
if (write)
ch->programming.cen = val;
else
val = ch->programming.cen;
break;
case 0x12 / 2:
if (write)
ch->programming.cfn = val;
else
val = ch->programming.cfn;
break;
case 0x14 / 2:
if (write)
ch->programming.cfi = val;
else
val = ch->programming.cfi;
break;
case 0x16 / 2:
if (write)
ch->programming.cei = val;
else
val = ch->programming.cei;
break;
case 0x18 / 2:
if (write)
; //fobidden and ignored
else
val = ch->cpc;
break;
default:
return false;
}
if (!write)
*(uint16_t*)buf = val;
return true;
}
static uint32_t socDmaPrvAdjustAddress(struct SocDma *dma, struct DmaChannel *ch, uint32_t addr, uint_fast8_t accessSz, uint_fast8_t mode)
{
switch (mode) {
case 0: //constant addr
break;
case 1: //post-increment
addr += accessSz;
break;
case 2: //single-indexed
addr += accessSz - 1 + (int32_t)(int16_t)ch->active.cei - 1;
break;
case 3: //double-indexed
if (ch->curElemIdx)
addr += accessSz - 1 + (int32_t)(int16_t)ch->active.cei;
else
addr += accessSz - 1 + (int32_t)(int16_t)ch->active.cfi;
break;
default:
__builtin_unreachable();
}
return addr;
}
static void socDmaPrvChannelActIfNeeded(struct SocDma *dma, struct DmaChannel *ch)
{
uint_fast8_t reqNum = ch->ccr & 0x1f, accessSz = ch->csdp & 3 /* lg2 */;
uint_fast16_t i, nElems;
bool entire = true; //make memory-to-memory xfers instant
char xferBuf[4];
//bursting appears not mandatory so we do not do it
//packing appears not mandatory so we do not do it
//NOTE: maskable CSR bits are not set if respecitive CICR bits are clear
//off? then we do nothing
if (!(ch->ccr & 0x80))
goto update_irq;
//synchronized with a periph which is not requesting a transfer? do noting
if (reqNum) {
if (dma->pendingReqs & (1UL << reqNum))
ch->csr |= 0x40;
else {
ch->csr &=~ 0x40;
//no need for irq updates since this bit never generates irqs
goto update_irq;
}
entire = false;
}
//some sanity checking
if (accessSz == 3)
ERR("DMA.ch[%u].CDSP.DATA_TYPE = 3\n", (unsigned)(ch - dma->ch));
accessSz = 1 << accessSz; //convert to actual size
//sort out how much to xfer
if (entire) //mem to mem xfers are instant
nElems = ch->active.cen * ch->active.cfn;
else if (ch->ccr & 0x20)//transfer a frame
nElems = ch->active.cen;
else //transfer an element
nElems = 1;
//fprintf(stderr, "DMA ch %lu: xferring %u %u-byte elems from 0x%08x to 0x%08x\n", ch - dma->ch, (unsigned)nElems, accessSz, ch->curSrcAddr, ch->curDstAddr);
for (i = 0; i < nElems; i++) {
if (!memAccess(dma->mem, ch->curSrcAddr, accessSz, false, xferBuf))
ERR("DMA ch %u bus error on read at 0x%08lx\n", (unsigned)(ch - dma->ch), (unsigned long)ch->curSrcAddr);
else if (!memAccess(dma->mem, ch->curDstAddr, accessSz, true, xferBuf))
ERR("DMA ch %u bus error on write at 0x%08lx\n", (unsigned)(ch - dma->ch), (unsigned long)ch->curDstAddr);
if (++ch->curElemIdx == ch->active.cen) {
ch->curElemIdx = 0;
ch->csr |= 0x08 & ch->cicr;
if (++ch->curFrameIdx == ch->active.cfn)
ch->curFrameIdx = 0;
}
ch->curSrcAddr = socDmaPrvAdjustAddress(dma, ch, ch->curSrcAddr, accessSz, (ch->ccr >> 12) & 3);
ch->curDstAddr = socDmaPrvAdjustAddress(dma, ch, ch->curDstAddr, accessSz, (ch->ccr >> 14) & 3);
if (ch->curFrameIdx == ch->active.cfn - 1)
ch->csr |= 0x10 & ch->cicr;
if (ch->curElemIdx >= ch->active.cen / 2)
ch->csr |= 0x04 & ch->cicr;
}
if (!ch->curElemIdx && !ch->curFrameIdx) {
if(0)
if (ch - dma->ch != 7)
fprintf(stderr, "DMA done\n");
ch->csr |= 0x20 & ch->cicr;
if (!(ch->ccr & 0x0100)) { //.AUTOINIT
ch->ccr &=~ 0x0080; //channel stops
}
else if (ch->ccr & 0x0200) { //.REPEAT
socDmaPrvChannelInitialize(dma, ch, false);
}
else if (ch->ccr & 0x0800) { //.END_PROG
ch->ccr &=~ 0x0800; //clear it
socDmaPrvChannelInitialize(dma, ch, true);
}
else {
ch->waitingForEndProg = true;
}
}
update_irq:
socIcInt(dma->ic, ch->irqNo, (ch->csr & 0x3f) || (ch->friendChannel && (ch->friendChannel->csr & 0x3f)));
}
struct SocDma* socDmaInit(struct ArmMem *physMem, struct SocIc *ic)
{
static const uint8_t irqNos[] = {OMAP_I_DMA_CH0_CH6, OMAP_I_DMA_CH1_CH7, OMAP_I_DMA_CH2_CH8, OMAP_I_DMA_CH3, OMAP_I_DMA_CH4, OMAP_I_DMA_CH5, OMAP_I_DMA_CH0_CH6, OMAP_I_DMA_CH1_CH7, OMAP_I_DMA_CH2_CH8};
struct SocDma *dma = (struct SocDma*)malloc(sizeof(*dma));
uint_fast8_t i;
if (!dma)
ERR("cannot alloc DMA");
memset(dma, 0, sizeof (*dma));
dma->ic = ic;
dma->mem = physMem;
dma->gcr = 8;
for (i = 0; i < NUM_CHANNELS; i++) {
dma->ch[i].cicr = 3; //stopped or uninitialized
dma->ch[i].irqNo = irqNos[i];
}
//cross-link friend channels
for (i = 0; i < 3; i++) {
dma->ch[i].friendChannel = &dma->ch[i + 6];
dma->ch[i + 6].friendChannel = &dma->ch[i];
}
if (!memRegionAdd(physMem, OMAP_DMA_BASE, OMAP_DMA_SIZE, socDmaPrvCtrlMemAccessF, dma))
ERR("cannot add DMA control to MEM\n");
if (!memRegionAdd(physMem, OMAP_DMA_CHS_BASE, OMAP_DMA_CHS_SIZE, socDmaPrvChsMemAccessF, dma))
ERR("cannot add DMA channels to MEM\n");
return dma;
}
void socDmaExternalReq(struct SocDma* dma, uint_fast8_t chNum, bool requested)
{
if (chNum < sizeof(dma->pendingReqs) * CHAR_BIT) {
if (requested)
dma->pendingReqs |= 1UL << chNum;
else
dma->pendingReqs &=~ 1UL << chNum;
}
}
void socDmaPeriodic(struct SocDma* dma)
{
uint_fast8_t i;
for (i = 0; i < NUM_CHANNELS; i++)
socDmaPrvChannelActIfNeeded(dma, &dma->ch[i]);
}