/alps/ipecamera

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/ipecamera

« back to all changes in this revision

Viewing changes to dma/nwl_engine.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-04-27 00:28:57 UTC
  • Revision ID: csa@suren.me-20150427002857-82fk6r3e8rfgy4wr
First stand-alone ipecamera implementation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#define _BSD_SOURCE
2
 
 
3
 
#include <stdio.h>
4
 
#include <stdlib.h>
5
 
#include <string.h>
6
 
#include <unistd.h>
7
 
#include <sys/time.h>
8
 
 
9
 
#include "pci.h"
10
 
#include "pcilib.h"
11
 
#include "error.h"
12
 
#include "tools.h"
13
 
#include "nwl_private.h"
14
 
 
15
 
#include "nwl_defines.h"
16
 
 
17
 
#include "nwl_engine_buffers.h"
18
 
 
19
 
int dma_nwl_read_engine_config(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info, char *base) {
20
 
    uint32_t val;
21
 
    
22
 
    info->base_addr = base;
23
 
    
24
 
    nwl_read_register(val, ctx, base, REG_DMA_ENG_CAP);
25
 
 
26
 
    if ((val & DMA_ENG_PRESENT_MASK) == 0) return PCILIB_ERROR_NOTAVAILABLE;
27
 
    
28
 
    info->desc.addr = (val & DMA_ENG_NUMBER) >> DMA_ENG_NUMBER_SHIFT;
29
 
    if ((info->desc.addr > PCILIB_MAX_DMA_ENGINES)||(info->desc.addr < 0)) return PCILIB_ERROR_INVALID_DATA;
30
 
    
31
 
    switch (val & DMA_ENG_DIRECTION_MASK) {
32
 
        case  DMA_ENG_C2S:
33
 
            info->desc.direction = PCILIB_DMA_FROM_DEVICE;
34
 
        break;
35
 
        default:
36
 
            info->desc.direction = PCILIB_DMA_TO_DEVICE;
37
 
    }
38
 
    
39
 
    switch (val & DMA_ENG_TYPE_MASK) {
40
 
        case DMA_ENG_BLOCK:
41
 
            info->desc.type = PCILIB_DMA_TYPE_BLOCK;
42
 
        break;
43
 
        case DMA_ENG_PACKET:
44
 
            info->desc.type = PCILIB_DMA_TYPE_PACKET;
45
 
        break;
46
 
        default:
47
 
            info->desc.type = PCILIB_DMA_TYPE_UNKNOWN;
48
 
    }
49
 
    
50
 
    info->desc.addr_bits = (val & DMA_ENG_BD_MAX_BC) >> DMA_ENG_BD_MAX_BC_SHIFT;
51
 
 
52
 
    info->base_addr = base;
53
 
    
54
 
    return 0;
55
 
}
56
 
 
57
 
