Skip to content

Conversation

@kbidani
Copy link
Contributor

@kbidani kbidani commented Mar 13, 2025

This PR includes several improvements to the USB code for STM32 devices. The changes aim to enhance code maintainability and add necessary configurations for PHY clocks.

@kbidani kbidani force-pushed the usb_clean_clock branch 2 times, most recently from 7556439 to 54f099e Compare March 14, 2025 11:51
@kbidani kbidani changed the title Improve usb clock code USB STM32: Improve usb clock code Mar 14, 2025
@kbidani kbidani force-pushed the usb_clean_clock branch 11 times, most recently from 25be4dc to 70cf204 Compare March 29, 2025 10:01

#if USB_OTG_HS_ULPI_PHY || \
DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy) || \
USB_OTG_HS_EMB_PHYC
Copy link
Contributor

Choose a reason for hiding this comment

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

Indent

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually here also #if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC should be enough.

#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy)

static const struct stm32_pclken phy_pclken[] = STM32_DT_CLOCKS(DT_INST_PHANDLE(0, phys));
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy)
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpicking: remove the extra empty line above.

LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_USBPHY);
/* Both OTG HS and USBPHY sleep clock MUST be disabled here at the same time */
clock_control_on(clk, (clock_control_subsys_t)&phy_pclken[0]);
/* Both OTG HS and USBPHY sleep clock MUST be disabled here at the same time */
Copy link
Contributor

Choose a reason for hiding this comment

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

indentation

clocks = <&rcc STM32_CLOCK(AHB1, 26U)>;
reset-gpios = < &gpioj 4 GPIO_ACTIVE_LOW >;
#phy-cells = <0>;
clocks = <&rcc STM32_CLOCK(AHB1, 26U)>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can discard this change (no change)

@kbidani kbidani force-pushed the usb_clean_clock branch 2 times, most recently from 8216b18 to e8bd04d Compare April 1, 2025 14:44
#else
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_OTGHSULPI);
#endif
clock_control_on(clk, (clock_control_subsys_t)&phy_pclken[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

I find the duplication of this call all over the place confusing to read.
Is there anything preventing this clock_control_on call from being placed in a single spot, gated behind the same #if USB_OTG_HS_ULPI_PHY || DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy) || \ USB_OTG_HS_EMB_PHYC as the phy_pclken definition?

(If this is possible, having a top-level #define HAS_PHY_CLK 1 gated behind the existing #if test with all series/IP, and testing for HAS_PHY_CLK everywhere else, could be cleaner)

#if USB_OTG_HS_ULPI_PHY
#if defined(CONFIG_SOC_SERIES_STM32H7X)
clock_control_on(clk, (clock_control_subsys_t)&phy_pclken[0]);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_USB1OTGHSULPI);
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove line 1151 (duplicates the clock_control_on() call above)

clock_control_on(clk, (clock_control_subsys_t)&phy_pclken[0]);
/* Both OTG HS and USBPHY sleep clock MUST be disabled here at the same time */
LL_AHB2_GRP1_DisableClockStopSleep(LL_AHB2_GRP1_PERIPH_OTG_HS ||
LL_AHB2_GRP1_DisableClockStopSleep(LL_AHB2_GRP1_PERIPH_OTG_HS |
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is a fix. It deserves a dedicated commit IMHO.

clock_control_on(clk, (clock_control_subsys_t)&phy_pclken[0]);
/* Both OTG HS and USBPHY sleep clock MUST be disabled here at the same time */
LL_AHB2_GRP1_DisableClockStopSleep(LL_AHB2_GRP1_PERIPH_OTG_HS ||
LL_AHB2_GRP1_DisableClockStopSleep(LL_AHB2_GRP1_PERIPH_OTG_HS |
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixup change, deserves a dedicated commit.

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

The 3 last commits changing DTS/DTSI files should be placed before the 2 commits modifying the drivers implementation.

static const struct stm32_pclken phy_pclken[] = STM32_DT_CLOCKS(DT_INST_PHANDLE(0, phys));
#endif

#if USB_OTG_HS_ULPI_PHY || DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs) || USB_OTG_HS_EMB_PHYC
Copy link
Contributor

Choose a reason for hiding this comment

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

s/st_stm32_otghs/st_stm32u5_otghs_phy/?
If so, can define HAS_PHY_CLK in the above #ifdef/#endif close.

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_USB1OTGHSULPI);
#else
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_OTGHSULPI);
#if HAS_PHY_CLK
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking again, I'm not sure HAS_PHY_CLK can be used here.

In previous implementaion, line 416 is embedded when both compats st_stm32u5_otghs_phy AND st_stm32_otghs are enabled, that is when USB_OTG_HS_EMB_PHY is true.
So I think here it should be:

#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC || USB_OTG_HS_EMB_PHY

At line 105 (new implementation), you still need to define phy_pclken upon

#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC || DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy)

since it's required by usb_dc_stm32u5_phy_clock_select() and usb_dc_stm32u5_phy_clock_enable().

Could you cross-check that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes it's required for u5.

#define USB_OTG_HS_ULPI_PHY (DT_HAS_COMPAT_STATUS_OKAY(usb_ulpi_phy) && \
DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))

#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC
Copy link
Contributor

@etienne-lms etienne-lms Apr 10, 2025

Choose a reason for hiding this comment

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

|| USB_OTG_HS_EMB_PHY
(to cover case at line 1135 in previous implementation)

Copy link
Contributor

@etienne-lms etienne-lms Apr 10, 2025

Choose a reason for hiding this comment

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

And USB_OTG_HS_EMB_PHY should be defined:

#define USB_OTG_HS_EMB_PHY (DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy) && \
			    DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))

