Skip to contentSkip to conversation

usb: gadget: rcar: Add RZ/G2L support and lifecycle fixes

PATCH Superseded Michele Bisogno 1 participant 6 messages Apr 25 2 messages not in the archive

Subsystems (2)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#1 lore raw

PATCH v5 1/3 usb: gadget: rcar: Fix gadget registration lifecycle in remove

In reply to Michele Bisogno

The driver currently fails to unregister the USB gadget when the device is removed or the driver is unbound. This leads to dangling pointers in the UDC core and potential memory corruption.

Add a call to usb_del_gadget_udc() in the remove path to ensure a clean teardown of the gadget interface.

---
 drivers/usb/gadget/rcar/common.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/gadget/rcar/common.c +3 -0
index 2ba022a3f2c..d40d6736a54 100644
@@ -447,8 +447,11 @@ err_clk:
 static int usbhs_udc_otg_remove(struct udevice *dev)
 {
 	struct usbhs_priv_otg_data *priv = dev_get_priv(dev);
+	struct usb_gadget *gadget;
 
 	usbhs_rcar3_power_ctrl(&priv->usbhs_priv, false);
+	gadget = usbhsg_get_gadget(&priv->usbhs_priv);
+	usb_del_gadget_udc(gadget);
 	usbhs_mod_remove(&priv->usbhs_priv);
 	usbhs_fifo_remove(&priv->usbhs_priv);
 	usbhs_pipe_remove(&priv->usbhs_priv);
Signature · 1 line
2.34.1
#0 lore raw

v5 cover usb: gadget: rcar: Add RZ/G2L support and lifecycle fixes

This series adds support for the Renesas RZ/G2L (R9A07G044) USBHS controller. During development, several lifecycle and resource management issues were identified and addressed to satisfy RZ/G2L hardware requirements and prevent resource leaks across the driver.

The series is split into three logical parts to ensure bisectability:
1. A bugfix for the gadget deletion sequence during driver remove.
2. The addition of reset controller infrastructure and proper clock/reset teardown logic.
3. The RZ/G2L specific hardware parameters, SYSCFG logic, and compatible strings.

Changes in v5:
- Simplified usbhs_probe() signature to only take struct udevice *dev, deriving private data internally as requested.
- Fixed resource leak in usbhs_udc_otg_probe() error paths by adding reset_assert_bulk() to the teardown sequence.
- Standardized resource management by ensuring the probe error path perfectly mirrors the removal path.

Changes in v4:
- Split the monolithic patch into a 3-patch series as requested.
- Added usb_del_gadget_udc() to fix resource leak on driver remove.
- Implemented bulk reset controller support (required for RZ/G2L).
- Improved error handling in probe() to ensure clocks are disabled if reset initialization or deassertion fails.
- Moved reset and clock handles to private data to ensure persistence.
- Reorganized hunks to ensure each patch in the series compiles independently (bisectability).

Michele Bisogno (3):
usb: gadget: rcar: Fix gadget registration lifecycle in remove
usb: gadget: rcar: Add support for reset controller
usb: gadget: rcar: Add support for RZ/G2L (R9A07G044)

 drivers/usb/gadget/rcar/common.c      | 58 +++++++++++++++++++++++----
 drivers/usb/gadget/rcar/renesas_usb.h |  1 +
 2 files changed, 52 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
#5 lore raw

PATCH v5 3/3 usb: gadget: rcar: Add support for RZ/G2L (R9A07G044)

The Renesas RZ/G2L (and RZ/G2LC) USBHS controller requires the CNEN bit in the SYSCFG register to be set for function operation. Additionally, its CFIFO is byte-addressable.

Introduce a new renesas_usbhs_driver_param structure for the RZ/G2L SoC and link it via the udevice_id data pointer. Update usbhs_probe() to accept the udevice pointer to retrieve these parameters during initialization.

This alignment follows the logic used in the Linux kernel renesas_usbhs driver.

---
 drivers/usb/gadget/rcar/common.c      | 20 ++++++++++++++++++++
 drivers/usb/gadget/rcar/renesas_usb.h |  1 +
 2 files changed, 21 insertions(+)
drivers/usb/gadget/rcar/common.c +20 -0
index 32c4f1fadb1..580a0fbf090 100644
@@ -92,6 +92,12 @@ void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
 	u16 val  = HSE | USBE;
 
+	/* CNEN bit is required for function operation */
+	if (usbhs_get_dparam(priv, has_cnen)) {
+		mask |= CNEN;
+		val  |= CNEN;
+	}
+
 	/*
 	 * if enable
 	 *
@@ -363,13 +369,21 @@ static int usbhs_probe(struct udevice *dev)
 {
 	struct usbhs_priv_otg_data *otg_priv = dev_get_priv(dev);
 	struct usbhs_priv *priv = &otg_priv->usbhs_priv;
+	struct renesas_usbhs_driver_param *plat_param;
 	int ret;
 
+	plat_param = (struct renesas_usbhs_driver_param *)dev_get_driver_data(dev);
+
 	priv->dparam.type = USBHS_TYPE_RCAR_GEN3;
 	priv->dparam.pio_dma_border = 64;
 	priv->dparam.pipe_configs = usbhsc_new_pipe;
 	priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
 
+	if (plat_param) {
+		priv->dparam.has_cnen		= plat_param->has_cnen;
+		priv->dparam.cfifo_byte_addr	= plat_param->cfifo_byte_addr;
+	}
+
 	/* call pipe and module init */
 	ret = usbhs_pipe_probe(priv);
 	if (ret < 0)
@@ -482,8 +496,14 @@ static int usbhs_udc_otg_remove(struct udevice *dev)
 	return dm_scan_fdt_dev(dev);
 }
 
