nand_bbt.c 33 KB

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