Linux-PHY Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] phy: cadence-torrent: rewrite Torrent PHY driver
@ 2026-11-20 12:55 Benjamin Robin
  2026-11-20 12:55 ` [PATCH 1/4] arm64: dts: ti: k3-j7200: Fix QSGMII overlay by adding SERDES PHY Benjamin Robin
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Benjamin Robin @ 2026-11-20 12:55 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Yuti Amonkar,
	Philipp Zabel, Andrzej Hajda, Robert Foss, Laurent Pinchart,
	Jonas Karlman, Jernej Skrabec, Luca Ceresoli, Maarten Lankhorst,
	Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter,
	Nishanth Menon, Vignesh Raghavendra, Tero Kristo, Andrew Davis
  Cc: Vinod Koul, Chintan Vankar, Abhash Kumar Jha, Neil Armstrong,
	Manivannan Sadhasivam, Thomas Richard, Swapnil Jakhade,
	Siddharth Vadapalli, Gregory CLEMENT, Théo Lebrun,
	Tawfik Bayouk, Vladimir Kondratiev, Thomas Petazzoni,
	linux-arm-kernel, devicetree, linux-kernel, linux-phy,
	Benjamin Robin

This series rewrites the driver for the Cadence Torrent multi-protocol
PHY (sd0801).

Motivation
----------
The existing phy-cadence-torrent driver hard-codes, for each supported
configuration, the register values to program. The configuration space
is large: protocol and associated PLL frequency, DisplayPort bit rate,
reference clock frequency, spread-spectrum clocking (SSC), number of
lanes, PHY revision and technology. As a result, every new board or
standard combination requires a full extra table of opaque register
values, which is inflexible and hard to maintain.

The rewrite aims to:

  - simplify the addition of support for a new board;
  - support every reference clock frequencies supported by the Cadence
    Torrent PHY;
  - support multiple links per standard without restriction (the previous
    driver, for instance, was limited to two PCIe links);
  - allow, in the future, the dynamic reconfiguration of the SerDes link
    rate (for example, to switch between SGMII and 2500BASE-X).

Because this is a complete rewrite, the diff is large and not well suited
to a line-by-line review. The rewritten driver can be fetched from the
`phy-cadence-torrent` branch at https://github.com/benjarobin/linux

Design
------
The new driver computes the register values instead of hard-coding them:

  - registers that depend on the reference clock frequency or on a PLL
    frequency are computed at run time, from the reference clock
    obtained through the clock framework and from the PLL frequency
    derived from the selected standard;
  - the remaining registers, which only depend on the standard type, the
    PHY revision and the technology, are written through one function per
    register, with an internal table selected by a
    standard/revision/technology key.

The PHY revision and technology are detected from the PHY identification
registers, and the DisplayPort link initialization and rate-change
sequence were rewritten.

Validation
----------
Because the rewritten driver computes register values instead of
hard-coding them, its output was validated against two independent
references:

  - The Cadence "Torrent Programmers Guide" spreadsheet, for the
    registers that depend on the reference clock and on the PLL
    frequencies. The spreadsheet generates (as a CSV) the expected value
    of every register to write; the values computed by the driver were
    compared with these expected values over a large number of
    combinations of reference clock, PLL frequency.
    To make this comparison feasible, the reference-clock and PLL
    computation code was extracted into small userspace harnesses,
    so that many combinations can be swept without booting the hardware,
    and the results are fed to a comparison tool that checks them against
    the spreadsheet CSV.

  - The existing driver, for the complete set of programmed registers.
    Every supported configuration of the old driver was extracted as a
    test case. Two userspace harnesses were generated:
     - tp-full-old, mirroring the old cdns_torrent_phy_init() /
       cdns_torrent_phy_configure_multilink() path;
     - tp-full-new, mirroring the new tp_configure_links() path.
    These 2 userspace harnesses run the same test cases (single
    standards, standard pairs, PCIe-Multilink and internal-SSC variants)
    and then compare, register by register, the writes performed by each
    driver for the same configuration. The registers written by both
    drivers match, with a small number of remaining differences that are
    all explained (for example, values that were fixed in a later
    revision of the spreadsheet, or obsolete/unused registers);

The last patch of the series adds a debug trace on every register read
or write, so the same comparison can also be performed on real hardware
by capturing the driver logs.

The tooling used for the validation described above is available at:
https://github.com/benjarobin/cadence-torrent-phy-tooling

Status / testing
----------------
The driver has so far been validated on two TI boards:

  - J784S4XEVM: two PCIe links (x2, x4), one DisplayPort link (display
    output working) and one USB-C port (configured as host);
  - J721EXCPXEVM: two PCIe links (x2, x1) and a J721EXENETXPANEVM
    daughter card exposing four Ethernet ports over a single QSGMII
    link (~1 Gbps per port).

Comments on the overall approach are welcome, as is testing on additional
hardware, for which Tested-by tags would be appreciated.

Dependency
----------
This series should be applied on top of v7.4-rc3. This was done this way
since the commit "treewide: Convert devm_kmalloc() family to the
devm_kmalloc_obj() family" introduced conflicts.

The driver depends on the following series:

  - [1]: needed since the driver requires the consumers to call
    phy_power_on() and phy_power_off();
  - [2]: needed otherwise the QSGMII SerDes link is never powered ON.

The series [2] is included in this one.

[1] https://lore.kernel.org/all/20260601095041.3042950-1-a-kumar2@ti.com/
[2] https://lore.kernel.org/all/20260326134929.1696750-1-c-vankar@ti.com/

Signed-off-by: Benjamin Robin <benjamin.robin@bootlin.com>
---
Benjamin Robin (3):
      dt-bindings: phy: cadence-torrent: drop cdns,num-lanes default value
      dt-bindings: phy: cadence-torrent: add phy-mode constants
      phy: cadence-torrent: rewrite Torrent PHY driver

Chintan Vankar (1):
      arm64: dts: ti: k3-j7200: Fix QSGMII overlay by adding SERDES PHY

 .../bindings/phy/phy-cadence-torrent.yaml          |   10 +-
 .../dts/ti/k3-j7200-evm-quad-port-eth-exp.dtso     |    3 +-
 drivers/phy/cadence/phy-cadence-torrent.c          | 9417 +++++++++-----------
 include/dt-bindings/phy/phy-cadence.h              |    8 +
 4 files changed, 4376 insertions(+), 5062 deletions(-)
---
base-commit: 6515d69692fb374d01e3dc95eef53047ca8f947c
change-id: 20261103-phy-cadence-torrent-ng-8d497bd05f48

Best regards,
--  
Benjamin Robin <benjamin.robin@bootlin.com>


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] arm64: dts: ti: k3-j7200: Fix QSGMII overlay by adding SERDES PHY
  2026-11-20 12:55 [PATCH 0/4] phy: cadence-torrent: rewrite Torrent PHY driver Benjamin Robin
@ 2026-11-20 12:55 ` Benjamin Robin
  2026-11-20 12:55 ` [PATCH 2/4] dt-bindings: phy: cadence-torrent: drop cdns,num-lanes default value Benjamin Robin
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Benjamin Robin @ 2026-11-20 12:55 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Yuti Amonkar,
	Philipp Zabel, Andrzej Hajda, Robert Foss, Laurent Pinchart,
	Jonas Karlman, Jernej Skrabec, Luca Ceresoli, Maarten Lankhorst,
	Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter,
	Nishanth Menon, Vignesh Raghavendra, Tero Kristo, Andrew Davis
  Cc: Vinod Koul, Chintan Vankar, Abhash Kumar Jha, Neil Armstrong,
	Manivannan Sadhasivam, Thomas Richard, Swapnil Jakhade,
	Siddharth Vadapalli, Gregory CLEMENT, Théo Lebrun,
	Tawfik Bayouk, Vladimir Kondratiev, Thomas Petazzoni,
	linux-arm-kernel, devicetree, linux-kernel, linux-phy,
	Benjamin Robin

From: Chintan Vankar <c-vankar@ti.com>

For CPSW5G QSGMII ports, CPSW assumes SERDES to be configured. Since it
may not be always true, add SERDES phys to guarantee it.

Fixes: 496cdc82e05f ("arm64: dts: ti: k3-j7200: Add overlay to enable CPSW5G ports in QSGMII mode")
Signed-off-by: Chintan Vankar <c-vankar@ti.com>
Signed-off-by: Benjamin Robin <benjamin.robin@bootlin.com>
---
 arch/arm64/boot/dts/ti/k3-j7200-evm-quad-port-eth-exp.dtso | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/boot/dts/ti/k3-j7200-evm-quad-port-eth-exp.dtso b/arch/arm64/boot/dts/ti/k3-j7200-evm-quad-port-eth-exp.dtso
index 6432ca08ee8e..4824d53c95bb 100644
--- a/arch/arm64/boot/dts/ti/k3-j7200-evm-quad-port-eth-exp.dtso
+++ b/arch/arm64/boot/dts/ti/k3-j7200-evm-quad-port-eth-exp.dtso
@@ -32,7 +32,8 @@ &cpsw0_port1 {
 	phy-handle = <&cpsw5g_phy0>;
 	phy-mode = "qsgmii";
 	mac-address = [00 00 00 00 00 00];
-	phys = <&cpsw0_phy_gmii_sel 1>;
+	phys = <&cpsw0_phy_gmii_sel 1>, <&serdes0_qsgmii_link>;
+	phy-names = "mac", "serdes";
 };
 
 &cpsw0_port2 {

-- 
2.56.1


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] dt-bindings: phy: cadence-torrent: drop cdns,num-lanes default value
  2026-11-20 12:55 [PATCH 0/4] phy: cadence-torrent: rewrite Torrent PHY driver Benjamin Robin
  2026-11-20 12:55 ` [PATCH 1/4] arm64: dts: ti: k3-j7200: Fix QSGMII overlay by adding SERDES PHY Benjamin Robin
