Skip to contentSkip to conversation

Introduce SpacemiT K1 PCIe phy and host controller

PATCH Superseded Alex Elder 8 participants 31 messages Aug 13, 2025 – Oct 1, 2025

Subsystems (6)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Alex Elder

author
#0 lore raw

cover Introduce SpacemiT K1 PCIe phy and host controller

This series introduces a PHY driver and a PCIe driver to support PCIe on the SpacemiT K1 SoC. The PCIe implementation is derived from a Synopsys DesignWare PCIe IP. The PHY driver supports one combination PCIe/USB PHY as well as two PCIe-only PHYs. The combo PHY port uses one PCIe lane, and the other two ports each have two lanes. All PCIe ports operate at 5 GT/second.

The PCIe PHYs must be configured using a value that can only be determined using the combo PHY, operating in PCIe mode. To allow that PHY to be used for USB, the calibration step is performed by the PHY driver automatically at probe time. Once this step is done, the PHY can be used for either PCIe or USB.

-Alex

Alex Elder (6):
dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY
dt-bindings: phy: spacemit: introduce PCIe PHY
dt-bindings: phy: spacemit: introduce PCIe root complex
phy: spacemit: introduce PCIe/combo PHY
PCI: spacemit: introduce SpacemiT PCIe host driver
riscv: dts: spacemit: PCIe and PHY-related updates

 .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++
 .../bindings/phy/spacemit,k1-combo-phy.yaml   | 110 +++
 .../bindings/phy/spacemit,k1-pcie-phy.yaml    |  49 ++
 .../boot/dts/spacemit/k1-bananapi-f3.dts      |  28 +
 arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi  |  33 +
 arch/riscv/boot/dts/spacemit/k1.dtsi          | 169 +++++
 drivers/pci/controller/dwc/Kconfig            |  10 +
 drivers/pci/controller/dwc/Makefile           |   1 +
 drivers/pci/controller/dwc/pcie-k1.c          | 355 ++++++++++
 drivers/phy/Kconfig                           |  11 +
 drivers/phy/Makefile                          |   1 +
 drivers/phy/phy-spacemit-k1-pcie.c            | 639 ++++++++++++++++++
 12 files changed, 1547 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml
 create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml
 create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-pcie-phy.yaml
 create mode 100644 drivers/pci/controller/dwc/pcie-k1.c
 create mode 100644 drivers/phy/phy-spacemit-k1-pcie.c

base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585

Signature · 1 line
2.48.1
author
#1 lore raw

PATCH 1/6 dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY

Add the Device Tree binding for the PCIe/USB 3.0 combo PHY found in the SpacemiT K1 SoC. This is one of three PCIe PHYs, and is unusual in that only the combo PHY can perform a calibration step needed to determine settings used by the other two PCIe PHYs.

Calibration must be done with the combo PHY in PCIe mode, and to allow this to occur independent of the eventual use for the PHY (PCIe or USB) some PCIe-related properties must be supplied: clocks; resets; and a syscon phandle.

---
 .../bindings/phy/spacemit,k1-combo-phy.yaml   | 110 ++++++++++++++++++
 1 file changed, 110 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +110 -0
new file mode 100644
index 0000000000000..ed78083a53231
@@ -0,0 +1,110 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/phy/spacemit,k1-combo-phy.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCIe/USB3 Combo PHY
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:
+  Of the three PHYs on the SpacemiT K1 SoC capable of being used for
+  PCIe, one is a combo PHY that can also be configured for use by a
+  USB 3 controller.  Using PCIe or USB 3 is a board design decision.
+
+  The combo PHY is also the only PCIe PHY that is able to determine
+  PCIe calibration values to use, and this must be determined before
+  the other two PCIe PHYs can be used.  This calibration must be
+  performed with the combo PHY in PCIe mode, and is this is done
+  when the combo PHY is probed.
+
+  During normal operation, the PCIe or USB port driver is responsible
+  for ensuring all clocks needed by a PHY are enabled, and all resets
+  affecting the PHY are deasserted.  However, for the combo PHY to
+  perform calibration independent of whether it's later used for
+  PCIe or USB, all PCIe mode clocks and resets must be defined.
+
+properties:
+  compatible:
+    const: spacemit,k1-combo-phy
+
+  reg:
+    items:
+      - description: PHY control registers
+
+  clocks:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) clock
+      - description: DWC PCIe application AXI-bus Master interface clock
+      - description: DWC PCIe application AXI-bus Slave interface clock.
+
+  clock-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+
+  resets:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) reset
+      - description: DWC PCIe application AXI-bus Master interface reset
+      - description: DWC PCIe application AXI-bus Slave interface reset.
+      - description: Global reset; must be deasserted for PHY to function
+
+  reset-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+      - const: global
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in setting the mode
+    $ref: /schemas/types.yaml#/definitions/phandle
+
+  "#phy-cells":
+    const: 1
+    description:
+      The argument value (PHY_TYPE_PCIE or PHY_TYPE_USB3) determines
+      whether the PHY operates in PCIe or USB3 mode.
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#phy-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    combo_phy: phy@c0b10000 {
+        compatible = "spacemit,k1-combo-phy";
+        reg = <0xc0b10000 0x1000>;
+        clocks = <&syscon_apmu CLK_PCIE0_DBI>,
+                 <&syscon_apmu CLK_PCIE0_MASTER>,
+                 <&syscon_apmu CLK_PCIE0_SLAVE>;
+        clock-names = "dbi",
+                      "mstr",
+                      "slv";
+        resets = <&syscon_apmu RESET_PCIE0_DBI>,
+                 <&syscon_apmu RESET_PCIE0_MASTER>,
+                 <&syscon_apmu RESET_PCIE0_SLAVE>,
+                 <&syscon_apmu RESET_PCIE0_GLOBAL>;
+        reset-names = "dbi",
+                      "mstr",
+                      "slv",
+                      "global";
+        spacemit,syscon-pmu = <&syscon_apmu>;
+        #phy-cells = <1>;
+        status = "disabled";
+    };
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH 1/6] dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY

Quoted 21 lines · Alex Elder

Add the Device Tree binding for the PCIe/USB 3.0 combo PHY found in the SpacemiT K1 SoC. This is one of three PCIe PHYs, and is unusual in that only the combo PHY can perform a calibration step needed to determine settings used by the other two PCIe PHYs.

Calibration must be done with the combo PHY in PCIe mode, and to allow this to occur independent of the eventual use for the PHY (PCIe or USB) some PCIe-related properties must be supplied: clocks; resets; and a syscon phandle.

---
 .../bindings/phy/spacemit,k1-combo-phy.yaml   | 110 ++++++++++++++++++
 1 file changed, 110 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +0 -0
new file mode 100644
index 0000000000000..ed78083a53231

...

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 67

Quoted 5 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +5 -0
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in setting the mode
+    $ref: /schemas/types.yaml#/definitions/phandle

Clock controllers and ethernet controllers all use spacemit,apmu to refer the APMU system controller. Do you think it's better to keep them aligned?

...

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 90

Quoted 7 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +7 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    combo_phy: phy@c0b10000 {

This label is unnecessary.

Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +10 -0
+        compatible = "spacemit,k1-combo-phy";
+        reg = <0xc0b10000 0x1000>;
+        clocks = <&syscon_apmu CLK_PCIE0_DBI>,
+                 <&syscon_apmu CLK_PCIE0_MASTER>,
+                 <&syscon_apmu CLK_PCIE0_SLAVE>;
+        clock-names = "dbi",
+                      "mstr",
+                      "slv";
+        resets = <&syscon_apmu RESET_PCIE0_DBI>,
+                 <&syscon_apmu RESET_PCIE0_MASTER>,

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 110

Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +10 -0
+                 <&syscon_apmu RESET_PCIE0_SLAVE>,
+                 <&syscon_apmu RESET_PCIE0_GLOBAL>;
+        reset-names = "dbi",
+                      "mstr",
+                      "slv",
+                      "global";
+        spacemit,syscon-pmu = <&syscon_apmu>;
+        #phy-cells = <1>;
+        status = "disabled";
+    };

Best regards,
Yao Zi

author
#3 lore raw

Re: [PATCH 1/6] dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY

Quoted 31 lines · Yao Zi
Quoted 21 lines · Alex Elder

Add the Device Tree binding for the PCIe/USB 3.0 combo PHY found in the SpacemiT K1 SoC. This is one of three PCIe PHYs, and is unusual in that only the combo PHY can perform a calibration step needed to determine settings used by the other two PCIe PHYs.

Calibration must be done with the combo PHY in PCIe mode, and to allow this to occur independent of the eventual use for the PHY (PCIe or USB) some PCIe-related properties must be supplied: clocks; resets; and a syscon phandle.

---
  .../bindings/phy/spacemit,k1-combo-phy.yaml   | 110 ++++++++++++++++++
  1 file changed, 110 insertions(+)

create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +0 -0
new file mode 100644
index 0000000000000..ed78083a53231

...

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 67

Quoted 5 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +5 -0
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in setting the mode
+    $ref: /schemas/types.yaml#/definitions/phandle
Quoted 3 lines

Clock controllers and ethernet controllers all use spacemit,apmu to refer the APMU system controller. Do you think it's better to keep them aligned?

I do think it's better to keep them aligned.

And I appreciate your noticing this. I don't see anything that's accepted upstream that defines properties like this, but I now see this:

https://lore.kernel.org/lkml/[email protected]/

I did a quick scan for what others do when a property's value is a phandle, and other than just "syscon" it seems that word is omitted.

So unless someone else suggests otherwise, I'll use "spacemit,apmu" for this property in my next version.

Quoted 11 lines

...

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 90

Quoted 7 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +7 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    combo_phy: phy@c0b10000 {

This label is unnecessary.

OK. I used it when testing USB but we can add the label back when that driver gets reviewed.

Quoted 21 lines
Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +10 -0
+        compatible = "spacemit,k1-combo-phy";
+        reg = <0xc0b10000 0x1000>;
+        clocks = <&syscon_apmu CLK_PCIE0_DBI>,
+                 <&syscon_apmu CLK_PCIE0_MASTER>,
+                 <&syscon_apmu CLK_PCIE0_SLAVE>;
+        clock-names = "dbi",
+                      "mstr",
+                      "slv";
+        resets = <&syscon_apmu RESET_PCIE0_DBI>,
+                 <&syscon_apmu RESET_PCIE0_MASTER>,

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 110

Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +10 -0
+                 <&syscon_apmu RESET_PCIE0_SLAVE>,
+                 <&syscon_apmu RESET_PCIE0_GLOBAL>;
+        reset-names = "dbi",
+                      "mstr",
+                      "slv",
+                      "global";
+        spacemit,syscon-pmu = <&syscon_apmu>;
+        #phy-cells = <1>;
+        status = "disabled";
+    };
Quoted 2 lines

Best regards,
Yao Zi

Thanks a lot.

-Alex

#4 lore raw

Re: [PATCH 1/6] dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY

In reply to Alex Elder

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 109

Quoted 6 lines · Alex Elder
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +6 -0
+                      "mstr",
+                      "slv",
+                      "global";
+        spacemit,syscon-pmu = <&syscon_apmu>;
+        #phy-cells = <1>;
+        status = "disabled";

You cannot have disabled examples. This also means it could not be checked/tested.

Quoted 1 line

+ };

Best regards,
Krzysztof

author
#5 lore raw

Re: [PATCH 1/6] dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY

Quoted 8 lines · Krzysztof Kozlowski

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 109

Quoted 6 lines · Alex Elder
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +6 -0
+                      "mstr",
+                      "slv",
+                      "global";
+        spacemit,syscon-pmu = <&syscon_apmu>;
+        #phy-cells = <1>;
+        status = "disabled";
Quoted 8 lines

You cannot have disabled examples. This also means it could not be checked/tested.

Quoted 1 line

+ };

Best regards,
Krzysztof

OK I'll fix that in both places. I think I just copied in the actual DTS content, but I now understand the problem.

Thanks.

-Alex

maintainer
#6 lore raw

Re: [PATCH 1/6] dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY

In reply to Alex Elder
Quoted 24 lines · Alex Elder

Add the Device Tree binding for the PCIe/USB 3.0 combo PHY found in the SpacemiT K1 SoC. This is one of three PCIe PHYs, and is unusual in that only the combo PHY can perform a calibration step needed to determine settings used by the other two PCIe PHYs.

Calibration must be done with the combo PHY in PCIe mode, and to allow this to occur independent of the eventual use for the PHY (PCIe or USB) some PCIe-related properties must be supplied: clocks; resets; and a syscon phandle.

---
 .../bindings/phy/spacemit,k1-combo-phy.yaml   | 110 ++++++++++++++++++
 1 file changed, 110 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +2 -0
new file mode 100644
index 0000000000000..ed78083a53231
@@ -0,0 +1,110 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 12

Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +10 -0
+---
+$id: http://devicetree.org/schemas/phy/spacemit,k1-combo-phy.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCIe/USB3 Combo PHY
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:

You need a '>' or the paragraphs formatting will not be maintained (should we ever render docs from this).

Quoted 19 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +19 -0
+  Of the three PHYs on the SpacemiT K1 SoC capable of being used for
+  PCIe, one is a combo PHY that can also be configured for use by a
+  USB 3 controller.  Using PCIe or USB 3 is a board design decision.
+
+  The combo PHY is also the only PCIe PHY that is able to determine
+  PCIe calibration values to use, and this must be determined before
+  the other two PCIe PHYs can be used.  This calibration must be
+  performed with the combo PHY in PCIe mode, and is this is done
+  when the combo PHY is probed.
+
+  During normal operation, the PCIe or USB port driver is responsible
+  for ensuring all clocks needed by a PHY are enabled, and all resets
+  affecting the PHY are deasserted.  However, for the combo PHY to
+  perform calibration independent of whether it's later used for
+  PCIe or USB, all PCIe mode clocks and resets must be defined.
+
+properties:
+  compatible:
+    const: spacemit,k1-combo-phy

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 41

Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +10 -0
+
+  reg:
+    items:
+      - description: PHY control registers
+
+  clocks:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) clock
+      - description: DWC PCIe application AXI-bus Master interface clock
+      - description: DWC PCIe application AXI-bus Slave interface clock.

End with a period or don't. Just be consistent.

You need DWC PCIe clocks for your PHY? A ref clock would make sense, but these? I've never seen a PHY with a AXI master interface.

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 53

Quoted 12 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +12 -0
+
+  clock-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+
+  resets:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) reset
+      - description: DWC PCIe application AXI-bus Master interface reset
+      - description: DWC PCIe application AXI-bus Slave interface reset.

Same here (on both points).

Quoted 27 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +27 -0
+      - description: Global reset; must be deasserted for PHY to function
+
+  reset-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+      - const: global
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in setting the mode
+    $ref: /schemas/types.yaml#/definitions/phandle
+
+  "#phy-cells":
+    const: 1
+    description:
+      The argument value (PHY_TYPE_PCIE or PHY_TYPE_USB3) determines
+      whether the PHY operates in PCIe or USB3 mode.
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 90

Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +10 -0
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#phy-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    combo_phy: phy@c0b10000 {

Drop unused labels.

Quoted 23 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +20 -0
+        compatible = "spacemit,k1-combo-phy";
+        reg = <0xc0b10000 0x1000>;
+        clocks = <&syscon_apmu CLK_PCIE0_DBI>,
+                 <&syscon_apmu CLK_PCIE0_MASTER>,
+                 <&syscon_apmu CLK_PCIE0_SLAVE>;
+        clock-names = "dbi",
+                      "mstr",
+                      "slv";
+        resets = <&syscon_apmu RESET_PCIE0_DBI>,
+                 <&syscon_apmu RESET_PCIE0_MASTER>,
+                 <&syscon_apmu RESET_PCIE0_SLAVE>,
+                 <&syscon_apmu RESET_PCIE0_GLOBAL>;
+        reset-names = "dbi",
+                      "mstr",
+                      "slv",
+                      "global";
+        spacemit,syscon-pmu = <&syscon_apmu>;
+        #phy-cells = <1>;
+        status = "disabled";
+    };
Signature · 1 line
2.48.1
author
#7 lore raw

Re: [PATCH 1/6] dt-bindings: phy: spacemit: add SpacemiT PCIe/combo PHY

Quoted 30 lines · Rob Herring
Quoted 29 lines · Alex Elder

Add the Device Tree binding for the PCIe/USB 3.0 combo PHY found in the SpacemiT K1 SoC. This is one of three PCIe PHYs, and is unusual in that only the combo PHY can perform a calibration step needed to determine settings used by the other two PCIe PHYs.

Calibration must be done with the combo PHY in PCIe mode, and to allow this to occur independent of the eventual use for the PHY (PCIe or USB) some PCIe-related properties must be supplied: clocks; resets; and a syscon phandle.

---
  .../bindings/phy/spacemit,k1-combo-phy.yaml   | 110 ++++++++++++++++++
  1 file changed, 110 insertions(+)

create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +7 -0
new file mode 100644
index 0000000000000..ed78083a53231
@@ -0,0 +1,110 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/phy/spacemit,k1-combo-phy.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCIe/USB3 Combo PHY
Quoted 8 lines

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 12

Quoted 5 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +5 -0
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:

You need a '>' or the paragraphs formatting will not be maintained (should we ever render docs from this).

OK.

