nand_bbt.c 37 KB

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