Skip to contentSkip to conversation

Add UFS Host driver support for SpacemiT K3 SoC

PATCH Superseded Yixun Lan 5 participants 13 messages Aug 6 – Sep 8

Subsystems (5)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Yixun Lan

author maintainer
#0 lore raw

v3 cover Add UFS Host driver support for SpacemiT K3 SoC

This series try to add UFS support for SpacemiT K3 SoC, the controller components consists of System Bus Interface Unit, UFS Host Controller Interface, UFS Transport Protocol Layer, UFS Host Registers, Device Management Entity (DME), Transport Layer, Network Layer, Data Link Layer, PHY Adapter Layer, and M-PHY Interface. A more detail functional block diagram can be found in SpacemiT website, chapter 9.7.3 [1]

Please note, in order to test this driver, the UFS clock driver[2] here should be applied first as a prerequisite patch.

One known issue is that the device will occasionally raise BKOPS interrupt when doing some high load test, log from dmesg shows

[  806.710763] ufshcd-spacemit c0e00000.ufshc: ufshcd_bkops_exception_event_handler: device raised urgent BKOPS exception for bkops status 1

---
Changes in v3:
- fix dt-binding warning reported-by sashiko
- fix low freq in opp-2 table
- fix err checking in check_hibern8()
- propagate err code in link_startup_notify()
- update documents for event_notify()
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- fix missing interrupts property
- move ufs node to k3-com260.dtsi
- convert freq-table-hz to operating-points-v2
- rework reset_control, fix header and drop global variable
- drop dump_host_regs() debug interface
- converge and refactor dme_set() operations
- massive reduce of busy delay() loops
- drop vendor specific hce_enable_notify(), fallback to default
- drop unused member from struct ufs_spacemit_host
- add COMPILE_TEST support to Kconfig
- comment style fix
- Link to v1: https://patch.msgid.link/[email protected]

---
Yixun Lan (3):
scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC
scsi: ufs: spacemit: k3: Add UFS Host Controller driver
riscv: dts: spacemit: k3: Add UFS support

 .../devicetree/bindings/ufs/spacemit,k3-ufshc.yaml |  56 ++
 arch/riscv/boot/dts/spacemit/k3-com260.dtsi        |   4 +
 arch/riscv/boot/dts/spacemit/k3-pico-itx.dts       |   4 +
 arch/riscv/boot/dts/spacemit/k3.dtsi               |  22 +
 drivers/ufs/host/Kconfig                           |  12 +
 drivers/ufs/host/Makefile                          |   1 +
 drivers/ufs/host/ufs-spacemit.c                    | 687 +++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h                    |  79 +++
 8 files changed, 865 insertions(+)

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260605-08-k3-ufs-support-c8b308e415e2

Best regards,

Signature · 1 line
Yixun Lan <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/3 scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC

Document the compatible for UFS (Universal Flash Storage) Host Controller which found in SpacemiT K3 SoC chip. Its features are listed below:

- Compliant with MIPI UniPro v1.61 specification.
- Compliant with M-PHY v3.0 specification.
- Compliant with UFS HCI v2.1 specification.
- Supports up to 2 Tx and 2 Rx lanes, up to HS-GEAR3 5.8 Gbps per lane.
- Supports standard low-power hibernate to reduce power consumption.

---
 .../devicetree/bindings/ufs/spacemit,k3-ufshc.yaml | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
Documentation/devicetree/bindings/ufs/spacemit,k3-ufshc.yaml new file +56 -0
new file mode 100644
index 000000000000..dda533c852e8
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/ufs/spacemit,k3-ufshc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K3 SoC UFS Host Controller
+
+maintainers:
+  - Yixun Lan <[email protected]>
+
+properties:
+  compatible:
+    const: spacemit,k3-ufshc
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 2
+
+  clock-names:
+    items:
+      - const: aclk
+      - const: ref_clk
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+
+allOf:
+  - $ref: ufs-common.yaml
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k3-clocks.h>
+    #include <dt-bindings/reset/spacemit,k3-resets.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    ufs@c0e00000 {
+        compatible = "spacemit,k3-ufshc";
+        reg = <0xc0e00000 0x40000>;
+        clocks = <&syscon_apmu CLK_APMU_UFS_ACLK>,
+                 <&syscon_apmu CLK_APMU_UFS_REFCLK>;
+        clock-names = "aclk", "ref_clk";
+        resets = <&syscon_apmu RESET_APMU_UFS_ACLK>;
+        interrupts = <135 IRQ_TYPE_LEVEL_HIGH>;
+        lanes-per-direction = <2>;
+    };
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v3 1/3] scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC

Quoted 9 lines · Yixun Lan

Document the compatible for UFS (Universal Flash Storage) Host Controller which found in SpacemiT K3 SoC chip. Its features are listed below:

- Compliant with MIPI UniPro v1.61 specification.
- Compliant with M-PHY v3.0 specification.
- Compliant with UFS HCI v2.1 specification.
- Supports up to 2 Tx and 2 Rx lanes, up to HS-GEAR3 5.8 Gbps per lane.
- Supports standard low-power hibernate to reduce power consumption.

Quoted 5 lines
---
 .../devicetree/bindings/ufs/spacemit,k3-ufshc.yaml | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)

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

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Lexical error: Documentation/devicetree/bindings/ufs/spacemit,k3-ufshc.example.dts:31.36-55 Unexpected 'CLK_APMU_UFS_REFCLK' FATAL ERROR: Syntax error parsing input tree
make[2]: *** [scripts/Makefile.dtbs:140: Documentation/devicetree/bindings/ufs/spacemit,k3-ufshc.example.dtb] Error 1 make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/builds/robherring/dt-review-ci/linux/Makefile:1669: dt_binding_check] Error 2
make: *** [Makefile:248: __sub-make] Error 2

doc reference errors (make refcheckdocs):

See https://patchwork.kernel.org/project/devicetree/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.

#3 lore raw

Re: [PATCH v3 1/3] scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC

In reply to Yixun Lan
Quoted 11 lines · Yixun Lan

Document the compatible for UFS (Universal Flash Storage) Host Controller which found in SpacemiT K3 SoC chip. Its features are listed below:

