All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Glauber <jglauber@cavium.com>
To: Wolfram Sang <wsa@the-dreams.de>
Cc: linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org,
	David Daney <ddaney@caviumnetworks.com>,
	Jan Glauber <jglauber@cavium.com>
Subject: [PATCH v7 02/15] i2c: octeon: Use i2c recovery framework
Date: Mon, 25 Apr 2016 16:33:31 +0200	[thread overview]
Message-ID: <1461594824-7215-3-git-send-email-jglauber@cavium.com> (raw)
In-Reply-To: <1461594824-7215-1-git-send-email-jglauber@cavium.com>

Switch to the i2c bus recovery framework using generic SCL recovery.
If this fails try to reset the hardware. The recovery is triggered
during START on timeout of the interrupt or failure to reach
the START / repeated-START condition.

The START function is moved to xfer and while at it remove the
xfer debug message (i2c core already provides a debug message
for this).

Signed-off-by: Jan Glauber <jglauber@cavium.com>
---
 drivers/i2c/busses/i2c-octeon.c | 160 ++++++++++++++++++++++++----------------
 1 file changed, 97 insertions(+), 63 deletions(-)

diff --git a/drivers/i2c/busses/i2c-octeon.c b/drivers/i2c/busses/i2c-octeon.c
index 471d06e..0f536a1 100644
--- a/drivers/i2c/busses/i2c-octeon.c
+++ b/drivers/i2c/busses/i2c-octeon.c
@@ -90,6 +90,8 @@
 #define TWSI_INT_CORE_EN	BIT_ULL(6)
 #define TWSI_INT_SDA_OVR	BIT_ULL(8)
 #define TWSI_INT_SCL_OVR	BIT_ULL(9)
