linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	Tudor Ambarus <Tudor.Ambarus@microchip.com>,
	<linux-mtd@lists.infradead.org>
Cc: Julien Su <juliensu@mxic.com.tw>,
	ycllin@mxic.com.tw,
	Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	Miquel Raynal <miquel.raynal@bootlin.com>
Subject: [PATCH 07/20] mtd: nand: ecc-bch: Update the prototypes to be more generic
Date: Wed, 30 Sep 2020 01:01:11 +0200	[thread overview]
Message-ID: <20200929230124.31491-8-miquel.raynal@bootlin.com> (raw)
In-Reply-To: <20200929230124.31491-1-miquel.raynal@bootlin.com>

These functions must be usable by the main NAND core, so their names
must be technology-agnostic as well as the parameters. Hence, we pass
a generic nand_device instead of a raw nand_chip structure.

As it seems that changing the raw NAND functions to always pass a
generic NAND device is a lost of time, we prefer to create dedicated
raw NAND wrappers that will be useful in the near future to do the
translation.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/ecc-sw-bch.c       | 47 +++++++++++++++++------------
 drivers/mtd/nand/raw/nand_base.c    | 44 ++++++++++++++++++++++++---
 drivers/mtd/nand/raw/omap2.c        | 23 ++++++--------
 include/linux/mtd/nand-ecc-sw-bch.h | 45 ++++++++++-----------------
 include/linux/mtd/rawnand.h         |  5 +++
 5 files changed, 97 insertions(+), 67 deletions(-)

diff --git a/drivers/mtd/nand/ecc-sw-bch.c b/drivers/mtd/nand/ecc-sw-bch.c
index 97221ec3876e..b9bdfcf46c6d 100644
--- a/drivers/mtd/nand/ecc-sw-bch.c
+++ b/drivers/mtd/nand/ecc-sw-bch.c
@@ -13,6 +13,7 @@
 #include <linux/bitops.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/rawnand.h>
+#include <linux/mtd/nand.h>
 #include <linux/mtd/nand-ecc-sw-bch.h>
 #include <linux/bch.h>
 
@@ -29,14 +30,15 @@ struct nand_bch_control {
 };
 
 /**
- * nand_bch_calcuate_ecc - Calculate the ECC corresponding to a data block
- * @chip: NAND chip object
+ * nand_ecc_sw_bch_calculate - Calculate the ECC corresponding to a data block
+ * @nand: NAND device
  * @buf: Input buffer with raw data
  * @code: Output buffer with ECC
  */
-int nand_bch_calculate_ecc(struct nand_chip *chip, const unsigned char *buf,
-			   unsigned char *code)
+int nand_ecc_sw_bch_calculate(struct nand_device *nand,
+			      const unsigned char *buf, unsigned char *code)
 {
+	struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
 	struct nand_bch_control *nbc = chip->ecc.priv;
 	unsigned int i;
 
@@ -49,20 +51,21 @@ int nand_bch_calculate_ecc(struct nand_chip *chip, const unsigned char *buf,
 
 	return 0;
 }
-EXPORT_SYMBOL(nand_bch_calculate_ecc);
+EXPORT_SYMBOL(nand_ecc_sw_bch_calculate);
 
 /**
- * nand_bch_correct_data - Detect, correct and report bit error(s)
- * @chip: NAND chip object
+ * nand_ecc_sw_bch_correct - Detect, correct and report bit error(s)
+ * @nand: NAND device
  * @buf: Raw data read from the chip
  * @read_ecc: ECC bytes from the chip
  * @calc_ecc: ECC calculated from the raw data
  *
  * Detect and correct bit errors for a data block.
  */
-int nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf,
-			  unsigned char *read_ecc, unsigned char *calc_ecc)
+int nand_ecc_sw_bch_correct(struct nand_device *nand, unsigned char *buf,
+			    unsigned char *read_ecc, unsigned char *calc_ecc)
 {
+	struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
 	struct nand_bch_control *nbc = chip->ecc.priv;
 	unsigned int *errloc = nbc->errloc;
 	int i, count;
@@ -86,11 +89,11 @@ int nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf,
 
 	return count;
 }
