nand_bbt.c 38 KB

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