nand_bbt.c 39 KB

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