+static struct renesas_usbhs_driver_param rzg2l_param = {
+	.has_cnen = 1,
+	.cfifo_byte_addr = 1,
+};
+
 static const struct udevice_id usbhs_udc_otg_ids[] = {
 	{ .compatible = "renesas,rcar-gen3-usbhs" },
+	{ .compatible = "renesas,rzg2l-usbhs", .data = (unsigned long)&rzg2l_param },
 	{},
 };
 
drivers/usb/gadget/rcar/renesas_usb.h +1 -0
index 8155e3dcaf6..140a70251cf 100644
@@ -111,6 +111,7 @@ struct renesas_usbhs_driver_param {
 	u32 has_otg:1; /* for controlling PWEN/EXTLP */
 	u32 has_sudmac:1; /* for SUDMAC */
 	u32 has_usb_dmac:1; /* for USB-DMAC */
+	u32 has_cnen:1;
 	u32 cfifo_byte_addr:1; /* CFIFO is byte addressable */
 #define USBHS_USB_DMAC_XFER_SIZE	32	/* hardcode the xfer size */
 	u32 multi_clks:1;
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v5 2/3] usb: gadget: rcar: Add support for reset controller

In reply to a message not in the archive
Quoted 7 lines · Marek Vasut
Quoted 5 lines

#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#include <usb.h>
+#include <reset.h>

Please keep the header list sorted.

Ops, OK!

Quoted 15 lines
Quoted 1 line
  #include "common.h"

[...]

clk_enable_bulk() needs to jump to err_clock_enable() on failure, this is a bug in the existing driver .

drivers/usb/gadget/rcar/common.c static int usbhs_udc_otg_probe(struct udevice *dev) · line 425

Quoted 7 lines
drivers/usb/gadget/rcar/common.c +7 -0
+     ret = reset_get_bulk(dev, &priv->reset_bulk);
+     if (ret)
+             goto err_clk;
+
+     ret = reset_deassert_bulk(&priv->reset_bulk);
+     if (ret)
+             goto err_clk;
Quoted 3 lines

This has to jump to fail path which does reset_release_bulk() , i.e.

                goto err_reset_deassert;