Quoted 23 lines
Quoted 23 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +23 -0
+  Of the three PHYs on the SpacemiT K1 SoC capable of being used for
+  PCIe, one is a combo PHY that can also be configured for use by a
+  USB 3 controller.  Using PCIe or USB 3 is a board design decision.
+
+  The combo PHY is also the only PCIe PHY that is able to determine
+  PCIe calibration values to use, and this must be determined before
+  the other two PCIe PHYs can be used.  This calibration must be
+  performed with the combo PHY in PCIe mode, and is this is done
+  when the combo PHY is probed.
+
+  During normal operation, the PCIe or USB port driver is responsible
+  for ensuring all clocks needed by a PHY are enabled, and all resets
+  affecting the PHY are deasserted.  However, for the combo PHY to
+  perform calibration independent of whether it's later used for
+  PCIe or USB, all PCIe mode clocks and resets must be defined.
+
+properties:
+  compatible:
+    const: spacemit,k1-combo-phy
+
+  reg:
+    items:
+      - description: PHY control registers
Quoted 8 lines

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 41

Quoted 6 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +6 -0
+
+  clocks:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) clock
+      - description: DWC PCIe application AXI-bus Master interface clock
+      - description: DWC PCIe application AXI-bus Slave interface clock.

End with a period or don't. Just be consistent.

OK.

Quoted 2 lines

You need DWC PCIe clocks for your PHY? A ref clock would make sense, but these? I've never seen a PHY with a AXI master interface.

*This* is what I was waiting for. I explained it briefly in the patch headers and elsewhere but I expected questions.

This is needed to support USB mode, while also supporting the other PCIe interfaces.

The SpacemiT IP requires its PCIe interfaces to have 4-bit RX and TX receiver termination values be configured during initialization. The values to use must be determined dynamically by doing a calibration step, then reading a (PCIe) register that contains the values to use.

Only the combo PHY is able to perform this calibration. and the configuration values it determines must then be used to configure the other two PCIe (only) PHYs.

This means that to calibrate, the combo PHY must be started (clocks enabled, resets de-asserted) in PCIe mode.

If the combo PHY were going to be used for PCIe, this could be done when it is initialized, because the PCIe driver would ensure the clocks and resets were set up properly.

But if the combo PHY is going to be used for USB, the PCIe calibration step would (otherwise) never be done, and that means the other two PCIe interfaces could not be used.

So my solution is to move this calibration step into the PHY. The combo PHY performs the calibration step when it is probed, and to do that the driver needs to use its PCIe clocks and resets. Once the calibration values are known, the clocks and resets are essentially forgotten, and the PHY is available for use (by either PCIe or USB 3).

The other two PCIe interfaces cannot probe (-EPROBE_DEFER) until the calibration values are known, which means they might have to wait until after the combo PHY has probed successfully.

I asked SpacemiT about this a lot, but their answer is that the combo PHY is the only one that can determine these values, and they must be determined each time the hardware is powered up.

I think this approach is less ugly than some alternatives.

Is this clear? Can you think of a different way of handling it?

Quoted 6 lines
Quoted 6 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +6 -0
+
+  clock-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
Quoted 8 lines

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 53

Quoted 6 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +6 -0
+
+  resets:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) reset
+      - description: DWC PCIe application AXI-bus Master interface reset
+      - description: DWC PCIe application AXI-bus Slave interface reset.

Same here (on both points).

I will remove the period on the third one.

Quoted 32 lines
Quoted 32 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +31 -0
+      - description: Global reset; must be deasserted for PHY to function
+
+  reset-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+      - const: global
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in setting the mode
+    $ref: /schemas/types.yaml#/definitions/phandle
+
+  "#phy-cells":
+    const: 1
+    description:
+      The argument value (PHY_TYPE_PCIE or PHY_TYPE_USB3) determines
+      whether the PHY operates in PCIe or USB3 mode.
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#phy-cells"
+
Quoted 8 lines

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 90

Quoted 6 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +6 -0
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    combo_phy: phy@c0b10000 {

Drop unused labels.

OK.

Quoted 11 lines
Quoted 11 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +11 -0
+        compatible = "spacemit,k1-combo-phy";
+        reg = <0xc0b10000 0x1000>;
+        clocks = <&syscon_apmu CLK_PCIE0_DBI>,
+                 <&syscon_apmu CLK_PCIE0_MASTER>,
+                 <&syscon_apmu CLK_PCIE0_SLAVE>;
+        clock-names = "dbi",
+                      "mstr",
+                      "slv";
+        resets = <&syscon_apmu RESET_PCIE0_DBI>,
+                 <&syscon_apmu RESET_PCIE0_MASTER>,
+                 <&syscon_apmu RESET_PCIE0_SLAVE>,
Quoted 8 lines

Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml line 109

Quoted 8 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +8 -0
+                 <&syscon_apmu RESET_PCIE0_GLOBAL>;
+        reset-names = "dbi",
+                      "mstr",
+                      "slv",
+                      "global";
+        spacemit,syscon-pmu = <&syscon_apmu>;
+        #phy-cells = <1>;
+        status = "disabled";

Krzysztof also pointed out that I can't disable it so I'll drop the above line as well.

Thanks for the review.

-Alex

Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/phy/spacemit,k1-combo-phy.yaml new file +1 -0
+    };
Signature · 1 line
2.48.1
author
#8 lore raw

PATCH 2/6 dt-bindings: phy: spacemit: introduce PCIe PHY

In reply to Alex Elder

Add the Device Tree binding for two PCIe PHYs present on the SpacemiT K1 SoC. These PHYs are dependent on a separate combo PHY, which determines at probe time the calibration values used by the PCIe-only PHYs.

---
 .../bindings/phy/spacemit,k1-pcie-phy.yaml    | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k1-pcie-phy.yaml
Documentation/devicetree/bindings/phy/spacemit,k1-pcie-phy.yaml new file +49 -0
new file mode 100644
index 0000000000000..b0cbd231d9378
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/phy/spacemit,k1-pcie-phy.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCIe PHY
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:
+  There are two PHYs on the SpacemiT K1 SoC used for PCIe (only).
+  These PHYs must be configured using calibration values that are
+  determined by a third "combo PHY".  The combo PHY determines
+  these calibration values during probe so they can be used for
+  the two PCIe-only PHYs.
+
+  During normal operation, the PCIe port driver is responsible for
+  ensuring all clocks needed by a PHY are enabled, and all resets
+  affecting the PHY are deasserted.
+
+properties:
+  compatible:
+    const: spacemit,k1-pcie-phy
+
+  reg:
+    items:
+      - description: PHY control registers
+
+  "#phy-cells":
+    const: 0
+
+required:
+  - compatible
+  - reg
+  - "#phy-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    pcie1_phy: phy@c0c10000 {
+        compatible = "spacemit,k1-pcie-phy";
+        reg = <0xc0c10000 0x1000>;
+        #phy-cells = <0>;
+        status = "disabled";
+    };
Signature · 1 line
2.48.1
#9 lore raw

Re: [PATCH 2/6] dt-bindings: phy: spacemit: introduce PCIe PHY

Quoted 6 lines · Alex Elder
Documentation/devicetree/bindings/phy/spacemit,k1-pcie-phy.yaml new file +6 -0
+
+required:
+  - compatible
+  - reg
+  - "#phy-cells"
+

Documentation/devicetree/bindings/phy/spacemit,k1-pcie-phy.yaml line 48

Quoted 10 lines
Documentation/devicetree/bindings/phy/spacemit,k1-pcie-phy.yaml new file +10 -0
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    pcie1_phy: phy@c0c10000 {
+        compatible = "spacemit,k1-pcie-phy";
+        reg = <0xc0c10000 0x1000>;
+        #phy-cells = <0>;
+        status = "disabled";

Same problem here.

Best regards,
Krzysztof

author
#10 lore raw

PATCH 3/6 dt-bindings: phy: spacemit: introduce PCIe root complex

In reply to Alex Elder

Add the Device Tree binding for the PCIe root complex found on the SpacemiT K1 SoC. This device is derived from the Synopsys Designware PCIe IP. It supports up to three PCIe ports operating at PCIe gen 2 link speeds (5 GT/sec). One of the ports uses a combo PHY, which is typically used to support a USB 3 port.

---
 .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +141 -0
new file mode 100644
index 0000000000000..6bcca2f91a6fd
@@ -0,0 +1,141 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pci/spacemit,k1-pcie-rc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCI Express Root Complex
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:
+  The SpacemiT K1 SoC PCIe root complex controller is based on the
+  Synopsys DesignWare PCIe IP.
+
+properties:
+  compatible:
+    const: spacemit,k1-pcie-rc.yaml
+
+  reg:
+    items:
+      - description: DesignWare PCIe registers
+      - description: ATU address space
+      - description: PCIe configuration space
+      - description: Link control registers
+
+  reg-names:
+    items:
+      - const: dbi
+      - const: atu
+      - const: config
+      - const: link
+
+  clocks:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) clock
+      - description: DWC PCIe application AXI-bus Master interface clock
+      - description: DWC PCIe application AXI-bus Slave interface clock.
+
+  clock-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+
+  resets:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) reset
+      - description: DWC PCIe application AXI-bus Master interface reset
+      - description: DWC PCIe application AXI-bus Slave interface reset.
+      - description: Global reset; must be deasserted for PHY to function
+
+  reset-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+      - const: global
+
+  interrupts-extended:
+    maxItems: 1
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in managing resets and link state.
+    $ref: /schemas/types.yaml#/definitions/phandle
+
+  device_type:
+    const: pci
+
+  max-link-speed:
+    const: 2
+
+  num-viewport:
+    const: 8
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#address-cells"
+  - "#size-cells"
+  - device_type
+  - max-link-speed
+  - bus-range
+  - num-viewport
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    pcie0: pcie@ca000000 {
+        compatible = "spacemit,k1-pcie-rc";
+        reg = <0x0 0xca000000 0x0 0x00001000>,
+              <0x0 0xca300000 0x0 0x0001ff24>,
+              <0x0 0x8f000000 0x0 0x00002000>,
+              <0x0 0xc0b20000 0x0 0x00001000>;
+        reg-names = "dbi",
+                    "atu",
+                    "config",
+                    "link";
+
+        ranges = <0x01000000 0x8f002000 0x0 0x8f002000 0x0 0x100000>,
+                 <0x02000000 0x80000000 0x0 0x80000000 0x0 0x0f000000>;
+
+        clocks = <&syscon_apmu CLK_PCIE0_DBI>,
+                 <&syscon_apmu CLK_PCIE0_MASTER>,
+                 <&syscon_apmu CLK_PCIE0_SLAVE>;
+        clock-names = "dbi",
+                      "mstr",
+                      "slv";
+
+        resets = <&syscon_apmu RESET_PCIE0_DBI>,
+                 <&syscon_apmu RESET_PCIE0_MASTER>,
+                 <&syscon_apmu RESET_PCIE0_SLAVE>,
+                 <&syscon_apmu RESET_PCIE0_GLOBAL>;
+        reset-names = "dbi",
+                      "mstr",
+                      "slv",
+                      "global";
+
+        interrupts-extended = <&plic 141>;
+
+        spacemit,syscon-pmu = <&syscon_apmu 0x03cc>;
+
+        #address-cells = <3>;
+        #size-cells = <2>;
+
+        device_type = "pci";
+        max-link-speed = <2>;
+        bus-range = <0x00 0xff>;
+        num-viewport = <8>;
+
+        status = "disabled";
+    };
Signature · 1 line
2.48.1
#11 lore raw

Re: [PATCH 3/6] dt-bindings: phy: spacemit: introduce PCIe root complex

Quoted 6 lines · Alex Elder

Add the Device Tree binding for the PCIe root complex found on the SpacemiT K1 SoC. This device is derived from the Synopsys Designware PCIe IP. It supports up to three PCIe ports operating at PCIe gen 2 link speeds (5 GT/sec). One of the ports uses a combo PHY, which is typically used to support a USB 3 port.

Quoted 6 lines
---
 .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.example.dtb: /example-0/pcie@ca000000: failed to match any schema with compatible: ['spacemit,k1-pcie-rc']

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

author
#12 lore raw

Re: [PATCH 3/6] dt-bindings: phy: spacemit: introduce PCIe root complex

Quoted 8 lines · Rob Herring (Arm)
Quoted 6 lines · Alex Elder

Add the Device Tree binding for the PCIe root complex found on the SpacemiT K1 SoC. This device is derived from the Synopsys Designware PCIe IP. It supports up to three PCIe ports operating at PCIe gen 2 link speeds (5 GT/sec). One of the ports uses a combo PHY, which is typically used to support a USB 3 port.

Quoted 8 lines
Quoted 6 lines
---
  .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++++++++++++++++
  1 file changed, 141 insertions(+)

create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml

My bot found errors running 'make dt_binding_check' on your patch:

Mine didn't for some reason, so I must be doing something wrong.

Simple inspection shows my compatible string contains ".yaml"!

I'll fix in a new version. Sorry I missed this.

-Alex

Quoted 22 lines

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.example.dtb: /example-0/pcie@ca000000: failed to match any schema with compatible: ['spacemit,k1-pcie-rc']

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#13 lore raw

Re: [PATCH 3/6] dt-bindings: phy: spacemit: introduce PCIe root complex

In reply to Alex Elder

On Wed, Aug 13, 2025 at 01:46:57PM GMT, Alex Elder wrote:

Subject should have 'pci' prefix, not 'phy'.

Quoted 69 lines

Add the Device Tree binding for the PCIe root complex found on the SpacemiT K1 SoC. This device is derived from the Synopsys Designware PCIe IP. It supports up to three PCIe ports operating at PCIe gen 2 link speeds (5 GT/sec). One of the ports uses a combo PHY, which is typically used to support a USB 3 port.

---
 .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +51 -0
new file mode 100644
index 0000000000000..6bcca2f91a6fd
@@ -0,0 +1,141 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pci/spacemit,k1-pcie-rc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCI Express Root Complex
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:
+  The SpacemiT K1 SoC PCIe root complex controller is based on the
+  Synopsys DesignWare PCIe IP.
+
+properties:
+  compatible:
+    const: spacemit,k1-pcie-rc.yaml
+
+  reg:
+    items:
+      - description: DesignWare PCIe registers
+      - description: ATU address space
+      - description: PCIe configuration space
+      - description: Link control registers
+
+  reg-names:
+    items:
+      - const: dbi
+      - const: atu
+      - const: config
+      - const: link
+
+  clocks:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) clock
+      - description: DWC PCIe application AXI-bus Master interface clock
+      - description: DWC PCIe application AXI-bus Slave interface clock.
+
+  clock-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+
+  resets:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) reset
+      - description: DWC PCIe application AXI-bus Master interface reset
+      - description: DWC PCIe application AXI-bus Slave interface reset.
+      - description: Global reset; must be deasserted for PHY to function

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 61

Quoted 10 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +10 -0
+
+  reset-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+      - const: global
+
+  interrupts-extended:
+    maxItems: 1

What is the purpose of this property? Is it for MSI or INTx?

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 73

Quoted 12 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +12 -0
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in managing resets and link state.
+    $ref: /schemas/types.yaml#/definitions/phandle
+
+  device_type:
+    const: pci
+
+  max-link-speed:
+    const: 2

Why do you need to limit it to 5 GT/s always?

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+
+  num-viewport:
+    const: 8
+
+required:
+  - compatible

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 89

Quoted 10 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +10 -0
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#address-cells"
+  - "#size-cells"
+  - device_type
+  - max-link-speed

Same comment as above.

Quoted 10 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +10 -0
+  - bus-range
+  - num-viewport
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    pcie0: pcie@ca000000 {
+        compatible = "spacemit,k1-pcie-rc";

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 109

Quoted 10 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +10 -0
+        reg = <0x0 0xca000000 0x0 0x00001000>,
+              <0x0 0xca300000 0x0 0x0001ff24>,
+              <0x0 0x8f000000 0x0 0x00002000>,
+              <0x0 0xc0b20000 0x0 0x00001000>;
+        reg-names = "dbi",
+                    "atu",
+                    "config",
+                    "link";
+
+        ranges = <0x01000000 0x8f002000 0x0 0x8f002000 0x0 0x100000>,

I/O port CPU address starts from 0.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
author
#14 lore raw

Re: [PATCH 3/6] dt-bindings: phy: spacemit: introduce PCIe root complex

Quoted 3 lines · Manivannan Sadhasivam

On Wed, Aug 13, 2025 at 01:46:57PM GMT, Alex Elder wrote:

Subject should have 'pci' prefix, not 'phy'.

OK I'll update that in the next version.

Quoted 73 lines
Quoted 73 lines

Add the Device Tree binding for the PCIe root complex found on the SpacemiT K1 SoC. This device is derived from the Synopsys Designware PCIe IP. It supports up to three PCIe ports operating at PCIe gen 2 link speeds (5 GT/sec). One of the ports uses a combo PHY, which is typically used to support a USB 3 port.

---
  .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++++++++++++++++
  1 file changed, 141 insertions(+)

create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +55 -0
new file mode 100644
index 0000000000000..6bcca2f91a6fd
@@ -0,0 +1,141 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pci/spacemit,k1-pcie-rc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCI Express Root Complex
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:
+  The SpacemiT K1 SoC PCIe root complex controller is based on the
+  Synopsys DesignWare PCIe IP.
+
+properties:
+  compatible:
+    const: spacemit,k1-pcie-rc.yaml
+
+  reg:
+    items:
+      - description: DesignWare PCIe registers
+      - description: ATU address space
+      - description: PCIe configuration space
+      - description: Link control registers
+
+  reg-names:
+    items:
+      - const: dbi
+      - const: atu
+      - const: config
+      - const: link
+
+  clocks:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) clock
+      - description: DWC PCIe application AXI-bus Master interface clock
+      - description: DWC PCIe application AXI-bus Slave interface clock.
+
+  clock-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+
+  resets:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) reset
+      - description: DWC PCIe application AXI-bus Master interface reset
+      - description: DWC PCIe application AXI-bus Slave interface reset.
+      - description: Global reset; must be deasserted for PHY to function
+
+  reset-names:
+    items:
+      - const: dbi
Quoted 8 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 61

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+      - const: mstr
+      - const: slv
+      - const: global
+
+  interrupts-extended:
+    maxItems: 1

