core/psd/imx6ull/bch.c
void encode_bch(struct bch_control *bch, const uint8_t *data,
		unsigned int len, uint8_t *ecc)
{
	const unsigned int l = BCH_ECC_WORDS(bch)-1;
	unsigned int i, mlen;
	unsigned long m;
	uint32_t w, r[l+1];
	const uint32_t * const tab0 = bch->mod8_tab;
	const uint32_t * const tab1 = tab0 + 256*(l+1);
	const uint32_t * const tab2 = tab1 + 256*(l+1);
	const uint32_t * const tab3 = tab2 + 256*(l+1);
	const uint32_t *pdata, *p0, *p1, *p2, *p3;

	if (ecc) {
		/* load ecc parity bytes into internal 32-bit buffer */
		load_ecc8(bch, bch->ecc_buf, ecc);
	} else {
		memset(bch->ecc_buf, 0, sizeof(r));
	}

	/* process first unaligned data bytes */
	m = ((unsigned long)data) & 3;
	if (m) {
		mlen = (len < (4-m)) ? len : 4-m;
		encode_bch_unaligned(bch, data, mlen, bch->ecc_buf);
		data += mlen;
		len  -= mlen;
	}

	/* process 32-bit aligned data words */
	pdata = (uint32_t *)data;
	mlen  = len/4;
	data += 4*mlen;
	len  -= 4*mlen;
	memcpy(r, bch->ecc_buf, sizeof(r));

	/*
	 * split each 32-bit word into 4 polynomials of weight 8 as follows:
	 *
	 * 31 ...24  23 ...16  15 ... 8  7 ... 0
	 * xxxxxxxx  yyyyyyyy  zzzzzzzz  tttttttt
	 *                               tttttttt  mod g = r0 (precomputed)
	 *                     zzzzzzzz  00000000  mod g = r1 (precomputed)
	 *           yyyyyyyy  00000000  00000000  mod g = r2 (precomputed)
	 * xxxxxxxx  00000000  00000000  00000000  mod g = r3 (precomputed)
	 * xxxxxxxx  yyyyyyyy  zzzzzzzz  tttttttt  mod g = r0^r1^r2^r3
	 */
	while (mlen--) {
		/* input data is read in big-endian format */
		/*TODO: big little endian*/
		/*w = r[0]^cpu_to_be32(*pdata++);*/
		/*w = r[0]^(uint32_t)(*pdata++);*/
		w = r[0]^swap_data(*pdata++);
		p0 = tab0 + (l+1)*((w >>  0) & 0xff);
		p1 = tab1 + (l+1)*((w >>  8) & 0xff);
		p2 = tab2 + (l+1)*((w >> 16) & 0xff);
		p3 = tab3 + (l+1)*((w >> 24) & 0xff);

		for (i = 0; i < l; i++)
			r[i] = r[i+1]^p0[i]^p1[i]^p2[i]^p3[i];

		r[l] = p0[l]^p1[l]^p2[l]^p3[l];
	}
	memcpy(bch->ecc_buf, r, sizeof(r));

	/* process last unaligned bytes */
	if (len)
		encode_bch_unaligned(bch, data, len, bch->ecc_buf);

	/* store ecc parity bytes into original parity buffer */
	if (ecc)
		store_ecc8(bch, ecc, bch->ecc_buf);
}
/**
 * encode_bch - calculate BCH ecc parity of data
 * @bch:   BCH control structure
 * @data:  data to encode
 * @len:   data length in bytes
 * @ecc:   ecc parity data, must be initialized by caller
 *
 * The @ecc parity array is used both as input and output parameter, in order to
 * allow incremental computations. It should be of the size indicated by member
 * @ecc_bytes of @bch, and should be initialized to 0 before the first call.
 *
 * The exact number of computed ecc parity bits is given by member @ecc_bits of
 * @bch; it may be less than m*t for large values of t.
 */
calculate BCH ecc parity of data
@bch:   BCH control structure
@data:  data to encode
@len:   data length in bytes
@ecc:   ecc parity data, must be initialized by caller

The @ecc parity array is used both as input and output parameter, in order to
allow incremental computations. It should be of the size indicated by member
@ecc_bytes of @bch, and should be initialized to 0 before the first call.

