nand_util.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. */
  32. #include <common.h>
  33. #include <command.h>
  34. #include <watchdog.h>
  35. #include <malloc.h>
  36. #include <div64.h>
  37. #include <asm/errno.h>
  38. #include <linux/mtd/mtd.h>
  39. #include <nand.h>
  40. #include <jffs2/jffs2.h>
  41. #if !defined(CONFIG_SYS_64BIT_VSPRINTF)
  42. #warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
  43. #endif
  44. typedef struct erase_info erase_info_t;
  45. typedef struct mtd_info mtd_info_t;
  46. /* support only for native endian JFFS2 */
  47. #define cpu_to_je16(x) (x)
  48. #define cpu_to_je32(x) (x)
  49. /*****************************************************************************/
  50. static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)
  51. {
  52. return 0;
  53. }
  54. /**
  55. * nand_erase_opts: - erase NAND flash with support for various options
  56. * (jffs2 formating)
  57. *
  58. * @param meminfo NAND device to erase
  59. * @param opts options, @see struct nand_erase_options
  60. * @return 0 in case of success
  61. *
  62. * This code is ported from flash_eraseall.c from Linux mtd utils by
  63. * Arcom Control System Ltd.
  64. */
  65. int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
  66. {
  67. struct jffs2_unknown_node cleanmarker;
  68. erase_info_t erase;
  69. ulong erase_length;
  70. int bbtest = 1;
  71. int result;
  72. int percent_complete = -1;
  73. int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL;
  74. const char *mtd_device = meminfo->name;
  75. struct mtd_oob_ops oob_opts;
  76. struct nand_chip *chip = meminfo->priv;
  77. memset(&erase, 0, sizeof(erase));
  78. memset(&oob_opts, 0, sizeof(oob_opts));
  79. erase.mtd = meminfo;
  80. erase.len = meminfo->erasesize;
  81. erase.addr = opts->offset;
  82. erase_length = opts->length;
  83. cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
  84. cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
  85. cleanmarker.totlen = cpu_to_je32(8);
  86. /* scrub option allows to erase badblock. To prevent internal
  87. * check from erase() method, set block check method to dummy
  88. * and disable bad block table while erasing.
  89. */
  90. if (opts->scrub) {
  91. struct nand_chip *priv_nand = meminfo->priv;
  92. nand_block_bad_old = priv_nand->block_bad;
  93. priv_nand->block_bad = nand_block_bad_scrub;
  94. /* we don't need the bad block table anymore...
  95. * after scrub, there are no bad blocks left!
  96. */
  97. if (priv_nand->bbt) {
  98. kfree(priv_nand->bbt);
  99. }
  100. priv_nand->bbt = NULL;
  101. }
  102. if (erase_length < meminfo->erasesize) {
  103. printf("Warning: Erase size 0x%08lx smaller than one " \
  104. "erase block 0x%08x\n",erase_length, meminfo->erasesize);
  105. printf(" Erasing 0x%08x instead\n", meminfo->erasesize);
  106. erase_length = meminfo->erasesize;
  107. }
  108. for (;
  109. erase.addr < opts->offset + erase_length;
  110. erase.addr += meminfo->erasesize) {
  111. WATCHDOG_RESET ();
  112. if (!opts->scrub && bbtest) {
  113. int ret = meminfo->block_isbad(meminfo, erase.addr);
  114. if (ret > 0) {
  115. if (!opts->quiet)
  116. printf("\rSkipping bad block at "
  117. "0x%08llx "
  118. " \n",
  119. erase.addr);
  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. result = meminfo->erase(meminfo, &erase);
  129. if (result != 0) {
  130. printf("\n%s: MTD Erase failure: %d\n",
  131. mtd_device, result);
  132. continue;
  133. }
  134. /* format for JFFS2 ? */
  135. if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) {
  136. chip->ops.ooblen = 8;
  137. chip->ops.datbuf = NULL;
  138. chip->ops.oobbuf = (uint8_t *)&cleanmarker;
  139. chip->ops.ooboffs = 0;
  140. chip->ops.mode = MTD_OOB_AUTO;
  141. result = meminfo->write_oob(meminfo,
  142. erase.addr,
  143. &chip->ops);
  144. if (result != 0) {
  145. printf("\n%s: MTD writeoob failure: %d\n",
  146. mtd_device, result);
  147. continue;
  148. }
  149. }
  150. if (!opts->quiet) {
  151. unsigned long long n =(unsigned long long)
  152. (erase.addr + meminfo->erasesize - opts->offset)
  153. * 100;
  154. int percent;
  155. do_div(n, erase_length);
  156. percent = (int)n;
  157. /* output progress message only at whole percent
  158. * steps to reduce the number of messages printed
  159. * on (slow) serial consoles
  160. */
  161. if (percent != percent_complete) {
  162. percent_complete = percent;
  163. printf("\rErasing at 0x%llx -- %3d%% complete.",
  164. erase.addr, percent);
  165. if (opts->jffs2 && result == 0)
  166. printf(" Cleanmarker written at 0x%llx.",
  167. erase.addr);
  168. }
  169. }
  170. }
  171. if (!opts->quiet)
  172. printf("\n");
  173. if (nand_block_bad_old) {
  174. struct nand_chip *priv_nand = meminfo->priv;
  175. priv_nand->block_bad = nand_block_bad_old;
  176. priv_nand->scan_bbt(meminfo);
  177. }
  178. return 0;
  179. }
  180. /* XXX U-BOOT XXX */
  181. #if 0
  182. #define MAX_PAGE_SIZE 2048
  183. #define MAX_OOB_SIZE 64
  184. /*
  185. * buffer array used for writing data
  186. */
  187. static unsigned char data_buf[MAX_PAGE_SIZE];
  188. static unsigned char oob_buf[MAX_OOB_SIZE];
  189. /* OOB layouts to pass into the kernel as default */
  190. static struct nand_ecclayout none_ecclayout = {
  191. .useecc = MTD_NANDECC_OFF,
  192. };
  193. static struct nand_ecclayout jffs2_ecclayout = {
  194. .useecc = MTD_NANDECC_PLACE,
  195. .eccbytes = 6,
  196. .eccpos = { 0, 1, 2, 3, 6, 7 }
  197. };
  198. static struct nand_ecclayout yaffs_ecclayout = {
  199. .useecc = MTD_NANDECC_PLACE,
  200. .eccbytes = 6,
  201. .eccpos = { 8, 9, 10, 13, 14, 15}
  202. };
  203. static struct nand_ecclayout autoplace_ecclayout = {
  204. .useecc = MTD_NANDECC_AUTOPLACE
  205. };
  206. #endif
  207. /* XXX U-BOOT XXX */
  208. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  209. /******************************************************************************
  210. * Support for locking / unlocking operations of some NAND devices
  211. *****************************************************************************/
  212. #define NAND_CMD_LOCK 0x2a
  213. #define NAND_CMD_LOCK_TIGHT 0x2c
  214. #define NAND_CMD_UNLOCK1 0x23
  215. #define NAND_CMD_UNLOCK2 0x24
  216. #define NAND_CMD_LOCK_STATUS 0x7a
  217. /**
  218. * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
  219. * state
  220. *
  221. * @param mtd nand mtd instance
  222. * @param tight bring device in lock tight mode
  223. *
  224. * @return 0 on success, -1 in case of error
  225. *
  226. * The lock / lock-tight command only applies to the whole chip. To get some
  227. * parts of the chip lock and others unlocked use the following sequence:
  228. *
  229. * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
  230. * - Call nand_unlock() once for each consecutive area to be unlocked
  231. * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
  232. *
  233. * If the device is in lock-tight state software can't change the
  234. * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
  235. * calls will fail. It is only posible to leave lock-tight state by
  236. * an hardware signal (low pulse on _WP pin) or by power down.
  237. */
  238. int nand_lock(struct mtd_info *mtd, int tight)
  239. {
  240. int ret = 0;
  241. int status;
  242. struct nand_chip *chip = mtd->priv;
  243. /* select the NAND device */
  244. chip->select_chip(mtd, 0);
  245. chip->cmdfunc(mtd,
  246. (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
  247. -1, -1);
  248. /* call wait ready function */
  249. status = chip->waitfunc(mtd, chip);
  250. /* see if device thinks it succeeded */
  251. if (status & 0x01) {
  252. ret = -1;
  253. }
  254. /* de-select the NAND device */
  255. chip->select_chip(mtd, -1);
  256. return ret;
  257. }
  258. /**
  259. * nand_get_lock_status: - query current lock state from one page of NAND
  260. * flash
  261. *
  262. * @param mtd nand mtd instance
  263. * @param offset page address to query (muss be page aligned!)
  264. *
  265. * @return -1 in case of error
  266. * >0 lock status:
  267. * bitfield with the following combinations:
  268. * NAND_LOCK_STATUS_TIGHT: page in tight state
  269. * NAND_LOCK_STATUS_LOCK: page locked
  270. * NAND_LOCK_STATUS_UNLOCK: page unlocked
  271. *
  272. */
  273. int nand_get_lock_status(struct mtd_info *mtd, ulong offset)
  274. {
  275. int ret = 0;
  276. int chipnr;
  277. int page;
  278. struct nand_chip *chip = mtd->priv;
  279. /* select the NAND device */
  280. chipnr = (int)(offset >> chip->chip_shift);
  281. chip->select_chip(mtd, chipnr);
  282. if ((offset & (mtd->writesize - 1)) != 0) {
  283. printf ("nand_get_lock_status: "
  284. "Start address must be beginning of "
  285. "nand page!\n");
  286. ret = -1;
  287. goto out;
  288. }
  289. /* check the Lock Status */
  290. page = (int)(offset >> chip->page_shift);
  291. chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
  292. ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT
  293. | NAND_LOCK_STATUS_LOCK
  294. | NAND_LOCK_STATUS_UNLOCK);
  295. out:
  296. /* de-select the NAND device */
  297. chip->select_chip(mtd, -1);
  298. return ret;
  299. }
  300. /**
  301. * nand_unlock: - Unlock area of NAND pages
  302. * only one consecutive area can be unlocked at one time!
  303. *
  304. * @param mtd nand mtd instance
  305. * @param start start byte address
  306. * @param length number of bytes to unlock (must be a multiple of
  307. * page size nand->writesize)
  308. *
  309. * @return 0 on success, -1 in case of error
  310. */
  311. int nand_unlock(struct mtd_info *mtd, ulong start, ulong length)
  312. {
  313. int ret = 0;
  314. int chipnr;
  315. int status;
  316. int page;
  317. struct nand_chip *chip = mtd->priv;
  318. printf ("nand_unlock: start: %08x, length: %d!\n",
  319. (int)start, (int)length);
  320. /* select the NAND device */
  321. chipnr = (int)(start >> chip->chip_shift);
  322. chip->select_chip(mtd, chipnr);
  323. /* check the WP bit */
  324. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  325. if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) {
  326. printf ("nand_unlock: Device is write protected!\n");
  327. ret = -1;
  328. goto out;
  329. }
  330. if ((start & (mtd->erasesize - 1)) != 0) {
  331. printf ("nand_unlock: Start address must be beginning of "
  332. "nand block!\n");
  333. ret = -1;
  334. goto out;
  335. }
  336. if (length == 0 || (length & (mtd->erasesize - 1)) != 0) {
  337. printf ("nand_unlock: Length must be a multiple of nand block "
  338. "size %08x!\n", mtd->erasesize);
  339. ret = -1;
  340. goto out;
  341. }
  342. /*
  343. * Set length so that the last address is set to the
  344. * starting address of the last block
  345. */
  346. length -= mtd->erasesize;
  347. /* submit address of first page to unlock */
  348. page = (int)(start >> chip->page_shift);
  349. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  350. /* submit ADDRESS of LAST page to unlock */
  351. page += (int)(length >> chip->page_shift);
  352. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask);
  353. /* call wait ready function */
  354. status = chip->waitfunc(mtd, chip);
  355. /* see if device thinks it succeeded */
  356. if (status & 0x01) {
  357. /* there was an error */
  358. ret = -1;
  359. goto out;
  360. }
  361. out:
  362. /* de-select the NAND device */
  363. chip->select_chip(mtd, -1);
  364. return ret;
  365. }
  366. #endif
  367. /**
  368. * get_len_incl_bad
  369. *
  370. * Check if length including bad blocks fits into device.
  371. *
  372. * @param nand NAND device
  373. * @param offset offset in flash
  374. * @param length image length
  375. * @return image length including bad blocks
  376. */
  377. static size_t get_len_incl_bad (nand_info_t *nand, size_t offset,
  378. const size_t length)
  379. {
  380. size_t len_incl_bad = 0;
  381. size_t len_excl_bad = 0;
  382. size_t block_len;
  383. while (len_excl_bad < length) {
  384. block_len = nand->erasesize - (offset & (nand->erasesize - 1));
  385. if (!nand_block_isbad (nand, offset & ~(nand->erasesize - 1)))
  386. len_excl_bad += block_len;
  387. len_incl_bad += block_len;
  388. offset += block_len;
  389. if ((offset + len_incl_bad) >= nand->size)
  390. break;
  391. }
  392. return len_incl_bad;
  393. }
  394. /**
  395. * nand_write_skip_bad:
  396. *
  397. * Write image to NAND flash.
  398. * Blocks that are marked bad are skipped and the is written to the next
  399. * block instead as long as the image is short enough to fit even after
  400. * skipping the bad blocks.
  401. *
  402. * @param nand NAND device
  403. * @param offset offset in flash
  404. * @param length buffer length
  405. * @param buf buffer to read from
  406. * @return 0 in case of success
  407. */
  408. int nand_write_skip_bad(nand_info_t *nand, size_t offset, size_t *length,
  409. u_char *buffer)
  410. {
  411. int rval;
  412. size_t left_to_write = *length;
  413. size_t len_incl_bad;
  414. u_char *p_buffer = buffer;
  415. /* Reject writes, which are not page aligned */
  416. if ((offset & (nand->writesize - 1)) != 0 ||
  417. (*length & (nand->writesize - 1)) != 0) {
  418. printf ("Attempt to write non page aligned data\n");
  419. return -EINVAL;
  420. }
  421. len_incl_bad = get_len_incl_bad (nand, offset, *length);
  422. if ((offset + len_incl_bad) >= nand->size) {
  423. printf ("Attempt to write outside the flash area\n");
  424. return -EINVAL;
  425. }
  426. if (len_incl_bad == *length) {
  427. rval = nand_write (nand, offset, length, buffer);
  428. if (rval != 0)
  429. printf ("NAND write to offset %zx failed %d\n",
  430. offset, rval);
  431. return rval;
  432. }
  433. while (left_to_write > 0) {
  434. size_t block_offset = offset & (nand->erasesize - 1);
  435. size_t write_size;
  436. if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  437. printf ("Skip bad block 0x%08zx\n",
  438. offset & ~(nand->erasesize - 1));
  439. offset += nand->erasesize - block_offset;
  440. continue;
  441. }
  442. if (left_to_write < (nand->erasesize - block_offset))
  443. write_size = left_to_write;
  444. else
  445. write_size = nand->erasesize - block_offset;
  446. rval = nand_write (nand, offset, &write_size, p_buffer);
  447. if (rval != 0) {
  448. printf ("NAND write to offset %zx failed %d\n",
  449. offset, rval);
  450. *length -= left_to_write;
  451. return rval;
  452. }
  453. left_to_write -= write_size;
  454. offset += write_size;
  455. p_buffer += write_size;
  456. }
  457. return 0;
  458. }
  459. /**
  460. * nand_read_skip_bad:
  461. *
  462. * Read image from NAND flash.
  463. * Blocks that are marked bad are skipped and the next block is readen
  464. * instead as long as the image is short enough to fit even after skipping the
  465. * bad blocks.
  466. *
  467. * @param nand NAND device
  468. * @param offset offset in flash
  469. * @param length buffer length, on return holds remaining bytes to read
  470. * @param buffer buffer to write to
  471. * @return 0 in case of success
  472. */
  473. int nand_read_skip_bad(nand_info_t *nand, size_t offset, size_t *length,
  474. u_char *buffer)
  475. {
  476. int rval;
  477. size_t left_to_read = *length;
  478. size_t len_incl_bad;
  479. u_char *p_buffer = buffer;
  480. len_incl_bad = get_len_incl_bad (nand, offset, *length);
  481. if ((offset + len_incl_bad) >= nand->size) {
  482. printf ("Attempt to read outside the flash area\n");
  483. return -EINVAL;
  484. }
  485. if (len_incl_bad == *length) {
  486. rval = nand_read (nand, offset, length, buffer);
  487. if (rval != 0)
  488. printf ("NAND read from offset %zx failed %d\n",
  489. offset, rval);
  490. return rval;
  491. }
  492. while (left_to_read > 0) {
  493. size_t block_offset = offset & (nand->erasesize - 1);
  494. size_t read_length;
  495. if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  496. printf ("Skipping bad block 0x%08zx\n",
  497. offset & ~(nand->erasesize - 1));
  498. offset += nand->erasesize - block_offset;
  499. continue;
  500. }
  501. if (left_to_read < (nand->erasesize - block_offset))
  502. read_length = left_to_read;
  503. else
  504. read_length = nand->erasesize - block_offset;
  505. rval = nand_read (nand, offset, &read_length, p_buffer);
  506. if (rval != 0) {
  507. printf ("NAND read from offset %zx failed %d\n",
  508. offset, rval);
  509. *length -= left_to_read;
  510. return rval;
  511. }
  512. left_to_read -= read_length;
  513. offset += read_length;
  514. p_buffer += read_length;
  515. }
  516. return 0;
  517. }