summaryrefslogtreecommitdiffstats
path: root/c/src/libchip/network/dwmac-core.c
blob: 1050751e57a8733b983300c5ca0447e389fc7ee3 (plain) (blame)
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
/**
 * @file
 *
 * @brief DWMAC 10/100/1000 Network Interface Controllers Core Handling
 *
 * DWMAC 10/100/1000 on-chip Synopsys IP Ethernet controllers.
 * Driver core handling.
 */

/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include "dwmac-core.h"
#include "dwmac-common.h"
#include "dwmac-regs.h"

#undef DWMAC_CORE_DEBUG
#ifdef DWMAC_CORE_DEBUG
#define DWMAC_CORE_PRINT_DBG( fmt, args ... )  printk( fmt, ## args )
#else
#define DWMAC_CORE_PRINT_DBG( fmt, args ... )  do { } while ( 0 )
#endif

/* DMA default interrupt masks */
#define DWMAC_CORE_INTR_ENABLE_DEFAULT_MASK_RX \
  ( \
    DMAGRP_INTERRUPT_ENABLE_NIE \
    | DMAGRP_INTERRUPT_ENABLE_RIE \
  )
#define DWMAC_CORE_INTR_ENABLE_DEFAULT_MASK_TX \
  ( \
    DMAGRP_INTERRUPT_ENABLE_NIE \
    | DMAGRP_INTERRUPT_ENABLE_TIE \
    | DMAGRP_INTERRUPT_ENABLE_FBE \
    | DMAGRP_INTERRUPT_ENABLE_UNE \
    | DMAGRP_INTERRUPT_ENABLE_AIE \
  )

#define DWMAC_CORE_INTR_STATUS_DEFAULT_MASK_RX \
  ( \
    DMAGRP_STATUS_NIS \
    | DMAGRP_STATUS_RI \
  )
#define DWMAC_CORE_INTR_STATUS_DEFAULT_MASK_TX \
  ( \
    DMAGRP_STATUS_NIS \
    | DMAGRP_STATUS_TI \
    | DMAGRP_STATUS_FBI \
    | DMAGRP_STATUS_UNF \
    | DMAGRP_STATUS_AIS \
  )

/* CSR1 enables the transmit DMA to check for new descriptor */
void dwmac_core_dma_restart_tx( dwmac_common_context *self )
{
  self->dmagrp->transmit_poll_demand = 1;
}

void dwmac_core_enable_dma_irq_tx( dwmac_common_context *self )
{
  self->dmagrp->interrupt_enable |= DWMAC_CORE_INTR_ENABLE_DEFAULT_MASK_TX;
}

void dwmac_core_enable_dma_irq_rx( dwmac_common_context *self )
{
  self->dmagrp->interrupt_enable |= DWMAC_CORE_INTR_ENABLE_DEFAULT_MASK_RX;
}

void dwmac_core_disable_dma_irq_tx( dwmac_common_context *self )
{
  self->dmagrp->interrupt_enable &= ~DWMAC_CORE_INTR_ENABLE_DEFAULT_MASK_TX;
}

void dwmac_core_reset_dma_irq_status_tx( dwmac_common_context *self )
{
  self->dmagrp->status = DWMAC_CORE_INTR_STATUS_DEFAULT_MASK_TX;
}

void dwmac_core_reset_dma_irq_status_rx( dwmac_common_context *self )
{
  self->dmagrp->status = DWMAC_CORE_INTR_STATUS_DEFAULT_MASK_RX;
}

void dwmac_core_disable_dma_irq_rx( dwmac_common_context *self )
{
  self->dmagrp->interrupt_enable &= ~DWMAC_CORE_INTR_ENABLE_DEFAULT_MASK_RX;
}

void dwmac_core_dma_start_tx( dwmac_common_context *self )
{
  self->dmagrp->operation_mode |= DMAGRP_OPERATION_MODE_ST;
}

