nand_bbt.c 31 KB

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