nand_bbt.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. * drivers/mtd/nand_bbt.c
  3. *
  4. * Overview:
  5. * Bad block table support for the NAND driver
  6. *
  7. * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Description:
  14. *
  15. * When nand_scan_bbt is called, then it tries to find the bad block table
  16. * depending on the options in the BBT descriptor(s). If no flash based BBT
  17. * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
  18. * marked good / bad blocks. This information is used to create a memory BBT.
  19. * Once a new bad block is discovered then the "factory" information is updated
  20. * on the device.
  21. * If a flash based BBT is specified then the function first tries to find the
  22. * BBT on flash. If a BBT is found then the contents are read and the memory
  23. * based BBT is created. If a mirrored BBT is selected then the mirror is
  24. * searched too and the versions are compared. If the mirror has a greater
  25. * version number, then the mirror BBT is used to build the memory based BBT.
  26. * If the tables are not versioned, then we "or" the bad block information.
  27. * If one of the BBTs is out of date or does not exist it is (re)created.
  28. * If no BBT exists at all then the device is scanned for factory marked
  29. * good / bad blocks and the bad block tables are created.
  30. *
  31. * For manufacturer created BBTs like the one found on M-SYS DOC devices
  32. * the BBT is searched and read but never created
  33. *
  34. * The auto generated bad block table is located in the last good blocks
  35. * of the device. The table is mirrored, so it can be updated eventually.
  36. * The table is marked in the OOB area with an ident pattern and a version
  37. * number which indicates which of both tables is more up to date. If the NAND
  38. * controller needs the complete OOB area for the ECC information then the
  39. * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
  40. * course): it moves the ident pattern and the version byte into the data area
  41. * and the OOB area will remain untouched.
  42. *
  43. * The table uses 2 bits per block
  44. * 11b: block is good
  45. * 00b: block is factory marked bad
  46. * 01b, 10b: block is marked bad due to wear
  47. *
  48. * The memory bad block table uses the following scheme:
  49. * 00b: block is good
  50. * 01b: block is marked bad due to wear
  51. * 10b: block is reserved (to protect the bbt area)
  52. * 11b: block is factory marked bad
  53. *
  54. * Multichip devices like DOC store the bad block info per floor.
  55. *
  56. * Following assumptions are made:
  57. * - bbts start at a page boundary, if autolocated on a block boundary
  58. * - the space necessary for a bbt in FLASH does not exceed a block boundary
  59. *
  60. */
  61. #include <linux/slab.h>
  62. #include <linux/types.h>
  63. #include <linux/mtd/mtd.h>
  64. #include <linux/mtd/bbm.h>
  65. #include <linux/mtd/nand.h>
  66. #include <linux/mtd/nand_ecc.h>
  67. #include <linux/bitops.h>
  68. #include <linux/delay.h>
  69. #include <linux/vmalloc.h>
  70. #include <linux/export.h>
  71. #include <linux/string.h>
  72. #define BBT_BLOCK_GOOD 0x00
  73. #define BBT_BLOCK_WORN 0x01
  74. #define BBT_BLOCK_RESERVED 0x02
  75. #define BBT_BLOCK_FACTORY_BAD 0x03
  76. #define BBT_ENTRY_MASK 0x03
  77. #define BBT_ENTRY_SHIFT 2
  78. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);
  79. static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
  80. {
  81. uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
  82. entry >>= (block & BBT_ENTRY_MASK) * 2;
  83. return entry & BBT_ENTRY_MASK;
  84. }
  85. static inline void bbt_mark_entry(struct nand_chip *chip, int block,
  86. uint8_t mark)
  87. {
  88. uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
  89. chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
  90. }
  91. static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
  92. {
  93. if (memcmp(buf, td->pattern, td->len))
  94. return -1;
  95. return 0;
  96. }
  97. /**
  98. * check_pattern - [GENERIC] check if a pattern is in the buffer
  99. * @buf: the buffer to search
  100. * @len: the length of buffer to search
  101. * @paglen: the pagelength
  102. * @td: search pattern descriptor
  103. *
  104. * Check for a pattern at the given place. Used to search bad block tables and
  105. * good / bad block identifiers.
  106. */
  107. static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
  108. {
  109. if (td->options & NAND_BBT_NO_OOB)
  110. return check_pattern_no_oob(buf, td);
  111. /* Compare the pattern */
  112. if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
  113. return -1;
  114. return 0;
  115. }
  116. /**
  117. * check_short_pattern - [GENERIC] check if a pattern is in the buffer
  118. * @buf: the buffer to search
  119. * @td: search pattern descriptor
  120. *
  121. * Check for a pattern at the given place. Used to search bad block tables and
  122. * good / bad block identifiers. Same as check_pattern, but no optional empty
  123. * check.
  124. */
  125. static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
  126. {
  127. /* Compare the pattern */
  128. if (memcmp(buf + td->offs, td->pattern, td->len))
  129. return -1;
  130. return 0;
  131. }
  132. /**
  133. * add_marker_len - compute the length of the marker in data area
  134. * @td: BBT descriptor used for computation
  135. *
  136. * The length will be 0 if the marker is located in OOB area.
  137. */
  138. static u32 add_marker_len(struct nand_bbt_descr *td)
  139. {
  140. u32 len;
  141. if (!(td->options & NAND_BBT_NO_OOB))
  142. return 0;
  143. len = td->len;
  144. if (td->options & NAND_BBT_VERSION)
  145. len++;
  146. return len;
  147. }
  148. /**
  149. * read_bbt - [GENERIC] Read the bad block table starting from page
  150. * @mtd: MTD device structure
  151. * @buf: temporary buffer
  152. * @page: the starting page
  153. * @num: the number of bbt descriptors to read
  154. * @td: the bbt describtion table
  155. * @offs: block number offset in the table
  156. *
  157. * Read the bad block table starting from page.
  158. */
  159. static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
  160. struct nand_bbt_descr *td, int offs)
  161. {
  162. int res, ret = 0, i, j, act = 0;
  163. struct nand_chip *this = mtd->priv;
  164. size_t retlen, len, totlen;
  165. loff_t from;
  166. int bits = td->options & NAND_BBT_NRBITS_MSK;
  167. uint8_t msk = (uint8_t)((1 << bits) - 1);
  168. u32 marker_len;
  169. int reserved_block_code = td->reserved_block_code;
  170. totlen = (num * bits) >> 3;
  171. marker_len = add_marker_len(td);
  172. from = ((loff_t)page) << this->page_shift;
  173. while (totlen) {
  174. len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
  175. if (marker_len) {
  176. /*
  177. * In case the BBT marker is not in the OOB area it
  178. * will be just in the first page.
  179. */
  180. len -= marker_len;
  181. from += marker_len;
  182. marker_len = 0;
  183. }
  184. res = mtd_read(mtd, from, len, &retlen, buf);
  185. if (res < 0) {
  186. if (mtd_is_eccerr(res)) {
  187. pr_info("nand_bbt: ECC error in BBT at "
  188. "0x%012llx\n", from & ~mtd->writesize);
  189. return res;
  190. } else if (mtd_is_bitflip(res)) {
  191. pr_info("nand_bbt: corrected error in BBT at "
  192. "0x%012llx\n", from & ~mtd->writesize);
  193. ret = res;
  194. } else {
  195. pr_info("nand_bbt: error reading BBT\n");
  196. return res;
  197. }
  198. }
  199. /* Analyse data */
  200. for (i = 0; i < len; i++) {
  201. uint8_t dat = buf[i];
  202. for (j = 0; j < 8; j += bits, act++) {
  203. uint8_t tmp = (dat >> j) & msk;
  204. if (tmp == msk)
  205. continue;
  206. if (reserved_block_code && (tmp == reserved_block_code)) {
  207. pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
  208. (loff_t)(offs + act) <<
  209. this->bbt_erase_shift);
  210. bbt_mark_entry(this, offs + act,
  211. BBT_BLOCK_RESERVED);
  212. mtd->ecc_stats.bbtblocks++;
  213. continue;
  214. }
  215. /*
  216. * Leave it for now, if it's matured we can
  217. * move this message to pr_debug.
  218. */
  219. pr_info("nand_read_bbt: bad block at 0x%012llx\n",
  220. (loff_t)(offs + act) <<
  221. this->bbt_erase_shift);
  222. /* Factory marked bad or worn out? */
  223. if (tmp == 0)
  224. bbt_mark_entry(this, offs + act,
  225. BBT_BLOCK_FACTORY_BAD);
  226. else
  227. bbt_mark_entry(this, offs + act,
  228. BBT_BLOCK_WORN);
  229. mtd->ecc_stats.badblocks++;
  230. }
  231. }
  232. totlen -= len;
  233. from += len;
  234. }
  235. return ret;
  236. }
  237. /**
  238. * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  239. * @mtd: MTD device structure
  240. * @buf: temporary buffer
  241. * @td: descriptor for the bad block table
  242. * @chip: read the table for a specific chip, -1 read all chips; applies only if
  243. * NAND_BBT_PERCHIP option is set
  244. *
  245. * Read the bad block table for all chips starting at a given page. We assume
  246. * that the bbt bits are in consecutive order.
  247. */
  248. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  249. {
  250. struct nand_chip *this = mtd->priv;
  251. int res = 0, i;
  252. if (td->options & NAND_BBT_PERCHIP) {
  253. int offs = 0;
  254. for (i = 0; i < this->numchips; i++) {
  255. if (chip == -1 || chip == i)
  256. res = read_bbt(mtd, buf, td->pages[i],
  257. this->chipsize >> this->bbt_erase_shift,
  258. td, offs);
  259. if (res)
  260. return res;
  261. offs += this->chipsize >> this->bbt_erase_shift;
  262. }
  263. } else {
  264. res = read_bbt(mtd, buf, td->pages[0],
  265. mtd->size >> this->bbt_erase_shift, td, 0);
  266. if (res)
  267. return res;
  268. }
  269. return 0;
  270. }
  271. /* BBT marker is in the first page, no OOB */
  272. static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  273. struct nand_bbt_descr *td)
  274. {
  275. size_t retlen;
  276. size_t len;
  277. len = td->len;
  278. if (td->options & NAND_BBT_VERSION)
  279. len++;
  280. return mtd_read(mtd, offs, len, &retlen, buf);
  281. }
  282. /**
  283. * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
  284. * @mtd: MTD device structure
  285. * @buf: temporary buffer
  286. * @offs: offset at which to scan
  287. * @len: length of data region to read
  288. *
  289. * Scan read data from data+OOB. May traverse multiple pages, interleaving
  290. * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
  291. * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
  292. */
  293. static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  294. size_t len)
  295. {
  296. struct mtd_oob_ops ops;
  297. int res, ret = 0;
  298. ops.mode = MTD_OPS_PLACE_OOB;
  299. ops.ooboffs = 0;
  300. ops.ooblen = mtd->oobsize;
  301. while (len > 0) {
  302. ops.datbuf = buf;
  303. ops.len = min(len, (size_t)mtd->writesize);
  304. ops.oobbuf = buf + ops.len;
  305. res = mtd_read_oob(mtd, offs, &ops);
  306. if (res) {
  307. if (!mtd_is_bitflip_or_eccerr(res))
  308. return res;
  309. else if (mtd_is_eccerr(res) || !ret)
  310. ret = res;
  311. }
  312. buf += mtd->oobsize + mtd->writesize;
  313. len -= mtd->writesize;
  314. offs += mtd->writesize;
  315. }
  316. return ret;
  317. }
  318. static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  319. size_t len, struct nand_bbt_descr *td)
  320. {
  321. if (td->options & NAND_BBT_NO_OOB)
  322. return scan_read_data(mtd, buf, offs, td);
  323. else
  324. return scan_read_oob(mtd, buf, offs, len);
  325. }
  326. /* Scan write data with oob to flash */
  327. static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
  328. uint8_t *buf, uint8_t *oob)
  329. {
  330. struct mtd_oob_ops ops;
  331. ops.mode = MTD_OPS_PLACE_OOB;
  332. ops.ooboffs = 0;
  333. ops.ooblen = mtd->oobsize;
  334. ops.datbuf = buf;
  335. ops.oobbuf = oob;
  336. ops.len = len;
  337. return mtd_write_oob(mtd, offs, &ops);
  338. }
  339. static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
  340. {
  341. u32 ver_offs = td->veroffs;
  342. if (!(td->options & NAND_BBT_NO_OOB))
  343. ver_offs += mtd->writesize;
  344. return ver_offs;
  345. }
  346. /**
  347. * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
  348. * @mtd: MTD device structure
  349. * @buf: temporary buffer
  350. * @td: descriptor for the bad block table
  351. * @md: descriptor for the bad block table mirror
  352. *
  353. * Read the bad block table(s) for all chips starting at a given page. We
  354. * assume that the bbt bits are in consecutive order.
  355. */
  356. static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
  357. struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  358. {
  359. struct nand_chip *this = mtd->priv;
  360. /* Read the primary version, if available */
  361. if (td->options & NAND_BBT_VERSION) {
  362. scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
  363. mtd->writesize, td);
  364. td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
  365. pr_info("Bad block table at page %d, version 0x%02X\n",
  366. td->pages[0], td->version[0]);
  367. }
  368. /* Read the mirror version, if available */
  369. if (md && (md->options & NAND_BBT_VERSION)) {
  370. scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
  371. mtd->writesize, md);
  372. md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
  373. pr_info("Bad block table at page %d, version 0x%02X\n",
  374. md->pages[0], md->version[0]);
  375. }
  376. }
  377. /* Scan a given block partially */
  378. static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
  379. loff_t offs, uint8_t *buf, int numpages)
  380. {
  381. struct mtd_oob_ops ops;
  382. int j, ret;
  383. ops.ooblen = mtd->oobsize;
  384. ops.oobbuf = buf;
  385. ops.ooboffs = 0;
  386. ops.datbuf = NULL;
  387. ops.mode = MTD_OPS_PLACE_OOB;
  388. for (j = 0; j < numpages; j++) {
  389. /*
  390. * Read the full oob until read_oob is fixed to handle single
  391. * byte reads for 16 bit buswidth.
  392. */
  393. ret = mtd_read_oob(mtd, offs, &ops);
  394. /* Ignore ECC errors when checking for BBM */
  395. if (ret && !mtd_is_bitflip_or_eccerr(ret))
  396. return ret;
  397. if (check_short_pattern(buf, bd))
  398. return 1;
  399. offs += mtd->writesize;
  400. }
  401. return 0;
  402. }
  403. /**
  404. * create_bbt - [GENERIC] Create a bad block table by scanning the device
  405. * @mtd: MTD device structure
  406. * @buf: temporary buffer
  407. * @bd: descriptor for the good/bad block search pattern
  408. * @chip: create the table for a specific chip, -1 read all chips; applies only
  409. * if NAND_BBT_PERCHIP option is set
  410. *
  411. * Create a bad block table by scanning the device for the given good/bad block
  412. * identify pattern.
  413. */
  414. static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
  415. struct nand_bbt_descr *bd, int chip)
  416. {
  417. struct nand_chip *this = mtd->priv;
  418. int i, numblocks, numpages;
  419. int startblock;
  420. loff_t from;
  421. pr_info("Scanning device for bad blocks\n");
  422. if (bd->options & NAND_BBT_SCAN2NDPAGE)
  423. numpages = 2;
  424. else
  425. numpages = 1;
  426. if (chip == -1) {
  427. numblocks = mtd->size >> this->bbt_erase_shift;
  428. startblock = 0;
  429. from = 0;
  430. } else {
  431. if (chip >= this->numchips) {
  432. pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
  433. chip + 1, this->numchips);
  434. return -EINVAL;
  435. }
  436. numblocks = this->chipsize >> this->bbt_erase_shift;
  437. startblock = chip * numblocks;
  438. numblocks += startblock;
  439. from = (loff_t)startblock << this->bbt_erase_shift;
  440. }
  441. if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
  442. from += mtd->erasesize - (mtd->writesize * numpages);
  443. for (i = startblock; i < numblocks; i++) {
  444. int ret;
  445. BUG_ON(bd->options & NAND_BBT_NO_OOB);
  446. ret = scan_block_fast(mtd, bd, from, buf, numpages);
  447. if (ret < 0)
  448. return ret;
  449. if (ret) {
  450. bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
  451. pr_warn("Bad eraseblock %d at 0x%012llx\n",
  452. i, (unsigned long long)from);
  453. mtd->ecc_stats.badblocks++;
  454. }
  455. from += (1 << this->bbt_erase_shift);
  456. }
  457. return 0;
  458. }
  459. /**
  460. * search_bbt - [GENERIC] scan the device for a specific bad block table
  461. * @mtd: MTD device structure
  462. * @buf: temporary buffer
  463. * @td: descriptor for the bad block table
  464. *
  465. * Read the bad block table by searching for a given ident pattern. Search is
  466. * preformed either from the beginning up or from the end of the device
  467. * downwards. The search starts always at the start of a block. If the option
  468. * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
  469. * the bad block information of this chip. This is necessary to provide support
  470. * for certain DOC devices.
  471. *
  472. * The bbt ident pattern resides in the oob area of the first page in a block.
  473. */
  474. static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  475. {
  476. struct nand_chip *this = mtd->priv;
  477. int i, chips;
  478. int bits, startblock, block, dir;
  479. int scanlen = mtd->writesize + mtd->oobsize;
  480. int bbtblocks;
  481. int blocktopage = this->bbt_erase_shift - this->page_shift;
  482. /* Search direction top -> down? */
  483. if (td->options & NAND_BBT_LASTBLOCK) {
  484. startblock = (mtd->size >> this->bbt_erase_shift) - 1;
  485. dir = -1;
  486. } else {
  487. startblock = 0;
  488. dir = 1;
  489. }
  490. /* Do we have a bbt per chip? */
  491. if (td->options & NAND_BBT_PERCHIP) {
  492. chips = this->numchips;
  493. bbtblocks = this->chipsize >> this->bbt_erase_shift;
  494. startblock &= bbtblocks - 1;
  495. } else {
  496. chips = 1;
  497. bbtblocks = mtd->size >> this->bbt_erase_shift;
  498. }
  499. /* Number of bits for each erase block in the bbt */
  500. bits = td->options & NAND_BBT_NRBITS_MSK;
  501. for (i = 0; i < chips; i++) {
  502. /* Reset version information */
  503. td->version[i] = 0;
  504. td->pages[i] = -1;
  505. /* Scan the maximum number of blocks */
  506. for (block = 0; block < td->maxblocks; block++) {
  507. int actblock = startblock + dir * block;
  508. loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
  509. /* Read first page */
  510. scan_read(mtd, buf, offs, mtd->writesize, td);
  511. if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
  512. td->pages[i] = actblock << blocktopage;
  513. if (td->options & NAND_BBT_VERSION) {
  514. offs = bbt_get_ver_offs(mtd, td);
  515. td->version[i] = buf[offs];
  516. }
  517. break;
  518. }
  519. }
  520. startblock += this->chipsize >> this->bbt_erase_shift;
  521. }
  522. /* Check, if we found a bbt for each requested chip */
  523. for (i = 0; i < chips; i++) {
  524. if (td->pages[i] == -1)
  525. pr_warn("Bad block table not found for chip %d\n", i);
  526. else
  527. pr_info("Bad block table found at page %d, version "
  528. "0x%02X\n", td->pages[i], td->version[i]);
  529. }
  530. return 0;
  531. }
  532. /**
  533. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  534. * @mtd: MTD device structure
  535. * @buf: temporary buffer
  536. * @td: descriptor for the bad block table
  537. * @md: descriptor for the bad block table mirror
  538. *
  539. * Search and read the bad block table(s).
  540. */
  541. static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
  542. struct nand_bbt_descr *td,
  543. struct nand_bbt_descr *md)
  544. {
  545. /* Search the primary table */
  546. search_bbt(mtd, buf, td);
  547. /* Search the mirror table */
  548. if (md)
  549. search_bbt(mtd, buf, md);
  550. }
  551. /**
  552. * write_bbt - [GENERIC] (Re)write the bad block table
  553. * @mtd: MTD device structure
  554. * @buf: temporary buffer
  555. * @td: descriptor for the bad block table
  556. * @md: descriptor for the bad block table mirror
  557. * @chipsel: selector for a specific chip, -1 for all
  558. *
  559. * (Re)write the bad block table.
  560. */
  561. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  562. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  563. int chipsel)
  564. {
  565. struct nand_chip *this = mtd->priv;
  566. struct erase_info einfo;
  567. int i, res, chip = 0;
  568. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  569. int nrchips, pageoffs, ooboffs;
  570. uint8_t msk[4];
  571. uint8_t rcode = td->reserved_block_code;
  572. size_t retlen, len = 0;
  573. loff_t to;
  574. struct mtd_oob_ops ops;
  575. ops.ooblen = mtd->oobsize;
  576. ops.ooboffs = 0;
  577. ops.datbuf = NULL;
  578. ops.mode = MTD_OPS_PLACE_OOB;
  579. if (!rcode)
  580. rcode = 0xff;
  581. /* Write bad block table per chip rather than per device? */
  582. if (td->options & NAND_BBT_PERCHIP) {
  583. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  584. /* Full device write or specific chip? */
  585. if (chipsel == -1) {
  586. nrchips = this->numchips;
  587. } else {
  588. nrchips = chipsel + 1;
  589. chip = chipsel;
  590. }
  591. } else {
  592. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  593. nrchips = 1;
  594. }
  595. /* Loop through the chips */
  596. for (; chip < nrchips; chip++) {
  597. /*
  598. * There was already a version of the table, reuse the page
  599. * This applies for absolute placement too, as we have the
  600. * page nr. in td->pages.
  601. */
  602. if (td->pages[chip] != -1) {
  603. page = td->pages[chip];
  604. goto write;
  605. }
  606. /*
  607. * Automatic placement of the bad block table. Search direction
  608. * top -> down?
  609. */
  610. if (td->options & NAND_BBT_LASTBLOCK) {
  611. startblock = numblocks * (chip + 1) - 1;
  612. dir = -1;
  613. } else {
  614. startblock = chip * numblocks;
  615. dir = 1;
  616. }
  617. for (i = 0; i < td->maxblocks; i++) {
  618. int block = startblock + dir * i;
  619. /* Check, if the block is bad */
  620. switch (bbt_get_entry(this, block)) {
  621. case BBT_BLOCK_WORN:
  622. case BBT_BLOCK_FACTORY_BAD:
  623. continue;
  624. }
  625. page = block <<
  626. (this->bbt_erase_shift - this->page_shift);
  627. /* Check, if the block is used by the mirror table */
  628. if (!md || md->pages[chip] != page)
  629. goto write;
  630. }
  631. pr_err("No space left to write bad block table\n");
  632. return -ENOSPC;
  633. write:
  634. /* Set up shift count and masks for the flash table */
  635. bits = td->options & NAND_BBT_NRBITS_MSK;
  636. msk[2] = ~rcode;
  637. switch (bits) {
  638. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  639. msk[3] = 0x01;
  640. break;
  641. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  642. msk[3] = 0x03;
  643. break;
  644. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  645. msk[3] = 0x0f;
  646. break;
  647. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  648. msk[3] = 0xff;
  649. break;
  650. default: return -EINVAL;
  651. }
  652. to = ((loff_t)page) << this->page_shift;
  653. /* Must we save the block contents? */
  654. if (td->options & NAND_BBT_SAVECONTENT) {
  655. /* Make it block aligned */
  656. to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
  657. len = 1 << this->bbt_erase_shift;
  658. res = mtd_read(mtd, to, len, &retlen, buf);
  659. if (res < 0) {
  660. if (retlen != len) {
  661. pr_info("nand_bbt: error reading block "
  662. "for writing the bad block table\n");
  663. return res;
  664. }
  665. pr_warn("nand_bbt: ECC error while reading "
  666. "block for writing bad block table\n");
  667. }
  668. /* Read oob data */
  669. ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
  670. ops.oobbuf = &buf[len];
  671. res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
  672. if (res < 0 || ops.oobretlen != ops.ooblen)
  673. goto outerr;
  674. /* Calc the byte offset in the buffer */
  675. pageoffs = page - (int)(to >> this->page_shift);
  676. offs = pageoffs << this->page_shift;
  677. /* Preset the bbt area with 0xff */
  678. memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
  679. ooboffs = len + (pageoffs * mtd->oobsize);
  680. } else if (td->options & NAND_BBT_NO_OOB) {
  681. ooboffs = 0;
  682. offs = td->len;
  683. /* The version byte */
  684. if (td->options & NAND_BBT_VERSION)
  685. offs++;
  686. /* Calc length */
  687. len = (size_t)(numblocks >> sft);
  688. len += offs;
  689. /* Make it page aligned! */
  690. len = ALIGN(len, mtd->writesize);
  691. /* Preset the buffer with 0xff */
  692. memset(buf, 0xff, len);
  693. /* Pattern is located at the begin of first page */
  694. memcpy(buf, td->pattern, td->len);
  695. } else {
  696. /* Calc length */
  697. len = (size_t)(numblocks >> sft);
  698. /* Make it page aligned! */
  699. len = ALIGN(len, mtd->writesize);
  700. /* Preset the buffer with 0xff */
  701. memset(buf, 0xff, len +
  702. (len >> this->page_shift)* mtd->oobsize);
  703. offs = 0;
  704. ooboffs = len;
  705. /* Pattern is located in oob area of first page */
  706. memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
  707. }
  708. if (td->options & NAND_BBT_VERSION)
  709. buf[ooboffs + td->veroffs] = td->version[chip];
  710. /* Walk through the memory table */
  711. for (i = 0; i < numblocks; i++) {
  712. uint8_t dat;
  713. int sftcnt = (i << (3 - sft)) & sftmsk;
  714. dat = bbt_get_entry(this, chip * numblocks + i);
  715. /* Do not store the reserved bbt blocks! */
  716. buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
  717. }
  718. memset(&einfo, 0, sizeof(einfo));
  719. einfo.mtd = mtd;
  720. einfo.addr = to;
  721. einfo.len = 1 << this->bbt_erase_shift;
  722. res = nand_erase_nand(mtd, &einfo, 1);
  723. if (res < 0)
  724. goto outerr;
  725. res = scan_write_bbt(mtd, to, len, buf,
  726. td->options & NAND_BBT_NO_OOB ? NULL :
  727. &buf[len]);
  728. if (res < 0)
  729. goto outerr;
  730. pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
  731. (unsigned long long)to, td->version[chip]);
  732. /* Mark it as used */
  733. td->pages[chip] = page;
  734. }
  735. return 0;
  736. outerr:
  737. pr_warn("nand_bbt: error while writing bad block table %d\n", res);
  738. return res;
  739. }
  740. /**
  741. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  742. * @mtd: MTD device structure
  743. * @bd: descriptor for the good/bad block search pattern
  744. *
  745. * The function creates a memory based bbt by scanning the device for
  746. * manufacturer / software marked good / bad blocks.
  747. */
  748. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  749. {
  750. struct nand_chip *this = mtd->priv;
  751. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  752. }
  753. /**
  754. * check_create - [GENERIC] create and write bbt(s) if necessary
  755. * @mtd: MTD device structure
  756. * @buf: temporary buffer
  757. * @bd: descriptor for the good/bad block search pattern
  758. *
  759. * The function checks the results of the previous call to read_bbt and creates
  760. * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
  761. * for the chip/device. Update is necessary if one of the tables is missing or
  762. * the version nr. of one table is less than the other.
  763. */
  764. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  765. {
  766. int i, chips, writeops, create, chipsel, res, res2;
  767. struct nand_chip *this = mtd->priv;
  768. struct nand_bbt_descr *td = this->bbt_td;
  769. struct nand_bbt_descr *md = this->bbt_md;
  770. struct nand_bbt_descr *rd, *rd2;
  771. /* Do we have a bbt per chip? */
  772. if (td->options & NAND_BBT_PERCHIP)
  773. chips = this->numchips;
  774. else
  775. chips = 1;
  776. for (i = 0; i < chips; i++) {
  777. writeops = 0;
  778. create = 0;
  779. rd = NULL;
  780. rd2 = NULL;
  781. res = res2 = 0;
  782. /* Per chip or per device? */
  783. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  784. /* Mirrored table available? */
  785. if (md) {
  786. if (td->pages[i] == -1 && md->pages[i] == -1) {
  787. create = 1;
  788. writeops = 0x03;
  789. } else if (td->pages[i] == -1) {
  790. rd = md;
  791. writeops = 0x01;
  792. } else if (md->pages[i] == -1) {
  793. rd = td;
  794. writeops = 0x02;
  795. } else if (td->version[i] == md->version[i]) {
  796. rd = td;
  797. if (!(td->options & NAND_BBT_VERSION))
  798. rd2 = md;
  799. } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
  800. rd = td;
  801. writeops = 0x02;
  802. } else {
  803. rd = md;
  804. writeops = 0x01;
  805. }
  806. } else {
  807. if (td->pages[i] == -1) {
  808. create = 1;
  809. writeops = 0x01;
  810. } else {
  811. rd = td;
  812. }
  813. }
  814. if (create) {
  815. /* Create the bad block table by scanning the device? */
  816. if (!(td->options & NAND_BBT_CREATE))
  817. continue;
  818. /* Create the table in memory by scanning the chip(s) */
  819. if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
  820. create_bbt(mtd, buf, bd, chipsel);
  821. td->version[i] = 1;
  822. if (md)
  823. md->version[i] = 1;
  824. }
  825. /* Read back first? */
  826. if (rd) {
  827. res = read_abs_bbt(mtd, buf, rd, chipsel);
  828. if (mtd_is_eccerr(res)) {
  829. /* Mark table as invalid */
  830. rd->pages[i] = -1;
  831. rd->version[i] = 0;
  832. i--;
  833. continue;
  834. }
  835. }
  836. /* If they weren't versioned, read both */
  837. if (rd2) {
  838. res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
  839. if (mtd_is_eccerr(res2)) {
  840. /* Mark table as invalid */
  841. rd2->pages[i] = -1;
  842. rd2->version[i] = 0;
  843. i--;
  844. continue;
  845. }
  846. }
  847. /* Scrub the flash table(s)? */
  848. if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
  849. writeops = 0x03;
  850. /* Update version numbers before writing */
  851. if (md) {
  852. td->version[i] = max(td->version[i], md->version[i]);
  853. md->version[i] = td->version[i];
  854. }
  855. /* Write the bad block table to the device? */
  856. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  857. res = write_bbt(mtd, buf, td, md, chipsel);
  858. if (res < 0)
  859. return res;
  860. }
  861. /* Write the mirror bad block table to the device? */
  862. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  863. res = write_bbt(mtd, buf, md, td, chipsel);
  864. if (res < 0)
  865. return res;
  866. }
  867. }
  868. return 0;
  869. }
  870. /**
  871. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  872. * @mtd: MTD device structure
  873. * @td: bad block table descriptor
  874. *
  875. * The bad block table regions are marked as "bad" to prevent accidental
  876. * erasures / writes. The regions are identified by the mark 0x02.
  877. */
  878. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  879. {
  880. struct nand_chip *this = mtd->priv;
  881. int i, j, chips, block, nrblocks, update;
  882. uint8_t oldval;
  883. /* Do we have a bbt per chip? */
  884. if (td->options & NAND_BBT_PERCHIP) {
  885. chips = this->numchips;
  886. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  887. } else {
  888. chips = 1;
  889. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  890. }
  891. for (i = 0; i < chips; i++) {
  892. if ((td->options & NAND_BBT_ABSPAGE) ||
  893. !(td->options & NAND_BBT_WRITE)) {
  894. if (td->pages[i] == -1)
  895. continue;
  896. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  897. oldval = bbt_get_entry(this, block);
  898. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  899. if ((oldval != BBT_BLOCK_RESERVED) &&
  900. td->reserved_block_code)
  901. nand_update_bbt(mtd, (loff_t)block <<
  902. this->bbt_erase_shift);
  903. continue;
  904. }
  905. update = 0;
  906. if (td->options & NAND_BBT_LASTBLOCK)
  907. block = ((i + 1) * nrblocks) - td->maxblocks;
  908. else
  909. block = i * nrblocks;
  910. for (j = 0; j < td->maxblocks; j++) {
  911. oldval = bbt_get_entry(this, block);
  912. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  913. if (oldval != BBT_BLOCK_RESERVED)
  914. update = 1;
  915. block++;
  916. }
  917. /*
  918. * If we want reserved blocks to be recorded to flash, and some
  919. * new ones have been marked, then we need to update the stored
  920. * bbts. This should only happen once.
  921. */
  922. if (update && td->reserved_block_code)
  923. nand_update_bbt(mtd, (loff_t)(block - 1) <<
  924. this->bbt_erase_shift);
  925. }
  926. }
  927. /**
  928. * verify_bbt_descr - verify the bad block description
  929. * @mtd: MTD device structure
  930. * @bd: the table to verify
  931. *
  932. * This functions performs a few sanity checks on the bad block description
  933. * table.
  934. */
  935. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  936. {
  937. struct nand_chip *this = mtd->priv;
  938. u32 pattern_len;
  939. u32 bits;
  940. u32 table_size;
  941. if (!bd)
  942. return;
  943. pattern_len = bd->len;
  944. bits = bd->options & NAND_BBT_NRBITS_MSK;
  945. BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
  946. !(this->bbt_options & NAND_BBT_USE_FLASH));
  947. BUG_ON(!bits);
  948. if (bd->options & NAND_BBT_VERSION)
  949. pattern_len++;
  950. if (bd->options & NAND_BBT_NO_OOB) {
  951. BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
  952. BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
  953. BUG_ON(bd->offs);
  954. if (bd->options & NAND_BBT_VERSION)
  955. BUG_ON(bd->veroffs != bd->len);
  956. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  957. }
  958. if (bd->options & NAND_BBT_PERCHIP)
  959. table_size = this->chipsize >> this->bbt_erase_shift;
  960. else
  961. table_size = mtd->size >> this->bbt_erase_shift;
  962. table_size >>= 3;
  963. table_size *= bits;
  964. if (bd->options & NAND_BBT_NO_OOB)
  965. table_size += pattern_len;
  966. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  967. }
  968. /**
  969. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  970. * @mtd: MTD device structure
  971. * @bd: descriptor for the good/bad block search pattern
  972. *
  973. * The function checks, if a bad block table(s) is/are already available. If
  974. * not it scans the device for manufacturer marked good / bad blocks and writes
  975. * the bad block table(s) to the selected place.
  976. *
  977. * The bad block table memory is allocated here. It must be freed by calling
  978. * the nand_free_bbt function.
  979. */
  980. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  981. {
  982. struct nand_chip *this = mtd->priv;
  983. int len, res = 0;
  984. uint8_t *buf;
  985. struct nand_bbt_descr *td = this->bbt_td;
  986. struct nand_bbt_descr *md = this->bbt_md;
  987. len = mtd->size >> (this->bbt_erase_shift + 2);
  988. /*
  989. * Allocate memory (2bit per block) and clear the memory bad block
  990. * table.
  991. */
  992. this->bbt = kzalloc(len, GFP_KERNEL);
  993. if (!this->bbt)
  994. return -ENOMEM;
  995. /*
  996. * If no primary table decriptor is given, scan the device to build a
  997. * memory based bad block table.
  998. */
  999. if (!td) {
  1000. if ((res = nand_memory_bbt(mtd, bd))) {
  1001. pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
  1002. kfree(this->bbt);
  1003. this->bbt = NULL;
  1004. }
  1005. return res;
  1006. }
  1007. verify_bbt_descr(mtd, td);
  1008. verify_bbt_descr(mtd, md);
  1009. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1010. len = (1 << this->bbt_erase_shift);
  1011. len += (len >> this->page_shift) * mtd->oobsize;
  1012. buf = vmalloc(len);
  1013. if (!buf) {
  1014. kfree(this->bbt);
  1015. this->bbt = NULL;
  1016. return -ENOMEM;
  1017. }
  1018. /* Is the bbt at a given page? */
  1019. if (td->options & NAND_BBT_ABSPAGE) {
  1020. read_abs_bbts(mtd, buf, td, md);
  1021. } else {
  1022. /* Search the bad block table using a pattern in oob */
  1023. search_read_bbts(mtd, buf, td, md);
  1024. }
  1025. res = check_create(mtd, buf, bd);
  1026. /* Prevent the bbt regions from erasing / writing */
  1027. mark_bbt_region(mtd, td);
  1028. if (md)
  1029. mark_bbt_region(mtd, md);
  1030. vfree(buf);
  1031. return res;
  1032. }
  1033. /**
  1034. * nand_update_bbt - update bad block table(s)
  1035. * @mtd: MTD device structure
  1036. * @offs: the offset of the newly marked block
  1037. *
  1038. * The function updates the bad block table(s).
  1039. */
  1040. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1041. {
  1042. struct nand_chip *this = mtd->priv;
  1043. int len, res = 0;
  1044. int chip, chipsel;
  1045. uint8_t *buf;
  1046. struct nand_bbt_descr *td = this->bbt_td;
  1047. struct nand_bbt_descr *md = this->bbt_md;
  1048. if (!this->bbt || !td)
  1049. return -EINVAL;
  1050. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1051. len = (1 << this->bbt_erase_shift);
  1052. len += (len >> this->page_shift) * mtd->oobsize;
  1053. buf = kmalloc(len, GFP_KERNEL);
  1054. if (!buf)
  1055. return -ENOMEM;
  1056. /* Do we have a bbt per chip? */
  1057. if (td->options & NAND_BBT_PERCHIP) {
  1058. chip = (int)(offs >> this->chip_shift);
  1059. chipsel = chip;
  1060. } else {
  1061. chip = 0;
  1062. chipsel = -1;
  1063. }
  1064. td->version[chip]++;
  1065. if (md)
  1066. md->version[chip]++;
  1067. /* Write the bad block table to the device? */
  1068. if (td->options & NAND_BBT_WRITE) {
  1069. res = write_bbt(mtd, buf, td, md, chipsel);
  1070. if (res < 0)
  1071. goto out;
  1072. }
  1073. /* Write the mirror bad block table to the device? */
  1074. if (md && (md->options & NAND_BBT_WRITE)) {
  1075. res = write_bbt(mtd, buf, md, td, chipsel);
  1076. }
  1077. out:
  1078. kfree(buf);
  1079. return res;
  1080. }
  1081. /*
  1082. * Define some generic bad / good block scan pattern which are used
  1083. * while scanning a device for factory marked good / bad blocks.
  1084. */
  1085. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1086. /* Generic flash bbt descriptors */
  1087. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1088. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1089. static struct nand_bbt_descr bbt_main_descr = {
  1090. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1091. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1092. .offs = 8,
  1093. .len = 4,
  1094. .veroffs = 12,
  1095. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1096. .pattern = bbt_pattern
  1097. };
  1098. static struct nand_bbt_descr bbt_mirror_descr = {
  1099. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1100. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1101. .offs = 8,
  1102. .len = 4,
  1103. .veroffs = 12,
  1104. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1105. .pattern = mirror_pattern
  1106. };
  1107. static struct nand_bbt_descr bbt_main_no_oob_descr = {
  1108. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1109. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1110. | NAND_BBT_NO_OOB,
  1111. .len = 4,
  1112. .veroffs = 4,
  1113. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1114. .pattern = bbt_pattern
  1115. };
  1116. static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
  1117. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1118. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1119. | NAND_BBT_NO_OOB,
  1120. .len = 4,
  1121. .veroffs = 4,
  1122. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1123. .pattern = mirror_pattern
  1124. };
  1125. #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
  1126. /**
  1127. * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
  1128. * @this: NAND chip to create descriptor for
  1129. *
  1130. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1131. * based on the properties of @this. The new descriptor is stored in
  1132. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1133. * passed to this function.
  1134. */
  1135. static int nand_create_badblock_pattern(struct nand_chip *this)
  1136. {
  1137. struct nand_bbt_descr *bd;
  1138. if (this->badblock_pattern) {
  1139. pr_warn("Bad block pattern already allocated; not replacing\n");
  1140. return -EINVAL;
  1141. }
  1142. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1143. if (!bd)
  1144. return -ENOMEM;
  1145. bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
  1146. bd->offs = this->badblockpos;
  1147. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1148. bd->pattern = scan_ff_pattern;
  1149. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1150. this->badblock_pattern = bd;
  1151. return 0;
  1152. }
  1153. /**
  1154. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1155. * @mtd: MTD device structure
  1156. *
  1157. * This function selects the default bad block table support for the device and
  1158. * calls the nand_scan_bbt function.
  1159. */
  1160. int nand_default_bbt(struct mtd_info *mtd)
  1161. {
  1162. struct nand_chip *this = mtd->priv;
  1163. /* Is a flash based bad block table requested? */
  1164. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1165. /* Use the default pattern descriptors */
  1166. if (!this->bbt_td) {
  1167. if (this->bbt_options & NAND_BBT_NO_OOB) {
  1168. this->bbt_td = &bbt_main_no_oob_descr;
  1169. this->bbt_md = &bbt_mirror_no_oob_descr;
  1170. } else {
  1171. this->bbt_td = &bbt_main_descr;
  1172. this->bbt_md = &bbt_mirror_descr;
  1173. }
  1174. }
  1175. } else {
  1176. this->bbt_td = NULL;
  1177. this->bbt_md = NULL;
  1178. }
  1179. if (!this->badblock_pattern)
  1180. nand_create_badblock_pattern(this);
  1181. return nand_scan_bbt(mtd, this->badblock_pattern);
  1182. }
  1183. /**
  1184. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1185. * @mtd: MTD device structure
  1186. * @offs: offset in the device
  1187. * @allowbbt: allow access to bad block table region
  1188. */
  1189. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1190. {
  1191. struct nand_chip *this = mtd->priv;
  1192. int block, res;
  1193. block = (int)(offs >> this->bbt_erase_shift);
  1194. res = bbt_get_entry(this, block);
  1195. pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
  1196. "(block %d) 0x%02x\n",
  1197. (unsigned int)offs, block, res);
  1198. switch (res) {
  1199. case BBT_BLOCK_GOOD:
  1200. return 0;
  1201. case BBT_BLOCK_WORN:
  1202. return 1;
  1203. case BBT_BLOCK_RESERVED:
  1204. return allowbbt ? 0 : 1;
  1205. }
  1206. return 1;
  1207. }
  1208. /**
  1209. * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
  1210. * @mtd: MTD device structure
  1211. * @offs: offset of the bad block
  1212. */
  1213. int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
  1214. {
  1215. struct nand_chip *this = mtd->priv;
  1216. int block, ret = 0;
  1217. block = (int)(offs >> this->bbt_erase_shift);
  1218. /* Mark bad block in memory */
  1219. bbt_mark_entry(this, block, BBT_BLOCK_WORN);
  1220. /* Update flash-based bad block table */
  1221. if (this->bbt_options & NAND_BBT_USE_FLASH)
  1222. ret = nand_update_bbt(mtd, offs);
  1223. return ret;
  1224. }
  1225. EXPORT_SYMBOL(nand_scan_bbt);