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-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 <limits.h>
36 #include "chip.h"
37 #include "barriers.h"
38 #include "core/net.h"
40 #include "debug.h"
41 
42 //Underlying network interface
43 static NetInterface *nicDriverInterface;
44 
45 //IAR EWARM compiler?
46 #if defined(__ICCARM__)
47 
48 //TX buffer
49 #pragma data_alignment = 8
50 #pragma location = SAMA5D3_ETH2_RAM_SECTION
52 //RX buffer
53 #pragma data_alignment = 8
54 #pragma location = SAMA5D3_ETH2_RAM_SECTION
56 //TX buffer descriptors
57 #pragma data_alignment = 8
58 #pragma location = SAMA5D3_ETH2_RAM_SECTION
60 //RX buffer descriptors
61 #pragma data_alignment = 8
62 #pragma location = SAMA5D3_ETH2_RAM_SECTION
64 
65 //GCC compiler?
66 #else
67 
68 //TX buffer
70  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
71 //RX buffer
73  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
74 //TX buffer descriptors
76  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
77 //RX buffer descriptors
79  __attribute__((aligned(8), __section__(SAMA5D3_ETH2_RAM_SECTION)));
80 
81 #endif
82 
83 //TX buffer index
84 static uint_t txBufferIndex;
85 //RX buffer index
86 static uint_t rxBufferIndex;
87 
88 
89 /**
90  * @brief SAMA5D3 Ethernet MAC driver (GMAC instance)
91  **/
92 
94 {
96  ETH_MTU,
107  TRUE,
108  TRUE,
109  TRUE,
110  FALSE
111 };
112 
113 
114 /**
115  * @brief SAMA5D3 Ethernet MAC initialization
116  * @param[in] interface Underlying network interface
117  * @return Error code
118  **/
119 
121 {
122  error_t error;
123  volatile uint32_t status;
124 
125  //Debug message
126  TRACE_INFO("Initializing SAMA5D3 Ethernet MAC (GMAC)...\r\n");
127 
128  //Save underlying network interface
129  nicDriverInterface = interface;
130 
131  //Enable GMAC peripheral clock
132  PMC->PMC_PCER1 = (1 << (ID_GMAC0 - 32));
133  //Enable IRQ controller peripheral clock
134  PMC->PMC_PCER1 = (1 << (ID_AIC - 32));
135 
136  //Disable transmit and receive circuits
137  GMAC0->GMAC_NCR = 0;
138 
139  //GPIO configuration
140  sama5d3Eth2InitGpio(interface);
141 
142  //Configure MDC clock speed
143  GMAC0->GMAC_NCFGR = GMAC_NCFGR_DBW_DBW64 | GMAC_NCFGR_CLK_MCK_224;
144  //Enable management port (MDC and MDIO)
145  GMAC0->GMAC_NCR |= GMAC_NCR_MPE;
146 
147  //Valid Ethernet PHY or switch driver?
148  if(interface->phyDriver != NULL)
149  {
150  //Ethernet PHY initialization
151  error = interface->phyDriver->init(interface);
152  }
153  else if(interface->switchDriver != NULL)
154  {
155  //Ethernet switch initialization
156  error = interface->switchDriver->init(interface);
157  }
158  else
159  {
160  //The interface is not properly configured
161  error = ERROR_FAILURE;
162  }
163 
164  //Any error to report?
165  if(error)
166  {
167  return error;
168  }
169 
170  //Set the MAC address of the station
171  GMAC0->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
172  GMAC0->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
173 
174  //The MAC supports 3 additional addresses for unicast perfect filtering
175  GMAC0->GMAC_SA[1].GMAC_SAB = 0;
176  GMAC0->GMAC_SA[2].GMAC_SAB = 0;
177  GMAC0->GMAC_SA[3].GMAC_SAB = 0;
178 
179  //Initialize hash table
180  GMAC0->GMAC_HRB = 0;
181  GMAC0->GMAC_HRT = 0;
182 
183  //Configure the receive filter
184  GMAC0->GMAC_NCFGR |= GMAC_NCFGR_MAXFS | GMAC_NCFGR_MTIHEN;
185 
186  //Initialize buffer descriptors
187  sama5d3Eth2InitBufferDesc(interface);
188 
189  //Clear transmit status register
190  GMAC0->GMAC_TSR = GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP |
191  GMAC_TSR_TFC | GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL |
192  GMAC_TSR_UBR;
193 
194  //Clear receive status register
195  GMAC0->GMAC_RSR = GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC |
196  GMAC_RSR_BNA;
197 
198  //First disable all GMAC interrupts
199  GMAC0->GMAC_IDR = 0xFFFFFFFF;
200 
201  //Only the desired ones are enabled
202  GMAC0->GMAC_IER = GMAC_IER_HRESP | GMAC_IER_ROVR | GMAC_IER_TCOMP |
203  GMAC_IER_TFC | GMAC_IER_RLEX | GMAC_IER_TUR | GMAC_IER_RXUBR |
204  GMAC_IER_RCOMP;
205 
206  //Read GMAC_ISR register to clear any pending interrupt
207  status = GMAC0->GMAC_ISR;
208  (void) status;
209 
210  //Configure interrupt controller
211  AIC->AIC_SSR = ID_GMAC0;
212  AIC->AIC_SMR = AIC_SMR_SRCTYPE_INT_LEVEL_SENSITIVE | AIC_SMR_PRIOR(SAMA5D3_ETH2_IRQ_PRIORITY);
213  AIC->AIC_SVR = (uint32_t) sama5d3Eth2IrqHandler;
214 
215  //Enable the GMAC to transmit and receive data
216  GMAC0->GMAC_NCR |= GMAC_NCR_TXEN | GMAC_NCR_RXEN;
217 
218  //Accept any packets from the upper layer
219  osSetEvent(&interface->nicTxEvent);
220 
221  //Successful initialization
222  return NO_ERROR;
223 }
224 
225 
226 /**
227  * @brief GPIO configuration
228  * @param[in] interface Underlying network interface
229  **/
230 
231 __weak_func void sama5d3Eth2InitGpio(NetInterface *interface)
232 {
233 //SAMA5D3-Xplained, SAMA5D3-EDS or EVB-KSZ9477 evaluation board?
234 #if defined(CONFIG_BOARD_SAMA5D3_XPLAINED)
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  GMAC0->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  GMAC0->GMAC_TBQB = (uint32_t) txBufferDesc;
307  //Start location of the RX descriptor list
308  GMAC0->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_GMAC0;
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_GMAC0;
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 = GMAC0->GMAC_ISR;
419  tsr = GMAC0->GMAC_TSR;
420  rsr = GMAC0->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  GMAC0->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(&nicDriverInterface->netContext->event);
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 = GMAC0->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  GMAC0->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  //Data synchronization barrier
544  dsb();
545 
546  //Set the TSTART bit to initiate transmission
547  GMAC0->GMAC_NCR |= GMAC_NCR_TSTART;
548 
549  //Check whether the next buffer is available for writing
550  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
551  {
552  //The transmitter can accept another packet
553  osSetEvent(&interface->nicTxEvent);
554  }
555 
556  //Successful processing
557  return NO_ERROR;
558 }
559 
560 
561 /**
562  * @brief Receive a packet
563  * @param[in] interface Underlying network interface
564  * @return Error code
565  **/
566 
568 {
569  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
570  error_t error;
571  uint_t i;
572  uint_t j;
573  uint_t sofIndex;
574  uint_t eofIndex;
575  size_t n;
576  size_t size;
577  size_t length;
578 
579  //Initialize variables
580  size = 0;
581  sofIndex = UINT_MAX;
582  eofIndex = UINT_MAX;
583 
584  //Search for SOF and EOF flags
585  for(i = 0; i < SAMA5D3_ETH2_RX_BUFFER_COUNT; i++)
586  {
587  //Point to the current entry
588  j = rxBufferIndex + i;
589 
590  //Wrap around to the beginning of the buffer if necessary
592  {
594  }
595 
596  //No more entries to process?
597  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
598  {
599  //Stop processing
600  break;
601  }
602 
603  //A valid SOF has been found?
604  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
605  {
606  //Save the position of the SOF
607  sofIndex = i;
608  }
609 
610  //A valid EOF has been found?
611  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
612  {
613  //Save the position of the EOF
614  eofIndex = i;
615  //Retrieve the length of the frame
616  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
617  //Limit the number of data to read
618  size = MIN(size, ETH_MAX_FRAME_SIZE);
619  //Stop processing since we have reached the end of the frame
620  break;
621  }
622  }
623 
624  //Determine the number of entries to process
625  if(eofIndex != UINT_MAX)
626  {
627  j = eofIndex + 1;
628  }
629  else if(sofIndex != UINT_MAX)
630  {
631  j = sofIndex;
632  }
633  else
634  {
635  j = i;
636  }
637 
638  //Total number of bytes that have been copied from the receive buffer
639  length = 0;
640 
641  //Process incoming frame
642  for(i = 0; i < j; i++)
643  {
644  //Any data to copy from current buffer?
645  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
646  {
647  //Calculate the number of bytes to read at a time
649  //Copy data from receive buffer
650  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
651  //Update byte counters
652  length += n;
653  size -= n;
654  }
655 
656  //Mark the current buffer as free
657  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
658 
659  //Point to the following entry
660  rxBufferIndex++;
661 
662  //Wrap around to the beginning of the buffer if necessary
663  if(rxBufferIndex >= SAMA5D3_ETH2_RX_BUFFER_COUNT)
664  {
665  rxBufferIndex = 0;
666  }
667  }
668 
669  //Any packet to process?
670  if(length > 0)
671  {
672  NetRxAncillary ancillary;
673 
674  //Additional options can be passed to the stack along with the packet
675  ancillary = NET_DEFAULT_RX_ANCILLARY;
676 
677  //Pass the packet to the upper layer
678  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
679  //Valid packet received
680  error = NO_ERROR;
681  }
682  else
683  {
684  //No more data in the receive buffer
685  error = ERROR_BUFFER_EMPTY;
686  }
687 
688  //Return status code
689  return error;
690 }
691 
692 
693 /**
694  * @brief Configure MAC address filtering
695  * @param[in] interface Underlying network interface
696  * @return Error code
697  **/
698 
700 {
701  uint_t i;
702  uint_t j;
703  uint_t k;
704  uint8_t *p;
705  uint32_t hashTable[2];
706  MacAddr unicastMacAddr[3];
707  MacFilterEntry *entry;
708 
709  //Debug message
710  TRACE_DEBUG("Updating MAC filter...\r\n");
711 
712  //Set the MAC address of the station
713  GMAC0->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
714  GMAC0->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
715 
716  //The MAC supports 3 additional addresses for unicast perfect filtering
717  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
718  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
719  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
720 
721  //The hash table is used for multicast address filtering
722  hashTable[0] = 0;
723  hashTable[1] = 0;
724 
725  //The MAC address filter contains the list of MAC addresses to accept
726  //when receiving an Ethernet frame
727  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
728  {
729  //Point to the current entry
730  entry = &interface->macAddrFilter[i];
731 
732  //Valid entry?
733  if(entry->refCount > 0)
734  {
735  //Multicast address?
736  if(macIsMulticastAddr(&entry->addr))
737  {
738  //Point to the MAC address
739  p = entry->addr.b;
740 
741  //Apply the hash function
742  k = (p[0] >> 6) ^ p[0];
743  k ^= (p[1] >> 4) ^ (p[1] << 2);
744  k ^= (p[2] >> 2) ^ (p[2] << 4);
745  k ^= (p[3] >> 6) ^ p[3];
746  k ^= (p[4] >> 4) ^ (p[4] << 2);
747  k ^= (p[5] >> 2) ^ (p[5] << 4);
748 
749  //The hash value is reduced to a 6-bit index
750  k &= 0x3F;
751 
752  //Update hash table contents
753  hashTable[k / 32] |= (1 << (k % 32));
754  }
755  else
756  {
757  //Up to 3 additional MAC addresses can be specified
758  if(j < 3)
759  {
760  //Save the unicast address
761  unicastMacAddr[j] = entry->addr;
762  }
763  else
764  {
765  //Point to the MAC address
766  p = entry->addr.b;
767 
768  //Apply the hash function
769  k = (p[0] >> 6) ^ p[0];
770  k ^= (p[1] >> 4) ^ (p[1] << 2);
771  k ^= (p[2] >> 2) ^ (p[2] << 4);
772  k ^= (p[3] >> 6) ^ p[3];
773  k ^= (p[4] >> 4) ^ (p[4] << 2);
774  k ^= (p[5] >> 2) ^ (p[5] << 4);
775 
776  //The hash value is reduced to a 6-bit index
777  k &= 0x3F;
778 
779  //Update hash table contents
780  hashTable[k / 32] |= (1 << (k % 32));
781  }
782 
783  //Increment the number of unicast addresses
784  j++;
785  }
786  }
787  }
788 
789  //Configure the first unicast address filter
790  if(j >= 1)
791  {
792  //The address is activated when SAT register is written
793  GMAC0->GMAC_SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
794  GMAC0->GMAC_SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
795  }
796  else
797  {
798  //The address is deactivated when SAB register is written
799  GMAC0->GMAC_SA[1].GMAC_SAB = 0;
800  }
801 
802  //Configure the second unicast address filter
803  if(j >= 2)
804  {
805  //The address is activated when SAT register is written
806  GMAC0->GMAC_SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
807  GMAC0->GMAC_SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
808  }
809  else
810  {
811  //The address is deactivated when SAB register is written
812  GMAC0->GMAC_SA[2].GMAC_SAB = 0;
813  }
814 
815  //Configure the third unicast address filter
816  if(j >= 3)
817  {
818  //The address is activated when SAT register is written
819  GMAC0->GMAC_SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
820  GMAC0->GMAC_SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
821  }
822  else
823  {
824  //The address is deactivated when SAB register is written
825  GMAC0->GMAC_SA[3].GMAC_SAB = 0;
826  }
827 
828  //The perfect MAC filter supports only 3 unicast addresses
829  if(j >= 4)
830  {
831  GMAC0->GMAC_NCFGR |= GMAC_NCFGR_UNIHEN;
832  }
833  else
834  {
835  GMAC0->GMAC_NCFGR &= ~GMAC_NCFGR_UNIHEN;
836  }
837 
838  //Configure the multicast hash table
839  GMAC0->GMAC_HRB = hashTable[0];
840  GMAC0->GMAC_HRT = hashTable[1];
841 
842  //Debug message
843  TRACE_DEBUG(" HRB = 0x%08" PRIX32 "\r\n", GMAC0->GMAC_HRB);
844  TRACE_DEBUG(" HRT = 0x%08" PRIX32 "\r\n", GMAC0->GMAC_HRT);
845 
846  //Successful processing
847  return NO_ERROR;
848 }
849 
850 
851 /**
852  * @brief Adjust MAC configuration parameters for proper operation
853  * @param[in] interface Underlying network interface
854  * @return Error code
855  **/
856 
858 {
859  uint32_t config;
860 
861  //Read network configuration register
862  config = GMAC0->GMAC_NCFGR;
863 
864  //1000BASE-T operation mode?
865  if(interface->linkSpeed == NIC_LINK_SPEED_1GBPS)
866  {
867  config |= GMAC_NCFGR_GBE;
868  config &= ~GMAC_NCFGR_SPD;
869  }
870  //100BASE-TX operation mode?
871  else if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
872  {
873  config &= ~GMAC_NCFGR_GBE;
874  config |= GMAC_NCFGR_SPD;
875  }
876  //10BASE-T operation mode?
877  else
878  {
879  config &= ~GMAC_NCFGR_GBE;
880  config &= ~GMAC_NCFGR_SPD;
881  }
882 
883  //Half-duplex or full-duplex mode?
884  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
885  {
886  config |= GMAC_NCFGR_FD;
887  }
888  else
889  {
890  config &= ~GMAC_NCFGR_FD;
891  }
892 
893  //Write configuration value back to NCFGR register
894  GMAC0->GMAC_NCFGR = config;
895 
896  //Successful processing
897  return NO_ERROR;
898 }
899 
900 
901 /**
902  * @brief Write PHY register
903  * @param[in] opcode Access type (2 bits)
904  * @param[in] phyAddr PHY address (5 bits)
905  * @param[in] regAddr Register address (5 bits)
906  * @param[in] data Register value
907  **/
908 
909 void sama5d3Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr,
910  uint8_t regAddr, uint16_t data)
911 {
912  uint32_t temp;
913 
914  //Valid opcode?
915  if(opcode == SMI_OPCODE_WRITE)
916  {
917  //Set up a write operation
918  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
919  //PHY address
920  temp |= GMAC_MAN_PHYA(phyAddr);
921  //Register address
922  temp |= GMAC_MAN_REGA(regAddr);
923  //Register value
924  temp |= GMAC_MAN_DATA(data);
925 
926  //Start a write operation
927  GMAC0->GMAC_MAN = temp;
928  //Wait for the write to complete
929  while((GMAC0->GMAC_NSR & GMAC_NSR_IDLE) == 0)
930  {
931  }
932  }
933  else
934  {
935  //The MAC peripheral only supports standard Clause 22 opcodes
936  }
937 }
938 
939 
940 /**
941  * @brief Read PHY register
942  * @param[in] opcode Access type (2 bits)
943  * @param[in] phyAddr PHY address (5 bits)
944  * @param[in] regAddr Register address (5 bits)
945  * @return Register value
946  **/
947 
948 uint16_t sama5d3Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
949  uint8_t regAddr)
950 {
951  uint16_t data;
952  uint32_t temp;
953 
954  //Valid opcode?
955  if(opcode == SMI_OPCODE_READ)
956  {
957  //Set up a read operation
958  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
959  //PHY address
960  temp |= GMAC_MAN_PHYA(phyAddr);
961  //Register address
962  temp |= GMAC_MAN_REGA(regAddr);
963 
964  //Start a read operation
965  GMAC0->GMAC_MAN = temp;
966  //Wait for the read to complete
967  while((GMAC0->GMAC_NSR & GMAC_NSR_IDLE) == 0)
968  {
969  }
970 
971  //Get register value
972  data = GMAC0->GMAC_MAN & GMAC_MAN_DATA_Msk;
973  }
974  else
975  {
976  //The MAC peripheral only supports standard Clause 22 opcodes
977  data = 0;
978  }
979 
980  //Return the value of the PHY register
981  return data;
982 }
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:191
int bool_t
Definition: compiler_port.h:63
#define GMAC_TX_LENGTH
@ 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:224
#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:266
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:418
#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:103
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
MacAddr addr
MAC address.
Definition: ethernet.h:265
@ 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:197
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:264
void sama5d3Eth2DisableIrq(NetInterface *interface)
Disable interrupts.
Ipv6Addr address[]
Definition: ipv6.h:345
__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:51
#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