nand_util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 formating)
  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->writesize - 1)) != 0) {
  78. printf("Attempt to erase non page 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. #define NAND_CMD_LOCK 0x2a
  181. #define NAND_CMD_LOCK_TIGHT 0x2c
  182. #define NAND_CMD_UNLOCK1 0x23
  183. #define NAND_CMD_UNLOCK2 0x24
  184. #define NAND_CMD_LOCK_STATUS 0x7a
  185. /**
  186. * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
  187. * state
  188. *
  189. * @param mtd nand mtd instance
  190. * @param tight bring device in lock tight mode
  191. *
  192. * @return 0 on success, -1 in case of error
  193. *
  194. * The lock / lock-tight command only applies to the whole chip. To get some
  195. * parts of the chip lock and others unlocked use the following sequence:
  196. *
  197. * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
  198. * - Call nand_unlock() once for each consecutive area to be unlocked
  199. * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
  200. *
  201. * If the device is in lock-tight state software can't change the
  202. * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
  203. * calls will fail. It is only posible to leave lock-tight state by
  204. * an hardware signal (low pulse on _WP pin) or by power down.
  205. */
  206. int nand_lock(struct mtd_info *mtd, int tight)
  207. {
  208. int ret = 0;
  209. int status;
  210. struct nand_chip *chip = mtd->priv;
  211. /* select the NAND device */
  212. chip->select_chip(mtd, 0);
  213. chip->cmdfunc(mtd,
  214. (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
  215. -1, -1);
  216. /* call wait ready function */
  217. status = chip->waitfunc(mtd, chip);
  218. /* see if device thinks it succeeded */
  219. if (status & 0x01) {
  220. ret = -1;
  221. }
  222. /* de-select the NAND device */
  223. chip->select_chip(mtd, -1);
  224. return ret;
  225. }
  226. /**
  227. * nand_get_lock_status: - query current lock state from one page of NAND
  228. * flash
  229. *
  230. * @param mtd nand mtd instance
  231. * @param offset page address to query (muss be page aligned!)
  232. *
  233. * @return -1 in case of error
  234. * >0 lock status:
  235. * bitfield with the following combinations:
  236. * NAND_LOCK_STATUS_TIGHT: page in tight state
  237. * NAND_LOCK_STATUS_LOCK: page locked
  238. * NAND_LOCK_STATUS_UNLOCK: page unlocked
  239. *
  240. */
  241. int nand_get_lock_status(struct mtd_info *mtd, loff_t offset)
  242. {
  243. int ret = 0;
  244. int chipnr;
  245. int page;
  246. struct nand_chip *chip = mtd->priv;
  247. /* select the NAND device */
  248. chipnr = (int)(offset >> chip->chip_shift);
  249. chip->select_chip(mtd, chipnr);
  250. if ((offset & (mtd->writesize - 1)) != 0) {
  251. printf ("nand_get_lock_status: "
  252. "Start address must be beginning of "
  253. "nand page!\n");
  254. ret = -1;
  255. goto out;
  256. }
  257. /* check the Lock Status */
  258. page = (int)(offset >> chip->page_shift);
  259. chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
  260. ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT
  261. | NAND_LOCK_STATUS_LOCK
  262. | NAND_LOCK_STATUS_UNLOCK);
  263. out:
  264. /* de-select the NAND device */
  265. chip->select_chip(mtd, -1);
  266. return ret;
  267. }
  268. /**
  269. * nand_unlock: - Unlock area of NAND pages
  270. * only one consecutive area can be unlocked at one time!
  271. *
  272. * @param mtd nand mtd instance
  273. * @param start start byte address
  274. * @param length number of bytes to unlock (must be a multiple of
  275. * page size nand->writesize)
  276. *
  277. * @return 0 on success, -1 in case of error
  278. */
  279. int nand_unlock(struct mtd_info *mtd, ulong start, ulong length)
  280. {
  281. int ret = 0;
  282. int chipnr;
  283. int status;
  284. int page;
  285. struct nand_chip *chip = mtd->priv;
  286. printf ("nand_unlock: start: %08x, length: %d!\n",
  287. (int)start, (int)length);
  288. /* select the NAND device */
  289. chipnr = (int)(start >> chip->chip_shift);
  290. chip->select_chip(mtd, chipnr);
  291. /* check the WP bit */
  292. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  293. if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) {
  294. printf ("nand_unlock: Device is write protected!\n");
  295. ret = -1;
  296. goto out;
  297. }
  298. if ((start & (mtd->erasesize - 1)) != 0) {
  299. printf ("nand_unlock: Start address must be beginning of "
  300. "nand block!\n");
  301. ret = -1;
  302. goto out;
  303. }
  304. if (length == 0 || (length & (mtd->erasesize - 1)) != 0) {
  305. printf ("nand_unlock: Length must be a multiple of nand block "
  306. "size %08x!\n", mtd->erasesize);
  307. ret = -1;
  308. goto out;
  309. }
  310. /*
  311. * Set length so that the last address is set to the
  312. * starting address of the last block
  313. */
  314. length -= mtd->erasesize;
  315. /* submit address of first page to unlock */
  316. page = (int)(start >> chip->page_shift);
  317. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  318. /* submit ADDRESS of LAST page to unlock */
  319. page += (int)(length >> chip->page_shift);
  320. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask);
  321. /* call wait ready function */
  322. status = chip->waitfunc(mtd, chip);
  323. /* see if device thinks it succeeded */
  324. if (status & 0x01) {
  325. /* there was an error */
  326. ret = -1;
  327. goto out;
  328. }
  329. out:
  330. /* de-select the NAND device */
  331. chip->select_chip(mtd, -1);
  332. return ret;
  333. }
  334. #endif
  335. /**
  336. * check_skip_len
  337. *
  338. * Check if there are any bad blocks, and whether length including bad
  339. * blocks fits into device
  340. *
  341. * @param nand NAND device
  342. * @param offset offset in flash
  343. * @param length image length
  344. * @return 0 if the image fits and there are no bad blocks
  345. * 1 if the image fits, but there are bad blocks
  346. * -1 if the image does not fit
  347. */
  348. static int check_skip_len(nand_info_t *nand, loff_t offset, size_t length)
  349. {
  350. size_t len_excl_bad = 0;
  351. int ret = 0;
  352. while (len_excl_bad < length) {
  353. size_t block_len, block_off;
  354. loff_t block_start;
  355. if (offset >= nand->size)
  356. return -1;
  357. block_start = offset & ~(loff_t)(nand->erasesize - 1);
  358. block_off = offset & (nand->erasesize - 1);
  359. block_len = nand->erasesize - block_off;
  360. if (!nand_block_isbad(nand, block_start))
  361. len_excl_bad += block_len;
  362. else
  363. ret = 1;
  364. offset += block_len;
  365. }
  366. return ret;
  367. }
  368. #ifdef CONFIG_CMD_NAND_TRIMFFS
  369. static size_t drop_ffs(const nand_info_t *nand, const u_char *buf,
  370. const size_t *len)
  371. {
  372. size_t i, l = *len;
  373. for (i = l - 1; i >= 0; i--)
  374. if (buf[i] != 0xFF)
  375. break;
  376. /* The resulting length must be aligned to the minimum flash I/O size */
  377. l = i + 1;
  378. l = (l + nand->writesize - 1) / nand->writesize;
  379. l *= nand->writesize;
  380. /*
  381. * since the input length may be unaligned, prevent access past the end
  382. * of the buffer
  383. */
  384. return min(l, *len);
  385. }
  386. #endif
  387. /**
  388. * nand_write_skip_bad:
  389. *
  390. * Write image to NAND flash.
  391. * Blocks that are marked bad are skipped and the is written to the next
  392. * block instead as long as the image is short enough to fit even after
  393. * skipping the bad blocks.
  394. *
  395. * @param nand NAND device
  396. * @param offset offset in flash
  397. * @param length buffer length
  398. * @param buffer buffer to read from
  399. * @param flags flags modifying the behaviour of the write to NAND
  400. * @return 0 in case of success
  401. */
  402. int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  403. u_char *buffer, int flags)
  404. {
  405. int rval = 0, blocksize;
  406. size_t left_to_write = *length;
  407. u_char *p_buffer = buffer;
  408. int need_skip;
  409. #ifdef CONFIG_CMD_NAND_YAFFS
  410. if (flags & WITH_YAFFS_OOB) {
  411. if (flags & ~WITH_YAFFS_OOB)
  412. return -EINVAL;
  413. int pages;
  414. pages = nand->erasesize / nand->writesize;
  415. blocksize = (pages * nand->oobsize) + nand->erasesize;
  416. if (*length % (nand->writesize + nand->oobsize)) {
  417. printf ("Attempt to write incomplete page"
  418. " in yaffs mode\n");
  419. return -EINVAL;
  420. }
  421. } else
  422. #endif
  423. {
  424. blocksize = nand->erasesize;
  425. }
  426. /*
  427. * nand_write() handles unaligned, partial page writes.
  428. *
  429. * We allow length to be unaligned, for convenience in
  430. * using the $filesize variable.
  431. *
  432. * However, starting at an unaligned offset makes the
  433. * semantics of bad block skipping ambiguous (really,
  434. * you should only start a block skipping access at a
  435. * partition boundary). So don't try to handle that.
  436. */
  437. if ((offset & (nand->writesize - 1)) != 0) {
  438. printf ("Attempt to write non page aligned data\n");
  439. *length = 0;
  440. return -EINVAL;
  441. }
  442. need_skip = check_skip_len(nand, offset, *length);
  443. if (need_skip < 0) {
  444. printf ("Attempt to write outside the flash area\n");
  445. *length = 0;
  446. return -EINVAL;
  447. }
  448. if (!need_skip && !(flags & WITH_DROP_FFS)) {
  449. rval = nand_write (nand, offset, length, buffer);
  450. if (rval == 0)
  451. return 0;
  452. *length = 0;
  453. printf ("NAND write to offset %llx failed %d\n",
  454. offset, rval);
  455. return rval;
  456. }
  457. while (left_to_write > 0) {
  458. size_t block_offset = offset & (nand->erasesize - 1);
  459. size_t write_size, truncated_write_size;
  460. WATCHDOG_RESET ();
  461. if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  462. printf ("Skip bad block 0x%08llx\n",
  463. offset & ~(nand->erasesize - 1));
  464. offset += nand->erasesize - block_offset;
  465. continue;
  466. }
  467. if (left_to_write < (blocksize - block_offset))
  468. write_size = left_to_write;
  469. else
  470. write_size = blocksize - block_offset;
  471. #ifdef CONFIG_CMD_NAND_YAFFS
  472. if (flags & WITH_YAFFS_OOB) {
  473. int page, pages;
  474. size_t pagesize = nand->writesize;
  475. size_t pagesize_oob = pagesize + nand->oobsize;
  476. struct mtd_oob_ops ops;
  477. ops.len = pagesize;
  478. ops.ooblen = nand->oobsize;
  479. ops.mode = MTD_OOB_AUTO;
  480. ops.ooboffs = 0;
  481. pages = write_size / pagesize_oob;
  482. for (page = 0; page < pages; page++) {
  483. WATCHDOG_RESET();
  484. ops.datbuf = p_buffer;
  485. ops.oobbuf = ops.datbuf + pagesize;
  486. rval = nand->write_oob(nand, offset, &ops);
  487. if (rval != 0)
  488. break;
  489. offset += pagesize;
  490. p_buffer += pagesize_oob;
  491. }
  492. }
  493. else
  494. #endif
  495. {
  496. truncated_write_size = write_size;
  497. #ifdef CONFIG_CMD_NAND_TRIMFFS
  498. if (flags & WITH_DROP_FFS)
  499. truncated_write_size = drop_ffs(nand, p_buffer,
  500. &write_size);
  501. #endif
  502. rval = nand_write(nand, offset, &truncated_write_size,
  503. p_buffer);
  504. offset += write_size;
  505. p_buffer += write_size;
  506. }
  507. if (rval != 0) {
  508. printf ("NAND write to offset %llx failed %d\n",
  509. offset, rval);
  510. *length -= left_to_write;
  511. return rval;
  512. }
  513. left_to_write -= write_size;
  514. }
  515. return 0;
  516. }
  517. /**
  518. * nand_read_skip_bad:
  519. *
  520. * Read image from NAND flash.
  521. * Blocks that are marked bad are skipped and the next block is readen
  522. * instead as long as the image is short enough to fit even after skipping the
  523. * bad blocks.
  524. *
  525. * @param nand NAND device
  526. * @param offset offset in flash
  527. * @param length buffer length, on return holds remaining bytes to read
  528. * @param buffer buffer to write to
  529. * @return 0 in case of success
  530. */
  531. int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  532. u_char *buffer)
  533. {
  534. int rval;
  535. size_t left_to_read = *length;
  536. u_char *p_buffer = buffer;
  537. int need_skip;
  538. if ((offset & (nand->writesize - 1)) != 0) {
  539. printf ("Attempt to read non page aligned data\n");
  540. *length = 0;
  541. return -EINVAL;
  542. }
  543. need_skip = check_skip_len(nand, offset, *length);
  544. if (need_skip < 0) {
  545. printf ("Attempt to read outside the flash area\n");
  546. *length = 0;
  547. return -EINVAL;
  548. }
  549. if (!need_skip) {
  550. rval = nand_read (nand, offset, length, buffer);
  551. if (!rval || rval == -EUCLEAN)
  552. return 0;
  553. *length = 0;
  554. printf ("NAND read from offset %llx failed %d\n",
  555. offset, rval);
  556. return rval;
  557. }
  558. while (left_to_read > 0) {
  559. size_t block_offset = offset & (nand->erasesize - 1);
  560. size_t read_length;
  561. WATCHDOG_RESET ();
  562. if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  563. printf ("Skipping bad block 0x%08llx\n",
  564. offset & ~(nand->erasesize - 1));
  565. offset += nand->erasesize - block_offset;
  566. continue;
  567. }
  568. if (left_to_read < (nand->erasesize - block_offset))
  569. read_length = left_to_read;
  570. else
  571. read_length = nand->erasesize - block_offset;
  572. rval = nand_read (nand, offset, &read_length, p_buffer);
  573. if (rval && rval != -EUCLEAN) {
  574. printf ("NAND read from offset %llx failed %d\n",
  575. offset, rval);
  576. *length -= left_to_read;
  577. return rval;
  578. }
  579. left_to_read -= read_length;
  580. offset += read_length;
  581. p_buffer += read_length;
  582. }
  583. return 0;
  584. }