Skip to contentSkip to conversation

usb: dwc3: add Realtek DHC RTD SoC dwc3 glue layer driver

PATCH In review Stanley Chang 2 participants 7 messages Aug 26, 2023 – Nov 4, 2023 2 messages not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v6 1/2 usb: dwc3: add Realtek DHC RTD SoC dwc3 glue layer driver

Realtek DHC RTD SoCs integrate dwc3 IP and has some customizations to support different generations of SoCs.

The RTD1619b subclass SoC only supports USB 2.0 from dwc3. The driver can set a maximum speed to support this. Add role switching function, that can switch USB roles through other drivers, or switch USB roles through user space through set /sys/class/usb_role/.

---
v5 to v6 minor change:
1. Revised a function return check.
2. Add address for device name.
3. Change dev_info to dev_dbg.
v4 to v5 change:
1. Use enumeration and define usb_role uniformly instead of dr_mode to avoid confusing dr_mode and usb_role.
2. Split the register read and write to separate operations.
v3 to v4 change:
Get max-speed from dwc3 node.
Add the register set for pm control.
v2 to v3 change:
Remove the support_drd_mode of struct dwc3_rtk. Remove unlink_usb3_port and disable_usb3_phy. Disabled usb3 phy if the max speed is not super-speed.
v1 to v2 change:
Remove the code about the property realtek,enable-l4icg. Select USB_ROLE_SWITCH in Kconfig.
Add dependency OF and ARCH_REALTEK in Kconfig.

---
 drivers/usb/dwc3/Kconfig    |  11 +
 drivers/usb/dwc3/Makefile   |   1 +
 drivers/usb/dwc3/dwc3-rtk.c | 475 ++++++++++++++++++++++++++++++++++++
 3 files changed, 487 insertions(+)
 create mode 100644 drivers/usb/dwc3/dwc3-rtk.c
drivers/usb/dwc3/Kconfig +11 -0
index 98efcbb76c88..5fc27b20df63 100644
@@ -178,4 +178,15 @@ config USB_DWC3_OCTEON
 	  Only the host mode is currently supported.
 	  Say 'Y' or 'M' here if you have one such device.
 
+config USB_DWC3_RTK
+	tristate "Realtek DWC3 Platform Driver"
+	depends on OF && ARCH_REALTEK
+	default USB_DWC3
+	select USB_ROLE_SWITCH
+	help
+	  RTK DHC RTD SoCs with DesignWare Core USB3 IP inside,
+	  and IP Core configured for USB 2.0 and USB 3.0 in host
+	  or dual-role mode.
+	  Say 'Y' or 'M' if you have such device.
+
 endif
drivers/usb/dwc3/Makefile +1 -0
index fe1493d4bbe5..124eda2522d9 100644
@@ -55,3 +55,4 @@ obj-$(CONFIG_USB_DWC3_QCOM)		+= dwc3-qcom.o
 obj-$(CONFIG_USB_DWC3_IMX8MP)		+= dwc3-imx8mp.o
 obj-$(CONFIG_USB_DWC3_XILINX)		+= dwc3-xilinx.o
 obj-$(CONFIG_USB_DWC3_OCTEON)		+= dwc3-octeon.o
+obj-$(CONFIG_USB_DWC3_RTK)		+= dwc3-rtk.o
drivers/usb/dwc3/dwc3-rtk.c new file +475 -0

475 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

PATCH v6 2/2 dt-bindings: usb: dwc3: Add Realtek DHC RTD SoC DWC3 USB

Document the DWC3 USB bindings for Realtek SoCs.

---
v5 to v6 change:
No change.
v4 to v5 change:
No change.
v3 to v4 change:
Add reg for register set for pm control.
Remove maximum-speed in example.
v2 to v3 change:
Add description for reg
Remove property for realtek,unlink-usb3-port. Remove property for realtek,disable-usb3-phy. Use the maximum-speed instead of the above two properties.
v1 to v2 change:
Revise the subject.
Rename the file.
Fix dtschema warnings.
Remove the property realtek,enable-l4icg.
Drop status.