What is the purpose of this property? Is it for MSI or INTx?

It is for MSIs, which are translated into this interrupt. I'll add a short description indicating this.

Is there a better way to represent this?

Quoted 6 lines
Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in managing resets and link state.
+    $ref: /schemas/types.yaml#/definitions/phandle
Quoted 8 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 73

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+
+  device_type:
+    const: pci
+
+  max-link-speed:
+    const: 2

Why do you need to limit it to 5 GT/s always?

It's what the hardware overview says is the speed
of the ports.
PCIE PortA Gen2x1
PCIE PortB Gen2x2
PCIE PortC Gen2x2

But I think what you're asking might be "why do you need to specify in DT that the link speed is limited". And in that case, I realize now that it is not needed. I will specify dw_pcie->max_link_speed to 2 before calling dw_pcie_host_init().

If that's not what you meant, please let me know.

Quoted 32 lines
Quoted 5 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +5 -0
+  num-viewport:
+    const: 8
+
+required:
+  - compatible

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 89

Quoted 10 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +10 -0
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#address-cells"
+  - "#size-cells"
+  - device_type
+  - max-link-speed

Same comment as above.

Quoted 14 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +14 -0
+  - bus-range
+  - num-viewport
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    pcie0: pcie@ca000000 {
+        compatible = "spacemit,k1-pcie-rc";
+        reg = <0x0 0xca000000 0x0 0x00001000>,
+              <0x0 0xca300000 0x0 0x0001ff24>,
+              <0x0 0x8f000000 0x0 0x00002000>,
+              <0x0 0xc0b20000 0x0 0x00001000>;
Quoted 8 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 109

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+        reg-names = "dbi",
+                    "atu",
+                    "config",
+                    "link";
+
+        ranges = <0x01000000 0x8f002000 0x0 0x8f002000 0x0 0x100000>,

I/O port CPU address starts from 0.

First, I'm not sure what this comment means.

But second, this ranges value (which I'm sure I just copied from the working DTS file) somehow got munged.

I know there were other errors in this YAML file that I have to fix, and I'll fix this too.

Thank you Mani.

-Alex

Quoted 3 lines

- Mani

#15 lore raw

Re: [PATCH 3/6] dt-bindings: phy: spacemit: introduce PCIe root complex

Quoted 89 lines · Alex Elder
Quoted 3 lines · Manivannan Sadhasivam

On Wed, Aug 13, 2025 at 01:46:57PM GMT, Alex Elder wrote:

Subject should have 'pci' prefix, not 'phy'.

OK I'll update that in the next version.

Quoted 73 lines
Quoted 73 lines

Add the Device Tree binding for the PCIe root complex found on the SpacemiT K1 SoC. This device is derived from the Synopsys Designware PCIe IP. It supports up to three PCIe ports operating at PCIe gen 2 link speeds (5 GT/sec). One of the ports uses a combo PHY, which is typically used to support a USB 3 port.

---
  .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++++++++++++++++
  1 file changed, 141 insertions(+)

create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +55 -0
new file mode 100644
index 0000000000000..6bcca2f91a6fd
@@ -0,0 +1,141 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pci/spacemit,k1-pcie-rc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 PCI Express Root Complex
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:
+  The SpacemiT K1 SoC PCIe root complex controller is based on the
+  Synopsys DesignWare PCIe IP.
+
+properties:
+  compatible:
+    const: spacemit,k1-pcie-rc.yaml
+
+  reg:
+    items:
+      - description: DesignWare PCIe registers
+      - description: ATU address space
+      - description: PCIe configuration space
+      - description: Link control registers
+
+  reg-names:
+    items:
+      - const: dbi
+      - const: atu
+      - const: config
+      - const: link
+
+  clocks:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) clock
+      - description: DWC PCIe application AXI-bus Master interface clock
+      - description: DWC PCIe application AXI-bus Slave interface clock.
+
+  clock-names:
+    items:
+      - const: dbi
+      - const: mstr
+      - const: slv
+
+  resets:
+    items:
+      - description: DWC PCIe Data Bus Interface (DBI) reset
+      - description: DWC PCIe application AXI-bus Master interface reset
+      - description: DWC PCIe application AXI-bus Slave interface reset.
+      - description: Global reset; must be deasserted for PHY to function
+
+  reset-names:
+    items:
+      - const: dbi
Quoted 8 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 61

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+      - const: mstr
+      - const: slv
+      - const: global
+
+  interrupts-extended:
+    maxItems: 1

What is the purpose of this property? Is it for MSI or INTx?

Quoted 5 lines

It is for MSIs, which are translated into this interrupt. I'll add a short description indicating this.

Is there a better way to represent this?

For external MSI controllers, it is recommended to use 'msi-map' property as the client often need to pass RID, RID length and other sideband data to the MSI controller.

Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in managing resets and link state.
+    $ref: /schemas/types.yaml#/definitions/phandle
Quoted 8 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 73

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+
+  device_type:
+    const: pci
+
+  max-link-speed:
+    const: 2

Why do you need to limit it to 5 GT/s always?

It's what the hardware overview says is the speed of the ports.
PCIE PortA Gen2x1
PCIE PortB Gen2x2
PCIE PortC Gen2x2

Quoted 6 lines

But I think what you're asking might be "why do you need to specify in DT that the link speed is limited". And in that case, I realize now that it is not needed. I will specify dw_pcie->max_link_speed to 2 before calling dw_pcie_host_init().

You only need to specify this property if you want to limit the link speed to a certain data rate by the controller driver than what is supported by the hardware.

Looks like your hardware supports 5 GT/s by default for all ports. So you can omit this property and also no need to set 'dw_pcie->max_link_speed'. In the absence of this property, 'dw_pcie->max_link_speed' will be populated with the hardware default value, which will be 2 in your case.

Quoted 43 lines

If that's not what you meant, please let me know.

Quoted 32 lines
Quoted 5 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +5 -0
+  num-viewport:
+    const: 8
+
+required:
+  - compatible

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 89

Quoted 10 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +10 -0
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#address-cells"
+  - "#size-cells"
+  - device_type
+  - max-link-speed

Same comment as above.

Quoted 14 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +14 -0
+  - bus-range
+  - num-viewport
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    pcie0: pcie@ca000000 {
+        compatible = "spacemit,k1-pcie-rc";
+        reg = <0x0 0xca000000 0x0 0x00001000>,
+              <0x0 0xca300000 0x0 0x0001ff24>,
+              <0x0 0x8f000000 0x0 0x00002000>,
+              <0x0 0xc0b20000 0x0 0x00001000>;
Quoted 8 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 109

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+        reg-names = "dbi",
+                    "atu",
+                    "config",
+                    "link";
+
+        ranges = <0x01000000 0x8f002000 0x0 0x8f002000 0x0 0x100000>,

I/O port CPU address starts from 0.

Quoted 2 lines

First, I'm not sure what this comment means.

Sorry, I meant to say 'PCI address' not 'CPU address'. The second element in your 'ranges' example corresponds to the PCI start address of the I/O port and it should always start from 0. Also, the encoding for both tuples are wrong. It should be:

	ranges = <0x01000000 0x0 0x00000000 0x8f002000 0x0 0x100000>,
		 <0x02000000 0x0 0x80000000 0x80000000 0x0 0x0f000000>; 

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
author
#16 lore raw

Re: [PATCH 3/6] dt-bindings: phy: spacemit: introduce PCIe root complex

Quoted 7 lines · Manivannan Sadhasivam
Quoted 6 lines · Alex Elder
Quoted 3 lines · Manivannan Sadhasivam

On Wed, Aug 13, 2025 at 01:46:57PM GMT, Alex Elder wrote:

Subject should have 'pci' prefix, not 'phy'.

OK I'll update that in the next version.

I have a new version of this code ready, but I'll delay sending it until the end of the merge window.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Add the Device Tree binding for the PCIe root complex found on the SpacemiT K1 SoC. This device is derived from the Synopsys Designware PCIe IP. It supports up to three PCIe ports operating at PCIe gen 2 link speeds (5 GT/sec). One of the ports uses a combo PHY, which is typically used to support a USB 3 port.

---
   .../bindings/pci/spacemit,k1-pcie-rc.yaml     | 141 ++++++++++++++++++
   1 file changed, 141 insertions(+)

create mode 100644 Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +0 -0
new file mode 100644
index 0000000000000..6bcca2f91a6fd
@@ -0,0 +1,141 @@

. . .

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 61

Quoted 2 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +2 -0
+  interrupts-extended:
+    maxItems: 1

What is the purpose of this property? Is it for MSI or INTx?

It is for MSIs, which are translated into this interrupt. I'll add a short description indicating this.

Quoted 6 lines
Quoted 2 lines

Is there a better way to represent this?

For external MSI controllers, it is recommended to use 'msi-map' property as the client often need to pass RID, RID length and other sideband data to the MSI controller.

This implementation is using the DWC built-in MSI controller, not an external controller. It implements 256 MSIs, and they're handled by dw_handle_msi_irq(). The interrupt will be given name "msi" in the next version and I hope that at least helps clarify things.

Quoted 29 lines
Quoted 22 lines
Quoted 15 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 73

Quoted 13 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +12 -0
+
+  spacemit,syscon-pmu:
+    description:
+      PHandle that refers to the APMU system controller, whose
+      regmap is used in managing resets and link state.
+    $ref: /schemas/types.yaml#/definitions/phandle
+
+  device_type:
+    const: pci
+
+  max-link-speed:
+    const: 2

Why do you need to limit it to 5 GT/s always?

It's what the hardware overview says is the speed of the ports.
PCIE PortA Gen2x1
PCIE PortB Gen2x2
PCIE PortC Gen2x2

Quoted 6 lines

But I think what you're asking might be "why do you need to specify in DT that the link speed is limited". And in that case, I realize now that it is not needed. I will specify dw_pcie->max_link_speed to 2 before calling dw_pcie_host_init().

Quoted 3 lines

You only need to specify this property if you want to limit the link speed to a certain data rate by the controller driver than what is supported by the hardware.

Yes, this makes sense.

Quoted 4 lines

Looks like your hardware supports 5 GT/s by default for all ports. So you can omit this property and also no need to set 'dw_pcie->max_link_speed'. In the absence of this property, 'dw_pcie->max_link_speed' will be populated with the hardware default value, which will be 2 in your case.

You are correct, and I verified this. I won't specify the link speed "manually," and the hardware default (which is 2) will be used.

Quoted 46 lines
Quoted 43 lines

If that's not what you meant, please let me know.

Quoted 32 lines
Quoted 5 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +5 -0
+  num-viewport:
+    const: 8
+
+required:
+  - compatible

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 89

Quoted 10 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +10 -0
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - spacemit,syscon-pmu
+  - "#address-cells"
+  - "#size-cells"
+  - device_type
+  - max-link-speed

Same comment as above.

Quoted 14 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +14 -0
+  - bus-range
+  - num-viewport
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    pcie0: pcie@ca000000 {
+        compatible = "spacemit,k1-pcie-rc";
+        reg = <0x0 0xca000000 0x0 0x00001000>,
+              <0x0 0xca300000 0x0 0x0001ff24>,
+              <0x0 0x8f000000 0x0 0x00002000>,
+              <0x0 0xc0b20000 0x0 0x00001000>;
Quoted 8 lines

Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml line 109

Quoted 6 lines
Documentation/devicetree/bindings/pci/spacemit,k1-pcie-rc.yaml new file +6 -0
+        reg-names = "dbi",
+                    "atu",
+                    "config",
+                    "link";
+
+        ranges = <0x01000000 0x8f002000 0x0 0x8f002000 0x0 0x100000>,

I/O port CPU address starts from 0.

Quoted 2 lines

First, I'm not sure what this comment means.

Quoted 7 lines

Sorry, I meant to say 'PCI address' not 'CPU address'. The second element in your 'ranges' example corresponds to the PCI start address of the I/O port and it should always start from 0. Also, the encoding for both tuples are wrong. It should be:

	ranges = <0x01000000 0x0 0x00000000 0x8f002000 0x0 0x100000>,
		 <0x02000000 0x0 0x80000000 0x80000000 0x0 0x0f000000>;

Simply switching to what you show works just fine. PCI addresses show up being zero-based, as they should be. What I don't fully understand is why it worked before. Maybe because this is simply defining bounds in the mapping?

In any case, I am using ranges properties like what you show.

Thanks Mani.

-Alex

Quoted 1 line

- Mani

author
#17 lore raw

PATCH 4/6 phy: spacemit: introduce PCIe/combo PHY

In reply to Alex Elder

Introduce a driver that supports three PHYs found on the SpacemiT K1 SoC. The first PHY is a combo PHY that can be configured for use for either USB 3 or PCIe. The other two PHYs support PCIe only.

All three PHYs must be programmed with an 8 bit receiver termination value, which must be determined dynamically; only the combo PHY is able to determine this value. The combo PHY performs a special calibration step at probe time to discover this, and that value is used to program each PHY that operates in PCIe mode. The combo PHY must therefore be probed--first--if either of the PCIe-only PHYs will be used.

During normal operation, the USB or PCIe driver using the PHY must ensure clocks and resets are set up properly. However clocks are enabled and resets are de-asserted temporarily by this driver to perform the calibration step on the combo PHY.

---
 drivers/phy/Kconfig                |  11 +
 drivers/phy/Makefile               |   1 +
 drivers/phy/phy-spacemit-k1-pcie.c | 639 +++++++++++++++++++++++++++++
 3 files changed, 651 insertions(+)
 create mode 100644 drivers/phy/phy-spacemit-k1-pcie.c
drivers/phy/Kconfig +11 -0
index 58c911e1b2d20..0fa343203f289 100644
@@ -101,6 +101,17 @@ config PHY_NXP_PTN3222
 	  schemes. It supports all three USB 2.0 data rates: Low Speed, Full
 	  Speed and High Speed.
 
+config PHY_SPACEMIT_K1_PCIE
+	tristate "PCIe and combo PHY driver for the SpacemiT K1 SoC"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on HAS_IOMEM
+	depends on OF
+	select GENERIC_PHY
+	default ARCH_SPACEMIT
+	help
+	  Enable support for the PCIe and USB 3 combo PHY and two
+	  PCIe-only PHYs used in the SpacemiT K1 SoC.
+
 source "drivers/phy/allwinner/Kconfig"
 source "drivers/phy/amlogic/Kconfig"
 source "drivers/phy/broadcom/Kconfig"
drivers/phy/Makefile +1 -0
index c670a8dac4680..20f0078e543c7 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_PHY_SNPS_EUSB2)		+= phy-snps-eusb2.o
 obj-$(CONFIG_USB_LGM_PHY)		+= phy-lgm-usb.o
 obj-$(CONFIG_PHY_AIROHA_PCIE)		+= phy-airoha-pcie.o
 obj-$(CONFIG_PHY_NXP_PTN3222)		+= phy-nxp-ptn3222.o
+obj-$(CONFIG_PHY_SPACEMIT_K1_PCIE)	+= phy-spacemit-k1-pcie.o
 obj-y					+= allwinner/	\
 					   amlogic/	\
 					   broadcom/	\
drivers/phy/phy-spacemit-k1-pcie.c new file +639 -0

639 lines not on this page · raw message

Signature · 1 line
2.48.1
#18 lore raw

Re: [PATCH 4/6] phy: spacemit: introduce PCIe/combo PHY

Quoted 328 lines · Alex Elder

Introduce a driver that supports three PHYs found on the SpacemiT K1 SoC. The first PHY is a combo PHY that can be configured for use for either USB 3 or PCIe. The other two PHYs support PCIe only.

All three PHYs must be programmed with an 8 bit receiver termination value, which must be determined dynamically; only the combo PHY is able to determine this value. The combo PHY performs a special calibration step at probe time to discover this, and that value is used to program each PHY that operates in PCIe mode. The combo PHY must therefore be probed--first--if either of the PCIe-only PHYs will be used.

During normal operation, the USB or PCIe driver using the PHY must ensure clocks and resets are set up properly. However clocks are enabled and resets are de-asserted temporarily by this driver to perform the calibration step on the combo PHY.

---
 drivers/phy/Kconfig                |  11 +
 drivers/phy/Makefile               |   1 +
 drivers/phy/phy-spacemit-k1-pcie.c | 639 +++++++++++++++++++++++++++++
 3 files changed, 651 insertions(+)
 create mode 100644 drivers/phy/phy-spacemit-k1-pcie.c
drivers/phy/Kconfig +11 -0
index 58c911e1b2d20..0fa343203f289 100644
@@ -101,6 +101,17 @@ config PHY_NXP_PTN3222
 	  schemes. It supports all three USB 2.0 data rates: Low Speed, Full
 	  Speed and High Speed.
 
+config PHY_SPACEMIT_K1_PCIE
+	tristate "PCIe and combo PHY driver for the SpacemiT K1 SoC"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on HAS_IOMEM
+	depends on OF
+	select GENERIC_PHY
+	default ARCH_SPACEMIT
+	help
+	  Enable support for the PCIe and USB 3 combo PHY and two
+	  PCIe-only PHYs used in the SpacemiT K1 SoC.
+
 source "drivers/phy/allwinner/Kconfig"
 source "drivers/phy/amlogic/Kconfig"
 source "drivers/phy/broadcom/Kconfig"
drivers/phy/Makefile +1 -0
index c670a8dac4680..20f0078e543c7 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_PHY_SNPS_EUSB2)		+= phy-snps-eusb2.o
 obj-$(CONFIG_USB_LGM_PHY)		+= phy-lgm-usb.o
 obj-$(CONFIG_PHY_AIROHA_PCIE)		+= phy-airoha-pcie.o
 obj-$(CONFIG_PHY_NXP_PTN3222)		+= phy-nxp-ptn3222.o
+obj-$(CONFIG_PHY_SPACEMIT_K1_PCIE)	+= phy-spacemit-k1-pcie.o
 obj-y					+= allwinner/	\
 					   amlogic/	\
 					   broadcom/	\
drivers/phy/phy-spacemit-k1-pcie.c new file +260 -0
new file mode 100644
index 0000000000000..32dce53170fbb
@@ -0,0 +1,639 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SpacemiT K1 PCIe and PCIe/USB 3 combo PHY driver
+ *
+ * Copyright (C) 2025 by RISCstar Solutions Corporation.  All rights reserved.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/iopoll.h>
+#include <linux/kernel.h>
+#include <linux/mfd/syscon.h>
+#include <linux/module.h>
+#include <linux/phy/phy.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+
+#include <dt-bindings/phy/phy.h>
+
+/*
+ * Three PCIe ports are supported in the SpacemiT K1 SoC, and this driver
+ * supports their PHYs.
+ *
+ * The PHY for PCIe port A is different from the PHYs for ports B and C:
+ * - It has one lane, while ports B and C have two
+ * - It is a combo PHY can be used for PCIe or USB 3
+ * - It can automatically calibrate PCIe TX and RX termination settings
+ *
+ * The PHY functionality for PCIe ports B and C is identical:
+ * - They have two PCIe lanes (but can be restricted to 1 via Device Tree)
+ * - They are used for PCIe only
+ * - They are configured using TX and RX values computed for port A
+ *
+ * A given board is designed to use the combo PHY for either PCIe or USB 3.
+ * Whether the combo PHY is configured for PCIe or USB 3 is specified in
+ * Device Tree using a phandle plus an argument.  The argument indicates
+ * the type (either PHY_TYPE_PCIE or PHY_TYPE_USB3).
+ *
+ * Each PHY depends on clocks and resets provided by the controller
+ * hardware (PCIe or USB) it is associated with.  The controller drivers
+ * are required to enable any clocks and de-assert any resets that affect
+ * PHY operation.
+ *
+ * PCIe PHYs must be programmed with RX and TX calibration values.  The
+ * combo PHY is the only one that can determine these values.  They are
+ * determined by temporarily enabling the combo PHY in PCIe mode at probe
+ * time (if necessary).  This calibration only needs to be done once, and
+ * when it has completed the TX and RX values are saved.
+ *
+ * To allow the combo PHY to be enabled for calibration, the resets and
+ * clocks it uses in PCIe mode must be supplied.
+ */
+
+struct k1_pcie_phy {
+	struct device *dev;		/* PHY provider device */
+	struct phy *phy;
+	void __iomem *regs;
+	u32 pcie_lanes;			/* Max unless limited by DT */
+	/* The remaining fields are only used for the combo PHY */
+	u32 type;			/* PHY_TYPE_PCIE or PHY_TYPE_USB3 */
+	struct regmap *pmu;
+};
+
+#define CALIBRATION_TIMEOUT		500000	/* microseconds */
+#define PLL_TIMEOUT			500000	/* microseconds */
+#define POLL_DELAY			500	/* microseconds */
+
+/* Selecting the combo PHY operating mode requires PMU regmap access */
+#define SYSCON_PMU			"spacemit,syscon-pmu"
+
+/* PMU space, for selecting between PCIe and USB3 mode on the combo PHY */
+
+#define PMUA_USB_PHY_CTRL0			0x0110
+#define COMBO_PHY_SEL			BIT(3)	/* 0: PCIe; 1: USB3 */
+
+#define PCIE_CLK_RES_CTRL			0x03cc
+#define PCIE_APP_HOLD_PHY_RST		BIT(30)
+
+/* PHY register space */
+
+/* Offset between lane 0 and lane 1 registers when there are two */
+#define PHY_LANE_OFFSET				0x0400
+
+#define PCIE_PU_ADDR_CLK_CFG			0x0008
+#define PLL_READY			BIT(0)		/* read-only */
+#define CFG_RXCLK_EN			BIT(3)
+#define CFG_TXCLK_EN			BIT(4)
+#define CFG_PCLK_EN			BIT(5)
+#define CFG_PIPE_PCLK_EN		BIT(6)
+#define CFG_INTERNAL_TIMER_ADJ		GENMASK(10, 7)
+#define TIMER_ADJ_USB		0x2
+#define TIMER_ADJ_PCIE		0x6
+#define CFG_SW_PHY_INIT_DONE		BIT(11)	/* We set after PLL config */
+
+#define PCIE_RC_DONE_STATUS			0x0018
+#define CFG_FORCE_RCV_RETRY		BIT(10)
+
+#define PCIE_RC_CAL_REG2			0x0020
+#define RC_CAL_TOGGLE			BIT(22)
+#define CLKSEL				GENMASK(31, 29)
+#define CLKSEL_24M		0x3
+
+#define PCIE_PU_PLL_1				0x0048
+#define REF_100_WSSC			BIT(12)	/* 1: input is 100MHz, SSC */
+#define FREF_SEL			GENMASK(15, 13)
+#define FREF_24M		0x1
+#define SSC_DEP_SEL			GENMASK(19, 16)
+#define SSC_DEP_NONE		0x0
+#define SSC_DEP_5000PPM		0xa
+#define SSC_MODE			GENMASK(21, 20)
+#define SSC_MODE_DOWN_SPREAD	0x3
+#define SSC_OFFSET			GENMASK(23, 22)
+#define SSC_OFFSET_0_PPM	0x0
+
+#define PCIE_PU_PLL_2				0x004c
+#define GEN_REF100			BIT(4)	/* 1: generate 100MHz clk */
+
+#define PCIE_RX_REG1				0x0050
+#define EN_RTERM			BIT(3)
+#define AFE_RTERM_REG			GENMASK(11, 8)
+
+#define PCIE_RX_REG2				0x0054
+#define RX_RTERM_SEL			BIT(5)	/* 0: use AFE_RTERM_REG value */
+
+#define PCIE_LTSSM_DIS_ENTRY			0x005c
+#define CFG_REFCLK_MODE			GENMASK(9, 8)
+#define RFCLK_MODE_DRIVER	0x1
+#define OVRD_REFCLK_MODE		BIT(10)	/* 1: use CFG_RFCLK_MODE */
+
+#define PCIE_TX_REG1				0x0064
+#define TX_RTERM_REG			GENMASK(15, 12)
+#define TX_RTERM_SEL			BIT(25)	/* 1: use TX_RTERM_REG */
+
+#define USB3_TEST_CTRL				0x0068
+
+#define PCIE_RCAL_RESULT			0x0084	/* Port A PHY only */
+#define RTERM_VALUE_RX			GENMASK(3, 0)
+#define RTERM_VALUE_TX			GENMASK(7, 4)
+#define R_TUNE_DONE			BIT(10)
+
+static u32 k1_phy_rterm = ~0;     /* Invalid initial value */
+
+/* Save the RX and TX receiver termination values */
+static void k1_phy_rterm_set(u32 val)
+{
+	k1_phy_rterm = val & (RTERM_VALUE_RX | RTERM_VALUE_TX);
+}
+
+static bool k1_phy_rterm_valid(void)
+{
+	/* Valid if no bits outside those we care about are set */
+	return !(k1_phy_rterm & ~(RTERM_VALUE_RX | RTERM_VALUE_TX));
+}
+
+static u32 k1_phy_rterm_rx(void)
+{
+	return FIELD_GET(RTERM_VALUE_RX, k1_phy_rterm);
+}
+
+static u32 k1_phy_rterm_tx(void)
+{
+	return FIELD_GET(RTERM_VALUE_TX, k1_phy_rterm);
+}
+
+/* Only the combo PHY has a PMU pointer defined */
+static bool k1_phy_port_a(struct k1_pcie_phy *k1_phy)
+{
+	return !!k1_phy->pmu;
+}
+
+/*
+ * Select PCIe or USB 3 mode for the combo PHY.  Return 1 if the bit
+ * was changed, 0 if it was not, or a negative error value otherwise.
+ */
+static int k1_combo_phy_sel(struct k1_pcie_phy *k1_phy, bool usb3)
+{
+	int ret;
+
+	ret = regmap_test_bits(k1_phy->pmu, PMUA_USB_PHY_CTRL0, COMBO_PHY_SEL);
+	if (ret < 0)
+		return ret;
+
+	/* If it's already in the desired state, we're done */
+	if (!!ret == usb3)
+		return 0;
+
+	/* Change the bit */
+	ret = regmap_assign_bits(k1_phy->pmu, PMUA_USB_PHY_CTRL0,
+				 COMBO_PHY_SEL, usb3);
+
+	return ret < 0 ? ret : 1;
+}
+
 
+static void k1_pcie_phy_init_pll(struct k1_pcie_phy *k1_phy,
+				 void __iomem *regs, bool pcie)
+{
+	void __iomem *virt;
+	u32 timer_adj;
+	u32 ssc_dep;
+	u32 val;
+
+	if (pcie) {
+		timer_adj = TIMER_ADJ_PCIE;
+		ssc_dep = SSC_DEP_NONE;
+	} else {
+		timer_adj = TIMER_ADJ_USB;
+		ssc_dep = SSC_DEP_5000PPM;
+	}
+
+	/*
+	 * Disable 100 MHz input reference with spread-spectrum
+	 * clocking and select the 24 MHz clock input frequency
+	 */
+	virt = k1_phy->regs + PCIE_PU_PLL_1;
+	val = readl(virt);
+	val &= ~REF_100_WSSC;
+
+	val &= ~FREF_SEL;
+	val |= FIELD_PREP(FREF_SEL, FREF_24M);
+
+	val &= ~SSC_DEP_SEL;
+	val |= FIELD_PREP(SSC_DEP_SEL, ssc_dep);
+
+	val &= ~SSC_MODE;
+	val |= FIELD_PREP(SSC_MODE, SSC_MODE_DOWN_SPREAD);
+
+	val &= ~SSC_OFFSET;
+	val |= FIELD_PREP(SSC_OFFSET, SSC_OFFSET_0_PPM);
+	writel(val, virt);
+
+	if (pcie) {
+		virt = regs + PCIE_PU_PLL_2;
+		val = readl(virt);
+		val |= GEN_REF100;	/* Enable 100 MHz PLL output clock */
+		writel(val, virt);
+	}
+
+	/* Enable clocks and mark PLL initialization done */
+	virt = regs + PCIE_PU_ADDR_CLK_CFG;
+	val = readl(virt);
+	val |= CFG_RXCLK_EN;
+	val |= CFG_TXCLK_EN;
+	val |= CFG_PCLK_EN;
+	val |= CFG_PIPE_PCLK_EN;
+
+	val &= ~CFG_INTERNAL_TIMER_ADJ;
+	val |= FIELD_PREP(CFG_INTERNAL_TIMER_ADJ, timer_adj);
+
+	val |= CFG_SW_PHY_INIT_DONE;
+	writel(val, virt);
+}
+
+static int k1_pcie_pll_lock(struct k1_pcie_phy *k1_phy, bool pcie)
+{
+	u32 val = pcie ? CFG_FORCE_RCV_RETRY : 0;
+	void __iomem *virt;
+
+	writel(val, k1_phy->regs + PCIE_RC_DONE_STATUS);
+

drivers/phy/phy-spacemit-k1-pcie.c line 270

Quoted 10 lines
drivers/phy/phy-spacemit-k1-pcie.c new file +10 -0
+	/*
+	 * Wait for indication the PHY PLL is locked.  Lanes for ports
+	 * B and C share a PLL, so it's enough to sample just lane 0.
+	 */
+	virt = k1_phy->regs + PCIE_PU_ADDR_CLK_CFG;	/* Lane 0 */
+
+	return readl_poll_timeout(virt, val, val & PLL_READY,
+				  POLL_DELAY, PLL_TIMEOUT);
+}
+

Can we use standard clk_ops and clk_mux to normalize this process?

Regards,
Inochi

author
#19 lore raw

Re: [PATCH 4/6] phy: spacemit: introduce PCIe/combo PHY

Quoted 19 lines · Inochi Amaoto
Quoted 18 lines · Alex Elder

Introduce a driver that supports three PHYs found on the SpacemiT K1 SoC. The first PHY is a combo PHY that can be configured for use for either USB 3 or PCIe. The other two PHYs support PCIe only.

All three PHYs must be programmed with an 8 bit receiver termination value, which must be determined dynamically; only the combo PHY is able to determine this value. The combo PHY performs a special calibration step at probe time to discover this, and that value is used to program each PHY that operates in PCIe mode. The combo PHY must therefore be probed--first--if either of the PCIe-only PHYs will be used.

During normal operation, the USB or PCIe driver using the PHY must ensure clocks and resets are set up properly. However clocks are enabled and resets are de-asserted temporarily by this driver to perform the calibration step on the combo PHY.

Quoted 8 lines
Quoted 8 lines
---
  drivers/phy/Kconfig                |  11 +
  drivers/phy/Makefile               |   1 +
  drivers/phy/phy-spacemit-k1-pcie.c | 639 +++++++++++++++++++++++++++++
  3 files changed, 651 insertions(+)

create mode 100644 drivers/phy/phy-spacemit-k1-pcie.c

. . .

Quoted 4 lines

drivers/phy/Makefile

Quoted 4 lines
drivers/phy/Makefile +0 -0
index c670a8dac4680..20f0078e543c7 100644

. . .

Quoted 11 lines
Quoted 11 lines
drivers/phy/Makefile +11 -0
+static int k1_pcie_pll_lock(struct k1_pcie_phy *k1_phy, bool pcie)
+{
+	u32 val = pcie ? CFG_FORCE_RCV_RETRY : 0;
+	void __iomem *virt;
+
+	writel(val, k1_phy->regs + PCIE_RC_DONE_STATUS);
+
+	/*
+	 * Wait for indication the PHY PLL is locked.  Lanes for ports
+	 * B and C share a PLL, so it's enough to sample just lane 0.
+	 */
Quoted 8 lines

drivers/phy/Makefile

Quoted 6 lines
drivers/phy/Makefile +6 -0
+	virt = k1_phy->regs + PCIE_PU_ADDR_CLK_CFG;	/* Lane 0 */
+
+	return readl_poll_timeout(virt, val, val & PLL_READY,
+				  POLL_DELAY, PLL_TIMEOUT);
+}
+

Can we use standard clk_ops and clk_mux to normalize this process?

I understand you're suggesting that we represent this as a clock.

Can you be more specific about how you suggest I do that?

For example, are you suggesting I create a separate clock driver for this one PLL (in each PCIe register space)?

Or do you mean use clock structures and callbacks within this driver to represent this?

I'm just not sure what you have in mind, and the two options I mention seem a lot more complicated than this one function.

Thanks.

-Alex

Quoted 2 lines

Regards,
Inochi

#20 lore raw

Re: [PATCH 4/6] phy: spacemit: introduce PCIe/combo PHY

Quoted 60 lines · Alex Elder
Quoted 19 lines · Inochi Amaoto
Quoted 18 lines · Alex Elder

Introduce a driver that supports three PHYs found on the SpacemiT K1 SoC. The first PHY is a combo PHY that can be configured for use for either USB 3 or PCIe. The other two PHYs support PCIe only.

All three PHYs must be programmed with an 8 bit receiver termination value, which must be determined dynamically; only the combo PHY is able to determine this value. The combo PHY performs a special calibration step at probe time to discover this, and that value is used to program each PHY that operates in PCIe mode. The combo PHY must therefore be probed--first--if either of the PCIe-only PHYs will be used.

During normal operation, the USB or PCIe driver using the PHY must ensure clocks and resets are set up properly. However clocks are enabled and resets are de-asserted temporarily by this driver to perform the calibration step on the combo PHY.

Quoted 8 lines
Quoted 8 lines
---
  drivers/phy/Kconfig                |  11 +
  drivers/phy/Makefile               |   1 +
  drivers/phy/phy-spacemit-k1-pcie.c | 639 +++++++++++++++++++++++++++++
  3 files changed, 651 insertions(+)

create mode 100644 drivers/phy/phy-spacemit-k1-pcie.c

. . .

Quoted 4 lines

drivers/phy/Makefile

Quoted 4 lines
drivers/phy/Makefile +0 -0
index c670a8dac4680..20f0078e543c7 100644

. . .

Quoted 11 lines
Quoted 11 lines
drivers/phy/Makefile +11 -0
+static int k1_pcie_pll_lock(struct k1_pcie_phy *k1_phy, bool pcie)
+{
+	u32 val = pcie ? CFG_FORCE_RCV_RETRY : 0;
+	void __iomem *virt;
+
+	writel(val, k1_phy->regs + PCIE_RC_DONE_STATUS);
+
+	/*
+	 * Wait for indication the PHY PLL is locked.  Lanes for ports
+	 * B and C share a PLL, so it's enough to sample just lane 0.
+	 */
Quoted 8 lines

drivers/phy/Makefile

Quoted 6 lines
drivers/phy/Makefile +6 -0
+	virt = k1_phy->regs + PCIE_PU_ADDR_CLK_CFG;	/* Lane 0 */
+
+	return readl_poll_timeout(virt, val, val & PLL_READY,
+				  POLL_DELAY, PLL_TIMEOUT);
+}
+

Can we use standard clk_ops and clk_mux to normalize this process?

I understand you're suggesting that we represent this as a clock.

Quoted 8 lines

Can you be more specific about how you suggest I do that?

For example, are you suggesting I create a separate clock driver for this one PLL (in each PCIe register space)?

Or do you mean use clock structures and callbacks within this driver to represent this?

I think using clock structures and registering them is just fine, there are many phy drivers already do this.

Quoted 3 lines

I'm just not sure what you have in mind, and the two options I mention seem a lot more complicated than this one function.

Yes, it is more complex than just adding one function, but it make the code clean and easy to understand as the clock looks complex. Also, register this clock allow us to know the state of internal clock by looking clk_summary.

Regards,
Inochi

#21 lore raw

Re: [PATCH 4/6] phy: spacemit: introduce PCIe/combo PHY

In reply to Alex Elder

Hi Alex,

Quoted 65 lines · Alex Elder
Quoted 19 lines · Inochi Amaoto
Quoted 18 lines · Alex Elder

Introduce a driver that supports three PHYs found on the SpacemiT K1 SoC. The first PHY is a combo PHY that can be configured for use for either USB 3 or PCIe. The other two PHYs support PCIe only.

All three PHYs must be programmed with an 8 bit receiver termination value, which must be determined dynamically; only the combo PHY is able to determine this value. The combo PHY performs a special calibration step at probe time to discover this, and that value is used to program each PHY that operates in PCIe mode. The combo PHY must therefore be probed--first--if either of the PCIe-only PHYs will be used.

During normal operation, the USB or PCIe driver using the PHY must ensure clocks and resets are set up properly. However clocks are enabled and resets are de-asserted temporarily by this driver to perform the calibration step on the combo PHY.

Quoted 8 lines
Quoted 8 lines
---
  drivers/phy/Kconfig                |  11 +
  drivers/phy/Makefile               |   1 +
  drivers/phy/phy-spacemit-k1-pcie.c | 639 +++++++++++++++++++++++++++++
  3 files changed, 651 insertions(+)

create mode 100644 drivers/phy/phy-spacemit-k1-pcie.c

. . .

Quoted 4 lines

drivers/phy/Makefile

Quoted 4 lines
drivers/phy/Makefile +0 -0
index c670a8dac4680..20f0078e543c7 100644

. . .

Quoted 11 lines
Quoted 11 lines
drivers/phy/Makefile +11 -0
+static int k1_pcie_pll_lock(struct k1_pcie_phy *k1_phy, bool pcie)
+{
+	u32 val = pcie ? CFG_FORCE_RCV_RETRY : 0;
+	void __iomem *virt;
+
+	writel(val, k1_phy->regs + PCIE_RC_DONE_STATUS);
+
+	/*
+	 * Wait for indication the PHY PLL is locked.  Lanes for ports
+	 * B and C share a PLL, so it's enough to sample just lane 0.
+	 */
Quoted 8 lines

drivers/phy/Makefile

Quoted 6 lines
drivers/phy/Makefile +6 -0
+	virt = k1_phy->regs + PCIE_PU_ADDR_CLK_CFG;	/* Lane 0 */
+
+	return readl_poll_timeout(virt, val, val & PLL_READY,
+				  POLL_DELAY, PLL_TIMEOUT);
+}
+

Can we use standard clk_ops and clk_mux to normalize this process?

I understand you're suggesting that we represent this as a clock.

Can you be more specific about how you suggest I do that?

For example, are you suggesting I create a separate clock driver for this one PLL (in each PCIe register space)?

Quoted 7 lines

Or do you mean use clock structures and callbacks within this driver to represent this?

I'm just not sure what you have in mind, and the two options I mention seem a lot more complicated than this one function.

Thanks.

you can take a look at k1's i2c patch that Troy just sent which has similar case

https://lore.kernel.org/all/[email protected]/

Quoted 6 lines

-Alex

Quoted 2 lines

Regards,
Inochi

Signature · 1 line
Yixun Lan (dlan)
author
#22 lore raw

PATCH 5/6 PCI: spacemit: introduce SpacemiT PCIe host driver

In reply to Alex Elder

Introduce a driver for the PCIe root complex found in the SpacemiT K1 SoC. The hardware is derived from the Synopsys DesignWare PCIe IP. The driver supports three PCIe ports that operate at PCIe v2 transfer rates (5 GT/sec). The first port uses a combo PHY, which may be configured for use for USB 3 instead.

---
 drivers/pci/controller/dwc/Kconfig   |  10 +
 drivers/pci/controller/dwc/Makefile  |   1 +
 drivers/pci/controller/dwc/pcie-k1.c | 355 +++++++++++++++++++++++++++
 3 files changed, 366 insertions(+)
 create mode 100644 drivers/pci/controller/dwc/pcie-k1.c
drivers/pci/controller/dwc/Kconfig +10 -0
index ff6b6d9e18ecf..ca5782c041ce8 100644
@@ -492,4 +492,14 @@ config PCIE_VISCONTI_HOST
 	  Say Y here if you want PCIe controller support on Toshiba Visconti SoC.
 	  This driver supports TMPV7708 SoC.
 
+config PCIE_K1
+	bool "SpacemiT K1 host mode PCIe controller"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on PCI && OF && HAS_IOMEM
+	select PCIE_DW_HOST
+	default ARCH_SPACEMIT
+	help
+	  Enables support for the PCIe controller in the K1 SoC operating
+	  in host mode.
+
 endmenu
drivers/pci/controller/dwc/Makefile +1 -0
index 6919d27798d13..62d9d4e7dd4d3 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_PCIE_UNIPHIER) += pcie-uniphier.o
 obj-$(CONFIG_PCIE_UNIPHIER_EP) += pcie-uniphier-ep.o
 obj-$(CONFIG_PCIE_VISCONTI_HOST) += pcie-visconti.o
 obj-$(CONFIG_PCIE_RCAR_GEN4) += pcie-rcar-gen4.o
