Commit graph

62 commits

Author SHA1 Message Date
Mathieu Choplain
2e998cf17d drivers: usb: udc: stm32: use instance MPS for IN endpoints capabilities
When setting endpoint capabilities, the driver uses the MPS stored in each
instance's configuration for OUT endpoints, but used a hardcoded 1023 for
IN endpoints.

Use the instance MPS when preparing IN endpoints' capabilities too.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-31 10:22:29 -04:00
Mathieu Choplain
2e7ad10247 drivers: usb: udc: stm32: use addr_before_status capability
Instead of snooping SETUP packets for SET_ADDRESS request to call the HAL
SetAddress() function manually, mark device with the addr_before_status
capability such that the UDC stack will call udc_stm32_set_address()
before submitting status by itself.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-30 18:38:29 -04:00
Etienne Carriere
464d929651 drivers: usb: udc: stm32: split variable definition in multi-line
Split variables definition into 2 line as per Zephyr coding style
expectations since they are of different type.

No functional changes.

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2025-10-24 08:57:55 -07:00
Etienne Carriere
0b297bdbb8 drivers: usb: udc: stm32: remove useless test on buffer length
Remove useless test on buffer length in handle_msg_setup() since the
buffer was allocated few lines above with a known non zero size.

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2025-10-24 08:57:55 -07:00
Etienne Carriere
f8addbaa28 drivers: usb: udc: stm32: inline comment reference to selected_speed
Clarify inline comment referring to field selected_speed of
struct udc_stm32_config.

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2025-10-24 08:57:55 -07:00
Etienne Carriere
7df2ce80ae drivers: usb: udc: stm32: remove useless OR operation on return value
Remove useless OR operation on return value is udc_stm32_enable()
since the return value is always 0 before it's OR-ed with a new return
value.

No functional changes.

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2025-10-24 08:57:55 -07:00
Etienne Carriere
86616fd406 drivers: usb: udc: stm32: test essentially boolean value
Change implementation to ensure all boolean test are done on essentially
boolean values.

No functional changes.

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2025-10-24 08:57:55 -07:00
Etienne Carriere
16237e4147 drivers: usb: udc: stm32: rename endpoint config variable
Rename endpoint configuration pointer variable in HAL_PCD_ResetCallback(),
udc_stm32_ep_mem_config() and udc_stm32_ep_disable() from ep to ep_cfg
to prevent confusion since ep is usually used as endpoint address.

For consistency, also use ep_cfg elsewhere where an endpoint config
pointer is used, instead of using either cfg, ecfg and ep_cfg.

No functional changes.

Suggested-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2025-10-24 08:57:55 -07:00
Etienne Carriere
66478fd11a drivers: usb: udc: stm32: test HAL return value
Add missing test of some HAL functions return value.

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2025-10-24 08:57:55 -07:00
Mathieu Choplain
f95b84b501 drivers: usb: udc: stm32: add support for USB on STM32WBA6x
Add support for USB on STM32WBA6x series, along with a tiny rework of how
the st,stm32u5-otghs-phy is handled to make the code more generic by
actually consuming DT information.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-22 15:59:27 +03:00
Mathieu Choplain
f8c0da3444 drivers: usb: udc: stm32: fix FS mode on OTG_FS for STM32F4 series
On most parts of the STM32F4 series, when the OTG_HS controller is used in
FS mode, the ULIP **low-power** clock must also be disabled for proper
operation. This was done properly in an old version of the driver but was
lost as part of refactoring[1].

Re-introduce ULPI low-power clock disable when OTG_HS is used in FS mode.

[1] See commit e31ddec781

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-21 17:21:52 +03:00
Mathieu Choplain
f413980cc0 drivers: usb: udc: stm32: fix N6 power-up sequence logic
Actually do what the comment says and wait for Vdd33USB to be ready,
instead of waiting for the opposite.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-21 17:21:18 +03:00
Mathieu Choplain
dca6b87668 drivers: usb: udc: stm32: handle multi-packet-Data OUT Control transfers
During a Host-to-Device Control transfer, an arbitrary amount of data is
sent from Host to the Device using OUT Data packets. If the total amount of
data to transfer, communicated via the wLength field of the SETUP packet,
exceeds the EP0 MaxPacketSize, several Data packets until all data has been
transfered.

