nand_bbt.c 38 KB

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