Skip to contentSkip to conversation

i2c: axxia: Add bus recovery functionality

PATCH Superseded Alexander Sverdlin 5 participants 7 messages Apr 28, 2015 – May 13, 2015 5 messages not in the archive I2C subsystem host drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: axxia: Add bus recovery functionality

Use recovery framework and implement bus recovery using "Bus Monitor" register. Tests show that shortening SCL to GND results in "completion" timeout with "BUSY" bit still set. So initiate recovery in case of generic timeout and "Arbitration Lost" condition.

---
 drivers/i2c/busses/i2c-axxia.c |   44 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
drivers/i2c/busses/i2c-axxia.c +44 -0
index ad0ad15..070fd31 100644
@@ -42,6 +42,10 @@
 #define IBML_LOW_SEXT		0x18
 #define TIMER_CLOCK_DIV		0x1c
 #define I2C_BUS_MONITOR		0x20
+#define   BM_SDAC		BIT(3)
+#define   BM_SCLC		BIT(2)
+#define   BM_SDAS		BIT(1)
+#define   BM_SCLS		BIT(0)
 #define SOFT_RESET		0x24
 #define MST_COMMAND		0x28
 #define   CMD_BUSY		(1<<3)
@@ -394,6 +398,12 @@ static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg)
 	if (time_left == 0)
 		idev->msg_err = -ETIMEDOUT;
 
+	switch (idev->msg_err) {
+	case -ETIMEDOUT:
+	case -EAGAIN:
+		i2c_recover_bus(&idev->adapter);
+	}
+
 	if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO)
 		axxia_i2c_init(idev);
 
@@ -437,6 +447,39 @@ axxia_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
 	return ret ? : i;
 }
 
+static int axxia_i2c_get_scl(struct i2c_adapter *adap)
+{
+	struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
+
+	return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SCLS);
+}
+
+static void axxia_i2c_set_scl(struct i2c_adapter *adap, int val)
+{
+	struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
+	u32 tmp;
+
+	/* Preserve SDA Control */
+	tmp = readl(idev->base + I2C_BUS_MONITOR) & BM_SDAC;
+	if (!val)
+		tmp |= BM_SCLC;
+	writel(tmp, idev->base + I2C_BUS_MONITOR);
+}
+
+static int axxia_i2c_get_sda(struct i2c_adapter *adap)
+{
+	struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
+
+	return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SDAS);
+}
+
+static struct i2c_bus_recovery_info axxia_i2c_recovery_info = {
+	.recover_bus = i2c_generic_scl_recovery,
+	.get_scl = axxia_i2c_get_scl,
+	.set_scl = axxia_i2c_set_scl,
+	.get_sda = axxia_i2c_get_sda,
+};
+
 static u32 axxia_i2c_func(struct i2c_adapter *adap)
 {
 	u32 caps = (I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
@@ -511,6 +554,7 @@ static int axxia_i2c_probe(struct platform_device *pdev)
 	strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name));
 	idev->adapter.owner = THIS_MODULE;
 	idev->adapter.algo = &axxia_i2c_algo;
+	idev->adapter.bus_recovery_info = &axxia_i2c_recovery_info;
 	idev->adapter.quirks = &axxia_i2c_quirks;
 	idev->adapter.dev.parent = &pdev->dev;
 	idev->adapter.dev.of_node = pdev->dev.of_node;
#2 lore raw
In reply to a message not in the archive
Quoted 4 lines · Alexander Sverdlin

Use recovery framework and implement bus recovery using "Bus Monitor" register. Tests show that shortening SCL to GND results in "completion" timeout with "BUSY" bit still set. So initiate recovery in case of generic timeout and "Arbitration Lost" condition.

So, how could you verify in this setup if the recovery actually worked?

drivers/i2c/busses/i2c-axxia.c static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg) · line 406

Quoted 6 lines
drivers/i2c/busses/i2c-axxia.c +6 -0
+	switch (idev->msg_err) {
+	case -ETIMEDOUT:
+	case -EAGAIN:
+		i2c_recover_bus(&idev->adapter);
+	}
+

Doesn't look right. -EAGAIN means arbitration lost which is not a condition to reset the bus in a multi-master setup.

Other than that, it looks like a nice example of using this framework. Just a few little helper functions and the core will do the rest.

#4 lore raw
In reply to a message not in the archive

Am 12.05.2015 um 21:12 schrieb Wolfram Sang:

Quoted 2 lines

Doesn't look right. -EAGAIN means arbitration lost which is not a condition to reset the bus in a multi-master setup.

IMHO in multi-master scenario bus recovery is never save. There is need of higher level synchronization to find out whether any master is active.

Signature · 1 line
Michael
#6 lore raw
In reply to a message not in the archive
Quoted 7 lines · Michael Lawnick

Am 12.05.2015 um 21:12 schrieb Wolfram Sang:

Quoted 2 lines

