]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mtd/nand/nand_bch.c
35d2140dabc1db28169baca7e96b33989e452798
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.
5 * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
7 * SPDX-License-Identifier: GPL-2.0+
11 /*#include <asm/io.h>*/
12 #include <linux/types.h>
14 #include <linux/bitops.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/nand.h>
17 #include <linux/mtd/nand_bch.h>
18 #include <linux/bch.h>
22 * struct nand_bch_control - private NAND BCH control structure
23 * @bch: BCH control structure
24 * @ecclayout: private ecc layout for this BCH configuration
25 * @errloc: error location array
26 * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid
28 struct nand_bch_control
{
29 struct bch_control
*bch
;
30 struct nand_ecclayout ecclayout
;
32 unsigned char *eccmask
;
36 * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block
37 * @mtd: MTD block structure
38 * @buf: input buffer with raw data
39 * @code: output buffer with ECC
41 int nand_bch_calculate_ecc(struct mtd_info
*mtd
, const unsigned char *buf
,
44 const struct nand_chip
*chip
= mtd
->priv
;
45 struct nand_bch_control
*nbc
= chip
->ecc
.priv
;
48 memset(code
, 0, chip
->ecc
.bytes
);
49 encode_bch(nbc
->bch
, buf
, chip
->ecc
.size
, code
);
51 /* apply mask so that an erased page is a valid codeword */
52 for (i
= 0; i
< chip
->ecc
.bytes
; i
++)
53 code
[i
] ^= nbc
->eccmask
[i
];
59 * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
60 * @mtd: MTD block structure
61 * @buf: raw data read from the chip
62 * @read_ecc: ECC from the chip
63 * @calc_ecc: the ECC calculated from raw data
65 * Detect and correct bit errors for a data byte block
67 int nand_bch_correct_data(struct mtd_info
*mtd
, unsigned char *buf
,
68 unsigned char *read_ecc
, unsigned char *calc_ecc
)
70 const struct nand_chip
*chip
= mtd
->priv
;
71 struct nand_bch_control
*nbc
= chip
->ecc
.priv
;
72 unsigned int *errloc
= nbc
->errloc
;
75 count
= decode_bch(nbc
->bch
, NULL
, chip
->ecc
.size
, read_ecc
, calc_ecc
,
78 for (i
= 0; i
< count
; i
++) {
79 if (errloc
[i
] < (chip
->ecc
.size
*8))
80 /* error is located in data, correct it */
81 buf
[errloc
[i
] >> 3] ^= (1 << (errloc
[i
] & 7));
82 /* else error in ecc, no action needed */
84 MTDDEBUG(MTD_DEBUG_LEVEL0
, "%s: corrected bitflip %u\n",
87 } else if (count
< 0) {
88 printk(KERN_ERR
"ecc unrecoverable error\n");
95 * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
96 * @mtd: MTD block structure
97 * @eccsize: ecc block size in bytes
98 * @eccbytes: ecc length in bytes
99 * @ecclayout: output default layout
102 * a pointer to a new NAND BCH control structure, or NULL upon failure
104 * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
105 * are used to compute BCH parameters m (Galois field order) and t (error
106 * correction capability). @eccbytes should be equal to the number of bytes
107 * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
109 * Example: to configure 4 bit correction per 512 bytes, you should pass
110 * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
111 * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
113 struct nand_bch_control
*
114 nand_bch_init(struct mtd_info
*mtd
, unsigned int eccsize
, unsigned int eccbytes
,
115 struct nand_ecclayout
**ecclayout
)
117 unsigned int m
, t
, eccsteps
, i
;
118 struct nand_ecclayout
*layout
;
119 struct nand_bch_control
*nbc
= NULL
;
120 unsigned char *erased_page
;
122 if (!eccsize
|| !eccbytes
) {
123 printk(KERN_WARNING
"ecc parameters not supplied\n");
127 m
= fls(1+8*eccsize
);
130 nbc
= kzalloc(sizeof(*nbc
), GFP_KERNEL
);
134 nbc
->bch
= init_bch(m
, t
, 0);
138 /* verify that eccbytes has the expected value */
139 if (nbc
->bch
->ecc_bytes
!= eccbytes
) {
140 printk(KERN_WARNING
"invalid eccbytes %u, should be %u\n",
141 eccbytes
, nbc
->bch
->ecc_bytes
);
145 eccsteps
= mtd
->writesize
/eccsize
;
147 /* if no ecc placement scheme was provided, build one */
150 /* handle large page devices only */
151 if (mtd
->oobsize
< 64) {
152 printk(KERN_WARNING
"must provide an oob scheme for "
153 "oobsize %d\n", mtd
->oobsize
);
157 layout
= &nbc
->ecclayout
;
158 layout
->eccbytes
= eccsteps
*eccbytes
;
160 /* reserve 2 bytes for bad block marker */
161 if (layout
->eccbytes
+2 > mtd
->oobsize
) {
162 printk(KERN_WARNING
"no suitable oob scheme available "
163 "for oobsize %d eccbytes %u\n", mtd
->oobsize
,
167 /* put ecc bytes at oob tail */
168 for (i
= 0; i
< layout
->eccbytes
; i
++)
169 layout
->eccpos
[i
] = mtd
->oobsize
-layout
->eccbytes
+i
;
171 layout
->oobfree
[0].offset
= 2;
172 layout
->oobfree
[0].length
= mtd
->oobsize
-2-layout
->eccbytes
;
178 if (8*(eccsize
+eccbytes
) >= (1 << m
)) {
179 printk(KERN_WARNING
"eccsize %u is too large\n", eccsize
);
182 if ((*ecclayout
)->eccbytes
!= (eccsteps
*eccbytes
)) {
183 printk(KERN_WARNING
"invalid ecc layout\n");
187 nbc
->eccmask
= kmalloc(eccbytes
, GFP_KERNEL
);
188 nbc
->errloc
= kmalloc(t
*sizeof(*nbc
->errloc
), GFP_KERNEL
);
189 if (!nbc
->eccmask
|| !nbc
->errloc
)
192 * compute and store the inverted ecc of an erased ecc block
194 erased_page
= kmalloc(eccsize
, GFP_KERNEL
);
198 memset(erased_page
, 0xff, eccsize
);
199 memset(nbc
->eccmask
, 0, eccbytes
);
200 encode_bch(nbc
->bch
, erased_page
, eccsize
, nbc
->eccmask
);
203 for (i
= 0; i
< eccbytes
; i
++)
204 nbc
->eccmask
[i
] ^= 0xff;
213 * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
214 * @nbc: NAND BCH control structure
216 void nand_bch_free(struct nand_bch_control
*nbc
)