onenand_bbt.c 7.0 KB

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