mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vasantha Likitha T <vasanthalikitha.tandra@amd.com>
To: <linux-i2c@vger.kernel.org>
Cc: <michal.simek@amd.com>, <andi.shyti@kernel.org>,
	<linux-kernel@vger.kernel.org>, <git@amd.com>,
	Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>,
	Vasantha Likitha T <vasanthalikitha.tandra@amd.com>
Subject: [PATCH v2] i2c: xiic: Rework dynamic-mode RX FIFO drain in xiic_read_rx
Date: Mon, 28 Sep 2026 13:04:25 +0530	[thread overview]
Message-ID: <20260928073425.2597782-1-vasanthalikitha.tandra@amd.com> (raw)

From: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>

Programming the RFD (RX FIFO depth) threshold at runtime causes a
spurious SCL clock pulse which results in the STOP condition being
missed.

Fix this by programming RFD once, when the receive transfer starts, and
handling the final-byte sequencing in software: select how many bytes to
drain from the RX FIFO on each RX_FULL interrupt instead of
reprogramming RFD after every read.

RX_FULL is then always raised at the same threshold. When fewer bytes
remain to be received after the current FIFO contents are accounted for,
drain only that remaining count and retain the other bytes in the FIFO.
The retained bytes together with the bytes still arriving restore the
FIFO occupancy to the fixed threshold. Otherwise, drain the FIFO
completely.

Fixes: e1d5b6598cdc ("i2c: Add support for Xilinx XPS IIC Bus Interface")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Co-developed-by: Vasantha Likitha T <vasanthalikitha.tandra@amd.com>
Signed-off-by: Vasantha Likitha T <vasanthalikitha.tandra@amd.com>
---
Changes in v2:
- Updated commit message and Fixes tag
- Removed dead code
- Reused cached rx_space
- Improved comment wording
---
 drivers/i2c/busses/i2c-xiic.c | 39 ++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 14 deletions(-)

diff --git a/drivers/i2c/busses/i2c-xiic.c b/drivers/i2c/busses/i2c-xiic.c
index 3e7735e1dae0..474540d4d611 100644
--- a/drivers/i2c/busses/i2c-xiic.c
+++ b/drivers/i2c/busses/i2c-xiic.c
@@ -578,7 +578,8 @@ static void xiic_smbus_block_read_setup(struct xiic_i2c *i2c)
 static void xiic_read_rx(struct xiic_i2c *i2c)
 {
 	u8 bytes_in_fifo, cr = 0, bytes_to_read = 0;
-	u32 bytes_rem = 0;
+	u32 bytes_rem;
+	u32 rx_space;
 	int i;
 
 	bytes_in_fifo = xiic_getreg8(i2c, XIIC_RFO_REG_OFFSET) + 1;
@@ -590,13 +591,32 @@ static void xiic_read_rx(struct xiic_i2c *i2c)
 			xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
 			xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
 
-	if (bytes_in_fifo > xiic_rx_space(i2c))
-		bytes_in_fifo = xiic_rx_space(i2c);
+	rx_space = xiic_rx_space(i2c);
+
+	/* Do not read more bytes than are still expected for this message */
+	if (bytes_in_fifo > rx_space)
+		bytes_in_fifo = rx_space;
 
 	bytes_to_read = bytes_in_fifo;
 
-	if (!i2c->dynamic) {
-		bytes_rem = xiic_rx_space(i2c) - bytes_in_fifo;
+	if (i2c->dynamic) {
+		/*
+		 * RFD is set by xiic_start_recv() and must not be changed during
+		 * a transfer, because reprogramming it may generate a spurious
+		 * SCL pulse and cause the STOP condition to be missed.
+		 */
+		bytes_rem = rx_space - bytes_in_fifo;
+
+		/*
+		 * If fewer bytes remain to arrive than are currently in the FIFO,
+		 * drain only that remaining count. The retained FIFO bytes together
+		 * with the bytes still arriving restore the FIFO occupancy to the
+		 * fixed threshold. Otherwise, drain the FIFO completely.
+		 */
+		if (bytes_rem && bytes_rem < bytes_in_fifo)
+			bytes_to_read = bytes_rem;
+	} else {
+		bytes_rem = rx_space - bytes_in_fifo;
 
 		/* Set msg length if smbus_block_read */
 		if (i2c->rx_msg->flags & I2C_M_RECV_LEN) {
@@ -636,15 +656,6 @@ static void xiic_read_rx(struct xiic_i2c *i2c)
 		i2c->rx_msg->buf[i2c->rx_pos++] =
 			xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
 	}
-
-	if (i2c->dynamic) {
-		u8 bytes;
-
-		/* Receive remaining bytes if less than fifo depth */
-		bytes = min_t(u8, xiic_rx_space(i2c), IIC_RX_FIFO_DEPTH);
-		bytes--;
-		xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, bytes);
-	}
 }
 
 static bool xiic_error_check(struct xiic_i2c *i2c)
-- 
2.44.4


             reply	other threads:[~2026-09-28  7:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-28  7:34 Vasantha Likitha T [this message]
2026-11-16  5:44 ` Tandra, Vasantha Likitha

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=20260928073425.2597782-1-vasanthalikitha.tandra@amd.com \
    --to=vasanthalikitha.tandra@amd.com \
    --cc=andi.shyti@kernel.org \
    --cc=git@amd.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.simek@amd.com \
    --cc=shubhrajyoti.datta@amd.com \
    /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

all inboxes | Powered by JetHome®