1cdbe8df5SMiquel Raynal // SPDX-License-Identifier: GPL-2.0-or-later
2cdbe8df5SMiquel Raynal /*
3cdbe8df5SMiquel Raynal * This file provides ECC correction for more than 1 bit per block of data,
4cdbe8df5SMiquel Raynal * using binary BCH codes. It relies on the generic BCH library lib/bch.c.
5cdbe8df5SMiquel Raynal *
6cdbe8df5SMiquel Raynal * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
7cdbe8df5SMiquel Raynal */
8cdbe8df5SMiquel Raynal
9cdbe8df5SMiquel Raynal #include <linux/types.h>
10cdbe8df5SMiquel Raynal #include <linux/kernel.h>
11cdbe8df5SMiquel Raynal #include <linux/module.h>
12cdbe8df5SMiquel Raynal #include <linux/slab.h>
13cdbe8df5SMiquel Raynal #include <linux/bitops.h>
14ea146d7fSMiquel Raynal #include <linux/mtd/nand.h>
15cdbe8df5SMiquel Raynal #include <linux/mtd/nand-ecc-sw-bch.h>
16cdbe8df5SMiquel Raynal
17cdbe8df5SMiquel Raynal /**
18ea146d7fSMiquel Raynal * nand_ecc_sw_bch_calculate - Calculate the ECC corresponding to a data block
19ea146d7fSMiquel Raynal * @nand: NAND device
208c5c2092SMiquel Raynal * @buf: Input buffer with raw data
218c5c2092SMiquel Raynal * @code: Output buffer with ECC
22cdbe8df5SMiquel Raynal */
nand_ecc_sw_bch_calculate(struct nand_device * nand,const unsigned char * buf,unsigned char * code)23ea146d7fSMiquel Raynal int nand_ecc_sw_bch_calculate(struct nand_device *nand,
24ea146d7fSMiquel Raynal const unsigned char *buf, unsigned char *code)
25cdbe8df5SMiquel Raynal {
2680fe6031SMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
27cdbe8df5SMiquel Raynal unsigned int i;
28cdbe8df5SMiquel Raynal
2980fe6031SMiquel Raynal memset(code, 0, engine_conf->code_size);
3080fe6031SMiquel Raynal bch_encode(engine_conf->bch, buf, nand->ecc.ctx.conf.step_size, code);
31cdbe8df5SMiquel Raynal
32cdbe8df5SMiquel Raynal /* apply mask so that an erased page is a valid codeword */
3380fe6031SMiquel Raynal for (i = 0; i < engine_conf->code_size; i++)
3480fe6031SMiquel Raynal code[i] ^= engine_conf->eccmask[i];
35cdbe8df5SMiquel Raynal
36cdbe8df5SMiquel Raynal return 0;
37cdbe8df5SMiquel Raynal }
38ea146d7fSMiquel Raynal EXPORT_SYMBOL(nand_ecc_sw_bch_calculate);
39cdbe8df5SMiquel Raynal
40cdbe8df5SMiquel Raynal /**
41ea146d7fSMiquel Raynal * nand_ecc_sw_bch_correct - Detect, correct and report bit error(s)
42ea146d7fSMiquel Raynal * @nand: NAND device
438c5c2092SMiquel Raynal * @buf: Raw data read from the chip
448c5c2092SMiquel Raynal * @read_ecc: ECC bytes from the chip
458c5c2092SMiquel Raynal * @calc_ecc: ECC calculated from the raw data
46cdbe8df5SMiquel Raynal *
478c5c2092SMiquel Raynal * Detect and correct bit errors for a data block.
48cdbe8df5SMiquel Raynal */
nand_ecc_sw_bch_correct(struct nand_device * nand,unsigned char * buf,unsigned char * read_ecc,unsigned char * calc_ecc)49ea146d7fSMiquel Raynal int nand_ecc_sw_bch_correct(struct nand_device *nand, unsigned char *buf,
50cdbe8df5SMiquel Raynal unsigned char *read_ecc, unsigned char *calc_ecc)
51cdbe8df5SMiquel Raynal {
5280fe6031SMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
5380fe6031SMiquel Raynal unsigned int step_size = nand->ecc.ctx.conf.step_size;
5480fe6031SMiquel Raynal unsigned int *errloc = engine_conf->errloc;
55cdbe8df5SMiquel Raynal int i, count;
56cdbe8df5SMiquel Raynal
5780fe6031SMiquel Raynal count = bch_decode(engine_conf->bch, NULL, step_size, read_ecc,
5880fe6031SMiquel Raynal calc_ecc, NULL, errloc);
59cdbe8df5SMiquel Raynal if (count > 0) {
60cdbe8df5SMiquel Raynal for (i = 0; i < count; i++) {
6180fe6031SMiquel Raynal if (errloc[i] < (step_size * 8))
628c5c2092SMiquel Raynal /* The error is in the data area: correct it */
63cdbe8df5SMiquel Raynal buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
64cdbe8df5SMiquel Raynal
658c5c2092SMiquel Raynal /* Otherwise the error is in the ECC area: nothing to do */
66cdbe8df5SMiquel Raynal pr_debug("%s: corrected bitflip %u\n", __func__,
67cdbe8df5SMiquel Raynal errloc[i]);
68cdbe8df5SMiquel Raynal }
69cdbe8df5SMiquel Raynal } else if (count < 0) {
708c5c2092SMiquel Raynal pr_err("ECC unrecoverable error\n");
71cdbe8df5SMiquel Raynal count = -EBADMSG;
72cdbe8df5SMiquel Raynal }
738c5c2092SMiquel Raynal
74cdbe8df5SMiquel Raynal return count;
75cdbe8df5SMiquel Raynal }
76ea146d7fSMiquel Raynal EXPORT_SYMBOL(nand_ecc_sw_bch_correct);
77cdbe8df5SMiquel Raynal
78cdbe8df5SMiquel Raynal /**
799994bb3fSMiquel Raynal * nand_ecc_sw_bch_cleanup - Cleanup software BCH ECC resources
809994bb3fSMiquel Raynal * @nand: NAND device
819994bb3fSMiquel Raynal */
nand_ecc_sw_bch_cleanup(struct nand_device * nand)829994bb3fSMiquel Raynal static void nand_ecc_sw_bch_cleanup(struct nand_device *nand)
839994bb3fSMiquel Raynal {
849994bb3fSMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
859994bb3fSMiquel Raynal
869994bb3fSMiquel Raynal bch_free(engine_conf->bch);
879994bb3fSMiquel Raynal kfree(engine_conf->errloc);
889994bb3fSMiquel Raynal kfree(engine_conf->eccmask);
899994bb3fSMiquel Raynal }
909994bb3fSMiquel Raynal
919994bb3fSMiquel Raynal /**
92ea146d7fSMiquel Raynal * nand_ecc_sw_bch_init - Initialize software BCH ECC engine
93ea146d7fSMiquel Raynal * @nand: NAND device
94cdbe8df5SMiquel Raynal *
958c5c2092SMiquel Raynal * Returns: a pointer to a new NAND BCH control structure, or NULL upon failure
96cdbe8df5SMiquel Raynal *
9780fe6031SMiquel Raynal * Initialize NAND BCH error correction. @nand.ecc parameters 'step_size' and
9880fe6031SMiquel Raynal * 'bytes' are used to compute the following BCH parameters:
998c5c2092SMiquel Raynal * m, the Galois field order
1008c5c2092SMiquel Raynal * t, the error correction capability
10180fe6031SMiquel Raynal * 'bytes' should be equal to the number of bytes required to store m * t
1028c5c2092SMiquel Raynal * bits, where m is such that 2^m - 1 > step_size * 8.
103cdbe8df5SMiquel Raynal *
104cdbe8df5SMiquel Raynal * Example: to configure 4 bit correction per 512 bytes, you should pass
10580fe6031SMiquel Raynal * step_size = 512 (thus, m = 13 is the smallest integer such that 2^m - 1 > 512 * 8)
10680fe6031SMiquel Raynal * bytes = 7 (7 bytes are required to store m * t = 13 * 4 = 52 bits)
107cdbe8df5SMiquel Raynal */
nand_ecc_sw_bch_init(struct nand_device * nand)1089994bb3fSMiquel Raynal static int nand_ecc_sw_bch_init(struct nand_device *nand)
109cdbe8df5SMiquel Raynal {
11080fe6031SMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
11180fe6031SMiquel Raynal unsigned int eccsize = nand->ecc.ctx.conf.step_size;
11280fe6031SMiquel Raynal unsigned int eccbytes = engine_conf->code_size;
1139994bb3fSMiquel Raynal unsigned int m, t, i;
1149994bb3fSMiquel Raynal unsigned char *erased_page;
1159994bb3fSMiquel Raynal int ret;
116cdbe8df5SMiquel Raynal
1179994bb3fSMiquel Raynal m = fls(1 + (8 * eccsize));
118cdbe8df5SMiquel Raynal t = (eccbytes * 8) / m;
119cdbe8df5SMiquel Raynal
12080fe6031SMiquel Raynal engine_conf->bch = bch_init(m, t, 0, false);
12180fe6031SMiquel Raynal if (!engine_conf->bch)
12280fe6031SMiquel Raynal return -EINVAL;
123cdbe8df5SMiquel Raynal
12480fe6031SMiquel Raynal engine_conf->eccmask = kzalloc(eccbytes, GFP_KERNEL);
12580fe6031SMiquel Raynal engine_conf->errloc = kmalloc_array(t, sizeof(*engine_conf->errloc),
12680fe6031SMiquel Raynal GFP_KERNEL);
1279994bb3fSMiquel Raynal if (!engine_conf->eccmask || !engine_conf->errloc) {
1289994bb3fSMiquel Raynal ret = -ENOMEM;
1299994bb3fSMiquel Raynal goto cleanup;
1309994bb3fSMiquel Raynal }
1318c5c2092SMiquel Raynal
1329994bb3fSMiquel Raynal /* Compute and store the inverted ECC of an erased step */
133cdbe8df5SMiquel Raynal erased_page = kmalloc(eccsize, GFP_KERNEL);
1349994bb3fSMiquel Raynal if (!erased_page) {
1359994bb3fSMiquel Raynal ret = -ENOMEM;
1369994bb3fSMiquel Raynal goto cleanup;
1379994bb3fSMiquel Raynal }
138cdbe8df5SMiquel Raynal
139cdbe8df5SMiquel Raynal memset(erased_page, 0xff, eccsize);
14080fe6031SMiquel Raynal bch_encode(engine_conf->bch, erased_page, eccsize,
14180fe6031SMiquel Raynal engine_conf->eccmask);
142cdbe8df5SMiquel Raynal kfree(erased_page);
143cdbe8df5SMiquel Raynal
144cdbe8df5SMiquel Raynal for (i = 0; i < eccbytes; i++)
14580fe6031SMiquel Raynal engine_conf->eccmask[i] ^= 0xff;
146cdbe8df5SMiquel Raynal
1479994bb3fSMiquel Raynal /* Verify that the number of code bytes has the expected value */
1489994bb3fSMiquel Raynal if (engine_conf->bch->ecc_bytes != eccbytes) {
1499994bb3fSMiquel Raynal pr_err("Invalid number of ECC bytes: %u, expected: %u\n",
1509994bb3fSMiquel Raynal eccbytes, engine_conf->bch->ecc_bytes);
1519994bb3fSMiquel Raynal ret = -EINVAL;
1529994bb3fSMiquel Raynal goto cleanup;
1539994bb3fSMiquel Raynal }
1549994bb3fSMiquel Raynal
1559994bb3fSMiquel Raynal /* Sanity checks */
1569994bb3fSMiquel Raynal if (8 * (eccsize + eccbytes) >= (1 << m)) {
1579994bb3fSMiquel Raynal pr_err("ECC step size is too large (%u)\n", eccsize);
1589994bb3fSMiquel Raynal ret = -EINVAL;
1599994bb3fSMiquel Raynal goto cleanup;
1609994bb3fSMiquel Raynal }
161cdbe8df5SMiquel Raynal
1623c0fe36aSMiquel Raynal return 0;
163ea146d7fSMiquel Raynal
1649994bb3fSMiquel Raynal cleanup:
165ea146d7fSMiquel Raynal nand_ecc_sw_bch_cleanup(nand);
166ea146d7fSMiquel Raynal
1679994bb3fSMiquel Raynal return ret;
1689994bb3fSMiquel Raynal }
1699994bb3fSMiquel Raynal
nand_ecc_sw_bch_init_ctx(struct nand_device * nand)1709994bb3fSMiquel Raynal int nand_ecc_sw_bch_init_ctx(struct nand_device *nand)
1719994bb3fSMiquel Raynal {
1729994bb3fSMiquel Raynal struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
1739994bb3fSMiquel Raynal struct mtd_info *mtd = nanddev_to_mtd(nand);
1749994bb3fSMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf;
1759994bb3fSMiquel Raynal unsigned int code_size = 0, nsteps;
1769994bb3fSMiquel Raynal int ret;
1779994bb3fSMiquel Raynal
1789994bb3fSMiquel Raynal /* Only large page NAND chips may use BCH */
1799994bb3fSMiquel Raynal if (mtd->oobsize < 64) {
1809994bb3fSMiquel Raynal pr_err("BCH cannot be used with small page NAND chips\n");
1813c0fe36aSMiquel Raynal return -EINVAL;
182cdbe8df5SMiquel Raynal }
183cdbe8df5SMiquel Raynal
1849994bb3fSMiquel Raynal if (!mtd->ooblayout)
1859994bb3fSMiquel Raynal mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
1869994bb3fSMiquel Raynal
1879994bb3fSMiquel Raynal conf->engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
1889994bb3fSMiquel Raynal conf->algo = NAND_ECC_ALGO_BCH;
1899994bb3fSMiquel Raynal conf->step_size = nand->ecc.user_conf.step_size;
1909994bb3fSMiquel Raynal conf->strength = nand->ecc.user_conf.strength;
1919994bb3fSMiquel Raynal
1929994bb3fSMiquel Raynal /*
1939994bb3fSMiquel Raynal * Board driver should supply ECC size and ECC strength
1949994bb3fSMiquel Raynal * values to select how many bits are correctable.
1959994bb3fSMiquel Raynal * Otherwise, default to 512 bytes for large page devices and 256 for
1969994bb3fSMiquel Raynal * small page devices.
197cdbe8df5SMiquel Raynal */
1989994bb3fSMiquel Raynal if (!conf->step_size) {
1999994bb3fSMiquel Raynal if (mtd->oobsize >= 64)
2009994bb3fSMiquel Raynal conf->step_size = 512;
2019994bb3fSMiquel Raynal else
2029994bb3fSMiquel Raynal conf->step_size = 256;
2039994bb3fSMiquel Raynal
2049994bb3fSMiquel Raynal conf->strength = 4;
2059994bb3fSMiquel Raynal }
2069994bb3fSMiquel Raynal
2079994bb3fSMiquel Raynal nsteps = mtd->writesize / conf->step_size;
2089994bb3fSMiquel Raynal
2099994bb3fSMiquel Raynal /* Maximize */
2109994bb3fSMiquel Raynal if (nand->ecc.user_conf.flags & NAND_ECC_MAXIMIZE_STRENGTH) {
2119994bb3fSMiquel Raynal conf->step_size = 1024;
2129994bb3fSMiquel Raynal nsteps = mtd->writesize / conf->step_size;
2139994bb3fSMiquel Raynal /* Reserve 2 bytes for the BBM */
2149994bb3fSMiquel Raynal code_size = (mtd->oobsize - 2) / nsteps;
2159994bb3fSMiquel Raynal conf->strength = code_size * 8 / fls(8 * conf->step_size);
2169994bb3fSMiquel Raynal }
2179994bb3fSMiquel Raynal
2189994bb3fSMiquel Raynal if (!code_size)
2199994bb3fSMiquel Raynal code_size = DIV_ROUND_UP(conf->strength *
2209994bb3fSMiquel Raynal fls(8 * conf->step_size), 8);
2219994bb3fSMiquel Raynal
2229994bb3fSMiquel Raynal if (!conf->strength)
2239994bb3fSMiquel Raynal conf->strength = (code_size * 8) / fls(8 * conf->step_size);
2249994bb3fSMiquel Raynal
2259994bb3fSMiquel Raynal if (!code_size && !conf->strength) {
2269994bb3fSMiquel Raynal pr_err("Missing ECC parameters\n");
2279994bb3fSMiquel Raynal return -EINVAL;
2289994bb3fSMiquel Raynal }
2299994bb3fSMiquel Raynal
2309994bb3fSMiquel Raynal engine_conf = kzalloc(sizeof(*engine_conf), GFP_KERNEL);
2319994bb3fSMiquel Raynal if (!engine_conf)
2329994bb3fSMiquel Raynal return -ENOMEM;
2339994bb3fSMiquel Raynal
2349994bb3fSMiquel Raynal ret = nand_ecc_init_req_tweaking(&engine_conf->req_ctx, nand);
2359994bb3fSMiquel Raynal if (ret)
2369994bb3fSMiquel Raynal goto free_engine_conf;
2379994bb3fSMiquel Raynal
2389994bb3fSMiquel Raynal engine_conf->code_size = code_size;
2399994bb3fSMiquel Raynal engine_conf->calc_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2409994bb3fSMiquel Raynal engine_conf->code_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2419994bb3fSMiquel Raynal if (!engine_conf->calc_buf || !engine_conf->code_buf) {
2429994bb3fSMiquel Raynal ret = -ENOMEM;
2439994bb3fSMiquel Raynal goto free_bufs;
2449994bb3fSMiquel Raynal }
2459994bb3fSMiquel Raynal
2469994bb3fSMiquel Raynal nand->ecc.ctx.priv = engine_conf;
2477cd37e7eSMiquel Raynal nand->ecc.ctx.nsteps = nsteps;
2489994bb3fSMiquel Raynal nand->ecc.ctx.total = nsteps * code_size;
2499994bb3fSMiquel Raynal
2509994bb3fSMiquel Raynal ret = nand_ecc_sw_bch_init(nand);
2519994bb3fSMiquel Raynal if (ret)
2529994bb3fSMiquel Raynal goto free_bufs;
2539994bb3fSMiquel Raynal
2549994bb3fSMiquel Raynal /* Verify the layout validity */
2559994bb3fSMiquel Raynal if (mtd_ooblayout_count_eccbytes(mtd) !=
256*3e66843cSMiquel Raynal nand->ecc.ctx.nsteps * engine_conf->code_size) {
2579994bb3fSMiquel Raynal pr_err("Invalid ECC layout\n");
2589994bb3fSMiquel Raynal ret = -EINVAL;
2599994bb3fSMiquel Raynal goto cleanup_bch_ctx;
2609994bb3fSMiquel Raynal }
2619994bb3fSMiquel Raynal
2629994bb3fSMiquel Raynal return 0;
2639994bb3fSMiquel Raynal
2649994bb3fSMiquel Raynal cleanup_bch_ctx:
2659994bb3fSMiquel Raynal nand_ecc_sw_bch_cleanup(nand);
2669994bb3fSMiquel Raynal free_bufs:
2679994bb3fSMiquel Raynal nand_ecc_cleanup_req_tweaking(&engine_conf->req_ctx);
2689994bb3fSMiquel Raynal kfree(engine_conf->calc_buf);
2699994bb3fSMiquel Raynal kfree(engine_conf->code_buf);
2709994bb3fSMiquel Raynal free_engine_conf:
2719994bb3fSMiquel Raynal kfree(engine_conf);
2729994bb3fSMiquel Raynal
2739994bb3fSMiquel Raynal return ret;
2749994bb3fSMiquel Raynal }
2759994bb3fSMiquel Raynal EXPORT_SYMBOL(nand_ecc_sw_bch_init_ctx);
2769994bb3fSMiquel Raynal
nand_ecc_sw_bch_cleanup_ctx(struct nand_device * nand)2779994bb3fSMiquel Raynal void nand_ecc_sw_bch_cleanup_ctx(struct nand_device *nand)
278cdbe8df5SMiquel Raynal {
27980fe6031SMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
2803c0fe36aSMiquel Raynal
28180fe6031SMiquel Raynal if (engine_conf) {
2829994bb3fSMiquel Raynal nand_ecc_sw_bch_cleanup(nand);
2839994bb3fSMiquel Raynal nand_ecc_cleanup_req_tweaking(&engine_conf->req_ctx);
2849994bb3fSMiquel Raynal kfree(engine_conf->calc_buf);
2859994bb3fSMiquel Raynal kfree(engine_conf->code_buf);
2869994bb3fSMiquel Raynal kfree(engine_conf);
287cdbe8df5SMiquel Raynal }
288cdbe8df5SMiquel Raynal }
2899994bb3fSMiquel Raynal EXPORT_SYMBOL(nand_ecc_sw_bch_cleanup_ctx);
2909994bb3fSMiquel Raynal
nand_ecc_sw_bch_prepare_io_req(struct nand_device * nand,struct nand_page_io_req * req)2919994bb3fSMiquel Raynal static int nand_ecc_sw_bch_prepare_io_req(struct nand_device *nand,
2929994bb3fSMiquel Raynal struct nand_page_io_req *req)
2939994bb3fSMiquel Raynal {
2949994bb3fSMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
2959994bb3fSMiquel Raynal struct mtd_info *mtd = nanddev_to_mtd(nand);
2969994bb3fSMiquel Raynal int eccsize = nand->ecc.ctx.conf.step_size;
2979994bb3fSMiquel Raynal int eccbytes = engine_conf->code_size;
298*3e66843cSMiquel Raynal int eccsteps = nand->ecc.ctx.nsteps;
2999994bb3fSMiquel Raynal int total = nand->ecc.ctx.total;
3009994bb3fSMiquel Raynal u8 *ecccalc = engine_conf->calc_buf;
3019994bb3fSMiquel Raynal const u8 *data;
3029994bb3fSMiquel Raynal int i;
3039994bb3fSMiquel Raynal
3049994bb3fSMiquel Raynal /* Nothing to do for a raw operation */
3059994bb3fSMiquel Raynal if (req->mode == MTD_OPS_RAW)
3069994bb3fSMiquel Raynal return 0;
3079994bb3fSMiquel Raynal
3089994bb3fSMiquel Raynal /* This engine does not provide BBM/free OOB bytes protection */
3099994bb3fSMiquel Raynal if (!req->datalen)
3109994bb3fSMiquel Raynal return 0;
3119994bb3fSMiquel Raynal
3129994bb3fSMiquel Raynal nand_ecc_tweak_req(&engine_conf->req_ctx, req);
3139994bb3fSMiquel Raynal
3149994bb3fSMiquel Raynal /* No more preparation for page read */
3159994bb3fSMiquel Raynal if (req->type == NAND_PAGE_READ)
3169994bb3fSMiquel Raynal return 0;
3179994bb3fSMiquel Raynal
3189994bb3fSMiquel Raynal /* Preparation for page write: derive the ECC bytes and place them */
3199994bb3fSMiquel Raynal for (i = 0, data = req->databuf.out;
3209994bb3fSMiquel Raynal eccsteps;
3219994bb3fSMiquel Raynal eccsteps--, i += eccbytes, data += eccsize)
3229994bb3fSMiquel Raynal nand_ecc_sw_bch_calculate(nand, data, &ecccalc[i]);
3239994bb3fSMiquel Raynal
3249994bb3fSMiquel Raynal return mtd_ooblayout_set_eccbytes(mtd, ecccalc, (void *)req->oobbuf.out,
3259994bb3fSMiquel Raynal 0, total);
3269994bb3fSMiquel Raynal }
3279994bb3fSMiquel Raynal
nand_ecc_sw_bch_finish_io_req(struct nand_device * nand,struct nand_page_io_req * req)3289994bb3fSMiquel Raynal static int nand_ecc_sw_bch_finish_io_req(struct nand_device *nand,
3299994bb3fSMiquel Raynal struct nand_page_io_req *req)
3309994bb3fSMiquel Raynal {
3319994bb3fSMiquel Raynal struct nand_ecc_sw_bch_conf *engine_conf = nand->ecc.ctx.priv;
3329994bb3fSMiquel Raynal struct mtd_info *mtd = nanddev_to_mtd(nand);
3339994bb3fSMiquel Raynal int eccsize = nand->ecc.ctx.conf.step_size;
3349994bb3fSMiquel Raynal int total = nand->ecc.ctx.total;
3359994bb3fSMiquel Raynal int eccbytes = engine_conf->code_size;
336*3e66843cSMiquel Raynal int eccsteps = nand->ecc.ctx.nsteps;
3379994bb3fSMiquel Raynal u8 *ecccalc = engine_conf->calc_buf;
3389994bb3fSMiquel Raynal u8 *ecccode = engine_conf->code_buf;
3399994bb3fSMiquel Raynal unsigned int max_bitflips = 0;
3409994bb3fSMiquel Raynal u8 *data = req->databuf.in;
3419994bb3fSMiquel Raynal int i, ret;
3429994bb3fSMiquel Raynal
3439994bb3fSMiquel Raynal /* Nothing to do for a raw operation */
3449994bb3fSMiquel Raynal if (req->mode == MTD_OPS_RAW)
3459994bb3fSMiquel Raynal return 0;
3469994bb3fSMiquel Raynal
3479994bb3fSMiquel Raynal /* This engine does not provide BBM/free OOB bytes protection */
3489994bb3fSMiquel Raynal if (!req->datalen)
3499994bb3fSMiquel Raynal return 0;
3509994bb3fSMiquel Raynal
3519994bb3fSMiquel Raynal /* No more preparation for page write */
3529994bb3fSMiquel Raynal if (req->type == NAND_PAGE_WRITE) {
3539994bb3fSMiquel Raynal nand_ecc_restore_req(&engine_conf->req_ctx, req);
3549994bb3fSMiquel Raynal return 0;
3559994bb3fSMiquel Raynal }
3569994bb3fSMiquel Raynal
3579994bb3fSMiquel Raynal /* Finish a page read: retrieve the (raw) ECC bytes*/
3589994bb3fSMiquel Raynal ret = mtd_ooblayout_get_eccbytes(mtd, ecccode, req->oobbuf.in, 0,
3599994bb3fSMiquel Raynal total);
3609994bb3fSMiquel Raynal if (ret)
3619994bb3fSMiquel Raynal return ret;
3629994bb3fSMiquel Raynal
3639994bb3fSMiquel Raynal /* Calculate the ECC bytes */
3649994bb3fSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, data += eccsize)
3659994bb3fSMiquel Raynal nand_ecc_sw_bch_calculate(nand, data, &ecccalc[i]);
3669994bb3fSMiquel Raynal
3679994bb3fSMiquel Raynal /* Finish a page read: compare and correct */
368*3e66843cSMiquel Raynal for (eccsteps = nand->ecc.ctx.nsteps, i = 0, data = req->databuf.in;
3699994bb3fSMiquel Raynal eccsteps;
3709994bb3fSMiquel Raynal eccsteps--, i += eccbytes, data += eccsize) {
3719994bb3fSMiquel Raynal int stat = nand_ecc_sw_bch_correct(nand, data,
3729994bb3fSMiquel Raynal &ecccode[i],
3739994bb3fSMiquel Raynal &ecccalc[i]);
3749994bb3fSMiquel Raynal if (stat < 0) {
3759994bb3fSMiquel Raynal mtd->ecc_stats.failed++;
3769994bb3fSMiquel Raynal } else {
3779994bb3fSMiquel Raynal mtd->ecc_stats.corrected += stat;
3789994bb3fSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, stat);
3799994bb3fSMiquel Raynal }
3809994bb3fSMiquel Raynal }
3819994bb3fSMiquel Raynal
3829994bb3fSMiquel Raynal nand_ecc_restore_req(&engine_conf->req_ctx, req);
3839994bb3fSMiquel Raynal
3849994bb3fSMiquel Raynal return max_bitflips;
3859994bb3fSMiquel Raynal }
3869994bb3fSMiquel Raynal
3879994bb3fSMiquel Raynal static struct nand_ecc_engine_ops nand_ecc_sw_bch_engine_ops = {
3889994bb3fSMiquel Raynal .init_ctx = nand_ecc_sw_bch_init_ctx,
3899994bb3fSMiquel Raynal .cleanup_ctx = nand_ecc_sw_bch_cleanup_ctx,
3909994bb3fSMiquel Raynal .prepare_io_req = nand_ecc_sw_bch_prepare_io_req,
3919994bb3fSMiquel Raynal .finish_io_req = nand_ecc_sw_bch_finish_io_req,
3929994bb3fSMiquel Raynal };
3939994bb3fSMiquel Raynal
3949994bb3fSMiquel Raynal static struct nand_ecc_engine nand_ecc_sw_bch_engine = {
3959994bb3fSMiquel Raynal .ops = &nand_ecc_sw_bch_engine_ops,
3969994bb3fSMiquel Raynal };
3979994bb3fSMiquel Raynal
nand_ecc_sw_bch_get_engine(void)3989994bb3fSMiquel Raynal struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void)
3999994bb3fSMiquel Raynal {
4009994bb3fSMiquel Raynal return &nand_ecc_sw_bch_engine;
4019994bb3fSMiquel Raynal }
4029994bb3fSMiquel Raynal EXPORT_SYMBOL(nand_ecc_sw_bch_get_engine);
403cdbe8df5SMiquel Raynal
404cdbe8df5SMiquel Raynal MODULE_LICENSE("GPL");
405cdbe8df5SMiquel Raynal MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
406cdbe8df5SMiquel Raynal MODULE_DESCRIPTION("NAND software BCH ECC support");
407