nand_bbt.c 33 KB

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