xref: /openbmc/u-boot/include/linux/bch.h (revision 3ba98ed8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Generic binary BCH encoding/decoding library
4  *
5  * Copyright © 2011 Parrot S.A.
6  *
7  * Author: Ivan Djelic <ivan.djelic@parrot.com>
8  *
9  * Description:
10  *
11  * This library provides runtime configurable encoding/decoding of binary
12  * Bose-Chaudhuri-Hocquenghem (BCH) codes.
13 */
14 #ifndef _BCH_H
15 #define _BCH_H
16 
17 #include <linux/types.h>
18 
19 /**
20  * struct bch_control - BCH control structure
21  * @m:          Galois field order
22  * @n:          maximum codeword size in bits (= 2^m-1)
23  * @t:          error correction capability in bits
24  * @ecc_bits:   ecc exact size in bits, i.e. generator polynomial degree (<=m*t)
25  * @ecc_bytes:  ecc max size (m*t bits) in bytes
26  * @a_pow_tab:  Galois field GF(2^m) exponentiation lookup table
27  * @a_log_tab:  Galois field GF(2^m) log lookup table
28  * @mod8_tab:   remainder generator polynomial lookup tables
29  * @ecc_buf:    ecc parity words buffer
30  * @ecc_buf2:   ecc parity words buffer
31  * @xi_tab:     GF(2^m) base for solving degree 2 polynomial roots
32  * @syn:        syndrome buffer
33  * @cache:      log-based polynomial representation buffer
34  * @elp:        error locator polynomial
35  * @poly_2t:    temporary polynomials of degree 2t
36  */
37 struct bch_control {
38 	unsigned int    m;
39 	unsigned int    n;
40 	unsigned int    t;
41 	unsigned int    ecc_bits;
42 	unsigned int    ecc_bytes;
43 /* private: */
44 	uint16_t       *a_pow_tab;
45 	uint16_t       *a_log_tab;
46 	uint32_t       *mod8_tab;
47 	uint32_t       *ecc_buf;
48 	uint32_t       *ecc_buf2;
49 	unsigned int   *xi_tab;
50 	unsigned int   *syn;
51 	int            *cache;
52 	struct gf_poly *elp;
53 	struct gf_poly *poly_2t[4];
54 };
55 
56 struct bch_control *init_bch(int m, int t, unsigned int prim_poly);
57 
58 void free_bch(struct bch_control *bch);
59 
60 void encode_bch(struct bch_control *bch, const uint8_t *data,
61 		unsigned int len, uint8_t *ecc);
62 
63 int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len,
64 	       const uint8_t *recv_ecc, const uint8_t *calc_ecc,
65 	       const unsigned int *syn, unsigned int *errloc);
66 
67 #endif /* _BCH_H */
68