nand_bbt.c 30 KB

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