OpenCores
URL https://opencores.org/ocsvn/pcie_ds_dma/pcie_ds_dma/trunk

Subversion Repositories pcie_ds_dma

[/] [pcie_ds_dma/] [trunk/] [soft/] [linux/] [common/] [pex/] [pex_board.cpp] - Blame information for rev 21

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dsmv
 
2
#ifndef __PEX_BOARD_H__
3
#include "pex_board.h"
4
#endif
5
 
6
//-----------------------------------------------------------------------------
7
 
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <fcntl.h>
11
#include <errno.h>
12
#include <sys/mman.h>
13
#include <sys/ioctl.h>
14
 
15
#include <cassert>
16
#include <cstdlib>
17
#include <cstring>
18
#include <iostream>
19
#include <iomanip>
20
#include <climits>
21
 
22
//-----------------------------------------------------------------------------
23
 
24
using namespace std;
25
 
26
//-----------------------------------------------------------------------------
27
 
28
pex_board::pex_board()
29
{
30
    fd = -1;
31
    bar0 = bar1 = NULL;
32
    memset(&bi, 0, sizeof(bi));
33 6 v.karak
    //m_dma = new dma_memory();
34 2 dsmv
}
35
 
36
//-----------------------------------------------------------------------------
37
 
38
pex_board::~pex_board()
39
{
40 6 v.karak
    //if(m_dma) delete m_dma;
41 2 dsmv
    core_close();
42
}
43
 
44
//-----------------------------------------------------------------------------
45
 
46
int pex_board::core_open(const char *name)
47
{
48
    int error = 0;
49
 
50
    if(fd > 0)
51
        return 0;
52
 
53
    fd = open(name, S_IROTH | S_IWOTH );
54
    if(fd < 0) {
55
        std::cerr << __FUNCTION__ << "(): " << " error open device: " << name << endl;
56
        goto do_out;
57
    }
58
 
59
    error = core_board_info();
60
    if(error < 0) {
61
        std::cerr << __FUNCTION__ << "(): " << " error get board info" << endl;
62
        goto do_close;
63
    }
64
 
65
    bar0 = (u32*)mmap(NULL, bi.Size[0], PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)bi.PhysAddress[0]);
66
    if( bar0 == MAP_FAILED ) {
67
        std::cerr << __FUNCTION__ << "(): " << " error map bar0 address" << endl;
68
        error = -EINVAL;
69
        goto do_close;
70
    }
71
 
72
    bar1 = (u32*)mmap(NULL, bi.Size[1], PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)bi.PhysAddress[1]);
73
    if( bar1== MAP_FAILED ) {
74
        std::cerr << __FUNCTION__ << "(): " << " error map bar1 address" << endl;
75
        error = -EINVAL;
76
        goto do_unmap_bar0;
77
    }
78
 
79
    std::cout << "Map BAR0: 0x" << hex << bi.PhysAddress[0] << " -> " << bar0 << dec << endl;
80
    std::cout << "Map BAR1: 0x" << hex << bi.PhysAddress[1] << " -> " << bar1 << dec << endl;
81
 
82
    return 0;
83
 
84
do_unmap_bar0:
85
    munmap(bar0, bi.Size[0]);
86
 
87
do_close:
88
    close(fd);
89
 
90
do_out:
91
    return error;
92
}
93
 
94
//-----------------------------------------------------------------------------
95
 
