nand_bbt.c 39 KB

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