nand_bbt.c 33 KB

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