Skip to content

Commit ea146d7

Browse files
committed
mtd: nand: ecc-bch: Update the prototypes to be more generic
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> Link: https://lore.kernel.org/linux-mtd/20200929230124.31491-8-miquel.raynal@bootlin.com
1 parent 127aae6 commit ea146d7

5 files changed

Lines changed: 97 additions & 67 deletions

File tree

‎drivers/mtd/nand/ecc-sw-bch.c‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/bitops.h>
1414
#include <linux/mtd/mtd.h>
1515
#include <linux/mtd/rawnand.h>
16+
#include <linux/mtd/nand.h>
1617
#include <linux/mtd/nand-ecc-sw-bch.h>
1718
#include <linux/bch.h>
1819

@@ -29,14 +30,15 @@ struct nand_bch_control {
2930
};
3031

3132
/**
32-
* nand_bch_calcuate_ecc - Calculate the ECC corresponding to a data block
33-
* @chip: NAND chip object
33+
* nand_ecc_sw_bch_calculate - Calculate the ECC corresponding to a data block
34+
* @nand: NAND device
3435
* @buf: Input buffer with raw data
3536
* @code: Output buffer with ECC
3637
*/
37-
int nand_bch_calculate_ecc(struct nand_chip *chip, const unsigned char *buf,
38-
unsigned char *code)
38+
int nand_ecc_sw_bch_calculate(struct nand_device *nand,
39+
const unsigned char *buf, unsigned char *code)
3940
{
41+
struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
4042
struct nand_bch_control *nbc = chip->ecc.priv;
4143
unsigned int i;
4244

@@ -49,20 +51,21 @@ int nand_bch_calculate_ecc(struct nand_chip *chip, const unsigned char *buf,
4951

5052
return 0;
5153
}
52-
EXPORT_SYMBOL(nand_bch_calculate_ecc);
54+
EXPORT_SYMBOL(nand_ecc_sw_bch_calculate);
5355

5456
/**
55-
* nand_bch_correct_data - Detect, correct and report bit error(s)
56-
* @chip: NAND chip object
57+
* nand_ecc_sw_bch_correct - Detect, correct and report bit error(s)
58+
* @nand: NAND device
5759
* @buf: Raw data read from the chip
5860
* @read_ecc: ECC bytes from the chip
5961
* @calc_ecc: ECC calculated from the raw data
6062
*
6163
* Detect and correct bit errors for a data block.
6264
*/
63-
int nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf,
64-
unsigned char *read_ecc, unsigned char *calc_ecc)
65+
int nand_ecc_sw_bch_correct(struct nand_device *nand, unsigned char *buf,
66+
unsigned char *read_ecc, unsigned char *calc_ecc)
6567
{
68+
struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
6669
struct nand_bch_control *nbc = chip->ecc.priv;
6770
unsigned int *errloc = nbc->errloc;
6871
int i, count;
@@ -86,11 +89,11 @@ int nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf,
8689

8790
return count;
8891
}
89-
EXPORT_SYMBOL(nand_bch_correct_data);
92+
EXPORT_SYMBOL(nand_ecc_sw_bch_correct);
9093

9194
/**
92-
* nand_bch_init - Initialize software BCH ECC engine
93-
* @chip: NAND chip object
95+
* nand_ecc_sw_bch_init - Initialize software BCH ECC engine
96+
* @nand: NAND device
9497
*
9598
* Returns: a pointer to a new NAND BCH control structure, or NULL upon failure
9699
*
@@ -105,9 +108,10 @@ EXPORT_SYMBOL(nand_bch_correct_data);
105108
* @eccsize = 512 (thus, m = 13 is the smallest integer such that 2^m - 1 > 512 * 8)
106109
* @eccbytes = 7 (7 bytes are required to store m * t = 13 * 4 = 52 bits)
107110
*/
108-
int nand_bch_init(struct nand_chip *chip)
111+
int nand_ecc_sw_bch_init(struct nand_device *nand)
109112
{
110-
struct mtd_info *mtd = nand_to_mtd(chip);
113+
struct mtd_info *mtd = nanddev_to_mtd(nand);
114+
struct nand_chip *chip = mtd_to_nand(mtd);
111115
unsigned int m, t, eccsteps, i;
112116
struct nand_bch_control *nbc = NULL;
113117
unsigned char *erased_page;
@@ -198,18 +202,21 @@ int nand_bch_init(struct nand_chip *chip)
198202
chip->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
199203

200204
return 0;
205+
201206
fail:
202-
nand_bch_free(chip);
207+
nand_ecc_sw_bch_cleanup(nand);
208+
203209
return -EINVAL;
204210
}
205-
EXPORT_SYMBOL(nand_bch_init);
211+
EXPORT_SYMBOL(nand_ecc_sw_bch_init);
206212

207213
/**
208-
* nand_bch_free - Release NAND BCH ECC resources
209-
* @nbc: NAND BCH control structure
214+
* nand_ecc_sw_bch_cleanup - Cleanup software BCH ECC resources
215+
* @nand: NAND device
210216
*/
211-
void nand_bch_free(struct nand_chip *chip)
217+
void nand_ecc_sw_bch_cleanup(struct nand_device *nand)
212218
{
219+
struct nand_chip *chip = mtd_to_nand(nanddev_to_mtd(nand));
213220
struct nand_bch_control *nbc = chip->ecc.priv;
214221

215222
if (nbc) {
@@ -219,7 +226,7 @@ void nand_bch_free(struct nand_chip *chip)
219226
kfree(nbc);
220227
}
221228
}
222-
EXPORT_SYMBOL(nand_bch_free);
229+
EXPORT_SYMBOL(nand_ecc_sw_bch_cleanup);
223230

224231
MODULE_LICENSE("GPL");
225232
MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");

‎drivers/mtd/nand/raw/nand_base.c‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5139,6 +5139,42 @@ static void nand_scan_ident_cleanup(struct nand_chip *chip)
51395139
kfree(chip->parameters.onfi);
51405140
}
51415141

5142+
int rawnand_sw_bch_init(struct nand_chip *chip)
5143+
{
5144+
struct nand_device *base = &chip->base;
5145+
5146+
return nand_ecc_sw_bch_init(base);
5147+
}
5148+
EXPORT_SYMBOL(rawnand_sw_bch_init);
5149+
5150+
static int rawnand_sw_bch_calculate(struct nand_chip *chip,
5151+
const unsigned char *buf,
5152+
unsigned char *code)
5153+
{
5154+
struct nand_device *base = &chip->base;
5155+
5156+
return nand_ecc_sw_bch_calculate(base, buf, code);
5157+
}
5158+
5159+
int rawnand_sw_bch_correct(struct nand_chip *chip, unsigned char *buf,
5160+
unsigned char *read_ecc, unsigned char *calc_ecc)
5161+
{
5162+
struct nand_device *base = &chip->base;
5163+
5164+
return nand_ecc_sw_bch_correct(base, buf, read_ecc, calc_ecc);
5165+
}
5166+
EXPORT_SYMBOL(rawnand_sw_bch_correct);
5167+
5168+
void rawnand_sw_bch_cleanup(struct nand_chip *chip)
5169+
{
5170+
struct nand_device *base = &chip->base;
5171+
5172+
nand_ecc_sw_bch_cleanup(base);
5173+
5174+
chip->ecc.priv = NULL;
5175+
}
5176+
EXPORT_SYMBOL(rawnand_sw_bch_cleanup);
5177+
51425178
static int nand_set_ecc_on_host_ops(struct nand_chip *chip)
51435179
{
51445180
struct nand_ecc_ctrl *ecc = &chip->ecc;
@@ -5235,8 +5271,8 @@ static int nand_set_ecc_soft_ops(struct nand_chip *chip)
52355271
WARN(1, "CONFIG_MTD_NAND_ECC_SW_BCH not enabled\n");
52365272
return -EINVAL;
52375273
}
5238-
ecc->calculate = nand_bch_calculate_ecc;
5239-
ecc->correct = nand_bch_correct_data;
5274+
ecc->calculate = rawnand_sw_bch_calculate;
5275+
ecc->correct = rawnand_sw_bch_correct;
52405276
ecc->read_page = nand_read_page_swecc;
52415277
ecc->read_subpage = nand_read_subpage;
52425278
ecc->write_page = nand_write_page_swecc;
@@ -5292,7 +5328,7 @@ static int nand_set_ecc_soft_ops(struct nand_chip *chip)
52925328