@ 2026-11-20 12:55 ` Benjamin Robin
  2026-11-20 12:55 ` [PATCH 3/4] dt-bindings: phy: cadence-torrent: add phy-mode constants Benjamin Robin
       [not found] ` <20261120-phy-cadence-torrent-ng-v1-4-da2db7125cd7@bootlin.com>
  3 siblings, 0 replies; 6+ messages in thread
From: Benjamin Robin @ 2026-11-20 12:55 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Yuti Amonkar,
	Philipp Zabel, Andrzej Hajda, Robert Foss, Laurent Pinchart,
	Jonas Karlman, Jernej Skrabec, Luca Ceresoli, Maarten Lankhorst,
	Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter,
	Nishanth Menon, Vignesh Raghavendra, Tero Kristo, Andrew Davis
  Cc: Vinod Koul, Chintan Vankar, Abhash Kumar Jha, Neil Armstrong,
	Manivannan Sadhasivam, Thomas Richard, Swapnil Jakhade,
	Siddharth Vadapalli, Gregory CLEMENT, Théo Lebrun,
	Tawfik Bayouk, Vladimir Kondratiev, Thomas Petazzoni,
	linux-arm-kernel, devicetree, linux-kernel, linux-phy,
	Benjamin Robin

The driver treats cdns,num-lanes as mandatory and refuses to probe a PHY
sub-node that does not provide it, logging:

  No "cdns,num-lanes"-property.

A default value in the schema wrongly suggests that the property is
optional, which it is not since it is listed as required. Remove the
"default: 4" so the binding reflects the actual driver behavior and the
mandatory nature of the property.

Signed-off-by: Benjamin Robin <benjamin.robin@bootlin.com>
---
 Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml b/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
index 9af39b33646a..8e845e2373ac 100644
--- a/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
+++ b/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
@@ -106,7 +106,6 @@ patternProperties:
           Number of lanes.
         $ref: /schemas/types.yaml#/definitions/uint32
         enum: [1, 2, 3, 4]
-        default: 4
 
       cdns,ssc-mode:
         description:

-- 
2.56.1


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] dt-bindings: phy: cadence-torrent: add phy-mode constants
  2026-11-20 12:55 [PATCH 0/4] phy: cadence-torrent: rewrite Torrent PHY driver Benjamin Robin
  2026-11-20 12:55 ` [PATCH 1/4] arm64: dts: ti: k3-j7200: Fix QSGMII overlay by adding SERDES PHY Benjamin Robin
  2026-11-20 12:55 ` [PATCH 2/4] dt-bindings: phy: cadence-torrent: drop cdns,num-lanes default value Benjamin Robin
@ 2026-11-20 12:55 ` Benjamin Robin
  2026-11-20 13:10   ` sashiko-bot
       [not found] ` <20261120-phy-cadence-torrent-ng-v1-4-da2db7125cd7@bootlin.com>
  3 siblings, 1 reply; 6+ messages in thread
From: Benjamin Robin @ 2026-11-20 12:55 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Yuti Amonkar,
	Philipp Zabel, Andrzej Hajda, Robert Foss, Laurent Pinchart,
	Jonas Karlman, Jernej Skrabec, Luca Ceresoli, Maarten Lankhorst,
	Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter,
	Nishanth Menon, Vignesh Raghavendra, Tero Kristo, Andrew Davis
  Cc: Vinod Koul, Chintan Vankar, Abhash Kumar Jha, Neil Armstrong,
	Manivannan Sadhasivam, Thomas Richard, Swapnil Jakhade,
	Siddharth Vadapalli, Gregory CLEMENT, Théo Lebrun,
	Tawfik Bayouk, Vladimir Kondratiev, Thomas Petazzoni,
	linux-arm-kernel, devicetree, linux-kernel, linux-phy,
	Benjamin Robin

Add the `CDNS_TORRENT_MODE_*` constants to
include/dt-bindings/phy/phy-cadence.h such as SATA-LP and 2500BASE-X.

These constants are used by the new Cadence Torrent PHY driver and
are referenced by the `cdns,phy-mode` property of the Cadence Torrent
PHY binding to select the PHY mode.

Signed-off-by: Benjamin Robin <benjamin.robin@bootlin.com>
---
 Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml | 9 +++++++++
 include/dt-bindings/phy/phy-cadence.h                          | 8 ++++++++
 2 files changed, 17 insertions(+)

