Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3114635ybf; Mon, 2 Mar 2020 23:22:26 -0800 (PST) X-Google-Smtp-Source: ADFU+vubDak6ri8vn2ZxgNubDybmGH3jr4lVxfXnK2Gi+vHPDh2gFe7EXWWT4UVlRTbFs+oYZZRa X-Received: by 2002:a05:6830:231d:: with SMTP id u29mr1419629ote.1.1583220146107; Mon, 02 Mar 2020 23:22:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583220146; cv=none; d=google.com; s=arc-20160816; b=TXy1J59q6X37LLI7/3mSCbGGBh2xOYyNfhQyAJkN0+qYGRjC0xVPu1B+9GVBb7iXNd 4IXTy7XiNjiGTW3ilnDkmkQZtNtJYZu+yb411crJD1Oh+S8jKGg8PKdku534Padx8mvv FZuZ/lRUYLKc16f2/Qw7JVSBhchD52onBm2zWrN7TCVQWNNHbdRS0z5BfnT5YAdrASRI uiAln03bGZ/PwJzHJ7WuidEgfX7SIHGCBwRW1cyvuUO6fG7+lfUTYtLgivzayAFBs0J6 dTsF3pAgyAiR7CZkbN8BDwH2Adu3WfY2zuJmd6GMWl9GTxmiYJxyenCjUiOzzr0HQ5EI FoOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=gv5oR/l7VlY+AcIYiwGNG3ntGu+HKnQ8ZyeeVu5XoJM=; b=1HjYWQ3UNCnvdL/lEZ7cLH2nLq4atHyupjcLBTiwdBnW7V8WhwKteowAYIO1pdEoNy 4y7JtU78cKC+RdbzLf1eIf28LHNJrAFiT41NYZovWKBYmpBoPQM8MEzxFmO0lB8wVUps DkqZxUe5wDxjnKaRVjarp1TmMEvweqiMNDa/bG0VpmzfDu7gMiF+WeWxOXpoyDuXhKNv RmqU07X1vyylhcCx3N1ylknr997dRc5sjx9FCAU0nWbCWNrHEWLtr3uKIK1geZREOi1v jtdc+su1S5fCpTpZJEAUhDpWHdd9otSiOogdZyOVDuXlqh+wOL13gpLG4ys/G68uksvj Wt5w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k128si8161298oih.135.2020.03.02.23.22.12; Mon, 02 Mar 2020 23:22:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727617AbgCCHVx (ORCPT + 99 others); Tue, 3 Mar 2020 02:21:53 -0500 Received: from twhmllg3.macronix.com ([211.75.127.131]:31761 "EHLO TWHMLLG3.macronix.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727600AbgCCHVw (ORCPT ); Tue, 3 Mar 2020 02:21:52 -0500 Received: from localhost.localdomain ([172.17.195.96]) by TWHMLLG3.macronix.com with ESMTP id 0237LRLB023026; Tue, 3 Mar 2020 15:21:30 +0800 (GMT-8) (envelope-from masonccyang@mxic.com.tw) From: Mason Yang To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com Cc: frieder.schrempf@kontron.de, tglx@linutronix.de, stefan@agner.ch, juliensu@mxic.com.tw, allison@lohutok.net, linux-kernel@vger.kernel.org, bbrezillon@kernel.org, rfontana@redhat.com, linux-mtd@lists.infradead.org, yuehaibing@huawei.com, s.hauer@pengutronix.de, Mason Yang Subject: [PATCH v3 2/4] mtd: rawnand: Add support Macronix Block Protection function Date: Tue, 3 Mar 2020 15:21:22 +0800 Message-Id: <1583220084-10890-3-git-send-email-masonccyang@mxic.com.tw> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1583220084-10890-1-git-send-email-masonccyang@mxic.com.tw> References: <1583220084-10890-1-git-send-email-masonccyang@mxic.com.tw> X-MAIL: TWHMLLG3.macronix.com 0237LRLB023026 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Macronix AC/AD series support using SET_FEATURES to change Block Portection and Unprotection. By GET_FEATURES operation to detect if block protection support. Signed-off-by: Mason Yang --- drivers/mtd/nand/raw/nand_macronix.c | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/drivers/mtd/nand/raw/nand_macronix.c b/drivers/mtd/nand/raw/nand_macronix.c index 3ff7ce0..a4cd12c 100644 --- a/drivers/mtd/nand/raw/nand_macronix.c +++ b/drivers/mtd/nand/raw/nand_macronix.c @@ -11,6 +11,10 @@ #define MACRONIX_READ_RETRY_BIT BIT(0) #define MACRONIX_NUM_READ_RETRY_MODES 6 +#define ONFI_FEATURE_ADDR_MXIC_PROTECTION 0xA0 +#define MXIC_BLOCK_PROTECTION_ALL_LOCK 0x38 +#define MXIC_BLOCK_PROTECTION_ALL_UNLOCK 0x0 + struct nand_onfi_vendor_macronix { u8 reserved; u8 reliability_func; @@ -91,6 +95,73 @@ static void macronix_nand_fix_broken_get_timings(struct nand_chip *chip) ONFI_FEATURE_ADDR_TIMING_MODE, 1); } +/* + * Macronix NAND supports Block Protection by Protectoin(PT) pin; + * active high at power-on which protects the entire chip even the #WP is + * disabled. Lock/unlock protection area can be partition according to + * protection bits, i.e. upper 1/2 locked, upper 1/4 locked and so on. + */ +static int mxic_nand_lock(struct nand_chip *chip, loff_t ofs, uint64_t len) +{ + u8 feature[ONFI_SUBFEATURE_PARAM_LEN]; + int ret; + + feature[0] = MXIC_BLOCK_PROTECTION_ALL_LOCK; + nand_select_target(chip, 0); + ret = nand_set_features(chip, ONFI_FEATURE_ADDR_MXIC_PROTECTION, + feature); + nand_deselect_target(chip); + if (ret) + pr_err("%s all blocks failed\n", __func__); + + return ret; +} + +static int mxic_nand_unlock(struct nand_chip *chip, loff_t ofs, uint64_t len) +{ + u8 feature[ONFI_SUBFEATURE_PARAM_LEN]; + int ret; + + feature[0] = MXIC_BLOCK_PROTECTION_ALL_UNLOCK; + nand_select_target(chip, 0); + ret = nand_set_features(chip, ONFI_FEATURE_ADDR_MXIC_PROTECTION, + feature); + nand_deselect_target(chip); + if (ret) + pr_err("%s all blocks failed\n", __func__); + + return ret; +} + +static void macronix_nand_block_protection_support(struct nand_chip *chip) +{ + u8 feature[ONFI_SUBFEATURE_PARAM_LEN]; + int ret; + + bitmap_set(chip->parameters.get_feature_list, + ONFI_FEATURE_ADDR_MXIC_PROTECTION, 1); + + feature[0] = MXIC_BLOCK_PROTECTION_ALL_UNLOCK; + nand_select_target(chip, 0); + ret = nand_get_features(chip, ONFI_FEATURE_ADDR_MXIC_PROTECTION, + feature); + nand_deselect_target(chip); + if (ret || feature[0] != MXIC_BLOCK_PROTECTION_ALL_LOCK) { + if (ret) + pr_err("Block protection check failed\n"); + + bitmap_clear(chip->parameters.get_feature_list, + ONFI_FEATURE_ADDR_MXIC_PROTECTION, 1); + return; + } + + bitmap_set(chip->parameters.set_feature_list, + ONFI_FEATURE_ADDR_MXIC_PROTECTION, 1); + + chip->lock_area = mxic_nand_lock; + chip->unlock_area = mxic_nand_unlock; +} + static int macronix_nand_init(struct nand_chip *chip) { if (nand_is_slc(chip)) @@ -98,6 +169,7 @@ static int macronix_nand_init(struct nand_chip *chip) macronix_nand_fix_broken_get_timings(chip); macronix_nand_onfi_init(chip); + macronix_nand_block_protection_support(chip); return 0; } -- 1.9.1