nand_util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * drivers/mtd/nand/nand_util.c
  3. *
  4. * Copyright (C) 2006 by Weiss-Electronic GmbH.
  5. * All rights reserved.
  6. *
  7. * @author: Guido Classen <clagix@gmail.com>
  8. * @descr: NAND Flash support
  9. * @references: borrowed heavily from Linux mtd-utils code:
  10. * flash_eraseall.c by Arcom Control System Ltd
  11. * nandwrite.c by Steven J. Hill (sjhill@realitydiluted.com)
  12. * and Thomas Gleixner (tglx@linutronix.de)
  13. *
  14. * Copyright (C) 2008 Nokia Corporation: drop_ffs() function by
  15. * Artem Bityutskiy <dedekind1@gmail.com> from mtd-utils
  16. *
  17. * See file CREDITS for list of people who contributed to this
  18. * project.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License version
  22. * 2 as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. *
  34. * Copyright 2010 Freescale Semiconductor
  35. * The portions of this file whose copyright is held by Freescale and which
  36. * are not considered a derived work of GPL v2-only code may be distributed
  37. * and/or modified under the terms of the GNU General Public License as
  38. * published by the Free Software Foundation; either version 2 of the
  39. * License, or (at your option) any later version.
  40. */
  41. #include <common.h>
  42. #include <command.h>
  43. #include <watchdog.h>
  44. #include <malloc.h>
  45. #include <div64.h>
  46. #include <asm/errno.h>
  47. #include <linux/mtd/mtd.h>
  48. #include <nand.h>
  49. #include <jffs2/jffs2.h>
  50. typedef struct erase_info erase_info_t;
  51. typedef struct mtd_info mtd_info_t;
  52. /* support only for native endian JFFS2 */
  53. #define cpu_to_je16(x) (x)
  54. #define cpu_to_je32(x) (x)
  55. /**
  56. * nand_erase_opts: - erase NAND flash with support for various options
  57. * (jffs2 formatting)
  58. *
  59. * @param meminfo NAND device to erase
  60. * @param opts options, @see struct nand_erase_options
  61. * @return 0 in case of success
  62. *
  63. * This code is ported from flash_eraseall.c from Linux mtd utils by
  64. * Arcom Control System Ltd.
  65. */
  66. int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
  67. {
  68. struct jffs2_unknown_node cleanmarker;
  69. erase_info_t erase;
  70. unsigned long erase_length, erased_length; /* in blocks */
  71. int bbtest = 1;
  72. int result;
  73. int percent_complete = -1;
  74. const char *mtd_device = meminfo->name;
  75. struct mtd_oob_ops oob_opts;
  76. struct nand_chip *chip = meminfo->priv;
  77. if ((opts->offset & (meminfo->erasesize - 1)) != 0) {
  78. printf("Attempt to erase non block-aligned data\n");
  79. return -1;
  80. }
  81. memset(&erase, 0, sizeof(erase));
  82. memset(&oob_opts, 0, sizeof(oob_opts));
  83. erase.mtd = meminfo;
  84. erase.len = meminfo->erasesize;
  85. erase.addr = opts->offset;
  86. erase_length = lldiv(opts->length + meminfo->erasesize - 1,
  87. meminfo->erasesize);
  88. cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  89. cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
  90. cleanmarker.totlen = cpu_to_je32(8);
  91. /* scrub option allows to erase badblock. To prevent internal
  92. * check from erase() method, set block check method to dummy
  93. * and disable bad block table while erasing.
  94. */
  95. if (opts->scrub) {
  96. erase.scrub = opts->scrub;
  97. /*
  98. * We don't need the bad block table anymore...
  99. * after scrub, there are no bad blocks left!
  100. */
  101. if (chip->bbt) {
  102. kfree(chip->bbt);
  103. }
  104. chip->bbt = NULL;
  105. }
  106. for (erased_length = 0;
  107. erased_length < erase_length;
  108. erase.addr += meminfo->erasesize) {
  109. WATCHDOG_RESET();
  110. if (!opts->scrub && bbtest) {
  111. int ret = meminfo->block_isbad(meminfo, erase.addr);
  112. if (ret > 0) {
  113. if (!opts->quiet)
  114. printf("\rSkipping bad block at "
  115. "0x%08llx "
  116. " \n",
  117. erase.addr);
  118. if (!opts->spread)
  119. erased_length++;
  120. continue;
  121. } else if (ret < 0) {
  122. printf("\n%s: MTD get bad block failed: %d\n",
  123. mtd_device,
  124. ret);
  125. return -1;
  126. }
  127. }
  128. erased_length++;
  129. result = meminfo->erase(meminfo, &erase);
  130. if (result != 0) {
  131. printf("\n%s: MTD Erase failure: %d\n",
  132. mtd_device, result);
  133. continue;
  134. }
  135. /* format for JFFS2 ? */
  136. if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) {
  137. chip->ops.ooblen = 8;
  138. chip->ops.datbuf = NULL;
  139. chip->ops.oobbuf = (uint8_t *)&cleanmarker;
  140. chip->ops.ooboffs = 0;
  141. chip->ops.mode = MTD_OOB_AUTO;
  142. result = meminfo->write_oob(meminfo,
  143. erase.addr,
  144. &chip->ops);
  145. if (result != 0) {
  146. printf("\n%s: MTD writeoob failure: %d\n",
  147. mtd_device, result);
  148. continue;
  149. }
  150. }
  151. if (!opts->quiet) {
  152. unsigned long long n = erased_length * 100ULL;
  153. int percent;
  154. do_div(n, erase_length);
  155. percent = (int)n;
  156. /* output progress message only at whole percent
  157. * steps to reduce the number of messages printed
  158. * on (slow) serial consoles
  159. */
  160. if (percent != percent_complete) {
  161. percent_complete = percent;
  162. printf("\rErasing at 0x%llx -- %3d%% complete.",
  163. erase.addr, percent);
  164. if (opts->jffs2 && result == 0)
  165. printf(" Cleanmarker written at 0x%llx.",
  166. erase.addr);
  167. }
  168. }
  169. }
  170. if (!opts->quiet)
  171. printf("\n");
  172. if (opts->scrub)
  173. chip->scan_bbt(meminfo);
  174. return 0;
  175. }
  176. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  177. /******************************************************************************
  178. * Support for locking / unlocking operations of some NAND devices
  179. *****************************************************************************/
  180. /**
  181. * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
  182. * state
  183. *
  184. * @param mtd nand mtd instance
  185. * @param tight bring device in lock tight mode
  186. *
  187. * @return 0 on success, -1 in case of error
  188. *
  189. * The lock / lock-tight command only applies to the whole chip. To get some
  190. * parts of the chip lock and others unlocked use the following sequence:
  191. *
  192. * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
  193. * - Call nand_unlock() once for each consecutive area to be unlocked
  194. * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
  195. *
  196. * If the device is in lock-tight state software can't change the
  197. * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
  198. * calls will fail. It is only posible to leave lock-tight state by
  199. * an hardware signal (low pulse on _WP pin) or by power down.
  200. */
  201. int nand_lock(struct mtd_info *mtd, int tight)
  202. {
  203. int ret = 0;
  204. int status;
  205. struct nand_chip *chip = mtd->priv;
  206. /* select the NAND device */
  207. chip->select_chip(mtd, 0);
  208. chip->cmdfunc(mtd,
  209. (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
  210. -1, -1);
  211. /* call wait ready function */
  212. status = chip->waitfunc(mtd, chip);
  213. /* see if device thinks it succeeded */
  214. if (status & 0x01) {
  215. ret = -1;
  216. }
  217. /* de-select the NAND device */
  218. chip->select_chip(mtd, -1);
  219. return ret;
  220. }
  221. /**
  222. * nand_get_lock_status: - query current lock state from one page of NAND
  223. * flash
  224. *
  225. * @param mtd nand mtd instance
  226. * @param offset page address to query (must be page-aligned!)
  227. *
  228. * @return -1 in case of error
  229. * >0 lock status:
  230. * bitfield with the following combinations:
  231. * NAND_LOCK_STATUS_TIGHT: page in tight state
  232. * NAND_LOCK_STATUS_UNLOCK: page unlocked
  233. *
  234. */
  235. int nand_get_lock_status(struct mtd_info *mtd, loff_t offset)
  236. {
  237. int ret = 0;
  238. int chipnr;
  239. int page;
  240. struct nand_chip *chip = mtd->priv;
  241. /* select the NAND device */
  242. chipnr = (int)(offset >> chip->chip_shift);
  243. chip->select_chip(mtd, chipnr);
  244. if ((offset & (mtd->writesize - 1)) != 0) {
  245. printf("nand_get_lock_status: "
  246. "Start address must be beginning of "
  247. "nand page!\n");
  248. ret = -1;
  249. goto out;
  250. }
  251. /* check the Lock Status */
  252. page = (int)(offset >> chip->page_shift);
  253. chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
  254. ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT
  255. | NAND_LOCK_STATUS_UNLOCK);
  256. out:
  257. /* de-select the NAND device */
  258. chip->select_chip(mtd, -1);
  259. return ret;
  260. }
  261. /**
  262. * nand_unlock: - Unlock area of NAND pages
  263. * only one consecutive area can be unlocked at one time!
  264. *
  265. * @param mtd nand mtd instance
  266. * @param start start byte address
  267. * @param length number of bytes to unlock (must be a multiple of
  268. * page size nand->writesize)
  269. * @param allexcept if set, unlock everything not selected
  270. *
  271. * @return 0 on success, -1 in case of error
  272. */
  273. int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
  274. int allexcept)
  275. {
  276. int ret = 0;
  277. int chipnr;
  278. int status;
  279. int page;
  280. struct nand_chip *chip = mtd->priv;
  281. debug("nand_unlock%s: start: %08llx, length: %d!\n",
  282. allexcept ? " (allexcept)" : "", start, length);
  283. /* select the NAND device */
  284. chipnr = (int)(start >> chip->chip_shift);
  285. chip->select_chip(mtd, chipnr);
  286. /* check the WP bit */
  287. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  288. if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) {
  289. printf("nand_unlock: Device is write protected!\n");
  290. ret = -1;
  291. goto out;
  292. }
  293. if ((start & (mtd->erasesize - 1)) != 0) {
  294. printf("nand_unlock: Start address must be beginning of "
  295. "nand block!\n");
  296. ret = -1;
  297. goto out;
  298. }
  299. if (length == 0 || (length & (mtd->erasesize - 1)) != 0) {
  300. printf("nand_unlock: Length must be a multiple of nand block "
  301. "size %08x!\n", mtd->erasesize);
  302. ret = -1;
  303. goto out;
  304. }
  305. /*
  306. * Set length so that the last address is set to the
  307. * starting address of the last block
  308. */
  309. length -= mtd->erasesize;
  310. /* submit address of first page to unlock */
  311. page = (int)(start >> chip->page_shift);
  312. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  313. /* submit ADDRESS of LAST page to unlock */
  314. page += (int)(length >> chip->page_shift);
  315. /*
  316. * Page addresses for unlocking are supposed to be block-aligned.
  317. * At least some NAND chips use the low bit to indicate that the
  318. * page range should be inverted.
  319. */
  320. if (allexcept)
  321. page |= 1;
  322. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask);
  323. /* call wait ready function */
  324. status = chip->waitfunc(mtd, chip);
  325. /* see if device thinks it succeeded */
  326. if (status & 0x01) {
  327. /* there was an error */
  328. ret = -1;
  329. goto out;
  330. }
  331. out:
  332. /* de-select the NAND device */
  333. chip->select_chip(mtd, -1);
  334. return ret;
  335. }
  336. #endif
  337. /**
  338. * check_skip_len
  339. *
  340. * Check if there are any bad blocks, and whether length including bad
  341. * blocks fits into device
  342. *
  343. * @param nand NAND device
  344. * @param offset offset in flash
  345. * @param length image length
  346. * @return 0 if the image fits and there are no bad blocks
  347. * 1 if the image fits, but there are bad blocks
  348. * -1 if the image does not fit
  349. */
  350. static int check_skip_len(nand_info_t *nand, loff_t offset, size_t length)
  351. {
  352. size_t len_excl_bad = 0;
  353. int ret = 0;
  354. while (len_excl_bad < length) {
  355. size_t block_len, block_off;
  356. loff_t block_start;
  357. if (offset >= nand->size)
  358. return -1;
  359. block_start = offset & ~(loff_t)(nand->erasesize - 1);
  360. block_off = offset & (nand->erasesize - 1);
  361. block_len = nand->erasesize - block_off;
  362. if (!nand_block_isbad(nand, block_start))
  363. len_excl_bad += block_len;
  364. else
  365. ret = 1;
  366. offset += block_len;
  367. }
  368. return ret;
  369. }
  370. #ifdef CONFIG_CMD_NAND_TRIMFFS
  371. static size_t drop_ffs(const nand_info_t *nand, const u_char *buf,
  372. const size_t *len)
  373. {
  374. size_t i, l = *len;
  375. for (i = l - 1; i >= 0; i--)
  376. if (buf[i] != 0xFF)
  377. break;
  378. /* The resulting length must be aligned to the minimum flash I/O size */
  379. l = i + 1;
  380. l = (l + nand->writesize - 1) / nand->writesize;
  381. l *= nand->writesize;
  382. /*
  383. * since the input length may be unaligned, prevent access past the end
  384. * of the buffer
  385. */
  386. return min(l, *len);
  387. }
  388. #endif
  389. /**
  390. * nand_write_skip_bad:
  391. *
  392. * Write image to NAND flash.
  393. * Blocks that are marked bad are skipped and the is written to the next
  394. * block instead as long as the image is short enough to fit even after
  395. * skipping the bad blocks.
  396. *
  397. * @param nand NAND device
  398. * @param offset offset in flash
  399. * @param length buffer length
  400. * @param buffer buffer to read from
  401. * @param flags flags modifying the behaviour of the write to NAND
  402. * @return 0 in case of success
  403. */
  404. int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  405. u_char *buffer, int flags)
  406. {
  407. int rval = 0, blocksize;
  408. size_t left_to_write = *length;
  409. u_char *p_buffer = buffer;
  410. int need_skip;
  411. #ifdef CONFIG_CMD_NAND_YAFFS
  412. if (flags & WITH_YAFFS_OOB) {
  413. if (flags & ~WITH_YAFFS_OOB)
  414. return -EINVAL;
  415. int pages;
  416. pages = nand->erasesize / nand->writesize;
  417. blocksize = (pages * nand->oobsize) + nand->erasesize;
  418. if (*length % (nand->writesize + nand->oobsize)) {
  419. printf("Attempt to write incomplete page"
  420. " in yaffs mode\n");
  421. return -EINVAL;
  422. }
  423. } else
  424. #endif
  425. {
  426. blocksize = nand->erasesize;
  427. }
  428. /*
  429. * nand_write() handles unaligned, partial page writes.
  430. *
  431. * We allow length to be unaligned, for convenience in
  432. * using the $filesize variable.
  433. *
  434. * However, starting at an unaligned offset makes the
  435. * semantics of bad block skipping ambiguous (really,
  436. * you should only start a block skipping access at a
  437. * partition boundary). So don't try to handle that.
  438. */
  439. if ((offset & (nand->writesize - 1)) != 0) {
  440. printf("Attempt to write non page-aligned data\n");
  441. *length = 0;
  442. return -EINVAL;
  443. }
  444. need_skip = check_skip_len(nand, offset, *length);
  445. if (need_skip < 0) {
  446. printf("Attempt to write outside the flash area\n");
  447. *length = 0;
  448. return -EINVAL;
  449. }
  450. if (!need_skip && !(flags & WITH_DROP_FFS)) {
  451. rval = nand_write(nand, offset, length, buffer);
  452. if (rval == 0)
  453. return 0;
  454. *length = 0;
  455. printf("NAND write to offset %llx failed %d\n",
  456. offset, rval);
  457. return rval;
  458. }
  459. while (left_to_write > 0) {
  460. size_t block_offset = offset & (nand->erasesize - 1);
  461. size_t write_size, truncated_write_size;
  462. WATCHDOG_RESET();
  463. if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
  464. printf("Skip bad block 0x%08llx\n",
  465. offset & ~(nand->erasesize - 1));
  466. offset += nand->erasesize - block_offset;
  467. continue;
  468. }
  469. if (left_to_write < (blocksize - block_offset))
  470. write_size = left_to_write;
  471. else
  472. write_size = blocksize - block_offset;
  473. #ifdef CONFIG_CMD_NAND_YAFFS
  474. if (flags & WITH_YAFFS_OOB) {
  475. int page, pages;
  476. size_t pagesize = nand->writesize;
  477. size_t pagesize_oob = pagesize + nand->oobsize;
  478. struct mtd_oob_ops ops;
  479. ops.len = pagesize;
  480. ops.ooblen = nand->oobsize;
  481. ops.mode = MTD_OOB_AUTO;
  482. ops.ooboffs = 0;
  483. pages = write_size / pagesize_oob;
  484. for (page = 0; page < pages; page++) {
  485. WATCHDOG_RESET();
  486. ops.datbuf = p_buffer;
  487. ops.oobbuf = ops.datbuf + pagesize;
  488. rval = nand->write_oob(nand, offset, &ops);
  489. if (rval != 0)
  490. break;
  491. offset += pagesize;
  492. p_buffer += pagesize_oob;
  493. }
  494. }
  495. else
  496. #endif
  497. {
  498. truncated_write_size = write_size;
  499. #ifdef CONFIG_CMD_NAND_TRIMFFS
  500. if (flags & WITH_DROP_FFS)
  501. truncated_write_size = drop_ffs(nand, p_buffer,
  502. &write_size);
  503. #endif
  504. rval = nand_write(nand, offset, &truncated_write_size,
  505. p_buffer);
  506. offset += write_size;
  507. p_buffer += write_size;
  508. }
  509. if (rval != 0) {
  510. printf("NAND write to offset %llx failed %d\n",
  511. offset, rval);
  512. *length -= left_to_write;
  513. return rval;
  514. }
  515. left_to_write -= write_size;
  516. }
  517. return 0;
  518. }
  519. /**
  520. * nand_read_skip_bad:
  521. *
  522. * Read image from NAND flash.
  523. * Blocks that are marked bad are skipped and the next block is read
  524. * instead as long as the image is short enough to fit even after skipping the
  525. * bad blocks.
  526. *
  527. * @param nand NAND device
  528. * @param offset offset in flash
  529. * @param length buffer length, on return holds number of read bytes
  530. * @param buffer buffer to write to
  531. * @return 0 in case of success
  532. */
  533. int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  534. u_char *buffer)
  535. {
  536. int rval;
  537. size_t left_to_read = *length;
  538. u_char *p_buffer = buffer;
  539. int need_skip;
  540. if ((offset & (nand->writesize - 1)) != 0) {
  541. printf("Attempt to read non page-aligned data\n");
  542. *length = 0;
  543. return -EINVAL;
  544. }
  545. need_skip = check_skip_len(nand, offset, *length);
  546. if (need_skip < 0) {
  547. printf("Attempt to read outside the flash area\n");
  548. *length = 0;
  549. return -EINVAL;
  550. }
  551. if (!need_skip) {
  552. rval = nand_read(nand, offset, length, buffer);
  553. if (!rval || rval == -EUCLEAN)
  554. return 0;
  555. *length = 0;
  556. printf("NAND read from offset %llx failed %d\n",
  557. offset, rval);
  558. return rval;
  559. }
  560. while (left_to_read > 0) {
  561. size_t block_offset = offset & (nand->erasesize - 1);
  562. size_t read_length;
  563. WATCHDOG_RESET();
  564. if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
  565. printf("Skipping bad block 0x%08llx\n",
  566. offset & ~(nand->erasesize - 1));
  567. offset += nand->erasesize - block_offset;
  568. continue;
  569. }
  570. if (left_to_read < (nand->erasesize - block_offset))
  571. read_length = left_to_read;
  572. else
  573. read_length = nand->erasesize - block_offset;
  574. rval = nand_read(nand, offset, &read_length, p_buffer);
  575. if (rval && rval != -EUCLEAN) {
  576. printf("NAND read from offset %llx failed %d\n",
  577. offset, rval);
  578. *length -= left_to_read;
  579. return rval;
  580. }
  581. left_to_read -= read_length;
  582. offset += read_length;
  583. p_buffer += read_length;
  584. }
  585. return 0;
  586. }