nand_bbt.c 35 KB

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