void dwmac_core_dma_stop_tx( dwmac_common_context *self )
{
  self->dmagrp->operation_mode &= ~DMAGRP_OPERATION_MODE_ST;
}

void dwmac_core_dma_start_rx( dwmac_common_context *self )
{
  self->dmagrp->operation_mode |= DMAGRP_OPERATION_MODE_SR;
}

void dwmac_core_dma_stop_rx( dwmac_common_context *self )
{
  self->dmagrp->operation_mode &= ~DMAGRP_OPERATION_MODE_SR;
}

void dwmac_core_dma_restart_rx( dwmac_common_context *self )
{
  self->dmagrp->receive_poll_demand = 1;
}

#ifdef DWMAC_CORE_DEBUG
static void show_tx_process_state( const uint32_t status )
{
  const uint32_t STATE = DMAGRP_STATUS_TS_GET( status );


  switch ( STATE ) {
    case 0:
      DWMAC_CORE_PRINT_DBG( "- TX (Stopped): Reset or Stop command\n" );
      break;
    case 1:
      DWMAC_CORE_PRINT_DBG( "- TX (Running):Fetching the Tx desc\n" );
      break;
    case 2:
      DWMAC_CORE_PRINT_DBG( "- TX (Running): Waiting for end of tx\n" );
      break;
    case 3:
      DWMAC_CORE_PRINT_DBG( "- TX (Running): Reading the data "
                            "and queuing the data into the Tx buf\n" );
      break;
    case 6:
      DWMAC_CORE_PRINT_DBG( "- TX (Suspended): Tx Buff Underflow "
                            "or an unavailable Transmit descriptor\n" );
      break;
    case 7:
      DWMAC_CORE_PRINT_DBG( "- TX (Running): Closing Tx descriptor\n" );
      break;
    default:
      break;
  }
}

static void show_rx_process_state( const uint32_t status )
{
  const uint32_t STATE = DMAGRP_STATUS_RS_GET( status );


  switch ( STATE ) {
    case 0:
      DWMAC_CORE_PRINT_DBG( "- RX (Stopped): Reset or Stop command\n" );
      break;
    case 1:
      DWMAC_CORE_PRINT_DBG( "- RX (Running): Fetching the Rx desc\n" );
      break;
    case 2:
      DWMAC_CORE_PRINT_DBG( "- RX (Running):Checking for end of pkt\n" );
      break;
    case 3:
      DWMAC_CORE_PRINT_DBG( "- RX (Running): Waiting for Rx pkt\n" );
      break;
    case 4:
      DWMAC_CORE_PRINT_DBG( "- RX (Suspended): Unavailable Rx buf\n" );
      break;
    case 5:
      DWMAC_CORE_PRINT_DBG( "- RX (Running): Closing Rx descriptor\n" );
      break;
    case 6:
      DWMAC_CORE_PRINT_DBG( "- RX(Running): Flushing the current frame"
                            " from the Rx buf\n" );
      break;
    case 7:
      DWMAC_CORE_PRINT_DBG( "- RX (Running): Queuing the Rx frame"
                            " from the Rx buf into memory\n" );
      break;
    default:
      break;
  }
}

#else /*  DWMAC_CORE_DEBUG */
  #define show_tx_process_state( status )
  #define show_rx_process_state( status )
#endif /* DWMAC_CORE_DEBUG */

