Skip to contentSkip to conversation

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

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

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#1 lore raw

PATCH v4 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 | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/gadget/rcar/common.c +4 -0
index 2ba022a3f2c..f7b34f0485f 100644
@@ -447,8 +447,12 @@ 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);
+	if (gadget)
+		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

v4 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 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      | 53 ++++++++++++++++++++++++---
 drivers/usb/gadget/rcar/renesas_usb.h |  1 +
 2 files changed, 48 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
#7 lore raw

PATCH v4 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 a5d56f8dbe4..051cab8a422 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
 	 *
@@ -361,13 +367,21 @@ static int usbhs_udc_otg_gadget_handle_interrupts(struct udevice *dev)
 
 static int usbhs_probe(struct usbhs_priv *priv, struct udevice *dev)
 {
+	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)
@@ -479,8 +493,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

PATCH v4 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 | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)
drivers/usb/gadget/rcar/common.c +23 -6
index f7b34f0485f..a5d56f8dbe4 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,7 +359,7 @@ 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 usbhs_priv *priv, struct udevice *dev)
 {
 	int ret;
 
@@ -396,21 +400,28 @@ 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);
 
@@ -423,7 +434,7 @@ static int usbhs_udc_otg_probe(struct udevice *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(&priv->usbhs_priv, dev);
 	if (ret < 0)
 		goto err_phy;
 
@@ -440,7 +451,7 @@ static int usbhs_udc_otg_probe(struct udevice *dev)
 err_phy:
 	generic_shutdown_phy(&priv->phy);
 err_clk:
-	clk_disable_bulk(&clk_bulk);
+	clk_disable_bulk(&priv->clk_bulk);
 	return ret;
 }
 
@@ -457,6 +468,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
#3 lore raw

Re: [PATCH v4 1/3] usb: gadget: rcar: Fix gadget registration lifecycle in remove

In reply to a message not in the archive
Quoted 24 lines · Marek Vasut
Quoted 22 lines · 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 | 4 ++++
  1 file changed, 4 insertions(+)
drivers/usb/gadget/rcar/common.c +1 -0
index 2ba022a3f2c..f7b34f0485f 100644
@@ -447,8 +447,12 @@ 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;
 
Quoted 5 lines

drivers/usb/gadget/rcar/common.c err_clk: · line 453

Quoted 2 lines
drivers/usb/gadget/rcar/common.c +1 -0
      usbhs_rcar3_power_ctrl(&priv->usbhs_priv, false);
+     gadget = usbhsg_get_gadget(&priv->usbhs_priv);

One more nitpick, usbhsg_get_gadget() can never return NULL, so this test for NULL is unnecessary, please drop it.

OK.

#6 lore raw

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

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

On 4/22/26 6:59 PM, Michele Bisogno wrote:

[...]

Quoted 8 lines
drivers/usb/gadget/rcar/common.c +1 -1
-static int usbhs_probe(struct usbhs_priv *priv)
+static int usbhs_probe(struct usbhs_priv *priv, struct udevice *dev)
  {
      int ret;
@@ -396,21 +400,28 @@ static int usbhs_udc_otg_probe(struct udevice *dev)
  {
      struct usbhs_priv_otg_data *priv = dev_get_priv(dev);

[...]

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

Quoted 2 lines
drivers/usb/gadget/rcar/common.c +1 -1
-     ret = usbhs_probe(&priv->usbhs_priv);
+     ret = usbhs_probe(&priv->usbhs_priv, dev);
Quoted 8 lines

It would be sufficient to pass dev to usbhs_probe().

usbhs_probe() can extract the struct usbhs_priv *priv from it:

"
struct usbhs_priv_otg_data *priv = dev_get_priv(dev); struct usbhs_priv *priv = &priv->usbhs_priv; "

OK, actually I thought about that but I was unsure which one was best.

Quoted 11 lines

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

Quoted 9 lines
      if (ret < 0)
              goto err_phy;
drivers/usb/gadget/rcar/common.c +1 -1
@@ -440,7 +451,7 @@ static int usbhs_udc_otg_probe(struct udevice *dev)
  err_phy:
      generic_shutdown_phy(&priv->phy);
  err_clk:
-     clk_disable_bulk(&clk_bulk);
+     clk_disable_bulk(&priv->clk_bulk);

Reset likely needs to be handled in this fail path too ?

OK.

Thanks for your review and suggestions!