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