blob: 9e3c2da0f3b1743a7e9f6f96a912a36b49048275 [file] [log] [blame]
Ivan Djelic193bd402011-03-11 11:05:33 +01001/*
2 * This file provides ECC correction for more than 1 bit per block of data,
3 * using binary BCH codes. It relies on the generic BCH library lib/bch.c.
4 *
5 * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
6 *
7 * This file is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 or (at your option) any
10 * later version.
11 *
12 * This file is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this file; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 */
21
22#include <linux/types.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/slab.h>
26#include <linux/bitops.h>
27#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020028#include <linux/mtd/rawnand.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010029#include <linux/mtd/nand_bch.h>
30#include <linux/bch.h>
31
32/**
33 * struct nand_bch_control - private NAND BCH control structure
34 * @bch: BCH control structure
Ivan Djelic193bd402011-03-11 11:05:33 +010035 * @errloc: error location array
36 * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid
37 */
38struct nand_bch_control {
39 struct bch_control *bch;
Ivan Djelic193bd402011-03-11 11:05:33 +010040 unsigned int *errloc;
41 unsigned char *eccmask;
42};
43
44/**
45 * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block
Boris Brezillonaf37d2c2018-09-06 14:05:18 +020046 * @chip: NAND chip object
Ivan Djelic193bd402011-03-11 11:05:33 +010047 * @buf: input buffer with raw data
48 * @code: output buffer with ECC
49 */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +020050int nand_bch_calculate_ecc(struct nand_chip *chip, const unsigned char *buf,
Ivan Djelic193bd402011-03-11 11:05:33 +010051 unsigned char *code)
52{
Ivan Djelic193bd402011-03-11 11:05:33 +010053 struct nand_bch_control *nbc = chip->ecc.priv;
54 unsigned int i;
55
56 memset(code, 0, chip->ecc.bytes);
57 encode_bch(nbc->bch, buf, chip->ecc.size, code);
58
59 /* apply mask so that an erased page is a valid codeword */
60 for (i = 0; i < chip->ecc.bytes; i++)
61 code[i] ^= nbc->eccmask[i];
62
63 return 0;
64}
65EXPORT_SYMBOL(nand_bch_calculate_ecc);
66
67/**
68 * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
69 * @mtd: MTD block structure
70 * @buf: raw data read from the chip
71 * @read_ecc: ECC from the chip
72 * @calc_ecc: the ECC calculated from raw data
73 *
74 * Detect and correct bit errors for a data byte block
75 */
76int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
77 unsigned char *read_ecc, unsigned char *calc_ecc)
78{
Boris BREZILLON862eba52015-12-01 12:03:03 +010079 const struct nand_chip *chip = mtd_to_nand(mtd);
Ivan Djelic193bd402011-03-11 11:05:33 +010080 struct nand_bch_control *nbc = chip->ecc.priv;
81 unsigned int *errloc = nbc->errloc;
82 int i, count;
83
84 count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
85 NULL, errloc);
86 if (count > 0) {
87 for (i = 0; i < count; i++) {
88 if (errloc[i] < (chip->ecc.size*8))
89 /* error is located in data, correct it */
90 buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
91 /* else error in ecc, no action needed */
92
Brian Norris0a32a102011-07-19 10:06:10 -070093 pr_debug("%s: corrected bitflip %u\n", __func__,
94 errloc[i]);
Ivan Djelic193bd402011-03-11 11:05:33 +010095 }
96 } else if (count < 0) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +053097 pr_err("ecc unrecoverable error\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +010098 count = -EBADMSG;
Ivan Djelic193bd402011-03-11 11:05:33 +010099 }
100 return count;
101}
102EXPORT_SYMBOL(nand_bch_correct_data);
103
104/**
105 * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
106 * @mtd: MTD block structure
Ivan Djelic193bd402011-03-11 11:05:33 +0100107 *
108 * Returns:
109 * a pointer to a new NAND BCH control structure, or NULL upon failure
110 *
111 * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
112 * are used to compute BCH parameters m (Galois field order) and t (error
113 * correction capability). @eccbytes should be equal to the number of bytes
114 * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
115 *
116 * Example: to configure 4 bit correction per 512 bytes, you should pass
117 * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
118 * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
119 */
Boris BREZILLONa8c65d52016-03-07 10:46:54 +0100120struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
Ivan Djelic193bd402011-03-11 11:05:33 +0100121{
Boris BREZILLONa8c65d52016-03-07 10:46:54 +0100122 struct nand_chip *nand = mtd_to_nand(mtd);
Ivan Djelic193bd402011-03-11 11:05:33 +0100123 unsigned int m, t, eccsteps, i;
Ivan Djelic193bd402011-03-11 11:05:33 +0100124 struct nand_bch_control *nbc = NULL;
125 unsigned char *erased_page;
Boris BREZILLONa8c65d52016-03-07 10:46:54 +0100126 unsigned int eccsize = nand->ecc.size;
127 unsigned int eccbytes = nand->ecc.bytes;
128 unsigned int eccstrength = nand->ecc.strength;
129
130 if (!eccbytes && eccstrength) {
131 eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
132 nand->ecc.bytes = eccbytes;
133 }
Ivan Djelic193bd402011-03-11 11:05:33 +0100134
135 if (!eccsize || !eccbytes) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530136 pr_warn("ecc parameters not supplied\n");
Ivan Djelic193bd402011-03-11 11:05:33 +0100137 goto fail;
138 }
139
140 m = fls(1+8*eccsize);
141 t = (eccbytes*8)/m;
142
143 nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
144 if (!nbc)
145 goto fail;
146
147 nbc->bch = init_bch(m, t, 0);
148 if (!nbc->bch)
149 goto fail;
150
151 /* verify that eccbytes has the expected value */
152 if (nbc->bch->ecc_bytes != eccbytes) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530153 pr_warn("invalid eccbytes %u, should be %u\n",
154 eccbytes, nbc->bch->ecc_bytes);
Ivan Djelic193bd402011-03-11 11:05:33 +0100155 goto fail;
156 }
157
158 eccsteps = mtd->writesize/eccsize;
159
Boris Brezillon7cf9c192016-02-03 19:53:40 +0100160 /* Check that we have an oob layout description. */
161 if (!mtd->ooblayout) {
162 pr_warn("missing oob scheme");
163 goto fail;
Ivan Djelic193bd402011-03-11 11:05:33 +0100164 }
165
166 /* sanity checks */
167 if (8*(eccsize+eccbytes) >= (1 << m)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530168 pr_warn("eccsize %u is too large\n", eccsize);
Ivan Djelic193bd402011-03-11 11:05:33 +0100169 goto fail;
170 }
Boris Brezillon846031d2016-02-03 20:11:00 +0100171
172 /*
173 * ecc->steps and ecc->total might be used by mtd->ooblayout->ecc(),
174 * which is called by mtd_ooblayout_count_eccbytes().
175 * Make sure they are properly initialized before calling
176 * mtd_ooblayout_count_eccbytes().
Brian Norris3603ea02016-05-06 09:31:18 -0700177 * FIXME: we should probably rework the sequencing in nand_scan_tail()
Boris Brezillon846031d2016-02-03 20:11:00 +0100178 * to avoid setting those fields twice.
179 */
180 nand->ecc.steps = eccsteps;
181 nand->ecc.total = eccsteps * eccbytes;
182 if (mtd_ooblayout_count_eccbytes(mtd) != (eccsteps*eccbytes)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530183 pr_warn("invalid ecc layout\n");
Ivan Djelic193bd402011-03-11 11:05:33 +0100184 goto fail;
185 }
186
187 nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
Kees Cook6da2ec52018-06-12 13:55:00 -0700188 nbc->errloc = kmalloc_array(t, sizeof(*nbc->errloc), GFP_KERNEL);
Ivan Djelic193bd402011-03-11 11:05:33 +0100189 if (!nbc->eccmask || !nbc->errloc)
190 goto fail;
191 /*
192 * compute and store the inverted ecc of an erased ecc block
193 */
194 erased_page = kmalloc(eccsize, GFP_KERNEL);
195 if (!erased_page)
196 goto fail;
197
198 memset(erased_page, 0xff, eccsize);
199 memset(nbc->eccmask, 0, eccbytes);
200 encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
201 kfree(erased_page);
202
203 for (i = 0; i < eccbytes; i++)
204 nbc->eccmask[i] ^= 0xff;
205
Boris BREZILLONa8c65d52016-03-07 10:46:54 +0100206 if (!eccstrength)
207 nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
208
Ivan Djelic193bd402011-03-11 11:05:33 +0100209 return nbc;
210fail:
211 nand_bch_free(nbc);
212 return NULL;
213}
214EXPORT_SYMBOL(nand_bch_init);
215
216/**
217 * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
218 * @nbc: NAND BCH control structure
219 */
220void nand_bch_free(struct nand_bch_control *nbc)
221{
222 if (nbc) {
223 free_bch(nbc->bch);
224 kfree(nbc->errloc);
225 kfree(nbc->eccmask);
226 kfree(nbc);
227 }
228}
229EXPORT_SYMBOL(nand_bch_free);
230
231MODULE_LICENSE("GPL");
232MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
233MODULE_DESCRIPTION("NAND software BCH ECC support");