(edited: fixed)

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry my previous comment was inaccurate and confusing.

For consistency with device/usb_dc_stm32.c implementation, I think we should add commit to rename macro USB_OTG_HS_EMB_PHY to USB_OTG_HS_EMB_PHYC in this udc/udc_stm32.c and then introduce USB_OTG_HS_EMB_PHY related to STM32U5 OTGHS PHY, as done in usb_dc_stm32.c:

 #define USB_OTG_HS_EMB_PHYC (DT_HAS_COMPAT_STATUS_OKAY(st_stm32_usbphyc) && \
 			      DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))
 
+#define USB_OTG_HS_EMB_PHY (DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy) && \
+			     DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))
+
 #define USB_OTG_HS_ULPI_PHY (DT_HAS_COMPAT_STATUS_OKAY(usb_ulpi_phy) && \
 			      DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))

Based on that, your can factorize PHY clock management upon USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC || USB_OTG_HS_EMB_PHY.

Renaming USB_OTG_HS_EMB_PHY to USB_OTG_HS_EMB_PHYC could be controversial. @erwango, what do you think.

Copy link
Member

Choose a reason for hiding this comment

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

TBH, I'd minimize the changes to the minimum considered the pain experienced in previous PR.
But the long term direction sounds fine though, but we should not forget that device/usb_dc_stm32.c should disappear in 1 or 2 releases.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I understand renaming is not the preferred way.
However I think current proposal won't match: USB_OTG_HS_EMB_PHYC is not defined (..._PHY not ..._PHYC) and U5 case is not considered (at line 1135 in previous implementation).

That would be something like:

 #define USB_OTG_HS_EMB_PHY (DT_HAS_COMPAT_STATUS_OKAY(st_stm32_usbphyc) && \
 			    DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))				    
 
 #define USB_OTG_HS_ULPI_PHY (DT_HAS_COMPAT_STATUS_OKAY(usb_ulpi_phy) && \
 			    DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))				    
 
+#define USB_OTG_HS_EMB_U5_PHY (DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy) && \
+				DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))
+
-#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC
+#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHY || USB_OTG_HS_EMB_U5_PHY
 #define HAS_PHY_CLK

That said, you proposal implemented in the next commit of these series could apply here, no?

-#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC
+#if DT_NODE_HAS_PROP(DT_PHANDLE(DT_INST(0, DT_DRV_COMPAT), phys), clocks)
#define HAS_PHY_CLK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

replaced with: #if DT_NODE_HAS_PROP(DT_PHANDLE(DT_INST(0, DT_DRV_COMPAT), phys), clocks)

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_USB1OTGHSULPI);
#else
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_OTGHSULPI);
#if HAS_PHY_CLK
Copy link
Contributor

Choose a reason for hiding this comment

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

#ifdef

}
#endif
#elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs) /* USB_OTG_HS_ULPI_PHY */
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs) /* USB_OTG_HS_ULPI_PHY */
Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, I think an empty line above would be welcome.

@kbidani kbidani requested a review from etienne-lms April 10, 2025 08:14
Comment on lines 45 to 50
otghs_ulpi_phy: otghs_ulpis_phy {
compatible = "usb-ulpi-phy";
reset-gpios = <&gpiod 7 (GPIO_ACTIVE_LOW)>;
clocks = <&rcc STM32_CLOCK(AHB1, 30)>;
#phy-cells = <0>;
};
Copy link
Member

Choose a reason for hiding this comment

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

