nand_bch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * This file provides ECC correction for more than 1 bit per block of data,
  3. * using binary BCH codes. It relies on the generic BCH library lib/bch.c.
  4. *
  5. * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. /*#include <asm/io.h>*/
  11. #include <linux/types.h>
  12. #include <linux/bitops.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/nand.h>
  15. #include <linux/mtd/nand_bch.h>
  16. #include <linux/bch.h>
  17. #include <malloc.h>
  18. /**
  19. * struct nand_bch_control - private NAND BCH control structure
  20. * @bch: BCH control structure
  21. * @ecclayout: private ecc layout for this BCH configuration
  22. * @errloc: error location array
  23. * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid
  24. */
  25. struct nand_bch_control {
  26. struct bch_control *bch;
  27. struct nand_ecclayout ecclayout;
  28. unsigned int *errloc;
  29. unsigned char *eccmask;
  30. };
  31. /**
  32. * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block
  33. * @mtd: MTD block structure
  34. * @buf: input buffer with raw data
  35. * @code: output buffer with ECC
  36. */
  37. int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  38. unsigned char *code)
  39. {
  40. const struct nand_chip *chip = mtd_to_nand(mtd);
  41. struct nand_bch_control *nbc = chip->ecc.priv;
  42. unsigned int i;
  43. memset(code, 0, chip->ecc.bytes);
  44. encode_bch(nbc->bch, buf, chip->ecc.size, code);
  45. /* apply mask so that an erased page is a valid codeword */
  46. for (i = 0; i < chip->ecc.bytes; i++)
  47. code[i] ^= nbc->eccmask[i];
  48. return 0;
  49. }
  50. /**
  51. * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
  52. * @mtd: MTD block structure
  53. * @buf: raw data read from the chip
  54. * @read_ecc: ECC from the chip
  55. * @calc_ecc: the ECC calculated from raw data
  56. *
  57. * Detect and correct bit errors for a data byte block
  58. */
  59. int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  60. unsigned char *read_ecc, unsigned char *calc_ecc)
  61. {
  62. const struct nand_chip *chip = mtd_to_nand(mtd);
  63. struct nand_bch_control *nbc = chip->ecc.priv;
  64. unsigned int *errloc = nbc->errloc;
  65. int i, count;
  66. count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
  67. NULL, errloc);
  68. if (count > 0) {
  69. for (i = 0; i < count; i++) {
  70. if (errloc[i] < (chip->ecc.size*8))
  71. /* error is located in data, correct it */
  72. buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
  73. /* else error in ecc, no action needed */
  74. MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: corrected bitflip %u\n",
  75. __func__, errloc[i]);
  76. }
  77. } else if (count < 0) {
  78. printk(KERN_ERR "ecc unrecoverable error\n");
  79. count = -1;
  80. }
  81. return count;
  82. }
  83. /**
  84. * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  85. * @mtd: MTD block structure
  86. * @eccsize: ecc block size in bytes
  87. * @eccbytes: ecc length in bytes
  88. * @ecclayout: output default layout
  89. *
  90. * Returns:
  91. * a pointer to a new NAND BCH control structure, or NULL upon failure
  92. *
  93. * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
  94. * are used to compute BCH parameters m (Galois field order) and t (error
  95. * correction capability). @eccbytes should be equal to the number of bytes
  96. * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
  97. *
  98. * Example: to configure 4 bit correction per 512 bytes, you should pass
  99. * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  100. * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
  101. */
  102. struct nand_bch_control *
  103. nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
  104. struct nand_ecclayout **ecclayout)
  105. {
  106. unsigned int m, t, eccsteps, i;
  107. struct nand_ecclayout *layout;
  108. struct nand_bch_control *nbc = NULL;
  109. unsigned char *erased_page;
  110. if (!eccsize || !eccbytes) {
  111. printk(KERN_WARNING "ecc parameters not supplied\n");
  112. goto fail;
  113. }
  114. m = fls(1+8*eccsize);
  115. t = (eccbytes*8)/m;
  116. nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
  117. if (!nbc)
  118. goto fail;
  119. nbc->bch = init_bch(m, t, 0);
  120. if (!nbc->bch)
  121. goto fail;
  122. /* verify that eccbytes has the expected value */
  123. if (nbc->bch->ecc_bytes != eccbytes) {
  124. printk(KERN_WARNING "invalid eccbytes %u, should be %u\n",
  125. eccbytes, nbc->bch->ecc_bytes);
  126. goto fail;
  127. }
  128. eccsteps = mtd->writesize/eccsize;
  129. /* if no ecc placement scheme was provided, build one */
  130. if (!*ecclayout) {
  131. /* handle large page devices only */
  132. if (mtd->oobsize < 64) {
  133. printk(KERN_WARNING "must provide an oob scheme for "
  134. "oobsize %d\n", mtd->oobsize);
  135. goto fail;
  136. }
  137. layout = &nbc->ecclayout;
  138. layout->eccbytes = eccsteps*eccbytes;
  139. /* reserve 2 bytes for bad block marker */
  140. if (layout->eccbytes+2 > mtd->oobsize) {
  141. printk(KERN_WARNING "no suitable oob scheme available "
  142. "for oobsize %d eccbytes %u\n", mtd->oobsize,
  143. eccbytes);
  144. goto fail;
  145. }
  146. /* put ecc bytes at oob tail */
  147. for (i = 0; i < layout->eccbytes; i++)
  148. layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  149. layout->oobfree[0].offset = 2;
  150. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  151. *ecclayout = layout;
  152. }
  153. /* sanity checks */
  154. if (8*(eccsize+eccbytes) >= (1 << m)) {
  155. printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
  156. goto fail;
  157. }
  158. if ((*ecclayout)->eccbytes != (eccsteps*eccbytes)) {
  159. printk(KERN_WARNING "invalid ecc layout\n");
  160. goto fail;
  161. }
  162. nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
  163. nbc->errloc = kmalloc(t*sizeof(*nbc->errloc), GFP_KERNEL);
  164. if (!nbc->eccmask || !nbc->errloc)
  165. goto fail;
  166. /*
  167. * compute and store the inverted ecc of an erased ecc block
  168. */
  169. erased_page = kmalloc(eccsize, GFP_KERNEL);
  170. if (!erased_page)
  171. goto fail;
  172. memset(erased_page, 0xff, eccsize);
  173. memset(nbc->eccmask, 0, eccbytes);
  174. encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
  175. kfree(erased_page);
  176. for (i = 0; i < eccbytes; i++)
  177. nbc->eccmask[i] ^= 0xff;
  178. return nbc;
  179. fail:
  180. nand_bch_free(nbc);
  181. return NULL;
  182. }
  183. /**
  184. * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
  185. * @nbc: NAND BCH control structure
  186. */
  187. void nand_bch_free(struct nand_bch_control *nbc)
  188. {
  189. if (nbc) {
  190. free_bch(nbc->bch);
  191. kfree(nbc->errloc);
  192. kfree(nbc->eccmask);
  193. kfree(nbc);
  194. }
  195. }