nand_bbt.c 38 KB

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