Linux-i3c Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Manikandan Muralidharan <manikandan.m@microchip.com>
To: <alexandre.belloni@bootlin.com>, <Frank.Li@nxp.com>,
	<adrian.hunter@intel.com>, <Jm_Liao@asmedia.com.tw>,
	<Patrick_Yen@asmedia.com.tw>, <linux-kernel@vger.kernel.org>,
	<linux-i3c@lists.infradead.org>
Cc: <manikandan.m@microchip.com>
Subject: [PATCH 2/3] i3c: mipi-i3c-hci: add Microchip quirk to derive SCL timings from gclk
Date: Fri, 4 Dec 2026 14:04:48 +0530	[thread overview]
Message-ID: <20261204083449.933526-3-manikandan.m@microchip.com> (raw)
In-Reply-To: <20261204083449.933526-1-manikandan.m@microchip.com>

The SAMA7D65 I3C controller currently reuses the AMD OD/PP timing quirk,
which the register values tuned for the AMD core clock.Introduce
hci_quirks_mchp.c with mchp_cfg_od_pp_timing(), which derives
the push-pull, open-drain, SDR1..SDR4 extended low count and bus free
timings from the gclk rate and the bus I3C/I2C SCL rates, and gate it
with a new HCI_QUIRK_MCHP_OD_PP_TIMING quirk.

The timings are programmed from bus_init(), once the SCL rates are
known, and from i3c_hci_reset_and_init() so that they are restored
whenever the controller is reset.

Signed-off-by: Manikandan Muralidharan <manikandan.m@microchip.com>
---
 drivers/i3c/master/mipi-i3c-hci/Makefile      |   2 +-
 drivers/i3c/master/mipi-i3c-hci/core.c        |  21 ++-
 drivers/i3c/master/mipi-i3c-hci/hci.h         |   4 +
 .../i3c/master/mipi-i3c-hci/hci_quirks_mchp.c | 124 ++++++++++++++++++
 4 files changed, 147 insertions(+), 4 deletions(-)
 create mode 100644 drivers/i3c/master/mipi-i3c-hci/hci_quirks_mchp.c

diff --git a/drivers/i3c/master/mipi-i3c-hci/Makefile b/drivers/i3c/master/mipi-i3c-hci/Makefile
index e3d3ef757035..8139d3a049c0 100644
--- a/drivers/i3c/master/mipi-i3c-hci/Makefile
+++ b/drivers/i3c/master/mipi-i3c-hci/Makefile
@@ -4,5 +4,5 @@ obj-$(CONFIG_MIPI_I3C_HCI)		+= mipi-i3c-hci.o
 mipi-i3c-hci-y				:= core.o ext_caps.o pio.o dma.o \
 					   cmd_v1.o cmd_v2.o \
 					   dat_v1.o dct_v1.o \
-					   hci_quirks.o
+					   hci_quirks.o hci_quirks_mchp.o
 obj-$(CONFIG_MIPI_I3C_HCI_PCI)		+= mipi-i3c-hci-pci.o
diff --git a/drivers/i3c/master/mipi-i3c-hci/core.c b/drivers/i3c/master/mipi-i3c-hci/core.c
index 23853ca134fb..bc71871be2f0 100644
--- a/drivers/i3c/master/mipi-i3c-hci/core.c
+++ b/drivers/i3c/master/mipi-i3c-hci/core.c
@@ -183,6 +183,13 @@ static int i3c_hci_bus_init(struct i3c_master_controller *m)
 	if (hci->quirks & HCI_QUIRK_RESP_BUF_THLD)
 		amd_set_resp_buf_thld(hci);
 
+	/* Configure OD and PP timings once the bus SCL rates are known */
+	if (hci->quirks & HCI_QUIRK_MCHP_OD_PP_TIMING) {
+		ret = mchp_cfg_od_pp_timing(hci);
+		if (ret)
+			return ret;
+	}
+
 	scoped_guard(spinlock_irqsave, &hci->lock)
 		hci->irq_inactive = false;
 
@@ -961,6 +968,10 @@ static int i3c_hci_reset_and_init(struct i3c_hci *hci)
 	if (hci->quirks & HCI_QUIRK_OD_PP_TIMING)
 		amd_set_od_pp_timing(hci);
 
