riscv: spacemit: Add K3 PCIe/USB comb phy support
v2 cover riscv: spacemit: Add K3 PCIe/USB comb phy support
The PCIe/USB comb phy on K3 is a big phy that contains multiple standalone phys for each PCIe and USB controllers. This phy is required to configure a syscon device for mux configuration and calibration.
Changed from v1:
patch 1:
1. Add extra description for "#phy-cells" and "spacemit,apmu" patch 2:
1. Fix multiple spelling errors and copy-paste errors.
2. Make some magic number as meaningful macros.
3. Remove USB3 set_speed() callback as it is not needed.
4. Fix the wrong land data config and mask.
Inochi Amaoto (2):
dt-bindings: phy: Add Spacemit K3 USB3/PCIe comb phy support
phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
.../bindings/phy/spacemit,k3-combo-phy.yaml | 68 +++ drivers/phy/spacemit/Kconfig | 16 + drivers/phy/spacemit/Makefile | 2 + drivers/phy/spacemit/phy-k3-combo.c | 252 +++++++++++ drivers/phy/spacemit/phy-k3-common.c | 391 ++++++++++++++++++ drivers/phy/spacemit/phy-k3-common.h | 27 ++ 6 files changed, 756 insertions(+) create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k3-combo-phy.yaml create mode 100644 drivers/phy/spacemit/phy-k3-combo.c create mode 100644 drivers/phy/spacemit/phy-k3-common.c create mode 100644 drivers/phy/spacemit/phy-k3-common.h
--
2.55.0
PATCH v2 1/2 dt-bindings: phy: Add Spacemit K3 USB3/PCIe comb phy support
The USB3/PCIe comb PHY on the K3 is a complex PHY group that can provide multiple phy for both PCIe and USB controller. Its mux configuration is controlled by the APMU syscon device.
--- .../bindings/phy/spacemit,k3-combo-phy.yaml | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k3-combo-phy.yaml
Documentation/devicetree/bindings/phy/spacemit,k3-combo-phy.yaml new file +68 -0
@@ -0,0 +1,68 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/phy/spacemit,k3-combo-phy.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Spacemit K3 PCIE/USB3 Comb PHY + +maintainers: + - Inochi Amaoto <[email protected]> + +properties: + compatible: + const: spacemit,k3-combo-phy + + reg: + maxItems: 1 + + "#phy-cells": + const: 2 + description: + The first one is phy id, the second one is phy type. This + only supports types are PCIe and USB . + + spacemit,apb-spare: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to APB SPARE system controller interface, used for + PHY calibration. + + spacemit,apmu: + $ref: /schemas/types.yaml#/definitions/phandle-array + items: + - items: + - description: phandle of APMU syscon + - description: configuration of the PHY lanes + description: | + Phandle to control PHY mux configuration. The configuration + is described as follows: + bit 4: 0 - PCIe A x8 mode, 1 - PCIe lane share mode + bit 3: 0 - PCIe A x4 mode, 1 - PCIe A x2 and PCIe B x2 mode + bit 2: 0 - PCIe C lane 0 is PCIe mode , 1 - USB mode + bit 1: 0 - PCIe C lane 1 is PCIe mode , 1 - USB mode + bit 0: 0 - PCIe D lane is PCIe mode , 1 - USB mode + + The bit[3:0] is only valid when bit 4 is 1. + + This mux control does not affect the PHY for PCIe E, it is always + in PCIe mode. + +required: + - compatible + - reg + - "#phy-cells" + - spacemit,apb-spare + - spacemit,apmu + +additionalProperties: false + +examples: + - | + phy@81d00000 { + compatible = "spacemit,k3-combo-phy"; + reg = <0x81d00000 0x600000>; + #phy-cells = <2>; + spacemit,apb-spare = <&apb_spare>; + spacemit,apmu = <&apmu 0x00>; + };
Signature · 1 line
2.55.0
Re: [PATCH v2 1/2] dt-bindings: phy: Add Spacemit K3 USB3/PCIe comb phy support
Quoted 10 lines · Inochi Amaoto
The USB3/PCIe comb PHY on the K3 is a complex PHY group that can provide multiple phy for both PCIe and USB controller. Its mux configuration is controlled by the APMU syscon device.
Signed-off-by Inochi Amaoto <[email protected]>--- .../bindings/phy/spacemit,k3-combo-phy.yaml | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k3-combo-phy.yaml
PATCH v2 2/2 phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
In reply to Inochi AmaotoThe comb PHY on K3 requires to configure a syscon device for the right mux configuration. And it requires calibration before any usage.
Add USB3/PCIe comb PHY driver for Spacemit K3.
--- drivers/phy/spacemit/Kconfig | 16 ++ drivers/phy/spacemit/Makefile | 2 + drivers/phy/spacemit/phy-k3-combo.c | 252 +++++++++++++++++ drivers/phy/spacemit/phy-k3-common.c | 391 +++++++++++++++++++++++++++ drivers/phy/spacemit/phy-k3-common.h | 27 ++ 5 files changed, 688 insertions(+) create mode 100644 drivers/phy/spacemit/phy-k3-combo.c create mode 100644 drivers/phy/spacemit/phy-k3-common.c create mode 100644 drivers/phy/spacemit/phy-k3-common.h
drivers/phy/spacemit/Kconfig +16 -0
@@ -23,3 +23,19 @@ config PHY_SPACEMIT_K1_USB2 help Enable this to support K1 USB 2.0 PHY driver. This driver takes care of enabling and clock setup and will be used by K1 udc/ehci/otg/xhci driver. + +config PHY_SPACEMIT_K3_COMMON_OPS + tristate + select MFD_SYSCON + select GENERIC_PHY + +config PHY_SPACEMIT_K3_COMBO_PHY + tristate "SpacemiT K3 USB3/PCIe PHY support" + depends on (ARCH_SPACEMIT || COMPILE_TEST) && OF + depends on COMMON_CLK + select PHY_SPACEMIT_K3_COMMON_OPS + help + Enable this to support K3 USB3/PCIe combo PHY driver. This + driver takes care of enabling and clock setup and will be used + by K3 dwc3 driver. + If unsure, say N.
drivers/phy/spacemit/Makefile +2 -0
@@ -1,3 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_PHY_SPACEMIT_K1_PCIE) += phy-k1-pcie.o obj-$(CONFIG_PHY_SPACEMIT_K1_USB2) += phy-k1-usb2.o +obj-$(CONFIG_PHY_SPACEMIT_K3_COMBO_PHY) += phy-k3-combo.o +obj-$(CONFIG_PHY_SPACEMIT_K3_COMMON_OPS) += phy-k3-common.o
drivers/phy/spacemit/phy-k3-combo.c new file +252 -0
252 lines not on this page · raw message
drivers/phy/spacemit/phy-k3-common.c new file +391 -0
391 lines not on this page · raw message
drivers/phy/spacemit/phy-k3-common.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _PHY_K3_COMMON_H +#define _PHY_K3_COMMON_H + +#include <linux/phy/phy.h> + +struct k3_phy_lane_group_data { +u32 lanes; +u8 config; +u8 mask; + u32 offsets[] __counted_by(lanes); +}; + +struct k3_lane_group { +const struct k3_phy_lane_group_data *data; +void __iomem *base; +struct phy *phy; +bool is_pcie; +}; + +extern const struct phy_ops k3_pcie_phy_ops; +extern const struct phy_ops k3_usb3_phy_ops; + +int k3_phy_calibrate (struct regmap *apb_spare); + +#endif
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 2/2] phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Hardware register clobbering due to missing read-modify-write operations in PHY initialization. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Hardware register clobbering due to missing read-modify-write operations in PHY initialization. --
commit c71cb5c4ebfcb4fc5aff5c18a7769949e15dae55
Author: Inochi Amaoto <[email protected]>
phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
Adds a USB3/PCIe comb PHY driver for Spacemit K3. The comb PHY requires configuring a syscon device for the correct mux configuration and needs calibration before usage.
Quoted 5 lines
drivers/phy/spacemit/phy-k3-common.c new file +0
-0
[ ... ]
drivers/phy/spacemit/phy-k3-common.c line 158
Quoted 10 lines
drivers/phy/spacemit/phy-k3-common.c new file +10
-0+/* Power down 100MHz refclk buffer */ + val =readl (base +PHY_PU_CK_REG ); + val =u32_replace_bits (val,0 ,PHY_PU_REFCLK_100 ); +writel (val, base +PHY_PU_CK_REG ); + +/* Program PLL REG1 configure the SSC */ + val =FIELD_PREP (PHY_PLL_SSC_MODE ,PHY_PLL_SSC_MODE_DOWN_SPREAD1 ) | +FIELD_PREP (PHY_PLL_SSC_DEP_SEL ,PHY_PLL_SSC_5000PPM ) | +FIELD_PREP (PHY_PLL_FREF_SEL ,PHY_PLL_FREF_24M ); +writel (val, base +PHY_PLL_REG1 );
[Severity: High]
Does this completely overwrite PHY_PLL_REG1 and zero out unspecified bits?
Unlike the PCIe initialization path which uses a read-modify-write for this register, this direct writel() in k3_usb3phy_init_single() clobbers the entire register. Could this cause important default settings or calibration values to be lost?
[ ... ]
Quoted 7 lines
drivers/phy/spacemit/phy-k3-common.c new file +7
-0+for (i =0 ; i < lg->data->lanes; i++) { +void __iomem *lane_base = lg->base + lg->data->offsets[i]; + + val =readl (lane_base +PCIE_RX_REG1 ); + val =u32_replace_bits (val,0 ,0x3 ); +writel (val, lane_base +PCIE_RX_REG1 ); + }
drivers/phy/spacemit/phy-k3-common.c line 277
Quoted 10 lines
drivers/phy/spacemit/phy-k3-common.c new file +10
-0+ + val =readl (phy_base +PHY_PLL_REG2 ); + val |=BIT (20 ); +writel (val, phy_base +PHY_PLL_REG2 ); + +/* The write is needed as clock requires renegotiation */ + val =FIELD_PREP (PCIE_RX_REFCLK_MODE ,PCIE_RX_REFCLK_MODE_DRIVER ) | +PCIE_RX_SEL_TRI_CODE | +FIELD_PREP (PCIE_RX_LEGACY ,PCIE_RX_LEGACY_DEFAULT ); +writel (val, phy_base +PCIE_RX_REG1 );
[Severity: High]
Does this clobber the entire PCIE_RX_REG1 register for lane 0?
The preceding loop performs a read-modify-write on PCIE_RX_REG1 for all lanes, but this direct writel() on phy_base (which points to lane 0) invalidates that previous work and zeroes out any bits not explicitly set here.
Should this also use a read-modify-write operation to preserve the register state?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/2] phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
Quoted 21 lines · On Fri, Jul 03, 2026 at 02:19:28AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Hardware register clobbering due to missing read-modify-write operations in PHY initialization. --commit
c71cb5c4ebfcb4fc5aff5c18a7769949e15dae55
Author: Inochi Amaoto <[email protected]>phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
Adds a USB3/PCIe comb PHY driver for Spacemit K3. The comb PHY requires configuring a syscon device for the correct mux configuration and needs calibration before usage.
Quoted 5 lines
drivers/phy/spacemit/phy-k3-common.c new file +0
-0[ ... ]
Quoted 14 lines
drivers/phy/spacemit/phy-k3-common.c line 158
Quoted 10 lines
drivers/phy/spacemit/phy-k3-common.c new file +10
-0+/* Power down 100MHz refclk buffer */ + val =readl (base +PHY_PU_CK_REG ); + val =u32_replace_bits (val,0 ,PHY_PU_REFCLK_100 ); +writel (val, base +PHY_PU_CK_REG ); + +/* Program PLL REG1 configure the SSC */ + val =FIELD_PREP (PHY_PLL_SSC_MODE ,PHY_PLL_SSC_MODE_DOWN_SPREAD1 ) | +FIELD_PREP (PHY_PLL_SSC_DEP_SEL ,PHY_PLL_SSC_5000PPM ) | +FIELD_PREP (PHY_PLL_FREF_SEL ,PHY_PLL_FREF_24M ); +writel (val, base +PHY_PLL_REG1 );[Severity: High]
Does this completely overwritePHY_PLL_REG1and zero out unspecified bits?
No, All the field are filled, so no data is lost.
Quoted 19 lines
Unlike the PCIe initialization path which uses a read-modify-write for this register, this direct
writel()ink3_usb3phy_init_single()clobbers the entire register. Could this cause important default settings or calibration values to be lost?[ ... ]
Quoted 12 lines
drivers/phy/spacemit/phy-k3-common.c new file +12
-0+for (i =0 ; i < lg->data->lanes; i++) { +void __iomem *lane_base = lg->base + lg->data->offsets[i]; + + val =readl (lane_base +PCIE_RX_REG1 ); + val =u32_replace_bits (val,0 ,0x3 ); +writel (val, lane_base +PCIE_RX_REG1 ); + } + + val =readl (phy_base +PHY_PLL_REG2 ); + val |=BIT (20 ); +writel (val, phy_base +PHY_PLL_REG2 ); +
Quoted 9 lines
drivers/phy/spacemit/phy-k3-common.c line 277
Quoted 5 lines
drivers/phy/spacemit/phy-k3-common.c new file +5
-0+/* The write is needed as clock requires renegotiation */ + val =FIELD_PREP (PCIE_RX_REFCLK_MODE ,PCIE_RX_REFCLK_MODE_DRIVER ) | +PCIE_RX_SEL_TRI_CODE | +FIELD_PREP (PCIE_RX_LEGACY ,PCIE_RX_LEGACY_DEFAULT ); +writel (val, phy_base +PCIE_RX_REG1 );[Severity: High]
Does this clobber the entirePCIE_RX_REG1register for lane 0?
This also serves as the whole clock. If the phy has two lanes, they share some data and the data is store at lane 0.
Quoted 7 lines
The preceding loop performs a read-modify-write on
PCIE_RX_REG1for all lanes, but this directwritel()onphy_base(which points to lane 0) invalidates that previous work and zeroes out any bits not explicitly set here.Should this also use a read-modify-write operation to preserve the register state?
No, it is not needed, as all the field are filled or it should be zero.
Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/2] phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
In reply to Inochi AmaotoQuoted 7 lines · Inochi Amaoto
The comb PHY on K3 requires to configure a syscon device for the right mux configuration. And it requires calibration before any usage.
Add USB3/PCIe comb PHY driver for Spacemit K3.
Signed-off-by Inochi Amaoto <[email protected]>
Quoted 10 lines
--- drivers/phy/spacemit/Kconfig | 16 ++ drivers/phy/spacemit/Makefile | 2 + drivers/phy/spacemit/phy-k3-combo.c | 252 +++++++++++++++++ drivers/phy/spacemit/phy-k3-common.c | 391 +++++++++++++++++++++++++++ drivers/phy/spacemit/phy-k3-common.h | 27 ++ 5 files changed, 688 insertions(+) create mode 100644 drivers/phy/spacemit/phy-k3-combo.c create mode 100644 drivers/phy/spacemit/phy-k3-common.c create mode 100644 drivers/phy/spacemit/phy-k3-common.h
Signature · 2 lines
Aurelien Jarno GPG: 4096R/1DDD8C9B [email protected] http://aurel32.net
Quoted 14 lines · Inochi Amaoto
The PCIe/USB comb phy on K3 is a big phy that contains multiple standalone phys for each PCIe and USB controllers. This phy is required to configure a syscon device for mux configuration and calibration.
Changed from v1:
patch 1:
1. Add extra description for "#phy-cells" and "spacemit,apmu" patch 2:
1. Fix multiple spelling errors and copy-paste errors.
2. Make some magic number as meaningful macros.
3. Remove USB3set_speed()callback as it is not needed.
4. Fix the wrong land data config and mask.
Quoted 1 line
[...]
Applied, thanks!
[1/2] dt-bindings: phy: Add Spacemit K3 USB3/PCIe comb phy support
commit: 013004374b4da621e292408112413b103dd28b03
[2/2] phy: spacemit: Add USB3/PCIe comb PHY driver for Spacemit K3
commit: 7a099760ea63ced7b194852b7dbbdcd951585f15Best regards,
Signature · 1 line
~Vinod