nand_util.c 22 KB

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