OK!

Quoted 20 lines
Quoted 20 lines
      clrsetbits_le32(priv->base + UGCTRL2, UGCTRL2_USB0SEL_MASK, UGCTRL2_USB0SEL_EHCI);
      clrsetbits_le16(priv->base + LPSTS, LPSTS_SUSPM, LPSTS_SUSPM);

      ret = generic_setup_phy(dev, &priv->phy, 0, PHY_MODE_USB_OTG, 1);
      if (ret)
drivers/usb/gadget/rcar/common.c +2 -2
-             goto err_clk;
+             goto err_reset;
 
      priv->phybase = dev_read_addr_ptr(priv->phy.dev);
 
      priv->usbhs_priv.pdev = &priv->usbhs_dev;
      priv->usbhs_priv.base = priv->base;
      priv->usbhs_dev.dev.driver_data = &priv->usbhs_priv;
-     ret = usbhs_probe(&priv->usbhs_priv);
+     ret = usbhs_probe(dev);
      if (ret < 0)
              goto err_phy;
@@ -439,8 +452,10 @@ static int usbhs_udc_otg_probe(struct udevice *dev)
 
Quoted 7 lines

drivers/usb/gadget/rcar/common.c static int usbhs_udc_otg_probe(struct udevice *dev) · line 456

Quoted 4 lines
drivers/usb/gadget/rcar/common.c +2 -0
  err_phy:
      generic_shutdown_phy(&priv->phy);
+err_reset:
+     reset_assert_bulk(&priv->reset_bulk);

err_reset_deassert:

        reset_release_bulk(&priv->reset_bulk);

Alright.

Quoted 7 lines

drivers/usb/gadget/rcar/common.c static int usbhs_udc_otg_probe(struct udevice *dev) · line 458

Quoted 3 lines
drivers/usb/gadget/rcar/common.c +1 -1
  err_clk:
-     clk_disable_bulk(&clk_bulk);
+     clk_disable_bulk(&priv->clk_bulk);

err_clk_enable:

        clk_release_bulk(&priv->clk_bulk);

OK.

Quoted 15 lines
Quoted 15 lines
drivers/usb/gadget/rcar/common.c +6 -0
      return ret;
  }
 
@@ -456,6 +471,12 @@ static int usbhs_udc_otg_remove(struct udevice *dev)
      usbhs_fifo_remove(&priv->usbhs_priv);
      usbhs_pipe_remove(&priv->usbhs_priv);
 
+     reset_assert_bulk(&priv->reset_bulk);
+     reset_release_bulk(&priv->reset_bulk);
+
+     clk_disable_bulk(&priv->clk_bulk);
+     clk_release_bulk(&priv->clk_bulk);
+
      generic_shutdown_phy(&priv->phy);
 
Quoted 3 lines

drivers/usb/gadget/rcar/common.c static int usbhs_udc_otg_remove(struct udevice *dev) · line 482

Quoted 1 line
drivers/usb/gadget/rcar/common.c +0 -0
      return dm_scan_fdt_dev(dev);

Thank you for your help!

Thanks for your patience! :)

#2 lore raw

PATCH v5 2/3 usb: gadget: rcar: Add support for reset controller

In reply to Michele Bisogno

Some Renesas SoCs, such as the RZ/G2L, require the USBHS core to be explicitly deasserted from reset before register access is possible.

Update the OTG probe to handle a bulk reset controller. To maintain hardware stability, the reset is deasserted after clocks are enabled in probe(), and asserted before clocks are disabled in remove().

Update the error paths in probe to ensures clocks are disabled if the reset initialization fails.

---
 drivers/usb/gadget/rcar/common.c | 35 +++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)
drivers/usb/gadget/rcar/common.c +28 -7
index d40d6736a54..32c4f1fadb1 100644
@@ -16,6 +16,7 @@
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
 #include <usb.h>
+#include <reset.h>
 
 #include "common.h"
 
