i2c: designware: Release slave SCL hold in __i2c_dw_disable()
Subsystems (4)
PATCH 1/1 i2c: designware: Release slave SCL hold in __i2c_dw_disable()
When an external I2C master initiates a read to the Synopsys DW_apb_i2c adapter in slave mode during kexec teardown, runtime suspend, or driver unbind, the slave hardware stretches SCL low (0V) waiting for TX data, setting DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY.
In master mode, __i2c_dw_disable() issues DW_IC_ENABLE_ABORT to release the bus. However, according to the Synopsys DW_apb_i2c Databook, the ABORT bit applies only to master transfers and has no effect in slave mode. Because the slave cannot disable while stretching SCL, __i2c_dw_disable() times out, leaving DW_IC_ENABLE_STATUS[0] = 1 across kexec or rebind. Subsequent writes to DW_IC_SAR are write-protected and silently dropped by hardware, wedging the slave and blocking any other devices sharing the physical bus.
Fix this by:
1. In __i2c_dw_disable(), if DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY is set while disabling, temporarily re-enable the controller so the APB interface accepts data commands without racing with the slave ISR, clear pending RD_REQ/TX_ABRT interrupts, write a dummy 0xFF byte to DW_IC_DATA_CMD to release the SCL stretch, and immediately request disable via __i2c_dw_disable_nowait(). Also drain the RX FIFO if RFNE is set during SLAVE_ACTIVITY.
2. In i2c_dw_reg_slave(), replace __i2c_dw_disable_nowait() with __i2c_dw_disable() to ensure the controller is fully disabled before calling i2c_dw_set_mode(), which programs DW_IC_SAR and DW_IC_CON.
--- drivers/i2c/busses/i2c-designware-common.c | 46 ++++++++++++++++++++++ drivers/i2c/busses/i2c-designware-slave.c | 2 +- include/linux/designware_i2c.h | 1 + 3 files changed, 48 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-designware-common.c +46 -0
@@ -619,6 +619,50 @@ static int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev)return ret; } +/* +* DW_IC_ENABLE_ABORT only aborts master transfers. If the adapter is acting +* as a slave and holding SCL low waiting for TX data (SLAVE_HOLD_TX_FIFO_EMPTY), +* hardware refuses to disable (IC_ENABLE_STATUS[0] stays 1) until a byte is +* written to DW_IC_DATA_CMD. +* +* Mask interrupts and temporarily set DW_IC_ENABLE_ENABLE so the APB interface +* accepts writes to the TX/RX FIFOs without racing with the slave ISR, clear +* pending RD_REQ/TX_ABRT interrupts, feed a dummy 0xFF byte to release SCL +* (or drain any pending RX byte), and immediately request disable again via +* __i2c_dw_disable_nowait() so the controller initiates disable upon +* completing the current byte. +*/ +static void i2c_dw_release_slave_hold (struct dw_i2c_dev *dev) +{ +unsigned int status, dummy; +bool tx_hold, rx_pending; + +if (!IS_ENABLED (CONFIG_I2C_DESIGNWARE_SLAVE ) || dev->mode !=DW_IC_SLAVE ) +return ; + +regmap_read (dev->map,DW_IC_STATUS , &status); + + tx_hold = status &DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY ; + rx_pending = (status &DW_IC_STATUS_SLAVE_ACTIVITY ) && + (status &DW_IC_STATUS_RFNE ); + +if (!tx_hold && !rx_pending) +return ; + +regmap_write (dev->map,DW_IC_INTR_MASK ,0 ); +regmap_write (dev->map,DW_IC_ENABLE ,DW_IC_ENABLE_ENABLE ); + +if (tx_hold) { +regmap_read (dev->map,DW_IC_CLR_RD_REQ , &dummy); +regmap_read (dev->map,DW_IC_CLR_TX_ABRT , &dummy); +regmap_write (dev->map,DW_IC_DATA_CMD ,0xFF ); + } +if (rx_pending) +regmap_read (dev->map,DW_IC_DATA_CMD , &dummy); + +__i2c_dw_disable_nowait (dev); +} +void __i2c_dw_disable (struct dw_i2c_dev *dev) {struct i2c_timings *t = &dev->timings;
@@ -676,6 +720,8 @@ void __i2c_dw_disable(struct dw_i2c_dev *dev)if (!(status &1 ))return ; +i2c_dw_release_slave_hold (dev); +/* * Wait 10 times the signaling period of the highest I2C * transfer supported by the driver (for 400kHz this is
drivers/i2c/busses/i2c-designware-slave.c +1 -1
@@ -40,7 +40,7 @@ int i2c_dw_reg_slave(struct i2c_client *slave)return ret;pm_runtime_get_sync (dev->dev); -__i2c_dw_disable_nowait (dev); +__i2c_dw_disable (dev); dev->slave = slave;i2c_dw_set_mode (dev,DW_IC_SLAVE );
include/linux/designware_i2c.h +1 -0
@@ -100,6 +100,7 @@#define DW_IC_STATUS_MASTER_ACTIVITY BIT(5)#define DW_IC_STATUS_SLAVE_ACTIVITY BIT(6)#define DW_IC_STATUS_MASTER_HOLD_TX_FIFO_EMPTY BIT(7) +#define DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY BIT(9)/* DW_IC_SMBUS_INTR_* bits */ #define DW_IC_SMBUS_INTR_ALERT BIT(10)
Signature · 1 line
2.56.0.317.g454f8302e1-goog
Mika Westerberg
maintainerHi,
Quoted 72 lines · Atif Ashhar
When an external I2C master initiates a read to the Synopsys
DW_apb_i2cadapter in slave mode during kexec teardown, runtime suspend, or driver unbind, the slave hardware stretches SCL low (0V) waiting for TX data, settingDW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY.In master mode,
__i2c_dw_disable()issuesDW_IC_ENABLE_ABORTto release the bus. However, according to the SynopsysDW_apb_i2cDatabook, the ABORT bit applies only to master transfers and has no effect in slave mode. Because the slave cannot disable while stretching SCL,__i2c_dw_disable()times out, leaving DW_IC_ENABLE_STATUS[0] = 1 across kexec or rebind. Subsequent writes toDW_IC_SARare write-protected and silently dropped by hardware, wedging the slave and blocking any other devices sharing the physical bus.Fix this by:
1. In
__i2c_dw_disable(), ifDW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTYis set while disabling, temporarily re-enable the controller so the APB interface accepts data commands without racing with the slave ISR, clear pending RD_REQ/TX_ABRT interrupts, write a dummy0xFFbyte toDW_IC_DATA_CMDto release the SCL stretch, and immediately request disable via__i2c_dw_disable_nowait(). Also drain the RX FIFO if RFNE is set duringSLAVE_ACTIVITY.2. In
i2c_dw_reg_slave(), replace__i2c_dw_disable_nowait()with__i2c_dw_disable()to ensure the controller is fully disabled before callingi2c_dw_set_mode(), which programsDW_IC_SARandDW_IC_CON.Fixes 9f3e065c54b0 ("i2c: designware: add SLAVE mode functions")Signed-off-by Atif Ashhar <[email protected]>--- drivers/i2c/busses/i2c-designware-common.c | 46 ++++++++++++++++++++++ drivers/i2c/busses/i2c-designware-slave.c | 2 +- include/linux/designware_i2c.h | 1 + 3 files changed, 48 insertions(+), 1 deletion(-)drivers/i2c/busses/i2c-designware-common.c +27
-0@@ -619,6 +619,50 @@ static int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev)return ret; } +/* +* DW_IC_ENABLE_ABORT only aborts master transfers. If the adapter is acting +* as a slave and holding SCL low waiting for TX data (SLAVE_HOLD_TX_FIFO_EMPTY), +* hardware refuses to disable (IC_ENABLE_STATUS[0] stays 1) until a byte is +* written to DW_IC_DATA_CMD. +* +* Mask interrupts and temporarily set DW_IC_ENABLE_ENABLE so the APB interface +* accepts writes to the TX/RX FIFOs without racing with the slave ISR, clear +* pending RD_REQ/TX_ABRT interrupts, feed a dummy 0xFF byte to release SCL +* (or drain any pending RX byte), and immediately request disable again via +* __i2c_dw_disable_nowait() so the controller initiates disable upon +* completing the current byte. +*/ +static void i2c_dw_release_slave_hold (struct dw_i2c_dev *dev) +{ +unsigned int status, dummy; +bool tx_hold, rx_pending; + +if (!IS_ENABLED (CONFIG_I2C_DESIGNWARE_SLAVE ) || dev->mode !=DW_IC_SLAVE ) +return ; + +regmap_read (dev->map,DW_IC_STATUS , &status); + + tx_hold = status &DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY ; + rx_pending = (status &DW_IC_STATUS_SLAVE_ACTIVITY ) && + (status &DW_IC_STATUS_RFNE ); +
drivers/i2c/busses/i2c-designware-common.c static int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev) · line 658
Quoted 10 lines
drivers/i2c/busses/i2c-designware-common.c +10
-0+if (!tx_hold && !rx_pending) +return ; + +regmap_write (dev->map,DW_IC_INTR_MASK ,0 ); +regmap_write (dev->map,DW_IC_ENABLE ,DW_IC_ENABLE_ENABLE ); + +if (tx_hold) { +regmap_read (dev->map,DW_IC_CLR_RD_REQ , &dummy); +regmap_read (dev->map,DW_IC_CLR_TX_ABRT , &dummy); +regmap_write (dev->map,DW_IC_DATA_CMD ,0xFF );
I'm not super familiar with the slave mode so adding Heikki just in case. Can this cause some unintended traffic on the bus towards the master?
Also use 0xff instead.
Quoted 46 lines
drivers/i2c/busses/i2c-designware-common.c +9
-0+ } +if (rx_pending) +regmap_read (dev->map,DW_IC_DATA_CMD , &dummy); + +__i2c_dw_disable_nowait (dev); +} +void __i2c_dw_disable (struct dw_i2c_dev *dev) {struct i2c_timings *t = &dev->timings;@@ -676,6 +720,8 @@ void __i2c_dw_disable(struct dw_i2c_dev *dev)if (!(status &1 ))return ; +i2c_dw_release_slave_hold (dev); +/* * Wait 10 times the signaling period of the highest I2C * transfer supported by the driver (for 400kHz this is drivers/i2c/busses/i2c-designware-slave.c +1
-1@@ -40,7 +40,7 @@ int i2c_dw_reg_slave(struct i2c_client *slave)return ret;pm_runtime_get_sync (dev->dev); -__i2c_dw_disable_nowait (dev); +__i2c_dw_disable (dev); dev->slave = slave;i2c_dw_set_mode (dev,DW_IC_SLAVE );include/linux/designware_i2c.h +1
-0@@ -100,6 +100,7 @@#define DW_IC_STATUS_MASTER_ACTIVITY BIT(5)#define DW_IC_STATUS_SLAVE_ACTIVITY BIT(6)#define DW_IC_STATUS_MASTER_HOLD_TX_FIFO_EMPTY BIT(7) +#define DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY BIT(9)/* DW_IC_SMBUS_INTR_* bits */ #define DW_IC_SMBUS_INTR_ALERT BIT(10)Signature · 1 line
2.56.0.317.g454f8302e1-goog
Hi Mika,
Quoted 7 lines · Mika Westerberg
drivers/i2c/busses/i2c-designware-common.c static int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev) · line 658
Quoted 4 lines
drivers/i2c/busses/i2c-designware-common.c +4
-0+if (tx_hold) { +regmap_read (dev->map,DW_IC_CLR_RD_REQ , &dummy); +regmap_read (dev->map,DW_IC_CLR_TX_ABRT , &dummy); +regmap_write (dev->map,DW_IC_DATA_CMD ,0xFF );I'm not super familiar with the slave mode so adding Heikki just in case. Can this cause some unintended traffic on the bus towards the master?
No, it cannot initiate new traffic. DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY is only asserted when an external master has already addressed this controller and initiated a read transfer, and the slave hardware is actively stretching SCL low waiting for TX data. When no slave read hold is active, i2c_dw_release_slave_hold() returns early without writing to DW_IC_DATA_CMD.
Writing 0xff simply satisfies the ongoing master-clocked read byte while keeping SDA passively high (released via pull-ups), allowing the external master to finish clocking the byte and terminate the transfer with NACK/STOP instead of hanging the bus indefinitely.
Quoted 1 line
Also use
0xffinstead.
Ack, I will update 0xFF to 0xff (and wait to see if Heikki has any comments before sending v2).
Thanks,
Atif
Hi guys,
Quoted 23 lines · Atif Ashhar
Hi Mika,
Quoted 7 lines · Mika Westerberg
drivers/i2c/busses/i2c-designware-common.c static int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev) · line 658
Quoted 4 lines
drivers/i2c/busses/i2c-designware-common.c +4
-0+if (tx_hold) { +regmap_read (dev->map,DW_IC_CLR_RD_REQ , &dummy); +regmap_read (dev->map,DW_IC_CLR_TX_ABRT , &dummy); +regmap_write (dev->map,DW_IC_DATA_CMD ,0xFF );I'm not super familiar with the slave mode so adding Heikki just in case. Can this cause some unintended traffic on the bus towards the master?
No, it cannot initiate new traffic.
DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTYis only asserted when an external master has already addressed this controller and initiated a read transfer, and the slave hardware is actively stretching SCL low waiting for TX data. When no slave read hold is active,i2c_dw_release_slave_hold()returns early without writing toDW_IC_DATA_CMD.Writing
0xffsimply satisfies the ongoing master-clocked read byte while keeping SDA passively high (released via pull-ups), allowing the external master to finish clocking the byte and terminate the transfer with NACK/STOP instead of hanging the bus indefinitely.
Quoted 4 lines
Quoted 1 line
Also use
0xffinstead.Ack, I will update
0xFFto0xff(and wait to see if Heikki has any comments before sending v2).
No comments yet, but just out of curiosity, I'll try to test this.
Please go ahead with the v2. I can then give you my Tested-by tag if I figure out how to reproduce the problem fast enough.
Thanks,
Signature · 1 line
heikki
PATCH v2 1/1 i2c: designware: Release slave SCL hold in __i2c_dw_disable()
In reply to Atif AshharWhen an external I2C master initiates a read to the Synopsys DW_apb_i2c adapter in slave mode during kexec teardown, runtime suspend, or driver unbind, the slave hardware stretches SCL low (0V) waiting for TX data, setting DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY.
In master mode, __i2c_dw_disable() issues DW_IC_ENABLE_ABORT to release the bus. However, according to the Synopsys DW_apb_i2c Databook, the ABORT bit applies only to master transfers and has no effect in slave mode. Because the slave cannot disable while stretching SCL, __i2c_dw_disable() times out, leaving DW_IC_ENABLE_STATUS[0] = 1 across kexec or rebind. Subsequent writes to DW_IC_SAR are write-protected and silently dropped by hardware, wedging the slave and blocking any other devices sharing the physical bus.
Fix this by:
1. In __i2c_dw_disable(), if DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY is set while disabling, temporarily re-enable the controller so the APB interface accepts data commands without racing with the slave ISR, clear pending RD_REQ/TX_ABRT interrupts, write a dummy 0xff byte to DW_IC_DATA_CMD to release the SCL stretch, and immediately request disable via __i2c_dw_disable_nowait(). Also drain the RX FIFO if RFNE is set during SLAVE_ACTIVITY.
2. In i2c_dw_reg_slave(), replace __i2c_dw_disable_nowait() with __i2c_dw_disable() to ensure the controller is fully disabled before calling i2c_dw_set_mode(), which programs DW_IC_SAR and DW_IC_CON.
---
Changes since v1:
- Use lowercase 0xff instead of 0xFF in regmap_write(), comments, and commit message (Mika Westerberg).
- Explained on the v1 thread why writing a dummy 0xff byte to DW_IC_DATA_CMD does not generate unintended bus traffic when immediately followed by __i2c_dw_disable_nowait().
drivers/i2c/busses/i2c-designware-common.c | 46 ++++++++++++++++++++++ drivers/i2c/busses/i2c-designware-slave.c | 2 +- include/linux/designware_i2c.h | 1 + 3 files changed, 48 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-designware-common.c +46 -0
@@ -619,6 +619,50 @@ static int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev)return ret; } +/* +* DW_IC_ENABLE_ABORT only aborts master transfers. If the adapter is acting +* as a slave and holding SCL low waiting for TX data (SLAVE_HOLD_TX_FIFO_EMPTY), +* hardware refuses to disable (IC_ENABLE_STATUS[0] stays 1) until a byte is +* written to DW_IC_DATA_CMD. +* +* Mask interrupts and temporarily set DW_IC_ENABLE_ENABLE so the APB interface +* accepts writes to the TX/RX FIFOs without racing with the slave ISR, clear +* pending RD_REQ/TX_ABRT interrupts, feed a dummy 0xff byte to release SCL +* (or drain any pending RX byte), and immediately request disable again via +* __i2c_dw_disable_nowait() so the controller initiates disable upon +* completing the current byte. +*/ +static void i2c_dw_release_slave_hold (struct dw_i2c_dev *dev) +{ +unsigned int status, dummy; +bool tx_hold, rx_pending; + +if (!IS_ENABLED (CONFIG_I2C_DESIGNWARE_SLAVE ) || dev->mode !=DW_IC_SLAVE ) +return ; + +regmap_read (dev->map,DW_IC_STATUS , &status); + + tx_hold = status &DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY ; + rx_pending = (status &DW_IC_STATUS_SLAVE_ACTIVITY ) && + (status &DW_IC_STATUS_RFNE ); + +if (!tx_hold && !rx_pending) +return ; + +regmap_write (dev->map,DW_IC_INTR_MASK ,0 ); +regmap_write (dev->map,DW_IC_ENABLE ,DW_IC_ENABLE_ENABLE ); + +if (tx_hold) { +regmap_read (dev->map,DW_IC_CLR_RD_REQ , &dummy); +regmap_read (dev->map,DW_IC_CLR_TX_ABRT , &dummy); +regmap_write (dev->map,DW_IC_DATA_CMD ,0xff ); + } +if (rx_pending) +regmap_read (dev->map,DW_IC_DATA_CMD , &dummy); + +__i2c_dw_disable_nowait (dev); +} +void __i2c_dw_disable (struct dw_i2c_dev *dev) {struct i2c_timings *t = &dev->timings;
@@ -676,6 +720,8 @@ void __i2c_dw_disable(struct dw_i2c_dev *dev)if (!(status &1 ))return ; +i2c_dw_release_slave_hold (dev); +/* * Wait 10 times the signaling period of the highest I2C * transfer supported by the driver (for 400kHz this is
drivers/i2c/busses/i2c-designware-slave.c +1 -1
@@ -40,7 +40,7 @@ int i2c_dw_reg_slave(struct i2c_client *slave)return ret;pm_runtime_get_sync (dev->dev); -__i2c_dw_disable_nowait (dev); +__i2c_dw_disable (dev); dev->slave = slave;i2c_dw_set_mode (dev,DW_IC_SLAVE );
include/linux/designware_i2c.h +1 -0
@@ -100,6 +100,7 @@#define DW_IC_STATUS_MASTER_ACTIVITY BIT(5)#define DW_IC_STATUS_SLAVE_ACTIVITY BIT(6)#define DW_IC_STATUS_MASTER_HOLD_TX_FIFO_EMPTY BIT(7) +#define DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY BIT(9)/* DW_IC_SMBUS_INTR_* bits */ #define DW_IC_SMBUS_INTR_ALERT BIT(10)
Signature · 1 line
2.56.0.567.gdb80ae8cd5-goog
Quoted 24 lines · Atif Ashhar
When an external I2C master initiates a read to the Synopsys
DW_apb_i2cadapter in slave mode during kexec teardown, runtime suspend, or driver unbind, the slave hardware stretches SCL low (0V) waiting for TX data, settingDW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY.In master mode,
__i2c_dw_disable()issuesDW_IC_ENABLE_ABORTto release the bus. However, according to the SynopsysDW_apb_i2cDatabook, the ABORT bit applies only to master transfers and has no effect in slave mode. Because the slave cannot disable while stretching SCL,__i2c_dw_disable()times out, leaving DW_IC_ENABLE_STATUS[0] = 1 across kexec or rebind. Subsequent writes toDW_IC_SARare write-protected and silently dropped by hardware, wedging the slave and blocking any other devices sharing the physical bus.Fix this by:
1. In
__i2c_dw_disable(), ifDW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTYis set while disabling, temporarily re-enable the controller so the APB interface accepts data commands without racing with the slave ISR, clear pending RD_REQ/TX_ABRT interrupts, write a dummy0xffbyte toDW_IC_DATA_CMDto release the SCL stretch, and immediately request disable via__i2c_dw_disable_nowait(). Also drain the RX FIFO if RFNE is set duringSLAVE_ACTIVITY.
Quoted 7 lines
2. In
i2c_dw_reg_slave(), replace__i2c_dw_disable_nowait()with__i2c_dw_disable()to ensure the controller is fully disabled before callingi2c_dw_set_mode(), which programsDW_IC_SARandDW_IC_CON.Fixes 9f3e065c54b0 ("i2c: designware: add SLAVE mode functions")Signed-off-by Atif Ashhar <[email protected]>
I don't have time to test this unfortunately :( But it LGTM. FWIW, if it's okay by Mika:
Quoted 105 lines
---
Changes since v1:
- Use lowercase0xffinstead of0xFFinregmap_write(), comments, and commit message (Mika Westerberg).
- Explained on the v1 thread why writing a dummy0xffbyte toDW_IC_DATA_CMDdoes not generate unintended bus traffic when immediately followed by__i2c_dw_disable_nowait().drivers/i2c/busses/i2c-designware-common.c | 46 ++++++++++++++++++++++ drivers/i2c/busses/i2c-designware-slave.c | 2 +- include/linux/designware_i2c.h | 1 + 3 files changed, 48 insertions(+), 1 deletion(-)drivers/i2c/busses/i2c-designware-common.c +46
-0@@ -619,6 +619,50 @@ static int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev)return ret; } +/* +* DW_IC_ENABLE_ABORT only aborts master transfers. If the adapter is acting +* as a slave and holding SCL low waiting for TX data (SLAVE_HOLD_TX_FIFO_EMPTY), +* hardware refuses to disable (IC_ENABLE_STATUS[0] stays 1) until a byte is +* written to DW_IC_DATA_CMD. +* +* Mask interrupts and temporarily set DW_IC_ENABLE_ENABLE so the APB interface +* accepts writes to the TX/RX FIFOs without racing with the slave ISR, clear +* pending RD_REQ/TX_ABRT interrupts, feed a dummy 0xff byte to release SCL +* (or drain any pending RX byte), and immediately request disable again via +* __i2c_dw_disable_nowait() so the controller initiates disable upon +* completing the current byte. +*/ +static void i2c_dw_release_slave_hold (struct dw_i2c_dev *dev) +{ +unsigned int status, dummy; +bool tx_hold, rx_pending; + +if (!IS_ENABLED (CONFIG_I2C_DESIGNWARE_SLAVE ) || dev->mode !=DW_IC_SLAVE ) +return ; + +regmap_read (dev->map,DW_IC_STATUS , &status); + + tx_hold = status &DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY ; + rx_pending = (status &DW_IC_STATUS_SLAVE_ACTIVITY ) && + (status &DW_IC_STATUS_RFNE ); + +if (!tx_hold && !rx_pending) +return ; + +regmap_write (dev->map,DW_IC_INTR_MASK ,0 ); +regmap_write (dev->map,DW_IC_ENABLE ,DW_IC_ENABLE_ENABLE ); + +if (tx_hold) { +regmap_read (dev->map,DW_IC_CLR_RD_REQ , &dummy); +regmap_read (dev->map,DW_IC_CLR_TX_ABRT , &dummy); +regmap_write (dev->map,DW_IC_DATA_CMD ,0xff ); + } +if (rx_pending) +regmap_read (dev->map,DW_IC_DATA_CMD , &dummy); + +__i2c_dw_disable_nowait (dev); +} +void __i2c_dw_disable (struct dw_i2c_dev *dev) {struct i2c_timings *t = &dev->timings;@@ -676,6 +720,8 @@ void __i2c_dw_disable(struct dw_i2c_dev *dev)if (!(status &1 ))return ; +i2c_dw_release_slave_hold (dev); +/* * Wait 10 times the signaling period of the highest I2C * transfer supported by the driver (for 400kHz this is drivers/i2c/busses/i2c-designware-slave.c +1
-1@@ -40,7 +40,7 @@ int i2c_dw_reg_slave(struct i2c_client *slave)return ret;pm_runtime_get_sync (dev->dev); -__i2c_dw_disable_nowait (dev); +__i2c_dw_disable (dev); dev->slave = slave;i2c_dw_set_mode (dev,DW_IC_SLAVE );include/linux/designware_i2c.h +1
-0@@ -100,6 +100,7 @@#define DW_IC_STATUS_MASTER_ACTIVITY BIT(5)#define DW_IC_STATUS_SLAVE_ACTIVITY BIT(6)#define DW_IC_STATUS_MASTER_HOLD_TX_FIFO_EMPTY BIT(7) +#define DW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY BIT(9)/* DW_IC_SMBUS_INTR_* bits */ #define DW_IC_SMBUS_INTR_ALERT BIT(10)Signature · 1 line
2.56.0.567.gdb80ae8cd5-goog
Signature · 1 line
heikki
Quoted 29 lines · Heikki Krogerus
Quoted 28 lines · Atif Ashhar
When an external I2C master initiates a read to the Synopsys
DW_apb_i2cadapter in slave mode during kexec teardown, runtime suspend, or driver unbind, the slave hardware stretches SCL low (0V) waiting for TX data, settingDW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTY.In master mode,
__i2c_dw_disable()issuesDW_IC_ENABLE_ABORTto release the bus. However, according to the SynopsysDW_apb_i2cDatabook, the ABORT bit applies only to master transfers and has no effect in slave mode. Because the slave cannot disable while stretching SCL,__i2c_dw_disable()times out, leaving DW_IC_ENABLE_STATUS[0] = 1 across kexec or rebind. Subsequent writes toDW_IC_SARare write-protected and silently dropped by hardware, wedging the slave and blocking any other devices sharing the physical bus.Fix this by:
1. In
__i2c_dw_disable(), ifDW_IC_STATUS_SLAVE_HOLD_TX_FIFO_EMPTYis set while disabling, temporarily re-enable the controller so the APB interface accepts data commands without racing with the slave ISR, clear pending RD_REQ/TX_ABRT interrupts, write a dummy0xffbyte toDW_IC_DATA_CMDto release the SCL stretch, and immediately request disable via__i2c_dw_disable_nowait(). Also drain the RX FIFO if RFNE is set duringSLAVE_ACTIVITY.2. In
i2c_dw_reg_slave(), replace__i2c_dw_disable_nowait()with__i2c_dw_disable()to ensure the controller is fully disabled before callingi2c_dw_set_mode(), which programsDW_IC_SARandDW_IC_CON.
Quoted 8 lines
Quoted 3 lines
Fixes 9f3e065c54b0 ("i2c: designware: add SLAVE mode functions")Signed-off-by Atif Ashhar <[email protected]>I don't have time to test this unfortunately :( But it LGTM. FWIW, if it's okay by Mika:
Reviewed-by Heikki Krogerus <[email protected]>
Yes it's okay, thanks Heikki!