nand_bbt.c 35 KB

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