From patchwork Wed Oct 2 11:19:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13819670 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 64E68CF31AF for ; Wed, 2 Oct 2024 11:23:26 +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=pWe1LOAQAUHDMm2EMz2P+3VNJFGofn6qsW8nJYyiXVM=; b=ZSElqr3Z5enzKF+2LKTHvK/24z rZrPypDwNes8cf1vEZ6XPyOn9QOr7iDbEwN5MmWlF89mXCPZdiBC4zLh/2Ea3tisu77M6Z/iRJoLb yN+hADizM/XLPjEhQ8kqIXxFdkDsooIQ8Ed2F+Sk0/o41U3uxUI7wjeGrWfmBUpwOoEla7y/v6WKt 7eayqPRLTGVh4fYTKHZJ3vDsSTf3fi0SYzy+eeay4shl6mVqhPTbZ/YD/sk0VPU0j0zPPawZb5vQ7 jeBEQHEVkmNkmJUNOWgcj1wkEbtuiIqNvWWuEMvCAcF8gFWCkqic0uqjw3Hn6wiv679DJgG0vI7rl mAMLWAzA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svxRp-00000005dsM-0nX2; Wed, 02 Oct 2024 11:23:13 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1svxPD-00000005d6y-00DB for linux-arm-kernel@lists.infradead.org; Wed, 02 Oct 2024 11:20:32 +0000 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-42cae6bb895so60283025e9.1 for ; Wed, 02 Oct 2024 04:20:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727868029; x=1728472829; 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=pWe1LOAQAUHDMm2EMz2P+3VNJFGofn6qsW8nJYyiXVM=; b=kLFHV9AScygf+apLITxsFKu5RjhkC0cRmGZC48U8BcrtAjLZJH4KlLDmDQde9NzA33 lArMOv+kwZpTVVu3usxzL0jBruSE1fyty2GU46GLbLxy00OMibHU75HmyQdIkbgX8kG5 I4+s0tCyP67KR5Xs1BJMm78h27bihMpzxiECArekM88ykxHDtcJCu/B/5yklykYh2Ve8 bAINfYvwu5NDyCfp7LlUeeP03HyYtdA47PE2uxFbBxwdmciarvw6SJcBqpKTSttxdamE H2t3lXYnORYKfRkfehtVzpfamOC2zfH+gWHcTRpuJ2J0kZst4OTJL94PVkR0+PzLiSVO Zmvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727868029; x=1728472829; 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=pWe1LOAQAUHDMm2EMz2P+3VNJFGofn6qsW8nJYyiXVM=; b=btmYKE82QZt7TBQ2RtV/CzD2KOecfWnGffIGkypKuHxMa6VwlBCeXucqoWQWFBoKHg EjEilUjwBfbdFJAQk3m0BbUHc8p5pxY2irNcvBuRlSp+LwLdaep+mRx4QDleQ4I00R4j 1IIj6Z05lV91ZnH7O5o5LWVL3ax4FccjUFbF06v3P3iKhhJhfI/wDpjvk2TPosDe7YA9 CK+9FYATO1Awh/WrDxtx0Vrds9zjCLcWnyEGbN+e5B+8yPsnMFhiMXZV3T/6vJ9BOq8B GIKg58uzCtX+z4rYTIGNcLvz3yN9DbT6U9V8iz3PmBlsVTuMoTz9ERJ1C8f50GQVnSsP jCjQ== X-Forwarded-Encrypted: i=1; AJvYcCUicxWfaIRqGBOcmH1fq79qTW6mDFrVn9X8JY77oRba4Ru1TQISExm3tJX9NIGO0Aow4NLWug/ZnYf//fa23c8O@lists.infradead.org X-Gm-Message-State: AOJu0Yx8XeGG1pUK687SIHds56EP8vvw9ttzd9+Pdr4dcPfl3IrI9ot/ 4onn8mOq7PHMCkrL9+IeIfv5BpEYl2db1F+oQB3YYKOay4DtaJi8vgzf7+d7 X-Google-Smtp-Source: AGHT+IFGTSatLO1Ox4UQ8gJUxFQ+Sve6VZQyHmLuQ8fa68lXptO2HqFrHIWSzqjI6q344kc1YAHVhQ== X-Received: by 2002:a5d:6189:0:b0:374:cbc4:53b1 with SMTP id ffacd0b85a97d-37cfba0a641mr1674756f8f.40.1727868028950; Wed, 02 Oct 2024 04:20:28 -0700 (PDT) Received: from eichest-laptop.corp.toradex.com (31-10-206-125.static.upc.ch. [31.10.206.125]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42f771a1209sm23530685e9.0.2024.10.02.04.20.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 04:20:28 -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, 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, francesco.dolcini@toradex.com, Stefan Eichenberger Subject: [PATCH v4 1/4] i2c: imx: only poll for bus busy in multi master mode Date: Wed, 2 Oct 2024 13:19:39 +0200 Message-ID: <20241002112020.23913-2-eichest@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241002112020.23913-1-eichest@gmail.com> References: <20241002112020.23913-1-eichest@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241002_042031_066560_3473FF79 X-CRM114-Status: GOOD ( 15.38 ) 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. Signed-off-by: Stefan Eichenberger Reviewed-by: Frank Li Acked-by: Oleksij Rempel --- drivers/i2c/busses/i2c-imx.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index 98539313cbc97..fbacdfaf6b28e 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,8 @@ static int i2c_imx_probe(struct platform_device *pdev) goto rpm_disable; } + i2c_imx->multi_master = of_property_read_bool(pdev->dev.of_node, "multi-master"); + /* Set up clock divider */ i2c_imx->bitrate = I2C_MAX_STANDARD_MODE_FREQ; ret = of_property_read_u32(pdev->dev.of_node, From patchwork Wed Oct 2 11:19:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13819671 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 69AA8CF31B5 for ; Wed, 2 Oct 2024 11:24:39 +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=HfSuN/9R2eLcV7k6Kk+kiVJyE0XEHkvEyhQ/EQ6vegg=; b=ujpnYd/eq8caEkhrYlWiG8iYqy VQJ2Ej/IIBFv147/9WKOkbyAKIbqPs8DAgMUv+Qx4Kdr0q4Ct5j42dY3ZEs8XR8ntd6Rua9PuFOp0 eYpihptpMIj0Loz8p7f12+G0890aoT7DzFFvofkhEjmpIJH8A12RouDyzUb00NLVinFP/buojwkSf DKydSU4cN8NXpOLbfPuhCWBF13nDb4UcV0sdL3xy6WCeQ/vvQQVFV30mDTuXsgFd28b0tI9ykqjEb mQpa3rYt/1UeUnA3uXfrqNuQ33T6Lz/AnqJmMhZBG2PK4MLDKTNwYZf5iLswVyYSlDOfhVgq6i48/ FVegi+2Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svxT1-00000005e7Z-39ER; Wed, 02 Oct 2024 11:24:27 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1svxPF-00000005d7I-0n3v for linux-arm-kernel@lists.infradead.org; Wed, 02 Oct 2024 11:20:34 +0000 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-37cdbdbbb1cso2488789f8f.3 for ; Wed, 02 Oct 2024 04:20:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727868031; x=1728472831; 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=HfSuN/9R2eLcV7k6Kk+kiVJyE0XEHkvEyhQ/EQ6vegg=; b=BjUs9yliflBa7XJtXo3MCmyCFQ8IZQsgMigdy/3Vp3+x636Ie97F8wFPTnK+XfEPh9 aCaP6+42KIHQxw5uFrsyiKn3yX04anvh7BxvzAsOvEHGJqQmqzmABP9X0v6MvdfTKWL+ SqTCb+hdKIYHzataYb0Fa3XBv3M6jh99H252pv3HxRpjBIJ8P0U+KtwkMtu3iC8x/+NO po30wAyCFa0hQ9Dji38Ecsg69aZ+WpJfFdv4e0ey4bcIWRMcL8+p/nQ0ELI15hJJyB3I czbcLGK2K90qJXvBTNTxV9p422VYbJCowNHt0sADLpU3CeIoWJ2N3XJDoHHCe3tz0PBB tP7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727868031; x=1728472831; 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=HfSuN/9R2eLcV7k6Kk+kiVJyE0XEHkvEyhQ/EQ6vegg=; b=mr0ra1dbd5sFH1ABUABkDY5IdG1fh5xQ7btb3zLKKSxCKLar+6e7OuHUInINUjOMrT wNh15D57czHV9iQ1oQT0qIG5IazkvRK+3HKJDXb4gtobzx1rJ4wEj7zNiEqg8IhoTrrl QHCXUUfB3bAMzeMiWPQpd5O5KMmWaYC/dsQ6xBbnDLFgwupD5YQ+FvW7rhWLj0Tv/vju kBE409+L4mT5XipDpMtGNojJJmTCfPniLPBjc7AGcGeyfS+OcKAOHd4r0zUqepUF93P8 s9ntiaqOg+o2o4MDbe3YnSoPFWbHzxXlsq+6ag3qcDs5tftTMQyqQq86n77I8zF9EB3k /sQQ== X-Forwarded-Encrypted: i=1; AJvYcCWb/VSlPQi/kCLNe39ZO6OdqMGG3gkUn0LbIHyrYs6rdcwfQW5rol3rY/2QYEJV3hqoh8B1GL531OMBKAplEzjy@lists.infradead.org X-Gm-Message-State: AOJu0Ywz7TlBeumppU/3UCo5wCq/lEDSsIt4NfAF5NMn3FbWe72yMCYW 9+DyM+GidpkdXXThR+vGPRUy0jjsVx91MGuOQdNhk4r0kqJ5Lxvc X-Google-Smtp-Source: AGHT+IHA06m26rIP/ioTpXEk53Q2NwfqbhJDeAbiLo7zhCOBS8PDUS/+gwi/4+3YNDN5go+YVrwIiA== X-Received: by 2002:a05:6000:184b:b0:374:c432:4971 with SMTP id ffacd0b85a97d-37cfb8b559cmr2069721f8f.16.1727868030624; Wed, 02 Oct 2024 04:20:30 -0700 (PDT) Received: from eichest-laptop.corp.toradex.com (31-10-206-125.static.upc.ch. [31.10.206.125]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42f771a1209sm23530685e9.0.2024.10.02.04.20.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 04:20:29 -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, 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, francesco.dolcini@toradex.com, Stefan Eichenberger Subject: [PATCH v4 2/4] i2c: imx: separate atomic, dma and non-dma use case Date: Wed, 2 Oct 2024 13:19:40 +0200 Message-ID: <20241002112020.23913-3-eichest@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241002112020.23913-1-eichest@gmail.com> References: <20241002112020.23913-1-eichest@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241002_042033_261147_D4BF2E94 X-CRM114-Status: GOOD ( 20.56 ) 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 fbacdfaf6b28e..8e9cdeff278a8 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 Wed Oct 2 11:19:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13819672 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 E89F6CF31B4 for ; Wed, 2 Oct 2024 11:25:52 +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=4V7Qw24AIjD9e3DTOuaXw9uJfa/Y+gcgalY/kZOlqpg=; b=2ZdRucB5Hh9iq6R/UqX5bWUt7O PHp7yHiUU4FrrynqvkHQbu6IrNHPn5MJIJOCDp3m3/9PofHSF/Ht+qPCHWe+1ZKgKKjuPtnZOvqIA D9C0koK+hPIfq7MHN/LG7od5ijyER2o9JqTxfGIDzn6cqbi/yPEx8XutEePA1kpF3mqWXhuAZvTbr 3V/WYOtQ6kA8KJqbJWjJN/R7nn5aVNC6OScTKDzikW8XMhqKYroOOnKWbccPa/VcxheEglwxVn3a3 SEVRgSy9PgjYx8bQcWrPwmgpx8cOD3em2WnyT9fsQZc8Tz36KBck5uPTst0nmnD1o4Aiej9DP/nZU eNTPPrDA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svxUE-00000005eNz-0Utj; Wed, 02 Oct 2024 11:25:42 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1svxPF-00000005d7h-2aMt for linux-arm-kernel@lists.infradead.org; Wed, 02 Oct 2024 11:20:35 +0000 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-42cb6f3a5bcso80549415e9.2 for ; Wed, 02 Oct 2024 04:20:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727868032; x=1728472832; 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=4V7Qw24AIjD9e3DTOuaXw9uJfa/Y+gcgalY/kZOlqpg=; b=Q2L2enIQ51FpeSM3Cu5R1ZRZXvHdEXBL6CPOtABJOrU53upPq8HfJifbg981YQejmY fHwssRrXQUEo59bMi26owePbtklldTR5JoLUiojqqdFBJ3fmrA/4Oqipm25PU3x8WsGL T24KvHvL8NXouIghYx4ppXhfx11HqJ+eb0NLQVJQlYfW+K7xoaKUd6WiK5Rea6MYdzj+ z+vtvBb1dMe6XwqB2mh40nkW09JTB35qNxH445IHfi9Qii/DI4bevYEyOWuH3T9TV6So 9l0zRSfAAqmL33BWCg+7isiUQO/xCo0mcuChgvAd3UpjJW3o6moTn1pRAe68m+H2y5Lj ilPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727868032; x=1728472832; 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=4V7Qw24AIjD9e3DTOuaXw9uJfa/Y+gcgalY/kZOlqpg=; b=KrSsYxzXbKExkTUGguHv0pDFKEMOkRaKs3hLmNbQO2dKiKzCexX0sPrnjuDX+LYeko HD/1sSo7aiVveFQIUkak3XR+Zgem4yCftujf+/DL88OUX29Zl5TFTaA83Z4SQrtC69fy 2opai4c6EPi5fRF0agaJMxmOWGikhhmfT0uecT2JfmixTanNBMBeTQwzIClwSAJ1v6Sk WejgyXi/LxWOK59KnB2pMmw0pArSplnNrQpzUkml2HpY6GCxm45XM1xCqidNB2CrI6+J PBzHPbU4EIcJdmApt0tqd+GSFAZvdj8sgfAHsUXVBqKyuikl6Alfgf8Oc8VNZEDF1iVA gJzQ== X-Forwarded-Encrypted: i=1; AJvYcCXwt2xx/3/oxL/JJtXpRvB7hBv0N5aE00qcILRMQemNDv5KSoUZPBRnH4ZEt2TVu+WYPSsMnXxNDacbixL9FUOd@lists.infradead.org X-Gm-Message-State: AOJu0YyNFjsqKs4zsM/ydSmNofSouIZO7YDn1chuyhhFQ8P8lZBa9ap/ q4Jl/q8dc7Ku4LVBjHd+9r4MOs98bNj2k8rjEWM1lZuxinK0DO+1a1KTfSEW X-Google-Smtp-Source: AGHT+IGehBz2wvp4octtBZnLUT1OhdST1Vf9e5fzfCVsKngbJHX2vTvVmrVGdPh9ghtRzv5CYCn1eQ== X-Received: by 2002:a05:600c:1d88:b0:42c:a387:6a6f with SMTP id 5b1f17b1804b1-42f777c365fmr23211065e9.20.1727868032188; Wed, 02 Oct 2024 04:20:32 -0700 (PDT) Received: from eichest-laptop.corp.toradex.com (31-10-206-125.static.upc.ch. [31.10.206.125]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42f771a1209sm23530685e9.0.2024.10.02.04.20.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 04:20:31 -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, 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, francesco.dolcini@toradex.com, Stefan Eichenberger Subject: [PATCH v4 3/4] i2c: imx: use readb_relaxed and writeb_relaxed Date: Wed, 2 Oct 2024 13:19:41 +0200 Message-ID: <20241002112020.23913-4-eichest@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241002112020.23913-1-eichest@gmail.com> References: <20241002112020.23913-1-eichest@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241002_042033_685391_A819F8C6 X-CRM114-Status: GOOD ( 14.44 ) 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 Use the relaxed version of readb and writeb to reduce overhead. It is safe to use the relaxed version because we either do not rely on dma completion, or we use a dma callback to ensure that the dma transfer is complete before we continue. Signed-off-by: Stefan Eichenberger Acked-by: Oleksij Rempel --- drivers/i2c/busses/i2c-imx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index 8e9cdeff278a8..f70522a83f823 100644 --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -313,13 +313,13 @@ static inline int is_vf610_i2c(struct imx_i2c_struct *i2c_imx) static inline void imx_i2c_write_reg(unsigned int val, struct imx_i2c_struct *i2c_imx, unsigned int reg) { - writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); + writeb_relaxed(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); } static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx, unsigned int reg) { - return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); + return readb_relaxed(i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); } static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned int bits) From patchwork Wed Oct 2 11:19:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13819673 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 517CACF31B5 for ; Wed, 2 Oct 2024 11:27:07 +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=i0UqMrIXPYSH8EyqJY2SsPH+wmHdgC0pdLL8XDK1EHE=; b=CUlSchMetkKNGwAdIb7ljy/tAS 0t6ranw5cMgaVfOX1NiSFwXGNAGSSCjuWHvEKcOr7rpVupoPvFYusHcTrhikVLAvOe44BLIPomEdb nZo3SpayfZedkHsFTThEveE8YAnHqJlfAcA0imdR6ftzOGFFC5ZtC/dcTU80zqqKPFxdV2Mr0yrqk 8mr1TmGTVKqLsc7jVLcPe3Z9Ky/JYhvOrHmu+hYA3Lj7J3hvyZRZPL2+Ospx4ZccaSAjrPRSHFnRx Ap2IUceBJX7M//kDfv2lgYMLQvGY6aBzHUX0rsbeTUVtOAGamKHspkFxTil+CidRW+Y97GrNrBtr7 ntZQOMnA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svxVP-00000005edx-2Fr9; Wed, 02 Oct 2024 11:26:55 +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 1svxPH-00000005d8B-1BD7 for linux-arm-kernel@lists.infradead.org; Wed, 02 Oct 2024 11:20:37 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-42cde6b5094so58865995e9.3 for ; Wed, 02 Oct 2024 04:20:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727868033; x=1728472833; 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=i0UqMrIXPYSH8EyqJY2SsPH+wmHdgC0pdLL8XDK1EHE=; b=dqttFFkuF8fXSOPQzEv5gwGbEnAMwOl91N+ZhUxM1avYLwBIbb4TxQknrjWw0z08qa A0RWxhT2MmSoFXeNtXicjNBaJLmjBJGbL6RkEMcZXdyzDeuZJIcqwo6x8n+/FasuhCkS 0pNU5KoRPPgRrHR62jwXNnbMsa0F8oei/cWyC9bwb0H8bSE/MtoJMLyqtiGMsAOWuGJ0 P6iQW7T0h/SvUnz01d+jVCsLJJgrrsC4khFQnlFusqiBbXd2byilqDBcT1W/kM13ZiUO f9BHDnZlcUbuGhxWOxVjlPz5bWCRhCsQrLAE6f71uJAzkRB46b6tBL854JDPkInmirqu Xz9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727868033; x=1728472833; 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=i0UqMrIXPYSH8EyqJY2SsPH+wmHdgC0pdLL8XDK1EHE=; b=PyGJNLZuRq6/XrppSYgO8wFm+O+K0dlQfzDv4rvTfsyHl/kMxwHuUqRilCl3qE9v2l EwOYOR+v7vS8dIuspbCggIBFwTDDxDiSCZm1N59xGIaWFooHCl5TnnJDdM3m2ZY23x/u 3All2c4T9cUZojEeOHD4VA/EzmH1r5dh8NHGh6gZQHvKka3sqLPhfJ+STzrCdUz661r6 1LhSWQ1bFXq/4dEyupoeA7wVc2f5Mo6EbcngAK71pjFNo0QuYHFvoJ+EExC+yvilEcq6 3yNrXXs27f+msm8YMBjbHDnuc/4bTyiob4CAbKVnClaZMMFY9FXS9d8u627ydveyi0rV tDPA== X-Forwarded-Encrypted: i=1; AJvYcCVdJppeKL+P2tB2+EqSyaA5jt3CEKhKT38M0BD91tEsBIq5OOapjeWclS4NV5UkoHLQ52pw5RzzYF8sc2xm3j4B@lists.infradead.org X-Gm-Message-State: AOJu0YyvAgf5az9UnyAg4CjY5oOBqyAvcyn+cIaWXMjvByNWqxoTtF6H UsI58U1bOrP6xb1pAZO93MIAB1pTdBfoS6LOGGZXU9WNnqQvwNPl X-Google-Smtp-Source: AGHT+IFWUguRAwnKc+f8S01CTcv6elxrDMwcpqnVEdijvi4uwKSnK67yaYiDOJ71rmTO3oXT30QFGg== X-Received: by 2002:a05:600c:a41:b0:42c:bd5a:9479 with SMTP id 5b1f17b1804b1-42f777c8503mr20510475e9.18.1727868033283; Wed, 02 Oct 2024 04:20:33 -0700 (PDT) Received: from eichest-laptop.corp.toradex.com (31-10-206-125.static.upc.ch. [31.10.206.125]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42f771a1209sm23530685e9.0.2024.10.02.04.20.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 04:20:32 -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, 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, francesco.dolcini@toradex.com, Stefan Eichenberger Subject: [PATCH v4 4/4] i2c: imx: prevent rescheduling in non dma mode Date: Wed, 2 Oct 2024 13:19:42 +0200 Message-ID: <20241002112020.23913-5-eichest@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241002112020.23913-1-eichest@gmail.com> References: <20241002112020.23913-1-eichest@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241002_042035_386863_FDAAB3C4 X-CRM114-Status: GOOD ( 32.71 ) 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 f70522a83f823..d26a728aab2de 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_err(&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_err(&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; + } + 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;