Doesn't look right. -EAGAIN means arbitration lost which is not a condition to reset the bus in a multi-master setup.

IMHO in multi-master scenario bus recovery is never save. There is need of higher level synchronization to find out whether any master is active.

Right. But you might be lucky and it's ok when done on error conditions like "sda is low since 300 ms". But on arbitration loss it's definitely wrong. Consider two masters where one resets the bus while the other does a transfer. That results in a arbitration loss for the latter. So the latter resets the bus and in the meantime the first one does a transfer now that the bus should be free. You see the loop?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#8 lore raw
In reply to a message not in the archive

Am 13.05.2015 um 08:55 schrieb ext Uwe Kleine-König:

Quoted 8 lines · Michael Lawnick
Quoted 7 lines

Am 12.05.2015 um 21:12 schrieb Wolfram Sang:

Quoted 2 lines

Doesn't look right. -EAGAIN means arbitration lost which is not a condition to reset the bus in a multi-master setup.

IMHO in multi-master scenario bus recovery is never save. There is need of higher level synchronization to find out whether any master is active.

Quoted 6 lines

Right. But you might be lucky and it's ok when done on error conditions like "sda is low since 300 ms". But on arbitration loss it's definitely wrong. Consider two masters where one resets the bus while the other does a transfer. That results in a arbitration loss for the latter. So the latter resets the bus and in the meantime the first one does a transfer now that the bus should be free. You see the loop?

Of course.
The question is: do/can controllers properly distinguish between arbitration loss and permanent SDL low? I always worked with single master assumption in last 10 years and /feel/ /to/ /remember/ to have observed situations were bus lock was signaled as arbitration loss.

I'm simply in doubt that multi master can be handled at driver level at all. This might mean in consequence that unlocking the bus should not be done automatically, but by an extra I2C method triggered by application level.

Signature · 2 lines
JM2C
Michael
#10 lore raw
In reply to a message not in the archive

Hello,

Quoted 16 lines · Michael Lawnick

Am 13.05.2015 um 08:55 schrieb ext Uwe Kleine-König:

Quoted 8 lines · Michael Lawnick
Quoted 7 lines

Am 12.05.2015 um 21:12 schrieb Wolfram Sang:

Quoted 2 lines

Doesn't look right. -EAGAIN means arbitration lost which is not a condition to reset the bus in a multi-master setup.

IMHO in multi-master scenario bus recovery is never save. There is need of higher level synchronization to find out whether any master is active.

Quoted 6 lines

Right. But you might be lucky and it's ok when done on error conditions like "sda is low since 300 ms". But on arbitration loss it's definitely wrong. Consider two masters where one resets the bus while the other does a transfer. That results in a arbitration loss for the latter. So the latter resets the bus and in the meantime the first one does a transfer now that the bus should be free. You see the loop?

Quoted 6 lines

Of course.
The question is: do/can controllers properly distinguish between arbitration loss and permanent SDL low? I always worked with single master assumption in last 10 years and /feel/ /to/ /remember/ to have observed situations were bus lock was signaled as arbitration loss.

yeah, that might be true for some (or even most) controllers. Still from a high-level view "arbitration lost" means the controller started the transfer and in the middle observed a low SDA while it doesn't pull it low. If SDA is stuck low instead the controller cannot even put a start condition on the bus.

Quoted 4 lines

I'm simply in doubt that multi master can be handled at driver level at all. This might mean in consequence that unlocking the bus should not be done automatically, but by an extra I2C method triggered by application level.

At least it should not automatically triggered on error conditions that might result from another master on the bus. IMHO doing it once at probe time and then only when SDA is stuck low for say 1s should be fine.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#11 lore raw
In reply to a message not in the archive

Hi!

Quoted 5 lines · ext Wolfram Sang
Quoted 4 lines

Use recovery framework and implement bus recovery using "Bus Monitor"

Quoted 3 lines

register. Tests show that shortening SCL to GND results in "completion" timeout with "BUSY" bit still set. So initiate recovery in case of generic timeout and "Arbitration Lost" condition.

So, how could you verify in this setup if the recovery actually worked?

Yes, it's tested with jumpers and oscilloscope on a custom Axxia AXM5508-based board. 9 CLK pulses are there and it's also jumping out of recovery if SDA is not low any more.

Quoted 8 lines
Quoted 6 lines

drivers/i2c/busses/i2c-axxia.c static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg) · line 406

Quoted 6 lines
drivers/i2c/busses/i2c-axxia.c +6 -0
+	switch (idev->msg_err) {
+	case -ETIMEDOUT:
+	case -EAGAIN:
+		i2c_recover_bus(&idev->adapter);
+	}
+

Doesn't look right. -EAGAIN means arbitration lost which is not a condition to reset the bus in a multi-master setup.

I'll resend with only -ETIMEDOUT triggering the recovery.

Signature · 2 lines
Best regards,
Alexander Sverdlin.