mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sky Huang <SkyLake.Huang@mediatek.com>
To: Matthias Brugger <matthias.bgg@gmail.com>,
	Miquel Raynal <miquel.raynal@bootlin.com>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	Daniel Golle <daniel@makrotopia.org>,
	Chia-Lin Kao <acelan.kao@canonical.com>,
	"Mika Westerberg" <mika.westerberg@linux.intel.com>,
	Cheng Ming Lin <chengminglin@mxic.com.tw>,
	<linux-kernel@vger.kernel.org>, <linux-mtd@lists.infradead.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>
Cc: Steven Liu <Steven.Liu@mediatek.com>,
	Sky.Huang <skylake.huang@mediatek.com>
Subject: [RFC PATCH nand/next 1/4] mtd: nand: Create param.c to do CRC check and bitwise majority for Parameter & CASN page
Date: Sun, 20 Oct 2024 21:27:19 +0800	[thread overview]
Message-ID: <20241020132722.20565-2-SkyLake.Huang@mediatek.com> (raw)
In-Reply-To: <20241020132722.20565-1-SkyLake.Huang@mediatek.com>

From: "Sky.Huang" <skylake.huang@mediatek.com>

Create drivers/mtd/nand/param.c so ONFI parameter page & CASN page
can both use nanddev_crc16() and nanddev_bit_wise_majority() directly
like this:
* For ONFI Parameter page:
onfi_crc16() -> nanddev_crc16()
* For CASN page: nanddev_crc16()

nanddev_bit_wise_majority() is same as nand_bit_wise_majority().
nanddev_crc16() is same as onfi_crc16(). But there are lots of
onfi_crc16() call, so keep onfi_crc16() there and hook it to
nanddev_crc16().

Signed-off-by: Sky Huang <skylake.huang@mediatek.com>
---
 drivers/mtd/nand/Makefile        |  2 +-
 drivers/mtd/nand/param.c         | 52 ++++++++++++++++++++++++++++++++
 drivers/mtd/nand/raw/nand_onfi.c | 43 ++------------------------
 include/linux/mtd/param.h        | 20 ++++++++++++
 4 files changed, 76 insertions(+), 41 deletions(-)
 create mode 100644 drivers/mtd/nand/param.c
 create mode 100644 include/linux/mtd/param.h

diff --git a/drivers/mtd/nand/Makefile b/drivers/mtd/nand/Makefile
index 19e1291ac4d5..790bde0148d1 100644
--- a/drivers/mtd/nand/Makefile
+++ b/drivers/mtd/nand/Makefile
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-nandcore-objs := core.o bbt.o
+nandcore-objs := core.o bbt.o param.o
 obj-$(CONFIG_MTD_NAND_CORE) += nandcore.o
 obj-$(CONFIG_MTD_NAND_ECC_MEDIATEK) += ecc-mtk.o
 
diff --git a/drivers/mtd/nand/param.c b/drivers/mtd/nand/param.c
new file mode 100644
index 000000000000..f67b9fe633d9
--- /dev/null
+++ b/drivers/mtd/nand/param.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2023 - Mediatek
+ *
+ * Author: Sky Huang <SkyLake.Huang@mediatek.com>
+ */
+
+#include <linux/mtd/param.h>
+
+u16 nanddev_crc16(u16 crc, u8 const *p, size_t len)
+{
+	int i;
+
+	while (len--) {
+		crc ^= *p++ << 8;
+		for (i = 0; i < 8; i++)
+			crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
+	}
+
+	return crc;
+}
+
+/*
+ * Recover data with bit-wise majority
+ */
+void nanddev_bit_wise_majority(const void **srcbufs,
+				   unsigned int nsrcbufs,
+				   void *dstbuf,
+				   unsigned int bufsize)
+{
+	int i, j, k;
+
+	for (i = 0; i < bufsize; i++) {
+		u8 val = 0;
+
+		for (j = 0; j < 8; j++) {
+			unsigned int cnt = 0;
+
+			for (k = 0; k < nsrcbufs; k++) {
+				const u8 *srcbuf = srcbufs[k];
+
+				if (srcbuf[i] & BIT(j))
+					cnt++;
+			}
+
+			if (cnt > nsrcbufs / 2)
+				val |= BIT(j);
+		}
+
+		((u8 *)dstbuf)[i] = val;
+	}
+}
diff --git a/drivers/mtd/nand/raw/nand_onfi.c b/drivers/mtd/nand/raw/nand_onfi.c
index 861975e44b55..5d330dd53e8f 100644
--- a/drivers/mtd/nand/raw/nand_onfi.c
+++ b/drivers/mtd/nand/raw/nand_onfi.c
@@ -12,6 +12,7 @@
  * This file contains all ONFI helpers.
  */
 