96
int pex_board::core_init()
97
{
98
    uint16_t temp = 0;
99
    uint16_t blockId = 0;
100
    uint16_t blockVer = 0;
101
    uint16_t deviceID = 0;
102
    uint16_t deviceRev = 0;
103
    int i = 0;
104
 
105
    fprintf(stderr,"%s()\n", __FUNCTION__);
106
 
107
    blockId =  core_block_read( 0, 0 );
108
    blockVer = core_block_read( 0, 1 );
109
 
110
    fprintf(stderr,"%s(): BlockID = 0x%X, BlockVER = 0x%X.\n", __FUNCTION__, blockId, blockVer);
111
 
112
    deviceID = core_block_read(  0, 2 );
113
    deviceRev = core_block_read( 0, 3 );
114
 
115
    fprintf(stderr,"%s(): DeviceID = 0x%X, DeviceRev = 0x%X.\n", __FUNCTION__, deviceID, deviceRev);
116
 
117
    temp = core_block_read( 0, 4 );
118
    int m_BlockCnt = core_block_read( 0, 5 );
119
 
120
    if( m_BlockCnt>8 ) {
121
        m_BlockCnt=8;
122
    }
123
 
124
    fprintf(stderr,"%s(): PldVER = 0x%X.\n", __FUNCTION__, temp);
125
    fprintf(stderr,"%s(): Block count = %d.\n", __FUNCTION__, m_BlockCnt);
126
 
127
    // определим какие каналы ПДП присутствуют и их характеристики:
128
    // направление передачи данных, размер FIFO, максимальный размер блока ПДП
129
 
130
    FIFO_ID FifoId;
131
    int m_DmaFifoSize[4] = {0};
132
    int m_MaxDmaSize[4] = {0};
133
    int m_DmaDir[4] = {0};
134
    int m_DmaChanMask = 0;
135
 
136
    for(int iBlock = 0; iBlock < m_BlockCnt; iBlock++)
137
    {
138
        uint16_t block_id = 0;
139
 
140
        block_id=core_block_read( iBlock, 0 );
141
        block_id &=0xFFF;
142
 
143
        if(block_id == PE_EXT_FIFO_ID)
144
        {
145
            u32 resource_id = 0;
146
            uint16_t iChan = core_block_read( iBlock, 3 );
147
            m_DmaChanMask |= (1 << iChan);
148
            FifoId.AsWhole = core_block_read( iBlock, 2 );
149
            m_DmaFifoSize[iChan] = FifoId.ByBits.Size;
150
            m_DmaDir[iChan] = FifoId.ByBits.Dir;
151
            m_MaxDmaSize[iChan] = 0x40000000; // макс. размер ПДП пусть будет 1 Гбайт
152
            resource_id = core_block_read( iBlock, 4 ); // RESOURCE
153
            fprintf(stderr,"%s(): Channel(ID) = %d(0x%x), FIFO size = %d Bytes, DMA Dir = %d, Max DMA size = %d MBytes, resource = 0x%x.\n", __FUNCTION__,
154
                    iChan, block_id, m_DmaFifoSize[iChan] * 4, m_DmaDir[iChan], m_MaxDmaSize[iChan] / 1024 / 1024, resource_id);
155
        }
156
    }
157
 
158
    // подготовим к работе ПЛИС ADM
159
    fprintf(stderr,"%s(): Prepare ADM PLD.\n", __FUNCTION__);
160
    core_block_write( 0, 8, 0);
161 6 v.karak
    core_delay(100);    // pause ~ 100 msec
162 2 dsmv
    for(i = 0; i < 10; i++)
163
    {
164
        core_block_write( 0, 8, 1);
165 6 v.karak
        core_delay(100);        // pause ~ 100 msec
166 2 dsmv
        core_block_write( 0, 8, 3);
167 6 v.karak
        core_delay(100);        // pause ~ 100 msec
168 2 dsmv
        core_block_write( 0, 8, 7);
169 6 v.karak
        core_delay(100);        // pause ~ 100 msec
170 2 dsmv
        temp = core_block_read( 0, 010 ) & 0x01;
171
        if(temp)
172
            break;
173
    }
174
    core_block_write( 0, 8, 0xF );
175 6 v.karak
    core_delay(100);    // pause ~ 100 msec
176 2 dsmv
 
177
    return 0;
178
}
179
 
180
//-----------------------------------------------------------------------------
181
 
182
int pex_board::core_reset()
183
{
184
    return 0;
185
}
186
 
187
//-----------------------------------------------------------------------------
188
 
189
int pex_board::core_close()
190
{
191
    if(bar0) {
192
        munmap(bar0, bi.Size[0]);
193
        bar0 = NULL;
194
    }
195
    if(bar1) {
196
        munmap(bar1, bi.Size[1]);
197
        bar1 = NULL;
198
    }
199
 
200
    close(fd);
201
    fd = -1;
202
 
203
 
204
    return 0;
205
}
206
 
207
//-----------------------------------------------------------------------------
208
 
209
int pex_board::core_load_dsp()
210
{
211
    return 0;
212
}
213
 
214
//-----------------------------------------------------------------------------
215
 
216
int pex_board::core_load_pld()
217
{
218
    return 0;
219
}
220
 
221
//-----------------------------------------------------------------------------
222
 
