nand_bbt.c 33 KB

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