xref: /rk3399_rockchip-uboot/include/linux/bch.h (revision 4c6de8560cb42a6b6c2d565d41b6801e4b02d4b3)
1*4c6de856SChristian Hitz /*
2*4c6de856SChristian Hitz  * Generic binary BCH encoding/decoding library
3*4c6de856SChristian Hitz  *
4*4c6de856SChristian Hitz  * This program is free software; you can redistribute it and/or modify it
5*4c6de856SChristian Hitz  * under the terms of the GNU General Public License version 2 as published by
6*4c6de856SChristian Hitz  * the Free Software Foundation.
7*4c6de856SChristian Hitz  *
8*4c6de856SChristian Hitz  * This program is distributed in the hope that it will be useful, but WITHOUT
9*4c6de856SChristian Hitz  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10*4c6de856SChristian Hitz  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11*4c6de856SChristian Hitz  * more details.
12*4c6de856SChristian Hitz  *
13*4c6de856SChristian Hitz  * You should have received a copy of the GNU General Public License along with
14*4c6de856SChristian Hitz  * this program; if not, write to the Free Software Foundation, Inc., 51
15*4c6de856SChristian Hitz  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16*4c6de856SChristian Hitz  *
17*4c6de856SChristian Hitz  * Copyright © 2011 Parrot S.A.
18*4c6de856SChristian Hitz  *
19*4c6de856SChristian Hitz  * Author: Ivan Djelic <ivan.djelic@parrot.com>
20*4c6de856SChristian Hitz  *
21*4c6de856SChristian Hitz  * Description:
22*4c6de856SChristian Hitz  *
23*4c6de856SChristian Hitz  * This library provides runtime configurable encoding/decoding of binary
24*4c6de856SChristian Hitz  * Bose-Chaudhuri-Hocquenghem (BCH) codes.
25*4c6de856SChristian Hitz */
26*4c6de856SChristian Hitz #ifndef _BCH_H
27*4c6de856SChristian Hitz #define _BCH_H
28*4c6de856SChristian Hitz 
29*4c6de856SChristian Hitz #include <linux/types.h>
30*4c6de856SChristian Hitz 
31*4c6de856SChristian Hitz /**
32*4c6de856SChristian Hitz  * struct bch_control - BCH control structure
33*4c6de856SChristian Hitz  * @m:          Galois field order
34*4c6de856SChristian Hitz  * @n:          maximum codeword size in bits (= 2^m-1)
35*4c6de856SChristian Hitz  * @t:          error correction capability in bits
36*4c6de856SChristian Hitz  * @ecc_bits:   ecc exact size in bits, i.e. generator polynomial degree (<=m*t)
37*4c6de856SChristian Hitz  * @ecc_bytes:  ecc max size (m*t bits) in bytes
38*4c6de856SChristian Hitz  * @a_pow_tab:  Galois field GF(2^m) exponentiation lookup table
39*4c6de856SChristian Hitz  * @a_log_tab:  Galois field GF(2^m) log lookup table
40*4c6de856SChristian Hitz  * @mod8_tab:   remainder generator polynomial lookup tables
41*4c6de856SChristian Hitz  * @ecc_buf:    ecc parity words buffer
42*4c6de856SChristian Hitz  * @ecc_buf2:   ecc parity words buffer
43*4c6de856SChristian Hitz  * @xi_tab:     GF(2^m) base for solving degree 2 polynomial roots
44*4c6de856SChristian Hitz  * @syn:        syndrome buffer
45*4c6de856SChristian Hitz  * @cache:      log-based polynomial representation buffer
46*4c6de856SChristian Hitz  * @elp:        error locator polynomial
47*4c6de856SChristian Hitz  * @poly_2t:    temporary polynomials of degree 2t
48*4c6de856SChristian Hitz  */
49*4c6de856SChristian Hitz struct bch_control {
50*4c6de856SChristian Hitz 	unsigned int    m;
51*4c6de856SChristian Hitz 	unsigned int    n;
52*4c6de856SChristian Hitz 	unsigned int    t;
53*4c6de856SChristian Hitz 	unsigned int    ecc_bits;
54*4c6de856SChristian Hitz 	unsigned int    ecc_bytes;
55*4c6de856SChristian Hitz /* private: */
56*4c6de856SChristian Hitz 	uint16_t       *a_pow_tab;
57*4c6de856SChristian Hitz 	uint16_t       *a_log_tab;
58*4c6de856SChristian Hitz 	uint32_t       *mod8_tab;
59*4c6de856SChristian Hitz 	uint32_t       *ecc_buf;
60*4c6de856SChristian Hitz 	uint32_t       *ecc_buf2;
61*4c6de856SChristian Hitz 	unsigned int   *xi_tab;
62*4c6de856SChristian Hitz 	unsigned int   *syn;
63*4c6de856SChristian Hitz 	int            *cache;
64*4c6de856SChristian Hitz 	struct gf_poly *elp;
65*4c6de856SChristian Hitz 	struct gf_poly *poly_2t[4];
66*4c6de856SChristian Hitz };
67*4c6de856SChristian Hitz 
68*4c6de856SChristian Hitz struct bch_control *init_bch(int m, int t, unsigned int prim_poly);
69*4c6de856SChristian Hitz 
70*4c6de856SChristian Hitz void free_bch(struct bch_control *bch);
71*4c6de856SChristian Hitz 
72*4c6de856SChristian Hitz void encode_bch(struct bch_control *bch, const uint8_t *data,
73*4c6de856SChristian Hitz 		unsigned int len, uint8_t *ecc);
74*4c6de856SChristian Hitz 
75*4c6de856SChristian Hitz int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len,
76*4c6de856SChristian Hitz 	       const uint8_t *recv_ecc, const uint8_t *calc_ecc,
77*4c6de856SChristian Hitz 	       const unsigned int *syn, unsigned int *errloc);
78*4c6de856SChristian Hitz 
79*4c6de856SChristian Hitz #endif /* _BCH_H */
80