nand_bbt.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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 0x%012llx\n",
  188. from & ~mtd->writesize);
  189. return res;
  190. } else if (mtd_is_bitflip(res)) {
  191. pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
  192. 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 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. for (i = 0; i < chips; i++) {
  500. /* Reset version information */
  501. td->version[i] = 0;
  502. td->pages[i] = -1;
  503. /* Scan the maximum number of blocks */
  504. for (block = 0; block < td->maxblocks; block++) {
  505. int actblock = startblock + dir * block;
  506. loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
  507. /* Read first page */
  508. scan_read(mtd, buf, offs, mtd->writesize, td);
  509. if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
  510. td->pages[i] = actblock << blocktopage;
  511. if (td->options & NAND_BBT_VERSION) {
  512. offs = bbt_get_ver_offs(mtd, td);
  513. td->version[i] = buf[offs];
  514. }
  515. break;
  516. }
  517. }
  518. startblock += this->chipsize >> this->bbt_erase_shift;
  519. }
  520. /* Check, if we found a bbt for each requested chip */
  521. for (i = 0; i < chips; i++) {
  522. if (td->pages[i] == -1)
  523. pr_warn("Bad block table not found for chip %d\n", i);
  524. else
  525. pr_info("Bad block table found at page %d, version 0x%02X\n",
  526. td->pages[i], td->version[i]);
  527. }
  528. return 0;
  529. }
  530. /**
  531. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  532. * @mtd: MTD device structure
  533. * @buf: temporary buffer
  534. * @td: descriptor for the bad block table
  535. * @md: descriptor for the bad block table mirror
  536. *
  537. * Search and read the bad block table(s).
  538. */
  539. static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
  540. struct nand_bbt_descr *td,
  541. struct nand_bbt_descr *md)
  542. {
  543. /* Search the primary table */
  544. search_bbt(mtd, buf, td);
  545. /* Search the mirror table */
  546. if (md)
  547. search_bbt(mtd, buf, md);
  548. }
  549. /**
  550. * write_bbt - [GENERIC] (Re)write the bad block table
  551. * @mtd: MTD device structure
  552. * @buf: temporary buffer
  553. * @td: descriptor for the bad block table
  554. * @md: descriptor for the bad block table mirror
  555. * @chipsel: selector for a specific chip, -1 for all
  556. *
  557. * (Re)write the bad block table.
  558. */
  559. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  560. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  561. int chipsel)
  562. {
  563. struct nand_chip *this = mtd->priv;
  564. struct erase_info einfo;
  565. int i, res, chip = 0;
  566. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  567. int nrchips, pageoffs, ooboffs;
  568. uint8_t msk[4];
  569. uint8_t rcode = td->reserved_block_code;
  570. size_t retlen, len = 0;
  571. loff_t to;
  572. struct mtd_oob_ops ops;
  573. ops.ooblen = mtd->oobsize;
  574. ops.ooboffs = 0;
  575. ops.datbuf = NULL;
  576. ops.mode = MTD_OPS_PLACE_OOB;
  577. if (!rcode)
  578. rcode = 0xff;
  579. /* Write bad block table per chip rather than per device? */
  580. if (td->options & NAND_BBT_PERCHIP) {
  581. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  582. /* Full device write or specific chip? */
  583. if (chipsel == -1) {
  584. nrchips = this->numchips;
  585. } else {
  586. nrchips = chipsel + 1;
  587. chip = chipsel;
  588. }
  589. } else {
  590. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  591. nrchips = 1;
  592. }
  593. /* Loop through the chips */
  594. for (; chip < nrchips; chip++) {
  595. /*
  596. * There was already a version of the table, reuse the page
  597. * This applies for absolute placement too, as we have the
  598. * page nr. in td->pages.
  599. */
  600. if (td->pages[chip] != -1) {
  601. page = td->pages[chip];
  602. goto write;
  603. }
  604. /*
  605. * Automatic placement of the bad block table. Search direction
  606. * top -> down?
  607. */
  608. if (td->options & NAND_BBT_LASTBLOCK) {
  609. startblock = numblocks * (chip + 1) - 1;
  610. dir = -1;
  611. } else {
  612. startblock = chip * numblocks;
  613. dir = 1;
  614. }
  615. for (i = 0; i < td->maxblocks; i++) {
  616. int block = startblock + dir * i;
  617. /* Check, if the block is bad */
  618. switch (bbt_get_entry(this, block)) {
  619. case BBT_BLOCK_WORN:
  620. case BBT_BLOCK_FACTORY_BAD:
  621. continue;
  622. }
  623. page = block <<
  624. (this->bbt_erase_shift - this->page_shift);
  625. /* Check, if the block is used by the mirror table */
  626. if (!md || md->pages[chip] != page)
  627. goto write;
  628. }
  629. pr_err("No space left to write bad block table\n");
  630. return -ENOSPC;
  631. write:
  632. /* Set up shift count and masks for the flash table */
  633. bits = td->options & NAND_BBT_NRBITS_MSK;
  634. msk[2] = ~rcode;
  635. switch (bits) {
  636. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  637. msk[3] = 0x01;
  638. break;
  639. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  640. msk[3] = 0x03;
  641. break;
  642. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  643. msk[3] = 0x0f;
  644. break;
  645. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  646. msk[3] = 0xff;
  647. break;
  648. default: return -EINVAL;
  649. }
  650. to = ((loff_t)page) << this->page_shift;
  651. /* Must we save the block contents? */
  652. if (td->options & NAND_BBT_SAVECONTENT) {
  653. /* Make it block aligned */
  654. to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
  655. len = 1 << this->bbt_erase_shift;
  656. res = mtd_read(mtd, to, len, &retlen, buf);
  657. if (res < 0) {
  658. if (retlen != len) {
  659. pr_info("nand_bbt: error reading block for writing the bad block table\n");
  660. return res;
  661. }
  662. pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
  663. }
  664. /* Read oob data */
  665. ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
  666. ops.oobbuf = &buf[len];
  667. res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
  668. if (res < 0 || ops.oobretlen != ops.ooblen)
  669. goto outerr;
  670. /* Calc the byte offset in the buffer */
  671. pageoffs = page - (int)(to >> this->page_shift);
  672. offs = pageoffs << this->page_shift;
  673. /* Preset the bbt area with 0xff */
  674. memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
  675. ooboffs = len + (pageoffs * mtd->oobsize);
  676. } else if (td->options & NAND_BBT_NO_OOB) {
  677. ooboffs = 0;
  678. offs = td->len;
  679. /* The version byte */
  680. if (td->options & NAND_BBT_VERSION)
  681. offs++;
  682. /* Calc length */
  683. len = (size_t)(numblocks >> sft);
  684. len += offs;
  685. /* Make it page aligned! */
  686. len = ALIGN(len, mtd->writesize);
  687. /* Preset the buffer with 0xff */
  688. memset(buf, 0xff, len);
  689. /* Pattern is located at the begin of first page */
  690. memcpy(buf, td->pattern, td->len);
  691. } else {
  692. /* Calc length */
  693. len = (size_t)(numblocks >> sft);
  694. /* Make it page aligned! */
  695. len = ALIGN(len, mtd->writesize);
  696. /* Preset the buffer with 0xff */
  697. memset(buf, 0xff, len +
  698. (len >> this->page_shift)* mtd->oobsize);
  699. offs = 0;
  700. ooboffs = len;
  701. /* Pattern is located in oob area of first page */
  702. memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
  703. }
  704. if (td->options & NAND_BBT_VERSION)
  705. buf[ooboffs + td->veroffs] = td->version[chip];
  706. /* Walk through the memory table */
  707. for (i = 0; i < numblocks; i++) {
  708. uint8_t dat;
  709. int sftcnt = (i << (3 - sft)) & sftmsk;
  710. dat = bbt_get_entry(this, chip * numblocks + i);
  711. /* Do not store the reserved bbt blocks! */
  712. buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
  713. }
  714. memset(&einfo, 0, sizeof(einfo));
  715. einfo.mtd = mtd;
  716. einfo.addr = to;
  717. einfo.len = 1 << this->bbt_erase_shift;
  718. res = nand_erase_nand(mtd, &einfo, 1);
  719. if (res < 0)
  720. goto outerr;
  721. res = scan_write_bbt(mtd, to, len, buf,
  722. td->options & NAND_BBT_NO_OOB ? NULL :
  723. &buf[len]);
  724. if (res < 0)
  725. goto outerr;
  726. pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
  727. (unsigned long long)to, td->version[chip]);
  728. /* Mark it as used */
  729. td->pages[chip] = page;
  730. }
  731. return 0;
  732. outerr:
  733. pr_warn("nand_bbt: error while writing bad block table %d\n", res);
  734. return res;
  735. }
  736. /**
  737. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  738. * @mtd: MTD device structure
  739. * @bd: descriptor for the good/bad block search pattern
  740. *
  741. * The function creates a memory based bbt by scanning the device for
  742. * manufacturer / software marked good / bad blocks.
  743. */
  744. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  745. {
  746. struct nand_chip *this = mtd->priv;
  747. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  748. }
  749. /**
  750. * check_create - [GENERIC] create and write bbt(s) if necessary
  751. * @mtd: MTD device structure
  752. * @buf: temporary buffer
  753. * @bd: descriptor for the good/bad block search pattern
  754. *
  755. * The function checks the results of the previous call to read_bbt and creates
  756. * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
  757. * for the chip/device. Update is necessary if one of the tables is missing or
  758. * the version nr. of one table is less than the other.
  759. */
  760. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  761. {
  762. int i, chips, writeops, create, chipsel, res, res2;
  763. struct nand_chip *this = mtd->priv;
  764. struct nand_bbt_descr *td = this->bbt_td;
  765. struct nand_bbt_descr *md = this->bbt_md;
  766. struct nand_bbt_descr *rd, *rd2;
  767. /* Do we have a bbt per chip? */
  768. if (td->options & NAND_BBT_PERCHIP)
  769. chips = this->numchips;
  770. else
  771. chips = 1;
  772. for (i = 0; i < chips; i++) {
  773. writeops = 0;
  774. create = 0;
  775. rd = NULL;
  776. rd2 = NULL;
  777. res = res2 = 0;
  778. /* Per chip or per device? */
  779. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  780. /* Mirrored table available? */
  781. if (md) {
  782. if (td->pages[i] == -1 && md->pages[i] == -1) {
  783. create = 1;
  784. writeops = 0x03;
  785. } else if (td->pages[i] == -1) {
  786. rd = md;
  787. writeops = 0x01;
  788. } else if (md->pages[i] == -1) {
  789. rd = td;
  790. writeops = 0x02;
  791. } else if (td->version[i] == md->version[i]) {
  792. rd = td;
  793. if (!(td->options & NAND_BBT_VERSION))
  794. rd2 = md;
  795. } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
  796. rd = td;
  797. writeops = 0x02;
  798. } else {
  799. rd = md;
  800. writeops = 0x01;
  801. }
  802. } else {
  803. if (td->pages[i] == -1) {
  804. create = 1;
  805. writeops = 0x01;
  806. } else {
  807. rd = td;
  808. }
  809. }
  810. if (create) {
  811. /* Create the bad block table by scanning the device? */
  812. if (!(td->options & NAND_BBT_CREATE))
  813. continue;
  814. /* Create the table in memory by scanning the chip(s) */
  815. if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
  816. create_bbt(mtd, buf, bd, chipsel);
  817. td->version[i] = 1;
  818. if (md)
  819. md->version[i] = 1;
  820. }
  821. /* Read back first? */
  822. if (rd) {
  823. res = read_abs_bbt(mtd, buf, rd, chipsel);
  824. if (mtd_is_eccerr(res)) {
  825. /* Mark table as invalid */
  826. rd->pages[i] = -1;
  827. rd->version[i] = 0;
  828. i--;
  829. continue;
  830. }
  831. }
  832. /* If they weren't versioned, read both */
  833. if (rd2) {
  834. res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
  835. if (mtd_is_eccerr(res2)) {
  836. /* Mark table as invalid */
  837. rd2->pages[i] = -1;
  838. rd2->version[i] = 0;
  839. i--;
  840. continue;
  841. }
  842. }
  843. /* Scrub the flash table(s)? */
  844. if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
  845. writeops = 0x03;
  846. /* Update version numbers before writing */
  847. if (md) {
  848. td->version[i] = max(td->version[i], md->version[i]);
  849. md->version[i] = td->version[i];
  850. }
  851. /* Write the bad block table to the device? */
  852. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  853. res = write_bbt(mtd, buf, td, md, chipsel);
  854. if (res < 0)
  855. return res;
  856. }
  857. /* Write the mirror bad block table to the device? */
  858. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  859. res = write_bbt(mtd, buf, md, td, chipsel);
  860. if (res < 0)
  861. return res;
  862. }
  863. }
  864. return 0;
  865. }
  866. /**
  867. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  868. * @mtd: MTD device structure
  869. * @td: bad block table descriptor
  870. *
  871. * The bad block table regions are marked as "bad" to prevent accidental
  872. * erasures / writes. The regions are identified by the mark 0x02.
  873. */
  874. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  875. {
  876. struct nand_chip *this = mtd->priv;
  877. int i, j, chips, block, nrblocks, update;
  878. uint8_t oldval;
  879. /* Do we have a bbt per chip? */
  880. if (td->options & NAND_BBT_PERCHIP) {
  881. chips = this->numchips;
  882. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  883. } else {
  884. chips = 1;
  885. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  886. }
  887. for (i = 0; i < chips; i++) {
  888. if ((td->options & NAND_BBT_ABSPAGE) ||
  889. !(td->options & NAND_BBT_WRITE)) {
  890. if (td->pages[i] == -1)
  891. continue;
  892. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  893. oldval = bbt_get_entry(this, block);
  894. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  895. if ((oldval != BBT_BLOCK_RESERVED) &&
  896. td->reserved_block_code)
  897. nand_update_bbt(mtd, (loff_t)block <<
  898. this->bbt_erase_shift);
  899. continue;
  900. }
  901. update = 0;
  902. if (td->options & NAND_BBT_LASTBLOCK)
  903. block = ((i + 1) * nrblocks) - td->maxblocks;
  904. else
  905. block = i * nrblocks;
  906. for (j = 0; j < td->maxblocks; j++) {
  907. oldval = bbt_get_entry(this, block);
  908. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  909. if (oldval != BBT_BLOCK_RESERVED)
  910. update = 1;
  911. block++;
  912. }
  913. /*
  914. * If we want reserved blocks to be recorded to flash, and some
  915. * new ones have been marked, then we need to update the stored
  916. * bbts. This should only happen once.
  917. */
  918. if (update && td->reserved_block_code)
  919. nand_update_bbt(mtd, (loff_t)(block - 1) <<
  920. this->bbt_erase_shift);
  921. }
  922. }
  923. /**
  924. * verify_bbt_descr - verify the bad block description
  925. * @mtd: MTD device structure
  926. * @bd: the table to verify
  927. *
  928. * This functions performs a few sanity checks on the bad block description
  929. * table.
  930. */
  931. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  932. {
  933. struct nand_chip *this = mtd->priv;
  934. u32 pattern_len;
  935. u32 bits;
  936. u32 table_size;
  937. if (!bd)
  938. return;
  939. pattern_len = bd->len;
  940. bits = bd->options & NAND_BBT_NRBITS_MSK;
  941. BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
  942. !(this->bbt_options & NAND_BBT_USE_FLASH));
  943. BUG_ON(!bits);
  944. if (bd->options & NAND_BBT_VERSION)
  945. pattern_len++;
  946. if (bd->options & NAND_BBT_NO_OOB) {
  947. BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
  948. BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
  949. BUG_ON(bd->offs);
  950. if (bd->options & NAND_BBT_VERSION)
  951. BUG_ON(bd->veroffs != bd->len);
  952. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  953. }
  954. if (bd->options & NAND_BBT_PERCHIP)
  955. table_size = this->chipsize >> this->bbt_erase_shift;
  956. else
  957. table_size = mtd->size >> this->bbt_erase_shift;
  958. table_size >>= 3;
  959. table_size *= bits;
  960. if (bd->options & NAND_BBT_NO_OOB)
  961. table_size += pattern_len;
  962. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  963. }
  964. /**
  965. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  966. * @mtd: MTD device structure
  967. * @bd: descriptor for the good/bad block search pattern
  968. *
  969. * The function checks, if a bad block table(s) is/are already available. If
  970. * not it scans the device for manufacturer marked good / bad blocks and writes
  971. * the bad block table(s) to the selected place.
  972. *
  973. * The bad block table memory is allocated here. It must be freed by calling
  974. * the nand_free_bbt function.
  975. */
  976. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  977. {
  978. struct nand_chip *this = mtd->priv;
  979. int len, res = 0;
  980. uint8_t *buf;
  981. struct nand_bbt_descr *td = this->bbt_td;
  982. struct nand_bbt_descr *md = this->bbt_md;
  983. len = mtd->size >> (this->bbt_erase_shift + 2);
  984. /*
  985. * Allocate memory (2bit per block) and clear the memory bad block
  986. * table.
  987. */
  988. this->bbt = kzalloc(len, GFP_KERNEL);
  989. if (!this->bbt)
  990. return -ENOMEM;
  991. /*
  992. * If no primary table decriptor is given, scan the device to build a
  993. * memory based bad block table.
  994. */
  995. if (!td) {
  996. if ((res = nand_memory_bbt(mtd, bd))) {
  997. pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
  998. kfree(this->bbt);
  999. this->bbt = NULL;
  1000. }
  1001. return res;
  1002. }
  1003. verify_bbt_descr(mtd, td);
  1004. verify_bbt_descr(mtd, md);
  1005. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1006. len = (1 << this->bbt_erase_shift);
  1007. len += (len >> this->page_shift) * mtd->oobsize;
  1008. buf = vmalloc(len);
  1009. if (!buf) {
  1010. kfree(this->bbt);
  1011. this->bbt = NULL;
  1012. return -ENOMEM;
  1013. }
  1014. /* Is the bbt at a given page? */
  1015. if (td->options & NAND_BBT_ABSPAGE) {
  1016. read_abs_bbts(mtd, buf, td, md);
  1017. } else {
  1018. /* Search the bad block table using a pattern in oob */
  1019. search_read_bbts(mtd, buf, td, md);
  1020. }
  1021. res = check_create(mtd, buf, bd);
  1022. /* Prevent the bbt regions from erasing / writing */
  1023. mark_bbt_region(mtd, td);
  1024. if (md)
  1025. mark_bbt_region(mtd, md);
  1026. vfree(buf);
  1027. return res;
  1028. }
  1029. /**
  1030. * nand_update_bbt - update bad block table(s)
  1031. * @mtd: MTD device structure
  1032. * @offs: the offset of the newly marked block
  1033. *
  1034. * The function updates the bad block table(s).
  1035. */
  1036. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1037. {
  1038. struct nand_chip *this = mtd->priv;
  1039. int len, res = 0;
  1040. int chip, chipsel;
  1041. uint8_t *buf;
  1042. struct nand_bbt_descr *td = this->bbt_td;
  1043. struct nand_bbt_descr *md = this->bbt_md;
  1044. if (!this->bbt || !td)
  1045. return -EINVAL;
  1046. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1047. len = (1 << this->bbt_erase_shift);
  1048. len += (len >> this->page_shift) * mtd->oobsize;
  1049. buf = kmalloc(len, GFP_KERNEL);
  1050. if (!buf)
  1051. return -ENOMEM;
  1052. /* Do we have a bbt per chip? */
  1053. if (td->options & NAND_BBT_PERCHIP) {
  1054. chip = (int)(offs >> this->chip_shift);
  1055. chipsel = chip;
  1056. } else {
  1057. chip = 0;
  1058. chipsel = -1;
  1059. }
  1060. td->version[chip]++;
  1061. if (md)
  1062. md->version[chip]++;
  1063. /* Write the bad block table to the device? */
  1064. if (td->options & NAND_BBT_WRITE) {
  1065. res = write_bbt(mtd, buf, td, md, chipsel);
  1066. if (res < 0)
  1067. goto out;
  1068. }
  1069. /* Write the mirror bad block table to the device? */
  1070. if (md && (md->options & NAND_BBT_WRITE)) {
  1071. res = write_bbt(mtd, buf, md, td, chipsel);
  1072. }
  1073. out:
  1074. kfree(buf);
  1075. return res;
  1076. }
  1077. /*
  1078. * Define some generic bad / good block scan pattern which are used
  1079. * while scanning a device for factory marked good / bad blocks.
  1080. */
  1081. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1082. /* Generic flash bbt descriptors */
  1083. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1084. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1085. static struct nand_bbt_descr bbt_main_descr = {
  1086. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1087. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1088. .offs = 8,
  1089. .len = 4,
  1090. .veroffs = 12,
  1091. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1092. .pattern = bbt_pattern
  1093. };
  1094. static struct nand_bbt_descr bbt_mirror_descr = {
  1095. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1096. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1097. .offs = 8,
  1098. .len = 4,
  1099. .veroffs = 12,
  1100. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1101. .pattern = mirror_pattern
  1102. };
  1103. static struct nand_bbt_descr bbt_main_no_oob_descr = {
  1104. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1105. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1106. | NAND_BBT_NO_OOB,
  1107. .len = 4,
  1108. .veroffs = 4,
  1109. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1110. .pattern = bbt_pattern
  1111. };
  1112. static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
  1113. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1114. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1115. | NAND_BBT_NO_OOB,
  1116. .len = 4,
  1117. .veroffs = 4,
  1118. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1119. .pattern = mirror_pattern
  1120. };
  1121. #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
  1122. /**
  1123. * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
  1124. * @this: NAND chip to create descriptor for
  1125. *
  1126. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1127. * based on the properties of @this. The new descriptor is stored in
  1128. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1129. * passed to this function.
  1130. */
  1131. static int nand_create_badblock_pattern(struct nand_chip *this)
  1132. {
  1133. struct nand_bbt_descr *bd;
  1134. if (this->badblock_pattern) {
  1135. pr_warn("Bad block pattern already allocated; not replacing\n");
  1136. return -EINVAL;
  1137. }
  1138. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1139. if (!bd)
  1140. return -ENOMEM;
  1141. bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
  1142. bd->offs = this->badblockpos;
  1143. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1144. bd->pattern = scan_ff_pattern;
  1145. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1146. this->badblock_pattern = bd;
  1147. return 0;
  1148. }
  1149. /**
  1150. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1151. * @mtd: MTD device structure
  1152. *
  1153. * This function selects the default bad block table support for the device and
  1154. * calls the nand_scan_bbt function.
  1155. */
  1156. int nand_default_bbt(struct mtd_info *mtd)
  1157. {
  1158. struct nand_chip *this = mtd->priv;
  1159. int ret;
  1160. /* Is a flash based bad block table requested? */
  1161. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1162. /* Use the default pattern descriptors */
  1163. if (!this->bbt_td) {
  1164. if (this->bbt_options & NAND_BBT_NO_OOB) {
  1165. this->bbt_td = &bbt_main_no_oob_descr;
  1166. this->bbt_md = &bbt_mirror_no_oob_descr;
  1167. } else {
  1168. this->bbt_td = &bbt_main_descr;
  1169. this->bbt_md = &bbt_mirror_descr;
  1170. }
  1171. }
  1172. } else {
  1173. this->bbt_td = NULL;
  1174. this->bbt_md = NULL;
  1175. }
  1176. if (!this->badblock_pattern) {
  1177. ret = nand_create_badblock_pattern(this);
  1178. if (ret)
  1179. return ret;
  1180. }
  1181. return nand_scan_bbt(mtd, this->badblock_pattern);
  1182. }
  1183. /**
  1184. * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
  1185. * @mtd: MTD device structure
  1186. * @offs: offset in the device
  1187. */
  1188. int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
  1189. {
  1190. struct nand_chip *this = mtd->priv;
  1191. int block;
  1192. block = (int)(offs >> this->bbt_erase_shift);
  1193. return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
  1194. }
  1195. /**
  1196. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1197. * @mtd: MTD device structure
  1198. * @offs: offset in the device
  1199. * @allowbbt: allow access to bad block table region
  1200. */
  1201. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1202. {
  1203. struct nand_chip *this = mtd->priv;
  1204. int block, res;
  1205. block = (int)(offs >> this->bbt_erase_shift);
  1206. res = bbt_get_entry(this, block);
  1207. pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  1208. (unsigned int)offs, block, res);
  1209. switch (res) {
  1210. case BBT_BLOCK_GOOD:
  1211. return 0;
  1212. case BBT_BLOCK_WORN:
  1213. return 1;
  1214. case BBT_BLOCK_RESERVED:
  1215. return allowbbt ? 0 : 1;
  1216. }
  1217. return 1;
  1218. }
  1219. /**
  1220. * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
  1221. * @mtd: MTD device structure
  1222. * @offs: offset of the bad block
  1223. */
  1224. int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
  1225. {
  1226. struct nand_chip *this = mtd->priv;
  1227. int block, ret = 0;
  1228. block = (int)(offs >> this->bbt_erase_shift);
  1229. /* Mark bad block in memory */
  1230. bbt_mark_entry(this, block, BBT_BLOCK_WORN);
  1231. /* Update flash-based bad block table */
  1232. if (this->bbt_options & NAND_BBT_USE_FLASH)
  1233. ret = nand_update_bbt(mtd, offs);
  1234. return ret;
  1235. }
  1236. EXPORT_SYMBOL(nand_scan_bbt);