From patchwork Thu May 21 11:23:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yicong Yang X-Patchwork-Id: 1295142 X-Patchwork-Delegate: tudor.ambarus@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hisilicon.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=WCEh8EGi; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49SS2453zRz9sPF for ; Thu, 21 May 2020 21:24:28 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.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=wTh/dmczDWCmfDLOIz2EeHpF9/OKB7q8VftkHcExQ1Q=; b=WCEh8EGigsxiP3 2qVSPsjm/uZjZzy5H7J0TSjX+K95SgkhGp3bBdG9tDP06289+7Fro+EC30J6jYWzGTEfbBmKbygnn X8V4khXGmZBdqU53wkYy0YKBt9JHB8XVd8tYD4EwMZdttbykx3xUSYIgNtuC524BTCgGqzD1ICmW7 0vDJR34HWi0gCPMwQLjDnseUUdhrh2ATTC8YoE6a+FBuAhOsEJAH0QooGlxaScFG0+ml1PWmrPf6O YIWhMsgQjgU08QWHSCARpdAhGKyEd1kJGoyLTxn8jee3On2cfa38UCALX+l/s50fDbJ7cimupa+EP K63d7SomruBWIz7ZqGYQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jbjJK-0003o4-04; Thu, 21 May 2020 11:24:26 +0000 Received: from szxga07-in.huawei.com ([45.249.212.35] helo=huawei.com) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jbjJG-0003mx-OL for linux-mtd@lists.infradead.org; Thu, 21 May 2020 11:24:24 +0000 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id F079B301C4E315FE73B4; Thu, 21 May 2020 19:24:17 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.487.0; Thu, 21 May 2020 19:24:07 +0800 From: Yicong Yang To: , , , Subject: [RFC PATCH 1/3] spi: spi-mem: add optional prepare/unprepare methods in spi_controller_mem_ops Date: Thu, 21 May 2020 19:23:49 +0800 Message-ID: <1590060231-23242-2-git-send-email-yangyicong@hisilicon.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1590060231-23242-1-git-send-email-yangyicong@hisilicon.com> References: <1590060231-23242-1-git-send-email-yangyicong@hisilicon.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200521_042422_961534_B56AE3E3 X-CRM114-Status: GOOD ( 12.37 ) X-Spam-Score: -2.1 (--) X-Spam-Report: SpamAssassin version 3.4.4 on bombadil.infradead.org summary: Content analysis details: (-2.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [45.249.212.35 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: richard@nod.at, john.garry@huawei.com, vigneshr@ti.com, miquel.raynal@bootlin.com Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Some prepare/unprepare works may be necessary before/after a set of operations of the spimem device. For example, before/after spi-nor flash's read/write/erase/lock/unlock. Add optional prepare/unprepare methods in spi_controller_mem_ops to allow the controller to do specific works after/before the operations. The upper user can use spi_mem_{prepare, unprepare}() to call the methods of the controller. Signed-off-by: Yicong Yang --- drivers/spi/spi-mem.c | 20 ++++++++++++++++++++ include/linux/spi/spi-mem.h | 11 +++++++++++ 2 files changed, 31 insertions(+) diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c index adaa0c4..f09cef1 100644 --- a/drivers/spi/spi-mem.c +++ b/drivers/spi/spi-mem.c @@ -220,6 +220,26 @@ bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) } EXPORT_SYMBOL_GPL(spi_mem_supports_op); +int spi_mem_prepare(struct spi_mem *mem) +{ + struct spi_controller *ctlr = mem->spi->controller; + + if (ctlr->mem_ops && ctlr->mem_ops->prepare) + return ctlr->mem_ops->prepare(mem); + + return 0; +} +EXPORT_SYMBOL_GPL(spi_mem_prepare); + +void spi_mem_unprepare(struct spi_mem *mem) +{ + struct spi_controller *ctlr = mem->spi->controller; + + if (ctlr->mem_ops && ctlr->mem_ops->unprepare) + ctlr->mem_ops->unprepare(mem); +} +EXPORT_SYMBOL_GPL(spi_mem_unprepare); + static int spi_mem_access_start(struct spi_mem *mem) { struct spi_controller *ctlr = mem->spi->controller; diff --git a/include/linux/spi/spi-mem.h b/include/linux/spi/spi-mem.h index af9ff2f..e40b5c3 100644 --- a/include/linux/spi/spi-mem.h +++ b/include/linux/spi/spi-mem.h @@ -215,6 +215,12 @@ static inline void *spi_mem_get_drvdata(struct spi_mem *mem) * limitations) * @supports_op: check if an operation is supported by the controller * @exec_op: execute a SPI memory operation + * @prepare: do some prepare works before a set of operations. for example, + * read/write/erase/lock/unlock operations of spi-nor flash. This + * method is optional. + * @unprepare: do some post works after a set of operations. for example, + * read/write/erase/lock/unlock operations of spi-nor flash. This + * method is optional. * @get_name: get a custom name for the SPI mem device from the controller. * This might be needed if the controller driver has been ported * to use the SPI mem layer and a custom name is used to keep @@ -253,6 +259,8 @@ struct spi_controller_mem_ops { int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op); bool (*supports_op)(struct spi_mem *mem, const struct spi_mem_op *op); + int (*prepare)(struct spi_mem *mem); + void (*unprepare)(struct spi_mem *mem); int (*exec_op)(struct spi_mem *mem, const struct spi_mem_op *op); const char *(*get_name)(struct spi_mem *mem); @@ -329,6 +337,9 @@ int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op); bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op); +int spi_mem_prepare(struct spi_mem *mem); +void spi_mem_unprepare(struct spi_mem *mem); + int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op); From patchwork Thu May 21 11:23:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yicong Yang X-Patchwork-Id: 1295145 X-Patchwork-Delegate: tudor.ambarus@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hisilicon.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=T97DJOhB; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49SS2z0FRkz9sPF for ; Thu, 21 May 2020 21:25:15 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.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=w6mLEIhD1XJDTiny5X2FzzVQAB/n5bteyAmDhQAsMFo=; b=T97DJOhBtPDh0+ 6kyChh+y58jVTu1LsBd7J4g7R0MYVlMT114DlPdOgig//hlcoEZvrP34cO9f2JLVFG5SW0l/kSw9p 6alz6KKHHHYIDPj9CbqDIVO1Dvx2i2dSBY+O9awvPcYy2IVNvXs0KxRDDnJZBfzdz8WK8/RHvEkO7 Vb3NtgOt10z5tLQZxcm2e0h6tpOvpWAawtIMUTcGyhmk+GT22RBTi5yoafWnlL8pQJx1oe7Vpyo2T Y0JHDRD2XM84Ln6DnTqNcEQOxiiA8QVSWi65ztDb7n17qr8ACxcH5Hv9cL5wic5gUeSKPa4sx8UDX RlRiZ86uKQpid065RLrQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jbjK0-00051f-LA; Thu, 21 May 2020 11:25:08 +0000 Received: from szxga07-in.huawei.com ([45.249.212.35] helo=huawei.com) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jbjJL-0003mz-PL for linux-mtd@lists.infradead.org; Thu, 21 May 2020 11:24:29 +0000 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 0389779028A765082C68; Thu, 21 May 2020 19:24:18 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.487.0; Thu, 21 May 2020 19:24:07 +0800 From: Yicong Yang To: , , , Subject: [RFC PATCH 2/3] mtd: spi-nor: Add prepare/unprepare support for spimem device Date: Thu, 21 May 2020 19:23:50 +0800 Message-ID: <1590060231-23242-3-git-send-email-yangyicong@hisilicon.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1590060231-23242-1-git-send-email-yangyicong@hisilicon.com> References: <1590060231-23242-1-git-send-email-yangyicong@hisilicon.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200521_042428_023519_D4CB61D8 X-CRM114-Status: GOOD ( 11.57 ) X-Spam-Score: -2.1 (--) X-Spam-Report: SpamAssassin version 3.4.4 on bombadil.infradead.org summary: Content analysis details: (-2.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [45.249.212.35 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: richard@nod.at, john.garry@huawei.com, vigneshr@ti.com, miquel.raynal@bootlin.com Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org spi-nor flash's read/write/erase/lock/unlock may be composed of a set of operations, and some prepare/unprepare works need to be done before/ after these operations in spi_nor_{lock, unlock}_and_{prep, unprep}(). Previously we only call spi-nor controllers' prepare/unprepare method in the functions, without spimem devices'. Add spimem devices' prepare/unprepare support. Call spi_mem_{prepare, unprepare}() function if it's a spimem device. Signed-off-by: Yicong Yang --- drivers/mtd/spi-nor/core.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c index cc68ea8..3a7e40a 100644 --- a/drivers/mtd/spi-nor/core.c +++ b/drivers/mtd/spi-nor/core.c @@ -1103,7 +1103,9 @@ int spi_nor_lock_and_prep(struct spi_nor *nor) mutex_lock(&nor->lock); - if (nor->controller_ops && nor->controller_ops->prepare) { + if (nor->spimem) { + ret = spi_mem_prepare(nor->spimem); + } else if (nor->controller_ops && nor->controller_ops->prepare) { ret = nor->controller_ops->prepare(nor); if (ret) { mutex_unlock(&nor->lock); @@ -1115,7 +1117,9 @@ int spi_nor_lock_and_prep(struct spi_nor *nor) void spi_nor_unlock_and_unprep(struct spi_nor *nor) { - if (nor->controller_ops && nor->controller_ops->unprepare) + if (nor->spimem) + spi_mem_unprepare(nor->spimem); + else if (nor->controller_ops && nor->controller_ops->unprepare) nor->controller_ops->unprepare(nor); mutex_unlock(&nor->lock); } From patchwork Thu May 21 11:23:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yicong Yang X-Patchwork-Id: 1295143 X-Patchwork-Delegate: tudor.ambarus@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hisilicon.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=IAlKo9nP; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49SS2Q615sz9sPF for ; Thu, 21 May 2020 21:24:46 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.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=448jzB8vZ0VExs/7Q0EBQpMsTESM2sew8PGU2icfdCo=; b=IAlKo9nPrrPIl5 EloMCS2OLE78PDWI1R2betI2+XiDT7kfs5AMF0fhWev1v6Q0szcdn1gUKAD1PdYNdU1begeU7U8YZ 94XvSmDS29R6fLKgb5dJ7YUBqoxVjLHjzEu0A2PKurf3vrCS38uKljoGQ0xmmjM3qD+wgMtl//nxY TjiWxKm6t61kEx2UuZpUjPvsR8Ox9FXfES0C6uOFxKLojQTrOdv2ANlhgQQbft2Jo6O/SVv6BMVGN MFrPugb7d/UnHtl7/Icc/nzqdK2WnK0AkRqKSe2sQO8k8yc8Kq0qmi8A0IDAXt2C42tJKoT0f77mY 8CRa6xLBXS3tPGi+wdfQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jbjJT-0003xC-LX; Thu, 21 May 2020 11:24:35 +0000 Received: from szxga07-in.huawei.com ([45.249.212.35] helo=huawei.com) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jbjJG-0003my-Oy for linux-mtd@lists.infradead.org; Thu, 21 May 2020 11:24:24 +0000 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 09BDBD8DE7A7168C8D15; Thu, 21 May 2020 19:24:18 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.487.0; Thu, 21 May 2020 19:24:08 +0800 From: Yicong Yang To: , , , Subject: [RFC PATCH 3/3] spi: hisi-sfc-v3xx: Add prepare/unprepare methods to avoid race condition Date: Thu, 21 May 2020 19:23:51 +0800 Message-ID: <1590060231-23242-4-git-send-email-yangyicong@hisilicon.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1590060231-23242-1-git-send-email-yangyicong@hisilicon.com> References: <1590060231-23242-1-git-send-email-yangyicong@hisilicon.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200521_042422_976800_1894B243 X-CRM114-Status: GOOD ( 16.10 ) X-Spam-Score: -2.1 (--) X-Spam-Report: SpamAssassin version 3.4.4 on bombadil.infradead.org summary: Content analysis details: (-2.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [45.249.212.35 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: richard@nod.at, john.garry@huawei.com, vigneshr@ti.com, miquel.raynal@bootlin.com Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The controller can be shared with the firmware, which may cause race problems. As most read/write/erase/lock/unlock of spi-nor flash are composed of a set of operations, while the firmware may use the controller and start its own operation in the middle of the process started by the kernel driver, which may lead to the kernel driver's function broken. Bit[20] in HISI_SFC_V3XX_CMD_CFG register plays a role of a lock, to protect the controller from firmware access, which means the firmware cannot reach the controller if the driver set the bit. Add prepare/ unprepare methods for the controller, we'll hold the lock in prepare method and release it in unprepare method, which will solve the race issue. Signed-off-by: Yicong Yang --- drivers/spi/spi-hisi-sfc-v3xx.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi-hisi-sfc-v3xx.c b/drivers/spi/spi-hisi-sfc-v3xx.c index e3b5725..13c161c 100644 --- a/drivers/spi/spi-hisi-sfc-v3xx.c +++ b/drivers/spi/spi-hisi-sfc-v3xx.c @@ -18,6 +18,7 @@ #define HISI_SFC_V3XX_VERSION (0x1f8) #define HISI_SFC_V3XX_CMD_CFG (0x300) +#define HISI_SFC_V3XX_CMD_CFG_LOCK BIT(20) #define HISI_SFC_V3XX_CMD_CFG_DUAL_IN_DUAL_OUT (1 << 17) #define HISI_SFC_V3XX_CMD_CFG_DUAL_IO (2 << 17) #define HISI_SFC_V3XX_CMD_CFG_FULL_DIO (3 << 17) @@ -41,6 +42,34 @@ struct hisi_sfc_v3xx_host { int max_cmd_dword; }; +int hisi_sfc_v3xx_op_prepare(struct spi_mem *mem) +{ + struct spi_device *spi = mem->spi; + struct hisi_sfc_v3xx_host *host; + u32 reg = HISI_SFC_V3XX_CMD_CFG_LOCK; + + host = spi_controller_get_devdata(spi->master); + + writel(reg, host->regbase + HISI_SFC_V3XX_CMD_CFG); + + reg = readl(host->regbase + HISI_SFC_V3XX_CMD_CFG); + if (!(reg & HISI_SFC_V3XX_CMD_CFG_LOCK)) + return -EIO; + + return 0; +} + +void hisi_sfc_v3xx_op_unprepare(struct spi_mem *mem) +{ + struct spi_device *spi = mem->spi; + struct hisi_sfc_v3xx_host *host; + + host = spi_controller_get_devdata(spi->master); + + /* Release the lock and clear the command register. */ + writel(0, host->regbase + HISI_SFC_V3XX_CMD_CFG); +} + #define HISI_SFC_V3XX_WAIT_TIMEOUT_US 1000000 #define HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US 10 @@ -163,7 +192,15 @@ static int hisi_sfc_v3xx_generic_exec_op(struct hisi_sfc_v3xx_host *host, u8 chip_select) { int ret, len = op->data.nbytes; - u32 config = 0; + u32 config; + + /* + * The lock bit is in the command register. Clear the command + * field with lock bit held if it has been set in + * .prepare(). + */ + config = readl(host->regbase + HISI_SFC_V3XX_CMD_CFG); + config &= HISI_SFC_V3XX_CMD_CFG_LOCK; if (op->addr.nbytes) config |= HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK; @@ -248,6 +285,8 @@ static int hisi_sfc_v3xx_exec_op(struct spi_mem *mem, static const struct spi_controller_mem_ops hisi_sfc_v3xx_mem_ops = { .adjust_op_size = hisi_sfc_v3xx_adjust_op_size, + .prepare = hisi_sfc_v3xx_op_prepare, + .unprepare = hisi_sfc_v3xx_op_unprepare, .exec_op = hisi_sfc_v3xx_exec_op, };