---
 .../bindings/usb/realtek,rtd-dwc3.yaml        | 80 +++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/usb/realtek,rtd-dwc3.yaml
Documentation/devicetree/bindings/usb/realtek,rtd-dwc3.yaml new file +80 -0
new file mode 100644
index 000000000000..345d0132d4a5
@@ -0,0 +1,80 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+# Copyright 2023 Realtek Semiconductor Corporation
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/usb/realtek,rtd-dwc3.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Realtek DWC3 USB SoC Controller Glue
+
+maintainers:
+  - Stanley Chang <[email protected]>
+
+description:
+  The Realtek DHC SoC embeds a DWC3 USB IP Core configured for USB 2.0
+  and USB 3.0 in host or dual-role mode.
+
+properties:
+  compatible:
+    items:
+      - enum:
+          - realtek,rtd1295-dwc3
+          - realtek,rtd1315e-dwc3
+          - realtek,rtd1319-dwc3
+          - realtek,rtd1319d-dwc3
+          - realtek,rtd1395-dwc3
+          - realtek,rtd1619-dwc3
+          - realtek,rtd1619b-dwc3
+      - const: realtek,rtd-dwc3
+
+  reg:
+    items:
+      - description: Address and length of register set for wrapper of dwc3 core.
+      - description: Address and length of register set for pm control.
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 1
+
+  ranges: true
+
+patternProperties:
+  "^usb@[0-9a-f]+$":
+    $ref: snps,dwc3.yaml#
+    description: Required child node
+
+required:
+  - compatible
+  - reg
+  - "#address-cells"
+  - "#size-cells"
+  - ranges
+
+additionalProperties: false
+
+examples:
+  - |
+    usb@98013e00 {
+        compatible = "realtek,rtd1319d-dwc3", "realtek,rtd-dwc3";
+        reg = <0x98013e00 0x140>, <0x98013f60 0x4>;
+        #address-cells = <1>;
+        #size-cells = <1>;
+        ranges;
+
+        usb@98050000 {
+            compatible = "snps,dwc3";
+            reg = <0x98050000 0x9000>;
+            interrupts = <0 94 4>;
+            phys = <&usb2phy &usb3phy>;
+            phy-names = "usb2-phy", "usb3-phy";
+            dr_mode = "otg";
+            usb-role-switch;
+            role-switch-default-mode = "host";
+            snps,dis_u2_susphy_quirk;
+            snps,parkmode-disable-ss-quirk;
+            snps,parkmode-disable-hs-quirk;
+            maximum-speed = "high-speed";
+        };
+    };
Signature · 1 line
2.34.1
#2 lore raw
In reply to Stanley Chang

Le 26/08/2023 à 05:10, Stanley Chang a écrit :

Quoted 11 lines

Realtek DHC RTD SoCs integrate dwc3 IP and has some customizations to support different generations of SoCs.

The RTD1619b subclass SoC only supports USB 2.0 from dwc3. The driver can set a maximum speed to support this. Add role switching function, that can switch USB roles through other drivers, or switch USB roles through user space through set /sys/class/usb_role/.

---

...

