diff --git a/common/Kconfig b/common/Kconfig index f7ff046..aac9f1a 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -101,6 +101,7 @@ depends on BAREBOX_UPDATE depends on MTD_WRITE depends on NAND_MXS + select BCH if ARCH_IMX6 default y config UBIFORMAT diff --git a/common/imx-bbu-nand-fcb.c b/common/imx-bbu-nand-fcb.c index 34a5f83..5d3d3f7 100644 --- a/common/imx-bbu-nand-fcb.c +++ b/common/imx-bbu-nand-fcb.c @@ -33,9 +33,12 @@ #include #include #include +#include +#include #include #include #include +#include #include struct dbbt_block { @@ -129,6 +132,132 @@ return p; } +static uint8_t reverse_bit(uint8_t b) +{ + b = (b & 0xf0) >> 4 | (b & 0x0f) << 4; + b = (b & 0xcc) >> 2 | (b & 0x33) << 2; + b = (b & 0xaa) >> 1 | (b & 0x55) << 1; + + return b; +} + +static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits) +{ + int i, j, m = 13; + int blocksize = 128; + int numblocks = 8; + int ecc_buf_size = (m * eccbits + 7) / 8; + struct bch_control *bch = init_bch(m, eccbits, 0); + uint8_t *ecc_buf = xmalloc(ecc_buf_size); + uint8_t *tmp_buf = xzalloc(blocksize * numblocks); + uint8_t *psrc, *pdst; + + /* + * The blocks here are bit aligned. If eccbits is a multiple of 8, + * we just can copy bytes. Otherwiese we must move the blocks to + * the next free bit position. + */ + BUG_ON(eccbits % 8); + + memcpy(tmp_buf, fcb, sizeof(*fcb)); + + for (i = 0; i < numblocks; i++) { + memset(ecc_buf, 0, ecc_buf_size); + psrc = tmp_buf + i * blocksize; + pdst = buf + i * (blocksize + ecc_buf_size); + + /* copy data byte aligned to destination buf */ + memcpy(pdst, psrc, blocksize); + + /* + * imx-kobs use a modified encode_bch which reverse the + * bit order of the data before calculating bch. + * Do this in the buffer and use the bch lib here. + */ + for (j = 0; j < blocksize; j++) + psrc[j] = reverse_bit(psrc[j]); + + encode_bch(bch, psrc, blocksize, ecc_buf); + + /* reverse ecc bit */ + for (j = 0; j < ecc_buf_size; j++) + ecc_buf[j] = reverse_bit(ecc_buf[j]); + + /* Here eccbuf is byte aligned and we can just copy it */ + memcpy(pdst + blocksize, ecc_buf, ecc_buf_size); + } + + free(ecc_buf); + free(tmp_buf); + free_bch(bch); +} + +struct fcb_block *read_fcb_bch(void *rawpage, int eccbits) +{ + int i, j, ret, errbit, m = 13; + int blocksize = 128; + int numblocks = 8; + int ecc_buf_size = (m * eccbits + 7) / 8; + struct bch_control *bch = init_bch(m, eccbits, 0); + uint8_t *fcb = xmalloc(numblocks * blocksize); + uint8_t *ecc_buf = xmalloc(ecc_buf_size); + uint8_t *data_buf = xmalloc(blocksize); + unsigned int *errloc = xmalloc(eccbits * sizeof(*errloc)); + uint8_t *psrc, *pdst; + + /* see encode_bch_ecc */ + BUG_ON(eccbits % 8); + + for (i = 0; i < numblocks; i++) { + psrc = rawpage + 32 + i * (blocksize + ecc_buf_size); + pdst = fcb + i * blocksize; + + /* reverse data bit */ + for (j = 0; j < blocksize; j++) + data_buf[j] = reverse_bit(psrc[j]); + + /* reverse ecc bit */ + for (j = 0; j < ecc_buf_size; j++) + ecc_buf[j] = reverse_bit(psrc[j + blocksize]); + + ret = decode_bch(bch, data_buf, blocksize, ecc_buf, + NULL, NULL, errloc); + + if (ret < 0) { + pr_err("Uncorrectable error at block %d\n", i); + free(fcb); + fcb = ERR_PTR(ret); + goto out; + } + if (ret > 0) + pr_info("Found %d correctable errors in block %d\n", + ret, i); + + for (j = 0; j < ret; j++) { + /* + * calculate the reverse position + * pos - (pos % 8) -> byte offset + * 7 - (pos % 8) -> reverse bit position + */ + errbit = errloc[j] - 2 * (errloc[j] % 8) + 7; + pr_debug("Found error: bit %d in block %d\n", + errbit, i); + if (errbit < blocksize * 8) + change_bit(errbit, psrc); + /* else error in ecc, ignore it */ + } + memcpy(pdst, psrc, blocksize); + } + +out: + free(data_buf); + free(ecc_buf); + free(errloc); + free_bch(bch); + + return (struct fcb_block *)fcb; +} + static void encode_hamming_13_8(void *_src, void *_ecc, size_t size) { int i; @@ -312,7 +441,11 @@ goto err; } - fcb = read_fcb_hamming_13_8(rawpage); + if (cpu_is_mx6ul()) + fcb = read_fcb_bch(rawpage, 40); + else + fcb = read_fcb_hamming_13_8(rawpage); + if (IS_ERR(fcb)) { pr_err("Cannot read fcb\n"); ret = PTR_ERR(fcb); @@ -766,8 +899,14 @@ fcb_raw_page = xzalloc(mtd->writesize + mtd->oobsize); - memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block)); - encode_hamming_13_8(fcb_raw_page + 12, fcb_raw_page + 12 + 512, 512); + if (cpu_is_mx6ul()) { + /* 40 bit BCH, for i.MX6UL */ + encode_bch_ecc(fcb_raw_page + 32, fcb, 40); + } else { + memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block)); + encode_hamming_13_8(fcb_raw_page + 12, + fcb_raw_page + 12 + 512, 512); + } dbbt = dbbt_data_create(mtd);