Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: ethernet: stm32: remove phy specific code #87593

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

maass-hamburg
Copy link
Collaborator

@maass-hamburg maass-hamburg commented Mar 24, 2025

  • enable mdio also on legacy devices
  • remove phy specific code from stm32 ethernet driver
  • setting the phy-handle of the ethernac mac is now mandatory
  • phys, that don't use mdio or the standard registers can now be used.

@maass-hamburg maass-hamburg force-pushed the stm32_ethernet_improve branch from 24d4c5b to 60cc0af Compare March 25, 2025 12:30
@maass-hamburg maass-hamburg force-pushed the stm32_ethernet_improve branch from 60cc0af to ac5451d Compare March 25, 2025 15:51
Copy link
Collaborator

@mathieuchopstm mathieuchopstm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I had something similar in stock (but not upstreamed); I have published the branch here if you want to take a look: https://github.com/mathieuchopstm/zephyr/tree/stm32_ethernet_rework

This branch is more conservative in terms of changes to the ETH driver, but uses some macrobatics in the MDIO driver to share the ETH_HandleTypeDef with its parent controller (saves RAM as the structure is quite large, but inhibits usage as a general-purpose MDIO; maybe an in-between solution could be found).

Another change I did was rename all "MDIO"-related stuff to clearly emphasize that this driver is for the MDIO controller built into the Ethernet IP (as opposed to the dedicated "general-purpose" MDIO controllers found on STM32H7) - this could be appreciated, but is mostly cosmetic, so not that important.

@mathieuchopstm
Copy link
Collaborator

Can someone verify, that it works on boards, that use the v1 eth hal api? I don't have a stm32f1* or stm32f2* board on hand.

Tested OK on Nucleo-F207ZG with samples/net/dhcpv4_client. I haven't found stm3210c_eval board here, and it seems to be the only STM32F1 board supported in Zephyr that has Ethernet, so F1 is untested for now...

Not 100% related but the call to HAL_ETH_Init() in eth_initialize() is blocking, delaying boot and console output by 5s when cable is unplugged (and still ~1.5s when plugged). I would have sworn I didn't encounter this behavior when I was testing my original patch, but I can't see any commit that changed this behavior, so I'm not really sure that my memory is correct... either way, this falls outside PR scope so ignore it.

@maass-hamburg
Copy link
Collaborator Author

maass-hamburg commented Mar 27, 2025

Can someone verify, that it works on boards, that use the v1 eth hal api? I don't have a stm32f1* or stm32f2* board on hand.

Tested OK on Nucleo-F207ZG with samples/net/dhcpv4_client. I haven't found stm3210c_eval board here, and it seems to be the only STM32F1 board supported in Zephyr that has Ethernet, so F1 is untested for now...

Not 100% related but the call to HAL_ETH_Init() in eth_initialize() is blocking, delaying boot and console output by 5s when cable is unplugged (and still ~1.5s when plugged). I would have sworn I didn't encounter this behavior when I was testing my original patch, but I can't see any commit that changed this behavior, so I'm not really sure that my memory is correct... either way, this falls outside PR scope so ignore it.

The thing is, that HAL_ETH_Init() in the v1 api also communicates (blocking) with the phy via mdio. Best thing would be, that f1 and f2 get the new api in the hal, next best thing would be to copy the content of these functions to the zepyhr tree and remove the communication part with the phy.

@mathieuchopstm
Copy link
Collaborator

The thing is, that HAL_ETH_Init() in the v1 api also communicates (blocking) with the phy via mdio.

I didn't look at the implementation but assumed it was something like that, so makes sense.
Though as I said, it's not an issue/regression since the driver presumably always behaved like this. We can think of ways to improve this in a follow-up PR 🙂

