32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "fsl_device_registers.h"
37 #include "fsl_iomuxc.h"
46 #if defined(__ICCARM__)
49 #pragma data_alignment = 64
50 #pragma location = MIMXRT1020_ETH_RAM_SECTION
53 #pragma data_alignment = 64
54 #pragma location = MIMXRT1020_ETH_RAM_SECTION
57 #pragma data_alignment = 64
58 #pragma location = MIMXRT1020_ETH_RAM_SECTION
61 #pragma data_alignment = 64
62 #pragma location = MIMXRT1020_ETH_RAM_SECTION
84 static uint_t txBufferIndex;
86 static uint_t rxBufferIndex;
126 TRACE_INFO(
"Initializing i.MX RT1020 Ethernet MAC...\r\n");
129 nicDriverInterface = interface;
132 CLOCK_EnableClock(kCLOCK_Enet);
138 ENET->ECR = ENET_ECR_RESET_MASK;
140 while((ENET->ECR & ENET_ECR_RESET_MASK) != 0)
146 ENET_RCR_RMII_MODE_MASK | ENET_RCR_MII_MODE_MASK;
151 ENET->MSCR = ENET_MSCR_HOLDTIME(10) | ENET_MSCR_MII_SPEED(120);
154 if(interface->phyDriver != NULL)
157 error = interface->phyDriver->init(interface);
159 else if(interface->switchDriver != NULL)
162 error = interface->switchDriver->init(interface);
177 value = interface->macAddr.b[5];
178 value |= (interface->macAddr.b[4] << 8);
179 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
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 ENET->PALR = ENET_PALR_PADDR1(
value);
201 ENET->ECR = ENET_ECR_DBSWP_MASK | ENET_ECR_EN1588_MASK;
204 ENET->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
211 ENET->EIR = 0xFFFFFFFF;
213 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
223 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
225 ENET->RDAR = ENET_RDAR_RDAR_MASK;
243 #if defined(USE_MIMXRT1020_EVK) || defined(USE_MIMXRT1024_EVK)
244 gpio_pin_config_t pinConfig;
245 clock_enet_pll_config_t pllConfig;
248 pllConfig.enableClkOutput =
true;
249 pllConfig.enableClkOutput500M =
false;
250 pllConfig.enableClkOutput25M =
false;
251 pllConfig.loopDivider = 1;
253 CLOCK_InitEnetPll(&pllConfig);
256 IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir,
true);
259 CLOCK_EnableClock(kCLOCK_Iomuxc);
262 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_08_ENET_REF_CLK1, 1);
265 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_08_ENET_REF_CLK1,
266 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
267 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
268 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
269 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
270 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
271 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
272 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
273 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
276 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_09_ENET_RDATA01, 0);
279 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_09_ENET_RDATA01,
280 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
281 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
282 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
283 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
284 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
285 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
286 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
287 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
290 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_10_ENET_RDATA00, 0);
293 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_10_ENET_RDATA00,
294 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
295 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
296 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
297 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
298 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
299 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
300 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
301 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
304 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_11_ENET_RX_EN, 0);
307 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_11_ENET_RX_EN,
308 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
309 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
310 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
311 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
312 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
313 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
314 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
315 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
318 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_12_ENET_RX_ER, 0);
321 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_12_ENET_RX_ER,
322 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
323 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
324 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
325 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
326 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
327 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
328 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
329 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
332 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_13_ENET_TX_EN, 0);
335 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_13_ENET_TX_EN,
336 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
337 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
338 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
339 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
340 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
341 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
342 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
343 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
346 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_14_ENET_TDATA00, 0);
349 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_14_ENET_TDATA00,
350 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
351 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
352 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
353 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
354 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
355 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
356 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
357 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
360 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_15_ENET_TDATA01, 0);
363 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_15_ENET_TDATA01,
364 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
365 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
366 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
367 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
368 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
369 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
370 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
371 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
374 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_40_ENET_MDIO, 0);
377 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_40_ENET_MDIO,
378 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
379 IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
380 IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
381 IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
382 IOMUXC_SW_PAD_CTL_PAD_ODE(1) |
383 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
384 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
385 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
388 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_41_ENET_MDC, 0);
391 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_41_ENET_MDC,
392 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
393 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
394 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
395 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
396 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
397 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
398 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
399 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
402 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_04_GPIO1_IO04, 0);
405 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_04_GPIO1_IO04,
406 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
407 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
408 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
409 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
410 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
411 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
412 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
413 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
416 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_06_GPIO1_IO22, 0);
419 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_06_GPIO1_IO22,
420 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
421 IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
422 IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
423 IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
424 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
425 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
426 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
427 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
430 pinConfig.direction = kGPIO_DigitalOutput;
431 pinConfig.outputLogic = 0;
432 pinConfig.interruptMode = kGPIO_NoIntmode;
433 GPIO_PinInit(GPIO1, 4, &pinConfig);
436 pinConfig.direction = kGPIO_DigitalInput;
437 pinConfig.outputLogic = 0;
438 pinConfig.interruptMode = kGPIO_NoIntmode;
439 GPIO_PinInit(GPIO1, 22, &pinConfig);
442 GPIO_PinWrite(GPIO1, 4, 0);
444 GPIO_PinWrite(GPIO1, 4, 1);
461 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
462 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
499 ENET->TDSR = (uint32_t) txBufferDesc;
501 ENET->RDSR = (uint32_t) rxBufferDesc;
519 if(interface->phyDriver != NULL)
522 interface->phyDriver->tick(interface);
524 else if(interface->switchDriver != NULL)
527 interface->switchDriver->tick(interface);
544 NVIC_EnableIRQ(ENET_IRQn);
547 if(interface->phyDriver != NULL)
550 interface->phyDriver->enableIrq(interface);
552 else if(interface->switchDriver != NULL)
555 interface->switchDriver->enableIrq(interface);
572 NVIC_DisableIRQ(ENET_IRQn);
575 if(interface->phyDriver != NULL)
578 interface->phyDriver->disableIrq(interface);
580 else if(interface->switchDriver != NULL)
583 interface->switchDriver->disableIrq(interface);
610 if((events & ENET_EIR_TXF_MASK) != 0)
613 ENET->EIR = ENET_EIR_TXF_MASK;
616 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
623 ENET->TDAR = ENET_TDAR_TDAR_MASK;
627 if((events & ENET_EIR_RXF_MASK) != 0)
630 ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
633 nicDriverInterface->nicEvent =
TRUE;
639 if((events & ENET_EIR_EBERR_MASK) != 0)
642 ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
645 nicDriverInterface->nicEvent =
TRUE;
669 if((status & ENET_EIR_RXF_MASK) != 0)
672 ENET->EIR = ENET_EIR_RXF_MASK;
685 if((status & ENET_EIR_EBERR_MASK) != 0)
688 ENET->EIR = ENET_EIR_EBERR_MASK;
691 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
695 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
697 ENET->RDAR = ENET_RDAR_RDAR_MASK;
701 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
733 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) != 0)
742 txBufferDesc[txBufferIndex][4] = 0;
768 ENET->TDAR = ENET_TDAR_TDAR_MASK;
771 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
795 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_E) == 0)
798 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_L) != 0)
831 rxBufferDesc[rxBufferIndex][4] = 0;
850 ENET->RDAR = ENET_RDAR_RDAR_MASK;
875 uint32_t unicastHashTable[2];
876 uint32_t multicastHashTable[2];
883 value = interface->macAddr.b[5];
884 value |= (interface->macAddr.b[4] << 8);
885 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
888 value = interface->macAddr.b[3];
889 value |= (interface->macAddr.b[2] << 8);
890 value |= (interface->macAddr.b[1] << 16);
891 value |= (interface->macAddr.b[0] << 24);
892 ENET->PALR = ENET_PALR_PADDR1(
value);
895 unicastHashTable[0] = 0;
896 unicastHashTable[1] = 0;
899 multicastHashTable[0] = 0;
900 multicastHashTable[1] = 0;
907 entry = &interface->macAddrFilter[i];
917 k = (crc >> 26) & 0x3F;
923 multicastHashTable[k / 32] |= (1 << (k % 32));
928 unicastHashTable[k / 32] |= (1 << (k % 32));
934 ENET->IALR = unicastHashTable[0];
935 ENET->IAUR = unicastHashTable[1];
938 ENET->GALR = multicastHashTable[0];
939 ENET->GAUR = multicastHashTable[1];
942 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET->IALR);
943 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET->IAUR);
944 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET->GALR);
945 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET->GAUR);
961 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
967 ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
972 ENET->RCR |= ENET_RCR_RMII_10T_MASK;
979 ENET->TCR |= ENET_TCR_FDEN_MASK;
981 ENET->RCR &= ~ENET_RCR_DRT_MASK;
986 ENET->TCR &= ~ENET_TCR_FDEN_MASK;
988 ENET->RCR |= ENET_RCR_DRT_MASK;
995 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
997 ENET->RDAR = ENET_RDAR_RDAR_MASK;
1021 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
1023 temp |= ENET_MMFR_PA(phyAddr);
1025 temp |= ENET_MMFR_RA(
regAddr);
1027 temp |= ENET_MMFR_DATA(
data);
1030 ENET->EIR = ENET_EIR_MII_MASK;
1035 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1064 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1066 temp |= ENET_MMFR_PA(phyAddr);
1068 temp |= ENET_MMFR_RA(
regAddr);
1071 ENET->EIR = ENET_EIR_MII_MASK;
1076 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1081 data = ENET->MMFR & ENET_MMFR_DATA_MASK;
1109 p = (uint8_t *)
data;
1114 for(i = 0; i <
length; i++)
1120 for(j = 0; j < 8; j++)
1122 if((crc & 0x01) != 0)
1124 crc = (crc >> 1) ^ 0xEDB88320;