[SRU][F:linux-bluefield][PATCH v3 1/1] UBUNTU: SAUCE: i2c-mlxbf.c: support lock mechanism

Asmaa Mnebhi asmaa at nvidia.com
Wed Jul 20 13:37:58 UTC 2022


Buglink: https://bugs.launchpad.net/bugs/1981105

Support the I2C lock mechanism, otherwise there could be
unexpected behavior when an i2c bus is accessed by
several entities like the linux driver, ATF driver and UEFI driver.

Make sure to pick up the ATF/UEFI image to accompany this change
because at boot time ATF will ensure that the lock is released.

Signed-off-by: Asmaa Mnebhi <asmaa at nvidia.com>
---
 drivers/i2c/busses/i2c-mlxbf.c | 40 ++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/drivers/i2c/busses/i2c-mlxbf.c b/drivers/i2c/busses/i2c-mlxbf.c
index 0ac9e70244a7..b951443b91f8 100644
--- a/drivers/i2c/busses/i2c-mlxbf.c
+++ b/drivers/i2c/busses/i2c-mlxbf.c
@@ -340,7 +340,8 @@ enum {
  * Timeout is given in microsends. Note also that timeout handling is not
  * exact.
  */
-#define SMBUS_TIMEOUT   (300 * 1000) /* 300ms */
+#define SMBUS_TIMEOUT               (300 * 1000) /* 300ms */
+#define SMBUS_LOCK_POLL_TIMEOUT           (300 * 1000) /* 300ms */
 
 /* Encapsulates timing parameters */
 struct mlx_i2c_timings {
@@ -573,6 +574,25 @@ static bool mlx_smbus_master_wait_for_idle(struct mlx_i2c_priv *priv)
 	return false;
 }
 
+/*
+ * wait for the lock to be released before acquiring it.
+ */
+static bool mlx_smbus_master_lock(struct mlx_i2c_priv *priv)
+{
+	if (mlx_smbus_poll(priv->smbus->io, SMBUS_MASTER_GW,
+			   1 << MASTER_LOCK_BIT_OFF, true,
+			   SMBUS_LOCK_POLL_TIMEOUT))
+		return true;
+
+	return false;
+}
+
+static void mlx_smbus_master_unlock(struct mlx_i2c_priv *priv)
+{
+	/* Clear the gw to clear the lock */
+	writel(0, priv->smbus->io + SMBUS_MASTER_GW);
+}
+
 /*
  * Poll SMBus master status and return transaction status,
  * i.e. whether succeeded or failed. I2C and SMBus fault codes
@@ -744,9 +764,17 @@ static int mlx_smbus_start_transaction(struct mlx_i2c_priv *priv,
 	slave     = request->slave & 0x7f;
 	addr      = slave << 1;
 
-	/* First of all, check whether the HW is idle */
-	if (WARN_ON(!mlx_smbus_master_wait_for_idle(priv)))
+	/* Try to acquire the smbus gw lock before any reads of the GW register since
+	 * a read sets the lock.
+	 */
+	if (WARN_ON(!mlx_smbus_master_lock(priv)))
+		return -EBUSY;
+
+	/* Check whether the HW is idle */
+	if (WARN_ON(!mlx_smbus_master_wait_for_idle(priv))) {
+		mlx_smbus_master_unlock(priv);
 		return -EBUSY;
+	}
 
 	/* Set first byte */
 	data_desc[data_idx++] = addr;
@@ -802,8 +830,10 @@ static int mlx_smbus_start_transaction(struct mlx_i2c_priv *priv,
 	if (write_en) {
 		ret = mlx_smbus_enable(priv, slave, write_len, block_en,
 				       pec_en, 0);
-		if (ret != 0)
+		if (ret) {
+			mlx_smbus_master_unlock(priv);
 			return ret;
+		}
 	}
 
 	if (read_en) {
@@ -830,6 +860,8 @@ static int mlx_smbus_start_transaction(struct mlx_i2c_priv *priv,
 			    SMBUS_MASTER_FSM_PS_STATE_MASK);
 	}
 
+	mlx_smbus_master_unlock(priv);
+
 	return ret;
 }
 
-- 
2.30.1




More information about the kernel-team mailing list