nand_util.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. uint8_t buf[64];
  75. memset(buf, 0, sizeof(buf));
  76. memset(&erase, 0, sizeof(erase));
  77. memset(&oob_opts, 0, sizeof(oob_opts));
  78. erase.mtd = meminfo;
  79. erase.len = meminfo->erasesize;
  80. erase.addr = opts->offset;
  81. erase_length = opts->length;
  82. cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
  83. cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
  84. cleanmarker.totlen = cpu_to_je32(8);
  85. cleanmarker.hdr_crc = cpu_to_je32(
  86. crc32_no_comp(0, (unsigned char *) &cleanmarker,
  87. sizeof(struct jffs2_unknown_node) - 4));
  88. /* scrub option allows to erase badblock. To prevent internal
  89. * check from erase() method, set block check method to dummy
  90. * and disable bad block table while erasing.
  91. */
  92. if (opts->scrub) {
  93. struct nand_chip *priv_nand = meminfo->priv;
  94. nand_block_bad_old = priv_nand->block_bad;
  95. priv_nand->block_bad = nand_block_bad_scrub;
  96. /* we don't need the bad block table anymore...
  97. * after scrub, there are no bad blocks left!
  98. */
  99. if (priv_nand->bbt) {
  100. kfree(priv_nand->bbt);
  101. }
  102. priv_nand->bbt = NULL;
  103. }
  104. if (erase_length < meminfo->erasesize) {
  105. printf("Warning: Erase size 0x%08lx smaller than one " \
  106. "erase block 0x%08x\n",erase_length, meminfo->erasesize);
  107. printf(" Erasing 0x%08x instead\n", meminfo->erasesize);
  108. erase_length = meminfo->erasesize;
  109. }
  110. for (;
  111. erase.addr < opts->offset + 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%08x "
  120. " \n",
  121. erase.addr);
  122. continue;
  123. } else if (ret < 0) {
  124. printf("\n%s: MTD get bad block failed: %d\n",
  125. mtd_device,
  126. ret);
  127. return -1;
  128. }
  129. }
  130. result = meminfo->erase(meminfo, &erase);
  131. if (result != 0) {
  132. printf("\n%s: MTD Erase failure: %d\n",
  133. mtd_device, result);
  134. continue;
  135. }
  136. /* format for JFFS2 ? */
  137. if (opts->jffs2) {
  138. chip->ops.len = chip->ops.ooblen = 64;
  139. chip->ops.datbuf = NULL;
  140. chip->ops.oobbuf = buf;
  141. chip->ops.ooboffs = chip->badblockpos & ~0x01;
  142. result = meminfo->write_oob(meminfo,
  143. erase.addr + meminfo->oobsize,
  144. &chip->ops);
  145. if (result != 0) {
  146. printf("\n%s: MTD writeoob failure: %d\n",
  147. mtd_device, result);
  148. continue;
  149. }
  150. else
  151. printf("%s: MTD writeoob at 0x%08x\n",mtd_device, erase.addr + meminfo->oobsize );
  152. }
  153. if (!opts->quiet) {
  154. unsigned long long n =(unsigned long long)
  155. (erase.addr + meminfo->erasesize - opts->offset)
  156. * 100;
  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%x -- %3d%% complete.",
  167. erase.addr, percent);
  168. if (opts->jffs2 && result == 0)
  169. printf(" Cleanmarker written at 0x%x.",
  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. #if 0
  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 meminfo 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(nand_info_t *meminfo, int tight)
  242. {
  243. int ret = 0;
  244. int status;
  245. struct nand_chip *this = meminfo->priv;
  246. /* select the NAND device */
  247. this->select_chip(meminfo, 0);
  248. this->cmdfunc(meminfo,
  249. (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
  250. -1, -1);
  251. /* call wait ready function */
  252. status = this->waitfunc(meminfo, this, FL_WRITING);
  253. /* see if device thinks it succeeded */
  254. if (status & 0x01) {
  255. ret = -1;
  256. }
  257. /* de-select the NAND device */
  258. this->select_chip(meminfo, -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 meminfo 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(nand_info_t *meminfo, ulong offset)
  277. {
  278. int ret = 0;
  279. int chipnr;
  280. int page;
  281. struct nand_chip *this = meminfo->priv;
  282. /* select the NAND device */
  283. chipnr = (int)(offset >> this->chip_shift);
  284. this->select_chip(meminfo, chipnr);
  285. if ((offset & (meminfo->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 >> this->page_shift);
  294. this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask);
  295. ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT
  296. | NAND_LOCK_STATUS_LOCK
  297. | NAND_LOCK_STATUS_UNLOCK);
  298. out:
  299. /* de-select the NAND device */
  300. this->select_chip(meminfo, -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 meminfo 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(nand_info_t *meminfo, ulong start, ulong length)
  315. {
  316. int ret = 0;
  317. int chipnr;
  318. int status;
  319. int page;
  320. struct nand_chip *this = meminfo->priv;
  321. printf ("nand_unlock: start: %08x, length: %d!\n",
  322. (int)start, (int)length);
  323. /* select the NAND device */
  324. chipnr = (int)(start >> this->chip_shift);
  325. this->select_chip(meminfo, chipnr);
  326. /* check the WP bit */
  327. this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1);
  328. if ((this->read_byte(meminfo) & 0x80) == 0) {
  329. printf ("nand_unlock: Device is write protected!\n");
  330. ret = -1;
  331. goto out;
  332. }
  333. if ((start & (meminfo->writesize - 1)) != 0) {
  334. printf ("nand_unlock: Start address must be beginning of "
  335. "nand page!\n");
  336. ret = -1;
  337. goto out;
  338. }
  339. if (length == 0 || (length & (meminfo->writesize - 1)) != 0) {
  340. printf ("nand_unlock: Length must be a multiple of nand page "
  341. "size!\n");
  342. ret = -1;
  343. goto out;
  344. }
  345. /* submit address of first page to unlock */
  346. page = (int)(start >> this->page_shift);
  347. this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask);
  348. /* submit ADDRESS of LAST page to unlock */
  349. page += (int)(length >> this->page_shift) - 1;
  350. this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask);
  351. /* call wait ready function */
  352. status = this->waitfunc(meminfo, this, FL_WRITING);
  353. /* see if device thinks it succeeded */
  354. if (status & 0x01) {
  355. /* there was an error */
  356. ret = -1;
  357. goto out;
  358. }
  359. out:
  360. /* de-select the NAND device */
  361. this->select_chip(meminfo, -1);
  362. return ret;
  363. }
  364. #endif
  365. /**
  366. * get_len_incl_bad
  367. *
  368. * Check if length including bad blocks fits into device.
  369. *
  370. * @param nand NAND device
  371. * @param offset offset in flash
  372. * @param length image length
  373. * @return image length including bad blocks
  374. */
  375. static size_t get_len_incl_bad (nand_info_t *nand, size_t offset,
  376. const size_t length)
  377. {
  378. size_t len_incl_bad = 0;
  379. size_t len_excl_bad = 0;
  380. size_t block_len;
  381. while (len_excl_bad < length) {
  382. block_len = nand->erasesize - (offset & (nand->erasesize - 1));
  383. if (!nand_block_isbad (nand, offset & ~(nand->erasesize - 1)))
  384. len_excl_bad += block_len;
  385. len_incl_bad += block_len;
  386. offset += block_len;
  387. if ((offset + len_incl_bad) >= nand->size)
  388. break;
  389. }
  390. return len_incl_bad;
  391. }
  392. /**
  393. * nand_write_skip_bad:
  394. *
  395. * Write image to NAND flash.
  396. * Blocks that are marked bad are skipped and the is written to the next
  397. * block instead as long as the image is short enough to fit even after
  398. * skipping the bad blocks.
  399. *
  400. * @param nand NAND device
  401. * @param offset offset in flash
  402. * @param length buffer length
  403. * @param buf buffer to read from
  404. * @return 0 in case of success
  405. */
  406. int nand_write_skip_bad(nand_info_t *nand, size_t offset, size_t *length,
  407. u_char *buffer)
  408. {
  409. int rval;
  410. size_t left_to_write = *length;
  411. size_t len_incl_bad;
  412. u_char *p_buffer = buffer;
  413. /* Reject writes, which are not page aligned */
  414. if ((offset & (nand->writesize - 1)) != 0 ||
  415. (*length & (nand->writesize - 1)) != 0) {
  416. printf ("Attempt to write non page aligned data\n");
  417. return -EINVAL;
  418. }
  419. len_incl_bad = get_len_incl_bad (nand, offset, *length);
  420. if ((offset + len_incl_bad) >= nand->size) {
  421. printf ("Attempt to write outside the flash area\n");
  422. return -EINVAL;
  423. }
  424. if (len_incl_bad == *length) {
  425. rval = nand_write (nand, offset, length, buffer);
  426. if (rval != 0) {
  427. printf ("NAND write to offset %x failed %d\n",
  428. offset, rval);
  429. return rval;
  430. }
  431. }
  432. while (left_to_write > 0) {
  433. size_t block_offset = offset & (nand->erasesize - 1);
  434. size_t write_size;
  435. if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  436. printf ("Skip bad block 0x%08x\n",
  437. offset & ~(nand->erasesize - 1));
  438. offset += nand->erasesize - block_offset;
  439. continue;
  440. }
  441. if (left_to_write < (nand->erasesize - block_offset))
  442. write_size = left_to_write;
  443. else
  444. write_size = nand->erasesize - block_offset;
  445. rval = nand_write (nand, offset, &write_size, p_buffer);
  446. if (rval != 0) {
  447. printf ("NAND write to offset %x failed %d\n",
  448. offset, rval);
  449. *length -= left_to_write;
  450. return rval;
  451. }
  452. left_to_write -= write_size;
  453. offset += write_size;
  454. p_buffer += write_size;
  455. }
  456. return 0;
  457. }
  458. /**
  459. * nand_read_skip_bad:
  460. *
  461. * Read image from NAND flash.
  462. * Blocks that are marked bad are skipped and the next block is readen
  463. * instead as long as the image is short enough to fit even after skipping the
  464. * bad blocks.
  465. *
  466. * @param nand NAND device
  467. * @param offset offset in flash
  468. * @param length buffer length, on return holds remaining bytes to read
  469. * @param buffer buffer to write to
  470. * @return 0 in case of success
  471. */
  472. int nand_read_skip_bad(nand_info_t *nand, size_t offset, size_t *length,
  473. u_char *buffer)
  474. {
  475. int rval;
  476. size_t left_to_read = *length;
  477. size_t len_incl_bad;
  478. u_char *p_buffer = buffer;
  479. len_incl_bad = get_len_incl_bad (nand, offset, *length);
  480. if ((offset + len_incl_bad) >= nand->size) {
  481. printf ("Attempt to read outside the flash area\n");
  482. return -EINVAL;
  483. }
  484. if (len_incl_bad == *length) {
  485. rval = nand_read (nand, offset, length, buffer);
  486. if (rval != 0) {
  487. printf ("NAND read from offset %x failed %d\n",
  488. offset, rval);
  489. return rval;
  490. }
  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%08x\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 %x 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. }