@@ -290,6 +291,9 @@ struct usbhs_priv_otg_data {
 	void __iomem		*base;
 	void __iomem		*phybase;
 
+	struct clk_bulk clk_bulk;
+	struct reset_ctl_bulk reset_bulk;
+
 	struct platform_device	usbhs_dev;
 	struct usbhs_priv	usbhs_priv;
 
@@ -355,8 +359,10 @@ static int usbhs_udc_otg_gadget_handle_interrupts(struct udevice *dev)
 	return 0;
 }
 
-static int usbhs_probe(struct usbhs_priv *priv)
+static int usbhs_probe(struct udevice *dev)
 {
+	struct usbhs_priv_otg_data *otg_priv = dev_get_priv(dev);
+	struct usbhs_priv *priv = &otg_priv->usbhs_priv;
 	int ret;
 
 	priv->dparam.type = USBHS_TYPE_RCAR_GEN3;
@@ -396,34 +402,41 @@ static int usbhs_udc_otg_probe(struct udevice *dev)
 {
 	struct usbhs_priv_otg_data *priv = dev_get_priv(dev);
 	struct usb_gadget *gadget;
-	struct clk_bulk clk_bulk;
 	int ret = -EINVAL;
 
 	priv->base = dev_read_addr_ptr(dev);
 	if (!priv->base)
 		return -EINVAL;
 
-	ret = clk_get_bulk(dev, &clk_bulk);
+	ret = clk_get_bulk(dev, &priv->clk_bulk);
 	if (ret)
 		return ret;
 
-	ret = clk_enable_bulk(&clk_bulk);
+	ret = clk_enable_bulk(&priv->clk_bulk);
 	if (ret)
 		return ret;
 
+	ret = reset_get_bulk(dev, &priv->reset_bulk);
+	if (ret)
+		goto err_clk;
+
+	ret = reset_deassert_bulk(&priv->reset_bulk);
+	if (ret)
+		goto err_clk;
+
 	clrsetbits_le32(priv->base + UGCTRL2, UGCTRL2_USB0SEL_MASK, UGCTRL2_USB0SEL_EHCI);
 	clrsetbits_le16(priv->base + LPSTS, LPSTS_SUSPM, LPSTS_SUSPM);
 
 	ret = generic_setup_phy(dev, &priv->phy, 0, PHY_MODE_USB_OTG, 1);
 	if (ret)
-		goto err_clk;
+		goto err_reset;
 
 	priv->phybase = dev_read_addr_ptr(priv->phy.dev);
 
 	priv->usbhs_priv.pdev = &priv->usbhs_dev;
 	priv->usbhs_priv.base = priv->base;
 	priv->usbhs_dev.dev.driver_data = &priv->usbhs_priv;
-	ret = usbhs_probe(&priv->usbhs_priv);
+	ret = usbhs_probe(dev);
 	if (ret < 0)
 		goto err_phy;
 
@@ -439,8 +452,10 @@ static int usbhs_udc_otg_probe(struct udevice *dev)
 
 err_phy:
 	generic_shutdown_phy(&priv->phy);
+err_reset:
+	reset_assert_bulk(&priv->reset_bulk);
 err_clk:
-	clk_disable_bulk(&clk_bulk);
+	clk_disable_bulk(&priv->clk_bulk);
 	return ret;
 }
 
@@ -456,6 +471,12 @@ static int usbhs_udc_otg_remove(struct udevice *dev)
 	usbhs_fifo_remove(&priv->usbhs_priv);
 	usbhs_pipe_remove(&priv->usbhs_priv);
 
+	reset_assert_bulk(&priv->reset_bulk);
+	reset_release_bulk(&priv->reset_bulk);
+
+	clk_disable_bulk(&priv->clk_bulk);
+	clk_release_bulk(&priv->clk_bulk);
+
 	generic_shutdown_phy(&priv->phy);
 
 	return dm_scan_fdt_dev(dev);
Signature · 1 line
2.34.1