Comment on lines 25 to 27
clocks = <&rcc STM32_CLOCK(AHB1, 14)>;
mac: ethernet {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To improve readability, please add a newline between the properties and the child nodes:

Suggested change
clocks = <&rcc STM32_CLOCK(AHB1, 14)>;
mac: ethernet {
clocks = <&rcc STM32_CLOCK(AHB1, 14)>;
mac: ethernet {

Ditto for all DTSI.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 111 to 137
#ifdef CONFIG_ETH_STM32_HAL_API_V2
HAL_ETH_SetMDIOClockRange(heth);
#else /* CONFIG_ETH_STM32_HAL_API_V1 */
/* The legacy V1 HAL API does not provide a way to set the MDC clock range
* via a separated function call.
*/
uint32_t tmpreg1 = 0U;

/* Get the ETHERNET MACMIIAR value */
tmpreg1 = (heth->Instance)->MACMIIAR;
/* Clear CSR Clock Range CR[2:0] bits */
tmpreg1 &= ETH_MACMIIAR_CR_MASK;

/* Set CR bits depending on CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC value */
#ifdef SOC_SERIES_STM32F1X
if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 20000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 35000000U)) {
/* CSR Clock Range between 20-35 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_DIV16;
} else if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 35000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 60000000U)) {
/* CSR Clock Range between 35-60 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_DIV26;
} else {
/* CSR Clock Range between 60-72 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_DIV42;
}
#else /* SOC_SERIES_STM32F2X */
if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 20000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 35000000U)) {
/* CSR Clock Range between 20-35 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div16;
} else if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 35000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 60000000U)) {
/* CSR Clock Range between 35-60 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div26;
} else if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 60000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 100000000U)) {
/* CSR Clock Range between 60-100 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div42;
} else {
/* CSR Clock Range between 100-120 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div62;
}
#endif /* SOC_SERIES_STM32F2X */

/* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
(heth->Instance)->MACMIIAR = (uint32_t)tmpreg1;
#endif /* CONFIG_ETH_STM32_HAL_API_V1 */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split out to a dedicated function somewhere else in this file:

Suggested change
#ifdef CONFIG_ETH_STM32_HAL_API_V2
HAL_ETH_SetMDIOClockRange(heth);
#else /* CONFIG_ETH_STM32_HAL_API_V1 */
/* The legacy V1 HAL API does not provide a way to set the MDC clock range
* via a separated function call.
*/
uint32_t tmpreg1 = 0U;
/* Get the ETHERNET MACMIIAR value */
tmpreg1 = (heth->Instance)->MACMIIAR;
/* Clear CSR Clock Range CR[2:0] bits */
tmpreg1 &= ETH_MACMIIAR_CR_MASK;
/* Set CR bits depending on CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC value */
#ifdef SOC_SERIES_STM32F1X
if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 20000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 35000000U)) {
/* CSR Clock Range between 20-35 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_DIV16;
} else if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 35000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 60000000U)) {
/* CSR Clock Range between 35-60 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_DIV26;
} else {
/* CSR Clock Range between 60-72 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_DIV42;
}
#else /* SOC_SERIES_STM32F2X */
if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 20000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 35000000U)) {
/* CSR Clock Range between 20-35 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div16;
} else if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 35000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 60000000U)) {
/* CSR Clock Range between 35-60 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div26;
} else if ((CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= 60000000U) &&
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC < 100000000U)) {
/* CSR Clock Range between 60-100 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div42;
} else {
/* CSR Clock Range between 100-120 MHz */
tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div62;
}
#endif /* SOC_SERIES_STM32F2X */
/* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
(heth->Instance)->MACMIIAR = (uint32_t)tmpreg1;
#endif /* CONFIG_ETH_STM32_HAL_API_V1 */
#ifdef CONFIG_ETH_STM32_HAL_API_V2
HAL_ETH_SetMDIOClockRange(heth);
#else /* CONFIG_ETH_STM32_HAL_API_V1 */
/* The legacy V1 HAL API does not provide a way to set the MDC clock range
* via a separated function call. Implement an equivalent function ourselves
* based on what the V1 HAL performs in HAL_ETH_Init().
*/
eth_set_mdio_clock_range_for_hal_v1(heth);
#endif /* CONFIG_ETH_STM32_HAL_API_V1 */

(function name is a suggestion, feel free to use something else if it feels better to you)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@maass-hamburg maass-hamburg force-pushed the stm32_ethernet_improve branch from b8091e7 to f4d8759 Compare March 27, 2025 18:42
@maass-hamburg maass-hamburg removed the DNM This PR should not be merged (Do Not Merge) label Mar 27, 2025
@github-actions github-actions bot added the Release Notes To be mentioned in the release notes label Mar 27, 2025
@github-actions github-actions bot requested a review from fabiobaltieri March 27, 2025 19:14
@maass-hamburg maass-hamburg force-pushed the stm32_ethernet_improve branch 2 times, most recently from 45c454a to f333bf5 Compare March 28, 2025 11:10
@maass-hamburg
Copy link
Collaborator Author

#85679 seems to be merged soon, have to apply changes to their new boards/st/nucleo_f439zi/nucleo_f439zi.dts too, when it's merged

add mdio for legacy stm32 api

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
The api takes care, if the mdio bus_enable and bus_disable
are not needed, so there is no need to have tis in the driver.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
@maass-hamburg maass-hamburg force-pushed the stm32_ethernet_improve branch from 6950734 to f89f9aa Compare March 28, 2025 15:17
add mdio and phy node to every stm32board that
supports ethernet.
Also set the phy-handle for every ethernet mac.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
mac and mdio are now on the same level, this way
phy-handle can be used.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
move stmmaceth clock to parent, so it can also be
used by mdio and rename it to ``stm-eth``.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
rewirte code and remove code that use internal phy functions.
A few Kconfig options got removed, that are now set by the
phy via the DT.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
make stm32 mdio driver more independent

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
unfortunatly HAL_ETH_SetMDIOClockRange() isn't available
in the V1 API, so I had to copy the parts from the hal.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
update migration guide for changes of the
stm32 ethernet driver.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
@maass-hamburg maass-hamburg force-pushed the stm32_ethernet_improve branch from f89f9aa to d20d92f Compare March 28, 2025 15:21
@maass-hamburg
Copy link
Collaborator Author

#85679 seems to be merged soon, have to apply changes to their new boards/st/nucleo_f439zi/nucleo_f439zi.dts too, when it's merged

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Ethernet area: MDIO block: HW Test Testing on hardware required before merging platform: STM32 ST Micro STM32 Release Notes To be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants