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: 835965 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 208301AA7AF; Mon, 14 Oct 2024 13:16:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728911806; cv=none; b=qdVW1zoxUSXlPNDwH4oazTdUuRNtWzCyog5gSOXH2EabW55lw4QTODlsy3ydP5scKZsVHwQ2ugC9hLg2Y+G5eWW+zAcJuZhkGAS/CGWgivlnYZfej28fV7h4UsLUOz6L7lvRHmpzszZ+RTSBVtVvGX2yOMi9/5geqQldk/klsS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728911806; c=relaxed/simple; bh=0o2/bTnN9GhGXVe/7JM6EbEH/20m49itO+Em9O9ym6g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ia+v2AGu3ynujI0T09GdS2XQf4peL2m+ojVWa5Wnq9mRymBrSbCuyn/DyfFZiNv2XpWe3t7KubVP8Tl/laOMoWT40Gnlj9hnD6u/AriNzKJR3ltvAwRj+E0KkqB7h7s4dbT/46CBvHKcU9buK27aL5FcbHXo6+5uO5THn+WUOfY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nSDTP9fO; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nSDTP9fO" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4311ac1994dso32374905e9.1; 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=vger.kernel.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=nSDTP9fOSVFLXvL7IzyAKqv/xZOOE0ZBFtM/P2FElqZ0T27l/a1j/6Zk2pPS7O3z60 d2dB7qP1sdMWiF9yiAA59f2s+LS/9bQT+E+iFIXata7Pq35OzZJ0Rij3uVzpy19kNiEv 8LYrByBXReJRKJNYIneRQPU13RYvIG7oX5Vcu6eRfufDr5r0MvJpsbmK6jwzwX3xHkr/ mjaa9b/J/g4+KLi1pNG2d9Lqc96gdBeTafDmwu0H6FX3u+OSmAUUyQKQGdTb4LOV1kYI qgyaXuMuCMqD6G4MrlMeREGH8d6L2cBqgIrPlX/uCGeVhaaLEfKNCmY6TvZLkXzKwd0v 7G1g== 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=ONIHv3FrXkDJS7E+WNriPyCmlbgXH977YrJPUTOlZec5Cnn2x340AwBXosTtJaB2Ip CF8rM+kG7UxMIaTF8MGMPjEhtPH1LW/nvghoUq47GJXwDLy9Ku+ylki4nUYOuyqxqUoF UvLg97szIxOAg739piR7fJvPu5CcoSbGSiKpr0MlhH8FuOYfbPkqUdcMm3lrXDyVoA8b CITymD42JlGhZSSASDRXo0RSxVLKesNvOMRFfBEJwqeSXj7/W2xiZrxpuqcvy3b0xck7 nD66ApOYI4bpkunFmHSGOmwAbfNTGlKo6ezJZ19eBululOzbOApNHPj7TEl2e6V/R/qZ jn7g== X-Forwarded-Encrypted: i=1; AJvYcCVc5qxZCR9Oa1KiKRlP4JgyCHD+M5MZhgITwpqKlD6pug7E+t/x/D92V694jEwdaI49PEmomzcGDSbTVPU=@vger.kernel.org X-Gm-Message-State: AOJu0YwxsRLA9Y0aw0j/erswfMhsv8vxeAOYHqNj2Z2yLVsKR6AmHqb0 AVQYAaKvzuHcYfLvcv/o1loPlf8/x9RuI0TidIVSg20HBaJUzm22 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> Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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;