+obj-$(CONFIG_PCIE_K1) += pcie-k1.o
 
 # The following drivers are for devices that use the generic ACPI
 # pci_root.c driver but don't support standard ECAM config access.
drivers/pci/controller/dwc/pcie-k1.c new file +355 -0

355 lines not on this page · raw message

Signature · 1 line
2.48.1
#23 lore raw

Re: [PATCH 5/6] PCI: spacemit: introduce SpacemiT PCIe host driver

Quoted 5 lines · Alex Elder

Introduce a driver for the PCIe root complex found in the SpacemiT K1 SoC. The hardware is derived from the Synopsys DesignWare PCIe IP. The driver supports three PCIe ports that operate at PCIe v2 transfer rates (5 GT/sec). The first port uses a combo PHY, which may be configured for use for USB 3 instead.

I assume "PCIe v2" means what most people call "PCIe gen2", but the spec encourages avoidance "genX" because it's ambiguous.

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 496

Quoted 2 lines
drivers/pci/controller/dwc/Kconfig +2 -0
+config PCIE_K1
+	bool "SpacemiT K1 host mode PCIe controller"

Style of nearby entries is:

"SpacemiT K1 PCIe controller (host mode)"

Please alphabetize by the company name ("SpacemiT") in the menu entry.

drivers/pci/controller/dwc/pcie-k1.c line 30

