nand_util.c 17 KB

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