nand_bbt.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
  70. {
  71. int ret;
  72. ret = memcmp(buf, td->pattern, td->len);
  73. if (!ret)
  74. return ret;
  75. return -1;
  76. }
  77. /**
  78. * check_pattern - [GENERIC] check if a pattern is in the buffer
  79. * @buf: the buffer to search
  80. * @len: the length of buffer to search
  81. * @paglen: the pagelength
  82. * @td: search pattern descriptor
  83. *
  84. * Check for a pattern at the given place. Used to search bad block tables and
  85. * good / bad block identifiers. If the SCAN_EMPTY option is set then check, if
  86. * all bytes except the pattern area contain 0xff.
  87. */
  88. static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
  89. {
  90. int i, end = 0;
  91. uint8_t *p = buf;
  92. if (td->options & NAND_BBT_NO_OOB)
  93. return check_pattern_no_oob(buf, td);
  94. end = paglen + td->offs;
  95. if (td->options & NAND_BBT_SCANEMPTY) {
  96. for (i = 0; i < end; i++) {
  97. if (p[i] != 0xff)
  98. return -1;
  99. }
  100. }
  101. p += end;
  102. /* Compare the pattern */
  103. for (i = 0; i < td->len; i++) {
  104. if (p[i] != td->pattern[i])
  105. return -1;
  106. }
  107. if (td->options & NAND_BBT_SCANEMPTY) {
  108. p += td->len;
  109. end += td->len;
  110. for (i = end; i < len; i++) {
  111. if (*p++ != 0xff)
  112. return -1;
  113. }
  114. }
  115. return 0;
  116. }
  117. /**
  118. * check_short_pattern - [GENERIC] check if a pattern is in the buffer
  119. * @buf: the buffer to search
  120. * @td: search pattern descriptor
  121. *
  122. * Check for a pattern at the given place. Used to search bad block tables and
  123. * good / bad block identifiers. Same as check_pattern, but no optional empty
  124. * check.
  125. */
  126. static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
  127. {
  128. int i;
  129. uint8_t *p = buf;
  130. /* Compare the pattern */
  131. for (i = 0; i < td->len; i++) {
  132. if (p[td->offs + i] != td->pattern[i])
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. /**
  138. * add_marker_len - compute the length of the marker in data area
  139. * @td: BBT descriptor used for computation
  140. *
  141. * The length will be 0 if the marker is located in OOB area.
  142. */
  143. static u32 add_marker_len(struct nand_bbt_descr *td)
  144. {
  145. u32 len;
  146. if (!(td->options & NAND_BBT_NO_OOB))
  147. return 0;
  148. len = td->len;
  149. if (td->options & NAND_BBT_VERSION)
  150. len++;
  151. return len;
  152. }
  153. /**
  154. * read_bbt - [GENERIC] Read the bad block table starting from page
  155. * @mtd: MTD device structure
  156. * @buf: temporary buffer
  157. * @page: the starting page
  158. * @num: the number of bbt descriptors to read
  159. * @td: the bbt describtion table
  160. * @offs: offset in the memory table
  161. *
  162. * Read the bad block table starting from page.
  163. */
  164. static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
  165. struct nand_bbt_descr *td, int offs)
  166. {
  167. int res, i, j, act = 0;
  168. struct nand_chip *this = mtd->priv;
  169. size_t retlen, len, totlen;
  170. loff_t from;
  171. int bits = td->options & NAND_BBT_NRBITS_MSK;
  172. uint8_t msk = (uint8_t) ((1 << bits) - 1);
  173. u32 marker_len;
  174. int reserved_block_code = td->reserved_block_code;
  175. totlen = (num * bits) >> 3;
  176. marker_len = add_marker_len(td);
  177. from = ((loff_t) page) << this->page_shift;
  178. while (totlen) {
  179. len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
  180. if (marker_len) {
  181. /*
  182. * In case the BBT marker is not in the OOB area it
  183. * will be just in the first page.
  184. */
  185. len -= marker_len;
  186. from += marker_len;
  187. marker_len = 0;
  188. }
  189. res = mtd->read(mtd, from, len, &retlen, buf);
  190. if (res < 0) {
  191. if (retlen != len) {
  192. pr_info("nand_bbt: error reading bad block table\n");
  193. return res;
  194. }
  195. pr_warn("nand_bbt: ECC error while reading bad block table\n");
  196. }
  197. /* Analyse data */
  198. for (i = 0; i < len; i++) {
  199. uint8_t dat = buf[i];
  200. for (j = 0; j < 8; j += bits, act += 2) {
  201. uint8_t tmp = (dat >> j) & msk;
  202. if (tmp == msk)
  203. continue;
  204. if (reserved_block_code && (tmp == reserved_block_code)) {
  205. pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
  206. (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  207. this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
  208. mtd->ecc_stats.bbtblocks++;
  209. continue;
  210. }
  211. /*
  212. * Leave it for now, if it's matured we can
  213. * move this message to pr_debug.
  214. */
  215. pr_info("nand_read_bbt: bad block at 0x%012llx\n",
  216. (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  217. /* Factory marked bad or worn out? */
  218. if (tmp == 0)
  219. this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
  220. else
  221. this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
  222. mtd->ecc_stats.badblocks++;
  223. }
  224. }
  225. totlen -= len;
  226. from += len;
  227. }
  228. return 0;
  229. }
  230. /**
  231. * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  232. * @mtd: MTD device structure
  233. * @buf: temporary buffer
  234. * @td: descriptor for the bad block table
  235. * @chip: read the table for a specific chip, -1 read all chips; aplies only if
  236. * NAND_BBT_PERCHIP option is set
  237. *
  238. * Read the bad block table for all chips starting at a given page. We assume
  239. * that the bbt bits are in consecutive order.
  240. */
  241. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  242. {
  243. struct nand_chip *this = mtd->priv;
  244. int res = 0, i;
  245. if (td->options & NAND_BBT_PERCHIP) {
  246. int offs = 0;
  247. for (i = 0; i < this->numchips; i++) {
  248. if (chip == -1 || chip == i)
  249. res = read_bbt(mtd, buf, td->pages[i],
  250. this->chipsize >> this->bbt_erase_shift,
  251. td, offs);
  252. if (res)
  253. return res;
  254. offs += this->chipsize >> (this->bbt_erase_shift + 2);
  255. }
  256. } else {
  257. res = read_bbt(mtd, buf, td->pages[0],
  258. mtd->size >> this->bbt_erase_shift, td, 0);
  259. if (res)
  260. return res;
  261. }
  262. return 0;
  263. }
  264. /* BBT marker is in the first page, no OOB */
  265. static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  266. struct nand_bbt_descr *td)
  267. {
  268. size_t retlen;
  269. size_t len;
  270. len = td->len;
  271. if (td->options & NAND_BBT_VERSION)
  272. len++;
  273. return mtd->read(mtd, offs, len, &retlen, buf);
  274. }
  275. /* Scan read raw data from flash */
  276. static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  277. size_t len)
  278. {
  279. struct mtd_oob_ops ops;
  280. int res;
  281. ops.mode = MTD_OOB_RAW;
  282. ops.ooboffs = 0;
  283. ops.ooblen = mtd->oobsize;
  284. while (len > 0) {
  285. if (len <= mtd->writesize) {
  286. ops.oobbuf = buf + len;
  287. ops.datbuf = buf;
  288. ops.len = len;
  289. res = mtd->read_oob(mtd, offs, &ops);
  290. /* Ignore ECC errors when checking for BBM */
  291. if (res != -EUCLEAN && res != -EBADMSG)
  292. return res;
  293. return 0;
  294. } else {
  295. ops.oobbuf = buf + mtd->writesize;
  296. ops.datbuf = buf;
  297. ops.len = mtd->writesize;
  298. res = mtd->read_oob(mtd, offs, &ops);
  299. /* Ignore ECC errors when checking for BBM */
  300. if (res && res != -EUCLEAN && res != -EBADMSG)
  301. return res;
  302. }
  303. buf += mtd->oobsize + mtd->writesize;
  304. len -= mtd->writesize;
  305. }
  306. return 0;
  307. }
  308. static int scan_read_raw(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_raw_data(mtd, buf, offs, td);
  313. else
  314. return scan_read_raw_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_OOB_PLACE;
  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 int 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_raw(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_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
  361. mtd->writesize, td);
  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. return 1;
  367. }
  368. /* Scan a given block full */
  369. static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
  370. loff_t offs, uint8_t *buf, size_t readlen,
  371. int scanlen, int len)
  372. {
  373. int ret, j;
  374. ret = scan_read_raw_oob(mtd, buf, offs, readlen);
  375. if (ret)
  376. return ret;
  377. for (j = 0; j < len; 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 len)
  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_OOB_PLACE;
  394. for (j = 0; j < len; 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 && ret != -EUCLEAN && ret != -EBADMSG)
  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, len, 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. len = 1 << (this->bbt_erase_shift - this->page_shift);
  431. else if (bd->options & NAND_BBT_SCAN2NDPAGE)
  432. len = 2;
  433. else
  434. len = 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 = len * 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 * len);
  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, len);
  471. else
  472. ret = scan_block_fast(mtd, bd, from, buf, len);
  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_raw(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 int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  569. {
  570. /* Search the primary table */
  571. search_bbt(mtd, buf, td);
  572. /* Search the mirror table */
  573. if (md)
  574. search_bbt(mtd, buf, md);
  575. /* Force result check */
  576. return 1;
  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_OOB_PLACE;
  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, chipsel, res;
  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. rd = NULL;
  813. rd2 = NULL;
  814. /* Per chip or per device? */
  815. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  816. /* Mirrored table available? */
  817. if (md) {
  818. if (td->pages[i] == -1 && md->pages[i] == -1) {
  819. writeops = 0x03;
  820. goto create;
  821. }
  822. if (td->pages[i] == -1) {
  823. rd = md;
  824. td->version[i] = md->version[i];
  825. writeops = 1;
  826. goto writecheck;
  827. }
  828. if (md->pages[i] == -1) {
  829. rd = td;
  830. md->version[i] = td->version[i];
  831. writeops = 2;
  832. goto writecheck;
  833. }
  834. if (td->version[i] == md->version[i]) {
  835. rd = td;
  836. if (!(td->options & NAND_BBT_VERSION))
  837. rd2 = md;
  838. goto writecheck;
  839. }
  840. if (((int8_t) (td->version[i] - md->version[i])) > 0) {
  841. rd = td;
  842. md->version[i] = td->version[i];
  843. writeops = 2;
  844. } else {
  845. rd = md;
  846. td->version[i] = md->version[i];
  847. writeops = 1;
  848. }
  849. goto writecheck;
  850. } else {
  851. if (td->pages[i] == -1) {
  852. writeops = 0x01;
  853. goto create;
  854. }
  855. rd = td;
  856. goto writecheck;
  857. }
  858. create:
  859. /* Create the bad block table by scanning the device? */
  860. if (!(td->options & NAND_BBT_CREATE))
  861. continue;
  862. /* Create the table in memory by scanning the chip(s) */
  863. if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
  864. create_bbt(mtd, buf, bd, chipsel);
  865. td->version[i] = 1;
  866. if (md)
  867. md->version[i] = 1;
  868. writecheck:
  869. /* Read back first? */
  870. if (rd)
  871. read_abs_bbt(mtd, buf, rd, chipsel);
  872. /* If they weren't versioned, read both */
  873. if (rd2)
  874. read_abs_bbt(mtd, buf, rd2, chipsel);
  875. /* Write the bad block table to the device? */
  876. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  877. res = write_bbt(mtd, buf, td, md, chipsel);
  878. if (res < 0)
  879. return res;
  880. }
  881. /* Write the mirror bad block table to the device? */
  882. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  883. res = write_bbt(mtd, buf, md, td, chipsel);
  884. if (res < 0)
  885. return res;
  886. }
  887. }
  888. return 0;
  889. }
  890. /**
  891. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  892. * @mtd: MTD device structure
  893. * @td: bad block table descriptor
  894. *
  895. * The bad block table regions are marked as "bad" to prevent accidental
  896. * erasures / writes. The regions are identified by the mark 0x02.
  897. */
  898. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  899. {
  900. struct nand_chip *this = mtd->priv;
  901. int i, j, chips, block, nrblocks, update;
  902. uint8_t oldval, newval;
  903. /* Do we have a bbt per chip? */
  904. if (td->options & NAND_BBT_PERCHIP) {
  905. chips = this->numchips;
  906. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  907. } else {
  908. chips = 1;
  909. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  910. }
  911. for (i = 0; i < chips; i++) {
  912. if ((td->options & NAND_BBT_ABSPAGE) ||
  913. !(td->options & NAND_BBT_WRITE)) {
  914. if (td->pages[i] == -1)
  915. continue;
  916. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  917. block <<= 1;
  918. oldval = this->bbt[(block >> 3)];
  919. newval = oldval | (0x2 << (block & 0x06));
  920. this->bbt[(block >> 3)] = newval;
  921. if ((oldval != newval) && td->reserved_block_code)
  922. nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
  923. continue;
  924. }
  925. update = 0;
  926. if (td->options & NAND_BBT_LASTBLOCK)
  927. block = ((i + 1) * nrblocks) - td->maxblocks;
  928. else
  929. block = i * nrblocks;
  930. block <<= 1;
  931. for (j = 0; j < td->maxblocks; j++) {
  932. oldval = this->bbt[(block >> 3)];
  933. newval = oldval | (0x2 << (block & 0x06));
  934. this->bbt[(block >> 3)] = newval;
  935. if (oldval != newval)
  936. update = 1;
  937. block += 2;
  938. }
  939. /*
  940. * If we want reserved blocks to be recorded to flash, and some
  941. * new ones have been marked, then we need to update the stored
  942. * bbts. This should only happen once.
  943. */
  944. if (update && td->reserved_block_code)
  945. nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
  946. }
  947. }
  948. /**
  949. * verify_bbt_descr - verify the bad block description
  950. * @mtd: MTD device structure
  951. * @bd: the table to verify
  952. *
  953. * This functions performs a few sanity checks on the bad block description
  954. * table.
  955. */
  956. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  957. {
  958. struct nand_chip *this = mtd->priv;
  959. u32 pattern_len;
  960. u32 bits;
  961. u32 table_size;
  962. if (!bd)
  963. return;
  964. pattern_len = bd->len;
  965. bits = bd->options & NAND_BBT_NRBITS_MSK;
  966. BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
  967. !(this->bbt_options & NAND_BBT_USE_FLASH));
  968. BUG_ON(!bits);
  969. if (bd->options & NAND_BBT_VERSION)
  970. pattern_len++;
  971. if (bd->options & NAND_BBT_NO_OOB) {
  972. BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
  973. BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
  974. BUG_ON(bd->offs);
  975. if (bd->options & NAND_BBT_VERSION)
  976. BUG_ON(bd->veroffs != bd->len);
  977. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  978. }
  979. if (bd->options & NAND_BBT_PERCHIP)
  980. table_size = this->chipsize >> this->bbt_erase_shift;
  981. else
  982. table_size = mtd->size >> this->bbt_erase_shift;
  983. table_size >>= 3;
  984. table_size *= bits;
  985. if (bd->options & NAND_BBT_NO_OOB)
  986. table_size += pattern_len;
  987. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  988. }
  989. /**
  990. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  991. * @mtd: MTD device structure
  992. * @bd: descriptor for the good/bad block search pattern
  993. *
  994. * The function checks, if a bad block table(s) is/are already available. If
  995. * not it scans the device for manufacturer marked good / bad blocks and writes
  996. * the bad block table(s) to the selected place.
  997. *
  998. * The bad block table memory is allocated here. It must be freed by calling
  999. * the nand_free_bbt function.
  1000. */
  1001. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  1002. {
  1003. struct nand_chip *this = mtd->priv;
  1004. int len, res = 0;
  1005. uint8_t *buf;
  1006. struct nand_bbt_descr *td = this->bbt_td;
  1007. struct nand_bbt_descr *md = this->bbt_md;
  1008. len = mtd->size >> (this->bbt_erase_shift + 2);
  1009. /*
  1010. * Allocate memory (2bit per block) and clear the memory bad block
  1011. * table.
  1012. */
  1013. this->bbt = kzalloc(len, GFP_KERNEL);
  1014. if (!this->bbt)
  1015. return -ENOMEM;
  1016. /*
  1017. * If no primary table decriptor is given, scan the device to build a
  1018. * memory based bad block table.
  1019. */
  1020. if (!td) {
  1021. if ((res = nand_memory_bbt(mtd, bd))) {
  1022. pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
  1023. kfree(this->bbt);
  1024. this->bbt = NULL;
  1025. }
  1026. return res;
  1027. }
  1028. verify_bbt_descr(mtd, td);
  1029. verify_bbt_descr(mtd, md);
  1030. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1031. len = (1 << this->bbt_erase_shift);
  1032. len += (len >> this->page_shift) * mtd->oobsize;
  1033. buf = vmalloc(len);
  1034. if (!buf) {
  1035. kfree(this->bbt);
  1036. this->bbt = NULL;
  1037. return -ENOMEM;
  1038. }
  1039. /* Is the bbt at a given page? */
  1040. if (td->options & NAND_BBT_ABSPAGE) {
  1041. res = read_abs_bbts(mtd, buf, td, md);
  1042. } else {
  1043. /* Search the bad block table using a pattern in oob */
  1044. res = search_read_bbts(mtd, buf, td, md);
  1045. }
  1046. if (res)
  1047. res = check_create(mtd, buf, bd);
  1048. /* Prevent the bbt regions from erasing / writing */
  1049. mark_bbt_region(mtd, td);
  1050. if (md)
  1051. mark_bbt_region(mtd, md);
  1052. vfree(buf);
  1053. return res;
  1054. }
  1055. /**
  1056. * nand_update_bbt - [NAND Interface] update bad block table(s)
  1057. * @mtd: MTD device structure
  1058. * @offs: the offset of the newly marked block
  1059. *
  1060. * The function updates the bad block table(s).
  1061. */
  1062. int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1063. {
  1064. struct nand_chip *this = mtd->priv;
  1065. int len, res = 0, writeops = 0;
  1066. int chip, chipsel;
  1067. uint8_t *buf;
  1068. struct nand_bbt_descr *td = this->bbt_td;
  1069. struct nand_bbt_descr *md = this->bbt_md;
  1070. if (!this->bbt || !td)
  1071. return -EINVAL;
  1072. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1073. len = (1 << this->bbt_erase_shift);
  1074. len += (len >> this->page_shift) * mtd->oobsize;
  1075. buf = kmalloc(len, GFP_KERNEL);
  1076. if (!buf)
  1077. return -ENOMEM;
  1078. writeops = md != NULL ? 0x03 : 0x01;
  1079. /* Do we have a bbt per chip? */
  1080. if (td->options & NAND_BBT_PERCHIP) {
  1081. chip = (int)(offs >> this->chip_shift);
  1082. chipsel = chip;
  1083. } else {
  1084. chip = 0;
  1085. chipsel = -1;
  1086. }
  1087. td->version[chip]++;
  1088. if (md)
  1089. md->version[chip]++;
  1090. /* Write the bad block table to the device? */
  1091. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  1092. res = write_bbt(mtd, buf, td, md, chipsel);
  1093. if (res < 0)
  1094. goto out;
  1095. }
  1096. /* Write the mirror bad block table to the device? */
  1097. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  1098. res = write_bbt(mtd, buf, md, td, chipsel);
  1099. }
  1100. out:
  1101. kfree(buf);
  1102. return res;
  1103. }
  1104. /*
  1105. * Define some generic bad / good block scan pattern which are used
  1106. * while scanning a device for factory marked good / bad blocks.
  1107. */
  1108. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1109. static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
  1110. static struct nand_bbt_descr agand_flashbased = {
  1111. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  1112. .offs = 0x20,
  1113. .len = 6,
  1114. .pattern = scan_agand_pattern
  1115. };
  1116. /* Generic flash bbt descriptors */
  1117. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1118. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1119. static struct nand_bbt_descr bbt_main_descr = {
  1120. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1121. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1122. .offs = 8,
  1123. .len = 4,
  1124. .veroffs = 12,
  1125. .maxblocks = 4,
  1126. .pattern = bbt_pattern
  1127. };
  1128. static struct nand_bbt_descr bbt_mirror_descr = {
  1129. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1130. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1131. .offs = 8,
  1132. .len = 4,
  1133. .veroffs = 12,
  1134. .maxblocks = 4,
  1135. .pattern = mirror_pattern
  1136. };
  1137. static struct nand_bbt_descr bbt_main_no_bbt_descr = {
  1138. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1139. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1140. | NAND_BBT_NO_OOB,
  1141. .len = 4,
  1142. .veroffs = 4,
  1143. .maxblocks = 4,
  1144. .pattern = bbt_pattern
  1145. };
  1146. static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
  1147. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1148. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1149. | NAND_BBT_NO_OOB,
  1150. .len = 4,
  1151. .veroffs = 4,
  1152. .maxblocks = 4,
  1153. .pattern = mirror_pattern
  1154. };
  1155. /**
  1156. * nand_create_default_bbt_descr - [INTERN] Creates a BBT descriptor structure
  1157. * @this: NAND chip to create descriptor for
  1158. *
  1159. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1160. * based on the properties of "this". The new descriptor is stored in
  1161. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1162. * passed to this function.
  1163. */
  1164. static int nand_create_default_bbt_descr(struct nand_chip *this)
  1165. {
  1166. struct nand_bbt_descr *bd;
  1167. if (this->badblock_pattern) {
  1168. pr_warn("BBT descr already allocated; not replacing\n");
  1169. return -EINVAL;
  1170. }
  1171. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1172. if (!bd)
  1173. return -ENOMEM;
  1174. bd->options = this->bbt_options;
  1175. bd->offs = this->badblockpos;
  1176. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1177. bd->pattern = scan_ff_pattern;
  1178. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1179. this->badblock_pattern = bd;
  1180. return 0;
  1181. }
  1182. /**
  1183. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1184. * @mtd: MTD device structure
  1185. *
  1186. * This function selects the default bad block table support for the device and
  1187. * calls the nand_scan_bbt function.
  1188. */
  1189. int nand_default_bbt(struct mtd_info *mtd)
  1190. {
  1191. struct nand_chip *this = mtd->priv;
  1192. /*
  1193. * Default for AG-AND. We must use a flash based bad block table as the
  1194. * devices have factory marked _good_ blocks. Erasing those blocks
  1195. * leads to loss of the good / bad information, so we _must_ store this
  1196. * information in a good / bad table during startup.
  1197. */
  1198. if (this->options & NAND_IS_AND) {
  1199. /* Use the default pattern descriptors */
  1200. if (!this->bbt_td) {
  1201. this->bbt_td = &bbt_main_descr;
  1202. this->bbt_md = &bbt_mirror_descr;
  1203. }
  1204. this->bbt_options |= NAND_BBT_USE_FLASH;
  1205. return nand_scan_bbt(mtd, &agand_flashbased);
  1206. }
  1207. /* Is a flash based bad block table requested? */
  1208. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1209. /* Use the default pattern descriptors */
  1210. if (!this->bbt_td) {
  1211. if (this->bbt_options & NAND_BBT_NO_OOB) {
  1212. this->bbt_td = &bbt_main_no_bbt_descr;
  1213. this->bbt_md = &bbt_mirror_no_bbt_descr;
  1214. } else {
  1215. this->bbt_td = &bbt_main_descr;
  1216. this->bbt_md = &bbt_mirror_descr;
  1217. }
  1218. }
  1219. } else {
  1220. this->bbt_td = NULL;
  1221. this->bbt_md = NULL;
  1222. }
  1223. if (!this->badblock_pattern)
  1224. nand_create_default_bbt_descr(this);
  1225. return nand_scan_bbt(mtd, this->badblock_pattern);
  1226. }
  1227. /**
  1228. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1229. * @mtd: MTD device structure
  1230. * @offs: offset in the device
  1231. * @allowbbt: allow access to bad block table region
  1232. */
  1233. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1234. {
  1235. struct nand_chip *this = mtd->priv;
  1236. int block;
  1237. uint8_t res;
  1238. /* Get block number * 2 */
  1239. block = (int)(offs >> (this->bbt_erase_shift - 1));
  1240. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1241. pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
  1242. "(block %d) 0x%02x\n",
  1243. (unsigned int)offs, block >> 1, res);
  1244. switch ((int)res) {
  1245. case 0x00:
  1246. return 0;
  1247. case 0x01:
  1248. return 1;
  1249. case 0x02:
  1250. return allowbbt ? 0 : 1;
  1251. }
  1252. return 1;
  1253. }
  1254. EXPORT_SYMBOL(nand_scan_bbt);
  1255. EXPORT_SYMBOL(nand_default_bbt);