void dwmac_core_dma_interrupt( void *arg )
{
  dwmac_common_context        *self            = (dwmac_common_context *) arg;
  dwmac_common_dma_irq_counts *count           = &self->stats.dma_irq_counts;
  rtems_event_set              events_receive  = 0;
  rtems_event_set              events_transmit = 0;

  /* Get interrupt status */
  uint32_t irq_status  = self->dmagrp->status & self->dmagrp->interrupt_enable;
  uint32_t irq_handled = 0;
  uint32_t irq_disable = 0;


  DWMAC_CORE_PRINT_DBG( "%s: [CSR5: 0x%08x]\n", __func__, irq_status );

  /* It displays the DMA process states (CSR5 register) if DWMAC_CORE_DEBUG is #defined */
  show_tx_process_state( self->dmagrp->status );
  show_rx_process_state( self->dmagrp->status );

  /* Is there any abnormal interrupt? */
  if ( irq_status & DMAGRP_STATUS_AIS ) {
    DWMAC_CORE_PRINT_DBG( "CSR5[15] DMA ABNORMAL IRQ: " );

    if ( irq_status & DMAGRP_STATUS_UNF ) {
      DWMAC_CORE_PRINT_DBG( "transmit underflow\n" );
      events_transmit |= DWMAC_COMMON_EVENT_TX_BUMP_UP_DMA_THRESHOLD;
      irq_handled     |= DMAGRP_STATUS_UNF;
      irq_disable     |= DMAGRP_INTERRUPT_ENABLE_UNE;
      ++count->tx_underflow;
    }

    if ( irq_status & DMAGRP_STATUS_TJT ) {
      DWMAC_CORE_PRINT_DBG( "transmit jabber\n" );
      irq_handled |= DMAGRP_STATUS_TJT;
      ++count->tx_jabber;
    }

    if ( irq_status & DMAGRP_STATUS_OVF ) {
      DWMAC_CORE_PRINT_DBG( "recv overflow\n" );
      irq_handled |= DMAGRP_STATUS_OVF;
      ++count->rx_overflow;
    }

    if ( irq_status & DMAGRP_STATUS_TU ) {
      DWMAC_CORE_PRINT_DBG( "transmit buffer unavailable\n" );
      irq_handled |= DMAGRP_STATUS_TU;
      ++count->tx_buf_unav;
    }

    if ( irq_status & DMAGRP_STATUS_RU ) {
      DWMAC_CORE_PRINT_DBG( "receive buffer unavailable\n" );
      irq_handled |= DMAGRP_STATUS_RU;
      ++count->rx_buf_unav;
    }

    if ( irq_status & DMAGRP_STATUS_RPS ) {
      DWMAC_CORE_PRINT_DBG( "receive process stopped\n" );
      irq_handled |= DMAGRP_STATUS_RPS;
      ++count->rx_process_stopped;
    }

    if ( irq_status & DMAGRP_STATUS_RWT ) {
      DWMAC_CORE_PRINT_DBG( "receive watchdog\n" );
      irq_handled |= DMAGRP_STATUS_RWT;
      ++count->rx_watchdog;
    }

    if ( irq_status & DMAGRP_STATUS_ETI ) {
      DWMAC_CORE_PRINT_DBG( "transmit early interrupt\n" );
      irq_handled |= DMAGRP_STATUS_ETI;
      ++count->tx_early;
    }

    if ( irq_status & DMAGRP_STATUS_ERI ) {
      DWMAC_CORE_PRINT_DBG( "receive early interrupt\n" );
      irq_handled |= DMAGRP_STATUS_ERI;
      ++count->rx_early;
    }

    if ( irq_status & DMAGRP_STATUS_TPS ) {
      DWMAC_CORE_PRINT_DBG( "transmit process stopped\n" );
      events_transmit |= DWMAC_COMMON_EVENT_TASK_INIT;
      irq_handled     |= DMAGRP_STATUS_TPS;
      irq_disable     |= DMAGRP_INTERRUPT_ENABLE_TSE;
      ++count->tx_process_stopped;
    }

    if ( irq_status & DMAGRP_STATUS_FBI ) {
      DWMAC_CORE_PRINT_DBG( "fatal bus error\n" );
      events_transmit |= DWMAC_COMMON_EVENT_TASK_INIT;
      irq_handled     |= DMAGRP_STATUS_FBI;
      irq_disable     |= DMAGRP_INTERRUPT_ENABLE_FBE;
      ++count->fatal_bus_error;
    }

    irq_handled |= DMAGRP_STATUS_AIS;
  }

  /* Is there any normal interrupt? */
  if ( irq_status & DMAGRP_STATUS_NIS ) {
    /* Transmit interrupt */
    if ( irq_status & DMAGRP_STATUS_TI ) {
      events_transmit |= DWMAC_COMMON_EVENT_TX_FRAME_TRANSMITTED;
      irq_handled     |= DMAGRP_STATUS_TI;
      irq_disable     |= DMAGRP_INTERRUPT_ENABLE_TIE;
      ++count->transmit;
    }

    /* Receive interrupt */
    if ( irq_status & DMAGRP_STATUS_RI ) {
      events_receive |= DWMAC_COMMON_EVENT_RX_FRAME_RECEIVED;
      irq_handled    |= DMAGRP_STATUS_RI;
      irq_disable    |= DMAGRP_INTERRUPT_ENABLE_RIE;
      ++count->receive;
    }

    irq_handled |= DMAGRP_STATUS_NIS;
  }

  /* Optional hardware blocks, interrupts should be disabled */
  if ( irq_status
       & ( DMAGRP_STATUS_GMI | DMAGRP_STATUS_GLI ) ) {
    DWMAC_CORE_PRINT_DBG( "%s: unexpected status %08x\n", __func__,
                          irq_status );

    if ( irq_status & DMAGRP_STATUS_GMI ) {
      irq_handled |= DMAGRP_STATUS_GMI;
      ++count->unhandled;
    }

    if ( irq_status & DMAGRP_STATUS_GLI ) {
      irq_handled |= DMAGRP_STATUS_GLI;
      ++count->unhandled;
    }
  }

  /* Count remaining unhandled interrupts (there should not be any)  */
  if ( ( irq_status & 0x1FFCF ) != irq_handled ) {
    ++count->unhandled;
  }

  /* Disable interrupts which need further handling by tasks.
   * The tasks will re-enable them. */
  self->dmagrp->interrupt_enable &= ~irq_disable;

  /* Clear interrupts */
  self->dmagrp->status = irq_handled;

  /* Send events to receive task */
  if ( events_receive != 0 ) {
    (void) rtems_bsdnet_event_send( self->task_id_rx, events_receive );
  }

  /* Send events to transmit task */
  if ( events_transmit != 0 ) {
    (void) rtems_bsdnet_event_send( self->task_id_tx, events_transmit );
  }

  DWMAC_CORE_PRINT_DBG( "\n\n" );
}

