mcimx6ul_eth2_driver.c
Go to the documentation of this file.
1 /**
2  * @file mcimx6ul_eth2_driver.c
3  * @brief NXP i.MX6UL Ethernet MAC driver (ENET2 instance)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "fsl_device_registers.h"
36 #include "fsl_gpio.h"
37 #include "fsl_iomuxc.h"
38 #include "core/net.h"
40 #include "debug.h"
41 
42 //Underlying network interface
43 static NetInterface *nicDriverInterface;
44 
45 //IAR EWARM compiler?
46 #if defined(__ICCARM__)
47 
48 //TX buffer
49 #pragma data_alignment = 64
50 #pragma location = MCIMX6UL_ETH2_RAM_SECTION
52 //RX buffer
53 #pragma data_alignment = 64
54 #pragma location = MCIMX6UL_ETH2_RAM_SECTION
56 //TX buffer descriptors
57 #pragma data_alignment = 64
58 #pragma location = MCIMX6UL_ETH2_RAM_SECTION
59 static uint32_t txBufferDesc[MCIMX6UL_ETH2_TX_BUFFER_COUNT][8];
60 //RX buffer descriptors
61 #pragma data_alignment = 64
62 #pragma location = MCIMX6UL_ETH2_RAM_SECTION
63 static uint32_t rxBufferDesc[MCIMX6UL_ETH2_RX_BUFFER_COUNT][8];
64 
65 //ARM or GCC compiler?
66 #else
67 
68 //TX buffer
70  __attribute__((aligned(64), __section__(MCIMX6UL_ETH2_RAM_SECTION)));
71 //RX buffer
73  __attribute__((aligned(64), __section__(MCIMX6UL_ETH2_RAM_SECTION)));
74 //TX buffer descriptors
75 static uint32_t txBufferDesc[MCIMX6UL_ETH2_TX_BUFFER_COUNT][8]
76  __attribute__((aligned(64), __section__(MCIMX6UL_ETH2_RAM_SECTION)));
77 //RX buffer descriptors
78 static uint32_t rxBufferDesc[MCIMX6UL_ETH2_RX_BUFFER_COUNT][8]
79  __attribute__((aligned(64), __section__(MCIMX6UL_ETH2_RAM_SECTION)));
80 
81 #endif
82 
83 //TX buffer index
84 static uint_t txBufferIndex;
85 //RX buffer index
86 static uint_t rxBufferIndex;
87 
88 
89 /**
90  * @brief i.MX6UL Ethernet MAC driver (ENET2 instance)
91  **/
92 
94 {
96  ETH_MTU,
107  TRUE,
108  TRUE,
109  TRUE,
110  FALSE
111 };
112 
113 
114 /**
115  * @brief i.MX6UL Ethernet MAC initialization
116  * @param[in] interface Underlying network interface
117  * @return Error code
118  **/
119 
121 {
122  error_t error;
123  uint32_t value;
124 
125  //Debug message
126  TRACE_INFO("Initializing i.MX6UL Ethernet MAC (ENET2)...\r\n");
127 
128  //Save underlying network interface
129  nicDriverInterface = interface;
130 
131  //Enable ENET peripheral clock
132  CLOCK_EnableClock(kCLOCK_Enet);
133 
134  //GPIO configuration
135  mcimx6ulEth2InitGpio(interface);
136 
137  //Reset ENET2 module
138  ENET2->ECR = ENET_ECR_RESET_MASK;
139  //Wait for the reset to complete
140  while((ENET2->ECR & ENET_ECR_RESET_MASK) != 0)
141  {
142  }
143 
144  //Receive control register
145  ENET2->RCR = ENET_RCR_MAX_FL(MCIMX6UL_ETH2_RX_BUFFER_SIZE) |
146  ENET_RCR_RMII_MODE_MASK | ENET_RCR_MII_MODE_MASK;
147 
148  //Transmit control register
149  ENET2->TCR = 0;
150  //Configure MDC clock frequency
151  ENET2->MSCR = ENET_MSCR_HOLDTIME(10) | ENET_MSCR_MII_SPEED(120);
152 
153  //Valid Ethernet PHY or switch driver?
154  if(interface->phyDriver != NULL)
155  {
156  //Ethernet PHY initialization
157  error = interface->phyDriver->init(interface);
158  }
159  else if(interface->switchDriver != NULL)
160  {
161  //Ethernet switch initialization
162  error = interface->switchDriver->init(interface);
163  }
164  else
165  {
166  //The interface is not properly configured
167  error = ERROR_FAILURE;
168  }
169 
170  //Any error to report?
171  if(error)
172  {
173  return error;
174  }
175 
176  //Set the MAC address of the station (upper 16 bits)
177  value = interface->macAddr.b[5];
178  value |= (interface->macAddr.b[4] << 8);
179  ENET2->PAUR = ENET_PAUR_PADDR2(value) | ENET_PAUR_TYPE(0x8808);
180 
181  //Set the MAC address of the station (lower 32 bits)
182  value = interface->macAddr.b[3];
183  value |= (interface->macAddr.b[2] << 8);
184  value |= (interface->macAddr.b[1] << 16);
185  value |= (interface->macAddr.b[0] << 24);
186  ENET2->PALR = ENET_PALR_PADDR1(value);
187 
188  //Hash table for unicast address filtering
189  ENET2->IALR = 0;
190  ENET2->IAUR = 0;
191  //Hash table for multicast address filtering
192  ENET2->GALR = 0;
193  ENET2->GAUR = 0;
194 
195  //Disable transmit accelerator functions
196  ENET2->TACC = 0;
197  //Disable receive accelerator functions
198  ENET2->RACC = 0;
199 
200  //Use enhanced buffer descriptors
201  ENET2->ECR = ENET_ECR_DBSWP_MASK | ENET_ECR_EN1588_MASK;
202 
203  //Reset statistics counters
204  ENET2->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
205  ENET2->MIBC = 0;
206 
207  //Initialize buffer descriptors
208  mcimx6ulEth2InitBufferDesc(interface);
209 
210  //Clear any pending interrupts
211  ENET2->EIR = 0xFFFFFFFF;
212  //Enable desired interrupts
213  ENET2->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
214 
215  //Configure ENET2 interrupt priority
216  GIC_SetPriority(ENET2_IRQn, MCIMX6UL_ETH2_IRQ_PRIORITY);
217 
218  //Enable Ethernet MAC
219  ENET2->ECR |= ENET_ECR_ETHEREN_MASK;
220  //Instruct the DMA to poll the receive descriptor list
221  ENET2->RDAR = ENET_RDAR_RDAR_MASK;
222 
223  //Accept any packets from the upper layer
224  osSetEvent(&interface->nicTxEvent);
225 
226  //Successful initialization
227  return NO_ERROR;
228 }
229 
230 
231 /**
232  * @brief GPIO configuration
233  * @param[in] interface Underlying network interface
234  **/
235 
236 __weak_func void mcimx6ulEth2InitGpio(NetInterface *interface)
237 {
238 //MCIMX6UL-EVKB or MCIMX6ULL-EVK evaluation board?
239 #if defined(USE_MCIMX6UL_EVKB) || defined(USE_MCIMX6ULL_EVK)
240  gpio_pin_config_t pinConfig;
241 
242  //Enable ENET2_TX_CLK output driver
243  IOMUXC_GPR->GPR1 |= IOMUXC_GPR_GPR1_ENET2_TX_CLK_DIR(1);
244 
245  //Enable IOMUXC clock
246  CLOCK_EnableClock(kCLOCK_Iomuxc);
247 
248  //Configure ENET2_TX_CLK pin as ENET2_REF_CLK2
249  IOMUXC_SetPinMux(IOMUXC_ENET2_TX_CLK_ENET2_REF_CLK2, 1);
250 
251  //Set ENET2_TX_CLK pad properties
252  IOMUXC_SetPinConfig(IOMUXC_ENET2_TX_CLK_ENET2_REF_CLK2,
253  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
254  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
255  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
256  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
257  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
258  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
259  IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
260  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
261 
262  //Configure ENET2_TX_EN pin as ENET2_TX_EN
263  IOMUXC_SetPinMux(IOMUXC_ENET2_TX_EN_ENET2_TX_EN, 0);
264 
265  //Set ENET2_TX_EN pad properties
266  IOMUXC_SetPinConfig(IOMUXC_ENET2_TX_EN_ENET2_TX_EN,
267  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
268  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
269  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
270  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
271  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
272  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
273  IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
274  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
275 
276  //Configure ENET2_TX_DATA0 pin as ENET2_TDATA00
277  IOMUXC_SetPinMux(IOMUXC_ENET2_TX_DATA0_ENET2_TDATA00, 0);
278 
279  //Set ENET2_TX_DATA0 pad properties
280  IOMUXC_SetPinConfig(IOMUXC_ENET2_TX_DATA0_ENET2_TDATA00,
281  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
282  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
283  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
284  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
285  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
286  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
287  IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
288  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
289 
290  //Configure ENET2_TX_DATA1 pin as ENET2_TDATA01
291  IOMUXC_SetPinMux(IOMUXC_ENET2_TX_DATA1_ENET2_TDATA01, 0);
292 
293  //Set ENET2_TX_DATA1 pad properties
294  IOMUXC_SetPinConfig(IOMUXC_ENET2_TX_DATA1_ENET2_TDATA01,
295  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
296  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
297  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
298  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
299  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
300  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
301  IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
302  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
303 
304  //Configure ENET2_RX_EN pin as ENET2_RX_EN
305  IOMUXC_SetPinMux(IOMUXC_ENET2_RX_EN_ENET2_RX_EN, 0);
306 
307  //Set ENET2_RX_EN pad properties
308  IOMUXC_SetPinConfig(IOMUXC_ENET2_RX_EN_ENET2_RX_EN,
309  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
310  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
311  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
312  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
313  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
314  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
315  IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
316  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
317 
318  //Configure ENET2_RX_ER pin as ENET2_RX_ER
319  IOMUXC_SetPinMux(IOMUXC_ENET2_RX_ER_ENET2_RX_ER, 0);
320 
321  //Set ENET2_RX_ER pad properties
322  IOMUXC_SetPinConfig(IOMUXC_ENET2_RX_ER_ENET2_RX_ER,
323  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
324  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
325  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
326  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
327  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
328  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
329  IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
330  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
331 
332  //Configure ENET2_RX_DATA0 pin as ENET2_RDATA00
333  IOMUXC_SetPinMux(IOMUXC_ENET2_RX_DATA0_ENET2_RDATA00, 0);
334 
335  //Set ENET2_RX_DATA0 pad properties
336  IOMUXC_SetPinConfig(IOMUXC_ENET2_RX_DATA0_ENET2_RDATA00,
337  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
338  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
339  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
340  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
341  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
342  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
343  IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
344  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
345 
346  //Configure ENET2_RX_DATA1 pin as ENET2_RDATA01
347  IOMUXC_SetPinMux(IOMUXC_ENET2_RX_DATA1_ENET2_RDATA01, 0);
348 
349  //Set ENET2_RX_DATA1 pad properties
350  IOMUXC_SetPinConfig(IOMUXC_ENET2_RX_DATA1_ENET2_RDATA01,
351  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
352  IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
353  IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
354  IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
355  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
356  IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
357  IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
358  IOMUXC_SW_PAD_CTL_PAD_SRE(1));
359 
360 #if defined(USE_MCIMX6UL_EVKB)
361  //Configure SNVS_TAMPER6 pin as GPIO5_IO06
362  IOMUXC_SetPinMux(IOMUXC_SNVS_TAMPER6_GPIO5_IO06, 0);
363 
364  //Set SNVS_TAMPER5 pad properties
365  IOMUXC_SetPinConfig(IOMUXC_SNVS_TAMPER6_GPIO5_IO06,
366  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
367  IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
368  IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
369  IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
370  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
371  IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
372  IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
373  IOMUXC_SW_PAD_CTL_PAD_SRE(0));
374 
375 #elif defined(USE_MCIMX6ULL_EVK)
376  //Configure SNVS_TAMPER6 pin as GPIO5_IO06
377  IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER6_GPIO5_IO06, 0);
378 
379  //Set SNVS_TAMPER5 pad properties
380  IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER6_GPIO5_IO06,
381  IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
382  IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
383  IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
384  IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
385  IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
386  IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
387  IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
388  IOMUXC_SW_PAD_CTL_PAD_SRE(0));
389 #endif
390 
391  //Configure ENET2_INT as an input
392  pinConfig.direction = kGPIO_DigitalInput;
393  pinConfig.outputLogic = 0;
394  pinConfig.interruptMode = kGPIO_NoIntmode;
395  GPIO_PinInit(GPIO5, 6, &pinConfig);
396 #endif
397 }
398 
399 
400 /**
401  * @brief Initialize buffer descriptors
402  * @param[in] interface Underlying network interface
403  **/
404 
406 {
407  uint_t i;
408  uint32_t address;
409 
410  //Clear TX and RX buffer descriptors
411  osMemset(txBufferDesc, 0, sizeof(txBufferDesc));
412  osMemset(rxBufferDesc, 0, sizeof(rxBufferDesc));
413 
414  //Initialize TX buffer descriptors
415  for(i = 0; i < MCIMX6UL_ETH2_TX_BUFFER_COUNT; i++)
416  {
417  //Calculate the address of the current TX buffer
418  address = (uint32_t) txBuffer[i];
419  //Transmit buffer address
420  txBufferDesc[i][1] = address;
421  //Generate interrupts
422  txBufferDesc[i][2] = ENET_TBD2_INT;
423  }
424 
425  //Mark the last descriptor entry with the wrap flag
426  txBufferDesc[i - 1][0] |= ENET_TBD0_W;
427  //Initialize TX buffer index
428  txBufferIndex = 0;
429 
430  //Initialize RX buffer descriptors
431  for(i = 0; i < MCIMX6UL_ETH2_RX_BUFFER_COUNT; i++)
432  {
433  //Calculate the address of the current RX buffer
434  address = (uint32_t) rxBuffer[i];
435  //The descriptor is initially owned by the DMA
436  rxBufferDesc[i][0] = ENET_RBD0_E;
437  //Receive buffer address
438  rxBufferDesc[i][1] = address;
439  //Generate interrupts
440  rxBufferDesc[i][2] = ENET_RBD2_INT;
441  }
442 
443  //Mark the last descriptor entry with the wrap flag
444  rxBufferDesc[i - 1][0] |= ENET_RBD0_W;
445  //Initialize RX buffer index
446  rxBufferIndex = 0;
447 
448  //Start location of the TX descriptor list
449  ENET2->TDSR = (uint32_t) txBufferDesc;
450  //Start location of the RX descriptor list
451  ENET2->RDSR = (uint32_t) rxBufferDesc;
452  //Maximum receive buffer size
453  ENET2->MRBR = MCIMX6UL_ETH2_RX_BUFFER_SIZE;
454 }
455 
456 
457 /**
458  * @brief i.MX6UL Ethernet MAC timer handler
459  *
460  * This routine is periodically called by the TCP/IP stack to handle periodic
461  * operations such as polling the link state
462  *
463  * @param[in] interface Underlying network interface
464  **/
465 
467 {
468  //Valid Ethernet PHY or switch driver?
469  if(interface->phyDriver != NULL)
470  {
471  //Handle periodic operations
472  interface->phyDriver->tick(interface);
473  }
474  else if(interface->switchDriver != NULL)
475  {
476  //Handle periodic operations
477  interface->switchDriver->tick(interface);
478  }
479  else
480  {
481  //Just for sanity
482  }
483 }
484 
485 
486 /**
487  * @brief Enable interrupts
488  * @param[in] interface Underlying network interface
489  **/
490 
492 {
493  //Enable Ethernet MAC interrupts
494  GIC_EnableIRQ(ENET2_IRQn);
495 
496  //Valid Ethernet PHY or switch driver?
497  if(interface->phyDriver != NULL)
498  {
499  //Enable Ethernet PHY interrupts
500  interface->phyDriver->enableIrq(interface);
501  }
502  else if(interface->switchDriver != NULL)
503  {
504  //Enable Ethernet switch interrupts
505  interface->switchDriver->enableIrq(interface);
506  }
507  else
508  {
509  //Just for sanity
510  }
511 }
512 
513 
514 /**
515  * @brief Disable interrupts
516  * @param[in] interface Underlying network interface
517  **/
518 
520 {
521  //Disable Ethernet MAC interrupts
522  GIC_DisableIRQ(ENET2_IRQn);
523 
524  //Valid Ethernet PHY or switch driver?
525  if(interface->phyDriver != NULL)
526  {
527  //Disable Ethernet PHY interrupts
528  interface->phyDriver->disableIrq(interface);
529  }
530  else if(interface->switchDriver != NULL)
531  {
532  //Disable Ethernet switch interrupts
533  interface->switchDriver->disableIrq(interface);
534  }
535  else
536  {
537  //Just for sanity
538  }
539 }
540 
541 
542 /**
543  * @brief Ethernet MAC interrupt (ENET2 instance)
544  * @param[in] giccIar Value of the GICC_IAR register
545  * @param[in] userParam User parameter
546  **/
547 
548 void ENET2_DriverIRQHandler(uint32_t giccIar, void *userParam)
549 {
550  bool_t flag;
551  uint32_t events;
552 
553  //Interrupt service routine prologue
554  osEnterIsr();
555 
556  //This flag will be set if a higher priority task must be woken
557  flag = FALSE;
558  //Read interrupt event register
559  events = ENET2->EIR;
560 
561  //Packet transmitted?
562  if((events & ENET_EIR_TXF_MASK) != 0)
563  {
564  //Clear TXF interrupt flag
565  ENET2->EIR = ENET_EIR_TXF_MASK;
566 
567  //Check whether the TX buffer is available for writing
568  if((txBufferDesc[txBufferIndex][0] & ENET_TBD0_R) == 0)
569  {
570  //Notify the TCP/IP stack that the transmitter is ready to send
571  flag = osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
572  }
573 
574  //Instruct the DMA to poll the transmit descriptor list
575  ENET2->TDAR = ENET_TDAR_TDAR_MASK;
576  }
577 
578  //Packet received?
579  if((events & ENET_EIR_RXF_MASK) != 0)
580  {
581  //Disable RXF interrupt
582  ENET2->EIMR &= ~ENET_EIMR_RXF_MASK;
583 
584  //Set event flag
585  nicDriverInterface->nicEvent = TRUE;
586  //Notify the TCP/IP stack of the event
587  flag = osSetEventFromIsr(&netEvent);
588  }
589 
590  //System bus error?
591  if((events & ENET_EIR_EBERR_MASK) != 0)
592  {
593  //Disable EBERR interrupt
594  ENET2->EIMR &= ~ENET_EIMR_EBERR_MASK;
595 
596  //Set event flag
597  nicDriverInterface->nicEvent = TRUE;
598  //Notify the TCP/IP stack of the event
599  flag |= osSetEventFromIsr(&netEvent);
600  }
601 
602  //Interrupt service routine epilogue
603  osExitIsr(flag);
604 }
605 
606 
607 /**
608  * @brief i.MX6UL Ethernet MAC event handler
609  * @param[in] interface Underlying network interface
610  **/
611 
613 {
614  error_t error;
615  uint32_t status;
616 
617  //Read interrupt event register
618  status = ENET2->EIR;
619 
620  //Packet received?
621  if((status & ENET_EIR_RXF_MASK) != 0)
622  {
623  //Clear RXF interrupt flag
624  ENET2->EIR = ENET_EIR_RXF_MASK;
625 
626  //Process all pending packets
627  do
628  {
629  //Read incoming packet
630  error = mcimx6ulEth2ReceivePacket(interface);
631 
632  //No more data in the receive buffer?
633  } while(error != ERROR_BUFFER_EMPTY);
634  }
635 
636  //System bus error?
637  if((status & ENET_EIR_EBERR_MASK) != 0)
638  {
639  //Clear EBERR interrupt flag
640  ENET2->EIR = ENET_EIR_EBERR_MASK;
641 
642  //Disable Ethernet MAC
643  ENET2->ECR &= ~ENET_ECR_ETHEREN_MASK;
644  //Reset buffer descriptors
645  mcimx6ulEth2InitBufferDesc(interface);
646  //Resume normal operation
647  ENET2->ECR |= ENET_ECR_ETHEREN_MASK;
648  //Instruct the DMA to poll the receive descriptor list
649  ENET2->RDAR = ENET_RDAR_RDAR_MASK;
650  }
651 
652  //Re-enable Ethernet MAC interrupts
653  ENET2->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
654 }
655 
656 
657 /**
658  * @brief Send a packet
659  * @param[in] interface Underlying network interface
660  * @param[in] buffer Multi-part buffer containing the data to send
661  * @param[in] offset Offset to the first data byte
662  * @param[in] ancillary Additional options passed to the stack along with
663  * the packet
664  * @return Error code
665  **/
666 
668  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
669 {
670  static uint32_t temp[MCIMX6UL_ETH2_TX_BUFFER_SIZE / 4];
671  size_t length;
672 
673  //Retrieve the length of the packet
674  length = netBufferGetLength(buffer) - offset;
675 
676  //Check the frame length
678  {
679  //The transmitter can accept another packet
680  osSetEvent(&interface->nicTxEvent);
681  //Report an error
682  return ERROR_INVALID_LENGTH;
683  }
684 
685  //Make sure the current buffer is available for writing
686  if((txBufferDesc[txBufferIndex][0] & ENET_TBD0_R) != 0)
687  {
688  return ERROR_FAILURE;
689  }
690 
691  //Copy user data to the transmit buffer
692  netBufferRead(temp, buffer, offset, length);
693  osMemcpy(txBuffer[txBufferIndex], temp, (length + 3) & ~3UL);
694 
695  //Clear BDU flag
696  txBufferDesc[txBufferIndex][4] = 0;
697 
698  //Check current index
699  if(txBufferIndex < (MCIMX6UL_ETH2_TX_BUFFER_COUNT - 1))
700  {
701  //Give the ownership of the descriptor to the DMA engine
702  txBufferDesc[txBufferIndex][0] = ENET_TBD0_R | ENET_TBD0_L |
704 
705  //Point to the next buffer
706  txBufferIndex++;
707  }
708  else
709  {
710  //Give the ownership of the descriptor to the DMA engine
711  txBufferDesc[txBufferIndex][0] = ENET_TBD0_R | ENET_TBD0_W |
713 
714  //Wrap around
715  txBufferIndex = 0;
716  }
717 
718  //Data synchronization barrier
719  __DSB();
720 
721  //Instruct the DMA to poll the transmit descriptor list
722  ENET2->TDAR = ENET_TDAR_TDAR_MASK;
723 
724  //Check whether the next buffer is available for writing
725  if((txBufferDesc[txBufferIndex][0] & ENET_TBD0_R) == 0)
726  {
727  //The transmitter can accept another packet
728  osSetEvent(&interface->nicTxEvent);
729  }
730 
731  //Successful processing
732  return NO_ERROR;
733 }
734 
735 
736 /**
737  * @brief Receive a packet
738  * @param[in] interface Underlying network interface
739  * @return Error code
740  **/
741 
743 {
744  static uint32_t temp[MCIMX6UL_ETH2_RX_BUFFER_SIZE / 4];
745  error_t error;
746  size_t n;
747  NetRxAncillary ancillary;
748 
749  //Current buffer available for reading?
750  if((rxBufferDesc[rxBufferIndex][0] & ENET_RBD0_E) == 0)
751  {
752  //The frame should not span multiple buffers
753  if((rxBufferDesc[rxBufferIndex][0] & ENET_RBD0_L) != 0)
754  {
755  //Check whether an error occurred
756  if((rxBufferDesc[rxBufferIndex][0] & (ENET_RBD0_LG | ENET_RBD0_NO |
758  {
759  //Retrieve the length of the frame
760  n = rxBufferDesc[rxBufferIndex][0] & ENET_RBD0_DATA_LENGTH;
761  //Limit the number of data to read
763 
764  //Copy data from the receive buffer
765  osMemcpy(temp, rxBuffer[rxBufferIndex], (n + 3) & ~3UL);
766 
767  //Additional options can be passed to the stack along with the packet
768  ancillary = NET_DEFAULT_RX_ANCILLARY;
769 
770  //Pass the packet to the upper layer
771  nicProcessPacket(interface, (uint8_t *) temp, n, &ancillary);
772 
773  //Valid packet received
774  error = NO_ERROR;
775  }
776  else
777  {
778  //The received packet contains an error
779  error = ERROR_INVALID_PACKET;
780  }
781  }
782  else
783  {
784  //The packet is not valid
785  error = ERROR_INVALID_PACKET;
786  }
787 
788  //Clear BDU flag
789  rxBufferDesc[rxBufferIndex][4] = 0;
790 
791  //Check current index
792  if(rxBufferIndex < (MCIMX6UL_ETH2_RX_BUFFER_COUNT - 1))
793  {
794  //Give the ownership of the descriptor back to the DMA engine
795  rxBufferDesc[rxBufferIndex][0] = ENET_RBD0_E;
796  //Point to the next buffer
797  rxBufferIndex++;
798  }
799  else
800  {
801  //Give the ownership of the descriptor back to the DMA engine
802  rxBufferDesc[rxBufferIndex][0] = ENET_RBD0_E | ENET_RBD0_W;
803  //Wrap around
804  rxBufferIndex = 0;
805  }
806 
807  //Instruct the DMA to poll the receive descriptor list
808  ENET2->RDAR = ENET_RDAR_RDAR_MASK;
809  }
810  else
811  {
812  //No more data in the receive buffer
813  error = ERROR_BUFFER_EMPTY;
814  }
815 
816  //Return status code
817  return error;
818 }
819 
820 
821 /**
822  * @brief Configure MAC address filtering
823  * @param[in] interface Underlying network interface
824  * @return Error code
825  **/
826 
828 {
829  uint_t i;
830  uint_t k;
831  uint32_t crc;
832  uint32_t value;
833  uint32_t unicastHashTable[2];
834  uint32_t multicastHashTable[2];
835  MacFilterEntry *entry;
836 
837  //Debug message
838  TRACE_DEBUG("Updating MAC filter...\r\n");
839 
840  //Set the MAC address of the station (upper 16 bits)
841  value = interface->macAddr.b[5];
842  value |= (interface->macAddr.b[4] << 8);
843  ENET2->PAUR = ENET_PAUR_PADDR2(value) | ENET_PAUR_TYPE(0x8808);
844 
845  //Set the MAC address of the station (lower 32 bits)
846  value = interface->macAddr.b[3];
847  value |= (interface->macAddr.b[2] << 8);
848  value |= (interface->macAddr.b[1] << 16);
849  value |= (interface->macAddr.b[0] << 24);
850  ENET2->PALR = ENET_PALR_PADDR1(value);
851 
852  //Clear hash table (unicast address filtering)
853  unicastHashTable[0] = 0;
854  unicastHashTable[1] = 0;
855 
856  //Clear hash table (multicast address filtering)
857  multicastHashTable[0] = 0;
858  multicastHashTable[1] = 0;
859 
860  //The MAC address filter contains the list of MAC addresses to accept
861  //when receiving an Ethernet frame
862  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
863  {
864  //Point to the current entry
865  entry = &interface->macAddrFilter[i];
866 
867  //Valid entry?
868  if(entry->refCount > 0)
869  {
870  //Compute CRC over the current MAC address
871  crc = mcimx6ulEth2CalcCrc(&entry->addr, sizeof(MacAddr));
872 
873  //The upper 6 bits in the CRC register are used to index the
874  //contents of the hash table
875  k = (crc >> 26) & 0x3F;
876 
877  //Multicast address?
878  if(macIsMulticastAddr(&entry->addr))
879  {
880  //Update the multicast hash table
881  multicastHashTable[k / 32] |= (1 << (k % 32));
882  }
883  else
884  {
885  //Update the unicast hash table
886  unicastHashTable[k / 32] |= (1 << (k % 32));
887  }
888  }
889  }
890 
891  //Write the hash table (unicast address filtering)
892  ENET2->IALR = unicastHashTable[0];
893  ENET2->IAUR = unicastHashTable[1];
894 
895  //Write the hash table (multicast address filtering)
896  ENET2->GALR = multicastHashTable[0];
897  ENET2->GAUR = multicastHashTable[1];
898 
899  //Debug message
900  TRACE_DEBUG(" IALR = %08" PRIX32 "\r\n", ENET2->IALR);
901  TRACE_DEBUG(" IAUR = %08" PRIX32 "\r\n", ENET2->IAUR);
902  TRACE_DEBUG(" GALR = %08" PRIX32 "\r\n", ENET2->GALR);
903  TRACE_DEBUG(" GAUR = %08" PRIX32 "\r\n", ENET2->GAUR);
904 
905  //Successful processing
906  return NO_ERROR;
907 }
908 
909 
910 /**
911  * @brief Adjust MAC configuration parameters for proper operation
912  * @param[in] interface Underlying network interface
913  * @return Error code
914  **/
915 
917 {
918  //Disable Ethernet MAC while modifying configuration registers
919  ENET2->ECR &= ~ENET_ECR_ETHEREN_MASK;
920 
921  //10BASE-T or 100BASE-TX operation mode?
922  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
923  {
924  //100 Mbps operation
925  ENET2->RCR &= ~ENET_RCR_RMII_10T_MASK;
926  }
927  else
928  {
929  //10 Mbps operation
930  ENET2->RCR |= ENET_RCR_RMII_10T_MASK;
931  }
932 
933  //Half-duplex or full-duplex mode?
934  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
935  {
936  //Full-duplex mode
937  ENET2->TCR |= ENET_TCR_FDEN_MASK;
938  //Receive path operates independently of transmit
939  ENET2->RCR &= ~ENET_RCR_DRT_MASK;
940  }
941  else
942  {
943  //Half-duplex mode
944  ENET2->TCR &= ~ENET_TCR_FDEN_MASK;
945  //Disable reception of frames while transmitting
946  ENET2->RCR |= ENET_RCR_DRT_MASK;
947  }
948 
949  //Reset buffer descriptors
950  mcimx6ulEth2InitBufferDesc(interface);
951 
952  //Re-enable Ethernet MAC
953  ENET2->ECR |= ENET_ECR_ETHEREN_MASK;
954  //Instruct the DMA to poll the receive descriptor list
955  ENET2->RDAR = ENET_RDAR_RDAR_MASK;
956 
957  //Successful processing
958  return NO_ERROR;
959 }
960 
961 
962 /**
963  * @brief Write PHY register
964  * @param[in] opcode Access type (2 bits)
965  * @param[in] phyAddr PHY address (5 bits)
966  * @param[in] regAddr Register address (5 bits)
967  * @param[in] data Register value
968  **/
969 
970 void mcimx6ulEth2WritePhyReg(uint8_t opcode, uint8_t phyAddr,
971  uint8_t regAddr, uint16_t data)
972 {
973  uint32_t temp;
974 
975  //Valid opcode?
976  if(opcode == SMI_OPCODE_WRITE)
977  {
978  //Set up a write operation
979  temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
980  //PHY address
981  temp |= ENET_MMFR_PA(phyAddr);
982  //Register address
983  temp |= ENET_MMFR_RA(regAddr);
984  //Register value
985  temp |= ENET_MMFR_DATA(data);
986 
987  //Clear MII interrupt flag
988  ENET2->EIR = ENET_EIR_MII_MASK;
989  //Start a write operation
990  ENET2->MMFR = temp;
991 
992  //Wait for the write to complete
993  while((ENET2->EIR & ENET_EIR_MII_MASK) == 0)
994  {
995  }
996  }
997  else
998  {
999  //The MAC peripheral only supports standard Clause 22 opcodes
1000  }
1001 }
1002 
1003 
1004 /**
1005  * @brief Read PHY register
1006  * @param[in] opcode Access type (2 bits)
1007  * @param[in] phyAddr PHY address (5 bits)
1008  * @param[in] regAddr Register address (5 bits)
1009  * @return Register value
1010  **/
1011 
1012 uint16_t mcimx6ulEth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
1013  uint8_t regAddr)
1014 {
1015  uint16_t data;
1016  uint32_t temp;
1017 
1018  //Valid opcode?
1019  if(opcode == SMI_OPCODE_READ)
1020  {
1021  //Set up a read operation
1022  temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1023  //PHY address
1024  temp |= ENET_MMFR_PA(phyAddr);
1025  //Register address
1026  temp |= ENET_MMFR_RA(regAddr);
1027 
1028  //Clear MII interrupt flag
1029  ENET2->EIR = ENET_EIR_MII_MASK;
1030  //Start a read operation
1031  ENET2->MMFR = temp;
1032 
1033  //Wait for the read to complete
1034  while((ENET2->EIR & ENET_EIR_MII_MASK) == 0)
1035  {
1036  }
1037 
1038  //Get register value
1039  data = ENET2->MMFR & ENET_MMFR_DATA_MASK;
1040  }
1041  else
1042  {
1043  //The MAC peripheral only supports standard Clause 22 opcodes
1044  data = 0;
1045  }
1046 
1047  //Return the value of the PHY register
1048  return data;
1049 }
1050 
1051 
1052 /**
1053  * @brief CRC calculation
1054  * @param[in] data Pointer to the data over which to calculate the CRC
1055  * @param[in] length Number of bytes to process
1056  * @return Resulting CRC value
1057  **/
1058 
1059 uint32_t mcimx6ulEth2CalcCrc(const void *data, size_t length)
1060 {
1061  uint_t i;
1062  uint_t j;
1063  uint32_t crc;
1064  const uint8_t *p;
1065 
1066  //Point to the data over which to calculate the CRC
1067  p = (uint8_t *) data;
1068  //CRC preset value
1069  crc = 0xFFFFFFFF;
1070 
1071  //Loop through data
1072  for(i = 0; i < length; i++)
1073  {
1074  //Update CRC value
1075  crc ^= p[i];
1076 
1077  //The message is processed bit by bit
1078  for(j = 0; j < 8; j++)
1079  {
1080  if((crc & 0x01) != 0)
1081  {
1082  crc = (crc >> 1) ^ 0xEDB88320;
1083  }
1084  else
1085  {
1086  crc = crc >> 1;
1087  }
1088  }
1089  }
1090 
1091  //Return CRC value
1092  return crc;
1093 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define MCIMX6UL_ETH2_TX_BUFFER_COUNT
uint8_t opcode
Definition: dns_common.h:188
uint32_t mcimx6ulEth2CalcCrc(const void *data, size_t length)
CRC calculation.
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define ENET_TBD0_L
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
#define ENET_RBD0_DATA_LENGTH
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
error_t mcimx6ulEth2SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
void mcimx6ulEth2DisableIrq(NetInterface *interface)
Disable interrupts.
#define ENET_TBD0_DATA_LENGTH
error_t mcimx6ulEth2UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define ENET_TBD0_W
#define ENET_TBD0_TC
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 SMI_OPCODE_WRITE
Definition: nic.h:66
error_t mcimx6ulEth2ReceivePacket(NetInterface *interface)
Receive a packet.
#define ENET_RBD0_L
#define FALSE
Definition: os_port.h:46
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
void ENET2_DriverIRQHandler(uint32_t giccIar, void *userParam)
Ethernet MAC interrupt (ENET2 instance)
#define MCIMX6UL_ETH2_IRQ_PRIORITY
#define MCIMX6UL_ETH2_RAM_SECTION
error_t
Error codes.
Definition: error.h:43
NXP i.MX6UL Ethernet MAC driver (ENET2 instance)
#define MCIMX6UL_ETH2_TX_BUFFER_SIZE
const NicDriver mcimx6ulEth2Driver
i.MX6UL Ethernet MAC driver (ENET2 instance)
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
void mcimx6ulEth2EventHandler(NetInterface *interface)
i.MX6UL Ethernet MAC event handler
#define MCIMX6UL_ETH2_RX_BUFFER_SIZE
#define txBuffer
void mcimx6ulEth2EnableIrq(NetInterface *interface)
Enable interrupts.
#define NetRxAncillary
Definition: net_misc.h:40
error_t mcimx6ulEth2Init(NetInterface *interface)
i.MX6UL Ethernet MAC initialization
@ ERROR_INVALID_PACKET
Definition: error.h:140
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
@ ERROR_INVALID_LENGTH
Definition: error.h:111
#define ENET_RBD0_W
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
#define ENET_RBD0_TR
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t length
Definition: tcp.h:368
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define MIN(a, b)
Definition: os_port.h:63
#define rxBuffer
void mcimx6ulEth2Tick(NetInterface *interface)
i.MX6UL Ethernet MAC timer handler
MacAddr
Definition: ethernet.h:195
#define ENET_RBD0_LG
#define MCIMX6UL_ETH2_RX_BUFFER_COUNT
#define TRACE_DEBUG(...)
Definition: debug.h:107
uint16_t regAddr
#define ENET_RBD0_CR
#define ENET_RBD0_OV
#define ETH_MTU
Definition: ethernet.h:116
__weak_func void mcimx6ulEth2InitGpio(NetInterface *interface)
GPIO configuration.
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
Ipv6Addr address[]
Definition: ipv6.h:325
#define osEnterIsr()
void mcimx6ulEth2WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define ENET_TBD0_R
uint8_t value[]
Definition: tcp.h:369
#define ENET_TBD2_INT
#define ENET_RBD0_E
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:50
#define osMemset(p, value, length)
Definition: os_port.h:135
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
uint16_t mcimx6ulEth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define ENET_RBD0_NO
#define ENET_RBD2_INT
error_t mcimx6ulEth2UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
void mcimx6ulEth2InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83