Skip to contentSkip to conversation

i2c: imx: do not acknowledge the last byte of a block read of one byte

PATCH Superseded Mehmet Fide 2 participants 3 messages Nov 16

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: imx: do not acknowledge the last byte of a block read of one byte

From: Mehmet Fide <[email protected]>

In the interrupt driven read, the read of I2DR that takes the count byte of an SMBus block read starts the reception of the next byte. With a count of 1 that byte is the last one, but I2CR_TXAK is only set in i2c_imx_isr_read_continue() for the byte after it, so the last byte is acknowledged and the target goes on sending. The read fails with -ETIMEDOUT, the bus stays busy and the following transfers on it fail too.

Set I2CR_TXAK after taking a count of 1, as the read before the interrupt driven state machine did and the atomic read still does.

Fixes 5f5c2d4579ca ("i2c: imx: prevent rescheduling in non dma mode")
Cc [email protected] # v6.13+

---

Notes:
Tested on a Colibri VF50 on the Iris carrier, kernel at this base plus "i2c: imx: acknowledge the count byte of an atomic block read", with the M41T0 RTC at 0x68. A test-only hook passed the messages that i2c_smbus_xfer_emulated() builds for a block read to the adapter, alone and followed by a one byte read. Register 0x05 (0x01) as count byte:
without the patch 40 of 40 failed (35 -ETIMEDOUT, 2 -EAGAIN, 3 -EINVAL), after which the RTC driver could not read the time any more; with it 01 01 and, in the second case, the byte after it, 200 of 200. A count of 0x13 (register 0x04) reads the same bytes as before.

 drivers/i2c/busses/i2c-imx.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/i2c/busses/i2c-imx.c +6 -0
index ccf08cd63151..0a59ebe70eb0 100644
@@ -1095,6 +1095,12 @@ static inline void i2c_imx_isr_read_block_data_len(struct imx_i2c_struct *i2c_im
 	}
 	i2c_imx->msg->len += len;
 	i2c_imx->msg->buf[i2c_imx->msg_buf_idx++] = len;
+
+	if (len == 1) {
+		temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+		temp |= I2CR_TXAK;
+		imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
+	}
 }
 
 static irqreturn_t i2c_imx_master_isr(struct imx_i2c_struct *i2c_imx, unsigned int status)

base-commit: 25e7ab89f4774b0f842cee0e8a20c11f755924d5

Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] i2c: imx: premature NACK in i2c_imx_isr_read_block_data_len() with PEC --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] i2c: imx: premature NACK in i2c_imx_isr_read_block_data_len() with PEC
--

commit 5f9d6cebf7f9ee55cbfb82a72f126d816180c9da
Author: Mehmet Fide <[email protected]>

i2c: imx: do not acknowledge the last byte of a block read of one byte

This commit sets I2CR_TXAK in i2c_imx_isr_read_block_data_len() when the received SMBus block length byte is 1 so that the following byte is not acknowledged.

Quoted 5 lines
drivers/i2c/busses/i2c-imx.c +0 -0
index ccf08cd63151f..0a59ebe70eb00 100644

drivers/i2c/busses/i2c-imx.c static inline void i2c_imx_isr_read_block_data_len(struct imx_i2c_struct *i2c_im · line 1104

Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +6 -0
@@ -1095,6 +1095,12 @@ static inline void i2c_imx_isr_read_block_data_len(struct imx_i2c_struct *i2c_im
 	}
 	i2c_imx->msg->len += len;
 	i2c_imx->msg->buf[i2c_imx->msg_buf_idx++] = len;
+
+	if (len == 1) {
+		temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+		temp |= I2CR_TXAK;
+		imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
+	}
 }

[Severity: Medium]
Does checking len == 1 rather than i2c_imx->msg->len == 2 prematurely NACK the data byte on a 1-byte SMBus block read with PEC enabled?

When I2C_CLIENT_PEC is set, i2c_smbus_xfer_emulated() increments the initial read msg->len from 1 to 2 to account for the trailing PEC byte. A block count of 1 then updates i2c_imx->msg->len to 3 (count, data[0], PEC), so data[0] must be ACKed and I2CR_TXAK should only be set on the next interrupt in i2c_imx_isr_read_continue(). Setting I2CR_TXAK when len == 1 NACKs data[0] instead of the PEC byte, causing the target to stop transmitting and the read to fail with -EBADMSG.

Checking i2c_imx->msg->len == 2 (matching the i == (msgs->len - 2) check at i == 0 in i2c_imx_atomic_read()) avoids NACKing early when PEC is present.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1