Skip to contentSkip to conversation

Add UFS Host driver support for SpacemiT K3 SoC

PATCH Superseded Yixun Lan 4 participants 11 messages Sep 16 – Sep 21

Subsystems (5)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Yixun Lan

author maintainer
#0 lore raw

v5 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]

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 v5:
- adjust clock names
- add runtime power support
- collect tags
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- fix clock and reset settings
- drop function which retrieve value of tx_lanes
- propagate return error of ufs_spacemit_dme_set()
- check return value ufs_spacemit_check_hibern8()
- drop call to ufshcd_vops_phy_initialization()
- handle compiling warning if CONFIG_OF not enabled
add __maybe_unused to ufs_spacemit_of_match
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- collect tags
- 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()
- ufs clock driver is merged into v7.3-rc1, update cover letter
- 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                    | 688 +++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h                    |  79 +++
 8 files changed, 866 insertions(+)

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

Best regards,

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

PATCH v5 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..8512a81f1cc7
@@ -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: axi
+      - const: ref
+
+  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 = "axi", "ref";
+        resets = <&syscon_apmu RESET_APMU_UFS_ACLK>;
+        interrupts = <135 IRQ_TYPE_LEVEL_HIGH>;
+        lanes-per-direction = <2>;
+    };
Signature · 1 line
2.55.0
Reviewed-by on v5 1/3
#2 lore raw

Re: [PATCH v5 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 4 lines
---
 .../devicetree/bindings/ufs/spacemit,k3-ufshc.yaml | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)

Best regards,
Krzysztof

author maintainer
#3 lore raw

PATCH v5 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 | 688 ++++++++++++++++++++++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h |  79 +++++
 4 files changed, 780 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 7d8db67eb23c..a3c447b8fc21 100644
@@ -13,6 +13,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 +688 -0

688 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
#4 lore raw

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

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] scsi: ufs: missing M-PHY attributes configuration for lane 1 - [Low] scsi: ufs: dead code via…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] scsi: ufs: missing M-PHY attributes configuration for lane 1
- [Low] scsi: ufs: dead code via unused struct ufs_spacemit_host
- [Low] scsi: ufs: unused macros and magic numbers in M-PHY config
--

commit 82db4e922a94ed053811c2ea8440617bd6a94d08
Author: Yixun Lan <[email protected]>
Subject: scsi: ufs: spacemit: k3: Add UFS Host Controller driver

This commit adds the UFS Host Controller driver for the SpacemiT K3 SoC. The hardware features are compatible with JEDEC UFS 2.2, MIPI UniPro v1.61, and M-PHY v3.0 standards.

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.c new file +0 -0
index 0000000000000..179e6edb2ac25

[ ... ]

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

Quoted 7 lines
drivers/ufs/host/ufs-spacemit.c new file +7 -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 */
+		{ UIC_ARG_MIB(0x1553), 0xf }, /* PA_TXHSG1PREPARELENGTH */
+		{ UIC_ARG_MIB(0x1554), 0x4f }, /* PA_TXHSG2SYNCLENGTH */
+		{ UIC_ARG_MIB(0x1555), 0xf }, /* PA_TXHSG2PREPARELENGTH */

[Severity: Low]
Is there a reason the raw hex literals are used here instead of the macros like PA_TXHSG1SYNCLENGTH and PA_TXHSG1PREPARELENGTH that were explicitly defined at the top of the file?

[ ... ]

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

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -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 },
+		/* RX garbage cnt = 32 SI */

[Severity: High]
Should the TX EQ 3DB attribute also be configured for lane 1 here? The surrounding attributes (like TX/RX HB8_TIME CAP and RX garbage cnt) properly target both lanes 0 and 1.

If lane 1 is left asymmetrically configured, could this lead to PHY link instability, CRC errors, or failure to maintain high-speed gears in a 2-lane setup?

Quoted 6 lines
drivers/ufs/host/ufs-spacemit.c new file +6 -0
+		{ UIC_ARG_MIB_SEL(0x00F2, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x9F },
+		{ UIC_ARG_MIB_SEL(0x00F2, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x9F },
+	};
+
+	return ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+}

[ ... ]

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

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -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 },
+		{ UIC_ARG_MIB(DL_AFC0REQTIMEOUTVAL), UFS_DL_AFC0REQTIMEOUTVAL_MAX },
+	};

[Severity: High]
Does this workaround need to be applied to lane 1 as well? The inline comment explicitly documents this as a "2Lane" workaround, but the configuration is only applied to lane 0 via UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0).

[ ... ]

Quoted 8 lines
drivers/ufs/host/ufs-spacemit.c new file +8 -0
+static int ufs_spacemit_pwr_change_notify(struct ufs_hba *hba,
+					  enum ufs_notify_change_status status,
+					  struct ufs_pa_layer_attr *dev_req_params)
+{
+	struct ufs_spacemit_host *host = ufshcd_get_variant(hba);
+	int ret = 0;
+
+	if (!dev_req_params) {

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

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+		dev_err(hba->dev, "Invalid Parameters\n");
+		return -EINVAL;
+	}
+
+	switch (status) {
+	case PRE_CHANGE:
+		break;
+	case POST_CHANGE:
+		/* Cache the power mode parameters to use internally */
+		memcpy(&host->dev_req_params, dev_req_params, sizeof(*dev_req_params));

[Severity: Low]
Are the cached power mode parameters used elsewhere in the driver? We are copying dev_req_params into host->dev_req_params here, but the structure field doesn't appear to be read anywhere else.

[ ... ]

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

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -0
+static int ufs_spacemit_init(struct ufs_hba *hba)
+{
+	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;

[Severity: Low]
Since the ufs_spacemit_host structure is only written to and never read, do we need to allocate this memory during initialization, or is there some planned functionality missing that was supposed to consume it?

Quoted 2 lines
drivers/ufs/host/ufs-spacemit.c new file +2 -0
+
+	rst = devm_reset_control_get_exclusive_deasserted(dev, NULL);

[ ... ]

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.h new file +0 -0
index 0000000000000..5e459f6ba243c

[ ... ]

drivers/ufs/host/ufs-spacemit.h line 34

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.h new file +4 -0
+/* ATOP base*/
+#define UFS_ATOP_BASE			0x1C00
+
+#define UFS_SYSCLK			499

[Severity: Low]
Is this UFS_SYSCLK macro intended to be used? It doesn't appear to be referenced anywhere in the implementation.

Quoted 3 lines
drivers/ufs/host/ufs-spacemit.h new file +3 -0
+#define UFS_TX_SYMBO_CLK		0x800
+#define UFS_MAX_LINKSTARTUP_TIMER	0xFFFFFFFF
+#define UFS_DL_AFC0REQTIMEOUTVAL_MAX	0xFFFF

[ ... ]

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

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

Hi ALL,

Quoted 21 lines · On 03:10 Wed 16 Sep , [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] scsi: ufs: missing M-PHY attributes configuration for lane 1
- [Low] scsi: ufs: dead code via unused struct ufs_spacemit_host
- [Low] scsi: ufs: unused macros and magic numbers in M-PHY config
--

commit 82db4e922a94ed053811c2ea8440617bd6a94d08
Author: Yixun Lan <[email protected]>
Subject: scsi: ufs: spacemit: k3: Add UFS Host Controller driver

This commit adds the UFS Host Controller driver for the SpacemiT K3 SoC. The hardware features are compatible with JEDEC UFS 2.2, MIPI UniPro v1.61, and M-PHY v3.0 standards.

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.c new file +0 -0
index 0000000000000..179e6edb2ac25

[ ... ]

Quoted 13 lines

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

Quoted 7 lines
drivers/ufs/host/ufs-spacemit.c new file +7 -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 */
+		{ UIC_ARG_MIB(0x1553), 0xf }, /* PA_TXHSG1PREPARELENGTH */
+		{ UIC_ARG_MIB(0x1554), 0x4f }, /* PA_TXHSG2SYNCLENGTH */
+		{ UIC_ARG_MIB(0x1555), 0xf }, /* PA_TXHSG2PREPARELENGTH */

[Severity: Low]
Is there a reason the raw hex literals are used here instead of the macros like PA_TXHSG1SYNCLENGTH and PA_TXHSG1PREPARELENGTH that were explicitly defined at the top of the file?

I found there are some macros defined in incude/ufs/unipro.h already I will use them while leave others unchanged which not defined in global

I will also drop local macros from this drivers for now, it's probably better to promote them into global header, but I'd prefer to do it in a separate patch

Quoted 17 lines

[ ... ]

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

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -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 },
+		/* RX garbage cnt = 32 SI */

[Severity: High]
Should the TX EQ 3DB attribute also be configured for lane 1 here? The surrounding attributes (like TX/RX HB8_TIME CAP and RX garbage cnt) properly target both lanes 0 and 1.

Quoted 4 lines

If lane 1 is left asymmetrically configured, could this lead to PHY link instability, CRC errors, or failure to maintain high-speed gears in a 2-lane setup?

false report, the configuration will actually apply to lane 1 too, so ignore

Quoted 9 lines
Quoted 6 lines
drivers/ufs/host/ufs-spacemit.c new file +6 -0
+		{ UIC_ARG_MIB_SEL(0x00F2, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x9F },
+		{ UIC_ARG_MIB_SEL(0x00F2, UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x9F },
+	};
+
+	return ufs_spacemit_dme_set(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
+}

[ ... ]

Quoted 15 lines

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

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -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 },
+		{ UIC_ARG_MIB(DL_AFC0REQTIMEOUTVAL), UFS_DL_AFC0REQTIMEOUTVAL_MAX },
+	};

[Severity: High]
Does this workaround need to be applied to lane 1 as well? The inline comment explicitly documents this as a "2Lane" workaround, but the configuration is only applied to lane 0 via UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0).

ditto, wil ignore

Quoted 17 lines

[ ... ]

Quoted 15 lines
drivers/ufs/host/ufs-spacemit.c new file +15 -0
+static int ufs_spacemit_pwr_change_notify(struct ufs_hba *hba,
+					  enum ufs_notify_change_status status,
+					  struct ufs_pa_layer_attr *dev_req_params)
+{
+	struct ufs_spacemit_host *host = ufshcd_get_variant(hba);
+	int ret = 0;
+
+	if (!dev_req_params) {
+		dev_err(hba->dev, "Invalid Parameters\n");
+		return -EINVAL;
+	}
+
+	switch (status) {
+	case PRE_CHANGE:
+		break;
Quoted 9 lines

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

Quoted 3 lines
drivers/ufs/host/ufs-spacemit.c new file +3 -0
+	case POST_CHANGE:
+		/* Cache the power mode parameters to use internally */
+		memcpy(&host->dev_req_params, dev_req_params, sizeof(*dev_req_params));

[Severity: Low]
Are the cached power mode parameters used elsewhere in the driver? We are copying dev_req_params into host->dev_req_params here, but the structure field doesn't appear to be read anywhere else.

it's true, I will drop this cache parameters ..

Quoted 17 lines

[ ... ]

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

Quoted 9 lines
drivers/ufs/host/ufs-spacemit.c new file +9 -0
+static int ufs_spacemit_init(struct ufs_hba *hba)
+{
+	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;

[Severity: Low]
Since the ufs_spacemit_host structure is only written to and never read, do we need to allocate this memory during initialization, or is there some planned functionality missing that was supposed to consume it?

I think this due to massive cleanup from vendor driver, but I would keep it as is for now, for easy to implement vendor specific feature.

Quoted 12 lines
Quoted 2 lines
drivers/ufs/host/ufs-spacemit.c new file +2 -0
+
+	rst = devm_reset_control_get_exclusive_deasserted(dev, NULL);

[ ... ]

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.h new file +0 -0
index 0000000000000..5e459f6ba243c

[ ... ]

Quoted 9 lines

drivers/ufs/host/ufs-spacemit.h line 34

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.h new file +4 -0
+/* ATOP base*/
+#define UFS_ATOP_BASE			0x1C00
+
+#define UFS_SYSCLK			499

[Severity: Low]
Is this UFS_SYSCLK macro intended to be used? It doesn't appear to be referenced anywhere in the implementation.

will drop it

Quoted 8 lines
Quoted 3 lines
drivers/ufs/host/ufs-spacemit.h new file +3 -0
+#define UFS_TX_SYMBO_CLK		0x800
+#define UFS_MAX_LINKSTARTUP_TIMER	0xFFFFFFFF
+#define UFS_DL_AFC0REQTIMEOUTVAL_MAX	0xFFFF

[ ... ]

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

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

In reply to Yixun Lan

Hi Yixun,

Quoted 101 lines · 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 | 688 ++++++++++++++++++++++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h |  79 +++++
 4 files changed, 780 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 7d8db67eb23c..a3c447b8fc21 100644
@@ -13,6 +13,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 +46 -0
new file mode 100644
index 000000000000..179e6edb2ac2
@@ -0,0 +1,688 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2026 SpacemiT (Hangzhou) Technology Co. Ltd
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/slab.h>
+
+#include <ufs/ufshcd.h>
+#include <ufs/ufshci.h>
+#include <ufs/ufs_quirks.h>
+#include <ufs/unipro.h>
+
+#include "ufshcd-pltfrm.h"
+#include "ufs-spacemit.h"
+
+/* PA Layer Gettable and settable M-PHY Specific Attributes */
+#define PA_TXHSG1SYNCLENGTH		0x1552
+#define PA_TXHSG1PREPARELENGTH		0x1553
+#define PA_TXHSG2SYNCLENGTH		0x1554
+#define PA_TXHSG2PREPARELENGTH		0x1555
+#define PA_TXHSG3SYNCLENGTH		0x1556
+#define PA_TXHSG3PREPARELENGTH		0x1557
+#define PA_TXMK2EXTENSION		0x155A
+#define PA_PEERSCRAMBLING		0x155B
+#define PA_TXSKIP			0x155C
+#define PA_TXSKIPPERIOD			0x155D
+#define PA_PEER_TX_LCC_ENABLE		0x155F
+
+#define PA_SCRAMBLING			0x1585
+#define PA_MK2EXTENSIONGUARDBAND	0x15AB
+
+/* Special TX/RX Configuration Attributes */
+#define RX_LS_PRE_LEN_CAP		0x008D
+#define RX_LANE_HB8_BKDOOR_ATTR		0x00F4
+#define RX_PWRM_CLOSURE_LEN_CAP		0x008E
+#define RX_MIN_STALL_CAP		0x0088
+#define RX_LANE_SOF_BKDOOR_ATT		0x00F2
+#define RX_GARBAGE_COUNT_OFFSET		0x00F2

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

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+
+/* Special analog register */
+#define ANA_EQ_CTRL_REG_ATTR		0x00CD
+#define ANA_HSGEAR_CTRL_ATTR		0x00C1
+
+/*
+ * Keep UFS ACLK at a lower parent rate (409.6MHz) for stable init.
+ * This mirrors the "ufs-low-aclk-freq" change from the other environment.
+ */
+#define UFS_ACLK_LOW_FREQ_HZ		409600000UL

You probably want to rename that to AXI, although here it's not that important.

Quoted 78 lines
drivers/ufs/host/ufs-spacemit.c new file +78 -0
+
+/* PHY register magic values */
+#define MPHY_PU_ALL			0x87f
+#define MPHY_PU_WITH_HB8_RESET		0xb7f
+#define MPHY_DEVICE_REFCLK_OE		BIT(8)
+#define MPHY_DEVICE_HW_RESET		BIT(0)
+
+#define MPHY_PLL_LOCK_BIT		BIT(31)
+#define MPHY_PLL_LOCK_TIMEOUT_US	10000
+
+/* FSM states */
+#define FSM_STATE_HIBERN8		0x1
+#define FSM_STATE_ACTIVE		0x3
+#define FSM_STATE_LS_BURST		0x5
+
+#define VENDOR_DUMP_BUF_SIZE		2048
+#define VENDOR_MAX_OFFSET		0xE0
+
+/* M-PHY FSM states */
+#define MPHY_RX_FSM_STATE	0xC1
+#define MPHY_TX_FSM_STATE	0x41
+
+static int ufs_spacemit_dme_set(struct ufs_hba *hba,
+				const struct ufshcd_dme_attr_val *v, int n)
+{
+	int ret = 0;
+	int attr_node = 0;
+
+	for (attr_node = 0; attr_node < n; attr_node++) {
+		ret = ufshcd_dme_set(hba, v[attr_node].attr_sel,
+				     v[attr_node].mib_val);
+
+		if (ret)
+			return ret;
+	}
+
+	return 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,
+						     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;
+
+		usleep_range(100, 200);
+	} while (--retries > 0);
+
+	if (err) {
+		dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
+			__func__, err);
+	} else if (tx_fsm_val_0 != TX_FSM_HIBERN8 ||
+		   tx_fsm_val_1 != TX_FSM_HIBERN8) {
+		err = -ETIMEDOUT;
+		dev_err(hba->dev,
+			"%s: invalid TX_FSM_STATE, lane0 = %u, lane1 = %u\n",
+			__func__, tx_fsm_val_0, tx_fsm_val_1);
+	}
+
+	return err;
+}

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

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+
+static u32 ufs_spacemit_get_sys1clk_1us(struct ufs_hba *hba)
+{
+	struct ufs_clk_info *clki, *ufs_aclk = NULL;
+	struct list_head *head = &hba->clk_list_head;
+	unsigned long rate_hz = 0;
+
+	if (!list_empty(head)) {
+		list_for_each_entry(clki, head, list) {
+			if (clki->name && !strcmp(clki->name, "aclk") && clki->clk) {

On the other hand, you definitely want to rename this one to "axi" to match the device tree change. Right now the whole patchset just doesn't work.

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.c new file +4 -0
+				ufs_aclk = clki;
+				break;
+			}
+		}

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

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+	}
+
+	if (ufs_aclk && ufs_aclk->clk)
+		rate_hz = clk_get_rate(ufs_aclk->clk);
+
+	if (!rate_hz)
+		return 0;
+
+	return DIV_ROUND_CLOSEST(rate_hz, 1000000);
+}

Regards
Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
author maintainer
#7 lore raw

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

Hi Aurelien,

Quoted 64 lines · Aurelien Jarno

Hi Yixun,

Quoted 61 lines · 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 | 688 ++++++++++++++++++++++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h |  79 +++++
 4 files changed, 780 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 7d8db67eb23c..a3c447b8fc21 100644
@@ -13,6 +13,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 +6 -0
new file mode 100644
index 000000000000..179e6edb2ac2
@@ -0,0 +1,688 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2026 SpacemiT (Hangzhou) Technology Co. Ltd
+ */
+
+

...

Quoted 9 lines

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

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+/*
+ * Keep UFS ACLK at a lower parent rate (409.6MHz) for stable init.
+ * This mirrors the "ufs-low-aclk-freq" change from the other environment.
+ */
+#define UFS_ACLK_LOW_FREQ_HZ		409600000UL

You probably want to rename that to AXI, although here it's not that important.

Ok, will do

...

Quoted 5 lines
Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+static u32 ufs_spacemit_get_sys1clk_1us(struct ufs_hba *hba)
+{
+	struct ufs_clk_info *clki, *ufs_aclk = NULL;
+	struct list_head *head = &hba->clk_list_head;
+	unsigned long rate_hz = 0;
Quoted 9 lines

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

Quoted 4 lines
drivers/ufs/host/ufs-spacemit.c new file +4 -0
+
+	if (!list_empty(head)) {
+		list_for_each_entry(clki, head, list) {
+			if (clki->name && !strcmp(clki->name, "aclk") && clki->clk) {

On the other hand, you definitely want to rename this one to "axi" to match the device tree change. Right now the whole patchset just doesn't work.

Oops, you right, will fix it

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

PATCH v5 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 37627963a649..3251ee8405de 100644
@@ -220,3 +220,7 @@ &uart0 {
 	pinctrl-0 = <&uart0_0_cfg>;
 	status = "okay";
 };
+
+&ufshc {
+	status = "okay";
+};
arch/riscv/boot/dts/spacemit/k3-pico-itx.dts +4 -0
index b084a35aa00e..6369602e81da 100644
@@ -307,6 +307,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 c3f2dce0969c..436e98f1a17b 100644
@@ -513,6 +513,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 = "axi", "ref";
+			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
#9 lore raw

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

Quoted 11 lines · 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(+)

Please do not combine DTS in UFS patchset, because it might end up taken by SCSI/UFS folks and it should go via soc tree instead.

Best regards,
Krzysztof

author maintainer
#10 lore raw

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

Hi Krzysztof,

Quoted 13 lines · Krzysztof Kozlowski
Quoted 11 lines · 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(+)
Quoted 3 lines

Please do not combine DTS in UFS patchset, because it might end up taken by SCSI/UFS folks and it should go via soc tree instead.

Ok, will do in next version and send DT in separate series

Signature · 1 line
Yixun Lan (dlan)