i2c: designware: fix controller is holding SCL low while ENABLE bit is disabled

It was observed that issuing the ABORT bit (IC_ENABLE[1]) will not
work when IC_ENABLE is already disabled.

Check if the ENABLE bit (IC_ENABLE[0]) is disabled when the controller
is holding SCL low. If the ENABLE bit is disabled, the software needs
to enable it before trying to issue the ABORT bit. otherwise,
the controller ignores any write to ABORT bit.

These kernel logs show up whenever an I2C transaction is
attempted after this failure.
i2c_designware e95e0000.i2c: timeout waiting for bus ready
i2c_designware e95e0000.i2c: timeout in disabling adapter

The patch fixes the issue where the controller cannot be disabled
while SCL is held low if the ENABLE bit is already disabled.

Fixes: 2409205acd3c ("i2c: designware: fix __i2c_dw_disable() in case master is holding SCL low")
Signed-off-by: Kimriver Liu <kimriver.liu@siengine.com>
Cc: <stable@vger.kernel.org> # v6.6+
Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Signed-off-by: Andi Shyti <andi.shyti@kernel.org>

authored by Kimriver Liu and committed by Andi Shyti 5d69d5a0 4e2c9cd7

+53
+14
drivers/i2c/busses/i2c-designware-common.c
··· 523 523 524 524 void __i2c_dw_disable(struct dw_i2c_dev *dev) 525 525 { 526 + struct i2c_timings *t = &dev->timings; 526 527 unsigned int raw_intr_stats; 527 528 unsigned int enable; 528 529 int timeout = 100; ··· 536 535 537 536 abort_needed = raw_intr_stats & DW_IC_INTR_MST_ON_HOLD; 538 537 if (abort_needed) { 538 + if (!(enable & DW_IC_ENABLE_ENABLE)) { 539 + regmap_write(dev->map, DW_IC_ENABLE, DW_IC_ENABLE_ENABLE); 540 + /* 541 + * Wait 10 times the signaling period of the highest I2C 542 + * transfer supported by the driver (for 400KHz this is 543 + * 25us) to ensure the I2C ENABLE bit is already set 544 + * as described in the DesignWare I2C databook. 545 + */ 546 + fsleep(DIV_ROUND_CLOSEST_ULL(10 * MICRO, t->bus_freq_hz)); 547 + /* Set ENABLE bit before setting ABORT */ 548 + enable |= DW_IC_ENABLE_ENABLE; 549 + } 550 + 539 551 regmap_write(dev->map, DW_IC_ENABLE, enable | DW_IC_ENABLE_ABORT); 540 552 ret = regmap_read_poll_timeout(dev->map, DW_IC_ENABLE, enable, 541 553 !(enable & DW_IC_ENABLE_ABORT), 10,
+1
drivers/i2c/busses/i2c-designware-core.h
··· 108 108 DW_IC_INTR_RX_UNDER | \ 109 109 DW_IC_INTR_RD_REQ) 110 110 111 + #define DW_IC_ENABLE_ENABLE BIT(0) 111 112 #define DW_IC_ENABLE_ABORT BIT(1) 112 113 113 114 #define DW_IC_STATUS_ACTIVITY BIT(0)
+38
drivers/i2c/busses/i2c-designware-master.c
··· 271 271 __i2c_dw_write_intr_mask(dev, DW_IC_INTR_MASTER_MASK); 272 272 } 273 273 274 + /* 275 + * This function waits for the controller to be idle before disabling I2C 276 + * When the controller is not in the IDLE state, the MST_ACTIVITY bit 277 + * (IC_STATUS[5]) is set. 278 + * 279 + * Values: 280 + * 0x1 (ACTIVE): Controller not idle 281 + * 0x0 (IDLE): Controller is idle 282 + * 283 + * The function is called after completing the current transfer. 284 + * 285 + * Returns: 286 + * False when the controller is in the IDLE state. 287 + * True when the controller is in the ACTIVE state. 288 + */ 289 + static bool i2c_dw_is_controller_active(struct dw_i2c_dev *dev) 290 + { 291 + u32 status; 292 + 293 + regmap_read(dev->map, DW_IC_STATUS, &status); 294 + if (!(status & DW_IC_STATUS_MASTER_ACTIVITY)) 295 + return false; 296 + 297 + return regmap_read_poll_timeout(dev->map, DW_IC_STATUS, status, 298 + !(status & DW_IC_STATUS_MASTER_ACTIVITY), 299 + 1100, 20000) != 0; 300 + } 301 + 274 302 static int i2c_dw_check_stopbit(struct dw_i2c_dev *dev) 275 303 { 276 304 u32 val; ··· 833 805 i2c_dw_init_master(dev); 834 806 goto done; 835 807 } 808 + 809 + /* 810 + * This happens rarely (~1:500) and is hard to reproduce. Debug trace 811 + * showed that IC_STATUS had value of 0x23 when STOP_DET occurred, 812 + * if disable IC_ENABLE.ENABLE immediately that can result in 813 + * IC_RAW_INTR_STAT.MASTER_ON_HOLD holding SCL low. Check if 814 + * controller is still ACTIVE before disabling I2C. 815 + */ 816 + if (i2c_dw_is_controller_active(dev)) 817 + dev_err(dev->dev, "controller active\n"); 836 818 837 819 /* 838 820 * We must disable the adapter before returning and signaling the end