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