nand_bbt.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. * @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 = bd->len;
  997. u32 bits = bd->options & NAND_BBT_NRBITS_MSK;
  998. u32 table_size;
  999. if (!bd)
  1000. return;
  1001. BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) &&
  1002. !(this->options & NAND_USE_FLASH_BBT));
  1003. BUG_ON(!bits);
  1004. if (bd->options & NAND_BBT_VERSION)
  1005. pattern_len++;
  1006. if (bd->options & NAND_BBT_NO_OOB) {
  1007. BUG_ON(!(this->options & NAND_USE_FLASH_BBT));
  1008. BUG_ON(!(this->options & NAND_USE_FLASH_BBT_NO_OOB));
  1009. BUG_ON(bd->offs);
  1010. if (bd->options & NAND_BBT_VERSION)
  1011. BUG_ON(bd->veroffs != bd->len);
  1012. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  1013. }
  1014. if (bd->options & NAND_BBT_PERCHIP)
  1015. table_size = this->chipsize >> this->bbt_erase_shift;
  1016. else
  1017. table_size = mtd->size >> this->bbt_erase_shift;
  1018. table_size >>= 3;
  1019. table_size *= bits;
  1020. if (bd->options & NAND_BBT_NO_OOB)
  1021. table_size += pattern_len;
  1022. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  1023. }
  1024. /**
  1025. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  1026. * @mtd: MTD device structure
  1027. * @bd: descriptor for the good/bad block search pattern
  1028. *
  1029. * The function checks, if a bad block table(s) is/are already
  1030. * available. If not it scans the device for manufacturer
  1031. * marked good / bad blocks and writes the bad block table(s) to
  1032. * the selected place.
  1033. *
  1034. * The bad block table memory is allocated here. It must be freed
  1035. * by calling the nand_free_bbt function.
  1036. *
  1037. */
  1038. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  1039. {
  1040. struct nand_chip *this = mtd->priv;
  1041. int len, res = 0;
  1042. uint8_t *buf;
  1043. struct nand_bbt_descr *td = this->bbt_td;
  1044. struct nand_bbt_descr *md = this->bbt_md;
  1045. len = mtd->size >> (this->bbt_erase_shift + 2);
  1046. /* Allocate memory (2bit per block) and clear the memory bad block table */
  1047. this->bbt = kzalloc(len, GFP_KERNEL);
  1048. if (!this->bbt) {
  1049. printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
  1050. return -ENOMEM;
  1051. }
  1052. /* If no primary table decriptor is given, scan the device
  1053. * to build a memory based bad block table
  1054. */
  1055. if (!td) {
  1056. if ((res = nand_memory_bbt(mtd, bd))) {
  1057. printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
  1058. kfree(this->bbt);
  1059. this->bbt = NULL;
  1060. }
  1061. return res;
  1062. }
  1063. verify_bbt_descr(mtd, td);
  1064. verify_bbt_descr(mtd, md);
  1065. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1066. len = (1 << this->bbt_erase_shift);
  1067. len += (len >> this->page_shift) * mtd->oobsize;
  1068. buf = vmalloc(len);
  1069. if (!buf) {
  1070. printk(KERN_ERR "nand_bbt: Out of memory\n");
  1071. kfree(this->bbt);
  1072. this->bbt = NULL;
  1073. return -ENOMEM;
  1074. }
  1075. /* Is the bbt at a given page ? */
  1076. if (td->options & NAND_BBT_ABSPAGE) {
  1077. res = read_abs_bbts(mtd, buf, td, md);
  1078. } else {
  1079. /* Search the bad block table using a pattern in oob */
  1080. res = search_read_bbts(mtd, buf, td, md);
  1081. }
  1082. if (res)
  1083. res = check_create(mtd, buf, bd);
  1084. /* Prevent the bbt regions from erasing / writing */
  1085. mark_bbt_region(mtd, td);
  1086. if (md)
  1087. mark_bbt_region(mtd, md);
  1088. vfree(buf);
  1089. return res;
  1090. }
  1091. /**
  1092. * nand_update_bbt - [NAND Interface] update bad block table(s)
  1093. * @mtd: MTD device structure
  1094. * @offs: the offset of the newly marked block
  1095. *
  1096. * The function updates the bad block table(s)
  1097. */
  1098. int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1099. {
  1100. struct nand_chip *this = mtd->priv;
  1101. int len, res = 0, writeops = 0;
  1102. int chip, chipsel;
  1103. uint8_t *buf;
  1104. struct nand_bbt_descr *td = this->bbt_td;
  1105. struct nand_bbt_descr *md = this->bbt_md;
  1106. if (!this->bbt || !td)
  1107. return -EINVAL;
  1108. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1109. len = (1 << this->bbt_erase_shift);
  1110. len += (len >> this->page_shift) * mtd->oobsize;
  1111. buf = kmalloc(len, GFP_KERNEL);
  1112. if (!buf) {
  1113. printk(KERN_ERR "nand_update_bbt: Out of memory\n");
  1114. return -ENOMEM;
  1115. }
  1116. writeops = md != NULL ? 0x03 : 0x01;
  1117. /* Do we have a bbt per chip ? */
  1118. if (td->options & NAND_BBT_PERCHIP) {
  1119. chip = (int)(offs >> this->chip_shift);
  1120. chipsel = chip;
  1121. } else {
  1122. chip = 0;
  1123. chipsel = -1;
  1124. }
  1125. td->version[chip]++;
  1126. if (md)
  1127. md->version[chip]++;
  1128. /* Write the bad block table to the device ? */
  1129. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  1130. res = write_bbt(mtd, buf, td, md, chipsel);
  1131. if (res < 0)
  1132. goto out;
  1133. }
  1134. /* Write the mirror bad block table to the device ? */
  1135. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  1136. res = write_bbt(mtd, buf, md, td, chipsel);
  1137. }
  1138. out:
  1139. kfree(buf);
  1140. return res;
  1141. }
  1142. /* Define some generic bad / good block scan pattern which are used
  1143. * while scanning a device for factory marked good / bad blocks. */
  1144. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1145. static struct nand_bbt_descr smallpage_flashbased = {
  1146. .options = NAND_BBT_SCAN2NDPAGE,
  1147. .offs = NAND_SMALL_BADBLOCK_POS,
  1148. .len = 1,
  1149. .pattern = scan_ff_pattern
  1150. };
  1151. static struct nand_bbt_descr largepage_flashbased = {
  1152. .options = NAND_BBT_SCAN2NDPAGE,
  1153. .offs = NAND_LARGE_BADBLOCK_POS,
  1154. .len = 2,
  1155. .pattern = scan_ff_pattern
  1156. };
  1157. static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
  1158. static struct nand_bbt_descr agand_flashbased = {
  1159. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  1160. .offs = 0x20,
  1161. .len = 6,
  1162. .pattern = scan_agand_pattern
  1163. };
  1164. /* Generic flash bbt decriptors
  1165. */
  1166. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1167. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1168. static struct nand_bbt_descr bbt_main_descr = {
  1169. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1170. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1171. .offs = 8,
  1172. .len = 4,
  1173. .veroffs = 12,
  1174. .maxblocks = 4,
  1175. .pattern = bbt_pattern
  1176. };
  1177. static struct nand_bbt_descr bbt_mirror_descr = {
  1178. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1179. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1180. .offs = 8,
  1181. .len = 4,
  1182. .veroffs = 12,
  1183. .maxblocks = 4,
  1184. .pattern = mirror_pattern
  1185. };
  1186. static struct nand_bbt_descr bbt_main_no_bbt_descr = {
  1187. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1188. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1189. | NAND_BBT_NO_OOB,
  1190. .len = 4,
  1191. .veroffs = 4,
  1192. .maxblocks = 4,
  1193. .pattern = bbt_pattern
  1194. };
  1195. static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
  1196. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1197. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1198. | NAND_BBT_NO_OOB,
  1199. .len = 4,
  1200. .veroffs = 4,
  1201. .maxblocks = 4,
  1202. .pattern = mirror_pattern
  1203. };
  1204. #define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
  1205. NAND_BBT_SCANBYTE1AND6)
  1206. /**
  1207. * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
  1208. * @this: NAND chip to create descriptor for
  1209. *
  1210. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1211. * based on the properties of "this". The new descriptor is stored in
  1212. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1213. * passed to this function.
  1214. *
  1215. * TODO: Handle other flags, replace other static structs
  1216. * (e.g. handle NAND_BBT_FLASH for flash-based BBT,
  1217. * replace smallpage_flashbased)
  1218. *
  1219. */
  1220. static int nand_create_default_bbt_descr(struct nand_chip *this)
  1221. {
  1222. struct nand_bbt_descr *bd;
  1223. if (this->badblock_pattern) {
  1224. printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
  1225. return -EINVAL;
  1226. }
  1227. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1228. if (!bd) {
  1229. printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
  1230. return -ENOMEM;
  1231. }
  1232. bd->options = this->options & BBT_SCAN_OPTIONS;
  1233. bd->offs = this->badblockpos;
  1234. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1235. bd->pattern = scan_ff_pattern;
  1236. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1237. this->badblock_pattern = bd;
  1238. return 0;
  1239. }
  1240. /**
  1241. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1242. * @mtd: MTD device structure
  1243. *
  1244. * This function selects the default bad block table
  1245. * support for the device and calls the nand_scan_bbt function
  1246. *
  1247. */
  1248. int nand_default_bbt(struct mtd_info *mtd)
  1249. {
  1250. struct nand_chip *this = mtd->priv;
  1251. /* Default for AG-AND. We must use a flash based
  1252. * bad block table as the devices have factory marked
  1253. * _good_ blocks. Erasing those blocks leads to loss
  1254. * of the good / bad information, so we _must_ store
  1255. * this information in a good / bad table during
  1256. * startup
  1257. */
  1258. if (this->options & NAND_IS_AND) {
  1259. /* Use the default pattern descriptors */
  1260. if (!this->bbt_td) {
  1261. this->bbt_td = &bbt_main_descr;
  1262. this->bbt_md = &bbt_mirror_descr;
  1263. }
  1264. this->options |= NAND_USE_FLASH_BBT;
  1265. return nand_scan_bbt(mtd, &agand_flashbased);
  1266. }
  1267. /* Is a flash based bad block table requested ? */
  1268. if (this->options & NAND_USE_FLASH_BBT) {
  1269. /* Use the default pattern descriptors */
  1270. if (!this->bbt_td) {
  1271. if (this->options & NAND_USE_FLASH_BBT_NO_OOB) {
  1272. this->bbt_td = &bbt_main_no_bbt_descr;
  1273. this->bbt_md = &bbt_mirror_no_bbt_descr;
  1274. } else {
  1275. this->bbt_td = &bbt_main_descr;
  1276. this->bbt_md = &bbt_mirror_descr;
  1277. }
  1278. }
  1279. if (!this->badblock_pattern) {
  1280. this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
  1281. }
  1282. } else {
  1283. this->bbt_td = NULL;
  1284. this->bbt_md = NULL;
  1285. if (!this->badblock_pattern)
  1286. nand_create_default_bbt_descr(this);
  1287. }
  1288. return nand_scan_bbt(mtd, this->badblock_pattern);
  1289. }
  1290. /**
  1291. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1292. * @mtd: MTD device structure
  1293. * @offs: offset in the device
  1294. * @allowbbt: allow access to bad block table region
  1295. *
  1296. */
  1297. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1298. {
  1299. struct nand_chip *this = mtd->priv;
  1300. int block;
  1301. uint8_t res;
  1302. /* Get block number * 2 */
  1303. block = (int)(offs >> (this->bbt_erase_shift - 1));
  1304. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1305. DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  1306. (unsigned int)offs, block >> 1, res);
  1307. switch ((int)res) {
  1308. case 0x00:
  1309. return 0;
  1310. case 0x01:
  1311. return 1;
  1312. case 0x02:
  1313. return allowbbt ? 0 : 1;
  1314. }
  1315. return 1;
  1316. }
  1317. EXPORT_SYMBOL(nand_scan_bbt);
  1318. EXPORT_SYMBOL(nand_default_bbt);