onenand_bbt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /**
  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_OOB_PLACE;
  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. bd->options &= ~NAND_BBT_SCANEMPTY;
  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. DEBUG(MTD_DEBUG_LEVEL2, "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. 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. printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
  168. return -ENOMEM;
  169. }
  170. /* Set the bad block position */
  171. bbm->badblockpos = ONENAND_BADBLOCK_POS;
  172. /* Set erase shift */
  173. bbm->bbt_erase_shift = this->erase_shift;
  174. if (!bbm->isbad_bbt)
  175. bbm->isbad_bbt = onenand_isbad_bbt;
  176. /* Scan the device to build a memory based bad block table */
  177. if ((ret = onenand_memory_bbt(mtd, bd))) {
  178. printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
  179. kfree(bbm->bbt);
  180. bbm->bbt = NULL;
  181. }
  182. return ret;
  183. }
  184. /*
  185. * Define some generic bad / good block scan pattern which are used
  186. * while scanning a device for factory marked good / bad blocks.
  187. */
  188. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  189. static struct nand_bbt_descr largepage_memorybased = {
  190. .options = 0,
  191. .offs = 0,
  192. .len = 2,
  193. .pattern = scan_ff_pattern,
  194. };
  195. /**
  196. * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
  197. * @param mtd MTD device structure
  198. *
  199. * This function selects the default bad block table
  200. * support for the device and calls the onenand_scan_bbt function
  201. */
  202. int onenand_default_bbt(struct mtd_info *mtd)
  203. {
  204. struct onenand_chip *this = mtd->priv;
  205. struct bbm_info *bbm;
  206. this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
  207. if (!this->bbm)
  208. return -ENOMEM;
  209. bbm = this->bbm;
  210. /* 1KB page has same configuration as 2KB page */
  211. if (!bbm->badblock_pattern)
  212. bbm->badblock_pattern = &largepage_memorybased;
  213. return onenand_scan_bbt(mtd, bbm->badblock_pattern);
  214. }
  215. EXPORT_SYMBOL(onenand_scan_bbt);
  216. EXPORT_SYMBOL(onenand_default_bbt);