nand_bbt.c 37 KB

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