nand_util.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 <nand.h>
  39. #include <jffs2/jffs2.h>
  40. typedef struct erase_info erase_info_t;
  41. typedef struct mtd_info mtd_info_t;
  42. /* support only for native endian JFFS2 */
  43. #define cpu_to_je16(x) (x)
  44. #define cpu_to_je32(x) (x)
  45. /*****************************************************************************/
  46. static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)
  47. {
  48. return 0;
  49. }
  50. /**
  51. * nand_erase_opts: - erase NAND flash with support for various options
  52. * (jffs2 formating)
  53. *
  54. * @param meminfo NAND device to erase
  55. * @param opts options, @see struct nand_erase_options
  56. * @return 0 in case of success
  57. *
  58. * This code is ported from flash_eraseall.c from Linux mtd utils by
  59. * Arcom Control System Ltd.
  60. */
  61. int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
  62. {
  63. struct jffs2_unknown_node cleanmarker;
  64. int clmpos = 0;
  65. int clmlen = 8;
  66. erase_info_t erase;
  67. ulong erase_length;
  68. int isNAND;
  69. int bbtest = 1;
  70. int result;
  71. int percent_complete = -1;
  72. int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL;
  73. const char *mtd_device = meminfo->name;
  74. memset(&erase, 0, sizeof(erase));
  75. erase.mtd = meminfo;
  76. erase.len = meminfo->erasesize;
  77. erase.addr = opts->offset;
  78. erase_length = opts->length;
  79. isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0;
  80. if (opts->jffs2) {
  81. cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
  82. cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
  83. if (isNAND) {
  84. struct nand_oobinfo *oobinfo = &meminfo->oobinfo;
  85. /* check for autoplacement */
  86. if (oobinfo->useecc == MTD_NANDECC_AUTOPLACE) {
  87. /* get the position of the free bytes */
  88. if (!oobinfo->oobfree[0][1]) {
  89. printf(" Eeep. Autoplacement selected "
  90. "and no empty space in oob\n");
  91. return -1;
  92. }
  93. clmpos = oobinfo->oobfree[0][0];
  94. clmlen = oobinfo->oobfree[0][1];
  95. if (clmlen > 8)
  96. clmlen = 8;
  97. } else {
  98. /* legacy mode */
  99. switch (meminfo->oobsize) {
  100. case 8:
  101. clmpos = 6;
  102. clmlen = 2;
  103. break;
  104. case 16:
  105. clmpos = 8;
  106. clmlen = 8;
  107. break;
  108. case 64:
  109. clmpos = 16;
  110. clmlen = 8;
  111. break;
  112. }
  113. }
  114. cleanmarker.totlen = cpu_to_je32(8);
  115. } else {
  116. cleanmarker.totlen =
  117. cpu_to_je32(sizeof(struct jffs2_unknown_node));
  118. }
  119. cleanmarker.hdr_crc = cpu_to_je32(
  120. crc32_no_comp(0, (unsigned char *) &cleanmarker,
  121. sizeof(struct jffs2_unknown_node) - 4));
  122. }
  123. /* scrub option allows to erase badblock. To prevent internal
  124. * check from erase() method, set block check method to dummy
  125. * and disable bad block table while erasing.
  126. */
  127. if (opts->scrub) {
  128. struct nand_chip *priv_nand = meminfo->priv;
  129. nand_block_bad_old = priv_nand->block_bad;
  130. priv_nand->block_bad = nand_block_bad_scrub;
  131. /* we don't need the bad block table anymore...
  132. * after scrub, there are no bad blocks left!
  133. */
  134. if (priv_nand->bbt) {
  135. kfree(priv_nand->bbt);
  136. }
  137. priv_nand->bbt = NULL;
  138. }
  139. if (erase_length < meminfo->erasesize) {
  140. printf("Warning: Erase size 0x%08x smaller than one " \
  141. "erase block 0x%08x\n",erase_length, meminfo->erasesize);
  142. printf(" Erasing 0x%08x instead\n", meminfo->erasesize);
  143. erase_length = meminfo->erasesize;
  144. }
  145. for (;
  146. erase.addr < opts->offset + erase_length;
  147. erase.addr += meminfo->erasesize) {
  148. WATCHDOG_RESET ();
  149. if (!opts->scrub && bbtest) {
  150. int ret = meminfo->block_isbad(meminfo, erase.addr);
  151. if (ret > 0) {
  152. if (!opts->quiet)
  153. printf("\rSkipping bad block at "
  154. "0x%08x "
  155. " \n",
  156. erase.addr);
  157. continue;
  158. } else if (ret < 0) {
  159. printf("\n%s: MTD get bad block failed: %d\n",
  160. mtd_device,
  161. ret);
  162. return -1;
  163. }
  164. }
  165. result = meminfo->erase(meminfo, &erase);
  166. if (result != 0) {
  167. printf("\n%s: MTD Erase failure: %d\n",
  168. mtd_device, result);
  169. continue;
  170. }
  171. /* format for JFFS2 ? */
  172. if (opts->jffs2) {
  173. /* write cleanmarker */
  174. if (isNAND) {
  175. size_t written;
  176. result = meminfo->write_oob(meminfo,
  177. erase.addr + clmpos,
  178. clmlen,
  179. &written,
  180. (unsigned char *)
  181. &cleanmarker);
  182. if (result != 0) {
  183. printf("\n%s: MTD writeoob failure: %d\n",
  184. mtd_device, result);
  185. continue;
  186. }
  187. } else {
  188. printf("\n%s: this erase routine only supports"
  189. " NAND devices!\n",
  190. mtd_device);
  191. }
  192. }
  193. if (!opts->quiet) {
  194. unsigned long long n =(unsigned long long)
  195. (erase.addr + meminfo->erasesize - opts->offset)
  196. * 100;
  197. int percent;
  198. do_div(n, erase_length);
  199. percent = (int)n;
  200. /* output progress message only at whole percent
  201. * steps to reduce the number of messages printed
  202. * on (slow) serial consoles
  203. */
  204. if (percent != percent_complete) {
  205. percent_complete = percent;
  206. printf("\rErasing at 0x%x -- %3d%% complete.",
  207. erase.addr, percent);
  208. if (opts->jffs2 && result == 0)
  209. printf(" Cleanmarker written at 0x%x.",
  210. erase.addr);
  211. }
  212. }
  213. }
  214. if (!opts->quiet)
  215. printf("\n");
  216. if (nand_block_bad_old) {
  217. struct nand_chip *priv_nand = meminfo->priv;
  218. priv_nand->block_bad = nand_block_bad_old;
  219. priv_nand->scan_bbt(meminfo);
  220. }
  221. return 0;
  222. }
  223. #define MAX_PAGE_SIZE 2048
  224. #define MAX_OOB_SIZE 64
  225. /*
  226. * buffer array used for writing data
  227. */
  228. static unsigned char data_buf[MAX_PAGE_SIZE];
  229. static unsigned char oob_buf[MAX_OOB_SIZE];
  230. /* OOB layouts to pass into the kernel as default */
  231. static struct nand_oobinfo none_oobinfo = {
  232. .useecc = MTD_NANDECC_OFF,
  233. };
  234. static struct nand_oobinfo jffs2_oobinfo = {
  235. .useecc = MTD_NANDECC_PLACE,
  236. .eccbytes = 6,
  237. .eccpos = { 0, 1, 2, 3, 6, 7 }
  238. };
  239. static struct nand_oobinfo yaffs_oobinfo = {
  240. .useecc = MTD_NANDECC_PLACE,
  241. .eccbytes = 6,
  242. .eccpos = { 8, 9, 10, 13, 14, 15}
  243. };
  244. static struct nand_oobinfo autoplace_oobinfo = {
  245. .useecc = MTD_NANDECC_AUTOPLACE
  246. };
  247. /**
  248. * nand_write_opts: - write image to NAND flash with support for various options
  249. *
  250. * @param meminfo NAND device to erase
  251. * @param opts write options (@see nand_write_options)
  252. * @return 0 in case of success
  253. *
  254. * This code is ported from nandwrite.c from Linux mtd utils by
  255. * Steven J. Hill and Thomas Gleixner.
  256. */
  257. int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts)
  258. {
  259. int imglen = 0;
  260. int pagelen;
  261. int baderaseblock;
  262. int blockstart = -1;
  263. loff_t offs;
  264. int readlen;
  265. int oobinfochanged = 0;
  266. int percent_complete = -1;
  267. struct nand_oobinfo old_oobinfo;
  268. ulong mtdoffset = opts->offset;
  269. ulong erasesize_blockalign;
  270. u_char *buffer = opts->buffer;
  271. size_t written;
  272. int result;
  273. if (opts->pad && opts->writeoob) {
  274. printf("Can't pad when oob data is present.\n");
  275. return -1;
  276. }
  277. /* set erasesize to specified number of blocks - to match
  278. * jffs2 (virtual) block size */
  279. if (opts->blockalign == 0) {
  280. erasesize_blockalign = meminfo->erasesize;
  281. } else {
  282. erasesize_blockalign = meminfo->erasesize * opts->blockalign;
  283. }
  284. /* make sure device page sizes are valid */
  285. if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
  286. && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
  287. && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
  288. printf("Unknown flash (not normal NAND)\n");
  289. return -1;
  290. }
  291. /* read the current oob info */
  292. memcpy(&old_oobinfo, &meminfo->oobinfo, sizeof(old_oobinfo));
  293. /* write without ecc? */
  294. if (opts->noecc) {
  295. memcpy(&meminfo->oobinfo, &none_oobinfo,
  296. sizeof(meminfo->oobinfo));
  297. oobinfochanged = 1;
  298. }
  299. /* autoplace ECC? */
  300. if (opts->autoplace && (old_oobinfo.useecc != MTD_NANDECC_AUTOPLACE)) {
  301. memcpy(&meminfo->oobinfo, &autoplace_oobinfo,
  302. sizeof(meminfo->oobinfo));
  303. oobinfochanged = 1;
  304. }
  305. /* force OOB layout for jffs2 or yaffs? */
  306. if (opts->forcejffs2 || opts->forceyaffs) {
  307. struct nand_oobinfo *oobsel =
  308. opts->forcejffs2 ? &jffs2_oobinfo : &yaffs_oobinfo;
  309. if (meminfo->oobsize == 8) {
  310. if (opts->forceyaffs) {
  311. printf("YAFSS cannot operate on "
  312. "256 Byte page size\n");
  313. goto restoreoob;
  314. }
  315. /* Adjust number of ecc bytes */
  316. jffs2_oobinfo.eccbytes = 3;
  317. }
  318. memcpy(&meminfo->oobinfo, oobsel, sizeof(meminfo->oobinfo));
  319. }
  320. /* get image length */
  321. imglen = opts->length;
  322. pagelen = meminfo->oobblock
  323. + ((opts->writeoob != 0) ? meminfo->oobsize : 0);
  324. /* check, if file is pagealigned */
  325. if ((!opts->pad) && ((imglen % pagelen) != 0)) {
  326. printf("Input block length is not page aligned\n");
  327. goto restoreoob;
  328. }
  329. /* check, if length fits into device */
  330. if (((imglen / pagelen) * meminfo->oobblock)
  331. > (meminfo->size - opts->offset)) {
  332. printf("Image %d bytes, NAND page %d bytes, "
  333. "OOB area %u bytes, device size %u bytes\n",
  334. imglen, pagelen, meminfo->oobblock, meminfo->size);
  335. printf("Input block does not fit into device\n");
  336. goto restoreoob;
  337. }
  338. if (!opts->quiet)
  339. printf("\n");
  340. /* get data from input and write to the device */
  341. while (imglen && (mtdoffset < meminfo->size)) {
  342. WATCHDOG_RESET ();
  343. /*
  344. * new eraseblock, check for bad block(s). Stay in the
  345. * loop to be sure if the offset changes because of
  346. * a bad block, that the next block that will be
  347. * written to is also checked. Thus avoiding errors if
  348. * the block(s) after the skipped block(s) is also bad
  349. * (number of blocks depending on the blockalign
  350. */
  351. while (blockstart != (mtdoffset & (~erasesize_blockalign+1))) {
  352. blockstart = mtdoffset & (~erasesize_blockalign+1);
  353. offs = blockstart;
  354. baderaseblock = 0;
  355. /* check all the blocks in an erase block for
  356. * bad blocks */
  357. do {
  358. int ret = meminfo->block_isbad(meminfo, offs);
  359. if (ret < 0) {
  360. printf("Bad block check failed\n");
  361. goto restoreoob;
  362. }
  363. if (ret == 1) {
  364. baderaseblock = 1;
  365. if (!opts->quiet)
  366. printf("\rBad block at 0x%lx "
  367. "in erase block from "
  368. "0x%x will be skipped\n",
  369. (long) offs,
  370. blockstart);
  371. }
  372. if (baderaseblock) {
  373. mtdoffset = blockstart
  374. + erasesize_blockalign;
  375. }
  376. offs += erasesize_blockalign
  377. / opts->blockalign;
  378. } while (offs < blockstart + erasesize_blockalign);
  379. }
  380. readlen = meminfo->oobblock;
  381. if (opts->pad && (imglen < readlen)) {
  382. readlen = imglen;
  383. memset(data_buf + readlen, 0xff,
  384. meminfo->oobblock - readlen);
  385. }
  386. /* read page data from input memory buffer */
  387. memcpy(data_buf, buffer, readlen);
  388. buffer += readlen;
  389. if (opts->writeoob) {
  390. /* read OOB data from input memory block, exit
  391. * on failure */
  392. memcpy(oob_buf, buffer, meminfo->oobsize);
  393. buffer += meminfo->oobsize;
  394. /* write OOB data first, as ecc will be placed
  395. * in there*/
  396. result = meminfo->write_oob(meminfo,
  397. mtdoffset,
  398. meminfo->oobsize,
  399. &written,
  400. (unsigned char *)
  401. &oob_buf);
  402. if (result != 0) {
  403. printf("\nMTD writeoob failure: %d\n",
  404. result);
  405. goto restoreoob;
  406. }
  407. imglen -= meminfo->oobsize;
  408. }
  409. /* write out the page data */
  410. result = meminfo->write(meminfo,
  411. mtdoffset,
  412. meminfo->oobblock,
  413. &written,
  414. (unsigned char *) &data_buf);
  415. if (result != 0) {
  416. printf("writing NAND page at offset 0x%lx failed\n",
  417. mtdoffset);
  418. goto restoreoob;
  419. }
  420. imglen -= readlen;
  421. if (!opts->quiet) {
  422. unsigned long long n = (unsigned long long)
  423. (opts->length-imglen) * 100;
  424. int percent;
  425. do_div(n, opts->length);
  426. percent = (int)n;
  427. /* output progress message only at whole percent
  428. * steps to reduce the number of messages printed
  429. * on (slow) serial consoles
  430. */
  431. if (percent != percent_complete) {
  432. printf("\rWriting data at 0x%x "
  433. "-- %3d%% complete.",
  434. mtdoffset, percent);
  435. percent_complete = percent;
  436. }
  437. }
  438. mtdoffset += meminfo->oobblock;
  439. }
  440. if (!opts->quiet)
  441. printf("\n");
  442. restoreoob:
  443. if (oobinfochanged) {
  444. memcpy(&meminfo->oobinfo, &old_oobinfo,
  445. sizeof(meminfo->oobinfo));
  446. }
  447. if (imglen > 0) {
  448. printf("Data did not fit into device, due to bad blocks\n");
  449. return -1;
  450. }
  451. /* return happy */
  452. return 0;
  453. }
  454. /**
  455. * nand_read_opts: - read image from NAND flash with support for various options
  456. *
  457. * @param meminfo NAND device to erase
  458. * @param opts read options (@see struct nand_read_options)
  459. * @return 0 in case of success
  460. *
  461. */
  462. int nand_read_opts(nand_info_t *meminfo, const nand_read_options_t *opts)
  463. {
  464. int imglen = opts->length;
  465. int pagelen;
  466. int baderaseblock;
  467. int blockstart = -1;
  468. int percent_complete = -1;
  469. loff_t offs;
  470. size_t readlen;
  471. ulong mtdoffset = opts->offset;
  472. u_char *buffer = opts->buffer;
  473. int result;
  474. /* make sure device page sizes are valid */
  475. if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
  476. && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
  477. && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
  478. printf("Unknown flash (not normal NAND)\n");
  479. return -1;
  480. }
  481. pagelen = meminfo->oobblock
  482. + ((opts->readoob != 0) ? meminfo->oobsize : 0);
  483. /* check, if length is not larger than device */
  484. if (((imglen / pagelen) * meminfo->oobblock)
  485. > (meminfo->size - opts->offset)) {
  486. printf("Image %d bytes, NAND page %d bytes, "
  487. "OOB area %u bytes, device size %u bytes\n",
  488. imglen, pagelen, meminfo->oobblock, meminfo->size);
  489. printf("Input block is larger than device\n");
  490. return -1;
  491. }
  492. if (!opts->quiet)
  493. printf("\n");
  494. /* get data from input and write to the device */
  495. while (imglen && (mtdoffset < meminfo->size)) {
  496. WATCHDOG_RESET ();
  497. /*
  498. * new eraseblock, check for bad block(s). Stay in the
  499. * loop to be sure if the offset changes because of
  500. * a bad block, that the next block that will be
  501. * written to is also checked. Thus avoiding errors if
  502. * the block(s) after the skipped block(s) is also bad
  503. * (number of blocks depending on the blockalign
  504. */
  505. while (blockstart != (mtdoffset & (~meminfo->erasesize+1))) {
  506. blockstart = mtdoffset & (~meminfo->erasesize+1);
  507. offs = blockstart;
  508. baderaseblock = 0;
  509. /* check all the blocks in an erase block for
  510. * bad blocks */
  511. do {
  512. int ret = meminfo->block_isbad(meminfo, offs);
  513. if (ret < 0) {
  514. printf("Bad block check failed\n");
  515. return -1;
  516. }
  517. if (ret == 1) {
  518. baderaseblock = 1;
  519. if (!opts->quiet)
  520. printf("\rBad block at 0x%lx "
  521. "in erase block from "
  522. "0x%x will be skipped\n",
  523. (long) offs,
  524. blockstart);
  525. }
  526. if (baderaseblock) {
  527. mtdoffset = blockstart
  528. + meminfo->erasesize;
  529. }
  530. offs += meminfo->erasesize;
  531. } while (offs < blockstart + meminfo->erasesize);
  532. }
  533. /* read page data to memory buffer */
  534. result = meminfo->read(meminfo,
  535. mtdoffset,
  536. meminfo->oobblock,
  537. &readlen,
  538. (unsigned char *) &data_buf);
  539. if (result != 0) {
  540. printf("reading NAND page at offset 0x%lx failed\n",
  541. mtdoffset);
  542. return -1;
  543. }
  544. if (imglen < readlen) {
  545. readlen = imglen;
  546. }
  547. memcpy(buffer, data_buf, readlen);
  548. buffer += readlen;
  549. imglen -= readlen;
  550. if (opts->readoob) {
  551. result = meminfo->read_oob(meminfo,
  552. mtdoffset,
  553. meminfo->oobsize,
  554. &readlen,
  555. (unsigned char *)
  556. &oob_buf);
  557. if (result != 0) {
  558. printf("\nMTD readoob failure: %d\n",
  559. result);
  560. return -1;
  561. }
  562. if (imglen < readlen) {
  563. readlen = imglen;
  564. }
  565. memcpy(buffer, oob_buf, readlen);
  566. buffer += readlen;
  567. imglen -= readlen;
  568. }
  569. if (!opts->quiet) {
  570. unsigned long long n = (unsigned long long)
  571. (opts->length-imglen) * 100;
  572. int percent;
  573. do_div(n, opts->length);
  574. percent = (int)n;
  575. /* output progress message only at whole percent
  576. * steps to reduce the number of messages printed
  577. * on (slow) serial consoles
  578. */
  579. if (percent != percent_complete) {
  580. if (!opts->quiet)
  581. printf("\rReading data from 0x%x "
  582. "-- %3d%% complete.",
  583. mtdoffset, percent);
  584. percent_complete = percent;
  585. }
  586. }
  587. mtdoffset += meminfo->oobblock;
  588. }
  589. if (!opts->quiet)
  590. printf("\n");
  591. if (imglen > 0) {
  592. printf("Could not read entire image due to bad blocks\n");
  593. return -1;
  594. }
  595. /* return happy */
  596. return 0;
  597. }
  598. /******************************************************************************
  599. * Support for locking / unlocking operations of some NAND devices
  600. *****************************************************************************/
  601. #define NAND_CMD_LOCK 0x2a
  602. #define NAND_CMD_LOCK_TIGHT 0x2c
  603. #define NAND_CMD_UNLOCK1 0x23
  604. #define NAND_CMD_UNLOCK2 0x24
  605. #define NAND_CMD_LOCK_STATUS 0x7a
  606. /**
  607. * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
  608. * state
  609. *
  610. * @param meminfo nand mtd instance
  611. * @param tight bring device in lock tight mode
  612. *
  613. * @return 0 on success, -1 in case of error
  614. *
  615. * The lock / lock-tight command only applies to the whole chip. To get some
  616. * parts of the chip lock and others unlocked use the following sequence:
  617. *
  618. * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
  619. * - Call nand_unlock() once for each consecutive area to be unlocked
  620. * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
  621. *
  622. * If the device is in lock-tight state software can't change the
  623. * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
  624. * calls will fail. It is only posible to leave lock-tight state by
  625. * an hardware signal (low pulse on _WP pin) or by power down.
  626. */
  627. int nand_lock(nand_info_t *meminfo, int tight)
  628. {
  629. int ret = 0;
  630. int status;
  631. struct nand_chip *this = meminfo->priv;
  632. /* select the NAND device */
  633. this->select_chip(meminfo, 0);
  634. this->cmdfunc(meminfo,
  635. (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
  636. -1, -1);
  637. /* call wait ready function */
  638. status = this->waitfunc(meminfo, this, FL_WRITING);
  639. /* see if device thinks it succeeded */
  640. if (status & 0x01) {
  641. ret = -1;
  642. }
  643. /* de-select the NAND device */
  644. this->select_chip(meminfo, -1);
  645. return ret;
  646. }
  647. /**
  648. * nand_get_lock_status: - query current lock state from one page of NAND
  649. * flash
  650. *
  651. * @param meminfo nand mtd instance
  652. * @param offset page address to query (muss be page aligned!)
  653. *
  654. * @return -1 in case of error
  655. * >0 lock status:
  656. * bitfield with the following combinations:
  657. * NAND_LOCK_STATUS_TIGHT: page in tight state
  658. * NAND_LOCK_STATUS_LOCK: page locked
  659. * NAND_LOCK_STATUS_UNLOCK: page unlocked
  660. *
  661. */
  662. int nand_get_lock_status(nand_info_t *meminfo, ulong offset)
  663. {
  664. int ret = 0;
  665. int chipnr;
  666. int page;
  667. struct nand_chip *this = meminfo->priv;
  668. /* select the NAND device */
  669. chipnr = (int)(offset >> this->chip_shift);
  670. this->select_chip(meminfo, chipnr);
  671. if ((offset & (meminfo->oobblock - 1)) != 0) {
  672. printf ("nand_get_lock_status: "
  673. "Start address must be beginning of "
  674. "nand page!\n");
  675. ret = -1;
  676. goto out;
  677. }
  678. /* check the Lock Status */
  679. page = (int)(offset >> this->page_shift);
  680. this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask);
  681. ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT
  682. | NAND_LOCK_STATUS_LOCK
  683. | NAND_LOCK_STATUS_UNLOCK);
  684. out:
  685. /* de-select the NAND device */
  686. this->select_chip(meminfo, -1);
  687. return ret;
  688. }
  689. /**
  690. * nand_unlock: - Unlock area of NAND pages
  691. * only one consecutive area can be unlocked at one time!
  692. *
  693. * @param meminfo nand mtd instance
  694. * @param start start byte address
  695. * @param length number of bytes to unlock (must be a multiple of
  696. * page size nand->oobblock)
  697. *
  698. * @return 0 on success, -1 in case of error
  699. */
  700. int nand_unlock(nand_info_t *meminfo, ulong start, ulong length)
  701. {
  702. int ret = 0;
  703. int chipnr;
  704. int status;
  705. int page;
  706. struct nand_chip *this = meminfo->priv;
  707. printf ("nand_unlock: start: %08x, length: %d!\n",
  708. (int)start, (int)length);
  709. /* select the NAND device */
  710. chipnr = (int)(start >> this->chip_shift);
  711. this->select_chip(meminfo, chipnr);
  712. /* check the WP bit */
  713. this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1);
  714. if ((this->read_byte(meminfo) & 0x80) == 0) {
  715. printf ("nand_unlock: Device is write protected!\n");
  716. ret = -1;
  717. goto out;
  718. }
  719. if ((start & (meminfo->oobblock - 1)) != 0) {
  720. printf ("nand_unlock: Start address must be beginning of "
  721. "nand page!\n");
  722. ret = -1;
  723. goto out;
  724. }
  725. if (length == 0 || (length & (meminfo->oobblock - 1)) != 0) {
  726. printf ("nand_unlock: Length must be a multiple of nand page "
  727. "size!\n");
  728. ret = -1;
  729. goto out;
  730. }
  731. /* submit address of first page to unlock */
  732. page = (int)(start >> this->page_shift);
  733. this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask);
  734. /* submit ADDRESS of LAST page to unlock */
  735. page += (int)(length >> this->page_shift) - 1;
  736. this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask);
  737. /* call wait ready function */
  738. status = this->waitfunc(meminfo, this, FL_WRITING);
  739. /* see if device thinks it succeeded */
  740. if (status & 0x01) {
  741. /* there was an error */
  742. ret = -1;
  743. goto out;
  744. }
  745. out:
  746. /* de-select the NAND device */
  747. this->select_chip(meminfo, -1);
  748. return ret;
  749. }
  750. #endif