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