nand_bbt.c 38 KB

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