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-2024 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.4.4
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) || defined(USE_EVB_KSZ9477)
234  //Enable PIO peripheral clock
235  PMC->PMC_PCER0 = (1 << ID_PIOB);
236 
237  //Disable pull-up resistors on RGMII pins
238  PIOB->PIO_PUDR = GMAC_RGMII_MASK;
239  //Disable interrupts-on-change
240  PIOB->PIO_IDR = GMAC_RGMII_MASK;
241  //Assign MII pins to peripheral A function
242  PIOB->PIO_ABCDSR[0] &= ~GMAC_RGMII_MASK;
243  PIOB->PIO_ABCDSR[1] &= ~GMAC_RGMII_MASK;
244  //Disable the PIO from controlling the corresponding pins
245  PIOB->PIO_PDR = GMAC_RGMII_MASK;
246 
247  //Select RGMII operation mode
248  GMAC->GMAC_UR = GMAC_UR_RGMII;
249 #endif
250 }
251 
252 
253 /**
254  * @brief Initialize buffer descriptors
255  * @param[in] interface Underlying network interface
256  **/
257 
259 {
260  uint_t i;
261  uint32_t address;
262 
263  //Initialize TX buffer descriptors
264  for(i = 0; i < SAMA5D3_ETH2_TX_BUFFER_COUNT; i++)
265  {
266  //Calculate the address of the current TX buffer
267  address = (uint32_t) txBuffer[i];
268  //Write the address to the descriptor entry
269  txBufferDesc[i].address = address;
270  //Initialize status field
271  txBufferDesc[i].status = GMAC_TX_USED;
272  }
273 
274  //Mark the last descriptor entry with the wrap flag
275  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
276  //Initialize TX buffer index
277  txBufferIndex = 0;
278 
279  //Initialize RX buffer descriptors
280  for(i = 0; i < SAMA5D3_ETH2_RX_BUFFER_COUNT; i++)
281  {
282  //Calculate the address of the current RX buffer
283  address = (uint32_t) rxBuffer[i];
284  //Write the address to the descriptor entry
285  rxBufferDesc[i].address = address & GMAC_RX_ADDRESS;
286  //Clear status field
287  rxBufferDesc[i].status = 0;
288  }
289 
290  //Mark the last descriptor entry with the wrap flag
291  rxBufferDesc[i - 1].address |= GMAC_RX_WRAP;
292  //Initialize RX buffer index
293  rxBufferIndex = 0;
294 
295  //Start location of the TX descriptor list
296  GMAC->GMAC_TBQB = (uint32_t) txBufferDesc;
297  //Start location of the RX descriptor list
298  GMAC->GMAC_RBQB = (uint32_t) rxBufferDesc;
299 }
300 
301 
302 /**
303  * @brief SAMA5D3 Ethernet MAC timer handler
304  *
305  * This routine is periodically called by the TCP/IP stack to handle periodic
306  * operations such as polling the link state
307  *
308  * @param[in] interface Underlying network interface
309  **/
310 
312 {
313  //Valid Ethernet PHY or switch driver?
314  if(interface->phyDriver != NULL)
315  {
316  //Handle periodic operations
317  interface->phyDriver->tick(interface);
318  }
319  else if(interface->switchDriver != NULL)
320  {
321  //Handle periodic operations
322  interface->switchDriver->tick(interface);
323  }
324  else
325  {
326  //Just for sanity
327  }
328 }
329 
330 
331 /**
332  * @brief Enable interrupts
333  * @param[in] interface Underlying network interface
334  **/
335 
337 {
338  //Enable Ethernet MAC interrupts
339  AIC->AIC_SSR = ID_GMAC;
340  AIC->AIC_IECR = AIC_IECR_INTEN;
341 
342  //Valid Ethernet PHY or switch driver?
343  if(interface->phyDriver != NULL)
344  {
345  //Enable Ethernet PHY interrupts
346  interface->phyDriver->enableIrq(interface);
347  }
348  else if(interface->switchDriver != NULL)
349  {
350  //Enable Ethernet switch interrupts
351  interface->switchDriver->enableIrq(interface);
352  }
353  else
354  {
355  //Just for sanity
356  }
357 }
358 
359 
360 /**
361  * @brief Disable interrupts
362  * @param[in] interface Underlying network interface
363  **/
364 
366 {
367  //Disable Ethernet MAC interrupts
368  AIC->AIC_SSR = ID_GMAC;
369  AIC->AIC_IDCR = AIC_IDCR_INTD;
370 
371  //Valid Ethernet PHY or switch driver?
372  if(interface->phyDriver != NULL)
373  {
374  //Disable Ethernet PHY interrupts
375  interface->phyDriver->disableIrq(interface);
376  }
377  else if(interface->switchDriver != NULL)
378  {
379  //Disable Ethernet switch interrupts
380  interface->switchDriver->disableIrq(interface);
381  }
382  else
383  {
384  //Just for sanity
385  }
386 }
387 
388 
389 /**
390  * @brief SAMA5D3 Ethernet MAC interrupt service routine
391  **/
392 
394 {
395  bool_t flag;
396  volatile uint32_t isr;
397  volatile uint32_t tsr;
398  volatile uint32_t rsr;
399 
400  //Interrupt service routine prologue
401  osEnterIsr();
402 
403  //This flag will be set if a higher priority task must be woken
404  flag = FALSE;
405 
406  //Each time the software reads GMAC_ISR, it has to check the contents
407  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
408  isr = GMAC->GMAC_ISR;
409  tsr = GMAC->GMAC_TSR;
410  rsr = GMAC->GMAC_RSR;
411  (void) isr;
412 
413  //Packet transmitted?
414  if((tsr & (GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP | GMAC_TSR_TFC |
415  GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL | GMAC_TSR_UBR)) != 0)
416  {
417  //Only clear TSR flags that are currently set
418  GMAC->GMAC_TSR = tsr;
419 
420  //Avoid DMA lockup by sending only one frame at a time (see errata 57.5.1)
421  if((txBufferDesc[0].status & GMAC_TX_USED) != 0 &&
422  (txBufferDesc[1].status & GMAC_TX_USED) != 0)
423  {
424  //Notify the TCP/IP stack that the transmitter is ready to send
425  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
426  }
427  }
428 
429  //Packet received?
430  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
431  {
432  //Set event flag
433  nicDriverInterface->nicEvent = TRUE;
434  //Notify the TCP/IP stack of the event
435  flag |= osSetEventFromIsr(&netEvent);
436  }
437 
438  //Write AIC_EOICR register before exiting
439  AIC->AIC_EOICR = 0;
440 
441  //Interrupt service routine epilogue
442  osExitIsr(flag);
443 }
444 
445 
446 /**
447  * @brief SAMA5D3 Ethernet MAC event handler
448  * @param[in] interface Underlying network interface
449  **/
450 
452 {
453  error_t error;
454  uint32_t rsr;
455 
456  //Read receive status
457  rsr = GMAC->GMAC_RSR;
458 
459  //Packet received?
460  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
461  {
462  //Only clear RSR flags that are currently set
463  GMAC->GMAC_RSR = rsr;
464 
465  //Process all pending packets
466  do
467  {
468  //Read incoming packet
469  error = sama5d3Eth2ReceivePacket(interface);
470 
471  //No more data in the receive buffer?
472  } while(error != ERROR_BUFFER_EMPTY);
473  }
474 }
475 
476 
477 /**
478  * @brief Send a packet
479  * @param[in] interface Underlying network interface
480  * @param[in] buffer Multi-part buffer containing the data to send
481  * @param[in] offset Offset to the first data byte
482  * @param[in] ancillary Additional options passed to the stack along with
483  * the packet
484  * @return Error code
485  **/
486 
488  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
489 {
490  size_t length;
491 
492  //Retrieve the length of the packet
493  length = netBufferGetLength(buffer) - offset;
494 
495  //Check the frame length
497  {
498  //The transmitter can accept another packet
499  osSetEvent(&interface->nicTxEvent);
500  //Report an error
501  return ERROR_INVALID_LENGTH;
502  }
503 
504  //Make sure the current buffer is available for writing
505  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
506  {
507  return ERROR_FAILURE;
508  }
509 
510  //Copy user data to the transmit buffer
511  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
512 
513  //Set the necessary flags in the descriptor entry
514  if(txBufferIndex < (SAMA5D3_ETH2_TX_BUFFER_COUNT - 1))
515  {
516  //Write the status word
517  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
519 
520  //Point to the next buffer
521  txBufferIndex++;
522  }
523  else
524  {
525  //Write the status word
526  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
528 
529  //Wrap around
530  txBufferIndex = 0;
531  }
532 
533  //Set the TSTART bit to initiate transmission
534  GMAC->GMAC_NCR |= GMAC_NCR_TSTART;
535 
536  //Check whether the next buffer is available for writing
537  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
538  {
539  //The transmitter can accept another packet
540  osSetEvent(&interface->nicTxEvent);
541  }
542 
543  //Successful processing
544  return NO_ERROR;
545 }
546 
547 
548 /**
549  * @brief Receive a packet
550  * @param[in] interface Underlying network interface
551  * @return Error code
552  **/
553 
555 {
556  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
557  error_t error;
558  uint_t i;
559  uint_t j;
560  uint_t sofIndex;
561  uint_t eofIndex;
562  size_t n;
563  size_t size;
564  size_t length;
565 
566  //Initialize variables
567  size = 0;
568  sofIndex = UINT_MAX;
569  eofIndex = UINT_MAX;
570 
571  //Search for SOF and EOF flags
572  for(i = 0; i < SAMA5D3_ETH2_RX_BUFFER_COUNT; i++)
573  {
574  //Point to the current entry
575  j = rxBufferIndex + i;
576 
577  //Wrap around to the beginning of the buffer if necessary
579  {
581  }
582 
583  //No more entries to process?
584  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
585  {
586  //Stop processing
587  break;
588  }
589 
590  //A valid SOF has been found?
591  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
592  {
593  //Save the position of the SOF
594  sofIndex = i;
595  }
596 
597  //A valid EOF has been found?
598  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
599  {
600  //Save the position of the EOF
601  eofIndex = i;
602  //Retrieve the length of the frame
603  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
604  //Limit the number of data to read
605  size = MIN(size, ETH_MAX_FRAME_SIZE);
606  //Stop processing since we have reached the end of the frame
607  break;
608  }
609  }
610 
611  //Determine the number of entries to process
612  if(eofIndex != UINT_MAX)
613  {
614  j = eofIndex + 1;
615  }
616  else if(sofIndex != UINT_MAX)
617  {
618  j = sofIndex;
619  }
620  else
621  {
622  j = i;
623  }
624 
625  //Total number of bytes that have been copied from the receive buffer
626  length = 0;
627 
628  //Process incoming frame
629  for(i = 0; i < j; i++)
630  {
631  //Any data to copy from current buffer?
632  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
633  {
634  //Calculate the number of bytes to read at a time
636  //Copy data from receive buffer
637  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
638  //Update byte counters
639  length += n;
640  size -= n;
641  }
642 
643  //Mark the current buffer as free
644  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
645 
646  //Point to the following entry
647  rxBufferIndex++;
648 
649  //Wrap around to the beginning of the buffer if necessary
650  if(rxBufferIndex >= SAMA5D3_ETH2_RX_BUFFER_COUNT)
651  {
652  rxBufferIndex = 0;
653  }
654  }
655 
656  //Any packet to process?
657  if(length > 0)
658  {
659  NetRxAncillary ancillary;
660 
661  //Additional options can be passed to the stack along with the packet
662  ancillary = NET_DEFAULT_RX_ANCILLARY;
663 
664  //Pass the packet to the upper layer
665  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
666  //Valid packet received
667  error = NO_ERROR;
668  }
669  else
670  {
671  //No more data in the receive buffer
672  error = ERROR_BUFFER_EMPTY;
673  }
674 
675  //Return status code
676  return error;
677 }
678 
679 
680 /**
681  * @brief Configure MAC address filtering
682  * @param[in] interface Underlying network interface
683  * @return Error code
684  **/
685 
687 {
688  uint_t i;
689  uint_t j;
690  uint_t k;
691  uint8_t *p;
692  uint32_t hashTable[2];
693  MacAddr unicastMacAddr[3];
694  MacFilterEntry *entry;
695 
696  //Debug message
697  TRACE_DEBUG("Updating MAC filter...\r\n");
698 
699  //Set the MAC address of the station
700  GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
701  GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
702 
703  //The MAC supports 3 additional addresses for unicast perfect filtering
704  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
705  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
706  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
707 
708  //The hash table is used for multicast address filtering
709  hashTable[0] = 0;
710  hashTable[1] = 0;
711 
712  //The MAC address filter contains the list of MAC addresses to accept
713  //when receiving an Ethernet frame
714  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
715  {
716  //Point to the current entry
717  entry = &interface->macAddrFilter[i];
718 
719  //Valid entry?
720  if(entry->refCount > 0)
721  {
722  //Multicast address?
723  if(macIsMulticastAddr(&entry->addr))
724  {
725  //Point to the MAC address
726  p = entry->addr.b;
727 
728  //Apply the hash function
729  k = (p[0] >> 6) ^ p[0];
730  k ^= (p[1] >> 4) ^ (p[1] << 2);
731  k ^= (p[2] >> 2) ^ (p[2] << 4);
732  k ^= (p[3] >> 6) ^ p[3];
733  k ^= (p[4] >> 4) ^ (p[4] << 2);
734  k ^= (p[5] >> 2) ^ (p[5] << 4);
735 
736  //The hash value is reduced to a 6-bit index
737  k &= 0x3F;
738 
739  //Update hash table contents
740  hashTable[k / 32] |= (1 << (k % 32));
741  }
742  else
743  {
744  //Up to 3 additional MAC addresses can be specified
745  if(j < 3)
746  {
747  //Save the unicast address
748  unicastMacAddr[j] = entry->addr;
749  }
750  else
751  {
752  //Point to the MAC address
753  p = entry->addr.b;
754 
755  //Apply the hash function
756  k = (p[0] >> 6) ^ p[0];
757  k ^= (p[1] >> 4) ^ (p[1] << 2);
758  k ^= (p[2] >> 2) ^ (p[2] << 4);
759  k ^= (p[3] >> 6) ^ p[3];
760  k ^= (p[4] >> 4) ^ (p[4] << 2);
761  k ^= (p[5] >> 2) ^ (p[5] << 4);
762 
763  //The hash value is reduced to a 6-bit index
764  k &= 0x3F;
765 
766  //Update hash table contents
767  hashTable[k / 32] |= (1 << (k % 32));
768  }
769 
770  //Increment the number of unicast addresses
771  j++;
772  }
773  }
774  }
775 
776  //Configure the first unicast address filter
777  if(j >= 1)
778  {
779  //The address is activated when SAT register is written
780  GMAC->GMAC_SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
781  GMAC->GMAC_SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
782  }
783  else
784  {
785  //The address is deactivated when SAB register is written
786  GMAC->GMAC_SA[1].GMAC_SAB = 0;
787  }
788 
789  //Configure the second unicast address filter
790  if(j >= 2)
791  {
792  //The address is activated when SAT register is written
793  GMAC->GMAC_SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
794  GMAC->GMAC_SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
795  }
796  else
797  {
798  //The address is deactivated when SAB register is written
799  GMAC->GMAC_SA[2].GMAC_SAB = 0;
800  }
801 
802  //Configure the third unicast address filter
803  if(j >= 3)
804  {
805  //The address is activated when SAT register is written
806  GMAC->GMAC_SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
807  GMAC->GMAC_SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
808  }
809  else
810  {
811  //The address is deactivated when SAB register is written
812  GMAC->GMAC_SA[3].GMAC_SAB = 0;
813  }
814 
815  //The perfect MAC filter supports only 3 unicast addresses
816  if(j >= 4)
817  {
818  GMAC->GMAC_NCFGR |= GMAC_NCFGR_UNIHEN;
819  }
820  else
821  {
822  GMAC->GMAC_NCFGR &= ~GMAC_NCFGR_UNIHEN;
823  }
824 
825  //Configure the multicast hash table
826  GMAC->GMAC_HRB = hashTable[0];
827  GMAC->GMAC_HRT = hashTable[1];
828 
829  //Debug message
830  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", GMAC->GMAC_HRB);
831  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", GMAC->GMAC_HRT);
832 
833  //Successful processing
834  return NO_ERROR;
835 }
836 
837 
838 /**
839  * @brief Adjust MAC configuration parameters for proper operation
840  * @param[in] interface Underlying network interface
841  * @return Error code
842  **/
843 
845 {
846  uint32_t config;
847 
848  //Read network configuration register
849  config = GMAC->GMAC_NCFGR;
850 
851  //1000BASE-T operation mode?
852  if(interface->linkSpeed == NIC_LINK_SPEED_1GBPS)
853  {
854  config |= GMAC_NCFGR_GBE;
855  config &= ~GMAC_NCFGR_SPD;
856  }
857  //100BASE-TX operation mode?
858  else if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
859  {
860  config &= ~GMAC_NCFGR_GBE;
861  config |= GMAC_NCFGR_SPD;
862  }
863  //10BASE-T operation mode?
864  else
865  {
866  config &= ~GMAC_NCFGR_GBE;
867  config &= ~GMAC_NCFGR_SPD;
868  }
869 
870  //Half-duplex or full-duplex mode?
871  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
872  {
873  config |= GMAC_NCFGR_FD;
874  }
875  else
876  {
877  config &= ~GMAC_NCFGR_FD;
878  }
879 
880  //Write configuration value back to NCFGR register
881  GMAC->GMAC_NCFGR = config;
882 
883  //Successful processing
884  return NO_ERROR;
885 }
886 
887 
888 /**
889  * @brief Write PHY register
890  * @param[in] opcode Access type (2 bits)
891  * @param[in] phyAddr PHY address (5 bits)
892  * @param[in] regAddr Register address (5 bits)
893  * @param[in] data Register value
894  **/
895 
896 void sama5d3Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr,
897  uint8_t regAddr, uint16_t data)
898 {
899  uint32_t temp;
900 
901  //Valid opcode?
902  if(opcode == SMI_OPCODE_WRITE)
903  {
904  //Set up a write operation
905  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
906  //PHY address
907  temp |= GMAC_MAN_PHYA(phyAddr);
908  //Register address
909  temp |= GMAC_MAN_REGA(regAddr);
910  //Register value
911  temp |= GMAC_MAN_DATA(data);
912 
913  //Start a write operation
914  GMAC->GMAC_MAN = temp;
915  //Wait for the write to complete
916  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
917  {
918  }
919  }
920  else
921  {
922  //The MAC peripheral only supports standard Clause 22 opcodes
923  }
924 }
925 
926 
927 /**
928  * @brief Read PHY register
929  * @param[in] opcode Access type (2 bits)
930  * @param[in] phyAddr PHY address (5 bits)
931  * @param[in] regAddr Register address (5 bits)
932  * @return Register value
933  **/
934 
935 uint16_t sama5d3Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
936  uint8_t regAddr)
937 {
938  uint16_t data;
939  uint32_t temp;
940 
941  //Valid opcode?
942  if(opcode == SMI_OPCODE_READ)
943  {
944  //Set up a read operation
945  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
946  //PHY address
947  temp |= GMAC_MAN_PHYA(phyAddr);
948  //Register address
949  temp |= GMAC_MAN_REGA(regAddr);
950 
951  //Start a read operation
952  GMAC->GMAC_MAN = temp;
953  //Wait for the read to complete
954  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
955  {
956  }
957 
958  //Get register value
959  data = GMAC->GMAC_MAN & GMAC_MAN_DATA_Msk;
960  }
961  else
962  {
963  //The MAC peripheral only supports standard Clause 22 opcodes
964  data = 0;
965  }
966 
967  //Return the value of the PHY register
968  return data;
969 }
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:53
#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
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 SMI_OPCODE_WRITE
Definition: nic.h:66
#define SAMA5D3_ETH2_RX_BUFFER_SIZE
#define GMAC_TX_USED
#define FALSE
Definition: os_port.h:46
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
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:104
@ 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:141
void sama5d3Eth2Tick(NetInterface *interface)
SAMA5D3 Ethernet MAC timer handler.
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t length
Definition: tcp.h:368
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:107
#define GMAC_RGMII_MASK
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.
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:50
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
#define GMAC_RX_OWNERSHIP
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
@ 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