From patchwork Thu Jul 30 17:34:53 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Brezillon X-Patchwork-Id: 502228 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DAD97140297 for ; Fri, 31 Jul 2015 03:37:19 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZKrjv-0006fl-NG; Thu, 30 Jul 2015 17:35:31 +0000 Received: from down.free-electrons.com ([37.187.137.238] helo=mail.free-electrons.com) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZKrjm-0005NN-8i for linux-mtd@lists.infradead.org; Thu, 30 Jul 2015 17:35:24 +0000 Received: by mail.free-electrons.com (Postfix, from userid 106) id 9D5F438A; Thu, 30 Jul 2015 19:35:01 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.free-electrons.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT, URIBL_BLOCKED shortcircuit=ham autolearn=disabled version=3.4.0 Received: from localhost.localdomain (AToulouse-657-1-1085-64.w92-146.abo.wanadoo.fr [92.146.163.64]) by mail.free-electrons.com (Postfix) with ESMTPSA id 4AD026F7; Thu, 30 Jul 2015 19:35:01 +0200 (CEST) From: Boris Brezillon To: David Woodhouse , Brian Norris , linux-mtd@lists.infradead.org Subject: [RFC PATCH 1/2] mtd: nand: add nand_check_erased helper functions Date: Thu, 30 Jul 2015 19:34:53 +0200 Message-Id: <1438277694-23763-2-git-send-email-boris.brezillon@free-electrons.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1438277694-23763-1-git-send-email-boris.brezillon@free-electrons.com> References: <1438277694-23763-1-git-send-email-boris.brezillon@free-electrons.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150730_103522_694895_5D48B467 X-CRM114-Status: GOOD ( 17.31 ) X-Spam-Score: -4.0 (----) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-4.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [37.187.137.238 listed in list.dnswl.org] -1.4 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Boris Brezillon , linux-kernel@vger.kernel.org MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add two helper functions to help NAND controller drivers test whether a specific NAND region is erased or not. Signed-off-by: Boris Brezillon --- drivers/mtd/nand/nand_base.c | 104 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/nand.h | 8 ++++ 2 files changed, 112 insertions(+) diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c index ceb68ca..1542ea7 100644 --- a/drivers/mtd/nand/nand_base.c +++ b/drivers/mtd/nand/nand_base.c @@ -1101,6 +1101,110 @@ out: EXPORT_SYMBOL(nand_lock); /** + * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data + * @buf: buffer to test + * @len: buffer length + * @bitflips_threshold:maximum number of bitflips + * + * Check if a buffer contains only 0xff, which means the underlying region + * has been erased and is ready to be programmed. + * The bitflips_threshold specify the maximum number of bitflips before + * considering the region is not erased. + * Note: The logic of this function has been extracted from the memweight + * implementation, except that nand_check_erased_buf function exit before + * testing the whole buffer if the number of bitflips exceed the + * bitflips_threshold value. + * + * Returns a positive number of bitflips or -ERROR_CODE. + */ +int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) +{ + const unsigned char *bitmap = buf; + int bitflips = 0; + int weight; + int longs; + + for (; len && ((unsigned long)bitmap) % sizeof(long); + len--, bitmap++) { + weight = hweight8(*bitmap); + + bitflips += sizeof(u8) - weight; + if (bitflips > bitflips_threshold) + return -EINVAL; + } + + + for (longs = len / sizeof(long); longs; + longs--, bitmap += sizeof(long)) { + BUG_ON(longs >= INT_MAX / BITS_PER_LONG); + weight = hweight_long(*((unsigned long *)bitmap)); + + bitflips += sizeof(long) - weight; + if (bitflips > bitflips_threshold) + return -EINVAL; + } + + len %= sizeof(long); + + for (; len > 0; len--, bitmap++) { + weight = hweight8(*bitmap); + bitflips += sizeof(u8) - weight; + } + + return bitflips; +} +EXPORT_SYMBOL(nand_check_erased_buf); + +/** + * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only + * 0xff data + * @data: data buffer to test + * @datalen: data length + * @ecc: ECC buffer + * @ecclen: ECC length + * @extraoob: extra OOB buffer + * @extraooblen: extra OOB length + * @bitflips_threshold: maximum number of bitflips + * + * Check if a data buffer and its associated ECC and OOB data contains only + * 0xff pattern, which means the underlying region has been erased and is + * ready to be programmed. + * The bitflips_threshold specify the maximum number of bitflips before + * considering the region as not erased. + * + * Returns a positive number of bitflips or -ERROR_CODE. + */ +int nand_check_erased_ecc_chunk(void *data, int datalen, + void *ecc, int ecclen, + void *extraoob, int extraooblen, + int bitflips_threshold) +{ + int bitflips = 0; + int ret; + + ret = nand_check_erased_buf(data, datalen, bitflips_threshold); + if (ret < 0) + return ret; + + bitflips += ret; + bitflips_threshold -= ret; + + ret = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); + if (ret < 0) + return ret; + + bitflips += ret; + bitflips_threshold -= ret; + + ret = nand_check_erased_buf(extraoob, extraooblen, bitflips_threshold); + if (ret < 0) + return ret; + + return bitflips + ret; +} +EXPORT_SYMBOL(nand_check_erased_ecc_chunk); + +/** * nand_read_page_raw - [INTERN] read raw page data without ecc * @mtd: mtd info structure * @chip: nand chip info structure diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h index 272f429..ae06a07 100644 --- a/include/linux/mtd/nand.h +++ b/include/linux/mtd/nand.h @@ -1030,4 +1030,12 @@ struct nand_sdr_timings { /* get timing characteristics from ONFI timing mode. */ const struct nand_sdr_timings *onfi_async_timing_mode_to_sdr_timings(int mode); + +int nand_check_erased_buf(void *data, int datalen, + int threshold); + +int nand_check_erased_ecc_chunk(void *data, int datalen, + void *ecc, int ecclen, + void *extraoob, int extraooblen, + int threshold); #endif /* __LINUX_MTD_NAND_H */