From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=139.219.17.92 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796364409; b=Bzlz/Etlefx3KwoEAavWVw9dMAwLNqGYuWGpCi45RYcJ5wz+fEQ6jrwN6A+At/Hntiyb eFL2iD4F4DT1wpVcIGsfHWLw2DnMxvpiOyXouO3Zqq/x5JRr5+CWYhcY68h8BzAhGDDhU 0w7EYmzXdUbRdHXpl8MDkJrjU4w6IAzl8zeWr476vEt2UIZSdOhFKXswcBTgffea7XQ8/ 2pAV+fMVmxZ9c3/9I15vF4PR19q1nyH67hv7oNgxPEfgA/wSQHvfzRJClLbbJEAkc61bY ep/BS5Ok8scJXzjkT3vQiiWpdUjCt+Td6PskULdWIcBb0WifdHcKn9fb53C347W6aUg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796364409; h=DMARC-Filter:Received:Received:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:Content-Transfer-Encoding: Content-Type:X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=tUyTXfel3+9kKzDMY0WitsPeTOlSM54q+8NwXwswt94=; b=s5qUd583JaZj5iq2k7L1fN76JW+PS3U8LXQsTDFjP+Xhbi7i7W+a5Au7yA5/1FU0uV3Y rn96JZU1kfITkoQo4gWBXJr2+/d4BRvdxBJDzxWTgQ3oNnZSXkQAi6eAHrX/Lhzk3sq0i a0ajvb7OAcwQuWeLmEVgMnCgsG6kMr4QdQJpi/kBBik8FrtYxkzrBugdzHJhyVSpzpyhY 9/wLBPUjOVxjB0eIHpfQqqTYQ8pRxC02XMiNIe5OVAAUv8fgvrpvEanraJulMx9BplEJS AqFHVSL8lzsKVShAcWryO9XPfV4aby9xsrh+gLMGsoauiXw3OL5M8zmenxIWaAS0s4A== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=starfivetech.com; spf=pass smtp.mailfrom=starfivetech.com; arc=pass header.oldest-pass=0 smtp.remote-ip=139.219.17.92 Received: from CHN02-BJS-obe.outbound.protection.partner.outlook.cn (mail-bjschn02on2092.outbound.protection.partner.outlook.cn [139.219.17.92]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 265D8606A32F; Fri, 4 Dec 2026 06:06:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 265D8606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=starfivetech.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=starfivetech.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qgpm6cseJbBJ42KoVr9n3lDYymThI+UjDkPlXNDqOxV6zMrMsZm2IUSI8ILPywBxoOl0tzwX9B+ZLpI8GqUZpDeywRiD/yrgtx9k28zOCm7bxUCaMGuTBrca8gp2M5ky6C8mHpC4ZwHurcR1qQMyf1m3K0sqDB+Fx5VTdwP2JxNeqHsKiejO807DQK5Wv17zbp68rHv2TQNidCJG2PoJDDzULrTZIyMqU9nFINBZDOYoTdPqJEsidzbXZOyufHf7yAcH9qQ8PgEONMuxfhCI+gTankLo8CQhh42nBjeU6IsRc/+Ghtd/h3Ip+gJRfapZ+IeihxyDPlFmQSc3nfbUfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=tUyTXfel3+9kKzDMY0WitsPeTOlSM54q+8NwXwswt94=; b=mrEu7Fd823d73l0aeWV8XLBm9mOaxtSTrkbOUTBX+iHwGS2152oAv7hiyonmEDONhfjTBsasqAqqGvTsVgeoMxhhKB4S+GKxPxPmhPW1iA2XC/3jJC6L9Xs00pm8Uy8JwX065jsSIPHq5BayruTGi7aR3niq5HEl9Y5mw3nU2PJvONPVXcZNho15Lfv4FxnaOnsekmH8OQ9fWmMiDzo6XodSViE2zJG8rcLe4j4nE4KGwiXqP5mSGC2+c39lr8/CSum6GbfA29m9HS4kay84aKQMlV+6L9HziT/qwfZ7hQD7XeZq+9Go8fJvHSnQHk9TWLTCq67nWN2Qpk5Uhg1zoQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=starfivetech.com; dmarc=pass action=none header.from=starfivetech.com; dkim=pass header.d=starfivetech.com; arc=none Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=starfivetech.com; Received: from BJSPR01MB0577.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:d::12) by BJSPR01MB0881.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:f::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.639.18; Fri, 4 Dec 2026 06:06:37 +0000 Received: from BJSPR01MB0577.CHNPR01.prod.partner.outlook.cn ([fe80::c362:8d6f:de71:524d]) by BJSPR01MB0577.CHNPR01.prod.partner.outlook.cn ([fe80::c362:8d6f:de71:524d%7]) with mapi id 15.21.0639.015; Fri, 4 Dec 2026 06:06:37 +0000 From: Mason Huo To: Lee Jones , Corey Minyard , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: mfd@lists.linux.dev, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, openipmi-developer@lists.sourceforge.net, Mason Huo Subject: [PATCH v2 3/3] ipmi: Add StarFive KCS BMC driver Date: Fri, 4 Dec 2026 14:06:23 +0800 Message-ID: <20261204060623.2994008-4-mason.huo@starfivetech.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20261204060623.2994008-1-mason.huo@starfivetech.com> References: <20261204060623.2994008-1-mason.huo@starfivetech.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: ZQ0PR01CA0006.CHNPR01.prod.partner.outlook.cn (2406:e500:c550:5::16) To BJSPR01MB0577.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:d::12) X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BJSPR01MB0577:EE_|BJSPR01MB0881:EE_ X-MS-Office365-Filtering-Correlation-Id: f8592d16-a4d9-4ceb-5097-08df51ccc1b1 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|376014|366016|23010399003|1800799024|6133799003|261009223027099003|3023799007|22082099003|18002099003|56012099006|261026121527099003|261102123227099003|261022105027099003|261028144727099003|261026140227099003|261021121427099003|10067099003|261022075727099003|261021135927099003|38350700014; X-Microsoft-Antispam-Message-Info: Q6ASXPKiBPvVWVXiRWdNSiEzZOj54dIKv7YtssiYGjBp/JmFaL0T5hNxIU9ch50GO69kg3tRcujkPYQpeXnjqVXKDVALMjVdo8zfbdidVYOrHeI34Gkkae0lUoWdA5m8AFrgTwTfRKB4uIHoHKL32jlnrEGfbx1cWNdQYaMfdqKvAgmHgzLhSEIdVkIcaRFFjEGLbpLrSBJMmOpXK0XrtN/qvcK0HJiXofw8g5Ca79LjVgqboogq4BZaOCkr8VF1UY/fKsRj5wMUQ5S7PKOyhBAqBaRdXRHvFQlD4vDas99/Xo1yqkqzfUMyC+8yiHKqxDttG5EBMaoVEdYs9lkI8Tzxv3oo+WOsRdWFuxK149zX8dQcxhiHHrDdWrY1e5pe+BNqn7tit1T8TQJL7Sa39j7TThDO8Wge1bhKA2tLJQ/UuPN7Qcq5Tg50UjX61FFn5kaVIPEkSZYjla4V7lHQ/JZYsrIlCLxbP3jxKUxOhdHGFlvtIntSuabHhA69XgQSsqvh+ci4mgUixOGcHXqGYBeqDpaBUayEEMKpnRtTG8Xg5A592QoDoZKvbYA4Mn52 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BJSPR01MB0577.CHNPR01.prod.partner.outlook.cn;PTR:;CAT:NONE;SFS:(13230040)(52116014)(376014)(366016)(23010399003)(1800799024)(6133799003)(261009223027099003)(3023799007)(22082099003)(18002099003)(56012099006)(261026121527099003)(261102123227099003)(261022105027099003)(261028144727099003)(261026140227099003)(261021121427099003)(10067099003)(261022075727099003)(261021135927099003)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?LgPrqlLStrOlL+A8If95DT0m8ghzdED6EKtu5EBgwcjOecEgMrNwWCl+ylQE?= =?us-ascii?Q?nVsGmXeWEvGgoEdFHpUTKo7sXsRZSJPX7tEpSLgudTLCp+o4za1uYUV2JAfr?= =?us-ascii?Q?bBa8qDvH+e6mwq8C957Odu/GqYLiLVwvufkjgadWGfShSy2F91sENd0oCuPU?= =?us-ascii?Q?0D1B/GghQ3/aeDn2JK3pPTajVeIJRv5YHb6y5LsD+1MbDfJcFeI8jXaSyzED?= =?us-ascii?Q?qMzkZWYL8HXqxtEVDR8CW7iMaHB5267VQTPUrR5RHcoylMN/IPYSIQkoYmHP?= =?us-ascii?Q?CaNd9dxuwcAM+f6vqVnOeVgXv5epm0TEBEk6wfpLLSaN79PzNxueQJFtHlyL?= =?us-ascii?Q?QeHOEM0EyL+XLLdLKFbOf08edfMNufZo9TABr0jmMIDDJjYC/2c/+ND/JneL?= =?us-ascii?Q?QBLl+LLKDy9SGCTlIobk7ILo/GlNu3WNv+Rd9X03EenMt3L8JPwHMmtiZyYD?= =?us-ascii?Q?Wcs5g3pg2M69GO1cQTmj0+pnrl03JIj0BMD0HGpN9exzcUP+2sbzK+dICsWN?= =?us-ascii?Q?sxzl6PMQy/tg78FZH9IEO0K43M7wc55Tw6QuMQ9rEmmwG55fEEJKMcUVMzlz?= =?us-ascii?Q?zaZpWH/QATyReD8Vxp5ZO5sZxyFnuLi87Da/0WoLp+UIGELyIyTolkeyRGFr?= =?us-ascii?Q?hsZKitTjSRUTlUBvEvgPNZ34Z1Eeu5QhIl9HqwMORL3OIhOxVLvE75yqsKA3?= =?us-ascii?Q?K4VpnFBs5NRj9Hwqlj9moTxoH9QrwHAdnnVrUdo7ukFupJsJudQTKA4SMD+W?= =?us-ascii?Q?AS811VGzoVZX/GlgglJo/yT10IN0f3w88NrBdrN2M3TXddfmjj8LWM+vYlDI?= =?us-ascii?Q?2z/mFFl0wm/zRUZ21Mea8EG5AH7wowntXWKV2uXvfRzS0xtEho1ZYWcth2yy?= =?us-ascii?Q?FbV8yeG3ApNvhvvZbcaG4Vtr7eM1LWlOtW4viPW53mJXXFLQMZiq0JZp3z22?= =?us-ascii?Q?G/67dIktDqDAOIi49ogIPayDvdjEjUtOvSNx+S/qoYg1s2W88EFDzl4RnewP?= =?us-ascii?Q?0DCZyb8ZsVA1wDmCsLlHY5JeFnMDFg5itVlkZXdekNub7HeTe6XQK4VCIOtI?= =?us-ascii?Q?39wg0BFXfYxiwpd9Adk5F1jRSo0e3wzZakBY2JDn/ix+cvPPqTnS0OJoe7nZ?= =?us-ascii?Q?hQcfZBRFdhUbHClpqq+UI3AZhUTlyLUwIKFzzWITagK3FybHUlfgp9riVPGh?= =?us-ascii?Q?cq9S9JyCkYO7GQyt4AB5RQ14Bp+NNrXfFvDWB4eBBDh5/7ZxlHWfg84LngGT?= =?us-ascii?Q?Mc3Nog+f0Ph+wwCvfqQ52emOqtJ9GH5J0Dm6Pf193xtVmFLhGqve00TDVsSL?= =?us-ascii?Q?0kaWVNPhuPhjb4pjWhJ+SZoFJJbu1Z8CPKqBHq5yGJjCc50oKmRP/7MBiu1e?= =?us-ascii?Q?HAvO+3vuzcGbx2xn+oiXlkncjRt7RIp4rzlAPbBveDo5P6WaHob1ySUMeV4A?= =?us-ascii?Q?nMPxNXvsY+dWkbjQC8kQzp9H1ovvNJmjrawUGQh80eSPojrIzIgQ4F7MFyPJ?= =?us-ascii?Q?FoONjfdImdPH6hsggo09oUabbPSBNLBCwxNLrTYKfSDNEb5aKdCld3Yd7BC9?= =?us-ascii?Q?V8U/MB05x5zeplB6AHlyCwR8kXyc1gwqXr6STXt9pwKuR49ABWOgTjjQF3Be?= =?us-ascii?Q?+wWIiexzakZz+U2j6jnM1kcVgcBURaaP5Uli/8aFHacIS8XlbyStnfdoUrKP?= =?us-ascii?Q?zLXasn8SDlbub/8TNcBM/c2FhDk3E/xq7RI0VihmAHkAeTluYE4lhWDxLXoF?= =?us-ascii?Q?CzjnenxZfN4H+bNhmOvtFZE7Aa37sgY=3D?= X-OriginatorOrg: starfivetech.com X-MS-Exchange-CrossTenant-Network-Message-Id: f8592d16-a4d9-4ceb-5097-08df51ccc1b1 X-MS-Exchange-CrossTenant-AuthSource: BJSPR01MB0577.CHNPR01.prod.partner.outlook.cn X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 06:06:37.5908 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 06fe3fa3-1221-43d3-861b-5a4ee687a85c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: r1tLS3noPRZGUjbe1Q2LTF8uCWGhnyd/QOxrEFQKRWLE4fe2Qat8R8WWK/w/QAJ0GnTCuKh72zGKPGxdb6rLtpzDJRoYnlH2zyENUegH1bs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BJSPR01MB0881 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 --- 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 +S: Supported +F: drivers/char/ipmi/kcs_bmc_starfive.c + STARFIVE JHB100 PINCTRL DRIVERS M: Changhuang Liang M: Lianfeng Ouyang 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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" for channel 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 "); +MODULE_DESCRIPTION("StarFive device interface to the KCS BMC device"); -- 2.50.1