- Compliant with MIPI UniPro v1.61 specification.
- Compliant with M-PHY v3.0 specification.
- Compliant with UFS HCI v2.1 specification.
- Supports up to 2 Tx and 2 Rx lanes, up to HS-GEAR3 5.8 Gbps per lane.
- Supports standard low-power hibernate to reduce power consumption.

---

You keep having the same failure for every version, no? Never responded to it...

I'll drop from Patchwork.

Best regards,
Krzysztof

author maintainer
#4 lore raw

Re: [PATCH v3 1/3] scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC

Quoted 10 lines · Krzysztof Kozlowski
Quoted 9 lines · Yixun Lan

Document the compatible for UFS (Universal Flash Storage) Host Controller which found in SpacemiT K3 SoC chip. Its features are listed below:

- Compliant with MIPI UniPro v1.61 specification.
- Compliant with M-PHY v3.0 specification.
- Compliant with UFS HCI v2.1 specification.
- Supports up to 2 Tx and 2 Rx lanes, up to HS-GEAR3 5.8 Gbps per lane.
- Supports standard low-power hibernate to reduce power consumption.

Quoted 5 lines
Quoted 2 lines

---

You keep having the same failure for every version, no? Never responded to it...

sorry, I should respond in previous series

for the Unexpected 'CLK_APMU_UFS_REFCLK' error, it depend on UFS clock patch[1], also I've sent a PR to clock maintainer for inclusion to v7.3 [2], It should be solved after v7.3-rc1 if all things goes well

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

Re: [PATCH v3 1/3] scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC

Quoted 15 lines · Yixun Lan
Quoted 13 lines · Krzysztof Kozlowski
Quoted 11 lines · Yixun Lan

Document the compatible for UFS (Universal Flash Storage) Host Controller which found in SpacemiT K3 SoC chip. Its features are listed below:

- Compliant with MIPI UniPro v1.61 specification.
- Compliant with M-PHY v3.0 specification.
- Compliant with UFS HCI v2.1 specification.
- Supports up to 2 Tx and 2 Rx lanes, up to HS-GEAR3 5.8 Gbps per lane.
- Supports standard low-power hibernate to reduce power consumption.

---

Quoted 7 lines
Quoted 2 lines

You keep having the same failure for every version, no? Never responded to it...

sorry, I should respond in previous series

for the Unexpected 'CLK_APMU_UFS_REFCLK' error, it depend on UFS clock patch[1], also I've sent a PR to clock maintainer for inclusion to v7.3 [2], It should be solved after v7.3-rc1 if all things goes well

So this cannot be tested and applied via UFS tree. Please do not send new versions before dependency hits mainline (or decouple dependencies as usually we recommend in such case).

Best regards,
Krzysztof

author maintainer
#6 lore raw

PATCH v3 2/3 scsi: ufs: spacemit: k3: Add UFS Host Controller driver

In reply to Yixun Lan

SpacemiT K3 SoC consist of UFS (Universal Flash Storage) Host Controller which has features compatible with JEDEC UFS 2.2, MIPI UniPro v1.61 and M-PHY v3.0 standard.

---
 drivers/ufs/host/Kconfig        |  12 +
 drivers/ufs/host/Makefile       |   1 +
 drivers/ufs/host/ufs-spacemit.c | 687 ++++++++++++++++++++++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h |  79 +++++
 4 files changed, 779 insertions(+)
drivers/ufs/host/Kconfig +12 -0
index ff170c0b6da0..6146872fcecc 100644
@@ -132,6 +132,18 @@ config SCSI_UFS_VARIABLE_SG_ENTRY_SIZE
 	bool
 	default y if SCSI_UFS_EXYNOS && SCSI_UFS_CRYPTO
 
+config SCSI_UFS_SPACEMIT
+	tristate "SpacemiT UFS controller driver"
+	depends on SCSI_UFSHCD_PLATFORM && (ARCH_SPACEMIT || COMPILE_TEST)
+	help
+	  This selects the SpacemiT specific additions to UFSHCD platform driver.
+	  UFS host on SpacemiT needs some vendor specific configuration before
+	  accessing the hardware which includes PHY configuration and vendor
+	  specific registers.
+
+	  Select this if you have UFS controller on SpacemiT chipset.
+	  If unsure, say N.
+
 config SCSI_UFS_SPRD
 	tristate "Unisoc specific hooks to UFS controller platform driver"
 	depends on SCSI_UFSHCD_PLATFORM && (ARCH_SPRD || COMPILE_TEST)
drivers/ufs/host/Makefile +1 -0
index 65d8bb23ab7b..151bf43dfba7 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
 obj-$(CONFIG_SCSI_UFS_MEDIATEK) += ufs-mediatek.o
 obj-$(CONFIG_SCSI_UFS_RENESAS) += ufs-renesas.o
 obj-$(CONFIG_SCSI_UFS_ROCKCHIP) += ufs-rockchip.o
+obj-$(CONFIG_SCSI_UFS_SPACEMIT) += ufs-spacemit.o
 obj-$(CONFIG_SCSI_UFS_SPRD) += ufs-sprd.o
 obj-$(CONFIG_SCSI_UFS_TI_J721E) += ti-j721e-ufs.o
 obj-$(CONFIG_SCSI_UFS_AMD_VERSAL2) += ufs-amd-versal2.o ufshcd-dwc.o
drivers/ufs/host/ufs-spacemit.c new file +687 -0

687 lines not on this page · raw message

drivers/ufs/host/ufs-spacemit.h new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v3 2/3] scsi: ufs: spacemit: k3: Add UFS Host Controller driver

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] Vendor M-PHY TX attributes are only configured for TX Lane 0, neglecting Lane 1 in a 2-lane setup.…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Vendor M-PHY TX attributes are only configured for TX Lane 0, neglecting Lane 1 in a 2-lane setup.
- [High] Hardcoded dual-lane check in ufs_spacemit_check_hibern8 will fail and timeout on 1-lane UFS devices.
- [High] The UFS hardware reset pulse width is too short, violating the JEDEC UFS specification.
- [Medium] Misleading macro names and comments for device reset contradict hardware behavior.
- [Medium] Silently ignoring DME configuration errors.
- [Low] Usage of of_match_ptr causes build warnings when CONFIG_OF is disabled.
--

--- Patch [2]: [PATCH v3 2/3] scsi: ufs: spacemit: k3: Add UFS Host Controller driver --- commit a6bb8f511d306cddb9d21e41123fafdbcd63a6c9
Author: Yixun Lan <[email protected]>
Subject: scsi: ufs: spacemit: k3: Add UFS Host Controller driver

SpacemiT K3 SoC consist of UFS (Universal Flash Storage) Host Controller which has features compatible with JEDEC UFS 2.2, MIPI UniPro v1.61 and M-PHY v3.0 standard.

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +0 -0
new file mode 100644
index 0000000000000..94942ea27fb7d

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 62

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+/* PHY register magic values */
+#define MPHY_PU_ALL			0x87f
+#define MPHY_PU_WITH_HB8_RESET		0xb7f
+#define MPHY_DEVICE_RESET_DEASSERT	0x101
+#define MPHY_DEVICE_RESET_ASSERT	0x001

[Severity: Medium]
Is this macro name misleading? Looking at ufs_spacemit_device_reset(), it uses 0x000 to assert the reset and 0x101 to de-assert it. This means 0x001 actually disables the reference clock while keeping the reset de-asserted.

[ ... ]

Quoted 12 lines
drivers/ufs/host/ufs-spacemit.c new file +12 -0
+static int ufs_spacemit_check_hibern8(struct ufs_hba *hba)
+{
+	u32 tx_fsm_val_0 = 0;
+	u32 tx_fsm_val_1 = 0;
+	int retries = DIV_ROUND_UP(HBRN8_POLL_TOUT_MS * 1000, 100);
+	int err = 0;
+
+	do {
+		err = ufshcd_dme_get(hba,
+				     UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
+						     UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
+				     &tx_fsm_val_0);

drivers/ufs/host/ufs-spacemit.c line 116

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+		if (err)
+			break;
+
+		err = ufshcd_dme_get(hba,
+				     UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
+						     UIC_ARG_MPHY_TX_GEN_SEL_INDEX(1)),
+				     &tx_fsm_val_1);
+		if (err || (tx_fsm_val_0 == TX_FSM_HIBERN8 &&
+			    tx_fsm_val_1 == TX_FSM_HIBERN8))
+			break;

[Severity: High]
Will this hardcoded dual-lane check timeout on single-lane UFS devices?

If a 1-lane memory chip is connected, Lane 1 is inactive. Querying its state will either return a DME error or read a non-Hibern8 state, causing the loop to timeout after 100ms and break power management.

Could this check be updated to use the dynamically negotiated lane count from dev_req_params to know how many lanes to poll?

[ ... ]

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.c new file +4 -0
+static int ufs_spacemit_uniprov1p6_init(struct ufs_hba *hba)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB(0x1552), 0x4f }, /* PA_TXHSG1SYNCLENGTH */

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 270

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+		/* RX HB8_TIME CAP */
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x64 },
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x64 },
+		/* TX EQ 3DB */
+		{ UIC_ARG_MIB_SEL(0x00CD, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),  0x5 },

[Severity: High]
Should this TX equalization be applied to Lane 1 as well?

The host controller is configured for 2 lanes, but this setup attribute only targets UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0). Omitting tuning parameters for the second lane could cause asymmetric lane configurations or link instability.

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 340

Quoted 7 lines
drivers/ufs/host/ufs-spacemit.c new file +7 -0
+static int ufs_spacemit_link_startup_post_change(struct ufs_hba *hba)
+{
+	/* Add 0xe8 make UFS2.1 run GEAR3 + 2Lane@409M */
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x97 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0xd7 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x17 },

[Severity: High]
Does attribute 0xe8 need to be programmed for both lanes?

Similar to the TX equalization setup, this only programs Lane 0 despite the driver specifying dual-lane support.

[ ... ]

Quoted 3 lines
drivers/ufs/host/ufs-spacemit.c new file +3 -0
+static int ufs_spacemit_apply_dev_quirks(struct ufs_hba *hba)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 461

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -0
+	};
+
+	if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
+		ufs_spacemit_quirk_host_pa_saveconfigtime(hba);
+
+	if (hba->dev_info.wmanufacturerid == UFS_VENDOR_WDC)
+		hba->dev_quirks |= UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE;
+
+	ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));

[Severity: Medium]
Is it safe to ignore the return values of these DME configuration functions?

If ufs_spacemit_quirk_host_pa_saveconfigtime() or ufs_spacemit_dme_set() fail, the errors are not propagated. Could this allow initialization to proceed with an incorrectly configured M-PHY?

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 519

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+static int ufs_spacemit_device_reset(struct ufs_hba *hba)
+{
+	/* Stop device ref_clk & asserted ufs device reset */
+	ufshcd_writel(hba, 0x000, UFS_PHY_MNG_BASE + UFS_DEVICE_IO_CTRL);
+	usleep_range(10, 15);

[Severity: High]
Is this reset pulse width long enough?

The JEDEC UFS specification mandates that the host must maintain the RST_n signal low for a minimum of 1 ms. A 10 to 15 microsecond delay might cause the memory chip to fail to reset properly.

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 607

Quoted 11 lines
drivers/ufs/host/ufs-spacemit.c new file +11 -0
+static void ufs_spacemit_post_hibern8(struct ufs_hba *hba, enum uic_cmd_dme cmd)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x80 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x80 },
+		{ UIC_ARG_MIB(0xdd), 0x57 },
+		{ UIC_ARG_MIB(0xdd), 0xd7 },
+		{ UIC_ARG_MIB(0xe8), 0x57 },
+		{ UIC_ARG_MIB(0xe8), 0xd7 },

[Severity: High]
Are these attributes defaulting to Lane 0?

Using UIC_ARG_MIB() defaults to Lane 0. If these need to be applied to Lane 1 as well during post-Hibern8 configuration, they are currently missing.

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 620

