]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/Zynq/x_emacpsif_dma.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / Zynq / x_emacpsif_dma.c
1 /*
2 FreeRTOS+TCP V2.0.11
3 Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of
6 this software and associated documentation files (the "Software"), to deal in
7 the Software without restriction, including without limitation the rights to
8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 the Software, and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22  http://aws.amazon.com/freertos
23  http://www.FreeRTOS.org
24 */
25
26 #include "Zynq/x_emacpsif.h"
27 #include "Zynq/x_topology.h"
28 #include "xstatus.h"
29
30 #include "xparameters.h"
31 #include "xparameters_ps.h"
32 #include "xil_exception.h"
33 #include "xil_mmu.h"
34
35 #include "FreeRTOS.h"
36 #include "task.h"
37 #include "timers.h"
38 #include "semphr.h"
39
40 /* FreeRTOS+TCP includes. */
41 #include "FreeRTOS_IP.h"
42 #include "FreeRTOS_Sockets.h"
43 #include "FreeRTOS_IP_Private.h"
44 #include "NetworkBufferManagement.h"
45
46 #include "uncached_memory.h"
47
48 /* Two defines used to set or clear the EMAC interrupt */
49 #define INTC_BASE_ADDR          XPAR_SCUGIC_CPU_BASEADDR
50 #define INTC_DIST_BASE_ADDR     XPAR_SCUGIC_DIST_BASEADDR
51
52
53
54 #if( ipconfigPACKET_FILLER_SIZE != 2 )
55         #error Please define ipconfigPACKET_FILLER_SIZE as the value '2'
56 #endif
57 #define TX_OFFSET                               ipconfigPACKET_FILLER_SIZE
58
59 #define RX_BUFFER_ALIGNMENT     14
60
61 /* Defined in NetworkInterface.c */
62 extern TaskHandle_t xEMACTaskHandle;
63
64 /*
65         pxDMA_tx_buffers: these are character arrays, each one is big enough to hold 1 MTU.
66         The actual TX buffers are located in uncached RAM.
67 */
68 static unsigned char *pxDMA_tx_buffers[ ipconfigNIC_N_TX_DESC ] = { NULL };
69
70 /*
71         pxDMA_rx_buffers: these are pointers to 'NetworkBufferDescriptor_t'.
72         Once a message has been received by the EMAC, the descriptor can be passed
73         immediately to the IP-task.
74 */
75 static NetworkBufferDescriptor_t *pxDMA_rx_buffers[ ipconfigNIC_N_RX_DESC ] = { NULL };
76
77 /*
78         The FreeRTOS+TCP port is using a fixed 'topology', which is declared in
79         ./portable/NetworkInterface/Zynq/NetworkInterface.c
80 */
81 extern struct xtopology_t xXTopology;
82
83 static SemaphoreHandle_t xTXDescriptorSemaphore = NULL;
84
85 /*
86         The FreeRTOS+TCP port does not make use of "src/xemacps_bdring.c".
87         In stead 'struct xemacpsif_s' has a "head" and a "tail" index.
88         "head" is the next index to be written, used.
89         "tail" is the next index to be read, freed.
90 */
91
92 int is_tx_space_available( xemacpsif_s *xemacpsif )
93 {
94 size_t uxCount;
95
96         if( xTXDescriptorSemaphore != NULL )
97         {
98                 uxCount = ( ( UBaseType_t ) ipconfigNIC_N_TX_DESC ) - uxSemaphoreGetCount( xTXDescriptorSemaphore );
99         }
100         else
101         {
102                 uxCount = ( UBaseType_t ) 0u;
103         }
104
105         return uxCount;
106 }
107
108 void emacps_check_tx( xemacpsif_s *xemacpsif )
109 {
110 int tail = xemacpsif->txTail;
111 int head = xemacpsif->txHead;
112 size_t uxCount = ( ( UBaseType_t ) ipconfigNIC_N_TX_DESC ) - uxSemaphoreGetCount( xTXDescriptorSemaphore );
113
114         /* uxCount is the number of TX descriptors that are in use by the DMA. */
115         /* When done, "TXBUF_USED" will be set. */
116
117         while( ( uxCount > 0 ) && ( ( xemacpsif->txSegments[ tail ].flags & XEMACPS_TXBUF_USED_MASK ) != 0 ) )
118         {
119                 if( ( tail == head ) && ( uxCount != ipconfigNIC_N_TX_DESC ) )
120                 {
121                         break;
122                 }
123 #if( ipconfigZERO_COPY_TX_DRIVER != 0 )
124 #warning ipconfigZERO_COPY_TX_DRIVER is defined
125                 {
126                 void *pvBuffer = pxDMA_tx_buffers[ tail ];
127                 NetworkBufferDescriptor_t *pxBuffer;
128
129                         if( pvBuffer != NULL )
130                         {
131                                 pxDMA_tx_buffers[ tail ] = NULL;
132                                 pxBuffer = pxPacketBuffer_to_NetworkBuffer( pvBuffer );
133                                 if( pxBuffer != NULL )
134                                 {
135                                         vReleaseNetworkBufferAndDescriptor( pxBuffer );
136                                 }
137                                 else
138                                 {
139                                         FreeRTOS_printf( ( "emacps_check_tx: Can not find network buffer\n" ) );
140                                 }
141                         }
142                 }
143 #endif
144                 /* Clear all but the "used" and "wrap" bits. */
145                 if( tail < ipconfigNIC_N_TX_DESC - 1 )
146                 {
147                         xemacpsif->txSegments[ tail ].flags = XEMACPS_TXBUF_USED_MASK;
148                 }
149                 else
150                 {
151                         xemacpsif->txSegments[ tail ].flags = XEMACPS_TXBUF_USED_MASK | XEMACPS_TXBUF_WRAP_MASK;
152                 }
153                 uxCount--;
154                 /* Tell the counting semaphore that one more TX descriptor is available. */
155                 xSemaphoreGive( xTXDescriptorSemaphore );
156                 if( ++tail == ipconfigNIC_N_TX_DESC )
157                 {
158                         tail = 0;
159                 }
160                 xemacpsif->txTail = tail;
161         }
162
163         return;
164 }
165
166 void emacps_send_handler(void *arg)
167 {
168 xemacpsif_s   *xemacpsif;
169 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
170
171         xemacpsif = (xemacpsif_s *)(arg);
172
173         /* In this port for FreeRTOS+TCP, the EMAC interrupts will only set a bit in
174         "isr_events". The task in NetworkInterface will wake-up and do the necessary work.
175         */
176         xemacpsif->isr_events |= EMAC_IF_TX_EVENT;
177         xemacpsif->txBusy = pdFALSE;
178
179         if( xEMACTaskHandle != NULL )
180         {
181                 vTaskNotifyGiveFromISR( xEMACTaskHandle, &xHigherPriorityTaskWoken );
182         }
183
184         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
185 }
186
187 static BaseType_t xValidLength( BaseType_t xLength )
188 {
189 BaseType_t xReturn;
190
191         if( ( xLength >= ( BaseType_t ) sizeof( struct xARP_PACKET ) ) && ( ( ( uint32_t ) xLength ) <= ipTOTAL_ETHERNET_FRAME_SIZE ) )
192         {
193                 xReturn = pdTRUE;
194         }
195         else
196         {
197                 xReturn =  pdFALSE;
198         }
199
200         return xReturn;
201 }
202
203 XStatus emacps_send_message(xemacpsif_s *xemacpsif, NetworkBufferDescriptor_t *pxBuffer, int iReleaseAfterSend )
204 {
205 int head = xemacpsif->txHead;
206 int iHasSent = 0;
207 uint32_t ulBaseAddress = xemacpsif->emacps.Config.BaseAddress;
208 TickType_t xBlockTimeTicks = pdMS_TO_TICKS( 5000u );
209
210         #if( ipconfigZERO_COPY_TX_DRIVER != 0 )
211         {
212                 /* This driver wants to own all network buffers which are to be transmitted. */
213                 configASSERT( iReleaseAfterSend != pdFALSE );
214         }
215         #endif
216
217         /* Open a do {} while ( 0 ) loop to be able to call break. */
218         do
219         {
220         uint32_t ulFlags = 0;
221
222                 if( xValidLength( pxBuffer->xDataLength ) != pdTRUE )
223                 {
224                         break;
225                 }
226
227                 if( xTXDescriptorSemaphore == NULL )
228                 {
229                         break;
230                 }
231
232                 if( xSemaphoreTake( xTXDescriptorSemaphore, xBlockTimeTicks ) != pdPASS )
233                 {
234                         FreeRTOS_printf( ( "emacps_send_message: Time-out waiting for TX buffer\n" ) );
235                         break;
236                 }
237
238 #if( ipconfigZERO_COPY_TX_DRIVER != 0 )
239                 /* Pass the pointer (and its ownership) directly to DMA. */
240                 pxDMA_tx_buffers[ head ] = pxBuffer->pucEthernetBuffer;
241                 if( ucIsCachedMemory( pxBuffer->pucEthernetBuffer ) != 0 )
242                 {
243                         Xil_DCacheFlushRange( ( unsigned )pxBuffer->pucEthernetBuffer, pxBuffer->xDataLength );
244                 }
245                 /* Buffer has been transferred, do not release it. */
246                 iReleaseAfterSend = pdFALSE;
247 #else
248                 if( pxDMA_tx_buffers[ head ] == NULL )
249                 {
250                         FreeRTOS_printf( ( "emacps_send_message: pxDMA_tx_buffers[ %d ] == NULL\n", head ) );
251                         break;
252                 }
253                 /* Copy the message to unbuffered space in RAM. */
254                 memcpy( pxDMA_tx_buffers[ head ], pxBuffer->pucEthernetBuffer, pxBuffer->xDataLength );
255 #endif
256                 /* Packets will be sent one-by-one, so for each packet
257                 the TXBUF_LAST bit will be set. */
258                 ulFlags |= XEMACPS_TXBUF_LAST_MASK;
259                 ulFlags |= ( pxBuffer->xDataLength & XEMACPS_TXBUF_LEN_MASK );
260                 if( head == ( ipconfigNIC_N_TX_DESC - 1 ) )
261                 {
262                         ulFlags |= XEMACPS_TXBUF_WRAP_MASK;
263                 }
264
265                 /* Copy the address of the buffer and set the flags. */
266                 xemacpsif->txSegments[ head ].address = ( uint32_t )pxDMA_tx_buffers[ head ];
267                 xemacpsif->txSegments[ head ].flags = ulFlags;
268
269                 iHasSent = pdTRUE;
270                 if( ++head == ipconfigNIC_N_TX_DESC )
271                 {
272                         head = 0;
273                 }
274                 /* Update the TX-head index. These variable are declared volatile so they will be
275                 accessed as little as possible. */
276                 xemacpsif->txHead = head;
277         } while( pdFALSE );
278
279         if( iReleaseAfterSend != pdFALSE )
280         {
281                 vReleaseNetworkBufferAndDescriptor( pxBuffer );
282                 pxBuffer = NULL;
283         }
284
285         /* Data Synchronization Barrier */
286         dsb();
287
288         if( iHasSent != pdFALSE )
289         {
290                 /* Make STARTTX high */
291                 uint32_t ulValue = XEmacPs_ReadReg( ulBaseAddress, XEMACPS_NWCTRL_OFFSET);
292                 /* Start transmit */
293                 xemacpsif->txBusy = pdTRUE;
294                 XEmacPs_WriteReg( ulBaseAddress, XEMACPS_NWCTRL_OFFSET, ( ulValue | XEMACPS_NWCTRL_STARTTX_MASK ) );
295         }
296         dsb();
297
298         return 0;
299 }
300
301 void emacps_recv_handler(void *arg)
302 {
303         xemacpsif_s *xemacpsif;
304         BaseType_t xHigherPriorityTaskWoken = pdFALSE;
305
306         xemacpsif = (xemacpsif_s *)(arg);
307         xemacpsif->isr_events |= EMAC_IF_RX_EVENT;
308
309         if( xEMACTaskHandle != NULL )
310         {
311                 vTaskNotifyGiveFromISR( xEMACTaskHandle, &xHigherPriorityTaskWoken );
312         }
313
314         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
315 }
316
317 static NetworkBufferDescriptor_t *ethMsg = NULL;
318 static NetworkBufferDescriptor_t *ethLast = NULL;
319
320 static void passEthMessages( void )
321 {
322 IPStackEvent_t xRxEvent;
323
324         xRxEvent.eEventType = eNetworkRxEvent;
325         xRxEvent.pvData = ( void * ) ethMsg;
326
327         if( xSendEventStructToIPTask( &xRxEvent, ( TickType_t ) 1000 ) != pdPASS )
328         {
329                 /* The buffer could not be sent to the stack so must be released again.
330                 This is a deferred handler taskr, not a real interrupt, so it is ok to
331                 use the task level function here. */
332                 do
333                 {
334                         NetworkBufferDescriptor_t *xNext = ethMsg->pxNextBuffer;
335                         vReleaseNetworkBufferAndDescriptor( ethMsg );
336                         ethMsg = xNext;
337                 } while( ethMsg != NULL );
338
339                 iptraceETHERNET_RX_EVENT_LOST();
340                 FreeRTOS_printf( ( "passEthMessages: Can not queue return packet!\n" ) );
341         }
342
343         ethMsg = ethLast = NULL;
344 }
345
346 int emacps_check_rx( xemacpsif_s *xemacpsif )
347 {
348 NetworkBufferDescriptor_t *pxBuffer, *pxNewBuffer;
349 int rx_bytes;
350 volatile int msgCount = 0;
351 int head = xemacpsif->rxHead;
352
353         /* There seems to be an issue (SI# 692601), see comments below. */
354         resetrx_on_no_rxdata(xemacpsif);
355
356         /* This FreeRTOS+TCP driver shall be compiled with the option
357         "ipconfigUSE_LINKED_RX_MESSAGES" enabled.  It allows the driver to send a
358         chain of RX messages within one message to the IP-task. */
359         for( ;; )
360         {
361                 if( ( ( xemacpsif->rxSegments[ head ].address & XEMACPS_RXBUF_NEW_MASK ) == 0 ) ||
362                         ( pxDMA_rx_buffers[ head ] == NULL ) )
363                 {
364                         break;
365                 }
366
367                 pxNewBuffer = pxGetNetworkBufferWithDescriptor( ipTOTAL_ETHERNET_FRAME_SIZE + RX_BUFFER_ALIGNMENT, ( TickType_t ) 0 );
368                 if( pxNewBuffer == NULL )
369                 {
370                         /* A packet has been received, but there is no replacement for this Network Buffer.
371                         The packet will be dropped, and it Network Buffer will stay in place. */
372                         FreeRTOS_printf( ("emacps_check_rx: unable to allocate a Netwrok Buffer\n" ) );
373                         pxNewBuffer = ( NetworkBufferDescriptor_t * )pxDMA_rx_buffers[ head ];
374                 }
375                 else
376                 {
377                         pxBuffer = ( NetworkBufferDescriptor_t * )pxDMA_rx_buffers[ head ];
378
379                         /* Just avoiding to use or refer to the same buffer again */
380                         pxDMA_rx_buffers[ head ] = pxNewBuffer;
381
382                         /*
383                          * Adjust the buffer size to the actual number of bytes received.
384                          */
385                         rx_bytes = xemacpsif->rxSegments[ head ].flags & XEMACPS_RXBUF_LEN_MASK;
386
387                         pxBuffer->xDataLength = rx_bytes;
388
389                         if( ucIsCachedMemory( pxBuffer->pucEthernetBuffer ) != 0 )
390                         {
391                                 Xil_DCacheInvalidateRange( ( ( uint32_t )pxBuffer->pucEthernetBuffer ) - ipconfigPACKET_FILLER_SIZE, (unsigned)rx_bytes );
392                         }
393
394                         /* store it in the receive queue, where it'll be processed by a
395                         different handler. */
396                         iptraceNETWORK_INTERFACE_RECEIVE();
397                         pxBuffer->pxNextBuffer = NULL;
398
399                         if( ethMsg == NULL )
400                         {
401                                 // Becomes the first message
402                                 ethMsg = pxBuffer;
403                         }
404                         else if( ethLast != NULL )
405                         {
406                                 // Add to the tail
407                                 ethLast->pxNextBuffer = pxBuffer;
408                         }
409
410                         ethLast = pxBuffer;
411                         msgCount++;
412                 }
413                 {
414                         if( ucIsCachedMemory( pxNewBuffer->pucEthernetBuffer ) != 0 )
415                         {
416                                 Xil_DCacheInvalidateRange( ( ( uint32_t )pxNewBuffer->pucEthernetBuffer ) - ipconfigPACKET_FILLER_SIZE, (unsigned)ipTOTAL_ETHERNET_FRAME_SIZE + RX_BUFFER_ALIGNMENT);
417                         }
418                         {
419                                 uint32_t addr = ( ( uint32_t )pxNewBuffer->pucEthernetBuffer ) & XEMACPS_RXBUF_ADD_MASK;
420                                 if( head == ( ipconfigNIC_N_RX_DESC - 1 ) )
421                                 {
422                                         addr |= XEMACPS_RXBUF_WRAP_MASK;
423                                 }
424                                 /* Clearing 'XEMACPS_RXBUF_NEW_MASK'       0x00000001 *< Used bit.. */
425                                 xemacpsif->rxSegments[ head ].address = addr;
426                                 xemacpsif->rxSegments[ head ].flags = 0;
427                         }
428                 }
429
430                 if( ++head == ipconfigNIC_N_RX_DESC )
431                 {
432                         head = 0;
433                 }
434                 xemacpsif->rxHead = head;
435         }
436
437         if( ethMsg != NULL )
438         {
439                 passEthMessages( );
440         }
441
442         return msgCount;
443 }
444
445 void clean_dma_txdescs(xemacpsif_s *xemacpsif)
446 {
447 int index;
448 unsigned char *ucTxBuffer;
449
450         /* Clear all TX descriptors and assign uncached memory to each descriptor.
451         "tx_space" points to the first available TX buffer. */
452         ucTxBuffer = xemacpsif->tx_space;
453
454         for( index = 0; index < ipconfigNIC_N_TX_DESC; index++ )
455         {
456                 xemacpsif->txSegments[ index ].address = ( uint32_t )ucTxBuffer;
457                 xemacpsif->txSegments[ index ].flags = XEMACPS_TXBUF_USED_MASK;
458 #if( ipconfigZERO_COPY_TX_DRIVER != 0 )
459                 pxDMA_tx_buffers[ index ] = ( void* )NULL;
460 #else
461                 pxDMA_tx_buffers[ index ] = ( void* )( ucTxBuffer + TX_OFFSET );
462 #endif
463                 ucTxBuffer += xemacpsif->uTxUnitSize;
464         }
465         xemacpsif->txSegments[ ipconfigNIC_N_TX_DESC - 1 ].flags =
466                 XEMACPS_TXBUF_USED_MASK | XEMACPS_TXBUF_WRAP_MASK;
467 }
468
469 XStatus init_dma(xemacpsif_s *xemacpsif)
470 {
471         NetworkBufferDescriptor_t *pxBuffer;
472
473         int iIndex;
474         UBaseType_t xRxSize;
475         UBaseType_t xTxSize;
476         struct xtopology_t *xtopologyp = &xXTopology;
477
478         xRxSize = ipconfigNIC_N_RX_DESC * sizeof( xemacpsif->rxSegments[ 0 ] );
479
480         xTxSize = ipconfigNIC_N_TX_DESC * sizeof( xemacpsif->txSegments[ 0 ] );
481
482         /* Also round-up to 4KB */
483         xemacpsif->uTxUnitSize = ( ipTOTAL_ETHERNET_FRAME_SIZE + 0x1000ul ) & ~0xffful;
484         /*
485          * We allocate 65536 bytes for RX BDs which can accommodate a
486          * maximum of 8192 BDs which is much more than any application
487          * will ever need.
488          */
489         xemacpsif->rxSegments = ( struct xBD_TYPE * )( pucGetUncachedMemory ( xRxSize )  );
490         xemacpsif->txSegments = ( struct xBD_TYPE * )( pucGetUncachedMemory ( xTxSize ) );
491         xemacpsif->tx_space   = ( unsigned char *   )( pucGetUncachedMemory ( ipconfigNIC_N_TX_DESC * xemacpsif->uTxUnitSize ) );
492
493         /* These variables will be used in XEmacPs_Start (see src/xemacps.c). */
494         xemacpsif->emacps.RxBdRing.BaseBdAddr = ( uint32_t ) xemacpsif->rxSegments;
495         xemacpsif->emacps.TxBdRing.BaseBdAddr = ( uint32_t ) xemacpsif->txSegments;
496
497         if( xTXDescriptorSemaphore == NULL )
498         {
499                 xTXDescriptorSemaphore = xSemaphoreCreateCounting( ( UBaseType_t ) ipconfigNIC_N_TX_DESC, ( UBaseType_t ) ipconfigNIC_N_TX_DESC );
500                 configASSERT( xTXDescriptorSemaphore );
501         }
502         /*
503          * Allocate RX descriptors, 1 RxBD at a time.
504          */
505         for( iIndex = 0; iIndex < ipconfigNIC_N_RX_DESC; iIndex++ )
506         {
507                 pxBuffer = pxDMA_rx_buffers[ iIndex ];
508                 if( pxBuffer == NULL )
509                 {
510                         pxBuffer = pxGetNetworkBufferWithDescriptor( ipTOTAL_ETHERNET_FRAME_SIZE + RX_BUFFER_ALIGNMENT, ( TickType_t ) 0 );
511                         if( pxBuffer == NULL )
512                         {
513                                 FreeRTOS_printf( ("Unable to allocate a network buffer in recv_handler\n" ) );
514                                 return -1;
515                         }
516                 }
517
518                 xemacpsif->rxSegments[ iIndex ].flags = 0;
519                 xemacpsif->rxSegments[ iIndex ].address = ( ( uint32_t )pxBuffer->pucEthernetBuffer ) & XEMACPS_RXBUF_ADD_MASK;
520
521                 pxDMA_rx_buffers[ iIndex ] = pxBuffer;
522                 /* Make sure this memory is not in cache for now. */
523                 if( ucIsCachedMemory( pxBuffer->pucEthernetBuffer ) != 0 )
524                 {
525                         Xil_DCacheInvalidateRange( ( ( uint32_t )pxBuffer->pucEthernetBuffer ) - ipconfigPACKET_FILLER_SIZE,
526                                 (unsigned)ipTOTAL_ETHERNET_FRAME_SIZE + RX_BUFFER_ALIGNMENT);
527                 }
528         }
529
530         xemacpsif->rxSegments[ ipconfigNIC_N_RX_DESC - 1 ].address |= XEMACPS_RXBUF_WRAP_MASK;
531
532         memset( xemacpsif->tx_space, '\0', ipconfigNIC_N_TX_DESC * xemacpsif->uTxUnitSize );
533
534         clean_dma_txdescs( xemacpsif );
535
536         {
537                 uint32_t value;
538                 value = XEmacPs_ReadReg( xemacpsif->emacps.Config.BaseAddress, XEMACPS_DMACR_OFFSET );
539
540                 // 1xxxx: Attempt to use INCR16 AHB bursts
541                 value = ( value & ~( XEMACPS_DMACR_BLENGTH_MASK ) ) | XEMACPS_DMACR_INCR16_AHB_BURST;
542 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM != 0 )
543                 value |= XEMACPS_DMACR_TCPCKSUM_MASK;
544 #else
545 #warning Are you sure the EMAC should not calculate outgoing checksums?
546                 value &= ~XEMACPS_DMACR_TCPCKSUM_MASK;
547 #endif
548                 XEmacPs_WriteReg( xemacpsif->emacps.Config.BaseAddress, XEMACPS_DMACR_OFFSET, value );
549         }
550         {
551                 uint32_t value;
552                 value = XEmacPs_ReadReg( xemacpsif->emacps.Config.BaseAddress, XEMACPS_NWCFG_OFFSET );
553
554                 /* Network buffers are 32-bit aligned + 2 bytes (because ipconfigPACKET_FILLER_SIZE = 2 ).
555                 Now tell the EMAC that received messages should be stored at "address + 2". */
556                 value = ( value & ~XEMACPS_NWCFG_RXOFFS_MASK ) | 0x8000;
557
558 #if( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM != 0 )
559                 value |= XEMACPS_NWCFG_RXCHKSUMEN_MASK;
560 #else
561 #warning Are you sure the EMAC should not calculate incoming checksums?
562                 value &= ~XEMACPS_NWCFG_RXCHKSUMEN_MASK;
563 #endif
564                 XEmacPs_WriteReg( xemacpsif->emacps.Config.BaseAddress, XEMACPS_NWCFG_OFFSET, value );
565         }
566
567         /*
568          * Connect the device driver handler that will be called when an
569          * interrupt for the device occurs, the handler defined above performs
570          * the specific interrupt processing for the device.
571          */
572         XScuGic_RegisterHandler(INTC_BASE_ADDR, xtopologyp->scugic_emac_intr,
573                 (Xil_ExceptionHandler)XEmacPs_IntrHandler,
574                 (void *)&xemacpsif->emacps);
575         /*
576          * Enable the interrupt for emacps.
577          */
578         EmacEnableIntr( );
579
580         return 0;
581 }
582
583 /*
584  * resetrx_on_no_rxdata():
585  *
586  * It is called at regular intervals through the API xemacpsif_resetrx_on_no_rxdata
587  * called by the user.
588  * The EmacPs has a HW bug (SI# 692601) on the Rx path for heavy Rx traffic.
589  * Under heavy Rx traffic because of the HW bug there are times when the Rx path
590  * becomes unresponsive. The workaround for it is to check for the Rx path for
591  * traffic (by reading the stats registers regularly). If the stats register
592  * does not increment for sometime (proving no Rx traffic), the function resets
593  * the Rx data path.
594  *
595  */
596
597 void resetrx_on_no_rxdata(xemacpsif_s *xemacpsif)
598 {
599         unsigned long regctrl;
600         unsigned long tempcntr;
601
602         tempcntr = XEmacPs_ReadReg( xemacpsif->emacps.Config.BaseAddress, XEMACPS_RXCNT_OFFSET );
603         if ( ( tempcntr == 0 ) && ( xemacpsif->last_rx_frms_cntr == 0 ) )
604         {
605                 regctrl = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
606                                 XEMACPS_NWCTRL_OFFSET);
607                 regctrl &= (~XEMACPS_NWCTRL_RXEN_MASK);
608                 XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
609                                 XEMACPS_NWCTRL_OFFSET, regctrl);
610                 regctrl = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress, XEMACPS_NWCTRL_OFFSET);
611                 regctrl |= (XEMACPS_NWCTRL_RXEN_MASK);
612                 XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress, XEMACPS_NWCTRL_OFFSET, regctrl);
613         }
614         xemacpsif->last_rx_frms_cntr = tempcntr;
615 }
616
617 void EmacDisableIntr(void)
618 {
619         XScuGic_DisableIntr(INTC_DIST_BASE_ADDR, xXTopology.scugic_emac_intr);
620 }
621
622 void EmacEnableIntr(void)
623 {
624         XScuGic_EnableIntr(INTC_DIST_BASE_ADDR, xXTopology.scugic_emac_intr);
625 }
626