nand_bbt.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 bits, 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. /* Number of bits for each erase block in the bbt */
  445. bits = td->options & NAND_BBT_NRBITS_MSK;
  446. for (i = 0; i < chips; i++) {
  447. /* Reset version information */
  448. td->version[i] = 0;
  449. td->pages[i] = -1;
  450. /* Scan the maximum number of blocks */
  451. for (block = 0; block < td->maxblocks; block++) {
  452. int actblock = startblock + dir * block;
  453. loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
  454. /* Read first page */
  455. scan_read_raw(mtd, buf, offs, mtd->writesize);
  456. if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
  457. td->pages[i] = actblock << blocktopage;
  458. if (td->options & NAND_BBT_VERSION) {
  459. td->version[i] = buf[mtd->writesize + td->veroffs];
  460. }
  461. break;
  462. }
  463. }
  464. startblock += this->chipsize >> this->bbt_erase_shift;
  465. }
  466. /* Check, if we found a bbt for each requested chip */
  467. for (i = 0; i < chips; i++) {
  468. if (td->pages[i] == -1)
  469. printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
  470. else
  471. printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
  472. td->version[i]);
  473. }
  474. return 0;
  475. }
  476. /**
  477. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  478. * @mtd: MTD device structure
  479. * @buf: temporary buffer
  480. * @td: descriptor for the bad block table
  481. * @md: descriptor for the bad block table mirror
  482. *
  483. * Search and read the bad block table(s)
  484. */
  485. static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  486. {
  487. /* Search the primary table */
  488. search_bbt(mtd, buf, td);
  489. /* Search the mirror table */
  490. if (md)
  491. search_bbt(mtd, buf, md);
  492. /* Force result check */
  493. return 1;
  494. }
  495. /**
  496. * write_bbt - [GENERIC] (Re)write the bad block table
  497. *
  498. * @mtd: MTD device structure
  499. * @buf: temporary buffer
  500. * @td: descriptor for the bad block table
  501. * @md: descriptor for the bad block table mirror
  502. * @chipsel: selector for a specific chip, -1 for all
  503. *
  504. * (Re)write the bad block table
  505. *
  506. */
  507. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  508. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  509. int chipsel)
  510. {
  511. struct nand_chip *this = mtd->priv;
  512. struct erase_info einfo;
  513. int i, j, res, chip = 0;
  514. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  515. int nrchips, bbtoffs, pageoffs, ooboffs;
  516. uint8_t msk[4];
  517. uint8_t rcode = td->reserved_block_code;
  518. size_t retlen, len = 0;
  519. loff_t to;
  520. struct mtd_oob_ops ops;
  521. ops.ooblen = mtd->oobsize;
  522. ops.ooboffs = 0;
  523. ops.datbuf = NULL;
  524. ops.mode = MTD_OOB_PLACE;
  525. if (!rcode)
  526. rcode = 0xff;
  527. /* Write bad block table per chip rather than per device ? */
  528. if (td->options & NAND_BBT_PERCHIP) {
  529. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  530. /* Full device write or specific chip ? */
  531. if (chipsel == -1) {
  532. nrchips = this->numchips;
  533. } else {
  534. nrchips = chipsel + 1;
  535. chip = chipsel;
  536. }
  537. } else {
  538. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  539. nrchips = 1;
  540. }
  541. /* Loop through the chips */
  542. for (; chip < nrchips; chip++) {
  543. /* There was already a version of the table, reuse the page
  544. * This applies for absolute placement too, as we have the
  545. * page nr. in td->pages.
  546. */
  547. if (td->pages[chip] != -1) {
  548. page = td->pages[chip];
  549. goto write;
  550. }
  551. /* Automatic placement of the bad block table */
  552. /* Search direction top -> down ? */
  553. if (td->options & NAND_BBT_LASTBLOCK) {
  554. startblock = numblocks * (chip + 1) - 1;
  555. dir = -1;
  556. } else {
  557. startblock = chip * numblocks;
  558. dir = 1;
  559. }
  560. for (i = 0; i < td->maxblocks; i++) {
  561. int block = startblock + dir * i;
  562. /* Check, if the block is bad */
  563. switch ((this->bbt[block >> 2] >>
  564. (2 * (block & 0x03))) & 0x03) {
  565. case 0x01:
  566. case 0x03:
  567. continue;
  568. }
  569. page = block <<
  570. (this->bbt_erase_shift - this->page_shift);
  571. /* Check, if the block is used by the mirror table */
  572. if (!md || md->pages[chip] != page)
  573. goto write;
  574. }
  575. printk(KERN_ERR "No space left to write bad block table\n");
  576. return -ENOSPC;
  577. write:
  578. /* Set up shift count and masks for the flash table */
  579. bits = td->options & NAND_BBT_NRBITS_MSK;
  580. msk[2] = ~rcode;
  581. switch (bits) {
  582. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  583. msk[3] = 0x01;
  584. break;
  585. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  586. msk[3] = 0x03;
  587. break;
  588. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  589. msk[3] = 0x0f;
  590. break;
  591. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  592. msk[3] = 0xff;
  593. break;
  594. default: return -EINVAL;
  595. }
  596. bbtoffs = chip * (numblocks >> 2);
  597. to = ((loff_t) page) << this->page_shift;
  598. /* Must we save the block contents ? */
  599. if (td->options & NAND_BBT_SAVECONTENT) {
  600. /* Make it block aligned */
  601. to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
  602. len = 1 << this->bbt_erase_shift;
  603. res = mtd->read(mtd, to, len, &retlen, buf);
  604. if (res < 0) {
  605. if (retlen != len) {
  606. printk(KERN_INFO "nand_bbt: Error "
  607. "reading block for writing "
  608. "the bad block table\n");
  609. return res;
  610. }
  611. printk(KERN_WARNING "nand_bbt: ECC error "
  612. "while reading block for writing "
  613. "bad block table\n");
  614. }
  615. /* Read oob data */
  616. ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
  617. ops.oobbuf = &buf[len];
  618. res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
  619. if (res < 0 || ops.oobretlen != ops.ooblen)
  620. goto outerr;
  621. /* Calc the byte offset in the buffer */
  622. pageoffs = page - (int)(to >> this->page_shift);
  623. offs = pageoffs << this->page_shift;
  624. /* Preset the bbt area with 0xff */
  625. memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
  626. ooboffs = len + (pageoffs * mtd->oobsize);
  627. } else {
  628. /* Calc length */
  629. len = (size_t) (numblocks >> sft);
  630. /* Make it page aligned ! */
  631. len = (len + (mtd->writesize - 1)) &
  632. ~(mtd->writesize - 1);
  633. /* Preset the buffer with 0xff */
  634. memset(buf, 0xff, len +
  635. (len >> this->page_shift)* mtd->oobsize);
  636. offs = 0;
  637. ooboffs = len;
  638. /* Pattern is located in oob area of first page */
  639. memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
  640. }
  641. if (td->options & NAND_BBT_VERSION)
  642. buf[ooboffs + td->veroffs] = td->version[chip];
  643. /* walk through the memory table */
  644. for (i = 0; i < numblocks;) {
  645. uint8_t dat;
  646. dat = this->bbt[bbtoffs + (i >> 2)];
  647. for (j = 0; j < 4; j++, i++) {
  648. int sftcnt = (i << (3 - sft)) & sftmsk;
  649. /* Do not store the reserved bbt blocks ! */
  650. buf[offs + (i >> sft)] &=
  651. ~(msk[dat & 0x03] << sftcnt);
  652. dat >>= 2;
  653. }
  654. }
  655. memset(&einfo, 0, sizeof(einfo));
  656. einfo.mtd = mtd;
  657. einfo.addr = to;
  658. einfo.len = 1 << this->bbt_erase_shift;
  659. res = nand_erase_nand(mtd, &einfo, 1);
  660. if (res < 0)
  661. goto outerr;
  662. res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
  663. if (res < 0)
  664. goto outerr;
  665. printk(KERN_DEBUG "Bad block table written to 0x%012llx, "
  666. "version 0x%02X\n", (unsigned long long)to,
  667. 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, (loff_t)block <<
  828. (this->bbt_erase_shift - 1));
  829. continue;
  830. }
  831. update = 0;
  832. if (td->options & NAND_BBT_LASTBLOCK)
  833. block = ((i + 1) * nrblocks) - td->maxblocks;
  834. else
  835. block = i * nrblocks;
  836. block <<= 1;
  837. for (j = 0; j < td->maxblocks; j++) {
  838. oldval = this->bbt[(block >> 3)];
  839. newval = oldval | (0x2 << (block & 0x06));
  840. this->bbt[(block >> 3)] = newval;
  841. if (oldval != newval)
  842. update = 1;
  843. block += 2;
  844. }
  845. /* If we want reserved blocks to be recorded to flash, and some
  846. new ones have been marked, then we need to update the stored
  847. bbts. This should only happen once. */
  848. if (update && td->reserved_block_code)
  849. nand_update_bbt(mtd, (loff_t)(block - 2) <<
  850. (this->bbt_erase_shift - 1));
  851. }
  852. }
  853. /**
  854. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  855. * @mtd: MTD device structure
  856. * @bd: descriptor for the good/bad block search pattern
  857. *
  858. * The function checks, if a bad block table(s) is/are already
  859. * available. If not it scans the device for manufacturer
  860. * marked good / bad blocks and writes the bad block table(s) to
  861. * the selected place.
  862. *
  863. * The bad block table memory is allocated here. It must be freed
  864. * by calling the nand_free_bbt function.
  865. *
  866. */
  867. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  868. {
  869. struct nand_chip *this = mtd->priv;
  870. int len, res = 0;
  871. uint8_t *buf;
  872. struct nand_bbt_descr *td = this->bbt_td;
  873. struct nand_bbt_descr *md = this->bbt_md;
  874. len = mtd->size >> (this->bbt_erase_shift + 2);
  875. /* Allocate memory (2bit per block) and clear the memory bad block table */
  876. this->bbt = kzalloc(len, GFP_KERNEL);
  877. if (!this->bbt) {
  878. printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
  879. return -ENOMEM;
  880. }
  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. /* Allocate a temporary buffer for one eraseblock incl. oob */
  936. len = (1 << this->bbt_erase_shift);
  937. len += (len >> this->page_shift) * mtd->oobsize;
  938. buf = kmalloc(len, GFP_KERNEL);
  939. if (!buf) {
  940. printk(KERN_ERR "nand_update_bbt: Out of memory\n");
  941. return -ENOMEM;
  942. }
  943. writeops = md != NULL ? 0x03 : 0x01;
  944. /* Do we have a bbt per chip ? */
  945. if (td->options & NAND_BBT_PERCHIP) {
  946. chip = (int)(offs >> this->chip_shift);
  947. chipsel = chip;
  948. } else {
  949. chip = 0;
  950. chipsel = -1;
  951. }
  952. td->version[chip]++;
  953. if (md)
  954. md->version[chip]++;
  955. /* Write the bad block table to the device ? */
  956. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  957. res = write_bbt(mtd, buf, td, md, chipsel);
  958. if (res < 0)
  959. goto out;
  960. }
  961. /* Write the mirror bad block table to the device ? */
  962. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  963. res = write_bbt(mtd, buf, md, td, chipsel);
  964. }
  965. out:
  966. kfree(buf);
  967. return res;
  968. }
  969. /* Define some generic bad / good block scan pattern which are used
  970. * while scanning a device for factory marked good / bad blocks. */
  971. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  972. static struct nand_bbt_descr smallpage_memorybased = {
  973. .options = NAND_BBT_SCAN2NDPAGE,
  974. .offs = 5,
  975. .len = 1,
  976. .pattern = scan_ff_pattern
  977. };
  978. static struct nand_bbt_descr largepage_memorybased = {
  979. .options = 0,
  980. .offs = 0,
  981. .len = 2,
  982. .pattern = scan_ff_pattern
  983. };
  984. static struct nand_bbt_descr smallpage_flashbased = {
  985. .options = NAND_BBT_SCAN2NDPAGE,
  986. .offs = 5,
  987. .len = 1,
  988. .pattern = scan_ff_pattern
  989. };
  990. static struct nand_bbt_descr largepage_flashbased = {
  991. .options = NAND_BBT_SCAN2NDPAGE,
  992. .offs = 0,
  993. .len = 2,
  994. .pattern = scan_ff_pattern
  995. };
  996. static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
  997. static struct nand_bbt_descr agand_flashbased = {
  998. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  999. .offs = 0x20,
  1000. .len = 6,
  1001. .pattern = scan_agand_pattern
  1002. };
  1003. /* Generic flash bbt decriptors
  1004. */
  1005. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1006. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1007. static struct nand_bbt_descr bbt_main_descr = {
  1008. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1009. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1010. .offs = 8,
  1011. .len = 4,
  1012. .veroffs = 12,
  1013. .maxblocks = 4,
  1014. .pattern = bbt_pattern
  1015. };
  1016. static struct nand_bbt_descr bbt_mirror_descr = {
  1017. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1018. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1019. .offs = 8,
  1020. .len = 4,
  1021. .veroffs = 12,
  1022. .maxblocks = 4,
  1023. .pattern = mirror_pattern
  1024. };
  1025. /**
  1026. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1027. * @mtd: MTD device structure
  1028. *
  1029. * This function selects the default bad block table
  1030. * support for the device and calls the nand_scan_bbt function
  1031. *
  1032. */
  1033. int nand_default_bbt(struct mtd_info *mtd)
  1034. {
  1035. struct nand_chip *this = mtd->priv;
  1036. /* Default for AG-AND. We must use a flash based
  1037. * bad block table as the devices have factory marked
  1038. * _good_ blocks. Erasing those blocks leads to loss
  1039. * of the good / bad information, so we _must_ store
  1040. * this information in a good / bad table during
  1041. * startup
  1042. */
  1043. if (this->options & NAND_IS_AND) {
  1044. /* Use the default pattern descriptors */
  1045. if (!this->bbt_td) {
  1046. this->bbt_td = &bbt_main_descr;
  1047. this->bbt_md = &bbt_mirror_descr;
  1048. }
  1049. this->options |= NAND_USE_FLASH_BBT;
  1050. return nand_scan_bbt(mtd, &agand_flashbased);
  1051. }
  1052. /* Is a flash based bad block table requested ? */
  1053. if (this->options & NAND_USE_FLASH_BBT) {
  1054. /* Use the default pattern descriptors */
  1055. if (!this->bbt_td) {
  1056. this->bbt_td = &bbt_main_descr;
  1057. this->bbt_md = &bbt_mirror_descr;
  1058. }
  1059. if (!this->badblock_pattern) {
  1060. this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
  1061. }
  1062. } else {
  1063. this->bbt_td = NULL;
  1064. this->bbt_md = NULL;
  1065. if (!this->badblock_pattern) {
  1066. this->badblock_pattern = (mtd->writesize > 512) ?
  1067. &largepage_memorybased : &smallpage_memorybased;
  1068. }
  1069. }
  1070. return nand_scan_bbt(mtd, this->badblock_pattern);
  1071. }
  1072. /**
  1073. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1074. * @mtd: MTD device structure
  1075. * @offs: offset in the device
  1076. * @allowbbt: allow access to bad block table region
  1077. *
  1078. */
  1079. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1080. {
  1081. struct nand_chip *this = mtd->priv;
  1082. int block;
  1083. uint8_t res;
  1084. /* Get block number * 2 */
  1085. block = (int)(offs >> (this->bbt_erase_shift - 1));
  1086. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1087. MTDDEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: "
  1088. "(block %d) 0x%02x\n", (unsigned int)offs, res, block >> 1);
  1089. switch ((int)res) {
  1090. case 0x00:
  1091. return 0;
  1092. case 0x01:
  1093. return 1;
  1094. case 0x02:
  1095. return allowbbt ? 0 : 1;
  1096. }
  1097. return 1;
  1098. }