public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Stefan Roese <sr@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 2/6 v2] i2c: designware_i2c: Add dw_i2c_enable() helper function
Date: Wed, 23 Mar 2016 06:46:24 +0100	[thread overview]
Message-ID: <1458711988-3235-2-git-send-email-sr@denx.de> (raw)
In-Reply-To: <1458711988-3235-1-git-send-email-sr@denx.de>

dw_i2c_enable() is used to dis-/en-able the I2C controller. It makes
sense to add such a function, as the controller is dis-/en-abled
multiple times in the code. Additionally, this function now checks,
if the controller is really dis-/en-abled. This code is copied
from the Linux I2C driver version.

Signed-off-by: Stefan Roese <sr@denx.de>
Cc: Simon Glass <sjg@chromium.org>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Marek Vasut <marex@denx.de>
Cc: Heiko Schocher <hs@denx.de>
---
v2:
- Use true / false for binary values as suggested by Bin

 drivers/i2c/designware_i2c.c | 46 +++++++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/drivers/i2c/designware_i2c.c b/drivers/i2c/designware_i2c.c
index e768cde..2b0c8cb 100644
--- a/drivers/i2c/designware_i2c.c
+++ b/drivers/i2c/designware_i2c.c
@@ -34,6 +34,26 @@ static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
 	return NULL;
 }
 
+static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
+{
+	int timeout = 100;
+
+	do {
+		writel(enable, &i2c_base->ic_enable);
+		if ((readl(&i2c_base->ic_enable_status) & 1) == enable)
+			return;
+
+		/*
+		 * Wait 10 times the signaling period of the highest I2C
+		 * transfer supported by the driver (for 400KHz this is
+		 * 25us) as described in the DesignWare I2C databook.
+		 */
+		udelay(25);
+	} while (timeout--);
+
+	printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
+}
+
 /*
  * set_speed - Set the i2c speed mode (standard, high, fast)
  * @i2c_spd:	required i2c speed mode
@@ -45,12 +65,9 @@ static void set_speed(struct i2c_adapter *adap, int i2c_spd)
 	struct i2c_regs *i2c_base = i2c_get_base(adap);
 	unsigned int cntl;
 	unsigned int hcnt, lcnt;
-	unsigned int enbl;
 
 	/* to set speed cltr must be disabled */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl &= ~IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, false);
 
 	cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
 
@@ -84,8 +101,7 @@ static void set_speed(struct i2c_adapter *adap, int i2c_spd)
 	writel(cntl, &i2c_base->ic_con);
 
 	/* Enable back i2c now speed set */
-	enbl |= IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, true);
 }
 
 /*
@@ -123,12 +139,9 @@ static void dw_i2c_init(struct i2c_adapter *adap, int speed,
 			int slaveaddr)
 {
 	struct i2c_regs *i2c_base = i2c_get_base(adap);
-	unsigned int enbl;
 
 	/* Disable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl &= ~IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, false);
 
 	writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con);
 	writel(IC_RX_TL, &i2c_base->ic_rx_tl);
@@ -138,9 +151,7 @@ static void dw_i2c_init(struct i2c_adapter *adap, int speed,
 	writel(slaveaddr, &i2c_base->ic_sar);
 
 	/* Enable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl |= IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, true);
 }
 
 /*
@@ -152,19 +163,14 @@ static void dw_i2c_init(struct i2c_adapter *adap, int speed,
 static void i2c_setaddress(struct i2c_adapter *adap, unsigned int i2c_addr)
 {
 	struct i2c_regs *i2c_base = i2c_get_base(adap);
-	unsigned int enbl;
 
 	/* Disable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl &= ~IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, false);
 
 	writel(i2c_addr, &i2c_base->ic_tar);
 
 	/* Enable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl |= IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, true);
 }
 
 /*
-- 
2.7.4

  reply	other threads:[~2016-03-23  5:46 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-23  5:46 [U-Boot] [PATCH 1/6 v2] i2c: designware_i2c: Add ic_enable_status to ic_regs struct Stefan Roese
2016-03-23  5:46 ` Stefan Roese [this message]
2016-03-23  6:16   ` [U-Boot] [PATCH 2/6 v2] i2c: designware_i2c: Add dw_i2c_enable() helper function Marek Vasut
2016-03-23  5:46 ` [U-Boot] [PATCH 3/6 v2] i2c: designware_i2c: Integrate set_speed() into dw_i2c_set_bus_speed() Stefan Roese
2016-03-23  5:46 ` [U-Boot] [PATCH 4/6 v2] i2c: designware_i2c: Prepare for DM driver conversion Stefan Roese
2016-03-28  7:33   ` Heiko Schocher
2016-04-12 13:22     ` Stefan Roese
2016-04-13  5:49       ` Heiko Schocher
2016-04-13  5:56         ` Stefan Roese
2016-04-13  7:00           ` Heiko Schocher
2016-04-13  7:05             ` Stefan Roese
2016-04-13  7:39               ` Heiko Schocher
2016-04-13  8:05                 ` Bin Meng
2016-04-13  8:10                   ` Stefan Roese
2016-03-23  5:46 ` [U-Boot] [PATCH 5/6 v2] i2c: designware_i2c: Add DM support Stefan Roese
2016-03-23  5:46 ` [U-Boot] [PATCH 6/6 v2] i2c: designware_i2c: Add support for PCI(e) based I2C cores (x86) Stefan Roese

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=1458711988-3235-2-git-send-email-sr@denx.de \
    --to=sr@denx.de \
    --cc=u-boot@lists.denx.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