From patchwork Thu Apr 13 09:37:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Schiffer X-Patchwork-Id: 673510 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C569C77B61 for ; Thu, 13 Apr 2023 09:42:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229580AbjDMJl7 (ORCPT ); Thu, 13 Apr 2023 05:41:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229498AbjDMJl7 (ORCPT ); Thu, 13 Apr 2023 05:41:59 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B5C610C2; Thu, 13 Apr 2023 02:41:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1681378917; x=1712914917; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=GTkgi9DEYv2y6KPQY0Pk1iavkXW4SL1GDX6CwlELQMI=; b=lKpAdEMfw4ASWTAmPIb+9+AQTa6spusmwB8l1K6/TNge1k3+cREXarYV Ptg/6JWyAU4Vc3fKIrrrK4Pku1j2nDSUlRoE5ep89LJL3rYwR9wsL3C5u 9w2I00tezldMV4CVP1RaHgmdZtEGp6QXtFlgougQCaPKISla9kJyxN9C3 qgwiYNSvNMISuhudh7A48UZco3FDf0glRoeOnwFQgrYftWNCuWlRSMHf+ MtVzNjiu2SoShNHFex2afvvXw40nRfOr14g2ozst1AucbwQ6Inz0EbQyN vvwoP73PW7njoe0EMpR81g0x8HEtSH0wexKgHG0l6X9I4ttAQM6zEb3jJ g==; X-IronPort-AV: E=Sophos;i="5.98,341,1673910000"; d="scan'208";a="30301482" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 13 Apr 2023 11:41:55 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Thu, 13 Apr 2023 11:41:55 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Thu, 13 Apr 2023 11:41:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1681378915; x=1712914915; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=GTkgi9DEYv2y6KPQY0Pk1iavkXW4SL1GDX6CwlELQMI=; b=V/bPdRtF8bE9tV1RLYx7bexUigLICd3rCt9QZCDjwaN28heBW8M9vnex BfZ7qalF4ryANR9dySTLR0OcpwIOfK2CUZJb0ThIJhOiWrH0+pda5qzKq QiXdpk8BZwNZg0MIOhm8Xdm/8ag/sPuPH6ky9BlF7IaY7nHle8+VkxJUt 1NNsqCORRVlrT9WweL8ALZF+fs9MLguudRJwVjOHGfRO1ghGHnXtQaerF jgjRJyUkM0X3JuxIvZBwO1OwmxEj1cHMI4mXGcjSO5D5kz+kTByd+S/Z5 qlGPsUIiFrV5q1h8c8ZgaQ5zSFBykiXViR5Il72CAYU5FUOyQDgeV40OJ A==; X-IronPort-AV: E=Sophos;i="5.98,341,1673910000"; d="scan'208";a="30301481" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 13 Apr 2023 11:41:54 +0200 Received: from localhost.localdomain (SCHIFFERM-M2.tq-net.de [10.121.49.20]) by vtuxmail01.tq-net.de (Postfix) with ESMTPA id 6579B280056; Thu, 13 Apr 2023 11:41:54 +0200 (CEST) From: Matthias Schiffer To: Peter Korsgaard , Andrew Lunn Cc: Federico Vaga , Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, linux@ew.tq-group.com, Gregor Herburger , Matthias Schiffer Subject: [PATCH v2] i2c: ocores: generate stop condition after timeout in polling mode Date: Thu, 13 Apr 2023 11:37:37 +0200 Message-Id: <20230413093737.15303-1-matthias.schiffer@ew.tq-group.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org From: Gregor Herburger In polling mode, no stop condition is generated after a timeout. This causes SCL to remain low and thereby block the bus. If this happens during a transfer it can cause slaves to misinterpret the subsequent transfer and return wrong values. To solve this, pass the ETIMEDOUT error up from ocores_process_polling() instead of setting STATE_ERROR directly. The caller is adjusted to call ocores_process_timeout() on error both in polling and in IRQ mode, which will set STATE_ERROR and generate a stop condition. Fixes: 69c8c0c0efa8 ("i2c: ocores: add polling interface") Signed-off-by: Gregor Herburger Signed-off-by: Matthias Schiffer --- v2: style improvements based on feedback from Federico and Andrew. I went with a slightly different solution than Andrew suggested to avoid using the ret variable for two different kinds of returns. drivers/i2c/busses/i2c-ocores.c | 35 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c index a0af027db04c1..2e575856c5cd5 100644 --- a/drivers/i2c/busses/i2c-ocores.c +++ b/drivers/i2c/busses/i2c-ocores.c @@ -342,18 +342,18 @@ static int ocores_poll_wait(struct ocores_i2c *i2c) * ocores_isr(), we just add our polling code around it. * * It can run in atomic context + * + * Return: 0 on success, -ETIMEDOUT on timeout */ -static void ocores_process_polling(struct ocores_i2c *i2c) +static int ocores_process_polling(struct ocores_i2c *i2c) { - while (1) { - irqreturn_t ret; - int err; + irqreturn_t ret; + int err = 0; + while (1) { err = ocores_poll_wait(i2c); - if (err) { - i2c->state = STATE_ERROR; + if (err) break; /* timeout */ - } ret = ocores_isr(-1, i2c); if (ret == IRQ_NONE) @@ -364,13 +364,15 @@ static void ocores_process_polling(struct ocores_i2c *i2c) break; } } + + return err; } static int ocores_xfer_core(struct ocores_i2c *i2c, struct i2c_msg *msgs, int num, bool polling) { - int ret; + int ret = 0; u8 ctrl; ctrl = oc_getreg(i2c, OCI2C_CONTROL); @@ -388,15 +390,16 @@ static int ocores_xfer_core(struct ocores_i2c *i2c, oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); if (polling) { - ocores_process_polling(i2c); + ret = ocores_process_polling(i2c); } else { - ret = wait_event_timeout(i2c->wait, - (i2c->state == STATE_ERROR) || - (i2c->state == STATE_DONE), HZ); - if (ret == 0) { - ocores_process_timeout(i2c); - return -ETIMEDOUT; - } + if (wait_event_timeout(i2c->wait, + (i2c->state == STATE_ERROR) || + (i2c->state == STATE_DONE), HZ) == 0) + ret = -ETIMEDOUT; + } + if (ret) { + ocores_process_timeout(i2c); + return ret; } return (i2c->state == STATE_DONE) ? num : -EIO;