Quoted 131 lines
drivers/usb/dwc3/dwc3-rtk.c new file +131 -0
+static int dwc3_rtk_probe_dwc3_core(struct dwc3_rtk *rtk)
+{
+	struct device *dev = rtk->dev;
+	struct device_node *node = dev->of_node;
+	struct platform_device *dwc3_pdev;
+	struct device *dwc3_dev;
+	struct device_node *dwc3_node;
+	enum usb_dr_mode dr_mode;
+	int ret = 0;
+
+	ret = dwc3_rtk_init(rtk);
+	if (ret)
+		return -EINVAL;
+
+	ret = of_platform_populate(node, NULL, NULL, dev);
+	if (ret) {
+		dev_err(dev, "failed to add dwc3 core\n");
+		return ret;
+	}
+
+	dwc3_node = of_get_compatible_child(node, "snps,dwc3");
+	if (!dwc3_node) {
+		dev_err(dev, "failed to find dwc3 core node\n");
+		ret = -ENODEV;
+		goto depopulate;
+	}
+
+	dwc3_pdev = of_find_device_by_node(dwc3_node);
+	if (!dwc3_pdev) {
+		dev_err(dev, "failed to find dwc3 core platform_device\n");
+		ret = -ENODEV;
+		goto err_node_put;
+	}
+
+	dwc3_dev = &dwc3_pdev->dev;
+	rtk->dwc = platform_get_drvdata(dwc3_pdev);
+	if (!rtk->dwc) {
+		dev_err(dev, "failed to find dwc3 core\n");
+		ret = -ENODEV;
+		goto err_pdev_put;
+	}
+
+	dr_mode = usb_get_dr_mode(dwc3_dev);
+	if (dr_mode != rtk->dwc->dr_mode) {
+		dev_info(dev, "dts set dr_mode=%d, but dwc3 set dr_mode=%d\n",
+			 dr_mode, rtk->dwc->dr_mode);
+		dr_mode = rtk->dwc->dr_mode;
+	}
+
+	switch (dr_mode) {
+	case USB_DR_MODE_PERIPHERAL:
+		rtk->cur_role = USB_ROLE_DEVICE;
+		break;
+	case USB_DR_MODE_HOST:
+		rtk->cur_role = USB_ROLE_HOST;
+		break;
+	default:
+		dev_dbg(rtk->dev, "%s: dr_mode=%d\n", __func__, dr_mode);
+		break;
+	}
+
+	if (device_property_read_bool(dwc3_dev, "usb-role-switch")) {
+		ret = dwc3_rtk_setup_role_switch(rtk);
+		if (ret) {
+			dev_err(dev, "dwc3_rtk_setup_role_switch fail=%d\n", ret);
+			goto err_pdev_put;
+		}
+		rtk->cur_role = dwc3_rtk_get_role(rtk);
+	}
+
+	switch_usb2_role(rtk, rtk->cur_role);
+
+	return 0;
+
+err_pdev_put:
+	platform_device_put(dwc3_pdev);
+err_node_put:
+	of_node_put(dwc3_node);
+depopulate:
+	of_platform_depopulate(dev);
+
+	return ret;
+}
+
+static int dwc3_rtk_probe(struct platform_device *pdev)
+{
+	struct dwc3_rtk *rtk;
+	struct device *dev = &pdev->dev;
+	struct resource *res;
+	void __iomem *regs;
+	int ret = 0;
+
+	rtk = devm_kzalloc(dev, sizeof(*rtk), GFP_KERNEL);
+	if (!rtk) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	platform_set_drvdata(pdev, rtk);
+
+	rtk->dev = dev;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res) {
+		dev_err(dev, "missing memory resource\n");
+		ret = -ENODEV;
+		goto out;
+	}
+
+	regs = devm_ioremap_resource(dev, res);
+	if (IS_ERR(regs)) {
+		ret = PTR_ERR(regs);
+		goto out;
+	}
+
+	rtk->regs = regs;
+	rtk->regs_size = resource_size(res);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	if (res) {
+		rtk->pm_base = devm_ioremap_resource(dev, res);
+		if (IS_ERR(rtk->pm_base)) {
+			ret = PTR_ERR(rtk->pm_base);
+			goto out;
+		}
+	}
+
+	ret = dwc3_rtk_probe_dwc3_core(rtk);
+
+out:
+	return ret;

drivers/usb/dwc3/dwc3-rtk.c line 410

Quoted 10 lines
drivers/usb/dwc3/dwc3-rtk.c new file +10 -0
+}
+
+static void dwc3_rtk_remove(struct platform_device *pdev)
+{
+	struct dwc3_rtk *rtk = platform_get_drvdata(pdev);
+
+	rtk->dwc = NULL;
+
+	dwc3_rtk_remove_role_switch(rtk);
+

Hi,

Is something like

	platform_device_put(dwc3_pdev);
	of_node_put(dwc3_node);

needed in the remove function?

(as done in the error handling path of dwc3_rtk_probe_dwc3_core())

Or should it be added at the end of dwc3_rtk_probe_dwc3_core() if the reference are nor needed anymore when we leave the function?

CJ

Quoted 3 lines
drivers/usb/dwc3/dwc3-rtk.c new file +3 -0
+	of_platform_depopulate(rtk->dev);
+}
+

