32 #define TRACE_LEVEL NIC_TRACE_LEVEL
45 #if defined(__ICCARM__)
48 #pragma data_alignment = 8
49 #pragma location = SAMA5D3_ETH2_RAM_SECTION
52 #pragma data_alignment = 8
53 #pragma location = SAMA5D3_ETH2_RAM_SECTION
56 #pragma data_alignment = 8
57 #pragma location = SAMA5D3_ETH2_RAM_SECTION
60 #pragma data_alignment = 8
61 #pragma location = SAMA5D3_ETH2_RAM_SECTION
83 static uint_t txBufferIndex;
85 static uint_t rxBufferIndex;
122 volatile uint32_t status;
125 TRACE_INFO(
"Initializing SAMA5D3 Ethernet MAC (GMAC)...\r\n");
128 nicDriverInterface = interface;
131 PMC->PMC_PCER1 = (1 << (ID_GMAC - 32));
133 PMC->PMC_PCER1 = (1 << (ID_IRQ - 32));
142 GMAC->GMAC_NCFGR = GMAC_NCFGR_DBW_DBW64 | GMAC_NCFGR_CLK_MCK_224;
144 GMAC->GMAC_NCR |= GMAC_NCR_MPE;
147 if(interface->phyDriver != NULL)
150 error = interface->phyDriver->init(interface);
152 else if(interface->switchDriver != NULL)
155 error = interface->switchDriver->init(interface);
170 GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
171 GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
174 GMAC->GMAC_SA[1].GMAC_SAB = 0;
175 GMAC->GMAC_SA[2].GMAC_SAB = 0;
176 GMAC->GMAC_SA[3].GMAC_SAB = 0;
183 GMAC->GMAC_NCFGR |= GMAC_NCFGR_MAXFS | GMAC_NCFGR_MTIHEN;
189 GMAC->GMAC_TSR = GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP |
190 GMAC_TSR_TFC | GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL |
194 GMAC->GMAC_RSR = GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC |
198 GMAC->GMAC_IDR = 0xFFFFFFFF;
201 GMAC->GMAC_IER = GMAC_IER_HRESP | GMAC_IER_ROVR | GMAC_IER_TCOMP |
202 GMAC_IER_TFC | GMAC_IER_RLEX | GMAC_IER_TUR | GMAC_IER_RXUBR |
206 status = GMAC->GMAC_ISR;
210 AIC->AIC_SSR = ID_GMAC;
215 GMAC->GMAC_NCR |= GMAC_NCR_TXEN | GMAC_NCR_RXEN;
233 #if defined(USE_SAMA5D3_XPLAINED) || defined(USE_SAMA5D3_EDS) || defined(USE_EVB_KSZ9477)
235 PMC->PMC_PCER0 = (1 << ID_PIOB);
248 GMAC->GMAC_UR = GMAC_UR_RGMII;
287 rxBufferDesc[i].
status = 0;
296 GMAC->GMAC_TBQB = (uint32_t) txBufferDesc;
298 GMAC->GMAC_RBQB = (uint32_t) rxBufferDesc;
314 if(interface->phyDriver != NULL)
317 interface->phyDriver->tick(interface);
319 else if(interface->switchDriver != NULL)
322 interface->switchDriver->tick(interface);
339 AIC->AIC_SSR = ID_GMAC;
340 AIC->AIC_IECR = AIC_IECR_INTEN;
343 if(interface->phyDriver != NULL)
346 interface->phyDriver->enableIrq(interface);
348 else if(interface->switchDriver != NULL)
351 interface->switchDriver->enableIrq(interface);
368 AIC->AIC_SSR = ID_GMAC;
369 AIC->AIC_IDCR = AIC_IDCR_INTD;
372 if(interface->phyDriver != NULL)
375 interface->phyDriver->disableIrq(interface);
377 else if(interface->switchDriver != NULL)
380 interface->switchDriver->disableIrq(interface);
396 volatile uint32_t isr;
397 volatile uint32_t tsr;
398 volatile uint32_t rsr;
408 isr = GMAC->GMAC_ISR;
409 tsr = GMAC->GMAC_TSR;
410 rsr = GMAC->GMAC_RSR;
414 if((tsr & (GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP | GMAC_TSR_TFC |
415 GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL | GMAC_TSR_UBR)) != 0)
418 GMAC->GMAC_TSR = tsr;
430 if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
433 nicDriverInterface->nicEvent =
TRUE;
457 rsr = GMAC->GMAC_RSR;
460 if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
463 GMAC->GMAC_RSR = rsr;
505 if((txBufferDesc[txBufferIndex].status &
GMAC_TX_USED) == 0)
534 GMAC->GMAC_NCR |= GMAC_NCR_TSTART;
537 if((txBufferDesc[txBufferIndex].status &
GMAC_TX_USED) != 0)
575 j = rxBufferIndex + i;
598 if((rxBufferDesc[j].status &
GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
612 if(eofIndex != UINT_MAX)
616 else if(sofIndex != UINT_MAX)
629 for(i = 0; i < j; i++)
632 if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
692 uint32_t hashTable[2];
700 GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
701 GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
717 entry = &interface->macAddrFilter[i];
729 k = (
p[0] >> 6) ^
p[0];
730 k ^= (
p[1] >> 4) ^ (
p[1] << 2);
731 k ^= (
p[2] >> 2) ^ (
p[2] << 4);
732 k ^= (
p[3] >> 6) ^
p[3];
733 k ^= (
p[4] >> 4) ^ (
p[4] << 2);
734 k ^= (
p[5] >> 2) ^ (
p[5] << 4);
740 hashTable[k / 32] |= (1 << (k % 32));
748 unicastMacAddr[j] = entry->
addr;
756 k = (
p[0] >> 6) ^
p[0];
757 k ^= (
p[1] >> 4) ^ (
p[1] << 2);
758 k ^= (
p[2] >> 2) ^ (
p[2] << 4);
759 k ^= (
p[3] >> 6) ^
p[3];
760 k ^= (
p[4] >> 4) ^ (
p[4] << 2);
761 k ^= (
p[5] >> 2) ^ (
p[5] << 4);
767 hashTable[k / 32] |= (1 << (k % 32));
780 GMAC->GMAC_SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
781 GMAC->GMAC_SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
786 GMAC->GMAC_SA[1].GMAC_SAB = 0;
793 GMAC->GMAC_SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
794 GMAC->GMAC_SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
799 GMAC->GMAC_SA[2].GMAC_SAB = 0;
806 GMAC->GMAC_SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
807 GMAC->GMAC_SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
812 GMAC->GMAC_SA[3].GMAC_SAB = 0;
818 GMAC->GMAC_NCFGR |= GMAC_NCFGR_UNIHEN;
822 GMAC->GMAC_NCFGR &= ~GMAC_NCFGR_UNIHEN;
826 GMAC->GMAC_HRB = hashTable[0];
827 GMAC->GMAC_HRT = hashTable[1];
830 TRACE_DEBUG(
" HRB = %08" PRIX32
"\r\n", GMAC->GMAC_HRB);
831 TRACE_DEBUG(
" HRT = %08" PRIX32
"\r\n", GMAC->GMAC_HRT);
849 config = GMAC->GMAC_NCFGR;
854 config |= GMAC_NCFGR_GBE;
855 config &= ~GMAC_NCFGR_SPD;
860 config &= ~GMAC_NCFGR_GBE;
861 config |= GMAC_NCFGR_SPD;
866 config &= ~GMAC_NCFGR_GBE;
867 config &= ~GMAC_NCFGR_SPD;
873 config |= GMAC_NCFGR_FD;
877 config &= ~GMAC_NCFGR_FD;
881 GMAC->GMAC_NCFGR = config;
905 temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
907 temp |= GMAC_MAN_PHYA(phyAddr);
909 temp |= GMAC_MAN_REGA(
regAddr);
911 temp |= GMAC_MAN_DATA(
data);
914 GMAC->GMAC_MAN = temp;
916 while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
945 temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
947 temp |= GMAC_MAN_PHYA(phyAddr);
949 temp |= GMAC_MAN_REGA(
regAddr);
952 GMAC->GMAC_MAN = temp;
954 while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
959 data = GMAC->GMAC_MAN & GMAC_MAN_DATA_Msk;