From patchwork Wed Jun 12 10:47:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagar Shrikant Kadam X-Patchwork-Id: 10989387 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EAA5E1395 for ; Wed, 12 Jun 2019 10:49:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D600228426 for ; Wed, 12 Jun 2019 10:49:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C94AC28949; Wed, 12 Jun 2019 10:49:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3243E28426 for ; Wed, 12 Jun 2019 10:49:39 +0000 (UTC) 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=uk9eR2XbozUCXgE6XfrAtd91A6KIbvvEns/R8K1TGRg=; b=fJzl3GBWYPx+Bie9gKWdk2VlNv 3P9ga2R128cY3u4LetS8thfPxh0vKKdEHW3hCnopMpyyGnecy8LCxdmU0HPhCPIamZ3t5QtLMRHy8 /cUr5cVo15Pt8VM2a0DjaY3gTyLC44U+kjcVdWGJX6iDL1+MdQGin11Ezs1aeGYSfPe0W1RAVEjRL 4rYL9bEMs4MnFAt1II3SMLBrat+pU13PuDlWCJO2aaF7O0hfpCizjDhCMeYPj6UCoNG0MNI5H03xJ wfbns1hyWDB3C5/gHbmtqyiUaMRjNGNxs8Xh8BBhGuMk/UpCXDgS+PiNsWzD6BO5iN9T+7GJylTn9 HgMiUeIg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hb0ow-0001CX-Bi; Wed, 12 Jun 2019 10:49:34 +0000 Received: from mail-pf1-x443.google.com ([2607:f8b0:4864:20::443]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hb0ob-0000p7-Js for linux-riscv@lists.infradead.org; Wed, 12 Jun 2019 10:49:15 +0000 Received: by mail-pf1-x443.google.com with SMTP id i189so9427353pfg.10 for ; Wed, 12 Jun 2019 03:49:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/34VUba2TMn1HUJYz+e67ilwN8q7isgKAmoXZ4qRiqs=; b=SC/wtDlWHVohDk7cxk5knZoolyQZl4Clqn455YPX3TFOuVKH5GRuCtgk3p6HL+y8jn b+keSwdI+s29Sse4jokvis6wMRIfvVT0nh7ABPa6t1aMvDSdLkexGNnD+j9irzPHXud5 mXH820BF/AhKZIJ8f8JFX1tOayUZBgudZUu6F9jLp6An15h3NMu2RgXBMjjgrj0dzVE+ jI0yzEL+/aQu0+z0MXvYsxxXEs0Kr3/VIp5zaMWvsgVIK046vdZVePonBYmVst2ug4iH JDJCWMFbeaAou4tAl8U+yJ0W9eO5ruf4/AJYDr+Mo6IdiPCHaIm2kjWrXud2/P76YHmh KGDg== 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=/34VUba2TMn1HUJYz+e67ilwN8q7isgKAmoXZ4qRiqs=; b=chCU595hddNqsMs+5q8UFk+l3jErAluyv62u2CvgIGy1RR5H+mLVFmWGOeZu20hVCm G+U2Ie+YVGTsutXGOvqrcMIoef+2OT2gu/mgDQB1FOIj/G2/v1vaTi1HurgWiDI9224t JlV8FMoUzAdx2S+tUOX4Q2fLKrR1PxsFpytOYzFFT73piAoqRc7Eed6+K6RcX+HuISCM dqeB63Qr2I1QFwuoujSDbIA1AtLSoq6+ZFiJex9EbO7iZ1A4wDam5Tjn0wh1PTGxqYFW 97pN4kwFeErpB8NGTnjU5r1YfsmGDgyVWUgE0iPvLVfpWTorKL290RcNmyFt/AyPHA4m mr4w== X-Gm-Message-State: APjAAAViB4oDIbRVKK8MjOkQrPn+S31VLArCZxbF4cnwM6MkQpYi3XSZ IQAc2abzVvc4QXLivfEQt4GiAg== X-Google-Smtp-Source: APXvYqyyIMxcvMkdOmgbGdxqwUCfj1i8ToNEidoIVG/3vovKkuMaa+3pJ9drMOOqrY0fD0slF0BNZg== X-Received: by 2002:a62:5c84:: with SMTP id q126mr59382546pfb.247.1560336553124; Wed, 12 Jun 2019 03:49:13 -0700 (PDT) Received: from buildserver-90.open-silicon.com ([114.143.65.226]) by smtp.googlemail.com with ESMTPSA id y22sm12241561pfm.70.2019.06.12.03.49.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 12 Jun 2019 03:49:12 -0700 (PDT) From: Sagar Shrikant Kadam To: marek.vasut@gmail.com, tudor.ambarus@microchip.com, dwmw2@infradead.org, computersforpeace@gmail.com, miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com, linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH v5 2/3] mtd: spi-nor: add support to unlock flash device Date: Wed, 12 Jun 2019 16:17:55 +0530 Message-Id: <1560336476-31763-3-git-send-email-sagar.kadam@sifive.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1560336476-31763-1-git-send-email-sagar.kadam@sifive.com> References: <1560336476-31763-1-git-send-email-sagar.kadam@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190612_034913_673261_9D375547 X-CRM114-Status: GOOD ( 16.84 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: wesley@sifive.com, palmer@sifive.com, Sagar Shrikant Kadam , aou@eecs.berkeley.edu, paul.walmsley@sifive.com MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Nor device (is25wp256 mounted on HiFive unleashed Rev A00 board) from ISSI have memory blocks guarded by block protection bits BP[0,1,2,3]. Clearing block protection bits,unlocks the flash memory regions The unlock scheme is registered during nor scans. Based on code developed by Wesley Terpstra and/or Palmer Dabbelt . https://github.com/riscv/riscv-linux/commit/c94e267766d62bc9a669611c3d0c8ed5ea26569b Signed-off-by: Sagar Shrikant Kadam --- drivers/mtd/spi-nor/spi-nor.c | 51 ++++++++++++++++++++++++++++++++++++++++++- include/linux/mtd/spi-nor.h | 1 + 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c index 2d5a925..b7c6261 100644 --- a/drivers/mtd/spi-nor/spi-nor.c +++ b/drivers/mtd/spi-nor/spi-nor.c @@ -1461,6 +1461,49 @@ static int macronix_quad_enable(struct spi_nor *nor) } /** + * issi_unlock() - clear BP[0123] write-protection. + * @nor: pointer to a 'struct spi_nor'. + * @ofs: offset from which to unlock memory. + * @len: number of bytes to unlock. + * + * Bits [2345] of the Status Register are BP[0123]. + * ISSI chips use a different block protection scheme than other chips. + * Just disable the write-protect unilaterally. + * + * Return: 0 on success, -errno otherwise. + */ +static int issi_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len) +{ + int ret, val; + u8 mask = SR_BP0 | SR_BP1 | SR_BP2 | SR_BP3; + + val = read_sr(nor); + if (val < 0) + return val; + if (!(val & mask)) + return 0; + + write_enable(nor); + + write_sr(nor, val & ~mask); + + ret = spi_nor_wait_till_ready(nor); + if (ret) + return ret; + + ret = read_sr(nor); + if (ret > 0 && !(ret & mask)) { + dev_info(nor->dev, + "ISSI Block Protection Bits cleared SR=0x%x", ret); + ret = 0; + } else { + dev_err(nor->dev, "ISSI Block Protection Bits not cleared\n"); + ret = -EINVAL; + } + return ret; +} + +/** * spansion_quad_enable() - set QE bit in Configuraiton Register. * @nor: pointer to a 'struct spi_nor' * @@ -1836,7 +1879,7 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor) SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) }, { "is25wp256", INFO(0x9d7019, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | - SPI_NOR_4B_OPCODES) + SPI_NOR_4B_OPCODES | SPI_NOR_HAS_LOCK) }, /* Macronix */ @@ -4080,6 +4123,12 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, nor->flash_is_locked = stm_is_locked; } + /* NOR protection support for ISSI chips */ + if (JEDEC_MFR(info) == SNOR_MFR_ISSI || + info->flags & SPI_NOR_HAS_LOCK) { + nor->flash_unlock = issi_unlock; + + } if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) { mtd->_lock = spi_nor_lock; mtd->_unlock = spi_nor_unlock; diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h index ff13297..9a7d719 100644 --- a/include/linux/mtd/spi-nor.h +++ b/include/linux/mtd/spi-nor.h @@ -127,6 +127,7 @@ #define SR_BP0 BIT(2) /* Block protect 0 */ #define SR_BP1 BIT(3) /* Block protect 1 */ #define SR_BP2 BIT(4) /* Block protect 2 */ +#define SR_BP3 BIT(5) /* Block protect 3 for ISSI device*/ #define SR_TB BIT(5) /* Top/Bottom protect */ #define SR_SRWD BIT(7) /* SR write protect */ /* Spansion/Cypress specific status bits */