Devicetree
 help / color / mirror / Atom feed
From: Mason Huo <mason.huo@starfivetech.com>
To: Lee Jones <lee@kernel.org>, Corey Minyard <corey@minyard.net>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>
Cc: mfd@lists.linux.dev, linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org,
	openipmi-developer@lists.sourceforge.net,
	Mason Huo <mason.huo@starfivetech.com>
Subject: [PATCH v2 3/3] ipmi: Add StarFive KCS BMC driver
Date: Fri,  4 Dec 2026 14:06:23 +0800	[thread overview]
Message-ID: <20261204060623.2994008-4-mason.huo@starfivetech.com> (raw)
In-Reply-To: <20261204060623.2994008-1-mason.huo@starfivetech.com>

This driver exposes the Keyboard Controller Style (KCS) interface on
StarFive SoCs as a character device. Such SOCs are commonly used
as a BaseBoard Management Controller (BMC) on a server board, and KCS
interface is commonly used to perform the in-band IPMI communication
between the server and its BMC.

Signed-off-by: Mason Huo <mason.huo@starfivetech.com>
---
 MAINTAINERS                          |   5 +
 drivers/char/ipmi/Kconfig            |  16 ++
 drivers/char/ipmi/Makefile           |   1 +
 drivers/char/ipmi/kcs_bmc_starfive.c | 281 +++++++++++++++++++++++++++
 4 files changed, 303 insertions(+)
 create mode 100644 drivers/char/ipmi/kcs_bmc_starfive.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 53ee9b601dcb..b1e02a7fa6e5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -26815,6 +26815,11 @@ S:	Supported
 F:	Documentation/devicetree/bindings/interrupt-controller/starfive,jhb100-intc.yaml
 F:	drivers/irqchip/irq-starfive-jhb100-intc.c
 
+STARFIVE JHB100 KCS-BMC DRIVER
+M:	Mason Huo <mason.huo@starfivetech.com>
+S:	Supported
+F:	drivers/char/ipmi/kcs_bmc_starfive.c
+
 STARFIVE JHB100 PINCTRL DRIVERS
 M:	Changhuang Liang <changhuang.liang@starfivetech.com>
 M:	Lianfeng Ouyang <lianfeng.ouyang@starfivetech.com>
diff --git a/drivers/char/ipmi/Kconfig b/drivers/char/ipmi/Kconfig
index 538a7d3c65bf..118f901a4e28 100644
--- a/drivers/char/ipmi/Kconfig
+++ b/drivers/char/ipmi/Kconfig
@@ -149,6 +149,22 @@ config NPCM7XX_KCS_IPMI_BMC
 	  This support is also available as a module.  If so, the module
 	  will be called kcs_bmc_npcm7xx.
 
+config STARFIVE_KCS_IPMI_BMC
+	depends on MFD_STARFIVE_JHB100_SIO || COMPILE_TEST
+	depends on HAS_IOMEM
+	select IPMI_KCS_BMC
+	select REGMAP_MMIO
+	tristate "StarFive KCS IPMI BMC driver"
+	help
+	  Provides a driver for the KCS (Keyboard Controller Style) IPMI
+	  interface found on StarFive SoCs.
+
+	  The driver implements the BMC side of the KCS controller, it
+	  provides the access of KCS IO space for BMC side.
+
+	  This support is also available as a module.  If so, the module
+	  will be called kcs_bmc_starfive.
+
 config IPMI_KCS_BMC_CDEV_IPMI
 	depends on IPMI_KCS_BMC
 	tristate "IPMI character device interface for BMC KCS devices"
diff --git a/drivers/char/ipmi/Makefile b/drivers/char/ipmi/Makefile
index 4ea450a82242..800d570dd4dc 100644
--- a/drivers/char/ipmi/Makefile
+++ b/drivers/char/ipmi/Makefile
@@ -27,5 +27,6 @@ obj-$(CONFIG_IPMI_KCS_BMC_CDEV_IPMI) += kcs_bmc_cdev_ipmi.o
 obj-$(CONFIG_ASPEED_BT_IPMI_BMC) += bt-bmc.o
 obj-$(CONFIG_ASPEED_KCS_IPMI_BMC) += kcs_bmc_aspeed.o
 obj-$(CONFIG_NPCM7XX_KCS_IPMI_BMC) += kcs_bmc_npcm7xx.o
