phy: nuvoton: Add MA35D1 USB2 OTG PHY driver
cover phy: nuvoton: Add MA35D1 USB2 OTG PHY driver
The Nuvoton MA35D1 SoC has two USB 2.0 ports:
USB0 is an OTG-capable port. Its physical signals are routed by a hardware mux to either a DWC2 gadget controller or the EHCI0/OHCI0 host controllers, depending on the USB ID pin state. The DWC2 IP is device-only in hardware, so all host-mode operation on USB0 is handled by EHCI0/OHCI0.
USB1 is a dedicated host-only port served by EHCI1/OHCI1.
About this driver:
- Runs the PHY Power-On Reset sequence, with a guard that skips re-initialization if the PHY is already operational.
- Supports optional resistor calibration trim (nuvoton,rcalcode) and over-current detect polarity configuration (nuvoton,oc-active-high).
- For PHY0 (USB0) only: registers a USB role switch that reads the hardware ID pin state from PWRONOTP[16] on every query.
Joey Lu (2):
dt-bindings: phy: nuvoton: Add MA35D1 USB2 OTG PHY binding
phy: nuvoton: Add MA35D1 USB2 OTG PHY driver
.../phy/nuvoton,ma35d1-usb2-phy-otg.yaml | 79 ++++++ drivers/phy/nuvoton/Kconfig | 15 + drivers/phy/nuvoton/Makefile | 1 + drivers/phy/nuvoton/phy-ma35d1-otg.c | 264 ++++++++++++++++++ 4 files changed, 359 insertions(+) create mode 100644 Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml create mode 100644 drivers/phy/nuvoton/phy-ma35d1-otg.c
Signature · 1 line
2.43.0
PATCH 1/2 dt-bindings: phy: nuvoton: Add MA35D1 USB2 OTG PHY binding
Add device tree binding documentation for the Nuvoton MA35D1 USB 2.0 OTG PHY driver (nuvoton,ma35d1-usb2-phy-otg).
PHY index 0 (USB0) is an OTG port whose signals are routed by a hardware mux to either the DWC2 device controller or the EHCI0/OHCI0 host controllers depending on the USB ID pin state. PHY index 1 (USB1) is a dedicated host-only port.
Optional properties allow board-specific resistor calibration trim (nuvoton,rcalcode) and over-current detect polarity configuration (nuvoton,oc-active-high).
--- .../phy/nuvoton,ma35d1-usb2-phy-otg.yaml | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +79 -0
@@ -0,0 +1,79 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/phy/nuvoton,ma35d1-usb2-phy-otg.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Nuvoton MA35D1 USB 2.0 host PHY + +maintainers: + - Joey Lu <[email protected]> + +description: + USB 2.0 PHY driver for the Nuvoton MA35D1 SoC, used by the EHCI and + OHCI host controllers. + + USB0 (PHY index 0) is an OTG port whose physical signals are routed to + either the DWC2 device controller or the EHCI0/OHCI0 host controller by + a hardware mux that follows the USB ID pin. + + USB1 (PHY index 1) is a dedicated host port with no OTG capability. + +properties: + compatible: + const: nuvoton,ma35d1-usb2-phy-otg + + clocks: + maxItems: 1 + + nuvoton,sys: + $ref: /schemas/types.yaml#/definitions/phandle-array + items: + - items: + - description: phandle to the system management syscon. + - description: PHY instance index. + enum: + - 0 # USB0, OTG port (shared with DWC2 gadget controller) + - 1 # USB1, host-only port + description: + A phandle to the syscon node covering the SYS register block, with + one argument selecting the PHY instance. Index 0 selects the OTG + port PHY (USB0) and index 1 selects the host-only PHY (USB1). + + "#phy-cells": + const: 0 + + nuvoton,rcalcode: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 0 + maximum: 15 + description: + Resistor calibration trim code written to the RCALCODE field in + USBPMISCR. The 4-bit value adjusts the PHY's internal termination + resistance. When absent the hardware reset default is used. + + nuvoton,oc-active-high: + type: boolean + description: + When present, the over-current detect input from the VBUS power + switch is treated as active-high. The default (property absent) is + active-low. This setting is shared by both USB host ports. + +required: + - compatible + - clocks + - nuvoton,sys + - "#phy-cells" + +additionalProperties: false + +examples: + - | + #include <dt-bindings/clock/nuvoton,ma35d1-clk.h> + + usb_hphy0: usb-host-phy { + compatible = "nuvoton,ma35d1-usb2-phy-otg"; + clocks = <&clk HUSBH0_GATE>; + nuvoton,sys = <&sys 0>; + #phy-cells = <0>; + };
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 1/2] dt-bindings: phy: nuvoton: Add MA35D1 USB2 OTG PHY binding
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The binding incorrectly represents a multi-port PHY controller as multiple independent nodes…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The binding incorrectly represents a multi-port PHY controller as multiple independent nodes instead of a single provider with #phy-cells = <1>. --
commit edfd20765d416e895bc152da5eefd3bf59f431bd
Author: Joey Lu <[email protected]>
dt-bindings: phy: nuvoton: Add MA35D1 USB2 OTG PHY binding
Adds the device tree binding documentation for the Nuvoton MA35D1 USB 2.0 OTG PHY driver. The PHY supports two ports (OTG and host-only) and allows board-specific resistor calibration and over-current detect polarity.
Quoted 4 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +0
-0
[ ... ]
Quoted 6 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +6
-0+ nuvoton,sys: + $ref: /schemas/types.yaml#/definitions/phandle-array + items: + - items: + - description: phandle to the system management syscon. + - description: PHY instance index.
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml line 44
Quoted 10 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +10
-0+ enum: + - 0 # USB0, OTG port (shared with DWC2 gadget controller) + - 1 # USB1, host-only port + description: + A phandle to the syscon node covering the SYS register block, with + one argument selecting the PHY instance. Index 0 selects the OTG + port PHY (USB0) and index 1 selects the host-only PHY (USB1). + + "#phy-cells": + const: 0
[Severity: Medium]
Should this multi-port PHY controller be represented as a single provider node with #phy-cells = <1> instead of embedding the index in nuvoton,sys?
Using independent nodes for each port with #phy-cells = <0> sidesteps the standard consumer argument pattern and forces non-standard node names to avoid collisions at the same tree level.
[ ... ]
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml line 60
Quoted 6 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +6
-0+ nuvoton,oc-active-high: + type: boolean + description: + When present, the over-current detect input from the VBUS power + switch is treated as active-high. The default (property absent) is + active-low. This setting is shared by both USB host ports.
Since this property is explicitly described as shared by both ports, does instantiating separate nodes per port risk conflicting states if the nodes configure this property differently?
A single PHY provider node would allow shared properties to be defined once, while consumers could specify their port index via standard phys arguments.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/2] dt-bindings: phy: nuvoton: Add MA35D1 USB2 OTG PHY binding
In reply to Joey LuOn Thu, Jun 04, 2026 at 06:12:19PM +0800, Joey Lu wrote:
A nit, subject: drop second/last, redundant "binding". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://elixir.bootlin.com/linux/v6.17-rc3/source/Documentation/devicetree/bindings/submitting-patches.rst#L18
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml line 25
Quoted 4 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +4
-0+properties: + compatible: + const: nuvoton,ma35d1-usb2-phy-otg +
Where is reg? MMIO is expressed with reg, not via custom phandle.
Quoted 39 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +39
-0+ clocks: + maxItems: 1 + + nuvoton,sys: + $ref: /schemas/types.yaml#/definitions/phandle-array + items: + - items: + - description: phandle to the system management syscon. + - description: PHY instance index. + enum: + - 0 # USB0, OTG port (shared with DWC2 gadget controller) + - 1 # USB1, host-only port + description: + A phandle to the syscon node covering the SYS register block, with + one argument selecting the PHY instance. Index 0 selects the OTG + port PHY (USB0) and index 1 selects the host-only PHY (USB1). + + "#phy-cells": + const: 0 + + nuvoton,rcalcode: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 0 + maximum: 15 + description: + Resistor calibration trim code written to the RCALCODE field in + USBPMISCR. The 4-bit value adjusts the PHY's internal termination + resistance. When absent the hardware reset default is used. + + nuvoton,oc-active-high: + type: boolean + description: + When present, the over-current detect input from the VBUS power + switch is treated as active-high. The default (property absent) is + active-low. This setting is shared by both USB host ports. + +required: + - compatible + - clocks
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml line 74
Quoted 10 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +10
-0+ - nuvoton,sys + - "#phy-cells" + +additionalProperties: false + +examples: + - | + #include <dt-bindings/clock/nuvoton,ma35d1-clk.h> + + usb_hphy0: usb-host-phy {
usb-phy
And drop unused label.
Quoted 8 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +5
-0+ compatible = "nuvoton,ma35d1-usb2-phy-otg"; + clocks = <&clk HUSBH0_GATE>; + nuvoton,sys = <&sys 0>; + #phy-cells = <0>; + };Signature · 1 line
2.43.0
Re: [PATCH 1/2] dt-bindings: phy: nuvoton: Add MA35D1 USB2 OTG PHY binding
Quoted 6 lines · Krzysztof Kozlowski
On Thu, Jun 04, 2026 at 06:12:19PM +0800, Joey Lu wrote:
A nit, subject: drop second/last, redundant "binding". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://elixir.bootlin.com/linux/v6.17-rc3/source/Documentation/devicetree/bindings/submitting-patches.rst#L18
Understood. The trailing "binding" will be dropped from the patch subject line in the next submission.
Quoted 5 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml line 25
Quoted 4 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +4
-0+properties: + compatible: + const: nuvoton,ma35d1-usb2-phy-otg +Where is reg? MMIO is expressed with reg, not via custom phandle.
Understood. The PHY node will be restructured as a child of the syscon node, with a reg = <0x60 0x14> property covering the writable PHY control registers (USBPMISCR, MISCFCR0) within the parent's address space. The nuvoton,sys custom phandle will be removed; the driver will obtain the parent's regmap via syscon_node_to_regmap(dev->parent->of_node), which looks up the global syscon list by of_node. PHY instance selection will be expressed via #phy-cells = <1>, with 0 for the OTG port (USB0) and 1 for the host-only port (USB1).
Quoted 52 lines
Quoted 40 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +39
-0+ clocks: + maxItems: 1 + + nuvoton,sys: + $ref: /schemas/types.yaml#/definitions/phandle-array + items: + - items: + - description: phandle to the system management syscon. + - description: PHY instance index. + enum: + - 0 # USB0, OTG port (shared with DWC2 gadget controller) + - 1 # USB1, host-only port + description: + A phandle to the syscon node covering the SYS register block, with + one argument selecting the PHY instance. Index 0 selects the OTG + port PHY (USB0) and index 1 selects the host-only PHY (USB1). + + "#phy-cells": + const: 0 + + nuvoton,rcalcode: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 0 + maximum: 15 + description: + Resistor calibration trim code written to the RCALCODE field in + USBPMISCR. The 4-bit value adjusts the PHY's internal termination + resistance. When absent the hardware reset default is used. + + nuvoton,oc-active-high: + type: boolean + description: + When present, the over-current detect input from the VBUS power + switch is treated as active-high. The default (property absent) is + active-low. This setting is shared by both USB host ports. + +required: + - compatible + - clocksDocumentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml line 74
Quoted 10 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +10
-0+ - nuvoton,sys + - "#phy-cells" + +additionalProperties: false + +examples: + - | + #include <dt-bindings/clock/nuvoton,ma35d1-clk.h> + + usb_hphy0: usb-host-phy {usb-phy
Quoted 1 line
And drop unused label.
Understood. The node name will be changed to usb-phy and the unused label usb_hphy0: will be removed.
Thanks for the review!
Quoted 8 lines
Quoted 8 lines
Documentation/devicetree/bindings/phy/nuvoton,ma35d1-usb2-phy-otg.yaml new file +5
-0+ compatible = "nuvoton,ma35d1-usb2-phy-otg"; + clocks = <&clk HUSBH0_GATE>; + nuvoton,sys = <&sys 0>; + #phy-cells = <0>; + };Signature · 1 line
2.43.0
PATCH 2/2 phy: nuvoton: Add MA35D1 USB2 OTG PHY driver
In reply to Joey LuAdd a PHY driver for the USB 2.0 PHYs in the Nuvoton MA35D1 SoC, intended for use with the EHCI and OHCI host controllers.
The MA35D1 SoC has two USB ports:
- USB0: an OTG port shared between a DWC2 gadget controller and EHCI0/OHCI0 host controllers. A hardware mux automatically routes the physical USB0 signals to the appropriate controller based on the USB ID pin state. The DWC2 IP is device-only in hardware, so host-mode operation on USB0 is handled entirely by EHCI0/OHCI0.
- USB1: a dedicated host-only port served by EHCI1/OHCI1.
The driver implements:
- Power-On Reset sequence with a guard that skips re-initialization if the PHY is already operational. This protects PHY0 when the DWC2 gadget driver has already run its own init before EHCI0 probes.
- Optional resistor calibration trim via nuvoton,rcalcode.
- Optional over-current detect polarity via nuvoton,oc-active-high.
- For PHY0 only: a USB role switch that exposes the hardware ID pin
state (PWRONOTP[16]).
--- drivers/phy/nuvoton/Kconfig | 15 ++ drivers/phy/nuvoton/Makefile | 1 + drivers/phy/nuvoton/phy-ma35d1-otg.c | 264 +++++++++++++++++++++++++++ 3 files changed, 280 insertions(+) create mode 100644 drivers/phy/nuvoton/phy-ma35d1-otg.c
drivers/phy/nuvoton/Kconfig +15 -0
@@ -10,3 +10,18 @@ config PHY_MA35_USB help Enable this to support the USB2.0 PHY on the Nuvoton MA35 series SoCs. + +config PHY_MA35_USB_OTG + tristate "Nuvoton MA35 USB2.0 OTG PHY driver" + depends on ARCH_MA35 || COMPILE_TEST + depends on OF + select GENERIC_PHY + select MFD_SYSCON + select USB_ROLE_SWITCH + help + Enable this to support the USB2.0 OTG PHY on the Nuvoton MA35 + series SoCs. This driver handles PHY initialization for the + EHCI/OHCI host controllers, including per-PHY power-on reset, + resistor calibration trim, and over-current polarity + configuration. For the OTG port (PHY0), it also monitors the + USB ID pin and registers a USB role switch.
drivers/phy/nuvoton/Makefile +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_PHY_MA35_USB) += phy-ma35d1-usb2.o +obj-$(CONFIG_PHY_MA35_USB_OTG) += phy-ma35d1-otg.o
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +264 -0
264 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] The driver implements a dummy USB role switch that falsely reports success without modifying…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] The driver implements a dummy USB role switch that falsely reports success without modifying hardware state.
- [High] Race condition exposing the PHY provider before initialization completes leads to a Use-After-Free.
- [High] PHY initialization fails due to polling clock stability bits while the PHY clock is disabled.
- [Medium] Ignoring the return value of regmap_read() results in the use of uninitialized stack memory. --
commit 9e57cc2572b36a8f75f7ca9bc25a14c367c18b1a
Author: Joey Lu <[email protected]>
Subject: phy: nuvoton: Add MA35D1 USB2 OTG PHY driver
Add a PHY driver for the USB 2.0 PHYs in the Nuvoton MA35D1 SoC. It implements Power-On Reset sequence, optional resistor calibration trim, and a USB role switch.
Quoted 5 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +0
-0
[ ... ]
drivers/phy/nuvoton/phy-ma35d1-otg.c line 67
Quoted 11 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +11
-0+static int ma35_otg_phy_init (struct phy *phy) +{ +struct ma35_otg_phy *p =phy_get_drvdata (phy); +unsigned int n = p->phy_idx; +u32 ready_mask =USBPMISCR_PHY_HOST_READY (n); +unsigned int val; +int ret; + +regmap_read (p->sysreg,MA35_SYS_USBPMISCR , &val); +if ((val & ready_mask) == ready_mask) +return 0 ;
[Severity: Medium]
Does ignoring the return value of regmap_read() risk evaluating uninitialized stack memory here?
If the read fails, val remains uninitialized, and using it in the bitwise operation could cause non-deterministic behavior, such as incorrectly skipping the PHY power-on reset sequence.
drivers/phy/nuvoton/phy-ma35d1-otg.c line 80
Quoted 13 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +13
-0+ +regmap_update_bits (p->sysreg,MA35_SYS_USBPMISCR , +USBPMISCR_PHY_CTL_MASK (n), +USBPMISCR_PHY_POR (n) |USBPMISCR_PHY_SUSPEND (n)); +msleep (20 ); + +regmap_update_bits (p->sysreg,MA35_SYS_USBPMISCR , +USBPMISCR_PHY_CTL_MASK (n), +USBPMISCR_PHY_SUSPEND (n)); + + ret =regmap_read_poll_timeout (p->sysreg,MA35_SYS_USBPMISCR , val, + (val & ready_mask) == ready_mask, +10 ,1000 );
[Severity: High]
Is it possible for this polling to time out because the PHY clock isn't enabled yet?
The polling expects the PHY clock to be stable, but the PHY clock (p->clk) is only enabled later in ma35_otg_phy_power_on(). Because the Linux PHY framework guarantees that phy_init() is called before phy_power_on(), the PHY reference clock remains disabled during this stability polling.
Quoted 8 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +8
-0+if (ret) { +dev_err (p->dev,"USB PHY%u clock not stable (USBPMISCR=0x%08x)\n" , + n, val); +return ret; + } + +return 0 ; +}
[ ... ]
drivers/phy/nuvoton/phy-ma35d1-otg.c line 118
Quoted 7 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +7
-0+static enum usb_role ma35_otg_read_id (struct ma35_otg_phy *p) +{ +unsigned int val; + +regmap_read (p->sysreg,MA35_SYS_PWRONOTP , &val); +return (val &PWRONOTP_USBP0ID ) ?USB_ROLE_HOST :USB_ROLE_DEVICE ; +}
[Severity: Medium]
Can this return an arbitrary USB role if regmap_read() fails?
If the read fails, the uninitialized stack variable val is evaluated, leading to a potentially incorrect role being reported.
drivers/phy/nuvoton/phy-ma35d1-otg.c line 128
Quoted 10 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +10
-0+ +static int ma35_otg_role_sw_set (struct usb_role_switch *sw, +enum usb_role role) +{ +struct ma35_otg_phy *p =usb_role_switch_get_drvdata (sw); + + p->cur_role = role; + +return 0 ; +}
[Severity: High]
Since the hardware mux is entirely automatic based on the physical USB ID pin, should this .set callback return an error instead of falsely reporting success?
Writing to p->cur_role doesn't configure any hardware register, and the .get callback correctly queries the hardware directly. Returning 0 here defeats the subsystem's -EOPNOTSUPP rejection for read-only switches, as writes from userspace will appear to succeed but will not change the actual hardware state.
Quoted 8 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +8
-0+ +static enum usb_role ma35_otg_role_sw_get (struct usb_role_switch *sw) +{ +struct ma35_otg_phy *p =usb_role_switch_get_drvdata (sw); + +return ma35_otg_read_id (p); +} +
drivers/phy/nuvoton/phy-ma35d1-otg.c line 146
Quoted 10 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +10
-0+static int ma35_otg_role_switch_init (struct platform_device *pdev, +struct ma35_otg_phy *p) +{ +struct usb_role_switch_desc sw_desc = { }; + + p->cur_role =ma35_otg_read_id (p); + + sw_desc.set = ma35_otg_role_sw_set; + sw_desc.get = ma35_otg_role_sw_get; + sw_desc.allow_userspace_control = true;
[Severity: High]
Is it appropriate to set allow_userspace_control to true given that the switch strictly reflects an automatic hardware state?
Quoted 2 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +2
-0+ sw_desc.driver_data = p; + sw_desc.fwnode =dev_fwnode (&pdev->dev);
[ ... ]
Quoted 2 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +2
-0+static int ma35_otg_phy_probe (struct platform_device *pdev) +{
[ ... ]
Quoted 4 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +4
-0+ provider =devm_of_phy_provider_register (&pdev->dev, + of_phy_simple_xlate); +if (IS_ERR (provider)) + return dev_err_probe(&pdev->dev, PTR_ERR(provider),
drivers/phy/nuvoton/phy-ma35d1-otg.c line 237
Quoted 10 lines
drivers/phy/nuvoton/phy-ma35d1-otg.c new file +10
-0+ "Failed to register PHY provider\n"); + +if (p->phy_idx ==0 ) { + ret =ma35_otg_role_switch_init (pdev, p); +if (ret) +return ret; + } + +return 0 ; +}
[Severity: High]
Could registering the PHY provider before role switch initialization risk a use-after-free?
If devm_of_phy_provider_register() exposes the PHY globally, a consumer on another CPU could look up the PHY and call phy_init(), acquiring a reference.
If ma35_otg_role_switch_init() subsequently fails, probe returns an error, triggering the devm cleanup which frees the struct ma35_otg_phy *p memory. Because the consumer already holds a valid reference, subsequent calls to PHY APIs will dereference the freed pointer.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Vinod Koul
maintainerQuoted 58 lines · Joey Lu
Add a PHY driver for the USB 2.0 PHYs in the Nuvoton MA35D1 SoC, intended for use with the EHCI and OHCI host controllers.
The MA35D1 SoC has two USB ports:
- USB0: an OTG port shared between a DWC2 gadget controller and EHCI0/OHCI0 host controllers. A hardware mux automatically routes the physical USB0 signals to the appropriate controller based on the USB ID pin state. The DWC2 IP is device-only in hardware, so host-mode operation on USB0 is handled entirely by EHCI0/OHCI0.
- USB1: a dedicated host-only port served by EHCI1/OHCI1.
The driver implements:
- Power-On Reset sequence with a guard that skips re-initialization if the PHY is already operational. This protects PHY0 when the DWC2 gadget driver has already run its own init before EHCI0 probes.
- Optional resistor calibration trim via nuvoton,rcalcode.
- Optional over-current detect polarity via nuvoton,oc-active-high.
- For PHY0 only: a USB role switch that exposes the hardware ID pinstate (PWRONOTP[16]).Signed-off-by Joey Lu <[email protected]>--- drivers/phy/nuvoton/Kconfig | 15 ++ drivers/phy/nuvoton/Makefile | 1 + drivers/phy/nuvoton/phy-ma35d1-otg.c | 264 +++++++++++++++++++++++++++ 3 files changed, 280 insertions(+) create mode 100644 drivers/phy/nuvoton/phy-ma35d1-otg.cdrivers/phy/nuvoton/Kconfig +15
-0@@ -10,3 +10,18 @@ config PHY_MA35_USB help Enable this to support the USB2.0 PHY on the Nuvoton MA35 series SoCs. + +config PHY_MA35_USB_OTG + tristate "Nuvoton MA35 USB2.0 OTG PHY driver" + depends on ARCH_MA35 || COMPILE_TEST + depends on OF + select GENERIC_PHY + select MFD_SYSCON + select USB_ROLE_SWITCH + help + Enable this to support the USB2.0 OTG PHY on the Nuvoton MA35 + series SoCs. This driver handles PHY initialization for the + EHCI/OHCI host controllers, including per-PHY power-on reset, + resistor calibration trim, and over-current polarity + configuration. For the OTG port (PHY0), it also monitors the + USB ID pin and registers a USB role switch.drivers/phy/nuvoton/Makefile +0
-0
drivers/phy/nuvoton/Makefile line 4
Quoted 4 lines
drivers/phy/nuvoton/Makefile +1
-0@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_PHY_MA35_USB) += phy-ma35d1-usb2.o +obj-$(CONFIG_PHY_MA35_USB_OTG) += phy-ma35d1-otg.o
Have you considered reusing usb2 driver with a different power_on function? Or handle the differences internally in the driver. There are few similarities in two and some things are different
Signature · 1 line
~Vinod
Quoted 61 lines · Vinod Koul
Quoted 60 lines · Joey Lu
Add a PHY driver for the USB 2.0 PHYs in the Nuvoton MA35D1 SoC, intended for use with the EHCI and OHCI host controllers.
The MA35D1 SoC has two USB ports:
- USB0: an OTG port shared between a DWC2 gadget controller and EHCI0/OHCI0 host controllers. A hardware mux automatically routes the physical USB0 signals to the appropriate controller based on the USB ID pin state. The DWC2 IP is device-only in hardware, so host-mode operation on USB0 is handled entirely by EHCI0/OHCI0.
- USB1: a dedicated host-only port served by EHCI1/OHCI1.
The driver implements:
- Power-On Reset sequence with a guard that skips re-initialization if the PHY is already operational. This protects PHY0 when the DWC2 gadget driver has already run its own init before EHCI0 probes.
- Optional resistor calibration trim via nuvoton,rcalcode.
- Optional over-current detect polarity via nuvoton,oc-active-high.
- For PHY0 only: a USB role switch that exposes the hardware ID pinstate (PWRONOTP[16]).Signed-off-by Joey Lu <[email protected]>--- drivers/phy/nuvoton/Kconfig | 15 ++ drivers/phy/nuvoton/Makefile | 1 + drivers/phy/nuvoton/phy-ma35d1-otg.c | 264 +++++++++++++++++++++++++++ 3 files changed, 280 insertions(+)create mode 100644
drivers/phy/nuvoton/phy-ma35d1-otg.cdrivers/phy/nuvoton/Kconfig +15
-0@@ -10,3 +10,18 @@ config PHY_MA35_USB help Enable this to support the USB2.0 PHY on the Nuvoton MA35 series SoCs. + +config PHY_MA35_USB_OTG + tristate "Nuvoton MA35 USB2.0 OTG PHY driver" + depends on ARCH_MA35 || COMPILE_TEST + depends on OF + select GENERIC_PHY + select MFD_SYSCON + select USB_ROLE_SWITCH + help + Enable this to support the USB2.0 OTG PHY on the Nuvoton MA35 + series SoCs. This driver handles PHY initialization for the + EHCI/OHCI host controllers, including per-PHY power-on reset, + resistor calibration trim, and over-current polarity + configuration. For the OTG port (PHY0), it also monitors the + USB ID pin and registers a USB role switch.drivers/phy/nuvoton/Makefile +0
-0@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0
Quoted 5 lines
drivers/phy/nuvoton/Makefile line 4
Quoted 2 lines
drivers/phy/nuvoton/Makefile +1
-0obj-$(CONFIG_PHY_MA35_USB) += phy-ma35d1-usb2.o +obj-$(CONFIG_PHY_MA35_USB_OTG) += phy-ma35d1-otg.oHave you considered reusing usb2 driver with a different
power_onfunction? Or handle the differences internally in the driver. There are few similarities in two and some things are different
Thank you for the excellent suggestion regarding reusing the existing USB2 driver.
After further evaluation and local testing, I verified that it is entirely feasible to reuse the driver. Consequently, I will drop the separate phy-ma35d1-otg.c patch series and submit a new patch set that extends the existing phy-ma35d1-usb2.c mainline driver.
In the upcoming patch series, I will expand the driver's capability from a single-port PHY0 peripheral driver to a dual-port manager supporting both PHY0 and PHY1, while integrating OTG features.
BR,
Joey