32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "fsl_device_registers.h"
44 #if defined(__ICCARM__)
47 #pragma data_alignment = 16
50 #pragma data_alignment = 16
53 #pragma data_alignment = 16
56 #pragma data_alignment = 16
78 static uint_t txBufferIndex;
80 static uint_t rxBufferIndex;
120 TRACE_INFO(
"Initializing Kinetis K6x Ethernet MAC...\r\n");
123 nicDriverInterface = interface;
129 OSC->CR |= OSC_CR_ERCLKEN_MASK;
131 SIM->SCGC2 |= SIM_SCGC2_ENET_MASK;
137 ENET->ECR = ENET_ECR_RESET_MASK;
139 while((ENET->ECR & ENET_ECR_RESET_MASK) != 0)
145 ENET_RCR_RMII_MODE_MASK | ENET_RCR_MII_MODE_MASK;
150 ENET->MSCR = ENET_MSCR_MII_SPEED(59);
153 if(interface->phyDriver != NULL)
156 error = interface->phyDriver->init(interface);
158 else if(interface->switchDriver != NULL)
161 error = interface->switchDriver->init(interface);
176 value = interface->macAddr.b[5];
177 value |= (interface->macAddr.b[4] << 8);
178 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
181 value = interface->macAddr.b[3];
182 value |= (interface->macAddr.b[2] << 8);
183 value |= (interface->macAddr.b[1] << 16);
184 value |= (interface->macAddr.b[0] << 24);
185 ENET->PALR = ENET_PALR_PADDR1(
value);
200 ENET->ECR = ENET_ECR_EN1588_MASK;
203 ENET->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
210 ENET->EIR = 0xFFFFFFFF;
212 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
230 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
232 ENET->RDAR = ENET_RDAR_RDAR_MASK;
250 #if defined(USE_TWR_K60N512) || defined(USE_TWR_K60D100M) || \
251 defined(USE_TWR_K60F120M)
253 SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK;
256 PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
258 PORTA->PCR[12] = PORT_PCR_MUX(4);
260 PORTA->PCR[13] = PORT_PCR_MUX(4);
262 PORTA->PCR[14] = PORT_PCR_MUX(4);
264 PORTA->PCR[15] = PORT_PCR_MUX(4);
266 PORTA->PCR[16] = PORT_PCR_MUX(4);
268 PORTA->PCR[17] = PORT_PCR_MUX(4);
271 PORTB->PCR[0] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
273 PORTB->PCR[1] = PORT_PCR_MUX(4);
276 #elif defined(USE_FRDM_K64F) || defined(USE_TWR_K64F120M)
278 SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK;
281 PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
283 PORTA->PCR[12] = PORT_PCR_MUX(4);
285 PORTA->PCR[13] = PORT_PCR_MUX(4);
287 PORTA->PCR[14] = PORT_PCR_MUX(4);
289 PORTA->PCR[15] = PORT_PCR_MUX(4);
291 PORTA->PCR[16] = PORT_PCR_MUX(4);
293 PORTA->PCR[17] = PORT_PCR_MUX(4);
296 PORTB->PCR[0] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
298 PORTB->PCR[1] = PORT_PCR_MUX(4);
301 SIM->SOPT2 &= ~SIM_SOPT2_RMIISRC_MASK;
304 #elif defined(USE_TWR_K65F180M)
306 SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTE_MASK;
309 PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
311 PORTA->PCR[7] = PORT_PCR_MUX(5) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
313 PORTA->PCR[8] = PORT_PCR_MUX(5);
315 PORTA->PCR[12] = PORT_PCR_MUX(4);
317 PORTA->PCR[13] = PORT_PCR_MUX(4);
319 PORTA->PCR[14] = PORT_PCR_MUX(4);
321 PORTA->PCR[15] = PORT_PCR_MUX(4);
323 PORTA->PCR[16] = PORT_PCR_MUX(4);
325 PORTA->PCR[17] = PORT_PCR_MUX(4);
328 PORTE->PCR[26] = PORT_PCR_MUX(2);
331 SIM->SOPT2 |= SIM_SOPT2_RMIISRC_MASK;
334 #elif defined(USE_FRDM_K66F)
336 SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK |
337 SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTE_MASK;
340 PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
342 PORTA->PCR[12] = PORT_PCR_MUX(4);
344 PORTA->PCR[13] = PORT_PCR_MUX(4);
346 PORTA->PCR[14] = PORT_PCR_MUX(4);
348 PORTA->PCR[15] = PORT_PCR_MUX(4);
350 PORTA->PCR[16] = PORT_PCR_MUX(4);
352 PORTA->PCR[17] = PORT_PCR_MUX(4);
355 PORTB->PCR[0] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
357 PORTB->PCR[1] = PORT_PCR_MUX(4);
360 PORTE->PCR[26] = PORT_PCR_MUX(2);
363 SIM->SOPT2 |= SIM_SOPT2_RMIISRC_MASK;
366 #elif defined(USE_EMBOS_IP_SWITCH_BOARD)
368 SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTE_MASK;
371 PORTA->PCR[12] = PORT_PCR_MUX(4);
373 PORTA->PCR[13] = PORT_PCR_MUX(4);
375 PORTA->PCR[14] = PORT_PCR_MUX(4);
377 PORTA->PCR[15] = PORT_PCR_MUX(4);
379 PORTA->PCR[16] = PORT_PCR_MUX(4);
381 PORTA->PCR[17] = PORT_PCR_MUX(4);
384 PORTE->PCR[26] = PORT_PCR_MUX(2);
387 SIM->SOPT2 |= SIM_SOPT2_RMIISRC_MASK;
403 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
404 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
443 ENET->TDSR = (uint32_t) txBufferDesc;
445 ENET->RDSR = (uint32_t) rxBufferDesc;
463 if(interface->phyDriver != NULL)
466 interface->phyDriver->tick(interface);
468 else if(interface->switchDriver != NULL)
471 interface->switchDriver->tick(interface);
488 NVIC_EnableIRQ(ENET_Transmit_IRQn);
489 NVIC_EnableIRQ(ENET_Receive_IRQn);
490 NVIC_EnableIRQ(ENET_Error_IRQn);
493 if(interface->phyDriver != NULL)
496 interface->phyDriver->enableIrq(interface);
498 else if(interface->switchDriver != NULL)
501 interface->switchDriver->enableIrq(interface);
518 NVIC_DisableIRQ(ENET_Transmit_IRQn);
519 NVIC_DisableIRQ(ENET_Receive_IRQn);
520 NVIC_DisableIRQ(ENET_Error_IRQn);
523 if(interface->phyDriver != NULL)
526 interface->phyDriver->disableIrq(interface);
528 else if(interface->switchDriver != NULL)
531 interface->switchDriver->disableIrq(interface);
555 if((ENET->EIR & ENET_EIR_TXF_MASK) != 0)
558 ENET->EIR = ENET_EIR_TXF_MASK;
568 ENET->TDAR = ENET_TDAR_TDAR_MASK;
591 if((ENET->EIR & ENET_EIR_RXF_MASK) != 0)
594 ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
597 nicDriverInterface->nicEvent =
TRUE;
622 if((ENET->EIR & ENET_EIR_EBERR_MASK) != 0)
625 ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
628 nicDriverInterface->nicEvent =
TRUE;
652 if((status & ENET_EIR_RXF_MASK) != 0)
655 ENET->EIR = ENET_EIR_RXF_MASK;
668 if((status & ENET_EIR_EBERR_MASK) != 0)
671 ENET->EIR = ENET_EIR_EBERR_MASK;
674 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
678 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
680 ENET->RDAR = ENET_RDAR_RDAR_MASK;
684 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
727 txBufferDesc[txBufferIndex][8] = 0;
750 ENET->TDAR = ENET_TDAR_TDAR_MASK;
787 n =
betoh16(rxBufferDesc[rxBufferIndex][1]);
813 rxBufferDesc[rxBufferIndex][8] = 0;
832 ENET->RDAR = ENET_RDAR_RDAR_MASK;
857 uint32_t unicastHashTable[2];
858 uint32_t multicastHashTable[2];
865 value = interface->macAddr.b[5];
866 value |= (interface->macAddr.b[4] << 8);
867 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
870 value = interface->macAddr.b[3];
871 value |= (interface->macAddr.b[2] << 8);
872 value |= (interface->macAddr.b[1] << 16);
873 value |= (interface->macAddr.b[0] << 24);
874 ENET->PALR = ENET_PALR_PADDR1(
value);
877 unicastHashTable[0] = 0;
878 unicastHashTable[1] = 0;
881 multicastHashTable[0] = 0;
882 multicastHashTable[1] = 0;
889 entry = &interface->macAddrFilter[i];
899 k = (crc >> 26) & 0x3F;
905 multicastHashTable[k / 32] |= (1 << (k % 32));
910 unicastHashTable[k / 32] |= (1 << (k % 32));
916 ENET->IALR = unicastHashTable[0];
917 ENET->IAUR = unicastHashTable[1];
920 ENET->GALR = multicastHashTable[0];
921 ENET->GAUR = multicastHashTable[1];
924 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET->IALR);
925 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET->IAUR);
926 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET->GALR);
927 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET->GAUR);
943 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
949 ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
954 ENET->RCR |= ENET_RCR_RMII_10T_MASK;
961 ENET->TCR |= ENET_TCR_FDEN_MASK;
963 ENET->RCR &= ~ENET_RCR_DRT_MASK;
968 ENET->TCR &= ~ENET_TCR_FDEN_MASK;
970 ENET->RCR |= ENET_RCR_DRT_MASK;
977 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
979 ENET->RDAR = ENET_RDAR_RDAR_MASK;
1003 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
1005 temp |= ENET_MMFR_PA(phyAddr);
1007 temp |= ENET_MMFR_RA(
regAddr);
1009 temp |= ENET_MMFR_DATA(
data);
1012 ENET->EIR = ENET_EIR_MII_MASK;
1017 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1046 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1048 temp |= ENET_MMFR_PA(phyAddr);
1050 temp |= ENET_MMFR_RA(
regAddr);
1053 ENET->EIR = ENET_EIR_MII_MASK;
1058 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1063 data = ENET->MMFR & ENET_MMFR_DATA_MASK;
1091 p = (uint8_t *)
data;
1096 for(i = 0; i <
length; i++)
1102 for(j = 0; j < 8; j++)
1104 if((crc & 0x01) != 0)
1106 crc = (crc >> 1) ^ 0xEDB88320;