-EXPORT_SYMBOL(nand_bch_correct_data);
+EXPORT_SYMBOL(nand_ecc_sw_bch_correct);
 
 /**
- * nand_bch_init - Initialize software BCH ECC engine
- * @chip: NAND chip object
+ * nand_ecc_sw_bch_init - Initialize software BCH ECC engine
+ * @nand: NAND device
  *
  * Returns: a pointer to a new NAND BCH control structure, or NULL upon failure
  *
@@ -105,9 +108,10 @@ EXPORT_SYMBOL(nand_bch_correct_data);
  * @eccsize = 512 (thus, m = 13 is the smallest integer such that 2^m - 1 > 512 * 8)
  * @eccbytes = 7 (7 bytes are required to store m * t = 13 * 4 = 52 bits)
  */
-int nand_bch_init(struct nand_chip *chip)
+int nand_ecc_sw_bch_init(struct nand_device *nand)
 {
-	struct mtd_info *mtd = nand_to_mtd(chip);
+	struct mtd_info *mtd = nanddev_to_mtd(nand);
+	struct nand_chip *chip = mtd_to_nand(mtd);
 	unsigned int m, t, eccsteps, i;
 	struct nand_bch_control *nbc = NULL;
 	unsigned char *erased_page;
@@ -197,18 +201,21 @@ int nand_bch_init(struct nand_chip *chip)
 		chip->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
 
 	return 0;
+
 fail:
-	nand_bch_free(chip);
+	nand_ecc_sw_bch_cleanup(nand);
+
 	return -EINVAL;
 }
-EXPORT_SYMBOL(nand_bch_init);
+EXPORT_SYMBOL(nand_ecc_sw_bch_init);
 
 /**
- * nand_bch_free - Release NAND BCH ECC resources
- * @nbc: NAND BCH control structure
+ * nand_ecc_sw_bch_cleanup - Cleanup software BCH ECC resources
+ * @nand: NAND device
  */
-void nand_bch_free(struct nand_chip *chip)
+void nand_ecc_sw_bch_cleanup(struct nand_device *nand)
 {
+	struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
 	struct nand_bch_control *nbc = chip->ecc.priv;
 
 	if (nbc) {
@@ -218,7 +225,7 @@ void nand_bch_free(struct nand_chip *chip)
 		kfree(nbc);
 	}
 }
-EXPORT_SYMBOL(nand_bch_free);
+EXPORT_SYMBOL(nand_ecc_sw_bch_cleanup);
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index a6b6ab26a22a..69bdbb1c3b60 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -5144,6 +5144,42 @@ static void nand_scan_ident_cleanup(struct nand_chip *chip)
 	kfree(chip->parameters.onfi);
 }
 
+int rawnand_sw_bch_init(struct nand_chip *chip)
+{
+	struct nand_device *base = &chip->base;
+
+	return nand_ecc_sw_bch_init(base);
+}
+EXPORT_SYMBOL(rawnand_sw_bch_init);
+
+static int rawnand_sw_bch_calculate(struct nand_chip *chip,
+				    const unsigned char *buf,
+				    unsigned char *code)
+{
+	struct nand_device *base = &chip->base;
+
+	return nand_ecc_sw_bch_calculate(base, buf, code);
+}
+
+int rawnand_sw_bch_correct(struct nand_chip *chip, unsigned char *buf,
+			   unsigned char *read_ecc, unsigned char *calc_ecc)
+{
+	struct nand_device *base = &chip->base;
+
+	return nand_ecc_sw_bch_correct(base, buf, read_ecc, calc_ecc);
+}
+EXPORT_SYMBOL(rawnand_sw_bch_correct);
+
+void rawnand_sw_bch_cleanup(struct nand_chip *chip)
+{
+	struct nand_device *base = &chip->base;
+
+	nand_ecc_sw_bch_cleanup(base);
+
+	chip->ecc.priv = NULL;
+}
+EXPORT_SYMBOL(rawnand_sw_bch_cleanup);
+
 static int nand_set_ecc_on_host_ops(struct nand_chip *chip)
 {
 	struct nand_ecc_ctrl *ecc = &chip->ecc;
@@ -5240,8 +5276,8 @@ static int nand_set_ecc_soft_ops(struct nand_chip *chip)
 			WARN(1, "CONFIG_MTD_NAND_ECC_SW_BCH not enabled\n");
 			return -EINVAL;
 		}
