onenand_bbt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/mtd/compatmac.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. 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 = mtd->size >> (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, from + j * mtd->writesize + bd->offs, &ops);
  84. /* If it is a initial bad block, just ignore it */
  85. if (ret == ONENAND_BBT_READ_FATAL_ERROR)
  86. return -EIO;
  87. if (ret || check_short_pattern(&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
  88. bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
  89. printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
  90. i >> 1, (unsigned int) from);
  91. mtd->ecc_stats.badblocks++;
  92. break;
  93. }
  94. }
  95. i += 2;
  96. from += (1 << bbm->bbt_erase_shift);
  97. }
  98. return 0;
  99. }
  100. /**
  101. * onenand_memory_bbt - [GENERIC] create a memory based bad block table
  102. * @param mtd MTD device structure
  103. * @param bd descriptor for the good/bad block search pattern
  104. *
  105. * The function creates a memory based bbt by scanning the device
  106. * for manufacturer / software marked good / bad blocks
  107. */
  108. static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
  109. {
  110. struct onenand_chip *this = mtd->priv;
  111. bd->options &= ~NAND_BBT_SCANEMPTY;
  112. return create_bbt(mtd, this->page_buf, bd, -1);
  113. }
  114. /**
  115. * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
  116. * @param mtd MTD device structure
  117. * @param offs offset in the device
  118. * @param allowbbt allow access to bad block table region
  119. */
  120. static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  121. {
  122. struct onenand_chip *this = mtd->priv;
  123. struct bbm_info *bbm = this->bbm;
  124. int block;
  125. uint8_t res;
  126. /* Get block number * 2 */
  127. block = (int) (offs >> (bbm->bbt_erase_shift - 1));
  128. res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  129. DEBUG(MTD_DEBUG_LEVEL2, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  130. (unsigned int) offs, block >> 1, res);
  131. switch ((int) res) {
  132. case 0x00: return 0;
  133. case 0x01: return 1;
  134. case 0x02: return allowbbt ? 0 : 1;
  135. }
  136. return 1;
  137. }
  138. /**
  139. * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
  140. * @param mtd MTD device structure
  141. * @param bd descriptor for the good/bad block search pattern
  142. *
  143. * The function checks, if a bad block table(s) is/are already
  144. * available. If not it scans the device for manufacturer
  145. * marked good / bad blocks and writes the bad block table(s) to
  146. * the selected place.
  147. *
  148. * The bad block table memory is allocated here. It is freed
  149. * by the onenand_release function.
  150. *
  151. */
  152. int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  153. {
  154. struct onenand_chip *this = mtd->priv;
  155. struct bbm_info *bbm = this->bbm;
  156. int len, ret = 0;
  157. len = mtd->size >> (this->erase_shift + 2);
  158. /* Allocate memory (2bit per block) and clear the memory bad block table */
  159. bbm->bbt = kzalloc(len, GFP_KERNEL);
  160. if (!bbm->bbt) {
  161. printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
  162. return -ENOMEM;
  163. }
  164. /* Set the bad block position */
  165. bbm->badblockpos = ONENAND_BADBLOCK_POS;
  166. /* Set erase shift */
  167. bbm->bbt_erase_shift = this->erase_shift;
  168. if (!bbm->isbad_bbt)
  169. bbm->isbad_bbt = onenand_isbad_bbt;
  170. /* Scan the device to build a memory based bad block table */
  171. if ((ret = onenand_memory_bbt(mtd, bd))) {
  172. printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
  173. kfree(bbm->bbt);
  174. bbm->bbt = NULL;
  175. }
  176. return ret;
  177. }
  178. /*
  179. * Define some generic bad / good block scan pattern which are used
  180. * while scanning a device for factory marked good / bad blocks.
  181. */
  182. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  183. static struct nand_bbt_descr largepage_memorybased = {
  184. .options = 0,
  185. .offs = 0,
  186. .len = 2,
  187. .pattern = scan_ff_pattern,
  188. };
  189. /**
  190. * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
  191. * @param mtd MTD device structure
  192. *
  193. * This function selects the default bad block table
  194. * support for the device and calls the onenand_scan_bbt function
  195. */
  196. int onenand_default_bbt(struct mtd_info *mtd)
  197. {
  198. struct onenand_chip *this = mtd->priv;
  199. struct bbm_info *bbm;
  200. this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
  201. if (!this->bbm)
  202. return -ENOMEM;
  203. bbm = this->bbm;
  204. /* 1KB page has same configuration as 2KB page */
  205. if (!bbm->badblock_pattern)
  206. bbm->badblock_pattern = &largepage_memorybased;
  207. return onenand_scan_bbt(mtd, bbm->badblock_pattern);
  208. }
  209. EXPORT_SYMBOL(onenand_scan_bbt);
  210. EXPORT_SYMBOL(onenand_default_bbt);