void dwmac_core_dma_flush_tx_fifo( dwmac_common_context *self )
{
  self->dmagrp->operation_mode |= DMAGRP_OPERATION_MODE_FTF;

  do {
  } while ( ( self->dmagrp->operation_mode & DMAGRP_OPERATION_MODE_FTF ) != 0 );
}

void dwmac_core_set_mac_addr(
  const uint8_t      addr[6],
  volatile uint32_t *reg_high,
  volatile uint32_t *reg_low )
{
  uint32_t data = MAC_HIGH_ADDRHI( ( addr[5] << 8 ) | addr[4] );


  /* For MAC Addr registers se have to set the Address Enable (AE)
   * bit that has no effect on the High Reg 0 where the bit 31 (MO)
   * is RO.
   */
  data     |= MAC_HIGH_AE;
  *reg_high = data;

  data      =
    ( (uint32_t) addr[3] << 24 )
    | ( (uint32_t) addr[2] << 16 )
    | ( (uint32_t) addr[1] << 8 )
    | addr[0];
  *reg_low = data;
}

/* Enable disable MAC RX/TX */
void dwmac_core_set_mac(
  dwmac_common_context *self,
  const bool            enable )
{
  uint32_t value = self->macgrp->mac_configuration;


  if ( enable ) {
    value |= MACGRP_MAC_CONFIGURATION_RE | MACGRP_MAC_CONFIGURATION_TE;
  } else {
    value &= ~( MACGRP_MAC_CONFIGURATION_RE | MACGRP_MAC_CONFIGURATION_TE );
  }

  self->macgrp->mac_configuration = value;
}