blob: 828cc338adbd4da11ba113c6b29cb6a6175a635b [file] [log] [blame]
Stefan Roese2255b2d2006-10-10 12:36:02 +02001/*
Marcel Ziswiler7817cb22007-12-30 03:30:46 +01002 * drivers/mtd/nand/nand_util.c
Stefan Roese2255b2d2006-10-10 12:36:02 +02003 *
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
33#include <common.h>
34
Jon Loeligercb51c0b2007-07-09 17:39:42 -050035#if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
Stefan Roese2255b2d2006-10-10 12:36:02 +020036
37#include <command.h>
38#include <watchdog.h>
39#include <malloc.h>
Dirk Behme3a6d56c2007-08-02 17:42:08 +020040#include <div64.h>
Stefan Roese2255b2d2006-10-10 12:36:02 +020041
42#include <nand.h>
43#include <jffs2/jffs2.h>
44
45typedef struct erase_info erase_info_t;
46typedef struct mtd_info mtd_info_t;
47
48/* support only for native endian JFFS2 */
49#define cpu_to_je16(x) (x)
50#define cpu_to_je32(x) (x)
51
52/*****************************************************************************/
53static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)
54{
55 return 0;
56}
57
58/**
59 * nand_erase_opts: - erase NAND flash with support for various options
60 * (jffs2 formating)
61 *
62 * @param meminfo NAND device to erase
63 * @param opts options, @see struct nand_erase_options
64 * @return 0 in case of success
65 *
66 * This code is ported from flash_eraseall.c from Linux mtd utils by
67 * Arcom Control System Ltd.
68 */
69int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
70{
71 struct jffs2_unknown_node cleanmarker;
72 int clmpos = 0;
73 int clmlen = 8;
74 erase_info_t erase;
75 ulong erase_length;
76 int isNAND;
77 int bbtest = 1;
78 int result;
79 int percent_complete = -1;
80 int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL;
81 const char *mtd_device = meminfo->name;
82
83 memset(&erase, 0, sizeof(erase));
84
85 erase.mtd = meminfo;
86 erase.len = meminfo->erasesize;
Stefan Roese856f0542006-10-28 15:55:52 +020087 erase.addr = opts->offset;
88 erase_length = opts->length;
Stefan Roese2255b2d2006-10-10 12:36:02 +020089
90 isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0;
91
92 if (opts->jffs2) {
93 cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
94 cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
95 if (isNAND) {
96 struct nand_oobinfo *oobinfo = &meminfo->oobinfo;
97
98 /* check for autoplacement */
99 if (oobinfo->useecc == MTD_NANDECC_AUTOPLACE) {
100 /* get the position of the free bytes */
101 if (!oobinfo->oobfree[0][1]) {
102 printf(" Eeep. Autoplacement selected "
103 "and no empty space in oob\n");
104 return -1;
105 }
106 clmpos = oobinfo->oobfree[0][0];
107 clmlen = oobinfo->oobfree[0][1];
108 if (clmlen > 8)
109 clmlen = 8;
110 } else {
111 /* legacy mode */
112 switch (meminfo->oobsize) {
113 case 8:
114 clmpos = 6;
115 clmlen = 2;
116 break;
117 case 16:
118 clmpos = 8;
119 clmlen = 8;
120 break;
121 case 64:
122 clmpos = 16;
123 clmlen = 8;
124 break;
125 }
126 }
127
128 cleanmarker.totlen = cpu_to_je32(8);
129 } else {
130 cleanmarker.totlen =
131 cpu_to_je32(sizeof(struct jffs2_unknown_node));
132 }
133 cleanmarker.hdr_crc = cpu_to_je32(
134 crc32_no_comp(0, (unsigned char *) &cleanmarker,
135 sizeof(struct jffs2_unknown_node) - 4));
136 }
137
138 /* scrub option allows to erase badblock. To prevent internal
139 * check from erase() method, set block check method to dummy
140 * and disable bad block table while erasing.
141 */
142 if (opts->scrub) {
143 struct nand_chip *priv_nand = meminfo->priv;
144
145 nand_block_bad_old = priv_nand->block_bad;
146 priv_nand->block_bad = nand_block_bad_scrub;
147 /* we don't need the bad block table anymore...
148 * after scrub, there are no bad blocks left!
149 */
150 if (priv_nand->bbt) {
151 kfree(priv_nand->bbt);
152 }
153 priv_nand->bbt = NULL;
154 }
155
Dirk Behme9723bbb2008-01-16 14:26:59 +0100156 if (erase_length < meminfo->erasesize) {
Stefan Roesee8706902008-07-10 10:10:54 +0200157 printf("Warning: Erase size 0x%08lx smaller than one " \
Dirk Behme9723bbb2008-01-16 14:26:59 +0100158 "erase block 0x%08x\n",erase_length, meminfo->erasesize);
159 printf(" Erasing 0x%08x instead\n", meminfo->erasesize);
160 erase_length = meminfo->erasesize;
161 }
162
Stefan Roese2255b2d2006-10-10 12:36:02 +0200163 for (;
164 erase.addr < opts->offset + erase_length;
165 erase.addr += meminfo->erasesize) {
166
167 WATCHDOG_RESET ();
168
169 if (!opts->scrub && bbtest) {
170 int ret = meminfo->block_isbad(meminfo, erase.addr);
171 if (ret > 0) {
172 if (!opts->quiet)
173 printf("\rSkipping bad block at "
Wolfgang Denk87621bc2006-10-12 11:43:47 +0200174 "0x%08x "
175 " \n",
176 erase.addr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200177 continue;
178
179 } else if (ret < 0) {
180 printf("\n%s: MTD get bad block failed: %d\n",
181 mtd_device,
182 ret);
183 return -1;
184 }
185 }
186
187 result = meminfo->erase(meminfo, &erase);
188 if (result != 0) {
189 printf("\n%s: MTD Erase failure: %d\n",
190 mtd_device, result);
191 continue;
192 }
193
194 /* format for JFFS2 ? */
195 if (opts->jffs2) {
196
197 /* write cleanmarker */
198 if (isNAND) {
199 size_t written;
200 result = meminfo->write_oob(meminfo,
201 erase.addr + clmpos,
202 clmlen,
203 &written,
204 (unsigned char *)
205 &cleanmarker);
206 if (result != 0) {
207 printf("\n%s: MTD writeoob failure: %d\n",
208 mtd_device, result);
209 continue;
210 }
211 } else {
212 printf("\n%s: this erase routine only supports"
213 " NAND devices!\n",
214 mtd_device);
215 }
216 }
217
218 if (!opts->quiet) {
Wolfgang Denkbe5d72d2007-08-13 21:57:53 +0200219 unsigned long long n =(unsigned long long)
Matthias Fuchs5bd7fe92007-09-11 17:04:00 +0200220 (erase.addr + meminfo->erasesize - opts->offset)
221 * 100;
222 int percent;
223
224 do_div(n, erase_length);
225 percent = (int)n;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200226
227 /* output progress message only at whole percent
228 * steps to reduce the number of messages printed
229 * on (slow) serial consoles
230 */
231 if (percent != percent_complete) {
232 percent_complete = percent;
233
234 printf("\rErasing at 0x%x -- %3d%% complete.",
235 erase.addr, percent);
236
237 if (opts->jffs2 && result == 0)
238 printf(" Cleanmarker written at 0x%x.",
239 erase.addr);
240 }
241 }
242 }
243 if (!opts->quiet)
244 printf("\n");
245
246 if (nand_block_bad_old) {
247 struct nand_chip *priv_nand = meminfo->priv;
248
249 priv_nand->block_bad = nand_block_bad_old;
250 priv_nand->scan_bbt(meminfo);
251 }
252
253 return 0;
254}
255
256#define MAX_PAGE_SIZE 2048
257#define MAX_OOB_SIZE 64
258
259/*
260 * buffer array used for writing data
261 */
262static unsigned char data_buf[MAX_PAGE_SIZE];
263static unsigned char oob_buf[MAX_OOB_SIZE];
264
265/* OOB layouts to pass into the kernel as default */
266static struct nand_oobinfo none_oobinfo = {
267 .useecc = MTD_NANDECC_OFF,
268};
269
270static struct nand_oobinfo jffs2_oobinfo = {
271 .useecc = MTD_NANDECC_PLACE,
272 .eccbytes = 6,
273 .eccpos = { 0, 1, 2, 3, 6, 7 }
274};
275
276static struct nand_oobinfo yaffs_oobinfo = {
277 .useecc = MTD_NANDECC_PLACE,
278 .eccbytes = 6,
279 .eccpos = { 8, 9, 10, 13, 14, 15}
280};
281
282static struct nand_oobinfo autoplace_oobinfo = {
283 .useecc = MTD_NANDECC_AUTOPLACE
284};
285
286/**
287 * nand_write_opts: - write image to NAND flash with support for various options
288 *
289 * @param meminfo NAND device to erase
290 * @param opts write options (@see nand_write_options)
291 * @return 0 in case of success
292 *
293 * This code is ported from nandwrite.c from Linux mtd utils by
294 * Steven J. Hill and Thomas Gleixner.
295 */
296int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts)
297{
298 int imglen = 0;
299 int pagelen;
300 int baderaseblock;
301 int blockstart = -1;
302 loff_t offs;
303 int readlen;
304 int oobinfochanged = 0;
305 int percent_complete = -1;
306 struct nand_oobinfo old_oobinfo;
307 ulong mtdoffset = opts->offset;
308 ulong erasesize_blockalign;
309 u_char *buffer = opts->buffer;
310 size_t written;
311 int result;
312
313 if (opts->pad && opts->writeoob) {
314 printf("Can't pad when oob data is present.\n");
315 return -1;
316 }
317
318 /* set erasesize to specified number of blocks - to match
319 * jffs2 (virtual) block size */
320 if (opts->blockalign == 0) {
321 erasesize_blockalign = meminfo->erasesize;
322 } else {
323 erasesize_blockalign = meminfo->erasesize * opts->blockalign;
324 }
325
326 /* make sure device page sizes are valid */
327 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
328 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
329 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
330 printf("Unknown flash (not normal NAND)\n");
331 return -1;
332 }
333
334 /* read the current oob info */
335 memcpy(&old_oobinfo, &meminfo->oobinfo, sizeof(old_oobinfo));
336
337 /* write without ecc? */
338 if (opts->noecc) {
339 memcpy(&meminfo->oobinfo, &none_oobinfo,
340 sizeof(meminfo->oobinfo));
341 oobinfochanged = 1;
342 }
343
344 /* autoplace ECC? */
345 if (opts->autoplace && (old_oobinfo.useecc != MTD_NANDECC_AUTOPLACE)) {
346
347 memcpy(&meminfo->oobinfo, &autoplace_oobinfo,
348 sizeof(meminfo->oobinfo));
349 oobinfochanged = 1;
350 }
351
352 /* force OOB layout for jffs2 or yaffs? */
353 if (opts->forcejffs2 || opts->forceyaffs) {
354 struct nand_oobinfo *oobsel =
355 opts->forcejffs2 ? &jffs2_oobinfo : &yaffs_oobinfo;
356
357 if (meminfo->oobsize == 8) {
358 if (opts->forceyaffs) {
359 printf("YAFSS cannot operate on "
360 "256 Byte page size\n");
361 goto restoreoob;
362 }
363 /* Adjust number of ecc bytes */
364 jffs2_oobinfo.eccbytes = 3;
365 }
366
367 memcpy(&meminfo->oobinfo, oobsel, sizeof(meminfo->oobinfo));
368 }
369
370 /* get image length */
371 imglen = opts->length;
372 pagelen = meminfo->oobblock
373 + ((opts->writeoob != 0) ? meminfo->oobsize : 0);
374
375 /* check, if file is pagealigned */
376 if ((!opts->pad) && ((imglen % pagelen) != 0)) {
377 printf("Input block length is not page aligned\n");
378 goto restoreoob;
379 }
380
381 /* check, if length fits into device */
382 if (((imglen / pagelen) * meminfo->oobblock)
383 > (meminfo->size - opts->offset)) {
384 printf("Image %d bytes, NAND page %d bytes, "
385 "OOB area %u bytes, device size %u bytes\n",
386 imglen, pagelen, meminfo->oobblock, meminfo->size);
387 printf("Input block does not fit into device\n");
388 goto restoreoob;
389 }
390
391 if (!opts->quiet)
392 printf("\n");
393
394 /* get data from input and write to the device */
395 while (imglen && (mtdoffset < meminfo->size)) {
396
397 WATCHDOG_RESET ();
398
399 /*
400 * new eraseblock, check for bad block(s). Stay in the
401 * loop to be sure if the offset changes because of
402 * a bad block, that the next block that will be
403 * written to is also checked. Thus avoiding errors if
404 * the block(s) after the skipped block(s) is also bad
405 * (number of blocks depending on the blockalign
406 */
407 while (blockstart != (mtdoffset & (~erasesize_blockalign+1))) {
408 blockstart = mtdoffset & (~erasesize_blockalign+1);
409 offs = blockstart;
410 baderaseblock = 0;
411
412 /* check all the blocks in an erase block for
413 * bad blocks */
414 do {
415 int ret = meminfo->block_isbad(meminfo, offs);
416
417 if (ret < 0) {
418 printf("Bad block check failed\n");
419 goto restoreoob;
420 }
421 if (ret == 1) {
422 baderaseblock = 1;
423 if (!opts->quiet)
424 printf("\rBad block at 0x%lx "
425 "in erase block from "
426 "0x%x will be skipped\n",
427 (long) offs,
428 blockstart);
429 }
430
431 if (baderaseblock) {
432 mtdoffset = blockstart
433 + erasesize_blockalign;
434 }
435 offs += erasesize_blockalign
436 / opts->blockalign;
437 } while (offs < blockstart + erasesize_blockalign);
438 }
439
440 readlen = meminfo->oobblock;
441 if (opts->pad && (imglen < readlen)) {
442 readlen = imglen;
443 memset(data_buf + readlen, 0xff,
444 meminfo->oobblock - readlen);
445 }
446
447 /* read page data from input memory buffer */
448 memcpy(data_buf, buffer, readlen);
449 buffer += readlen;
450
451 if (opts->writeoob) {
452 /* read OOB data from input memory block, exit
453 * on failure */
454 memcpy(oob_buf, buffer, meminfo->oobsize);
455 buffer += meminfo->oobsize;
456
457 /* write OOB data first, as ecc will be placed
458 * in there*/
459 result = meminfo->write_oob(meminfo,
460 mtdoffset,
461 meminfo->oobsize,
462 &written,
463 (unsigned char *)
464 &oob_buf);
465
466 if (result != 0) {
467 printf("\nMTD writeoob failure: %d\n",
468 result);
469 goto restoreoob;
470 }
471 imglen -= meminfo->oobsize;
472 }
473
474 /* write out the page data */
475 result = meminfo->write(meminfo,
476 mtdoffset,
477 meminfo->oobblock,
478 &written,
479 (unsigned char *) &data_buf);
480
481 if (result != 0) {
482 printf("writing NAND page at offset 0x%lx failed\n",
483 mtdoffset);
484 goto restoreoob;
485 }
486 imglen -= readlen;
487
488 if (!opts->quiet) {
Wolfgang Denkbe5d72d2007-08-13 21:57:53 +0200489 unsigned long long n = (unsigned long long)
490 (opts->length-imglen) * 100;
Matthias Fuchs5bd7fe92007-09-11 17:04:00 +0200491 int percent;
492
493 do_div(n, opts->length);
494 percent = (int)n;
495
Stefan Roese2255b2d2006-10-10 12:36:02 +0200496 /* output progress message only at whole percent
497 * steps to reduce the number of messages printed
498 * on (slow) serial consoles
499 */
500 if (percent != percent_complete) {
Stefan Roesee8706902008-07-10 10:10:54 +0200501 printf("\rWriting data at 0x%lx "
Stefan Roese2255b2d2006-10-10 12:36:02 +0200502 "-- %3d%% complete.",
503 mtdoffset, percent);
504 percent_complete = percent;
505 }
506 }
507
508 mtdoffset += meminfo->oobblock;
509 }
510
511 if (!opts->quiet)
512 printf("\n");
513
514restoreoob:
515 if (oobinfochanged) {
516 memcpy(&meminfo->oobinfo, &old_oobinfo,
517 sizeof(meminfo->oobinfo));
518 }
519
520 if (imglen > 0) {
521 printf("Data did not fit into device, due to bad blocks\n");
522 return -1;
523 }
524
525 /* return happy */
526 return 0;
527}
528
529/**
530 * nand_read_opts: - read image from NAND flash with support for various options
531 *
532 * @param meminfo NAND device to erase
533 * @param opts read options (@see struct nand_read_options)
534 * @return 0 in case of success
535 *
536 */
537int nand_read_opts(nand_info_t *meminfo, const nand_read_options_t *opts)
538{
539 int imglen = opts->length;
540 int pagelen;
541 int baderaseblock;
542 int blockstart = -1;
543 int percent_complete = -1;
544 loff_t offs;
545 size_t readlen;
546 ulong mtdoffset = opts->offset;
547 u_char *buffer = opts->buffer;
548 int result;
549
550 /* make sure device page sizes are valid */
551 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
552 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
553 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
554 printf("Unknown flash (not normal NAND)\n");
555 return -1;
556 }
557
558 pagelen = meminfo->oobblock
559 + ((opts->readoob != 0) ? meminfo->oobsize : 0);
560
561 /* check, if length is not larger than device */
562 if (((imglen / pagelen) * meminfo->oobblock)
563 > (meminfo->size - opts->offset)) {
564 printf("Image %d bytes, NAND page %d bytes, "
565 "OOB area %u bytes, device size %u bytes\n",
566 imglen, pagelen, meminfo->oobblock, meminfo->size);
567 printf("Input block is larger than device\n");
568 return -1;
569 }
570
571 if (!opts->quiet)
572 printf("\n");
573
574 /* get data from input and write to the device */
575 while (imglen && (mtdoffset < meminfo->size)) {
576
577 WATCHDOG_RESET ();
578
579 /*
580 * new eraseblock, check for bad block(s). Stay in the
581 * loop to be sure if the offset changes because of
582 * a bad block, that the next block that will be
583 * written to is also checked. Thus avoiding errors if
584 * the block(s) after the skipped block(s) is also bad
585 * (number of blocks depending on the blockalign
586 */
587 while (blockstart != (mtdoffset & (~meminfo->erasesize+1))) {
588 blockstart = mtdoffset & (~meminfo->erasesize+1);
589 offs = blockstart;
590 baderaseblock = 0;
591
592 /* check all the blocks in an erase block for
593 * bad blocks */
594 do {
595 int ret = meminfo->block_isbad(meminfo, offs);
596
597 if (ret < 0) {
598 printf("Bad block check failed\n");
599 return -1;
600 }
601 if (ret == 1) {
602 baderaseblock = 1;
603 if (!opts->quiet)
604 printf("\rBad block at 0x%lx "
605 "in erase block from "
606 "0x%x will be skipped\n",
607 (long) offs,
608 blockstart);
609 }
610
611 if (baderaseblock) {
612 mtdoffset = blockstart
613 + meminfo->erasesize;
614 }
615 offs += meminfo->erasesize;
616
617 } while (offs < blockstart + meminfo->erasesize);
618 }
619
620
621 /* read page data to memory buffer */
622 result = meminfo->read(meminfo,
623 mtdoffset,
624 meminfo->oobblock,
625 &readlen,
626 (unsigned char *) &data_buf);
627
628 if (result != 0) {
629 printf("reading NAND page at offset 0x%lx failed\n",
630 mtdoffset);
631 return -1;
632 }
633
634 if (imglen < readlen) {
635 readlen = imglen;
636 }
637
638 memcpy(buffer, data_buf, readlen);
639 buffer += readlen;
640 imglen -= readlen;
641
642 if (opts->readoob) {
643 result = meminfo->read_oob(meminfo,
644 mtdoffset,
645 meminfo->oobsize,
646 &readlen,
647 (unsigned char *)
648 &oob_buf);
649
650 if (result != 0) {
651 printf("\nMTD readoob failure: %d\n",
652 result);
653 return -1;
654 }
655
656
657 if (imglen < readlen) {
658 readlen = imglen;
659 }
660
661 memcpy(buffer, oob_buf, readlen);
662
663 buffer += readlen;
664 imglen -= readlen;
665 }
666
667 if (!opts->quiet) {
Wolfgang Denkbe5d72d2007-08-13 21:57:53 +0200668 unsigned long long n = (unsigned long long)
669 (opts->length-imglen) * 100;
Matthias Fuchs5bd7fe92007-09-11 17:04:00 +0200670 int percent;
671
672 do_div(n, opts->length);
673 percent = (int)n;
674
Stefan Roese2255b2d2006-10-10 12:36:02 +0200675 /* output progress message only at whole percent
676 * steps to reduce the number of messages printed
677 * on (slow) serial consoles
678 */
679 if (percent != percent_complete) {
680 if (!opts->quiet)
Stefan Roesee8706902008-07-10 10:10:54 +0200681 printf("\rReading data from 0x%lx "
Stefan Roese2255b2d2006-10-10 12:36:02 +0200682 "-- %3d%% complete.",
683 mtdoffset, percent);
684 percent_complete = percent;
685 }
686 }
687
688 mtdoffset += meminfo->oobblock;
689 }
690
691 if (!opts->quiet)
692 printf("\n");
693
694 if (imglen > 0) {
695 printf("Could not read entire image due to bad blocks\n");
696 return -1;
697 }
698
699 /* return happy */
700 return 0;
701}
702
703/******************************************************************************
704 * Support for locking / unlocking operations of some NAND devices
705 *****************************************************************************/
706
707#define NAND_CMD_LOCK 0x2a
708#define NAND_CMD_LOCK_TIGHT 0x2c
709#define NAND_CMD_UNLOCK1 0x23
710#define NAND_CMD_UNLOCK2 0x24
711#define NAND_CMD_LOCK_STATUS 0x7a
712
713/**
714 * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
715 * state
716 *
717 * @param meminfo nand mtd instance
718 * @param tight bring device in lock tight mode
719 *
720 * @return 0 on success, -1 in case of error
721 *
722 * The lock / lock-tight command only applies to the whole chip. To get some
723 * parts of the chip lock and others unlocked use the following sequence:
724 *
725 * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
726 * - Call nand_unlock() once for each consecutive area to be unlocked
727 * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
728 *
729 * If the device is in lock-tight state software can't change the
730 * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
731 * calls will fail. It is only posible to leave lock-tight state by
732 * an hardware signal (low pulse on _WP pin) or by power down.
733 */
734int nand_lock(nand_info_t *meminfo, int tight)
735{
736 int ret = 0;
737 int status;
738 struct nand_chip *this = meminfo->priv;
739
740 /* select the NAND device */
741 this->select_chip(meminfo, 0);
742
743 this->cmdfunc(meminfo,
744 (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
745 -1, -1);
746
747 /* call wait ready function */
748 status = this->waitfunc(meminfo, this, FL_WRITING);
749
750 /* see if device thinks it succeeded */
751 if (status & 0x01) {
752 ret = -1;
753 }
754
755 /* de-select the NAND device */
756 this->select_chip(meminfo, -1);
757 return ret;
758}
759
760/**
761 * nand_get_lock_status: - query current lock state from one page of NAND
762 * flash
763 *
764 * @param meminfo nand mtd instance
765 * @param offset page address to query (muss be page aligned!)
766 *
767 * @return -1 in case of error
768 * >0 lock status:
769 * bitfield with the following combinations:
770 * NAND_LOCK_STATUS_TIGHT: page in tight state
771 * NAND_LOCK_STATUS_LOCK: page locked
772 * NAND_LOCK_STATUS_UNLOCK: page unlocked
773 *
774 */
775int nand_get_lock_status(nand_info_t *meminfo, ulong offset)
776{
777 int ret = 0;
778 int chipnr;
779 int page;
780 struct nand_chip *this = meminfo->priv;
781
782 /* select the NAND device */
783 chipnr = (int)(offset >> this->chip_shift);
784 this->select_chip(meminfo, chipnr);
785
786
787 if ((offset & (meminfo->oobblock - 1)) != 0) {
788 printf ("nand_get_lock_status: "
789 "Start address must be beginning of "
790 "nand page!\n");
791 ret = -1;
792 goto out;
793 }
794
795 /* check the Lock Status */
796 page = (int)(offset >> this->page_shift);
797 this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask);
798
799 ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT
800 | NAND_LOCK_STATUS_LOCK
801 | NAND_LOCK_STATUS_UNLOCK);
802
803 out:
804 /* de-select the NAND device */
805 this->select_chip(meminfo, -1);
806 return ret;
807}
808
809/**
810 * nand_unlock: - Unlock area of NAND pages
811 * only one consecutive area can be unlocked at one time!
812 *
813 * @param meminfo nand mtd instance
814 * @param start start byte address
815 * @param length number of bytes to unlock (must be a multiple of
816 * page size nand->oobblock)
817 *
818 * @return 0 on success, -1 in case of error
819 */
820int nand_unlock(nand_info_t *meminfo, ulong start, ulong length)
821{
822 int ret = 0;
823 int chipnr;
824 int status;
825 int page;
826 struct nand_chip *this = meminfo->priv;
827 printf ("nand_unlock: start: %08x, length: %d!\n",
828 (int)start, (int)length);
829
830 /* select the NAND device */
831 chipnr = (int)(start >> this->chip_shift);
832 this->select_chip(meminfo, chipnr);
833
834 /* check the WP bit */
835 this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1);
836 if ((this->read_byte(meminfo) & 0x80) == 0) {
837 printf ("nand_unlock: Device is write protected!\n");
838 ret = -1;
839 goto out;
840 }
841
842 if ((start & (meminfo->oobblock - 1)) != 0) {
843 printf ("nand_unlock: Start address must be beginning of "
844 "nand page!\n");
845 ret = -1;
846 goto out;
847 }
848
849 if (length == 0 || (length & (meminfo->oobblock - 1)) != 0) {
850 printf ("nand_unlock: Length must be a multiple of nand page "
851 "size!\n");
852 ret = -1;
853 goto out;
854 }
855
856 /* submit address of first page to unlock */
857 page = (int)(start >> this->page_shift);
858 this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask);
859
860 /* submit ADDRESS of LAST page to unlock */
861 page += (int)(length >> this->page_shift) - 1;
862 this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask);
863
864 /* call wait ready function */
865 status = this->waitfunc(meminfo, this, FL_WRITING);
866 /* see if device thinks it succeeded */
867 if (status & 0x01) {
868 /* there was an error */
869 ret = -1;
870 goto out;
871 }
872
873 out:
874 /* de-select the NAND device */
875 this->select_chip(meminfo, -1);
876 return ret;
877}
878
Jon Loeligercb51c0b2007-07-09 17:39:42 -0500879#endif