223
int pex_board::core_board_info()
224
{
225
    int error = ioctl(fd, IOCTL_PEX_BOARD_INFO, &bi);
226
    if(error < 0) {
227
        std::cerr << __FUNCTION__ << "(): " << " error get board info" << endl;
228
        return -1;
229
    }
230
/*
231
    fprintf(stderr, "VENDOR ID: 0x%X\n", bi.vendor_id);
232
    fprintf(stderr, "DEVICE ID: 0x%X\n", bi.device_id);
233
    fprintf(stderr, "BAR0: 0x%zX\n", bi.PhysAddress[0]);
234
    fprintf(stderr, "SIZE: 0x%zX\n", bi.Size[0]);
235
    fprintf(stderr, "BAR1 0x%zX\n", bi.PhysAddress[1]);
236
    fprintf(stderr, "SIZE: 0x%zX\n", bi.Size[1]);
237
    fprintf(stderr, "IRQ: 0x%zX\n", bi.InterruptVector);
238
*/
239
    return 0;
240
}
241
 
242
//-----------------------------------------------------------------------------
243
 
244
int pex_board::core_pld_info()
245
{
246
    u32 d = 0;
247
    u32 d1 = 0;
248
    u32 d2 = 0;
249
    u32 d3 = 0;
250
    u32 d4 = 0;
251
    u32 d5 = 0;
252
    int ii = 0;
253
 
254
    if(!bar1)
255
        return -1;
256
 
257
    fprintf(stderr," Firmware PLD ADM\n" );
258
    core_reg_poke_dir(0, 1, 1);
259
    core_reg_poke_dir(0, 1, 1);
260
 
261
    d=core_reg_peek_ind( 0, 0x108 );
262
    if( d==0x4953 ) {
263
        fprintf(stderr, " SIG = 0x%.4X - Ok\n", d );
264
    } else {
265
        fprintf(stderr, " SIG = 0x%.4X - Error, waiting 0x4953\n", d );
266
        return -1;
267
    }
268
 
269
    d=core_reg_peek_ind(  0, 0x109 );  fprintf(stderr, " ADM interface version:  %d.%d\n", d>>8, d&0xFF );
270
    d=core_reg_peek_ind(  0, 0x110 ); d1=core_reg_peek_ind(  0, 0x111 );
271
    fprintf(stderr,  " Base module: 0x%.4X  v%d.%d\n", d, d1>>8, d1&0xFF );
272
 
273
    d=core_reg_peek_ind(  0, 0x112 ); d1=core_reg_peek_ind(  0, 0x113 );
274
    fprintf(stderr,  " Submodule: 0x%.4X  v%d.%d\n", d, d1>>8, d1&0xFF );
275
 
276
    d=core_reg_peek_ind(  0, 0x10B );  fprintf(stderr,  " Firmware modificaton:  %d \n", d );
277
    d=core_reg_peek_ind(  0, 0x10A );  fprintf(stderr,  " Firmware version:       %d.%d\n", d>>8, d&0xFF );
278
    d=core_reg_peek_ind(  0, 0x114 );  fprintf(stderr,  " Firmware build number: 0x%.4X\n", d );
279
 
280
    fprintf(stderr,  "\n Information about the tetrads:\n\n" );
281
    for( ii=0; ii<8; ii++ ) {
282
 
283
        const char *str;
284
 
285
        d=core_reg_peek_ind(  ii, 0x100 );
286
        d1=core_reg_peek_ind(  ii, 0x101 );
287
        d2=core_reg_peek_ind(  ii, 0x102 );
288
        d3=core_reg_peek_ind(  ii, 0x103 );
289
        d4=core_reg_peek_ind(  ii, 0x104 );
290
        d5=core_reg_peek_ind(  ii, 0x105 );
291
 
292
        switch( d ) {
293 6 v.karak
        case 1: str="TRD_MAIN         "; break;
294
        case 2: str="TRD_BASE_DAC     "; break;
295
        case 3: str="TRD_PIO_STD      "; break;
296 2 dsmv
        case 0:    str=" -            "; break;
297
        case 0x47: str="SBSRAM_IN     "; break;
298
        case 0x48: str="SBSRAM_OUT    "; break;
299
        case 0x12: str="DIO64_OUT     "; break;
300
        case 0x13: str="DIO64_IN      "; break;
301
        case 0x14: str="ADM212x200M   "; break;
302
        case 0x5D: str="ADM212x500M   "; break;
303
        case 0x41: str="DDS9956       "; break;
304
        case 0x4F: str="TEST_CTRL     "; break;
305
        case 0x3F: str="ADM214x200M   "; break;
306
        case 0x40: str="ADM216x100    "; break;
307
        case 0x2F: str="ADM28x1G      "; break;
308
        case 0x2D: str="TRD128_OUT    "; break;
309
        case 0x4C: str="TRD128_IN     "; break;
310
        case 0x30: str="ADMDDC5016    "; break;
311
        case 0x2E: str="ADMFOTR2G     "; break;
312
        case 0x49: str="ADMFOTR3G     "; break;
313
        case 0x67: str="DDS9912       "; break;
314
        case 0x70: str="AMBPEX5_SDRAM "; break;
315
        case 0x71: str="TRD_MSG       "; break;
316
        case 0x72: str="TRD_TS201     "; break;
317
        case 0x73: str="TRD_STREAM_IN "; break;
318
        case 0x74: str="TRD_STREAM_OUT"; break;
319 6 v.karak
        case 0xA0: str="TRD_ADC       "; break;
320
        case 0xA1: str="TRD_DAC       "; break;
321
        case 0x91: str="TRD_EMAC      "; break;
322 2 dsmv
 
323
 
324
        default: str="UNKNOWN"; break;
325
        }
326
        fprintf(stderr,  " %d  0x%.4X %s ", ii, d, str );
327
        if( d>0 ) {
328
            fprintf(stderr,  " MOD: %-2d VER: %d.%d ", d1, d2>>8, d2&0xFF );
329
            if( d3 & 0x10 ) {
330
                fprintf(stderr,  "FIFO IN   %dx%d\n", d4, d5 );
331
            } else if( d3 & 0x20 ) {
332
                fprintf(stderr,  "FIFO OUT  %dx%d\n", d4, d5 );
333
            } else {
334
                fprintf(stderr,  "\n" );
335
            }
336
        } else {
337
            fprintf(stderr,  "\n" );
338
        }
339
 
340
    }
341
 
342
    return 0;
343
}
344
 