int dma_nwl_start_engine(nwl_dma_t *ctx, pcilib_dma_engine_t dma) {
58
 
    int err;
59
 
    uint32_t val;
60
 
    uint32_t ring_pa;
61
 
    struct timeval start, cur;
62
 
 
63
 
    pcilib_nwl_engine_description_t *info = ctx->engines + dma;
64
 
    char *base = ctx->engines[dma].base_addr;
65
 
    
66
 
    if (info->started) return 0;
67
 
 
68
 
 
69
 
        // This will only successed if there are no parallel access to DMA engine
70
 
    err = dma_nwl_allocate_engine_buffers(ctx, info);
71
 
    if (err) {
72
 
        info->started = 1;
73
 
        dma_nwl_stop_engine(ctx, dma);
74
 
        return err;
75
 
    }
76
 
    
77
 
    if (info->reused) {
78
 
        info->preserve = 1;
79
 
 
80
 
        dma_nwl_acknowledge_irq((pcilib_dma_context_t*)ctx, PCILIB_DMA_IRQ, dma);
81
 
 
82
 
#ifdef NWL_GENERATE_DMA_IRQ
83
 
        dma_nwl_enable_engine_irq(ctx, dma);
84
 
#endif /* NWL_GENERATE_DMA_IRQ */
85
 
    } else {
86
 
        // Disable IRQs
87
 
        err = dma_nwl_disable_engine_irq(ctx, dma);
88
 
        if (err) {
89
 
            info->started = 1;
90
 
            dma_nwl_stop_engine(ctx, dma);
91
 
            return err;
92
 
        }
93
 
 
94
 
        // Disable Engine & Reseting 
95
 
        val = DMA_ENG_DISABLE|DMA_ENG_USER_RESET;
96
 
        nwl_write_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
97
 
 
98
 
        gettimeofday(&start, NULL);
99
 
        do {
100
 
            nwl_read_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
101
 
            gettimeofday(&cur, NULL);
102
 
        } while ((val & (DMA_ENG_STATE_MASK|DMA_ENG_USER_RESET))&&(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < PCILIB_REGISTER_TIMEOUT));
103
 
    
104
 
        if (val & (DMA_ENG_STATE_MASK|DMA_ENG_USER_RESET)) {
105
 
            pcilib_error("Timeout during reset of DMA engine %i", info->desc.addr);
106
 
 
107
 
            info->started = 1;
108
 
            dma_nwl_stop_engine(ctx, dma);
109
 
            return PCILIB_ERROR_TIMEOUT;
110
 
        }
111
 
 
112
 
        val = DMA_ENG_RESET; 
113
 
        nwl_write_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
114
 
    
115
 
        gettimeofday(&start, NULL);
116
 
        do {
117
 
            nwl_read_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
118
 
            gettimeofday(&cur, NULL);
119
 
        } while ((val & DMA_ENG_RESET)&&(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < PCILIB_REGISTER_TIMEOUT));
120
 
    
121
 
        if (val & DMA_ENG_RESET) {
122
 
            pcilib_error("Timeout during reset of DMA engine %i", info->desc.addr);
123
 
 
124
 
            info->started = 1;
125
 
            dma_nwl_stop_engine(ctx, dma);
126
 
            return PCILIB_ERROR_TIMEOUT;
127
 
        }
128
 
    
129
 
        dma_nwl_acknowledge_irq((pcilib_dma_context_t*)ctx, PCILIB_DMA_IRQ, dma);
130
 
 
131
 
        ring_pa = pcilib_kmem_get_pa(ctx->pcilib, info->ring);
132
 
        nwl_write_register(ring_pa, ctx, info->base_addr, REG_DMA_ENG_NEXT_BD);
133
 
        nwl_write_register(ring_pa, ctx, info->base_addr, REG_SW_NEXT_BD);
134
 
 
135
 
        __sync_synchronize();
136
 
 
137
 
        nwl_read_register(val, ctx, info->base_addr, REG_DMA_ENG_CTRL_STATUS);
138
 
        val |= (DMA_ENG_ENABLE);
139
 
        nwl_write_register(val, ctx, info->base_addr, REG_DMA_ENG_CTRL_STATUS);
140
 
 
141
 
        __sync_synchronize();
142
 
 
143
 
#ifdef NWL_GENERATE_DMA_IRQ
144
 
        dma_nwl_enable_engine_irq(ctx, dma);
145
 
#endif /* NWL_GENERATE_DMA_IRQ */
146
 
 
147
 
        if (info->desc.direction == PCILIB_DMA_FROM_DEVICE) {
148
 
            ring_pa += (info->ring_size - 1) * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
149
 
            nwl_write_register(ring_pa, ctx, info->base_addr, REG_SW_NEXT_BD);
150
 
 
151
 
            info->tail = 0;
152
 
            info->head = (info->ring_size - 1);
153
 
        } else {
154
 
            info->tail = 0;
155
 
            info->head = 0;
156
 
        }
157
 
    }
158
 
    
159
 
    info->started = 1;
160
 
    
161
 
    return 0;
162
 
}
163
 
 
164
 
 
165
 
