@@ -41,26 +41,41 @@ LOG_MODULE_REGISTER(udc_stm32, CONFIG_UDC_DRIVER_LOG_LEVEL);
4141#define  UDC_STM32_IRQ 		DT_INST_IRQ_BY_NAME(0, UDC_STM32_IRQ_NAME, irq)
4242#define  UDC_STM32_IRQ_PRI 	DT_INST_IRQ_BY_NAME(0, UDC_STM32_IRQ_NAME, priority)
4343
44- #define  USB_OTG_HS_EMB_PHY  (DT_HAS_COMPAT_STATUS_OKAY(st_stm32_usbphyc) && \
44+ #define  USB_OTG_HS_EMB_PHYC  (DT_HAS_COMPAT_STATUS_OKAY(st_stm32_usbphyc) && \
45+ 			     DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))
46+ 
47+ #define  USB_OTG_HS_EMB_PHY  (DT_HAS_COMPAT_STATUS_OKAY(st_stm32u5_otghs_phy) && \
4548			    DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))
4649
4750#define  USB_OTG_HS_ULPI_PHY  (DT_HAS_COMPAT_STATUS_OKAY(usb_ulpi_phy) && \
4851			    DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs))
4952
5053/** 
51-  * The following defines are used to map the value of the "maxiumum-speed" 
52-  * DT property to the corresponding definition used by the STM32 HAL. 
54+  * Select the desired speed based on enabled high speed support and phy. 
55+  * 
56+  * 1.  high speed support enabled and hs phy is used 
57+  * 1.1 maximum speed is reduced by devicetree configuration 
58+  * 1.2 maximum speed is high speed 
59+  * 2.  high speed phy is used, but high speed support is not enabled 
60+  * 3.  non-hs capable hardware, but with st_stm32_usb compatible 
61+  * 4.  everything else 
5362 */ 
54- #if  defined(CONFIG_SOC_SERIES_STM32H7X ) ||  USB_OTG_HS_EMB_PHY 
55- #define  UDC_STM32_HIGH_SPEED              USB_OTG_SPEED_HIGH_IN_FULL
63+ #if  CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED  &&  \
64+ 	(USB_OTG_HS_EMB_PHYC  ||  USB_OTG_HS_EMB_PHY  ||  USB_OTG_HS_ULPI_PHY )
65+ /* as UDC_BUS_SPEED_HS is an enum we can not directly use is in the following 
66+    preprocessor logic. make sure it has the expected value during compilation. */ 
67+ BUILD_ASSERT (UDC_BUS_SPEED_HS  ==  2 , "UDC_BUS_SPEED_HS is expected to be 2" );
68+ #if  DT_ENUM_IDX_OR (DT_DRV_INST (0 ), maximum_speed , 2 ) !=  2 
69+ #define  UDC_STM32_SPEED                   USB_OTG_SPEED_HIGH_IN_FULL
5670#else 
57- #define  UDC_STM32_HIGH_SPEED              USB_OTG_SPEED_HIGH
71+ #define  UDC_STM32_SPEED                    USB_OTG_SPEED_HIGH
5872#endif 
59- 
60- #if  DT_HAS_COMPAT_STATUS_OKAY (st_stm32_usb )
61- #define  UDC_STM32_FULL_SPEED              PCD_SPEED_FULL
73+ #elif  USB_OTG_HS_EMB_PHYC  ||  USB_OTG_HS_EMB_PHY  ||  USB_OTG_HS_ULPI_PHY 
74+ #define  UDC_STM32_SPEED                   USB_OTG_SPEED_HIGH_IN_FULL
75+ #elif  DT_HAS_COMPAT_STATUS_OKAY (st_stm32_usb )
76+ #define  UDC_STM32_SPEED                   PCD_SPEED_FULL
6277#else 
63- #define  UDC_STM32_FULL_SPEED              USB_OTG_SPEED_FULL
78+ #define  UDC_STM32_SPEED                    USB_OTG_SPEED_FULL
6479#endif 
6580
6681#if  DT_HAS_COMPAT_STATUS_OKAY (st_stm32n6_otghs )
@@ -85,7 +100,6 @@ struct udc_stm32_config {
85100	uint32_t  dram_size ;
86101	uint16_t  ep0_mps ;
87102	uint16_t  ep_mps ;
88- 	int  speed_idx ;
89103};
90104
91105enum  udc_stm32_msg_type  {
@@ -986,7 +1000,6 @@ static const struct udc_stm32_config udc0_cfg  = {
9861000	.pma_offset  =  USB_BTABLE_SIZE ,
9871001	.ep0_mps  =  EP0_MPS ,
9881002	.ep_mps  =  EP_MPS ,
989- 	.speed_idx  =  DT_ENUM_IDX_OR (DT_DRV_INST (0 ), maximum_speed , 1 ),
9901003};
9911004
9921005static  void  priv_pcd_prepare (const  struct  device  * dev )
@@ -999,7 +1012,7 @@ static void priv_pcd_prepare(const struct device *dev)
9991012	/* Default values */ 
10001013	priv -> pcd .Init .dev_endpoints  =  cfg -> num_endpoints ;
10011014	priv -> pcd .Init .ep0_mps  =  cfg -> ep0_mps ;
1002- 	priv -> pcd .Init .speed  =  UTIL_CAT ( UDC_STM32_ ,  DT_INST_STRING_UPPER_TOKEN ( 0 ,  maximum_speed )) ;
1015+ 	priv -> pcd .Init .speed  =  UDC_STM32_SPEED ;
10031016
10041017	/* Per controller/Phy values */ 
10051018#if  defined(USB )
@@ -1010,13 +1023,13 @@ static void priv_pcd_prepare(const struct device *dev)
10101023	priv -> pcd .Instance  =  (USB_OTG_GlobalTypeDef  * )UDC_STM32_BASE_ADDRESS ;
10111024#endif  /* USB */ 
10121025
1013- #if  USB_OTG_HS_EMB_PHY 
1026+ #if  USB_OTG_HS_EMB_PHYC   ||   USB_OTG_HS_EMB_PHY 
10141027	priv -> pcd .Init .phy_itface  =  USB_OTG_HS_EMBEDDED_PHY ;
10151028#elif  USB_OTG_HS_ULPI_PHY 
10161029	priv -> pcd .Init .phy_itface  =  USB_OTG_ULPI_PHY ;
10171030#else 
10181031	priv -> pcd .Init .phy_itface  =  PCD_PHY_EMBEDDED ;
1019- #endif  /* USB_OTG_HS_EMB_PHY */ 
1032+ #endif  /* USB_OTG_HS_EMB_PHYC ||  USB_OTG_HS_EMB_PHY */ 
10201033}
10211034
10221035static  const  struct  stm32_pclken  pclken [] =  STM32_DT_INST_CLOCKS (0 );
@@ -1159,7 +1172,7 @@ static int priv_clock_enable(void)
11591172	LL_AHB1_GRP1_DisableClockLowPower (LL_AHB1_GRP1_PERIPH_OTGHSULPI );
11601173#endif  /* defined(CONFIG_SOC_SERIES_STM32H7X) */ 
11611174
1162- #if  USB_OTG_HS_EMB_PHY 
1175+ #if  USB_OTG_HS_EMB_PHYC 
11631176#if  !DT_HAS_COMPAT_STATUS_OKAY (st_stm32n6_otghs )
11641177	LL_APB2_GRP1_EnableClock (LL_APB2_GRP1_PERIPH_OTGPHYC );
11651178#endif 
@@ -1257,9 +1270,9 @@ static int udc_stm32_driver_init0(const struct device *dev)
12571270	data -> caps .rwup  =  true;
12581271	data -> caps .out_ack  =  false;
12591272	data -> caps .mps0  =  UDC_MPS0_64 ;
1260- 	 if  ( cfg -> speed_idx  ==  2 ) { 
1261- 		 data -> caps .hs  =  true;
1262- 	} 
1273+ # ifUDC_STM32_SPEED  ==  USB_OTG_SPEED_HIGH 
1274+ 	data -> caps .hs  =  true;
1275+ #endif 
12631276
12641277	priv -> dev  =  dev ;
12651278	priv -> irq  =  UDC_STM32_IRQ ;
0 commit comments