345
//-----------------------------------------------------------------------------
346
 
347
int pex_board::core_resource()
348
{
349
    return 0;
350
}
351
 
352
//-----------------------------------------------------------------------------
353
 
354 6 v.karak
void pex_board::core_delay(int ms)
355 2 dsmv
{
356
    struct timeval tv = {0, 0};
357
    tv.tv_usec = 1000*ms;
358
 
359
    select(0,NULL,NULL,NULL,&tv);
360
}
361
 
362
//-----------------------------------------------------------------------------
363
 
364
u32 pex_board::core_reg_peek_dir( u32 trd, u32 reg )
365
{
366
    if( (trd>15) || (reg>3) )
367
        return -1;
368
 
369
    u32 offset = trd*0x4000 + reg*0x1000;
370
    u32 ret = *(bar1 + offset/4);
371
 
372
    return ret;
373
}
374
 
375
//-----------------------------------------------------------------------------
376
 
377
u32 pex_board::core_reg_peek_ind( u32 trd, u32 reg )
378
{
379
    if( (trd>15) || (reg>0x3FF) )
380
        return -1;
381
 
382
    u32 status;
383
    u32 Status  = trd*0x4000;
384
    u32 CmdAdr  = trd*0x4000 + 0x2000;
385
    u32 CmdData = trd*0x4000 + 0x3000;
386
    u32 ret;
387
 
388
    bar1[CmdAdr/4] = reg;
389
 
390
    for( int ii=0; ; ii++ ) {
391
 
392
        status = bar1[Status/4];
393
        if( status & 1 )
394
            break;
395
 
396
        if( ii>10000 )
397 6 v.karak
            core_delay( 1 );
398 2 dsmv
        if( ii>20000 ) {
399
            return 0xFFFF;
400
        }
401
    }
402
 
403
    ret = bar1[CmdData/4];
404
    ret &= 0xFFFF;
405
 
406
    return ret;
407
}
408
 
409
//-----------------------------------------------------------------------------
410
 
411
void pex_board::core_reg_poke_dir( u32 trd, u32 reg, u32 val )
412
{
413
    if( (trd>15) || (reg>3) )
414
        return;
415
 
416
    u32 offset = trd*0x4000+reg*0x1000;
417
 
418
    bar1[offset/4]=val;
419
}
420
 
421
//-----------------------------------------------------------------------------
422
 
423
void pex_board::core_reg_poke_ind( u32 trd, u32 reg, u32 val )
424
{
425
    if( (trd>15) || (reg>0x3FF) )
426
        return;
427
 
428
    u32 status;
429
    u32 Status  = trd*0x4000;
430
    u32 CmdAdr  = trd*0x4000 + 0x2000;
431
    u32 CmdData = trd*0x4000 + 0x3000;
432
 
433
    bar1[CmdAdr/4] = reg;
434
 
435
    for( int ii=0; ; ii++ ) {
436
 
437
        status = bar1[Status/4];
438
        if( status & 1 )
439
            break;
440
 
441
        if( ii>10000 )
442 6 v.karak
            core_delay( 1 );
443 2 dsmv
        if( ii>20000 ) {
444
            return;
445
        }
446
    }
447
 
448
    bar1[CmdData/4] = val;
449
}
450
 
