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