diff --git a/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml b/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
index 8e845e2373ac..17a8343ac899 100644
--- a/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
+++ b/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
@@ -101,6 +101,15 @@ patternProperties:
         $ref: /schemas/types.yaml#/definitions/uint32
         enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
 
+      cdns,phy-mode:
+        description:
+          Specifies an optional mode of PHY for which the group of PHY lanes is used.
+          Refer include/dt-bindings/phy/phy-cadence.h for the constants to be used.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        minimum: 0
+        maximum: 63
+        default: 0
+
       cdns,num-lanes:
         description:
           Number of lanes.
diff --git a/include/dt-bindings/phy/phy-cadence.h b/include/dt-bindings/phy/phy-cadence.h
index 0671991208fc..94945b843288 100644
--- a/include/dt-bindings/phy/phy-cadence.h
+++ b/include/dt-bindings/phy/phy-cadence.h
@@ -15,6 +15,14 @@
 #define CDNS_TORRENT_DERIVED_REFCLK	1
 #define CDNS_TORRENT_RECEIVED_REFCLK	2
 
+#define CDNS_TORRENT_MODE_NONE		0x00
+#define CDNS_TORRENT_MODE_USB_G1	0x01
+#define CDNS_TORRENT_MODE_USB_IO_11V	0x02
+#define CDNS_TORRENT_MODE_XAUI_LP	0x04
+#define CDNS_TORRENT_MODE_SATA_LP	0x08
+#define CDNS_TORRENT_MODE_2500_BASEX	0x10
+#define CDNS_TORRENT_MODE_QSGMII	0x20
+
 /* Sierra */
 #define CDNS_SIERRA_PLL_CMNLC		0
 #define CDNS_SIERRA_PLL_CMNLC1		1

-- 
2.56.1


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 3/4] dt-bindings: phy: cadence-torrent: add phy-mode constants
  2026-11-20 12:55 ` [PATCH 3/4] dt-bindings: phy: cadence-torrent: add phy-mode constants Benjamin Robin
@ 2026-11-20 13:10   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-20 13:10 UTC (permalink / raw)
  To: Benjamin Robin
  Cc: devicetree, linux-phy, robh, conor+dt, vkoul, neil.armstrong,
	olteanv

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dt-bindings: phy: cadence-torrent: cdns,phy-mode conflates
  modes and duplicates QSGMII
--

commit 2de59f32b4a9ba0a123cffdae1bfb6c23d74476c
Author: Benjamin Robin <benjamin.robin@bootlin.com>

dt-bindings: phy: cadence-torrent: add phy-mode constants

This commit adds the cdns,phy-mode property to the Cadence Torrent PHY
binding and defines the CDNS_TORRENT_MODE_* constants in
include/dt-bindings/phy/phy-cadence.h.

> diff --git a/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml b/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
> index 8e845e2373acd..17a8343ac8990 100644
> --- a/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
> +++ b/Documentation/devicetree/bindings/phy/phy-cadence-torrent.yaml
> @@ -101,6 +101,15 @@ patternProperties:
>          $ref: /schemas/types.yaml#/definitions/uint32
>          enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>  
> +      cdns,phy-mode:
> +        description:
> +          Specifies an optional mode of PHY for which the group of PHY lanes is used.
> +          Refer include/dt-bindings/phy/phy-cadence.h for the constants to be used.
> +        $ref: /schemas/types.yaml#/definitions/uint32
> +        minimum: 0
> +        maximum: 63
> +        default: 0
> +
>        cdns,num-lanes:
>          description:
>            Number of lanes.
> diff --git a/include/dt-bindings/phy/phy-cadence.h b/include/dt-bindings/phy/phy-cadence.h
> index 0671991208fcd..94945b843288c 100644
> --- a/include/dt-bindings/phy/phy-cadence.h
> +++ b/include/dt-bindings/phy/phy-cadence.h
> @@ -15,6 +15,14 @@
>  #define CDNS_TORRENT_DERIVED_REFCLK	1
>  #define CDNS_TORRENT_RECEIVED_REFCLK	2
>  
> +#define CDNS_TORRENT_MODE_NONE		0x00
> +#define CDNS_TORRENT_MODE_USB_G1	0x01
> +#define CDNS_TORRENT_MODE_USB_IO_11V	0x02
> +#define CDNS_TORRENT_MODE_XAUI_LP	0x04
> +#define CDNS_TORRENT_MODE_SATA_LP	0x08
> +#define CDNS_TORRENT_MODE_2500_BASEX	0x10
> +#define CDNS_TORRENT_MODE_QSGMII	0x20
> +
>  /* Sierra */
>  #define CDNS_SIERRA_PLL_CMNLC		0
>  #define CDNS_SIERRA_PLL_CMNLC1		1

[Severity: Medium]
Should cdns,phy-mode be modeling all of these as a single unconstrained
0..63 bitmask?

First, CDNS_TORRENT_MODE_QSGMII duplicates PHY_TYPE_QSGMII (9 in
include/dt-bindings/phy/phy.h), which is already accepted by cdns,phy-type,
creating two ways to describe QSGMII in Device Tree.

Second, the CDNS_TORRENT_MODE_* flags pack orthogonal hardware properties
into one bitmask: an I/O supply voltage characteristic
(CDNS_TORRENT_MODE_USB_IO_11V), a USB link generation limit
(CDNS_TORRENT_MODE_USB_G1), protocol low-power settings
(CDNS_TORRENT_MODE_XAUI_LP and CDNS_TORRENT_MODE_SATA_LP), and Ethernet
interface sub-modes (CDNS_TORRENT_MODE_2500_BASEX and
CDNS_TORRENT_MODE_QSGMII).

Finally, constraining cdns,phy-mode only with minimum: 0 and maximum: 63
allows arbitrary bitwise combinations of mutually exclusive protocol flags
to pass dtbs_check without per-cdns,phy-type validation.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-phy-cadence-torrent-ng-v1-0-da2db7125cd7@bootlin.com?part=3

-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 4/4] phy: cadence-torrent: rewrite Torrent PHY driver
       [not found] ` <20261120-phy-cadence-torrent-ng-v1-4-da2db7125cd7@bootlin.com>
@ 2026-11-20 13:33   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-20 13:33 UTC (permalink / raw)
  To: Benjamin Robin
  Cc: devicetree, linux-phy, robh, conor+dt, vkoul, neil.armstrong,
	olteanv

Thank you for your contribution! Sashiko AI review found 14 potential
issues to consider:
- [High] phy: cadence-torrent: 32-bit shift overflow in
  tp_write_pll_regs()
- [High] phy: cadence-torrent: CDB register access before apb_rst
  deassertion
- [High] phy: cadence-torrent: DP init and configure break cdns-mhdp8546
  consumer
- [High] phy: cadence-torrent: missing mlane shift in
  tp_dptx_set_pll_en() and lanes
- [High] phy: cadence-torrent: nb_phy_on underflow in tp_phy_off() when
  preconfigured
- [High] phy: cadence-torrent: tp_resume_noirq() leaves active links
  held in reset
- [Medium] phy: cadence-torrent: REV_R110 enum typo in
  tp_write_pll_cp_padj()