-		ecc->calculate = nand_bch_calculate_ecc;
-		ecc->correct = nand_bch_correct_data;
+		ecc->calculate = rawnand_sw_bch_calculate;
+		ecc->correct = rawnand_sw_bch_correct;
 		ecc->read_page = nand_read_page_swecc;
 		ecc->read_subpage = nand_read_subpage;
 		ecc->write_page = nand_write_page_swecc;
@@ -5297,7 +5333,7 @@ static int nand_set_ecc_soft_ops(struct nand_chip *chip)
 
 		/* See the software BCH ECC initialization for details */
 		ecc->bytes = 0;
-		ret = nand_bch_init(chip);
+		ret = rawnand_sw_bch_init(chip);
 		if (ret) {
 			WARN(1, "BCH ECC initialization failed!\n");
 			return ret;
@@ -5962,7 +5998,7 @@ void nand_cleanup(struct nand_chip *chip)
 {
 	if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
 	    chip->ecc.algo == NAND_ECC_ALGO_BCH)
-		nand_bch_free(chip);
+		rawnand_sw_bch_cleanup(chip);
 
 	nanddev_cleanup(&chip->base);
 
diff --git a/drivers/mtd/nand/raw/omap2.c b/drivers/mtd/nand/raw/omap2.c
index 6aab57336690..4cc47ab7f01a 100644
--- a/drivers/mtd/nand/raw/omap2.c
+++ b/drivers/mtd/nand/raw/omap2.c
@@ -23,7 +23,6 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 
-#include <linux/mtd/nand-ecc-sw-bch.h>
 #include <linux/platform_data/elm.h>
 
 #include <linux/omap-gpmc.h>
@@ -2041,13 +2040,13 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
 		chip->ecc.bytes		= 7;
 		chip->ecc.strength	= 4;
 		chip->ecc.hwctl		= omap_enable_hwecc_bch;
-		chip->ecc.correct	= nand_bch_correct_data;
+		chip->ecc.correct	= rawnand_sw_bch_correct;
 		chip->ecc.calculate	= omap_calculate_ecc_bch_sw;
 		mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
 		/* Reserve one byte for the OMAP marker */
 		oobbytes_per_step	= chip->ecc.bytes + 1;
 		/* Software BCH library is used for locating errors */
-		err = nand_bch_init(chip);
+		err = rawnand_sw_bch_init(chip);
 		if (err) {
 			dev_err(dev, "Unable to use BCH library\n");
 			return err;
@@ -2083,13 +2082,13 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
 		chip->ecc.bytes		= 13;
 		chip->ecc.strength	= 8;
 		chip->ecc.hwctl		= omap_enable_hwecc_bch;
-		chip->ecc.correct	= nand_bch_correct_data;
+		chip->ecc.correct	= rawnand_sw_bch_correct;
 		chip->ecc.calculate	= omap_calculate_ecc_bch_sw;
 		mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
 		/* Reserve one byte for the OMAP marker */
 		oobbytes_per_step	= chip->ecc.bytes + 1;
 		/* Software BCH library is used for locating errors */
-		err = nand_bch_init(chip);
+		err = rawnand_sw_bch_init(chip);
 		if (err) {
 			dev_err(dev, "unable to use BCH library\n");
 			return err;
@@ -2195,7 +2194,6 @@ static int omap_nand_probe(struct platform_device *pdev)
 	nand_chip		= &info->nand;
 	mtd			= nand_to_mtd(nand_chip);
 	mtd->dev.parent		= &pdev->dev;
-	nand_chip->ecc.priv	= NULL;
 	nand_set_flash_node(nand_chip, dev->of_node);
 
 	if (!mtd->name) {
@@ -2271,10 +2269,9 @@ static int omap_nand_probe(struct platform_device *pdev)
 return_error:
 	if (!IS_ERR_OR_NULL(info->dma))
 		dma_release_channel(info->dma);
-	if (nand_chip->ecc.priv) {
-		nand_bch_free(nand_chip);
-		nand_chip->ecc.priv = NULL;
-	}
+
+	rawnand_sw_bch_cleanup(nand_chip);
+
 	return err;
 }
 
@@ -2285,10 +2282,8 @@ static int omap_nand_remove(struct platform_device *pdev)
 	struct omap_nand_info *info = mtd_to_omap(mtd);
 	int ret;
 
-	if (nand_chip->ecc.priv) {
-		nand_bch_free(nand_chip);
-		nand_chip->ecc.priv = NULL;
-	}
+	rawnand_sw_bch_cleanup(nand_chip);
+
 	if (info->dma)
 		dma_release_channel(info->dma);
 	ret = mtd_device_unregister(mtd);
diff --git a/include/linux/mtd/nand-ecc-sw-bch.h b/include/linux/mtd/nand-ecc-sw-bch.h
index 7b62996d9e01..f0caee3b03d0 100644
--- a/include/linux/mtd/nand-ecc-sw-bch.h
+++ b/include/linux/mtd/nand-ecc-sw-bch.h
@@ -8,53 +8,40 @@
 #ifndef __MTD_NAND_ECC_SW_BCH_H__
 #define __MTD_NAND_ECC_SW_BCH_H__
 
-struct mtd_info;
-struct nand_chip;
+#include <linux/mtd/nand.h>
 
 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)
 
-/*
- * Calculate BCH ecc code
- */
-int nand_bch_calculate_ecc(struct nand_chip *chip, const u_char *dat,
-			   u_char *ecc_code);
-
-/*
- * Detect and correct bit errors
- */
-int nand_bch_correct_data(struct nand_chip *chip, u_char *dat,
-			  u_char *read_ecc, u_char *calc_ecc);
-/*
- * Initialize BCH encoder/decoder
- */
-int nand_bch_init(struct nand_chip *chip);
-/*
- * Release BCH encoder/decoder resources
- */
-void nand_bch_free(struct nand_chip *chip);
+int nand_ecc_sw_bch_calculate(struct nand_device *nand,
+			      const unsigned char *buf, unsigned char *code);
+int nand_ecc_sw_bch_correct(struct nand_device *nand, unsigned char *buf,
+			    unsigned char *read_ecc, unsigned char *calc_ecc);
+int nand_ecc_sw_bch_init(struct nand_device *nand);
+void nand_ecc_sw_bch_cleanup(struct nand_device *nand);
 
 #else /* !CONFIG_MTD_NAND_ECC_SW_BCH */
 
-static inline int
-nand_bch_calculate_ecc(struct nand_chip *chip, const u_char *dat,
-		       u_char *ecc_code)
+static inline int nand_ecc_sw_bch_calculate(struct nand_device *nand,
+					    const unsigned char *buf,
+					    unsigned char *code)
 {
 	return -ENOTSUPP;
 }
 
-static inline int
-nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf,
-		      unsigned char *read_ecc, unsigned char *calc_ecc)
+static inline int nand_ecc_sw_bch_correct(struct nand_device *nand,
+					  unsigned char *buf,
+					  unsigned char *read_ecc,
+					  unsigned char *calc_ecc)
 {
 	return -ENOTSUPP;
 }
 
-static inline int nand_bch_init(struct nand_chip *chip)
+static inline int nand_ecc_sw_bch_init(struct nand_device *nand)
 {
 	return -ENOTSUPP;
 }
 
-static inline void nand_bch_free(struct nand_chip *chip) {}
+static inline void nand_ecc_sw_bch_cleanup(struct nand_device *nand) {}
 
 #endif /* CONFIG_MTD_NAND_ECC_SW_BCH */
 
diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h
index aac07940de09..23623beaad1d 100644
--- a/include/linux/mtd/rawnand.h
+++ b/include/linux/mtd/rawnand.h
@@ -1303,6 +1303,11 @@ static inline int nand_opcode_8bits(unsigned int command)
 	return 0;
 }
 
