nand_bbt.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. /*
  2. * drivers/mtd/nand_bbt.c
  3. *
  4. * Overview:
  5. * Bad block table support for the NAND driver
  6. *
  7. * Copyright (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, 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 (retlen != len) {
  192. printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
  193. return res;
  194. }
  195. printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
  196. }
  197. /* Analyse data */
  198. for (i = 0; i < len; i++) {
  199. uint8_t dat = buf[i];
  200. for (j = 0; j < 8; j += bits, act += 2) {
  201. uint8_t tmp = (dat >> j) & msk;
  202. if (tmp == msk)
  203. continue;
  204. if (reserved_block_code && (tmp == reserved_block_code)) {
  205. printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
  206. (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  207. this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
  208. mtd->ecc_stats.bbtblocks++;
  209. continue;
  210. }
  211. /*
  212. * Leave it for now, if it's matured we can
  213. * move this message to MTD_DEBUG_LEVEL0.
  214. */
  215. printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
  216. (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  217. /* Factory marked bad or worn out? */
  218. if (tmp == 0)
  219. this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
  220. else
  221. this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
  222. mtd->ecc_stats.badblocks++;
  223. }
  224. }
  225. totlen -= len;
  226. from += len;
  227. }
  228. return 0;
  229. }
  230. /**
  231. * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  232. * @mtd: MTD device structure
  233. * @buf: temporary buffer
  234. * @td: descriptor for the bad block table
  235. * @chip: read the table for a specific chip, -1 read all chips; aplies only if
  236. * NAND_BBT_PERCHIP option is set
  237. *
  238. * Read the bad block table for all chips starting at a given page. We assume
  239. * that the bbt bits are in consecutive order.
  240. */
  241. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  242. {
  243. struct nand_chip *this = mtd->priv;
  244. int res = 0, i;
  245. if (td->options & NAND_BBT_PERCHIP) {
  246. int offs = 0;
  247. for (i = 0; i < this->numchips; i++) {
  248. if (chip == -1 || chip == i)
  249. res = read_bbt(mtd, buf, td->pages[i],
  250. this->chipsize >> this->bbt_erase_shift,
  251. td, offs);
  252. if (res)
  253. return res;
  254. offs += this->chipsize >> (this->bbt_erase_shift + 2);
  255. }
  256. } else {
  257. res = read_bbt(mtd, buf, td->pages[0],
  258. mtd->size >> this->bbt_erase_shift, td, 0);
  259. if (res)
  260. return res;
  261. }
  262. return 0;
  263. }
  264. /* BBT marker is in the first page, no OOB */
  265. static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  266. struct nand_bbt_descr *td)
  267. {
  268. size_t retlen;
  269. size_t len;
  270. len = td->len;
  271. if (td->options & NAND_BBT_VERSION)
  272. len++;
  273. return mtd->read(mtd, offs, len, &retlen, buf);
  274. }
  275. /* Scan read raw data from flash */
  276. static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  277. size_t len)
  278. {
  279. struct mtd_oob_ops ops;
  280. int res;
  281. ops.mode = MTD_OOB_RAW;
  282. ops.ooboffs = 0;
  283. ops.ooblen = mtd->oobsize;
  284. while (len > 0) {
  285. if (len <= mtd->writesize) {
  286. ops.oobbuf = buf + len;
  287. ops.datbuf = buf;
  288. ops.len = len;
  289. return mtd->read_oob(mtd, offs, &ops);
  290. } else {
  291. ops.oobbuf = buf + mtd->writesize;
  292. ops.datbuf = buf;
  293. ops.len = mtd->writesize;
  294. res = mtd->read_oob(mtd, offs, &ops);
  295. if (res)
  296. return res;
  297. }
  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_OOB_PLACE;
  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. printk(KERN_DEBUG "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. printk(KERN_DEBUG "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. if (ret)
  371. return ret;
  372. for (j = 0; j < len; j++, buf += scanlen) {
  373. if (check_pattern(buf, scanlen, mtd->writesize, bd))
  374. return 1;
  375. }
  376. return 0;
  377. }
  378. /* Scan a given block partially */
  379. static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
  380. loff_t offs, uint8_t *buf, int len)
  381. {
  382. struct mtd_oob_ops ops;
  383. int j, ret;
  384. ops.ooblen = mtd->oobsize;
  385. ops.oobbuf = buf;
  386. ops.ooboffs = 0;
  387. ops.datbuf = NULL;
  388. ops.mode = MTD_OOB_PLACE;
  389. for (j = 0; j < len; j++) {
  390. /*
  391. * Read the full oob until read_oob is fixed to handle single
  392. * byte reads for 16 bit buswidth.
  393. */
  394. ret = mtd->read_oob(mtd, offs, &ops);
  395. if (ret)
  396. return ret;
  397. if (check_short_pattern(buf, bd))
  398. return 1;
  399. offs += mtd->writesize;
  400. }
  401. return 0;
  402. }
  403. /**
  404. * create_bbt - [GENERIC] Create a bad block table by scanning the device
  405. * @mtd: MTD device structure
  406. * @buf: temporary buffer
  407. * @bd: descriptor for the good/bad block search pattern
  408. * @chip: create the table for a specific chip, -1 read all chips; applies only
  409. * if NAND_BBT_PERCHIP option is set
  410. *
  411. * Create a bad block table by scanning the device for the given good/bad block
  412. * identify pattern.
  413. */
  414. static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
  415. struct nand_bbt_descr *bd, int chip)
  416. {
  417. struct nand_chip *this = mtd->priv;
  418. int i, numblocks, len, scanlen;
  419. int startblock;
  420. loff_t from;
  421. size_t readlen;
  422. printk(KERN_INFO "Scanning device for bad blocks\n");
  423. if (bd->options & NAND_BBT_SCANALLPAGES)
  424. len = 1 << (this->bbt_erase_shift - this->page_shift);
  425. else if (bd->options & NAND_BBT_SCAN2NDPAGE)
  426. len = 2;
  427. else
  428. len = 1;
  429. if (!(bd->options & NAND_BBT_SCANEMPTY)) {
  430. /* We need only read few bytes from the OOB area */
  431. scanlen = 0;
  432. readlen = bd->len;
  433. } else {
  434. /* Full page content should be read */
  435. scanlen = mtd->writesize + mtd->oobsize;
  436. readlen = len * mtd->writesize;
  437. }
  438. if (chip == -1) {
  439. /*
  440. * Note that numblocks is 2 * (real numblocks) here, see i+=2
  441. * below as it makes shifting and masking less painful
  442. */
  443. numblocks = mtd->size >> (this->bbt_erase_shift - 1);
  444. startblock = 0;
  445. from = 0;
  446. } else {
  447. if (chip >= this->numchips) {
  448. printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
  449. chip + 1, this->numchips);
  450. return -EINVAL;
  451. }
  452. numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
  453. startblock = chip * numblocks;
  454. numblocks += startblock;
  455. from = (loff_t)startblock << (this->bbt_erase_shift - 1);
  456. }
  457. if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
  458. from += mtd->erasesize - (mtd->writesize * len);
  459. for (i = startblock; i < numblocks;) {
  460. int ret;
  461. BUG_ON(bd->options & NAND_BBT_NO_OOB);
  462. if (bd->options & NAND_BBT_SCANALLPAGES)
  463. ret = scan_block_full(mtd, bd, from, buf, readlen,
  464. scanlen, len);
  465. else
  466. ret = scan_block_fast(mtd, bd, from, buf, len);
  467. if (ret < 0)
  468. return ret;
  469. if (ret) {
  470. this->bbt[i >> 3] |= 0x03 << (i & 0x6);
  471. printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
  472. i >> 1, (unsigned long long)from);
  473. mtd->ecc_stats.badblocks++;
  474. }
  475. i += 2;
  476. from += (1 << this->bbt_erase_shift);
  477. }
  478. return 0;
  479. }
  480. /**
  481. * search_bbt - [GENERIC] scan the device for a specific bad block table
  482. * @mtd: MTD device structure
  483. * @buf: temporary buffer
  484. * @td: descriptor for the bad block table
  485. *
  486. * Read the bad block table by searching for a given ident pattern. Search is
  487. * preformed either from the beginning up or from the end of the device
  488. * downwards. The search starts always at the start of a block. If the option
  489. * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
  490. * the bad block information of this chip. This is necessary to provide support
  491. * for certain DOC devices.
  492. *
  493. * The bbt ident pattern resides in the oob area of the first page in a block.
  494. */
  495. static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  496. {
  497. struct nand_chip *this = mtd->priv;
  498. int i, chips;
  499. int bits, startblock, block, dir;
  500. int scanlen = mtd->writesize + mtd->oobsize;
  501. int bbtblocks;
  502. int blocktopage = this->bbt_erase_shift - this->page_shift;
  503. /* Search direction top -> down? */
  504. if (td->options & NAND_BBT_LASTBLOCK) {
  505. startblock = (mtd->size >> this->bbt_erase_shift) - 1;
  506. dir = -1;
  507. } else {
  508. startblock = 0;
  509. dir = 1;
  510. }
  511. /* Do we have a bbt per chip? */
  512. if (td->options & NAND_BBT_PERCHIP) {
  513. chips = this->numchips;
  514. bbtblocks = this->chipsize >> this->bbt_erase_shift;
  515. startblock &= bbtblocks - 1;
  516. } else {
  517. chips = 1;
  518. bbtblocks = mtd->size >> this->bbt_erase_shift;
  519. }
  520. /* Number of bits for each erase block in the bbt */
  521. bits = td->options & NAND_BBT_NRBITS_MSK;
  522. for (i = 0; i < chips; i++) {
  523. /* Reset version information */
  524. td->version[i] = 0;
  525. td->pages[i] = -1;
  526. /* Scan the maximum number of blocks */
  527. for (block = 0; block < td->maxblocks; block++) {
  528. int actblock = startblock + dir * block;
  529. loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
  530. /* Read first page */
  531. scan_read_raw(mtd, buf, offs, mtd->writesize, td);
  532. if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
  533. td->pages[i] = actblock << blocktopage;
  534. if (td->options & NAND_BBT_VERSION) {
  535. offs = bbt_get_ver_offs(mtd, td);
  536. td->version[i] = buf[offs];
  537. }
  538. break;
  539. }
  540. }
  541. startblock += this->chipsize >> this->bbt_erase_shift;
  542. }
  543. /* Check, if we found a bbt for each requested chip */
  544. for (i = 0; i < chips; i++) {
  545. if (td->pages[i] == -1)
  546. printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
  547. else
  548. printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
  549. td->version[i]);
  550. }
  551. return 0;
  552. }
  553. /**
  554. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  555. * @mtd: MTD device structure
  556. * @buf: temporary buffer
  557. * @td: descriptor for the bad block table
  558. * @md: descriptor for the bad block table mirror
  559. *
  560. * Search and read the bad block table(s).
  561. */
  562. static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  563. {
  564. /* Search the primary table */
  565. search_bbt(mtd, buf, td);
  566. /* Search the mirror table */
  567. if (md)
  568. search_bbt(mtd, buf, md);
  569. /* Force result check */
  570. return 1;
  571. }
  572. /**
  573. * write_bbt - [GENERIC] (Re)write the bad block table
  574. * @mtd: MTD device structure
  575. * @buf: temporary buffer
  576. * @td: descriptor for the bad block table
  577. * @md: descriptor for the bad block table mirror
  578. * @chipsel: selector for a specific chip, -1 for all
  579. *
  580. * (Re)write the bad block table.
  581. */
  582. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  583. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  584. int chipsel)
  585. {
  586. struct nand_chip *this = mtd->priv;
  587. struct erase_info einfo;
  588. int i, j, res, chip = 0;
  589. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  590. int nrchips, bbtoffs, pageoffs, ooboffs;
  591. uint8_t msk[4];
  592. uint8_t rcode = td->reserved_block_code;
  593. size_t retlen, len = 0;
  594. loff_t to;
  595. struct mtd_oob_ops ops;
  596. ops.ooblen = mtd->oobsize;
  597. ops.ooboffs = 0;
  598. ops.datbuf = NULL;
  599. ops.mode = MTD_OOB_PLACE;
  600. if (!rcode)
  601. rcode = 0xff;
  602. /* Write bad block table per chip rather than per device? */
  603. if (td->options & NAND_BBT_PERCHIP) {
  604. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  605. /* Full device write or specific chip? */
  606. if (chipsel == -1) {
  607. nrchips = this->numchips;
  608. } else {
  609. nrchips = chipsel + 1;
  610. chip = chipsel;
  611. }
  612. } else {
  613. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  614. nrchips = 1;
  615. }
  616. /* Loop through the chips */
  617. for (; chip < nrchips; chip++) {
  618. /*
  619. * There was already a version of the table, reuse the page
  620. * This applies for absolute placement too, as we have the
  621. * page nr. in td->pages.
  622. */
  623. if (td->pages[chip] != -1) {
  624. page = td->pages[chip];
  625. goto write;
  626. }
  627. /*
  628. * Automatic placement of the bad block table. Search direction
  629. * top -> down?
  630. */
  631. if (td->options & NAND_BBT_LASTBLOCK) {
  632. startblock = numblocks * (chip + 1) - 1;
  633. dir = -1;
  634. } else {
  635. startblock = chip * numblocks;
  636. dir = 1;
  637. }
  638. for (i = 0; i < td->maxblocks; i++) {
  639. int block = startblock + dir * i;
  640. /* Check, if the block is bad */
  641. switch ((this->bbt[block >> 2] >>
  642. (2 * (block & 0x03))) & 0x03) {
  643. case 0x01:
  644. case 0x03:
  645. continue;
  646. }
  647. page = block <<
  648. (this->bbt_erase_shift - this->page_shift);
  649. /* Check, if the block is used by the mirror table */
  650. if (!md || md->pages[chip] != page)
  651. goto write;
  652. }
  653. printk(KERN_ERR "No space left to write bad block table\n");
  654. return -ENOSPC;
  655. write:
  656. /* Set up shift count and masks for the flash table */
  657. bits = td->options & NAND_BBT_NRBITS_MSK;
  658. msk[2] = ~rcode;
  659. switch (bits) {
  660. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  661. msk[3] = 0x01;
  662. break;
  663. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  664. msk[3] = 0x03;
  665. break;
  666. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  667. msk[3] = 0x0f;
  668. break;
  669. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  670. msk[3] = 0xff;
  671. break;
  672. default: return -EINVAL;
  673. }
  674. bbtoffs = chip * (numblocks >> 2);
  675. to = ((loff_t) page) << this->page_shift;
  676. /* Must we save the block contents? */
  677. if (td->options & NAND_BBT_SAVECONTENT) {
  678. /* Make it block aligned */
  679. to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
  680. len = 1 << this->bbt_erase_shift;
  681. res = mtd->read(mtd, to, len, &retlen, buf);
  682. if (res < 0) {
  683. if (retlen != len) {
  684. printk(KERN_INFO "nand_bbt: Error "
  685. "reading block for writing "
  686. "the bad block table\n");
  687. return res;
  688. }
  689. printk(KERN_WARNING "nand_bbt: ECC error "
  690. "while reading block for writing "
  691. "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. printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
  760. "0x%02X\n", (unsigned long long)to, td->version[chip]);
  761. /* Mark it as used */
  762. td->pages[chip] = page;
  763. }
  764. return 0;
  765. outerr:
  766. printk(KERN_WARNING
  767. "nand_bbt: Error while writing bad block table %d\n", res);
  768. return res;
  769. }
  770. /**
  771. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  772. * @mtd: MTD device structure
  773. * @bd: descriptor for the good/bad block search pattern
  774. *
  775. * The function creates a memory based bbt by scanning the device for
  776. * manufacturer / software marked good / bad blocks.
  777. */
  778. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  779. {
  780. struct nand_chip *this = mtd->priv;
  781. bd->options &= ~NAND_BBT_SCANEMPTY;
  782. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  783. }
  784. /**
  785. * check_create - [GENERIC] create and write bbt(s) if necessary
  786. * @mtd: MTD device structure
  787. * @buf: temporary buffer
  788. * @bd: descriptor for the good/bad block search pattern
  789. *
  790. * The function checks the results of the previous call to read_bbt and creates
  791. * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
  792. * for the chip/device. Update is necessary if one of the tables is missing or
  793. * the version nr. of one table is less than the other.
  794. */
  795. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  796. {
  797. int i, chips, writeops, chipsel, res;
  798. struct nand_chip *this = mtd->priv;
  799. struct nand_bbt_descr *td = this->bbt_td;
  800. struct nand_bbt_descr *md = this->bbt_md;
  801. struct nand_bbt_descr *rd, *rd2;
  802. /* Do we have a bbt per chip? */
  803. if (td->options & NAND_BBT_PERCHIP)
  804. chips = this->numchips;
  805. else
  806. chips = 1;
  807. for (i = 0; i < chips; i++) {
  808. writeops = 0;
  809. rd = NULL;
  810. rd2 = NULL;
  811. /* Per chip or per device? */
  812. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  813. /* Mirrored table available? */
  814. if (md) {
  815. if (td->pages[i] == -1 && md->pages[i] == -1) {
  816. writeops = 0x03;
  817. goto create;
  818. }
  819. if (td->pages[i] == -1) {
  820. rd = md;
  821. td->version[i] = md->version[i];
  822. writeops = 1;
  823. goto writecheck;
  824. }
  825. if (md->pages[i] == -1) {
  826. rd = td;
  827. md->version[i] = td->version[i];
  828. writeops = 2;
  829. goto writecheck;
  830. }
  831. if (td->version[i] == md->version[i]) {
  832. rd = td;
  833. if (!(td->options & NAND_BBT_VERSION))
  834. rd2 = md;
  835. goto writecheck;
  836. }
  837. if (((int8_t) (td->version[i] - md->version[i])) > 0) {
  838. rd = td;
  839. md->version[i] = td->version[i];
  840. writeops = 2;
  841. } else {
  842. rd = md;
  843. td->version[i] = md->version[i];
  844. writeops = 1;
  845. }
  846. goto writecheck;
  847. } else {
  848. if (td->pages[i] == -1) {
  849. writeops = 0x01;
  850. goto create;
  851. }
  852. rd = td;
  853. goto writecheck;
  854. }
  855. create:
  856. /* Create the bad block table by scanning the device? */
  857. if (!(td->options & NAND_BBT_CREATE))
  858. continue;
  859. /* Create the table in memory by scanning the chip(s) */
  860. if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
  861. create_bbt(mtd, buf, bd, chipsel);
  862. td->version[i] = 1;
  863. if (md)
  864. md->version[i] = 1;
  865. writecheck:
  866. /* Read back first? */
  867. if (rd)
  868. read_abs_bbt(mtd, buf, rd, chipsel);
  869. /* If they weren't versioned, read both */
  870. if (rd2)
  871. read_abs_bbt(mtd, buf, rd2, chipsel);
  872. /* Write the bad block table to the device? */
  873. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  874. res = write_bbt(mtd, buf, td, md, chipsel);
  875. if (res < 0)
  876. return res;
  877. }
  878. /* Write the mirror bad block table to the device? */
  879. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  880. res = write_bbt(mtd, buf, md, td, chipsel);
  881. if (res < 0)
  882. return res;
  883. }
  884. }
  885. return 0;
  886. }
  887. /**
  888. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  889. * @mtd: MTD device structure
  890. * @td: bad block table descriptor
  891. *
  892. * The bad block table regions are marked as "bad" to prevent accidental
  893. * erasures / writes. The regions are identified by the mark 0x02.
  894. */
  895. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  896. {
  897. struct nand_chip *this = mtd->priv;
  898. int i, j, chips, block, nrblocks, update;
  899. uint8_t oldval, newval;
  900. /* Do we have a bbt per chip? */
  901. if (td->options & NAND_BBT_PERCHIP) {
  902. chips = this->numchips;
  903. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  904. } else {
  905. chips = 1;
  906. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  907. }
  908. for (i = 0; i < chips; i++) {
  909. if ((td->options & NAND_BBT_ABSPAGE) ||
  910. !(td->options & NAND_BBT_WRITE)) {
  911. if (td->pages[i] == -1)
  912. continue;
  913. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  914. block <<= 1;
  915. oldval = this->bbt[(block >> 3)];
  916. newval = oldval | (0x2 << (block & 0x06));
  917. this->bbt[(block >> 3)] = newval;
  918. if ((oldval != newval) && td->reserved_block_code)
  919. nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
  920. continue;
  921. }
  922. update = 0;
  923. if (td->options & NAND_BBT_LASTBLOCK)
  924. block = ((i + 1) * nrblocks) - td->maxblocks;
  925. else
  926. block = i * nrblocks;
  927. block <<= 1;
  928. for (j = 0; j < td->maxblocks; j++) {
  929. oldval = this->bbt[(block >> 3)];
  930. newval = oldval | (0x2 << (block & 0x06));
  931. this->bbt[(block >> 3)] = newval;
  932. if (oldval != newval)
  933. update = 1;
  934. block += 2;
  935. }
  936. /*
  937. * If we want reserved blocks to be recorded to flash, and some
  938. * new ones have been marked, then we need to update the stored
  939. * bbts. This should only happen once.
  940. */
  941. if (update && td->reserved_block_code)
  942. nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
  943. }
  944. }
  945. /**
  946. * verify_bbt_descr - verify the bad block description
  947. * @mtd: MTD device structure
  948. * @bd: the table to verify
  949. *
  950. * This functions performs a few sanity checks on the bad block description
  951. * table.
  952. */
  953. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  954. {
  955. struct nand_chip *this = mtd->priv;
  956. u32 pattern_len;
  957. u32 bits;
  958. u32 table_size;
  959. if (!bd)
  960. return;
  961. pattern_len = bd->len;
  962. bits = bd->options & NAND_BBT_NRBITS_MSK;
  963. BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
  964. !(this->bbt_options & NAND_BBT_USE_FLASH));
  965. BUG_ON(!bits);
  966. if (bd->options & NAND_BBT_VERSION)
  967. pattern_len++;
  968. if (bd->options & NAND_BBT_NO_OOB) {
  969. BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
  970. BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
  971. BUG_ON(bd->offs);
  972. if (bd->options & NAND_BBT_VERSION)
  973. BUG_ON(bd->veroffs != bd->len);
  974. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  975. }
  976. if (bd->options & NAND_BBT_PERCHIP)
  977. table_size = this->chipsize >> this->bbt_erase_shift;
  978. else
  979. table_size = mtd->size >> this->bbt_erase_shift;
  980. table_size >>= 3;
  981. table_size *= bits;
  982. if (bd->options & NAND_BBT_NO_OOB)
  983. table_size += pattern_len;
  984. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  985. }
  986. /**
  987. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  988. * @mtd: MTD device structure
  989. * @bd: descriptor for the good/bad block search pattern
  990. *
  991. * The function checks, if a bad block table(s) is/are already available. If
  992. * not it scans the device for manufacturer marked good / bad blocks and writes
  993. * the bad block table(s) to the selected place.
  994. *
  995. * The bad block table memory is allocated here. It must be freed by calling
  996. * the nand_free_bbt function.
  997. */
  998. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  999. {
  1000. struct nand_chip *this = mtd->priv;
  1001. int len, res = 0;
  1002. uint8_t *buf;
  1003. struct nand_bbt_descr *td = this->bbt_td;
  1004. struct nand_bbt_descr *md = this->bbt_md;
  1005. len = mtd->size >> (this->bbt_erase_shift + 2);
  1006. /*
  1007. * Allocate memory (2bit per block) and clear the memory bad block
  1008. * table.
  1009. */
  1010. this->bbt = kzalloc(len, GFP_KERNEL);
  1011. if (!this->bbt)
  1012. return -ENOMEM;
  1013. /*
  1014. * If no primary table decriptor is given, scan the device to build a
  1015. * memory based bad block table.
  1016. */
  1017. if (!td) {
  1018. if ((res = nand_memory_bbt(mtd, bd))) {
  1019. printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
  1020. kfree(this->bbt);
  1021. this->bbt = NULL;
  1022. }
  1023. return res;
  1024. }
  1025. verify_bbt_descr(mtd, td);
  1026. verify_bbt_descr(mtd, md);
  1027. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1028. len = (1 << this->bbt_erase_shift);
  1029. len += (len >> this->page_shift) * mtd->oobsize;
  1030. buf = vmalloc(len);
  1031. if (!buf) {
  1032. kfree(this->bbt);
  1033. this->bbt = NULL;
  1034. return -ENOMEM;
  1035. }
  1036. /* Is the bbt at a given page? */
  1037. if (td->options & NAND_BBT_ABSPAGE) {
  1038. res = read_abs_bbts(mtd, buf, td, md);
  1039. } else {
  1040. /* Search the bad block table using a pattern in oob */
  1041. res = search_read_bbts(mtd, buf, td, md);
  1042. }
  1043. if (res)
  1044. res = check_create(mtd, buf, bd);
  1045. /* Prevent the bbt regions from erasing / writing */
  1046. mark_bbt_region(mtd, td);
  1047. if (md)
  1048. mark_bbt_region(mtd, md);
  1049. vfree(buf);
  1050. return res;
  1051. }
  1052. /**
  1053. * nand_update_bbt - [NAND Interface] update bad block table(s)
  1054. * @mtd: MTD device structure
  1055. * @offs: the offset of the newly marked block
  1056. *
  1057. * The function updates the bad block table(s).
  1058. */
  1059. int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1060. {
  1061. struct nand_chip *this = mtd->priv;
  1062. int len, res = 0, writeops = 0;
  1063. int chip, chipsel;
  1064. uint8_t *buf;
  1065. struct nand_bbt_descr *td = this->bbt_td;
  1066. struct nand_bbt_descr *md = this->bbt_md;
  1067. if (!this->bbt || !td)
  1068. return -EINVAL;
  1069. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1070. len = (1 << this->bbt_erase_shift);
  1071. len += (len >> this->page_shift) * mtd->oobsize;
  1072. buf = kmalloc(len, GFP_KERNEL);
  1073. if (!buf)
  1074. return -ENOMEM;
  1075. writeops = md != NULL ? 0x03 : 0x01;
  1076. /* Do we have a bbt per chip? */
  1077. if (td->options & NAND_BBT_PERCHIP) {
  1078. chip = (int)(offs >> this->chip_shift);
  1079. chipsel = chip;
  1080. } else {
  1081. chip = 0;
  1082. chipsel = -1;
  1083. }
  1084. td->version[chip]++;
  1085. if (md)
  1086. md->version[chip]++;
  1087. /* Write the bad block table to the device? */
  1088. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  1089. res = write_bbt(mtd, buf, td, md, chipsel);
  1090. if (res < 0)
  1091. goto out;
  1092. }
  1093. /* Write the mirror bad block table to the device? */
  1094. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  1095. res = write_bbt(mtd, buf, md, td, chipsel);
  1096. }
  1097. out:
  1098. kfree(buf);
  1099. return res;
  1100. }
  1101. /*
  1102. * Define some generic bad / good block scan pattern which are used
  1103. * while scanning a device for factory marked good / bad blocks.
  1104. */
  1105. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1106. static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
  1107. static struct nand_bbt_descr agand_flashbased = {
  1108. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  1109. .offs = 0x20,
  1110. .len = 6,
  1111. .pattern = scan_agand_pattern
  1112. };
  1113. /* Generic flash bbt descriptors */
  1114. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1115. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1116. static struct nand_bbt_descr bbt_main_descr = {
  1117. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1118. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1119. .offs = 8,
  1120. .len = 4,
  1121. .veroffs = 12,
  1122. .maxblocks = 4,
  1123. .pattern = bbt_pattern
  1124. };
  1125. static struct nand_bbt_descr bbt_mirror_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 = mirror_pattern
  1133. };
  1134. static struct nand_bbt_descr bbt_main_no_bbt_descr = {
  1135. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1136. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1137. | NAND_BBT_NO_OOB,
  1138. .len = 4,
  1139. .veroffs = 4,
  1140. .maxblocks = 4,
  1141. .pattern = bbt_pattern
  1142. };
  1143. static struct nand_bbt_descr bbt_mirror_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 = mirror_pattern
  1151. };
  1152. /**
  1153. * nand_create_default_bbt_descr - [INTERN] Creates a BBT descriptor structure
  1154. * @this: NAND chip to create descriptor for
  1155. *
  1156. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1157. * based on the properties of "this". The new descriptor is stored in
  1158. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1159. * passed to this function.
  1160. */
  1161. static int nand_create_default_bbt_descr(struct nand_chip *this)
  1162. {
  1163. struct nand_bbt_descr *bd;
  1164. if (this->badblock_pattern) {
  1165. printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
  1166. return -EINVAL;
  1167. }
  1168. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1169. if (!bd)
  1170. return -ENOMEM;
  1171. bd->options = this->bbt_options;
  1172. bd->offs = this->badblockpos;
  1173. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1174. bd->pattern = scan_ff_pattern;
  1175. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1176. this->badblock_pattern = bd;
  1177. return 0;
  1178. }
  1179. /**
  1180. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1181. * @mtd: MTD device structure
  1182. *
  1183. * This function selects the default bad block table support for the device and
  1184. * calls the nand_scan_bbt function.
  1185. */
  1186. int nand_default_bbt(struct mtd_info *mtd)
  1187. {
  1188. struct nand_chip *this = mtd->priv;
  1189. /*
  1190. * Default for AG-AND. We must use a flash based bad block table as the
  1191. * devices have factory marked _good_ blocks. Erasing those blocks
  1192. * leads to loss of the good / bad information, so we _must_ store this
  1193. * information in a good / bad table during startup.
  1194. */
  1195. if (this->options & NAND_IS_AND) {
  1196. /* Use the default pattern descriptors */
  1197. if (!this->bbt_td) {
  1198. this->bbt_td = &bbt_main_descr;
  1199. this->bbt_md = &bbt_mirror_descr;
  1200. }
  1201. this->bbt_options |= NAND_BBT_USE_FLASH;
  1202. return nand_scan_bbt(mtd, &agand_flashbased);
  1203. }
  1204. /* Is a flash based bad block table requested? */
  1205. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1206. /* Use the default pattern descriptors */
  1207. if (!this->bbt_td) {
  1208. if (this->bbt_options & NAND_BBT_NO_OOB) {
  1209. this->bbt_td = &bbt_main_no_bbt_descr;
  1210. this->bbt_md = &bbt_mirror_no_bbt_descr;
  1211. } else {
  1212. this->bbt_td = &bbt_main_descr;
  1213. this->bbt_md = &bbt_mirror_descr;
  1214. }
  1215. }
  1216. } else {
  1217. this->bbt_td = NULL;
  1218. this->bbt_md = NULL;
  1219. }
  1220. if (!this->badblock_pattern)
  1221. nand_create_default_bbt_descr(this);
  1222. return nand_scan_bbt(mtd, this->badblock_pattern);
  1223. }
  1224. /**
  1225. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1226. * @mtd: MTD device structure
  1227. * @offs: offset in the device
  1228. * @allowbbt: allow access to bad block table region
  1229. */
  1230. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1231. {
  1232. struct nand_chip *this = mtd->priv;
  1233. int block;
  1234. uint8_t res;
  1235. /* Get block number * 2 */
  1236. block = (int)(offs >> (this->bbt_erase_shift - 1));
  1237. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1238. DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  1239. (unsigned int)offs, block >> 1, res);
  1240. switch ((int)res) {
  1241. case 0x00:
  1242. return 0;
  1243. case 0x01:
  1244. return 1;
  1245. case 0x02:
  1246. return allowbbt ? 0 : 1;
  1247. }
  1248. return 1;
  1249. }
  1250. EXPORT_SYMBOL(nand_scan_bbt);
  1251. EXPORT_SYMBOL(nand_default_bbt);