+#include <linux/mtd/param.h>
 #include <linux/slab.h>
 
 #include "internals.h"
@@ -20,14 +21,7 @@
 
 u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
 {
-	int i;
-	while (len--) {
-		crc ^= *p++ << 8;
-		for (i = 0; i < 8; i++)
-			crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
-	}
-
-	return crc;
+	return nanddev_crc16(crc, p, len);
 }
 
 /* Parse the Extended Parameter Page. */
@@ -107,37 +101,6 @@ static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
 	return ret;
 }
 
-/*
- * Recover data with bit-wise majority
- */
-static void nand_bit_wise_majority(const void **srcbufs,
-				   unsigned int nsrcbufs,
-				   void *dstbuf,
-				   unsigned int bufsize)
-{
-	int i, j, k;
-
-	for (i = 0; i < bufsize; i++) {
-		u8 val = 0;
-
-		for (j = 0; j < 8; j++) {
-			unsigned int cnt = 0;
-
-			for (k = 0; k < nsrcbufs; k++) {
-				const u8 *srcbuf = srcbufs[k];
-
-				if (srcbuf[i] & BIT(j))
-					cnt++;
-			}
-
-			if (cnt > nsrcbufs / 2)
-				val |= BIT(j);
-		}
-
-		((u8 *)dstbuf)[i] = val;
-	}
-}
-
 /*
  * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  */
@@ -200,7 +163,7 @@ int nand_onfi_detect(struct nand_chip *chip)
 			srcbufs[j] = pbuf + j;
 
 		pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
-		nand_bit_wise_majority(srcbufs, ONFI_PARAM_PAGES, pbuf,
+		nanddev_bit_wise_majority(srcbufs, ONFI_PARAM_PAGES, pbuf,
 				       sizeof(*pbuf));
 
 		crc = onfi_crc16(ONFI_CRC_BASE, (u8 *)pbuf, 254);
diff --git a/include/linux/mtd/param.h b/include/linux/mtd/param.h
new file mode 100644
index 000000000000..39636f66f1b4
--- /dev/null
+++ b/include/linux/mtd/param.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2023 - Mediatek
+ *
+ * Author: Sky Huang <SkyLake.Huang@mediatek.com>
+ */
+
+#ifndef __LINUX_NAND_PARAM
+#define __LINUX_NAND_PARAM
+
+#include <linux/io.h>
+
+u16 nanddev_crc16(u16 crc, u8 const *p, size_t len);
+void nanddev_bit_wise_majority(const void **srcbufs,
+				   unsigned int nsrcbufs,
+				   void *dstbuf,
+				   unsigned int bufsize);
+
+#endif /* __LINUX_NAND_PARAM */
+
-- 
2.45.2


  reply	other threads:[~2024-10-20 13:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-20 13:27 [RFC PATCH nand/next 0/4] mtd: nand: spi: Add CASN page support Sky Huang
2024-10-20 13:27 ` Sky Huang [this message]
2024-11-18 10:55   ` [RFC PATCH nand/next 1/4] mtd: nand: Create param.c to do CRC check and bitwise majority for Parameter & CASN page Miquel Raynal
2024-10-20 13:27 ` [RFC PATCH nand/next 2/4] include: mtd: Add CASN page definition and related structs Sky Huang
2024-11-18 10:58   ` Miquel Raynal
2024-10-20 13:27 ` [RFC PATCH nand/next 3/4] include: mtd: spinand: Add CASN page related macros and flags Sky Huang
2024-11-18 13:06   ` Miquel Raynal
2024-10-20 13:27 ` [RFC PATCH nand/next 4/4] mtd: nand: spi: Add CASN page support Sky Huang
2024-10-28 15:25 ` [RFC PATCH nand/next 0/4] " SkyLake Huang (黃啟澤)
2024-11-06 15:20   ` Miquel Raynal
2024-11-18 10:53 ` Miquel Raynal
2024-11-20  7:24   ` SkyLake Huang (黃啟澤)
2024-11-20 10:13     ` Miquel Raynal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241020132722.20565-2-SkyLake.Huang@mediatek.com \
    --to=skylake.huang@mediatek.com \
    --cc=Steven.Liu@mediatek.com \
    --cc=acelan.kao@canonical.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=chengminglin@mxic.com.tw \
    --cc=daniel@makrotopia.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=mika.westerberg@linux.intel.com \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=vigneshr@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®