nand_util.c 16 KB

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