onenand_bbt.c 6.7 KB

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