Quoted 11 lines
drivers/ufs/host/ufs-spacemit.c new file +11 -0
+	if (cmd == UIC_CMD_DME_HIBER_ENTER) {
+		ufs_spacemit_check_hibern8(hba);
+
+		ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+
+		/* Power down M-PHY */
+		ufshcd_writel(hba, 0x0, UFS_PHY_MNG_BASE + UFS_MPHY_PU_CTRL);
+
+		/* Keep reference clock enabled, assert device reset */
+		ufshcd_writel(hba, MPHY_DEVICE_RESET_ASSERT,
+			      UFS_PHY_MNG_BASE + UFS_DEVICE_IO_CTRL);

[Severity: Medium]
Does this operation contradict its comment?

The code writes MPHY_DEVICE_RESET_ASSERT (0x001), which disables the reference clock but keeps the reset de-asserted. The comment says "Keep reference clock enabled, assert device reset", which is the exact opposite.

While keeping reset de-asserted is the correct action for Hibern8 (asserting reset would destroy the device's state), the comment and macro name suggest incorrect hardware behavior.

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 683

Quoted 8 lines
drivers/ufs/host/ufs-spacemit.c new file +8 -0
+static struct platform_driver ufs_spacemit_pltform = {
+	.probe	= ufs_spacemit_probe,
+	.remove	= ufs_spacemit_remove,
+	.driver	= {
+		.name	= "ufshcd-spacemit",
+		.of_match_table = of_match_ptr(ufs_spacemit_of_match),
+	},
+};

[Severity: Low]
Does the use of of_match_ptr() here cause a compiler warning?

Because ufs_spacemit_of_match is defined unconditionally, building with CONFIG_OF disabled will evaluate of_match_ptr() to NULL, leaving the array unused and triggering a -Wunused-variable warning.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#8 lore raw

Re: [PATCH v3 2/3] scsi: ufs: spacemit: k3: Add UFS Host Controller driver

Hi All,

Quoted 26 lines · On 03:10 Thu 06 Aug , [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Vendor M-PHY TX attributes are only configured for TX Lane 0, neglecting Lane 1 in a 2-lane setup.
- [High] Hardcoded dual-lane check in ufs_spacemit_check_hibern8 will fail and timeout on 1-lane UFS devices.
- [High] The UFS hardware reset pulse width is too short, violating the JEDEC UFS specification.
- [Medium] Misleading macro names and comments for device reset contradict hardware behavior.
- [Medium] Silently ignoring DME configuration errors.
- [Low] Usage of of_match_ptr causes build warnings when CONFIG_OF is disabled.
--

--- Patch [2]: [PATCH v3 2/3] scsi: ufs: spacemit: k3: Add UFS Host Controller driver --- commit a6bb8f511d306cddb9d21e41123fafdbcd63a6c9
Author: Yixun Lan <[email protected]>
Subject: scsi: ufs: spacemit: k3: Add UFS Host Controller driver

SpacemiT K3 SoC consist of UFS (Universal Flash Storage) Host Controller which has features compatible with JEDEC UFS 2.2, MIPI UniPro v1.61 and M-PHY v3.0 standard.

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +0 -0
new file mode 100644
index 0000000000000..94942ea27fb7d

[ ... ]

Quoted 11 lines

drivers/ufs/host/ufs-spacemit.c line 62

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+/* PHY register magic values */
+#define MPHY_PU_ALL			0x87f
+#define MPHY_PU_WITH_HB8_RESET		0xb7f
+#define MPHY_DEVICE_RESET_DEASSERT	0x101
+#define MPHY_DEVICE_RESET_ASSERT	0x001

[Severity: Medium]
Is this macro name misleading? Looking at ufs_spacemit_device_reset(), it uses 0x000 to assert the reset and 0x101 to de-assert it. This means 0x001 actually disables the reference clock while keeping the reset de-asserted.

Yes, will separate the definition of clock and reset, which something like below, and will adjust code accordingly

#define MPHY_DEVICE_REFCLK_OE BIT(8)
#define MPHY_DEVICE_HW_RESET BIT(0)

Quoted 19 lines

[ ... ]

Quoted 17 lines
drivers/ufs/host/ufs-spacemit.c new file +17 -0
+static int ufs_spacemit_check_hibern8(struct ufs_hba *hba)
+{
+	u32 tx_fsm_val_0 = 0;
+	u32 tx_fsm_val_1 = 0;
+	int retries = DIV_ROUND_UP(HBRN8_POLL_TOUT_MS * 1000, 100);
+	int err = 0;
+
+	do {
+		err = ufshcd_dme_get(hba,
+				     UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
+						     UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
+				     &tx_fsm_val_0);
+		if (err)
+			break;
+
+		err = ufshcd_dme_get(hba,
+				     UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
Quoted 9 lines

drivers/ufs/host/ufs-spacemit.c line 116

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+						     UIC_ARG_MPHY_TX_GEN_SEL_INDEX(1)),
+				     &tx_fsm_val_1);
+		if (err || (tx_fsm_val_0 == TX_FSM_HIBERN8 &&
+			    tx_fsm_val_1 == TX_FSM_HIBERN8))
+			break;

[Severity: High]
Will this hardcoded dual-lane check timeout on single-lane UFS devices?

it seems valid, we shouldn't check lane 1 if single-lane device connected..

Quoted 4 lines

If a 1-lane memory chip is connected, Lane 1 is inactive. Querying its state will either return a DME error or read a non-Hibern8 state, causing the loop to timeout after 100ms and break power management.

..

Quoted 3 lines

Could this check be updated to use the dynamically negotiated lane count from dev_req_params to know how many lanes to poll?

but, I need some suggestion from maintainer to see if I really need to go this way, currently there is no real single-lane device on K3 platform to test. I probably will leave this code unchanged for now..

Quoted 7 lines

[ ... ]

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.c new file +4 -0
+static int ufs_spacemit_uniprov1p6_init(struct ufs_hba *hba)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB(0x1552), 0x4f }, /* PA_TXHSG1SYNCLENGTH */

[ ... ]

Quoted 9 lines

drivers/ufs/host/ufs-spacemit.c line 270

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+		/* RX HB8_TIME CAP */
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x64 },
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x64 },
+		/* TX EQ 3DB */
+		{ UIC_ARG_MIB_SEL(0x00CD, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),  0x5 },

[Severity: High]
Should this TX equalization be applied to Lane 1 as well?

Not needed

Quoted 4 lines

The host controller is configured for 2 lanes, but this setup attribute only targets UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0). Omitting tuning parameters for the second lane could cause asymmetric lane configurations or link instability.

The vendor confirm the configuration will apply to both lanes

Quoted 13 lines

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 340

Quoted 7 lines
drivers/ufs/host/ufs-spacemit.c new file +7 -0
+static int ufs_spacemit_link_startup_post_change(struct ufs_hba *hba)
+{
+	/* Add 0xe8 make UFS2.1 run GEAR3 + 2Lane@409M */
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x97 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0xd7 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x17 },

