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 = MIMXRT1170_ETH2_RAM_SECTION
53 #pragma data_alignment = 64
54 #pragma location = MIMXRT1170_ETH2_RAM_SECTION
57 #pragma data_alignment = 64
58 #pragma location = MIMXRT1170_ETH2_RAM_SECTION
61 #pragma data_alignment = 64
62 #pragma location = MIMXRT1170_ETH2_RAM_SECTION
84 static uint_t txBufferIndex;
86 static uint_t rxBufferIndex;
126 TRACE_INFO(
"Initializing i.MX RT1170 Ethernet MAC (ENET_1G)...\r\n");
129 nicDriverInterface = interface;
132 CLOCK_EnableClock(kCLOCK_Enet_1g);
138 ENET_1G->ECR = ENET_ECR_RESET_MASK;
140 while((ENET_1G->ECR & ENET_ECR_RESET_MASK) != 0)
146 ENET_RCR_RGMII_EN_MASK | ENET_RCR_MII_MODE_MASK;
151 ENET_1G->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_1G->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_1G->PALR = ENET_PALR_PADDR1(
value);
201 ENET_1G->ECR = ENET_ECR_DBSWP_MASK | ENET_ECR_EN1588_MASK;
204 ENET_1G->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
211 ENET_1G->EIR = 0xFFFFFFFF;
213 ENET_1G->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
223 ENET_1G->ECR |= ENET_ECR_ETHEREN_MASK;
225 ENET_1G->RDAR = ENET_RDAR_RDAR_MASK;
243 #if defined(USE_MIMXRT1170_EVK)
244 gpio_pin_config_t pinConfig;
245 clock_root_config_t rootConfig = {0};
247 clock_sys_pll1_config_t sysPll1Config = {0};
250 sysPll1Config.pllDiv2En =
true;
251 CLOCK_InitSysPll1(&sysPll1Config);
255 rootConfig.clockOff =
false;
256 rootConfig.mux = kCLOCK_ENET2_ClockRoot_MuxSysPll1Div2;
258 CLOCK_SetRootClock(kCLOCK_Root_Enet2, &rootConfig);
262 CLOCK_InitPfd(kCLOCK_PllSys2, kCLOCK_Pfd3, 24);
265 rootConfig.clockOff =
false;
266 rootConfig.mux = kCLOCK_BUS_ClockRoot_MuxSysPll2Pfd3;
268 CLOCK_SetRootClock(kCLOCK_Root_Bus, &rootConfig);
272 IOMUXC_GPR->GPR5 &= ~IOMUXC_GPR_GPR5_ENET1G_TX_CLK_SEL_MASK;
274 IOMUXC_GPR->GPR5 |= IOMUXC_GPR_GPR5_ENET1G_RGMII_EN_MASK;
277 CLOCK_EnableClock(kCLOCK_Iomuxc);
280 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_00_ENET_1G_RX_EN, 0);
283 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_00_ENET_1G_RX_EN,
284 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
285 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
286 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
287 IOMUXC_SW_PAD_CTL_PAD_PULL(2) |
288 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
291 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_01_ENET_1G_RX_CLK, 0);
294 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_01_ENET_1G_RX_CLK,
295 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
296 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
297 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
298 IOMUXC_SW_PAD_CTL_PAD_PULL(2) |
299 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
302 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_02_ENET_1G_RX_DATA00, 0);
305 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_02_ENET_1G_RX_DATA00,
306 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
307 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
308 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
309 IOMUXC_SW_PAD_CTL_PAD_PULL(2) |
310 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
313 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_03_ENET_1G_RX_DATA01, 0);
316 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_03_ENET_1G_RX_DATA01,
317 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
318 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
319 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
320 IOMUXC_SW_PAD_CTL_PAD_PULL(2) |
321 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
324 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_04_ENET_1G_RX_DATA02, 0);
327 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_04_ENET_1G_RX_DATA02,
328 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
329 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
330 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
331 IOMUXC_SW_PAD_CTL_PAD_PULL(2) |
332 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
335 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_05_ENET_1G_RX_DATA03, 0);
338 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_05_ENET_1G_RX_DATA03,
339 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
340 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
341 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
342 IOMUXC_SW_PAD_CTL_PAD_PULL(2) |
343 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
346 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_06_ENET_1G_TX_DATA03, 0);
349 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_06_ENET_1G_TX_DATA03,
350 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
351 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
352 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
353 IOMUXC_SW_PAD_CTL_PAD_PULL(3) |
354 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
357 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_07_ENET_1G_TX_DATA02, 0);
360 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_07_ENET_1G_TX_DATA02,
361 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
362 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
363 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
364 IOMUXC_SW_PAD_CTL_PAD_PULL(3) |
365 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
368 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_08_ENET_1G_TX_DATA01, 0);
371 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_08_ENET_1G_TX_DATA01,
372 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
373 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
374 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
375 IOMUXC_SW_PAD_CTL_PAD_PULL(3) |
376 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
379 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_09_ENET_1G_TX_DATA00, 0);
382 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_09_ENET_1G_TX_DATA00,
383 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
384 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
385 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
386 IOMUXC_SW_PAD_CTL_PAD_PULL(3) |
387 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
390 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_10_ENET_1G_TX_EN, 0);
393 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_10_ENET_1G_TX_EN,
394 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
395 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
396 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
397 IOMUXC_SW_PAD_CTL_PAD_PULL(3) |
398 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
401 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B1_11_ENET_1G_TX_CLK_IO, 0);
404 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B1_11_ENET_1G_TX_CLK_IO,
405 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
406 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
407 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
408 IOMUXC_SW_PAD_CTL_PAD_PULL(3) |
409 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
412 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_B2_19_ENET_1G_MDC, 0);
415 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_B2_19_ENET_1G_MDC,
416 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
417 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
418 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
419 IOMUXC_SW_PAD_CTL_PAD_PULL(3) |
420 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
423 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_B2_20_ENET_1G_MDIO, 0);
426 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_B2_20_ENET_1G_MDIO,
427 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
428 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
429 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
430 IOMUXC_SW_PAD_CTL_PAD_PULL(1) |
431 IOMUXC_SW_PAD_CTL_PAD_PDRV(0));
434 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_13_GPIO11_IO14, 0);
437 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_13_GPIO11_IO14,
438 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
439 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
440 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
441 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
442 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
443 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
444 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
447 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_12_GPIO11_IO13, 0);
450 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_12_GPIO11_IO13,
451 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
452 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
453 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
454 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
455 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
456 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
457 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
460 pinConfig.direction = kGPIO_DigitalOutput;
461 pinConfig.outputLogic = 0;
462 pinConfig.interruptMode = kGPIO_NoIntmode;
463 GPIO_PinInit(GPIO11, 14, &pinConfig);
466 pinConfig.direction = kGPIO_DigitalInput;
467 pinConfig.outputLogic = 0;
468 pinConfig.interruptMode = kGPIO_NoIntmode;
469 GPIO_PinInit(GPIO11, 13, &pinConfig);
472 GPIO_PinWrite(GPIO11, 14, 0);
474 GPIO_PinWrite(GPIO11, 14, 1);
491 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
492 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
529 ENET_1G->TDSR = (uint32_t) txBufferDesc;
531 ENET_1G->RDSR = (uint32_t) rxBufferDesc;
549 if(interface->phyDriver != NULL)
552 interface->phyDriver->tick(interface);
554 else if(interface->switchDriver != NULL)
557 interface->switchDriver->tick(interface);
574 NVIC_EnableIRQ(ENET_1G_IRQn);
577 if(interface->phyDriver != NULL)
580 interface->phyDriver->enableIrq(interface);
582 else if(interface->switchDriver != NULL)
585 interface->switchDriver->enableIrq(interface);
602 NVIC_DisableIRQ(ENET_1G_IRQn);
605 if(interface->phyDriver != NULL)
608 interface->phyDriver->disableIrq(interface);
610 else if(interface->switchDriver != NULL)
613 interface->switchDriver->disableIrq(interface);
637 events = ENET_1G->EIR;
640 if((events & ENET_EIR_TXF_MASK) != 0)
643 ENET_1G->EIR = ENET_EIR_TXF_MASK;
646 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
653 ENET_1G->TDAR = ENET_TDAR_TDAR_MASK;
657 if((events & ENET_EIR_RXF_MASK) != 0)
660 ENET_1G->EIMR &= ~ENET_EIMR_RXF_MASK;
663 nicDriverInterface->nicEvent =
TRUE;
669 if((events & ENET_EIR_EBERR_MASK) != 0)
672 ENET_1G->EIMR &= ~ENET_EIMR_EBERR_MASK;
675 nicDriverInterface->nicEvent =
TRUE;
696 status = ENET_1G->EIR;
699 if((status & ENET_EIR_RXF_MASK) != 0)
702 ENET_1G->EIR = ENET_EIR_RXF_MASK;
715 if((status & ENET_EIR_EBERR_MASK) != 0)
718 ENET_1G->EIR = ENET_EIR_EBERR_MASK;
721 ENET_1G->ECR &= ~ENET_ECR_ETHEREN_MASK;
725 ENET_1G->ECR |= ENET_ECR_ETHEREN_MASK;
727 ENET_1G->RDAR = ENET_RDAR_RDAR_MASK;
731 ENET_1G->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
763 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) != 0)
772 txBufferDesc[txBufferIndex][4] = 0;
798 ENET_1G->TDAR = ENET_TDAR_TDAR_MASK;
801 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
825 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_E) == 0)
828 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_L) != 0)
861 rxBufferDesc[rxBufferIndex][4] = 0;
880 ENET_1G->RDAR = ENET_RDAR_RDAR_MASK;
905 uint32_t unicastHashTable[2];
906 uint32_t multicastHashTable[2];
913 value = interface->macAddr.b[5];
914 value |= (interface->macAddr.b[4] << 8);
915 ENET_1G->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
918 value = interface->macAddr.b[3];
919 value |= (interface->macAddr.b[2] << 8);
920 value |= (interface->macAddr.b[1] << 16);
921 value |= (interface->macAddr.b[0] << 24);
922 ENET_1G->PALR = ENET_PALR_PADDR1(
value);
925 unicastHashTable[0] = 0;
926 unicastHashTable[1] = 0;
929 multicastHashTable[0] = 0;
930 multicastHashTable[1] = 0;
937 entry = &interface->macAddrFilter[i];
947 k = (crc >> 26) & 0x3F;
953 multicastHashTable[k / 32] |= (1 << (k % 32));
958 unicastHashTable[k / 32] |= (1 << (k % 32));
964 ENET_1G->IALR = unicastHashTable[0];
965 ENET_1G->IAUR = unicastHashTable[1];
968 ENET_1G->GALR = multicastHashTable[0];
969 ENET_1G->GAUR = multicastHashTable[1];
972 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET_1G->IALR);
973 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET_1G->IAUR);
974 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET_1G->GALR);
975 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET_1G->GAUR);
991 ENET_1G->ECR &= ~ENET_ECR_ETHEREN_MASK;
996 ENET_1G->ECR |= ENET_ECR_SPEED_MASK;
997 ENET_1G->RCR &= ~ENET_RCR_RMII_10T_MASK;
1002 ENET_1G->ECR &= ~ENET_ECR_SPEED_MASK;
1003 ENET_1G->RCR &= ~ENET_RCR_RMII_10T_MASK;
1008 ENET_1G->ECR &= ~ENET_ECR_SPEED_MASK;
1009 ENET_1G->RCR |= ENET_RCR_RMII_10T_MASK;
1016 ENET_1G->TCR |= ENET_TCR_FDEN_MASK;
1018 ENET_1G->RCR &= ~ENET_RCR_DRT_MASK;
1023 ENET_1G->TCR &= ~ENET_TCR_FDEN_MASK;
1025 ENET_1G->RCR |= ENET_RCR_DRT_MASK;
1032 ENET_1G->ECR |= ENET_ECR_ETHEREN_MASK;
1034 ENET_1G->RDAR = ENET_RDAR_RDAR_MASK;
1058 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
1060 temp |= ENET_MMFR_PA(phyAddr);
1062 temp |= ENET_MMFR_RA(
regAddr);
1064 temp |= ENET_MMFR_DATA(
data);
1067 ENET_1G->EIR = ENET_EIR_MII_MASK;
1069 ENET_1G->MMFR = temp;
1072 while((ENET_1G->EIR & ENET_EIR_MII_MASK) == 0)
1101 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1103 temp |= ENET_MMFR_PA(phyAddr);
1105 temp |= ENET_MMFR_RA(
regAddr);
1108 ENET_1G->EIR = ENET_EIR_MII_MASK;
1110 ENET_1G->MMFR = temp;
1113 while((ENET_1G->EIR & ENET_EIR_MII_MASK) == 0)
1118 data = ENET_1G->MMFR & ENET_MMFR_DATA_MASK;
1146 p = (uint8_t *)
data;
1151 for(i = 0; i <
length; i++)
1157 for(j = 0; j < 8; j++)
1159 if((crc & 0x01) != 0)
1161 crc = (crc >> 1) ^ 0xEDB88320;