onenand_bbt.c 6.7 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_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 (ret)
  84. return ret;
  85. if (check_short_pattern(&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
  86. bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
  87. printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
  88. i >> 1, (unsigned int) from);
  89. mtd->ecc_stats.badblocks++;
  90. break;
  91. }
  92. }
  93. i += 2;
  94. from += (1 << bbm->bbt_erase_shift);
  95. }
  96. return 0;
  97. }
  98. /**
  99. * onenand_memory_bbt - [GENERIC] create a memory based bad block table
  100. * @param mtd MTD device structure
  101. * @param bd descriptor for the good/bad block search pattern
  102. *
  103. * The function creates a memory based bbt by scanning the device
  104. * for manufacturer / software marked good / bad blocks
  105. */
  106. static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
  107. {
  108. struct onenand_chip *this = mtd->priv;
  109. bd->options &= ~NAND_BBT_SCANEMPTY;
  110. return create_bbt(mtd, this->page_buf, bd, -1);
  111. }
  112. /**
  113. * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
  114. * @param mtd MTD device structure
  115. * @param offs offset in the device
  116. * @param allowbbt allow access to bad block table region
  117. */
  118. static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  119. {
  120. struct onenand_chip *this = mtd->priv;
  121. struct bbm_info *bbm = this->bbm;
  122. int block;
  123. uint8_t res;
  124. /* Get block number * 2 */
  125. block = (int) (offs >> (bbm->bbt_erase_shift - 1));
  126. res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  127. DEBUG(MTD_DEBUG_LEVEL2, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  128. (unsigned int) offs, block >> 1, res);
  129. switch ((int) res) {
  130. case 0x00: return 0;
  131. case 0x01: return 1;
  132. case 0x02: return allowbbt ? 0 : 1;
  133. }
  134. return 1;
  135. }
  136. /**
  137. * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
  138. * @param mtd MTD device structure
  139. * @param bd descriptor for the good/bad block search pattern
  140. *
  141. * The function checks, if a bad block table(s) is/are already
  142. * available. If not it scans the device for manufacturer
  143. * marked good / bad blocks and writes the bad block table(s) to
  144. * the selected place.
  145. *
  146. * The bad block table memory is allocated here. It must be freed
  147. * by calling the onenand_free_bbt function.
  148. *
  149. */
  150. int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  151. {
  152. struct onenand_chip *this = mtd->priv;
  153. struct bbm_info *bbm = this->bbm;
  154. int len, ret = 0;
  155. len = mtd->size >> (this->erase_shift + 2);
  156. /* Allocate memory (2bit per block) */
  157. bbm->bbt = kmalloc(len, GFP_KERNEL);
  158. if (!bbm->bbt) {
  159. printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
  160. return -ENOMEM;
  161. }
  162. /* Clear the memory bad block table */
  163. memset(bbm->bbt, 0x00, len);
  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 = kmalloc(sizeof(struct bbm_info), GFP_KERNEL);
  201. if (!this->bbm)
  202. return -ENOMEM;
  203. bbm = this->bbm;
  204. memset(bbm, 0, sizeof(struct bbm_info));
  205. /* 1KB page has same configuration as 2KB page */
  206. if (!bbm->badblock_pattern)
  207. bbm->badblock_pattern = &largepage_memorybased;
  208. return onenand_scan_bbt(mtd, bbm->badblock_pattern);
  209. }
  210. EXPORT_SYMBOL(onenand_scan_bbt);
  211. EXPORT_SYMBOL(onenand_default_bbt);