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