nand_bbt.c 40 KB

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