nand_bbt.c 41 KB

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