nand_bbt.c 33 KB

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