linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Felipe Balbi <balbi@ti.com>
To: Alexander Kochetkov <al.kochet@gmail.com>
Cc: linux-omap@vger.kernel.org, linux-i2c@vger.kernel.org,
	linux-kernel@vger.kernel.org, Wolfram Sang <wsa@the-dreams.de>,
	Tony Lindgren <tony@atomide.com>, Felipe Balbi <balbi@ti.com>,
	Kevin Hilman <khilman@linaro.org>
Subject: Re: [PATCH 2/4] i2c: omap: implement workaround for handling invalid BB-bit values
Date: Mon, 24 Nov 2014 13:09:37 -0600	[thread overview]
Message-ID: <20141124190937.GF25712@saruman> (raw)
In-Reply-To: <1416518925-20679-3-git-send-email-al.kochet@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 6080 bytes --]

Hi,

On Fri, Nov 21, 2014 at 01:28:43AM +0400, Alexander Kochetkov wrote:
> In a multimaster environment, after IP software reset, BB-bit value doesn't
> correspond to the current bus state. It may happen what BB-bit will be 0,
> while the bus is busy due to another I2C master activity.
> 
> Any transfer started when BB=0 and bus is busy wouldn't be completed by IP
> and results in controller timeout. More over, in some cases IP could
> interrupt another master's transfer and corrupt data on wire.
> 
> The commit implement method allowing to prevent IP from entering into
> "controller timeout" state and from "data corruption" state.
> 
> The one drawback is the need to wait for 10ms before the first transfer.
> 
> Tested on Beagleboard XM C.
> 
> Signed-off-by: Alexander Kochetkov <al.kochet@gmail.com>

we have a report from Kevin Hilman that this commit breaks OMAP3530, see
[1]

[1] http://storage.armcloud.us/kernel-ci/next/next-20141124/arm-omap2plus_defconfig/boot-omap3-overo-tobi.log

