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