From patchwork Thu Sep 24 15:27:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 11797725 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 B87466CB for ; Thu, 24 Sep 2020 15:28:38 +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 7482221D91 for ; Thu, 24 Sep 2020 15:28:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="DMClC9PM"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OEI3vPl+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7482221D91 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-mediatek-bounces+patchwork-linux-mediatek=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=Tgkanm4vvI1yAlCAwsUtUi06vL8SIya1GzV0vbfxsiM=; b=DMClC9PMvDRNJ+XNhRoQZBM32 R4QuRYguYQC/kWof54UF0qDsVQhGY/2Dawh2bGm/E+P66pQdJWMTaYv4gWAWIjSqfVLhnX7J4P2FG 0zO+ImuJcaSnMAMaF4g5xU733sw0SyWv8J8eV3zi81LuZjxiQeV3NxdCQhM4F/cuOcIH0X/KVBqjH nKUyZc9iPvR8Cm02p9BA4faLjCHP5T7qHn6aI+PnE/DTL+5/E/6lyGfhwo9dXiEcvGAodbBwsVo+j MT1N7km54clfMnwf/UppoFdr0Md19MCmPoacs+J077RD9hOgYtjF3YOFik0zIPfSN1XI6XAj+fmmD 7Ohlz6EJQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLTAi-0003Eq-6F; Thu, 24 Sep 2020 15:28:36 +0000 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLTAX-000394-19; Thu, 24 Sep 2020 15:28:26 +0000 Received: by mail-pj1-x1041.google.com with SMTP id q4so1880257pjh.5; Thu, 24 Sep 2020 08:28:24 -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=vzz0659k4nwMVwakwm/RmtcC/L9vr67nkqocIx4yobo=; b=OEI3vPl+yd9QIKrq9/edbzxv4ksL18bROyX1URD2V/OelcE0t1iKUAP6iDZgDIC261 LKb8mf02MKWWNaQdnurAtYfmW9xYlXU+poqtaDanPW8I7bGg/R5caOTedxHIC3E/iIw4 Nh/WNn0BRTsriHXreeuhR0T1JFprH3Dh1AufH4ew5tch71spMA7p4z79FY+WoC5qkBB9 dmXPmdoRs9L5PiFnj88o6/m7IAz4I6my2tsFMvbdfHwMQr+X73NtV+/Gm9QXwJt2ETCu jZ4VaARxJ5X3DpFN4ACIZzyBxvvTicDqfO4Mum9tOJj3MjZ5fWt0EhKZxIp4aAt8q+Cm 5SSA== 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=vzz0659k4nwMVwakwm/RmtcC/L9vr67nkqocIx4yobo=; b=YmJDYPxxssV4uOLgCKnkiE/ZDIHrasx7EmEK5D0XclhVgqsuZ81MgKbDlzVw/3rG6c Ye7YED8mfwilaUZFty5eS3H/fiIdEmCy/Zc5G7YfaCscBsGJAFJe4j2NygIeLszfZXjg LuwMz4NerNq9HRNxRt2XT4ZzeSStXinYNblIRebUek8esuLCe7GSyM8EXZvzCQCTafZ0 3yISfRHJkKG3wf6zH2+mC121pLVGPOZ6FUtIy1M9Ma1uHlxFA02OJseG1Vd0j7aFUvXs 8xl/EePpLnw/KZPV8pwtYmVNapeAugyzEkkk+fYtR1mbsLSPm+2PfWf0GR3MgjupAR8e IIaw== X-Gm-Message-State: AOAM531H/JEPuxh3XMriLjZyAWbTYizq5uRhOnZYNbITx8rQcU8xtQHl cPFBuBgSRnWhu41CSARbHwI= X-Google-Smtp-Source: ABdhPJziJuVlIk0Pqjl8kKPAPvOSPiY8oac/jAD+WQLAQXWe0/6y7/sJek4nx6EQk8nCWigsCeI9/g== X-Received: by 2002:a17:902:bf08:b029:d1:bafb:ae2a with SMTP id bi8-20020a170902bf08b02900d1bafbae2amr4919264plb.39.1600961302878; Thu, 24 Sep 2020 08:28:22 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2020 08:28:22 -0700 (PDT) From: Chuanhong Guo To: linux-spi@vger.kernel.org Subject: [PATCH 1/3] spi: spi-mtk-nor: make use of full capability of prg mode Date: Thu, 24 Sep 2020 23:27:28 +0800 Message-Id: <20200924152730.733243-2-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_112825_130819_878F3D23 X-CRM114-Status: GOOD ( 21.19 ) 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:1041 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-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org "program" mode on this controller can trigger up to 56 bits of data shifting. During the operation, data in PRGDATA[0-5] will be shifted out from MOSI, and data from MISO will be continuously filling SHREG[0-9]. Currently this mode is used to implement transfer_one_message for 6-byte full-duplex transfer, but it can execute a transfer for up-to 7 bytes as long as the last byte is read only. transfer_one_message is expected to perform full-duplex transfer, instead of transfer with specific format. mtk_nor_spi_mem_prg is added here to use this extra byte. Newer version of this controller can trigger longer data shifting with shift bytes more than PRGDATA_MAX + SHREG_MAX. This patch is implemented with that in mind and it checks against both SHREG_MAX and PRG_CNT_MAX for future support of new controllers. Signed-off-by: Chuanhong Guo --- There are two calls of mtk_nor_spi_mem_prg in mtk_nor_exec_op, which doesn't look great. But I'd prefer keeping it this way to minimize change of mtk_nor_exec_op. I'll follow up with a cleanup patchset after this one gets merged. drivers/spi/spi-mtk-nor.c | 82 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi-mtk-nor.c b/drivers/spi/spi-mtk-nor.c index 62f5ff2779884..a0087a5e869b9 100644 --- a/drivers/spi/spi-mtk-nor.c +++ b/drivers/spi/spi-mtk-nor.c @@ -27,6 +27,7 @@ #define MTK_NOR_CMD_MASK GENMASK(5, 0) #define MTK_NOR_REG_PRG_CNT 0x04 +#define MTK_NOR_PRG_CNT_MAX 56 #define MTK_NOR_REG_RDATA 0x0c #define MTK_NOR_REG_RADR0 0x10 @@ -404,6 +405,83 @@ static int mtk_nor_pp_unbuffered(struct mtk_nor *sp, return mtk_nor_cmd_exec(sp, MTK_NOR_CMD_WRITE, 6 * BITS_PER_BYTE); } +static int mtk_nor_spi_mem_prg(struct mtk_nor *sp, const struct spi_mem_op *op) +{ + int rx_len = 0; + int reg_offset = MTK_NOR_REG_PRGDATA_MAX; + int tx_len, prg_len; + int i, ret; + void __iomem *reg; + u8 bufbyte; + + tx_len = op->cmd.nbytes + op->addr.nbytes; + + // count dummy bytes only if we need to write data after it + if (op->data.dir == SPI_MEM_DATA_OUT) + tx_len += op->dummy.nbytes + op->data.nbytes; + else if (op->data.dir == SPI_MEM_DATA_IN) + rx_len = op->data.nbytes; + + prg_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes + + op->data.nbytes; + + // an invalid op may reach here if the caller calls exec_op without + // adjust_op_size. return -EINVAL instead of -ENOTSUPP so that + // spi-mem won't try this op again with generic spi transfers. + if ((tx_len > MTK_NOR_REG_PRGDATA_MAX + 1) || + (rx_len > MTK_NOR_REG_SHIFT_MAX + 1) || + (prg_len > MTK_NOR_PRG_CNT_MAX / 8)) + return -EINVAL; + + // fill tx data + for (i = op->cmd.nbytes; i > 0; i--, reg_offset--) { + reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset); + bufbyte = (op->cmd.opcode >> ((i - 1) * BITS_PER_BYTE)) & 0xff; + writeb(bufbyte, reg); + } + + for (i = op->addr.nbytes; i > 0; i--, reg_offset--) { + reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset); + bufbyte = (op->addr.val >> ((i - 1) * BITS_PER_BYTE)) & 0xff; + writeb(bufbyte, reg); + } + + if (op->data.dir == SPI_MEM_DATA_OUT) { + for (i = 0; i < op->dummy.nbytes; i++, reg_offset--) { + reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset); + writeb(0, reg); + } + + for (i = 0; i < op->data.nbytes; i++, reg_offset--) { + reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset); + writeb(((const u8 *)(op->data.buf.out))[i], reg); + } + } + + for (; reg_offset >= 0; reg_offset--) { + reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset); + writeb(0, reg); + } + + // trigger op + writel(prg_len * BITS_PER_BYTE, sp->base + MTK_NOR_REG_PRG_CNT); + ret = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_PROGRAM, + prg_len * BITS_PER_BYTE); + if (ret) + return ret; + + // fetch read data + reg_offset = 0; + if (op->data.dir == SPI_MEM_DATA_IN) { + for (i = op->data.nbytes - 1; i >= 0; i--, reg_offset++) { + reg = sp->base + MTK_NOR_REG_SHIFT(reg_offset); + ((u8 *)(op->data.buf.in))[i] = readb(reg); + } + } + + return 0; +} + static int mtk_nor_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) { struct mtk_nor *sp = spi_controller_get_devdata(mem->spi->master); @@ -411,7 +489,7 @@ static int mtk_nor_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) if ((op->data.nbytes == 0) || ((op->addr.nbytes != 3) && (op->addr.nbytes != 4))) - return -ENOTSUPP; + return mtk_nor_spi_mem_prg(sp, op); if (op->data.dir == SPI_MEM_DATA_OUT) { mtk_nor_set_addr(sp, op); @@ -441,7 +519,7 @@ static int mtk_nor_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) } } - return -ENOTSUPP; + return mtk_nor_spi_mem_prg(sp, op); } static int mtk_nor_setup(struct spi_device *spi) 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: 11797729 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 3238D6CA for ; Thu, 24 Sep 2020 15:28:45 +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 D0A2021D91 for ; Thu, 24 Sep 2020 15:28:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="mT1V/5uu"; 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 D0A2021D91 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-mediatek-bounces+patchwork-linux-mediatek=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=KU1FJaBZs2NeQ0d1WoLy2yl52/dJCDYicK/bGdk1V1E=; b=mT1V/5uuM1WO6SgvHVsr+262E uzZrDma6hCrsAxsQz+jp9Ln0pZpVukYW2tx925lufJcha3o+6f6vtyarabmIxqIHUtwlZtQ/udeq6 5vkpkw4TQ7S838bt6GBwcxiFBynRNmszvZttBnPs8GLccwmizdHLe9/2hSSj/uyQAehJHG/jHDrXL UC1YzFJ2idkV/i/xL6C4kvKv2bZ/fSzuzsNRzuNP30vM1RKPzOFQRIYbBVYlvUpbdPvbm0Zg1hQr5 T8WsZQNClAXFM9kWUjoTcJwd1oSTDG46BHBhSZNHcRaMbCtnBkWAcKyzIs90lkxBv3kRVGUNUiSP0 hR+V7R3Gw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLTAn-0003JT-Rp; Thu, 24 Sep 2020 15:28:41 +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-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=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; } From patchwork Thu Sep 24 15:27:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 11797733 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 B08B86CB for ; Thu, 24 Sep 2020 15:28:57 +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 71E8321D91 for ; Thu, 24 Sep 2020 15:28:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="u+JFtWnc"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="knL//Jbb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 71E8321D91 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-mediatek-bounces+patchwork-linux-mediatek=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=GsumoIewsEQf47TAno/TJM0NHdxmH1bwP/pSMLee1qI=; b=u+JFtWncNhT7scGwPNhQ8j9iy IU8c7M7nkJAFncZOdzbtcWmstayVZMNxm32+2bR0m54l4+lLl/qpPiQJdAn1GfQ7I7Cs08TiqR/jY nwWTPGp1fC/iITbJFttXlvvtrWuu0oxQqlH1eUnMVPaezPb5YKviwc8HWvEvX6gpph3ZxtE7i+rDK +xTI+yPbHLSBIp8WiXOY8vZhkfo/EQY55Y9v4vF+dkuVLKCTZ5qS11urAwmq6TKVtNvuELaT+aMgc eWEA/MSeUrIPvdX4qUSlJzyYDJEqR847DERrRSWRaOQIHvpKQUN+wOoNSmYiVrT4kf2FeJOTQ0KxG PiqU2h0iQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLTB1-0003Pq-5h; Thu, 24 Sep 2020 15:28:55 +0000 Received: from mail-pj1-x1043.google.com ([2607:f8b0:4864:20::1043]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLTAd-0003B9-Az; Thu, 24 Sep 2020 15:28:32 +0000 Received: by mail-pj1-x1043.google.com with SMTP id mn7so1778312pjb.5; Thu, 24 Sep 2020 08:28:31 -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=Ydse9JJ+ftTyRWsvhcjm/YwUAWDIwbgK0aq4Xyc/pN8=; b=knL//JbbhIACLiQWUd5tja3co0hcd2If1nx/SGslZd13vEJI3NG9V0tLfyx38WXjOp d5sOdxkLd1CwWw6QFJ/IS6BcPPGHSd+0A5Z65VB/ZjlfR4LpbCKgqtJYiPdEu8ZD7Zdr 7b1y37RbdH1IzBFOsLRbb0JlFj7FquwrV9kheoYHfsiIhTRW+ZJGpJRl22lfWUacMhDu Wq1mng24/fMotHwEcTWgTNmeE3gSkuNwnhwfDTu8fYLhGj/oHn0LwWOhceG4t5JF2Y5r 5LEGswbVc8YoTsIAV2d5j1PFugoocmqh8NzgOdA8f6UdtLMpeKOW72S/OL1EbIoqEPkx 7MCQ== 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=Ydse9JJ+ftTyRWsvhcjm/YwUAWDIwbgK0aq4Xyc/pN8=; b=Y5gjBipBCc5omEO9yGxLwOJsLGd7xwN+b6i7PP7uj0WAxhe5hPGG+EEQqWYssyGu8Z KBshkdnSyK7kqMQlo/1lbxzeN5ZBindiRVOGCIhBPuUukBAoK3I9ZtjkMxfNE81rPGTX pzEBGGd93g7jr0LdZlhoLHu2WVBbpvjSzuflPRsc1CvA9hhKZnhhoQfh9r+7uSKmhKkG 4peyv577507IKaRf29WgO1CzFp7YAsaSKOrMZCBxc0cGmu/mWMlyISTRIydkpFvBaWHE 5eNFwTHYdeCqwQ8U25zwmsOHyKFvCETLDpIsdkfteZtAU/gxJJVPbCRyIyIbSCNAvyVI Rfsw== X-Gm-Message-State: AOAM533ZylvbyravTNjHiK49ebsKTq/drVu1CtZcWtY8iFac5jka3M+I 344xgWSu8Kl9E+0sPNfXqPQdgovMZEhMVA== X-Google-Smtp-Source: ABdhPJwj0O9VBAEbiiRMiUQP412SndgpBFZlx6tI3A6GsG2itd7F6Gc5r4f6bDJSVHSJD6gBN4IWrg== X-Received: by 2002:a17:90a:c255:: with SMTP id d21mr4647745pjx.212.1600961309445; Thu, 24 Sep 2020 08:28:29 -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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2020 08:28:29 -0700 (PDT) From: Chuanhong Guo To: linux-spi@vger.kernel.org Subject: [PATCH 3/3] spi: spi-mtk-nor: fix op checks in supports_op Date: Thu, 24 Sep 2020 23:27:30 +0800 Message-Id: <20200924152730.733243-4-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_112831_424378_94A7A4E0 X-CRM114-Status: GOOD ( 14.89 ) 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:1043 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-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org commit a59b2c7c56bf7 ("spi: spi-mtk-nor: support standard spi properties") tries to inverse the logic of supports_op when adding spi_mem_default_supports_op check, but it didn't get it done properly. There are two regressions introduced by this commit: 1. reading ops supported by program mode is rejected. 2. all ops with special controller routines are incorrectly further checked against program mode. This commits inverses the logic back: 1. check spi_mem_default_supports_op and reject unsupported ops first. 2. return true for ops with special controller routines. 3. check the left ops against controller program mode. Fixes: a59b2c7c56bf7 ("spi: spi-mtk-nor: support standard spi properties") Signed-off-by: Chuanhong Guo --- drivers/spi/spi-mtk-nor.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/drivers/spi/spi-mtk-nor.c b/drivers/spi/spi-mtk-nor.c index 4bbf38ef5b4b1..ea39736de2918 100644 --- a/drivers/spi/spi-mtk-nor.c +++ b/drivers/spi/spi-mtk-nor.c @@ -273,7 +273,8 @@ static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) static bool mtk_nor_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) { - size_t len; + if (!spi_mem_default_supports_op(mem, op)) + return false; if (op->cmd.buswidth != 1) return false; @@ -281,25 +282,21 @@ static bool mtk_nor_supports_op(struct spi_mem *mem, if ((op->addr.nbytes == 3) || (op->addr.nbytes == 4)) { switch(op->data.dir) { case SPI_MEM_DATA_IN: - if (!mtk_nor_match_read(op)) - return false; + if (mtk_nor_match_read(op)) + return true; break; case SPI_MEM_DATA_OUT: - if ((op->addr.buswidth != 1) || - (op->dummy.nbytes != 0) || - (op->data.buswidth != 1)) - return false; + if ((op->addr.buswidth == 1) && + (op->dummy.nbytes == 0) && + (op->data.buswidth == 1)) + return true; break; default: break; } } - len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; - if ((len > MTK_NOR_PRG_MAX_SIZE) || - ((op->data.nbytes) && (len == MTK_NOR_PRG_MAX_SIZE))) - return false; - return spi_mem_default_supports_op(mem, op); + return mtk_nor_match_prg(op); } static void mtk_nor_setup_bus(struct mtk_nor *sp, const struct spi_mem_op *op)