nand_bbt.c 37 KB

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