nand_bch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <common.h>
  22. /*#include <asm/io.h>*/
  23. #include <linux/types.h>
  24. #include <linux/bitops.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/nand.h>
  27. #include <linux/mtd/nand_bch.h>
  28. #include <linux/bch.h>
  29. #include <malloc.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. /**
  63. * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
  64. * @mtd: MTD block structure
  65. * @buf: raw data read from the chip
  66. * @read_ecc: ECC from the chip
  67. * @calc_ecc: the ECC calculated from raw data
  68. *
  69. * Detect and correct bit errors for a data byte block
  70. */
  71. int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  72. unsigned char *read_ecc, unsigned char *calc_ecc)
  73. {
  74. const struct nand_chip *chip = mtd->priv;
  75. struct nand_bch_control *nbc = chip->ecc.priv;
  76. unsigned int *errloc = nbc->errloc;
  77. int i, count;
  78. count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
  79. NULL, errloc);
  80. if (count > 0) {
  81. for (i = 0; i < count; i++) {
  82. if (errloc[i] < (chip->ecc.size*8))
  83. /* error is located in data, correct it */
  84. buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
  85. /* else error in ecc, no action needed */
  86. MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: corrected bitflip %u\n",
  87. __func__, errloc[i]);
  88. }
  89. } else if (count < 0) {
  90. printk(KERN_ERR "ecc unrecoverable error\n");
  91. count = -1;
  92. }
  93. return count;
  94. }
  95. /**
  96. * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  97. * @mtd: MTD block structure
  98. * @eccsize: ecc block size in bytes
  99. * @eccbytes: ecc length in bytes
  100. * @ecclayout: output default layout
  101. *
  102. * Returns:
  103. * a pointer to a new NAND BCH control structure, or NULL upon failure
  104. *
  105. * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
  106. * are used to compute BCH parameters m (Galois field order) and t (error
  107. * correction capability). @eccbytes should be equal to the number of bytes
  108. * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
  109. *
  110. * Example: to configure 4 bit correction per 512 bytes, you should pass
  111. * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  112. * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
  113. */
  114. struct nand_bch_control *
  115. nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
  116. struct nand_ecclayout **ecclayout)
  117. {
  118. unsigned int m, t, eccsteps, i;
  119. struct nand_ecclayout *layout;
  120. struct nand_bch_control *nbc = NULL;
  121. unsigned char *erased_page;
  122. if (!eccsize || !eccbytes) {
  123. printk(KERN_WARNING "ecc parameters not supplied\n");
  124. goto fail;
  125. }
  126. m = fls(1+8*eccsize);
  127. t = (eccbytes*8)/m;
  128. nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
  129. if (!nbc)
  130. goto fail;
  131. nbc->bch = init_bch(m, t, 0);
  132. if (!nbc->bch)
  133. goto fail;
  134. /* verify that eccbytes has the expected value */
  135. if (nbc->bch->ecc_bytes != eccbytes) {
  136. printk(KERN_WARNING "invalid eccbytes %u, should be %u\n",
  137. eccbytes, nbc->bch->ecc_bytes);
  138. goto fail;
  139. }
  140. eccsteps = mtd->writesize/eccsize;
  141. /* if no ecc placement scheme was provided, build one */
  142. if (!*ecclayout) {
  143. /* handle large page devices only */
  144. if (mtd->oobsize < 64) {
  145. printk(KERN_WARNING "must provide an oob scheme for "
  146. "oobsize %d\n", mtd->oobsize);
  147. goto fail;
  148. }
  149. layout = &nbc->ecclayout;
  150. layout->eccbytes = eccsteps*eccbytes;
  151. /* reserve 2 bytes for bad block marker */
  152. if (layout->eccbytes+2 > mtd->oobsize) {
  153. printk(KERN_WARNING "no suitable oob scheme available "
  154. "for oobsize %d eccbytes %u\n", mtd->oobsize,
  155. eccbytes);
  156. goto fail;
  157. }
  158. /* put ecc bytes at oob tail */
  159. for (i = 0; i < layout->eccbytes; i++)
  160. layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  161. layout->oobfree[0].offset = 2;
  162. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  163. *ecclayout = layout;
  164. }
  165. /* sanity checks */
  166. if (8*(eccsize+eccbytes) >= (1 << m)) {
  167. printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
  168. goto fail;
  169. }
  170. if ((*ecclayout)->eccbytes != (eccsteps*eccbytes)) {
  171. printk(KERN_WARNING "invalid ecc layout\n");
  172. goto fail;
  173. }
  174. nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
  175. nbc->errloc = kmalloc(t*sizeof(*nbc->errloc), GFP_KERNEL);
  176. if (!nbc->eccmask || !nbc->errloc)
  177. goto fail;
  178. /*
  179. * compute and store the inverted ecc of an erased ecc block
  180. */
  181. erased_page = kmalloc(eccsize, GFP_KERNEL);
  182. if (!erased_page)
  183. goto fail;
  184. memset(erased_page, 0xff, eccsize);
  185. memset(nbc->eccmask, 0, eccbytes);
  186. encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
  187. kfree(erased_page);
  188. for (i = 0; i < eccbytes; i++)
  189. nbc->eccmask[i] ^= 0xff;
  190. return nbc;
  191. fail:
  192. nand_bch_free(nbc);
  193. return NULL;
  194. }
  195. /**
  196. * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
  197. * @nbc: NAND BCH control structure
  198. */
  199. void nand_bch_free(struct nand_bch_control *nbc)
  200. {
  201. if (nbc) {
  202. free_bch(nbc->bch);
  203. kfree(nbc->errloc);
  204. kfree(nbc->eccmask);
  205. kfree(nbc);
  206. }
  207. }