nand_bbt.c 30 KB

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