Combined with HAL behavior, the STM32 driver did not handle this situation
properly and always ended reception after a single Data packet was received
regardless of whether or not all data had actually been received from Host.

Modify driver to handle this situation properly by keeping track of how
much data has been received and restarting transfers until we have received
everything the Host promised it would send.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-20 14:22:28 -04:00
Mathieu Choplain
2a0a6f5cca drivers: usb: udc: stm32: empty transfer queue when SETUP packet arrives
Drop all existing transfers in control endpoints' queue when a new SETUP
packet is received. Also use the appropriate net_buf API in a nearby place.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-20 14:22:28 -04:00
Mathieu Choplain
1609f10082 drivers: usb: udc: stm32: configure OTGFS/HS RxFIFO size using Kconfig
Create a new Kconfig option allowing to tweak the RxFIFO size on OTG_FS
and OTG_HS instances, and replace the old hardcoded method with this new
mecanism.

The default value of 600 bytes yields a similar size to the the previous
hardcoded default of 160 words (= 640 bytes) when combined with the fixed
overhead computed by the driver (~56 bytes on OTG_FS with 6 endpoints).

Also fix a tiny error in a logging message (DRAM size in bytes, not bits).

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
6efa7f31fc drivers: usb: udc: stm32: allow EP max packet size up to HW capabilities
The maximal packet size (for non-control endpoints) was obtained by the
driver from HAL definitions which appear to not properly reflect hardware
capabilities.

Update driver to allow endpoints with wMaxPacketSize up to the maximal
value allowed by the USB Specification depending on operation mode, since
all STM32 USB controllers always support these values. Also move the EP
max packet size field in the 'struct udc_stm32_config' to avoid implicit
padding and add a documentation comment.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
bbb8aba17d drivers: usb: udc: stm32: accept non-word-aligned MaxPacketSize or FIFOs
STM32 OTG USB controllers use word-addressable RAM with a 32-bit word size
so all allocations from the USB SRAM must be 32-bit aligned. The driver did
not accept unaligned values of wMaxPacketSize, and FIFO allocation code was
implicitly expecting FIFO sizes to be aligned as well (since it allocated
"bytes / 4" without rounding up).

Update driver to accept values of wMaxPacketSize that aren't word-aligned
and to allocate properly sized FIFOs sizes when an unaligned size has been
requested.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
ac998f7109 drivers: usb: udc: stm32: clean up handling of USB buffer table
The ST USB controller (compatible 'st,stm32-usb') is fitted with private
SRAM called the Private Memory Area or PMA. This is primarily used to hold
data transfered over USB, but it also contains the so-called 'buffer table'
(a.k.a. BTABLE) which holds the base address and size of buffers in the PMA
allocated to each endpoint. The BTABLE is placed by the driver at the start
of the PMA and occupies a fixed size ('USB_BTABLE_SIZE'), which was stored
in the driver configuration field 'pma_offset'. This mechanism is unused on
non-ST USB controllers from STM32 microcontrollers, but USB_BTABLE_SIZE is
still defined (to a dummy value) and stored in the 'pma_offset' field which
becomes unused.

Remove the 'USB_BTABLE_SIZE' definition and the 'pma_offset' field from the
driver configuration, and update the ST USB controller-specific verison of
'udc_stm32_mem_init' to derive the BTABLE size from the number of endpoints
that the controller has instead.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
3788b2c0a6 drivers: usb: udc: stm32: turn EP0 max packet size into a constant
The EP0 max packet size was de facto a constant because its value was the
same regardless of which USB IP was in use. However, it was stored as part
of the instance configuration anyways which is wasteful and slower.

Create new "UDC_STM32_EP0_MAX_PACKET_SIZE" driver-level constant with which
all usage of the per-instance configuration field is replaced.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Alain Volmat
c3995b65ba drivers: usb: udc: stm32: avoid unnecessary clock_control_subsys_t cast
Avoid incorrect (clock_control_subsys_t *) incorrect cast and
remove the cast of pclken in calls to the clock_control_
framework.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
2025-10-02 16:47:53 +02:00
Mathieu Choplain
092a0820c4 drivers: usb: udc: stm32: enhance PHY type & operating speed determination
Rework how the STM32 UDC driver determines the PHY used by a given instance
and the speed at which the instance is able to and allowed to operate.