The exact number of computed ecc parity bits is given by member @ecc_bits of
@bch; it may be less than m*t for large values of t.
calculate BCH ecc parity of data @bch : BCH control structure @data : data to encode @len : data length in bytes @ecc : ecc parity data must be initialized by caller The @ecc parity array is used both as input and output parameter in order to allow incremental computations . It should be of the size indicated by member @ecc_bytes of @bch and should be initialized to 0 before the first call . The exact number of computed ecc parity bits is given by member @ecc_bits of @bch ; it may be less than m * t for large values of t .
void encode_bch(struct bch_control *bch, const uint8_t *data,
		unsigned int len, uint8_t *ecc)
{
	const unsigned int l = BCH_ECC_WORDS(bch)-1;
	unsigned int i, mlen;
	unsigned long m;
	uint32_t w, r[l+1];
	const uint32_t * const tab0 = bch->mod8_tab;
	const uint32_t * const tab1 = tab0 + 256*(l+1);
	const uint32_t * const tab2 = tab1 + 256*(l+1);
	const uint32_t * const tab3 = tab2 + 256*(l+1);
	const uint32_t *pdata, *p0, *p1, *p2, *p3;
	if (ecc) {
		load_ecc8(bch, bch->ecc_buf, ecc);
	} else {
		memset(bch->ecc_buf, 0, sizeof(r));
	}
	m = ((unsigned long)data) & 3;
	if (m) {
		mlen = (len < (4-m)) ? len : 4-m;
		encode_bch_unaligned(bch, data, mlen, bch->ecc_buf);
		data += mlen;
		len  -= mlen;
	}
	pdata = (uint32_t *)data;
	mlen  = len/4;
	data += 4*mlen;
	len  -= 4*mlen;
	memcpy(r, bch->ecc_buf, sizeof(r));
	while (mlen--) {
		w = r[0]^swap_data(*pdata++);
		p0 = tab0 + (l+1)*((w >>  0) & 0xff);
		p1 = tab1 + (l+1)*((w >>  8) & 0xff);
		p2 = tab2 + (l+1)*((w >> 16) & 0xff);
		p3 = tab3 + (l+1)*((w >> 24) & 0xff);
		for (i = 0; i < l; i++)
			r[i] = r[i+1]^p0[i]^p1[i]^p2[i]^p3[i];
		r[l] = p0[l]^p1[l]^p2[l]^p3[l];
	}
	memcpy(bch->ecc_buf, r, sizeof(r));
	if (len)
		encode_bch_unaligned(bch, data, len, bch->ecc_buf);
	if (ecc)
		store_ecc8(bch, ecc, bch->ecc_buf);
}
void encode_bch ( struct bch_control * bch , const uint8_t * data , unsigned int len , uint8_t * ecc ) { const unsigned int l = BCH_ECC_WORDS ( bch ) - 1 ; unsigned int i , mlen ; unsigned long m ; uint32_t w , r [ l + 1 ] ; const uint32_t * const tab0 = bch -> mod8_tab ; const uint32_t * const tab1 = tab0 + 256 * ( l + 1 ) ; const uint32_t * const tab2 = tab1 + 256 * ( l + 1 ) ; const uint32_t * const tab3 = tab2 + 256 * ( l + 1 ) ; const uint32_t * pdata , * p0 , * p1 , * p2 , * p3 ; if ( ecc ) { load_ecc8 ( bch , bch -> ecc_buf , ecc ) ; } else { memset ( bch -> ecc_buf , 0 , sizeof ( r ) ) ; } m = ( ( unsigned long ) data ) & 3 ; if ( m ) { mlen = ( len < ( 4 - m ) ) ? len : 4 - m ; encode_bch_unaligned ( bch , data , mlen , bch -> ecc_buf ) ; data += mlen ; len -= mlen ; } pdata = ( uint32_t * ) data ; mlen = len / 4 ; data += 4 * mlen ; len -= 4 * mlen ; memcpy ( r , bch -> ecc_buf , sizeof ( r ) ) ; while ( mlen -- ) { w = r [ 0 ] ^ swap_data ( * pdata ++ ) ; p0 = tab0 + ( l + 1 ) * ( ( w >> 0 ) & 0xff ) ; p1 = tab1 + ( l + 1 ) * ( ( w >> 8 ) & 0xff ) ; p2 = tab2 + ( l + 1 ) * ( ( w >> 16 ) & 0xff ) ; p3 = tab3 + ( l + 1 ) * ( ( w >> 24 ) & 0xff ) ; for ( i = 0 ; i < l ; i ++ ) r [ i ] = r [ i + 1 ] ^ p0 [ i ] ^ p1 [ i ] ^ p2 [ i ] ^ p3 [ i ] ; r [ l ] = p0 [ l ] ^ p1 [ l ] ^ p2 [ l ] ^ p3 [ l ] ; } memcpy ( bch -> ecc_buf , r , sizeof ( r ) ) ; if ( len ) encode_bch_unaligned ( bch , data , len , bch -> ecc_buf ) ; if ( ecc ) store_ecc8 ( bch , ecc , bch -> ecc_buf ) ; }
encode_bch - calculate BCH ecc parity of data
 @bch:   BCH control structure
 @data:  data to encode
 @len:   data length in bytes
 @ecc:   ecc parity data, must be initialized by caller
encode_bch - calculate BCH ecc parity of data @bch : BCH control structure @data : data to encode @len : data length in bytes @ecc : ecc parity data must be initialized by caller
/* load ecc parity bytes into internal 32-bit buffer */ /* process first unaligned data bytes */ /* process 32-bit aligned data words */ /*
	 * split each 32-bit word into 4 polynomials of weight 8 as follows:
	 *
	 * 31 ...24  23 ...16  15 ... 8  7 ... 0
	 * xxxxxxxx  yyyyyyyy  zzzzzzzz  tttttttt
	 *                               tttttttt  mod g = r0 (precomputed)
	 *                     zzzzzzzz  00000000  mod g = r1 (precomputed)
	 *           yyyyyyyy  00000000  00000000  mod g = r2 (precomputed)
	 * xxxxxxxx  00000000  00000000  00000000  mod g = r3 (precomputed)
	 * xxxxxxxx  yyyyyyyy  zzzzzzzz  tttttttt  mod g = r0^r1^r2^r3
	 */ /* input data is read in big-endian format */ /*TODO: big little endian*/ /*w = r[0]^cpu_to_be32(*pdata++);*/ /*w = r[0]^(uint32_t)(*pdata++);*/ /* process last unaligned bytes */ /* store ecc parity bytes into original parity buffer */