nand_bbt.c 39 KB

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