From: Asmaa Mnebhi <asmaa@nvidia.com>
To: Wolfram Sang <wsa+renesas@sang-engineering.com>,
linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Asmaa Mnebhi <asmaa@nvidia.com>, Khalil Blaiech <kblaiech@nvidia.com>
Subject: [PATCH v5 5/8] i2c: i2c-mlxbf.c: support lock mechanism
Date: Tue, 20 Sep 2022 13:47:33 -0400 [thread overview]
Message-ID: <20220920174736.9766-6-asmaa@nvidia.com> (raw)
In-Reply-To: <20220920174736.9766-1-asmaa@nvidia.com>
Linux is not the only entity using the BlueField I2C busses so
support a lock mechanism provided by hardware to avoid issues
when multiple entities are trying to access the same bus.
The lock is acquired whenever written explicitely or the lock
register is read. So make sure it is always released at the end
of a successful or failed transaction.
Fixes: b5b5b32081cd206b (i2c: mlxbf: I2C SMBus driver for Mellanox BlueField SoC)
Reviewed-by: Khalil Blaiech <kblaiech@nvidia.com>
Signed-off-by: Asmaa Mnebhi <asmaa@nvidia.com>
---
drivers/i2c/busses/i2c-mlxbf.c | 44 ++++++++++++++++++++++++++++++----
1 file changed, 39 insertions(+), 5 deletions(-)
diff --git a/drivers/i2c/busses/i2c-mlxbf.c b/drivers/i2c/busses/i2c-mlxbf.c
index 1a0fc9640c23..78b2bc9b0a34 100644
--- a/drivers/i2c/busses/i2c-mlxbf.c
+++ b/drivers/i2c/busses/i2c-mlxbf.c
@@ -306,6 +306,7 @@ static u64 mlxbf_i2c_corepll_frequency;
* exact.
*/
#define MLXBF_I2C_SMBUS_TIMEOUT (300 * 1000) /* 300ms */
+#define MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT (300 * 1000) /* 300ms */
/* Encapsulates timing parameters. */
struct mlxbf_i2c_timings {
@@ -514,6 +515,25 @@ static bool mlxbf_smbus_master_wait_for_idle(struct mlxbf_i2c_priv *priv)
return false;
}
+/*
+ * wait for the lock to be released before acquiring it.
+ */
+static bool mlxbf_i2c_smbus_master_lock(struct mlxbf_i2c_priv *priv)
+{
+ if (mlxbf_smbus_poll(priv->smbus->io, MLXBF_I2C_SMBUS_MASTER_GW,
+ MLXBF_I2C_MASTER_LOCK_BIT, true,
+ MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT))
+ return true;
+
+ return false;
+}
+
+static void mlxbf_i2c_smbus_master_unlock(struct mlxbf_i2c_priv *priv)
+{
+ /* Clear the gw to clear the lock */
+ writel(0, priv->smbus->io + MLXBF_I2C_SMBUS_MASTER_GW);
+}
+
static bool mlxbf_i2c_smbus_transaction_success(u32 master_status,
u32 cause_status)
{
@@ -705,10 +725,19 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
slave = request->slave & GENMASK(6, 0);
addr = slave << 1;
- /* First of all, check whether the HW is idle. */
- if (WARN_ON(!mlxbf_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(!mlxbf_i2c_smbus_master_lock(priv)))
return -EBUSY;
+ /* Check whether the HW is idle */
+ if (WARN_ON(!mlxbf_smbus_master_wait_for_idle(priv))) {
+ ret = -EBUSY;
+ goto out_unlock;
+ }
+
/* Set first byte. */
data_desc[data_idx++] = addr;
@@ -732,8 +761,10 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
write_en = 1;
write_len += operation->length;
if (data_idx + operation->length >
- MLXBF_I2C_MASTER_DATA_DESC_SIZE)
- return -ENOBUFS;
+ MLXBF_I2C_MASTER_DATA_DESC_SIZE) {
+ ret = -ENOBUFS;
+ goto out_unlock;
+ }
memcpy(data_desc + data_idx,
operation->buffer, operation->length);
data_idx += operation->length;
@@ -765,7 +796,7 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
ret = mlxbf_i2c_smbus_enable(priv, slave, write_len, block_en,
pec_en, 0);
if (ret)
- return ret;
+ goto out_unlock;
}
if (read_en) {
@@ -792,6 +823,9 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
priv->smbus->io + MLXBF_I2C_SMBUS_MASTER_FSM);
}
+out_unlock:
+ mlxbf_i2c_smbus_master_unlock(priv);
+
return ret;
}
--
2.30.1
next prev parent reply other threads:[~2022-09-20 17:48 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-20 17:47 [PATCH v5 0/8] i2c: i2c-mlxbf.c: bug fixes and new feature support Asmaa Mnebhi
2022-09-20 17:47 ` [PATCH v5 1/8] i2c: i2c-mlxbf.c: Fix frequency calculation Asmaa Mnebhi
2022-09-21 19:44 ` Wolfram Sang
2022-09-20 17:47 ` [PATCH v5 2/8] i2c: i2c-mlxbf.c: remove IRQF_ONESHOT Asmaa Mnebhi
2022-09-20 17:47 ` [PATCH v5 3/8] i2c: i2c-mlxbf.c: incorrect base address passed during io write Asmaa Mnebhi
2022-09-20 17:47 ` [PATCH v5 4/8] i2c: i2c-mlxbf: prevent stack overflow in mlxbf_i2c_smbus_start_transaction() Asmaa Mnebhi
2022-09-20 17:47 ` Asmaa Mnebhi [this message]
2022-09-20 17:47 ` [PATCH v5 6/8] i2c: i2c-mlxbf: add multi slave functionality Asmaa Mnebhi
2022-09-20 17:47 ` [PATCH v5 7/8] i2c: i2c-mlxbf.c: support BlueField-3 SoC Asmaa Mnebhi
2022-09-20 17:47 ` [PATCH v5 8/8] i2c: i2c-mlxbf.c: Update binding devicetree Asmaa Mnebhi
2022-09-21 6:55 ` Krzysztof Kozlowski
2022-09-21 13:12 ` Asmaa Mnebhi
2022-09-21 13:19 ` Krzysztof Kozlowski
2022-09-21 20:01 ` How to remove DT support from a driver? (was Re: [PATCH v5 8/8] i2c: i2c-mlxbf.c: Update binding devicetree) Wolfram Sang
2022-09-21 20:17 ` Asmaa Mnebhi
2022-09-24 17:31 ` Rob Herring
2022-09-26 13:18 ` Asmaa Mnebhi
2022-09-26 18:57 ` Wolfram Sang
2022-09-21 6:57 ` [PATCH v5 8/8] i2c: i2c-mlxbf.c: Update binding devicetree Krzysztof Kozlowski
2022-09-21 6:59 ` Krzysztof Kozlowski
2022-09-21 13:24 ` [PATCH v5 0/8] i2c: i2c-mlxbf.c: bug fixes and new feature support Asmaa Mnebhi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220920174736.9766-6-asmaa@nvidia.com \
--to=asmaa@nvidia.com \
--cc=kblaiech@nvidia.com \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wsa+renesas@sang-engineering.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox