nand_bbt.c 39 KB

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