Skip to contentSkip to conversation

i2c: i2c-xiic: fix clearing of bnb irq

PATCH Superseded Thilo Zirnstein 3 participants 5 messages Oct 15 – Oct 16

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover i2c: i2c-xiic: fix clearing of bnb irq

Dear all,

This patch fixes the clearing routine for the bus not busy (bnb) irq. The bnb is used to indicate that the current transfer is done. A high level view of the software flow for tx:

* init i2c controller
* prepare all registers ( fill fifo, enable irqs, ... )
* start i2c txfer
* clear and enable bnb
* wait for completion
* ( serve interrupts )
* ( bnb interrupt ): complete completion

The datasheet ( pg090-axi-iic, Interrupt-Status-Register ISR) states that the bnb bit can only be cleared if
the bus busy bit in the status register is set.
So the iic controller must have recognized that their is some activity on the bus. ( Start condition )

Currently the bnb bit is cleared without checking or waiting for this condition. This can lead to spurious bnb interrupts.

I am kind of unsure which branch to use for handing in my patch, this one is based i2c/i2c-fixes.

Kind regards,

Thilo Zirnstein
---

Changes in v2:
- Unintentional changes removed .

Thilo Zirnstein (1):
i2c: i2c-xiic: fix clearing of bnb irq

 drivers/i2c/busses/i2c-xiic.c | 42 +++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/1 i2c: i2c-xiic: fix clearing of bnb irq

Add a function for clearing the bnb bit,
It waits for all preconditions before clearing the bnb bit or times out.

Tested on Zynqmp dev board with some custom logic.

---
 drivers/i2c/busses/i2c-xiic.c | 42 +++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 9 deletions(-)
drivers/i2c/busses/i2c-xiic.c +33 -9
index 5e397a7e63f6..da9cedb86f94 100644
@@ -230,7 +230,9 @@ static const struct timing_regs timing_reg_values[] = {
 
 #define XIIC_RESET_MASK             0xAUL
 
-#define XIIC_PM_TIMEOUT		1000	/* ms */
+#define XIIC_PM_TIMEOUT 1000 /* ms */
+/* timeout waiting for bus busy */
+#define XIIC_BB_TIMEOUT		(msecs_to_jiffies(1))
 /* timeout waiting for the controller to respond */
 #define XIIC_I2C_TIMEOUT	(msecs_to_jiffies(1000))
 /* timeout waiting for the controller finish transfers */
@@ -392,6 +394,30 @@ static int xiic_wait_tx_empty(struct xiic_i2c *i2c)
 	return 0;
 }
 
+static int xiic_clear_en_bnb(struct xiic_i2c *i2c)
+{
+	u8 sr = 0, isr = 0;
+	unsigned long timeout;
+
+	timeout = jiffies + XIIC_BB_TIMEOUT;
+	for (isr = xiic_getreg8(i2c, XIIC_IISR_OFFSET);
+		(isr & XIIC_INTR_BNB_MASK);
+		isr = xiic_getreg8(i2c, XIIC_IISR_OFFSET)) {
+
+		sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
+		if (sr & XIIC_SR_BUS_BUSY_MASK)
+			xiic_setreg8(i2c, XIIC_IISR_OFFSET, XIIC_INTR_BNB_MASK);
+
+		if (time_after(jiffies, timeout)) {
+			dev_err(i2c->dev, "Failed to clear bnb\n");
+			return -ETIMEDOUT;
+		}
+	}
+
+	xiic_irq_en(i2c, XIIC_INTR_BNB_MASK);
+	return 0;
+}
+
 /**
  * xiic_setclk - Sets the configured clock rate
  * @i2c: Pointer to the xiic device structure
@@ -881,9 +907,6 @@ static irqreturn_t xiic_process(int irq, void *dev_id)
 	}
 
 	if (pend & XIIC_INTR_BNB_MASK) {
-		/* IIC bus has transitioned to not busy */
-		clr |= XIIC_INTR_BNB_MASK;
-
 		/* The bus is not busy, disable BusNotBusy interrupt */
 		xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
 
@@ -1028,7 +1051,7 @@ static void xiic_start_recv(struct xiic_i2c *i2c)
 
 		xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, val);
 