451
//-----------------------------------------------------------------------------
452
 
453
u32  pex_board::core_bar0_read( u32 offset )
454
{
455 6 v.karak
    return bar0[2*offset];
456 2 dsmv
}
457
 
458
//-----------------------------------------------------------------------------
459
 
460
void pex_board::core_bar0_write( u32 offset, u32 val )
461
{
462 6 v.karak
    bar0[2*offset] = val;
463 2 dsmv
}
464
 
465
//-----------------------------------------------------------------------------
466
 
467
u32  pex_board::core_bar1_read( u32 offset )
468
{
469 6 v.karak
    return bar1[2*offset];
470 2 dsmv
}
471
 
472
//-----------------------------------------------------------------------------
473
 
474
void pex_board::core_bar1_write( u32 offset, u32 val )
475
{
476 19 dsmv
    bar1[2*offset] = val;
477 2 dsmv
}
478
 
479
//-----------------------------------------------------------------------------
480
 
481
void pex_board::core_block_write( u32 nb, u32 reg, u32 val )
482
{
483
    if( (nb>7) || (reg>31) )
484
        return;
485
 
486
    *(bar0+nb*64+reg*2)=val;
487
}
488
 
489
//-----------------------------------------------------------------------------
490
 
491
u32  pex_board::core_block_read( u32 nb, u32 reg )
492
{
493
    if( (nb>7) || (reg>31) )
494
        return -1;
495
 
496
    u32 ret = 0;
497
 
498
    ret=*(bar0+nb*64+reg*2);
499
    if( reg<8 )
500
        ret&=0xFFFF;
501
 
502
    return ret;
503
}
504
 
505
//-----------------------------------------------------------------------------
506
 
507
u32 pex_board::core_alloc(int DmaChan, BRDctrl_StreamCBufAlloc* sSCA)
508
{
509
    m_DescrSize[DmaChan] = sizeof(AMB_MEM_DMA_CHANNEL) + (sSCA->blkNum - 1) * sizeof(void*);
510
    m_Descr[DmaChan] = (AMB_MEM_DMA_CHANNEL*) new u8[m_DescrSize[DmaChan]];
511
 
512
    m_Descr[DmaChan]->DmaChanNum = DmaChan;
513
    m_Descr[DmaChan]->Direction = sSCA->dir;
514
    m_Descr[DmaChan]->LocalAddr = 0;
515
    m_Descr[DmaChan]->MemType = sSCA->isCont;
516
    m_Descr[DmaChan]->BlockCnt = sSCA->blkNum;
517
    m_Descr[DmaChan]->BlockSize = sSCA->blkSize;
518
    m_Descr[DmaChan]->pStub = NULL;
519
 
520
    for(u32 iBlk = 0; iBlk < sSCA->blkNum; iBlk++) {
521
            m_Descr[DmaChan]->pBlock[iBlk] = NULL;
522
    }
523
 
524
    if( ioctl(fd, IOCTL_AMB_SET_MEMIO, m_Descr[DmaChan]) < 0 ) {
525
        fprintf(stderr, "%s(): Error allocate memory\n", __FUNCTION__ );
526
        return -1;
527
    }
528
 
529
    for(u32 iBlk = 0; iBlk < m_Descr[DmaChan]->BlockCnt; iBlk++) {
530
 
531
        void *MappedAddress = mmap( NULL,
532
                                    m_Descr[DmaChan]->BlockSize,
533
                                    PROT_READ | PROT_WRITE,
534
                                    MAP_SHARED,
535
                                    fd,
536
                                    (off_t)m_Descr[DmaChan]->pBlock[iBlk] );
537
 
538
        if(MappedAddress == MAP_FAILED) {
539
            fprintf(stderr, "%s(): Error map memory\n", __FUNCTION__ );
540
            return -1;
541
        }
542
 
543
        fprintf(stderr,"%d: %p -> %p\n", iBlk, (void*)m_Descr[DmaChan]->pBlock[iBlk], MappedAddress);
544
 
545
        //сохраним отображенный в процесс физический адрес текущего блока
546
        m_Descr[DmaChan]->pBlock[iBlk] = MappedAddress;
547
        sSCA->ppBlk[iBlk] = MappedAddress;
548
/*
549
        u32 *buffer = (u32*)MappedAddress;
550
        for(u32 jj=0; jj<m_Descr[DmaChan]->BlockSize/4; jj+=0x100) {
551
            fprintf(stdout,"%x ", buffer[jj]);
552
        }
553
        fprintf(stdout,"\n");
554
*/
555
    }
556
 
557
    if(m_Descr[DmaChan]->pStub) {
558
 
559
        void *StubAddress = mmap( NULL,
560
                                  sizeof(AMB_STUB),
561
                                  PROT_READ | PROT_WRITE,
562
                                  MAP_SHARED,
563
                                  fd,
564
                                  (off_t)m_Descr[DmaChan]->pStub );
565
 
566
        if(StubAddress == MAP_FAILED) {
567
            fprintf(stderr, "%s(): Error map stub\n", __FUNCTION__ );
568
            return -1;
569
        }
570
 
571
        fprintf(stderr,"Stub: %p -> %p\n", (void*)m_Descr[DmaChan]->pStub, StubAddress);
572
 
573
        m_Descr[DmaChan]->pStub = StubAddress;
574
        sSCA->pStub = (BRDstrm_Stub*)m_Descr[DmaChan]->pStub;
575
    }
576
 
577
    //сохраним информацию в буфере пользователя
578
    sSCA->blkNum = m_Descr[DmaChan]->BlockCnt;
579
 
580
    return 0;
581
}
582
 
