nand_bbt.c 39 KB

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