+#define TWSI_INT_SDA		BIT_ULL(10)
+#define TWSI_INT_SCL		BIT_ULL(11)
 
 struct octeon_i2c {
 	wait_queue_head_t queue;
@@ -152,6 +154,18 @@ static u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
 #define octeon_i2c_stat_read(i2c)					\
 	octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
 
+
+/**
+ * octeon_i2c_read_int - read the TWSI_INT register
+ * @i2c: The struct octeon_i2c
+ *
+ * Returns the value of the register.
+ */
+static u64 octeon_i2c_read_int(struct octeon_i2c *i2c)
+{
+	return __raw_readq(i2c->twsi_base + TWSI_INT);
+}
+
 /**
  * octeon_i2c_write_int - write the TWSI_INT register
  * @i2c: The struct octeon_i2c
@@ -182,33 +196,6 @@ static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
 	octeon_i2c_write_int(i2c, 0);
 }
 
-/**
- * octeon_i2c_unblock - unblock the bus
- * @i2c: The struct octeon_i2c
- *
- * If there was a reset while a device was driving 0 to bus, bus is blocked.
- * We toggle it free manually by some clock cycles and send a stop.
- */
-static void octeon_i2c_unblock(struct octeon_i2c *i2c)
-{
-	int i;
-
-	dev_dbg(i2c->dev, "%s\n", __func__);
-
-	for (i = 0; i < 9; i++) {
-		octeon_i2c_write_int(i2c, 0);
-		udelay(5);
-		octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
-		udelay(5);
-	}
-	/* hand-crank a STOP */
-	octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
-	udelay(5);
-	octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
-	udelay(5);
-	octeon_i2c_write_int(i2c, 0);
-}
-
 /* interrupt service routine */
 static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
 {
@@ -371,6 +358,17 @@ static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
 	return -EIO;
 }
 
+static int octeon_i2c_recovery(struct octeon_i2c *i2c)
+{
+	int ret;
+
+	ret = i2c_recover_bus(&i2c->adap);
+	if (ret)
+		/* recover failed, try hardware re-init */
+		ret = octeon_i2c_init_lowlevel(i2c);
+	return ret;
+}
+
 /**
  * octeon_i2c_start - send START to the bus
  * @i2c: The struct octeon_i2c
@@ -379,34 +377,23 @@ static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
  */
 static int octeon_i2c_start(struct octeon_i2c *i2c)
 {
-	int result;
-	u8 data;
+	int ret;
+	u8 stat;
 
 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
+	ret = octeon_i2c_wait(i2c);
+	if (ret)
+		goto error;
 
-	result = octeon_i2c_wait(i2c);
-	if (result) {
-		if (octeon_i2c_stat_read(i2c) == STAT_IDLE) {
-			/*
-			 * Controller refused to send start flag May
-			 * be a client is holding SDA low - let's try
-			 * to free it.
-			 */
-			octeon_i2c_unblock(i2c);
-			octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
-			result = octeon_i2c_wait(i2c);
-		}
-		if (result)
-			return result;
-	}
-
-	data = octeon_i2c_stat_read(i2c);
-	if ((data != STAT_START) && (data != STAT_REP_START)) {
-		dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
-		return -EIO;
-	}
+	stat = octeon_i2c_stat_read(i2c);
+	if (stat == STAT_START || stat == STAT_REP_START)
+		/* START successful, bail out */
+		return 0;
 
-	return 0;
+error:
+	/* START failed, try to recover */
+	ret = octeon_i2c_recovery(i2c);
+	return (ret) ? ret : -EAGAIN;
 }
 
 /* send STOP to the bus */
@@ -431,10 +418,6 @@ static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
 {
 	int i, result;
 
-	result = octeon_i2c_start(i2c);
-	if (result)
-		return result;
-
 	octeon_i2c_data_write(i2c, target << 1);
 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
 
@@ -479,10 +462,6 @@ static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
 	if (length < 1)
 		return -EINVAL;
 
-	result = octeon_i2c_start(i2c);
-	if (result)
-		return result;
-
 	octeon_i2c_data_write(i2c, (target << 1) | 1);
 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
 
@@ -546,10 +525,10 @@ static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 	for (i = 0; ret == 0 && i < num; i++) {
 		struct i2c_msg *pmsg = &msgs[i];
 
-		dev_dbg(i2c->dev,
-			"Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
-			 pmsg->flags & I2C_M_RD ? "read" : "write",
-			 pmsg->len, pmsg->addr, i + 1, num);
+		ret = octeon_i2c_start(i2c);
+		if (ret)
+			return ret;
+
 		if (pmsg->flags & I2C_M_RD)
 			ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
 					      &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
@@ -562,6 +541,60 @@ static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 	return (ret != 0) ? ret : num;
 }
 
+static int octeon_i2c_get_scl(struct i2c_adapter *adap)
+{
+	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
+	u64 state;
+
+	state = octeon_i2c_read_int(i2c);
+	return state & TWSI_INT_SCL;
+}
+
+static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
+{
+	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
+
+	octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
+}
+
+static int octeon_i2c_get_sda(struct i2c_adapter *adap)
+{
+	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
+	u64 state;
+
+	state = octeon_i2c_read_int(i2c);
+	return state & TWSI_INT_SDA;
+}
+
+static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
+{
+	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
+
+	/*
+	 * The stop resets the state machine, does not _transmit_ STOP unless
+	 * engine was active.
+	 */
+	octeon_i2c_stop(i2c);
+
+	octeon_i2c_write_int(i2c, 0);
+}
+
+static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
+{
+	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
+
+	octeon_i2c_write_int(i2c, 0);
+}
+
+static struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
+	.recover_bus = i2c_generic_scl_recovery,
+	.get_scl = octeon_i2c_get_scl,
+	.set_scl = octeon_i2c_set_scl,
+	.get_sda = octeon_i2c_get_sda,
+	.prepare_recovery = octeon_i2c_prepare_recovery,
+	.unprepare_recovery = octeon_i2c_unprepare_recovery,
+};
+
 static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
 {
 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
@@ -642,6 +675,7 @@ static int octeon_i2c_probe(struct platform_device *pdev)
 	i2c->adap = octeon_i2c_ops;
 	i2c->adap.timeout = msecs_to_jiffies(2);
 	i2c->adap.retries = 5;
+	i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
 	i2c->adap.dev.parent = &pdev->dev;
 	i2c->adap.dev.of_node = node;
 	i2c_set_adapdata(&i2c->adap, i2c);
-- 
1.9.1

  parent reply	other threads:[~2016-04-25 14:39 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-25 14:33 [PATCH v7 00/15] i2c-octeon and i2c-thunderx drivers Jan Glauber
2016-04-25 14:33 ` [PATCH v7 01/15] i2c: octeon: Improve error status checking Jan Glauber
2016-04-25 21:20   ` Wolfram Sang
2016-04-25 14:33 ` Jan Glauber [this message]
2016-04-25 21:29   ` [PATCH v7 02/15] i2c: octeon: Use i2c recovery framework Wolfram Sang
2016-04-25 14:33 ` [PATCH v7 03/15] i2c: octeon: Remove I2C_FUNC_SMBUS_QUICK support Jan Glauber
2016-04-25 22:16   ` Wolfram Sang
2016-04-25 22:28     ` David Daney
2016-04-25 22:28       ` David Daney
2016-04-26  5:58     ` Jan Glauber
2016-04-26  5:58       ` Jan Glauber
2016-04-26  6:42       ` Jan Glauber
2016-04-26  6:42         ` Jan Glauber
2016-04-26  7:36         ` Wolfram Sang
2016-04-26 12:34           ` Jan Glauber
2016-04-26 12:34             ` Jan Glauber
2016-04-26 12:53             ` Wolfram Sang
2016-04-26 14:26               ` [PATCH] i2c: octeon: Remove zero-length message support Jan Glauber
2016-04-26 21:04                 ` Wolfram Sang
2016-04-25 14:33 ` [PATCH v7 04/15] i2c: octeon: Add flush writeq helper function Jan Glauber
2016-04-25 21:33   ` Wolfram Sang
2016-04-25 14:33 ` [PATCH v7 05/15] i2c: octeon: Enable High-Level Controller Jan Glauber
2016-04-25 21:44   ` Wolfram Sang
2016-04-26  5:51     ` Jan Glauber
2016-04-26  5:51       ` Jan Glauber
2016-04-25 14:33 ` [PATCH v7 06/15] dt-bindings: i2c: Add Octeon cn78xx TWSI Jan Glauber
2016-04-25 21:47   ` Wolfram Sang
2016-04-25 14:33 ` [PATCH v7 07/15] i2c: octeon: Add support for cn78xx chips Jan Glauber
2016-04-25 21:47   ` Wolfram Sang
2016-04-25 14:33 ` [PATCH v7 08/15] i2c: octeon: Improve performance if interrupt is early Jan Glauber
2016-04-26 21:10   ` Wolfram Sang
2016-04-26 21:19     ` Wolfram Sang
2016-04-25 14:33 ` [PATCH v7 09/15] i2c: octeon: Add workaround for broken irqs on CN3860 Jan Glauber
2016-04-26 21:17   ` Wolfram Sang
2016-04-27  9:37     ` Jan Glauber
2016-04-27  9:37       ` Jan Glauber
2016-04-27  9:44     ` [PATCH] " Jan Glauber
2016-04-27 16:56       ` Wolfram Sang
2016-04-25 14:33 ` [PATCH v7 10/15] i2c: octeon: Move read function before write Jan Glauber
2016-04-25 14:33 ` [PATCH v7 11/15] i2c: octeon: Rename driver to prepare for split Jan Glauber
2016-04-25 14:33 ` [PATCH v7 12/15] i2c: octeon: Split the driver into two parts Jan Glauber
2016-04-25 14:33 ` [PATCH v7 13/15] i2c: thunderx: Add i2c driver for ThunderX SOC Jan Glauber
2016-04-25 14:33 ` [PATCH v7 14/15] i2c: octeon,thunderx: Move register offsets to struct Jan Glauber
2016-04-25 14:33 ` [PATCH v7 15/15] i2c: thunderx: Add smbus alert support Jan Glauber

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=1461594824-7215-3-git-send-email-jglauber@cavium.com \
    --to=jglauber@cavium.com \
    --cc=ddaney@caviumnetworks.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wsa@the-dreams.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.