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