nand_bbt.c 33 KB

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