The PHY determination now uses the 'phys' property instead of looking at
whether nodes with a specific compatible were enabled. Similarly, the speed
determination takes into account the 'maximum-speed' property, along with
the selected PHY's and the instance's capabilities.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-09-30 19:36:55 +02:00
Mathieu Choplain
e31ddec781 drivers: usb: udc: stm32: rework PWR and PHY initialization
Rework how the driver performs PWR configuration and initializes HS PHYs.

Originally, the initialization logic was split in half without any real
logic. There were also attempts to share some logic but this resulted in
code that was difficult to understand.

Initialize PWR and PHY in well separated steps, and use explicit series
and PHY type checks in each part to ensure the logic is easy to understand.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-09-30 19:36:55 +02:00
Mathieu Choplain
7ec83e5c95 drivers: usb: udc: stm32: use HAL PCD macros for speed
Rework the STM32 USB UDC driver to use HAL PCD macros for speed selection,
as should have always been done since that is the library used by the shim
driver. Also dummy definitions to ensure cross-series compatibility
without many #if blocks in the driver.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-09-30 19:36:55 +02:00
Mathieu CHOPLAIN
3ae98de48e drivers: usb: udc: stm32: handle endpoint halt properly
Handle endpoints in halted state properly by marking endpoints as halted
when appropriate, and inhibiting transfers involving halted endpoints.

Co-authored-by: IBEN EL HADJ MESSAOUD Marwa <marwa.ibenelhadjmessaoud-ext@st.com>
Signed-off-by: Mathieu CHOPLAIN <mathieu.choplain-ext@st.com>
2025-09-18 09:47:36 +01:00
BUDKE Gerson Fernando
77070941fa dts: phy: Add clock-reference prop in stm32u5-otghs-phy
The OTG_HS PHY from stm32u5a5xx device require the correct reference
clock frequency selction in SYSCFG_OTGHSPHYCR. The current default is
hard coded to 16Mhz (which matches the development board crystal).
However, a custom board my require a different crystal and then the
USB will not work. This add a required field in the
st,stm32u5-otghs-phy binding to force user to select the correct
clock reference. The current nucleo_u5a5zj_q baord was updated to
reflect the mandatory field.

Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
2025-08-20 12:05:24 +02:00
Johann Fischer
58b37007d2 drivers: udc_stm32: fix remote wakeup handling
Clear the suspended status and submit the resume event once the remote
wakeup signaling is finished. Clear the suspended status on bus reset as
well.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-08-18 11:56:57 +02:00
IBEN EL HADJ MESSAOUD Marwa
8b355e9306 drivers: usb: udc: prevent USB clock disable in sleep mode
Prevent disabling OTG HS and USBPHY clocks
during sleep on STM32U5 series

Disabling these clocks during sleep mode
was causing USB device initialization issues

Signed-off-by: IBEN EL HADJ MESSAOUD Marwa <marwa.ibenelhadjmessaoud-ext@st.com>
2025-07-22 08:11:49 -04:00
Marcin Niestroj
cd17871cfe drivers: udc: stm32: fix USB_OTG_HS_EMB_PHY macro value check
USB_OTG_HS_EMB_PHY macro is always defined, so
`defined(USB_OTG_HS_EMB_PHY)` always results in true. This means that
driver always selected `USB_OTG_SPEED_HIGH_IN_FULL` and never
`USB_OTG_SPEED_HIGH`.

Fix that by checking value of defined macro.

Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2025-07-11 11:42:33 -10:00
Riku Karjalainen
9493af937d drivers: usb: stm32: fix stm32u5x clock disable in sleep mode
Replace logical OR with bitwise OR.

Signed-off-by: Riku Karjalainen <riku.karjalainen@vaisala.com>
2025-06-23 15:43:55 +01:00
Johann Fischer
7b287ec133 drivers: udc: disable SOF interrupt by default
If the new Kconfig option is disabled, no SOF events are passed to the
higher layer.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-06-23 15:43:32 +01:00
IBEN EL HADJ MESSAOUD Marwa
9b14f9b924 drivers: usb: Enable USB and UDC OTGPHY clock
- Reset specific configuration bits in
  USB1_HS_PHYC->USBPHYC_CR before setting new values.
