nand_bbt.c 38 KB

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