nand_bch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * This file is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 or (at your option) any
  10. * later version.
  11. *
  12. * This file is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this file; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. */
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/bitops.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/nand.h>
  28. #include <linux/mtd/nand_bch.h>
  29. #include <linux/bch.h>
  30. /**
  31. * struct nand_bch_control - private NAND BCH control structure
  32. * @bch: BCH control structure
  33. * @errloc: error location array
  34. * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid
  35. */
  36. struct nand_bch_control {
  37. struct bch_control *bch;
  38. unsigned int *errloc;
  39. unsigned char *eccmask;
  40. };
  41. /**
  42. * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block
  43. * @mtd: MTD block structure
  44. * @buf: input buffer with raw data
  45. * @code: output buffer with ECC
  46. */
  47. int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  48. unsigned char *code)
  49. {
  50. const struct nand_chip *chip = mtd_to_nand(mtd);
  51. struct nand_bch_control *nbc = chip->ecc.priv;
  52. unsigned int i;
  53. memset(code, 0, chip->ecc.bytes);
  54. encode_bch(nbc->bch, buf, chip->ecc.size, code);
  55. /* apply mask so that an erased page is a valid codeword */
  56. for (i = 0; i < chip->ecc.bytes; i++)
  57. code[i] ^= nbc->eccmask[i];
  58. return 0;
  59. }
  60. EXPORT_SYMBOL(nand_bch_calculate_ecc);
  61. /**
  62. * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
  63. * @mtd: MTD block structure
  64. * @buf: raw data read from the chip
  65. * @read_ecc: ECC from the chip
  66. * @calc_ecc: the ECC calculated from raw data
  67. *
  68. * Detect and correct bit errors for a data byte block
  69. */
  70. int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  71. unsigned char *read_ecc, unsigned char *calc_ecc)
  72. {
  73. const struct nand_chip *chip = mtd_to_nand(mtd);
  74. struct nand_bch_control *nbc = chip->ecc.priv;
  75. unsigned int *errloc = nbc->errloc;
  76. int i, count;
  77. count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
  78. NULL, errloc);
  79. if (count > 0) {
  80. for (i = 0; i < count; i++) {
  81. if (errloc[i] < (chip->ecc.size*8))
  82. /* error is located in data, correct it */
  83. buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
  84. /* else error in ecc, no action needed */
  85. pr_debug("%s: corrected bitflip %u\n", __func__,
  86. errloc[i]);
  87. }
  88. } else if (count < 0) {
  89. printk(KERN_ERR "ecc unrecoverable error\n");
  90. count = -EBADMSG;
  91. }
  92. return count;
  93. }
  94. EXPORT_SYMBOL(nand_bch_correct_data);
  95. /**
  96. * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  97. * @mtd: MTD block structure
  98. *
  99. * Returns:
  100. * a pointer to a new NAND BCH control structure, or NULL upon failure
  101. *
  102. * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
  103. * are used to compute BCH parameters m (Galois field order) and t (error
  104. * correction capability). @eccbytes should be equal to the number of bytes
  105. * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
  106. *
  107. * Example: to configure 4 bit correction per 512 bytes, you should pass
  108. * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  109. * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
  110. */
  111. struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
  112. {
  113. struct nand_chip *nand = mtd_to_nand(mtd);
  114. unsigned int m, t, eccsteps, i;
  115. struct nand_bch_control *nbc = NULL;
  116. unsigned char *erased_page;
  117. unsigned int eccsize = nand->ecc.size;
  118. unsigned int eccbytes = nand->ecc.bytes;
  119. unsigned int eccstrength = nand->ecc.strength;
  120. if (!eccbytes && eccstrength) {
  121. eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
  122. nand->ecc.bytes = eccbytes;
  123. }
  124. if (!eccsize || !eccbytes) {
  125. printk(KERN_WARNING "ecc parameters not supplied\n");
  126. goto fail;
  127. }
  128. m = fls(1+8*eccsize);
  129. t = (eccbytes*8)/m;
  130. nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
  131. if (!nbc)
  132. goto fail;
  133. nbc->bch = init_bch(m, t, 0);
  134. if (!nbc->bch)
  135. goto fail;
  136. /* verify that eccbytes has the expected value */
  137. if (nbc->bch->ecc_bytes != eccbytes) {
  138. printk(KERN_WARNING "invalid eccbytes %u, should be %u\n",
  139. eccbytes, nbc->bch->ecc_bytes);
  140. goto fail;
  141. }
  142. eccsteps = mtd->writesize/eccsize;
  143. /* Check that we have an oob layout description. */
  144. if (!mtd->ooblayout) {
  145. pr_warn("missing oob scheme");
  146. goto fail;
  147. }
  148. /* sanity checks */
  149. if (8*(eccsize+eccbytes) >= (1 << m)) {
  150. printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
  151. goto fail;
  152. }
  153. /*
  154. * ecc->steps and ecc->total might be used by mtd->ooblayout->ecc(),
  155. * which is called by mtd_ooblayout_count_eccbytes().
  156. * Make sure they are properly initialized before calling
  157. * mtd_ooblayout_count_eccbytes().
  158. * FIXME: we should probably rework the sequencing in nand_scan_tail()
  159. * to avoid setting those fields twice.
  160. */
  161. nand->ecc.steps = eccsteps;
  162. nand->ecc.total = eccsteps * eccbytes;
  163. if (mtd_ooblayout_count_eccbytes(mtd) != (eccsteps*eccbytes)) {
  164. printk(KERN_WARNING "invalid ecc layout\n");
  165. goto fail;
  166. }
  167. nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
  168. nbc->errloc = kmalloc(t*sizeof(*nbc->errloc), GFP_KERNEL);
  169. if (!nbc->eccmask || !nbc->errloc)
  170. goto fail;
  171. /*
  172. * compute and store the inverted ecc of an erased ecc block
  173. */
  174. erased_page = kmalloc(eccsize, GFP_KERNEL);
  175. if (!erased_page)
  176. goto fail;
  177. memset(erased_page, 0xff, eccsize);
  178. memset(nbc->eccmask, 0, eccbytes);
  179. encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
  180. kfree(erased_page);
  181. for (i = 0; i < eccbytes; i++)
  182. nbc->eccmask[i] ^= 0xff;
  183. if (!eccstrength)
  184. nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
  185. return nbc;
  186. fail:
  187. nand_bch_free(nbc);
  188. return NULL;
  189. }
  190. EXPORT_SYMBOL(nand_bch_init);
  191. /**
  192. * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
  193. * @nbc: NAND BCH control structure
  194. */
  195. void nand_bch_free(struct nand_bch_control *nbc)
  196. {
  197. if (nbc) {
  198. free_bch(nbc->bch);
  199. kfree(nbc->errloc);
  200. kfree(nbc->eccmask);
  201. kfree(nbc);
  202. }
  203. }
  204. EXPORT_SYMBOL(nand_bch_free);
  205. MODULE_LICENSE("GPL");
  206. MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
  207. MODULE_DESCRIPTION("NAND software BCH ECC support");