-		xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
+		xiic_clear_en_bnb(i2c);
 	} else {
 		/*
 		 * If previous message is Tx, make sure that Tx FIFO is empty
@@ -1095,7 +1118,7 @@ static void xiic_start_recv(struct xiic_i2c *i2c)
 
 	if (i2c->nmsgs == 1)
 		/* very last, enable bus not busy as well */
-		xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
+		xiic_clear_en_bnb(i2c);
 
 	/* the message is tx:ed */
 	i2c->tx_pos = msg->len;
@@ -1179,11 +1202,11 @@ static void xiic_start_send(struct xiic_i2c *i2c)
 		/* Clear any pending Tx empty, Tx Error and then enable them */
 		xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK |
 				XIIC_INTR_TX_ERROR_MASK |
-				XIIC_INTR_BNB_MASK |
 				((i2c->nmsgs > 1 || xiic_tx_space(i2c)) ?
 				XIIC_INTR_TX_HALF_MASK : 0));
 
 		xiic_fill_tx_fifo(i2c);
+		xiic_clear_en_bnb(i2c);
 	} else {
 		/*
 		 * If previous message is Tx, make sure that Tx FIFO is empty
@@ -1225,8 +1248,9 @@ static void xiic_start_send(struct xiic_i2c *i2c)
 
 		/* Clear any pending Tx empty, Tx Error and then enable them */
 		xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK |
-				XIIC_INTR_TX_ERROR_MASK |
-				XIIC_INTR_BNB_MASK);
+						XIIC_INTR_TX_ERROR_MASK);
+
+		xiic_clear_en_bnb(i2c);
 	}
 
 	i2c->prev_msg_tx = true;
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Hi Thilo,

Quoted 6 lines · Thilo Zirnstein

Add a function for clearing the bnb bit,
It waits for all preconditions before clearing the bnb bit or times out.

Tested on Zynqmp dev board with some custom logic.

Please, don't leave blank lines between tags. Moreover in this case I don't need your Tested-by, I believe you tested your patch before sending.

Quoted 1 line

I'd need here an ack from Michal.

Quoted 9 lines
---
 drivers/i2c/busses/i2c-xiic.c | 42 +++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 9 deletions(-)
drivers/i2c/busses/i2c-xiic.c +0 -0
index 5e397a7e63f6..da9cedb86f94 100644

drivers/i2c/busses/i2c-xiic.c static const struct timing_regs timing_reg_values[] = { · line 233

Quoted 5 lines
drivers/i2c/busses/i2c-xiic.c +1 -1
@@ -230,7 +230,9 @@ static const struct timing_regs timing_reg_values[] = {
 
 #define XIIC_RESET_MASK             0xAUL
 
-#define XIIC_PM_TIMEOUT		1000	/* ms */
+#define XIIC_PM_TIMEOUT 1000 /* ms */

Why this change?

drivers/i2c/busses/i2c-xiic.c static const struct timing_regs timing_reg_values[] = { · line 235

Quoted 2 lines
drivers/i2c/busses/i2c-xiic.c +2 -0
+/* timeout waiting for bus busy */
+#define XIIC_BB_TIMEOUT		(msecs_to_jiffies(1))

I don't see much benefit from this define as it's used only once. It just makes it more difficult to read.

drivers/i2c/busses/i2c-xiic.c static int xiic_wait_tx_empty(struct xiic_i2c *i2c) · line 397

Quoted 8 lines
drivers/i2c/busses/i2c-xiic.c +1 -0
 /* timeout waiting for the controller to respond */
 #define XIIC_I2C_TIMEOUT	(msecs_to_jiffies(1000))
 /* timeout waiting for the controller finish transfers */
@@ -392,6 +394,30 @@ static int xiic_wait_tx_empty(struct xiic_i2c *i2c)
 	return 0;
 }
 
+static int xiic_clear_en_bnb(struct xiic_i2c *i2c)

Why do you need to return an error value if you ignore it everywhere?

drivers/i2c/busses/i2c-xiic.c static int xiic_wait_tx_empty(struct xiic_i2c *i2c) · line 405

Quoted 8 lines
drivers/i2c/busses/i2c-xiic.c +8 -0
+{
+	u8 sr = 0, isr = 0;
+	unsigned long timeout;
+
+	timeout = jiffies + XIIC_BB_TIMEOUT;
+	for (isr = xiic_getreg8(i2c, XIIC_IISR_OFFSET);
+		(isr & XIIC_INTR_BNB_MASK);
+		isr = xiic_getreg8(i2c, XIIC_IISR_OFFSET)) {

this for loop is quite ugly. I believe that with a do...while() you can write a clearer code.

drivers/i2c/busses/i2c-xiic.c static int xiic_wait_tx_empty(struct xiic_i2c *i2c) · line 411

Quoted 6 lines
drivers/i2c/busses/i2c-xiic.c +6 -0
+
+		sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
+		if (sr & XIIC_SR_BUS_BUSY_MASK)
+			xiic_setreg8(i2c, XIIC_IISR_OFFSET, XIIC_INTR_BNB_MASK);
+
+		if (time_after(jiffies, timeout)) {

If this is called in a context where interrupts are disabled, jiffies might not be incremented and you end up looping forever. How about using read_poll_timeout_atomic() instead?

Quoted 12 lines
drivers/i2c/busses/i2c-xiic.c +9 -0
+			dev_err(i2c->dev, "Failed to clear bnb\n");
+			return -ETIMEDOUT;
+		}
+	}
+
+	xiic_irq_en(i2c, XIIC_INTR_BNB_MASK);
+	return 0;
+}
+
 /**
  * xiic_setclk - Sets the configured clock rate
  * @i2c: Pointer to the xiic device structure

...

drivers/i2c/busses/i2c-xiic.c static void xiic_start_send(struct xiic_i2c *i2c) · line 1252

Quoted 8 lines
drivers/i2c/busses/i2c-xiic.c +2 -2
@@ -1225,8 +1248,9 @@ static void xiic_start_send(struct xiic_i2c *i2c)
 
 		/* Clear any pending Tx empty, Tx Error and then enable them */
 		xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK |
-				XIIC_INTR_TX_ERROR_MASK |
-				XIIC_INTR_BNB_MASK);
+						XIIC_INTR_TX_ERROR_MASK);
+