Quoted 2 lines
drivers/pci/controller/dwc/pcie-k1.c new file +2 -0
+#define K1_PCIE_VENDOR_ID	0x201f
+#define K1_PCIE_DEVICE_ID	0x0001

I assume this (0x201f) has been reserved by the PCI-SIG? I don't see it at:

https://pcisig.com/membership/member-companies?combine=0x201f

Possibly rename this to PCI_VENDOR_ID_K1 (or maybe PCI_VENDOR_ID_SPACEMIT?) to match the usual format in include/linux/pci_ids.h, since it seems likely to end up there eventually.

Quoted 1 line

+#define PCIE_RC_PERST BIT(12) /* 0: PERST# high; 1: low */

Maybe avoid confusion by describing as "1: assert PERST#" or similar?

drivers/pci/controller/dwc/pcie-k1.c line 170

Quoted 2 lines
drivers/pci/controller/dwc/pcie-k1.c new file +2 -0
+	/* Wait the PCIe-mandated 100 msec before deasserting PERST# */
+	mdelay(100);

I think this is PCIE_T_PVPERL_MS. Comment is superfluous then.

drivers/pci/controller/dwc/pcie-k1.c line 289

Quoted 12 lines
drivers/pci/controller/dwc/pcie-k1.c new file +12 -0
+static int k1_pcie_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct dw_pcie_rp *pp;
+	struct dw_pcie *pci;
+	struct k1_pcie *k1;
+	int ret;
+
+	k1 = devm_kzalloc(dev, sizeof(*k1), GFP_KERNEL);
+	if (!k1)
+		return -ENOMEM;
+	dev_set_drvdata(dev, k1);

Most neighboring drivers use platform_set_drvdata(). Personally, I would set drvdata after initializing k1 because I don't like to advertise pointers to uninitialized things.

drivers/pci/controller/dwc/pcie-k1.c line 335

Quoted 3 lines
drivers/pci/controller/dwc/pcie-k1.c new file +3 -0
+static void k1_pcie_remove(struct platform_device *pdev)
+{
+	struct k1_pcie *k1 = dev_get_drvdata(&pdev->dev);

Neighbors use platform_get_drvdata().

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+	struct dw_pcie_rp *pp = &k1->pci.pp;
+
+	dw_pcie_host_deinit(pp);
+}
author
#24 lore raw

Re: [PATCH 5/6] PCI: spacemit: introduce SpacemiT PCIe host driver

Quoted 9 lines · Bjorn Helgaas
Quoted 5 lines · Alex Elder

Introduce a driver for the PCIe root complex found in the SpacemiT K1 SoC. The hardware is derived from the Synopsys DesignWare PCIe IP. The driver supports three PCIe ports that operate at PCIe v2 transfer rates (5 GT/sec). The first port uses a combo PHY, which may be configured for use for USB 3 instead.

I assume "PCIe v2" means what most people call "PCIe gen2", but the spec encourages avoidance "genX" because it's ambiguous.

Yes, that's what I meant, but I did try to clarify with the transfer rate.

Quoted 6 lines

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 496

Quoted 2 lines
drivers/pci/controller/dwc/Kconfig +2 -0
+config PCIE_K1
+	bool "SpacemiT K1 host mode PCIe controller"

Style of nearby entries is:

"SpacemiT K1 PCIe controller (host mode)"

OK I'll fix that.

Quoted 1 line

Please alphabetize by the company name ("SpacemiT") in the menu entry.

OK.

Quoted 7 lines

drivers/pci/controller/dwc/pcie-k1.c line 30

Quoted 2 lines
drivers/pci/controller/dwc/pcie-k1.c new file +2 -0
+#define K1_PCIE_VENDOR_ID	0x201f
+#define K1_PCIE_DEVICE_ID	0x0001

I assume this (0x201f) has been reserved by the PCI-SIG? I don't see it at:

   https://pcisig.com/membership/member-companies?combine=0x201f

I hadn't even thought to check that. I will follow up. Thanks for pointing this out.

Quoted 4 lines

Possibly rename this to PCI_VENDOR_ID_K1 (or maybe PCI_VENDOR_ID_SPACEMIT?) to match the usual format in include/linux/pci_ids.h, since it seems likely to end up there eventually.

OK.

Quoted 3 lines
Quoted 1 line

+#define PCIE_RC_PERST BIT(12) /* 0: PERST# high; 1: low */

Maybe avoid confusion by describing as "1: assert PERST#" or similar?

OK. I struggled with how to express this to avoid confusion. But I do think "assert PERST#" is better.

Quoted 4 lines

drivers/pci/controller/dwc/pcie-k1.c line 170

Quoted 2 lines
drivers/pci/controller/dwc/pcie-k1.c new file +2 -0
+	/* Wait the PCIe-mandated 100 msec before deasserting PERST# */
+	mdelay(100);

I think this is PCIE_T_PVPERL_MS. Comment is superfluous then.

Excellent, thank you, I'll use that.

Quoted 8 lines
Quoted 8 lines
drivers/pci/controller/dwc/pcie-k1.c new file +8 -0
+static int k1_pcie_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct dw_pcie_rp *pp;
+	struct dw_pcie *pci;
+	struct k1_pcie *k1;
+	int ret;
+
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 289

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+	k1 = devm_kzalloc(dev, sizeof(*k1), GFP_KERNEL);
+	if (!k1)
+		return -ENOMEM;
+	dev_set_drvdata(dev, k1);

Most neighboring drivers use platform_set_drvdata(). Personally, I would set drvdata after initializing k1 because I don't like to advertise pointers to uninitialized things.

OK, I understand that and will do it the way you suggest.

Quoted 5 lines

drivers/pci/controller/dwc/pcie-k1.c line 335

Quoted 3 lines
drivers/pci/controller/dwc/pcie-k1.c new file +3 -0
+static void k1_pcie_remove(struct platform_device *pdev)
+{
+	struct k1_pcie *k1 = dev_get_drvdata(&pdev->dev);

Neighbors use platform_get_drvdata().

Yes, that goes with platform_set_drvdata().

Quoted 4 lines

drivers/pci/controller/dwc/pcie-k1.c line 339

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+	struct dw_pcie_rp *pp = &k1->pci.pp;
+
+	dw_pcie_host_deinit(pp);
+}

Thank you very much for your review.

-Alex

author
#25 lore raw

Re: [PATCH 5/6] PCI: spacemit: introduce SpacemiT PCIe host driver

Quoted 7 lines · Alex Elder
Quoted 6 lines · Bjorn Helgaas
Quoted 5 lines · Alex Elder

Introduce a driver for the PCIe root complex found in the SpacemiT K1 SoC.  The hardware is derived from the Synopsys DesignWare PCIe IP. The driver supports three PCIe ports that operate at PCIe v2 transfer rates (5 GT/sec).  The first port uses a combo PHY, which may be configured for use for USB 3 instead.

I'm following up on a few things I said last month.

Quoted 11 lines
Quoted 2 lines

I assume "PCIe v2" means what most people call "PCIe gen2", but the spec encourages avoidance "genX" because it's ambiguous.

Yes, that's what I meant, but I did try to clarify with the transfer rate.

Quoted 5 lines
Quoted 2 lines

+config PCIE_K1
+    bool "SpacemiT K1 host mode PCIe controller"

Style of nearby entries is:

Quoted 7 lines
Quoted 1 line

   "SpacemiT K1 PCIe controller (host mode)"

OK I'll fix that.

Quoted 1 line

Please alphabetize by the company name ("SpacemiT") in the menu entry.

OK.

I will be renaming the Kconfig option to be PCIE_SPACEMIT_K1 (instead of just PCIE_K1). I'm renaming the source file to be "pcie-spacemit-k1.c" instead of "pcie-k1.c" as well.

Quoted 10 lines
Quoted 7 lines

drivers/pci/controller/dwc/pcie-k1.c line 30

Quoted 2 lines
drivers/pci/controller/dwc/pcie-k1.c new file +2 -0
+#define K1_PCIE_VENDOR_ID    0x201f
+#define K1_PCIE_DEVICE_ID    0x0001

I assume this (0x201f) has been reserved by the PCI-SIG?  I don't see it at:

   https://pcisig.com/membership/member-companies?combine=0x201f

I hadn't even thought to check that.  I will follow up.  Thanks for pointing this out.

I inquired yesterday about this, and was told that this will be finalized next week. I told them that the driver would not be accepted upstream unless the vendor ID had been properly reserved by PCI-SIG.

Quoted 6 lines
Quoted 4 lines

Possibly rename this to PCI_VENDOR_ID_K1 (or maybe PCI_VENDOR_ID_SPACEMIT?) to match the usual format in include/linux/pci_ids.h, since it seems likely to end up there eventually.

OK.

I will use PCI_VENDOR_ID_SPACEMIT and PCI_DEVICE_ID_SPACEMIT_K1.

Quoted 34 lines
Quoted 4 lines
Quoted 2 lines

+#define PCIE_RC_PERST            BIT(12)    /* 0: PERST# high; 1:
low */

Maybe avoid confusion by describing as "1: assert PERST#" or similar?

OK.  I struggled with how to express this to avoid confusion. But I do think "assert PERST#" is better.

Quoted 4 lines
Quoted 2 lines

+    /* Wait the PCIe-mandated 100 msec before deasserting PERST# */ +    mdelay(100);

I think this is PCIE_T_PVPERL_MS.  Comment is superfluous then.

Excellent, thank you, I'll use that.

