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 = MIMXRT1160_ETH1_RAM_SECTION
53 #pragma data_alignment = 64
54 #pragma location = MIMXRT1160_ETH1_RAM_SECTION
57 #pragma data_alignment = 64
58 #pragma location = MIMXRT1160_ETH1_RAM_SECTION
61 #pragma data_alignment = 64
62 #pragma location = MIMXRT1160_ETH1_RAM_SECTION
84 static uint_t txBufferIndex;
86 static uint_t rxBufferIndex;
126 TRACE_INFO(
"Initializing i.MX RT1160 Ethernet MAC (ENET)...\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_MIMXRT1160_EVK)
244 gpio_pin_config_t pinConfig;
245 clock_root_config_t rootConfig = {0};
246 clock_sys_pll1_config_t sysPll1Config = {0};
249 sysPll1Config.pllDiv2En =
true;
250 CLOCK_InitSysPll1(&sysPll1Config);
253 rootConfig.clockOff =
false;
254 rootConfig.mux = kCLOCK_ENET1_ClockRoot_MuxSysPll1Div2;
256 CLOCK_SetRootClock(kCLOCK_Root_Enet1, &rootConfig);
260 CLOCK_InitPfd(kCLOCK_PllSys2, kCLOCK_Pfd3, 24);
263 rootConfig.clockOff =
false;
264 rootConfig.mux = kCLOCK_BUS_ClockRoot_MuxSysPll2Pfd3;
266 CLOCK_SetRootClock(kCLOCK_Root_Bus, &rootConfig);
270 IOMUXC_GPR->GPR4 |= IOMUXC_GPR_GPR4_ENET_REF_CLK_DIR_MASK |
271 IOMUXC_GPR_GPR4_ENET_TX_CLK_SEL_MASK;
274 CLOCK_EnableClock(kCLOCK_Iomuxc);
277 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_02_ENET_TX_DATA00, 0);
280 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_02_ENET_TX_DATA00,
281 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
282 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
283 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
284 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
285 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
286 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
287 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
290 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_03_ENET_TX_DATA01, 0);
293 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_03_ENET_TX_DATA01,
294 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
295 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
296 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
297 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
298 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
299 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
300 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
303 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_04_ENET_TX_EN, 0);
306 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_04_ENET_TX_EN,
307 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
308 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
309 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
310 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
311 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
312 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
313 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
316 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_05_ENET_REF_CLK, 1);
319 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_05_ENET_REF_CLK,
320 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
321 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
322 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
323 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
324 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
325 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
326 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
329 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_06_ENET_RX_DATA00, 0);
332 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_06_ENET_RX_DATA00,
333 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
334 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
335 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
336 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
337 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
338 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
339 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
342 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_07_ENET_RX_DATA01, 0);
345 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_07_ENET_RX_DATA01,
346 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
347 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
348 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
349 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
350 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
351 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
352 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
355 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_08_ENET_RX_EN, 0);
358 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_08_ENET_RX_EN,
359 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
360 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
361 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
362 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
363 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
364 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
365 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
368 IOMUXC_SetPinMux(IOMUXC_GPIO_DISP_B2_09_ENET_RX_ER, 0);
371 IOMUXC_SetPinConfig(IOMUXC_GPIO_DISP_B2_09_ENET_RX_ER,
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_PUS(0) |
376 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
377 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
378 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
381 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_32_ENET_MDC, 0);
384 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_32_ENET_MDC,
385 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
386 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
387 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
388 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
389 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
390 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
391 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
394 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_33_ENET_MDIO, 0);
397 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_33_ENET_MDIO,
398 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
399 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
400 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
401 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
402 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
403 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
404 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
407 IOMUXC_SetPinMux(IOMUXC_GPIO_LPSR_12_GPIO12_IO12, 0);
410 IOMUXC_SetPinConfig(IOMUXC_GPIO_LPSR_12_GPIO12_IO12,
411 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
412 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
413 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
414 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
415 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
416 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
417 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
420 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_12_GPIO9_IO11, 0);
423 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_12_GPIO9_IO11,
424 IOMUXC_SW_PAD_CTL_PAD_DWP_LOCK(0) |
425 IOMUXC_SW_PAD_CTL_PAD_DWP(0) |
426 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
427 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
428 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
429 IOMUXC_SW_PAD_CTL_PAD_DSE(1) |
430 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
433 pinConfig.direction = kGPIO_DigitalOutput;
434 pinConfig.outputLogic = 0;
435 pinConfig.interruptMode = kGPIO_NoIntmode;
436 GPIO_PinInit(GPIO12, 12, &pinConfig);
439 pinConfig.direction = kGPIO_DigitalInput;
440 pinConfig.outputLogic = 0;
441 pinConfig.interruptMode = kGPIO_NoIntmode;
442 GPIO_PinInit(GPIO9, 11, &pinConfig);
445 GPIO_PinWrite(GPIO12, 12, 0);
447 GPIO_PinWrite(GPIO12, 12, 1);
464 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
465 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
502 ENET->TDSR = (uint32_t) txBufferDesc;
504 ENET->RDSR = (uint32_t) rxBufferDesc;
522 if(interface->phyDriver != NULL)
525 interface->phyDriver->tick(interface);
527 else if(interface->switchDriver != NULL)
530 interface->switchDriver->tick(interface);
547 NVIC_EnableIRQ(ENET_IRQn);
550 if(interface->phyDriver != NULL)
553 interface->phyDriver->enableIrq(interface);
555 else if(interface->switchDriver != NULL)
558 interface->switchDriver->enableIrq(interface);
575 NVIC_DisableIRQ(ENET_IRQn);
578 if(interface->phyDriver != NULL)
581 interface->phyDriver->disableIrq(interface);
583 else if(interface->switchDriver != NULL)
586 interface->switchDriver->disableIrq(interface);
613 if((events & ENET_EIR_TXF_MASK) != 0)
616 ENET->EIR = ENET_EIR_TXF_MASK;
619 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
626 ENET->TDAR = ENET_TDAR_TDAR_MASK;
630 if((events & ENET_EIR_RXF_MASK) != 0)
633 ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
636 nicDriverInterface->nicEvent =
TRUE;
642 if((events & ENET_EIR_EBERR_MASK) != 0)
645 ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
648 nicDriverInterface->nicEvent =
TRUE;
672 if((status & ENET_EIR_RXF_MASK) != 0)
675 ENET->EIR = ENET_EIR_RXF_MASK;
688 if((status & ENET_EIR_EBERR_MASK) != 0)
691 ENET->EIR = ENET_EIR_EBERR_MASK;
694 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
698 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
700 ENET->RDAR = ENET_RDAR_RDAR_MASK;
704 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
736 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) != 0)
745 txBufferDesc[txBufferIndex][4] = 0;
771 ENET->TDAR = ENET_TDAR_TDAR_MASK;
774 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
798 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_E) == 0)
801 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_L) != 0)
834 rxBufferDesc[rxBufferIndex][4] = 0;
853 ENET->RDAR = ENET_RDAR_RDAR_MASK;
878 uint32_t unicastHashTable[2];
879 uint32_t multicastHashTable[2];
886 value = interface->macAddr.b[5];
887 value |= (interface->macAddr.b[4] << 8);
888 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
891 value = interface->macAddr.b[3];
892 value |= (interface->macAddr.b[2] << 8);
893 value |= (interface->macAddr.b[1] << 16);
894 value |= (interface->macAddr.b[0] << 24);
895 ENET->PALR = ENET_PALR_PADDR1(
value);
898 unicastHashTable[0] = 0;
899 unicastHashTable[1] = 0;
902 multicastHashTable[0] = 0;
903 multicastHashTable[1] = 0;
910 entry = &interface->macAddrFilter[i];
920 k = (crc >> 26) & 0x3F;
926 multicastHashTable[k / 32] |= (1 << (k % 32));
931 unicastHashTable[k / 32] |= (1 << (k % 32));
937 ENET->IALR = unicastHashTable[0];
938 ENET->IAUR = unicastHashTable[1];
941 ENET->GALR = multicastHashTable[0];
942 ENET->GAUR = multicastHashTable[1];
945 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET->IALR);
946 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET->IAUR);
947 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET->GALR);
948 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET->GAUR);
964 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
970 ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
975 ENET->RCR |= ENET_RCR_RMII_10T_MASK;
982 ENET->TCR |= ENET_TCR_FDEN_MASK;
984 ENET->RCR &= ~ENET_RCR_DRT_MASK;
989 ENET->TCR &= ~ENET_TCR_FDEN_MASK;
991 ENET->RCR |= ENET_RCR_DRT_MASK;
998 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
1000 ENET->RDAR = ENET_RDAR_RDAR_MASK;
1024 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
1026 temp |= ENET_MMFR_PA(phyAddr);
1028 temp |= ENET_MMFR_RA(
regAddr);
1030 temp |= ENET_MMFR_DATA(
data);
1033 ENET->EIR = ENET_EIR_MII_MASK;
1038 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1067 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1069 temp |= ENET_MMFR_PA(phyAddr);
1071 temp |= ENET_MMFR_RA(
regAddr);
1074 ENET->EIR = ENET_EIR_MII_MASK;
1079 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1084 data = ENET->MMFR & ENET_MMFR_DATA_MASK;
1112 p = (uint8_t *)
data;
1117 for(i = 0; i <
length; i++)
1123 for(j = 0; j < 8; j++)
1125 if((crc & 0x01) != 0)
1127 crc = (crc >> 1) ^ 0xEDB88320;