From patchwork Thu Sep 24 15:27:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 11797743 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8266E6CA for ; Thu, 24 Sep 2020 15:30:23 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4CC8821D91 for ; Thu, 24 Sep 2020 15:30:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="bodnHRvF"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BS9G2Z10" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4CC8821D91 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ZAyHHlea+O+RRE14PK0c6o5mIbUoiykQFFTJl7vjA64=; b=bodnHRvFoIPXzGP1xCiPZzV2Q n2ZbdiWH9sMSrYW/WCxNREvEfNeu/NZBPnDAzafaCAr4MZyW8xVHFY/d2sQZcOA6e0vKz1muzlUMw Lp9ntcunV8nqDDJyfkH797vJMgRAnokiBVF3Dr4bJC0abaTDizwQSZtaPchoIT8v0j5KolKK3EYm+ 35gYtvmRKHPvKpqy44XOAo2T7G1rlOr6nrji6lDS+XUnaRGYw8gXiIA01UPSSXwcsbmU6vxeu30/Z Z0/rBXGg8jX/fxQXOEOfgSIDY3Qs6LLO2WZdIe6Yp0ogP1uozbdxNa7LVxdnuaA4sdy/gMKqus8+4 daPKkazJw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLTAk-0003Fd-75; Thu, 24 Sep 2020 15:28:38 +0000 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLTAZ-00039i-Rd; Thu, 24 Sep 2020 15:28:28 +0000 Received: by mail-pf1-x442.google.com with SMTP id x22so2092410pfo.12; Thu, 24 Sep 2020 08:28:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6HbdXWHUUwENmYPOkj3k/38B+LwukcgShCpL4ORLWz0=; b=BS9G2Z10aTHODKi9DPOqtRH1BDkXHi4BDW2MIXTXfcSvRcecmRWkw+uvS+ippogC5D lKAPVzmuNIXIw6t4Od6roKdghQUQkPpQKVIuXGnieXc4QEhSms2UVCbN4YLHwaAGihsR MD6srxVduIAJIDD2GsibdS10OiSOuV1Zsndkt41hmwVqFXYyGwLpgDzna+K7anx5M/T7 KjGyiQylxE7U56YZt+iU0W/TICckQsoPkNkiz0LSaAAwgwjdyBc1WvRAM2FXmDbX2dBx UaYx2IsHwnPqjkG1qWKq4FVGhyZDZC3OGHisP0d4B7mtyrjUGpkjq8VZ2/fyNv+eLtfG mQWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6HbdXWHUUwENmYPOkj3k/38B+LwukcgShCpL4ORLWz0=; b=gbjUuEEYn+7B7iVxJUscP6G1sWpdYkuy5kqQ+zLH0hQFL5tfNL8XhT5ga1gwc4jXUy tuTvNYlAZ4Voq6EIVhKcCoDY4mFd7LfW6W3UbnTEi/8GX0xzTjqNUmRD26rNh7B14ys9 oTl/vMfw7n2Ni9wpdDjbYKmPLfJaHlt/LUhfRiYG/lwlX97iv6D4P6fBuCVC5k7Y2eHu KNRtOvPObm9AkmHLHj6bR6bRHw9TY74uxg12lJTisWYttRNQv6p9Qpg7+W4lbI5WAcEN Fgu1SVFKdD1DM02HAiCtxwSt4nPu1daEyujLi+bv3WixH1EpzpGpnBim2VVWwHjBHoRE Zr7w== X-Gm-Message-State: AOAM532+BFJqb8PlNo6d8vpvm8WQHH1R8rISfF4MjPtwC4qp6XBUIeV0 caP6V8pRcm4B7NB7n4yLCiE= X-Google-Smtp-Source: ABdhPJwmevCejQOrLAOkrF2jd1QUYjk5raPouovzRqWETtYfP2zCKdaTqfzhPGY4cIWcuxFEPQ0GFQ== X-Received: by 2002:a63:ea4f:: with SMTP id l15mr148553pgk.434.1600961305967; Thu, 24 Sep 2020 08:28:25 -0700 (PDT) Received: from guoguo-omen.lan ([156.96.148.94]) by smtp.gmail.com with ESMTPSA id e62sm3233212pfh.76.2020.09.24.08.28.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2020 08:28:25 -0700 (PDT) From: Chuanhong Guo To: linux-spi@vger.kernel.org Subject: [PATCH 2/3] spi: spi-mtk-nor: add helper for checking prg mode ops Date: Thu, 24 Sep 2020 23:27:29 +0800 Message-Id: <20200924152730.733243-3-gch981213@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200924152730.733243-1-gch981213@gmail.com> References: <20200924152730.733243-1-gch981213@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200924_112827_920244_1A2491FC X-CRM114-Status: GOOD ( 18.12 ) X-Spam-Score: 2.6 (++) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- 2.6 RCVD_IN_SBL RBL: Received via a relay in Spamhaus SBL [156.96.148.94 listed in zen.spamhaus.org] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:442 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [gch981213[at]gmail.com] 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit [gch981213[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, bayi.cheng@mediatek.com, Mark Brown , linux-mediatek@lists.infradead.org, Matthias Brugger , Chuanhong Guo , Ikjoon Jang , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org op checking/resizing logic for the newly added mtk_nor_spi_mem_prg is more complicated. Add two helper functions for them: mtk_nor_match_prg: check whether an op is supported by prg mode. mtk_nor_adj_prg_size: adjust data size for mtk_nor_spi_mem_prg. mtk_nor_match_prg isn't called yet because supports_op is currently broken. It'll be used in the next fix commit. Signed-off-by: Chuanhong Guo --- drivers/spi/spi-mtk-nor.c | 76 +++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 7 deletions(-) diff --git a/drivers/spi/spi-mtk-nor.c b/drivers/spi/spi-mtk-nor.c index a0087a5e869b9..4bbf38ef5b4b1 100644 --- a/drivers/spi/spi-mtk-nor.c +++ b/drivers/spi/spi-mtk-nor.c @@ -168,10 +168,76 @@ static bool mtk_nor_match_read(const struct spi_mem_op *op) return false; } -static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) +static bool mtk_nor_match_prg(const struct spi_mem_op *op) { - size_t len; + int tx_len, rx_len, prg_len, prg_left; + + // prg mode is spi-only. + if ((op->cmd.buswidth > 1) || (op->addr.buswidth > 1) || + (op->dummy.buswidth > 1) || (op->data.buswidth > 1)) + return false; + + tx_len = op->cmd.nbytes + op->addr.nbytes; + + if (op->data.dir == SPI_MEM_DATA_OUT) { + // count dummy bytes only if we need to write data after it + tx_len += op->dummy.nbytes; + + // leave at least one byte for data + if (tx_len > MTK_NOR_REG_PRGDATA_MAX) + return false; + + // if there's no addr, meaning adjust_op_size is impossible, + // check data length as well. + if ((!op->addr.nbytes) && + (tx_len + op->data.nbytes > MTK_NOR_REG_PRGDATA_MAX + 1)) + return false; + } else if (op->data.dir == SPI_MEM_DATA_IN) { + if (tx_len > MTK_NOR_REG_PRGDATA_MAX + 1) + return false; + + rx_len = op->data.nbytes; + prg_left = MTK_NOR_PRG_CNT_MAX / 8 - tx_len - op->dummy.nbytes; + if (prg_left > MTK_NOR_REG_SHIFT_MAX + 1) + prg_left = MTK_NOR_REG_SHIFT_MAX + 1; + if (rx_len > prg_left) { + if (!op->addr.nbytes) + return false; + rx_len = prg_left; + } + + prg_len = tx_len + op->dummy.nbytes + rx_len; + if (prg_len > MTK_NOR_PRG_CNT_MAX / 8) + return false; + } else { + prg_len = tx_len + op->dummy.nbytes; + if (prg_len > MTK_NOR_PRG_CNT_MAX / 8) + return false; + } + return true; +} +static void mtk_nor_adj_prg_size(struct spi_mem_op *op) +{ + int tx_len, tx_left, prg_left; + + tx_len = op->cmd.nbytes + op->addr.nbytes; + if (op->data.dir == SPI_MEM_DATA_OUT) { + tx_len += op->dummy.nbytes; + tx_left = MTK_NOR_REG_PRGDATA_MAX + 1 - tx_len; + if (op->data.nbytes > tx_left) + op->data.nbytes = tx_left; + } else if (op->data.dir == SPI_MEM_DATA_IN) { + prg_left = MTK_NOR_PRG_CNT_MAX / 8 - tx_len - op->dummy.nbytes; + if (prg_left > MTK_NOR_REG_SHIFT_MAX + 1) + prg_left = MTK_NOR_REG_SHIFT_MAX + 1; + if (op->data.nbytes > prg_left) + op->data.nbytes = prg_left; + } +} + +static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) +{ if (!op->data.nbytes) return 0; @@ -200,11 +266,7 @@ static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) } } - len = MTK_NOR_PRG_MAX_SIZE - op->cmd.nbytes - op->addr.nbytes - - op->dummy.nbytes; - if (op->data.nbytes > len) - op->data.nbytes = len; - + mtk_nor_adj_prg_size(op); return 0; }