+obj-$(CONFIG_STARFIVE_KCS_IPMI_BMC) += kcs_bmc_starfive.o
 obj-$(CONFIG_IPMB_DEVICE_INTERFACE) += ipmb_dev_int.o
 obj-$(CONFIG_SSIF_IPMI_BMC) += ssif_bmc.o
diff --git a/drivers/char/ipmi/kcs_bmc_starfive.c b/drivers/char/ipmi/kcs_bmc_starfive.c
new file mode 100644
index 000000000000..51ddf474a382
--- /dev/null
+++ b/drivers/char/ipmi/kcs_bmc_starfive.c
@@ -0,0 +1,281 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 StarFive Technology Co., Ltd.
+ */
+
+#include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/spinlock.h>
+#include <linux/sprintf.h>
+
+#include "kcs_bmc_device.h"
+
+#define DEVICE_NAME		"starfive-kcs-bmc"
+
+/* Register offsets in the register window of a channel */
+#define KCS_DO			0x0
+#define KCS_DI			0x4
+#define KCS_STS			0x8
+
+#define KCS_INT			0xc
+#define   KCS_INT_IBF_EN	BIT(0)
+#define   KCS_INT_IBF_STS	BIT(1)
+
+struct starfive_kcs_bmc {
+	struct kcs_bmc_device kcs_bmc;
+	struct regmap *map;
+	/* Protects ie_en and serialises every write to KCS_INT */
+	spinlock_t ie_lock;
+	/* Shadow of the KCS_INT enable bits, restored after eSPI bus reset */
+	u32 ie_en;
+};
+
+static inline struct starfive_kcs_bmc *
+to_starfive_kcs_bmc(struct kcs_bmc_device *kcs_bmc)
+{
+	return container_of(kcs_bmc, struct starfive_kcs_bmc, kcs_bmc);
+}
+
+static u8 starfive_kcs_inb(struct kcs_bmc_device *kcs_bmc, u32 reg)
+{
+	struct starfive_kcs_bmc *priv = to_starfive_kcs_bmc(kcs_bmc);
+	u32 val = 0;
+	int rc;
+
+	rc = regmap_read(priv->map, reg, &val);
+	WARN(rc != 0, "regmap_read() failed: %d\n", rc);
+
+	return rc == 0 ? (u8)val : 0;
+}
+
+static void starfive_kcs_outb(struct kcs_bmc_device *kcs_bmc, u32 reg, u8 data)
+{
+	struct starfive_kcs_bmc *priv = to_starfive_kcs_bmc(kcs_bmc);
+	int rc;
+
+	rc = regmap_write(priv->map, reg, data);
+	WARN(rc != 0, "regmap_write() failed: %d\n", rc);
+}
+
+static void starfive_kcs_updateb(struct kcs_bmc_device *kcs_bmc,
+				 u32 reg, u8 mask, u8 data)
+{
+	struct starfive_kcs_bmc *priv = to_starfive_kcs_bmc(kcs_bmc);
+	int rc;
+
+	rc = regmap_update_bits(priv->map, reg, mask, data);
+	WARN(rc != 0, "regmap_update_bits() failed: %d\n", rc);
+}
+
+/*
+ * Write the shadow of the enable bits to KCS_INT. KCS_INT_IBF_STS is a
+ * read-only mirror of the IBF flag in KCS_STS, which the hardware clears
+ * once KCS_DI has been read, so only the enable bits are ever written.
+ */
+static void starfive_kcs_write_ie(struct starfive_kcs_bmc *priv)
+{
+	int rc;
+
+	lockdep_assert_held(&priv->ie_lock);
+
+	rc = regmap_write(priv->map, KCS_INT, priv->ie_en);
+	WARN(rc != 0, "regmap_write() failed: %d\n", rc);
+}
+
+static void starfive_kcs_irq_mask_update(struct kcs_bmc_device *kcs_bmc,
+					 u8 mask, u8 state)
+{
+	struct starfive_kcs_bmc *priv = to_starfive_kcs_bmc(kcs_bmc);
+	unsigned long flags;
+
+	/* The hardware has no OBE interrupt */
+	if (!(mask & KCS_BMC_EVENT_TYPE_IBF))
+		return;
+
+	spin_lock_irqsave(&priv->ie_lock, flags);
+	if (state & KCS_BMC_EVENT_TYPE_IBF)
+		priv->ie_en |= KCS_INT_IBF_EN;
+	else
+		priv->ie_en &= ~KCS_INT_IBF_EN;
+	starfive_kcs_write_ie(priv);
+	spin_unlock_irqrestore(&priv->ie_lock, flags);
+}
+
+static void starfive_kcs_hw_init(struct starfive_kcs_bmc *priv)
+{
+	unsigned long flags;
+
+	/* Restore the enables, e.g. after an eSPI bus reset */
+	spin_lock_irqsave(&priv->ie_lock, flags);
+	starfive_kcs_write_ie(priv);
+	spin_unlock_irqrestore(&priv->ie_lock, flags);
+}
+
+static irqreturn_t starfive_kcs_irq(int irq, void *arg)
+{
+	/* The interrupt deasserts once the client has read IDR */
+	return kcs_bmc_handle_event(arg);
+}
+
+/*
+ * "espi" is a dedicated interrupt line which carries a single event: the
+ * host has de-asserted the eSPI bus reset of the SIO block this channel
+ * belongs to. The line is requested with IRQF_SHARED only because every
+ * function of that SIO block (KCS, BT, mailbox, ...) listens to the same
+ * event and has to reprogram its own registers when it fires. It is not
+ * shared with unrelated interrupt sources and there is no per-function
+ * status bit to test, so each occurrence is relevant to all handlers and is
+ * claimed unconditionally. Rewriting KCS_INT from the shadow is idempotent.
+ */
+static irqreturn_t starfive_espi_rst_deassert_irq(int irq, void *arg)
+{
+	struct kcs_bmc_device *kcs_bmc = arg;
+	struct starfive_kcs_bmc *priv = to_starfive_kcs_bmc(kcs_bmc);
+
+	starfive_kcs_hw_init(priv);
+
+	return IRQ_HANDLED;
+}
+
+/*
+ * The cells of the SIO driver have no interrupt resources. The interrupts are
+ * those of the SIO block: "kcs<n>" for channel <n> of the block, which is the
+ * id of the cell, and "espi".
+ */
+static int starfive_kcs_config_irq(struct kcs_bmc_device *kcs_bmc,
+				   struct platform_device *pdev)
+{
+	struct platform_device *sio = to_platform_device(pdev->dev.parent);
+	const struct mfd_cell *cell = mfd_get_cell(pdev);
+	struct device *dev = &pdev->dev;
+	char name[16];
+	int rc, irq;
+
+	if (!cell)
+		return -ENODEV;
+
+	snprintf(name, sizeof(name), "kcs%d", cell->id);
+	irq = platform_get_irq_byname(sio, name);
+	if (irq < 0)
+		return irq;
+
+	rc = devm_request_irq(dev, irq, starfive_kcs_irq, 0,
+			      dev_name(dev), kcs_bmc);
+	if (rc < 0)
+		return dev_err_probe(dev, rc,
+				     "Unable to request kcs IRQ %d\n", irq);
+
+	irq = platform_get_irq_byname(sio, "espi");
+	if (irq < 0)
+		return irq;
+
+	rc = devm_request_irq(dev, irq, starfive_espi_rst_deassert_irq,
+			      IRQF_SHARED | IRQF_TRIGGER_RISING,
+			      dev_name(dev), kcs_bmc);
+	if (rc < 0)
+		return dev_err_probe(dev, rc,
+				     "Unable to request espi reset IRQ %d\n", irq);
+
+	return 0;
+}
+
+static const struct regmap_config starfive_kcs_regmap_config = {
+	.reg_bits = 32,
+	.val_bits = 32,
+	.reg_stride = 4,
+	.max_register = KCS_INT,
+};
+
+static const struct kcs_bmc_device_ops starfive_kcs_ops = {
+	.irq_mask_update = starfive_kcs_irq_mask_update,
+	.io_inputb = starfive_kcs_inb,
+	.io_outputb = starfive_kcs_outb,
+	.io_updateb = starfive_kcs_updateb,
+};
+
+static int starfive_kcs_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct starfive_kcs_bmc *priv;
+	struct kcs_bmc_device *kcs_bmc;
+	void __iomem *base;
+	int rc;
+
+	if (pdev->id < 0)
+		return dev_err_probe(dev, -EINVAL, "Unable to determine the channel\n");
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	spin_lock_init(&priv->ie_lock);
+
+	base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	priv->map = devm_regmap_init_mmio(dev, base, &starfive_kcs_regmap_config);
+	if (IS_ERR(priv->map))
+		return dev_err_probe(dev, PTR_ERR(priv->map),
+				     "Couldn't create regmap\n");
+
+	kcs_bmc = &priv->kcs_bmc;
+	kcs_bmc->dev = dev;
+	kcs_bmc->channel = pdev->id;
+	kcs_bmc->ioreg.idr = KCS_DI;
+	kcs_bmc->ioreg.odr = KCS_DO;
+	kcs_bmc->ioreg.str = KCS_STS;
+	kcs_bmc->ops = &starfive_kcs_ops;
+
+	platform_set_drvdata(pdev, priv);
+
+	starfive_kcs_hw_init(priv);
+	rc = starfive_kcs_config_irq(kcs_bmc, pdev);
+	if (rc)
+		return rc;
+
+	rc = kcs_bmc_add_device(kcs_bmc);
+	if (rc)
+		return dev_err_probe(dev, rc, "Failed to register channel %u\n",
+				     kcs_bmc->channel);
+
+	dev_dbg(dev, "Channel=%u idr=0x%x odr=0x%x str=0x%x\n",
+		kcs_bmc->channel, kcs_bmc->ioreg.idr, kcs_bmc->ioreg.odr,
+		kcs_bmc->ioreg.str);
+
+	return 0;
+}
+
+static void starfive_kcs_remove(struct platform_device *pdev)
+{
+	struct starfive_kcs_bmc *priv = platform_get_drvdata(pdev);
+	struct kcs_bmc_device *kcs_bmc = &priv->kcs_bmc;
+
+	kcs_bmc_remove_device(kcs_bmc);
+
+	starfive_kcs_irq_mask_update(kcs_bmc,
+				     (KCS_BMC_EVENT_TYPE_IBF |
+				      KCS_BMC_EVENT_TYPE_OBE),
+				     0);
+}
+
+static struct platform_driver starfive_kcs_bmc_driver = {
+	.driver = {
+		.name		= DEVICE_NAME,
+		.suppress_bind_attrs = true,
+	},
+	.probe	= starfive_kcs_probe,
+	.remove	= starfive_kcs_remove,
+};
+module_platform_driver(starfive_kcs_bmc_driver);
+
+MODULE_ALIAS("platform:" DEVICE_NAME);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Mason Huo <mason.huo@starfivetech.com>");
+MODULE_DESCRIPTION("StarFive device interface to the KCS BMC device");
-- 
2.50.1


      parent reply	other threads:[~2026-12-04  6:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  6:06 [PATCH v2 0/3] Add StarFive JHB100 SuperIO and KCS BMC drivers Mason Huo
2026-12-04  6:06 ` [PATCH v2 1/3] dt-bindings: mfd: Add StarFive JHB100 SuperIO Mason Huo
2026-12-04  6:06 ` [PATCH v2 2/3] mfd: Add StarFive JHB100 SuperIO driver Mason Huo
2026-12-04  6:06 ` Mason Huo [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=20261204060623.2994008-4-mason.huo@starfivetech.com \
    --to=mason.huo@starfivetech.com \
    --cc=conor+dt@kernel.org \
    --cc=corey@minyard.net \
    --cc=devicetree@vger.kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=openipmi-developer@lists.sourceforge.net \
    --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