nand_bbt.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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. static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
  70. {
  71. int ret;
  72. ret = memcmp(buf, td->pattern, td->len);
  73. if (!ret)
  74. return ret;
  75. return -1;
  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 i, 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. for (i = 0; i < end; i++) {
  97. if (p[i] != 0xff)
  98. return -1;
  99. }
  100. }
  101. p += end;
  102. /* Compare the pattern */
  103. for (i = 0; i < td->len; i++) {
  104. if (p[i] != td->pattern[i])
  105. return -1;
  106. }
  107. if (td->options & NAND_BBT_SCANEMPTY) {
  108. p += td->len;
  109. end += td->len;
  110. for (i = end; i < len; i++) {
  111. if (*p++ != 0xff)
  112. return -1;
  113. }
  114. }
  115. return 0;
  116. }
  117. /**
  118. * check_short_pattern - [GENERIC] check if a pattern is in the buffer
  119. * @buf: the buffer to search
  120. * @td: search pattern descriptor
  121. *
  122. * Check for a pattern at the given place. Used to search bad block tables and
  123. * good / bad block identifiers. Same as check_pattern, but no optional empty
  124. * check.
  125. */
  126. static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
  127. {
  128. int i;
  129. uint8_t *p = buf;
  130. /* Compare the pattern */
  131. for (i = 0; i < td->len; i++) {
  132. if (p[td->offs + i] != td->pattern[i])
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. /**
  138. * add_marker_len - compute the length of the marker in data area
  139. * @td: BBT descriptor used for computation
  140. *
  141. * The length will be 0 if the marker is located in OOB area.
  142. */
  143. static u32 add_marker_len(struct nand_bbt_descr *td)
  144. {
  145. u32 len;
  146. if (!(td->options & NAND_BBT_NO_OOB))
  147. return 0;
  148. len = td->len;
  149. if (td->options & NAND_BBT_VERSION)
  150. len++;
  151. return len;
  152. }
  153. /**
  154. * read_bbt - [GENERIC] Read the bad block table starting from page
  155. * @mtd: MTD device structure
  156. * @buf: temporary buffer
  157. * @page: the starting page
  158. * @num: the number of bbt descriptors to read
  159. * @td: the bbt describtion table
  160. * @offs: offset in the memory table
  161. *
  162. * Read the bad block table starting from page.
  163. */
  164. static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
  165. struct nand_bbt_descr *td, int offs)
  166. {
  167. int res, ret = 0, i, j, act = 0;
  168. struct nand_chip *this = mtd->priv;
  169. size_t retlen, len, totlen;
  170. loff_t from;
  171. int bits = td->options & NAND_BBT_NRBITS_MSK;
  172. uint8_t msk = (uint8_t)((1 << bits) - 1);
  173. u32 marker_len;
  174. int reserved_block_code = td->reserved_block_code;
  175. totlen = (num * bits) >> 3;
  176. marker_len = add_marker_len(td);
  177. from = ((loff_t)page) << this->page_shift;
  178. while (totlen) {
  179. len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
  180. if (marker_len) {
  181. /*
  182. * In case the BBT marker is not in the OOB area it
  183. * will be just in the first page.
  184. */
  185. len -= marker_len;
  186. from += marker_len;
  187. marker_len = 0;
  188. }
  189. res = mtd->read(mtd, from, len, &retlen, buf);
  190. if (res < 0) {
  191. if (mtd_is_eccerr(res)) {
  192. pr_info("nand_bbt: ECC error in BBT at "
  193. "0x%012llx\n", from & ~mtd->writesize);
  194. return res;
  195. } else if (mtd_is_bitflip(res)) {
  196. pr_info("nand_bbt: corrected error in BBT at "
  197. "0x%012llx\n", from & ~mtd->writesize);
  198. ret = res;
  199. } else {
  200. pr_info("nand_bbt: error reading BBT\n");
  201. return res;
  202. }
  203. }
  204. /* Analyse data */
  205. for (i = 0; i < len; i++) {
  206. uint8_t dat = buf[i];
  207. for (j = 0; j < 8; j += bits, act += 2) {
  208. uint8_t tmp = (dat >> j) & msk;
  209. if (tmp == msk)
  210. continue;
  211. if (reserved_block_code && (tmp == reserved_block_code)) {
  212. pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
  213. (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  214. this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
  215. mtd->ecc_stats.bbtblocks++;
  216. continue;
  217. }
  218. /*
  219. * Leave it for now, if it's matured we can
  220. * move this message to pr_debug.
  221. */
  222. pr_info("nand_read_bbt: bad block at 0x%012llx\n",
  223. (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  224. /* Factory marked bad or worn out? */
  225. if (tmp == 0)
  226. this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
  227. else
  228. this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
  229. mtd->ecc_stats.badblocks++;
  230. }
  231. }
  232. totlen -= len;
  233. from += len;
  234. }
  235. return ret;
  236. }
  237. /**
  238. * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  239. * @mtd: MTD device structure
  240. * @buf: temporary buffer
  241. * @td: descriptor for the bad block table
  242. * @chip: read the table for a specific chip, -1 read all chips; applies only if
  243. * NAND_BBT_PERCHIP option is set
  244. *
  245. * Read the bad block table for all chips starting at a given page. We assume
  246. * that the bbt bits are in consecutive order.
  247. */
  248. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  249. {
  250. struct nand_chip *this = mtd->priv;
  251. int res = 0, i;
  252. if (td->options & NAND_BBT_PERCHIP) {
  253. int offs = 0;
  254. for (i = 0; i < this->numchips; i++) {
  255. if (chip == -1 || chip == i)
  256. res = read_bbt(mtd, buf, td->pages[i],
  257. this->chipsize >> this->bbt_erase_shift,
  258. td, offs);
  259. if (res)
  260. return res;
  261. offs += this->chipsize >> (this->bbt_erase_shift + 2);
  262. }
  263. } else {
  264. res = read_bbt(mtd, buf, td->pages[0],
  265. mtd->size >> this->bbt_erase_shift, td, 0);
  266. if (res)
  267. return res;
  268. }
  269. return 0;
  270. }
  271. /* BBT marker is in the first page, no OOB */
  272. static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  273. struct nand_bbt_descr *td)
  274. {
  275. size_t retlen;
  276. size_t len;
  277. len = td->len;
  278. if (td->options & NAND_BBT_VERSION)
  279. len++;
  280. return mtd->read(mtd, offs, len, &retlen, buf);
  281. }
  282. /* Scan read raw data from flash */
  283. static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  284. size_t len)
  285. {
  286. struct mtd_oob_ops ops;
  287. int res;
  288. ops.mode = MTD_OPS_RAW;
  289. ops.ooboffs = 0;
  290. ops.ooblen = mtd->oobsize;
  291. while (len > 0) {
  292. ops.datbuf = buf;
  293. ops.len = min(len, (size_t)mtd->writesize);
  294. ops.oobbuf = buf + ops.len;
  295. res = mtd->read_oob(mtd, offs, &ops);
  296. if (res)
  297. return res;
  298. buf += mtd->oobsize + mtd->writesize;
  299. len -= 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, td);
  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. td->version[i] = md->version[i];
  822. writeops = 0x01;
  823. } else if (md->pages[i] == -1) {
  824. rd = td;
  825. md->version[i] = td->version[i];
  826. writeops = 0x02;
  827. } else if (td->version[i] == md->version[i]) {
  828. rd = td;
  829. if (!(td->options & NAND_BBT_VERSION))
  830. rd2 = md;
  831. } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
  832. rd = td;
  833. md->version[i] = td->version[i];
  834. writeops = 0x02;
  835. } else {
  836. rd = md;
  837. td->version[i] = md->version[i];
  838. writeops = 0x01;
  839. }
  840. } else {
  841. if (td->pages[i] == -1) {
  842. create = 1;
  843. writeops = 0x01;
  844. } else {
  845. rd = td;
  846. }
  847. }
  848. if (create) {
  849. /* Create the bad block table by scanning the device? */
  850. if (!(td->options & NAND_BBT_CREATE))
  851. continue;
  852. /* Create the table in memory by scanning the chip(s) */
  853. if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
  854. create_bbt(mtd, buf, bd, chipsel);
  855. td->version[i] = 1;
  856. if (md)
  857. md->version[i] = 1;
  858. }
  859. /* Read back first? */
  860. if (rd) {
  861. res = read_abs_bbt(mtd, buf, rd, chipsel);
  862. if (mtd_is_eccerr(res)) {
  863. /* Mark table as invalid */
  864. rd->pages[i] = -1;
  865. i--;
  866. continue;
  867. }
  868. }
  869. /* If they weren't versioned, read both */
  870. if (rd2) {
  871. res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
  872. if (mtd_is_eccerr(res2)) {
  873. /* Mark table as invalid */
  874. rd2->pages[i] = -1;
  875. i--;
  876. continue;
  877. }
  878. }
  879. /* Scrub the flash table(s)? */
  880. if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
  881. writeops = 0x03;
  882. /* Write the bad block table to the device? */
  883. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  884. res = write_bbt(mtd, buf, td, md, chipsel);
  885. if (res < 0)
  886. return res;
  887. }
  888. /* Write the mirror bad block table to the device? */
  889. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  890. res = write_bbt(mtd, buf, md, td, chipsel);
  891. if (res < 0)
  892. return res;
  893. }
  894. }
  895. return 0;
  896. }
  897. /**
  898. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  899. * @mtd: MTD device structure
  900. * @td: bad block table descriptor
  901. *
  902. * The bad block table regions are marked as "bad" to prevent accidental
  903. * erasures / writes. The regions are identified by the mark 0x02.
  904. */
  905. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  906. {
  907. struct nand_chip *this = mtd->priv;
  908. int i, j, chips, block, nrblocks, update;
  909. uint8_t oldval, newval;
  910. /* Do we have a bbt per chip? */
  911. if (td->options & NAND_BBT_PERCHIP) {
  912. chips = this->numchips;
  913. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  914. } else {
  915. chips = 1;
  916. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  917. }
  918. for (i = 0; i < chips; i++) {
  919. if ((td->options & NAND_BBT_ABSPAGE) ||
  920. !(td->options & NAND_BBT_WRITE)) {
  921. if (td->pages[i] == -1)
  922. continue;
  923. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  924. block <<= 1;
  925. oldval = this->bbt[(block >> 3)];
  926. newval = oldval | (0x2 << (block & 0x06));
  927. this->bbt[(block >> 3)] = newval;
  928. if ((oldval != newval) && td->reserved_block_code)
  929. nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
  930. continue;
  931. }
  932. update = 0;
  933. if (td->options & NAND_BBT_LASTBLOCK)
  934. block = ((i + 1) * nrblocks) - td->maxblocks;
  935. else
  936. block = i * nrblocks;
  937. block <<= 1;
  938. for (j = 0; j < td->maxblocks; j++) {
  939. oldval = this->bbt[(block >> 3)];
  940. newval = oldval | (0x2 << (block & 0x06));
  941. this->bbt[(block >> 3)] = newval;
  942. if (oldval != newval)
  943. update = 1;
  944. block += 2;
  945. }
  946. /*
  947. * If we want reserved blocks to be recorded to flash, and some
  948. * new ones have been marked, then we need to update the stored
  949. * bbts. This should only happen once.
  950. */
  951. if (update && td->reserved_block_code)
  952. nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
  953. }
  954. }
  955. /**
  956. * verify_bbt_descr - verify the bad block description
  957. * @mtd: MTD device structure
  958. * @bd: the table to verify
  959. *
  960. * This functions performs a few sanity checks on the bad block description
  961. * table.
  962. */
  963. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  964. {
  965. struct nand_chip *this = mtd->priv;
  966. u32 pattern_len;
  967. u32 bits;
  968. u32 table_size;
  969. if (!bd)
  970. return;
  971. pattern_len = bd->len;
  972. bits = bd->options & NAND_BBT_NRBITS_MSK;
  973. BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
  974. !(this->bbt_options & NAND_BBT_USE_FLASH));
  975. BUG_ON(!bits);
  976. if (bd->options & NAND_BBT_VERSION)
  977. pattern_len++;
  978. if (bd->options & NAND_BBT_NO_OOB) {
  979. BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
  980. BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
  981. BUG_ON(bd->offs);
  982. if (bd->options & NAND_BBT_VERSION)
  983. BUG_ON(bd->veroffs != bd->len);
  984. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  985. }
  986. if (bd->options & NAND_BBT_PERCHIP)
  987. table_size = this->chipsize >> this->bbt_erase_shift;
  988. else
  989. table_size = mtd->size >> this->bbt_erase_shift;
  990. table_size >>= 3;
  991. table_size *= bits;
  992. if (bd->options & NAND_BBT_NO_OOB)
  993. table_size += pattern_len;
  994. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  995. }
  996. /**
  997. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  998. * @mtd: MTD device structure
  999. * @bd: descriptor for the good/bad block search pattern
  1000. *
  1001. * The function checks, if a bad block table(s) is/are already available. If
  1002. * not it scans the device for manufacturer marked good / bad blocks and writes
  1003. * the bad block table(s) to the selected place.
  1004. *
  1005. * The bad block table memory is allocated here. It must be freed by calling
  1006. * the nand_free_bbt function.
  1007. */
  1008. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  1009. {
  1010. struct nand_chip *this = mtd->priv;
  1011. int len, res = 0;
  1012. uint8_t *buf;
  1013. struct nand_bbt_descr *td = this->bbt_td;
  1014. struct nand_bbt_descr *md = this->bbt_md;
  1015. len = mtd->size >> (this->bbt_erase_shift + 2);
  1016. /*
  1017. * Allocate memory (2bit per block) and clear the memory bad block
  1018. * table.
  1019. */
  1020. this->bbt = kzalloc(len, GFP_KERNEL);
  1021. if (!this->bbt)
  1022. return -ENOMEM;
  1023. /*
  1024. * If no primary table decriptor is given, scan the device to build a
  1025. * memory based bad block table.
  1026. */
  1027. if (!td) {
  1028. if ((res = nand_memory_bbt(mtd, bd))) {
  1029. pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
  1030. kfree(this->bbt);
  1031. this->bbt = NULL;
  1032. }
  1033. return res;
  1034. }
  1035. verify_bbt_descr(mtd, td);
  1036. verify_bbt_descr(mtd, md);
  1037. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1038. len = (1 << this->bbt_erase_shift);
  1039. len += (len >> this->page_shift) * mtd->oobsize;
  1040. buf = vmalloc(len);
  1041. if (!buf) {
  1042. kfree(this->bbt);
  1043. this->bbt = NULL;
  1044. return -ENOMEM;
  1045. }
  1046. /* Is the bbt at a given page? */
  1047. if (td->options & NAND_BBT_ABSPAGE) {
  1048. res = read_abs_bbts(mtd, buf, td, md);
  1049. } else {
  1050. /* Search the bad block table using a pattern in oob */
  1051. res = search_read_bbts(mtd, buf, td, md);
  1052. }
  1053. if (res)
  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 = 4,
  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 = 4,
  1141. .pattern = mirror_pattern
  1142. };
  1143. static struct nand_bbt_descr bbt_main_no_bbt_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 = 4,
  1150. .pattern = bbt_pattern
  1151. };
  1152. static struct nand_bbt_descr bbt_mirror_no_bbt_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 = 4,
  1159. .pattern = mirror_pattern
  1160. };
  1161. /**
  1162. * nand_create_default_bbt_descr - [INTERN] Creates a BBT descriptor structure
  1163. * @this: NAND chip to create descriptor for
  1164. *
  1165. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1166. * based on the properties of "this". The new descriptor is stored in
  1167. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1168. * passed to this function.
  1169. */
  1170. static int nand_create_default_bbt_descr(struct nand_chip *this)
  1171. {
  1172. struct nand_bbt_descr *bd;
  1173. if (this->badblock_pattern) {
  1174. pr_warn("BBT descr already allocated; not replacing\n");
  1175. return -EINVAL;
  1176. }
  1177. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1178. if (!bd)
  1179. return -ENOMEM;
  1180. bd->options = this->bbt_options;
  1181. bd->offs = this->badblockpos;
  1182. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1183. bd->pattern = scan_ff_pattern;
  1184. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1185. this->badblock_pattern = bd;
  1186. return 0;
  1187. }
  1188. /**
  1189. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1190. * @mtd: MTD device structure
  1191. *
  1192. * This function selects the default bad block table support for the device and
  1193. * calls the nand_scan_bbt function.
  1194. */
  1195. int nand_default_bbt(struct mtd_info *mtd)
  1196. {
  1197. struct nand_chip *this = mtd->priv;
  1198. /*
  1199. * Default for AG-AND. We must use a flash based bad block table as the
  1200. * devices have factory marked _good_ blocks. Erasing those blocks
  1201. * leads to loss of the good / bad information, so we _must_ store this
  1202. * information in a good / bad table during startup.
  1203. */
  1204. if (this->options & NAND_IS_AND) {
  1205. /* Use the default pattern descriptors */
  1206. if (!this->bbt_td) {
  1207. this->bbt_td = &bbt_main_descr;
  1208. this->bbt_md = &bbt_mirror_descr;
  1209. }
  1210. this->bbt_options |= NAND_BBT_USE_FLASH;
  1211. return nand_scan_bbt(mtd, &agand_flashbased);
  1212. }
  1213. /* Is a flash based bad block table requested? */
  1214. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1215. /* Use the default pattern descriptors */
  1216. if (!this->bbt_td) {
  1217. if (this->bbt_options & NAND_BBT_NO_OOB) {
  1218. this->bbt_td = &bbt_main_no_bbt_descr;
  1219. this->bbt_md = &bbt_mirror_no_bbt_descr;
  1220. } else {
  1221. this->bbt_td = &bbt_main_descr;
  1222. this->bbt_md = &bbt_mirror_descr;
  1223. }
  1224. }
  1225. } else {
  1226. this->bbt_td = NULL;
  1227. this->bbt_md = NULL;
  1228. }
  1229. if (!this->badblock_pattern)
  1230. nand_create_default_bbt_descr(this);
  1231. return nand_scan_bbt(mtd, this->badblock_pattern);
  1232. }
  1233. /**
  1234. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1235. * @mtd: MTD device structure
  1236. * @offs: offset in the device
  1237. * @allowbbt: allow access to bad block table region
  1238. */
  1239. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1240. {
  1241. struct nand_chip *this = mtd->priv;
  1242. int block;
  1243. uint8_t res;
  1244. /* Get block number * 2 */
  1245. block = (int)(offs >> (this->bbt_erase_shift - 1));
  1246. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1247. pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
  1248. "(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. }
  1260. EXPORT_SYMBOL(nand_scan_bbt);
  1261. EXPORT_SYMBOL(nand_default_bbt);