nand_bbt.c 37 KB

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