nand_bbt.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 neccecary 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_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob);
  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 &&
  165. (tmp == reserved_block_code)) {
  166. printk (KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
  167. ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  168. this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
  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. }
  181. }
  182. totlen -= len;
  183. from += len;
  184. }
  185. return 0;
  186. }
  187. /**
  188. * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  189. * @mtd: MTD device structure
  190. * @buf: temporary buffer
  191. * @td: descriptor for the bad block table
  192. * @chip: read the table for a specific chip, -1 read all chips.
  193. * Applies only if NAND_BBT_PERCHIP option is set
  194. *
  195. * Read the bad block table for all chips starting at a given page
  196. * We assume that the bbt bits are in consecutive order.
  197. */
  198. static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  199. {
  200. struct nand_chip *this = mtd->priv;
  201. int res = 0, i;
  202. int bits;
  203. bits = td->options & NAND_BBT_NRBITS_MSK;
  204. if (td->options & NAND_BBT_PERCHIP) {
  205. int offs = 0;
  206. for (i = 0; i < this->numchips; i++) {
  207. if (chip == -1 || chip == i)
  208. res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
  209. if (res)
  210. return res;
  211. offs += this->chipsize >> (this->bbt_erase_shift + 2);
  212. }
  213. } else {
  214. res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
  215. if (res)
  216. return res;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
  222. * @mtd: MTD device structure
  223. * @buf: temporary buffer
  224. * @td: descriptor for the bad block table
  225. * @md: descriptor for the bad block table mirror
  226. *
  227. * Read the bad block table(s) for all chips starting at a given page
  228. * We assume that the bbt bits are in consecutive order.
  229. *
  230. */
  231. static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td,
  232. struct nand_bbt_descr *md)
  233. {
  234. struct nand_chip *this = mtd->priv;
  235. /* Read the primary version, if available */
  236. if (td->options & NAND_BBT_VERSION) {
  237. nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
  238. td->version[0] = buf[mtd->oobblock + td->veroffs];
  239. printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]);
  240. }
  241. /* Read the mirror version, if available */
  242. if (md && (md->options & NAND_BBT_VERSION)) {
  243. nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
  244. md->version[0] = buf[mtd->oobblock + md->veroffs];
  245. printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]);
  246. }
  247. return 1;
  248. }
  249. /**
  250. * create_bbt - [GENERIC] Create a bad block table by scanning the device
  251. * @mtd: MTD device structure
  252. * @buf: temporary buffer
  253. * @bd: descriptor for the good/bad block search pattern
  254. * @chip: create the table for a specific chip, -1 read all chips.
  255. * Applies only if NAND_BBT_PERCHIP option is set
  256. *
  257. * Create a bad block table by scanning the device
  258. * for the given good/bad block identify pattern
  259. */
  260. static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
  261. {
  262. struct nand_chip *this = mtd->priv;
  263. int i, j, numblocks, len, scanlen;
  264. int startblock;
  265. loff_t from;
  266. size_t readlen, ooblen;
  267. printk (KERN_INFO "Scanning device for bad blocks\n");
  268. if (bd->options & NAND_BBT_SCANALLPAGES)
  269. len = 1 << (this->bbt_erase_shift - this->page_shift);
  270. else {
  271. if (bd->options & NAND_BBT_SCAN2NDPAGE)
  272. len = 2;
  273. else
  274. len = 1;
  275. }
  276. if (!(bd->options & NAND_BBT_SCANEMPTY)) {
  277. /* We need only read few bytes from the OOB area */
  278. scanlen = ooblen = 0;
  279. readlen = bd->len;
  280. } else {
  281. /* Full page content should be read */
  282. scanlen = mtd->oobblock + mtd->oobsize;
  283. readlen = len * mtd->oobblock;
  284. ooblen = len * mtd->oobsize;
  285. }
  286. if (chip == -1) {
  287. /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
  288. * makes shifting and masking less painful */
  289. numblocks = mtd->size >> (this->bbt_erase_shift - 1);
  290. startblock = 0;
  291. from = 0;
  292. } else {
  293. if (chip >= this->numchips) {
  294. printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
  295. chip + 1, this->numchips);
  296. return -EINVAL;
  297. }
  298. numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
  299. startblock = chip * numblocks;
  300. numblocks += startblock;
  301. from = startblock << (this->bbt_erase_shift - 1);
  302. }
  303. for (i = startblock; i < numblocks;) {
  304. int ret;
  305. if (bd->options & NAND_BBT_SCANEMPTY)
  306. if ((ret = nand_read_raw (mtd, buf, from, readlen, ooblen)))
  307. return ret;
  308. for (j = 0; j < len; j++) {
  309. if (!(bd->options & NAND_BBT_SCANEMPTY)) {
  310. size_t retlen;
  311. /* Read the full oob until read_oob is fixed to
  312. * handle single byte reads for 16 bit buswidth */
  313. ret = mtd->read_oob(mtd, from + j * mtd->oobblock,
  314. mtd->oobsize, &retlen, buf);
  315. if (ret)
  316. return ret;
  317. if (check_short_pattern (buf, bd)) {
  318. this->bbt[i >> 3] |= 0x03 << (i & 0x6);
  319. printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
  320. i >> 1, (unsigned int) from);
  321. break;
  322. }
  323. } else {
  324. if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
  325. this->bbt[i >> 3] |= 0x03 << (i & 0x6);
  326. printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
  327. i >> 1, (unsigned int) from);
  328. break;
  329. }
  330. }
  331. }
  332. i += 2;
  333. from += (1 << this->bbt_erase_shift);
  334. }
  335. return 0;
  336. }
  337. /**
  338. * search_bbt - [GENERIC] scan the device for a specific bad block table
  339. * @mtd: MTD device structure
  340. * @buf: temporary buffer
  341. * @td: descriptor for the bad block table
  342. *
  343. * Read the bad block table by searching for a given ident pattern.
  344. * Search is preformed either from the beginning up or from the end of
  345. * the device downwards. The search starts always at the start of a
  346. * block.
  347. * If the option NAND_BBT_PERCHIP is given, each chip is searched
  348. * for a bbt, which contains the bad block information of this chip.
  349. * This is neccecary to provide support for certain DOC devices.
  350. *
  351. * The bbt ident pattern resides in the oob area of the first page
  352. * in a block.
  353. */
  354. static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  355. {
  356. struct nand_chip *this = mtd->priv;
  357. int i, chips;
  358. int bits, startblock, block, dir;
  359. int scanlen = mtd->oobblock + mtd->oobsize;
  360. int bbtblocks;
  361. /* Search direction top -> down ? */
  362. if (td->options & NAND_BBT_LASTBLOCK) {
  363. startblock = (mtd->size >> this->bbt_erase_shift) -1;
  364. dir = -1;
  365. } else {
  366. startblock = 0;
  367. dir = 1;
  368. }
  369. /* Do we have a bbt per chip ? */
  370. if (td->options & NAND_BBT_PERCHIP) {
  371. chips = this->numchips;
  372. bbtblocks = this->chipsize >> this->bbt_erase_shift;
  373. startblock &= bbtblocks - 1;
  374. } else {
  375. chips = 1;
  376. bbtblocks = mtd->size >> this->bbt_erase_shift;
  377. }
  378. /* Number of bits for each erase block in the bbt */
  379. bits = td->options & NAND_BBT_NRBITS_MSK;
  380. for (i = 0; i < chips; i++) {
  381. /* Reset version information */
  382. td->version[i] = 0;
  383. td->pages[i] = -1;
  384. /* Scan the maximum number of blocks */
  385. for (block = 0; block < td->maxblocks; block++) {
  386. int actblock = startblock + dir * block;
  387. /* Read first page */
  388. nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize);
  389. if (!check_pattern(buf, scanlen, mtd->oobblock, td)) {
  390. td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift);
  391. if (td->options & NAND_BBT_VERSION) {
  392. td->version[i] = buf[mtd->oobblock + td->veroffs];
  393. }
  394. break;
  395. }
  396. }
  397. startblock += this->chipsize >> this->bbt_erase_shift;
  398. }
  399. /* Check, if we found a bbt for each requested chip */
  400. for (i = 0; i < chips; i++) {
  401. if (td->pages[i] == -1)
  402. printk (KERN_WARNING "Bad block table not found for chip %d\n", i);
  403. else
  404. printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]);
  405. }
  406. return 0;
  407. }
  408. /**
  409. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  410. * @mtd: MTD device structure
  411. * @buf: temporary buffer
  412. * @td: descriptor for the bad block table
  413. * @md: descriptor for the bad block table mirror
  414. *
  415. * Search and read the bad block table(s)
  416. */
  417. static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf,
  418. struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  419. {
  420. /* Search the primary table */
  421. search_bbt (mtd, buf, td);
  422. /* Search the mirror table */
  423. if (md)
  424. search_bbt (mtd, buf, md);
  425. /* Force result check */
  426. return 1;
  427. }
  428. /**
  429. * write_bbt - [GENERIC] (Re)write the bad block table
  430. *
  431. * @mtd: MTD device structure
  432. * @buf: temporary buffer
  433. * @td: descriptor for the bad block table
  434. * @md: descriptor for the bad block table mirror
  435. * @chipsel: selector for a specific chip, -1 for all
  436. *
  437. * (Re)write the bad block table
  438. *
  439. */
  440. static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
  441. struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel)
  442. {
  443. struct nand_chip *this = mtd->priv;
  444. struct nand_oobinfo oobinfo;
  445. struct erase_info einfo;
  446. int i, j, res, chip = 0;
  447. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  448. int nrchips, bbtoffs, pageoffs;
  449. uint8_t msk[4];
  450. uint8_t rcode = td->reserved_block_code;
  451. size_t retlen, len = 0;
  452. loff_t to;
  453. if (!rcode)
  454. rcode = 0xff;
  455. /* Write bad block table per chip rather than per device ? */
  456. if (td->options & NAND_BBT_PERCHIP) {
  457. numblocks = (int) (this->chipsize >> this->bbt_erase_shift);
  458. /* Full device write or specific chip ? */
  459. if (chipsel == -1) {
  460. nrchips = this->numchips;
  461. } else {
  462. nrchips = chipsel + 1;
  463. chip = chipsel;
  464. }
  465. } else {
  466. numblocks = (int) (mtd->size >> this->bbt_erase_shift);
  467. nrchips = 1;
  468. }
  469. /* Loop through the chips */
  470. for (; chip < nrchips; chip++) {
  471. /* There was already a version of the table, reuse the page
  472. * This applies for absolute placement too, as we have the
  473. * page nr. in td->pages.
  474. */
  475. if (td->pages[chip] != -1) {
  476. page = td->pages[chip];
  477. goto write;
  478. }
  479. /* Automatic placement of the bad block table */
  480. /* Search direction top -> down ? */
  481. if (td->options & NAND_BBT_LASTBLOCK) {
  482. startblock = numblocks * (chip + 1) - 1;
  483. dir = -1;
  484. } else {
  485. startblock = chip * numblocks;
  486. dir = 1;
  487. }
  488. for (i = 0; i < td->maxblocks; i++) {
  489. int block = startblock + dir * i;
  490. /* Check, if the block is bad */
  491. switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) {
  492. case 0x01:
  493. case 0x03:
  494. continue;
  495. }
  496. page = block << (this->bbt_erase_shift - this->page_shift);
  497. /* Check, if the block is used by the mirror table */
  498. if (!md || md->pages[chip] != page)
  499. goto write;
  500. }
  501. printk (KERN_ERR "No space left to write bad block table\n");
  502. return -ENOSPC;
  503. write:
  504. /* Set up shift count and masks for the flash table */
  505. bits = td->options & NAND_BBT_NRBITS_MSK;
  506. switch (bits) {
  507. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break;
  508. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break;
  509. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break;
  510. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break;
  511. default: return -EINVAL;
  512. }
  513. bbtoffs = chip * (numblocks >> 2);
  514. to = ((loff_t) page) << this->page_shift;
  515. memcpy (&oobinfo, this->autooob, sizeof(oobinfo));
  516. oobinfo.useecc = MTD_NANDECC_PLACEONLY;
  517. /* Must we save the block contents ? */
  518. if (td->options & NAND_BBT_SAVECONTENT) {
  519. /* Make it block aligned */
  520. to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
  521. len = 1 << this->bbt_erase_shift;
  522. res = mtd->read_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
  523. if (res < 0) {
  524. if (retlen != len) {
  525. printk (KERN_INFO "nand_bbt: Error reading block for writing the bad block table\n");
  526. return res;
  527. }
  528. printk (KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n");
  529. }
  530. /* Calc the byte offset in the buffer */
  531. pageoffs = page - (int)(to >> this->page_shift);
  532. offs = pageoffs << this->page_shift;
  533. /* Preset the bbt area with 0xff */
  534. memset (&buf[offs], 0xff, (size_t)(numblocks >> sft));
  535. /* Preset the bbt's oob area with 0xff */
  536. memset (&buf[len + pageoffs * mtd->oobsize], 0xff,
  537. ((len >> this->page_shift) - pageoffs) * mtd->oobsize);
  538. if (td->options & NAND_BBT_VERSION) {
  539. buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip];
  540. }
  541. } else {
  542. /* Calc length */
  543. len = (size_t) (numblocks >> sft);
  544. /* Make it page aligned ! */
  545. len = (len + (mtd->oobblock-1)) & ~(mtd->oobblock-1);
  546. /* Preset the buffer with 0xff */
  547. memset (buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize);
  548. offs = 0;
  549. /* Pattern is located in oob area of first page */
  550. memcpy (&buf[len + td->offs], td->pattern, td->len);
  551. if (td->options & NAND_BBT_VERSION) {
  552. buf[len + td->veroffs] = td->version[chip];
  553. }
  554. }
  555. /* walk through the memory table */
  556. for (i = 0; i < numblocks; ) {
  557. uint8_t dat;
  558. dat = this->bbt[bbtoffs + (i >> 2)];
  559. for (j = 0; j < 4; j++ , i++) {
  560. int sftcnt = (i << (3 - sft)) & sftmsk;
  561. /* Do not store the reserved bbt blocks ! */
  562. buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt);
  563. dat >>= 2;
  564. }
  565. }
  566. memset (&einfo, 0, sizeof (einfo));
  567. einfo.mtd = mtd;
  568. einfo.addr = (unsigned long) to;
  569. einfo.len = 1 << this->bbt_erase_shift;
  570. res = nand_erase_nand (mtd, &einfo, 1);
  571. if (res < 0) {
  572. printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res);
  573. return res;
  574. }
  575. res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
  576. if (res < 0) {
  577. printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res);
  578. return res;
  579. }
  580. printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n",
  581. (unsigned int) to, td->version[chip]);
  582. /* Mark it as used */
  583. td->pages[chip] = page;
  584. }
  585. return 0;
  586. }
  587. /**
  588. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  589. * @mtd: MTD device structure
  590. * @bd: descriptor for the good/bad block search pattern
  591. *
  592. * The function creates a memory based bbt by scanning the device
  593. * for manufacturer / software marked good / bad blocks
  594. */
  595. static inline int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
  596. {
  597. struct nand_chip *this = mtd->priv;
  598. bd->options &= ~NAND_BBT_SCANEMPTY;
  599. return create_bbt (mtd, this->data_buf, bd, -1);
  600. }
  601. /**
  602. * check_create - [GENERIC] create and write bbt(s) if neccecary
  603. * @mtd: MTD device structure
  604. * @buf: temporary buffer
  605. * @bd: descriptor for the good/bad block search pattern
  606. *
  607. * The function checks the results of the previous call to read_bbt
  608. * and creates / updates the bbt(s) if neccecary
  609. * Creation is neccecary if no bbt was found for the chip/device
  610. * Update is neccecary if one of the tables is missing or the
  611. * version nr. of one table is less than the other
  612. */
  613. static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  614. {
  615. int i, chips, writeops, chipsel, res;
  616. struct nand_chip *this = mtd->priv;
  617. struct nand_bbt_descr *td = this->bbt_td;
  618. struct nand_bbt_descr *md = this->bbt_md;
  619. struct nand_bbt_descr *rd, *rd2;
  620. /* Do we have a bbt per chip ? */
  621. if (td->options & NAND_BBT_PERCHIP)
  622. chips = this->numchips;
  623. else
  624. chips = 1;
  625. for (i = 0; i < chips; i++) {
  626. writeops = 0;
  627. rd = NULL;
  628. rd2 = NULL;
  629. /* Per chip or per device ? */
  630. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  631. /* Mirrored table avilable ? */
  632. if (md) {
  633. if (td->pages[i] == -1 && md->pages[i] == -1) {
  634. writeops = 0x03;
  635. goto create;
  636. }
  637. if (td->pages[i] == -1) {
  638. rd = md;
  639. td->version[i] = md->version[i];
  640. writeops = 1;
  641. goto writecheck;
  642. }
  643. if (md->pages[i] == -1) {
  644. rd = td;
  645. md->version[i] = td->version[i];
  646. writeops = 2;
  647. goto writecheck;
  648. }
  649. if (td->version[i] == md->version[i]) {
  650. rd = td;
  651. if (!(td->options & NAND_BBT_VERSION))
  652. rd2 = md;
  653. goto writecheck;
  654. }
  655. if (((int8_t) (td->version[i] - md->version[i])) > 0) {
  656. rd = td;
  657. md->version[i] = td->version[i];
  658. writeops = 2;
  659. } else {
  660. rd = md;
  661. td->version[i] = md->version[i];
  662. writeops = 1;
  663. }
  664. goto writecheck;
  665. } else {
  666. if (td->pages[i] == -1) {
  667. writeops = 0x01;
  668. goto create;
  669. }
  670. rd = td;
  671. goto writecheck;
  672. }
  673. create:
  674. /* Create the bad block table by scanning the device ? */
  675. if (!(td->options & NAND_BBT_CREATE))
  676. continue;
  677. /* Create the table in memory by scanning the chip(s) */
  678. create_bbt (mtd, buf, bd, chipsel);
  679. td->version[i] = 1;
  680. if (md)
  681. md->version[i] = 1;
  682. writecheck:
  683. /* read back first ? */
  684. if (rd)
  685. read_abs_bbt (mtd, buf, rd, chipsel);
  686. /* If they weren't versioned, read both. */
  687. if (rd2)
  688. read_abs_bbt (mtd, buf, rd2, chipsel);
  689. /* Write the bad block table to the device ? */
  690. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  691. res = write_bbt (mtd, buf, td, md, chipsel);
  692. if (res < 0)
  693. return res;
  694. }
  695. /* Write the mirror bad block table to the device ? */
  696. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  697. res = write_bbt (mtd, buf, md, td, chipsel);
  698. if (res < 0)
  699. return res;
  700. }
  701. }
  702. return 0;
  703. }
  704. /**
  705. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  706. * @mtd: MTD device structure
  707. * @td: bad block table descriptor
  708. *
  709. * The bad block table regions are marked as "bad" to prevent
  710. * accidental erasures / writes. The regions are identified by
  711. * the mark 0x02.
  712. */
  713. static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
  714. {
  715. struct nand_chip *this = mtd->priv;
  716. int i, j, chips, block, nrblocks, update;
  717. uint8_t oldval, newval;
  718. /* Do we have a bbt per chip ? */
  719. if (td->options & NAND_BBT_PERCHIP) {
  720. chips = this->numchips;
  721. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  722. } else {
  723. chips = 1;
  724. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  725. }
  726. for (i = 0; i < chips; i++) {
  727. if ((td->options & NAND_BBT_ABSPAGE) ||
  728. !(td->options & NAND_BBT_WRITE)) {
  729. if (td->pages[i] == -1) continue;
  730. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  731. block <<= 1;
  732. oldval = this->bbt[(block >> 3)];
  733. newval = oldval | (0x2 << (block & 0x06));
  734. this->bbt[(block >> 3)] = newval;
  735. if ((oldval != newval) && td->reserved_block_code)
  736. nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
  737. continue;
  738. }
  739. update = 0;
  740. if (td->options & NAND_BBT_LASTBLOCK)
  741. block = ((i + 1) * nrblocks) - td->maxblocks;
  742. else
  743. block = i * nrblocks;
  744. block <<= 1;
  745. for (j = 0; j < td->maxblocks; j++) {
  746. oldval = this->bbt[(block >> 3)];
  747. newval = oldval | (0x2 << (block & 0x06));
  748. this->bbt[(block >> 3)] = newval;
  749. if (oldval != newval) update = 1;
  750. block += 2;
  751. }
  752. /* If we want reserved blocks to be recorded to flash, and some
  753. new ones have been marked, then we need to update the stored
  754. bbts. This should only happen once. */
  755. if (update && td->reserved_block_code)
  756. nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
  757. }
  758. }
  759. /**
  760. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  761. * @mtd: MTD device structure
  762. * @bd: descriptor for the good/bad block search pattern
  763. *
  764. * The function checks, if a bad block table(s) is/are already
  765. * available. If not it scans the device for manufacturer
  766. * marked good / bad blocks and writes the bad block table(s) to
  767. * the selected place.
  768. *
  769. * The bad block table memory is allocated here. It must be freed
  770. * by calling the nand_free_bbt function.
  771. *
  772. */
  773. int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
  774. {
  775. struct nand_chip *this = mtd->priv;
  776. int len, res = 0;
  777. uint8_t *buf;
  778. struct nand_bbt_descr *td = this->bbt_td;
  779. struct nand_bbt_descr *md = this->bbt_md;
  780. len = mtd->size >> (this->bbt_erase_shift + 2);
  781. /* Allocate memory (2bit per block) */
  782. this->bbt = kmalloc (len, GFP_KERNEL);
  783. if (!this->bbt) {
  784. printk (KERN_ERR "nand_scan_bbt: Out of memory\n");
  785. return -ENOMEM;
  786. }
  787. /* Clear the memory bad block table */
  788. memset (this->bbt, 0x00, len);
  789. /* If no primary table decriptor is given, scan the device
  790. * to build a memory based bad block table
  791. */
  792. if (!td) {
  793. if ((res = nand_memory_bbt(mtd, bd))) {
  794. printk (KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
  795. kfree (this->bbt);
  796. this->bbt = NULL;
  797. }
  798. return res;
  799. }
  800. /* Allocate a temporary buffer for one eraseblock incl. oob */
  801. len = (1 << this->bbt_erase_shift);
  802. len += (len >> this->page_shift) * mtd->oobsize;
  803. buf = vmalloc(len);
  804. if (!buf) {
  805. printk (KERN_ERR "nand_bbt: Out of memory\n");
  806. kfree (this->bbt);
  807. this->bbt = NULL;
  808. return -ENOMEM;
  809. }
  810. /* Is the bbt at a given page ? */
  811. if (td->options & NAND_BBT_ABSPAGE) {
  812. res = read_abs_bbts (mtd, buf, td, md);
  813. } else {
  814. /* Search the bad block table using a pattern in oob */
  815. res = search_read_bbts (mtd, buf, td, md);
  816. }
  817. if (res)
  818. res = check_create (mtd, buf, bd);
  819. /* Prevent the bbt regions from erasing / writing */
  820. mark_bbt_region (mtd, td);
  821. if (md)
  822. mark_bbt_region (mtd, md);
  823. vfree (buf);
  824. return res;
  825. }
  826. /**
  827. * nand_update_bbt - [NAND Interface] update bad block table(s)
  828. * @mtd: MTD device structure
  829. * @offs: the offset of the newly marked block
  830. *
  831. * The function updates the bad block table(s)
  832. */
  833. int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
  834. {
  835. struct nand_chip *this = mtd->priv;
  836. int len, res = 0, writeops = 0;
  837. int chip, chipsel;
  838. uint8_t *buf;
  839. struct nand_bbt_descr *td = this->bbt_td;
  840. struct nand_bbt_descr *md = this->bbt_md;
  841. if (!this->bbt || !td)
  842. return -EINVAL;
  843. len = mtd->size >> (this->bbt_erase_shift + 2);
  844. /* Allocate a temporary buffer for one eraseblock incl. oob */
  845. len = (1 << this->bbt_erase_shift);
  846. len += (len >> this->page_shift) * mtd->oobsize;
  847. buf = kmalloc (len, GFP_KERNEL);
  848. if (!buf) {
  849. printk (KERN_ERR "nand_update_bbt: Out of memory\n");
  850. return -ENOMEM;
  851. }
  852. writeops = md != NULL ? 0x03 : 0x01;
  853. /* Do we have a bbt per chip ? */
  854. if (td->options & NAND_BBT_PERCHIP) {
  855. chip = (int) (offs >> this->chip_shift);
  856. chipsel = chip;
  857. } else {
  858. chip = 0;
  859. chipsel = -1;
  860. }
  861. td->version[chip]++;
  862. if (md)
  863. md->version[chip]++;
  864. /* Write the bad block table to the device ? */
  865. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  866. res = write_bbt (mtd, buf, td, md, chipsel);
  867. if (res < 0)
  868. goto out;
  869. }
  870. /* Write the mirror bad block table to the device ? */
  871. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  872. res = write_bbt (mtd, buf, md, td, chipsel);
  873. }
  874. out:
  875. kfree (buf);
  876. return res;
  877. }
  878. /* Define some generic bad / good block scan pattern which are used
  879. * while scanning a device for factory marked good / bad blocks. */
  880. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  881. static struct nand_bbt_descr smallpage_memorybased = {
  882. .options = NAND_BBT_SCAN2NDPAGE,
  883. .offs = 5,
  884. .len = 1,
  885. .pattern = scan_ff_pattern
  886. };
  887. static struct nand_bbt_descr largepage_memorybased = {
  888. .options = 0,
  889. .offs = 0,
  890. .len = 2,
  891. .pattern = scan_ff_pattern
  892. };
  893. static struct nand_bbt_descr smallpage_flashbased = {
  894. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  895. .offs = 5,
  896. .len = 1,
  897. .pattern = scan_ff_pattern
  898. };
  899. static struct nand_bbt_descr largepage_flashbased = {
  900. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  901. .offs = 0,
  902. .len = 2,
  903. .pattern = scan_ff_pattern
  904. };
  905. static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
  906. static struct nand_bbt_descr agand_flashbased = {
  907. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  908. .offs = 0x20,
  909. .len = 6,
  910. .pattern = scan_agand_pattern
  911. };
  912. /* Generic flash bbt decriptors
  913. */
  914. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  915. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  916. static struct nand_bbt_descr bbt_main_descr = {
  917. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  918. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  919. .offs = 8,
  920. .len = 4,
  921. .veroffs = 12,
  922. .maxblocks = 4,
  923. .pattern = bbt_pattern
  924. };
  925. static struct nand_bbt_descr bbt_mirror_descr = {
  926. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  927. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  928. .offs = 8,
  929. .len = 4,
  930. .veroffs = 12,
  931. .maxblocks = 4,
  932. .pattern = mirror_pattern
  933. };
  934. /**
  935. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  936. * @mtd: MTD device structure
  937. *
  938. * This function selects the default bad block table
  939. * support for the device and calls the nand_scan_bbt function
  940. *
  941. */
  942. int nand_default_bbt (struct mtd_info *mtd)
  943. {
  944. struct nand_chip *this = mtd->priv;
  945. /* Default for AG-AND. We must use a flash based
  946. * bad block table as the devices have factory marked
  947. * _good_ blocks. Erasing those blocks leads to loss
  948. * of the good / bad information, so we _must_ store
  949. * this information in a good / bad table during
  950. * startup
  951. */
  952. if (this->options & NAND_IS_AND) {
  953. /* Use the default pattern descriptors */
  954. if (!this->bbt_td) {
  955. this->bbt_td = &bbt_main_descr;
  956. this->bbt_md = &bbt_mirror_descr;
  957. }
  958. this->options |= NAND_USE_FLASH_BBT;
  959. return nand_scan_bbt (mtd, &agand_flashbased);
  960. }
  961. /* Is a flash based bad block table requested ? */
  962. if (this->options & NAND_USE_FLASH_BBT) {
  963. /* Use the default pattern descriptors */
  964. if (!this->bbt_td) {
  965. this->bbt_td = &bbt_main_descr;
  966. this->bbt_md = &bbt_mirror_descr;
  967. }
  968. if (!this->badblock_pattern) {
  969. this->badblock_pattern = (mtd->oobblock > 512) ?
  970. &largepage_flashbased : &smallpage_flashbased;
  971. }
  972. } else {
  973. this->bbt_td = NULL;
  974. this->bbt_md = NULL;
  975. if (!this->badblock_pattern) {
  976. this->badblock_pattern = (mtd->oobblock > 512) ?
  977. &largepage_memorybased : &smallpage_memorybased;
  978. }
  979. }
  980. return nand_scan_bbt (mtd, this->badblock_pattern);
  981. }
  982. /**
  983. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  984. * @mtd: MTD device structure
  985. * @offs: offset in the device
  986. * @allowbbt: allow access to bad block table region
  987. *
  988. */
  989. int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt)
  990. {
  991. struct nand_chip *this = mtd->priv;
  992. int block;
  993. uint8_t res;
  994. /* Get block number * 2 */
  995. block = (int) (offs >> (this->bbt_erase_shift - 1));
  996. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  997. DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  998. (unsigned int)offs, block >> 1, res);
  999. switch ((int)res) {
  1000. case 0x00: return 0;
  1001. case 0x01: return 1;
  1002. case 0x02: return allowbbt ? 0 : 1;
  1003. }
  1004. return 1;
  1005. }
  1006. EXPORT_SYMBOL (nand_scan_bbt);
  1007. EXPORT_SYMBOL (nand_default_bbt);