mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] i2c: xiic: Rework dynamic-mode RX FIFO drain in xiic_read_rx
@ 2026-09-28  7:34 Vasantha Likitha T
  2026-11-16  5:44 ` Tandra, Vasantha Likitha
  0 siblings, 1 reply; 2+ messages in thread
From: Vasantha Likitha T @ 2026-09-28  7:34 UTC (permalink / raw)
  To: linux-i2c
  Cc: michal.simek, andi.shyti, linux-kernel, git, Shubhrajyoti Datta,
	Vasantha Likitha T

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] i2c: xiic: Rework dynamic-mode RX FIFO drain in xiic_read_rx
  2026-09-28  7:34 [PATCH v2] i2c: xiic: Rework dynamic-mode RX FIFO drain in xiic_read_rx Vasantha Likitha T
@ 2026-11-16  5:44 ` Tandra, Vasantha Likitha
  0 siblings, 0 replies; 2+ messages in thread
From: Tandra, Vasantha Likitha @ 2026-11-16  5:44 UTC (permalink / raw)
  To: linux-i2c; +Cc: michal.simek, andi.shyti, linux-kernel, git, Shubhrajyoti Datta

Hi Andi,

Could you please review the patch?

Thanks,
Vasantha Likitha

On 9/28/2026 1:04 PM, Vasantha Likitha T wrote:
> 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)


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-16  5:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-28  7:34 [PATCH v2] i2c: xiic: Rework dynamic-mode RX FIFO drain in xiic_read_rx Vasantha Likitha T
2026-11-16  5:44 ` Tandra, Vasantha Likitha

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®