I'd propose to move node declaration with status = <disabled>; along with not configurable properties (clock, compatible, phy-cells) to .dtsi files.
Then board description should enable node and populate board specific properties like reset-gpios.
In any case, since it has impact on out of tree boards, this should be documented in migration guide (doc/releases/migration-guide-4.2.rst)

#define USB_DC_STM32_FULL_SPEED USB_OTG_SPEED_FULL
#endif

#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC || DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy)
Copy link
Member

Choose a reason for hiding this comment

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

Is there any way to use DT_NODE_HAS_PROP ?

Something like:
#if DT_NODE_HAS_PROP(DT_PHANDLE(DT_INST(0, DT_DRV_COMPAT), phys), clocks)

#define USB_OTG_HS_ULPI_PHY (DT_HAS_COMPAT_STATUS_OKAY(usb_ulpi_phy) && \
DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))

#if USB_OTG_HS_ULPI_PHY || USB_OTG_HS_EMB_PHYC
Copy link
Member

Choose a reason for hiding this comment

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

TBH, I'd minimize the changes to the minimum considered the pain experienced in previous PR.
But the long term direction sounds fine though, but we should not forget that device/usb_dc_stm32.c should disappear in 1 or 2 releases.

@kbidani kbidani force-pushed the usb_clean_clock branch 3 times, most recently from 6b538ac to 4e1d494 Compare April 10, 2025 16:40
}
#endif
#elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs) /* USB_OTG_HS_ULPI_PHY */
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here also I think an empty line above would be welcome.

@kbidani kbidani force-pushed the usb_clean_clock branch 2 times, most recently from bd5f229 to 81fe5a0 Compare April 13, 2025 17:50
compatible = "usb-nop-xceiv";
#phy-cells = <0>;
clocks = <&rcc STM32_CLOCK(AHB1, 30)>;
};
Copy link
Member

Choose a reason for hiding this comment

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

To be verified, but probably all these nodes should get a status = "disabled";
(When no status is provided, default is "okay").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for stm32f723 and stm32f769 status = "okay"

Comment on lines 28 to 33

otghs_ulpi_phy: otghs_ulpis_phy {
compatible = "usb-ulpi-phy";
clocks = <&rcc STM32_CLOCK(AHB1, 26)>;
};
Copy link
Member

Choose a reason for hiding this comment

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

This should be possible to use this on M4 side, so to be put in stm32h747.dtsi file.

Comment on lines 34 to 37

otghs_ulpi_phy: otghs_ulpis_phy {
clocks = <&rcc STM32_CLOCK(AHB1, 26)>;
};
Copy link
Member

Choose a reason for hiding this comment

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

To be removed as this will come by stm32h747 file inclusion

Comment on lines 28 to 33

otghs_ulpi_phy: otghs_ulpis_phy {
compatible = "usb-ulpi-phy";
#phy-cells = <0>;
clocks = <&rcc STM32_CLOCK(AHB1, 26)>;
};
Copy link
Member

Choose a reason for hiding this comment

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

To be moved to stm32h747.dtsi, or to be checked if it wouldn't be obtained by the already available inlcusion of stm32h745.dtsi.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to stm32h747 dtsi file.

compatible = "usb-ulpi-phy";
reset-gpios = < &gpioj 4 GPIO_ACTIVE_LOW >;
#phy-cells = <0>;
clocks = <&rcc STM32_CLOCK(AHB1, 26)>;
Copy link
Member

Choose a reason for hiding this comment

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

Part of phy descriptions that are not board specifics should be moved to soc files


otghs_ulpi_phy: otghs_ulpis_phy {
compatible = "usb-ulpi-phy";
clocks = <&rcc STM32_CLOCK(AHB1, 26)>;
Copy link
Member

Choose a reason for hiding this comment

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

ditto

add phy clocks to dtsi files.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
@kbidani kbidani force-pushed the usb_clean_clock branch 2 times, most recently from 07f922d to 884526e Compare April 14, 2025 15:15
@kbidani kbidani requested review from erwango and etienne-lms April 14, 2025 15:41
kbidani added 5 commits April 15, 2025 09:34
add phy clocks to dtsi files.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
Replace the direct call to LL_AHBx_GRP1_EnableClock
with the clock control API.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
Replace the direct call to LL_AHBx_GRP1_EnableClock
with the clock control API.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
corrected the logical OR (||) to bitwise OR (|) in the
LL_AHB2_GRP1_DisableClockStopSleep function call.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
corrected the logical OR (||) to bitwise OR (|) in the
LL_AHB2_GRP1_DisableClockStopSleep function call.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Jun 15, 2025
@github-actions github-actions bot closed this Jun 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants