onenand_bbt.c 6.5 KB

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