nand_bbt.c 38 KB

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