> ---
>  drivers/i2c/busses/i2c-omap.c |  103 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 103 insertions(+)
> 
> diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
> index a021733..3ffb9c0 100644
> --- a/drivers/i2c/busses/i2c-omap.c
> +++ b/drivers/i2c/busses/i2c-omap.c
> @@ -58,6 +58,9 @@
>  /* timeout for pm runtime autosuspend */
>  #define OMAP_I2C_PM_TIMEOUT		1000	/* ms */
>  
> +/* timeout for making decision on bus free status */
> +#define OMAP_I2C_BUS_FREE_TIMEOUT (msecs_to_jiffies(10))
> +
>  /* For OMAP3 I2C_IV has changed to I2C_WE (wakeup enable) */
>  enum {
>  	OMAP_I2C_REV_REG = 0,
> @@ -210,6 +213,9 @@ struct omap_i2c_dev {
>  						 */
>  	u32			rev;
>  	unsigned		b_hw:1;		/* bad h/w fixes */
> +	unsigned		bb_valid:1;	/* true when BB-bit reflects
> +						 * the I2C bus state
> +						 */
>  	unsigned		receiver:1;	/* true when we're in receiver mode */
>  	u16			iestate;	/* Saved interrupt register */
>  	u16			pscstate;
> @@ -336,7 +342,10 @@ static int omap_i2c_reset(struct omap_i2c_dev *dev)
>  		/* SYSC register is cleared by the reset; rewrite it */
>  		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, sysc);
>  
> +		/* Schedule I2C-bus monitoring on the next transfer */
> +		dev->bb_valid = 0;
>  	}
> +
>  	return 0;
>  }
>  
> @@ -449,6 +458,11 @@ static int omap_i2c_init(struct omap_i2c_dev *dev)
>  	dev->scllstate = scll;
>  	dev->sclhstate = sclh;
>  
> +	if (dev->rev < OMAP_I2C_OMAP1_REV_2) {
> +		/* Not implemented */
> +		dev->bb_valid = 1;
> +	}
> +
>  	__omap_i2c_init(dev);
>  
>  	return 0;
> @@ -473,6 +487,91 @@ static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
>  	return 0;
>  }
>  
> +/*
> + * Wait while BB-bit doesn't reflect the I2C bus state
> + *
> + * In a multimaster environment, after IP software reset, BB-bit value doesn't
> + * correspond to the current bus state. It may happen what BB-bit will be 0,
> + * while the bus is busy due to another I2C master activity.
> + * Here are BB-bit values after reset:
> + *     SDA   SCL   BB   NOTES
> + *       0     0    0   1, 2
> + *       1     0    0   1, 2
> + *       0     1    1
> + *       1     1    0   3
> + * Later, if IP detect SDA=0 and SCL=1 (ACK) or SDA 1->0 while SCL=1 (START)
> + * combinations on the bus, it set BB-bit to 1.
> + * If IP detect SDA 0->1 while SCL=1 (STOP) combination on the bus,
> + * it set BB-bit to 0 and BF to 1.
> + * BB and BF bits correctly tracks the bus state while IP is suspended
> + * BB bit became valid on the next FCLK clock after CON_EN bit set
> + *
> + * NOTES:
> + * 1. Any transfer started when BB=0 and bus is busy wouldn't be
> + *    completed by IP and results in controller timeout.
> + * 2. Any transfer started when BB=0 and SCL=0 results in IP
> + *    starting to drive SDA low. In that case IP corrupt data
> + *    on the bus.
> + * 3. Any transfer started in the middle of another master's transfer
> + *    results in unpredictable results and data corruption
> + */
> +static int omap_i2c_wait_for_bb_valid(struct omap_i2c_dev *dev)
> +{
> +	unsigned long bus_free_timeout = 0;
> +	unsigned long timeout;
> +	int bus_free = 0;
> +	u16 stat, systest;
> +
> +	if (dev->bb_valid)
> +		return 0;
> +
> +	timeout = jiffies + OMAP_I2C_TIMEOUT;
> +	while (1) {
> +		stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
> +		/*
> +		 * We will see BB or BF event in a case IP had detected any
> +		 * activity on the I2C bus. Now IP correctly tracks the bus
> +		 * state. BB-bit value is valid.
> +		 */
> +		if (stat & (OMAP_I2C_STAT_BB | OMAP_I2C_STAT_BF))
> +			break;
> +
> +		/*
> +		 * Otherwise, we must look signals on the bus to make
> +		 * the right decision.
> +		 */
> +		systest = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
> +		if ((systest & OMAP_I2C_SYSTEST_SCL_I_FUNC) &&
> +		    (systest & OMAP_I2C_SYSTEST_SDA_I_FUNC)) {
> +			if (!bus_free) {
> +				bus_free_timeout = jiffies +
> +					OMAP_I2C_BUS_FREE_TIMEOUT;
> +				bus_free = 1;
> +			}
> +
> +			/*
> +			 * SDA and SCL lines was high for 10 ms without bus
> +			 * activity detected. The bus is free. Consider
> +			 * BB-bit value is valid.
> +			 */
> +			if (time_after(jiffies, bus_free_timeout))
> +				break;
> +		} else {
> +			bus_free = 0;
> +		}
> +
> +		if (time_after(jiffies, timeout)) {
> +			dev_warn(dev->dev, "timeout waiting for bus ready\n");
> +			return -ETIMEDOUT;
> +		}
> +
> +		msleep(1);
> +	}
> +
> +	dev->bb_valid = 1;
> +	return 0;
> +}
> +
>  static void omap_i2c_resize_fifo(struct omap_i2c_dev *dev, u8 size, bool is_rx)
>  {
>  	u16		buf;
> @@ -643,6 +742,10 @@ omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
>  	if (IS_ERR_VALUE(r))
>  		goto out;
>  
> +	r = omap_i2c_wait_for_bb_valid(dev);
> +	if (r < 0)
> +		goto out;
> +
>  	r = omap_i2c_wait_for_bb(dev);
>  	if (r < 0)
>  		goto out;
> -- 
> 1.7.9.5
> 

-- 
balbi

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  parent reply	other threads:[~2014-11-24 19:09 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-20 21:28 [RFC 0/4] i2c: omap: patch series related to multimaster mode Alexander Kochetkov
2014-11-20 21:28 ` [PATCH 1/4] i2c: omap: cleanup register definitions Alexander Kochetkov
     [not found]   ` <1416518925-20679-2-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-21 16:08     ` Felipe Balbi
     [not found] ` <1416518925-20679-1-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-20 21:28   ` [PATCH 2/4] i2c: omap: implement workaround for handling invalid BB-bit values Alexander Kochetkov
2014-11-21 16:08     ` Felipe Balbi
2014-11-22 13:23       ` Wolfram Sang
2014-11-22 14:06         ` Alexander Kochetkov
     [not found]           ` <B6D9B4D7-25CB-456F-88B4-EB6A53E33A9D-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-22 18:02             ` Wolfram Sang
2014-11-23  4:43               ` Felipe Balbi
2014-11-23 13:18                 ` Alexander Kochetkov
2014-11-24 19:08                   ` Felipe Balbi
2014-11-24 19:09     ` Felipe Balbi [this message]
2014-11-22 19:47   ` [PATCH v3 0/4] i2c: omap: patch series related to multimaster mode Alexander Kochetkov
     [not found]     ` <1416685634-5864-1-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-22 19:47       ` [PATCH v3 1/4] i2c: omap: cleanup register definitions Alexander Kochetkov
2014-11-22 19:47       ` [PATCH v3 3/4] i2c: omap: don't reset controller if Arbitration Lost detected Alexander Kochetkov
2014-11-22 21:08       ` [PATCH v3 0/4] i2c: omap: patch series related to multimaster mode Wolfram Sang
2014-11-22 19:47     ` [PATCH v3 2/4] i2c: omap: implement workaround for handling invalid BB-bit values Alexander Kochetkov
2014-11-24 19:08       ` Kevin Hilman
     [not found]         ` <CAMAWPa8whctBR6fBFaSzPvm3qn+QK06CimRrxFxxH=0kNkF4UA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-11-24 19:10           ` Felipe Balbi
2014-11-24 19:13             ` Wolfram Sang
2014-11-24 19:25               ` Tony Lindgren
2014-11-24 19:39         ` Alexander Kochetkov
     [not found]           ` <47A1A441-952C-4AC3-859C-5A8E405767E0-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-24 19:47             ` Tony Lindgren
2014-11-24 20:05               ` Alexander Kochetkov
     [not found]                 ` <47749B61-5924-4E56-9931-77B0CFC0AAA0-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-25  0:09                   ` Alexander Kochetkov
     [not found]                     ` <06376010-5DED-49AA-9494-A9546567E7CA-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-25 15:41                       ` Tony Lindgren
2014-11-24 21:08               ` Alexander Kochetkov
2014-11-26 22:30               ` Alexander Kochetkov
     [not found]       ` <1416685634-5864-3-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-24 20:33         ` [PATCH] omap: i2c: don't check bus state IP rev3.3 and earlier Alexander Kochetkov
     [not found]           ` <1416861189-17109-1-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-24 21:08             ` Tony Lindgren
     [not found]               ` <20141124210809.GG2817-4v6yS6AI5VpBDgjK7y7TUQ@public.gmane.org>
2014-11-24 22:20                 ` [PATCH v2] " Alexander Kochetkov
2014-11-25 14:19                   ` Wolfram Sang
2014-11-25 14:38                     ` Alexander Kochetkov
     [not found]                       ` <7E0A0B24-91D5-41A4-B007-004C5965D0D7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-25 19:13                         ` Kevin Hilman
     [not found]                           ` <7hvbm3jdq1.fsf-1D3HCaltpLuhEniVeURVKkEOCMrvLtNR@public.gmane.org>
2014-11-26 19:26                             ` Alexander Kochetkov
     [not found]                               ` <65BAE81D-E894-48EB-B5A0-0D0FFB9B448F-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-26 21:33                                 ` Kevin Hilman
     [not found]                   ` <1416867655-22232-1-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-25 19:12                     ` Kevin Hilman
2014-11-26 19:05       ` [RFC] i2c: omap: TEST: do IP reset during probe Alexander Kochetkov
2014-11-26 21:25         ` Kevin Hilman
     [not found]           ` <7hzjbdd57j.fsf-1D3HCaltpLuhEniVeURVKkEOCMrvLtNR@public.gmane.org>
2014-11-28 22:13             ` Tony Lindgren
     [not found]               ` <20141128221350.GW2817-4v6yS6AI5VpBDgjK7y7TUQ@public.gmane.org>
2014-11-28 23:25                 ` Alexander Kochetkov
     [not found]                   ` <E4A4FA05-0752-41CB-B86B-84035C6EAF05-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-29 22:06                     ` Tony Lindgren
2014-11-29  2:14                 ` Alexander Kochetkov
2014-11-22 19:47     ` [PATCH v3 4/4] i2c: omap: add notes related to i2c multimaster mode Alexander Kochetkov
2014-11-20 21:28 ` [PATCH 3/4] i2c: omap: don't reset controller if Arbitration Lost detected Alexander Kochetkov
2014-11-21 16:08   ` Felipe Balbi
2014-11-21 18:11   ` Wolfram Sang
     [not found]   ` <1416518925-20679-4-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-21 22:51     ` [PATCH v2 " Alexander Kochetkov
     [not found]       ` <1416610307-3698-1-git-send-email-al.kochet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-11-22  1:07         ` Felipe Balbi
2014-11-20 21:28 ` [PATCH 4/4] i2c: omap: add notes related to i2c multimaster mode Alexander Kochetkov
2014-11-21 16:08   ` Felipe Balbi

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=20141124190937.GF25712@saruman \
    --to=balbi@ti.com \
    --cc=al.kochet@gmail.com \
    --cc=khilman@linaro.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=tony@atomide.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).