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