- Set the Frequency Selection (FSEL) bits to operate
  the USB PHY Control Register at 24 MHz for proper communication.
- Enable the OTGPHY1 peripheral clock using LL_AHB5_GRP1_EnableClock.

Signed-off-by: IBEN EL HADJ MESSAOUD Marwa <marwa.ibenelhadjmessaoud-ext@st.com>
2025-05-28 10:04:40 +02:00
IBEN EL HADJ MESSAOUD Marwa
1844e98d96 drivers: usb: Enhance USB and UDC drivers for STM32 OTG configurations
- Unified the handling of USB OTG HS and USB OTG FS
  by removing redundant preprocessor conditionals.
- Introduced a new macro `UDC_STM32_BASE_ADDRESS`
  to dynamically set the USB base address.

Signed-off-by: IBEN EL HADJ MESSAOUD Marwa <marwa.ibenelhadjmessaoud-ext@st.com>
2025-04-14 11:48:54 +02:00
IBEN EL HADJ MESSAOUD Marwa
6f005fdf7d drivers: usb: Add STM32N6 family support to UDC driver
Add STM32N6 family support to UDC driver

Signed-off-by: IBEN EL HADJ MESSAOUD Marwa <marwa.ibenelhadjmessaoud-ext@st.com>
2025-04-14 11:48:54 +02:00
Khaoula Bidani
00cc4441aa drivers: usb: udc_stm32: Update UDC speed macros
Updated UDC speed definition macros for STM32 series
to handle different speed configurations
based on the SoC compatibility.

Added conditional checks for full-speed definitions
using DT_HAS_COMPAT_STATUS_OKAY for st_stm32_usb.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
Signed-off-by: IBEN EL HADJ MESSAOUD Marwa <marwa.ibenelhadjmessaoud-ext@st.com>
2025-04-08 08:58:18 +02:00
Khaoula Bidani
ba1ad38f99 drivers: usb: udc: simplify code of maximum-speed
Add defines to avoid magic values in the dtsi.
Replace usb_dc_stm32_get_maximum_speed() with device tree property.
- Use DT_INST_STRING_UPPER_TOKEN(0, maximum_speed) to set the USB speed.
- Remove PCD_SPEED_FULL initialization.

Signed-off-by: Khaoula Bidani <khaoula.bidani-ext@st.com>
2025-04-03 08:43:29 +02:00
Tomasz Moń
1cccf28d5e drivers: usb: udc: Reduce unnecessary ep config lookups
UDC API passes struct udc_ep_config to all functions. Some UDC functions
were using endpoint config structure while some were using device and
endpoint number. This API inconsistency led to completely unnecessary
endpoint structure lookups. Remove unnecessary lookups by using endpoint
config structure pointer where it makes sense.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2025-03-31 14:32:17 +02:00
Johann Fischer
05fb46cb91 drivers: udc_stm32: handle ZLP flag for control transfers as well
I mistakenly assumed in the commit 6aaad0a5cd
("drivers: udc_stm32: handle ZLP flag") that the HAL driver would handle
ZLP flag in control transfers itself, but that does not seem to be the
case.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-02-25 20:41:03 +01:00
Tomasz Moń
23232e6bb2 drivers: udc: Do not allow lock/unlock to fail
USB stack does not check api->lock() and api->unlock() return value and
all UDC drivers block without timeout in its lock() and unlock() api
implementations. There is no realistic way to handle lock() and unlock()
errors without making USB device stack API unnecessarily complex.

Remove the return type from lock() and unlock() to make it clear that
the functions must not fail.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2025-02-11 10:13:03 +01:00
Pisit Sawangvonganan
0ef38013b7 drivers: udc: stm32: update STM32_CLOCK_CHECK definition
Introduce `UDC_STM32_CLOCK_CHECK` Kconfig option since
`USB_DC_STM32_CLOCK_CHECK` was intended for use with `usb_dc_stm32.c`,
which is mutually exclusive with `udc_stm32.c`.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2025-01-31 11:50:01 +01:00
Tobias Pisani
1ec660d916 drivers: udc: stm32: Fix RX FIFO min size
On USB HS, the previous lower limit of 64 is too small, the value
160 has been chosen apparently through trial and error.

