summaryrefslogtreecommitdiff
path: root/cesar/hal/phy/src/bridgedma.c
blob: b01ab9e9588c057fe7eccd3acf02365db35fc2e9 (plain)
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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/phy/src/bridgedma.c
 * \brief   Phy bridgedma HAL functions.
 * \ingroup hal_phy
 *
 */
#include "common/std.h"

#include "hal/arch/arch.h"
#include "hal/leon/itc2.h"

#include "hal/phy/bridgedma.h"
#include "hal/phy/inc/bridgedma.h"
#include "hal/phy/inc/bridgedma_regs.h"
#include "lib/slist.h"
#include "mac/common/timings.h"

#include <string.h>

/** Quick access to start bit. */
#define PHY_BRGDMA_START_BIT \
    BF_GET (PHY_BRIDGEDMA_CONTROL__START, PHY_BDGDMA_CONTROL_CONFIG)

/** Bridge DMA context. */
static phy_bridgedma_t phy_bridgedma_global;

/**
 * Start the bridgedma.
 * \param  ctx  the bridgedma context.
 * \param  job  the job to use for the start.
 */
static inline void
phy_bridge_dma_start__configure (phy_bridgedma_t *ctx,
                                 phy_bridgedma_job_t *job)
{
    PHY_BDGDMA_FIRST_JOBD_PTR = (u32) job;
    /* Start the bridgedma. */
    PHY_BDGDMA_CONTROL_CONFIG = BF_SET (PHY_BDGDMA_CONTROL_CONFIG,
                                        PHY_BRIDGEDMA_CONTROL__START,
                                        true);
}

/**
 * Start the BRGDMA with the pending DMA list.
 * \param  ctx  the bridgedma context.
 */
static void
phy_bridgedma_start_jobs_pending (phy_bridgedma_t *ctx)
{
    /* Set the last bit to true in the last job. */
    ctx->jobs_pending.tail->last = true;
    ctx->jobs_pending.tail->next =
        (phy_bridgedma_job_t *) PHY_BRGDMA_SPC_ADDRESS (
            (u32)ctx->jobs_pending.tail);
    arch_write_buffer_flush ();
    phy_bridge_dma_start__configure (ctx, ctx->jobs_pending.head);
    slist_push_back_range (ctx->bridge.jobs_process.,
                           ctx->jobs_pending.head,
                           ctx->jobs_pending.tail,
                           bare);
    slist_init (ctx->jobs_pending., bare);
}

/**
 * Return the BRG DMA status. (see maria:#913, and maria:#971)
 * \param  ctx  the bridgedma context.
 * \return  true if pending jobs list is not empty, or start bit or running
 * bit is true.
 */
static inline bool
_phy_bridgedma_status (phy_bridgedma_t *ctx)
{
    if (ctx->jobs_pending.head)
    {
        if (!PHY_BRGDMA_START_BIT)
            phy_bridgedma_start_jobs_pending (ctx);
        return true;
    }
    else
    {
        bool started = PHY_BRGDMA_START_BIT;
        bool running =
            BF_GET (PHY_BRIDGEDMA_STATUS_ERROR__RUNNING,
                    PHY_BDGDMA_STATUS_ERROR);
        return started || running;
    }
}

/** eCos ISR called by eCos each time the bridgedma ends a job with the it
 * flag setted.
 * \param  vector  the IT vector.
 * \param  data  the user data.
 */
static cyg_uint32
_bridgedma_ecos_isr(cyg_vector_t vector, cyg_addrword_t data)
{
    /* nothing to do except calling the bridgedma callback */
    phy_bridgedma_t *bridgedma_ctx = (phy_bridgedma_t *) data;

    /* ACK and unmask. */
    cyg_interrupt_acknowledge(PHY_BRIDGEDMA_END_INTERRUPT);
    cyg_interrupt_unmask(PHY_BRIDGEDMA_END_INTERRUPT);

    if((*bridgedma_ctx->bridge.bridgedma_cb)
       (bridgedma_ctx->bridge.user_data,
        _phy_bridgedma_status (bridgedma_ctx)))
        return CYG_ISR_CALL_DSR;  // Cause DSR to be run
    else
    {
        cyg_interrupt_unmask(PHY_BRIDGEDMA_END_INTERRUPT);
        return CYG_ISR_HANDLED;
    }
}

/** eCos DSR called by eCos each time the bridgedma ends a job with the it
 * flag setted.
 * \param  vector  the IT vector.
 * \param  data  the user data.
 */
static void
_bridgedma_ecos_dsr(cyg_vector_t vector, cyg_ucount32 count,
                    cyg_addrword_t data)
{
    /* nothing to do except calling the phy dsr */
    phy_bridgedma_t *bridgedma_ctx;
    bridgedma_ctx = (phy_bridgedma_t *)data;
    (*bridgedma_ctx->bridge.deferred_cb)(bridgedma_ctx->bridge.user_data);
}