583
//-----------------------------------------------------------------------------
584
 
585 6 v.karak
u32 pex_board::core_allocate_memory(int DmaChan,
586
                                    void** pBuf,
587
                                    u32 blkSize,
588
                                    u32 blkNum,
589
                                    u32 isSysMem,
590
                                    u32 dir,
591
                                    u32 addr,
592
                                    BRDstrm_Stub **pStub )
593 2 dsmv
{
594
    m_DescrSize[DmaChan] = sizeof(AMB_MEM_DMA_CHANNEL) + (blkNum - 1) * sizeof(void*);
595
    m_Descr[DmaChan] = (AMB_MEM_DMA_CHANNEL*) new u8[m_DescrSize[DmaChan]];
596
 
597
    m_Descr[DmaChan]->DmaChanNum = DmaChan;
598
    m_Descr[DmaChan]->Direction = dir;
599
    m_Descr[DmaChan]->LocalAddr = addr;
600
    m_Descr[DmaChan]->MemType = isSysMem;
601
    m_Descr[DmaChan]->BlockCnt = blkNum;
602
    m_Descr[DmaChan]->BlockSize = blkSize;
603
    m_Descr[DmaChan]->pStub = NULL;
604
 
605
    for(u32 iBlk = 0; iBlk < blkNum; iBlk++) {
606
            m_Descr[DmaChan]->pBlock[iBlk] = NULL;
607
    }
608
 
609
    if( ioctl(fd, IOCTL_AMB_SET_MEMIO, m_Descr[DmaChan]) < 0 ) {
610
        fprintf(stderr, "%s(): Error allocate memory\n", __FUNCTION__ );
611
        return -1;
612
    }
613
 
614
    for(u32 iBlk = 0; iBlk < blkNum; iBlk++) {
615
 
616
        void *MappedAddress = mmap( NULL,
617
                                    m_Descr[DmaChan]->BlockSize,
618
                                    PROT_READ | PROT_WRITE,
619
                                    MAP_SHARED,
620
                                    fd,
621
                                    (off_t)m_Descr[DmaChan]->pBlock[iBlk] );
622
 
623
        if(MappedAddress == MAP_FAILED) {
624
            fprintf(stderr, "%s(): Error map memory\n", __FUNCTION__ );
625
            return -1;
626
        }
627
 
628
        fprintf(stderr,"%d: %p -> %p\n", iBlk, (void*)m_Descr[DmaChan]->pBlock[iBlk], MappedAddress);
629
 
630
        //сохраним отображенный в процесс физический адрес текущего блока
631
        m_Descr[DmaChan]->pBlock[iBlk] = MappedAddress;
632
    }
633
 
634
    if(m_Descr[DmaChan]->pStub) {
635
 
636
        void *StubAddress = mmap( NULL,
637
                                  sizeof(AMB_STUB),
638
                                  PROT_READ | PROT_WRITE,
639
                                  MAP_SHARED,
640
                                  fd,
641
                                  (off_t)m_Descr[DmaChan]->pStub );
642
 
643
        if(StubAddress == MAP_FAILED) {
644
            fprintf(stderr, "%s(): Error map stub\n", __FUNCTION__ );
645
            return -1;
646
        }
647
 
648
        fprintf(stderr,"Stub: %p -> %p\n", (void*)m_Descr[DmaChan]->pStub, StubAddress);
649
 
650
        m_Descr[DmaChan]->pStub = StubAddress;
651
    }
652
 
653
    *pBuf = &m_Descr[DmaChan]->pBlock[0];
654 6 v.karak
    *pStub = (BRDstrm_Stub*)m_Descr[DmaChan]->pStub;
655 2 dsmv
 
656
    return 0;
657
}
658
 