- [Medium] phy: cadence-torrent: unreachable FBR mode in
  tp_calc_pcie_pll_fb_div_mode()
- [Medium] phy: cadence-torrent: stale SSC and divider regs in
  tp_write_pll_regs()
- [Medium] phy: cadence-torrent: unbalanced clk disable in
  tp_phy_cleanup()
- [Medium] phy: cadence-torrent: skipped refclk check in
  tp_ref_clks_init()
- [Medium] phy: cadence-torrent: missed PCIe ML flag in
  tp_check_multiple_pcie_links()
- [Medium] phy: cadence-torrent: rejected standard swap with clk1 in
  tp_find_link_cfg()
- [Medium] phy: cadence-torrent: unchecked tp_dp_run() and missing reset
  rollback
--

commit 3480ec972d0cd2d542d547f1d45d7f4f38415bc8
Author: Benjamin Robin <benjamin.robin@bootlin.com>

phy: cadence-torrent: rewrite Torrent PHY driver

This commit rewrites the Cadence Torrent PHY driver to compute clock and
PLL register values at runtime and look up remaining protocol, revision,
and process-node register settings per register instead of using static
register tables.

> diff --git a/drivers/phy/cadence/phy-cadence-torrent.c b/drivers/phy/cadence/phy-cadence-torrent.c
> index 1e178aa2ffd76..74a65f0f4b242 100644
> --- a/drivers/phy/cadence/phy-cadence-torrent.c
> +++ b/drivers/phy/cadence/phy-cadence-torrent.c

[ ... ]

