nand_bbt.c 37 KB

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