this needs to be aligned properly.

Andi

Quoted 7 lines
drivers/i2c/busses/i2c-xiic.c +1 -0
+		xiic_clear_en_bnb(i2c);
 	}
 
 	i2c->prev_msg_tx = true;
Signature · 1 line
2.43.0
#3 lore raw

Hi Andi,

Thanks for your review and feedback.

Quoted 6 lines · Andi Shyti

Hi Thilo,

Quoted 3 lines · Thilo Zirnstein

Add a function for clearing the bnb bit,
It waits for all preconditions before clearing the bnb bit or times out.

Quoted 7 lines
Quoted 3 lines

Tested on Zynqmp dev board with some custom logic.

Please, don't leave blank lines between tags. Moreover in this case I don't need your Tested-by, I believe you tested your patch before sending.

Ack

Quoted 4 lines
Quoted 2 lines

I'd need here an ack from Michal.

Ok, but this is nothing i can steer somehow - right ?

Quoted 11 lines
Quoted 11 lines
---
 drivers/i2c/busses/i2c-xiic.c | 42 +++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 9 deletions(-)
drivers/i2c/busses/i2c-xiic.c +0 -0
index 5e397a7e63f6..da9cedb86f94 100644
@@ -230,7 +230,9 @@ static const struct timing_regs timing_reg_values[] = {
 
Quoted 6 lines

drivers/i2c/busses/i2c-xiic.c static const struct timing_regs timing_reg_values[] = { · line 233

Quoted 4 lines
drivers/i2c/busses/i2c-xiic.c +1 -1
 #define XIIC_RESET_MASK             0xAUL
 
-#define XIIC_PM_TIMEOUT		1000	/* ms */
+#define XIIC_PM_TIMEOUT 1000 /* ms */

Why this change?

Unintentionally, :/

Quoted 6 lines

drivers/i2c/busses/i2c-xiic.c static const struct timing_regs timing_reg_values[] = { · line 235

Quoted 3 lines
drivers/i2c/busses/i2c-xiic.c +2 -0
+/* timeout waiting for bus busy */
+#define XIIC_BB_TIMEOUT		(msecs_to_jiffies(1))

I don't see much benefit from this define as it's used only once. It just makes it more difficult to read.

I think it is a question of personal preference,
I prefer to read a meaningfull name instead of a "magical" number. The driver code has some other defines which are just used one time ( XIIC_PM_TIMEOUT, XIIC_XFER_TIMEOUT_US ), so it fits to the code base too.

If you not insist, i keep it.

Quoted 13 lines

drivers/i2c/busses/i2c-xiic.c static int xiic_wait_tx_empty(struct xiic_i2c *i2c) · line 397

Quoted 9 lines
drivers/i2c/busses/i2c-xiic.c +1 -0
 /* timeout waiting for the controller to respond */
 #define XIIC_I2C_TIMEOUT	(msecs_to_jiffies(1000))
 /* timeout waiting for the controller finish transfers */
@@ -392,6 +394,30 @@ static int xiic_wait_tx_empty(struct xiic_i2c *i2c)
 	return 0;
 }
 
+static int xiic_clear_en_bnb(struct xiic_i2c *i2c)

Why do you need to return an error value if you ignore it everywhere?

Quoted 11 lines

drivers/i2c/busses/i2c-xiic.c static int xiic_wait_tx_empty(struct xiic_i2c *i2c) · line 405

Quoted 8 lines
drivers/i2c/busses/i2c-xiic.c +8 -0
+{
+	u8 sr = 0, isr = 0;
+	unsigned long timeout;
+
+	timeout = jiffies + XIIC_BB_TIMEOUT;
+	for (isr = xiic_getreg8(i2c, XIIC_IISR_OFFSET);
+		(isr & XIIC_INTR_BNB_MASK);
+		isr = xiic_getreg8(i2c, XIIC_IISR_OFFSET)) {

this for loop is quite ugly. I believe that with a do...while() you can write a clearer code.

Ack, I can get rid of this whole loop if using
read_poll_timeout_atomic()
A readback after clearing the bit is not needed.

Quoted 11 lines

drivers/i2c/busses/i2c-xiic.c static int xiic_wait_tx_empty(struct xiic_i2c *i2c) · line 411

Quoted 7 lines
drivers/i2c/busses/i2c-xiic.c +6 -0
+
+		sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
+		if (sr & XIIC_SR_BUS_BUSY_MASK)
+			xiic_setreg8(i2c, XIIC_IISR_OFFSET, XIIC_INTR_BNB_MASK);
+
+		if (time_after(jiffies, timeout)) {

If this is called in a context where interrupts are disabled, jiffies might not be incremented and you end up looping forever. How about using read_poll_timeout_atomic() instead?

Ack, good catch, i will fix that.

Quoted 18 lines
Quoted 13 lines
drivers/i2c/busses/i2c-xiic.c +9 -0
+			dev_err(i2c->dev, "Failed to clear bnb\n");
+			return -ETIMEDOUT;
+		}
+	}
+
+	xiic_irq_en(i2c, XIIC_INTR_BNB_MASK);
+	return 0;
+}
+
 /**
  * xiic_setclk - Sets the configured clock rate
  * @i2c: Pointer to the xiic device structure

...

Quoted 2 lines
drivers/i2c/busses/i2c-xiic.c +0 -0
@@ -1225,8 +1248,9 @@ static void xiic_start_send(struct xiic_i2c *i2c)
 
Quoted 8 lines

drivers/i2c/busses/i2c-xiic.c static void xiic_start_send(struct xiic_i2c *i2c) · line 1252

Quoted 6 lines
drivers/i2c/busses/i2c-xiic.c +2 -2
 		/* Clear any pending Tx empty, Tx Error and then enable them */
 		xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK |
-				XIIC_INTR_TX_ERROR_MASK |
-				XIIC_INTR_BNB_MASK);
+						XIIC_INTR_TX_ERROR_MASK);
+

this needs to be aligned properly.

Ack

Quoted 10 lines

Andi

Quoted 7 lines
drivers/i2c/busses/i2c-xiic.c +1 -0
+		xiic_clear_en_bnb(i2c);
 	}
 
 	i2c->prev_msg_tx = true;
Signature · 1 line
2.43.0

Kind regards,

Thilo

maintainer
#4 lore raw

+Shubhrajyoti, Venkatesh

Quoted 19 lines · Thilo Zirnstein

Hi Andi,

Thanks for your review and feedback.

Quoted 6 lines · Andi Shyti

Hi Thilo,

Quoted 3 lines · Thilo Zirnstein

Add a function for clearing the bnb bit,
It waits for all preconditions before clearing the bnb bit or times out.

Quoted 7 lines
Quoted 3 lines

Tested on Zynqmp dev board with some custom logic.

Please, don't leave blank lines between tags. Moreover in this case I don't need your Tested-by, I believe you tested your patch before sending.

Quoted 8 lines

Ack

Quoted 4 lines
Quoted 2 lines

I'd need here an ack from Michal.

Ok, but this is nothing i can steer somehow - right ?

Just give us some time to look at it.

Shubhrajyoti: Can you please look at the patch?

Venkatesh: Can you please test it on boards?

Thanks,
Michal