nand_util.c 16 KB

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