From patchwork Sun May 30 22:56:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 12288483 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64B73C47092 for ; Sun, 30 May 2021 22:58:58 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 197856023F for ; Sun, 30 May 2021 22:58:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 197856023F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=alliedtelesis.co.nz Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=A+52BG/uWLxNXYIGkLqgYLRI2bwq5uDstPShAeT2RTM=; b=s4O4/4H06xRa/c 4YzvvK2QW9AYjzmfv8zbhHTKLnmbf2Bw71mFS1y+jglu0K1IQmGEYNKbK34ftKcZWI73AoZ9fMtSB 9i0EjJaGeYzRnJzV2GOLsnyVnnkgOksEMVBbp2SJK/GZFOzUkhAechG5eazgrg9XUTXiQySZ9//wW uaH7aAzcYIr89lvSvPaSSKKbzGS3O6/as3VFm6Vb//EE/vWgVdt+xSpDdabcxr6AV9iLwb3CYVxx8 2gVCP0OZx5MJveWlmGbvmZSxoc9KBHDusi/Nm2scAh2PEWQIRR0fCFMNQEAk/VDW2pEKbAzYXvtKd cyHBIbLr3Npex8TVtbNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lnUMu-00AHBz-4I; Sun, 30 May 2021 22:57:16 +0000 Received: from gate2.alliedtelesis.co.nz ([202.36.163.20]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lnUMp-00AHBU-GL for linux-arm-kernel@lists.infradead.org; Sun, 30 May 2021 22:57:13 +0000 Received: from svr-chch-seg1.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id 257348364F; Mon, 31 May 2021 10:57:03 +1200 (NZST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1622415423; bh=r1PeCdtDfQf/3d2GiJhstp2bICjzI0nnfAcIwqjbXZ4=; h=From:To:Cc:Subject:Date; b=lOXuLy/60scgTUC2Bu9b4Mb4n4eas1m020Az7oK+E8nZ4IzvYSFnShj8cEkRmMNen dCvIJZfAA3vjwOoekMBbVzQbjz1qeLYBvWQKaZo+/LleN6KZDLsoW9sJ+48xQhsTqu uPMn40ebp9NNg2/G2jyk8VMADGlhrSlm2gyYy/JpM6q0mhKGBjl043pYBBWgLZquLX jfTIDmxKDg+aAKD3NfVeWMTt2zDbeEcOffXCyDNgIVFVN9YPh7HR4h/ZF0wYn6ltF8 vtpMeT+3RJevKtKv9qxIS/N1c8f0alA0/xFVxW1avejPR4sd5SPYlJW/fWHwGN3p71 hv5nVHGDwcJxA== Received: from pat.atlnz.lc (Not Verified[10.32.16.33]) by svr-chch-seg1.atlnz.lc with Trustwave SEG (v8, 2, 6, 11305) id ; Mon, 31 May 2021 10:57:03 +1200 Received: from chrisp-dl.ws.atlnz.lc (chrisp-dl.ws.atlnz.lc [10.33.22.20]) by pat.atlnz.lc (Postfix) with ESMTP id 04F3413EEA1; Mon, 31 May 2021 10:57:03 +1200 (NZST) Received: by chrisp-dl.ws.atlnz.lc (Postfix, from userid 1030) id F2E75283A4E; Mon, 31 May 2021 10:57:02 +1200 (NZST) From: Chris Packham To: wsa@kernel.org, rjui@broadcom.com, sbranden@broadcom.com, bcm-kernel-feedback-list@broadcom.com Cc: linux-i2c@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Richard Laing , Chris Packham Subject: [PATCH] i2c: bcm-iproc: Add i2c recovery support Date: Mon, 31 May 2021 10:56:59 +1200 Message-Id: <20210530225659.17138-1-chris.packham@alliedtelesis.co.nz> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-SEG-SpamProfiler-Analysis: v=2.3 cv=WOcBoUkR c=1 sm=1 tr=0 a=KLBiSEs5mFS1a/PbTCJxuA==:117 a=5FLXtPjwQuUA:10 a=Jg9UQ0_FWdfR-ABCIGgA:9 X-SEG-SpamProfiler-Score: 0 x-atlnz-ls: pat X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210530_155712_044782_9A7E134A X-CRM114-Status: GOOD ( 13.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Richard Laing The bcm-iproc controller can put the SDA/SCL lines into bit-bang mode, make use of this to support i2c bus recovery. Signed-off-by: Richard Laing Signed-off-by: Chris Packham --- Notes: Richard did most of the work on this. I'm just cleaning it up to get it upstream. drivers/i2c/busses/i2c-bcm-iproc.c | 115 +++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/drivers/i2c/busses/i2c-bcm-iproc.c b/drivers/i2c/busses/i2c-bcm-iproc.c index cceaf69279a9..d63a286c1660 100644 --- a/drivers/i2c/busses/i2c-bcm-iproc.c +++ b/drivers/i2c/busses/i2c-bcm-iproc.c @@ -26,6 +26,7 @@ #define CFG_RESET_SHIFT 31 #define CFG_EN_SHIFT 30 #define CFG_SLAVE_ADDR_0_SHIFT 28 +#define CFG_BIT_BANG_SHIFT 29 #define CFG_M_RETRY_CNT_SHIFT 16 #define CFG_M_RETRY_CNT_MASK 0x0f @@ -66,6 +67,12 @@ #define S_FIFO_RX_THLD_SHIFT 8 #define S_FIFO_RX_THLD_MASK 0x3f +#define M_BB_CTRL_OFFSET 0x14 +#define M_BB_SMBCLK_IN 31 +#define M_BB_SMBCLK_OUT_EN 30 +#define M_BB_SMBDAT_IN 29 +#define M_BB_SMBDAT_OUT_EN 28 + #define M_CMD_OFFSET 0x30 #define M_CMD_START_BUSY_SHIFT 31 #define M_CMD_STATUS_SHIFT 25 @@ -713,6 +720,112 @@ static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev *iproc_i2c, iproc_i2c_wr_reg(iproc_i2c, CFG_OFFSET, val); } +static void bcm_iproc_i2c_reset(struct bcm_iproc_i2c_dev *iproc_i2c) +{ + u32 tmp; + + tmp = readl(iproc_i2c->base + CFG_OFFSET); + tmp |= BIT(CFG_RESET_SHIFT); + writel(tmp, iproc_i2c->base + CFG_OFFSET); + udelay(100); + +} + +static void bcm_iproc_i2c_prepare_recovery(struct i2c_adapter *adap) +{ + struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adap); + u32 tmp; + + dev_dbg(iproc_i2c->device, "Prepare recovery\n"); + + /* Disable interrupts */ + writel(0, iproc_i2c->base + IE_OFFSET); + readl(iproc_i2c->base + IE_OFFSET); + synchronize_irq(iproc_i2c->irq); + + bcm_iproc_i2c_reset(iproc_i2c); + + /* Switch to bit-bang mode */ + tmp = readl(iproc_i2c->base + CFG_OFFSET); + tmp |= BIT(CFG_BIT_BANG_SHIFT); + writel(tmp, iproc_i2c->base + CFG_OFFSET); +} + +static void bcm_iproc_i2c_unprepare_recovery(struct i2c_adapter *adap) +{ + struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adap); + u32 tmp; + + /* Switch to normal mode */ + tmp = readl(iproc_i2c->base + CFG_OFFSET); + tmp &= ~BIT(CFG_BIT_BANG_SHIFT); + writel(tmp, iproc_i2c->base + CFG_OFFSET); + udelay(100); + + bcm_iproc_i2c_init(iproc_i2c); + bcm_iproc_i2c_enable_disable(iproc_i2c, true); + + dev_dbg(iproc_i2c->device, "Recovery complete\n"); +} + +static int bcm_iproc_i2c_get_scl(struct i2c_adapter *adap) +{ + struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adap); + u32 tmp; + + tmp = readl(iproc_i2c->base + M_BB_CTRL_OFFSET); + + return !!(tmp & BIT(M_BB_SMBCLK_IN)); +} + +static void bcm_iproc_i2c_set_scl(struct i2c_adapter *adap, int val) +{ + struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adap); + u32 tmp; + + tmp = readl(iproc_i2c->base + M_BB_CTRL_OFFSET); + if (val) + tmp |= BIT(M_BB_SMBCLK_OUT_EN); + else + tmp &= ~BIT(M_BB_SMBCLK_OUT_EN); + + writel(tmp, iproc_i2c->base + M_BB_CTRL_OFFSET); +} + +static void bcm_iproc_i2c_set_sda(struct i2c_adapter *adap, int val) +{ + struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adap); + u32 tmp; + + tmp = readl(iproc_i2c->base + M_BB_CTRL_OFFSET); + if (val) + tmp |= BIT(M_BB_SMBDAT_OUT_EN); + else + tmp &= ~BIT(M_BB_SMBDAT_OUT_EN); + + writel(tmp, iproc_i2c->base + M_BB_CTRL_OFFSET); +} + +static int bcm_iproc_i2c_get_sda(struct i2c_adapter *adap) +{ + struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adap); + u32 tmp; + + tmp = readl(iproc_i2c->base + M_BB_CTRL_OFFSET); + + return !!(tmp & BIT(M_BB_SMBDAT_IN)); +} + +static struct i2c_bus_recovery_info bcm_iproc_recovery_info = { + .recover_bus = i2c_generic_scl_recovery, + .prepare_recovery = bcm_iproc_i2c_prepare_recovery, + .unprepare_recovery = bcm_iproc_i2c_unprepare_recovery, + .set_scl = bcm_iproc_i2c_set_scl, + .get_scl = bcm_iproc_i2c_get_scl, + .set_sda = bcm_iproc_i2c_set_sda, + .get_sda = bcm_iproc_i2c_get_sda, +}; + static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev *iproc_i2c, struct i2c_msg *msg) { @@ -839,6 +952,7 @@ static int bcm_iproc_i2c_xfer_internal(struct bcm_iproc_i2c_dev *iproc_i2c, if (!!(iproc_i2c_rd_reg(iproc_i2c, M_CMD_OFFSET) & BIT(M_CMD_START_BUSY_SHIFT))) { dev_warn(iproc_i2c->device, "bus is busy\n"); + i2c_recover_bus(&iproc_i2c->adapter); return -EBUSY; } @@ -1111,6 +1225,7 @@ static int bcm_iproc_i2c_probe(struct platform_device *pdev) of_node_full_name(iproc_i2c->device->of_node)); adap->algo = &bcm_iproc_algo; adap->quirks = &bcm_iproc_i2c_quirks; + adap->bus_recovery_info = &bcm_iproc_recovery_info; adap->dev.parent = &pdev->dev; adap->dev.of_node = pdev->dev.of_node;