52935329
/* See the software BCH ECC initialization for details */
52945330
ecc->bytes = 0;
5295-
ret = nand_bch_init(chip);
5331+
ret = rawnand_sw_bch_init(chip);
52965332
if (ret) {
52975333
WARN(1, "BCH ECC initialization failed!\n");
52985334
return ret;
@@ -5957,7 +5993,7 @@ void nand_cleanup(struct nand_chip *chip)
59575993
{
59585994
if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
59595995
chip->ecc.algo == NAND_ECC_ALGO_BCH)
5960-
nand_bch_free(chip);
5996+
rawnand_sw_bch_cleanup(chip);
59615997

59625998
nanddev_cleanup(&chip->base);
59635999

‎drivers/mtd/nand/raw/omap2.c‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <linux/of.h>
2424
#include <linux/of_device.h>
2525

26-
#include <linux/mtd/nand-ecc-sw-bch.h>
2726
#include <linux/platform_data/elm.h>
2827

2928
#include <linux/omap-gpmc.h>
@@ -2041,13 +2040,13 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
20412040
chip->ecc.bytes = 7;
20422041
chip->ecc.strength = 4;
20432042
chip->ecc.hwctl = omap_enable_hwecc_bch;
2044-
chip->ecc.correct = nand_bch_correct_data;
2043+
chip->ecc.correct = rawnand_sw_bch_correct;
20452044
chip->ecc.calculate = omap_calculate_ecc_bch_sw;
20462045
mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
20472046
/* Reserve one byte for the OMAP marker */
20482047
oobbytes_per_step = chip->ecc.bytes + 1;
20492048
/* Software BCH library is used for locating errors */
2050-
err = nand_bch_init(chip);
2049+
err = rawnand_sw_bch_init(chip);
20512050
if (err) {
20522051
dev_err(dev, "Unable to use BCH library\n");
20532052
return err;
@@ -2083,13 +2082,13 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
20832082
chip->ecc.bytes = 13;
20842083
chip->ecc.strength = 8;
20852084
chip->ecc.hwctl = omap_enable_hwecc_bch;
2086-
chip->ecc.correct = nand_bch_correct_data;
2085+
chip->ecc.correct = rawnand_sw_bch_correct;
20872086
chip->ecc.calculate = omap_calculate_ecc_bch_sw;
20882087
mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
20892088
/* Reserve one byte for the OMAP marker */
20902089
oobbytes_per_step = chip->ecc.bytes + 1;
20912090
/* Software BCH library is used for locating errors */
2092-
err = nand_bch_init(chip);
2091+
err = rawnand_sw_bch_init(chip);
20932092
if (err) {
20942093
dev_err(dev, "unable to use BCH library\n");
20952094
return err;
@@ -2195,7 +2194,6 @@ static int omap_nand_probe(struct platform_device *pdev)
21952194
nand_chip = &info->nand;
21962195
mtd = nand_to_mtd(nand_chip);
21972196
mtd->dev.parent = &pdev->dev;
2198-
nand_chip->ecc.priv = NULL;
21992197
nand_set_flash_node(nand_chip, dev->of_node);
22002198

22012199
if (!mtd->name) {
@@ -2271,10 +2269,9 @@ static int omap_nand_probe(struct platform_device *pdev)
22712269
return_error:
22722270
if (!IS_ERR_OR_NULL(info->dma))
22732271
dma_release_channel(info->dma);
2274-
if (nand_chip->ecc.priv) {
2275-
nand_bch_free(nand_chip);
2276-
nand_chip->ecc.priv = NULL;
2277-
}
2272+
2273+
rawnand_sw_bch_cleanup(nand_chip);
2274+
22782275
return err;
22792276
}
22802277

@@ -2285,10 +2282,8 @@ static int omap_nand_remove(struct platform_device *pdev)
22852282
struct omap_nand_info *info = mtd_to_omap(mtd);
22862283
int ret;
22872284

2288-
if (nand_chip->ecc.priv) {
2289-
nand_bch_free(nand_chip);
2290-
nand_chip->ecc.priv = NULL;
2291-
}
2285+
rawnand_sw_bch_cleanup(nand_chip);
2286+
22922287
if (info->dma)
22932288
dma_release_channel(info->dma);
22942289
ret = mtd_device_unregister(mtd);

‎include/linux/mtd/nand-ecc-sw-bch.h‎

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,40 @@
88
#ifndef __MTD_NAND_ECC_SW_BCH_H__
99
#define __MTD_NAND_ECC_SW_BCH_H__
1010

11-
struct mtd_info;
12-
struct nand_chip;
11+
#include <linux/mtd/nand.h>
1312

1413
#if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)
1514

16-
/*
17-
* Calculate BCH ecc code
18-
*/
19-
int nand_bch_calculate_ecc(struct nand_chip *chip, const u_char *dat,
20-
u_char *ecc_code);
21-
22-
/*
23-
* Detect and correct bit errors
24-
*/
25-
int nand_bch_correct_data(struct nand_chip *chip, u_char *dat,
26-
u_char *read_ecc, u_char *calc_ecc);
27-
/*
28-
* Initialize BCH encoder/decoder
29-
*/
30-
int nand_bch_init(struct nand_chip *chip);
31-
/*
32-
* Release BCH encoder/decoder resources
33-
*/
34-
void nand_bch_free(struct nand_chip *chip);
15+
int nand_ecc_sw_bch_calculate(struct nand_device *nand,
16+
const unsigned char *buf, unsigned char *code);
17+
int nand_ecc_sw_bch_correct(struct nand_device *nand, unsigned char *buf,
18+
unsigned char *read_ecc, unsigned char *calc_ecc);
19+
int nand_ecc_sw_bch_init(struct nand_device *nand);
20+
void nand_ecc_sw_bch_cleanup(struct nand_device *nand);
3521

3622
#else /* !CONFIG_MTD_NAND_ECC_SW_BCH */
3723

38-
static inline int
39-
nand_bch_calculate_ecc(struct nand_chip *chip, const u_char *dat,
40-
u_char *ecc_code)
24+
static inline int nand_ecc_sw_bch_calculate(struct nand_device *nand,
25+
const unsigned char *buf,
26+
unsigned char *code)
4127
{
4228
return -ENOTSUPP;
4329
}
4430

45-
static inline int
46-
nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf,
47-
unsigned char *read_ecc, unsigned char *calc_ecc)
31+
static inline int nand_ecc_sw_bch_correct(struct nand_device *nand,
32+
unsigned char *buf,
33+
unsigned char *read_ecc,
34+
unsigned char *calc_ecc)
4835
{
4936
return -ENOTSUPP;
5037
}
5138

52-
static inline int nand_bch_init(struct nand_chip *chip)
39+
static inline int nand_ecc_sw_bch_init(struct nand_device *nand)
5340
{
5441
return -ENOTSUPP;
5542
}
5643

57-
static inline void nand_bch_free(struct nand_chip *chip) {}
44+
static inline void nand_ecc_sw_bch_cleanup(struct nand_device *nand) {}
5845

5946
#endif /* CONFIG_MTD_NAND_ECC_SW_BCH */
6047

‎include/linux/mtd/rawnand.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,11 @@ static inline int nand_opcode_8bits(unsigned int command)
13031303
return 0;
13041304
}
13051305

1306+
int rawnand_sw_bch_init(struct nand_chip *chip);
1307+
int rawnand_sw_bch_correct(struct nand_chip *chip, unsigned char *buf,
1308+
unsigned char *read_ecc, unsigned char *calc_ecc);
1309+
void rawnand_sw_bch_cleanup(struct nand_chip *chip);
1310+
13061311
int nand_check_erased_ecc_chunk(void *data, int datalen,
13071312
void *ecc, int ecclen,
13081313
void *extraoob, int extraooblen,

0 commit comments

Comments
 (0)