Skip to content

Commit 1d4a1ad

Browse files
robhancocksedAndi Shyti
authored andcommitted
i2c: xiic: Try re-initialization on bus busy timeout
In the event that the I2C bus was powered down when the I2C controller driver loads, or some spurious pulses occur on the I2C bus, it's possible that the controller detects a spurious I2C "start" condition. In this situation it may continue to report the bus is busy indefinitely and block the controller from working. The "single-master" DT flag can be specified to disable bus busy checks entirely, but this may not be safe to use in situations where other I2C masters may potentially exist. In the event that the controller reports "bus busy" for too long when starting a transaction, we can try reinitializing the controller to see if the busy condition clears. This allows recovering from this scenario. Fixes: e1d5b65 ("i2c: Add support for Xilinx XPS IIC Bus Interface") Signed-off-by: Robert Hancock <[email protected]> Cc: <[email protected]> # v2.6.34+ Reviewed-by: Manikanta Guntupalli <[email protected]> Acked-by: Michal Simek <[email protected]> Signed-off-by: Andi Shyti <[email protected]>
1 parent 521da1e commit 1d4a1ad

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

drivers/i2c/busses/i2c-xiic.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -843,23 +843,11 @@ static int xiic_bus_busy(struct xiic_i2c *i2c)
843843
return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;
844844
}
845845

846-
static int xiic_busy(struct xiic_i2c *i2c)
846+
static int xiic_wait_not_busy(struct xiic_i2c *i2c)
847847
{
848848
int tries = 3;
849849
int err;
850850

851-
if (i2c->tx_msg || i2c->rx_msg)
852-
return -EBUSY;
853-
854-
/* In single master mode bus can only be busy, when in use by this
855-
* driver. If the register indicates bus being busy for some reason we
856-
* should ignore it, since bus will never be released and i2c will be
857-
* stuck forever.
858-
*/
859-
if (i2c->singlemaster) {
860-
return 0;
861-
}
862-
863851
/* for instance if previous transfer was terminated due to TX error
864852
* it might be that the bus is on it's way to become available
865853
* give it at most 3 ms to wake
@@ -1103,13 +1091,36 @@ static int xiic_start_xfer(struct xiic_i2c *i2c, struct i2c_msg *msgs, int num)
11031091

11041092
mutex_lock(&i2c->lock);
11051093

1106-
ret = xiic_busy(i2c);
1107-
if (ret) {
1094+
if (i2c->tx_msg || i2c->rx_msg) {
11081095
dev_err(i2c->adap.dev.parent,
11091096
"cannot start a transfer while busy\n");
1097+
ret = -EBUSY;
11101098
goto out;
11111099
}
11121100

1101+
/* In single master mode bus can only be busy, when in use by this
1102+
* driver. If the register indicates bus being busy for some reason we
1103+
* should ignore it, since bus will never be released and i2c will be
1104+
* stuck forever.
1105+
*/
1106+
if (!i2c->singlemaster) {
1107+
ret = xiic_wait_not_busy(i2c);
1108+
if (ret) {
1109+
/* If the bus is stuck in a busy state, such as due to spurious low
1110+
* pulses on the bus causing a false start condition to be detected,
1111+
* then try to recover by re-initializing the controller and check
1112+
* again if the bus is still busy.
1113+
*/
1114+
dev_warn(i2c->adap.dev.parent, "I2C bus busy timeout, reinitializing\n");
1115+
ret = xiic_reinit(i2c);
1116+
if (ret)
1117+
goto out;
1118+
ret = xiic_wait_not_busy(i2c);
1119+
if (ret)
1120+
goto out;
1121+
}
1122+
}
1123+
11131124
i2c->tx_msg = msgs;
11141125
i2c->rx_msg = NULL;
11151126
i2c->nmsgs = num;

0 commit comments

Comments
 (0)