nand_bch.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file provides ECC correction for more than 1 bit per block of data,
  4. * using binary BCH codes. It relies on the generic BCH library lib/bch.c.
  5. *
  6. * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
  7. *
  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/rawnand.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. pr_debug("%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 = -EBADMSG;
  80. }
  81. return count;
  82. }
  83. /**
  84. * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  85. * @mtd: MTD block structure
  86. *
  87. * Returns:
  88. * a pointer to a new NAND BCH control structure, or NULL upon failure
  89. *
  90. * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
  91. * are used to compute BCH parameters m (Galois field order) and t (error
  92. * correction capability). @eccbytes should be equal to the number of bytes
  93. * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
  94. *
  95. * Example: to configure 4 bit correction per 512 bytes, you should pass
  96. * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  97. * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
  98. */
  99. struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
  100. {
  101. struct nand_chip *nand = mtd_to_nand(mtd);
  102. unsigned int m, t, eccsteps, i;
  103. struct nand_ecclayout *layout = nand->ecc.layout;
  104. struct nand_bch_control *nbc = NULL;
  105. unsigned char *erased_page;
  106. unsigned int eccsize = nand->ecc.size;
  107. unsigned int eccbytes = nand->ecc.bytes;
  108. unsigned int eccstrength = nand->ecc.strength;
  109. if (!eccbytes && eccstrength) {
  110. eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
  111. nand->ecc.bytes = eccbytes;
  112. }
  113. if (!eccsize || !eccbytes) {
  114. printk(KERN_WARNING "ecc parameters not supplied\n");
  115. goto fail;
  116. }
  117. m = fls(1+8*eccsize);
  118. t = (eccbytes*8)/m;
  119. nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
  120. if (!nbc)
  121. goto fail;
  122. nbc->bch = init_bch(m, t, 0);
  123. if (!nbc->bch)
  124. goto fail;
  125. /* verify that eccbytes has the expected value */
  126. if (nbc->bch->ecc_bytes != eccbytes) {
  127. printk(KERN_WARNING "invalid eccbytes %u, should be %u\n",
  128. eccbytes, nbc->bch->ecc_bytes);
  129. goto fail;
  130. }
  131. eccsteps = mtd->writesize/eccsize;
  132. /* if no ecc placement scheme was provided, build one */
  133. if (!layout) {
  134. /* handle large page devices only */
  135. if (mtd->oobsize < 64) {
  136. printk(KERN_WARNING "must provide an oob scheme for "
  137. "oobsize %d\n", mtd->oobsize);
  138. goto fail;
  139. }
  140. layout = &nbc->ecclayout;
  141. layout->eccbytes = eccsteps*eccbytes;
  142. /* reserve 2 bytes for bad block marker */
  143. if (layout->eccbytes+2 > mtd->oobsize) {
  144. printk(KERN_WARNING "no suitable oob scheme available "
  145. "for oobsize %d eccbytes %u\n", mtd->oobsize,
  146. eccbytes);
  147. goto fail;
  148. }
  149. /* put ecc bytes at oob tail */
  150. for (i = 0; i < layout->eccbytes; i++)
  151. layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  152. layout->oobfree[0].offset = 2;
  153. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  154. nand->ecc.layout = layout;
  155. }
  156. /* sanity checks */
  157. if (8*(eccsize+eccbytes) >= (1 << m)) {
  158. printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
  159. goto fail;
  160. }
  161. if (layout->eccbytes != (eccsteps*eccbytes)) {
  162. printk(KERN_WARNING "invalid ecc layout\n");
  163. goto fail;
  164. }
  165. nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
  166. nbc->errloc = kmalloc(t*sizeof(*nbc->errloc), GFP_KERNEL);
  167. if (!nbc->eccmask || !nbc->errloc)
  168. goto fail;
  169. /*
  170. * compute and store the inverted ecc of an erased ecc block
  171. */
  172. erased_page = kmalloc(eccsize, GFP_KERNEL);
  173. if (!erased_page)
  174. goto fail;
  175. memset(erased_page, 0xff, eccsize);
  176. memset(nbc->eccmask, 0, eccbytes);
  177. encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
  178. kfree(erased_page);
  179. for (i = 0; i < eccbytes; i++)
  180. nbc->eccmask[i] ^= 0xff;
  181. if (!eccstrength)
  182. nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
  183. return nbc;
  184. fail:
  185. nand_bch_free(nbc);
  186. return NULL;
  187. }
  188. /**
  189. * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
  190. * @nbc: NAND BCH control structure
  191. */
  192. void nand_bch_free(struct nand_bch_control *nbc)
  193. {
  194. if (nbc) {
  195. free_bch(nbc->bch);
  196. kfree(nbc->errloc);
  197. kfree(nbc->eccmask);
  198. kfree(nbc);
  199. }
  200. }