This patchset adds some fixes and updates, mostly for SAMA5D4, but other
platforms will still benefit.
The first patch is mostly a prerequisite for the second one. It only
moves the i2c_recover_bus() out of the actual transfer function. This
helps the second patch disable the controller before using GPIO
recovery. The second patch will keep the controller enabled when a
transfer occurs. Before using GPIO recovery, the controller must be
disabled, to ignore potential glitches. However, the controller must be
enabled for HW recovery (bus CLEAR command). The third and last patch
adds advanced digital filtering support for SAMA5D4. The TWI IP found in
SAMA5D4 supports advanced digital filtering, even if, at the moment of
this patch, the SAMA5D4 datasheet does not mention it.
Codrin Ciubotariu (3):
i2c: at91: move i2c_recover_bus() outside of at91_do_twi_transfer()
i2c: at91: keep the controller disabled when it is not used
i2c: at91: add advanced digital filtering support for SAMA5D4
drivers/i2c/busses/i2c-at91-core.c | 1 +
drivers/i2c/busses/i2c-at91-master.c | 53 ++++++++++++++++++++++------
2 files changed, 44 insertions(+), 10 deletions(-)
--
2.30.2
I2C/TWI IP variant found in SAMA5D4 supports advanced digital filtering,
even though, at the time of this patch, it is not present in Datasheet.
Signed-off-by: Codrin Ciubotariu <[email protected]>
---
drivers/i2c/busses/i2c-at91-core.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/i2c/busses/i2c-at91-core.c b/drivers/i2c/busses/i2c-at91-core.c
index e14edd236108..7b6dc56d8c1c 100644
--- a/drivers/i2c/busses/i2c-at91-core.c
+++ b/drivers/i2c/busses/i2c-at91-core.c
@@ -120,6 +120,7 @@ static struct at91_twi_pdata sama5d4_config = {
.clk_offset = 4,
.has_hold_field = true,
.has_dig_filtr = true,
+ .has_adv_dig_filtr = true,
};
static struct at91_twi_pdata sama5d2_config = {
--
2.30.2
The controller (master mode) should be disabled when it is not used, to
avoid potential glitches on the bus. The controller must be disabled
only when the controller is not running.
This fixes an issue on Microchip's SAMA5D4 platform when pinctrl changes
the I2C's pin states between GPIO and I2C, done when GPIO recovery is used.
Fixes: 813e30e9ab91 ("i2c: New Atmel AT91 bus driver")
Signed-off-by: Codrin Ciubotariu <[email protected]>
---
drivers/i2c/busses/i2c-at91-master.c | 34 ++++++++++++++++++++++++++--
1 file changed, 32 insertions(+), 2 deletions(-)
diff --git a/drivers/i2c/busses/i2c-at91-master.c b/drivers/i2c/busses/i2c-at91-master.c
index 0352dc09d697..1f91829438fd 100644
--- a/drivers/i2c/busses/i2c-at91-master.c
+++ b/drivers/i2c/busses/i2c-at91-master.c
@@ -38,7 +38,7 @@ void at91_init_twi_bus_master(struct at91_twi_dev *dev)
/* FIFO should be enabled immediately after the software reset */
if (dev->fifo_size)
at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
- at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
+ at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSDIS);
at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
@@ -593,7 +593,6 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
if (time_left == 0) {
dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
dev_err(dev->dev, "controller timed out\n");
- at91_init_twi_bus(dev);
ret = -ETIMEDOUT;
goto error;
}
@@ -642,6 +641,26 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
return ret;
}
+static void at91_twi_disable(struct at91_twi_dev *dev)
+{
+ /* return if previous operation is completed */
+ if (!(at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_TXCOMP)) {
+ /* wait for previous command to complete before disabling the controller */
+ dev_dbg(dev->dev, "wait for command to complete...\n");
+ reinit_completion(&dev->cmd_complete);
+ dev->transfer_status = 0;
+ at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
+ wait_for_completion_timeout(&dev->cmd_complete, dev->adapter.timeout);
+ if (!(dev->transfer_status & AT91_TWI_TXCOMP)) {
+ dev_dbg(dev->dev, "IP still busy, resetting the controller...\n");
+ at91_init_twi_bus(dev);
+ return;
+ }
+ }
+
+ at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSDIS);
+}
+
static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
{
struct at91_twi_dev *dev = i2c_get_adapdata(adap);
@@ -656,6 +675,8 @@ static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
if (ret < 0)
goto out;
+ at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
+
if (num == 2) {
int internal_address = 0;
int i;
@@ -699,13 +720,22 @@ static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
ret = at91_do_twi_transfer(dev);
if (ret < 0) {
+ /* disable controller before using GPIO recovery */
+ if (!dev->pdata->has_clear_cmd)
+ at91_twi_disable(dev);
+
/*
* some faulty I2C slave devices might hold SDA down;
* we can send a bus clear command, hoping that the pins will be
* released
*/
i2c_recover_bus(&dev->adapter);
+
+ /* disable controller if not disabled before */
+ if (dev->pdata->has_clear_cmd)
+ at91_twi_disable(dev);
} else {
+ at91_twi_disable(dev);
ret = num;
}
--
2.30.2
This patch doesn't add a functional change, it just separates the recovery
from the transfer itself.
Fixes: d3d3fdcc4c90 ("i2c: at91: implement i2c bus recovery")
Signed-off-by: Codrin Ciubotariu <[email protected]>
---
drivers/i2c/busses/i2c-at91-master.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/drivers/i2c/busses/i2c-at91-master.c b/drivers/i2c/busses/i2c-at91-master.c
index 1cceb6866689..0352dc09d697 100644
--- a/drivers/i2c/busses/i2c-at91-master.c
+++ b/drivers/i2c/busses/i2c-at91-master.c
@@ -639,13 +639,6 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
}
- /*
- * some faulty I2C slave devices might hold SDA down;
- * we can send a bus clear command, hoping that the pins will be
- * released
- */
- i2c_recover_bus(&dev->adapter);
-
return ret;
}
@@ -705,7 +698,17 @@ static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
ret = at91_do_twi_transfer(dev);
- ret = (ret < 0) ? ret : num;
+ if (ret < 0) {
+ /*
+ * some faulty I2C slave devices might hold SDA down;
+ * we can send a bus clear command, hoping that the pins will be
+ * released
+ */
+ i2c_recover_bus(&dev->adapter);
+ } else {
+ ret = num;
+ }
+
out:
pm_runtime_mark_last_busy(dev->dev);
pm_runtime_put_autosuspend(dev->dev);
--
2.30.2
Hi Codrin,
sorry for the super-long delay. There is an issue here with regard to
bus recovery which affetcs more drivers and I can't make up my mind how
to handle it...
> Fixes: d3d3fdcc4c90 ("i2c: at91: implement i2c bus recovery")
Sidenote: I don't think this is a fix.
> + if (ret < 0) {
> + /*
> + * some faulty I2C slave devices might hold SDA down;
> + * we can send a bus clear command, hoping that the pins will be
> + * released
> + */
> + i2c_recover_bus(&dev->adapter);
> + } else {
> + ret = num;
> + }
So, one issue is more straightforward. Bus recovery is applied on all
errors. It should only be called when SDA is stuck.
The other issue is that bus recovery is applied after a transfer. The
I2C specs mention bus recovery only at the beginning of a transfer when
SDA is detected low. I think it also makes more sense because the bus
may also be stuck because of a misbehaving bootloader etc. This will be
caught when the check is done at the beginning.
However, moving the detection to the beginning leaves room for a
regression, because your driver already does it at the end of a
transfer. However, I'd think all regressions coming up need seperate
fixing anyhow. Unless I overlooked something, of course.
So, I think it should be moved to the beginning of a transfer, but I am
open for discussion, so we get the best possible bus recovery in Linux.
Happy hacking,
Wolfram
On Tue, Jul 27, 2021 at 02:15:54PM +0300, Codrin Ciubotariu wrote:
> I2C/TWI IP variant found in SAMA5D4 supports advanced digital filtering,
> even though, at the time of this patch, it is not present in Datasheet.
Is it meanwhile? If not, can you add a comment, please?