From patchwork Wed Mar 23 07:48:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roese X-Patchwork-Id: 601128 X-Patchwork-Delegate: hs@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 3qVMC93tnZz9srh for ; Wed, 23 Mar 2016 18:49:09 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 4F7DEA750E; Wed, 23 Mar 2016 08:49:07 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GSl8LeV3z9w8; Wed, 23 Mar 2016 08:49:07 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id CC2BFA748F; Wed, 23 Mar 2016 08:49:06 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BBBAAA748F for ; Wed, 23 Mar 2016 08:49:03 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8UtYAG-oELGL for ; Wed, 23 Mar 2016 08:49:03 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from sohosted5.com (ns2.sohosted5.com [195.8.208.35]) by theia.denx.de (Postfix) with ESMTPS id 836FCA748A for ; Wed, 23 Mar 2016 08:49:00 +0100 (CET) Received: from stefan-work.fritz.box ([185.22.143.102]) by sohosted5.com with MailEnable ESMTP; Wed, 23 Mar 2016 08:48:53 +0100 From: Stefan Roese To: u-boot@lists.denx.de Date: Wed, 23 Mar 2016 08:48:52 +0100 Message-Id: <1458719332-15215-1-git-send-email-sr@denx.de> X-Mailer: git-send-email 2.7.4 X-ME-Bayesian: 0.000000 Cc: Marek Vasut Subject: [U-Boot] [PATCH 2/6 v3] i2c: designware_i2c: Add dw_i2c_enable() helper function X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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 Cc: Simon Glass Cc: Bin Meng Cc: Marek Vasut Cc: Heiko Schocher Reviewed-by: Bin Meng --- v3: - Rework dw_i2c_enable() as suggested by Marek v2: - Use true / false for binary values as suggested by Bin drivers/i2c/designware_i2c.c | 47 +++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/drivers/i2c/designware_i2c.c b/drivers/i2c/designware_i2c.c index e768cde..d5b3e29 100644 --- a/drivers/i2c/designware_i2c.c +++ b/drivers/i2c/designware_i2c.c @@ -34,6 +34,27 @@ 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) +{ + u32 ena = enable ? IC_ENABLE_0B : 0; + int timeout = 100; + + do { + writel(ena, &i2c_base->ic_enable); + if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena) + 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 +66,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 +102,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 +140,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 +152,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 +164,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); } /*