659
//-----------------------------------------------------------------------------
660
 
661
u32  pex_board::core_free_memory(int DmaChan)
662
{
663
    for(u32 iBlk = 0; iBlk < m_Descr[DmaChan]->BlockCnt; iBlk++) {
664
 
665
        munmap( m_Descr[DmaChan]->pBlock[iBlk], m_Descr[DmaChan]->BlockSize );
666
    }
667
 
668
    munmap( m_Descr[DmaChan]->pStub, sizeof(AMB_STUB) );
669
 
670
    if(ioctl(fd, IOCTL_AMB_FREE_MEMIO, m_Descr[DmaChan]) < 0) {
671
        fprintf(stderr, "%s(): Error free memory\n", __FUNCTION__ );
672
        return -1;
673
    }
674
 
675
    delete m_Descr[DmaChan];
676
    m_Descr[DmaChan] = NULL;
677
 
678
    return 0;
679
}
680
 
681
//-----------------------------------------------------------------------------
682
 
683
u32  pex_board::core_start_dma(int DmaChan, int IsCycling)
684
{
685 19 dsmv
 
686
fprintf(stderr, "%s(): DmaChan=%d IsCycling=%d \n", __FUNCTION__, DmaChan, IsCycling );
687 2 dsmv
    if(m_Descr[DmaChan])
688
    {
689
        AMB_START_DMA_CHANNEL StartDescrip;
690
        StartDescrip.DmaChanNum = DmaChan;
691
        StartDescrip.IsCycling = IsCycling;
692
 
693 19 dsmv
        fprintf(stderr, "%s(): IOCTL_AMB_START_MEMIO  ", __FUNCTION__ );
694
 
695 2 dsmv
        if (ioctl(fd,IOCTL_AMB_START_MEMIO,&StartDescrip) < 0) {
696
            fprintf(stderr, "%s(): Error start DMA\n", __FUNCTION__ );
697
            return -1;
698
        }
699 19 dsmv
        fprintf(stderr, " %s - OK \n", __FUNCTION__ );
700 2 dsmv
    }
701
    return 0;
702
}
703
 
704
//-----------------------------------------------------------------------------
705
 
706
u32  pex_board::core_stop_dma(int DmaChan)
707
{
708
    if(m_Descr[DmaChan])
709
    {
710
        AMB_STUB* pStub = (AMB_STUB*)m_Descr[DmaChan]->pStub;
711
        if(pStub->state == STATE_RUN)
712
        {
713
            AMB_STATE_DMA_CHANNEL StateDescrip;
714
            StateDescrip.DmaChanNum = DmaChan;
715
            StateDescrip.Timeout = 0;//pState->timeout; останавливает немедленно (в 0-кольце оставлю пока возможность ожидания)
716
 
717
            if (ioctl(fd, IOCTL_AMB_STOP_MEMIO, &StateDescrip) < 0) {
718
                fprintf(stderr, "%s(): Error stop DMA\n", __FUNCTION__ );
719
                return -1;
720
            }
721
        }
722
    }
723
    return 0;
724
}
725
 
726
//-----------------------------------------------------------------------------
727
 
728
u32  pex_board::core_state_dma(int DmaChan, u32 msTimeout, int& state, u32& blkNum)
729
{
730
    AMB_STATE_DMA_CHANNEL StateDescrip;
731
    StateDescrip.DmaChanNum = DmaChan;
732
    StateDescrip.Timeout = msTimeout;
733
 
734
    if (0 > ioctl(fd, IOCTL_AMB_STATE_MEMIO, &StateDescrip)) {
735
        fprintf(stderr, "%s(): Error state DMA\n", __FUNCTION__ );
736
        return -1;
737
    }
738
    blkNum = StateDescrip.BlockCntTotal;
739
    state = StateDescrip.DmaChanState;
740
 
741
    return 0;
742
}
743
 
744
//-----------------------------------------------------------------------------
745
 
