nand_util.c 22 KB

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