nand_bbt.c 37 KB

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