From patchwork Fri Feb 24 20:16:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kamal Dasu X-Patchwork-Id: 732290 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vVMrl1q9wz9s7v for ; Sat, 25 Feb 2017 07:19:19 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Nabrbap9"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="YgPDxU00"; dkim-atps=neutral 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:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: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=Taod9G90u7gMv+7wD1yWKmtxiyDCvMWa7JuZwyqskIc=; b=Nabrbap9FGgFjDJR+yVQ0VFx+l ncwQr5fTCT0BGeXPYspjjzDnav0YtpqRIWR1jTrgcmzxCHYe+6axyULB6uVA56ZGn3LTnQUhcPUck G41M1RZ5pXt4iD0fVX/OL3M+EdcaDrTH/54ktJ8tKEKMZFd4NhN5QPvXlknDe0gbHj7+D9T0yl8Oz U6NCLLtVfsRA1P1LhJ77hO1PRiqCQPrGw1JsUr1VeiKTB1FLOjSkec/RpF+1uDu/hF0etOnlv1Uyx UKWa/ry+BQdnbz98vU/V4tnxhioQ5T6YYtgblY/2YaHan/3cRTvkKDNBmRG5eJlar4+oJq77lJg/c tyWwoEkQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1chMKe-000326-PC; Fri, 24 Feb 2017 20:19:12 +0000 Received: from mail-qk0-x243.google.com ([2607:f8b0:400d:c09::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1chMJ6-0001q0-2h for linux-mtd@lists.infradead.org; Fri, 24 Feb 2017 20:17:38 +0000 Received: by mail-qk0-x243.google.com with SMTP id x71so4280627qkb.0 for ; Fri, 24 Feb 2017 12:17:15 -0800 (PST) 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; bh=N7/RsmZbOHDDO2It02Cx9ErT35LMtdFo/Bvazp4XajM=; b=YgPDxU00YOFnfsRsd1ME9PbpKryxjHF3nq9ezXwe3wGub5omyWpOXAtzmpW5mn4wVq 61k+PSYGLgn8NW3O3xdCY1/6BaarcLXaUkdtF2A5TZU9SuCQySUbOrmUtJbqM4Jsac0t bwiQAJcnBWtO0X0Olqo9Hj4XtKipoN9Y+iWJjGAuIo9GxzP21GFiLpMg22clj1QFb5rp MPVWAEAt2XtiSfKW3rZQ5CYYYeCSPTwF7DjlpjVEbEyD+3fqBfjvAz68OWrSLEQiDU/F ioos8teHKUj0HJ/3vAq5w1Z9BhBJ+5FWBh5XaSvYun8eFBlzSPMET5L44xT2Riay7fTY cBug== 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; bh=N7/RsmZbOHDDO2It02Cx9ErT35LMtdFo/Bvazp4XajM=; b=VS+1MJsBr6Qfiz2ZUmxoNeLl0LtI+ag3mEsWyRmH/zq3bZNm8Z7aLOfxU7M1coC2lP jHvm9MDUrLi2Fvd9uoaPUSVGbZUOVkJ0xUOxsChc1j/H6yPB/BzFxtLZd7YUVsZXyHkC eHLPNagAS/HwltWG/AWfdM+a+MgNCUTiGCFcsY51ObHeEsN1qKlJTLVX4G0oN+98NRK8 +JS9+wRSxj3hPcklhN9hkqatfVjYOnl50LO9mnLo12tLgAjFyVYCLPj5nVjnVhhD46ZF u+Y1Z9Lb+q6WaO8JeLGJIJ/1mkEnKNouwUdEmFX/rwiIlH7ORd44Vbmj77kcFMSTUrRy JS4A== X-Gm-Message-State: AMke39mViYagkFy9mBPdOswqxQG5CPAz2bRzLFQt9FR5K7xyFQnqcl0In7+NbptmHh+n/w== X-Received: by 10.55.25.147 with SMTP id 19mr4429745qkz.147.1487967434818; Fri, 24 Feb 2017 12:17:14 -0800 (PST) Received: from mail.broadcom.com ([192.19.218.250]) by smtp.gmail.com with ESMTPSA id o190sm5418808qkc.65.2017.02.24.12.17.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 24 Feb 2017 12:17:14 -0800 (PST) From: Kamal Dasu To: linux-spi@vger.kernel.org, cyrille.pitchen@atmel.com, marex@denx.de, broonie@kernel.org Subject: [PATCH v6 2/3] mtd: spi-nor: Add spi-nor flash device synchronization between flash states Date: Fri, 24 Feb 2017 15:16:38 -0500 Message-Id: <1487967399-28967-3-git-send-email-kdasu.kdev@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1487967399-28967-1-git-send-email-kdasu.kdev@gmail.com> References: <1487967399-28967-1-git-send-email-kdasu.kdev@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170224_121736_240397_348155A0 X-CRM114-Status: GOOD ( 15.85 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2607:f8b0:400d:c09:0:0:0:243 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (kdasu.kdev[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bcm-kernel-feedback-list@broadcom.com, f.fainelli@gmail.com, linux-mtd@lists.infradead.org, Kamal Dasu MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Added flash access synchronization methods spi_nor_get/release_device(). This change allows spi-nor driver to maintain flash states in spi-nor stucture for read, write, erase, lock, unlock nor ops. Only when the flash state is FL_READY a new state is set and spi-nor flash op is initiated. The state change is done with a spin_lock held and released as soon as the state is changed. Else the current process for spi-nor transfer is queued till the flash is in FL_READY state. This change allows us to add mtd layer synchronization when the mtd resume suspend handlers are added. Signed-off-by: Kamal Dasu --- drivers/mtd/spi-nor/spi-nor.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 4 +++ 2 files changed, 73 insertions(+) diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c index 8b71c11..5363807 100644 --- a/drivers/mtd/spi-nor/spi-nor.c +++ b/drivers/mtd/spi-nor/spi-nor.c @@ -89,6 +89,15 @@ struct flash_info { #define JEDEC_MFR(info) ((info)->id[0]) +/* map table for spi_nor op to flashchip state */ +static int spi_nor_state[] = { + [SPI_NOR_OPS_READ] = FL_READING, + [SPI_NOR_OPS_WRITE] = FL_WRITING, + [SPI_NOR_OPS_ERASE] = FL_ERASING, + [SPI_NOR_OPS_LOCK] = FL_LOCKING, + [SPI_NOR_OPS_UNLOCK] = FL_UNLOCKING, +}; + static const struct flash_info *spi_nor_match_id(const char *name); /* @@ -396,10 +405,64 @@ static int erase_chip(struct spi_nor *nor) return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0); } +/** + * spi_nor_get_device - [GENERIC] Get chip for selected access + * @param mtd MTD device structure + * @param new_state the state which is requested + * + * Get the nor flash device and lock it for exclusive access + */ +static int spi_nor_get_device(struct mtd_info *mtd, int new_state) +{ + struct spi_nor *nor = mtd_to_spi_nor(mtd); + DECLARE_WAITQUEUE(wait, current); + + /* + * Grab the lock and see if the device is available + */ + while (1) { + spin_lock(&nor->chip_lock); + if (nor->state == FL_READY) { + nor->state = new_state; + spin_unlock(&nor->chip_lock); + break; + } + if (new_state == FL_PM_SUSPENDED) { + spin_unlock(&nor->chip_lock); + return (nor->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN; + } + set_current_state(TASK_UNINTERRUPTIBLE); + add_wait_queue(&nor->wq, &wait); + spin_unlock(&nor->chip_lock); + schedule(); + remove_wait_queue(&nor->wq, &wait); + } + + return 0; +} + +/** + * spi_nor_release_device - [GENERIC] release chip + * @mtd: MTD device structure + * + * Release nor flash chip lock and wake up anyone waiting on the device. + */ +static void spi_nor_release_device(struct mtd_info *mtd) +{ + struct spi_nor *nor = mtd_to_spi_nor(mtd); + + /* Release the controller and the chip */ + spin_lock(&nor->chip_lock); + nor->state = FL_READY; + wake_up(&nor->wq); + spin_unlock(&nor->chip_lock); +} + static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops) { int ret = 0; + spi_nor_get_device(&nor->mtd, spi_nor_state[ops]); mutex_lock(&nor->lock); if (nor->prepare) { @@ -407,6 +470,7 @@ static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops) if (ret) { dev_err(nor->dev, "failed in the preparation.\n"); mutex_unlock(&nor->lock); + spi_nor_release_device(&nor->mtd); return ret; } } @@ -418,6 +482,7 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops) if (nor->unprepare) nor->unprepare(nor, ops); mutex_unlock(&nor->lock); + spi_nor_release_device(&nor->mtd); } /* @@ -1743,6 +1808,10 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode) return ret; } + nor->state = FL_READY; + init_waitqueue_head(&nor->wq); + spin_lock_init(&nor->chip_lock); + /* * call init function to send necessary spi-nor read/write config * commands to nor flash based on above nor settings diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h index 29a8283..244d98d 100644 --- a/include/linux/mtd/spi-nor.h +++ b/include/linux/mtd/spi-nor.h @@ -13,6 +13,7 @@ #include #include #include +#include /* * Manufacturer IDs @@ -210,6 +211,9 @@ struct spi_nor { void *priv; const struct flash_info *info; + spinlock_t chip_lock; + wait_queue_head_t wq; + flstate_t state; }; static inline void spi_nor_set_flash_node(struct spi_nor *nor,