+int rawnand_sw_bch_init(struct nand_chip *chip);
+int rawnand_sw_bch_correct(struct nand_chip *chip, unsigned char *buf,
+			   unsigned char *read_ecc, unsigned char *calc_ecc);
+void rawnand_sw_bch_cleanup(struct nand_chip *chip);
+
 int nand_check_erased_ecc_chunk(void *data, int datalen,
 				void *ecc, int ecclen,
 				void *extraoob, int extraooblen,
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  parent reply	other threads:[~2020-09-29 23:03 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-29 23:01 [PATCH 00/20] Create generic software ECC engines Miquel Raynal
2020-09-29 23:01 ` [PATCH 01/20] mtd: nand: ecc: Add an I/O request tweaking mechanism Miquel Raynal
2020-09-30  7:53   ` Thomas Petazzoni
2020-09-30  8:16     ` Miquel Raynal
2020-10-30 17:30   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 02/20] mtd: nand: ecc-bch: Move BCH code to the generic NAND layer Miquel Raynal
2020-10-30 17:30   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 03/20] mtd: nand: ecc-bch: Cleanup and style fixes Miquel Raynal
2020-10-30 17:30   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 04/20] mtd: nand: ecc-bch: Stop exporting the private structure Miquel Raynal
2021-01-09 14:46   ` Adam Ford
2021-01-11 10:20     ` Miquel Raynal
2021-01-12 14:35       ` Miquel Raynal
2021-01-12 16:01         ` Adam Ford
2021-01-12 17:20           ` Adam Ford
2021-01-14 15:42             ` Miquel Raynal
2021-01-15 12:23               ` Adam Ford
2021-01-15 16:06                 ` Adam Ford
2021-01-15 16:17                   ` Miquel Raynal
2021-01-15 16:28                     ` Adam Ford
2021-01-19 11:56                       ` Miquel Raynal
2021-01-19 14:21                         ` Adam Ford
2021-01-19 14:36                           ` Miquel Raynal
2021-01-19 15:49                             ` Adam Ford
2021-01-19 15:53                               ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 05/20] mtd: nand: ecc-bch: Return only valid error codes Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 06/20] mtd: nand: ecc-bch: Drop mtd_nand_has_bch() Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` Miquel Raynal [this message]
2020-10-30 17:29   ` [PATCH 07/20] mtd: nand: ecc-bch: Update the prototypes to be more generic Miquel Raynal
2020-09-29 23:01 ` [PATCH 08/20] mtd: nand: ecc-bch: Stop using raw NAND structures Miquel Raynal
2020-09-29 23:01 ` [PATCH 09/20] mtd: nand: ecc-bch: Create the software BCH engine Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 10/20] mtd: rawnand: Get rid of chip->ecc.priv Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 11/20] mtd: nand: ecc-hamming: Move Hamming code to the generic NAND layer Miquel Raynal
2020-09-29 23:01 ` [PATCH 12/20] mtd: nand: ecc-hamming: Clarify the driver descriptions Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 13/20] mtd: nand: ecc-hamming: Drop/fix the kernel doc Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 14/20] mtd: nand: ecc-hamming: Cleanup and style fixes Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 15/20] mtd: nand: ecc-hamming: Rename the exported functions Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 16/20] mtd: nand: ecc-hamming: Stop using raw NAND structures Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 17/20] mtd: nand: ecc-hamming: Remove useless includes Miquel Raynal
2020-09-29 23:01 ` [PATCH 18/20] mtd: nand: ecc-hamming: Let the software Hamming ECC engine be unselected Miquel Raynal
2020-10-30 17:29   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 19/20] mtd: nand: ecc-hamming: Create the software Hamming engine Miquel Raynal
2020-10-30 17:28   ` Miquel Raynal
2020-09-29 23:01 ` [PATCH 20/20] mtd: nand: Let software ECC engines be retrieved from the NAND core Miquel Raynal
2020-10-30 17:28   ` 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=20200929230124.31491-8-miquel.raynal@bootlin.com \
    --to=miquel.raynal@bootlin.com \
    --cc=Tudor.Ambarus@microchip.com \
    --cc=juliensu@mxic.com.tw \
    --cc=linux-mtd@lists.infradead.org \
    --cc=richard@nod.at \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vigneshr@ti.com \
    --cc=ycllin@mxic.com.tw \
    /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;
as well as URLs for NNTP newsgroup(s).