746
u32  pex_board::core_wait_buffer(int DmaChan, u32 msTimeout)
747
{
748
    AMB_STATE_DMA_CHANNEL WaitDmaDescr;
749
    WaitDmaDescr.DmaChanNum = DmaChan;
750
    WaitDmaDescr.Timeout = msTimeout;
751
 
752
    if(m_Descr[DmaChan])
753
    {
754
        if (0 > ioctl(fd, IOCTL_AMB_WAIT_DMA_BUFFER, &WaitDmaDescr)) {
755
            fprintf(stderr, "%s(): Error wait buffer DMA\n", __FUNCTION__ );
756
            return -1;
757
        }
758
    }
759
 
760
    return 0;
761
}
762
 
763
//-----------------------------------------------------------------------------
764
 
765
u32  pex_board::core_wait_block(int DmaChan, u32 msTimeout)
766
{
767
    AMB_STATE_DMA_CHANNEL WaitDmaDescr;
768
    WaitDmaDescr.DmaChanNum = DmaChan;
769
    WaitDmaDescr.Timeout = msTimeout;
770
 
771
    if(m_Descr[DmaChan])
772
    {
773
        if (0 > ioctl(fd, IOCTL_AMB_WAIT_DMA_BLOCK, &WaitDmaDescr)) {
774
            fprintf(stderr, "%s(): Error wait block DMA\n", __FUNCTION__ );
775
            return -1;
776
        }
777
    }
778
 
779
    return 0;
780
}
781
 
782
//-----------------------------------------------------------------------------
783
 
784
u32 pex_board::core_reset_fifo(int DmaChan)
785
{
786
    AMB_SET_DMA_CHANNEL DmaParam;
787
    DmaParam.DmaChanNum = DmaChan;
788
    DmaParam.Param = 0;
789
 
790
    if(m_Descr[DmaChan])
791
    {
792
        if (0 > ioctl(fd, IOCTL_AMB_RESET_FIFO, &DmaParam)) {
793
            fprintf(stderr, "%s(): Error reset FIFO\n", __FUNCTION__ );
794
            return -1;
795
        }
796
    }
797
 
798
    return 0;
799
}
800
 
801
//-----------------------------------------------------------------------------
802
 
803
u32 pex_board::core_set_local_addr(int DmaChan, u32 addr)
804
{
805
    AMB_SET_DMA_CHANNEL DmaParam;
806
    DmaParam.DmaChanNum = DmaChan;
807 5 v.karak
    DmaParam.Param = addr;
808 2 dsmv
 
809 19 dsmv
fprintf(stderr, "%s(): DmaChan=%d addr=0x%.8X \n", __FUNCTION__, DmaChan, addr );
810
 
811
 
812 2 dsmv
    if(m_Descr[DmaChan])
813
    {
814
        if (0 > ioctl(fd, IOCTL_AMB_SET_SRC_MEM, &DmaParam)) {
815
            fprintf(stderr, "%s(): Error set source for DMA\n", __FUNCTION__ );
816
            return -1;
817
        }
818 19 dsmv
 
819
 
820
       DmaParam.Param = m_Descr[DmaChan]->Direction;
821
 
822
          if (0 > ioctl(fd, IOCTL_AMB_SET_DIR_MEM, &DmaParam)) {
823
              fprintf(stderr, "%s(): Error set dir for DMA\n", __FUNCTION__ );
824
              return -1;
825
          }
826
 
827
 
828 2 dsmv
    }
829
 
830
    return 0;
831
}
832
 
833
//-----------------------------------------------------------------------------
834
 
835
u32 pex_board::core_adjust(int DmaChan, u32 mode)
836
{
837
    AMB_SET_DMA_CHANNEL DmaParam;
838
    DmaParam.DmaChanNum = DmaChan;
839
    DmaParam.Param = mode;
840
 
841
    if(m_Descr[DmaChan])
842
    {
843
        if (0 > ioctl(fd, IOCTL_AMB_ADJUST, &DmaParam)) {
844
            fprintf(stderr, "%s(): Error adjust DMA\n", __FUNCTION__ );
845
            return -1;
846
        }
847
    }
848
 
849
    return 0;
850
}
851
 
852
//-----------------------------------------------------------------------------
853
 
854
u32 pex_board::core_done(int DmaChan, u32 blockNumber)
855
{
856
    AMB_SET_DMA_CHANNEL DmaParam;
857
    DmaParam.DmaChanNum = DmaChan;
858
    DmaParam.Param = blockNumber;
859
 
860
    if(m_Descr[DmaChan])
861
    {
862
        if (0 > ioctl(fd, IOCTL_AMB_DONE, &DmaParam)) {
863
            fprintf(stderr, "%s(): Error done DMA\n", __FUNCTION__ );
864
            return -1;
865
        }
866
    }
867
 
868
    return 0;
869
}
870
 
871
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.