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