onenand_bbt.c 6.7 KB

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