sama5d3_eth2_driver.c
Go to the documentation of this file.
1 /**
2  * @file sama5d3_eth2_driver.c
3  * @brief SAMA5D3 Gigabit Ethernet MAC driver (GMAC instance)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include <limits.h>
36 #include "sama5d3x.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 //TX buffer
48 #pragma data_alignment = 8
49 #pragma location = SAMA5D3_ETH2_RAM_SECTION
51 //RX buffer
52 #pragma data_alignment = 8
53 #pragma location = SAMA5D3_ETH2_RAM_SECTION
55 //TX buffer descriptors
56 #pragma data_alignment = 8
57 #pragma location = SAMA5D3_ETH2_RAM_SECTION
59 //RX buffer descriptors
60 #pragma data_alignment = 8
61 #pragma location = SAMA5D3_ETH2_RAM_SECTION
63 
64 //GCC compiler?
65 #else
66 
67 //TX buffer
69  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
70 //RX buffer
72  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
73 //TX buffer descriptors
75  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
76 //RX buffer descriptors
78  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
79 
80 #endif
81 
82 //TX buffer index
83 static uint_t txBufferIndex;
84 //RX buffer index
85 static uint_t rxBufferIndex;
86 
87 
88 /**
89  * @brief SAMA5D3 Ethernet MAC driver (GMAC instance)
90  **/
91 
93 {
95  ETH_MTU,
106  TRUE,
107  TRUE,
108  TRUE,
109  FALSE
110 };
111 
112 
113 /**
114  * @brief SAMA5D3 Ethernet MAC initialization
115  * @param[in] interface Underlying network interface
116  * @return Error code
117  **/
118 
120 {
121  error_t error;
122  volatile uint32_t status;
123 
124  //Debug message
125  TRACE_INFO("Initializing SAMA5D3 Ethernet MAC (GMAC)...\r\n");
126 
127  //Save underlying network interface
128  nicDriverInterface = interface;
129 
130  //Enable GMAC peripheral clock
131  PMC->PMC_PCER1 = (1 << (ID_GMAC - 32));
132  //Enable IRQ controller peripheral clock
133  PMC->PMC_PCER1 = (1 << (ID_IRQ - 32));
134 
135  //Disable transmit and receive circuits
136  GMAC->GMAC_NCR = 0;
137 
138  //GPIO configuration
139  sama5d3Eth2InitGpio(interface);
140 
141  //Configure MDC clock speed
142  GMAC->GMAC_NCFGR = GMAC_NCFGR_DBW_DBW64 | GMAC_NCFGR_CLK_MCK_224;
143  //Enable management port (MDC and MDIO)
144  GMAC->GMAC_NCR |= GMAC_NCR_MPE;
145 
146  //Valid Ethernet PHY or switch driver?
147  if(interface->phyDriver != NULL)
148  {
149  //Ethernet PHY initialization
150  error = interface->phyDriver->init(interface);
151  }
152  else if(interface->switchDriver != NULL)
153  {
154  //Ethernet switch initialization
155  error = interface->switchDriver->init(interface);
156  }
157  else
158  {
159  //The interface is not properly configured
160  error = ERROR_FAILURE;
161  }
162 
163  //Any error to report?
164  if(error)
165  {
166  return error;
167  }
168 
169  //Set the MAC address of the station
170  GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
171  GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
172 
173  //The MAC supports 3 additional addresses for unicast perfect filtering
174  GMAC->GMAC_SA[1].GMAC_SAB = 0;
175  GMAC->GMAC_SA[2].GMAC_SAB = 0;
176  GMAC->GMAC_SA[3].GMAC_SAB = 0;
177 
178  //Initialize hash table
179  GMAC->GMAC_HRB = 0;
180  GMAC->GMAC_HRT = 0;
181 
182  //Configure the receive filter
183  GMAC->GMAC_NCFGR |= GMAC_NCFGR_MAXFS | GMAC_NCFGR_MTIHEN;
184 
185  //Initialize buffer descriptors
186  sama5d3Eth2InitBufferDesc(interface);
187 
188  //Clear transmit status register
189  GMAC->GMAC_TSR = GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP |
190  GMAC_TSR_TFC | GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL |
191  GMAC_TSR_UBR;
192 
193  //Clear receive status register
194  GMAC->GMAC_RSR = GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC |
195  GMAC_RSR_BNA;
196 
197  //First disable all GMAC interrupts
198  GMAC->GMAC_IDR = 0xFFFFFFFF;
199 
200  //Only the desired ones are enabled
201  GMAC->GMAC_IER = GMAC_IER_HRESP | GMAC_IER_ROVR | GMAC_IER_TCOMP |
202  GMAC_IER_TFC | GMAC_IER_RLEX | GMAC_IER_TUR | GMAC_IER_RXUBR |
203  GMAC_IER_RCOMP;
204 
205  //Read GMAC_ISR register to clear any pending interrupt
206  status = GMAC->GMAC_ISR;
207  (void) status;
208 
209  //Configure interrupt controller
210  AIC->AIC_SSR = ID_GMAC;
211  AIC->AIC_SMR = AIC_SMR_SRCTYPE_INT_LEVEL_SENSITIVE | AIC_SMR_PRIOR(SAMA5D3_ETH2_IRQ_PRIORITY);
212  AIC->AIC_SVR = (uint32_t) sama5d3Eth2IrqHandler;
213 
214  //Enable the GMAC to transmit and receive data
215  GMAC->GMAC_NCR |= GMAC_NCR_TXEN | GMAC_NCR_RXEN;
216 
217  //Accept any packets from the upper layer
218  osSetEvent(&interface->nicTxEvent);
219 
220  //Successful initialization
221  return NO_ERROR;
222 }
223 
224 
225 /**
226  * @brief GPIO configuration
227  * @param[in] interface Underlying network interface
228  **/
229 
230 __weak_func void sama5d3Eth2InitGpio(NetInterface *interface)
231 {
232 //SAMA5D3-Xplained, SAMA5D3-EDS or EVB-KSZ9477 evaluation board?
233 #if defined(USE_SAMA5D3_XPLAINED) || defined(USE_SAMA5D3_EDS) || \
234  defined(USE_EVB_KSZ9477)
235  uint32_t mask;
236 
237  //Enable PIO peripheral clock
238  PMC->PMC_PCER0 = (1 << ID_PIOB);
239 
240  //Configure RGMII pins
241  mask = PIO_PB18A_G125CK | PIO_PB17A_GMDIO | PIO_PB16A_GMDC |
242  PIO_PB13A_GRXER | PIO_PB12A_GRXDV | PIO_PB11A_GRXCK | PIO_PB9A_GTXEN |
243  PIO_PB8A_GTXCK | PIO_PB7A_GRX3 | PIO_PB6A_GRX2 | PIO_PB5A_GRX1 |
244  PIO_PB4A_GRX0 | PIO_PB3A_GTX3 | PIO_PB2A_GTX2 | PIO_PB1A_GTX1 |
245  PIO_PB0A_GTX0;
246 
247  //Disable pull-up resistors on RGMII pins
248  PIOB->PIO_PUDR = mask;
249  //Disable interrupts-on-change
250  PIOB->PIO_IDR = mask;
251  //Assign MII pins to peripheral A function
252  PIOB->PIO_ABCDSR[0] &= ~mask;
253  PIOB->PIO_ABCDSR[1] &= ~mask;
254  //Disable the PIO from controlling the corresponding pins
255  PIOB->PIO_PDR = mask;
256 
257  //Select RGMII operation mode
258  GMAC->GMAC_UR = GMAC_UR_RGMII;
259 #endif
260 }
261 
262 
263 /**
264  * @brief Initialize buffer descriptors
265  * @param[in] interface Underlying network interface
266  **/
267 
269 {
270  uint_t i;
271  uint32_t address;
272 
273  //Initialize TX buffer descriptors
274  for(i = 0; i < SAMA5D3_ETH2_TX_BUFFER_COUNT; i++)
275  {
276  //Calculate the address of the current TX buffer
277  address = (uint32_t) txBuffer[i];
278  //Write the address to the descriptor entry
279  txBufferDesc[i].address = address;
280  //Initialize status field
281  txBufferDesc[i].status = GMAC_TX_USED;
282  }
283 
284  //Mark the last descriptor entry with the wrap flag
285  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
286  //Initialize TX buffer index
287  txBufferIndex = 0;
288 
289  //Initialize RX buffer descriptors
290  for(i = 0; i < SAMA5D3_ETH2_RX_BUFFER_COUNT; i++)
291  {
292  //Calculate the address of the current RX buffer
293  address = (uint32_t) rxBuffer[i];
294  //Write the address to the descriptor entry
295  rxBufferDesc[i].address = address & GMAC_RX_ADDRESS;
296  //Clear status field
297  rxBufferDesc[i].status = 0;
298  }
299 
300  //Mark the last descriptor entry with the wrap flag
301  rxBufferDesc[i - 1].address |= GMAC_RX_WRAP;
302  //Initialize RX buffer index
303  rxBufferIndex = 0;
304 
305  //Start location of the TX descriptor list
306  GMAC->GMAC_TBQB = (uint32_t) txBufferDesc;
307  //Start location of the RX descriptor list
308  GMAC->GMAC_RBQB = (uint32_t) rxBufferDesc;
309 }
310 
311 
312 /**
313  * @brief SAMA5D3 Ethernet MAC timer handler
314  *
315  * This routine is periodically called by the TCP/IP stack to handle periodic
316  * operations such as polling the link state
317  *
318  * @param[in] interface Underlying network interface
319  **/
320 
322 {
323  //Valid Ethernet PHY or switch driver?
324  if(interface->phyDriver != NULL)
325  {
326  //Handle periodic operations
327  interface->phyDriver->tick(interface);
328  }
329  else if(interface->switchDriver != NULL)
330  {
331  //Handle periodic operations
332  interface->switchDriver->tick(interface);
333  }
334  else
335  {
336  //Just for sanity
337  }
338 }
339 
340 
341 /**
342  * @brief Enable interrupts
343  * @param[in] interface Underlying network interface
344  **/
345 
347 {
348  //Enable Ethernet MAC interrupts
349  AIC->AIC_SSR = ID_GMAC;
350  AIC->AIC_IECR = AIC_IECR_INTEN;
351 
352  //Valid Ethernet PHY or switch driver?
353  if(interface->phyDriver != NULL)
354  {
355  //Enable Ethernet PHY interrupts
356  interface->phyDriver->enableIrq(interface);
357  }
358  else if(interface->switchDriver != NULL)
359  {
360  //Enable Ethernet switch interrupts
361  interface->switchDriver->enableIrq(interface);
362  }
363  else
364  {
365  //Just for sanity
366  }
367 }
368 
369 
370 /**
371  * @brief Disable interrupts
372  * @param[in] interface Underlying network interface
373  **/
374 
376 {
377  //Disable Ethernet MAC interrupts
378  AIC->AIC_SSR = ID_GMAC;
379  AIC->AIC_IDCR = AIC_IDCR_INTD;
380 
381  //Valid Ethernet PHY or switch driver?
382  if(interface->phyDriver != NULL)
383  {
384  //Disable Ethernet PHY interrupts
385  interface->phyDriver->disableIrq(interface);
386  }
387  else if(interface->switchDriver != NULL)
388  {
389  //Disable Ethernet switch interrupts
390  interface->switchDriver->disableIrq(interface);
391  }
392  else
393  {
394  //Just for sanity
395  }
396 }
397 
398 
399 /**
400  * @brief SAMA5D3 Ethernet MAC interrupt service routine
401  **/
402 
404 {
405  bool_t flag;
406  volatile uint32_t isr;
407  volatile uint32_t tsr;
408  volatile uint32_t rsr;
409 
410  //Interrupt service routine prologue
411  osEnterIsr();
412 
413  //This flag will be set if a higher priority task must be woken
414  flag = FALSE;
415 
416  //Each time the software reads GMAC_ISR, it has to check the contents
417  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
418  isr = GMAC->GMAC_ISR;
419  tsr = GMAC->GMAC_TSR;
420  rsr = GMAC->GMAC_RSR;
421  (void) isr;
422 
423  //Packet transmitted?
424  if((tsr & (GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP | GMAC_TSR_TFC |
425  GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL | GMAC_TSR_UBR)) != 0)
426  {
427  //Only clear TSR flags that are currently set
428  GMAC->GMAC_TSR = tsr;
429 
430  //Avoid DMA lockup by sending only one frame at a time (see errata 57.5.1)
431  if((txBufferDesc[0].status & GMAC_TX_USED) != 0 &&
432  (txBufferDesc[1].status & GMAC_TX_USED) != 0)
433  {
434  //Notify the TCP/IP stack that the transmitter is ready to send
435  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
436  }
437  }
438 
439  //Packet received?
440  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
441  {
442  //Set event flag
443  nicDriverInterface->nicEvent = TRUE;
444  //Notify the TCP/IP stack of the event
445  flag |= osSetEventFromIsr(&netEvent);
446  }
447 
448  //Write AIC_EOICR register before exiting
449  AIC->AIC_EOICR = 0;
450 
451  //Interrupt service routine epilogue
452  osExitIsr(flag);
453 }
454 
455 
456 /**
457  * @brief SAMA5D3 Ethernet MAC event handler
458  * @param[in] interface Underlying network interface
459  **/
460 
462 {
463  error_t error;
464  uint32_t rsr;
465 
466  //Read receive status
467  rsr = GMAC->GMAC_RSR;
468 
469  //Packet received?
470  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
471  {
472  //Only clear RSR flags that are currently set
473  GMAC->GMAC_RSR = rsr;
474 
475  //Process all pending packets
476  do
477  {
478  //Read incoming packet
479  error = sama5d3Eth2ReceivePacket(interface);
480 
481  //No more data in the receive buffer?
482  } while(error != ERROR_BUFFER_EMPTY);
483  }
484 }
485 
486 
487 /**
488  * @brief Send a packet
489  * @param[in] interface Underlying network interface
490  * @param[in] buffer Multi-part buffer containing the data to send
491  * @param[in] offset Offset to the first data byte
492  * @param[in] ancillary Additional options passed to the stack along with
493  * the packet
494  * @return Error code
495  **/
496 
498  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
499 {
500  size_t length;
501 
502  //Retrieve the length of the packet
503  length = netBufferGetLength(buffer) - offset;
504 
505  //Check the frame length
507  {
508  //The transmitter can accept another packet
509  osSetEvent(&interface->nicTxEvent);
510  //Report an error
511  return ERROR_INVALID_LENGTH;
512  }
513 
514  //Make sure the current buffer is available for writing
515  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
516  {
517  return ERROR_FAILURE;
518  }
519 
520  //Copy user data to the transmit buffer
521  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
522 
523  //Set the necessary flags in the descriptor entry
524  if(txBufferIndex < (SAMA5D3_ETH2_TX_BUFFER_COUNT - 1))
525  {
526  //Write the status word
527  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
529 
530  //Point to the next buffer
531  txBufferIndex++;
532  }
533  else
534  {
535  //Write the status word
536  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
538 
539  //Wrap around
540  txBufferIndex = 0;
541  }
542 
543  //Set the TSTART bit to initiate transmission
544  GMAC->GMAC_NCR |= GMAC_NCR_TSTART;
545 
546  //Check whether the next buffer is available for writing
547  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
548  {
549  //The transmitter can accept another packet
550  osSetEvent(&interface->nicTxEvent);
551  }
552 
553  //Successful processing
554  return NO_ERROR;
555 }
556 
557 
558 /**
559  * @brief Receive a packet
560  * @param[in] interface Underlying network interface
561  * @return Error code
562  **/
563 
565 {
566  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
567  error_t error;
568  uint_t i;
569  uint_t j;
570  uint_t sofIndex;
571  uint_t eofIndex;
572  size_t n;
573  size_t size;
574  size_t length;
575 
576  //Initialize variables
577  size = 0;
578  sofIndex = UINT_MAX;
579  eofIndex = UINT_MAX;
580 
581  //Search for SOF and EOF flags
582  for(i = 0; i < SAMA5D3_ETH2_RX_BUFFER_COUNT; i++)
583  {
584  //Point to the current entry
585  j = rxBufferIndex + i;
586 
587  //Wrap around to the beginning of the buffer if necessary
589  {
591  }
592 
593  //No more entries to process?
594  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
595  {
596  //Stop processing
597  break;
598  }
599 
600  //A valid SOF has been found?
601  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
602  {
603  //Save the position of the SOF
604  sofIndex = i;
605  }
606 
607  //A valid EOF has been found?
608  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
609  {
610  //Save the position of the EOF
611  eofIndex = i;
612  //Retrieve the length of the frame
613  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
614  //Limit the number of data to read
615  size = MIN(size, ETH_MAX_FRAME_SIZE);
616  //Stop processing since we have reached the end of the frame
617  break;
618  }
619  }
620 
621  //Determine the number of entries to process
622  if(eofIndex != UINT_MAX)
623  {
624  j = eofIndex + 1;
625  }
626  else if(sofIndex != UINT_MAX)
627  {
628  j = sofIndex;
629  }
630  else
631  {
632  j = i;
633  }
634 
635  //Total number of bytes that have been copied from the receive buffer
636  length = 0;
637 
638  //Process incoming frame
639  for(i = 0; i < j; i++)
640  {
641  //Any data to copy from current buffer?
642  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
643  {
644  //Calculate the number of bytes to read at a time
646  //Copy data from receive buffer
647  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
648  //Update byte counters
649  length += n;
650  size -= n;
651  }
652 
653  //Mark the current buffer as free
654  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
655 
656  //Point to the following entry
657  rxBufferIndex++;
658 
659  //Wrap around to the beginning of the buffer if necessary
660  if(rxBufferIndex >= SAMA5D3_ETH2_RX_BUFFER_COUNT)
661  {
662  rxBufferIndex = 0;
663  }
664  }
665 
666  //Any packet to process?
667  if(length > 0)
668  {
669  NetRxAncillary ancillary;
670 
671  //Additional options can be passed to the stack along with the packet
672  ancillary = NET_DEFAULT_RX_ANCILLARY;
673 
674  //Pass the packet to the upper layer
675  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
676  //Valid packet received
677  error = NO_ERROR;
678  }
679  else
680  {
681  //No more data in the receive buffer
682  error = ERROR_BUFFER_EMPTY;
683  }
684 
685  //Return status code
686  return error;
687 }
688 
689 
690 /**
691  * @brief Configure MAC address filtering
692  * @param[in] interface Underlying network interface
693  * @return Error code
694  **/
695 
697 {
698  uint_t i;
699  uint_t j;
700  uint_t k;
701  uint8_t *p;
702  uint32_t hashTable[2];
703  MacAddr unicastMacAddr[3];
704  MacFilterEntry *entry;
705 
706  //Debug message
707  TRACE_DEBUG("Updating MAC filter...\r\n");
708 
709  //Set the MAC address of the station
710  GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
711  GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
712 
713  //The MAC supports 3 additional addresses for unicast perfect filtering
714  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
715  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
716  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
717 
718  //The hash table is used for multicast address filtering
719  hashTable[0] = 0;
720  hashTable[1] = 0;
721 
722  //The MAC address filter contains the list of MAC addresses to accept
723  //when receiving an Ethernet frame
724  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
725  {
726  //Point to the current entry
727  entry = &interface->macAddrFilter[i];
728 
729  //Valid entry?
730  if(entry->refCount > 0)
731  {
732  //Multicast address?
733  if(macIsMulticastAddr(&entry->addr))
734  {
735  //Point to the MAC address
736  p = entry->addr.b;
737 
738  //Apply the hash function
739  k = (p[0] >> 6) ^ p[0];
740  k ^= (p[1] >> 4) ^ (p[1] << 2);
741  k ^= (p[2] >> 2) ^ (p[2] << 4);
742  k ^= (p[3] >> 6) ^ p[3];
743  k ^= (p[4] >> 4) ^ (p[4] << 2);
744  k ^= (p[5] >> 2) ^ (p[5] << 4);
745 
746  //The hash value is reduced to a 6-bit index
747  k &= 0x3F;
748 
749  //Update hash table contents
750  hashTable[k / 32] |= (1 << (k % 32));
751  }
752  else
753  {
754  //Up to 3 additional MAC addresses can be specified
755  if(j < 3)
756  {
757  //Save the unicast address
758  unicastMacAddr[j] = entry->addr;
759  }
760  else
761  {
762  //Point to the MAC address
763  p = entry->addr.b;
764 
765  //Apply the hash function
766  k = (p[0] >> 6) ^ p[0];
767  k ^= (p[1] >> 4) ^ (p[1] << 2);
768  k ^= (p[2] >> 2) ^ (p[2] << 4);
769  k ^= (p[3] >> 6) ^ p[3];
770  k ^= (p[4] >> 4) ^ (p[4] << 2);
771  k ^= (p[5] >> 2) ^ (p[5] << 4);
772 
773  //The hash value is reduced to a 6-bit index
774  k &= 0x3F;
775 
776  //Update hash table contents
777  hashTable[k / 32] |= (1 << (k % 32));
778  }
779 
780  //Increment the number of unicast addresses
781  j++;
782  }
783  }
784  }
785 
786  //Configure the first unicast address filter
787  if(j >= 1)
788  {
789  //The address is activated when SAT register is written
790  GMAC->GMAC_SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
791  GMAC->GMAC_SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
792  }
793  else
794  {
795  //The address is deactivated when SAB register is written
796  GMAC->GMAC_SA[1].GMAC_SAB = 0;
797  }
798 
799  //Configure the second unicast address filter
800  if(j >= 2)
801  {
802  //The address is activated when SAT register is written
803  GMAC->GMAC_SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
804  GMAC->GMAC_SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
805  }
806  else
807  {
808  //The address is deactivated when SAB register is written
809  GMAC->GMAC_SA[2].GMAC_SAB = 0;
810  }
811 
812  //Configure the third unicast address filter
813  if(j >= 3)
814  {
815  //The address is activated when SAT register is written
816  GMAC->GMAC_SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
817  GMAC->GMAC_SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
818  }
819  else
820  {
821  //The address is deactivated when SAB register is written
822  GMAC->GMAC_SA[3].GMAC_SAB = 0;
823  }
824 
825  //The perfect MAC filter supports only 3 unicast addresses
826  if(j >= 4)
827  {
828  GMAC->GMAC_NCFGR |= GMAC_NCFGR_UNIHEN;
829  }
830  else
831  {
832  GMAC->GMAC_NCFGR &= ~GMAC_NCFGR_UNIHEN;
833  }
834 
835  //Configure the multicast hash table
836  GMAC->GMAC_HRB = hashTable[0];
837  GMAC->GMAC_HRT = hashTable[1];
838 
839  //Debug message
840  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", GMAC->GMAC_HRB);
841  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", GMAC->GMAC_HRT);
842 
843  //Successful processing
844  return NO_ERROR;
845 }
846 
847 
848 /**
849  * @brief Adjust MAC configuration parameters for proper operation
850  * @param[in] interface Underlying network interface
851  * @return Error code
852  **/
853 
855 {
856  uint32_t config;
857 
858  //Read network configuration register
859  config = GMAC->GMAC_NCFGR;
860 
861  //1000BASE-T operation mode?
862  if(interface->linkSpeed == NIC_LINK_SPEED_1GBPS)
863  {
864  config |= GMAC_NCFGR_GBE;
865  config &= ~GMAC_NCFGR_SPD;
866  }
867  //100BASE-TX operation mode?
868  else if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
869  {
870  config &= ~GMAC_NCFGR_GBE;
871  config |= GMAC_NCFGR_SPD;
872  }
873  //10BASE-T operation mode?
874  else
875  {
876  config &= ~GMAC_NCFGR_GBE;
877  config &= ~GMAC_NCFGR_SPD;
878  }
879 
880  //Half-duplex or full-duplex mode?
881  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
882  {
883  config |= GMAC_NCFGR_FD;
884  }
885  else
886  {
887  config &= ~GMAC_NCFGR_FD;
888  }
889 
890  //Write configuration value back to NCFGR register
891  GMAC->GMAC_NCFGR = config;
892 
893  //Successful processing
894  return NO_ERROR;
895 }
896 
897 
898 /**
899  * @brief Write PHY register
900  * @param[in] opcode Access type (2 bits)
901  * @param[in] phyAddr PHY address (5 bits)
902  * @param[in] regAddr Register address (5 bits)
903  * @param[in] data Register value
904  **/
905 
906 void sama5d3Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr,
907  uint8_t regAddr, uint16_t data)
908 {
909  uint32_t temp;
910 
911  //Valid opcode?
912  if(opcode == SMI_OPCODE_WRITE)
913  {
914  //Set up a write operation
915  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
916  //PHY address
917  temp |= GMAC_MAN_PHYA(phyAddr);
918  //Register address
919  temp |= GMAC_MAN_REGA(regAddr);
920  //Register value
921  temp |= GMAC_MAN_DATA(data);
922 
923  //Start a write operation
924  GMAC->GMAC_MAN = temp;
925  //Wait for the write to complete
926  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
927  {
928  }
929  }
930  else
931  {
932  //The MAC peripheral only supports standard Clause 22 opcodes
933  }
934 }
935 
936 
937 /**
938  * @brief Read PHY register
939  * @param[in] opcode Access type (2 bits)
940  * @param[in] phyAddr PHY address (5 bits)
941  * @param[in] regAddr Register address (5 bits)
942  * @return Register value
943  **/
944 
945 uint16_t sama5d3Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
946  uint8_t regAddr)
947 {
948  uint16_t data;
949  uint32_t temp;
950 
951  //Valid opcode?
952  if(opcode == SMI_OPCODE_READ)
953  {
954  //Set up a read operation
955  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
956  //PHY address
957  temp |= GMAC_MAN_PHYA(phyAddr);
958  //Register address
959  temp |= GMAC_MAN_REGA(regAddr);
960 
961  //Start a read operation
962  GMAC->GMAC_MAN = temp;
963  //Wait for the read to complete
964  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
965  {
966  }
967 
968  //Get register value
969  data = GMAC->GMAC_MAN & GMAC_MAN_DATA_Msk;
970  }
971  else
972  {
973  //The MAC peripheral only supports standard Clause 22 opcodes
974  data = 0;
975  }
976 
977  //Return the value of the PHY register
978  return data;
979 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:61
#define GMAC_TX_LENGTH
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define SAMA5D3_ETH2_RAM_SECTION
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
uint8_t p
Definition: ndp.h:300
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:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
#define SAMA5D3_ETH2_IRQ_PRIORITY
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
error_t sama5d3Eth2Init(NetInterface *interface)
SAMA5D3 Ethernet MAC initialization.
#define GMAC_RX_WRAP
#define GMAC_MAN_PHYA
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
uint16_t sama5d3Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define GMAC_RX_EOF
#define GMAC_MAN_DATA
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define SAMA5D3_ETH2_RX_BUFFER_SIZE
#define GMAC_TX_USED
#define GMAC_MAN_OP
#define FALSE
Definition: os_port.h:46
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
void sama5d3Eth2EventHandler(NetInterface *interface)
SAMA5D3 Ethernet MAC event handler.
#define GMAC_RX_ADDRESS
#define SAMA5D3_ETH2_TX_BUFFER_SIZE
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:105
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
@ ERROR_INVALID_LENGTH
Definition: error.h:111
const NicDriver sama5d3Eth2Driver
SAMA5D3 Ethernet MAC driver (GMAC instance)
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
void sama5d3Eth2Tick(NetInterface *interface)
SAMA5D3 Ethernet MAC timer handler.
#define NetTxAncillary
Definition: net_misc.h:36
uint8_t mask
Definition: web_socket.h:319
#define SMI_OPCODE_READ
Definition: nic.h:67
#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 SAMA5D3_ETH2_RX_BUFFER_COUNT
#define SAMA5D3_ETH2_TX_BUFFER_COUNT
#define MIN(a, b)
Definition: os_port.h:63
error_t sama5d3Eth2ReceivePacket(NetInterface *interface)
Receive a packet.
#define rxBuffer
#define GMAC_RX_SOF
MacAddr
Definition: ethernet.h:195
void sama5d3Eth2EnableIrq(NetInterface *interface)
Enable interrupts.
#define TRACE_DEBUG(...)
Definition: debug.h:119
uint16_t regAddr
#define GMAC_RX_LENGTH
#define ETH_MTU
Definition: ethernet.h:116
#define GMAC_TX_LAST
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
void sama5d3Eth2DisableIrq(NetInterface *interface)
Disable interrupts.
Ipv6Addr address[]
Definition: ipv6.h:325
__weak_func void sama5d3Eth2InitGpio(NetInterface *interface)
GPIO configuration.
void sama5d3Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define osEnterIsr()
error_t sama5d3Eth2UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Transmit buffer descriptor.
void sama5d3Eth2InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
#define GMAC_MAN_WTN
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t sama5d3Eth2UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
SAMA5D3 Gigabit Ethernet MAC driver (GMAC instance)
Receive buffer descriptor.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
#define GMAC_RX_OWNERSHIP
#define GMAC_MAN_REGA
void sama5d3Eth2IrqHandler(void)
SAMA5D3 Ethernet MAC interrupt service routine.
error_t sama5d3Eth2SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
#define GMAC_MAN_DATA_Msk
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
#define GMAC_TX_WRAP
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83