32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "stm32mp1xx.h"
36 #include "stm32mp1xx_hal.h"
45 #if defined(__ICCARM__)
48 #pragma data_alignment = 4
51 #pragma data_alignment = 4
54 #pragma data_alignment = 8
57 #pragma data_alignment = 8
121 TRACE_INFO(
"Initializing STM32MP1 Ethernet MAC...\r\n");
124 nicDriverInterface = interface;
130 __HAL_RCC_ETH1MAC_CLK_ENABLE();
131 __HAL_RCC_ETH1TX_CLK_ENABLE();
132 __HAL_RCC_ETH1RX_CLK_ENABLE();
135 __HAL_RCC_ETH1MAC_FORCE_RESET();
136 __HAL_RCC_ETH1MAC_RELEASE_RESET();
139 ETH->DMAMR |= ETH_DMAMR_SWR;
141 while((ETH->DMAMR & ETH_DMAMR_SWR) != 0)
149 if(interface->phyDriver != NULL)
152 error = interface->phyDriver->init(interface);
154 else if(interface->switchDriver != NULL)
157 error = interface->switchDriver->init(interface);
172 ETH->MACCR = ETH_MACCR_GPSLCE | ETH_MACCR_DO;
175 temp = ETH->MACECR & ~ETH_MACECR_GPSL;
191 ETH->DMASBMR |= ETH_DMASBMR_AAL;
214 ETH->MMCTXIMR = ETH_MMCTXIMR_TXLPITRCIM | ETH_MMCTXIMR_TXLPIUSCIM |
215 ETH_MMCTXIMR_TXGPKTIM | ETH_MMCTXIMR_TXMCOLGPIM | ETH_MMCTXIMR_TXSCOLGPIM;
219 ETH->MMCRXIMR = ETH_MMCRXIMR_RXLPITRCIM | ETH_MMCRXIMR_RXLPIUSCIM |
220 ETH_MMCRXIMR_RXUCGPIM | ETH_MMCRXIMR_RXALGNERPIM | ETH_MMCRXIMR_RXCRCERPIM;
225 ETH->DMAC0IER = ETH_DMAC0IER_NIE | ETH_DMAC0IER_RIE | ETH_DMAC0IER_TIE;
235 ETH->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
238 ETH->DMAC0TXCR |= ETH_DMAC0TXCR_ST;
239 ETH->DMAC0RXCR |= ETH_DMAC0RXCR_SR;
257 #if defined(USE_STM32MP15XX_DISCO)
258 GPIO_InitTypeDef GPIO_InitStructure;
261 __HAL_RCC_SYSCFG_CLK_ENABLE();
264 __HAL_RCC_GPIOA_CLK_ENABLE();
265 __HAL_RCC_GPIOB_CLK_ENABLE();
266 __HAL_RCC_GPIOC_CLK_ENABLE();
267 __HAL_RCC_GPIOE_CLK_ENABLE();
268 __HAL_RCC_GPIOG_CLK_ENABLE();
271 HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RGMII);
274 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
275 GPIO_InitStructure.Pull = GPIO_NOPULL;
276 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
277 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
281 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
282 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
286 GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_11;
287 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
291 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 | GPIO_PIN_5;
292 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
295 GPIO_InitStructure.Pin = GPIO_PIN_2;
296 HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
300 GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_13 | GPIO_PIN_14;
301 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
304 GPIO_InitStructure.Pin = GPIO_PIN_0;
305 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
306 GPIO_InitStructure.Pull = GPIO_NOPULL;
307 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
308 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
311 HAL_GPIO_WritePin(GPIOG, GPIO_PIN_0, GPIO_PIN_RESET);
313 HAL_GPIO_WritePin(GPIOG, GPIO_PIN_0, GPIO_PIN_SET);
317 #elif defined(USE_STM32MP15XX_EVAL)
318 GPIO_InitTypeDef GPIO_InitStructure;
321 __HAL_RCC_SYSCFG_CLK_ENABLE();
324 __HAL_RCC_GPIOA_CLK_ENABLE();
325 __HAL_RCC_GPIOB_CLK_ENABLE();
326 __HAL_RCC_GPIOC_CLK_ENABLE();
328 __HAL_RCC_GPIOE_CLK_ENABLE();
329 __HAL_RCC_GPIOG_CLK_ENABLE();
332 HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RGMII);
335 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
336 GPIO_InitStructure.Pull = GPIO_NOPULL;
337 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
338 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
342 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
343 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
347 GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_11;
348 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
352 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 | GPIO_PIN_5;
353 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
356 GPIO_InitStructure.Pin = GPIO_PIN_2;
357 HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
361 GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_13 | GPIO_PIN_14;
362 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
416 ETH->DMAC0TXDLAR = (uint32_t) &
txDmaDesc[0];
421 ETH->DMAC0RXDLAR = (uint32_t) &
rxDmaDesc[0];
439 if(interface->phyDriver != NULL)
442 interface->phyDriver->tick(interface);
444 else if(interface->switchDriver != NULL)
447 interface->switchDriver->tick(interface);
464 NVIC_EnableIRQ(ETH1_IRQn);
467 if(interface->phyDriver != NULL)
470 interface->phyDriver->enableIrq(interface);
472 else if(interface->switchDriver != NULL)
475 interface->switchDriver->enableIrq(interface);
492 NVIC_DisableIRQ(ETH1_IRQn);
495 if(interface->phyDriver != NULL)
498 interface->phyDriver->disableIrq(interface);
500 else if(interface->switchDriver != NULL)
503 interface->switchDriver->disableIrq(interface);
528 status = ETH->DMAC0SR;
531 if((status & ETH_DMAC0SR_TI) != 0)
534 ETH->DMAC0SR = ETH_DMAC0SR_TI;
545 if((status & ETH_DMAC0SR_RI) != 0)
548 ETH->DMAC0SR = ETH_DMAC0SR_RI;
551 nicDriverInterface->nicEvent =
TRUE;
557 ETH->DMAC0SR = ETH_DMAC0SR_NIS;
631 ETH->DMAC0SR = ETH_DMAC0SR_TBU;
633 ETH->DMAC0TXDTPR = 0;
719 ETH->DMAC0SR = ETH_DMAC0SR_RBU;
721 ETH->DMAC0RXDTPR = 0;
740 uint32_t hashTable[2];
748 if(interface->promiscuous)
751 ETH->MACPFR = ETH_MACPFR_PR;
756 ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
757 ETH->MACA0HR = interface->macAddr.w[2];
773 entry = &interface->macAddrFilter[i];
786 k = (crc >> 26) & 0x3F;
789 hashTable[k / 32] |= (1 << (k % 32));
797 unicastMacAddr[j++] = entry->
addr;
807 ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
808 ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACA1HR_AE;
821 ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
822 ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACA2HR_AE;
835 ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
836 ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACA3HR_AE;
847 if(interface->acceptAllMulticast)
850 ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_PM;
855 ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_HMC;
858 ETH->MACHT0R = hashTable[0];
859 ETH->MACHT1R = hashTable[1];
862 TRACE_DEBUG(
" MACHT0R = %08" PRIX32
"\r\n", ETH->MACHT0R);
863 TRACE_DEBUG(
" MACHT1R = %08" PRIX32
"\r\n", ETH->MACHT1R);
888 config &= ~ETH_MACCR_PS;
889 config &= ~ETH_MACCR_FES;
894 config |= ETH_MACCR_PS;
895 config |= ETH_MACCR_FES;
900 config |= ETH_MACCR_PS;
901 config &= ~ETH_MACCR_FES;
907 config |= ETH_MACCR_DM;
911 config &= ~ETH_MACCR_DM;
939 temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
943 temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
945 temp |= (
regAddr << 16) & ETH_MACMDIOAR_RDA;
948 ETH->MACMDIODR =
data & ETH_MACMDIODR_GD;
951 ETH->MACMDIOAR = temp;
953 while((ETH->MACMDIOAR & ETH_MACMDIOAR_GB) != 0)
982 temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
986 temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
988 temp |= (
regAddr << 16) & ETH_MACMDIOAR_RDA;
991 ETH->MACMDIOAR = temp;
993 while((ETH->MACMDIOAR & ETH_MACMDIOAR_GB) != 0)
998 data = ETH->MACMDIODR & ETH_MACMDIODR_GD;
1026 p = (uint8_t *)
data;
1031 for(i = 0; i <
length; i++)
1034 for(j = 0; j < 8; j++)
1037 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
1039 crc = (crc << 1) ^ 0x04C11DB7;