nand_bbt.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. loff_t to;
  617. int res;
  618. bbt_mark_entry(this, block, BBT_BLOCK_WORN);
  619. to = (loff_t)block << this->bbt_erase_shift;
  620. res = this->block_markbad(this, to);
  621. if (res)
  622. pr_warn("nand_bbt: error %d while marking block %d bad\n",
  623. res, block);
  624. td->pages[chip] = -1;
  625. }
  626. /**
  627. * write_bbt - [GENERIC] (Re)write the bad block table
  628. * @mtd: MTD device structure
  629. * @buf: temporary buffer
  630. * @td: descriptor for the bad block table
  631. * @md: descriptor for the bad block table mirror
  632. * @chipsel: selector for a specific chip, -1 for all
  633. *
  634. * (Re)write the bad block table.
  635. */
  636. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  637. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  638. int chipsel)
  639. {
  640. struct nand_chip *this = mtd_to_nand(mtd);
  641. struct erase_info einfo;
  642. int i, res, chip = 0;
  643. int bits, page, offs, numblocks, sft, sftmsk;
  644. int nrchips, pageoffs, ooboffs;
  645. uint8_t msk[4];
  646. uint8_t rcode = td->reserved_block_code;
  647. size_t retlen, len = 0;
  648. loff_t to;
  649. struct mtd_oob_ops ops;
  650. ops.ooblen = mtd->oobsize;
  651. ops.ooboffs = 0;
  652. ops.datbuf = NULL;
  653. ops.mode = MTD_OPS_PLACE_OOB;
  654. if (!rcode)
  655. rcode = 0xff;
  656. /* Write bad block table per chip rather than per device? */
  657. if (td->options & NAND_BBT_PERCHIP) {
  658. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  659. /* Full device write or specific chip? */
  660. if (chipsel == -1) {
  661. nrchips = this->numchips;
  662. } else {
  663. nrchips = chipsel + 1;
  664. chip = chipsel;
  665. }
  666. } else {
  667. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  668. nrchips = 1;
  669. }
  670. /* Loop through the chips */
  671. while (chip < nrchips) {
  672. int block;
  673. block = get_bbt_block(this, td, md, chip);
  674. if (block < 0) {
  675. pr_err("No space left to write bad block table\n");
  676. res = block;
  677. goto outerr;
  678. }
  679. /*
  680. * get_bbt_block() returns a block number, shift the value to
  681. * get a page number.
  682. */
  683. page = block << (this->bbt_erase_shift - this->page_shift);
  684. /* Set up shift count and masks for the flash table */
  685. bits = td->options & NAND_BBT_NRBITS_MSK;
  686. msk[2] = ~rcode;
  687. switch (bits) {
  688. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  689. msk[3] = 0x01;
  690. break;
  691. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  692. msk[3] = 0x03;
  693. break;
  694. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  695. msk[3] = 0x0f;
  696. break;
  697. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  698. msk[3] = 0xff;
  699. break;
  700. default: return -EINVAL;
  701. }
  702. to = ((loff_t)page) << this->page_shift;
  703. /* Must we save the block contents? */
  704. if (td->options & NAND_BBT_SAVECONTENT) {
  705. /* Make it block aligned */
  706. to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
  707. len = 1 << this->bbt_erase_shift;
  708. res = mtd_read(mtd, to, len, &retlen, buf);
  709. if (res < 0) {
  710. if (retlen != len) {
  711. pr_info("nand_bbt: error reading block for writing the bad block table\n");
  712. return res;
  713. }
  714. pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
  715. }
  716. /* Read oob data */
  717. ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
  718. ops.oobbuf = &buf[len];
  719. res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
  720. if (res < 0 || ops.oobretlen != ops.ooblen)
  721. goto outerr;
  722. /* Calc the byte offset in the buffer */
  723. pageoffs = page - (int)(to >> this->page_shift);
  724. offs = pageoffs << this->page_shift;
  725. /* Preset the bbt area with 0xff */
  726. memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
  727. ooboffs = len + (pageoffs * mtd->oobsize);
  728. } else if (td->options & NAND_BBT_NO_OOB) {
  729. ooboffs = 0;
  730. offs = td->len;
  731. /* The version byte */
  732. if (td->options & NAND_BBT_VERSION)
  733. offs++;
  734. /* Calc length */
  735. len = (size_t)(numblocks >> sft);
  736. len += offs;
  737. /* Make it page aligned! */
  738. len = ALIGN(len, mtd->writesize);
  739. /* Preset the buffer with 0xff */
  740. memset(buf, 0xff, len);
  741. /* Pattern is located at the begin of first page */
  742. memcpy(buf, td->pattern, td->len);
  743. } else {
  744. /* Calc length */
  745. len = (size_t)(numblocks >> sft);
  746. /* Make it page aligned! */
  747. len = ALIGN(len, mtd->writesize);
  748. /* Preset the buffer with 0xff */
  749. memset(buf, 0xff, len +
  750. (len >> this->page_shift)* mtd->oobsize);
  751. offs = 0;
  752. ooboffs = len;
  753. /* Pattern is located in oob area of first page */
  754. memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
  755. }
  756. if (td->options & NAND_BBT_VERSION)
  757. buf[ooboffs + td->veroffs] = td->version[chip];
  758. /* Walk through the memory table */
  759. for (i = 0; i < numblocks; i++) {
  760. uint8_t dat;
  761. int sftcnt = (i << (3 - sft)) & sftmsk;
  762. dat = bbt_get_entry(this, chip * numblocks + i);
  763. /* Do not store the reserved bbt blocks! */
  764. buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
  765. }
  766. memset(&einfo, 0, sizeof(einfo));
  767. einfo.addr = to;
  768. einfo.len = 1 << this->bbt_erase_shift;
  769. res = nand_erase_nand(this, &einfo, 1);
  770. if (res < 0) {
  771. pr_warn("nand_bbt: error while erasing BBT block %d\n",
  772. res);
  773. mark_bbt_block_bad(this, td, chip, block);
  774. continue;
  775. }
  776. res = scan_write_bbt(mtd, to, len, buf,
  777. td->options & NAND_BBT_NO_OOB ? NULL :
  778. &buf[len]);
  779. if (res < 0) {
  780. pr_warn("nand_bbt: error while writing BBT block %d\n",
  781. res);
  782. mark_bbt_block_bad(this, td, chip, block);
  783. continue;
  784. }
  785. pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
  786. (unsigned long long)to, td->version[chip]);
  787. /* Mark it as used */
  788. td->pages[chip++] = page;
  789. }
  790. return 0;
  791. outerr:
  792. pr_warn("nand_bbt: error while writing bad block table %d\n", res);
  793. return res;
  794. }
  795. /**
  796. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  797. * @mtd: MTD device structure
  798. * @bd: descriptor for the good/bad block search pattern
  799. *
  800. * The function creates a memory based bbt by scanning the device for
  801. * manufacturer / software marked good / bad blocks.
  802. */
  803. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  804. {
  805. struct nand_chip *this = mtd_to_nand(mtd);
  806. return create_bbt(mtd, this->data_buf, bd, -1);
  807. }
  808. /**
  809. * check_create - [GENERIC] create and write bbt(s) if necessary
  810. * @mtd: MTD device structure
  811. * @buf: temporary buffer
  812. * @bd: descriptor for the good/bad block search pattern
  813. *
  814. * The function checks the results of the previous call to read_bbt and creates
  815. * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
  816. * for the chip/device. Update is necessary if one of the tables is missing or
  817. * the version nr. of one table is less than the other.
  818. */
  819. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  820. {
  821. int i, chips, writeops, create, chipsel, res, res2;
  822. struct nand_chip *this = mtd_to_nand(mtd);
  823. struct nand_bbt_descr *td = this->bbt_td;
  824. struct nand_bbt_descr *md = this->bbt_md;
  825. struct nand_bbt_descr *rd, *rd2;
  826. /* Do we have a bbt per chip? */
  827. if (td->options & NAND_BBT_PERCHIP)
  828. chips = this->numchips;
  829. else
  830. chips = 1;
  831. for (i = 0; i < chips; i++) {
  832. writeops = 0;
  833. create = 0;
  834. rd = NULL;
  835. rd2 = NULL;
  836. res = res2 = 0;
  837. /* Per chip or per device? */
  838. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  839. /* Mirrored table available? */
  840. if (md) {
  841. if (td->pages[i] == -1 && md->pages[i] == -1) {
  842. create = 1;
  843. writeops = 0x03;
  844. } else if (td->pages[i] == -1) {
  845. rd = md;
  846. writeops = 0x01;
  847. } else if (md->pages[i] == -1) {
  848. rd = td;
  849. writeops = 0x02;
  850. } else if (td->version[i] == md->version[i]) {
  851. rd = td;
  852. if (!(td->options & NAND_BBT_VERSION))
  853. rd2 = md;
  854. } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
  855. rd = td;
  856. writeops = 0x02;
  857. } else {
  858. rd = md;
  859. writeops = 0x01;
  860. }
  861. } else {
  862. if (td->pages[i] == -1) {
  863. create = 1;
  864. writeops = 0x01;
  865. } else {
  866. rd = td;
  867. }
  868. }
  869. if (create) {
  870. /* Create the bad block table by scanning the device? */
  871. if (!(td->options & NAND_BBT_CREATE))
  872. continue;
  873. /* Create the table in memory by scanning the chip(s) */
  874. if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
  875. create_bbt(mtd, buf, bd, chipsel);
  876. td->version[i] = 1;
  877. if (md)
  878. md->version[i] = 1;
  879. }
  880. /* Read back first? */
  881. if (rd) {
  882. res = read_abs_bbt(mtd, buf, rd, chipsel);
  883. if (mtd_is_eccerr(res)) {
  884. /* Mark table as invalid */
  885. rd->pages[i] = -1;
  886. rd->version[i] = 0;
  887. i--;
  888. continue;
  889. }
  890. }
  891. /* If they weren't versioned, read both */
  892. if (rd2) {
  893. res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
  894. if (mtd_is_eccerr(res2)) {
  895. /* Mark table as invalid */
  896. rd2->pages[i] = -1;
  897. rd2->version[i] = 0;
  898. i--;
  899. continue;
  900. }
  901. }
  902. /* Scrub the flash table(s)? */
  903. if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
  904. writeops = 0x03;
  905. /* Update version numbers before writing */
  906. if (md) {
  907. td->version[i] = max(td->version[i], md->version[i]);
  908. md->version[i] = td->version[i];
  909. }
  910. /* Write the bad block table to the device? */
  911. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  912. res = write_bbt(mtd, buf, td, md, chipsel);
  913. if (res < 0)
  914. return res;
  915. }
  916. /* Write the mirror bad block table to the device? */
  917. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  918. res = write_bbt(mtd, buf, md, td, chipsel);
  919. if (res < 0)
  920. return res;
  921. }
  922. }
  923. return 0;
  924. }
  925. /**
  926. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  927. * @mtd: MTD device structure
  928. * @td: bad block table descriptor
  929. *
  930. * The bad block table regions are marked as "bad" to prevent accidental
  931. * erasures / writes. The regions are identified by the mark 0x02.
  932. */
  933. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  934. {
  935. struct nand_chip *this = mtd_to_nand(mtd);
  936. int i, j, chips, block, nrblocks, update;
  937. uint8_t oldval;
  938. /* Do we have a bbt per chip? */
  939. if (td->options & NAND_BBT_PERCHIP) {
  940. chips = this->numchips;
  941. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  942. } else {
  943. chips = 1;
  944. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  945. }
  946. for (i = 0; i < chips; i++) {
  947. if ((td->options & NAND_BBT_ABSPAGE) ||
  948. !(td->options & NAND_BBT_WRITE)) {
  949. if (td->pages[i] == -1)
  950. continue;
  951. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  952. oldval = bbt_get_entry(this, block);
  953. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  954. if ((oldval != BBT_BLOCK_RESERVED) &&
  955. td->reserved_block_code)
  956. nand_update_bbt(mtd, (loff_t)block <<
  957. this->bbt_erase_shift);
  958. continue;
  959. }
  960. update = 0;
  961. if (td->options & NAND_BBT_LASTBLOCK)
  962. block = ((i + 1) * nrblocks) - td->maxblocks;
  963. else
  964. block = i * nrblocks;
  965. for (j = 0; j < td->maxblocks; j++) {
  966. oldval = bbt_get_entry(this, block);
  967. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  968. if (oldval != BBT_BLOCK_RESERVED)
  969. update = 1;
  970. block++;
  971. }
  972. /*
  973. * If we want reserved blocks to be recorded to flash, and some
  974. * new ones have been marked, then we need to update the stored
  975. * bbts. This should only happen once.
  976. */
  977. if (update && td->reserved_block_code)
  978. nand_update_bbt(mtd, (loff_t)(block - 1) <<
  979. this->bbt_erase_shift);
  980. }
  981. }
  982. /**
  983. * verify_bbt_descr - verify the bad block description
  984. * @mtd: MTD device structure
  985. * @bd: the table to verify
  986. *
  987. * This functions performs a few sanity checks on the bad block description
  988. * table.
  989. */
  990. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  991. {
  992. struct nand_chip *this = mtd_to_nand(mtd);
  993. u32 pattern_len;
  994. u32 bits;
  995. u32 table_size;
  996. if (!bd)
  997. return;
  998. pattern_len = bd->len;
  999. bits = bd->options & NAND_BBT_NRBITS_MSK;
  1000. BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
  1001. !(this->bbt_options & NAND_BBT_USE_FLASH));
  1002. BUG_ON(!bits);
  1003. if (bd->options & NAND_BBT_VERSION)
  1004. pattern_len++;
  1005. if (bd->options & NAND_BBT_NO_OOB) {
  1006. BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
  1007. BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
  1008. BUG_ON(bd->offs);
  1009. if (bd->options & NAND_BBT_VERSION)
  1010. BUG_ON(bd->veroffs != bd->len);
  1011. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  1012. }
  1013. if (bd->options & NAND_BBT_PERCHIP)
  1014. table_size = this->chipsize >> this->bbt_erase_shift;
  1015. else
  1016. table_size = mtd->size >> this->bbt_erase_shift;
  1017. table_size >>= 3;
  1018. table_size *= bits;
  1019. if (bd->options & NAND_BBT_NO_OOB)
  1020. table_size += pattern_len;
  1021. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  1022. }
  1023. /**
  1024. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  1025. * @mtd: MTD device structure
  1026. * @bd: descriptor for the good/bad block search pattern
  1027. *
  1028. * The function checks, if a bad block table(s) is/are already available. If
  1029. * not it scans the device for manufacturer marked good / bad blocks and writes
  1030. * the bad block table(s) to the selected place.
  1031. *
  1032. * The bad block table memory is allocated here. It must be freed by calling
  1033. * the nand_free_bbt function.
  1034. */
  1035. static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  1036. {
  1037. struct nand_chip *this = mtd_to_nand(mtd);
  1038. int len, res;
  1039. uint8_t *buf;
  1040. struct nand_bbt_descr *td = this->bbt_td;
  1041. struct nand_bbt_descr *md = this->bbt_md;
  1042. len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
  1043. /*
  1044. * Allocate memory (2bit per block) and clear the memory bad block
  1045. * table.
  1046. */
  1047. this->bbt = kzalloc(len, GFP_KERNEL);
  1048. if (!this->bbt)
  1049. return -ENOMEM;
  1050. /*
  1051. * If no primary table decriptor is given, scan the device to build a
  1052. * memory based bad block table.
  1053. */
  1054. if (!td) {
  1055. if ((res = nand_memory_bbt(mtd, bd))) {
  1056. pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
  1057. goto err;
  1058. }
  1059. return 0;
  1060. }
  1061. verify_bbt_descr(mtd, td);
  1062. verify_bbt_descr(mtd, md);
  1063. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1064. len = (1 << this->bbt_erase_shift);
  1065. len += (len >> this->page_shift) * mtd->oobsize;
  1066. buf = vmalloc(len);
  1067. if (!buf) {
  1068. res = -ENOMEM;
  1069. goto err;
  1070. }
  1071. /* Is the bbt at a given page? */
  1072. if (td->options & NAND_BBT_ABSPAGE) {
  1073. read_abs_bbts(mtd, buf, td, md);
  1074. } else {
  1075. /* Search the bad block table using a pattern in oob */
  1076. search_read_bbts(mtd, buf, td, md);
  1077. }
  1078. res = check_create(mtd, buf, bd);
  1079. if (res)
  1080. goto err;
  1081. /* Prevent the bbt regions from erasing / writing */
  1082. mark_bbt_region(mtd, td);
  1083. if (md)
  1084. mark_bbt_region(mtd, md);
  1085. vfree(buf);
  1086. return 0;
  1087. err:
  1088. kfree(this->bbt);
  1089. this->bbt = NULL;
  1090. return res;
  1091. }
  1092. /**
  1093. * nand_update_bbt - update bad block table(s)
  1094. * @mtd: MTD device structure
  1095. * @offs: the offset of the newly marked block
  1096. *
  1097. * The function updates the bad block table(s).
  1098. */
  1099. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1100. {
  1101. struct nand_chip *this = mtd_to_nand(mtd);
  1102. int len, res = 0;
  1103. int chip, chipsel;
  1104. uint8_t *buf;
  1105. struct nand_bbt_descr *td = this->bbt_td;
  1106. struct nand_bbt_descr *md = this->bbt_md;
  1107. if (!this->bbt || !td)
  1108. return -EINVAL;
  1109. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1110. len = (1 << this->bbt_erase_shift);
  1111. len += (len >> this->page_shift) * mtd->oobsize;
  1112. buf = kmalloc(len, GFP_KERNEL);
  1113. if (!buf)
  1114. return -ENOMEM;
  1115. /* Do we have a bbt per chip? */
  1116. if (td->options & NAND_BBT_PERCHIP) {
  1117. chip = (int)(offs >> this->chip_shift);
  1118. chipsel = chip;
  1119. } else {
  1120. chip = 0;
  1121. chipsel = -1;
  1122. }
  1123. td->version[chip]++;
  1124. if (md)
  1125. md->version[chip]++;
  1126. /* Write the bad block table to the device? */
  1127. if (td->options & NAND_BBT_WRITE) {
  1128. res = write_bbt(mtd, buf, td, md, chipsel);
  1129. if (res < 0)
  1130. goto out;
  1131. }
  1132. /* Write the mirror bad block table to the device? */
  1133. if (md && (md->options & NAND_BBT_WRITE)) {
  1134. res = write_bbt(mtd, buf, md, td, chipsel);
  1135. }
  1136. out:
  1137. kfree(buf);
  1138. return res;
  1139. }
  1140. /*
  1141. * Define some generic bad / good block scan pattern which are used
  1142. * while scanning a device for factory marked good / bad blocks.
  1143. */
  1144. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1145. /* Generic flash bbt descriptors */
  1146. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1147. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1148. static struct nand_bbt_descr bbt_main_descr = {
  1149. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1150. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1151. .offs = 8,
  1152. .len = 4,
  1153. .veroffs = 12,
  1154. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1155. .pattern = bbt_pattern
  1156. };
  1157. static struct nand_bbt_descr bbt_mirror_descr = {
  1158. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1159. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1160. .offs = 8,
  1161. .len = 4,
  1162. .veroffs = 12,
  1163. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1164. .pattern = mirror_pattern
  1165. };
  1166. static struct nand_bbt_descr bbt_main_no_oob_descr = {
  1167. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1168. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1169. | NAND_BBT_NO_OOB,
  1170. .len = 4,
  1171. .veroffs = 4,
  1172. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1173. .pattern = bbt_pattern
  1174. };
  1175. static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
  1176. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1177. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1178. | NAND_BBT_NO_OOB,
  1179. .len = 4,
  1180. .veroffs = 4,
  1181. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1182. .pattern = mirror_pattern
  1183. };
  1184. #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
  1185. /**
  1186. * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
  1187. * @this: NAND chip to create descriptor for
  1188. *
  1189. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1190. * based on the properties of @this. The new descriptor is stored in
  1191. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1192. * passed to this function.
  1193. */
  1194. static int nand_create_badblock_pattern(struct nand_chip *this)
  1195. {
  1196. struct nand_bbt_descr *bd;
  1197. if (this->badblock_pattern) {
  1198. pr_warn("Bad block pattern already allocated; not replacing\n");
  1199. return -EINVAL;
  1200. }
  1201. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1202. if (!bd)
  1203. return -ENOMEM;
  1204. bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
  1205. bd->offs = this->badblockpos;
  1206. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1207. bd->pattern = scan_ff_pattern;
  1208. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1209. this->badblock_pattern = bd;
  1210. return 0;
  1211. }
  1212. /**
  1213. * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
  1214. * @this: NAND chip object
  1215. *
  1216. * This function selects the default bad block table support for the device and
  1217. * calls the nand_scan_bbt function.
  1218. */
  1219. int nand_create_bbt(struct nand_chip *this)
  1220. {
  1221. int ret;
  1222. /* Is a flash based bad block table requested? */
  1223. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1224. /* Use the default pattern descriptors */
  1225. if (!this->bbt_td) {
  1226. if (this->bbt_options & NAND_BBT_NO_OOB) {
  1227. this->bbt_td = &bbt_main_no_oob_descr;
  1228. this->bbt_md = &bbt_mirror_no_oob_descr;
  1229. } else {
  1230. this->bbt_td = &bbt_main_descr;
  1231. this->bbt_md = &bbt_mirror_descr;
  1232. }
  1233. }
  1234. } else {
  1235. this->bbt_td = NULL;
  1236. this->bbt_md = NULL;
  1237. }
  1238. if (!this->badblock_pattern) {
  1239. ret = nand_create_badblock_pattern(this);
  1240. if (ret)
  1241. return ret;
  1242. }
  1243. return nand_scan_bbt(nand_to_mtd(this), this->badblock_pattern);
  1244. }
  1245. EXPORT_SYMBOL(nand_create_bbt);
  1246. /**
  1247. * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
  1248. * @this: NAND chip object
  1249. * @offs: offset in the device
  1250. */
  1251. int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
  1252. {
  1253. int block;
  1254. block = (int)(offs >> this->bbt_erase_shift);
  1255. return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
  1256. }
  1257. /**
  1258. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1259. * @this: NAND chip object
  1260. * @offs: offset in the device
  1261. * @allowbbt: allow access to bad block table region
  1262. */
  1263. int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
  1264. {
  1265. int block, res;
  1266. block = (int)(offs >> this->bbt_erase_shift);
  1267. res = bbt_get_entry(this, block);
  1268. pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  1269. (unsigned int)offs, block, res);
  1270. switch (res) {
  1271. case BBT_BLOCK_GOOD:
  1272. return 0;
  1273. case BBT_BLOCK_WORN:
  1274. return 1;
  1275. case BBT_BLOCK_RESERVED:
  1276. return allowbbt ? 0 : 1;
  1277. }
  1278. return 1;
  1279. }
  1280. /**
  1281. * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
  1282. * @this: NAND chip object
  1283. * @offs: offset of the bad block
  1284. */
  1285. int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
  1286. {
  1287. struct mtd_info *mtd = nand_to_mtd(this);
  1288. int block, ret = 0;
  1289. block = (int)(offs >> this->bbt_erase_shift);
  1290. /* Mark bad block in memory */
  1291. bbt_mark_entry(this, block, BBT_BLOCK_WORN);
  1292. /* Update flash-based bad block table */
  1293. if (this->bbt_options & NAND_BBT_USE_FLASH)
  1294. ret = nand_update_bbt(mtd, offs);
  1295. return ret;
  1296. }