+	/* Configure OD and PP timings from the gclk rate for Microchip platforms */
+	if (hci->quirks & HCI_QUIRK_MCHP_OD_PP_TIMING)
+		return mchp_cfg_od_pp_timing(hci);
+
 	return 0;
 }
 
@@ -1186,7 +1197,6 @@ static int i3c_hci_init(struct i3c_hci *hci)
 static int i3c_hci_probe(struct platform_device *pdev)
 {
 	const struct mipi_i3c_hci_platform_data *pdata = pdev->dev.platform_data;
-	struct clk_bulk_data *clks;
 	struct i3c_hci *hci;
 	int irq, ret;
 
@@ -1222,10 +1232,15 @@ static int i3c_hci_probe(struct platform_device *pdev)
 	if (!hci->quirks && platform_get_device_id(pdev))
 		hci->quirks = platform_get_device_id(pdev)->driver_data;
 
-	ret = devm_clk_bulk_get_all_enabled(&pdev->dev, &clks);
+	ret = devm_clk_bulk_get_all_enabled(&pdev->dev, &hci->clks);
 	if (ret < 0)
 		return dev_err_probe(&pdev->dev, ret,
 				     "Failed to get clocks\n");
+	hci->num_clks = ret;
+
+	/* Microchip SCL timings are derived from the second (gclk) clock */
+	if ((hci->quirks & HCI_QUIRK_MCHP_OD_PP_TIMING) && hci->num_clks < 2)
+		return dev_err_probe(&pdev->dev, -EINVAL, "Missing gclk\n");
 
 	ret = i3c_hci_init(hci);
 	if (ret)
@@ -1268,7 +1283,7 @@ static void i3c_hci_remove(struct platform_device *pdev)
 static const __maybe_unused struct of_device_id i3c_hci_of_match[] = {
 	{ .compatible = "mipi-i3c-hci", },
 	{ .compatible = "microchip,sama7d65-i3c-hci",
-	  .data = (void *)(ulong)(HCI_QUIRK_PIO_MODE | HCI_QUIRK_OD_PP_TIMING |
+	  .data = (void *)(ulong)(HCI_QUIRK_PIO_MODE | HCI_QUIRK_MCHP_OD_PP_TIMING |
 				  HCI_QUIRK_RESP_BUF_THLD) },
 	{},
 };
diff --git a/drivers/i3c/master/mipi-i3c-hci/hci.h b/drivers/i3c/master/mipi-i3c-hci/hci.h
index 6a5b1f9875e1..0137c91e84cf 100644
--- a/drivers/i3c/master/mipi-i3c-hci/hci.h
+++ b/drivers/i3c/master/mipi-i3c-hci/hci.h
@@ -53,6 +53,8 @@ struct i3c_hci {
 	void __iomem *EXTCAPS_regs;
 	void __iomem *AUTOCMD_regs;
 	void __iomem *DEBUG_regs;
+	struct clk_bulk_data *clks;
+	int num_clks;
 	const struct hci_io_ops *io;
 	void *io_data;
 	const struct hci_cmd_ops *cmd;
@@ -178,6 +180,7 @@ struct i3c_hci_dev_data {
 #define HCI_QUIRK_RPM_PARENT_MANAGED	BIT(7)  /* Runtime PM managed by parent device */
 #define HCI_QUIRK_DMA_ABORT_REQUIRES_PIO_RESET	BIT(8)  /* Do PIO queue SW resets after DMA abort */
 #define HCI_QUIRK_DMA_REQUIRES_HC_ABORT		BIT(9)  /* Use HC_CONTROL ABORT to abort DMA */
+#define HCI_QUIRK_MCHP_OD_PP_TIMING	BIT(10) /* Set OD and PP timings from gclk for Microchip */
 
 /* Vendor-specific SCL timing registers shared by the quirk files */
 #define HCI_SCL_I3C_OD_TIMING		0x214
@@ -191,6 +194,7 @@ void mipi_i3c_hci_pio_reset_all_queues(struct i3c_hci *hci);
 void mipi_i3c_hci_dct_index_reset(struct i3c_hci *hci);
 void amd_set_od_pp_timing(struct i3c_hci *hci);
 void amd_set_resp_buf_thld(struct i3c_hci *hci);
+int mchp_cfg_od_pp_timing(struct i3c_hci *hci);
 void i3c_hci_sync_irq_inactive(struct i3c_hci *hci);
 int i3c_hci_process_xfer(struct i3c_hci *hci, struct hci_xfer *xfer, int n);
 
diff --git a/drivers/i3c/master/mipi-i3c-hci/hci_quirks_mchp.c b/drivers/i3c/master/mipi-i3c-hci/hci_quirks_mchp.c
new file mode 100644
index 000000000000..dff319d6287f
--- /dev/null
+++ b/drivers/i3c/master/mipi-i3c-hci/hci_quirks_mchp.c
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * I3C HCI Microchip Quirks
+ *
+ * Copyright (C) 2026 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Manikandan Muralidharan <manikandan.m@microchip.com>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/i3c/master.h>
+#include <linux/math.h>
+#include <linux/minmax.h>
+#include <linux/time64.h>
+#include "hci.h"
+
+/* Clock order fixed by the binding: pclk, gclk */
+#define MCHP_GCLK_IDX			1
+
+#define SCL_I3C_TIMING_HCNT		W0_MASK(23, 16)
+#define SCL_I3C_TIMING_LCNT		W0_MASK(7, 0)
+
+/*
+ * Microchip I3CC extended low-count timing (not part of the HCI standard).
+ * Each SDRx mode reuses the push-pull SCL-high count and stretches only the
+ * SCL-low period through its own field.
+ */
+#define MCHP_SCL_EXT_LCNT_TIMING	0x228
+#define MCHP_EXT_LCNT_SDR1		W0_MASK(7, 0)
+#define MCHP_EXT_LCNT_SDR2		W0_MASK(15, 8)
+#define MCHP_EXT_LCNT_SDR3		W0_MASK(23, 16)
+#define MCHP_EXT_LCNT_SDR4		W0_MASK(31, 24)
+
+#define MCHP_BUS_FREE_TIMING		0x234
+#define MCHP_BUS_I3C_MST_FREE		W0_MASK(15, 0)
+
+/* SDR1..SDR4 target SCL rates (Hz) */
+#define MCHP_I3C_SDR1_SCL_RATE		8000000
+#define MCHP_I3C_SDR2_SCL_RATE		6000000
+#define MCHP_I3C_SDR3_SCL_RATE		4000000
+#define MCHP_I3C_SDR4_SCL_RATE		2000000
+
+#define MCHP_SCL_CNT_MIN		1
+#define MCHP_SCL_CNT_MAX		0xff	/* each count field is 8 bits */
+
+/*
+ * Cycles for one half of an SCL period.
+ *
+ * @core_rate: I3C peripheral (generic) clock rate
+ * @scl_rate:  target rate for the phase/mode being programmed
+ * @base:      cycles already used by the opposite half of the period
+ *
+ * Returns round_up(core_rate / scl_rate) - base, clamped to a non-zero
+ * 8-bit count. Shared by the PP low, OD high, and SDR1..4 extended low counts.
+ */
+static u8 mchp_scl_cnt(unsigned long core_rate, unsigned long scl_rate, u8 base)
+{
+	u32 period = DIV_ROUND_UP(core_rate, scl_rate);
+
+	if (period <= base)
+		return MCHP_SCL_CNT_MIN;
+
+	return min_t(u32, period - base, MCHP_SCL_CNT_MAX);
+}
+
+int mchp_cfg_od_pp_timing(struct i3c_hci *hci)
+{
+	struct i3c_bus *bus = &hci->master.bus;
+	unsigned long core_rate, i2c_rate;
+	u32 ext_lcnt;
+	u8 hcnt, lcnt;
+
+	/* SCL rates are only known once the bus mode has been set */
+	if (!bus->scl_rate.i3c)
+		return 0;
+
+	core_rate = clk_get_rate(hci->clks[MCHP_GCLK_IDX].clk);
+	if (!core_rate)
+		return -EINVAL;
+
+	/* A pure I3C bus leaves the I2C rate unset, run open-drain at FM+ */
+	i2c_rate = bus->scl_rate.i2c ?: I3C_BUS_I2C_FM_PLUS_SCL_MAX_RATE;
+
+	/*
+	 * Push-pull timing. SCL-high is time-bounded (kept below the 50 ns I2C
+	 * spike-filter threshold so legacy I2C targets ignore the fast I3C
+	 * clock); the low half then fills the rest of the I3C SCL period.
+	 */
+	hcnt = clamp_t(u32, DIV_ROUND_UP_ULL((u64)I3C_BUS_THIGH_MIXED_MAX_NS * core_rate,
+					     NSEC_PER_SEC) - 1,
+		       MCHP_SCL_CNT_MIN, MCHP_SCL_CNT_MAX);
+	lcnt = mchp_scl_cnt(core_rate, bus->scl_rate.i3c, hcnt);
+	reg_write(HCI_SCL_I3C_PP_TIMING,
+		  FIELD_PREP(SCL_I3C_TIMING_HCNT, hcnt) |
+		  FIELD_PREP(SCL_I3C_TIMING_LCNT, lcnt));
+
+	/*
+	 * SDR1..SDR4 extended low counts. The SCL-high count is reused
+	 * unchanged; EXT_LCNT_x is the total SDRx low count, so that
+	 * period = PP_HCNT + EXT_LCNT_x.
+	 */
+	ext_lcnt = FIELD_PREP(MCHP_EXT_LCNT_SDR1,
+			      mchp_scl_cnt(core_rate, MCHP_I3C_SDR1_SCL_RATE, hcnt)) |
+		   FIELD_PREP(MCHP_EXT_LCNT_SDR2,
+			      mchp_scl_cnt(core_rate, MCHP_I3C_SDR2_SCL_RATE, hcnt)) |
+		   FIELD_PREP(MCHP_EXT_LCNT_SDR3,
+			      mchp_scl_cnt(core_rate, MCHP_I3C_SDR3_SCL_RATE, hcnt)) |
+		   FIELD_PREP(MCHP_EXT_LCNT_SDR4,
+			      mchp_scl_cnt(core_rate, MCHP_I3C_SDR4_SCL_RATE, hcnt));
+	reg_write(MCHP_SCL_EXT_LCNT_TIMING, ext_lcnt);
+
+	/* Open-drain timing, ~50% duty derived from the I2C SCL rate */
+	lcnt = mchp_scl_cnt(core_rate, 2 * i2c_rate, 0);
+	hcnt = mchp_scl_cnt(core_rate, i2c_rate, lcnt);
+	reg_write(HCI_SCL_I3C_OD_TIMING,
+		  FIELD_PREP(SCL_I3C_TIMING_HCNT, hcnt) |
+		  FIELD_PREP(SCL_I3C_TIMING_LCNT, lcnt));
+
+	/* Bus-free time matches the open-drain low period */
+	reg_write(MCHP_BUS_FREE_TIMING, FIELD_PREP(MCHP_BUS_I3C_MST_FREE, lcnt));
+
+	return 0;
+}
-- 
2.43.0


-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

  parent reply	other threads:[~2026-12-04  8:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  8:34 [PATCH 0/3] i3c: mipi-i3c-hci: derive SAMA7D65 SCL timings from gclk Manikandan Muralidharan
2026-12-04  8:34 ` [PATCH 1/3] i3c: mipi-i3c-hci: move shared SCL timing register offsets to hci.h Manikandan Muralidharan
2026-12-04 20:22   ` Frank Li
2026-12-04  8:34 ` Manikandan Muralidharan [this message]
2026-12-04  8:57   ` [PATCH 2/3] i3c: mipi-i3c-hci: add Microchip quirk to derive SCL timings from gclk sashiko-bot
2026-12-04 20:30     ` Frank Li
2026-12-04 20:29   ` Frank Li
2026-12-04  8:34 ` [PATCH 3/3] i3c: mipi-i3c-hci: add Microchip quirk to set response buffer threshold Manikandan Muralidharan
2026-12-04  8:48   ` sashiko-bot
2026-12-04 20:34   ` Frank Li

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=20261204083449.933526-3-manikandan.m@microchip.com \
    --to=manikandan.m@microchip.com \
    --cc=Frank.Li@nxp.com \
    --cc=Jm_Liao@asmedia.com.tw \
    --cc=Patrick_Yen@asmedia.com.tw \
    --cc=adrian.hunter@intel.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-i3c@lists.infradead.org \
    --cc=linux-kernel@vger.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