xref: /openbmc/u-boot/include/linux/bch.h (revision e8f80a5a)
1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0 */
24c6de856SChristian Hitz /*
34c6de856SChristian Hitz  * Generic binary BCH encoding/decoding library
44c6de856SChristian Hitz  *
54c6de856SChristian Hitz  * Copyright © 2011 Parrot S.A.
64c6de856SChristian Hitz  *
74c6de856SChristian Hitz  * Author: Ivan Djelic <ivan.djelic@parrot.com>
84c6de856SChristian Hitz  *
94c6de856SChristian Hitz  * Description:
104c6de856SChristian Hitz  *
114c6de856SChristian Hitz  * This library provides runtime configurable encoding/decoding of binary
124c6de856SChristian Hitz  * Bose-Chaudhuri-Hocquenghem (BCH) codes.
134c6de856SChristian Hitz */
144c6de856SChristian Hitz #ifndef _BCH_H
154c6de856SChristian Hitz #define _BCH_H
164c6de856SChristian Hitz 
174c6de856SChristian Hitz #include <linux/types.h>
184c6de856SChristian Hitz 
194c6de856SChristian Hitz /**
204c6de856SChristian Hitz  * struct bch_control - BCH control structure
214c6de856SChristian Hitz  * @m:          Galois field order
224c6de856SChristian Hitz  * @n:          maximum codeword size in bits (= 2^m-1)
234c6de856SChristian Hitz  * @t:          error correction capability in bits
244c6de856SChristian Hitz  * @ecc_bits:   ecc exact size in bits, i.e. generator polynomial degree (<=m*t)
254c6de856SChristian Hitz  * @ecc_bytes:  ecc max size (m*t bits) in bytes
264c6de856SChristian Hitz  * @a_pow_tab:  Galois field GF(2^m) exponentiation lookup table
274c6de856SChristian Hitz  * @a_log_tab:  Galois field GF(2^m) log lookup table
284c6de856SChristian Hitz  * @mod8_tab:   remainder generator polynomial lookup tables
294c6de856SChristian Hitz  * @ecc_buf:    ecc parity words buffer
304c6de856SChristian Hitz  * @ecc_buf2:   ecc parity words buffer
314c6de856SChristian Hitz  * @xi_tab:     GF(2^m) base for solving degree 2 polynomial roots
324c6de856SChristian Hitz  * @syn:        syndrome buffer
334c6de856SChristian Hitz  * @cache:      log-based polynomial representation buffer
344c6de856SChristian Hitz  * @elp:        error locator polynomial
354c6de856SChristian Hitz  * @poly_2t:    temporary polynomials of degree 2t
364c6de856SChristian Hitz  */
374c6de856SChristian Hitz struct bch_control {
384c6de856SChristian Hitz 	unsigned int    m;
394c6de856SChristian Hitz 	unsigned int    n;
404c6de856SChristian Hitz 	unsigned int    t;
414c6de856SChristian Hitz 	unsigned int    ecc_bits;
424c6de856SChristian Hitz 	unsigned int    ecc_bytes;
434c6de856SChristian Hitz /* private: */
444c6de856SChristian Hitz 	uint16_t       *a_pow_tab;
454c6de856SChristian Hitz 	uint16_t       *a_log_tab;
464c6de856SChristian Hitz 	uint32_t       *mod8_tab;
474c6de856SChristian Hitz 	uint32_t       *ecc_buf;
484c6de856SChristian Hitz 	uint32_t       *ecc_buf2;
494c6de856SChristian Hitz 	unsigned int   *xi_tab;
504c6de856SChristian Hitz 	unsigned int   *syn;
514c6de856SChristian Hitz 	int            *cache;
524c6de856SChristian Hitz 	struct gf_poly *elp;
534c6de856SChristian Hitz 	struct gf_poly *poly_2t[4];
544c6de856SChristian Hitz };
554c6de856SChristian Hitz 
564c6de856SChristian Hitz struct bch_control *init_bch(int m, int t, unsigned int prim_poly);
574c6de856SChristian Hitz 
584c6de856SChristian Hitz void free_bch(struct bch_control *bch);
594c6de856SChristian Hitz 
604c6de856SChristian Hitz void encode_bch(struct bch_control *bch, const uint8_t *data,
614c6de856SChristian Hitz 		unsigned int len, uint8_t *ecc);
624c6de856SChristian Hitz 
634c6de856SChristian Hitz int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len,
644c6de856SChristian Hitz 	       const uint8_t *recv_ecc, const uint8_t *calc_ecc,
654c6de856SChristian Hitz 	       const unsigned int *syn, unsigned int *errloc);
664c6de856SChristian Hitz 
674c6de856SChristian Hitz #endif /* _BCH_H */
68