nand_util.c 22 KB

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