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