nand_bbt.c 41 KB

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