move drivers/nand to drivers/mtd/nand
[barebox-mini2440.git] / drivers / mtd / nand / nand_util.c
blobd57294e6247c4a63cfd9ab2ba9b9bbbdf58577c8
1 /*
2 * drivers/nand/nand_util.c
4 * Copyright (C) 2006 by Weiss-Electronic GmbH.
5 * All rights reserved.
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)
14 * See file CREDITS for list of people who contributed to this
15 * project.
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.
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.
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
33 #include <common.h>
34 #include <command.h>
35 #include <watchdog.h>
36 #include <malloc.h>
38 #include <nand.h>
39 //#include <jffs2/jffs2.h>
41 typedef struct erase_info erase_info_t;
42 typedef struct mtd_info mtd_info_t;
44 /* support only for native endian JFFS2 */
45 #define cpu_to_je16(x) (x)
46 #define cpu_to_je32(x) (x)
48 /*****************************************************************************/
49 static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)
51 return 0;
54 /**
55 * nand_erase_opts: - erase NAND flash with support for various options
56 * (jffs2 formating)
58 * @param meminfo NAND device to erase
59 * @param opts options, @see struct nand_erase_options
60 * @return 0 in case of success
62 * This code is ported from flash_eraseall.c from Linux mtd utils by
63 * Arcom Control System Ltd.
65 int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
67 struct jffs2_unknown_node cleanmarker;
68 int clmpos = 0;
69 int clmlen = 8;
70 erase_info_t erase;
71 ulong erase_length;
72 int isNAND;
73 int bbtest = 1;
74 int result;
75 int percent_complete = -1;
76 int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL;
77 const char *mtd_device = meminfo->name;
79 memset(&erase, 0, sizeof(erase));
81 erase.mtd = meminfo;
82 erase.len = meminfo->erasesize;
83 erase.addr = opts->offset;
84 erase_length = opts->length;
86 isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0;
88 if (opts->jffs2) {
89 cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
90 cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
91 if (isNAND) {
92 struct nand_oobinfo *oobinfo = &meminfo->oobinfo;
94 /* check for autoplacement */
95 if (oobinfo->useecc == MTD_NANDECC_AUTOPLACE) {
96 /* get the position of the free bytes */
97 if (!oobinfo->oobfree[0][1]) {
98 printf(" Eeep. Autoplacement selected "
99 "and no empty space in oob\n");
100 return -1;
102 clmpos = oobinfo->oobfree[0][0];
103 clmlen = oobinfo->oobfree[0][1];
104 if (clmlen > 8)
105 clmlen = 8;
106 } else {
107 /* legacy mode */
108 switch (meminfo->oobsize) {
109 case 8:
110 clmpos = 6;
111 clmlen = 2;
112 break;
113 case 16:
114 clmpos = 8;
115 clmlen = 8;
116 break;
117 case 64:
118 clmpos = 16;
119 clmlen = 8;
120 break;
124 cleanmarker.totlen = cpu_to_je32(8);
125 } else {
126 cleanmarker.totlen =
127 cpu_to_je32(sizeof(struct jffs2_unknown_node));
129 cleanmarker.hdr_crc = cpu_to_je32(
130 crc32_no_comp(0, (unsigned char *) &cleanmarker,
131 sizeof(struct jffs2_unknown_node) - 4));
134 /* scrub option allows to erase badblock. To prevent internal
135 * check from erase() method, set block check method to dummy
136 * and disable bad block table while erasing.
138 if (opts->scrub) {
139 struct nand_chip *priv_nand = meminfo->priv;
141 nand_block_bad_old = priv_nand->block_bad;
142 priv_nand->block_bad = nand_block_bad_scrub;
143 /* we don't need the bad block table anymore...
144 * after scrub, there are no bad blocks left!
146 if (priv_nand->bbt) {
147 kfree(priv_nand->bbt);
149 priv_nand->bbt = NULL;
152 for (;
153 erase.addr < opts->offset + erase_length;
154 erase.addr += meminfo->erasesize) {
156 WATCHDOG_RESET ();
158 if (!opts->scrub && bbtest) {
159 int ret = meminfo->block_isbad(meminfo, erase.addr);
160 if (ret > 0) {
161 if (!opts->quiet)
162 printf("\rSkipping bad block at "
163 "0x%08x "
164 " \n",
165 erase.addr);
166 continue;
168 } else if (ret < 0) {
169 printf("\n%s: MTD get bad block failed: %d\n",
170 mtd_device,
171 ret);
172 return -1;
176 result = meminfo->erase(meminfo, &erase);
177 if (result != 0) {
178 printf("\n%s: MTD Erase failure: %d\n",
179 mtd_device, result);
180 continue;
183 /* format for JFFS2 ? */
184 if (opts->jffs2) {
186 /* write cleanmarker */
187 if (isNAND) {
188 size_t written;
189 result = meminfo->write_oob(meminfo,
190 erase.addr + clmpos,
191 clmlen,
192 &written,
193 (unsigned char *)
194 &cleanmarker);
195 if (result != 0) {
196 printf("\n%s: MTD writeoob failure: %d\n",
197 mtd_device, result);
198 continue;
200 } else {
201 printf("\n%s: this erase routine only supports"
202 " NAND devices!\n",
203 mtd_device);
207 if (!opts->quiet) {
208 int percent = (int)
209 ((unsigned long long)
210 (erase.addr+meminfo->erasesize-opts->offset)
211 * 100 / erase_length);
213 /* output progress message only at whole percent
214 * steps to reduce the number of messages printed
215 * on (slow) serial consoles
217 if (percent != percent_complete) {
218 percent_complete = percent;
220 printf("\rErasing at 0x%x -- %3d%% complete.",
221 erase.addr, percent);
223 if (opts->jffs2 && result == 0)
224 printf(" Cleanmarker written at 0x%x.",
225 erase.addr);
229 if (!opts->quiet)
230 printf("\n");
232 if (nand_block_bad_old) {
233 struct nand_chip *priv_nand = meminfo->priv;
235 priv_nand->block_bad = nand_block_bad_old;
236 priv_nand->scan_bbt(meminfo);
239 return 0;
242 #define MAX_PAGE_SIZE 2048
243 #define MAX_OOB_SIZE 64
246 * buffer array used for writing data
248 static unsigned char data_buf[MAX_PAGE_SIZE];
249 static unsigned char oob_buf[MAX_OOB_SIZE];
251 /* OOB layouts to pass into the kernel as default */
252 static struct nand_oobinfo none_oobinfo = {
253 .useecc = MTD_NANDECC_OFF,
256 static struct nand_oobinfo jffs2_oobinfo = {
257 .useecc = MTD_NANDECC_PLACE,
258 .eccbytes = 6,
259 .eccpos = { 0, 1, 2, 3, 6, 7 }
262 static struct nand_oobinfo yaffs_oobinfo = {
263 .useecc = MTD_NANDECC_PLACE,
264 .eccbytes = 6,
265 .eccpos = { 8, 9, 10, 13, 14, 15}
268 static struct nand_oobinfo autoplace_oobinfo = {
269 .useecc = MTD_NANDECC_AUTOPLACE
273 * nand_write_opts: - write image to NAND flash with support for various options
275 * @param meminfo NAND device to erase
276 * @param opts write options (@see nand_write_options)
277 * @return 0 in case of success
279 * This code is ported from nandwrite.c from Linux mtd utils by
280 * Steven J. Hill and Thomas Gleixner.
282 int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts)
284 int imglen = 0;
285 int pagelen;
286 int baderaseblock;
287 int blockstart = -1;
288 loff_t offs;
289 int readlen;
290 int oobinfochanged = 0;
291 int percent_complete = -1;
292 struct nand_oobinfo old_oobinfo;
293 ulong mtdoffset = opts->offset;
294 ulong erasesize_blockalign;
295 u_char *buffer = opts->buffer;
296 size_t written;
297 int result;
299 if (opts->pad && opts->writeoob) {
300 printf("Can't pad when oob data is present.\n");
301 return -1;
304 /* set erasesize to specified number of blocks - to match
305 * jffs2 (virtual) block size */
306 if (opts->blockalign == 0) {
307 erasesize_blockalign = meminfo->erasesize;
308 } else {
309 erasesize_blockalign = meminfo->erasesize * opts->blockalign;
312 /* make sure device page sizes are valid */
313 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
314 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
315 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
316 printf("Unknown flash (not normal NAND)\n");
317 return -1;
320 /* read the current oob info */
321 memcpy(&old_oobinfo, &meminfo->oobinfo, sizeof(old_oobinfo));
323 /* write without ecc? */
324 if (opts->noecc) {
325 memcpy(&meminfo->oobinfo, &none_oobinfo,
326 sizeof(meminfo->oobinfo));
327 oobinfochanged = 1;
330 /* autoplace ECC? */
331 if (opts->autoplace && (old_oobinfo.useecc != MTD_NANDECC_AUTOPLACE)) {
333 memcpy(&meminfo->oobinfo, &autoplace_oobinfo,
334 sizeof(meminfo->oobinfo));
335 oobinfochanged = 1;
338 /* force OOB layout for jffs2 or yaffs? */
339 if (opts->forcejffs2 || opts->forceyaffs) {
340 struct nand_oobinfo *oobsel =
341 opts->forcejffs2 ? &jffs2_oobinfo : &yaffs_oobinfo;
343 if (meminfo->oobsize == 8) {
344 if (opts->forceyaffs) {
345 printf("YAFSS cannot operate on "
346 "256 Byte page size\n");
347 goto restoreoob;
349 /* Adjust number of ecc bytes */
350 jffs2_oobinfo.eccbytes = 3;
353 memcpy(&meminfo->oobinfo, oobsel, sizeof(meminfo->oobinfo));
356 /* get image length */
357 imglen = opts->length;
358 pagelen = meminfo->oobblock
359 + ((opts->writeoob != 0) ? meminfo->oobsize : 0);
361 /* check, if file is pagealigned */
362 if ((!opts->pad) && ((imglen % pagelen) != 0)) {
363 printf("Input block length is not page aligned\n");
364 goto restoreoob;
367 /* check, if length fits into device */
368 if (((imglen / pagelen) * meminfo->oobblock)
369 > (meminfo->size - opts->offset)) {
370 printf("Image %d bytes, NAND page %d bytes, "
371 "OOB area %u bytes, device size %u bytes\n",
372 imglen, pagelen, meminfo->oobblock, meminfo->size);
373 printf("Input block does not fit into device\n");
374 goto restoreoob;
377 if (!opts->quiet)
378 printf("\n");
380 /* get data from input and write to the device */
381 while (imglen && (mtdoffset < meminfo->size)) {
383 WATCHDOG_RESET ();
386 * new eraseblock, check for bad block(s). Stay in the
387 * loop to be sure if the offset changes because of
388 * a bad block, that the next block that will be
389 * written to is also checked. Thus avoiding errors if
390 * the block(s) after the skipped block(s) is also bad
391 * (number of blocks depending on the blockalign
393 while (blockstart != (mtdoffset & (~erasesize_blockalign+1))) {
394 blockstart = mtdoffset & (~erasesize_blockalign+1);
395 offs = blockstart;
396 baderaseblock = 0;
398 /* check all the blocks in an erase block for
399 * bad blocks */
400 do {
401 int ret = meminfo->block_isbad(meminfo, offs);
403 if (ret < 0) {
404 printf("Bad block check failed\n");
405 goto restoreoob;
407 if (ret == 1) {
408 baderaseblock = 1;
409 if (!opts->quiet)
410 printf("\rBad block at 0x%lx "
411 "in erase block from "
412 "0x%x will be skipped\n",
413 (long) offs,
414 blockstart);
417 if (baderaseblock) {
418 mtdoffset = blockstart
419 + erasesize_blockalign;
421 offs += erasesize_blockalign
422 / opts->blockalign;
423 } while (offs < blockstart + erasesize_blockalign);
426 readlen = meminfo->oobblock;
427 if (opts->pad && (imglen < readlen)) {
428 readlen = imglen;
429 memset(data_buf + readlen, 0xff,
430 meminfo->oobblock - readlen);
433 /* read page data from input memory buffer */
434 memcpy(data_buf, buffer, readlen);
435 buffer += readlen;
437 if (opts->writeoob) {
438 /* read OOB data from input memory block, exit
439 * on failure */
440 memcpy(oob_buf, buffer, meminfo->oobsize);
441 buffer += meminfo->oobsize;
443 /* write OOB data first, as ecc will be placed
444 * in there*/
445 result = meminfo->write_oob(meminfo,
446 mtdoffset,
447 meminfo->oobsize,
448 &written,
449 (unsigned char *)
450 &oob_buf);
452 if (result != 0) {
453 printf("\nMTD writeoob failure: %d\n",
454 result);
455 goto restoreoob;
457 imglen -= meminfo->oobsize;
460 /* write out the page data */
461 result = meminfo->write(meminfo,
462 mtdoffset,
463 meminfo->oobblock,
464 &written,
465 (unsigned char *) &data_buf);
467 if (result != 0) {
468 printf("writing NAND page at offset 0x%lx failed\n",
469 mtdoffset);
470 goto restoreoob;
472 imglen -= readlen;
474 if (!opts->quiet) {
475 int percent = (int)
476 ((unsigned long long)
477 (opts->length-imglen) * 100
478 / opts->length);
479 /* output progress message only at whole percent
480 * steps to reduce the number of messages printed
481 * on (slow) serial consoles
483 if (percent != percent_complete) {
484 printf("\rWriting data at 0x%x "
485 "-- %3d%% complete.",
486 mtdoffset, percent);
487 percent_complete = percent;
491 mtdoffset += meminfo->oobblock;
494 if (!opts->quiet)
495 printf("\n");
497 restoreoob:
498 if (oobinfochanged) {
499 memcpy(&meminfo->oobinfo, &old_oobinfo,
500 sizeof(meminfo->oobinfo));
503 if (imglen > 0) {
504 printf("Data did not fit into device, due to bad blocks\n");
505 return -1;
508 /* return happy */
509 return 0;
513 * nand_read_opts: - read image from NAND flash with support for various options
515 * @param meminfo NAND device to erase
516 * @param opts read options (@see struct nand_read_options)
517 * @return 0 in case of success
520 int nand_read_opts(nand_info_t *meminfo, const nand_read_options_t *opts)
522 int imglen = opts->length;
523 int pagelen;
524 int baderaseblock;
525 int blockstart = -1;
526 int percent_complete = -1;
527 loff_t offs;
528 size_t readlen;
529 ulong mtdoffset = opts->offset;
530 u_char *buffer = opts->buffer;
531 int result;
533 /* make sure device page sizes are valid */
534 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
535 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
536 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
537 printf("Unknown flash (not normal NAND)\n");
538 return -1;
541 pagelen = meminfo->oobblock
542 + ((opts->readoob != 0) ? meminfo->oobsize : 0);
544 /* check, if length is not larger than device */
545 if (((imglen / pagelen) * meminfo->oobblock)
546 > (meminfo->size - opts->offset)) {
547 printf("Image %d bytes, NAND page %d bytes, "
548 "OOB area %u bytes, device size %u bytes\n",
549 imglen, pagelen, meminfo->oobblock, meminfo->size);
550 printf("Input block is larger than device\n");
551 return -1;
554 if (!opts->quiet)
555 printf("\n");
557 /* get data from input and write to the device */
558 while (imglen && (mtdoffset < meminfo->size)) {
560 WATCHDOG_RESET ();
563 * new eraseblock, check for bad block(s). Stay in the
564 * loop to be sure if the offset changes because of
565 * a bad block, that the next block that will be
566 * written to is also checked. Thus avoiding errors if
567 * the block(s) after the skipped block(s) is also bad
568 * (number of blocks depending on the blockalign
570 while (blockstart != (mtdoffset & (~meminfo->erasesize+1))) {
571 blockstart = mtdoffset & (~meminfo->erasesize+1);
572 offs = blockstart;
573 baderaseblock = 0;
575 /* check all the blocks in an erase block for
576 * bad blocks */
577 do {
578 int ret = meminfo->block_isbad(meminfo, offs);
580 if (ret < 0) {
581 printf("Bad block check failed\n");
582 return -1;
584 if (ret == 1) {
585 baderaseblock = 1;
586 if (!opts->quiet)
587 printf("\rBad block at 0x%lx "
588 "in erase block from "
589 "0x%x will be skipped\n",
590 (long) offs,
591 blockstart);
594 if (baderaseblock) {
595 mtdoffset = blockstart
596 + meminfo->erasesize;
598 offs += meminfo->erasesize;
600 } while (offs < blockstart + meminfo->erasesize);
604 /* read page data to memory buffer */
605 result = meminfo->read(meminfo,
606 mtdoffset,
607 meminfo->oobblock,
608 &readlen,
609 (unsigned char *) &data_buf);
611 if (result != 0) {
612 printf("reading NAND page at offset 0x%lx failed\n",
613 mtdoffset);
614 return -1;
617 if (imglen < readlen) {
618 readlen = imglen;
621 memcpy(buffer, data_buf, readlen);
622 buffer += readlen;
623 imglen -= readlen;
625 if (opts->readoob) {
626 result = meminfo->read_oob(meminfo,
627 mtdoffset,
628 meminfo->oobsize,
629 &readlen,
630 (unsigned char *)
631 &oob_buf);
633 if (result != 0) {
634 printf("\nMTD readoob failure: %d\n",
635 result);
636 return -1;
640 if (imglen < readlen) {
641 readlen = imglen;
644 memcpy(buffer, oob_buf, readlen);
646 buffer += readlen;
647 imglen -= readlen;
650 if (!opts->quiet) {
651 int percent = (int)
652 ((unsigned long long)
653 (opts->length-imglen) * 100
654 / opts->length);
655 /* output progress message only at whole percent
656 * steps to reduce the number of messages printed
657 * on (slow) serial consoles
659 if (percent != percent_complete) {
660 if (!opts->quiet)
661 printf("\rReading data from 0x%x "
662 "-- %3d%% complete.",
663 mtdoffset, percent);
664 percent_complete = percent;
668 mtdoffset += meminfo->oobblock;
671 if (!opts->quiet)
672 printf("\n");
674 if (imglen > 0) {
675 printf("Could not read entire image due to bad blocks\n");
676 return -1;
679 /* return happy */
680 return 0;
683 /******************************************************************************
684 * Support for locking / unlocking operations of some NAND devices
685 *****************************************************************************/
687 #define NAND_CMD_LOCK 0x2a
688 #define NAND_CMD_LOCK_TIGHT 0x2c
689 #define NAND_CMD_UNLOCK1 0x23
690 #define NAND_CMD_UNLOCK2 0x24
691 #define NAND_CMD_LOCK_STATUS 0x7a
694 * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
695 * state
697 * @param meminfo nand mtd instance
698 * @param tight bring device in lock tight mode
700 * @return 0 on success, -1 in case of error
702 * The lock / lock-tight command only applies to the whole chip. To get some
703 * parts of the chip lock and others unlocked use the following sequence:
705 * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
706 * - Call nand_unlock() once for each consecutive area to be unlocked
707 * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
709 * If the device is in lock-tight state software can't change the
710 * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
711 * calls will fail. It is only posible to leave lock-tight state by
712 * an hardware signal (low pulse on _WP pin) or by power down.
714 int nand_lock(nand_info_t *meminfo, int tight)
716 int ret = 0;
717 int status;
718 struct nand_chip *this = meminfo->priv;
720 /* select the NAND device */
721 this->select_chip(meminfo, 0);
723 this->cmdfunc(meminfo,
724 (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
725 -1, -1);
727 /* call wait ready function */
728 status = this->waitfunc(meminfo, this, FL_WRITING);
730 /* see if device thinks it succeeded */
731 if (status & 0x01) {
732 ret = -1;
735 /* de-select the NAND device */
736 this->select_chip(meminfo, -1);
737 return ret;
741 * nand_get_lock_status: - query current lock state from one page of NAND
742 * flash
744 * @param meminfo nand mtd instance
745 * @param offset page address to query (muss be page aligned!)
747 * @return -1 in case of error
748 * >0 lock status:
749 * bitfield with the following combinations:
750 * NAND_LOCK_STATUS_TIGHT: page in tight state
751 * NAND_LOCK_STATUS_LOCK: page locked
752 * NAND_LOCK_STATUS_UNLOCK: page unlocked
755 int nand_get_lock_status(nand_info_t *meminfo, ulong offset)
757 int ret = 0;
758 int chipnr;
759 int page;
760 struct nand_chip *this = meminfo->priv;
762 /* select the NAND device */
763 chipnr = (int)(offset >> this->chip_shift);
764 this->select_chip(meminfo, chipnr);
767 if ((offset & (meminfo->oobblock - 1)) != 0) {
768 printf ("nand_get_lock_status: "
769 "Start address must be beginning of "
770 "nand page!\n");
771 ret = -1;
772 goto out;
775 /* check the Lock Status */
776 page = (int)(offset >> this->page_shift);
777 this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask);
779 ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT
780 | NAND_LOCK_STATUS_LOCK
781 | NAND_LOCK_STATUS_UNLOCK);
783 out:
784 /* de-select the NAND device */
785 this->select_chip(meminfo, -1);
786 return ret;
790 * nand_unlock: - Unlock area of NAND pages
791 * only one consecutive area can be unlocked at one time!
793 * @param meminfo nand mtd instance
794 * @param start start byte address
795 * @param length number of bytes to unlock (must be a multiple of
796 * page size nand->oobblock)
798 * @return 0 on success, -1 in case of error
800 int nand_unlock(nand_info_t *meminfo, ulong start, ulong length)
802 int ret = 0;
803 int chipnr;
804 int status;
805 int page;
806 struct nand_chip *this = meminfo->priv;
807 printf ("nand_unlock: start: %08x, length: %d!\n",
808 (int)start, (int)length);
810 /* select the NAND device */
811 chipnr = (int)(start >> this->chip_shift);
812 this->select_chip(meminfo, chipnr);
814 /* check the WP bit */
815 this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1);
816 if ((this->read_byte(meminfo) & 0x80) == 0) {
817 printf ("nand_unlock: Device is write protected!\n");
818 ret = -1;
819 goto out;
822 if ((start & (meminfo->oobblock - 1)) != 0) {
823 printf ("nand_unlock: Start address must be beginning of "
824 "nand page!\n");
825 ret = -1;
826 goto out;
829 if (length == 0 || (length & (meminfo->oobblock - 1)) != 0) {
830 printf ("nand_unlock: Length must be a multiple of nand page "
831 "size!\n");
832 ret = -1;
833 goto out;
836 /* submit address of first page to unlock */
837 page = (int)(start >> this->page_shift);
838 this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask);
840 /* submit ADDRESS of LAST page to unlock */
841 page += (int)(length >> this->page_shift) - 1;
842 this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask);
844 /* call wait ready function */
845 status = this->waitfunc(meminfo, this, FL_WRITING);
846 /* see if device thinks it succeeded */
847 if (status & 0x01) {
848 /* there was an error */
849 ret = -1;
850 goto out;
853 out:
854 /* de-select the NAND device */
855 this->select_chip(meminfo, -1);
856 return ret;