32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "fsl_device_registers.h"
44 #if defined(__ICCARM__)
47 #pragma data_alignment = 16
48 #pragma location = MKV5X_ETH_RAM_SECTION
51 #pragma data_alignment = 16
52 #pragma location = MKV5X_ETH_RAM_SECTION
55 #pragma data_alignment = 16
56 #pragma location = MKV5X_ETH_RAM_SECTION
59 #pragma data_alignment = 16
60 #pragma location = MKV5X_ETH_RAM_SECTION
82 static uint_t txBufferIndex;
84 static uint_t rxBufferIndex;
124 TRACE_INFO(
"Initializing Kinetis KV5x Ethernet MAC...\r\n");
127 nicDriverInterface = interface;
130 SYSMPU->CESR &= ~SYSMPU_CESR_VLD_MASK;
133 SIM->SCGC2 |= SIM_SCGC2_ENET_MASK;
139 ENET->ECR = ENET_ECR_RESET_MASK;
141 while((ENET->ECR & ENET_ECR_RESET_MASK) != 0)
147 ENET_RCR_MII_MODE_MASK;
152 ENET->MSCR = ENET_MSCR_MII_SPEED(49);
155 if(interface->phyDriver != NULL)
158 error = interface->phyDriver->init(interface);
160 else if(interface->switchDriver != NULL)
163 error = interface->switchDriver->init(interface);
178 value = interface->macAddr.b[5];
179 value |= (interface->macAddr.b[4] << 8);
180 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
183 value = interface->macAddr.b[3];
184 value |= (interface->macAddr.b[2] << 8);
185 value |= (interface->macAddr.b[1] << 16);
186 value |= (interface->macAddr.b[0] << 24);
187 ENET->PALR = ENET_PALR_PADDR1(
value);
202 ENET->ECR = ENET_ECR_DBSWP_MASK | ENET_ECR_EN1588_MASK;
205 ENET->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
212 ENET->EIR = 0xFFFFFFFF;
214 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
232 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
234 ENET->RDAR = ENET_RDAR_RDAR_MASK;
252 #if defined(USE_TWR_KV58F220M)
254 SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
257 PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
259 PORTA->PCR[7] = PORT_PCR_MUX(5) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
261 PORTA->PCR[8] = PORT_PCR_MUX(5);
263 PORTA->PCR[9] = PORT_PCR_MUX(5);
265 PORTA->PCR[10] = PORT_PCR_MUX(5);
267 PORTA->PCR[11] = PORT_PCR_MUX(5);
269 PORTA->PCR[12] = PORT_PCR_MUX(5);
271 PORTA->PCR[13] = PORT_PCR_MUX(5);
273 PORTA->PCR[14] = PORT_PCR_MUX(5);
275 PORTA->PCR[15] = PORT_PCR_MUX(5);
277 PORTA->PCR[16] = PORT_PCR_MUX(5);
279 PORTA->PCR[17] = PORT_PCR_MUX(5);
281 PORTA->PCR[24] = PORT_PCR_MUX(5);
283 PORTA->PCR[25] = PORT_PCR_MUX(5);
285 PORTA->PCR[26] = PORT_PCR_MUX(5);
287 PORTA->PCR[27] = PORT_PCR_MUX(5);
289 PORTA->PCR[29] = PORT_PCR_MUX(5);
305 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
306 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
343 ENET->TDSR = (uint32_t) txBufferDesc;
345 ENET->RDSR = (uint32_t) rxBufferDesc;
363 if(interface->phyDriver != NULL)
366 interface->phyDriver->tick(interface);
368 else if(interface->switchDriver != NULL)
371 interface->switchDriver->tick(interface);
388 NVIC_EnableIRQ(ENET_Transmit_IRQn);
389 NVIC_EnableIRQ(ENET_Receive_IRQn);
390 NVIC_EnableIRQ(ENET_Error_IRQn);
393 if(interface->phyDriver != NULL)
396 interface->phyDriver->enableIrq(interface);
398 else if(interface->switchDriver != NULL)
401 interface->switchDriver->enableIrq(interface);
418 NVIC_DisableIRQ(ENET_Transmit_IRQn);
419 NVIC_DisableIRQ(ENET_Receive_IRQn);
420 NVIC_DisableIRQ(ENET_Error_IRQn);
423 if(interface->phyDriver != NULL)
426 interface->phyDriver->disableIrq(interface);
428 else if(interface->switchDriver != NULL)
431 interface->switchDriver->disableIrq(interface);
455 if((ENET->EIR & ENET_EIR_TXF_MASK) != 0)
458 ENET->EIR = ENET_EIR_TXF_MASK;
461 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
468 ENET->TDAR = ENET_TDAR_TDAR_MASK;
491 if((ENET->EIR & ENET_EIR_RXF_MASK) != 0)
494 ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
497 nicDriverInterface->nicEvent =
TRUE;
522 if((ENET->EIR & ENET_EIR_EBERR_MASK) != 0)
525 ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
528 nicDriverInterface->nicEvent =
TRUE;
552 if((status & ENET_EIR_RXF_MASK) != 0)
555 ENET->EIR = ENET_EIR_RXF_MASK;
568 if((status & ENET_EIR_EBERR_MASK) != 0)
571 ENET->EIR = ENET_EIR_EBERR_MASK;
574 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
578 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
580 ENET->RDAR = ENET_RDAR_RDAR_MASK;
584 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
616 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) != 0)
625 txBufferDesc[txBufferIndex][4] = 0;
651 ENET->TDAR = ENET_TDAR_TDAR_MASK;
654 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
678 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_E) == 0)
681 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_L) != 0)
714 rxBufferDesc[rxBufferIndex][4] = 0;
733 ENET->RDAR = ENET_RDAR_RDAR_MASK;
758 uint32_t unicastHashTable[2];
759 uint32_t multicastHashTable[2];
766 value = interface->macAddr.b[5];
767 value |= (interface->macAddr.b[4] << 8);
768 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
771 value = interface->macAddr.b[3];
772 value |= (interface->macAddr.b[2] << 8);
773 value |= (interface->macAddr.b[1] << 16);
774 value |= (interface->macAddr.b[0] << 24);
775 ENET->PALR = ENET_PALR_PADDR1(
value);
778 unicastHashTable[0] = 0;
779 unicastHashTable[1] = 0;
782 multicastHashTable[0] = 0;
783 multicastHashTable[1] = 0;
790 entry = &interface->macAddrFilter[i];
800 k = (crc >> 26) & 0x3F;
806 multicastHashTable[k / 32] |= (1 << (k % 32));
811 unicastHashTable[k / 32] |= (1 << (k % 32));
817 ENET->IALR = unicastHashTable[0];
818 ENET->IAUR = unicastHashTable[1];
821 ENET->GALR = multicastHashTable[0];
822 ENET->GAUR = multicastHashTable[1];
825 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET->IALR);
826 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET->IAUR);
827 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET->GALR);
828 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET->GAUR);
844 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
850 ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
855 ENET->RCR |= ENET_RCR_RMII_10T_MASK;
862 ENET->TCR |= ENET_TCR_FDEN_MASK;
864 ENET->RCR &= ~ENET_RCR_DRT_MASK;
869 ENET->TCR &= ~ENET_TCR_FDEN_MASK;
871 ENET->RCR |= ENET_RCR_DRT_MASK;
878 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
880 ENET->RDAR = ENET_RDAR_RDAR_MASK;
904 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
906 temp |= ENET_MMFR_PA(phyAddr);
910 temp |= ENET_MMFR_DATA(
data);
913 ENET->EIR = ENET_EIR_MII_MASK;
918 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
947 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
949 temp |= ENET_MMFR_PA(phyAddr);
954 ENET->EIR = ENET_EIR_MII_MASK;
959 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
964 data = ENET->MMFR & ENET_MMFR_DATA_MASK;
992 p = (uint8_t *)
data;
997 for(i = 0; i <
length; i++)
1003 for(j = 0; j < 8; j++)
1005 if((crc & 0x01) != 0)
1007 crc = (crc >> 1) ^ 0xEDB88320;