...

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Hi CJ,

I think these functions are not needed in remove function.

In dwc3_rtk_probe_dwc3_core,
I have used
dwc3_node = of_get_compatible_child(node, "snps,dwc3"); and
dwc3_pdev = of_find_device_by_node(dwc3_node);

So, I call these put functions.
platform_device_put(dwc3_pdev);
of_node_put(dwc3_node);

Thanks,
Stanley

Quoted 18 lines

Hi,

Is something like

        platform_device_put(dwc3_pdev);
        of_node_put(dwc3_node);

needed in the remove function?

(as done in the error handling path of dwc3_rtk_probe_dwc3_core())

Or should it be added at the end of dwc3_rtk_probe_dwc3_core() if the reference are nor needed anymore when we leave the function?

CJ

Quoted 2 lines
(quoted diff) +2 -0
+     of_platform_depopulate(rtk->dev); }
+

...

#5 lore raw

Le 01/11/2023 à 07:27, Stanley Chang[昌育德] a écrit :

Quoted 10 lines

Hi CJ,

I think these functions are not needed in remove function.

In dwc3_rtk_probe_dwc3_core,
I have used
dwc3_node = of_get_compatible_child(node, "snps,dwc3"); and
dwc3_pdev = of_find_device_by_node(dwc3_node);

Quoted 3 lines

So, I call these put functions.
platform_device_put(dwc3_pdev);
of_node_put(dwc3_node);

Yes, but you call it only in the error handling path of the function.

I wonder if they should also be called in the remove function in order to decrement the ref-counted reference.

Same in __get_dwc3_maximum_speed(), the reference taken by:
dwc3_np = of_get_compatible_child(np, "snps,dwc3");
is never released.

See the comment at [1] to see what I mean.

[1]: https://elixir.bootlin.com/linux/v6.6/source/drivers/of/base.c#L681

CJ

Quoted 23 lines

Thanks,
Stanley

Quoted 18 lines

Hi,

Is something like

         platform_device_put(dwc3_pdev);
         of_node_put(dwc3_node);

needed in the remove function?

(as done in the error handling path of dwc3_rtk_probe_dwc3_core())

Or should it be added at the end of dwc3_rtk_probe_dwc3_core() if the reference are nor needed anymore when we leave the function?

CJ

Quoted 2 lines
(quoted diff) +2 -0
+     of_platform_depopulate(rtk->dev); }
+

...

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Hi Christophe,

Quoted 20 lines · Stanley Chang[昌育德]
Quoted 4 lines

Hi CJ,

I think these functions are not needed in remove function.

Quoted 8 lines

In dwc3_rtk_probe_dwc3_core,
I have used
dwc3_node = of_get_compatible_child(node, "snps,dwc3"); and dwc3_pdev = of_find_device_by_node(dwc3_node);

So, I call these put functions.
platform_device_put(dwc3_pdev);
of_node_put(dwc3_node);

Yes, but you call it only in the error handling path of the function.

I wonder if they should also be called in the remove function in order to decrement the ref-counted reference.

Quoted 8 lines

Same in __get_dwc3_maximum_speed(), the reference taken by:
dwc3_np = of_get_compatible_child(np, "snps,dwc3"); is never released.

See the comment at [1] to see what I mean.

[1]: https://elixir.bootlin.com/linux/v6.6/source/drivers/of/base.c#L681

You are right!
For dwc3_pdev, dwc3_np or dwc3_node, I should add of_node_put to release them when the function exits.
https://elixir.bootlin.com/linux/v6.5.10/source/drivers/usb/dwc3/dwc3-meson-g12a.c#L567

I will add a patch to fix this.

For example,

drivers/usb/dwc3/dwc3-rtk.c +3 -0
index 590028e8fdcb..9d6f2a8bd6ce 100644
@@ -187,6 +187,7 @@ static enum usb_device_speed __get_dwc3_maximum_speed(struct device_node *np)
 
        ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
 