See 1204aa25 for the original implementation in the old device driver.

Signed-off-by: Tobias Pisani <mail@topisani.dev>
2025-01-28 18:13:59 +01:00
Tobias Pisani
227058d01c drivers: udc: stm32: set the hs capability if high speed is available
This fixes usbd_caps_speed(), which is used in sample_usbd_init, and the
documentation. Without it, no high speed configuration would be added.

Signed-off-by: Tobias Pisani <mail@topisani.dev>
2025-01-28 18:13:59 +01:00
Tobias Pisani
7b19e4f19b drivers: udc: stm32: Offload data callbacks to thread
All HAL callback handling is offloaded to a separate thread, as they
involve non isr-compatible operations such as mutexes.

Fixes #61464

Signed-off-by: Tobias Pisani <mail@topisani.dev>
2025-01-28 18:13:59 +01:00
Pisit Sawangvonganan
8983a610a3 drivers: udc: stm32: fix DOUT stage handling for st_stm32_usb
In the device that has `DT_DRV_COMPAT` equal to `st_stm32_usb`,
its behavior differs from `st_stm32_otghs` and `st_stm32_otgfs`
due to the underlying `HAL_PCD_IRQHandler`.
As a result, calling `usbd_ctrl_feed_dout` for the DOUT stage is not
compatible with the `st_stm32_usb` device.

Instead of calling `usbd_ctrl_feed_dout`, we still require flushing
the TX FIFO to the host.
This is achieved by calling `HAL_PCD_EP_Receive` with `len` = `0`.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2025-01-28 00:04:50 +01:00
David Schneider
ded2e0ba4a drivers: udc_stm32: set address only for standard device requests
Any request 5 did set the address even if it's a non standard
request like vendor specific requests.

Signed-off-by: David Schneider <schneidav81@gmail.com>
2024-11-18 13:17:31 -05:00
Johann Fischer
8617e46849 drivers: udc: do not use the MPS value directly for the endpoint size
Instead, use the helper to get the size field from the MPS value.
MPS value may contain information about additional transaction
per microframe (bits 12..11).

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-08-21 18:25:30 +01:00
Henrik Brix Andersen
69fe9b0c50 net: buf: remove use of special putter and getter functions
Convert users of net_buf_put() and net_buf_get() functions to use
non-wrapped putters and getters k_fifo_put() and k_fifo_get().

Special handling of net_bufs in k_fifos is no longer needed after commit
3d306c181f, since these actions are now
atomic regardless of any net_buf fragments.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2024-08-16 09:55:11 +02:00
Francois Ramu
d0108b2673 drivers: udc: stm32 usb device controller in sleep mode
Force disabling the USB OTG HS and PHY clock during sleepmode
By default, that clock is enabled by clock gating during sleep
mode. Like stm32H7, it has to be kept running.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
2024-08-01 12:36:58 +02:00
Francois Ramu
ac6bdffd01 drivers: udc: stm32 usb device controller clock and power
Special sequence to enable clock and power for the OTG HS
peripheral of the stm32U59x serie
This code is based on the stm32Cube HAL_HCD_MspInit/DeInit.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
2024-08-01 12:36:58 +02:00
Francois Ramu
ea6c5f0e85 drivers: usb: stm32 usb device controller correct USB PHY
Set the correct the phy_itface depending on the setting
Save few lines after resetting the priv->pcd structure
with the memset

Signed-off-by: Francois Ramu <francois.ramu@st.com>
2024-08-01 12:36:58 +02:00
Johann Fischer
043aa837fd drivers: udc_stm32: remove wrong header and fix udc_ep_enable()
Driver includes wrong header zephyr/usb/usb_device.h and uses defines
from include/zephyr/drivers/usb/usb_dc.h.
Also fix udc_ep_enable() implementation in general. HAL_PCD_EP_Open()
takes the ep_type parameter as uint8_t integer type and the shim driver
should not just pass int type.
It is recommended that drivers use ep_cfg or cfg for struct
udc_ep_config, fix this as well.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-07-30 18:29:26 +01:00