nand_bbt.c 38 KB

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