From: Xingyu Wu <xingyu.wu@starfivetech.com>
To: Bjorn Andersson <andersson@kernel.org>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>
Cc: Xingyu Wu <xingyu.wu@starfivetech.com>,
Mason Huo <mason.huo@starfivetech.com>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-remoteproc@vger.kernel.org
Subject: [PATCH v1 2/2] hwspinlock: Add StarFive hwspinlock device
Date: Fri, 24 Apr 2026 11:20:26 +0800 [thread overview]
Message-ID: <20260424032026.62301-3-xingyu.wu@starfivetech.com> (raw)
In-Reply-To: <20260424032026.62301-1-xingyu.wu@starfivetech.com>
Add support of hardware spinlock for the StarFive JHB100 SoC.
The hwspinlock provides 16 channels for using by secur core and AP core.
Signed-off-by: Xingyu Wu <xingyu.wu@starfivetech.com>
---
MAINTAINERS | 6 ++
drivers/hwspinlock/Kconfig | 8 ++
drivers/hwspinlock/Makefile | 1 +
drivers/hwspinlock/starfive_hwspinlock.c | 130 +++++++++++++++++++++++
4 files changed, 145 insertions(+)
create mode 100644 drivers/hwspinlock/starfive_hwspinlock.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 7d10988cbc62..adf04852186c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25266,6 +25266,12 @@ F: Documentation/devicetree/bindings/power/starfive*
F: drivers/pmdomain/starfive/
F: include/dt-bindings/power/starfive,jh7110-pmu.h
+STARFIVE JHB100 HARDWARE SPINLOCK DRIVER
+M: Xingyu Wu <xingyu.wu@starfivetech.com>
+S: Supported
+F: Documentation/devicetree/bindings/hwlock/starfive,jhb100-hwspinlock.yaml
+F: drivers/hwspinlock/starfive_hwspinlock.c
+
STARFIVE SOC DRIVERS
M: Conor Dooley <conor@kernel.org>
S: Maintained
diff --git a/drivers/hwspinlock/Kconfig b/drivers/hwspinlock/Kconfig
index 3874d15b0e9b..797f5a833547 100644
--- a/drivers/hwspinlock/Kconfig
+++ b/drivers/hwspinlock/Kconfig
@@ -36,6 +36,14 @@ config HWSPINLOCK_SPRD
If unsure, say N.
+config HWSPINLOCK_STARFIVE
+ tristate "StarFive Hardware Spinlock device"
+ depends on ARCH_STARFIVE || COMPILE_TEST
+ help
+ Say y here to support the StarFive Hardware Spinlock device.
+
+ If unsure, say N.
+
config HWSPINLOCK_STM32
tristate "STM32 Hardware Spinlock device"
depends on MACH_STM32MP157 || COMPILE_TEST
diff --git a/drivers/hwspinlock/Makefile b/drivers/hwspinlock/Makefile
index a0f16c9aaa82..bc713bdc7e04 100644
--- a/drivers/hwspinlock/Makefile
+++ b/drivers/hwspinlock/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_HWSPINLOCK) += hwspinlock_core.o
obj-$(CONFIG_HWSPINLOCK_OMAP) += omap_hwspinlock.o
obj-$(CONFIG_HWSPINLOCK_QCOM) += qcom_hwspinlock.o
obj-$(CONFIG_HWSPINLOCK_SPRD) += sprd_hwspinlock.o
+obj-$(CONFIG_HWSPINLOCK_STARFIVE) += starfive_hwspinlock.o
obj-$(CONFIG_HWSPINLOCK_STM32) += stm32_hwspinlock.o
obj-$(CONFIG_HWSPINLOCK_SUN6I) += sun6i_hwspinlock.o
obj-$(CONFIG_HSEM_U8500) += u8500_hsem.o
diff --git a/drivers/hwspinlock/starfive_hwspinlock.c b/drivers/hwspinlock/starfive_hwspinlock.c
new file mode 100644
index 000000000000..d85f43a48013
--- /dev/null
+++ b/drivers/hwspinlock/starfive_hwspinlock.c
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hardware spinlock driver for StarFive JHB100 SoC
+ *
+ * Copyright (C) 2026 StarFive Technology Co., Ltd.
+ */
+
+#include <linux/delay.h>
+#include <linux/hwspinlock.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+
+#include "hwspinlock_internal.h"
+
+/* reg offset */
+#define STARFIVE_REG_APP_LOCK_REQ 0x08
+#define STARFIVE_REG_APP_LOCK_RLS 0x0C
+#define STARFIVE_REG_LOCK_STA 0x10
+
+/* macro STARFIVE_REG_LOCK_STA reg*/
+#define STARFIVE_STA_APP_OWN BIT(1)
+#define STARFIVE_STA_OWN_MSK 0x3
+
+#define STARFIVE_NUM_LOCKS 16
+
+struct starfive_hwspinlock {
+ void __iomem *base;
+ struct reset_control *rst;
+ struct hwspinlock_device bank;
+};
+
+static int starfive_hwspinlock_trylock(struct hwspinlock *lock)
+{
+ struct starfive_hwspinlock *priv = dev_get_drvdata(lock->bank->dev);
+ int id = hwlock_to_id(lock);
+ u32 status;
+
+ writel(BIT(id), priv->base + STARFIVE_REG_APP_LOCK_REQ);
+ status = (readl(priv->base + STARFIVE_REG_LOCK_STA) >> (2 * id)) &
+ STARFIVE_STA_OWN_MSK;
+
+ return (status == STARFIVE_STA_APP_OWN);
+}
+
+static void starfive_hwspinlock_unlock(struct hwspinlock *lock)
+{
+ struct starfive_hwspinlock *priv = dev_get_drvdata(lock->bank->dev);
+ int id = hwlock_to_id(lock);
+
+ writel(BIT(id), priv->base + STARFIVE_REG_APP_LOCK_RLS);
+}
+
+static void starfive_hwspinlock_relax(struct hwspinlock *lock)
+{
+ ndelay(50);
+}
+
+static const struct hwspinlock_ops starfive_hwspinlock_ops = {
+ .trylock = starfive_hwspinlock_trylock,
+ .unlock = starfive_hwspinlock_unlock,
+ .relax = starfive_hwspinlock_relax,
+};
+
+static void starfive_hwspinlock_disable(void *data)
+{
+ struct starfive_hwspinlock *priv = data;
+
+ reset_control_assert(priv->rst);
+}
+
+static int starfive_hwspinlock_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct starfive_hwspinlock *priv;
+ int ret;
+
+ priv = devm_kzalloc(dev, struct_size(priv, bank.lock, STARFIVE_NUM_LOCKS),
+ GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ priv->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(priv->base))
+ return PTR_ERR(priv->base);
+
+ priv->rst = devm_reset_control_array_get_exclusive(dev);
+ if (IS_ERR(priv->rst))
+ return dev_err_probe(dev, PTR_ERR(priv->rst),
+ "failed to get reset\n");
+
+ ret = reset_control_deassert(priv->rst);
+ if (ret)
+ return ret;
+
+ platform_set_drvdata(pdev, priv);
+
+ ret = devm_add_action_or_reset(dev, starfive_hwspinlock_disable, priv);
+ if (ret)
+ goto fail_action;
+
+ return devm_hwspin_lock_register(dev, &priv->bank, &starfive_hwspinlock_ops,
+ 0, STARFIVE_NUM_LOCKS);
+
+fail_action:
+ reset_control_assert(priv->rst);
+ return ret;
+}
+
+static const struct of_device_id starfive_hwpinlock_ids[] = {
+ { .compatible = "starfive,jhb100-hwspinlock", },
+ {},
+};
+MODULE_DEVICE_TABLE(of, starfive_hwpinlock_ids);
+
+static struct platform_driver starfive_hwspinlock_driver = {
+ .probe = starfive_hwspinlock_probe,
+ .driver = {
+ .name = "starfive_hwspinlock",
+ .of_match_table = starfive_hwpinlock_ids,
+ },
+};
+module_platform_driver(starfive_hwspinlock_driver);
+
+MODULE_AUTHOR("Xingyu Wu <xingyu.wu@starfivetech.com>");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Hardware spinlock driver for StarFive JHB100 SoC");
--
2.34.1
prev parent reply other threads:[~2026-04-24 3:36 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-24 3:20 [PATCH v1 0/2] Add hwspinlock driver for StarFive JHB100 SoC Xingyu Wu
2026-04-24 3:20 ` [PATCH v1 1/2] dt-bindings: hwlock: Add the support of HWspinlock for StarFive JHB100 Xingyu Wu
2026-04-24 17:05 ` Conor Dooley
2026-04-27 6:22 ` Xingyu Wu
2026-04-25 10:15 ` Krzysztof Kozlowski
2026-04-27 6:24 ` Xingyu Wu
2026-04-24 3:20 ` Xingyu Wu [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260424032026.62301-3-xingyu.wu@starfivetech.com \
--to=xingyu.wu@starfivetech.com \
--cc=andersson@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-remoteproc@vger.kernel.org \
--cc=mason.huo@starfivetech.com \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox