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