nand_bbt.c 38 KB

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