Skip to content

Commit 1f0d9cb

Browse files
jaehyooWolfram Sang
authored andcommitted
i2c: aspeed: fix master pending state handling
In case of master pending state, it should not trigger a master command, otherwise data could be corrupted because this H/W shares the same data buffer for slave and master operations. It also means that H/W command queue handling is unreliable because of the buffer sharing issue. To fix this issue, it clears command queue if a master command is queued in pending state to use S/W solution instead of H/W command queue handling. Also, it refines restarting mechanism of the pending master command. Fixes: 2e57b7c ("i2c: aspeed: Add multi-master use case support") Signed-off-by: Jae Hyun Yoo <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Acked-by: Joel Stanley <[email protected]> Tested-by: Tao Ren <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 7d194c2 commit 1f0d9cb

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

drivers/i2c/busses/i2c-aspeed.c

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
#define ASPEED_I2CD_S_TX_CMD BIT(2)
109109
#define ASPEED_I2CD_M_TX_CMD BIT(1)
110110
#define ASPEED_I2CD_M_START_CMD BIT(0)
111+
#define ASPEED_I2CD_MASTER_CMDS_MASK \
112+
(ASPEED_I2CD_M_STOP_CMD | \
113+
ASPEED_I2CD_M_S_RX_CMD_LAST | \
114+
ASPEED_I2CD_M_RX_CMD | \
115+
ASPEED_I2CD_M_TX_CMD | \
116+
ASPEED_I2CD_M_START_CMD)
111117

112118
/* 0x18 : I2CD Slave Device Address Register */
113119
#define ASPEED_I2CD_DEV_ADDR_MASK GENMASK(6, 0)
@@ -336,18 +342,19 @@ static void aspeed_i2c_do_start(struct aspeed_i2c_bus *bus)
336342
struct i2c_msg *msg = &bus->msgs[bus->msgs_index];
337343
u8 slave_addr = i2c_8bit_addr_from_msg(msg);
338344

339-
bus->master_state = ASPEED_I2C_MASTER_START;
340-
341345
#if IS_ENABLED(CONFIG_I2C_SLAVE)
342346
/*
343347
* If it's requested in the middle of a slave session, set the master
344348
* state to 'pending' then H/W will continue handling this master
345349
* command when the bus comes back to the idle state.
346350
*/
347-
if (bus->slave_state != ASPEED_I2C_SLAVE_INACTIVE)
351+
if (bus->slave_state != ASPEED_I2C_SLAVE_INACTIVE) {
348352
bus->master_state = ASPEED_I2C_MASTER_PENDING;
353+
return;
354+
}
349355
#endif /* CONFIG_I2C_SLAVE */
350356

357+
bus->master_state = ASPEED_I2C_MASTER_START;
351358
bus->buf_index = 0;
352359

353360
if (msg->flags & I2C_M_RD) {
@@ -422,20 +429,6 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status)
422429
}
423430
}
424431

425-
#if IS_ENABLED(CONFIG_I2C_SLAVE)
426-
/*
427-
* A pending master command will be started by H/W when the bus comes
428-
* back to idle state after completing a slave operation so change the
429-
* master state from 'pending' to 'start' at here if slave is inactive.
430-
*/
431-
if (bus->master_state == ASPEED_I2C_MASTER_PENDING) {
432-
if (bus->slave_state != ASPEED_I2C_SLAVE_INACTIVE)
433-
goto out_no_complete;
434-
435-
bus->master_state = ASPEED_I2C_MASTER_START;
436-
}
437-
#endif /* CONFIG_I2C_SLAVE */
438-
439432
/* Master is not currently active, irq was for someone else. */
440433
if (bus->master_state == ASPEED_I2C_MASTER_INACTIVE ||
441434
bus->master_state == ASPEED_I2C_MASTER_PENDING)
@@ -462,11 +455,15 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status)
462455
#if IS_ENABLED(CONFIG_I2C_SLAVE)
463456
/*
464457
* If a peer master starts a xfer immediately after it queues a
465-
* master command, change its state to 'pending' then H/W will
466-
* continue the queued master xfer just after completing the
467-
* slave mode session.
458+
* master command, clear the queued master command and change
459+
* its state to 'pending'. To simplify handling of pending
460+
* cases, it uses S/W solution instead of H/W command queue
461+
* handling.
468462
*/
469463
if (unlikely(irq_status & ASPEED_I2CD_INTR_SLAVE_MATCH)) {
464+
writel(readl(bus->base + ASPEED_I2C_CMD_REG) &
465+
~ASPEED_I2CD_MASTER_CMDS_MASK,
466+
bus->base + ASPEED_I2C_CMD_REG);
470467
bus->master_state = ASPEED_I2C_MASTER_PENDING;
471468
dev_dbg(bus->dev,
472469
"master goes pending due to a slave start\n");
@@ -629,6 +626,14 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
629626
irq_handled |= aspeed_i2c_master_irq(bus,
630627
irq_remaining);
631628
}
629+
630+
/*
631+
* Start a pending master command at here if a slave operation is
632+
* completed.
633+
*/
634+
if (bus->master_state == ASPEED_I2C_MASTER_PENDING &&
635+
bus->slave_state == ASPEED_I2C_SLAVE_INACTIVE)
636+
aspeed_i2c_do_start(bus);
632637
#else
633638
irq_handled = aspeed_i2c_master_irq(bus, irq_remaining);
634639
#endif /* CONFIG_I2C_SLAVE */
@@ -691,6 +696,15 @@ static int aspeed_i2c_master_xfer(struct i2c_adapter *adap,
691696
ASPEED_I2CD_BUS_BUSY_STS))
692697
aspeed_i2c_recover_bus(bus);
693698

699+
/*
700+
* If timed out and the state is still pending, drop the pending
701+
* master command.
702+
*/
703+
spin_lock_irqsave(&bus->lock, flags);
704+
if (bus->master_state == ASPEED_I2C_MASTER_PENDING)
705+
bus->master_state = ASPEED_I2C_MASTER_INACTIVE;
706+
spin_unlock_irqrestore(&bus->lock, flags);
707+
694708
return -ETIMEDOUT;
695709
}
696710

0 commit comments

Comments
 (0)