onenand_bbt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Bad Block Table support for the OneNAND driver
  4. *
  5. * Copyright(c) 2005 Samsung Electronics
  6. * Kyungmin Park <kyungmin.park@samsung.com>
  7. *
  8. * Derived from nand_bbt.c
  9. *
  10. * TODO:
  11. * Split BBT core and chip specific BBT.
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/onenand.h>
  16. #include <linux/export.h>
  17. /**
  18. * check_short_pattern - [GENERIC] check if a pattern is in the buffer
  19. * @param buf the buffer to search
  20. * @param len the length of buffer to search
  21. * @param paglen the pagelength
  22. * @param td search pattern descriptor
  23. *
  24. * Check for a pattern at the given place. Used to search bad block
  25. * tables and good / bad block identifiers. Same as check_pattern, but
  26. * no optional empty check and the pattern is expected to start
  27. * at offset 0.
  28. *
  29. */
  30. static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
  31. {
  32. int i;
  33. uint8_t *p = buf;
  34. /* Compare the pattern */
  35. for (i = 0; i < td->len; i++) {
  36. if (p[i] != td->pattern[i])
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. /**
  42. * create_bbt - [GENERIC] Create a bad block table by scanning the device
  43. * @param mtd MTD device structure
  44. * @param buf temporary buffer
  45. * @param bd descriptor for the good/bad block search pattern
  46. * @param chip create the table for a specific chip, -1 read all chips.
  47. * Applies only if NAND_BBT_PERCHIP option is set
  48. *
  49. * Create a bad block table by scanning the device
  50. * for the given good/bad block identify pattern
  51. */
  52. static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
  53. {
  54. struct onenand_chip *this = mtd->priv;
  55. struct bbm_info *bbm = this->bbm;
  56. int i, j, numblocks, len, scanlen;
  57. int startblock;
  58. loff_t from;
  59. size_t readlen, ooblen;
  60. struct mtd_oob_ops ops;
  61. int rgn;
  62. printk(KERN_INFO "Scanning device for bad blocks\n");
  63. len = 2;
  64. /* We need only read few bytes from the OOB area */
  65. scanlen = ooblen = 0;
  66. readlen = bd->len;
  67. /* chip == -1 case only */
  68. /* Note that numblocks is 2 * (real numblocks) here;
  69. * see i += 2 below as it makses shifting and masking less painful
  70. */
  71. numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1);
  72. startblock = 0;
  73. from = 0;
  74. ops.mode = MTD_OPS_PLACE_OOB;
  75. ops.ooblen = readlen;
  76. ops.oobbuf = buf;
  77. ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
  78. for (i = startblock; i < numblocks; ) {
  79. int ret;
  80. for (j = 0; j < len; j++) {
  81. /* No need to read pages fully,
  82. * just read required OOB bytes */
  83. ret = onenand_bbt_read_oob(mtd,
  84. from + j * this->writesize + bd->offs, &ops);
  85. /* If it is a initial bad block, just ignore it */
  86. if (ret == ONENAND_BBT_READ_FATAL_ERROR)
  87. return -EIO;
  88. if (ret || check_short_pattern(&buf[j * scanlen],
  89. scanlen, this->writesize, bd)) {
  90. bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
  91. printk(KERN_INFO "OneNAND eraseblock %d is an "
  92. "initial bad block\n", i >> 1);
  93. mtd->ecc_stats.badblocks++;
  94. break;
  95. }
  96. }
  97. i += 2;
  98. if (FLEXONENAND(this)) {
  99. rgn = flexonenand_region(mtd, from);
  100. from += mtd->eraseregions[rgn].erasesize;
  101. } else
  102. from += (1 << bbm->bbt_erase_shift);
  103. }
  104. return 0;
  105. }
  106. /**
  107. * onenand_memory_bbt - [GENERIC] create a memory based bad block table
  108. * @param mtd MTD device structure
  109. * @param bd descriptor for the good/bad block search pattern
  110. *
  111. * The function creates a memory based bbt by scanning the device
  112. * for manufacturer / software marked good / bad blocks
  113. */
  114. static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
  115. {
  116. struct onenand_chip *this = mtd->priv;
  117. return create_bbt(mtd, this->page_buf, bd, -1);
  118. }
  119. /**
  120. * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
  121. * @param mtd MTD device structure
  122. * @param offs offset in the device
  123. * @param allowbbt allow access to bad block table region
  124. */
  125. static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  126. {
  127. struct onenand_chip *this = mtd->priv;
  128. struct bbm_info *bbm = this->bbm;
  129. int block;
  130. uint8_t res;
  131. /* Get block number * 2 */
  132. block = (int) (onenand_block(this, offs) << 1);
  133. res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  134. pr_debug("onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  135. (unsigned int) offs, block >> 1, res);
  136. switch ((int) res) {
  137. case 0x00: return 0;
  138. case 0x01: return 1;
  139. case 0x02: return allowbbt ? 0 : 1;
  140. }
  141. return 1;
  142. }
  143. /**
  144. * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
  145. * @param mtd MTD device structure
  146. * @param bd descriptor for the good/bad block search pattern
  147. *
  148. * The function checks, if a bad block table(s) is/are already
  149. * available. If not it scans the device for manufacturer
  150. * marked good / bad blocks and writes the bad block table(s) to
  151. * the selected place.
  152. *
  153. * The bad block table memory is allocated here. It is freed
  154. * by the onenand_release function.
  155. *
  156. */
  157. static int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  158. {
  159. struct onenand_chip *this = mtd->priv;
  160. struct bbm_info *bbm = this->bbm;
  161. int len, ret = 0;
  162. len = this->chipsize >> (this->erase_shift + 2);
  163. /* Allocate memory (2bit per block) and clear the memory bad block table */
  164. bbm->bbt = kzalloc(len, GFP_KERNEL);
  165. if (!bbm->bbt)
  166. return -ENOMEM;
  167. /* Set the bad block position */
  168. bbm->badblockpos = ONENAND_BADBLOCK_POS;
  169. /* Set erase shift */
  170. bbm->bbt_erase_shift = this->erase_shift;
  171. if (!bbm->isbad_bbt)
  172. bbm->isbad_bbt = onenand_isbad_bbt;
  173. /* Scan the device to build a memory based bad block table */
  174. if ((ret = onenand_memory_bbt(mtd, bd))) {
  175. printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
  176. kfree(bbm->bbt);
  177. bbm->bbt = NULL;
  178. }
  179. return ret;
  180. }
  181. /*
  182. * Define some generic bad / good block scan pattern which are used
  183. * while scanning a device for factory marked good / bad blocks.
  184. */
  185. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  186. static struct nand_bbt_descr largepage_memorybased = {
  187. .options = 0,
  188. .offs = 0,
  189. .len = 2,
  190. .pattern = scan_ff_pattern,
  191. };
  192. /**
  193. * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
  194. * @param mtd MTD device structure
  195. *
  196. * This function selects the default bad block table
  197. * support for the device and calls the onenand_scan_bbt function
  198. */
  199. int onenand_default_bbt(struct mtd_info *mtd)
  200. {
  201. struct onenand_chip *this = mtd->priv;
  202. struct bbm_info *bbm;
  203. this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
  204. if (!this->bbm)
  205. return -ENOMEM;
  206. bbm = this->bbm;
  207. /* 1KB page has same configuration as 2KB page */
  208. if (!bbm->badblock_pattern)
  209. bbm->badblock_pattern = &largepage_memorybased;
  210. return onenand_scan_bbt(mtd, bbm->badblock_pattern);
  211. }