nand_bbt.c 38 KB

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