sc589_eth1_driver.c
Go to the documentation of this file.
1 /**
2  * @file sc589_eth1_driver.c
3  * @brief ADSP-SC589 Ethernet MAC driver (EMAC0 instance)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include <sys/platform.h>
36 #include <services/int/adi_int.h>
37 #include "core/net.h"
39 #include "debug.h"
40 
41 //Underlying network interface
42 static NetInterface *nicDriverInterface;
43 
44 //IAR EWARM compiler?
45 #if defined(__ICCARM__)
46 
47 //Transmit buffer
48 #pragma data_alignment = 4
49 #pragma location = SC589_ETH1_RAM_SECTION
51 //Receive buffer
52 #pragma data_alignment = 4
53 #pragma location = SC589_ETH1_RAM_SECTION
55 //Transmit DMA descriptors
56 #pragma data_alignment = 8
57 #pragma location = SC589_ETH1_RAM_SECTION
59 //Receive DMA descriptors
60 #pragma data_alignment = 8
61 #pragma location = SC589_ETH1_RAM_SECTION
63 
64 //GCC compiler?
65 #else
66 
67 //Transmit buffer
69  __attribute__((aligned(4), __section__(SC589_ETH1_RAM_SECTION)));
70 //Receive buffer
72  __attribute__((aligned(4), __section__(SC589_ETH1_RAM_SECTION)));
73 //Transmit DMA descriptors
75  __attribute__((aligned(8), __section__(SC589_ETH1_RAM_SECTION)));
76 //Receive DMA descriptors
78  __attribute__((aligned(8), __section__(SC589_ETH1_RAM_SECTION)));
79 
80 #endif
81 
82 //Pointer to the current TX DMA descriptor
83 static Sc589Eth1TxDmaDesc *txCurDmaDesc;
84 //Pointer to the current RX DMA descriptor
85 static Sc589Eth1RxDmaDesc *rxCurDmaDesc;
86 
87 
88 /**
89  * @brief ADSP-SC589 Ethernet MAC driver (EMAC0 instance)
90  **/
91 
93 {
95  ETH_MTU,
106  TRUE,
107  TRUE,
108  TRUE,
109  FALSE
110 };
111 
112 
113 /**
114  * @brief ADSP-SC589 Ethernet MAC initialization
115  * @param[in] interface Underlying network interface
116  * @return Error code
117  **/
118 
120 {
121  error_t error;
122 
123  //Debug message
124  TRACE_INFO("Initializing ADSP-SC589 Ethernet MAC (EMAC0)...\r\n");
125 
126  //Save underlying network interface
127  nicDriverInterface = interface;
128 
129  //GPIO configuration
130  sc589Eth1InitGpio(interface);
131 
132  //Perform a software reset
133  *pREG_EMAC0_DMA0_BUSMODE |= BITM_EMAC_DMA0_BUSMODE_SWR;
134  //Wait for the reset to complete
135  while((*pREG_EMAC0_DMA0_BUSMODE & BITM_EMAC_DMA0_BUSMODE_SWR) != 0)
136  {
137  }
138 
139  //Adjust MDC clock range depending on CLKO7 frequency
140  *pREG_EMAC0_SMI_ADDR = ENUM_EMAC_SMI_ADDR_CR_DIV62;
141 
142  //Valid Ethernet PHY or switch driver?
143  if(interface->phyDriver != NULL)
144  {
145  //Ethernet PHY initialization
146  error = interface->phyDriver->init(interface);
147  }
148  else if(interface->switchDriver != NULL)
149  {
150  //Ethernet switch initialization
151  error = interface->switchDriver->init(interface);
152  }
153  else
154  {
155  //The interface is not properly configured
156  error = ERROR_FAILURE;
157  }
158 
159  //Any error to report?
160  if(error)
161  {
162  return error;
163  }
164 
165  //Use default MAC configuration
166  *pREG_EMAC0_MACCFG = BITM_EMAC_MACCFG_PS | BITM_EMAC_MACCFG_DO;
167 
168  //Configure MAC address filtering
169  sc589Eth1UpdateMacAddrFilter(interface);
170 
171  //Disable flow control
172  *pREG_EMAC0_FLOWCTL = 0;
173 
174  //Enable store and forward mode
175  *pREG_EMAC0_DMA0_OPMODE = BITM_EMAC_DMA0_OPMODE_RSF |
176  BITM_EMAC_DMA0_OPMODE_TSF;
177 
178  //Configure DMA bus mode
179  *pREG_EMAC0_DMA0_BUSMODE = BITM_EMAC_DMA0_BUSMODE_AAL |
180  BITM_EMAC_DMA0_BUSMODE_USP | ENUM_EMAC_DMA_BUSMODE_RPBL_32 |
181  ENUM_EMAC_DMA_BUSMODE_PBL_32 | BITM_EMAC_DMA0_BUSMODE_ATDS;
182 
183  //Initialize DMA descriptor lists
184  sc589Eth1InitDmaDesc(interface);
185 
186  //Prevent interrupts from being generated when statistic counters reach
187  //half their maximum value
188  *pREG_EMAC0_MMC_TXIMSK = 0x01FFFFFF;
189  *pREG_EMAC0_MMC_RXIMSK = 0x01FFFFFF;
190  *pREG_EMAC0_IPC_RXIMSK = 0x3FFFFFFF;
191 
192  //Disable MAC interrupts
193  *pREG_EMAC0_IMSK = BITM_EMAC_IMSK_LPIIM | BITM_EMAC_IMSK_TS;
194 
195  //Enable the desired DMA interrupts
196  *pREG_EMAC0_DMA0_IEN = BITM_EMAC_DMA0_IEN_NIE | BITM_EMAC_DMA0_IEN_RIE |
197  BITM_EMAC_DMA0_IEN_TIE;
198 
199  //Register interrupt handler
200  adi_int_InstallHandler(INTR_EMAC0_STAT, sc589Eth1IrqHandler, interface,
201  false);
202 
203  //Enable MAC transmission and reception
204  *pREG_EMAC0_MACCFG |= BITM_EMAC_MACCFG_TE | BITM_EMAC_MACCFG_RE;
205  //Enable DMA transmission and reception
206  *pREG_EMAC0_DMA0_OPMODE |= BITM_EMAC_DMA0_OPMODE_ST | BITM_EMAC_DMA0_OPMODE_SR;
207 
208  //Accept any packets from the upper layer
209  osSetEvent(&interface->nicTxEvent);
210 
211  //Successful initialization
212  return NO_ERROR;
213 }
214 
215 
216 /**
217  * @brief GPIO configuration
218  * @param[in] interface Underlying network interface
219  **/
220 
221 __weak_func void sc589Eth1InitGpio(NetInterface *interface)
222 {
223 //ADZS-SC589-EZLITE evaluation board?
224 #if defined(USE_ADZS_SC589_EZLITE)
225  uint32_t temp;
226 
227  //Configure PA_00 (ETH0_TXD0), PA_01 (ETH0_TXD1), PA_02 (ETH0_MDC),
228  //PA_03 (ETH0_MDIO), PA_04 (ETH0_RXD0), PA_05 (ETH0_RXD1),
229  //PA_06 (ETH0_RXCLK_REFCLK), PA_07 (ETH0_CRS), PA_08 (ETH0_RXD2),
230  //PA_09 (ETH0_RXD3), PA_10 (ETH0_TXEN), PA_11 (ETH0_TXCLK),
231  //PA_12 (ETH0_TXD2) and PA_13 (ETH0_TXD3)
232  temp = *pREG_PORTA_MUX;
233  temp = (temp & ~BITM_PORT_MUX_MUX0) | (0 << BITP_PORT_MUX_MUX0);
234  temp = (temp & ~BITM_PORT_MUX_MUX1) | (0 << BITP_PORT_MUX_MUX1);
235  temp = (temp & ~BITM_PORT_MUX_MUX2) | (0 << BITP_PORT_MUX_MUX2);
236  temp = (temp & ~BITM_PORT_MUX_MUX3) | (0 << BITP_PORT_MUX_MUX3);
237  temp = (temp & ~BITM_PORT_MUX_MUX4) | (0 << BITP_PORT_MUX_MUX4);
238  temp = (temp & ~BITM_PORT_MUX_MUX5) | (0 << BITP_PORT_MUX_MUX5);
239  temp = (temp & ~BITM_PORT_MUX_MUX6) | (0 << BITP_PORT_MUX_MUX6);
240  temp = (temp & ~BITM_PORT_MUX_MUX7) | (0 << BITP_PORT_MUX_MUX7);
241  temp = (temp & ~BITM_PORT_MUX_MUX8) | (0 << BITP_PORT_MUX_MUX8);
242  temp = (temp & ~BITM_PORT_MUX_MUX9) | (0 << BITP_PORT_MUX_MUX9);
243  temp = (temp & ~BITM_PORT_MUX_MUX10) | (0 << BITP_PORT_MUX_MUX10);
244  temp = (temp & ~BITM_PORT_MUX_MUX11) | (0 << BITP_PORT_MUX_MUX11);
245  temp = (temp & ~BITM_PORT_MUX_MUX12) | (0 << BITP_PORT_MUX_MUX12);
246  temp = (temp & ~BITM_PORT_MUX_MUX13) | (0 << BITP_PORT_MUX_MUX13);
247  *pREG_PORTA_MUX = temp;
248 
249  //Select peripheral mode
250  *pREG_PORTA_FER_SET = BITM_PORT_FER_PX0 | BITM_PORT_FER_PX1 |
251  BITM_PORT_FER_PX2 | BITM_PORT_FER_PX3 | BITM_PORT_FER_PX4 |
252  BITM_PORT_FER_PX5 | BITM_PORT_FER_PX6 | BITM_PORT_FER_PX7 |
253  BITM_PORT_FER_PX8 | BITM_PORT_FER_PX9 | BITM_PORT_FER_PX10 |
254  BITM_PORT_FER_PX11 | BITM_PORT_FER_PX12 | BITM_PORT_FER_PX13;
255 
256  //Configure ETH0_MD_INT (PC_15) as an input
257  *pREG_PORTC_FER_CLR = BITM_PORT_FER_PX15;
258  *pREG_PORTC_DIR_CLR = BITM_PORT_DIR_PX15;
259  *pREG_PORTC_INEN_SET = BITM_PORT_INEN_PX15;
260 
261  //Configure ETH0_RESET (PB_14) as an output
262  *pREG_PORTB_FER_CLR = BITM_PORT_FER_PX14;
263  *pREG_PORTB_DIR_SET = BITM_PORT_DIR_PX14;
264 
265  //Reset PHY transceiver (hard reset)
266  *pREG_PORTB_DATA_CLR = BITM_PORT_DATA_PX14;
267  sleep(10);
268  *pREG_PORTB_DATA_SET = BITM_PORT_DATA_PX14;
269  sleep(10);
270 
271  //Select RGMII interface mode
272  *pREG_PADS0_PCFG0 |= BITM_PADS_PCFG0_EMACPHYISEL;
273  //Reset PHY interface
274  *pREG_PADS0_PCFG0 |= BITM_PADS_PCFG0_EMACRESET;
275 #endif
276 }
277 
278 
279 /**
280  * @brief Initialize DMA descriptor lists
281  * @param[in] interface Underlying network interface
282  **/
283 
285 {
286  uint_t i;
287 
288  //Initialize TX DMA descriptor list
289  for(i = 0; i < SC589_ETH1_TX_BUFFER_COUNT; i++)
290  {
291  //Use chain structure rather than ring structure
293  //Initialize transmit buffer size
294  txDmaDesc[i].tdes1 = 0;
295 
296  //Transmit buffer address
297  txDmaDesc[i].tdes2 = adi_rtl_internal_to_system_addr(
298  (uint32_t) txBuffer[i], 1);
299 
300  //Next descriptor address
301  txDmaDesc[i].tdes3 = adi_rtl_internal_to_system_addr(
302  (uint32_t) &txDmaDesc[i + 1], 1);
303 
304  //Reserved fields
305  txDmaDesc[i].tdes4 = 0;
306  txDmaDesc[i].tdes5 = 0;
307  //Transmit frame time stamp
308  txDmaDesc[i].tdes6 = 0;
309  txDmaDesc[i].tdes7 = 0;
310  }
311 
312  //The last descriptor is chained to the first entry
313  txDmaDesc[i - 1].tdes3 = (uint32_t) &txDmaDesc[0];
314  //Point to the very first descriptor
315  txCurDmaDesc = &txDmaDesc[0];
316 
317  //Initialize RX DMA descriptor list
318  for(i = 0; i < SC589_ETH1_RX_BUFFER_COUNT; i++)
319  {
320  //The descriptor is initially owned by the DMA
321  rxDmaDesc[i].rdes0 = EMAC_RDES0_OWN;
322  //Use chain structure rather than ring structure
324 
325  //Receive buffer address
326  rxDmaDesc[i].rdes2 = adi_rtl_internal_to_system_addr(
327  (uint32_t) rxBuffer[i], 1);
328 
329  //Next descriptor address
330  rxDmaDesc[i].rdes3 = adi_rtl_internal_to_system_addr(
331  (uint32_t) &rxDmaDesc[i + 1], 1);
332 
333  //Extended status
334  rxDmaDesc[i].rdes4 = 0;
335  //Reserved field
336  rxDmaDesc[i].rdes5 = 0;
337  //Receive frame time stamp
338  rxDmaDesc[i].rdes6 = 0;
339  rxDmaDesc[i].rdes7 = 0;
340  }
341 
342  //The last descriptor is chained to the first entry
343  rxDmaDesc[i - 1].rdes3 = adi_rtl_internal_to_system_addr(
344  (uint32_t) &rxDmaDesc[0], 1);
345 
346  //Point to the very first descriptor
347  rxCurDmaDesc = &rxDmaDesc[0];
348 
349  //Start location of the TX descriptor list
350  *pREG_EMAC0_DMA0_TXDSC_ADDR = adi_rtl_internal_to_system_addr(
351  (uint32_t) txDmaDesc, 1);
352 
353  //Start location of the RX descriptor list
354  *pREG_EMAC0_DMA0_RXDSC_ADDR = adi_rtl_internal_to_system_addr(
355  (uint32_t) rxDmaDesc, 1);
356 }
357 
358 
359 /**
360  * @brief ADSP-SC589 Ethernet MAC timer handler
361  *
362  * This routine is periodically called by the TCP/IP stack to handle periodic
363  * operations such as polling the link state
364  *
365  * @param[in] interface Underlying network interface
366  **/
367 
368 void sc589Eth1Tick(NetInterface *interface)
369 {
370  //Valid Ethernet PHY or switch driver?
371  if(interface->phyDriver != NULL)
372  {
373  //Handle periodic operations
374  interface->phyDriver->tick(interface);
375  }
376  else if(interface->switchDriver != NULL)
377  {
378  //Handle periodic operations
379  interface->switchDriver->tick(interface);
380  }
381  else
382  {
383  //Just for sanity
384  }
385 }
386 
387 
388 /**
389  * @brief Enable interrupts
390  * @param[in] interface Underlying network interface
391  **/
392 
394 {
395  //Enable Ethernet MAC interrupts
396  adi_int_EnableInt(INTR_EMAC0_STAT, true);
397 
398  //Valid Ethernet PHY or switch driver?
399  if(interface->phyDriver != NULL)
400  {
401  //Enable Ethernet PHY interrupts
402  interface->phyDriver->enableIrq(interface);
403  }
404  else if(interface->switchDriver != NULL)
405  {
406  //Enable Ethernet switch interrupts
407  interface->switchDriver->enableIrq(interface);
408  }
409  else
410  {
411  //Just for sanity
412  }
413 }
414 
415 
416 /**
417  * @brief Disable interrupts
418  * @param[in] interface Underlying network interface
419  **/
420 
422 {
423  //Disable Ethernet MAC interrupts
424  adi_int_EnableInt(INTR_EMAC0_STAT, false);
425 
426  //Valid Ethernet PHY or switch driver?
427  if(interface->phyDriver != NULL)
428  {
429  //Disable Ethernet PHY interrupts
430  interface->phyDriver->disableIrq(interface);
431  }
432  else if(interface->switchDriver != NULL)
433  {
434  //Disable Ethernet switch interrupts
435  interface->switchDriver->disableIrq(interface);
436  }
437  else
438  {
439  //Just for sanity
440  }
441 }
442 
443 
444 /**
445  * @brief ADSP-SC589 Ethernet MAC interrupt service routine
446  * @param id Interrupt identifier
447  * @param param Unused parameter
448  **/
449 
450 void sc589Eth1IrqHandler(uint32_t id, void *param)
451 {
452  bool_t flag;
453  uint32_t status;
454 
455  //Interrupt service routine prologue
456  osEnterIsr();
457 
458  //This flag will be set if a higher priority task must be woken
459  flag = FALSE;
460 
461  //Read DMA status register
462  status = *pREG_EMAC0_DMA0_STAT;
463 
464  //Packet transmitted?
465  if((status & BITM_EMAC_DMA0_STAT_TI) != 0)
466  {
467  //Clear TI interrupt flag
468  *pREG_EMAC0_DMA0_STAT = BITM_EMAC_DMA0_STAT_TI;
469 
470  //Check whether the TX buffer is available for writing
471  if((txCurDmaDesc->tdes0 & EMAC_TDES0_OWN) == 0)
472  {
473  //Notify the TCP/IP stack that the transmitter is ready to send
474  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
475  }
476  }
477 
478  //Packet received?
479  if((status & BITM_EMAC_DMA0_STAT_RI) != 0)
480  {
481  //Clear RI interrupt flag
482  *pREG_EMAC0_DMA0_STAT = BITM_EMAC_DMA0_STAT_RI;
483 
484  //Set event flag
485  nicDriverInterface->nicEvent = TRUE;
486  //Notify the TCP/IP stack of the event
487  flag |= osSetEventFromIsr(&nicDriverInterface->netContext->event);
488  }
489 
490  //Clear NIS interrupt flag
491  *pREG_EMAC0_DMA0_STAT = BITM_EMAC_DMA0_STAT_NIS;
492 
493  //Interrupt service routine epilogue
494  osExitIsr(flag);
495 }
496 
497 
498 /**
499  * @brief ADSP-SC589 Ethernet MAC event handler
500  * @param[in] interface Underlying network interface
501  **/
502 
504 {
505  error_t error;
506 
507  //Process all pending packets
508  do
509  {
510  //Read incoming packet
511  error = sc589Eth1ReceivePacket(interface);
512 
513  //No more data in the receive buffer?
514  } while(error != ERROR_BUFFER_EMPTY);
515 }
516 
517 
518 /**
519  * @brief Send a packet
520  * @param[in] interface Underlying network interface
521  * @param[in] buffer Multi-part buffer containing the data to send
522  * @param[in] offset Offset to the first data byte
523  * @param[in] ancillary Additional options passed to the stack along with
524  * the packet
525  * @return Error code
526  **/
527 
529  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
530 {
531  size_t length;
532 
533  //Retrieve the length of the packet
534  length = netBufferGetLength(buffer) - offset;
535 
536  //Check the frame length
538  {
539  //The transmitter can accept another packet
540  osSetEvent(&interface->nicTxEvent);
541  //Report an error
542  return ERROR_INVALID_LENGTH;
543  }
544 
545  //Make sure the current buffer is available for writing
546  if((txCurDmaDesc->tdes0 & EMAC_TDES0_OWN) != 0)
547  {
548  return ERROR_FAILURE;
549  }
550 
551  //Copy user data to the transmit buffer
552  netBufferRead((uint8_t *) adi_rtl_system_to_internal_addr(
553  txCurDmaDesc->tdes2), buffer, offset, length);
554 
555  //Write the number of bytes to send
556  txCurDmaDesc->tdes1 = length & EMAC_TDES1_TBS1;
557  //Set LS and FS flags as the data fits in a single buffer
558  txCurDmaDesc->tdes0 |= EMAC_TDES0_LS | EMAC_TDES0_FS;
559  //Give the ownership of the descriptor to the DMA
560  txCurDmaDesc->tdes0 |= EMAC_TDES0_OWN;
561 
562  //Data synchronization barrier
563  __asm("dsb");
564 
565  //Clear TU flag to resume processing
566  *pREG_EMAC0_DMA0_STAT = BITM_EMAC_DMA0_STAT_TU;
567  //Instruct the DMA to poll the transmit descriptor list
568  *pREG_EMAC0_DMA0_TXPOLL = 0;
569 
570  //Point to the next descriptor in the list
571  txCurDmaDesc = (Sc589Eth1TxDmaDesc *) adi_rtl_system_to_internal_addr(
572  txCurDmaDesc->tdes3);
573 
574  //Check whether the next buffer is available for writing
575  if((txCurDmaDesc->tdes0 & EMAC_TDES0_OWN) == 0)
576  {
577  //The transmitter can accept another packet
578  osSetEvent(&interface->nicTxEvent);
579  }
580 
581  //Data successfully written
582  return NO_ERROR;
583 }
584 
585 
586 /**
587  * @brief Receive a packet
588  * @param[in] interface Underlying network interface
589  * @return Error code
590  **/
591 
593 {
594  error_t error;
595  size_t n;
596  NetRxAncillary ancillary;
597 
598  //Current buffer available for reading?
599  if((rxCurDmaDesc->rdes0 & EMAC_RDES0_OWN) == 0)
600  {
601  //FS and LS flags should be set
602  if((rxCurDmaDesc->rdes0 & EMAC_RDES0_FS) != 0 &&
603  (rxCurDmaDesc->rdes0 & EMAC_RDES0_LS) != 0)
604  {
605  //Make sure no error occurred
606  if((rxCurDmaDesc->rdes0 & EMAC_RDES0_ES) == 0)
607  {
608  //Retrieve the length of the frame
609  n = (rxCurDmaDesc->rdes0 & EMAC_RDES0_FL) >> 16;
610  //Limit the number of data to read
612 
613  //Additional options can be passed to the stack along with the packet
614  ancillary = NET_DEFAULT_RX_ANCILLARY;
615 
616  //Pass the packet to the upper layer
617  nicProcessPacket(interface, (uint8_t *) adi_rtl_system_to_internal_addr(
618  rxCurDmaDesc->rdes2), n, &ancillary);
619 
620  //Valid packet received
621  error = NO_ERROR;
622  }
623  else
624  {
625  //The received packet contains an error
626  error = ERROR_INVALID_PACKET;
627  }
628  }
629  else
630  {
631  //The packet is not valid
632  error = ERROR_INVALID_PACKET;
633  }
634 
635  //Give the ownership of the descriptor back to the DMA
636  rxCurDmaDesc->rdes0 = EMAC_RDES0_OWN;
637 
638  //Point to the next descriptor in the list
639  rxCurDmaDesc = (Sc589Eth1RxDmaDesc *) adi_rtl_system_to_internal_addr(
640  rxCurDmaDesc->rdes3);
641  }
642  else
643  {
644  //No more data in the receive buffer
645  error = ERROR_BUFFER_EMPTY;
646  }
647 
648  //Clear RU flag to resume processing
649  *pREG_EMAC0_DMA0_STAT = BITM_EMAC_DMA0_STAT_RU;
650  //Instruct the DMA to poll the receive descriptor list
651  *pREG_EMAC0_DMA0_RXPOLL = 0;
652 
653  //Return status code
654  return error;
655 }
656 
657 
658 /**
659  * @brief Configure MAC address filtering
660  * @param[in] interface Underlying network interface
661  * @return Error code
662  **/
663 
665 {
666  uint_t i;
667  uint_t j;
668  uint_t k;
669  uint32_t crc;
670  uint32_t hashTable[2];
671  MacAddr unicastMacAddr[1];
672  MacFilterEntry *entry;
673 
674  //Debug message
675  TRACE_DEBUG("Updating MAC filter...\r\n");
676 
677  //Promiscuous mode?
678  if(interface->promiscuous)
679  {
680  //Pass all incoming frames regardless of their destination address
681  *pREG_EMAC0_MACFRMFILT = BITM_EMAC_MACFRMFILT_PR;
682  }
683  else
684  {
685  //Set the MAC address of the station
686  *pREG_EMAC0_ADDR0_LO = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
687  *pREG_EMAC0_ADDR0_HI = interface->macAddr.w[2];
688 
689  //The MAC supports one additional address for unicast perfect filtering
690  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
691 
692  //The hash table is used for multicast address filtering
693  hashTable[0] = 0;
694  hashTable[1] = 0;
695 
696  //The MAC address filter contains the list of MAC addresses to accept
697  //when receiving an Ethernet frame
698  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
699  {
700  //Point to the current entry
701  entry = &interface->macAddrFilter[i];
702 
703  //Valid entry?
704  if(entry->refCount > 0)
705  {
706  //Multicast address?
707  if(macIsMulticastAddr(&entry->addr))
708  {
709  //Compute CRC over the current MAC address
710  crc = sc589Eth1CalcCrc(&entry->addr, sizeof(MacAddr));
711 
712  //The upper 6 bits in the CRC register are used to index the
713  //contents of the hash table
714  k = (crc >> 26) & 0x3F;
715 
716  //Update hash table contents
717  hashTable[k / 32] |= (1 << (k % 32));
718  }
719  else
720  {
721  //One additional MAC address can be specified
722  if(j < 1)
723  {
724  //Save the unicast address
725  unicastMacAddr[j++] = entry->addr;
726  }
727  }
728  }
729  }
730 
731  //Configure the first unicast address filter
732  if(j >= 1)
733  {
734  //When the AE bit is set, the entry is used for perfect filtering
735  *pREG_EMAC0_ADDR1_LO = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
736  *pREG_EMAC0_ADDR1_HI = unicastMacAddr[0].w[2] | BITM_EMAC_ADDR1_HI_AE;
737  }
738  else
739  {
740  //When the AE bit is cleared, the entry is ignored
741  *pREG_EMAC0_ADDR1_LO = 0;
742  *pREG_EMAC0_ADDR1_HI = 0;
743  }
744 
745  //Check whether frames with a multicast destination address should be
746  //accepted
747  if(interface->acceptAllMulticast)
748  {
749  //Configure the receive filter
750  *pREG_EMAC0_MACFRMFILT = BITM_EMAC_MACFRMFILT_HPF | BITM_EMAC_MACFRMFILT_PM;
751  }
752  else
753  {
754  //Configure the receive filter
755  *pREG_EMAC0_MACFRMFILT = BITM_EMAC_MACFRMFILT_HPF | BITM_EMAC_MACFRMFILT_HMC;
756 
757  //Configure the multicast hash table
758  *pREG_EMAC0_HASHTBL_LO = hashTable[0];
759  *pREG_EMAC0_HASHTBL_HI = hashTable[1];
760 
761  //Debug message
762  TRACE_DEBUG(" EMAC_HASHTBL_LO = 0x%08" PRIX32 "\r\n", *pREG_EMAC0_HASHTBL_LO);
763  TRACE_DEBUG(" EMAC_HASHTBL_HI = 0x%08" PRIX32 "\r\n", *pREG_EMAC0_HASHTBL_HI);
764  }
765  }
766 
767  //Successful processing
768  return NO_ERROR;
769 }
770 
771 
772 /**
773  * @brief Adjust MAC configuration parameters for proper operation
774  * @param[in] interface Underlying network interface
775  * @return Error code
776  **/
777 
779 {
780  uint32_t config;
781 
782  //Read current MAC configuration
783  config = *pREG_EMAC0_MACCFG;
784 
785  //1000BASE-T operation mode?
786  if(interface->linkSpeed == NIC_LINK_SPEED_1GBPS)
787  {
788  config &= ~BITM_EMAC_MACCFG_PS;
789  config &= ~BITM_EMAC_MACCFG_FES;
790  }
791  //100BASE-TX operation mode?
792  else if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
793  {
794  config |= BITM_EMAC_MACCFG_PS;
795  config |= BITM_EMAC_MACCFG_FES;
796  }
797  //10BASE-T operation mode?
798  else
799  {
800  config |= BITM_EMAC_MACCFG_PS;
801  config &= ~BITM_EMAC_MACCFG_FES;
802  }
803 
804  //Half-duplex or full-duplex mode?
805  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
806  {
807  config |= BITM_EMAC_MACCFG_DM;
808  }
809  else
810  {
811  config &= ~BITM_EMAC_MACCFG_DM;
812  }
813 
814  //Update MAC configuration register
815  *pREG_EMAC0_MACCFG = config;
816 
817  //Successful processing
818  return NO_ERROR;
819 }
820 
821 
822 /**
823  * @brief Write PHY register
824  * @param[in] opcode Access type (2 bits)
825  * @param[in] phyAddr PHY address (5 bits)
826  * @param[in] regAddr Register address (5 bits)
827  * @param[in] data Register value
828  **/
829 
830 void sc589Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr,
831  uint8_t regAddr, uint16_t data)
832 {
833  uint32_t temp;
834 
835  //Valid opcode?
836  if(opcode == SMI_OPCODE_WRITE)
837  {
838  //Take care not to alter MDC clock configuration
839  temp = *pREG_EMAC0_SMI_ADDR & BITM_EMAC_SMI_ADDR_CR;
840  //Set up a write operation
841  temp |= BITM_EMAC_SMI_ADDR_SMIW | BITM_EMAC_SMI_ADDR_SMIB;
842  //PHY address
843  temp |= (phyAddr << BITP_EMAC_SMI_ADDR_PA) & BITM_EMAC_SMI_ADDR_PA;
844  //Register address
845  temp |= (regAddr << BITP_EMAC_SMI_ADDR_SMIR) & BITM_EMAC_SMI_ADDR_SMIR;
846 
847  //Data to be written in the PHY register
848  *pREG_EMAC0_SMI_DATA = data & BITM_EMAC_SMI_DATA_SMID;
849 
850  //Start a write operation
851  *pREG_EMAC0_SMI_ADDR = temp;
852  //Wait for the write to complete
853  while((*pREG_EMAC0_SMI_ADDR & BITM_EMAC_SMI_ADDR_SMIB) != 0)
854  {
855  }
856  }
857  else
858  {
859  //The MAC peripheral only supports standard Clause 22 opcodes
860  }
861 }
862 
863 
864 /**
865  * @brief Read PHY register
866  * @param[in] opcode Access type (2 bits)
867  * @param[in] phyAddr PHY address (5 bits)
868  * @param[in] regAddr Register address (5 bits)
869  * @return Register value
870  **/
871 
872 uint16_t sc589Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
873  uint8_t regAddr)
874 {
875  uint16_t data;
876  uint32_t temp;
877 
878  //Valid opcode?
879  if(opcode == SMI_OPCODE_READ)
880  {
881  //Take care not to alter MDC clock configuration
882  temp = *pREG_EMAC0_SMI_ADDR & BITM_EMAC_SMI_ADDR_CR;
883  //Set up a read operation
884  temp |= BITM_EMAC_SMI_ADDR_SMIB;
885  //PHY address
886  temp |= (phyAddr << BITP_EMAC_SMI_ADDR_PA) & BITM_EMAC_SMI_ADDR_PA;
887  //Register address
888  temp |= (regAddr << BITP_EMAC_SMI_ADDR_SMIR) & BITM_EMAC_SMI_ADDR_SMIR;
889 
890  //Start a read operation
891  *pREG_EMAC0_SMI_ADDR = temp;
892  //Wait for the read to complete
893  while((*pREG_EMAC0_SMI_ADDR & BITM_EMAC_SMI_ADDR_SMIB) != 0)
894  {
895  }
896 
897  //Get register value
898  data = *pREG_EMAC0_SMI_DATA & BITM_EMAC_SMI_DATA_SMID;
899  }
900  else
901  {
902  //The MAC peripheral only supports standard Clause 22 opcodes
903  data = 0;
904  }
905 
906  //Return the value of the PHY register
907  return data;
908 }
909 
910 
911 /**
912  * @brief CRC calculation
913  * @param[in] data Pointer to the data over which to calculate the CRC
914  * @param[in] length Number of bytes to process
915  * @return Resulting CRC value
916  **/
917 
918 uint32_t sc589Eth1CalcCrc(const void *data, size_t length)
919 {
920  uint_t i;
921  uint_t j;
922  uint32_t crc;
923  const uint8_t *p;
924 
925  //Point to the data over which to calculate the CRC
926  p = (uint8_t *) data;
927  //CRC preset value
928  crc = 0xFFFFFFFF;
929 
930  //Loop through data
931  for(i = 0; i < length; i++)
932  {
933  //The message is processed bit by bit
934  for(j = 0; j < 8; j++)
935  {
936  //Update CRC value
937  if((((crc >> 31) ^ (p[i] >> j)) & 0x01) != 0)
938  {
939  crc = (crc << 1) ^ 0x04C11DB7;
940  }
941  else
942  {
943  crc = crc << 1;
944  }
945  }
946  }
947 
948  //Return CRC value
949  return ~crc;
950 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define ENUM_EMAC_SMI_ADDR_CR_DIV62
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
#define EMAC_RDES1_RBS1
uint8_t opcode
Definition: dns_common.h:191
int bool_t
Definition: compiler_port.h:63
void sc589Eth1InitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
void sc589Eth1EventHandler(NetInterface *interface)
ADSP-SC589 Ethernet MAC event handler.
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:690
error_t sc589Eth1SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#define EMAC_TDES0_OWN
uint8_t p
Definition: ndp.h:300
#define EMAC_RDES0_ES
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
#define sleep(delay)
Definition: os_port.h:310
error_t sc589Eth1UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:266
#define EMAC_TDES0_IC
void sc589Eth1EnableIrq(NetInterface *interface)
Enable interrupts.
error_t sc589Eth1Init(NetInterface *interface)
ADSP-SC589 Ethernet MAC initialization.
#define ENUM_EMAC_DMA_BUSMODE_RPBL_32
#define SC589_ETH1_TX_BUFFER_COUNT
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:418
#define SC589_ETH1_TX_BUFFER_SIZE
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define FALSE
Definition: os_port.h:46
#define EMAC_RDES1_RCH
#define EMAC_TDES1_TBS1
#define EMAC_RDES0_LS
void sc589Eth1IrqHandler(uint32_t id, void *param)
ADSP-SC589 Ethernet MAC interrupt service routine.
error_t
Error codes.
Definition: error.h:43
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:103
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
@ ERROR_INVALID_PACKET
Definition: error.h:141
#define NetInterface
Definition: net.h:40
MacAddr addr
MAC address.
Definition: ethernet.h:265
@ ERROR_INVALID_LENGTH
Definition: error.h:111
void sc589Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
uint32_t sc589Eth1CalcCrc(const void *data, size_t length)
CRC calculation.
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define MIN(a, b)
Definition: os_port.h:63
#define EMAC_TDES0_LS
#define rxBuffer
MacAddr
Definition: ethernet.h:197
#define EMAC_TDES0_TCH
Enhanced RX DMA descriptor.
#define EMAC_RDES0_FL
#define TRACE_DEBUG(...)
Definition: debug.h:119
#define ENUM_EMAC_DMA_BUSMODE_PBL_32
uint16_t regAddr
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:264
ADSP-SC589 Ethernet MAC driver (EMAC0 instance)
uint16_t sc589Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define SC589_ETH1_RX_BUFFER_SIZE
#define osEnterIsr()
#define SC589_ETH1_RX_BUFFER_COUNT
Enhanced TX DMA descriptor.
error_t sc589Eth1ReceivePacket(NetInterface *interface)
Receive a packet.
#define EMAC_TDES0_FS
#define rxDmaDesc
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
const NicDriver sc589Eth1Driver
ADSP-SC589 Ethernet MAC driver (EMAC0 instance)
#define txDmaDesc
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
__weak_func void sc589Eth1InitGpio(NetInterface *interface)
GPIO configuration.
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
void sc589Eth1Tick(NetInterface *interface)
ADSP-SC589 Ethernet MAC timer handler.
NIC driver.
Definition: nic.h:286
#define EMAC_RDES0_OWN
error_t sc589Eth1UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
@ NO_ERROR
Success.
Definition: error.h:44
#define SC589_ETH1_RAM_SECTION
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83
#define EMAC_RDES0_FS
void sc589Eth1DisableIrq(NetInterface *interface)
Disable interrupts.