Skip to content

drivers: usb: dwc2: refactor to use MMIO macros#103779

Merged
nashif merged 1 commit intozephyrproject-rtos:mainfrom
walidbadar:udc_dwc2
Apr 15, 2026
Merged

drivers: usb: dwc2: refactor to use MMIO macros#103779
nashif merged 1 commit intozephyrproject-rtos:mainfrom
walidbadar:udc_dwc2

Conversation

@walidbadar
Copy link
Copy Markdown
Contributor

@walidbadar walidbadar commented Feb 9, 2026

The DWC2 USB driver was accessing hardware registers through a raw dev->config->base pointer, which breaks on MMU-enabled platforms where physical addresses need to be mapped to virtual ones before use.

Use Zephyr's standard MMIO abstraction API by introducing DEVICE_MMIO_NAMED_RAM/ROM(core) in the driver's data and config structs, mapping the region at initialization time with DEVICE_MMIO_NAMED_MAP, and routing all register accesses through a dwc2_get_base() helper that calls DEVICE_MMIO_NAMED_GET. These changes allows the driver to be useful on MMU-based platforms like Broadcom BCM2711 SoC.

Copy link
Copy Markdown
Contributor

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

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

Please use git log to view examples at how to write commit message in this area. No idea what warning do you mean. I am not aware of any. Type conversion to mem_addr_t do not seem to be correct.

@walidbadar
Copy link
Copy Markdown
Contributor Author

walidbadar commented Feb 10, 2026

Please use git log to view examples at how to write commit message in this area. No idea what warning do you mean. I am not aware of any. Type conversion to mem_addr_t do not seem to be correct.

I apologize for the poorly written commit message, these log should help.

waleed-badar:zephyr$ west build -b rpi_4b samples/subsys/usb/cdc_acm
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/waleed-badar/workspace/zephyrproject/zephyr/samples/subsys/usb/cdc_acm
-- CMake version: 3.22.1
-- Found Python3: /usr/bin/python3 (found suitable version "3.10.12", minimum required is "3.10") found components: Interpreter 
-- Cache files will be written to: /home/waleed-badar/.cache/zephyr
-- Zephyr version: 4.3.99 (/home/waleed-badar/workspace/zephyrproject/zephyr)
-- Found west (found suitable version "1.5.0", minimum required is "0.14.0")
-- Board: rpi_4b, qualifiers: bcm2711
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.17.4 (/home/waleed-badar/zephyr-sdk-0.17.4)
-- Found toolchain: zephyr 0.17.4 (/home/waleed-badar/zephyr-sdk-0.17.4)
-- Found Dtc: /usr/bin/dtc (found suitable version "1.6.1", minimum required is "1.4.6") 
-- Found BOARD.dts: /home/waleed-badar/workspace/zephyrproject/zephyr/boards/raspberrypi/rpi_4b/rpi_4b.dts
-- Found devicetree overlay: /home/waleed-badar/workspace/zephyrproject/zephyr/samples/subsys/usb/cdc_acm/app.overlay
-- Generated zephyr.dts: /home/waleed-badar/workspace/zephyrproject/zephyr/build/zephyr/zephyr.dts
-- Generated pickled edt: /home/waleed-badar/workspace/zephyrproject/zephyr/build/zephyr/edt.pickle
-- Generated devicetree_generated.h: /home/waleed-badar/workspace/zephyrproject/zephyr/build/zephyr/include/generated/zephyr/devicetree_generated.h
/home/waleed-badar/workspace/zephyrproject/zephyr/build/zephyr/zephyr.dts:39.38-48.5: Warning (interrupt_provider): /soc/interrupt-controller@ff841000: Missing #address-cells in interrupt provider
Parsing /home/waleed-badar/workspace/zephyrproject/zephyr/samples/subsys/usb/cdc_acm/Kconfig
Loaded configuration '/home/waleed-badar/workspace/zephyrproject/zephyr/boards/raspberrypi/rpi_4b/rpi_4b_defconfig'
Merged configuration '/home/waleed-badar/workspace/zephyrproject/zephyr/samples/subsys/usb/cdc_acm/prj.conf'
Configuration saved to '/home/waleed-badar/workspace/zephyrproject/zephyr/build/zephyr/.config'
Kconfig header saved to '/home/waleed-badar/workspace/zephyrproject/zephyr/build/zephyr/include/generated/zephyr/autoconf.h'
-- Found GnuLd: /home/waleed-badar/zephyr-sdk-0.17.4/aarch64-zephyr-elf/aarch64-zephyr-elf/bin/ld.bfd (found version "2.38") 
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/waleed-badar/zephyr-sdk-0.17.4/aarch64-zephyr-elf/bin/aarch64-zephyr-elf-gcc
-- Using ccache: /usr/bin/ccache
-- Found gen_kobject_list: /home/waleed-badar/workspace/zephyrproject/zephyr/scripts/build/gen_kobject_list.py
-- Configuring done
-- Generating done
-- Build files have been written to: /home/waleed-badar/workspace/zephyrproject/zephyr/build
-- west build: building application
[1/157] Preparing syscall dependency handling

