onenand_bbt.c 6.8 KB

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