[Severity: High]
Does attribute 0xe8 need to be programmed for both lanes?

No

Quoted 3 lines

Similar to the TX equalization setup, this only programs Lane 0 despite the driver specifying dual-lane support.

Similar reason, the settings will apply to 2 lanes, so no need to change

Quoted 19 lines

[ ... ]

Quoted 3 lines
drivers/ufs/host/ufs-spacemit.c new file +3 -0
+static int ufs_spacemit_apply_dev_quirks(struct ufs_hba *hba)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 461

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -0
+	};
+
+	if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
+		ufs_spacemit_quirk_host_pa_saveconfigtime(hba);
+
+	if (hba->dev_info.wmanufacturerid == UFS_VENDOR_WDC)
+		hba->dev_quirks |= UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE;
+
+	ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));

[Severity: Medium]
Is it safe to ignore the return values of these DME configuration functions?

Quoted 4 lines

If ufs_spacemit_quirk_host_pa_saveconfigtime() or ufs_spacemit_dme_set() fail, the errors are not propagated. Could this allow initialization to proceed with an incorrectly configured M-PHY?

ok, will fix this and propagate error code

Quoted 11 lines

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 519

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+static int ufs_spacemit_device_reset(struct ufs_hba *hba)
+{
+	/* Stop device ref_clk & asserted ufs device reset */
+	ufshcd_writel(hba, 0x000, UFS_PHY_MNG_BASE + UFS_DEVICE_IO_CTRL);
+	usleep_range(10, 15);

[Severity: High]
Is this reset pulse width long enough?

yes

Quoted 4 lines

The JEDEC UFS specification mandates that the host must maintain the RST_n signal low for a minimum of 1 ms. A 10 to 15 microsecond delay might cause the memory chip to fail to reset properly.

this is false report, minimum delay is 1us, give 10 to 15us is sufficient

Quoted 17 lines

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 607

Quoted 11 lines
drivers/ufs/host/ufs-spacemit.c new file +11 -0
+static void ufs_spacemit_post_hibern8(struct ufs_hba *hba, enum uic_cmd_dme cmd)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x80 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x80 },
+		{ UIC_ARG_MIB(0xdd), 0x57 },
+		{ UIC_ARG_MIB(0xdd), 0xd7 },
+		{ UIC_ARG_MIB(0xe8), 0x57 },
+		{ UIC_ARG_MIB(0xe8), 0xd7 },

[Severity: High]
Are these attributes defaulting to Lane 0?

No, will apply to 2 lanes

Quoted 3 lines

Using UIC_ARG_MIB() defaults to Lane 0. If these need to be applied to Lane 1 as well during post-Hibern8 configuration, they are currently missing.

It will apply to Lane 1 too, so the code here is correct

Quoted 17 lines

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 620

Quoted 11 lines
drivers/ufs/host/ufs-spacemit.c new file +11 -0
+	if (cmd == UIC_CMD_DME_HIBER_ENTER) {
+		ufs_spacemit_check_hibern8(hba);
+
+		ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+
+		/* Power down M-PHY */
+		ufshcd_writel(hba, 0x0, UFS_PHY_MNG_BASE + UFS_MPHY_PU_CTRL);
+
+		/* Keep reference clock enabled, assert device reset */
+		ufshcd_writel(hba, MPHY_DEVICE_RESET_ASSERT,
+			      UFS_PHY_MNG_BASE + UFS_DEVICE_IO_CTRL);

[Severity: Medium]
Does this operation contradict its comment?

Quoted 8 lines

The code writes MPHY_DEVICE_RESET_ASSERT (0x001), which disables the reference clock but keeps the reset de-asserted. The comment says "Keep reference clock enabled, assert device reset", which is the exact opposite.

While keeping reset de-asserted is the correct action for Hibern8 (asserting reset would destroy the device's state), the comment and macro name suggest incorrect hardware behavior.

will fix this

Quoted 14 lines

[ ... ]

drivers/ufs/host/ufs-spacemit.c line 683

Quoted 8 lines
drivers/ufs/host/ufs-spacemit.c new file +8 -0
+static struct platform_driver ufs_spacemit_pltform = {
+	.probe	= ufs_spacemit_probe,
+	.remove	= ufs_spacemit_remove,
+	.driver	= {
+		.name	= "ufshcd-spacemit",
+		.of_match_table = of_match_ptr(ufs_spacemit_of_match),
+	},
+};

[Severity: Low]
Does the use of of_match_ptr() here cause a compiler warning?

yes

Quoted 4 lines

Because ufs_spacemit_of_match is defined unconditionally, building with CONFIG_OF disabled will evaluate of_match_ptr() to NULL, leaving the array unused and triggering a -Wunused-variable warning.

will fix it, will add a __maybe_unused annotate

Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Signature · 1 line
Yixun Lan (dlan)
author maintainer
#9 lore raw

PATCH v3 3/3 riscv: dts: spacemit: k3: Add UFS support

In reply to Yixun Lan

Add UFS Host Controller support for SpacemiT K3 SoC, and enable it both on Pico-ITX and CoM260-IFX boards.

---
 arch/riscv/boot/dts/spacemit/k3-com260.dtsi  |  4 ++++
 arch/riscv/boot/dts/spacemit/k3-pico-itx.dts |  4 ++++
 arch/riscv/boot/dts/spacemit/k3.dtsi         | 22 ++++++++++++++++++++++
 3 files changed, 30 insertions(+)
arch/riscv/boot/dts/spacemit/k3-com260.dtsi +4 -0
index a38d7b738258..a68a55c8e73d 100644
@@ -188,3 +188,7 @@ &uart0 {
 	pinctrl-0 = <&uart0_0_cfg>;
 	status = "okay";
 };
+
+&ufshc {
+	status = "okay";
+};
arch/riscv/boot/dts/spacemit/k3-pico-itx.dts +4 -0
index b89c1521e664..eab8c430f2d1 100644
@@ -206,6 +206,10 @@ &uart0 {
 	status = "okay";
 };
 
+&ufshc {
+	status = "okay";
+};
+
 &usb2_host {
 	#address-cells = <1>;
 	#size-cells = <0>;
arch/riscv/boot/dts/spacemit/k3.dtsi +22 -0
index 19fc9b49668e..cee1569608ea 100644
@@ -469,6 +469,28 @@ usb2_phy: phy@c0a20000 {
 			status = "disabled";
 		};
 
+		ufshc: ufshc@c0e00000 {
+			compatible = "spacemit,k3-ufshc";
+			reg = <0x0 0xc0e00000 0x0 0x40000>;
+			clocks = <&syscon_apmu CLK_APMU_UFS_ACLK>,
+				 <&syscon_apmu CLK_APMU_UFS_REFCLK>;
+			clock-names = "aclk", "ref_clk";
+			resets = <&syscon_apmu RESET_APMU_UFS_ACLK>;
+			interrupts = <135 IRQ_TYPE_LEVEL_HIGH>;
+			lanes-per-direction = <2>;
+			operating-points-v2 = <&ufs_opp_table>;
+			status = "disabled";
+
+			ufs_opp_table: opp-table {
+				compatible = "operating-points-v2";
+
+				opp-1 {
+					opp-hz = /bits/ 64 <491520000>,
+						 /bits/ 64 <19200000>;
+				};
+			};
+		};
+
 		eth0: ethernet@cac80000 {
 			compatible = "spacemit,k3-dwmac", "snps,dwmac-5.40a";
 			reg = <0x0 0xcac80000 0x0 0x2000>;
Signature · 1 line
2.55.0
#10 lore raw

Re: [PATCH v3 3/3] riscv: dts: spacemit: k3: Add UFS support

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Failure to enter HIBERN8 state is ignored before forcibly powering down the M-PHY. - [High]…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Failure to enter HIBERN8 state is ignored before forcibly powering down the M-PHY.
- [High] Missing DME attributes configuration for TX lane 1 and inconsistent MIB selector usage.
- [Medium] The retrieved connected TX lanes value is silently discarded.
- [Low] Redundant and ineffective call to ufshcd_vops_phy_initialization.
- [Low] The device tree references an undefined clock macro CLK_APMU_UFS_REFCLK, which causes a Device Tree compilation failure. --

--- Patch [3]: [PATCH v3 3/3] riscv: dts: spacemit: k3: Add UFS support --- commit 7b2ecae3ba0ff920d65fce0475cce3fc5045f08d
Author: Yixun Lan <[email protected]>
Subject: riscv: dts: spacemit: k3: Add UFS support

Add UFS Host Controller support for SpacemiT K3 SoC, and enable it both on Pico-ITX and CoM260-IFX boards.

arch/riscv/boot/dts/spacemit/k3.dtsi usb2_phy: phy@c0a20000 { · line 476

Quoted 13 lines
arch/riscv/boot/dts/spacemit/k3.dtsi +5 -0
index 19fc9b49668ed..cee1569608ea2 100644
@@ -469,6 +469,28 @@ usb2_phy: phy@c0a20000 {
 			status = "disabled";
 		};
 
+		ufshc: ufshc@c0e00000 {
+			compatible = "spacemit,k3-ufshc";
+			reg = <0x0 0xc0e00000 0x0 0x40000>;
+			clocks = <&syscon_apmu CLK_APMU_UFS_ACLK>,
+				 <&syscon_apmu CLK_APMU_UFS_REFCLK>;

[Severity: Low]
Does this reference an undefined clock macro?

The device tree references CLK_APMU_UFS_REFCLK, but this macro appears to be undefined. Will this cause a compilation failure when building the device tree?

[ ... ]

Quoted 6 lines
drivers/ufs/host/ufs-spacemit.c new file +0 -0
new file mode 100644
index 0000000000000..94942ea27fb7d
@@ -0,0 +1,687 @@

[ ... ]

Quoted 3 lines
drivers/ufs/host/ufs-spacemit.c new file +3 -0
+static int ufs_spacemit_uniprov1p6_init(struct ufs_hba *hba)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {

[ ... ]

drivers/ufs/host/ufs-spacemit.c

Quoted 8 lines
drivers/ufs/host/ufs-spacemit.c new file +8 -0
+		/* TX HB8_TIME CAP */
+		{ UIC_ARG_MIB_SEL(0x000F, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x64 },
+		{ UIC_ARG_MIB_SEL(0x000F, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(1)), 0x64 },
+		/* RX HB8_TIME CAP */
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x64 },
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x64 },
+		/* TX EQ 3DB */
+		{ UIC_ARG_MIB_SEL(0x00CD, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),  0x5 },

[Severity: High]
Are we missing the TX EQ 3DB configuration for lane 1 here?

The device tree specifies lanes-per-direction = <2>, but this DME attribute is only configured for TX lane 0. Could this leave lane 1 with undefined or default parameters and prevent successful 2-lane initialization?

[ ... ]

drivers/ufs/host/ufs-spacemit.c

Quoted 7 lines
drivers/ufs/host/ufs-spacemit.c new file +7 -0
+static int ufs_spacemit_link_startup_post_change(struct ufs_hba *hba)
+{
+	/* Add 0xe8 make UFS2.1 run GEAR3 + 2Lane@409M */
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x97 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0xd7 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x17 },

[Severity: High]
Is the DME configuration for TX lane 1 missing?

Similar to ufs_spacemit_uniprov1p6_init(), attribute 0xe8 is explicitly targeting only lane 0. Does lane 1 also need to be configured for 2-lane mode?

drivers/ufs/host/ufs-spacemit.c

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+		{ UIC_ARG_MIB(DL_AFC0REQTIMEOUTVAL), UFS_DL_AFC0REQTIMEOUTVAL_MAX },
+	};
+	u32 tx_lanes;
+	int err;
+
+	err = ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+	if (err < 0)
+		return err;
+
+	return ufs_spacemit_get_connected_tx_lanes(hba, &tx_lanes);

[Severity: Medium]
Is the retrieved tx_lanes value meant to be used?

The value of tx_lanes is populated by ufs_spacemit_get_connected_tx_lanes() but is then silently discarded when returning. Should this value be verified against the configuration?

[ ... ]

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+static void ufs_spacemit_post_hibern8(struct ufs_hba *hba, enum uic_cmd_dme cmd)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x80 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x80 },
+		{ UIC_ARG_MIB(0xdd), 0x57 },
+		{ UIC_ARG_MIB(0xdd), 0xd7 },
+		{ UIC_ARG_MIB(0xe8), 0x57 },

drivers/ufs/host/ufs-spacemit.c

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+		{ UIC_ARG_MIB(0xe8), 0xd7 },
+	};
+
+	if (cmd == UIC_CMD_DME_HIBER_ENTER) {
+		ufs_spacemit_check_hibern8(hba);
+
+		ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+
+		/* Power down M-PHY */
+		ufshcd_writel(hba, 0x0, UFS_PHY_MNG_BASE + UFS_MPHY_PU_CTRL);

[Severity: High]
Should we check the return value of ufs_spacemit_check_hibern8(hba) before powering down the M-PHY?

If the HIBERN8 check times out and returns -ETIMEDOUT, the error is ignored and the driver unconditionally powers down the PHY. Can this cause a bus lockup or hardware error if the M-PHY isn't actually in the HIBERN8 state?

[ ... ]

Quoted 12 lines
drivers/ufs/host/ufs-spacemit.c new file +12 -0
+static int ufs_spacemit_init(struct ufs_hba *hba)
+{
+	int err = 0;
+	struct device *dev = hba->dev;
+	struct ufs_spacemit_host *host;
+	struct reset_control *rst;
+
+	host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
+	if (!host)
+		return -ENOMEM;
+
+	rst = devm_reset_control_get_exclusive_deasserted(dev, NULL);

drivers/ufs/host/ufs-spacemit.c

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+	if (IS_ERR(rst))
+		return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset control\n");
+
+	host->hba = hba;
+	ufshcd_set_variant(hba, host);
+	ufs_spacemit_advertise_quirks(hba);
+
+	err = ufshcd_vops_phy_initialization(host->hba);
+	return err;
+}

[Severity: Low]
Is this call to ufshcd_vops_phy_initialization() effective?

ufs_hba_spacemit_vops does not define the phy_initialization callback, so the wrapper function will silently return 0. Does this need to be called here, or is it redundant since the real PHY initialization occurs in ufs_spacemit_link_startup_pre_change()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#11 lore raw

Re: [PATCH v3 3/3] riscv: dts: spacemit: k3: Add UFS support

Hi All,

Quoted 33 lines · On 03:12 Thu 06 Aug , [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Failure to enter HIBERN8 state is ignored before forcibly powering down the M-PHY.
- [High] Missing DME attributes configuration for TX lane 1 and inconsistent MIB selector usage.
- [Medium] The retrieved connected TX lanes value is silently discarded.
- [Low] Redundant and ineffective call to ufshcd_vops_phy_initialization.
- [Low] The device tree references an undefined clock macro CLK_APMU_UFS_REFCLK, which causes a Device Tree compilation failure. --

--- Patch [3]: [PATCH v3 3/3] riscv: dts: spacemit: k3: Add UFS support --- commit 7b2ecae3ba0ff920d65fce0475cce3fc5045f08d
Author: Yixun Lan <[email protected]>
Subject: riscv: dts: spacemit: k3: Add UFS support

Add UFS Host Controller support for SpacemiT K3 SoC, and enable it both on Pico-ITX and CoM260-IFX boards.

arch/riscv/boot/dts/spacemit/k3.dtsi usb2_phy: phy@c0a20000 { · line 476

Quoted 13 lines
arch/riscv/boot/dts/spacemit/k3.dtsi +5 -0
index 19fc9b49668ed..cee1569608ea2 100644
@@ -469,6 +469,28 @@ usb2_phy: phy@c0a20000 {
 			status = "disabled";
 		};
 
+		ufshc: ufshc@c0e00000 {
+			compatible = "spacemit,k3-ufshc";
+			reg = <0x0 0xc0e00000 0x0 0x40000>;
+			clocks = <&syscon_apmu CLK_APMU_UFS_ACLK>,
+				 <&syscon_apmu CLK_APMU_UFS_REFCLK>;

[Severity: Low]
Does this reference an undefined clock macro?

Quoted 3 lines

The device tree references CLK_APMU_UFS_REFCLK, but this macro appears to be undefined. Will this cause a compilation failure when building the device tree?

this should be solved in v7.3-rc1, which ufs clock driver has been merged

Quoted 29 lines

[ ... ]

Quoted 6 lines
drivers/ufs/host/ufs-spacemit.c new file +0 -0
new file mode 100644
index 0000000000000..94942ea27fb7d
@@ -0,0 +1,687 @@

[ ... ]

Quoted 3 lines
drivers/ufs/host/ufs-spacemit.c new file +3 -0
+static int ufs_spacemit_uniprov1p6_init(struct ufs_hba *hba)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {

[ ... ]

drivers/ufs/host/ufs-spacemit.c

Quoted 8 lines
drivers/ufs/host/ufs-spacemit.c new file +8 -0
+		/* TX HB8_TIME CAP */
+		{ UIC_ARG_MIB_SEL(0x000F, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x64 },
+		{ UIC_ARG_MIB_SEL(0x000F, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(1)), 0x64 },
+		/* RX HB8_TIME CAP */
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x64 },
+		{ UIC_ARG_MIB_SEL(0x0092, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x64 },
+		/* TX EQ 3DB */
+		{ UIC_ARG_MIB_SEL(0x00CD, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),  0x5 },

[Severity: High]
Are we missing the TX EQ 3DB configuration for lane 1 here?

Quoted 4 lines

The device tree specifies lanes-per-direction = <2>, but this DME attribute is only configured for TX lane 0. Could this leave lane 1 with undefined or default parameters and prevent successful 2-lane initialization?

No need, false report..
The configuration ally to lane 1 too

Quoted 13 lines

[ ... ]

drivers/ufs/host/ufs-spacemit.c

Quoted 7 lines
drivers/ufs/host/ufs-spacemit.c new file +7 -0
+static int ufs_spacemit_link_startup_post_change(struct ufs_hba *hba)
+{
+	/* Add 0xe8 make UFS2.1 run GEAR3 + 2Lane@409M */
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x97 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0xd7 },
+		{ UIC_ARG_MIB_SEL(0xe8, UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 0x17 },

[Severity: High]
Is the DME configuration for TX lane 1 missing?

Quoted 3 lines

Similar to ufs_spacemit_uniprov1p6_init(), attribute 0xe8 is explicitly targeting only lane 0. Does lane 1 also need to be configured for 2-lane mode?

no need, false report
same reason as above

Quoted 14 lines

drivers/ufs/host/ufs-spacemit.c

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+		{ UIC_ARG_MIB(DL_AFC0REQTIMEOUTVAL), UFS_DL_AFC0REQTIMEOUTVAL_MAX },
+	};
+	u32 tx_lanes;
+	int err;
+
+	err = ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+	if (err < 0)
+		return err;
+
+	return ufs_spacemit_get_connected_tx_lanes(hba, &tx_lanes);

[Severity: Medium]
Is the retrieved tx_lanes value meant to be used?

Quoted 4 lines

The value of tx_lanes is populated by ufs_spacemit_get_connected_tx_lanes() but is then silently discarded when returning. Should this value be verified against the configuration?

tx_lanes is not used, I will drop this

Quoted 18 lines

[ ... ]

Quoted 16 lines
drivers/ufs/host/ufs-spacemit.c new file +16 -0
+static void ufs_spacemit_post_hibern8(struct ufs_hba *hba, enum uic_cmd_dme cmd)
+{
+	static const struct ufshcd_dme_attr_val setup_attrs[] = {
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x84 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x80 },
+		{ UIC_ARG_MIB_SEL(0xf1, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x80 },
+		{ UIC_ARG_MIB(0xdd), 0x57 },
+		{ UIC_ARG_MIB(0xdd), 0xd7 },
+		{ UIC_ARG_MIB(0xe8), 0x57 },
+		{ UIC_ARG_MIB(0xe8), 0xd7 },
+	};
+
+	if (cmd == UIC_CMD_DME_HIBER_ENTER) {
+		ufs_spacemit_check_hibern8(hba);
+
Quoted 9 lines

drivers/ufs/host/ufs-spacemit.c

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.c new file +4 -0
+		ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+
+		/* Power down M-PHY */
+		ufshcd_writel(hba, 0x0, UFS_PHY_MNG_BASE + UFS_MPHY_PU_CTRL);

[Severity: High]
Should we check the return value of ufs_spacemit_check_hibern8(hba) before powering down the M-PHY?

will check return value, and give a warning

Quoted 4 lines

If the HIBERN8 check times out and returns -ETIMEDOUT, the error is ignored and the driver unconditionally powers down the PHY. Can this cause a bus lockup or hardware error if the M-PHY isn't actually in the HIBERN8 state?

I'm not sure what's the best way to deal with this.. any suggestion?

Quoted 28 lines

[ ... ]

Quoted 12 lines
drivers/ufs/host/ufs-spacemit.c new file +12 -0
+static int ufs_spacemit_init(struct ufs_hba *hba)
+{
+	int err = 0;
+	struct device *dev = hba->dev;
+	struct ufs_spacemit_host *host;
+	struct reset_control *rst;
+
+	host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
+	if (!host)
+		return -ENOMEM;
+
+	rst = devm_reset_control_get_exclusive_deasserted(dev, NULL);

drivers/ufs/host/ufs-spacemit.c

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+	if (IS_ERR(rst))
+		return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset control\n");
+
+	host->hba = hba;
+	ufshcd_set_variant(hba, host);
+	ufs_spacemit_advertise_quirks(hba);
+
+	err = ufshcd_vops_phy_initialization(host->hba);
+	return err;
+}

[Severity: Low]
Is this call to ufshcd_vops_phy_initialization() effective?

Quoted 4 lines

ufs_hba_spacemit_vops does not define the phy_initialization callback, so the wrapper function will silently return 0. Does this need to be called here, or is it redundant since the real PHY initialization occurs in ufs_spacemit_link_startup_pre_change()?

Indeed, there is no phy_initialization(), I will drop this code

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Signature · 1 line
Yixun Lan (dlan)
Tested-by on the v3 series
#12 lore raw
In reply to Yixun Lan
Quoted 56 lines · Yixun Lan

This series try to add UFS support for SpacemiT K3 SoC, the controller components consists of System Bus Interface Unit, UFS Host Controller Interface, UFS Transport Protocol Layer, UFS Host Registers, Device Management Entity (DME), Transport Layer, Network Layer, Data Link Layer, PHY Adapter Layer, and M-PHY Interface. A more detail functional block diagram can be found in SpacemiT website, chapter 9.7.3 [1]

Please note, in order to test this driver, the UFS clock driver[2] here should be applied first as a prerequisite patch.

One known issue is that the device will occasionally raise BKOPS interrupt when doing some high load test, log from dmesg shows

[  806.710763] ufshcd-spacemit c0e00000.ufshc: ufshcd_bkops_exception_event_handler: device raised urgent BKOPS exception for bkops status 1

---
Changes in v3:
- fix dt-binding warning reported-by sashiko
- fix low freq in opp-2 table
- fix err checking in check_hibern8()
- propagate err code in link_startup_notify()
- update documents for event_notify()
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- fix missing interrupts property
- move ufs node to k3-com260.dtsi
- convert freq-table-hz to operating-points-v2
- rework reset_control, fix header and drop global variable
- drop dump_host_regs() debug interface
- converge and refactor dme_set() operations
- massive reduce of busy delay() loops
- drop vendor specific hce_enable_notify(), fallback to default
- drop unused member from struct ufs_spacemit_host
- add COMPILE_TEST support to Kconfig
- comment style fix
- Link to v1: https://patch.msgid.link/[email protected]

---
Yixun Lan (3):
scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC
scsi: ufs: spacemit: k3: Add UFS Host Controller driver
riscv: dts: spacemit: k3: Add UFS support

 .../devicetree/bindings/ufs/spacemit,k3-ufshc.yaml |  56 ++
 arch/riscv/boot/dts/spacemit/k3-com260.dtsi        |   4 +
 arch/riscv/boot/dts/spacemit/k3-pico-itx.dts       |   4 +
 arch/riscv/boot/dts/spacemit/k3.dtsi               |  22 +
 drivers/ufs/host/Kconfig                           |  12 +
 drivers/ufs/host/Makefile                          |   1 +
 drivers/ufs/host/ufs-spacemit.c                    | 687 +++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h                    |  79 +++
 8 files changed, 865 insertions(+)
Quoted 4 lines

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260605-08-k3-ufs-support-c8b308e415e2

Thanks for this new version, and no worries about not adding the previous Tested-by tags. I have also tested this version, so patches 2 and 3 are:

Regards
Aurelien

Signature · 7 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv