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