nand_bbt.c 33 KB

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