Quoted 8 lines
Quoted 8 lines
drivers/pci/controller/dwc/pcie-k1.c new file +8 -0
+static int k1_pcie_probe(struct platform_device *pdev)
+{
+    struct device *dev = &pdev->dev;
+    struct dw_pcie_rp *pp;
+    struct dw_pcie *pci;
+    struct k1_pcie *k1;
+    int ret;
+
Quoted 8 lines
Quoted 4 lines

+    k1 = devm_kzalloc(dev, sizeof(*k1), GFP_KERNEL); +    if (!k1)
+        return -ENOMEM;
+    dev_set_drvdata(dev, k1);

Most neighboring drivers use platform_set_drvdata().  Personally, I would set drvdata after initializing k1 because I don't like to advertise pointers to uninitialized things.

OK, I understand that and will do it the way you suggest.

Quoted 7 lines
Quoted 5 lines

drivers/pci/controller/dwc/pcie-k1.c line 335

Quoted 3 lines
drivers/pci/controller/dwc/pcie-k1.c new file +3 -0
+static void k1_pcie_remove(struct platform_device *pdev)
+{
+    struct k1_pcie *k1 = dev_get_drvdata(&pdev->dev);

Neighbors use platform_get_drvdata().

Yes, that goes with platform_set_drvdata().

Actually, many of them use dev_get_drvdata(). And I think that's why I used dev_set_drvdata() in the first place, to match dev_get_drvdata().

But in any case, I'll switch to setting and getting platform driver data.

-Alex

Quoted 9 lines
Quoted 5 lines

drivers/pci/controller/dwc/pcie-k1.c line 339

Quoted 5 lines

+    struct dw_pcie_rp *pp = &k1->pci.pp;

drivers/pci/controller/dwc/pcie-k1.c new file +3 -0
+
+    dw_pcie_host_deinit(pp);
+}

Thank you very much for your review.

                    -Alex

#26 lore raw

Re: [PATCH 5/6] PCI: spacemit: introduce SpacemiT PCIe host driver

In reply to Alex Elder
Quoted 19 lines · Alex Elder

Introduce a driver for the PCIe root complex found in the SpacemiT K1 SoC. The hardware is derived from the Synopsys DesignWare PCIe IP. The driver supports three PCIe ports that operate at PCIe v2 transfer rates (5 GT/sec). The first port uses a combo PHY, which may be configured for use for USB 3 instead.

---
 drivers/pci/controller/dwc/Kconfig   |  10 +
 drivers/pci/controller/dwc/Makefile  |   1 +
 drivers/pci/controller/dwc/pcie-k1.c | 355 +++++++++++++++++++++++++++
 3 files changed, 366 insertions(+)
 create mode 100644 drivers/pci/controller/dwc/pcie-k1.c
drivers/pci/controller/dwc/Kconfig +0 -0
index ff6b6d9e18ecf..ca5782c041ce8 100644

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 496

Quoted 5 lines
drivers/pci/controller/dwc/Kconfig +2 -0
@@ -492,4 +492,14 @@ config PCIE_VISCONTI_HOST
 	  Say Y here if you want PCIe controller support on Toshiba Visconti SoC.
 	  This driver supports TMPV7708 SoC.
 
+config PCIE_K1
+	bool "SpacemiT K1 host mode PCIe controller"

No need to make it bool, build it as a module. Only the PCI controller drivers implementing irqchip need to be kept bool for irq disposal concerns.

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 503

Quoted 7 lines
drivers/pci/controller/dwc/Kconfig +7 -0
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on PCI && OF && HAS_IOMEM
+	select PCIE_DW_HOST
+	default ARCH_SPACEMIT
+	help
+	  Enables support for the PCIe controller in the K1 SoC operating
+	  in host mode.

Is the driver only applicable for K1 SoCs or other SoCs from spacemit? Even if it is the former, I would suggest renaming to 'pcie-spacemit-k1.c'

Quoted 26 lines
drivers/pci/controller/dwc/Kconfig +1 -0
+
 endmenu
drivers/pci/controller/dwc/Makefile +1 -0
index 6919d27798d13..62d9d4e7dd4d3 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_PCIE_UNIPHIER) += pcie-uniphier.o
 obj-$(CONFIG_PCIE_UNIPHIER_EP) += pcie-uniphier-ep.o
 obj-$(CONFIG_PCIE_VISCONTI_HOST) += pcie-visconti.o
 obj-$(CONFIG_PCIE_RCAR_GEN4) += pcie-rcar-gen4.o
+obj-$(CONFIG_PCIE_K1) += pcie-k1.o
 
 # The following drivers are for devices that use the generic ACPI
 # pci_root.c driver but don't support standard ECAM config access.
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
new file mode 100644
index 0000000000000..e9b1df3428d16
@@ -0,0 +1,355 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SpacemiT K1 PCIe host driver
+ *
+ * Copyright (C) 2025 by RISCstar Solutions Corporation.  All rights reserved.
+ * Copyright (c) 2023, spacemit Corporation.

drivers/pci/controller/dwc/pcie-k1.c line 16

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/gfp.h>
+#include <linux/irq.h>

unused?

Quoted 47 lines
drivers/pci/controller/dwc/pcie-k1.c new file +47 -0
+#include <linux/mfd/syscon.h>
+#include <linux/mod_devicetable.h>
+#include <linux/of.h>
+#include <linux/pci.h>
+#include <linux/phy/phy.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+#include <linux/types.h>
+
+#include "pcie-designware.h"
+
+#define K1_PCIE_VENDOR_ID	0x201f
+#define K1_PCIE_DEVICE_ID	0x0001
+
+/* Offsets and field definitions of link management registers */
+
+#define K1_PHY_AHB_IRQ_EN			0x0000
+#define PCIE_INTERRUPT_EN		BIT(0)
+
+#define K1_PHY_AHB_LINK_STS			0x0004
+#define SMLH_LINK_UP			BIT(1)
+#define RDLH_LINK_UP			BIT(12)
+
+#define INTR_ENABLE				0x0014
+#define MSI_CTRL_INT			BIT(11)
+
+/* Offsets and field definitions for PMU registers */
+
+#define PCIE_CLK_RESET_CONTROL			0x0000
+#define LTSSM_EN			BIT(6)
+#define PCIE_AUX_PWR_DET		BIT(9)
+#define PCIE_RC_PERST			BIT(12)	/* 0: PERST# high; 1: low */
+#define APP_HOLD_PHY_RST		BIT(30)
+#define DEVICE_TYPE_RC			BIT(31)	/* 0: endpoint; 1: RC */
+
+#define PCIE_CONTROL_LOGIC			0x0004
+#define PCIE_SOFT_RESET			BIT(0)
+
+struct k1_pcie {
+	struct dw_pcie pci;
+	void __iomem *link;
+	struct regmap *pmu;
+	u32 pmu_off;
+	struct phy *phy;
+	struct reset_control *global_reset;
+};

drivers/pci/controller/dwc/pcie-k1.c line 73

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+
+#define to_k1_pcie(dw_pcie)	dev_get_drvdata((dw_pcie)->dev)
+
+static int k1_pcie_toggle_soft_reset(struct k1_pcie *k1)
+{
+	u32 offset = k1->pmu_off + PCIE_CONTROL_LOGIC;
+	const u32 mask = PCIE_SOFT_RESET;
+	int ret;
+
+	ret = regmap_set_bits(k1->pmu, offset, mask);

For MMIO, it is OK to skip the error handling.

drivers/pci/controller/dwc/pcie-k1.c line 77

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+	if (ret)
+		return ret;
+
+	mdelay(2);

If the previous write to the PMU got stuck in the CPU cache, there is no guarantee that this delay of 2ms between write and clear will be enforced. So you should do a dummy read after write to ensure that the previous write has reached the PMU (or any device) and then clear the bits.

drivers/pci/controller/dwc/pcie-k1.c line 90

Quoted 13 lines
drivers/pci/controller/dwc/pcie-k1.c new file +13 -0
+
+	return regmap_clear_bits(k1->pmu, offset, mask);
+}
+
+/* Enable app clocks, deassert app resets */
+static int k1_pcie_app_enable(struct k1_pcie *k1)
+{
+	struct dw_pcie *pci = &k1->pci;
+	u32 clock_count;
+	u32 reset_count;
+	int ret;
+
+	clock_count = ARRAY_SIZE(pci->app_clks);

Just use ARRAY_SIZE() directly below.

drivers/pci/controller/dwc/pcie-k1.c line 95

Quoted 5 lines
drivers/pci/controller/dwc/pcie-k1.c new file +5 -0
+	ret = clk_bulk_prepare_enable(clock_count, pci->app_clks);
+	if (ret)
+		return ret;
+
+	reset_count = ARRAY_SIZE(pci->app_rsts);

Same here.

drivers/pci/controller/dwc/pcie-k1.c line 107

Quoted 12 lines
drivers/pci/controller/dwc/pcie-k1.c new file +12 -0
+	ret = reset_control_bulk_deassert(reset_count, pci->app_rsts);
+	if (ret)
+		goto err_disable_clks;
+
+	ret = reset_control_deassert(k1->global_reset);
+	if (ret)
+		goto err_assert_resets;
+
+	return 0;
+
+err_assert_resets:
+	(void)reset_control_bulk_assert(reset_count, pci->app_rsts);

Why void cast? Here and in other places.

Quoted 13 lines
drivers/pci/controller/dwc/pcie-k1.c new file +13 -0
+err_disable_clks:
+	clk_bulk_disable_unprepare(clock_count, pci->app_clks);
+
+	return ret;
+}
+
+/* Disable app clocks, assert app resets */
+static void k1_pcie_app_disable(struct k1_pcie *k1)
+{
+	struct dw_pcie *pci = &k1->pci;
+	u32 count;
+	int ret;
+

drivers/pci/controller/dwc/pcie-k1.c line 130

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	(void)reset_control_assert(k1->global_reset);
+
+	count = ARRAY_SIZE(pci->app_rsts);
+	ret = reset_control_bulk_assert(count, pci->app_rsts);
+	if (ret)
+		dev_err(pci->dev, "app reset assert failed (%d)\n", ret);
+
+	count = ARRAY_SIZE(pci->app_clks);
+	clk_bulk_disable_unprepare(count, pci->app_clks);
+}

Same comments as k1_pcie_app_enable().

Quoted 19 lines
drivers/pci/controller/dwc/pcie-k1.c new file +19 -0
+
+static int k1_pcie_init(struct dw_pcie_rp *pp)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	u32 offset;
+	u32 mask;
+	int ret;
+
+	ret = k1_pcie_toggle_soft_reset(k1);
+	if (ret)
+		goto err_app_disable;
+
+	ret = k1_pcie_app_enable(k1);
+	if (ret)
+		return ret;
+
+	ret = phy_init(k1->phy);
+	if (ret)

drivers/pci/controller/dwc/pcie-k1.c line 159

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+		goto err_app_disable;
+
+	/* Set the PCI vendor and device ID */
+	dw_pcie_dbi_ro_wr_en(pci);
+	dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, K1_PCIE_VENDOR_ID);
+	dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, K1_PCIE_DEVICE_ID);
+	dw_pcie_dbi_ro_wr_dis(pci);
+
+	/*
+	 * Put the port in root complex mode, record that Vaux is present.

There is no 3.3Vaux supply present in the binding. So the supply is guaranteed to be present and enabled always by the platform?

drivers/pci/controller/dwc/pcie-k1.c line 170

Quoted 11 lines
drivers/pci/controller/dwc/pcie-k1.c new file +11 -0
+	 * Assert fundamental reset (drive PERST# low).
+	 */
+	offset = k1->pmu_off + PCIE_CLK_RESET_CONTROL;
+	mask = DEVICE_TYPE_RC | PCIE_AUX_PWR_DET;
+	mask |= PCIE_RC_PERST;
+	ret = regmap_set_bits(k1->pmu, offset, mask);
+	if (ret)
+		goto err_phy_exit;
+
+	/* Wait the PCIe-mandated 100 msec before deasserting PERST# */
+	mdelay(100);

Same comment as k1_pcie_toggle_soft_reset() applies here.

drivers/pci/controller/dwc/pcie-k1.c line 174

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+
+	ret = regmap_clear_bits(k1->pmu, offset, PCIE_RC_PERST);
+	if (!ret)
+		return 0;	/* Success! */

Please use common pattern to return success:

	regmap_clear_bits()

	return 0;
Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+
+err_phy_exit:
+	(void)phy_exit(k1->phy);
+err_app_disable:
+	k1_pcie_app_disable(k1);
+

drivers/pci/controller/dwc/pcie-k1.c line 190

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	return ret;
+}
+
+/* Silently ignore any errors */
+static void k1_pcie_deinit(struct dw_pcie_rp *pp)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+
+	/* Re-assert fundamental reset (drive PERST# low) */

s/Re-assert/Assert

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	(void)regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+			      PCIE_RC_PERST);
+
+	(void)phy_exit(k1->phy);
+
+	k1_pcie_app_disable(k1);
+}
+
+static const struct dw_pcie_host_ops k1_pcie_host_ops = {
+	.init		= k1_pcie_init,

drivers/pci/controller/dwc/pcie-k1.c line 210

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	.deinit		= k1_pcie_deinit,
+};
+
+static void k1_pcie_enable_interrupts(struct k1_pcie *k1)
+{
+	void __iomem *virt;
+	u32 val;
+
+	/* Enable the MSI interrupt */
+	writel(MSI_CTRL_INT, k1->link + INTR_ENABLE);

If there are no ordering issues (I guess so), you can very well use the _relaxed variants throughout the driver.

Quoted 76 lines
drivers/pci/controller/dwc/pcie-k1.c new file +76 -0
+
+	/* Top-level interrupt enable */
+	virt = k1->link + K1_PHY_AHB_IRQ_EN;
+	val = readl(virt);
+	val |= PCIE_INTERRUPT_EN;
+	writel(val, virt);
+}
+
+static void k1_pcie_disable_interrupts(struct k1_pcie *k1)
+{
+	void __iomem *virt;
+	u32 val;
+
+	virt = k1->link + K1_PHY_AHB_IRQ_EN;
+	val = readl(virt);
+	val &= ~PCIE_INTERRUPT_EN;
+	writel(val, virt);
+
+	writel(0, k1->link + INTR_ENABLE);
+}
+
+static bool k1_pcie_link_up(struct dw_pcie *pci)
+{
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	u32 val;
+
+	val = readl(k1->link + K1_PHY_AHB_LINK_STS);
+
+	return (val & RDLH_LINK_UP) && (val & SMLH_LINK_UP);
+}
+
+static int k1_pcie_start_link(struct dw_pcie *pci)
+{
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	int ret;
+
+	/* Stop holding the PHY in reset, and enable link training */
+	ret = regmap_update_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+				 APP_HOLD_PHY_RST | LTSSM_EN, LTSSM_EN);
+	if (ret)
+		return ret;
+
+	k1_pcie_enable_interrupts(k1);
+
+	return 0;
+}
+
+static void k1_pcie_stop_link(struct dw_pcie *pci)
+{
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	int ret;
+
+	k1_pcie_disable_interrupts(k1);
+
+	/* Disable the link and hold the PHY in reset */
+	ret = regmap_update_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+				 APP_HOLD_PHY_RST | LTSSM_EN, APP_HOLD_PHY_RST);
+	if (ret)
+		dev_err(pci->dev, "disable LTSSM failed (%d)\n", ret);
+}
+
+static const struct dw_pcie_ops k1_pcie_ops = {
+	.link_up	= k1_pcie_link_up,
+	.start_link	= k1_pcie_start_link,
+	.stop_link	= k1_pcie_stop_link,
+};
+
+static int k1_pcie_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct dw_pcie_rp *pp;
+	struct dw_pcie *pci;
+	struct k1_pcie *k1;
+	int ret;
+
+	k1 = devm_kzalloc(dev, sizeof(*k1), GFP_KERNEL);

drivers/pci/controller/dwc/pcie-k1.c line 296

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	if (!k1)
+		return -ENOMEM;
+	dev_set_drvdata(dev, k1);
+
+	k1->pmu = syscon_regmap_lookup_by_phandle_args(dev_of_node(dev),
+						       "spacemit,syscon-pmu",
+						       1, &k1->pmu_off);
+	if (IS_ERR(k1->pmu))
+		return dev_err_probe(dev, PTR_ERR(k1->pmu),
+				     "lookup PMU regmap failed\n");

'Failed to lookup \"PMU\" registers'

drivers/pci/controller/dwc/pcie-k1.c line 300

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+
+	k1->link = devm_platform_ioremap_resource_byname(pdev, "link");
+	if (!k1->link)
+		return dev_err_probe(dev, -ENOMEM, "map link regs failed\n");

'Failed to map \"link\" registers

Same for below error prints as well.

Quoted 13 lines
drivers/pci/controller/dwc/pcie-k1.c new file +13 -0
+
+	k1->global_reset = devm_reset_control_get_shared(dev, "global");
+	if (IS_ERR(k1->global_reset))
+		return dev_err_probe(dev, PTR_ERR(k1->global_reset),
+				     "get global reset failed\n");
+
+	/* Hold the PHY in reset until we start the link */
+	ret = regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+			      APP_HOLD_PHY_RST);
+	if (ret)
+		return dev_err_probe(dev, ret, "hold PHY in reset failed\n");
+
+	k1->phy = devm_phy_get(dev, NULL);

drivers/pci/controller/dwc/pcie-k1.c line 323

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	if (IS_ERR(k1->phy))
+		return dev_err_probe(dev, PTR_ERR(k1->phy), "get PHY failed\n");
+
+	pci = &k1->pci;
+	dw_pcie_cap_set(pci, REQ_RES);
+	pci->dev = dev;
+	pci->ops = &k1_pcie_ops;
+
+	pp = &pci->pp;
+	pp->num_vectors = MAX_MSI_IRQS;

I don't understand how MSI is implemented in this platform. If the controller relies on an external interrupt controller for handling MSIs (I think it does), then there should be either 'msi-parent' or 'msi-map' existed in the binding.

But I see none, other than 'interrupts-extended'. So I don't know how MSI works at all.

