nand_bbt.c 30 KB

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