nand_bbt.c 38 KB

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