onenand_bbt.c 6.9 KB

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