From patchwork Mon Oct 14 13:15:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13835252 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 041CBD1812A for ; Mon, 14 Oct 2024 15:34:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=FUH25x79k8EHQsto7o4HWXcz+cLM47kx/MicJivdg7g=; b=z0qDRWV8fD2RpoEcdrUk5P0Nz0 K2sEoJcJFb78E3gABsLyIv+MWD9084KiFRbDSs+n8w06ZbFlbuZGLi/Ds9wMJFQ2lqBTf4YjqKzGV oL0BBkoG6pU+AGgzyJVkPsMqTa5m5J+VRyvq+82jyA2QPfFluVmeyOU/H/WIwi6FQTBcMgbBD4OQ9 mP/dVMzCojD4zRWbrxUi57BnZN24d8C3ENhQZHrzT+iQT0ID8MylDWCjAhG099F+X6wJF60pitlus uX5ujHc/9MOf/Io/FScIk1JmV8Fxr+ryB724CDXjf+Cp0xHhxj3WTjlsjVopFOpz50nr50COdGUcG ZqJsr6+A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t0N5m-00000005fdd-1D7S; Mon, 14 Oct 2024 15:34:42 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t0KwK-00000005EJ7-2xUJ for linux-arm-kernel@bombadil.infradead.org; Mon, 14 Oct 2024 13:16:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=FUH25x79k8EHQsto7o4HWXcz+cLM47kx/MicJivdg7g=; b=RKDg/aoag7egcruEZvcCQQkd8t JKElTS8Xo8/zi5BxWv9Sfs6ROyjm7iouXtJDbEHmjMKFKk0LVI64wFCStLsg3y0oq0J2P9ACxqIiA ElrRNGsaeQF4qSUFSWoL4PP+AKlUjTaDtaaGG7muWPkfIEgWjSj31UbZaYauGICWFf+PyGMRTz1fw xhcrCRzZVQOuoLhGh+l/eT8Ja60KCcjYjM6wVdkVTCws2qYir9lYK9hVQAlvY7i40nKoRXuuCHfrg MEQjy5LxWGDBHLujMKkj3LvTopVv+retkmrmq8DDsEfeyYehvfxlEUBpyGZoPpZntLpLLyb7AVT+G edgdiHJA==; Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t0KwH-00000006LUl-1mnj for linux-arm-kernel@lists.infradead.org; Mon, 14 Oct 2024 13:16:47 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-431160cdbd0so26772595e9.1 for ; Mon, 14 Oct 2024 06:16:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728911802; x=1729516602; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FUH25x79k8EHQsto7o4HWXcz+cLM47kx/MicJivdg7g=; b=itTCIhUoWAyN0pot1jK0nYUczM632ExWzpywC0hxrl/eDCL1Pgy0VfyHSIyDm5DsU7 mIbSwHkPqiB2NCHq21xaPReUxHq9CjCwIsRGq04R6ZZrO0bk7y+ha7wppzSJ4Vv6G5Qt O9PGU9dK79rP1siJO7sWxfUsaeDgPoni/pxVginXBagQb/Gu2j1plwRreeXvBt/u7mil 3/ZwBFPQPeFCXEuU/pHq1fEN9pPIrAZrjT3Cl2mgosf7GorvdxqUxEd+olPzzqSljdrw rbwW5bo88X1LEm6vdxLGprOckLyQg1KW0Qt2/+aAyLBpBIeBOYO3JUfz4I2eugN4T1jh IS5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728911802; x=1729516602; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FUH25x79k8EHQsto7o4HWXcz+cLM47kx/MicJivdg7g=; b=AFM6P1SJh9iEbruFUiTNlcnw2YCOvFtOZim81YxFUjAPLi021RT9940FFTR6iWoKma PGpZVB0ZiaKKXT/167KwbYXE7txoqGJ1hQq4+pW43rqxRb2uzlI/J3z9CMKshpfkfRyf AUAry/mH7RDIXPxJTYJnXX2KlZDFOYc1KGVoFkf4jdfa/rsb1qQ8H7lPEzxa/o2NOTIm D6ploJEVxc6F4GuGpe4s9LijL+YpW8waTrIJ4lCiWP17XYqEo4OeHMBkZzCp06jrYhMJ WtRE0TE6NisiOMAWncKSTKj+CQVK0HB06+bd3+gLe3mqJjNm06cM3B83yNxvkmtLsWOB eDFA== X-Forwarded-Encrypted: i=1; AJvYcCWzmo0+4/e06Ff6fxZM6vjtO/mcUHpyVXv0h7/ngobeEmFFRMfvvBcUMS6ZL/WMY8menyHVSI5w5mwky4uoLS8k@lists.infradead.org X-Gm-Message-State: AOJu0YwoHVfhuDEsr0YRjFefhja+pcvGag87QSEFj29RIWxvwK+WWXwG 4rBY65Z/DqjNNtVL5E8uRXjrR2PgGa9TsqlupczYHG8uVQ/MvYgc X-Google-Smtp-Source: AGHT+IHFCOhkWsbSszMYyapdJCiM6wV55PCzOwhLCTtrrsuvQ5Od0oVlQ1v7sPpCFmpXOlc0aIrgWA== X-Received: by 2002:a05:600c:3106:b0:431:136b:8bef with SMTP id 5b1f17b1804b1-43115aa52a6mr133830585e9.7.1728911802065; Mon, 14 Oct 2024 06:16:42 -0700 (PDT) Received: from eichest-laptop.lan ([2a02:168:af72:0:d130:f8ef:c6c1:55ee]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-430d748d42fsm153420425e9.43.2024.10.14.06.16.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 06:16:41 -0700 (PDT) From: Stefan Eichenberger To: o.rempel@pengutronix.de, kernel@pengutronix.de, andi.shyti@kernel.org, shawnguo@kernel.org, s.hauer@pengutronix.de, festevam@gmail.com, francesco.dolcini@toradex.com, l.stach@pengutronix.de, arnd@arndb.de, Frank.Li@nxp.com Cc: linux-i2c@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Stefan Eichenberger Subject: [PATCH v5 1/3] i2c: imx: do not poll for bus busy in single master mode Date: Mon, 14 Oct 2024 15:15:12 +0200 Message-ID: <20241014131635.205489-2-eichest@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241014131635.205489-1-eichest@gmail.com> References: <20241014131635.205489-1-eichest@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241014_141645_629766_C5CA8197 X-CRM114-Status: GOOD ( 17.33 ) 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: Stefan Eichenberger According to the i.MX8M Mini reference manual chapter "16.1.4.2 Generation of Start" it is only necessary to poll for bus busy and arbitration lost in multi master mode. This helps to avoid rescheduling while the i2c bus is busy and avoids SMBus devices to timeout. For backward compatibility, the single-master property needs to be explicitly set to disable the bus busy polling. Signed-off-by: Stefan Eichenberger Reviewed-by: Frank Li Acked-by: Oleksij Rempel --- @Frank and Oleksij: I kept your tags even though I made multi-master the new default. Let me know if you have any objections. drivers/i2c/busses/i2c-imx.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index 98539313cbc97..d697abd2459d4 100644 --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -216,6 +216,8 @@ struct imx_i2c_struct { struct i2c_client *slave; enum i2c_slave_event last_slave_event; + bool multi_master; + /* For checking slave events. */ spinlock_t slave_lock; struct hrtimer slave_timer; @@ -481,6 +483,9 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool a unsigned long orig_jiffies = jiffies; unsigned int temp; + if (!i2c_imx->multi_master) + return 0; + while (1) { temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); @@ -540,8 +545,8 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx, bool atomic) return -ETIMEDOUT; } - /* check for arbitration lost */ - if (i2c_imx->i2csr & I2SR_IAL) { + /* In multi-master mode check for arbitration lost */ + if (i2c_imx->multi_master && (i2c_imx->i2csr & I2SR_IAL)) { dev_dbg(&i2c_imx->adapter.dev, "<%s> Arbitration lost\n", __func__); i2c_imx_clear_irq(i2c_imx, I2SR_IAL); @@ -1468,6 +1473,12 @@ static int i2c_imx_probe(struct platform_device *pdev) goto rpm_disable; } + /* + * We use the single-master property for backward compatibility. + * By default multi master mode is enabled. + */ + i2c_imx->multi_master = !of_property_read_bool(pdev->dev.of_node, "single-master"); + /* Set up clock divider */ i2c_imx->bitrate = I2C_MAX_STANDARD_MODE_FREQ; ret = of_property_read_u32(pdev->dev.of_node, From patchwork Mon Oct 14 13:15:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13835099 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id EC46DD16273 for ; Mon, 14 Oct 2024 14:09:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=u1VfZCHujjXPaAUkUEAFh3cr3loh7MTJkvdgGiSjmCU=; b=AC6RE0xSBZEnBk3YWx4saLO7Yi TodpM0FsmYtEFlJWl7z1934uyw09dX5MHqfdv+iaiOvdTGBZ7wk877WtuwPhJAkiPnYrDGhHIWwWc /noddlJK42YgVmB7fKBswdWJU33glgl/81bzHBswYKehYhYF81qmjvaSOxKxV64YkVPUD5mTxfuRy 9RNTXNGFuAQjEEkasjIKCW0mUGGofkSvjU1L+4bNUYU2iBpKPYND4e1tVibLBl0llFwLayL69JPul PG48H2kOYBIJMVcTNzV+gdlMbHDM6B8VBHb8dZVVTXAq4Ukw4oxq76X7I+9P2HAB3Us4RfQiS593Y A/8m77eQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t0LlI-00000005QJS-0lEA; Mon, 14 Oct 2024 14:09:28 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t0KwH-00000005EIJ-1Nio for linux-arm-kernel@lists.infradead.org; Mon, 14 Oct 2024 13:18:45 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-43124d1ca5dso20317105e9.2 for ; Mon, 14 Oct 2024 06:16:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728911803; x=1729516603; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=u1VfZCHujjXPaAUkUEAFh3cr3loh7MTJkvdgGiSjmCU=; b=fUUbIvZ1PXe2NQTQBycmf4RG3rbZ4u8IcP6sN9k7el/vBtzJ9ZzCfOCzaISVR39tCG Ehi4Ve88A0CO9rA0IESCItn5iLd1M/N8OXM3HLqjD1/qDpbsdp1aK49L9Qt2sNe+xuJv BndoolVBMXKtOG/Cad2yLw7FXGoXinPXmR6zM73Pr0k4Eb4RnuL0CkUcte9BX9HP+Aco KIAc91OIaF6fxzY+kRqFNuEw4VPppD+KRJzu6lAYYLiVKPth6ONwONO5HL6t7mRvz6oF ImDrll5c3LUTWbrC7uOR6DWwEF1RPqWk2OnOtjXKAFJo4qCEiy7JjJkn0ktwuRiuzwGx +E0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728911803; x=1729516603; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u1VfZCHujjXPaAUkUEAFh3cr3loh7MTJkvdgGiSjmCU=; b=jmwK5OO2+Q5CFRaGI/ByZOvMqH5GyKDtI/Bs8fyvOYtRJZ1IZAtYtgi+UOZWHqjlg6 /lMiUfJc2k3UfkQfH5/Q4h6CoxVNgvv3CfuMD0ppLgsP451Z0Y07tBaI1A8n5H0WpjGz lsToft0/Kxlham3GlNmeO/Y6F08JPPCsvmNgVbIQfmYbEvkYbSN0sOUoLB8hQV2E6Zb9 djfplZSAHtPl6pX/nov9IPoepbJqZmgbb9V8/SQo917WBkXSlIx/SoJCb677Ja7ueLO0 AZiKVlLY4q7hVbQP+nobHFM2Vb2IkOUQJRmAOfRYSHjAytcSCoLrm8IKeO9CuxtuIG6u loAA== X-Forwarded-Encrypted: i=1; AJvYcCUw8NkssiSroWt/pTmHVeqRwPh52UTiWQNwTaY/yNKT56lkkXOZHahtS0w/kZALap2Zx87MyutN9Cx3Rn9TINkP@lists.infradead.org X-Gm-Message-State: AOJu0Yw9fjvl0Q0ui8HH/PjtI9k10ccWFlBmoip0WLrmM5wPZQ00LASW 1E3kGevUJ1L/eHczwZcaYTV6tD+c6CkjTb7RVBqRnYKdoR2/cs/q X-Google-Smtp-Source: AGHT+IFkf74BeYvy94wG3fIbGaiLbEYLJQrlubWzmDJ20oqKEKRdVRWn+QWLGFtlNr7tPgC3We3JUg== X-Received: by 2002:a05:600c:83c8:b0:42c:bfd6:9d4d with SMTP id 5b1f17b1804b1-4311dea4637mr89021395e9.2.1728911803218; Mon, 14 Oct 2024 06:16:43 -0700 (PDT) Received: from eichest-laptop.lan ([2a02:168:af72:0:d130:f8ef:c6c1:55ee]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-430d748d42fsm153420425e9.43.2024.10.14.06.16.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 06:16:42 -0700 (PDT) From: Stefan Eichenberger To: o.rempel@pengutronix.de, kernel@pengutronix.de, andi.shyti@kernel.org, shawnguo@kernel.org, s.hauer@pengutronix.de, festevam@gmail.com, francesco.dolcini@toradex.com, l.stach@pengutronix.de, arnd@arndb.de, Frank.Li@nxp.com Cc: linux-i2c@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Stefan Eichenberger Subject: [PATCH v5 2/3] i2c: imx: separate atomic, dma and non-dma use case Date: Mon, 14 Oct 2024 15:15:13 +0200 Message-ID: <20241014131635.205489-3-eichest@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241014131635.205489-1-eichest@gmail.com> References: <20241014131635.205489-1-eichest@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241014_061645_401560_A835308D X-CRM114-Status: GOOD ( 20.25 ) 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: Stefan Eichenberger Separate the atomic, dma and non-dma use case as a preparation step for moving the non-dma use case to the isr to avoid rescheduling while a transfer is in progress. Signed-off-by: Stefan Eichenberger Reviewed-by: Frank Li Acked-by: Oleksij Rempel --- drivers/i2c/busses/i2c-imx.c | 107 +++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 37 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index d697abd2459d4..e0821332c439a 100644 --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -1011,6 +1011,43 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx, return i2c_imx_acked(i2c_imx); } +static int i2c_imx_prepare_read(struct imx_i2c_struct *i2c_imx, + struct i2c_msg *msgs, bool atomic, + bool use_dma) +{ + int result; + unsigned int temp = 0; + + /* write slave address */ + imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); + result = i2c_imx_trx_complete(i2c_imx, atomic); + if (result) + return result; + result = i2c_imx_acked(i2c_imx); + if (result) + return result; + + dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__); + + /* setup bus to read data */ + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); + temp &= ~I2CR_MTX; + + /* + * Reset the I2CR_TXAK flag initially for SMBus block read since the + * length is unknown + */ + if (msgs->len - 1) + temp &= ~I2CR_TXAK; + if (use_dma) + temp |= I2CR_DMAEN; + + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); + imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */ + + return 0; +} + static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg) { @@ -1021,6 +1058,11 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx, struct imx_i2c_dma *dma = i2c_imx->dma; struct device *dev = &i2c_imx->adapter.dev; + result = i2c_imx_prepare_read(i2c_imx, msgs, false, true); + if (result) + return result; + + dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__); dma->chan_using = dma->chan_rx; dma->dma_transfer_dir = DMA_DEV_TO_MEM; @@ -1131,50 +1173,24 @@ static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, return 0; } +static int i2c_imx_atomic_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) +{ + return i2c_imx_write(i2c_imx, msgs, true); +} + static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg, bool atomic) { int i, result; unsigned int temp; int block_data = msgs->flags & I2C_M_RECV_LEN; - int use_dma = i2c_imx->dma && msgs->flags & I2C_M_DMA_SAFE && - msgs->len >= DMA_THRESHOLD && !block_data; - dev_dbg(&i2c_imx->adapter.dev, - "<%s> write slave address: addr=0x%x\n", - __func__, i2c_8bit_addr_from_msg(msgs)); - - /* write slave address */ - imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); - result = i2c_imx_trx_complete(i2c_imx, atomic); + result = i2c_imx_prepare_read(i2c_imx, msgs, atomic, false); if (result) return result; - result = i2c_imx_acked(i2c_imx); - if (result) - return result; - - dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__); - - /* setup bus to read data */ - temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); - temp &= ~I2CR_MTX; - - /* - * Reset the I2CR_TXAK flag initially for SMBus block read since the - * length is unknown - */ - if ((msgs->len - 1) || block_data) - temp &= ~I2CR_TXAK; - if (use_dma) - temp |= I2CR_DMAEN; - imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); - imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */ dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__); - if (use_dma) - return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg); - /* read data */ for (i = 0; i < msgs->len; i++) { u8 len = 0; @@ -1241,6 +1257,12 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, return 0; } +static int i2c_imx_atomic_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, + bool is_lastmsg) +{ + return i2c_imx_read(i2c_imx, msgs, is_lastmsg, true); +} + static int i2c_imx_xfer_common(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num, bool atomic) { @@ -1248,6 +1270,7 @@ static int i2c_imx_xfer_common(struct i2c_adapter *adapter, int result; bool is_lastmsg = false; struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); + int use_dma = 0; /* Start I2C transfer */ result = i2c_imx_start(i2c_imx, atomic); @@ -1300,15 +1323,25 @@ static int i2c_imx_xfer_common(struct i2c_adapter *adapter, (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0), (temp & I2SR_RXAK ? 1 : 0)); #endif + + use_dma = i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD && + msgs[i].flags & I2C_M_DMA_SAFE; if (msgs[i].flags & I2C_M_RD) { - result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg, atomic); + int block_data = msgs->flags & I2C_M_RECV_LEN; + + if (atomic) + result = i2c_imx_atomic_read(i2c_imx, &msgs[i], is_lastmsg); + else if (use_dma && !block_data) + result = i2c_imx_dma_read(i2c_imx, &msgs[i], is_lastmsg); + else + result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg, false); } else { - if (!atomic && - i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD && - msgs[i].flags & I2C_M_DMA_SAFE) + if (atomic) + result = i2c_imx_atomic_write(i2c_imx, &msgs[i]); + else if (use_dma) result = i2c_imx_dma_write(i2c_imx, &msgs[i]); else - result = i2c_imx_write(i2c_imx, &msgs[i], atomic); + result = i2c_imx_write(i2c_imx, &msgs[i], false); } if (result) goto fail0; From patchwork Mon Oct 14 13:15:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13835100 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 6188FD16273 for ; Mon, 14 Oct 2024 14:11:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=t8AezVwPrFjuUUf+MrjbX+IGpei906KShJIFKpwBFic=; b=caBeKg5tnGXRTUUySkqJc6xNBa kFm3Xky5E4TkcvBfVL5T/yXRLwu6rF8hhr6cMWh/6opGALzKR/gcj/Dv3WC4rO2WX/ilq7MIoQLa2 JnRna0Fs1Ko9QEVY1Mrn840oBwh+KW/F/j61xvlEF2sXi04bqzC1k33HVP/IFFqOMU/WsZYy6JlIz 3/HYNDvnrRj2piEbx8dySpmDVfU9dlQsOz+ACXlvuoUxwpKdhXS1pK6FOk7kD8gBokfMsGA0iEsOq G8HLuA6cvJCC1mXEe7+xpxezob7cQzp2wZQQfWiVmc3Xa0dowfiS3pB4QF08Bim6sbfHX7+qq0PfF L9qPx5RA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t0Lmf-00000005QTy-3vZW; Mon, 14 Oct 2024 14:10:53 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t0KwI-00000005EIR-2Jn3 for linux-arm-kernel@lists.infradead.org; Mon, 14 Oct 2024 13:18:46 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-37d58377339so3073889f8f.1 for ; Mon, 14 Oct 2024 06:16:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728911805; x=1729516605; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=t8AezVwPrFjuUUf+MrjbX+IGpei906KShJIFKpwBFic=; b=ANsGUXfIZCdZlVS0anqpgLn5kKUducGEoD3EHVOvhHtKgQmmi2KVv+mYzdXP69nhrr vBQcyh3OBae7tmyZu6FJZSGsyFkI25iLXVf/4dWHdZySYpXppJl3IzsboOJrLPt/Bsls MO6AVy6tlEnN9TU46yPwqcutEElcdj6cjIYLW7kStzNyvzSAU4VvkhZp6sAC80r08qvn NKGXSJDyVPhAe5+r/nYfzx203xFYtMWMK7ki+BrKFqkkNPGqFqHyobQ/1Kxl4Dvq6oKK pVemER2WfWP9r1F8TWpsCnX83h+BYlmgZfOxY2KOvMcwEIKXN5Zi82Te+HdH6S8wS3Fu 5tsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728911805; x=1729516605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t8AezVwPrFjuUUf+MrjbX+IGpei906KShJIFKpwBFic=; b=fYSM2/WTH94RmkpWIey0zMcE5xJ16SQUO9cpiJIM3hnwuWt6vU5cP8pOvL/bDdZjCg X1iN4ubCfnq/QiuRPrxZyOmE/YtL7KfeF31VHaw07dTmirxu2q/3AQu+XwwC9d3P1rRN A+DmiAjrwRJh5QHjmqroodeX5Tr40ifQiOVLxAQl4vrhvYKAQlvHbk0MQYsgr79CswUL J4/VwPyrdgFbhcJVOefIXZWzexepq0t9IQ9jMJAIjokOuiwjqbuuoZtrF2BPCB4CwNpV QRrPYCMM18zAzAXjZOSnqaG5ZgZ9cX7DdXl5V6qT0A4sfd8g1gIaNe3BkoU+sKYwBCZ+ aVVA== X-Forwarded-Encrypted: i=1; AJvYcCXbgfwUiS74K9IKZCY9EVH9T4K07yA8qOtQydbjMz9GotMcUhp4DxG2sHnz03/G5NriuYM4dvd8z1MmX8dVI6pz@lists.infradead.org X-Gm-Message-State: AOJu0Yy+MSdrDYTctbrGT8Vr50R/iK4tNjhqKzVOvVR6bewII/JIDPVQ Xpqe/oPcv3jdh1XcEa/xc3hrVgeJQ5fwz2J2eo717AMsDZZN1/YODs4MzoQb X-Google-Smtp-Source: AGHT+IEkcxsBd8EXsI3mjGhSeBuRkydLwhmHeXnNkmO+AuXIs5FW/RFlG2XvEEvTWzZ/6bVzK2oHCQ== X-Received: by 2002:adf:fcc3:0:b0:37d:542a:7995 with SMTP id ffacd0b85a97d-37d5ffb63d0mr7092665f8f.34.1728911804410; Mon, 14 Oct 2024 06:16:44 -0700 (PDT) Received: from eichest-laptop.lan ([2a02:168:af72:0:d130:f8ef:c6c1:55ee]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-430d748d42fsm153420425e9.43.2024.10.14.06.16.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 06:16:43 -0700 (PDT) From: Stefan Eichenberger To: o.rempel@pengutronix.de, kernel@pengutronix.de, andi.shyti@kernel.org, shawnguo@kernel.org, s.hauer@pengutronix.de, festevam@gmail.com, francesco.dolcini@toradex.com, l.stach@pengutronix.de, arnd@arndb.de, Frank.Li@nxp.com Cc: linux-i2c@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Stefan Eichenberger Subject: [PATCH v5 3/3] i2c: imx: prevent rescheduling in non dma mode Date: Mon, 14 Oct 2024 15:15:14 +0200 Message-ID: <20241014131635.205489-4-eichest@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241014131635.205489-1-eichest@gmail.com> References: <20241014131635.205489-1-eichest@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241014_061646_631573_DC3C4A3B X-CRM114-Status: GOOD ( 32.79 ) 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: Stefan Eichenberger We are experiencing a problem with the i.MX I2C controller when communicating with SMBus devices. We are seeing devices time-out because the time between sending/receiving two bytes is too long, and the SMBus device returns to the idle state. This happens because the i.MX I2C controller sends and receives byte by byte. When a byte is sent or received, we get an interrupt and can send or receive the next byte. The current implementation sends a byte and then waits for an event generated by the interrupt subroutine. After the event is received, the next byte is sent and we wait again. This waiting allows the scheduler to reschedule other tasks, with the disadvantage that we may not send the next byte for a long time because the send task is not immediately scheduled. For example, if the rescheduling takes more than 25ms, this can cause SMBus devices to timeout and communication to fail. This patch changes the behavior so that we do not reschedule the send/receive task, but instead send or receive the next byte in the interrupt subroutine. This prevents rescheduling and drastically reduces the time between sending/receiving bytes. The cost in the interrupt subroutine is relatively small, we check what state we are in and then send/receive the next byte. Before we had to call wake_up, which is even less expensive. However, we also had to do some scheduling, which increased the overall cost compared to the new solution. The wake_up function to wake up the send/receive task is now only called when an error occurs or when the transfer is complete. Signed-off-by: Stefan Eichenberger Acked-by: Oleksij Rempel --- drivers/i2c/busses/i2c-imx.c | 272 ++++++++++++++++++++++++++++++++--- 1 file changed, 249 insertions(+), 23 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index e0821332c439a..44749e594ddb6 100644 --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -197,6 +197,17 @@ struct imx_i2c_dma { enum dma_data_direction dma_data_dir; }; +enum imx_i2c_state { + IMX_I2C_STATE_DONE, + IMX_I2C_STATE_FAILED, + IMX_I2C_STATE_WRITE, + IMX_I2C_STATE_DMA, + IMX_I2C_STATE_READ, + IMX_I2C_STATE_READ_CONTINUE, + IMX_I2C_STATE_READ_BLOCK_DATA, + IMX_I2C_STATE_READ_BLOCK_DATA_LEN, +}; + struct imx_i2c_struct { struct i2c_adapter adapter; struct clk *clk; @@ -216,6 +227,12 @@ struct imx_i2c_struct { struct i2c_client *slave; enum i2c_slave_event last_slave_event; + struct i2c_msg *msg; + unsigned int msg_buf_idx; + int isr_result; + bool is_lastmsg; + enum imx_i2c_state state; + bool multi_master; /* For checking slave events. */ @@ -908,11 +925,156 @@ static int i2c_imx_unreg_slave(struct i2c_client *client) return ret; } +static inline int i2c_imx_isr_acked(struct imx_i2c_struct *i2c_imx) +{ + i2c_imx->isr_result = 0; + + if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) { + i2c_imx->state = IMX_I2C_STATE_FAILED; + i2c_imx->isr_result = -ENXIO; + wake_up(&i2c_imx->queue); + } + + return i2c_imx->isr_result; +} + +static inline int i2c_imx_isr_write(struct imx_i2c_struct *i2c_imx) +{ + int result; + + result = i2c_imx_isr_acked(i2c_imx); + if (result) + return result; + + if (i2c_imx->msg->len == i2c_imx->msg_buf_idx) + return 0; + + imx_i2c_write_reg(i2c_imx->msg->buf[i2c_imx->msg_buf_idx++], i2c_imx, IMX_I2C_I2DR); + + return 1; +} + +static inline int i2c_imx_isr_read(struct imx_i2c_struct *i2c_imx) +{ + int result; + unsigned int temp; + + result = i2c_imx_isr_acked(i2c_imx); + if (result) + return result; + + /* setup bus to read data */ + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); + temp &= ~I2CR_MTX; + if (i2c_imx->msg->len - 1) + temp &= ~I2CR_TXAK; + + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); + imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */ + + return 0; +} + +static inline void i2c_imx_isr_read_continue(struct imx_i2c_struct *i2c_imx) +{ + unsigned int temp; + + if ((i2c_imx->msg->len - 1) == i2c_imx->msg_buf_idx) { + if (i2c_imx->is_lastmsg) { + /* + * It must generate STOP before read I2DR to prevent + * controller from generating another clock cycle + */ + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); + if (!(temp & I2CR_MSTA)) + i2c_imx->stopped = 1; + temp &= ~(I2CR_MSTA | I2CR_MTX); + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); + } else { + /* + * For i2c master receiver repeat restart operation like: + * read -> repeat MSTA -> read/write + * The controller must set MTX before read the last byte in + * the first read operation, otherwise the first read cost + * one extra clock cycle. + */ + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); + temp |= I2CR_MTX; + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); + } + } else if (i2c_imx->msg_buf_idx == (i2c_imx->msg->len - 2)) { + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); + temp |= I2CR_TXAK; + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); + } + + i2c_imx->msg->buf[i2c_imx->msg_buf_idx++] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); +} + +static inline void i2c_imx_isr_read_block_data_len(struct imx_i2c_struct *i2c_imx) +{ + u8 len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); + + if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) { + i2c_imx->isr_result = -EPROTO; + i2c_imx->state = IMX_I2C_STATE_FAILED; + wake_up(&i2c_imx->queue); + } + i2c_imx->msg->len += len; +} + static irqreturn_t i2c_imx_master_isr(struct imx_i2c_struct *i2c_imx, unsigned int status) { - /* save status register */ - i2c_imx->i2csr = status; - wake_up(&i2c_imx->queue); + /* + * This state machine handles I2C reception and transmission in non-DMA + * mode. We must process all the data in the ISR to reduce the delay + * between two consecutive messages. If the data is not processed in + * the ISR, SMBus devices may timeout, leading to a bus error. + */ + switch (i2c_imx->state) { + case IMX_I2C_STATE_DMA: + i2c_imx->i2csr = status; + wake_up(&i2c_imx->queue); + break; + + case IMX_I2C_STATE_READ: + if (i2c_imx_isr_read(i2c_imx)) + break; + i2c_imx->state = IMX_I2C_STATE_READ_CONTINUE; + break; + + case IMX_I2C_STATE_READ_CONTINUE: + i2c_imx_isr_read_continue(i2c_imx); + if (i2c_imx->msg_buf_idx == i2c_imx->msg->len) { + i2c_imx->state = IMX_I2C_STATE_DONE; + wake_up(&i2c_imx->queue); + } + break; + + case IMX_I2C_STATE_READ_BLOCK_DATA: + if (i2c_imx_isr_read(i2c_imx)) + break; + i2c_imx->state = IMX_I2C_STATE_READ_BLOCK_DATA_LEN; + break; + + case IMX_I2C_STATE_READ_BLOCK_DATA_LEN: + i2c_imx_isr_read_block_data_len(i2c_imx); + i2c_imx->state = IMX_I2C_STATE_READ_CONTINUE; + break; + + case IMX_I2C_STATE_WRITE: + if (i2c_imx_isr_write(i2c_imx)) + break; + i2c_imx->state = IMX_I2C_STATE_DONE; + wake_up(&i2c_imx->queue); + break; + + default: + i2c_imx->i2csr = status; + i2c_imx->state = IMX_I2C_STATE_FAILED; + i2c_imx->isr_result = -EINVAL; + wake_up(&i2c_imx->queue); + } return IRQ_HANDLED; } @@ -959,6 +1121,8 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx, struct imx_i2c_dma *dma = i2c_imx->dma; struct device *dev = &i2c_imx->adapter.dev; + i2c_imx->state = IMX_I2C_STATE_DMA; + dma->chan_using = dma->chan_tx; dma->dma_transfer_dir = DMA_MEM_TO_DEV; dma->dma_data_dir = DMA_TO_DEVICE; @@ -1012,15 +1176,14 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx, } static int i2c_imx_prepare_read(struct imx_i2c_struct *i2c_imx, - struct i2c_msg *msgs, bool atomic, - bool use_dma) + struct i2c_msg *msgs, bool use_dma) { int result; unsigned int temp = 0; /* write slave address */ imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); - result = i2c_imx_trx_complete(i2c_imx, atomic); + result = i2c_imx_trx_complete(i2c_imx, !use_dma); if (result) return result; result = i2c_imx_acked(i2c_imx); @@ -1058,7 +1221,9 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx, struct imx_i2c_dma *dma = i2c_imx->dma; struct device *dev = &i2c_imx->adapter.dev; - result = i2c_imx_prepare_read(i2c_imx, msgs, false, true); + i2c_imx->state = IMX_I2C_STATE_DMA; + + result = i2c_imx_prepare_read(i2c_imx, msgs, true); if (result) return result; @@ -1139,8 +1304,8 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx, return 0; } -static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, - bool atomic) +static int i2c_imx_atomic_write(struct imx_i2c_struct *i2c_imx, + struct i2c_msg *msgs) { int i, result; @@ -1149,7 +1314,7 @@ static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, /* write slave address */ imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); - result = i2c_imx_trx_complete(i2c_imx, atomic); + result = i2c_imx_trx_complete(i2c_imx, true); if (result) return result; result = i2c_imx_acked(i2c_imx); @@ -1163,7 +1328,7 @@ static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, "<%s> write byte: B%d=0x%X\n", __func__, i, msgs->buf[i]); imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR); - result = i2c_imx_trx_complete(i2c_imx, atomic); + result = i2c_imx_trx_complete(i2c_imx, true); if (result) return result; result = i2c_imx_acked(i2c_imx); @@ -1173,19 +1338,44 @@ static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, return 0; } -static int i2c_imx_atomic_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) +static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) { - return i2c_imx_write(i2c_imx, msgs, true); + dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n", + __func__, i2c_8bit_addr_from_msg(msgs)); + + i2c_imx->state = IMX_I2C_STATE_WRITE; + i2c_imx->msg = msgs; + i2c_imx->msg_buf_idx = 0; + + /* + * By writing the device address we start the state machine in the ISR. + * The ISR will report when it is done or when it fails. + */ + imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); + wait_event_timeout(i2c_imx->queue, + i2c_imx->state == IMX_I2C_STATE_DONE || + i2c_imx->state == IMX_I2C_STATE_FAILED, + (msgs->len + 1) * HZ / 10); + if (i2c_imx->state == IMX_I2C_STATE_FAILED) { + dev_dbg(&i2c_imx->adapter.dev, "<%s> write failed with %d\n", + __func__, i2c_imx->isr_result); + return i2c_imx->isr_result; + } + if (i2c_imx->state != IMX_I2C_STATE_DONE) { + dev_err(&i2c_imx->adapter.dev, "<%s> write timedout\n", __func__); + return -ETIMEDOUT; + } + return 0; } -static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, - bool is_lastmsg, bool atomic) +static int i2c_imx_atomic_read(struct imx_i2c_struct *i2c_imx, + struct i2c_msg *msgs, bool is_lastmsg) { int i, result; unsigned int temp; int block_data = msgs->flags & I2C_M_RECV_LEN; - result = i2c_imx_prepare_read(i2c_imx, msgs, atomic, false); + result = i2c_imx_prepare_read(i2c_imx, msgs, false); if (result) return result; @@ -1195,7 +1385,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, for (i = 0; i < msgs->len; i++) { u8 len = 0; - result = i2c_imx_trx_complete(i2c_imx, atomic); + result = i2c_imx_trx_complete(i2c_imx, true); if (result) return result; /* @@ -1226,7 +1416,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, temp &= ~(I2CR_MSTA | I2CR_MTX); imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); if (!i2c_imx->stopped) - i2c_imx_bus_busy(i2c_imx, 0, atomic); + i2c_imx_bus_busy(i2c_imx, 0, true); } else { /* * For i2c master receiver repeat restart operation like: @@ -1257,10 +1447,46 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, return 0; } -static int i2c_imx_atomic_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, - bool is_lastmsg) +static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, + bool is_lastmsg) { - return i2c_imx_read(i2c_imx, msgs, is_lastmsg, true); + int block_data = msgs->flags & I2C_M_RECV_LEN; + + dev_dbg(&i2c_imx->adapter.dev, + "<%s> write slave address: addr=0x%x\n", + __func__, i2c_8bit_addr_from_msg(msgs)); + + i2c_imx->is_lastmsg = is_lastmsg; + + if (block_data) + i2c_imx->state = IMX_I2C_STATE_READ_BLOCK_DATA; + else + i2c_imx->state = IMX_I2C_STATE_READ; + i2c_imx->msg = msgs; + i2c_imx->msg_buf_idx = 0; + + /* + * By writing the device address we start the state machine in the ISR. + * The ISR will report when it is done or when it fails. + */ + imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); + wait_event_timeout(i2c_imx->queue, + i2c_imx->state == IMX_I2C_STATE_DONE || + i2c_imx->state == IMX_I2C_STATE_FAILED, + (msgs->len + 1) * HZ / 10); + if (i2c_imx->state == IMX_I2C_STATE_FAILED) { + dev_dbg(&i2c_imx->adapter.dev, "<%s> read failed with %d\n", + __func__, i2c_imx->isr_result); + return i2c_imx->isr_result; + } + if (i2c_imx->state != IMX_I2C_STATE_DONE) { + dev_err(&i2c_imx->adapter.dev, "<%s> read timedout\n", __func__); + return -ETIMEDOUT; + } + if (!i2c_imx->stopped) + return i2c_imx_bus_busy(i2c_imx, 0, false); + + return 0; } static int i2c_imx_xfer_common(struct i2c_adapter *adapter, @@ -1334,14 +1560,14 @@ static int i2c_imx_xfer_common(struct i2c_adapter *adapter, else if (use_dma && !block_data) result = i2c_imx_dma_read(i2c_imx, &msgs[i], is_lastmsg); else - result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg, false); + result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg); } else { if (atomic) result = i2c_imx_atomic_write(i2c_imx, &msgs[i]); else if (use_dma) result = i2c_imx_dma_write(i2c_imx, &msgs[i]); else - result = i2c_imx_write(i2c_imx, &msgs[i], false); + result = i2c_imx_write(i2c_imx, &msgs[i]); } if (result) goto fail0;