Skip to content

Commit ac05f83

Browse files
committed
drivers: ethernet: stm32: Fix the init call order
Fix the init call order for V2 API to be able to configure PTP as PTP initialization happens before iface initialization Signed-off-by: Julien Racki <[email protected]>
1 parent ee29ea2 commit ac05f83

File tree

1 file changed

+69
-71
lines changed

1 file changed

+69
-71
lines changed

drivers/ethernet/eth_stm32_hal.c

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,68 @@ static void RISAF_Config(void)
851851
}
852852
#endif
853853

854+
#if defined(CONFIG_ETH_STM32_HAL_API_V2)
855+
static int eth_init_api_v2(const struct device *dev)
856+
{
857+
HAL_StatusTypeDef hal_ret = HAL_OK;
858+
struct eth_stm32_hal_dev_data *dev_data = dev->data;
859+
ETH_HandleTypeDef *heth = &dev_data->heth;
860+
861+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32n6_ethernet)
862+
for (int ch = 0; ch < ETH_DMA_CH_CNT; ch++) {
863+
heth->Init.TxDesc[ch] = dma_tx_desc_tab[ch];
864+
heth->Init.RxDesc[ch] = dma_rx_desc_tab[ch];
865+
}
866+
#else
867+
heth->Init.TxDesc = dma_tx_desc_tab;
868+
heth->Init.RxDesc = dma_rx_desc_tab;
869+
#endif
870+
heth->Init.RxBuffLen = ETH_STM32_RX_BUF_SIZE;
871+
872+
hal_ret = HAL_ETH_Init(heth);
873+
if (hal_ret == HAL_TIMEOUT) {
874+
/* HAL Init time out. This could be linked to */
875+
/* a recoverable error. Log the issue and continue */
876+
/* driver initialisation */
877+
LOG_ERR("HAL_ETH_Init Timed out");
878+
} else if (hal_ret != HAL_OK) {
879+
LOG_ERR("HAL_ETH_Init failed: %d", hal_ret);
880+
return -EINVAL;
881+
}
882+
883+
#if defined(CONFIG_PTP_CLOCK_STM32_HAL)
884+
/* Enable timestamping of RX packets. We enable all packets to be
885+
* timestamped to cover both IEEE 1588 and gPTP.
886+
*/
887+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet)
888+
heth->Instance->MACTSCR |= ETH_MACTSCR_TSENALL;
889+
#else
890+
heth->Instance->PTPTSCR |= ETH_PTPTSCR_TSSARFE;
891+
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
892+
#endif /* CONFIG_PTP_CLOCK_STM32_HAL */
893+
894+
/* Initialize semaphores */
895+
k_mutex_init(&dev_data->tx_mutex);
896+
k_sem_init(&dev_data->rx_int_sem, 0, K_SEM_MAX_LIMIT);
897+
k_sem_init(&dev_data->tx_int_sem, 0, K_SEM_MAX_LIMIT);
898+
899+
/* Tx config init: */
900+
memset(&tx_config, 0, sizeof(ETH_TxPacketConfig));
901+
tx_config.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
902+
tx_config.ChecksumCtrl = IS_ENABLED(CONFIG_ETH_STM32_HW_CHECKSUM)
903+
? ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC
904+
: ETH_CHECKSUM_DISABLE;
905+
tx_config.CRCPadCtrl = ETH_CRC_PAD_INSERT;
906+
907+
/* prepare tx buffer header */
908+
for (uint16_t i = 0; i < ETH_TXBUFNB; ++i) {
909+
dma_tx_buffer_header[i].tx_buff.buffer = dma_tx_buffer[i];
910+
}
911+
912+
return 0;
913+
}
914+
#endif /* CONFIG_ETH_STM32_HAL_API_V2 */
915+
854916
static int eth_initialize(const struct device *dev)
855917
{
856918
struct eth_stm32_hal_dev_data *dev_data = dev->data;
@@ -930,7 +992,13 @@ static int eth_initialize(const struct device *dev)
930992
HAL_ETH_DMARxDescListInit(heth, dma_rx_desc_tab,
931993
&dma_rx_buffer[0][0], ETH_RXBUFNB);
932994

933-
#endif /* !CONFIG_ETH_STM32_HAL_API_V1 */
995+
#elif defined(CONFIG_ETH_STM32_HAL_API_V2)
996+
int ret = eth_init_api_v2(dev);
997+
998+
if (ret != 0) {
999+
return ret;
1000+
}
1001+
#endif /* CONFIG_ETH_STM32_HAL_API_V1 */
9341002

9351003
LOG_DBG("MAC %02x:%02x:%02x:%02x:%02x:%02x",
9361004
dev_data->mac_addr[0], dev_data->mac_addr[1],
@@ -988,68 +1056,6 @@ static void eth_stm32_mcast_filter(const struct device *dev, const struct ethern
9881056

9891057
#endif /* CONFIG_ETH_STM32_MULTICAST_FILTER */
9901058

991-
#if defined(CONFIG_ETH_STM32_HAL_API_V2)
992-
static int eth_init_api_v2(const struct device *dev)
993-
{
994-
HAL_StatusTypeDef hal_ret = HAL_OK;
995-
struct eth_stm32_hal_dev_data *dev_data = dev->data;
996-
ETH_HandleTypeDef *heth = &dev_data->heth;
997-
998-
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32n6_ethernet)
999-
for (int ch = 0; ch < ETH_DMA_CH_CNT; ch++) {
1000-
heth->Init.TxDesc[ch] = dma_tx_desc_tab[ch];
1001-
heth->Init.RxDesc[ch] = dma_rx_desc_tab[ch];
1002-
}
1003-
#else
1004-
heth->Init.TxDesc = dma_tx_desc_tab;
1005-
heth->Init.RxDesc = dma_rx_desc_tab;
1006-
#endif
1007-
heth->Init.RxBuffLen = ETH_STM32_RX_BUF_SIZE;
1008-
1009-
hal_ret = HAL_ETH_Init(heth);
1010-
if (hal_ret == HAL_TIMEOUT) {
1011-
/* HAL Init time out. This could be linked to */
1012-
/* a recoverable error. Log the issue and continue */
1013-
/* driver initialisation */
1014-
LOG_ERR("HAL_ETH_Init Timed out");
1015-
} else if (hal_ret != HAL_OK) {
1016-
LOG_ERR("HAL_ETH_Init failed: %d", hal_ret);
1017-
return -EINVAL;
1018-
}
1019-
1020-
#if defined(CONFIG_PTP_CLOCK_STM32_HAL)
1021-
/* Enable timestamping of RX packets. We enable all packets to be
1022-
* timestamped to cover both IEEE 1588 and gPTP.
1023-
*/
1024-
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet)
1025-
heth->Instance->MACTSCR |= ETH_MACTSCR_TSENALL;
1026-
#else
1027-
heth->Instance->PTPTSCR |= ETH_PTPTSCR_TSSARFE;
1028-
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
1029-
#endif /* CONFIG_PTP_CLOCK_STM32_HAL */
1030-
1031-
/* Initialize semaphores */
1032-
k_mutex_init(&dev_data->tx_mutex);
1033-
k_sem_init(&dev_data->rx_int_sem, 0, K_SEM_MAX_LIMIT);
1034-
k_sem_init(&dev_data->tx_int_sem, 0, K_SEM_MAX_LIMIT);
1035-
1036-
/* Tx config init: */
1037-
memset(&tx_config, 0, sizeof(ETH_TxPacketConfig));
1038-
tx_config.Attributes = ETH_TX_PACKETS_FEATURES_CSUM |
1039-
ETH_TX_PACKETS_FEATURES_CRCPAD;
1040-
tx_config.ChecksumCtrl = IS_ENABLED(CONFIG_ETH_STM32_HW_CHECKSUM) ?
1041-
ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC : ETH_CHECKSUM_DISABLE;
1042-
tx_config.CRCPadCtrl = ETH_CRC_PAD_INSERT;
1043-
1044-
/* prepare tx buffer header */
1045-
for (uint16_t i = 0; i < ETH_TXBUFNB; ++i) {
1046-
dma_tx_buffer_header[i].tx_buff.buffer = dma_tx_buffer[i];
1047-
}
1048-
1049-
return 0;
1050-
}
1051-
#endif /* CONFIG_ETH_STM32_HAL_API_V2 */
1052-
10531059
static void set_mac_config(const struct device *dev, struct phy_link_state *state)
10541060
{
10551061
struct eth_stm32_hal_dev_data *dev_data = dev->data;
@@ -1170,14 +1176,6 @@ static void eth_iface_init(struct net_if *iface)
11701176

11711177
ethernet_init(iface);
11721178

1173-
#if defined(CONFIG_ETH_STM32_HAL_API_V2)
1174-
/* This function requires the Ethernet interface to be
1175-
* properly initialized. In auto-negotiation mode, it reads the speed
1176-
* and duplex settings to configure the driver accordingly.
1177-
*/
1178-
eth_init_api_v2(dev);
1179-
#endif
1180-
11811179
setup_mac_filter(heth);
11821180

11831181
net_if_carrier_off(iface);

0 commit comments

Comments
 (0)