+       of_node_put(dwc3_np);
        return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
 }
 
@@ -339,6 +340,8 @@ static int dwc3_rtk_probe_dwc3_core(struct dwc3_rtk *rtk)
 
        switch_usb2_role(rtk, rtk->cur_role);
 
+       platform_device_put(dwc3_pdev);
+       of_node_put(dwc3_node);
        return 0;
 
 err_pdev_put:

Thanks,
Stanley

Quoted 24 lines

CJ

Quoted 23 lines

Thanks,
Stanley

Quoted 18 lines

Hi,

Is something like

         platform_device_put(dwc3_pdev);
         of_node_put(dwc3_node);

needed in the remove function?

(as done in the error handling path of dwc3_rtk_probe_dwc3_core())

Or should it be added at the end of dwc3_rtk_probe_dwc3_core() if the reference are nor needed anymore when we leave the function?

CJ

Quoted 2 lines
(quoted diff) +2 -0
+     of_platform_depopulate(rtk->dev); }
+

...

#8 lore raw

Le 03/11/2023 à 11:54, Stanley Chang[昌育德] a écrit :

Quoted 52 lines

Hi Christophe,

Quoted 20 lines · Stanley Chang[昌育德]
Quoted 4 lines

Hi CJ,

I think these functions are not needed in remove function.

Quoted 8 lines

In dwc3_rtk_probe_dwc3_core,
I have used
dwc3_node = of_get_compatible_child(node, "snps,dwc3"); and dwc3_pdev = of_find_device_by_node(dwc3_node);

So, I call these put functions.
platform_device_put(dwc3_pdev);
of_node_put(dwc3_node);

Yes, but you call it only in the error handling path of the function.

I wonder if they should also be called in the remove function in order to decrement the ref-counted reference.

Quoted 8 lines

Same in __get_dwc3_maximum_speed(), the reference taken by:
dwc3_np = of_get_compatible_child(np, "snps,dwc3"); is never released.

See the comment at [1] to see what I mean.

[1]: https://elixir.bootlin.com/linux/v6.6/source/drivers/of/base.c#L681

You are right!
For dwc3_pdev, dwc3_np or dwc3_node, I should add of_node_put to release them when the function exits.
https://elixir.bootlin.com/linux/v6.5.10/source/drivers/usb/dwc3/dwc3-meson-g12a.c#L567

I will add a patch to fix this.

For example,

drivers/usb/dwc3/dwc3-rtk.c +1 -0
index 590028e8fdcb..9d6f2a8bd6ce 100644
@@ -187,6 +187,7 @@ static enum usb_device_speed __get_dwc3_maximum_speed(struct device_node *np)
 
         ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
 
+       of_node_put(dwc3_np);
         return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
  }
 

drivers/usb/dwc3/dwc3-rtk.c static int dwc3_rtk_probe_dwc3_core(struct dwc3_rtk *rtk) · line 347

Quoted 8 lines
drivers/usb/dwc3/dwc3-rtk.c +2 -0
@@ -339,6 +340,8 @@ static int dwc3_rtk_probe_dwc3_core(struct dwc3_rtk *rtk)
 
         switch_usb2_role(rtk, rtk->cur_role);
 
+       platform_device_put(dwc3_pdev);
+       of_node_put(dwc3_node);
         return 0;
 
  err_pdev_put:

LGTM.

CJ

Quoted 29 lines

Thanks,
Stanley

Quoted 24 lines

CJ

Quoted 23 lines

Thanks,
Stanley

Quoted 18 lines

Hi,

Is something like

          platform_device_put(dwc3_pdev);
          of_node_put(dwc3_node);

needed in the remove function?

(as done in the error handling path of dwc3_rtk_probe_dwc3_core())

Or should it be added at the end of dwc3_rtk_probe_dwc3_core() if the reference are nor needed anymore when we leave the function?

CJ

Quoted 2 lines
(quoted diff) +2 -0
+     of_platform_depopulate(rtk->dev); }
+

...