Quoted 19 lines
drivers/pci/controller/dwc/pcie-k1.c new file +19 -0
+	pp->ops = &k1_pcie_host_ops;
+
+	ret = dw_pcie_host_init(pp);
+	if (ret)
+		return dev_err_probe(dev, ret, "host init failed\n");
+
+	return 0;
+}
+
+static void k1_pcie_remove(struct platform_device *pdev)
+{
+	struct k1_pcie *k1 = dev_get_drvdata(&pdev->dev);
+	struct dw_pcie_rp *pp = &k1->pci.pp;
+
+	dw_pcie_host_deinit(pp);
+}
+
+static const struct of_device_id k1_pcie_of_match_table[] = {
+	{ .compatible = "spacemit,k1-pcie-rc", },

drivers/pci/controller/dwc/pcie-k1.c line 352

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	{ },
+};
+
+static struct platform_driver k1_pcie_driver = {
+	.probe	= k1_pcie_probe,
+	.remove	= k1_pcie_remove,
+	.driver = {
+		.name			= "k1-dwc-pcie",
+		.of_match_table		= k1_pcie_of_match_table,
+		.suppress_bind_attrs	= true,

No need of this flag for the reason I mentioned in the Kcofig change.

You should also set,

.probe_type = PROBE_PREFER_ASYNCHRONOUS,

to make use of the async probing of the devices during boot. This does save some boot time.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
author
#27 lore raw

Re: [PATCH 5/6] PCI: spacemit: introduce SpacemiT PCIe host driver

Quoted 23 lines · Manivannan Sadhasivam
Quoted 22 lines · Alex Elder

Introduce a driver for the PCIe root complex found in the SpacemiT K1 SoC. The hardware is derived from the Synopsys DesignWare PCIe IP. The driver supports three PCIe ports that operate at PCIe v2 transfer rates (5 GT/sec). The first port uses a combo PHY, which may be configured for use for USB 3 instead.

---
  drivers/pci/controller/dwc/Kconfig   |  10 +
  drivers/pci/controller/dwc/Makefile  |   1 +
  drivers/pci/controller/dwc/pcie-k1.c | 355 +++++++++++++++++++++++++++
  3 files changed, 366 insertions(+)

create mode 100644 drivers/pci/controller/dwc/pcie-k1.c

drivers/pci/controller/dwc/Kconfig +0 -0
index ff6b6d9e18ecf..ca5782c041ce8 100644
@@ -492,4 +492,14 @@ config PCIE_VISCONTI_HOST
  	  Say Y here if you want PCIe controller support on Toshiba Visconti SoC.
  	  This driver supports TMPV7708 SoC.
 
Quoted 5 lines

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 496

Quoted 2 lines
drivers/pci/controller/dwc/Kconfig +2 -0
+config PCIE_K1
+	bool "SpacemiT K1 host mode PCIe controller"

No need to make it bool, build it as a module. Only the PCI controller drivers implementing irqchip need to be kept bool for irq disposal concerns.

OK.

Quoted 10 lines

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 503

Quoted 7 lines
drivers/pci/controller/dwc/Kconfig +7 -0
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on PCI && OF && HAS_IOMEM
+	select PCIE_DW_HOST
+	default ARCH_SPACEMIT
+	help
+	  Enables support for the PCIe controller in the K1 SoC operating
+	  in host mode.

Is the driver only applicable for K1 SoCs or other SoCs from spacemit? Even if it is the former, I would suggest renaming to 'pcie-spacemit-k1.c'

Yes, I will do that.

Quoted 29 lines
Quoted 29 lines
drivers/pci/controller/dwc/Kconfig +0 -0
  endmenu
drivers/pci/controller/dwc/Makefile +1 -0
index 6919d27798d13..62d9d4e7dd4d3 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_PCIE_UNIPHIER) += pcie-uniphier.o
  obj-$(CONFIG_PCIE_UNIPHIER_EP) += pcie-uniphier-ep.o
  obj-$(CONFIG_PCIE_VISCONTI_HOST) += pcie-visconti.o
  obj-$(CONFIG_PCIE_RCAR_GEN4) += pcie-rcar-gen4.o
+obj-$(CONFIG_PCIE_K1) += pcie-k1.o
 
  # The following drivers are for devices that use the generic ACPI
  # pci_root.c driver but don't support standard ECAM config access.
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
new file mode 100644
index 0000000000000..e9b1df3428d16
@@ -0,0 +1,355 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SpacemiT K1 PCIe host driver
+ *
+ * Copyright (C) 2025 by RISCstar Solutions Corporation.  All rights reserved.
+ * Copyright (c) 2023, spacemit Corporation.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 16

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/gfp.h>
+#include <linux/irq.h>

unused?

Yes, and there are a few others I can get rid of too.

Quoted 51 lines
Quoted 51 lines
drivers/pci/controller/dwc/pcie-k1.c new file +51 -0
+#include <linux/mfd/syscon.h>
+#include <linux/mod_devicetable.h>
+#include <linux/of.h>
+#include <linux/pci.h>
+#include <linux/phy/phy.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+#include <linux/types.h>
+
+#include "pcie-designware.h"
+
+#define K1_PCIE_VENDOR_ID	0x201f
+#define K1_PCIE_DEVICE_ID	0x0001
+
+/* Offsets and field definitions of link management registers */
+
+#define K1_PHY_AHB_IRQ_EN			0x0000
+#define PCIE_INTERRUPT_EN		BIT(0)
+
+#define K1_PHY_AHB_LINK_STS			0x0004
+#define SMLH_LINK_UP			BIT(1)
+#define RDLH_LINK_UP			BIT(12)
+
+#define INTR_ENABLE				0x0014
+#define MSI_CTRL_INT			BIT(11)
+
+/* Offsets and field definitions for PMU registers */
+
+#define PCIE_CLK_RESET_CONTROL			0x0000
+#define LTSSM_EN			BIT(6)
+#define PCIE_AUX_PWR_DET		BIT(9)
+#define PCIE_RC_PERST			BIT(12)	/* 0: PERST# high; 1: low */
+#define APP_HOLD_PHY_RST		BIT(30)
+#define DEVICE_TYPE_RC			BIT(31)	/* 0: endpoint; 1: RC */
+
+#define PCIE_CONTROL_LOGIC			0x0004
+#define PCIE_SOFT_RESET			BIT(0)
+
+struct k1_pcie {
+	struct dw_pcie pci;
+	void __iomem *link;
+	struct regmap *pmu;
+	u32 pmu_off;
+	struct phy *phy;
+	struct reset_control *global_reset;
+};
+
+#define to_k1_pcie(dw_pcie)	dev_get_drvdata((dw_pcie)->dev)
+
+static int k1_pcie_toggle_soft_reset(struct k1_pcie *k1)
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 73

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+{
+	u32 offset = k1->pmu_off + PCIE_CONTROL_LOGIC;
+	const u32 mask = PCIE_SOFT_RESET;
+	int ret;
+
+	ret = regmap_set_bits(k1->pmu, offset, mask);

For MMIO, it is OK to skip the error handling.

You mean even though the regmap API returns an error, it never will with MMIO?
- regmap_mmio_read() and regmap_mmio_write() always return 0 unless there's an error enabling its clock.

Sounds good, I'll simplify places that use this.

Quoted 9 lines

drivers/pci/controller/dwc/pcie-k1.c line 77

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+	if (ret)
+		return ret;
+
+	mdelay(2);

If the previous write to the PMU got stuck in the CPU cache, there is no guarantee that this delay of 2ms between write and clear will be enforced. So you should do a dummy read after write to ensure that the previous write has reached the PMU (or any device) and then clear the bits.

Wow, really? I was aware of this being possible for I/O writes but it seems like something regmap might handle.

I'll add a regmap_read() for the same offset and discard the result *before* the delay. I'll do the same for this:

        mdelay(PCIE_T_PVPERL_MS);
Quoted 6 lines
Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	return regmap_clear_bits(k1->pmu, offset, mask);
+}
+
+/* Enable app clocks, deassert app resets */
+static int k1_pcie_app_enable(struct k1_pcie *k1)
+{
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 90

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	struct dw_pcie *pci = &k1->pci;
+	u32 clock_count;
+	u32 reset_count;
+	int ret;
+
+	clock_count = ARRAY_SIZE(pci->app_clks);

Just use ARRAY_SIZE() directly below.

OK.

Quoted 7 lines

drivers/pci/controller/dwc/pcie-k1.c line 95

Quoted 5 lines
drivers/pci/controller/dwc/pcie-k1.c new file +5 -0
+	ret = clk_bulk_prepare_enable(clock_count, pci->app_clks);
+	if (ret)
+		return ret;
+
+	reset_count = ARRAY_SIZE(pci->app_rsts);

Same here.

OK.

Quoted 6 lines
Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	ret = reset_control_bulk_deassert(reset_count, pci->app_rsts);
+	if (ret)
+		goto err_disable_clks;
+
+	ret = reset_control_deassert(k1->global_reset);
+	if (ret)
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 107

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+		goto err_assert_resets;
+
+	return 0;
+
+err_assert_resets:
+	(void)reset_control_bulk_assert(reset_count, pci->app_rsts);

Why void cast? Here and in other places.

I put void casts when I'm ignoring a returned value. It's not necessary, but it reminds me that the function returns a value, and at some point I decided to ignore it. I can drop those if you find them offensive.

If you're suggesting I should issue a warning here if an error is returned here, tell me that.

Quoted 17 lines
Quoted 17 lines
drivers/pci/controller/dwc/pcie-k1.c new file +17 -0
+err_disable_clks:
+	clk_bulk_disable_unprepare(clock_count, pci->app_clks);
+
+	return ret;
+}
+
+/* Disable app clocks, assert app resets */
+static void k1_pcie_app_disable(struct k1_pcie *k1)
+{
+	struct dw_pcie *pci = &k1->pci;
+	u32 count;
+	int ret;
+
+	(void)reset_control_assert(k1->global_reset);
+
+	count = ARRAY_SIZE(pci->app_rsts);
+	ret = reset_control_bulk_assert(count, pci->app_rsts);
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 130

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	if (ret)
+		dev_err(pci->dev, "app reset assert failed (%d)\n", ret);
+
+	count = ARRAY_SIZE(pci->app_clks);
+	clk_bulk_disable_unprepare(count, pci->app_clks);
+}

Same comments as k1_pcie_app_enable().

OK.

Quoted 23 lines
Quoted 23 lines
drivers/pci/controller/dwc/pcie-k1.c new file +23 -0
+static int k1_pcie_init(struct dw_pcie_rp *pp)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	u32 offset;
+	u32 mask;
+	int ret;
+
+	ret = k1_pcie_toggle_soft_reset(k1);
+	if (ret)
+		goto err_app_disable;
+
+	ret = k1_pcie_app_enable(k1);
+	if (ret)
+		return ret;
+
+	ret = phy_init(k1->phy);
+	if (ret)
+		goto err_app_disable;
+
+	/* Set the PCI vendor and device ID */
+	dw_pcie_dbi_ro_wr_en(pci);
+	dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, K1_PCIE_VENDOR_ID);
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 159

Quoted 5 lines
drivers/pci/controller/dwc/pcie-k1.c new file +5 -0
+	dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, K1_PCIE_DEVICE_ID);
+	dw_pcie_dbi_ro_wr_dis(pci);
+
+	/*
+	 * Put the port in root complex mode, record that Vaux is present.

There is no 3.3Vaux supply present in the binding. So the supply is guaranteed to be present and enabled always by the platform?

Actually, I don't know, I'll ask. Thank you for pointing this out.

Quoted 5 lines
Quoted 5 lines
drivers/pci/controller/dwc/pcie-k1.c new file +5 -0
+	 * Assert fundamental reset (drive PERST# low).
+	 */
+	offset = k1->pmu_off + PCIE_CLK_RESET_CONTROL;
+	mask = DEVICE_TYPE_RC | PCIE_AUX_PWR_DET;
+	mask |= PCIE_RC_PERST;
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 170

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	ret = regmap_set_bits(k1->pmu, offset, mask);
+	if (ret)
+		goto err_phy_exit;
+
+	/* Wait the PCIe-mandated 100 msec before deasserting PERST# */
+	mdelay(100);

Same comment as k1_pcie_toggle_soft_reset() applies here.

Yes, understood.

Quoted 10 lines

drivers/pci/controller/dwc/pcie-k1.c line 174

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+
+	ret = regmap_clear_bits(k1->pmu, offset, PCIE_RC_PERST);
+	if (!ret)
+		return 0;	/* Success! */

Please use common pattern to return success:

	regmap_clear_bits()

	return 0;

Now that I won't be checking return values this will come naturally. So yes, it will look like this, and there are no other instances of this return pattern in this file.

Quoted 35 lines
Quoted 7 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+
+err_phy_exit:
+	(void)phy_exit(k1->phy);
+err_app_disable:
+	k1_pcie_app_disable(k1);
+

drivers/pci/controller/dwc/pcie-k1.c line 190

Quoted 10 lines
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
+	return ret;
+}
+
+/* Silently ignore any errors */
+static void k1_pcie_deinit(struct dw_pcie_rp *pp)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+
+	/* Re-assert fundamental reset (drive PERST# low) */

s/Re-assert/Assert

Quoted 15 lines
drivers/pci/controller/dwc/pcie-k1.c new file +15 -0
+	(void)regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+			      PCIE_RC_PERST);
+
+	(void)phy_exit(k1->phy);
+
+	k1_pcie_app_disable(k1);
+}
+
+static const struct dw_pcie_host_ops k1_pcie_host_ops = {
+	.init		= k1_pcie_init,
+	.deinit		= k1_pcie_deinit,
+};
+
+static void k1_pcie_enable_interrupts(struct k1_pcie *k1)
+{
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 210

Quoted 5 lines
drivers/pci/controller/dwc/pcie-k1.c new file +5 -0
+	void __iomem *virt;
+	u32 val;
+
+	/* Enable the MSI interrupt */
+	writel(MSI_CTRL_INT, k1->link + INTR_ENABLE);

If there are no ordering issues (I guess so), you can very well use the _relaxed variants throughout the driver.

The only writel() calls are related to updating these interrupt bits. I think you're right.

Quoted 79 lines
Quoted 79 lines
drivers/pci/controller/dwc/pcie-k1.c new file +79 -0
+	/* Top-level interrupt enable */
+	virt = k1->link + K1_PHY_AHB_IRQ_EN;
+	val = readl(virt);
+	val |= PCIE_INTERRUPT_EN;
+	writel(val, virt);
+}
+
+static void k1_pcie_disable_interrupts(struct k1_pcie *k1)
+{
+	void __iomem *virt;
+	u32 val;
+
+	virt = k1->link + K1_PHY_AHB_IRQ_EN;
+	val = readl(virt);
+	val &= ~PCIE_INTERRUPT_EN;
+	writel(val, virt);
+
+	writel(0, k1->link + INTR_ENABLE);
+}
+
+static bool k1_pcie_link_up(struct dw_pcie *pci)
+{
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	u32 val;
+
+	val = readl(k1->link + K1_PHY_AHB_LINK_STS);
+
+	return (val & RDLH_LINK_UP) && (val & SMLH_LINK_UP);
+}
+
+static int k1_pcie_start_link(struct dw_pcie *pci)
+{
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	int ret;
+
+	/* Stop holding the PHY in reset, and enable link training */
+	ret = regmap_update_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+				 APP_HOLD_PHY_RST | LTSSM_EN, LTSSM_EN);
+	if (ret)
+		return ret;
+
+	k1_pcie_enable_interrupts(k1);
+
+	return 0;
+}
+
+static void k1_pcie_stop_link(struct dw_pcie *pci)
+{
+	struct k1_pcie *k1 = to_k1_pcie(pci);
+	int ret;
+
+	k1_pcie_disable_interrupts(k1);
+
+	/* Disable the link and hold the PHY in reset */
+	ret = regmap_update_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+				 APP_HOLD_PHY_RST | LTSSM_EN, APP_HOLD_PHY_RST);
+	if (ret)
+		dev_err(pci->dev, "disable LTSSM failed (%d)\n", ret);
+}
+
+static const struct dw_pcie_ops k1_pcie_ops = {
+	.link_up	= k1_pcie_link_up,
+	.start_link	= k1_pcie_start_link,
+	.stop_link	= k1_pcie_stop_link,
+};
+
+static int k1_pcie_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct dw_pcie_rp *pp;
+	struct dw_pcie *pci;
+	struct k1_pcie *k1;
+	int ret;
+
+	k1 = devm_kzalloc(dev, sizeof(*k1), GFP_KERNEL);
+	if (!k1)
+		return -ENOMEM;
+	dev_set_drvdata(dev, k1);
+
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 296

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	k1->pmu = syscon_regmap_lookup_by_phandle_args(dev_of_node(dev),
+						       "spacemit,syscon-pmu",
+						       1, &k1->pmu_off);
+	if (IS_ERR(k1->pmu))
+		return dev_err_probe(dev, PTR_ERR(k1->pmu),
+				     "lookup PMU regmap failed\n");

'Failed to lookup \"PMU\" registers'

OK.

Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 300

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+
+	k1->link = devm_platform_ioremap_resource_byname(pdev, "link");
+	if (!k1->link)
+		return dev_err_probe(dev, -ENOMEM, "map link regs failed\n");

'Failed to map \"link\" registers

Same for below error prints as well.

OK.

Quoted 19 lines
Quoted 19 lines
drivers/pci/controller/dwc/pcie-k1.c new file +19 -0
+
+	k1->global_reset = devm_reset_control_get_shared(dev, "global");
+	if (IS_ERR(k1->global_reset))
+		return dev_err_probe(dev, PTR_ERR(k1->global_reset),
+				     "get global reset failed\n");
+
+	/* Hold the PHY in reset until we start the link */
+	ret = regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL,
+			      APP_HOLD_PHY_RST);
+	if (ret)
+		return dev_err_probe(dev, ret, "hold PHY in reset failed\n");
+
+	k1->phy = devm_phy_get(dev, NULL);
+	if (IS_ERR(k1->phy))
+		return dev_err_probe(dev, PTR_ERR(k1->phy), "get PHY failed\n");
+
+	pci = &k1->pci;
+	dw_pcie_cap_set(pci, REQ_RES);
+	pci->dev = dev;
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 323

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+	pci->ops = &k1_pcie_ops;
+
+	pp = &pci->pp;
+	pp->num_vectors = MAX_MSI_IRQS;

