Advertisement
Guest User

nand_bbt.c

a guest
Feb 26th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.92 KB | None | 0 0
  1. /*
  2. * Overview:
  3. * Bad block table support for the NAND driver
  4. *
  5. * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Description:
  12. *
  13. * When nand_scan_bbt is called, then it tries to find the bad block table
  14. * depending on the options in the BBT descriptor(s). If no flash based BBT
  15. * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
  16. * marked good / bad blocks. This information is used to create a memory BBT.
  17. * Once a new bad block is discovered then the "factory" information is updated
  18. * on the device.
  19. * If a flash based BBT is specified then the function first tries to find the
  20. * BBT on flash. If a BBT is found then the contents are read and the memory
  21. * based BBT is created. If a mirrored BBT is selected then the mirror is
  22. * searched too and the versions are compared. If the mirror has a greater
  23. * version number, then the mirror BBT is used to build the memory based BBT.
  24. * If the tables are not versioned, then we "or" the bad block information.
  25. * If one of the BBTs is out of date or does not exist it is (re)created.
  26. * If no BBT exists at all then the device is scanned for factory marked
  27. * good / bad blocks and the bad block tables are created.
  28. *
  29. * For manufacturer created BBTs like the one found on M-SYS DOC devices
  30. * the BBT is searched and read but never created
  31. *
  32. * The auto generated bad block table is located in the last good blocks
  33. * of the device. The table is mirrored, so it can be updated eventually.
  34. * The table is marked in the OOB area with an ident pattern and a version
  35. * number which indicates which of both tables is more up to date. If the NAND
  36. * controller needs the complete OOB area for the ECC information then the
  37. * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
  38. * course): it moves the ident pattern and the version byte into the data area
  39. * and the OOB area will remain untouched.
  40. *
  41. * The table uses 2 bits per block
  42. * 11b: block is good
  43. * 00b: block is factory marked bad
  44. * 01b, 10b: block is marked bad due to wear
  45. *
  46. * The memory bad block table uses the following scheme:
  47. * 00b: block is good
  48. * 01b: block is marked bad due to wear
  49. * 10b: block is reserved (to protect the bbt area)
  50. * 11b: block is factory marked bad
  51. *
  52. * Multichip devices like DOC store the bad block info per floor.
  53. *
  54. * Following assumptions are made:
  55. * - bbts start at a page boundary, if autolocated on a block boundary
  56. * - the space necessary for a bbt in FLASH does not exceed a block boundary
  57. *
  58. */
  59.  
  60. #include <linux/slab.h>
  61. #include <linux/types.h>
  62. #include <linux/mtd/mtd.h>
  63. #include <linux/mtd/bbm.h>
  64. #include <linux/mtd/rawnand.h>
  65. #include <linux/bitops.h>
  66. #include <linux/delay.h>
  67. #include <linux/vmalloc.h>
  68. #include <linux/export.h>
  69. #include <linux/string.h>
  70.  
  71. #define BBT_BLOCK_GOOD 0x00
  72. #define BBT_BLOCK_WORN 0x01
  73. #define BBT_BLOCK_RESERVED 0x02
  74. #define BBT_BLOCK_FACTORY_BAD 0x03
  75.  
  76. #define BBT_ENTRY_MASK 0x03
  77. #define BBT_ENTRY_SHIFT 2
  78.  
  79. #define CUSTOMIZED_BBT 1
  80. #if CUSTOMIZED_BBT
  81. #define BAD_BLK_OOB_MARK_START 4
  82. #define BAD_BLK_OOB_MARK_END 5
  83. #define BAD_BLK_OOB_MARK_PATT 0xFF
  84.  
  85.  
  86. #include <linux/mtd/rawnand.h>
  87. #include <linux/of.h>
  88.  
  89. static bool of_get_customized_bbt_from_mtd(struct mtd_info *mtd)
  90. {
  91. struct nand_chip *chip = mtd_to_nand(mtd);
  92. struct device_node *dn = nand_get_flash_node(chip);
  93. return of_property_read_bool(dn, "customized-samsung-K9F4G08U0x");
  94. }
  95.  
  96. static bool of_get_customized_bbt_from_chip(struct nand_chip *chip)
  97. {
  98. struct device_node *dn = nand_get_flash_node(chip);
  99. return of_property_read_bool(dn, "customized-samsung-K9F4G08U0x");
  100. }
  101.  
  102. #endif
  103.  
  104.  
  105. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);
  106.  
  107. static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
  108. {
  109. uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
  110. entry >>= (block & BBT_ENTRY_MASK) * 2;
  111. return entry & BBT_ENTRY_MASK;
  112. }
  113.  
  114. static inline void bbt_mark_entry(struct nand_chip *chip, int block,
  115. uint8_t mark)
  116. {
  117. uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
  118. chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
  119. }
  120.  
  121. static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
  122. {
  123. if (memcmp(buf, td->pattern, td->len))
  124. return -1;
  125. return 0;
  126. }
  127.  
  128. /**
  129. * check_pattern - [GENERIC] check if a pattern is in the buffer
  130. * @buf: the buffer to search
  131. * @len: the length of buffer to search
  132. * @paglen: the pagelength
  133. * @td: search pattern descriptor
  134. *
  135. * Check for a pattern at the given place. Used to search bad block tables and
  136. * good / bad block identifiers.
  137. */
  138. #if CUSTOMIZED_BBT
  139. static int check_pattern(struct mtd_info *mtd, uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
  140. {
  141. int i;
  142. uint8_t *p = buf;
  143. #else
  144. static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
  145. {
  146. #endif
  147. if (td->options & NAND_BBT_NO_OOB)
  148. return check_pattern_no_oob(buf, td);
  149.  
  150. /* Compare the pattern */
  151. if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
  152. return -1;
  153.  
  154. #if CUSTOMIZED_BBT /*ctc*/
  155. if (of_get_customized_bbt_from_mtd(mtd)) {
  156. for (i = BAD_BLK_OOB_MARK_START, p=buf+paglen; i <= BAD_BLK_OOB_MARK_END; i++) {
  157. if (p[i] != BAD_BLK_OOB_MARK_PATT)
  158. return -1;
  159. }
  160. }
  161. #endif
  162.  
  163. return 0;
  164. }
  165.  
  166. /**
  167. * check_short_pattern - [GENERIC] check if a pattern is in the buffer
  168. * @buf: the buffer to search
  169. * @td: search pattern descriptor
  170. *
  171. * Check for a pattern at the given place. Used to search bad block tables and
  172. * good / bad block identifiers. Same as check_pattern, but no optional empty
  173. * check.
  174. */
  175. static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
  176. {
  177. /* Compare the pattern */
  178. if (memcmp(buf + td->offs, td->pattern, td->len))
  179. return -1;
  180. return 0;
  181. }
  182.  
  183. /**
  184. * add_marker_len - compute the length of the marker in data area
  185. * @td: BBT descriptor used for computation
  186. *
  187. * The length will be 0 if the marker is located in OOB area.
  188. */
  189. static u32 add_marker_len(struct nand_bbt_descr *td)
  190. {
  191. u32 len;
  192.  
  193. if (!(td->options & NAND_BBT_NO_OOB))
  194. return 0;
  195.  
  196. len = td->len;
  197. if (td->options & NAND_BBT_VERSION)
  198. len++;
  199. return len;
  200. }
  201.  
  202. /**
  203. * read_bbt - [GENERIC] Read the bad block table starting from page
  204. * @mtd: MTD device structure
  205. * @buf: temporary buffer
  206. * @page: the starting page
  207. * @num: the number of bbt descriptors to read
  208. * @td: the bbt describtion table
  209. * @offs: block number offset in the table
  210. *
  211. * Read the bad block table starting from page.
  212. */
  213. static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
  214. struct nand_bbt_descr *td, int offs)
  215. {
  216. int res, ret = 0, i, j, act = 0;
  217. struct nand_chip *this = mtd_to_nand(mtd);
  218. size_t retlen, len, totlen;
  219. loff_t from;
  220. int bits = td->options & NAND_BBT_NRBITS_MSK;
  221. uint8_t msk = (uint8_t)((1 << bits) - 1);
  222. u32 marker_len;
  223. int reserved_block_code = td->reserved_block_code;
  224.  
  225. totlen = (num * bits) >> 3;
  226. marker_len = add_marker_len(td);
  227. from = ((loff_t)page) << this->page_shift;
  228.  
  229. while (totlen) {
  230. len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
  231. if (marker_len) {
  232. /*
  233. * In case the BBT marker is not in the OOB area it
  234. * will be just in the first page.
  235. */
  236. len -= marker_len;
  237. from += marker_len;
  238. marker_len = 0;
  239. }
  240. res = mtd_read(mtd, from, len, &retlen, buf);
  241. if (res < 0) {
  242. if (mtd_is_eccerr(res)) {
  243. pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
  244. from & ~mtd->writesize);
  245. return res;
  246. } else if (mtd_is_bitflip(res)) {
  247. pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
  248. from & ~mtd->writesize);
  249. ret = res;
  250. } else {
  251. pr_info("nand_bbt: error reading BBT\n");
  252. return res;
  253. }
  254. }
  255.  
  256. /* Analyse data */
  257. for (i = 0; i < len; i++) {
  258. uint8_t dat = buf[i];
  259. for (j = 0; j < 8; j += bits, act++) {
  260. uint8_t tmp = (dat >> j) & msk;
  261. if (tmp == msk)
  262. continue;
  263. if (reserved_block_code && (tmp == reserved_block_code)) {
  264. pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
  265. (loff_t)(offs + act) <<
  266. this->bbt_erase_shift);
  267. bbt_mark_entry(this, offs + act,
  268. BBT_BLOCK_RESERVED);
  269. mtd->ecc_stats.bbtblocks++;
  270. continue;
  271. }
  272. /*
  273. * Leave it for now, if it's matured we can
  274. * move this message to pr_debug.
  275. */
  276. pr_info("nand_read_bbt: bad block at 0x%012llx\n",
  277. (loff_t)(offs + act) <<
  278. this->bbt_erase_shift);
  279. /* Factory marked bad or worn out? */
  280. if (tmp == 0)
  281. bbt_mark_entry(this, offs + act,
  282. BBT_BLOCK_FACTORY_BAD);
  283. else
  284. bbt_mark_entry(this, offs + act,
  285. BBT_BLOCK_WORN);
  286. mtd->ecc_stats.badblocks++;
  287. }
  288. }
  289. totlen -= len;
  290. from += len;
  291. }
  292. return ret;
  293. }
  294.  
  295. /**
  296. * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  297. * @mtd: MTD device structure
  298. * @buf: temporary buffer
  299. * @td: descriptor for the bad block table
  300. * @chip: read the table for a specific chip, -1 read all chips; applies only if
  301. * NAND_BBT_PERCHIP option is set
  302. *
  303. * Read the bad block table for all chips starting at a given page. We assume
  304. * that the bbt bits are in consecutive order.
  305. */
  306. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  307. {
  308. struct nand_chip *this = mtd_to_nand(mtd);
  309. int res = 0, i;
  310.  
  311. if (td->options & NAND_BBT_PERCHIP) {
  312. int offs = 0;
  313. for (i = 0; i < this->numchips; i++) {
  314. if (chip == -1 || chip == i)
  315. res = read_bbt(mtd, buf, td->pages[i],
  316. this->chipsize >> this->bbt_erase_shift,
  317. td, offs);
  318. if (res)
  319. return res;
  320. offs += this->chipsize >> this->bbt_erase_shift;
  321. }
  322. } else {
  323. res = read_bbt(mtd, buf, td->pages[0],
  324. mtd->size >> this->bbt_erase_shift, td, 0);
  325. if (res)
  326. return res;
  327. }
  328. return 0;
  329. }
  330.  
  331. /* BBT marker is in the first page, no OOB */
  332. static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  333. struct nand_bbt_descr *td)
  334. {
  335. size_t retlen;
  336. size_t len;
  337.  
  338. len = td->len;
  339. if (td->options & NAND_BBT_VERSION)
  340. len++;
  341.  
  342. return mtd_read(mtd, offs, len, &retlen, buf);
  343. }
  344.  
  345. /**
  346. * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
  347. * @mtd: MTD device structure
  348. * @buf: temporary buffer
  349. * @offs: offset at which to scan
  350. * @len: length of data region to read
  351. *
  352. * Scan read data from data+OOB. May traverse multiple pages, interleaving
  353. * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
  354. * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
  355. */
  356. static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  357. size_t len)
  358. {
  359. struct mtd_oob_ops ops;
  360. int res, ret = 0;
  361.  
  362. ops.mode = MTD_OPS_PLACE_OOB;
  363. ops.ooboffs = 0;
  364. ops.ooblen = mtd->oobsize;
  365.  
  366. while (len > 0) {
  367. ops.datbuf = buf;
  368. ops.len = min(len, (size_t)mtd->writesize);
  369. ops.oobbuf = buf + ops.len;
  370.  
  371. res = mtd_read_oob(mtd, offs, &ops);
  372. if (res) {
  373. if (!mtd_is_bitflip_or_eccerr(res))
  374. return res;
  375. else if (mtd_is_eccerr(res) || !ret)
  376. ret = res;
  377. }
  378.  
  379. buf += mtd->oobsize + mtd->writesize;
  380. len -= mtd->writesize;
  381. offs += mtd->writesize;
  382. }
  383. return ret;
  384. }
  385.  
  386. static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  387. size_t len, struct nand_bbt_descr *td)
  388. {
  389. if (td->options & NAND_BBT_NO_OOB)
  390. return scan_read_data(mtd, buf, offs, td);
  391. else
  392. return scan_read_oob(mtd, buf, offs, len);
  393. }
  394.  
  395. /* Scan write data with oob to flash */
  396. static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
  397. uint8_t *buf, uint8_t *oob)
  398. {
  399. struct mtd_oob_ops ops;
  400.  
  401. ops.mode = MTD_OPS_PLACE_OOB;
  402. ops.ooboffs = 0;
  403. ops.ooblen = mtd->oobsize;
  404. ops.datbuf = buf;
  405. ops.oobbuf = oob;
  406. ops.len = len;
  407.  
  408. return mtd_write_oob(mtd, offs, &ops);
  409. }
  410.  
  411. static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
  412. {
  413. u32 ver_offs = td->veroffs;
  414.  
  415. if (!(td->options & NAND_BBT_NO_OOB))
  416. ver_offs += mtd->writesize;
  417. return ver_offs;
  418. }
  419.  
  420. /**
  421. * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
  422. * @mtd: MTD device structure
  423. * @buf: temporary buffer
  424. * @td: descriptor for the bad block table
  425. * @md: descriptor for the bad block table mirror
  426. *
  427. * Read the bad block table(s) for all chips starting at a given page. We
  428. * assume that the bbt bits are in consecutive order.
  429. */
  430. static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
  431. struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  432. {
  433. struct nand_chip *this = mtd_to_nand(mtd);
  434.  
  435. /* Read the primary version, if available */
  436. if (td->options & NAND_BBT_VERSION) {
  437. scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
  438. mtd->writesize, td);
  439. td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
  440. pr_info("Bad block table at page %d, version 0x%02X\n",
  441. td->pages[0], td->version[0]);
  442. }
  443.  
  444. /* Read the mirror version, if available */
  445. if (md && (md->options & NAND_BBT_VERSION)) {
  446. scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
  447. mtd->writesize, md);
  448. md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
  449. pr_info("Bad block table at page %d, version 0x%02X\n",
  450. md->pages[0], md->version[0]);
  451. }
  452. }
  453.  
  454. /* Scan a given block partially */
  455. static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
  456. loff_t offs, uint8_t *buf, int numpages)
  457. {
  458. struct mtd_oob_ops ops;
  459. int j, ret;
  460.  
  461. ops.ooblen = mtd->oobsize;
  462. ops.oobbuf = buf;
  463. ops.ooboffs = 0;
  464. ops.datbuf = NULL;
  465. ops.mode = MTD_OPS_PLACE_OOB;
  466.  
  467. for (j = 0; j < numpages; j++) {
  468. /*
  469. * Read the full oob until read_oob is fixed to handle single
  470. * byte reads for 16 bit buswidth.
  471. */
  472. ret = mtd_read_oob(mtd, offs, &ops);
  473. /* Ignore ECC errors when checking for BBM */
  474. if (ret && !mtd_is_bitflip_or_eccerr(ret))
  475. return ret;
  476.  
  477. if (check_short_pattern(buf, bd))
  478. return 1;
  479.  
  480. offs += mtd->writesize;
  481. }
  482. return 0;
  483. }
  484.  
  485. /**
  486. * create_bbt - [GENERIC] Create a bad block table by scanning the device
  487. * @mtd: MTD device structure
  488. * @buf: temporary buffer
  489. * @bd: descriptor for the good/bad block search pattern
  490. * @chip: create the table for a specific chip, -1 read all chips; applies only
  491. * if NAND_BBT_PERCHIP option is set
  492. *
  493. * Create a bad block table by scanning the device for the given good/bad block
  494. * identify pattern.
  495. */
  496. static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
  497. struct nand_bbt_descr *bd, int chip)
  498. {
  499. struct nand_chip *this = mtd_to_nand(mtd);
  500. int i, numblocks, numpages;
  501. int startblock;
  502. loff_t from;
  503.  
  504. pr_info("Scanning device for bad blocks\n");
  505.  
  506. if (bd->options & NAND_BBT_SCAN2NDPAGE)
  507. numpages = 2;
  508. else
  509. numpages = 1;
  510.  
  511. if (chip == -1) {
  512. numblocks = mtd->size >> this->bbt_erase_shift;
  513. startblock = 0;
  514. from = 0;
  515. } else {
  516. if (chip >= this->numchips) {
  517. pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
  518. chip + 1, this->numchips);
  519. return -EINVAL;
  520. }
  521. numblocks = this->chipsize >> this->bbt_erase_shift;
  522. startblock = chip * numblocks;
  523. numblocks += startblock;
  524. from = (loff_t)startblock << this->bbt_erase_shift;
  525. }
  526.  
  527. if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
  528. from += mtd->erasesize - (mtd->writesize * numpages);
  529.  
  530. for (i = startblock; i < numblocks; i++) {
  531. int ret;
  532.  
  533. BUG_ON(bd->options & NAND_BBT_NO_OOB);
  534.  
  535. ret = scan_block_fast(mtd, bd, from, buf, numpages);
  536. if (ret < 0)
  537. return ret;
  538.  
  539. if (ret) {
  540. bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
  541. pr_warn("Bad eraseblock %d at 0x%012llx\n",
  542. i, (unsigned long long)from);
  543. mtd->ecc_stats.badblocks++;
  544. }
  545.  
  546. from += (1 << this->bbt_erase_shift);
  547. }
  548. return 0;
  549. }
  550.  
  551. /**
  552. * search_bbt - [GENERIC] scan the device for a specific bad block table
  553. * @mtd: MTD device structure
  554. * @buf: temporary buffer
  555. * @td: descriptor for the bad block table
  556. *
  557. * Read the bad block table by searching for a given ident pattern. Search is
  558. * preformed either from the beginning up or from the end of the device
  559. * downwards. The search starts always at the start of a block. If the option
  560. * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
  561. * the bad block information of this chip. This is necessary to provide support
  562. * for certain DOC devices.
  563. *
  564. * The bbt ident pattern resides in the oob area of the first page in a block.
  565. */
  566. static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  567. {
  568. struct nand_chip *this = mtd_to_nand(mtd);
  569. int i, chips;
  570. int startblock, block, dir;
  571. int scanlen = mtd->writesize + mtd->oobsize;
  572. int bbtblocks;
  573. int blocktopage = this->bbt_erase_shift - this->page_shift;
  574.  
  575. /* Search direction top -> down? */
  576. if (td->options & NAND_BBT_LASTBLOCK) {
  577. startblock = (mtd->size >> this->bbt_erase_shift) - 1;
  578. dir = -1;
  579. } else {
  580. startblock = 0;
  581. dir = 1;
  582. }
  583.  
  584. /* Do we have a bbt per chip? */
  585. if (td->options & NAND_BBT_PERCHIP) {
  586. chips = this->numchips;
  587. bbtblocks = this->chipsize >> this->bbt_erase_shift;
  588. startblock &= bbtblocks - 1;
  589. } else {
  590. chips = 1;
  591. bbtblocks = mtd->size >> this->bbt_erase_shift;
  592. }
  593.  
  594. for (i = 0; i < chips; i++) {
  595. /* Reset version information */
  596. td->version[i] = 0;
  597. td->pages[i] = -1;
  598. /* Scan the maximum number of blocks */
  599. for (block = 0; block < td->maxblocks; block++) {
  600.  
  601. int actblock = startblock + dir * block;
  602. loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
  603.  
  604. /* Read first page */
  605. scan_read(mtd, buf, offs, mtd->writesize, td);
  606. #if CUSTOMIZED_BBT
  607. if (!check_pattern(mtd, buf, scanlen, mtd->writesize, td)) {
  608. #else
  609. if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
  610. #endif
  611. td->pages[i] = actblock << blocktopage;
  612. if (td->options & NAND_BBT_VERSION) {
  613. offs = bbt_get_ver_offs(mtd, td);
  614. td->version[i] = buf[offs];
  615. }
  616. break;
  617. }
  618. }
  619. startblock += this->chipsize >> this->bbt_erase_shift;
  620. }
  621. /* Check, if we found a bbt for each requested chip */
  622. for (i = 0; i < chips; i++) {
  623. if (td->pages[i] == -1)
  624. pr_warn("Bad block table not found for chip %d\n", i);
  625. else
  626. pr_info("Bad block table found at page %d, version 0x%02X\n",
  627. td->pages[i], td->version[i]);
  628. }
  629. return 0;
  630. }
  631.  
  632. /**
  633. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  634. * @mtd: MTD device structure
  635. * @buf: temporary buffer
  636. * @td: descriptor for the bad block table
  637. * @md: descriptor for the bad block table mirror
  638. *
  639. * Search and read the bad block table(s).
  640. */
  641. static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
  642. struct nand_bbt_descr *td,
  643. struct nand_bbt_descr *md)
  644. {
  645. /* Search the primary table */
  646. search_bbt(mtd, buf, td);
  647.  
  648. /* Search the mirror table */
  649. if (md)
  650. search_bbt(mtd, buf, md);
  651. }
  652.  
  653. /**
  654. * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
  655. * @this: the NAND device
  656. * @td: the BBT description
  657. * @md: the mirror BBT descriptor
  658. * @chip: the CHIP selector
  659. *
  660. * This functions returns a positive block number pointing a valid eraseblock
  661. * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
  662. * all blocks are already used of marked bad. If td->pages[chip] was already
  663. * pointing to a valid block we re-use it, otherwise we search for the next
  664. * valid one.
  665. */
  666. static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
  667. struct nand_bbt_descr *md, int chip)
  668. {
  669. int startblock, dir, page, numblocks, i;
  670.  
  671. /*
  672. * There was already a version of the table, reuse the page. This
  673. * applies for absolute placement too, as we have the page number in
  674. * td->pages.
  675. */
  676. if (td->pages[chip] != -1)
  677. return td->pages[chip] >>
  678. (this->bbt_erase_shift - this->page_shift);
  679.  
  680. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  681. if (!(td->options & NAND_BBT_PERCHIP))
  682. numblocks *= this->numchips;
  683.  
  684. /*
  685. * Automatic placement of the bad block table. Search direction
  686. * top -> down?
  687. */
  688. if (td->options & NAND_BBT_LASTBLOCK) {
  689. startblock = numblocks * (chip + 1) - 1;
  690. dir = -1;
  691. } else {
  692. startblock = chip * numblocks;
  693. dir = 1;
  694. }
  695.  
  696. for (i = 0; i < td->maxblocks; i++) {
  697. int block = startblock + dir * i;
  698.  
  699. /* Check, if the block is bad */
  700. switch (bbt_get_entry(this, block)) {
  701. case BBT_BLOCK_WORN:
  702. case BBT_BLOCK_FACTORY_BAD:
  703. continue;
  704. }
  705.  
  706. page = block << (this->bbt_erase_shift - this->page_shift);
  707.  
  708. /* Check, if the block is used by the mirror table */
  709. if (!md || md->pages[chip] != page)
  710. return block;
  711. }
  712.  
  713. return -ENOSPC;
  714. }
  715.  
  716. /**
  717. * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
  718. * @this: the NAND device
  719. * @td: the BBT description
  720. * @chip: the CHIP selector
  721. * @block: the BBT block to mark
  722. *
  723. * Blocks reserved for BBT can become bad. This functions is an helper to mark
  724. * such blocks as bad. It takes care of updating the in-memory BBT, marking the
  725. * block as bad using a bad block marker and invalidating the associated
  726. * td->pages[] entry.
  727. */
  728. static void mark_bbt_block_bad(struct nand_chip *this,
  729. struct nand_bbt_descr *td,
  730. int chip, int block)
  731. {
  732. struct mtd_info *mtd = nand_to_mtd(this);
  733. loff_t to;
  734. int res;
  735.  
  736. bbt_mark_entry(this, block, BBT_BLOCK_WORN);
  737.  
  738. to = (loff_t)block << this->bbt_erase_shift;
  739. res = this->block_markbad(mtd, to);
  740. if (res)
  741. pr_warn("nand_bbt: error %d while marking block %d bad\n",
  742. res, block);
  743.  
  744. td->pages[chip] = -1;
  745. }
  746.  
  747. /**
  748. * write_bbt - [GENERIC] (Re)write the bad block table
  749. * @mtd: MTD device structure
  750. * @buf: temporary buffer
  751. * @td: descriptor for the bad block table
  752. * @md: descriptor for the bad block table mirror
  753. * @chipsel: selector for a specific chip, -1 for all
  754. *
  755. * (Re)write the bad block table.
  756. */
  757. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  758. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  759. int chipsel)
  760. {
  761. struct nand_chip *this = mtd_to_nand(mtd);
  762. struct erase_info einfo;
  763. int i, res, chip = 0;
  764. int bits, page, offs, numblocks, sft, sftmsk;
  765. int nrchips, pageoffs, ooboffs;
  766. uint8_t msk[4];
  767. uint8_t rcode = td->reserved_block_code;
  768. size_t retlen, len = 0;
  769. loff_t to;
  770. struct mtd_oob_ops ops;
  771.  
  772. ops.ooblen = mtd->oobsize;
  773. ops.ooboffs = 0;
  774. ops.datbuf = NULL;
  775. ops.mode = MTD_OPS_PLACE_OOB;
  776.  
  777. if (!rcode)
  778. rcode = 0xff;
  779. /* Write bad block table per chip rather than per device? */
  780. if (td->options & NAND_BBT_PERCHIP) {
  781. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  782. /* Full device write or specific chip? */
  783. if (chipsel == -1) {
  784. nrchips = this->numchips;
  785. } else {
  786. nrchips = chipsel + 1;
  787. chip = chipsel;
  788. }
  789. } else {
  790. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  791. nrchips = 1;
  792. }
  793.  
  794. /* Loop through the chips */
  795. while (chip < nrchips) {
  796. int block;
  797.  
  798. block = get_bbt_block(this, td, md, chip);
  799. if (block < 0) {
  800. pr_err("No space left to write bad block table\n");
  801. res = block;
  802. goto outerr;
  803. }
  804.  
  805. /*
  806. * get_bbt_block() returns a block number, shift the value to
  807. * get a page number.
  808. */
  809. page = block << (this->bbt_erase_shift - this->page_shift);
  810.  
  811. /* Set up shift count and masks for the flash table */
  812. bits = td->options & NAND_BBT_NRBITS_MSK;
  813. msk[2] = ~rcode;
  814. switch (bits) {
  815. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  816. msk[3] = 0x01;
  817. break;
  818. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  819. msk[3] = 0x03;
  820. break;
  821. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  822. msk[3] = 0x0f;
  823. break;
  824. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  825. msk[3] = 0xff;
  826. break;
  827. default: return -EINVAL;
  828. }
  829.  
  830. to = ((loff_t)page) << this->page_shift;
  831.  
  832. /* Must we save the block contents? */
  833. if (td->options & NAND_BBT_SAVECONTENT) {
  834. /* Make it block aligned */
  835. to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
  836. len = 1 << this->bbt_erase_shift;
  837. res = mtd_read(mtd, to, len, &retlen, buf);
  838. if (res < 0) {
  839. if (retlen != len) {
  840. pr_info("nand_bbt: error reading block for writing the bad block table\n");
  841. return res;
  842. }
  843. pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
  844. }
  845. /* Read oob data */
  846. ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
  847. ops.oobbuf = &buf[len];
  848. res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
  849. if (res < 0 || ops.oobretlen != ops.ooblen)
  850. goto outerr;
  851.  
  852. /* Calc the byte offset in the buffer */
  853. pageoffs = page - (int)(to >> this->page_shift);
  854. offs = pageoffs << this->page_shift;
  855. /* Preset the bbt area with 0xff */
  856. memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
  857. ooboffs = len + (pageoffs * mtd->oobsize);
  858.  
  859. } else if (td->options & NAND_BBT_NO_OOB) {
  860. ooboffs = 0;
  861. offs = td->len;
  862. /* The version byte */
  863. if (td->options & NAND_BBT_VERSION)
  864. offs++;
  865. /* Calc length */
  866. len = (size_t)(numblocks >> sft);
  867. len += offs;
  868. /* Make it page aligned! */
  869. len = ALIGN(len, mtd->writesize);
  870. /* Preset the buffer with 0xff */
  871. memset(buf, 0xff, len);
  872. /* Pattern is located at the begin of first page */
  873. memcpy(buf, td->pattern, td->len);
  874. } else {
  875. /* Calc length */
  876. len = (size_t)(numblocks >> sft);
  877. /* Make it page aligned! */
  878. len = ALIGN(len, mtd->writesize);
  879. /* Preset the buffer with 0xff */
  880. memset(buf, 0xff, len +
  881. (len >> this->page_shift)* mtd->oobsize);
  882. offs = 0;
  883. ooboffs = len;
  884. /* Pattern is located in oob area of first page */
  885. memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
  886. }
  887.  
  888. if (td->options & NAND_BBT_VERSION)
  889. buf[ooboffs + td->veroffs] = td->version[chip];
  890.  
  891. /* Walk through the memory table */
  892. for (i = 0; i < numblocks; i++) {
  893. uint8_t dat;
  894. int sftcnt = (i << (3 - sft)) & sftmsk;
  895. dat = bbt_get_entry(this, chip * numblocks + i);
  896. /* Do not store the reserved bbt blocks! */
  897. buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
  898. }
  899.  
  900. memset(&einfo, 0, sizeof(einfo));
  901. einfo.mtd = mtd;
  902. einfo.addr = to;
  903. einfo.len = 1 << this->bbt_erase_shift;
  904. res = nand_erase_nand(mtd, &einfo, 1);
  905. if (res < 0) {
  906. pr_warn("nand_bbt: error while erasing BBT block %d\n",
  907. res);
  908. mark_bbt_block_bad(this, td, chip, block);
  909. continue;
  910. }
  911.  
  912. res = scan_write_bbt(mtd, to, len, buf,
  913. td->options & NAND_BBT_NO_OOB ? NULL :
  914. &buf[len]);
  915. if (res < 0) {
  916. pr_warn("nand_bbt: error while writing BBT block %d\n",
  917. res);
  918. mark_bbt_block_bad(this, td, chip, block);
  919. continue;
  920. }
  921.  
  922. pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
  923. (unsigned long long)to, td->version[chip]);
  924.  
  925. /* Mark it as used */
  926. td->pages[chip++] = page;
  927. }
  928. return 0;
  929.  
  930. outerr:
  931. pr_warn("nand_bbt: error while writing bad block table %d\n", res);
  932. return res;
  933. }
  934.  
  935. /**
  936. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  937. * @mtd: MTD device structure
  938. * @bd: descriptor for the good/bad block search pattern
  939. *
  940. * The function creates a memory based bbt by scanning the device for
  941. * manufacturer / software marked good / bad blocks.
  942. */
  943. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  944. {
  945. struct nand_chip *this = mtd_to_nand(mtd);
  946.  
  947. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  948. }
  949.  
  950. /**
  951. * check_create - [GENERIC] create and write bbt(s) if necessary
  952. * @mtd: MTD device structure
  953. * @buf: temporary buffer
  954. * @bd: descriptor for the good/bad block search pattern
  955. *
  956. * The function checks the results of the previous call to read_bbt and creates
  957. * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
  958. * for the chip/device. Update is necessary if one of the tables is missing or
  959. * the version nr. of one table is less than the other.
  960. */
  961. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  962. {
  963. int i, chips, writeops, create, chipsel, res, res2;
  964. struct nand_chip *this = mtd_to_nand(mtd);
  965. struct nand_bbt_descr *td = this->bbt_td;
  966. struct nand_bbt_descr *md = this->bbt_md;
  967. struct nand_bbt_descr *rd, *rd2;
  968.  
  969. /* Do we have a bbt per chip? */
  970. if (td->options & NAND_BBT_PERCHIP)
  971. chips = this->numchips;
  972. else
  973. chips = 1;
  974.  
  975. for (i = 0; i < chips; i++) {
  976. writeops = 0;
  977. create = 0;
  978. rd = NULL;
  979. rd2 = NULL;
  980. res = res2 = 0;
  981. /* Per chip or per device? */
  982. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  983. /* Mirrored table available? */
  984. if (md) {
  985. if (td->pages[i] == -1 && md->pages[i] == -1) {
  986. create = 1;
  987. writeops = 0x03;
  988. } else if (td->pages[i] == -1) {
  989. rd = md;
  990. writeops = 0x01;
  991. } else if (md->pages[i] == -1) {
  992. rd = td;
  993. writeops = 0x02;
  994. } else if (td->version[i] == md->version[i]) {
  995. rd = td;
  996. if (!(td->options & NAND_BBT_VERSION))
  997. rd2 = md;
  998. } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
  999. rd = td;
  1000. writeops = 0x02;
  1001. } else {
  1002. rd = md;
  1003. writeops = 0x01;
  1004. }
  1005. } else {
  1006. if (td->pages[i] == -1) {
  1007. create = 1;
  1008. writeops = 0x01;
  1009. } else {
  1010. rd = td;
  1011. }
  1012. }
  1013.  
  1014. if (create) {
  1015. /* Create the bad block table by scanning the device? */
  1016. if (!(td->options & NAND_BBT_CREATE))
  1017. continue;
  1018.  
  1019. /* Create the table in memory by scanning the chip(s) */
  1020. if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
  1021. create_bbt(mtd, buf, bd, chipsel);
  1022.  
  1023. td->version[i] = 1;
  1024. if (md)
  1025. md->version[i] = 1;
  1026. }
  1027.  
  1028. /* Read back first? */
  1029. if (rd) {
  1030. res = read_abs_bbt(mtd, buf, rd, chipsel);
  1031. if (mtd_is_eccerr(res)) {
  1032. /* Mark table as invalid */
  1033. rd->pages[i] = -1;
  1034. rd->version[i] = 0;
  1035. i--;
  1036. continue;
  1037. }
  1038. }
  1039. /* If they weren't versioned, read both */
  1040. if (rd2) {
  1041. res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
  1042. if (mtd_is_eccerr(res2)) {
  1043. /* Mark table as invalid */
  1044. rd2->pages[i] = -1;
  1045. rd2->version[i] = 0;
  1046. i--;
  1047. continue;
  1048. }
  1049. }
  1050.  
  1051. /* Scrub the flash table(s)? */
  1052. if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
  1053. writeops = 0x03;
  1054.  
  1055. /* Update version numbers before writing */
  1056. if (md) {
  1057. td->version[i] = max(td->version[i], md->version[i]);
  1058. md->version[i] = td->version[i];
  1059. }
  1060.  
  1061. /* Write the bad block table to the device? */
  1062. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  1063. res = write_bbt(mtd, buf, td, md, chipsel);
  1064. if (res < 0)
  1065. return res;
  1066. }
  1067.  
  1068. /* Write the mirror bad block table to the device? */
  1069. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  1070. res = write_bbt(mtd, buf, md, td, chipsel);
  1071. if (res < 0)
  1072. return res;
  1073. }
  1074. }
  1075. return 0;
  1076. }
  1077.  
  1078. /**
  1079. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  1080. * @mtd: MTD device structure
  1081. * @td: bad block table descriptor
  1082. *
  1083. * The bad block table regions are marked as "bad" to prevent accidental
  1084. * erasures / writes. The regions are identified by the mark 0x02.
  1085. */
  1086. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  1087. {
  1088. struct nand_chip *this = mtd_to_nand(mtd);
  1089. int i, j, chips, block, nrblocks, update;
  1090. uint8_t oldval;
  1091.  
  1092. /* Do we have a bbt per chip? */
  1093. if (td->options & NAND_BBT_PERCHIP) {
  1094. chips = this->numchips;
  1095. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  1096. } else {
  1097. chips = 1;
  1098. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  1099. }
  1100.  
  1101. for (i = 0; i < chips; i++) {
  1102. if ((td->options & NAND_BBT_ABSPAGE) ||
  1103. !(td->options & NAND_BBT_WRITE)) {
  1104. if (td->pages[i] == -1)
  1105. continue;
  1106. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  1107. oldval = bbt_get_entry(this, block);
  1108. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  1109. if ((oldval != BBT_BLOCK_RESERVED) &&
  1110. td->reserved_block_code)
  1111. nand_update_bbt(mtd, (loff_t)block <<
  1112. this->bbt_erase_shift);
  1113. continue;
  1114. }
  1115. update = 0;
  1116. if (td->options & NAND_BBT_LASTBLOCK)
  1117. block = ((i + 1) * nrblocks) - td->maxblocks;
  1118. else
  1119. block = i * nrblocks;
  1120. for (j = 0; j < td->maxblocks; j++) {
  1121. oldval = bbt_get_entry(this, block);
  1122. bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
  1123. if (oldval != BBT_BLOCK_RESERVED)
  1124. update = 1;
  1125. block++;
  1126. }
  1127. /*
  1128. * If we want reserved blocks to be recorded to flash, and some
  1129. * new ones have been marked, then we need to update the stored
  1130. * bbts. This should only happen once.
  1131. */
  1132. if (update && td->reserved_block_code)
  1133. nand_update_bbt(mtd, (loff_t)(block - 1) <<
  1134. this->bbt_erase_shift);
  1135. }
  1136. }
  1137.  
  1138. /**
  1139. * verify_bbt_descr - verify the bad block description
  1140. * @mtd: MTD device structure
  1141. * @bd: the table to verify
  1142. *
  1143. * This functions performs a few sanity checks on the bad block description
  1144. * table.
  1145. */
  1146. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  1147. {
  1148. struct nand_chip *this = mtd_to_nand(mtd);
  1149. u32 pattern_len;
  1150. u32 bits;
  1151. u32 table_size;
  1152.  
  1153. if (!bd)
  1154. return;
  1155.  
  1156. pattern_len = bd->len;
  1157. bits = bd->options & NAND_BBT_NRBITS_MSK;
  1158.  
  1159. BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
  1160. !(this->bbt_options & NAND_BBT_USE_FLASH));
  1161. BUG_ON(!bits);
  1162.  
  1163. if (bd->options & NAND_BBT_VERSION)
  1164. pattern_len++;
  1165.  
  1166. if (bd->options & NAND_BBT_NO_OOB) {
  1167. BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
  1168. BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
  1169. BUG_ON(bd->offs);
  1170. if (bd->options & NAND_BBT_VERSION)
  1171. BUG_ON(bd->veroffs != bd->len);
  1172. BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
  1173. }
  1174.  
  1175. if (bd->options & NAND_BBT_PERCHIP)
  1176. table_size = this->chipsize >> this->bbt_erase_shift;
  1177. else
  1178. table_size = mtd->size >> this->bbt_erase_shift;
  1179. table_size >>= 3;
  1180. table_size *= bits;
  1181. if (bd->options & NAND_BBT_NO_OOB)
  1182. table_size += pattern_len;
  1183. BUG_ON(table_size > (1 << this->bbt_erase_shift));
  1184. }
  1185.  
  1186. /**
  1187. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  1188. * @mtd: MTD device structure
  1189. * @bd: descriptor for the good/bad block search pattern
  1190. *
  1191. * The function checks, if a bad block table(s) is/are already available. If
  1192. * not it scans the device for manufacturer marked good / bad blocks and writes
  1193. * the bad block table(s) to the selected place.
  1194. *
  1195. * The bad block table memory is allocated here. It must be freed by calling
  1196. * the nand_free_bbt function.
  1197. */
  1198. static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  1199. {
  1200. struct nand_chip *this = mtd_to_nand(mtd);
  1201. int len, res;
  1202. uint8_t *buf;
  1203. struct nand_bbt_descr *td = this->bbt_td;
  1204. struct nand_bbt_descr *md = this->bbt_md;
  1205.  
  1206. len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
  1207. /*
  1208. * Allocate memory (2bit per block) and clear the memory bad block
  1209. * table.
  1210. */
  1211. this->bbt = kzalloc(len, GFP_KERNEL);
  1212. if (!this->bbt)
  1213. return -ENOMEM;
  1214.  
  1215. /*
  1216. * If no primary table decriptor is given, scan the device to build a
  1217. * memory based bad block table.
  1218. */
  1219. if (!td) {
  1220. if ((res = nand_memory_bbt(mtd, bd))) {
  1221. pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
  1222. goto err;
  1223. }
  1224. return 0;
  1225. }
  1226. verify_bbt_descr(mtd, td);
  1227. verify_bbt_descr(mtd, md);
  1228.  
  1229. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1230. len = (1 << this->bbt_erase_shift);
  1231. len += (len >> this->page_shift) * mtd->oobsize;
  1232. buf = vmalloc(len);
  1233. if (!buf) {
  1234. res = -ENOMEM;
  1235. goto err;
  1236. }
  1237.  
  1238. /* Is the bbt at a given page? */
  1239. if (td->options & NAND_BBT_ABSPAGE) {
  1240. read_abs_bbts(mtd, buf, td, md);
  1241. } else {
  1242. /* Search the bad block table using a pattern in oob */
  1243. search_read_bbts(mtd, buf, td, md);
  1244. }
  1245.  
  1246. res = check_create(mtd, buf, bd);
  1247. if (res)
  1248. goto err;
  1249.  
  1250. /* Prevent the bbt regions from erasing / writing */
  1251. mark_bbt_region(mtd, td);
  1252. if (md)
  1253. mark_bbt_region(mtd, md);
  1254.  
  1255. vfree(buf);
  1256. return 0;
  1257.  
  1258. err:
  1259. kfree(this->bbt);
  1260. this->bbt = NULL;
  1261. return res;
  1262. }
  1263.  
  1264. /**
  1265. * nand_update_bbt - update bad block table(s)
  1266. * @mtd: MTD device structure
  1267. * @offs: the offset of the newly marked block
  1268. *
  1269. * The function updates the bad block table(s).
  1270. */
  1271. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  1272. {
  1273. struct nand_chip *this = mtd_to_nand(mtd);
  1274. int len, res = 0;
  1275. int chip, chipsel;
  1276. uint8_t *buf;
  1277. struct nand_bbt_descr *td = this->bbt_td;
  1278. struct nand_bbt_descr *md = this->bbt_md;
  1279.  
  1280. if (!this->bbt || !td)
  1281. return -EINVAL;
  1282.  
  1283. /* Allocate a temporary buffer for one eraseblock incl. oob */
  1284. len = (1 << this->bbt_erase_shift);
  1285. len += (len >> this->page_shift) * mtd->oobsize;
  1286. buf = kmalloc(len, GFP_KERNEL);
  1287. if (!buf)
  1288. return -ENOMEM;
  1289.  
  1290. /* Do we have a bbt per chip? */
  1291. if (td->options & NAND_BBT_PERCHIP) {
  1292. chip = (int)(offs >> this->chip_shift);
  1293. chipsel = chip;
  1294. } else {
  1295. chip = 0;
  1296. chipsel = -1;
  1297. }
  1298.  
  1299. td->version[chip]++;
  1300. if (md)
  1301. md->version[chip]++;
  1302.  
  1303. /* Write the bad block table to the device? */
  1304. if (td->options & NAND_BBT_WRITE) {
  1305. res = write_bbt(mtd, buf, td, md, chipsel);
  1306. if (res < 0)
  1307. goto out;
  1308. }
  1309. /* Write the mirror bad block table to the device? */
  1310. if (md && (md->options & NAND_BBT_WRITE)) {
  1311. res = write_bbt(mtd, buf, md, td, chipsel);
  1312. }
  1313.  
  1314. out:
  1315. kfree(buf);
  1316. return res;
  1317. }
  1318.  
  1319. /*
  1320. * Define some generic bad / good block scan pattern which are used
  1321. * while scanning a device for factory marked good / bad blocks.
  1322. */
  1323. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  1324.  
  1325. /* Generic flash bbt descriptors */
  1326. //#if CUSTOMIZED_BBT /*ctc*/
  1327. //static uint8_t bbt_pattern[] = {'A', 'R', 'C', 'A' };
  1328. //static uint8_t mirror_pattern[] = {'a', 'c', 'r', 'a' };
  1329. //#else
  1330. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1331. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1332. //#endif
  1333.  
  1334. static struct nand_bbt_descr bbt_main_descr = {
  1335. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1336. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1337. .offs = 8,
  1338. .len = 4,
  1339. .veroffs = 12,
  1340. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1341. .pattern = bbt_pattern
  1342. };
  1343.  
  1344. static struct nand_bbt_descr bbt_mirror_descr = {
  1345. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1346. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1347. .offs = 8,
  1348. .len = 4,
  1349. .veroffs = 12,
  1350. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1351. .pattern = mirror_pattern
  1352. };
  1353.  
  1354. static struct nand_bbt_descr bbt_main_no_oob_descr = {
  1355. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1356. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1357. | NAND_BBT_NO_OOB,
  1358. .len = 4,
  1359. .veroffs = 4,
  1360. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1361. .pattern = bbt_pattern
  1362. };
  1363.  
  1364. static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
  1365. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1366. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
  1367. | NAND_BBT_NO_OOB,
  1368. .len = 4,
  1369. .veroffs = 4,
  1370. .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
  1371. .pattern = mirror_pattern
  1372. };
  1373.  
  1374. #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
  1375. /**
  1376. * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
  1377. * @this: NAND chip to create descriptor for
  1378. *
  1379. * This function allocates and initializes a nand_bbt_descr for BBM detection
  1380. * based on the properties of @this. The new descriptor is stored in
  1381. * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
  1382. * passed to this function.
  1383. */
  1384. static int nand_create_badblock_pattern(struct nand_chip *this)
  1385. {
  1386. struct nand_bbt_descr *bd;
  1387. if (this->badblock_pattern) {
  1388. pr_warn("Bad block pattern already allocated; not replacing\n");
  1389. return -EINVAL;
  1390. }
  1391. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  1392. if (!bd)
  1393. return -ENOMEM;
  1394.  
  1395. #if CUSTOMIZED_BBT /*ctc*/
  1396. if (of_get_customized_bbt_from_chip(this)) {
  1397. bd->options = 0 & BADBLOCK_SCAN_MASK;
  1398. } else {
  1399. bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
  1400. }
  1401. #else
  1402. bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
  1403. #endif
  1404. bd->offs = this->badblockpos;
  1405. bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1406. bd->pattern = scan_ff_pattern;
  1407. bd->options |= NAND_BBT_DYNAMICSTRUCT;
  1408. this->badblock_pattern = bd;
  1409. return 0;
  1410. }
  1411.  
  1412. /**
  1413. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1414. * @mtd: MTD device structure
  1415. *
  1416. * This function selects the default bad block table support for the device and
  1417. * calls the nand_scan_bbt function.
  1418. */
  1419. int nand_default_bbt(struct mtd_info *mtd)
  1420. {
  1421. struct nand_chip *this = mtd_to_nand(mtd);
  1422. int ret;
  1423. /* change the generic bad / good block scan pattern if of_get_customized_bbt_from_chip(this) true */
  1424. #if CUSTOMIZED_BBT
  1425. if(of_get_customized_bbt_from_chip(this)) {
  1426. bbt_pattern[0] = 'A';
  1427. bbt_pattern[1] = 'R';
  1428. bbt_pattern[2] = 'C';
  1429. bbt_pattern[3] = 'A';
  1430. mirror_pattern[0] = 'a';
  1431. mirror_pattern[1] = 'c';
  1432. mirror_pattern[2] = 'r';
  1433. mirror_pattern[3] = 'a';
  1434. }
  1435. #endif
  1436.  
  1437. /* Is a flash based bad block table requested? */
  1438. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1439. /* Use the default pattern descriptors */
  1440. if (!this->bbt_td) {
  1441. if (this->bbt_options & NAND_BBT_NO_OOB) {
  1442. this->bbt_td = &bbt_main_no_oob_descr;
  1443. this->bbt_md = &bbt_mirror_no_oob_descr;
  1444. } else {
  1445. this->bbt_td = &bbt_main_descr;
  1446. this->bbt_md = &bbt_mirror_descr;
  1447. }
  1448. }
  1449. } else {
  1450. this->bbt_td = NULL;
  1451. this->bbt_md = NULL;
  1452. }
  1453.  
  1454. if (!this->badblock_pattern) {
  1455. ret = nand_create_badblock_pattern(this);
  1456. if (ret)
  1457. return ret;
  1458. }
  1459.  
  1460. return nand_scan_bbt(mtd, this->badblock_pattern);
  1461. }
  1462.  
  1463. /**
  1464. * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
  1465. * @mtd: MTD device structure
  1466. * @offs: offset in the device
  1467. */
  1468. int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
  1469. {
  1470. struct nand_chip *this = mtd_to_nand(mtd);
  1471. int block;
  1472.  
  1473. block = (int)(offs >> this->bbt_erase_shift);
  1474. return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
  1475. }
  1476.  
  1477. /**
  1478. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1479. * @mtd: MTD device structure
  1480. * @offs: offset in the device
  1481. * @allowbbt: allow access to bad block table region
  1482. */
  1483. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1484. {
  1485. struct nand_chip *this = mtd_to_nand(mtd);
  1486. int block, res;
  1487.  
  1488. block = (int)(offs >> this->bbt_erase_shift);
  1489. res = bbt_get_entry(this, block);
  1490.  
  1491. pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
  1492. (unsigned int)offs, block, res);
  1493.  
  1494. switch (res) {
  1495. case BBT_BLOCK_GOOD:
  1496. return 0;
  1497. case BBT_BLOCK_WORN:
  1498. return 1;
  1499. case BBT_BLOCK_RESERVED:
  1500. return allowbbt ? 0 : 1;
  1501. }
  1502. return 1;
  1503. }
  1504.  
  1505. /**
  1506. * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
  1507. * @mtd: MTD device structure
  1508. * @offs: offset of the bad block
  1509. */
  1510. int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
  1511. {
  1512. struct nand_chip *this = mtd_to_nand(mtd);
  1513. int block, ret = 0;
  1514.  
  1515. block = (int)(offs >> this->bbt_erase_shift);
  1516.  
  1517. /* Mark bad block in memory */
  1518. bbt_mark_entry(this, block, BBT_BLOCK_WORN);
  1519.  
  1520. /* Update flash-based bad block table */
  1521. if (this->bbt_options & NAND_BBT_USE_FLASH)
  1522. ret = nand_update_bbt(mtd, offs);
  1523.  
  1524. return ret;
  1525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement