nand_util.c 22 KB

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