Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2204123rdb; Thu, 21 Sep 2023 11:22:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGgAynxBsp3N/eF63hMMBPQGEub3gQFtVjaDCfrb3dQjjvUKfJloLTKpa27VpVSt+ZItoO/ X-Received: by 2002:a17:902:dac5:b0:1c4:3ae6:381a with SMTP id q5-20020a170902dac500b001c43ae6381amr6517492plx.0.1695320539498; Thu, 21 Sep 2023 11:22:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695320539; cv=none; d=google.com; s=arc-20160816; b=p+iRnXNPLDZgsBUiKZSbod6GlEY4SyGWtoVIdAzeQJR2yN575iJEz4/KCbauuqs85O cY0ygH3clFhIYkOO58Nr5fWXLerqEhfNULiHcEj1tO75l8x5stFqCCYAftSKiJhccZIZ 1DVr+1C5neBgt4s4wWa7nWaiYOGNLR1j/OixVjh3RNwTuWhF1pMVTv2ccXMI0L10hXvb Qa4wMJiWL1kjKvb4ZplvPlIGKvQxTaxpM5+WFQB0zGEa4lNodOECUqIG8BmtszTXByrC pQTjOYdMWwrPizHlgk/AE4/koQsRIUE8P9rB3myOIftSP79aws14EmLNcKd26o8xbrTC ha+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=s0wKKoiqtlDTcKh8N3yQqYNfJRRdiEJl9z6GjDPSwHU=; fh=ZqJ3xrq0DGyvWtPBQo5VWOM7gaftcxyjM72UWd4YoC8=; b=ojY1VjH6pAnffIq5WH9wgc3YTGmcx4tp6FB1pBS72P/UWyofpJ4PGtRtsv99QDCNFJ euxquvzlO0Or1z28Xy2N/EQXtcY85kL7xccf/Q2CLZXKiPuvIfPLY1ZE37mC/NxZFRxS ZIu75C9wgMm9CNrW3RDkojUM/imd3SrJ4zqhskG7fNQgIoyL8GzqyzF5UmV3B+RJdzAS r/4uZyFWP1ZcnEyX9FNea0Gkr8ZJBYSDdxlw8mz0MS6qXSisaUQCaPh+AN1VgpCUfwY+ KC+ckqA1KZeZiM3Qh+O3OfXnHdPDS/Ueft8DV9koJSNmABDv/lxPFFIDvS80atKda1yq KYow== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p15-20020a170902ebcf00b001c0727658c3si1816871plg.259.2023.09.21.11.22.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 11:22:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E679182A38E2; Thu, 21 Sep 2023 11:12:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230246AbjIUSLw (ORCPT + 99 others); Thu, 21 Sep 2023 14:11:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbjIUSLP (ORCPT ); Thu, 21 Sep 2023 14:11:15 -0400 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7713573D5; Thu, 21 Sep 2023 10:18:34 -0700 (PDT) Received: from SHSQR01.spreadtrum.com (localhost [127.0.0.2] (may be forged)) by SHSQR01.spreadtrum.com with ESMTP id 38L8vFBT047648; Thu, 21 Sep 2023 16:57:15 +0800 (+08) (envelope-from Huangzheng.Lai@unisoc.com) Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 38L8tVw8040607; Thu, 21 Sep 2023 16:55:31 +0800 (+08) (envelope-from Huangzheng.Lai@unisoc.com) Received: from SHDLP.spreadtrum.com (shmbx04.spreadtrum.com [10.0.1.214]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4Rrq0B6YBSz2SZtbS; Thu, 21 Sep 2023 16:52:10 +0800 (CST) Received: from xm9614pcu.spreadtrum.com (10.13.2.29) by shmbx04.spreadtrum.com (10.0.1.214) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Thu, 21 Sep 2023 16:55:29 +0800 From: Huangzheng Lai To: Andi Shyti CC: Orson Zhai , Baolin Wang , Chunyan Zhang , , , huangzheng lai , Huangzheng Lai , Xiongpeng Wu Subject: [PATCH V2 3/7] i2c: sprd: Use global variables to record I2C ack/nack status instead of local variables Date: Thu, 21 Sep 2023 16:54:53 +0800 Message-ID: <20230921085457.32446-4-Huangzheng.Lai@unisoc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230921085457.32446-1-Huangzheng.Lai@unisoc.com> References: <20230921085457.32446-1-Huangzheng.Lai@unisoc.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.13.2.29] X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To shmbx04.spreadtrum.com (10.0.1.214) X-MAIL: SHSQR01.spreadtrum.com 38L8tVw8040607 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 21 Sep 2023 11:12:26 -0700 (PDT) We found that when the interrupt bit of the I2C controller is cleared, the ack/nack bit is also cleared at the same time. After clearing the interrupt bit in sprd_i2c_isr(), incorrect ack/nack information will be obtained in sprd_i2c_isr_thread(), resulting in incorrect communication when nack cannot be recognized. To solve this problem, we used a global variable to record ack/nack information before clearing the interrupt bit instead of a local variable. Fixes: 8b9ec0719834 ("i2c: Add Spreadtrum I2C controller driver") Cc: # v4.14+ Signed-off-by: Huangzheng Lai --- drivers/i2c/busses/i2c-sprd.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/i2c/busses/i2c-sprd.c b/drivers/i2c/busses/i2c-sprd.c index aa602958d4fd..dec627ef408c 100644 --- a/drivers/i2c/busses/i2c-sprd.c +++ b/drivers/i2c/busses/i2c-sprd.c @@ -85,6 +85,7 @@ struct sprd_i2c { struct clk *clk; u32 src_clk; u32 bus_freq; + bool ack_flag; struct completion complete; struct reset_control *rst; u8 *buf; @@ -119,6 +120,7 @@ static void sprd_i2c_clear_ack(struct sprd_i2c *i2c_dev) { u32 tmp = readl(i2c_dev->base + I2C_STATUS); + i2c_dev->ack_flag = 0; writel(tmp & ~I2C_RX_ACK, i2c_dev->base + I2C_STATUS); } @@ -393,7 +395,6 @@ static irqreturn_t sprd_i2c_isr_thread(int irq, void *dev_id) { struct sprd_i2c *i2c_dev = dev_id; struct i2c_msg *msg = i2c_dev->msg; - bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK); u32 i2c_tran; if (msg->flags & I2C_M_RD) @@ -409,7 +410,7 @@ static irqreturn_t sprd_i2c_isr_thread(int irq, void *dev_id) * For reading data, ack is always true, if i2c_tran is not 0 which * means we still need to contine to read data from slave. */ - if (i2c_tran && ack) { + if (i2c_tran && i2c_dev->ack_flag) { sprd_i2c_data_transfer(i2c_dev); return IRQ_HANDLED; } @@ -420,7 +421,7 @@ static irqreturn_t sprd_i2c_isr_thread(int irq, void *dev_id) * If we did not get one ACK from slave when writing data, we should * return -EIO to notify users. */ - if (!ack) + if (!i2c_dev->ack_flag) i2c_dev->err = -EIO; else if (msg->flags & I2C_M_RD && i2c_dev->count) sprd_i2c_read_bytes(i2c_dev, i2c_dev->buf, i2c_dev->count); @@ -437,7 +438,6 @@ static irqreturn_t sprd_i2c_isr(int irq, void *dev_id) { struct sprd_i2c *i2c_dev = dev_id; struct i2c_msg *msg = i2c_dev->msg; - bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK); u32 i2c_tran; if (msg->flags & I2C_M_RD) @@ -456,7 +456,8 @@ static irqreturn_t sprd_i2c_isr(int irq, void *dev_id) * means we can read all data in one time, then we can finish this * transmission too. */ - if (!i2c_tran || !ack) { + i2c_dev->ack_flag = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK); + if (!i2c_tran || !i2c_dev->ack_flag) { sprd_i2c_clear_start(i2c_dev); sprd_i2c_clear_irq(i2c_dev); } -- 2.17.1