I don't understand how MSI is implemented in this platform. If the controller relies on an external interrupt controller for handling MSIs (I think it does), then there should be either 'msi-parent' or 'msi-map' existed in the binding.

OK your comment earlier made me realize I had more to do here. I'll work to improve that.

Quoted 26 lines

But I see none, other than 'interrupts-extended'. So I don't know how MSI works at all.

Quoted 23 lines
drivers/pci/controller/dwc/pcie-k1.c new file +23 -0
+	pp->ops = &k1_pcie_host_ops;
+
+	ret = dw_pcie_host_init(pp);
+	if (ret)
+		return dev_err_probe(dev, ret, "host init failed\n");
+
+	return 0;
+}
+
+static void k1_pcie_remove(struct platform_device *pdev)
+{
+	struct k1_pcie *k1 = dev_get_drvdata(&pdev->dev);
+	struct dw_pcie_rp *pp = &k1->pci.pp;
+
+	dw_pcie_host_deinit(pp);
+}
+
+static const struct of_device_id k1_pcie_of_match_table[] = {
+	{ .compatible = "spacemit,k1-pcie-rc", },
+	{ },
+};
+
+static struct platform_driver k1_pcie_driver = {
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 352

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	.probe	= k1_pcie_probe,
+	.remove	= k1_pcie_remove,
+	.driver = {
+		.name			= "k1-dwc-pcie",
+		.of_match_table		= k1_pcie_of_match_table,
+		.suppress_bind_attrs	= true,

No need of this flag for the reason I mentioned in the Kcofig change.

Because this doesn't implement an irqchip?

Quoted 6 lines

You should also set,

.probe_type = PROBE_PREFER_ASYNCHRONOUS,

to make use of the async probing of the devices during boot. This does save some boot time.

That's great to know, there is a noticeable delay during probe.

Thank you very much for your careful review, Mani.

-Alex

Quoted 2 lines

- Mani

#28 lore raw

Re: [PATCH 5/6] PCI: spacemit: introduce SpacemiT PCIe host driver

Quoted 160 lines · Alex Elder
Quoted 15 lines · Manivannan Sadhasivam
Quoted 14 lines · Alex Elder

Introduce a driver for the PCIe root complex found in the SpacemiT K1 SoC. The hardware is derived from the Synopsys DesignWare PCIe IP. The driver supports three PCIe ports that operate at PCIe v2 transfer rates (5 GT/sec). The first port uses a combo PHY, which may be configured for use for USB 3 instead.

---
  drivers/pci/controller/dwc/Kconfig   |  10 +
  drivers/pci/controller/dwc/Makefile  |   1 +
  drivers/pci/controller/dwc/pcie-k1.c | 355 +++++++++++++++++++++++++++
  3 files changed, 366 insertions(+)

create mode 100644 drivers/pci/controller/dwc/pcie-k1.c

Quoted 12 lines

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 495

Quoted 9 lines
drivers/pci/controller/dwc/Kconfig +2 -0
index ff6b6d9e18ecf..ca5782c041ce8 100644
@@ -492,4 +492,14 @@ config PCIE_VISCONTI_HOST
  	  Say Y here if you want PCIe controller support on Toshiba Visconti SoC.
  	  This driver supports TMPV7708 SoC.
+config PCIE_K1
+	bool "SpacemiT K1 host mode PCIe controller"

No need to make it bool, build it as a module. Only the PCI controller drivers implementing irqchip need to be kept bool for irq disposal concerns.

OK.

Quoted 10 lines

drivers/pci/controller/dwc/Kconfig config PCIE_VISCONTI_HOST · line 503

Quoted 7 lines
drivers/pci/controller/dwc/Kconfig +7 -0
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on PCI && OF && HAS_IOMEM
+	select PCIE_DW_HOST
+	default ARCH_SPACEMIT
+	help
+	  Enables support for the PCIe controller in the K1 SoC operating
+	  in host mode.

Is the driver only applicable for K1 SoCs or other SoCs from spacemit? Even if it is the former, I would suggest renaming to 'pcie-spacemit-k1.c'

Yes, I will do that.

Quoted 28 lines
Quoted 28 lines
drivers/pci/controller/dwc/Kconfig +0 -0
  endmenu
drivers/pci/controller/dwc/Makefile +1 -0
index 6919d27798d13..62d9d4e7dd4d3 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_PCIE_UNIPHIER) += pcie-uniphier.o
  obj-$(CONFIG_PCIE_UNIPHIER_EP) += pcie-uniphier-ep.o
  obj-$(CONFIG_PCIE_VISCONTI_HOST) += pcie-visconti.o
  obj-$(CONFIG_PCIE_RCAR_GEN4) += pcie-rcar-gen4.o
+obj-$(CONFIG_PCIE_K1) += pcie-k1.o
  # The following drivers are for devices that use the generic ACPI
  # pci_root.c driver but don't support standard ECAM config access.
drivers/pci/controller/dwc/pcie-k1.c new file +10 -0
new file mode 100644
index 0000000000000..e9b1df3428d16
@@ -0,0 +1,355 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SpacemiT K1 PCIe host driver
+ *
+ * Copyright (C) 2025 by RISCstar Solutions Corporation.  All rights reserved.
+ * Copyright (c) 2023, spacemit Corporation.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 16

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/gfp.h>
+#include <linux/irq.h>

unused?

Yes, and there are a few others I can get rid of too.

Quoted 51 lines
Quoted 51 lines
drivers/pci/controller/dwc/pcie-k1.c new file +51 -0
+#include <linux/mfd/syscon.h>
+#include <linux/mod_devicetable.h>
+#include <linux/of.h>
+#include <linux/pci.h>
+#include <linux/phy/phy.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+#include <linux/types.h>
+
+#include "pcie-designware.h"
+
+#define K1_PCIE_VENDOR_ID	0x201f
+#define K1_PCIE_DEVICE_ID	0x0001
+
+/* Offsets and field definitions of link management registers */
+
+#define K1_PHY_AHB_IRQ_EN			0x0000
+#define PCIE_INTERRUPT_EN		BIT(0)
+
+#define K1_PHY_AHB_LINK_STS			0x0004
+#define SMLH_LINK_UP			BIT(1)
+#define RDLH_LINK_UP			BIT(12)
+
+#define INTR_ENABLE				0x0014
+#define MSI_CTRL_INT			BIT(11)
+
+/* Offsets and field definitions for PMU registers */
+
+#define PCIE_CLK_RESET_CONTROL			0x0000
+#define LTSSM_EN			BIT(6)
+#define PCIE_AUX_PWR_DET		BIT(9)
+#define PCIE_RC_PERST			BIT(12)	/* 0: PERST# high; 1: low */
+#define APP_HOLD_PHY_RST		BIT(30)
+#define DEVICE_TYPE_RC			BIT(31)	/* 0: endpoint; 1: RC */
+
+#define PCIE_CONTROL_LOGIC			0x0004
+#define PCIE_SOFT_RESET			BIT(0)
+
+struct k1_pcie {
+	struct dw_pcie pci;
+	void __iomem *link;
+	struct regmap *pmu;
+	u32 pmu_off;
+	struct phy *phy;
+	struct reset_control *global_reset;
+};
+
+#define to_k1_pcie(dw_pcie)	dev_get_drvdata((dw_pcie)->dev)
+
+static int k1_pcie_toggle_soft_reset(struct k1_pcie *k1)
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 73

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+{
+	u32 offset = k1->pmu_off + PCIE_CONTROL_LOGIC;
+	const u32 mask = PCIE_SOFT_RESET;
+	int ret;
+
+	ret = regmap_set_bits(k1->pmu, offset, mask);

For MMIO, it is OK to skip the error handling.

You mean even though the regmap API returns an error, it never will with MMIO?
- regmap_mmio_read() and regmap_mmio_write() always return 0 unless there's an error enabling its clock.

Sounds good, I'll simplify places that use this.

Quoted 9 lines

drivers/pci/controller/dwc/pcie-k1.c line 77

Quoted 4 lines
drivers/pci/controller/dwc/pcie-k1.c new file +4 -0
+	if (ret)
+		return ret;
+
+	mdelay(2);

If the previous write to the PMU got stuck in the CPU cache, there is no guarantee that this delay of 2ms between write and clear will be enforced. So you should do a dummy read after write to ensure that the previous write has reached the PMU (or any device) and then clear the bits.

Quoted 3 lines

Wow, really? I was aware of this being possible for I/O writes but it seems like something regmap might handle.

API cannot handle this scenario since the callers do not necessarily need to ensure that the write has reached the device all the time. It is only needed for special cases where we need to wait for some hardware recommended time, before triggering another write/read.

Quoted 47 lines

I'll add a regmap_read() for the same offset and discard the result *before* the delay. I'll do the same for this:

        mdelay(PCIE_T_PVPERL_MS);
Quoted 6 lines
Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	return regmap_clear_bits(k1->pmu, offset, mask);
+}
+
+/* Enable app clocks, deassert app resets */
+static int k1_pcie_app_enable(struct k1_pcie *k1)
+{
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 90

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	struct dw_pcie *pci = &k1->pci;
+	u32 clock_count;
+	u32 reset_count;
+	int ret;
+
+	clock_count = ARRAY_SIZE(pci->app_clks);

Just use ARRAY_SIZE() directly below.

OK.

Quoted 7 lines

drivers/pci/controller/dwc/pcie-k1.c line 95

Quoted 5 lines
drivers/pci/controller/dwc/pcie-k1.c new file +5 -0
+	ret = clk_bulk_prepare_enable(clock_count, pci->app_clks);
+	if (ret)
+		return ret;
+
+	reset_count = ARRAY_SIZE(pci->app_rsts);

Same here.

OK.

Quoted 6 lines
Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+	ret = reset_control_bulk_deassert(reset_count, pci->app_rsts);
+	if (ret)
+		goto err_disable_clks;
+
+	ret = reset_control_deassert(k1->global_reset);
+	if (ret)
Quoted 8 lines

drivers/pci/controller/dwc/pcie-k1.c line 107

Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+		goto err_assert_resets;
+
+	return 0;
+
+err_assert_resets:
+	(void)reset_control_bulk_assert(reset_count, pci->app_rsts);

Why void cast? Here and in other places.

Quoted 5 lines

I put void casts when I'm ignoring a returned value. It's not necessary, but it reminds me that the function returns a value, and at some point I decided to ignore it. I can drop those if you find them offensive.

Maybe it is my personal preference. To me, it is OK to ignore a return value for APIs that do not have the __must_check attribute. So if the attribute is not present and if there is a void cast to indicate that the return values are ignored, I don't think anyone would make use of it in the future. So it just adds noise IMO.

Quoted 3 lines

If you're suggesting I should issue a warning here if an error is returned here, tell me that.

Nah, it should be OK to ignore the return value of assert in the error path.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/pci/controller/dwc/pcie-k1.c new file +6 -0
+err_disable_clks:
+	clk_bulk_disable_unprepare(clock_count, pci->app_clks);
+
+	return ret;
+}
+

[...]

Quoted 10 lines
Quoted 9 lines

drivers/pci/controller/dwc/pcie-k1.c line 352

Quoted 7 lines
drivers/pci/controller/dwc/pcie-k1.c new file +7 -0
+static struct platform_driver k1_pcie_driver = {
+	.probe	= k1_pcie_probe,
+	.remove	= k1_pcie_remove,
+	.driver = {
+		.name			= "k1-dwc-pcie",
+		.of_match_table		= k1_pcie_of_match_table,
+		.suppress_bind_attrs	= true,

No need of this flag for the reason I mentioned in the Kcofig change.

Quoted 2 lines

Because this doesn't implement an irqchip?

Yeah. When the PCI controller driver is not implementing an irqchip, it can freely be unbinded/removed from the system. But you can find this suppress attribute to be set in various drivers, because all of them would be implementing INTx/MSI/MSI-X controller in the driver. So in that case, the irqchip subsystem doesn't guarantee that removing the irqchip controller will dispose all the IRQs requested by the clients (in this case, the PCI device drivers).

I think it would be better if I write a guide under Documentation/PCI on writing the PCI controller drivers and document all of these internal details.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
author
#29 lore raw

Re: [PATCH 5/6] PCI: spacemit: introduce SpacemiT PCIe host driver

In reply to Alex Elder
Quoted 32 lines · Alex Elder
Quoted 23 lines
Quoted 23 lines
drivers/pci/controller/dwc/pcie-k1.c new file +23 -0
+static int k1_pcie_init(struct dw_pcie_rp *pp)
+{
+    struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+    struct k1_pcie *k1 = to_k1_pcie(pci);
+    u32 offset;
+    u32 mask;
+    int ret;
+
+    ret = k1_pcie_toggle_soft_reset(k1);
+    if (ret)
+        goto err_app_disable;
+
+    ret = k1_pcie_app_enable(k1);
+    if (ret)
+        return ret;
+
+    ret = phy_init(k1->phy);
+    if (ret)
+        goto err_app_disable;
+
+    /* Set the PCI vendor and device ID */
+    dw_pcie_dbi_ro_wr_en(pci);
+    dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, K1_PCIE_VENDOR_ID);
Quoted 8 lines
Quoted 5 lines

+    dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, K1_PCIE_DEVICE_ID); +    dw_pcie_dbi_ro_wr_dis(pci);
+
+    /*
+     * Put the port in root complex mode, record that Vaux is present.

There is no 3.3Vaux supply present in the binding. So the supply is guaranteed to be present and enabled always by the platform?

Quoted 1 line

Actually, I don't know, I'll ask.  Thank you for pointing this out.

On the Banana Pi BPI-F3 platform, this supply is always on.

There do exist other (SpacemiT K1-based) platforms that enable this supply using a GPIO. I am not able to test that now.

However I will add a property in the DT binding to indicate the 3.3v supply. I see "vpcie3v3-supply" used as a property name and unless someone suggests doing something else, that's what I'll use.

-Alex

author
#30 lore raw

PATCH 6/6 riscv: dts: spacemit: PCIe and PHY-related updates

In reply to Alex Elder

Define PCIe and PHY-related Device Tree nodes for the SpacemiT K1 SoC.

Enable the combo PHY and the two PCIe-only PHYs on the Banana Pi BPI-F3 board. The combo PHY is used for USB on this board, and that will be enabled when USB 3 support is accepted.

---
 .../boot/dts/spacemit/k1-bananapi-f3.dts      |  28 +++
 arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi  |  33 ++++
 arch/riscv/boot/dts/spacemit/k1.dtsi          | 169 ++++++++++++++++++
 3 files changed, 230 insertions(+)
arch/riscv/boot/dts/spacemit/k1-bananapi-f3.dts +28 -0
index fe22c747c5012..1c75e38b1fab9 100644
@@ -40,6 +40,34 @@ &emmc {
 	status = "okay";
 };
 
+&combo_phy {
+	pinctrl-names = "default";
+	pinctrl-0 = <&pcie0_3_cfg>;
+	status = "okay";
+};
+
+&pcie1_phy {
+	pinctrl-names = "default";
+	pinctrl-0 = <&pcie1_3_cfg>;
+	status = "okay";
+};
+
+&pcie2_phy {
+	pinctrl-names = "default";
+	pinctrl-0 = <&pcie2_4_cfg>;
+	status = "okay";
+};
+
+&pcie1 {
+	phys = <&pcie1_phy>;
+	status = "okay";
+};
+
+&pcie2 {
+	phys = <&pcie2_phy>;
+	status = "okay";
+};
+
 &uart0 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&uart0_2_cfg>;
arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi +33 -0
index 3810557374228..e7dbecd7389b7 100644
@@ -21,6 +21,39 @@ uart0-2-pins {
 		};
 	};
 
+	pcie0_3_cfg: pcie0-3-cfg {
+		pcie0-3-pins {
+			pinmux = <K1_PADCONF(54, 3)>,	/* PERST# */
+				 <K1_PADCONF(55, 3)>,	/* WAKE */
+				 <K1_PADCONF(53, 3)>;	/* CLKREQ# */
+
+			bias-pull-up = <0>;
+			drive-strength = <21>;
+		};
+	};
+
+	pcie1_3_cfg: pcie1-3-cfg {
+		pcie1-3-pins {
+			pinmux = <K1_PADCONF(59, 4)>,	/* PERST# */
+				 <K1_PADCONF(60, 4)>,	/* WAKE */
+				 <K1_PADCONF(61, 4)>;	/* CLKREQ# */
+
+			bias-pull-up = <0>;
+			drive-strength = <21>;
+		};
+	};
+
+	pcie2_4_cfg: pcie2-4-cfg {
+		pcie2-4-pins {
+			pinmux = <K1_PADCONF(62, 4)>,	/* PERST# */
+				 <K1_PADCONF(112, 3)>,	/* WAKE */
+				 <K1_PADCONF(117, 4)>;	/* CLKREQ# */
+
+			bias-pull-up = <0>;
+			drive-strength = <21>;
+		};
+	};
+
 	pwm14_1_cfg: pwm14-1-cfg {
 		pwm14-1-pins {
 			pinmux = <K1_PADCONF(44, 4)>;
arch/riscv/boot/dts/spacemit/k1.dtsi +169 -0

187 lines not on this page · raw message

Signature · 1 line
2.48.1