nand_bbt.c 38 KB

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