phy_bridgedma_t *
phy_bridgedma_init (void *user_data, phy_bridgedma_cb_t bridgedma_cb,
                    phy_deferred_cb_t deferred_cb)
{
    /* DSR is only valid if ISR is set. */
    dbg_assert ((bridgedma_cb && deferred_cb) || !bridgedma_cb);
    memset (&phy_bridgedma_global, 0, sizeof (phy_bridgedma_t));

    phy_bridgedma_global.bridge.user_data = user_data;
    phy_bridgedma_global.bridge.bridgedma_cb = bridgedma_cb;
    phy_bridgedma_global.bridge.deferred_cb = deferred_cb;

    /* Reset bridge DMA. */
    PHY_RB_RST_MODULE = BF_SET (PHY_RB_RST_MODULE,
                                PHY_RB_RST_MODULE__BDGDMA_RESET,
                                1);
    PHY_RB_RST_MODULE = BF_SET (PHY_RB_RST_MODULE,
                                PHY_RB_RST_MODULE__BDGDMA_RESET,
                                0);

    /* ISR function callbacks is defined, register the ISR request to eCos. */
    if (bridgedma_cb)
    {
        /* register ISR and DSR to eCos */
        cyg_interrupt_create(PHY_BRIDGEDMA_END_INTERRUPT,
                             0,
                             (cyg_addrword_t)&phy_bridgedma_global,
                             _bridgedma_ecos_isr,
                             _bridgedma_ecos_dsr,
                             &phy_bridgedma_global.it_mgr.interrupt_handle,
                             &phy_bridgedma_global.it_mgr.interrupt);
        cyg_interrupt_attach(phy_bridgedma_global.it_mgr.interrupt_handle);

        cyg_interrupt_acknowledge(PHY_BRIDGEDMA_END_INTERRUPT);
        cyg_interrupt_unmask(PHY_BRIDGEDMA_END_INTERRUPT);
    }
    slist_init (phy_bridgedma_global.bridge.jobs_process., bare);
    slist_init (phy_bridgedma_global.jobs_pending., bare);
    return &phy_bridgedma_global;
}

void
phy_bridgedma_uninit (phy_bridgedma_t *ctx)
{
    dbg_assert (ctx);
    while (_phy_bridgedma_status (ctx));
}

void
phy_bridgedma_start (phy_bridgedma_t *ctx, phy_bridgedma_job_t *job_first,
                     phy_bridgedma_job_t *job_last)
{
    dbg_assert (ctx);
    dbg_assert (job_first);
    dbg_assert (job_last);
    dbg_assert (job_last->next == NULL);
    slist_push_back_range (ctx->jobs_pending., job_first, job_last, bare);
    /* Bridge DMA is stopped, add the job and start it. */
    if (!PHY_BRGDMA_START_BIT)
        phy_bridgedma_start_jobs_pending (ctx);
}

/**
 * Get the current job descriptor from the bridgedma.
 * \param  ctx  the Bridge DMA context.
 * \return  the address of the current job descriptor beeing processed by the
 * bridge DMA.
 *
 * It corresponds to the current job which is being processed by the
 * bridgedma when the Interruption arrived.
 */
static inline phy_bridgedma_job_t *
phy_bridgedma_current_job (phy_bridgedma_t *ctx)
{
    dbg_assert (ctx);

    return (phy_bridgedma_job_t *) PHY_BDGDMA_CURRENT_JOBD_PTR;
}

bool
phy_bridgedma_status (phy_bridgedma_t *ctx)
{
    return _phy_bridgedma_status (ctx);
}

phy_bridgedma_job_t *
phy_bridgedma_jobs_get_ended (phy_bridgedma_t *ctx)
{
    dbg_assert (ctx);
    phy_bridgedma_job_t *job_head = ctx->bridge.jobs_process.head;

    if (job_head)
    {
        /* Bridgedma is not running. */
        if (!_phy_bridgedma_status (ctx))
        {
            ctx->bridge.jobs_process.tail->next = NULL;
            slist_init (phy_bridgedma_global.bridge.jobs_process., bare);
        }
        else
        {
            phy_bridgedma_job_t *job_current;
            /* Wait current jobs to be fully processed. */
            for (job_current = phy_bridgedma_current_job (ctx);
                 job_current == phy_bridgedma_current_job (ctx)
                 && _phy_bridgedma_status (ctx)
                 ; )
                ;

            if ((u32) job_current
                != PHY_BRGDMA_SPC_ADDRESS ((u32) job_current))
            {
                if ((u32) job_current->next ==
                    PHY_BRGDMA_SPC_ADDRESS((u32)job_current))
                    slist_init (
                        phy_bridgedma_global.bridge.jobs_process., bare);
                else
                    ctx->bridge.jobs_process.head = job_current->next;
                job_current->next = NULL;
            }
            else
            {
                ctx->bridge.jobs_process.tail->next = NULL;
                slist_init (phy_bridgedma_global.bridge.jobs_process., bare);
            }
        }

        /* Return the list. */
        return job_head;
    }

    return NULL;
}

void
phy_bridgedma_stopped (phy_bridgedma_t *ctx)
{
    u32 expired = phy_date () + MAC_MS_TO_TCK (PHY_BRG_POLLING_WAIT_MS);
    while (_phy_bridgedma_status (ctx) && less_mod2p32 (phy_date (), expired))
        ;
    dbg_assert_print (less_mod2p32 (phy_date (), expired),
                      "Bridge DMA freezed");
}