[3/157] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.3.99 (/home/waleed-badar/workspace/zephyrproject/zephyr), build: v4.3.0-5720-gb55a618c64f2
[150/157] Building C object zephyr/drivers/usb/udc/CMakeFiles/drivers__usb__udc.dir/udc_dwc2.c.obj
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c: In function 'dwc2_tx_fifo_write':
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:598:29: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  598 |                 sys_write32((uint32_t)buf->data,
      |                             ^
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c: In function 'dwc2_prep_rx':
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:797:29: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  797 |                 sys_write32((uint32_t)data,
      |                             ^
In file included from /home/waleed-badar/workspace/zephyrproject/zephyr/include/zephyr/logging/log.h:11,
                 from /home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:25:
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c: In function 'dwc2_read_fifo_setup':
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:2615:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]
 2615 |                 LOG_ERR("%d bytes SETUP", size);
      |                         ^~~~~~~~~~~~~~~~  ~~~~
      |                                           |
      |                                           size_t {aka long unsigned int}
/home/waleed-badar/workspace/zephyrproject/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/home/waleed-badar/workspace/zephyrproject/zephyr/include/zephyr/logging/log.h:62:22: note: in expansion of macro 'Z_LOG'
   62 | #define LOG_ERR(...) Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__)
      |                      ^~~~~
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:2615:17: note: in expansion of macro 'LOG_ERR'
 2615 |                 LOG_ERR("%d bytes SETUP", size);
      |                 ^~~~~~~
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:2615:27: note: format string is defined here
 2615 |                 LOG_ERR("%d bytes SETUP", size);
      |                          ~^
      |                           |
      |                           int
      |                          %ld
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c: In function 'dwc2_handle_oepint':
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:2905:51: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
 2905 |                         sys_cache_data_invd_range((void *)(addr - 8), 8);
      |                                                   ^
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:2906:45: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
 2906 |                         memcpy(priv->setup, (void *)(addr - 8), sizeof(priv->setup));
      |                                             ^
[157/157] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:          0 GB         0 GB
             RAM:        224 KB       512 KB     43.75%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/waleed-badar/workspace/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: rpi_4b/bcm2711
waleed-badar:zephyr$ 

Copy link
Copy Markdown
Contributor

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

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

Again, "Please use git log to view examples at how to write commit message in this area."

Comment thread drivers/usb/udc/udc_dwc2.c Outdated
}

