nand_bbt.c 40 KB

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