nand_bbt.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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 <common.h>
  62. #include <malloc.h>
  63. #include <linux/compat.h>
  64. #include <linux/mtd/mtd.h>
  65. #include <linux/mtd/nand.h>
  66. #include <linux/mtd/nand_ecc.h>
  67. #include <linux/bitops.h>
  68. #include <asm/errno.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. MTDDEBUG(MTD_DEBUG_LEVEL0, "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. MTDDEBUG(MTD_DEBUG_LEVEL0,
  512. "Bad eraseblock %d at 0x%012llx\n",
  513. i >> 1, (unsigned long long)from);
  514. mtd->ecc_stats.badblocks++;
  515. }
  516. i += 2;
  517. from += (1 << this->bbt_erase_shift);
  518. }
  519. return 0;
  520. }
  521. /**
  522. * search_bbt - [GENERIC] scan the device for a specific bad block table
  523. * @mtd: MTD device structure
  524. * @buf: temporary buffer
  525. * @td: descriptor for the bad block table
  526. *
  527. * Read the bad block table by searching for a given ident pattern.
  528. * Search is preformed either from the beginning up or from the end of
  529. * the device downwards. The search starts always at the start of a
  530. * block.
  531. * If the option NAND_BBT_PERCHIP is given, each chip is searched
  532. * for a bbt, which contains the bad block information of this chip.
  533. * This is necessary to provide support for certain DOC devices.
  534. *
  535. * The bbt ident pattern resides in the oob area of the first page
  536. * in a block.
  537. */
  538. static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  539. {
  540. struct nand_chip *this = mtd->priv;
  541. int i, chips;
  542. int startblock, block, dir;
  543. int scanlen = mtd->writesize + mtd->oobsize;
  544. int bbtblocks;
  545. int blocktopage = this->bbt_erase_shift - this->page_shift;
  546. /* Search direction top -> down ? */
  547. if (td->options & NAND_BBT_LASTBLOCK) {
  548. startblock = (mtd->size >> this->bbt_erase_shift) - 1;
  549. dir = -1;
  550. } else {
  551. startblock = 0;
  552. dir = 1;
  553. }
  554. /* Do we have a bbt per chip ? */
  555. if (td->options & NAND_BBT_PERCHIP) {
  556. chips = this->numchips;
  557. bbtblocks = this->chipsize >> this->bbt_erase_shift;
  558. startblock &= bbtblocks - 1;
  559. } else {
  560. chips = 1;
  561. bbtblocks = mtd->size >> this->bbt_erase_shift;
  562. }
  563. for (i = 0; i < chips; i++) {
  564. /* Reset version information */
  565. td->version[i] = 0;
  566. td->pages[i] = -1;
  567. /* Scan the maximum number of blocks */
  568. for (block = 0; block < td->maxblocks; block++) {
  569. int actblock = startblock + dir * block;
  570. loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
  571. /* Read first page */
  572. scan_read_raw(mtd, buf, offs, mtd->writesize, td);
  573. if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
  574. td->pages[i] = actblock << blocktopage;
  575. if (td->options & NAND_BBT_VERSION) {
  576. offs = bbt_get_ver_offs(mtd, td);
  577. td->version[i] = buf[offs];
  578. }
  579. break;
  580. }
  581. }
  582. startblock += this->chipsize >> this->bbt_erase_shift;
  583. }
  584. /* Check, if we found a bbt for each requested chip */
  585. for (i = 0; i < chips; i++) {
  586. if (td->pages[i] == -1)
  587. printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
  588. else
  589. printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
  590. td->version[i]);
  591. }
  592. return 0;
  593. }
  594. /**
  595. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  596. * @mtd: MTD device structure
  597. * @buf: temporary buffer
  598. * @td: descriptor for the bad block table
  599. * @md: descriptor for the bad block table mirror
  600. *
  601. * Search and read the bad block table(s)
  602. */
  603. static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  604. {
  605. /* Search the primary table */
  606. search_bbt(mtd, buf, td);
  607. /* Search the mirror table */
  608. if (md)
  609. search_bbt(mtd, buf, md);
  610. /* Force result check */
  611. return 1;
  612. }
  613. /**
  614. * write_bbt - [GENERIC] (Re)write the bad block table
  615. *
  616. * @mtd: MTD device structure
  617. * @buf: temporary buffer
  618. * @td: descriptor for the bad block table
  619. * @md: descriptor for the bad block table mirror
  620. * @chipsel: selector for a specific chip, -1 for all
  621. *
  622. * (Re)write the bad block table
  623. *
  624. */
  625. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  626. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  627. int chipsel)
  628. {
  629. struct nand_chip *this = mtd->priv;
  630. struct erase_info einfo;
  631. int i, j, res, chip = 0;
  632. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  633. int nrchips, bbtoffs, pageoffs, ooboffs;
  634. uint8_t msk[4];
  635. uint8_t rcode = td->reserved_block_code;
  636. size_t retlen, len = 0;
  637. loff_t to;
  638. struct mtd_oob_ops ops;
  639. ops.ooblen = mtd->oobsize;
  640. ops.ooboffs = 0;
  641. ops.datbuf = NULL;
  642. ops.mode = MTD_OOB_PLACE;
  643. if (!rcode)
  644. rcode = 0xff;
  645. /* Write bad block table per chip rather than per device ? */
  646. if (td->options & NAND_BBT_PERCHIP) {
  647. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  648. /* Full device write or specific chip ? */
  649. if (chipsel == -1) {
  650. nrchips = this->numchips;
  651. } else {
  652. nrchips = chipsel + 1;
  653. chip = chipsel;
  654. }
  655. } else {
  656. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  657. nrchips = 1;
  658. }
  659. /* Loop through the chips */
  660. for (; chip < nrchips; chip++) {
  661. /* There was already a version of the table, reuse the page
  662. * This applies for absolute placement too, as we have the
  663. * page nr. in td->pages.
  664. */
  665. if (td->pages[chip] != -1) {
  666. page = td->pages[chip];
  667. goto write;
  668. }
  669. /* Automatic placement of the bad block table */
  670. /* Search direction top -> down ? */
  671. if (td->options & NAND_BBT_LASTBLOCK) {
  672. startblock = numblocks * (chip + 1) - 1;
  673. dir = -1;
  674. } else {
  675. startblock = chip * numblocks;
  676. dir = 1;
  677. }
  678. for (i = 0; i < td->maxblocks; i++) {
  679. int block = startblock + dir * i;
  680. /* Check, if the block is bad */
  681. switch ((this->bbt[block >> 2] >>
  682. (2 * (block & 0x03))) & 0x03) {
  683. case 0x01:
  684. case 0x03:
  685. continue;
  686. }
  687. page = block <<
  688. (this->bbt_erase_shift - this->page_shift);
  689. /* Check, if the block is used by the mirror table */
  690. if (!md || md->pages[chip] != page)
  691. goto write;
  692. }
  693. printk(KERN_ERR "No space left to write bad block table\n");
  694. return -ENOSPC;
  695. write:
  696. /* Set up shift count and masks for the flash table */
  697. bits = td->options & NAND_BBT_NRBITS_MSK;
  698. msk[2] = ~rcode;
  699. switch (bits) {
  700. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  701. msk[3] = 0x01;
  702. break;
  703. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  704. msk[3] = 0x03;
  705. break;
  706. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  707. msk[3] = 0x0f;
  708. break;
  709. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  710. msk[3] = 0xff;
  711. break;
  712. default: return -EINVAL;
  713. }
  714. bbtoffs = chip * (numblocks >> 2);
  715. to = ((loff_t) page) << this->page_shift;
  716. /* Must we save the block contents ? */
  717. if (td->options & NAND_BBT_SAVECONTENT) {
  718. /* Make it block aligned */
  719. to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
  720. len = 1 << this->bbt_erase_shift;
  721. res = mtd->read(mtd, to, len, &retlen, buf);
  722. if (res < 0) {
  723. if (retlen != len) {
  724. printk(KERN_INFO "nand_bbt: Error "
  725. "reading block for writing "
  726. "the bad block table\n");
  727. return res;
  728. }
  729. printk(KERN_WARNING "nand_bbt: ECC error "
  730. "while reading block for writing "
  731. "bad block table\n");
  732. }
  733. /* Read oob data */
  734. ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
  735. ops.oobbuf = &buf[len];
  736. res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
  737. if (res < 0 || ops.oobretlen != ops.ooblen)
  738. goto outerr;
  739. /* Calc the byte offset in the buffer */
  740. pageoffs = page - (int)(to >> this->page_shift);
  741. offs = pageoffs << this->page_shift;
  742. /* Preset the bbt area with 0xff */
  743. memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
  744. ooboffs = len + (pageoffs * mtd->oobsize);
  745. } else if (td->options & NAND_BBT_NO_OOB) {
  746. ooboffs = 0;
  747. offs = td->len;
  748. /* the version byte */
  749. if (td->options & NAND_BBT_VERSION)
  750. offs++;
  751. /* Calc length */
  752. len = (size_t) (numblocks >> sft);
  753. len += offs;
  754. /* Make it page aligned ! */
  755. len = ALIGN(len, mtd->writesize);
  756. /* Preset the buffer with 0xff */
  757. memset(buf, 0xff, len);
  758. /* Pattern is located at the begin of first page */
  759. memcpy(buf, td->pattern, td->len);
  760. } else {
  761. /* Calc length */
  762. len = (size_t) (numblocks >> sft);
  763. /* Make it page aligned ! */
  764. len = ALIGN(len, mtd->writesize);
  765. /* Preset the buffer with 0xff */
  766. memset(buf, 0xff, len +
  767. (len >> this->page_shift)* mtd->oobsize);
  768. offs = 0;
  769. ooboffs = len;
  770. /* Pattern is located in oob area of first page */
  771. memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
  772. }
  773. if (td->options & NAND_BBT_VERSION)
  774. buf[ooboffs + td->veroffs] = td->version[chip];
  775. /* walk through the memory table */
  776. for (i = 0; i < numblocks;) {
  777. uint8_t dat;
  778. dat = this->bbt[bbtoffs + (i >> 2)];
  779. for (j = 0; j < 4; j++, i++) {
  780. int sftcnt = (i << (3 - sft)) & sftmsk;
  781. /* Do not store the reserved bbt blocks ! */
  782. buf[offs + (i >> sft)] &=
  783. ~(msk[dat & 0x03] << sftcnt);
  784. dat >>= 2;
  785. }
  786. }
  787. memset(&einfo, 0, sizeof(einfo));
  788. einfo.mtd = mtd;
  789. einfo.addr = to;
  790. einfo.len = 1 << this->bbt_erase_shift;
  791. res = nand_erase_nand(mtd, &einfo, 1);
  792. if (res < 0)
  793. goto outerr;
  794. res = scan_write_bbt(mtd, to, len, buf,
  795. td->options & NAND_BBT_NO_OOB ? NULL :
  796. &buf[len]);
  797. if (res < 0)
  798. goto outerr;
  799. printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
  800. "0x%02X\n", (unsigned long long)to, td->version[chip]);
  801. /* Mark it as used */
  802. td->pages[chip] = page;
  803. }
  804. return 0;
  805. outerr:
  806. printk(KERN_WARNING
  807. "nand_bbt: Error while writing bad block table %d\n", res);
  808. return res;
  809. }
  810. /**
  811. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  812. * @mtd: MTD device structure
  813. * @bd: descriptor for the good/bad block search pattern
  814. *
  815. * The function creates a memory based bbt by scanning the device
  816. * for manufacturer / software marked good / bad blocks
  817. */
  818. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  819. {
  820. struct nand_chip *this = mtd->priv;
  821. bd->options &= ~NAND_BBT_SCANEMPTY;
  822. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  823. }
  824. /**
  825. * check_create - [GENERIC] create and write bbt(s) if necessary
  826. * @mtd: MTD device structure
  827. * @buf: temporary buffer
  828. * @bd: descriptor for the good/bad block search pattern
  829. *
  830. * The function checks the results of the previous call to read_bbt
  831. * and creates / updates the bbt(s) if necessary
  832. * Creation is necessary if no bbt was found for the chip/device
  833. * Update is necessary if one of the tables is missing or the
  834. * version nr. of one table is less than the other
  835. */
  836. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  837. {
  838. int i, chips, writeops, chipsel, res;
  839. struct nand_chip *this = mtd->priv;
  840. struct nand_bbt_descr *td = this->bbt_td;
  841. struct nand_bbt_descr *md = this->bbt_md;
  842. struct nand_bbt_descr *rd, *rd2;
  843. /* Do we have a bbt per chip ? */
  844. if (td->options & NAND_BBT_PERCHIP)
  845. chips = this->numchips;
  846. else
  847. chips = 1;
  848. for (i = 0; i < chips; i++) {
  849. writeops = 0;
  850. rd = NULL;
  851. rd2 = NULL;
  852. /* Per chip or per device ? */
  853. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  854. /* Mirrored table available ? */
  855. if (md) {
  856. if (td->pages[i] == -1 && md->pages[i] == -1) {
  857. writeops = 0x03;
  858. goto create;
  859. }
  860. if (td->pages[i] == -1) {
  861. rd = md;
  862. td->version[i] = md->version[i];
  863. writeops = 1;
  864. goto writecheck;
  865. }
  866. if (md->pages[i] == -1) {
  867. rd = td;
  868. md->version[i] = td->version[i];
  869. writeops = 2;
  870. goto writecheck;
  871. }
  872. if (td->version[i] == md->version[i]) {
  873. rd = td;
  874. if (!(td->options & NAND_BBT_VERSION))
  875. rd2 = md;
  876. goto writecheck;
  877. }
  878. if (((int8_t) (td->version[i] - md->version[i])) > 0) {
  879. rd = td;
  880. md->version[i] = td->version[i];
  881. writeops = 2;
  882. } else {
  883. rd = md;
  884. td->version[i] = md->version[i];
  885. writeops = 1;
  886. }
  887. goto writecheck;
  888. } else {
  889. if (td->pages[i] == -1) {
  890. writeops = 0x01;
  891. goto create;
  892. }
  893. rd = td;
  894. goto writecheck;
  895. }
  896. create:
  897. /* Create the bad block table by scanning the device ? */
  898. if (!(td->options & NAND_BBT_CREATE))
  899. continue;
  900. /* Create the table in memory by scanning the chip(s) */
  901. if (!(this->options & NAND_CREATE_EMPTY_BBT))
  902. create_bbt(mtd, buf, bd, chipsel);
  903. td->version[i] = 1;
  904. if (md)
  905. md->version[i] = 1;
  906. writecheck:
  907. /* read back first ? */
  908. if (rd)
  909. read_abs_bbt(mtd, buf, rd, chipsel);
  910. /* If they weren't versioned, read both. */
  911. if (rd2)
  912. read_abs_bbt(mtd, buf, rd2, chipsel);
  913. /* Write the bad block table to the device ? */
  914. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  915. res = write_bbt(mtd, buf, td, md, chipsel);
  916. if (res < 0)
  917. return res;
  918. }
  919. /* Write the mirror bad block table to the device ? */
  920. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  921. res = write_bbt(mtd, buf, md, td, chipsel);
  922. if (res < 0)
  923. return res;
  924. }
  925. }
  926. return 0;
  927. }
  928. /**
  929. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  930. * @mtd: MTD device structure
  931. * @td: bad block table descriptor
  932. *
  933. * The bad block table regions are marked as "bad" to prevent
  934. * accidental erasures / writes. The regions are identified by
  935. * the mark 0x02.
  936. */
  937. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  938. {
  939. struct nand_chip *this = mtd->priv;
  940. int i, j, chips, block, nrblocks, update;
  941. uint8_t oldval, newval;
  942. /* Do we have a bbt per chip ? */
  943. if (td->options & NAND_BBT_PERCHIP) {
  944. chips = this->numchips;
  945. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  946. } else {
  947. chips = 1;
  948. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  949. }
  950. for (i = 0; i < chips; i++) {
  951. if ((td->options & NAND_BBT_ABSPAGE) ||
  952. !(td->options & NAND_BBT_WRITE)) {
  953. if (td->pages[i] == -1)
  954. continue;
  955. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  956. block <<= 1;
  957. oldval = this->bbt[(block >> 3)];
  958. newval = oldval | (0x2 << (block & 0x06));
  959. this->bbt[(block >> 3)] = newval;
  960. if ((oldval != newval) && td->reserved_block_code)
  961. nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
  962. continue;
  963. }
  964. update = 0;
  965. if (td->options & NAND_BBT_LASTBLOCK)
  966. block = ((i + 1) * nrblocks) - td->maxblocks;
  967. else
  968. block = i * nrblocks;
  969. block <<= 1;
  970. for (j = 0; j < td->maxblocks; j++) {
  971. oldval = this->bbt[(block >> 3)];
  972. newval = oldval | (0x2 << (block & 0x06));
  973. this->bbt[(block >> 3)] = newval;
  974. if (oldval != newval)
  975. update = 1;
  976. block += 2;
  977. }
  978. /* If we want reserved blocks to be recorded to flash, and some
  979. new ones have been marked, then we need to update the stored
  980. bbts. This should only happen once. */
  981. if (update && td->reserved_block_code)
  982. nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
  983. }
  984. }
  985. /**
  986. * verify_bbt_descr - verify the bad block description
  987. * @mtd: MTD device structure
  988. * @bd: the table to verify
  989. *
  990. * This functions performs a few sanity checks on the bad block description
  991. * table.
  992. */
  993. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  994. {
  995. struct nand_chip *this = mtd->priv;
  996. u32 pattern_len;
  997. u32 bits;
  998. u32 table_size;
  999. if (!bd)
  1000. return;
  1001. pattern_len = bd->len;
  1002. bits = bd->options & NAND_BBT_NRBITS_MSK;
  1003. BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) &&
  1004. !(this->options & NAND_USE_FLASH_BBT));
  1005. BUG_ON(!bits);
  1006. if (bd->options & NAND_BBT_VERSION)
  1007. pattern_len++;
  1008. if (bd->options & NAND_BBT_NO_OOB) {
  1009. BUG_ON(!(this->options & NAND_USE_FLASH_BBT));
  1010. BUG_ON(!(this->options & NAND_USE_FLASH_BBT_NO_OOB));
  1011. BUG_ON(bd->offs);
  1012. if (bd->options & NAND_BBT_VERSION)
  1013. BUG_ON(bd->veroffs != bd->len);
  1014. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  1015. }
  1016. if (bd->options & NAND_BBT_PERCHIP)
  1017. table_size = this->chipsize >> this->bbt_erase_shift;
  1018. else
  1019. table_size = mtd->size >> this->bbt_erase_shift;
  1020. table_size >>= 3;
  1021. table_size *= bits;
  1022. if (bd->options & NAND_BBT_NO_OOB)
  1023. table_size += pattern_len;
  1024. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  1025. }
  1026. /**
  1027. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  1028. * @mtd: MTD device structure
  1029. * @bd: descriptor for the good/bad block search pattern
  1030. *
  1031. * The function checks, if a bad block table(s) is/are already
  1032. * available. If not it scans the device for manufacturer
  1033. * marked good / bad blocks and writes the bad block table(s) to
  1034. * the selected place.
  1035. *
  1036. * The bad block table memory is allocated here. It must be freed
  1037. * by calling the nand_free_bbt function.
  1038. *
  1039. */
  1040. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  1041. {
  1042. struct nand_chip *this = mtd->priv;
  1043. int len, res = 0;
  1044. uint8_t *buf;
  1045. struct nand_bbt_descr *td = this->bbt_td;
  1046. struct nand_bbt_descr *md = this->bbt_md;
  1047. len = mtd->size >> (this->bbt_erase_shift + 2);
  1048. /* Allocate memory (2bit per block) and clear the memory bad block table */
  1049. this->bbt = kzalloc(len, GFP_KERNEL);
  1050. if (!this->bbt) {
  1051. printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
  1052. return -ENOMEM;
  1053. }
  1054. /* If no primary table decriptor is given, scan the device
  1055. * to build a memory based bad block table
  1056. */
  1057. if (!td) {
  1058. if ((res = nand_memory_bbt(mtd, bd))) {
  1059. printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
  1060. kfree(this->bbt);
  1061. this->bbt = NULL;
  1062. }
  1063. return res;
  1064. }
  1065. verify_bbt_descr(mtd, td);
  1066. verify_bbt_descr(mtd, md);
  1067. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1068. len = (1 << this->bbt_erase_shift);
  1069. len += (len >> this->page_shift) * mtd->oobsize;
  1070. buf = vmalloc(len);
  1071. if (!buf) {
  1072. printk(KERN_ERR "nand_bbt: Out of memory\n");
  1073. kfree(this->bbt);
  1074. this->bbt = NULL;
  1075. return -ENOMEM;
  1076. }
  1077. /* Is the bbt at a given page ? */
  1078. if (td->options & NAND_BBT_ABSPAGE) {
  1079. res = read_abs_bbts(mtd, buf, td, md);
  1080. } else {
  1081. /* Search the bad block table using a pattern in oob */
  1082. res = search_read_bbts(mtd, buf, td, md);
  1083. }
  1084. if (res)
  1085. res = check_create(mtd, buf, bd);
  1086. /* Prevent the bbt regions from erasing / writing */
  1087. mark_bbt_region(mtd, td);
  1088. if (md)
  1089. mark_bbt_region(mtd, md);
  1090. vfree(buf);
  1091. return res;
  1092. }
  1093. /**
  1094. * nand_update_bbt - [NAND Interface] update bad block table(s)
  1095. * @mtd: MTD device structure
  1096. * @offs: the offset of the newly marked block
  1097. *
  1098. * The function updates the bad block table(s)
  1099. */
  1100. int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1101. {
  1102. struct nand_chip *this = mtd->priv;
  1103. int len, res = 0, writeops = 0;
  1104. int chip, chipsel;
  1105. uint8_t *buf;
  1106. struct nand_bbt_descr *td = this->bbt_td;
  1107. struct nand_bbt_descr *md = this->bbt_md;
  1108. if (!this->bbt || !td)
  1109. return -EINVAL;
  1110. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1111. len = (1 << this->bbt_erase_shift);
  1112. len += (len >> this->page_shift) * mtd->oobsize;
  1113. buf = kmalloc(len, GFP_KERNEL);
  1114. if (!buf) {
  1115. printk(KERN_ERR "nand_update_bbt: Out of memory\n");
  1116. return -ENOMEM;
  1117. }
  1118. writeops = md != NULL ? 0x03 : 0x01;
  1119. /* Do we have a bbt per chip ? */
  1120. if (td->options & NAND_BBT_PERCHIP) {
  1121. chip = (int)(offs >> this->chip_shift);
  1122. chipsel = chip;
  1123. } else {
  1124. chip = 0;
  1125. chipsel = -1;
  1126. }
  1127. td->version[chip]++;
  1128. if (md)
  1129. md->version[chip]++;
  1130. /* Write the bad block table to the device ? */
  1131. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  1132. res = write_bbt(mtd, buf, td, md, chipsel);
  1133. if (res < 0)
  1134. goto out;
  1135. }
  1136. /* Write the mirror bad block table to the device ? */
  1137. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  1138. res = write_bbt(mtd, buf, md, td, chipsel);
  1139. }
  1140. out:
  1141. kfree(buf);
  1142. return res;
  1143. }
  1144. /* Define some generic bad / good block scan pattern which are used
  1145. * while scanning a device for factory marked good / bad blocks. */
  1146. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1147. static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
  1148. static struct nand_bbt_descr agand_flashbased = {
  1149. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  1150. .offs = 0x20,
  1151. .len = 6,
  1152. .pattern = scan_agand_pattern
  1153. };
  1154. /* Generic flash bbt decriptors
  1155. */
  1156. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1157. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1158. static struct nand_bbt_descr bbt_main_descr = {
  1159. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1160. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1161. .offs = 8,
  1162. .len = 4,
  1163. .veroffs = 12,
  1164. .maxblocks = 4,
  1165. .pattern = bbt_pattern
  1166. };
  1167. static struct nand_bbt_descr bbt_mirror_descr = {
  1168. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1169. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1170. .offs = 8,
  1171. .len = 4,
  1172. .veroffs = 12,
  1173. .maxblocks = 4,
  1174. .pattern = mirror_pattern
  1175. };
  1176. static struct nand_bbt_descr bbt_main_no_bbt_descr = {
  1177. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1178. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1179. | NAND_BBT_NO_OOB,
  1180. .len = 4,
  1181. .veroffs = 4,
  1182. .maxblocks = 4,
  1183. .pattern = bbt_pattern
  1184. };
  1185. static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
  1186. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1187. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1188. | NAND_BBT_NO_OOB,
  1189. .len = 4,
  1190. .veroffs = 4,
  1191. .maxblocks = 4,
  1192. .pattern = mirror_pattern
  1193. };
  1194. #define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
  1195. NAND_BBT_SCANBYTE1AND6)
  1196. /**
  1197. * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
  1198. * @this: NAND chip to create descriptor for
  1199. *
  1200. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1201. * based on the properties of "this". The new descriptor is stored in
  1202. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1203. * passed to this function.
  1204. *
  1205. */
  1206. static int nand_create_default_bbt_descr(struct nand_chip *this)
  1207. {
  1208. struct nand_bbt_descr *bd;
  1209. if (this->badblock_pattern) {
  1210. printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
  1211. return -EINVAL;
  1212. }
  1213. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1214. if (!bd) {
  1215. printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
  1216. return -ENOMEM;
  1217. }
  1218. bd->options = this->options & BBT_SCAN_OPTIONS;
  1219. bd->offs = this->badblockpos;
  1220. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1221. bd->pattern = scan_ff_pattern;
  1222. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1223. this->badblock_pattern = bd;
  1224. return 0;
  1225. }
  1226. /**
  1227. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1228. * @mtd: MTD device structure
  1229. *
  1230. * This function selects the default bad block table
  1231. * support for the device and calls the nand_scan_bbt function
  1232. *
  1233. */
  1234. int nand_default_bbt(struct mtd_info *mtd)
  1235. {
  1236. struct nand_chip *this = mtd->priv;
  1237. /* Default for AG-AND. We must use a flash based
  1238. * bad block table as the devices have factory marked
  1239. * _good_ blocks. Erasing those blocks leads to loss
  1240. * of the good / bad information, so we _must_ store
  1241. * this information in a good / bad table during
  1242. * startup
  1243. */
  1244. if (this->options & NAND_IS_AND) {
  1245. /* Use the default pattern descriptors */
  1246. if (!this->bbt_td) {
  1247. this->bbt_td = &bbt_main_descr;
  1248. this->bbt_md = &bbt_mirror_descr;
  1249. }
  1250. this->options |= NAND_USE_FLASH_BBT;
  1251. return nand_scan_bbt(mtd, &agand_flashbased);
  1252. }
  1253. /* Is a flash based bad block table requested ? */
  1254. if (this->options & NAND_USE_FLASH_BBT) {
  1255. /* Use the default pattern descriptors */
  1256. if (!this->bbt_td) {
  1257. if (this->options & NAND_USE_FLASH_BBT_NO_OOB) {
  1258. this->bbt_td = &bbt_main_no_bbt_descr;
  1259. this->bbt_md = &bbt_mirror_no_bbt_descr;
  1260. } else {
  1261. this->bbt_td = &bbt_main_descr;
  1262. this->bbt_md = &bbt_mirror_descr;
  1263. }
  1264. }
  1265. } else {
  1266. this->bbt_td = NULL;
  1267. this->bbt_md = NULL;
  1268. }
  1269. if (!this->badblock_pattern)
  1270. nand_create_default_bbt_descr(this);
  1271. return nand_scan_bbt(mtd, this->badblock_pattern);
  1272. }
  1273. /**
  1274. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1275. * @mtd: MTD device structure
  1276. * @offs: offset in the device
  1277. * @allowbbt: allow access to bad block table region
  1278. *
  1279. */
  1280. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1281. {
  1282. struct nand_chip *this = mtd->priv;
  1283. int block;
  1284. uint8_t res;
  1285. /* Get block number * 2 */
  1286. block = (int)(offs >> (this->bbt_erase_shift - 1));
  1287. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1288. MTDDEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  1289. (unsigned int)offs, block >> 1, res);
  1290. switch ((int)res) {
  1291. case 0x00:
  1292. return 0;
  1293. case 0x01:
  1294. return 1;
  1295. case 0x02:
  1296. return allowbbt ? 0 : 1;
  1297. }
  1298. return 1;
  1299. }