nand_bbt.c 33 KB

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