nand_bch.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * @ecclayout: private ecc layout for this BCH configuration
  34. * @errloc: error location array
  35. * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid
  36. */
  37. struct nand_bch_control {
  38. struct bch_control *bch;
  39. struct nand_ecclayout ecclayout;
  40. unsigned int *errloc;
  41. unsigned char *eccmask;
  42. };
  43. /**
  44. * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block
  45. * @mtd: MTD block structure
  46. * @buf: input buffer with raw data
  47. * @code: output buffer with ECC
  48. */
  49. int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  50. unsigned char *code)
  51. {
  52. const struct nand_chip *chip = mtd->priv;
  53. struct nand_bch_control *nbc = chip->ecc.priv;
  54. unsigned int i;
  55. memset(code, 0, chip->ecc.bytes);
  56. encode_bch(nbc->bch, buf, chip->ecc.size, code);
  57. /* apply mask so that an erased page is a valid codeword */
  58. for (i = 0; i < chip->ecc.bytes; i++)
  59. code[i] ^= nbc->eccmask[i];
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(nand_bch_calculate_ecc);
  63. /**
  64. * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
  65. * @mtd: MTD block structure
  66. * @buf: raw data read from the chip
  67. * @read_ecc: ECC from the chip
  68. * @calc_ecc: the ECC calculated from raw data
  69. *
  70. * Detect and correct bit errors for a data byte block
  71. */
  72. int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  73. unsigned char *read_ecc, unsigned char *calc_ecc)
  74. {
  75. const struct nand_chip *chip = mtd->priv;
  76. struct nand_bch_control *nbc = chip->ecc.priv;
  77. unsigned int *errloc = nbc->errloc;
  78. int i, count;
  79. count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
  80. NULL, errloc);
  81. if (count > 0) {
  82. for (i = 0; i < count; i++) {
  83. if (errloc[i] < (chip->ecc.size*8))
  84. /* error is located in data, correct it */
  85. buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
  86. /* else error in ecc, no action needed */
  87. pr_debug("%s: corrected bitflip %u\n", __func__,
  88. errloc[i]);
  89. }
  90. } else if (count < 0) {
  91. printk(KERN_ERR "ecc unrecoverable error\n");
  92. count = -1;
  93. }
  94. return count;
  95. }
  96. EXPORT_SYMBOL(nand_bch_correct_data);
  97. /**
  98. * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  99. * @mtd: MTD block structure
  100. * @eccsize: ecc block size in bytes
  101. * @eccbytes: ecc length in bytes
  102. * @ecclayout: output default layout
  103. *
  104. * Returns:
  105. * a pointer to a new NAND BCH control structure, or NULL upon failure
  106. *
  107. * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
  108. * are used to compute BCH parameters m (Galois field order) and t (error
  109. * correction capability). @eccbytes should be equal to the number of bytes
  110. * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
  111. *
  112. * Example: to configure 4 bit correction per 512 bytes, you should pass
  113. * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  114. * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
  115. */
  116. struct nand_bch_control *
  117. nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
  118. struct nand_ecclayout **ecclayout)
  119. {
  120. unsigned int m, t, eccsteps, i;
  121. struct nand_ecclayout *layout;
  122. struct nand_bch_control *nbc = NULL;
  123. unsigned char *erased_page;
  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. /* if no ecc placement scheme was provided, build one */
  144. if (!*ecclayout) {
  145. /* handle large page devices only */
  146. if (mtd->oobsize < 64) {
  147. printk(KERN_WARNING "must provide an oob scheme for "
  148. "oobsize %d\n", mtd->oobsize);
  149. goto fail;
  150. }
  151. layout = &nbc->ecclayout;
  152. layout->eccbytes = eccsteps*eccbytes;
  153. /* reserve 2 bytes for bad block marker */
  154. if (layout->eccbytes+2 > mtd->oobsize) {
  155. printk(KERN_WARNING "no suitable oob scheme available "
  156. "for oobsize %d eccbytes %u\n", mtd->oobsize,
  157. eccbytes);
  158. goto fail;
  159. }
  160. /* put ecc bytes at oob tail */
  161. for (i = 0; i < layout->eccbytes; i++)
  162. layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  163. layout->oobfree[0].offset = 2;
  164. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  165. *ecclayout = layout;
  166. }
  167. /* sanity checks */
  168. if (8*(eccsize+eccbytes) >= (1 << m)) {
  169. printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
  170. goto fail;
  171. }
  172. if ((*ecclayout)->eccbytes != (eccsteps*eccbytes)) {
  173. printk(KERN_WARNING "invalid ecc layout\n");
  174. goto fail;
  175. }
  176. nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
  177. nbc->errloc = kmalloc(t*sizeof(*nbc->errloc), GFP_KERNEL);
  178. if (!nbc->eccmask || !nbc->errloc)
  179. goto fail;
  180. /*
  181. * compute and store the inverted ecc of an erased ecc block
  182. */
  183. erased_page = kmalloc(eccsize, GFP_KERNEL);
  184. if (!erased_page)
  185. goto fail;
  186. memset(erased_page, 0xff, eccsize);
  187. memset(nbc->eccmask, 0, eccbytes);
  188. encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
  189. kfree(erased_page);
  190. for (i = 0; i < eccbytes; i++)
  191. nbc->eccmask[i] ^= 0xff;
  192. return nbc;
  193. fail:
  194. nand_bch_free(nbc);
  195. return NULL;
  196. }
  197. EXPORT_SYMBOL(nand_bch_init);
  198. /**
  199. * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
  200. * @nbc: NAND BCH control structure
  201. */
  202. void nand_bch_free(struct nand_bch_control *nbc)
  203. {
  204. if (nbc) {
  205. free_bch(nbc->bch);
  206. kfree(nbc->errloc);
  207. kfree(nbc->eccmask);
  208. kfree(nbc);
  209. }
  210. }
  211. EXPORT_SYMBOL(nand_bch_free);
  212. MODULE_LICENSE("GPL");
  213. MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
  214. MODULE_DESCRIPTION("NAND software BCH ECC support");