> @@ -394,5453 +515,4629 @@ enum phy_powerstate {
>  	POWERSTATE_A3 = 3,
>  };
>  
> +static const struct tp_link_cfg tp_link_cfgs[] = {
> +	{
> +		/* PLL 0 M0: PCIe, PLL 0 M1: PCIe, PLL 1: None */
> +		.std0 = STD0_PCIE_SL,	.std1 = STD1_NONE,
> +		.pll_freq = { PLL_10_00_GHZ, PLL_8_00_GHZ, PLL_8_00_GHZ },
> +		.phy_pll_cfg = 0x0001,
> +		.pll_clk_sel = { 0x0601, 0x0400, 0x0400 },
> +		.xcvr_diag_plldrc_ctrl = { 0x0012, 0x0012 },
> +		.xcvr_diag_hsclk_sel = { 0x0000, 0x0000 },
> +		.xcvr_diag_hsclk_div = { 0x0001, 0x0001 },
> +	},

[ ... ]

> +static enum tp_pll_fb_div_mode tp_calc_pcie_pll_fb_div_mode(const struct tp_phy *tp_phy)
> +{
> +	u32 tech_tag = tp_phy->tech_tag;
> +	enum tp_revision rev = tp_phy->rev;
> +	enum tp_pll_fb_div_mode div_mode;
> +
> +	if (tech_tag & (TECH_TSMC_16 | TECH_TSMC_12)) {
> +		if (rev == REV_R100) {
> +			if (tp_phy->plls[PLL1_MODE0].freq_khz == 0)
> +				div_mode = PLL_FB_DIV_MODE_FBR;
> +			else
> +				div_mode = PLL_FB_DIV_MODE_D32;
> +		} else {

[Severity: Medium]
Can tp_phy->plls[PLL1_MODE0].freq_khz ever be 0 here?

Every entry in tp_link_cfgs[], including single-link PCIe (STD0_PCIE_SL
with STD1_NONE), sets pll_freq[PLL1_MODE0] to a non-zero frequency
(PLL_8_00_GHZ) rather than PLL_UNUSED.  Because tp_init_plls_data()
populates plls[PLL1_MODE0].freq_khz from pll_freq[PLL1_MODE0] before
calling tp_calc_pll_fb_div_modes(), single-link PCIe on TSMC 16nm/12nm
REV_R100 hardware always selects PLL_FB_DIV_MODE_D32 instead of
PLL_FB_DIV_MODE_FBR, misprogramming the PLL control, VCO calibration, and
lock registers.

[ ... ]

> +static void tp_write_pll_cp_padj(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> +{
> +	u32 tech_tag = tp_phy->tech_tag;
> +	u32 rev_msk = BIT(tp_phy->rev);
> +	const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> +	u16 val;
> +
> +	/* PLL charge pump proportional path adjust register */
> +	if (pll->freq_khz < 8000000) {
> +		val = 0x0B17;
> +	} else if (pll->fractional_mode) {
> +		if (tech_tag & TECH_TSMC_7)
> +			val = 0x0B17;
> +		else if (tech_tag & (TECH_SS_5 | TECH_SS_7))
> +			val = 0x0309;
> +		else
> +			val = 0x0509; /* TSMC_5, TSMC_12, TSMC_16, TSMC_16_0805 */
> +	} else if (pll->freq_khz >= 10000000) {
> +		if (tech_tag & TECH_TSMC_7)
> +			val = (rev_msk & (REV_MSK_R100 | REV_MSK_R110)) ? 0x0023 : 0x0035;
> +		else if (tech_tag & TECH_TSMC_5)
> +			val = (pll_id == PLL0_MODE0) ? 0x002D : 0x0035;
> +		else if (tech_tag & (TECH_SS_5 | TECH_SS_7))
> +			val = 0x013E;
> +		else
> +			val = 0x0028; /* TSMC_12, TSMC_16, TSMC_16_0805 */
> +	} else {
> +		if (tech_tag & TECH_TSMC_16)
> +			val = (rev_msk & REV_MSK_R100) ? 0x0028 : 0x0128;
> +		else if (tech_tag & TECH_TSMC_16_0805)
> +			val = 0x0128;
> +		else if (tech_tag & TECH_TSMC_12)
> +			val = 0x0028;
> +		else if (tech_tag & TECH_TSMC_7)
> +			val = (rev_msk & (REV_MSK_R100 | REV_R110)) ? 0x001D : 0x0031;

[Severity: Medium]
Should REV_R110 be REV_MSK_R110 in the TECH_TSMC_7 check?  REV_R110 is
the enum value 1 (equal to REV_MSK_R100) rather than BIT(REV_R110), so R110
hardware (rev_msk == 0x2) never matches and programs
CMN_PDIAG_PLLx_CP_PADJ_Mx with 0x0031 instead of 0x001D for 8 GHz
integer-mode PLLs.

[ ... ]

> +static void tp_write_pll_fd_div_regs(const struct tp_phy *tp_phy, enum tp_pll_id pll_id,
> +				     u32 div_ratio)
> +{
> +	const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> +	const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> +	u32 tech_tag = tp_phy->tech_tag;
> +	u16 high_div;
> +	u16 low_div;
> +
> +	/* In fractional mode, we use default/reset value */
> +	if (pll->fractional_mode)
> +		return;
> +
> +	if (pll_id == PLL0_MODE0 && pll->ref_clk_khz == 100000 && pll->freq_khz == 10000000) {
> +		/* 100 MHz refclk, 10 GHz PLL */
> +		if (tech_tag & (TECH_TSMC_12 | TECH_TSMC_16 | TECH_TSMC_16_0805)) {
> +			high_div = 30;
> +			low_div = 12;
> +		} else {
> +			high_div = 36;
> +			low_div = 6;
> +		}
> +	} else if (pll->ref_clk_khz == 100000 && pll->freq_khz == 8000000) {
> +		/* 100 MHz refclk, 8 GHz PLL */
> +		if (tech_tag & (TECH_TSMC_12 | TECH_TSMC_16 | TECH_TSMC_16_0805)) {
> +			high_div = 16;
> +			low_div = 16;
> +		} else {
> +			high_div = 28;
> +			low_div = 4;
> +		}
> +	} else {
> +		/* Generic calculations */
> +
> +		/* PLL high integer divider val: EVEN(div_ratio_2 * 67/100) - 4 */
> +		high_div = DIV_ROUND_UP((div_ratio >> 1) * 67U, 100U);
> +		high_div -= (high_div & 1U) ? 3U : 4U;
> +
> +		/* PLL low integer divider val: div_ratio_2 - 8 - high_div */
> +		low_div = (div_ratio >> 1) - 8 - high_div;
> +	}
> +
> +	tp_reg_write(base, CMN_PLLx_DSM_FBH_OVRD_Mx(pll_id), high_div);
> +	tp_reg_write(base, CMN_PLLx_DSM_FBL_OVRD_Mx(pll_id), low_div);
> +}
> +
> +static void tp_write_pll_itrim_reg(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> +{
> +	const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> +	const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> +
> +	if (tp_phy->tech_tag & TECH_TSMC_ALL) {
> +		u16 val = (pll->freq_khz >= 10000000) ? 0x003F : 0x007F;
> +
> +		tp_reg_write(base, CMN_PDIAG_PLLx_ITRIM_Mx(pll_id), val);
> +	}
> +}
> +
> +static void tp_write_pll_regs(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> +{
> +	const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> +	const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> +	u32 fd_div_prescale;
> +	u32 div_ratio;
> +	u16 fracdivl;
> +	u16 val;
> +
> +	if (pll_id != PLL0_MODE1)
> +		tp_write_pll_vco_regs(tp_phy, pll_id);
> +
> +	/* Divide ratio */
> +	div_ratio = pll->freq_khz / pll->ref_clk_khz;
> +
> +	/* PLL feedback divider prescale */
> +	fd_div_prescale = ((!pll->fractional_mode) || (div_ratio > 512)) ? 1 : 0;
> +
> +	/* PLL integer divider value */
> +	val = (pll->fractional_mode) ? (div_ratio >> fd_div_prescale) : 0;
> +	tp_reg_write(base, CMN_PLLx_INTDIV_Mx(pll_id), val);
> +
> +	/* PLL fractional divider value */
> +	if (pll->fractional_mode) {
> +		u32 den = pll->ref_clk_khz << fd_div_prescale;
> +		u32 rem = pll->freq_khz % den;
> +
> +		fracdivl = DIV_ROUND_CLOSEST(rem << 16, den);
> +	} else {
> +		fracdivl = 0;
> +	}
> +
> +	tp_reg_write(base, CMN_PLLx_FRACDIVL_Mx(pll_id), fracdivl);
> +	tp_reg_write(base, CMN_PLLx_FRACDIVH_Mx(pll_id), pll->fractional_mode ? 2 : 0);
> +
> +	/* PLL high threshold value */
> +	if (pll->fractional_mode) {
> +		/* EVEN(CEIL((fracdivl/65536 + (div_ratio >> fd_div_prescale)) * 2 / 3)) */
> +		u32 num = fracdivl + (div_ratio << (16 - fd_div_prescale));

[Severity: High]
Can rem << 16 overflow 32-bit u32 when computing fracdivl in
tp_write_pll_regs()?

When rem >= 65535 (for example, DisplayPort at the default 8100 Mbps rate
with a supported 156.25 MHz reference clock, where den = 156250 and
rem = 131250), rem << 16 overflows u32 and corrupts both
CMN_PLLx_FRACDIVL_Mx and CMN_PLLx_HIGH_THR_Mx (producing fracdivl = 0x004B
instead of 0xD70A).  Also, when fd_div_prescale == 1 and div_ratio is odd
(e.g. 20 MHz refclk with 10.3125 GHz USXGMII where div_ratio = 515),
div_ratio << (16 - fd_div_prescale) retains bit 0 of div_ratio instead of
using (div_ratio >> fd_div_prescale) << 16, double-counting the odd
remainder already included in fracdivl.

> +
> +		val = DIV_ROUND_UP(num, 3 << 15);
> +		if (val & 1)
> +			val++;
> +	} else {
> +		val = 0;
> +	}
> +
> +	tp_reg_write(base, CMN_PLLx_HIGH_THR_Mx(pll_id), val);
> +
> +	/* PLL delta sigma modulator diagnostics register */
> +	tp_reg_write(base, CMN_PLLx_DSM_DIAG_Mx(pll_id),
> +		     pll->fractional_mode ? 0x0004 : 0x4004);
> +
> +	/* PLL high and low integer divider value */
> +	tp_write_pll_fd_div_regs(tp_phy, pll_id, div_ratio);
> +
> +	if (pll->ssc)
> +		tp_write_ssc_ctrl(tp_phy, pll_id, fd_div_prescale);

[Severity: Medium]
Does dynamic DisplayPort rate reconfiguration leave stale values in
CMN_PLLx_SS_CTRL1_Mx..CTRL4_Mx and CMN_PLLx_DSM_FBH/FBL_OVRD_Mx?

When tp_dp_update_pll_freq() -> tp_write_pll_regs() reconfigures a DP link
from SSC enabled to SSC disabled, skipping tp_write_ssc_ctrl() leaves
CMN_PLLx_SS_CTRL1_Mx..CTRL4_Mx enabled in hardware while VCO and lock
counters are programmed for non-SSC operation.  Similarly, if a DP PLL
switches from integer mode (e.g. 2.7/5.4 Gbps with 100 MHz refclk and no
SSC) to fractional mode, returning early in tp_write_pll_fd_div_regs()
leaves the previous integer divider overrides in CMN_PLLx_DSM_FBH_OVRD_Mx
and CMN_PLLx_DSM_FBL_OVRD_Mx instead of restoring their reset values.

[ ... ]

> +static int tp_dptx_set_pll_en(const struct tp_phy *tp_phy, u32 num_lanes, bool enable)
> +{
> +	void __iomem *addr;
> +	u32 pll_ack_val;
> +	u32 rd_val;
> +	int ret;
> +
> +	/* Used to determine which bits to check for or enable in PHY_PMA_XCVR_PLLCLK_EN reg */
> +	u32 pll_bits;
> +	/* Used to enable or disable lanes. */
> +	u32 pll_val;
> +
> +	/* Select reg value and mask, depending on enabled lane count. */
> +	pll_val = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_PLLCLK_EN);
> +
> +	if (enable) {
> +		pll_bits = ((1 << num_lanes) - 1);
> +		pll_val |= pll_bits;
> +		pll_ack_val = pll_bits;
> +	} else {
> +		pll_bits = ((1 << num_lanes) - 1);
> +		pll_val &= (~pll_bits);
> +		pll_ack_val = 0;
> +	}

[ ... ]

> +static void tp_dp_set_lanes(const struct tp_phy *tp_phy, struct tp_link *link, u32 num_lanes)
> +{
> +	u32 pma_tx_elec_idle_mask;
> +	u8 used_lane_mask;
> +	u8 all_lane_mask;
> +	u32 pll_clk_en;
> +	u32 pwr_state;
> +	u32 value;
> +	u32 mlane = link->mlane;
> +
> +	used_lane_mask = ((1U << num_lanes) - 1) << mlane;
> +	all_lane_mask = ((1U << link->max_lanes) - 1) << mlane;

[ ... ]

> +	/* Clear requested power state set PLL clock enable to 0 on all used lanes. */
> +	pwr_state = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_POWER_STATE_REQ);
> +	pll_clk_en = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_PLLCLK_EN);
> +
> +	for (u32 i = 0; i < num_lanes; ++i)
> +		pwr_state &= ~(PMA_XCVR_POWER_STATE_REQ_LN_MASK << PHY_POWER_STATE_LN(mlane + i));
> +
> +	pll_clk_en &= ~used_lane_mask;
> +
> +	tp_reg_dptx_write(tp_phy, PHY_PMA_XCVR_POWER_STATE_REQ, pwr_state);
> +	tp_reg_dptx_write(tp_phy, PHY_PMA_XCVR_PLLCLK_EN, pll_clk_en);
> +
> +	/* release master lane reset */
> +	value |= 1 << mlane;
> +	tp_reg_dptx_write(tp_phy, PHY_RESET, value);
> +
> +	/* update the number of used lanes */\n+	link->num_lanes = num_lanes;
> +}

[Severity: High]
Should pll_bits in tp_dptx_set_pll_en() be shifted by link->mlane?

Unlike tp_dp_set_lanes() and tp_dptx_set_power_state(), tp_dptx_set_pll_en()
computes pll_bits = ((1 << num_lanes) - 1) without shifting by link->mlane,
so any DisplayPort link with mlane > 0 modifies PHY_PMA_XCVR_PLLCLK_EN for
lane 0 and times out polling PHY_PMA_XCVR_PLLCLK_EN_ACK.

In addition, tp_dp_set_lanes() only clears PHY_PMA_XCVR_POWER_STATE_REQ and
PHY_PMA_XCVR_PLLCLK_EN across used_lane_mask instead of all_lane_mask,
leaving disabled lanes' PLLCLK_EN bits set when reducing the lane count.
And when both set_lanes and set_rate are requested in tp_phy_configure(),
running tp_dp_set_lanes() before tp_dp_set_rate() resets the lanes, clears
PLLCLK_EN, and updates link->num_lanes before tp_dp_set_rate() transitions
the previously active lanes to POWERSTATE_A3 (while tp_write_lanes_reg()
also only programs link->num_lanes instead of link->max_lanes).

[ ... ]

> +static int tp_phy_configure(struct phy *phy, union phy_configure_opts *opts)
> +{
> +	struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> +	struct tp_link *link = phy_get_drvdata(phy);
> +	bool run_needed = false;
> +	int ret;
> +
> +	guard(mutex)(&tp_phy->lock);
> +
> +	if (tp_phy->already_configured)
> +		return 0;
> +
> +	if (link->type != STD_TYPE_DP)
> +		return -EINVAL;
> +
> +	ret = tp_dp_verify_config(link, &opts->dp);
> +	if (ret) {
> +		dev_err(&phy->dev, "invalid params for phy configure\n");
> +		return ret;
> +	}
> +
> +	if (opts->dp.set_lanes && opts->dp.lanes != link->num_lanes) {
> +		tp_dp_set_lanes(tp_phy, link, opts->dp.lanes);
> +		run_needed = true;
> +	}
> +
> +	if (opts->dp.set_rate) {
> +		ret = tp_dp_set_rate(tp_phy, link, &opts->dp);
> +		if (ret) {
> +			dev_err(&phy->dev, "Set rate failed\n");
> +			return ret;
> +		}
> +		run_needed = true;
> +	}
> +
> +	if (opts->dp.set_voltages)
> +		tp_dp_set_voltages(tp_phy, link, &opts->dp);
> +
> +	if (phy->power_count && run_needed)
> +		ret = tp_dp_run(tp_phy, link);
> +
> +	return ret;
> +}

[Severity: High]
Does moving DisplayPort reset deassertion and tp_dp_run() out of
tp_phy_init() and gating tp_dp_run() on phy->power_count break
cdns-mhdp8546?

The in-tree DisplayPort bridge consumer
(drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c) calls phy_init() in
cdns_mhdp_probe() and phy_configure() in cdns_mhdp_link_training_init(),
but never calls phy_power_on().  Because tp_dp_phy_init() no longer
deasserts phy_rst/lnk_rst or starts the DP PLL and lanes, and
tp_phy_configure() skips tp_dp_run() when phy->power_count is 0, the PHY
remains held in reset and tp_dp_set_rate() polls PHY_PMA_CMN_CTRL2 on an
unstarted PHY.

> +
> +static int tp_phy_on(struct phy *phy)
> +{
> +	struct tp_link *link = phy_get_drvdata(phy);
> +	struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> +	u16 val;
> +	int ret;
> +
> +	guard(mutex)(&tp_phy->lock);
> +
> +	if (tp_phy->already_configured) {
> +		/* Give 5ms to 10ms delay for the PIPE clock to be stable */
> +		usleep_range(5000, 10000);
> +		return 0;
> +	}
> +
> +	if (tp_phy->nb_phy_on == 0) {
> +		/* Take the PHY out of reset */
> +		ret = reset_control_deassert(tp_phy->phy_rst);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	/* Take the PHY lane group out of reset */
> +	ret = reset_control_deassert(link->lnk_rst);
> +	if (ret)
> +		return ret;
> +
> +	/*
> +	 * Wait for cmn_ready assertion
> +	 * PHY_PMA_CMN_CTRL1[0] == 1
> +	 */
> +	ret = read_poll_timeout(tp_reg_read, val, val & 1, 1000, PLL_LOCK_TIMEOUT, false,
> +				&tp_phy->reg_phy_pma_common_cdb, PHY_PMA_CMN_CTRL1);
> +	if (ret) {
> +		dev_err(tp_phy->dev, "Timeout waiting for CMN ready\n");
> +		return ret;
> +	}
> +
> +	if (link->type == STD_TYPE_DP) {
> +		tp_dp_run(tp_phy, link);
> +	} else if (link->type == STD_TYPE_PCIE || link->type == STD_TYPE_USB_TXRX) {
> +		struct tp_reg_base *reg_base;
> +
> +		reg_base = &tp_phy->reg_phy_pcs_lane_cdb[link->mlane];
> +		ret = read_poll_timeout(tp_reg_read, val, (val & 2) == 0, 1000,
> +					PLL_LOCK_TIMEOUT, false, reg_base,
> +					PHY_PCS_ISO_LINK_CTRL);
> +		if (ret < 0) {
> +			dev_err(tp_phy->dev, "Timeout waiting for PHY status ready\n");
> +			return ret;
> +		}
> +	}
> +
> +	tp_phy->nb_phy_on++;
> +	return 0;
> +}

[Severity: Medium]
Should tp_phy_on() check the return value of tp_dp_run() and re-assert
link->lnk_rst and tp_phy->phy_rst on error?

Right now a timeout in tp_dp_run() is ignored and tp_phy_on() increments
nb_phy_on and returns 0.  Conversely, if deasserting link->lnk_rst or
polling PHY_PMA_CMN_CTRL1 or PHY_PCS_ISO_LINK_CTRL fails, tp_phy_on()
returns an error without re-asserting the resets it deasserted and without
incrementing phy->power_count, so tp_phy_off() will not be called to clean
them up.

> +
> +static int tp_phy_off(struct phy *phy)
> +{
> +	struct tp_link *inst = phy_get_drvdata(phy);
> +	struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> +	int ret;
> +
> +	guard(mutex)(&tp_phy->lock);
> +
> +	ret = reset_control_assert(inst->lnk_rst);
> +	if (ret)
> +		return ret;
> +
> +	if (tp_phy->nb_phy_on == 1) {
> +		ret = reset_control_assert(tp_phy->phy_rst);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	tp_phy->nb_phy_on--;
> +	return 0;
> +}

[Severity: High]
Can tp_phy_off() underflow tp_phy->nb_phy_on when tp_phy->already_configured
is true?

When tp_phy->already_configured is true, tp_phy_on() returns early without
incrementing nb_phy_on (leaving it at 0), but tp_phy_off() has no
already_configured check: it asserts inst->lnk_rst and decrements the u8
nb_phy_on counter from 0 to 255.  A subsequent phy_power_on() then either
returns early with inst->lnk_rst still asserted, or after system suspend
clears already_configured, sees nb_phy_on != 0, skips deasserting
tp_phy->phy_rst, and times out waiting for CMN ready.

[ ... ]

> +static int tp_ref_clk_init(const struct tp_phy *tp_phy, struct clk *clk, const char *name,
> +			   u32 *p_ref_clk_khz)
> +{
> +	struct device *dev = tp_phy->dev;
> +	u32 ref_clk_rate;
> +	int ret;
> +
> +	ret = clk_prepare_enable(clk);
> +	if (ret) {
> +		dev_err(dev, "Failed to prepare %s ref clock: %d\n", name, ret);
> +		return ret;
> +	}
> +
> +	ref_clk_rate = clk_get_rate(clk);
> +	if (!ref_clk_rate) {
> +		dev_err(dev, "Failed to get %s ref clock rate\n", name);
> +		ret = -EINVAL;
> +		goto disable_clk;
> +	}
> +
> +	for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> +		if (!tp_is_ref_freq_supported(tp_phy->links[i].type, ref_clk_rate)) {
> +			dev_err(dev, "Invalid ref clock rate: %u Hz\n", ref_clk_rate);
> +			ret = -EINVAL;
> +			goto disable_clk;
> +		}
> +	}

[Severity: Medium]
Does this loop skip reference clock validation during probe and reject
valid dual-refclk configurations during resume?

In cdns_torrent_phy_probe(), tp_ref_clks_init() is called before the child
subnodes are parsed, so tp_phy->nb_links is 0 and this loop does not run
at probe time (allowing unsupported clock rates, or rates < 1000 Hz that
set ref_clk_khz to 0 and divide by zero in tp_write_pll_regs()).  Then in
tp_resume_noirq(), where nb_links > 0, tp_ref_clk_init() checks every link
against both tp_phy->clk and tp_phy->clk1 instead of only the links using
that PLL, failing resume with -EINVAL when PLL0 and PLL1 use different
protocol-specific reference frequencies.

[ ... ]

> +static void tp_check_multiple_pcie_links(struct tp_phy *tp_phy)
> +{
> +	struct tp_standard *std0 = &tp_phy->standards[0];
> +	u32 num_pcie_links = 0;
> +
> +	/* First check if the first standard is PCIe */
> +	if (std0->type != STD_TYPE_PCIE)
> +		return;
> +
> +	/* Count the number of links with PCIe protocol. */
> +	for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> +		if (tp_phy->links[i].type == STD_TYPE_PCIE)
> +			num_pcie_links++;
> +	}
> +
> +	/* PCIe Multilink configuration can be supported along with a non-PCIe protocol.
> +	 * Such configurations are handled by adding a flag named STD_FLAG_PCIE_ML
> +	 */
> +	if (num_pcie_links > 1)
> +		std0->flags |= STD_FLAG_PCIE_ML;
> +}

[Severity: Medium]
Does tp_check_multiple_pcie_links() miss multi-link PCIe when a non-PCIe
subnode appears first in Device Tree?

tp_add_phy_from_of_subnode() populates tp_phy->standards[] in DT subnode
order, so if phy@0 is USB or SGMII and phy@1 and phy@2 are PCIe, PCIe is in
standards[1] when tp_check_multiple_pcie_links() runs.  Because
tp_check_multiple_pcie_links() only checks standards[0], it returns without
setting STD_FLAG_PCIE_ML, and when tp_find_link_cfg() subsequently swaps
standards[0] and standards[1], the driver matches single-link PCIe
(STD0_PCIE_SL) instead of multi-link PCIe (STD0_PCIE_ML).

[ ... ]

> +static int tp_find_link_cfg(struct tp_phy *tp_phy)
> +{
> +	const struct tp_link_cfg *link_cfg;
> +	struct device *dev = tp_phy->dev;
> +	struct tp_standard *std0 = &tp_phy->standards[0];
> +	struct tp_standard *std1 = &tp_phy->standards[1];
> +
> +	/* Verify the validity of standard index in links */
> +	for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> +		const struct tp_link *link = &tp_phy->links[i];
> +
> +		if (WARN_ON(link->std_idx >= MAX_NUM_STANDARDS))
> +			return -EINVAL;
> +	}
> +
> +	link_cfg = tp_find_link_cfg_from_standards(std0, std1);
> +
> +	/* Only swap the two standards if PLL1 has no dedicated reference clock */
> +	if (!link_cfg && !tp_phy->clk1) {
> +		link_cfg = tp_find_link_cfg_from_standards(std1, std0);
> +		if (link_cfg) {
> +			/* Swap the 2 standards */
> +			swap(tp_phy->standards[0], tp_phy->standards[1]);
> +
> +			for (u8 i = 0; i < tp_phy->nb_links; ++i)
> +				tp_phy->links[i].std_idx ^= 1;
> +		}
> +	}

[Severity: Medium]
Why is swapping standards[0] and standards[1] disallowed when tp_phy->clk1
is present?

standards[0] and standards[1] are populated in DT child subnode order (lane
order), whereas tp_link_cfgs[] orders entries by PLL assignment (std0 on
PLL0 clocked by refclk, std1 on PLL1 clocked by pll1_refclk).  When
pll1_refclk is provided and the protocol on lower lanes (phy@0) uses PLL1
(for example, USXGMII on phy@0 with PLL1 at 156.25 MHz and PCIe on phy@2
with PLL0 at 100 MHz), refusing to swap standards[0] and standards[1]
causes tp_find_link_cfg() to fail probe with -EINVAL.

[ ... ]

> +static void tp_phy_cleanup(const struct tp_phy *tp_phy)
> +{
> +	reset_control_assert(tp_phy->phy_rst);
> +	reset_control_assert(tp_phy->apb_rst);
> +
> +	for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> +		reset_control_assert(tp_phy->links[i].lnk_rst);
> +		reset_control_put(tp_phy->links[i].lnk_rst);
> +	}
> +
> +	clk_disable_unprepare(tp_phy->clk1);
> +	clk_disable_unprepare(tp_phy->clk);
> +	tp_clk_cleanup(tp_phy);
> +}

[Severity: Medium]
Can tp_phy_cleanup() call clk_disable_unprepare() on clocks that were never
enabled or were already disabled?

In cdns_torrent_phy_probe(), jumping to cleanup when tp_of_get_clk() fails
on pll1_refclk after acquiring tp_phy->clk, when tp_ref_clks_init() fails
(after already disabling any clock it enabled), or when tp_phy->already_configured
is true (where tp_ref_clks_init() is skipped, which also affects
cdns_torrent_phy_remove()) unconditionally calls clk_disable_unprepare() on
tp_phy->clk1 and tp_phy->clk, triggering clock enable/prepare count
underflow warnings.

[ ... ]

> +static int tp_suspend_noirq(struct device *dev)
> +{
> +	struct tp_phy *tp_phy = dev_get_drvdata(dev);
> +
> +	tp_refclk_driver_suspend(tp_phy);
> +
> +	for (u8 i = 0; i < tp_phy->nb_links; ++i)
> +		reset_control_assert(tp_phy->links[i].lnk_rst);
> +
> +	reset_control_assert(tp_phy->phy_rst);
> +	reset_control_assert(tp_phy->apb_rst);
> +
> +	if (tp_phy->already_configured) {
> +		tp_phy->already_configured = false;
> +	} else {
> +		clk_disable_unprepare(tp_phy->clk1);
> +		clk_disable_unprepare(tp_phy->clk);
> +	}
> +
> +	return 0;
> +}
> +
> +static int tp_resume_noirq(struct device *dev)
> +{
> +	int ret;
> +	struct tp_phy *tp_phy = dev_get_drvdata(dev);
> +
> +	ret = tp_refclk_driver_resume(tp_phy);
> +	if (ret)
> +		return ret;
> +
> +	ret = tp_ref_clks_init(tp_phy);
> +	if (ret)
> +		return ret;
> +
> +	/* Enable APB */
> +	reset_control_deassert(tp_phy->apb_rst);
> +
> +	ret = tp_configure_links(tp_phy);
> +	if (ret)
> +		goto put_lnk_rst;
> +
> +	return 0;

[Severity: High]
Does tp_resume_noirq() leave active links held in reset after suspend?

tp_suspend_noirq() asserts lnk_rst for every link and asserts phy_rst
without changing tp_phy->nb_phy_on, but tp_resume_noirq() only deasserts
apb_rst and calls tp_configure_links() without deasserting phy_rst or
lnk_rst.  Consumers that keep their PHY powered on across suspend (such as
am65-cpsw-nuss for SGMII/QSGMII/USXGMII) are left held in reset after
resume, and sibling consumers on the same SERDES that call
phy_power_off()/phy_power_on() across suspend/resume (such as pci-j721e)
see tp_phy->nb_phy_on != 0 in tp_phy_on(), skip deasserting phy_rst, and
time out waiting for CMN ready.

> +
> +put_lnk_rst:
> +
> +	for (u8 i = 0; i < tp_phy->nb_links; ++i)
> +		reset_control_assert(tp_phy->links[i].lnk_rst);
> +
> +	reset_control_assert(tp_phy->apb_rst);
> +	reset_control_assert(tp_phy->phy_rst);
> +
> +	clk_disable_unprepare(tp_phy->clk1);
> +	clk_disable_unprepare(tp_phy->clk);
> +
> +	return ret;
> +}
> +
> +static DEFINE_NOIRQ_DEV_PM_OPS(tp_pm_ops, tp_suspend_noirq, tp_resume_noirq);
> +
> +static int cdns_torrent_phy_probe(struct platform_device *pdev)
> +{

[ ... ]

> +	tp_reg_bases_init(tp_phy);
> +	tp_reg_fields_init(tp_phy);
> +
> +	ret = tp_read_techno_revision(tp_phy);
> +	if (ret)
> +		return ret;
> +
> +	ret = tp_clk_register(tp_phy);
> +	if (ret)
> +		return ret;
> +
> +	ret = tp_of_get_reset(tp_phy);
> +	if (ret)
> +		goto cleanup;
> +
> +	ret = tp_of_get_clk(tp_phy);
> +	if (ret)
> +		goto cleanup;
> +
> +	tp_phy->already_configured = tp_is_already_configured(tp_phy);
> +	if (!tp_phy->already_configured) {
> +		ret = tp_ref_clks_init(tp_phy);
> +		if (ret)
> +			goto cleanup;
> +
> +		/* Enable APB */
> +		reset_control_deassert(tp_phy->apb_rst);
> +	}

[Severity: High]
Are CDB registers accessed here and in tp_resume_noirq() while
tp_phy->apb_rst is still asserted?

cdns_torrent_phy_probe() reads the PHY ID registers in
tp_read_techno_revision(), writes CMN_CDIAG_REFCLK_DRV0_CTRL in
tp_clk_register(), and reads PHY_PMA_CMN_CTRL1 in
tp_is_already_configured() before tp_of_get_reset() and
reset_control_deassert(tp_phy->apb_rst) run.  Similarly, tp_resume_noirq()
calls tp_refclk_driver_resume() (reading and writing
CMN_CDIAG_REFCLK_DRV0_CTRL) before deasserting tp_phy->apb_rst, which was
asserted in tp_suspend_noirq().  On platforms using the torrent_apb reset
(or when re-probing after driver removal, which asserts apb_rst), accessing
CDB registers while APB is in reset can fail probe with -ENODEV, lose
register writes, or fault.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-phy-cadence-torrent-ng-v1-0-da2db7125cd7@bootlin.com?part=4

-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-20 13:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 12:55 [PATCH 0/4] phy: cadence-torrent: rewrite Torrent PHY driver Benjamin Robin
2026-11-20 12:55 ` [PATCH 1/4] arm64: dts: ti: k3-j7200: Fix QSGMII overlay by adding SERDES PHY Benjamin Robin
2026-11-20 12:55 ` [PATCH 2/4] dt-bindings: phy: cadence-torrent: drop cdns,num-lanes default value Benjamin Robin
2026-11-20 12:55 ` [PATCH 3/4] dt-bindings: phy: cadence-torrent: add phy-mode constants Benjamin Robin
2026-11-20 13:10   ` sashiko-bot
     [not found] ` <20261120-phy-cadence-torrent-ng-v1-4-da2db7125cd7@bootlin.com>
2026-11-20 13:33   ` [PATCH 4/4] phy: cadence-torrent: rewrite Torrent PHY driver sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox