Skip to contentSkip to conversation

Add UFS Host driver support for SpacemiT K3 SoC

PATCH New Yixun Lan 2 participants 4 messages Nov 28

Subsystems (5)
lore
Share & tools
lore

Yixun Lan

author maintainer
#0 lore raw

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

The DTS patch has been dropped in this series [2], and will send in separate series once this patch accepted. For people who want to test UFS driver, please grab the DT patch from version v5 [3].

Enable UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND, This allows the device auto-bkops to be always enabled except during suspend (both runtime and suspend), with this feature, the BKOPS interrupt (log below) isn't reported anymore.

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

---
Changes in v9:
- return check of ufs_spacemit_dme_set()
- drop spacemit_ufs_set_dev_cap()
- log ->saved_err
- sort macro definition
- Link to v8: https://patch.msgid.link/[email protected]

Changes in v8:
- enable UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND
- rename to "ref_clk", see explanation in patch
- add delay between enable clk & deassert reset
- respect bRefClkGatingWaitTime, add wait delay
- drop comment of ufs_hba_spacemit_vops, which got CI complaint
- rebase to v7.4-rc1
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
- check err return value of ufs_spacemit_quirk_host_pa_saveconfigtime()
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
- drop unused macro and dev_req_params
- use global PX_xx macros
- fix axi clock name
- drop dts patch
- collect tags
- Link to v5: https://patch.msgid.link/[email protected]

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 (2):
scsi: ufs: spacemit: dt-bindings: Add UFS controller for K3 SoC
scsi: ufs: spacemit: k3: Add UFS Host Controller driver

 .../devicetree/bindings/ufs/spacemit,k3-ufshc.yaml |  56 ++
 drivers/ufs/host/Kconfig                           |  12 +
 drivers/ufs/host/Makefile                          |   1 +
 drivers/ufs/host/ufs-spacemit.c                    | 619 +++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h                    |  81 +++
 5 files changed, 769 insertions(+)

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

Best regards,

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

PATCH v9 1/2 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.

---
Changes in v8:
- rename to "ref_clk" clock
UFS core will search specific "ref_clk" clock, retrieve the frequency, and then set bRefClkFreq, see ufshcd_parse_dev_ref_clk_freq() and ufshcd_set_dev_ref_clk(). It will also set keep_link_active feature.

to Krzysztof, please note, I've kept the Reviewed-by in this version
even with clk name change, let me know if you have any objection.

---
 .../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..2d1b066b4187
@@ -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_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 = "axi", "ref_clk";
+        resets = <&syscon_apmu RESET_APMU_UFS_ACLK>;
+        interrupts = <135 IRQ_TYPE_LEVEL_HIGH>;
+        lanes-per-direction = <2>;
+    };
Signature · 1 line
2.56.0
author maintainer
#2 lore raw

PATCH v9 2/2 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 | 619 ++++++++++++++++++++++++++++++++++++++++
 drivers/ufs/host/ufs-spacemit.h |  81 ++++++
 4 files changed, 713 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 +619 -0

619 lines not on this page · raw message

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

81 lines not on this page · raw message

Signature · 1 line
2.56.0
#3 lore raw

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

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Low] scsi: ufs: spacemit: incomplete kernel-doc comments in ufs-spacemit.c - [Low] scsi: ufs: spacemit…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Low] scsi: ufs: spacemit: incomplete kernel-doc comments in ufs-spacemit.c
- [Low] scsi: ufs: spacemit: positive UIC error codes missed by negative err checks
- [Low] scsi: ufs: spacemit: unused variant struct, headers, macros, and duplicate tag
--

commit d1110f647e4d5741aaea4207a41324cfd65f105b
Author: Yixun Lan <[email protected]>

scsi: ufs: spacemit: k3: Add UFS Host Controller driver

This commit adds the UFS host controller driver for the SpacemiT K3 SoC, supporting UFS 2.2, MIPI UniPro v1.61, and M-PHY v3.0.

Quoted 6 lines
drivers/ufs/host/ufs-spacemit.c new file +0 -0
new file mode 100644
index 0000000000000..8024b2c1f4381
@@ -0,0 +1,619 @@

[ ... ]

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

Quoted 5 lines
drivers/ufs/host/ufs-spacemit.c new file +5 -0
+/**
+ * ufs_spacemit_mphy_init
+ * @hba: host controller instance
+ */
+static int ufs_spacemit_mphy_init(struct ufs_hba *hba)

[Severity: Low]
This kernel-doc comment is missing a short description after the function name and a Return: section, and ufs_spacemit_init() below is also missing a
Return: section, which causes W=1 and W=2 kernel-doc warnings.

[ ... ]

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

Quoted 12 lines
drivers/ufs/host/ufs-spacemit.c new file +12 -0
+static int ufs_spacemit_link_startup_pre_change(struct ufs_hba *hba)
+{
+	u32 value, sys1clk_1us;
+	int err;
+
+	err = ufs_spacemit_mphy_init(hba);
+	if (err < 0)
+		return err;
+
+	err = ufs_spacemit_uniprov1p6_init(hba);
+	if (err < 0)
+		return err;

[Severity: Low]
As reported by Troy Mitchell on v6 [1], ufshcd_dme_get() and ufshcd_dme_set() return positive 8-bit UIC command result codes (0x01..0xFF) when a DME command fails at the UniPro layer.

Checking "if (err < 0)" here and after ufs_spacemit_quirk_host_pa_saveconfigtime() in ufs_spacemit_apply_dev_quirks() misses those positive error codes and allows both functions to continue past failed DME operations instead of aborting early. In addition, ufs_spacemit_pwr_change_notify() ignores the return value of ufshcd_dme_set() in POST_CHANGE instead of returning it.

[ ... ]

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+/**
+ * ufs_spacemit_init - init phy and prepare clk
+ * @hba: host controller instance
+ */
+static int ufs_spacemit_init(struct ufs_hba *hba)
+{
+	struct device *dev = hba->dev;
+	struct ufs_spacemit_host *host;
+	struct reset_control *rst;
+

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

Quoted 10 lines
drivers/ufs/host/ufs-spacemit.c new file +10 -0
+	host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
+	if (!host)
+		return -ENOMEM;
+
+	rst = devm_reset_control_get_exclusive_deasserted(dev, NULL);
+	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);

[Severity: Low]
This isn't a bug, but struct ufs_spacemit_host is allocated and passed to ufshcd_set_variant() here without ever being retrieved via ufshcd_get_variant() anywhere in the driver.

Also, drivers/ufs/host/ufs-spacemit.h includes unused headers (linux/regulator/consumer.h, linux/pm_qos.h, and linux/workqueue.h) and defines 15 unused macros (UFS_LOCAL_PORT_ID_REG, UFS_PA_ERR_CODE, UFS_RETRY_TIMER_REG, UFS_CFG1, DEFAULT_CLK_RATE_HZ, BUS_VECTOR_NAME_LEN, UFS_HW_VER_*, UFS_DME_VS_CORE_CLK_CTRL, UFS_HCLKDIV_REG, and MPHY_RX_FSM_STATE), and the commit message has a duplicate Tested-by: tag for Aurelien Jarno.

[1] https://lore.kernel.org/r/[email protected]

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