int dma_nwl_stop_engine(nwl_dma_t *ctx, pcilib_dma_engine_t dma) {
166
 
    int err;
167
 
    uint32_t val;
168
 
    uint32_t ring_pa;
169
 
    struct timeval start, cur;
170
 
    pcilib_kmem_flags_t flags;
171
 
    
172
 
    
173
 
    pcilib_nwl_engine_description_t *info = ctx->engines + dma;
174
 
    char *base = ctx->engines[dma].base_addr;
175
 
 
176
 
    if (!info->started) return 0;
177
 
    
178
 
    info->started = 0;
179
 
 
180
 
    err = dma_nwl_disable_engine_irq(ctx, dma);
181
 
    if (err) return err;
182
 
 
183
 
    if (!info->preserve) {
184
 
            // Stopping DMA is not enough reset is required
185
 
        val = DMA_ENG_DISABLE|DMA_ENG_USER_RESET|DMA_ENG_RESET;
186
 
        nwl_write_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
187
 
 
188
 
        gettimeofday(&start, NULL);
189
 
        do {
190
 
            nwl_read_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
191
 
            gettimeofday(&cur, NULL);
192
 
        } while ((val & (DMA_ENG_RUNNING))&&(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < PCILIB_REGISTER_TIMEOUT));
193
 
 
194
 
        if (info->ring) {
195
 
            ring_pa = pcilib_kmem_get_pa(ctx->pcilib, info->ring);
196
 
            nwl_write_register(ring_pa, ctx, info->base_addr, REG_DMA_ENG_NEXT_BD);
197
 
            nwl_write_register(ring_pa, ctx, info->base_addr, REG_SW_NEXT_BD);
198
 
        }
199
 
    }
200
 
    
201
 
    dma_nwl_acknowledge_irq((pcilib_dma_context_t*)ctx, PCILIB_DMA_IRQ, dma);
202
 
 
203
 
    if (info->preserve) {
204
 
        flags = PCILIB_KMEM_FLAG_REUSE;
205
 
    } else {
206
 
        flags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT;
207
 
    }
208
 
    
209
 
        // Clean buffers
210
 
    if (info->ring) {
211
 
        pcilib_free_kernel_memory(ctx->pcilib, info->ring, flags);
212
 
        info->ring = NULL;
213
 
    }
214
 
 
215
 
    if (info->pages) {
216
 
        pcilib_free_kernel_memory(ctx->pcilib, info->pages, flags);
217
 
        info->pages = NULL;
218
 
    }
219
 
 
220
 
    return 0;
221
 
}
222
 
 
223
 
int dma_nwl_write_fragment(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, void *data, size_t *written) {
224
 
    int err;
225
 
    size_t pos;
226
 
    size_t bufnum;
227
 
    nwl_dma_t *ctx = (nwl_dma_t*)vctx;
228
 
 
229
 
    pcilib_nwl_engine_description_t *info = ctx->engines + dma;
230
 
 
231
 
    err = dma_nwl_start(vctx, dma, PCILIB_DMA_FLAGS_DEFAULT);
232
 
    if (err) return err;
233
 
 
234
 
    if (data) {
235
 
        for (pos = 0; pos < size; pos += info->page_size) {
236
 
            int block_size = min2(size - pos, info->page_size);
237
 
            
238
 
            bufnum = dma_nwl_get_next_buffer(ctx, info, 1, timeout);
239
 
            if (bufnum == PCILIB_DMA_BUFFER_INVALID) {
240
 
                if (written) *written = pos;
241
 
                return PCILIB_ERROR_TIMEOUT;
242
 
            }
243
 
        
244
 
            void *buf = pcilib_kmem_get_block_ua(ctx->pcilib, info->pages, bufnum);
245
 
 
246
 
            pcilib_kmem_sync_block(ctx->pcilib, info->pages, PCILIB_KMEM_SYNC_FROMDEVICE, bufnum);
247
 
            memcpy(buf, data, block_size);
248
 
            pcilib_kmem_sync_block(ctx->pcilib, info->pages, PCILIB_KMEM_SYNC_TODEVICE, bufnum);
249
 
 
250
 
            err = dma_nwl_push_buffer(ctx, info, block_size, (flags&PCILIB_DMA_FLAG_EOP)&&((pos + block_size) == size), timeout);
251
 
            if (err) {
252
 
                if (written) *written = pos;
253
 
                return err;
254
 
            }
255
 
        }    
256
 
    }
257
 
    
258
 
    if (written) *written = size;
259
 
    
260
 
    if (flags&PCILIB_DMA_FLAG_WAIT) {
261
 
        bufnum =  dma_nwl_get_next_buffer(ctx, info, PCILIB_NWL_DMA_PAGES - 1, timeout);
262
 
        if (bufnum == PCILIB_DMA_BUFFER_INVALID) return PCILIB_ERROR_TIMEOUT;
263
 
    }
264
 
    
265
 
    return 0;
266
 
}
267
 
 
268
 
int dma_nwl_stream_read(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, pcilib_dma_callback_t cb, void *cbattr) {
269
 
    int err, ret = PCILIB_STREAMING_REQ_PACKET;
270
 
    pcilib_timeout_t wait = 0;
271
 
    size_t res = 0;
272
 
    size_t bufnum;
273
 
    size_t bufsize;
274
 
    
275
 
    nwl_dma_t *ctx = (nwl_dma_t*)vctx;
276
 
 
277
 
    int eop;
278
 
 
279
 
    pcilib_nwl_engine_description_t *info = ctx->engines + dma;
280
 
 
281
 
    err = dma_nwl_start(vctx, dma, PCILIB_DMA_FLAGS_DEFAULT);
282
 
    if (err) return err;
283
 
    
284
 
    do {
285
 
        switch (ret&PCILIB_STREAMING_TIMEOUT_MASK) {
286
 
            case PCILIB_STREAMING_CONTINUE: wait = PCILIB_DMA_TIMEOUT; break;
287
 
            case PCILIB_STREAMING_WAIT: wait = timeout; break;
288
 
//          case PCILIB_STREAMING_CHECK: wait = 0; break;
289
 
        }
290
 
    
291
 
        bufnum = dma_nwl_wait_buffer(ctx, info, &bufsize, &eop, wait);
292
 
        if (bufnum == PCILIB_DMA_BUFFER_INVALID) {
293
 
            return (ret&PCILIB_STREAMING_FAIL)?PCILIB_ERROR_TIMEOUT:0;
294
 
        }
295
 
 
296
 
            // EOP is not respected in IPE Camera
297
 
        if (ctx->dmactx.ignore_eop) eop = 1;
298
 
        
299
 
        pcilib_kmem_sync_block(ctx->pcilib, info->pages, PCILIB_KMEM_SYNC_FROMDEVICE, bufnum);
300
 
        void *buf = pcilib_kmem_get_block_ua(ctx->pcilib, info->pages, bufnum);
301
 
        ret = cb(cbattr, (eop?PCILIB_DMA_FLAG_EOP:0), bufsize, buf);
302
 
        if (ret < 0) return -ret;
303
 
//      DS: Fixme, it looks like we can avoid calling this for the sake of performance
304
 
//      pcilib_kmem_sync_block(ctx->pcilib, info->pages, PCILIB_KMEM_SYNC_TODEVICE, bufnum);
305
 
        dma_nwl_return_buffer(ctx, info);
306
 
        
307
 
        res += bufsize;
308
 
 
309
 
    } while (ret);
310
 
    
311
 
    return 0;
312
 
}
313
 
 
314
 
int dma_nwl_wait_completion(nwl_dma_t * ctx, pcilib_dma_engine_t dma, pcilib_timeout_t timeout) {
315
 
    if (dma_nwl_get_next_buffer(ctx, ctx->engines + dma, PCILIB_NWL_DMA_PAGES - 1, PCILIB_DMA_TIMEOUT) == (PCILIB_NWL_DMA_PAGES - 1)) return 0;
316
 
    else return PCILIB_ERROR_TIMEOUT;
317
 
}
318