Writing 1 to DONE, ERR or CLKT in the S register clears the bit, but
bcm2835_i2c_write() did not recompute the interrupt, so with INTD set
the DONE interrupt stayed raised after the guest acknowledged it.
Linux i2c-bcm2835 clears C before it clears S and so lowers the line
anyway; a driver that clears DONE in S and keeps INTD set took the
interrupt again and again.

Update the interrupt after the write, as the C and FIFO writes do.

Test that clearing DONE lowers the interrupt.

Fixes: 9cf3bc65afdb ("hw/i2c: Implement Broadcom Serial Controller (BSC)")
Signed-off-by: Lorand Marton <[email protected]>
---
 hw/i2c/bcm2835_i2c.c           |  1 +
 tests/qtest/bcm2835-i2c-test.c | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/hw/i2c/bcm2835_i2c.c b/hw/i2c/bcm2835_i2c.c
index 5f6093087d..60f9d953c4 100644
--- a/hw/i2c/bcm2835_i2c.c
+++ b/hw/i2c/bcm2835_i2c.c
@@ -166,6 +166,7 @@ static void bcm2835_i2c_write(void *opaque, hwaddr addr,
         /* Clear DONE, CLKT and ERR by writing 1 */
         s->s &= ~(writeval & (BCM2835_I2C_S_DONE |
                   BCM2835_I2C_S_ERR | BCM2835_I2C_S_CLKT));
+        bcm2835_i2c_update_interrupt(s);
         break;
     case BCM2835_I2C_DLEN:
         s->dlen = writeval;
diff --git a/tests/qtest/bcm2835-i2c-test.c b/tests/qtest/bcm2835-i2c-test.c
index 1599194926..12c6ebdbcf 100644
--- a/tests/qtest/bcm2835-i2c-test.c
+++ b/tests/qtest/bcm2835-i2c-test.c
@@ -89,6 +89,29 @@ static void test_i2c_read_write(gconstpointer data)
 
 }
 
+/* Clearing DONE in the status register lowers the DONE interrupt */
+static void test_i2c_irq(void)
+{
+    uint32_t base_addr = bsc_base_addrs[0];
+
+    qtest_irq_intercept_out_named(global_qtest,
+                                  "/machine/soc/peripherals/bcm2835-i2c0",
+                                  "sysbus-irq");
+
+    writel(base_addr + BCM2835_I2C_A, 0x50);
+    writel(base_addr + BCM2835_I2C_DLEN, 1);
+    writel(base_addr + BCM2835_I2C_C, BCM2835_I2C_C_I2CEN |
+           BCM2835_I2C_C_INTD | BCM2835_I2C_C_ST | BCM2835_I2C_C_CLEAR);
+    writel(base_addr + BCM2835_I2C_FIFO, TMP105_REG_T_HIGH);
+    g_assert_cmphex(readl(base_addr + BCM2835_I2C_S) & BCM2835_I2C_S_DONE,
+                    ==, BCM2835_I2C_S_DONE);
+    g_assert_true(get_irq(0));
+
+    writel(base_addr + BCM2835_I2C_S, BCM2835_I2C_S_DONE);
+    g_assert_false(get_irq(0));
+    writel(base_addr + BCM2835_I2C_C, 0);
+}
+
 int main(int argc, char **argv)
 {
     int ret;
@@ -102,6 +125,7 @@ int main(int argc, char **argv)
         qtest_add_data_func(test_name, (void *)(intptr_t) i,
                             test_i2c_read_write);
     }
+    qtest_add_func("/bcm2835/bcm2835-i2c0/irq", test_i2c_irq);
 
     /* Run I2C tests with TMP105 slaves on all three buses */
     qtest_start("-M raspi3b "
-- 
2.56.0


Reply via email to