nand_bbt.c 32 KB

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