sys_write32((uint32_t)buf->data,
sys_write32((mem_addr_t)buf->data,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c: In function 'dwc2_tx_fifo_write':
/home/waleed-badar/workspace/zephyrproject/zephyr/drivers/usb/udc/udc_dwc2.c:598:29: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
598 | sys_write32((uint32_t)buf->data,

I do not think it is right way to fix it. As its name suggests, it writes (or reads) a 32-bit value. Same below L:797 and L:2894 (where the change is just wrong).

@walidbadar walidbadar marked this pull request as draft February 11, 2026 11:10
@walidbadar walidbadar changed the title drivers: usb: designware: fix warnings in dwc2 driver drivers: usb: designware: refactor to use DEVICE_MMIO Feb 11, 2026
Comment thread drivers/usb/udc/udc_dwc2.c Outdated
Comment thread drivers/usb/udc/udc_dwc2.h
@walidbadar walidbadar force-pushed the udc_dwc2 branch 3 times, most recently from a890c34 to fb14b75 Compare February 23, 2026 22:32
@walidbadar walidbadar marked this pull request as ready for review February 23, 2026 22:39
Comment thread drivers/usb/udc/udc_dwc2.c
@walidbadar walidbadar marked this pull request as draft February 25, 2026 09:00
@walidbadar walidbadar force-pushed the udc_dwc2 branch 2 times, most recently from a3f1049 to 6434c0c Compare February 26, 2026 06:58
@walidbadar walidbadar marked this pull request as ready for review February 28, 2026 00:30
@walidbadar walidbadar changed the title drivers: usb: udc: refactor dwc2 driver to use MMIO macros drivers: usb: dwc2: refactor to use MMIO macros Mar 16, 2026
@walidbadar
Copy link
Copy Markdown
Contributor Author

@jfischer-no Could you please provide more details on what needs to be updated in the commit message? I reviewed the log history but didn’t see anything specific for this area.

@stephanosio
Copy link
Copy Markdown
Member

Rebased to fix CI failures.

@walidbadar
Copy link
Copy Markdown
Contributor Author

walidbadar commented Mar 18, 2026

Hi @jfischer-no, sorry to bother you again, could you please take a look?

Copy link
Copy Markdown
Contributor

@carlocaione carlocaione left a comment

Choose a reason for hiding this comment

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

Also having This PR migrates... in a commit message doesn't make any sense when (if) the PR is merged

Comment thread drivers/usb/udc/udc_dwc2.c Outdated
Comment on lines +160 to +161
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(udc_dwc2, CONFIG_UDC_DRIVER_LOG_LEVEL);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this here now?

Comment thread drivers/usb/udc/udc_dwc2.c Outdated
uint8_t setup[8];
};

#include "udc_dwc2.h"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add a comment explaining why now this header is here. This sucks but now udc_dwc2.h cannot be included anymore anywhere else, so add a comment there as well.

@walidbadar
Copy link
Copy Markdown
Contributor Author

walidbadar commented Apr 6, 2026

Needed a rebase due to zephyr-sdk changes.

@walidbadar
Copy link
Copy Markdown
Contributor Author

Also having This PR migrates... in a commit message doesn't make any sense when (if) the PR is merged

I have updated the commit message.

@walidbadar walidbadar requested a review from carlocaione April 6, 2026 20:12
carlocaione
carlocaione previously approved these changes Apr 7, 2026
@walidbadar walidbadar requested a review from tmon-nordic April 7, 2026 13:45
Comment thread drivers/usb/udc/udc_dwc2.c Outdated
The DWC2 USB driver was accessing hardware registers
through a raw dev->config->base pointer, which breaks on
MMU-enabled platforms where physical addresses need to be
mapped to virtual ones before use.

Use Zephyr's standard MMIO abstraction API by introducing
DEVICE_MMIO_NAMED_RAM/ROM(core) in the driver's data and
config structs, mapping the region at initialization time
with DEVICE_MMIO_NAMED_MAP, and routing all register accesses
through a dwc2_get_base() helper that calls DEVICE_MMIO_NAMED_GET.
These changes allows the driver to be useful on MMU-based
platforms like Broadcom BCM2711 SoC.

Signed-off-by: Muhammad Waleed Badar <walid.badar@gmail.com>
Copy link
Copy Markdown
Contributor

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

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

Thank you.

Tested on nRF54LM20A.

@walidbadar
Copy link
Copy Markdown
Contributor Author

Thank you.

Tested on nRF54LM20A.

Thank you for approving and also tested on Espressif ESP32S3 and Broadcom BCM2711.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Apr 9, 2026

@jfischer-no
Copy link
Copy Markdown
Contributor

Thank you.
Tested on nRF54LM20A.

Thank you for approving and also tested on Espressif ESP32S3 and Broadcom BCM2711.

I hope there will be a follow-up PR to add vendor quirks to support Broadcom BCM2711 in the tree.

@nashif nashif merged commit 0d158a6 into zephyrproject-rtos:main Apr 15, 2026
27 checks passed
@walidbadar walidbadar deleted the udc_dwc2 branch April 15, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: USB Universal Serial Bus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants