Merge branch 'master' of /home/stefan/git/u-boot/u-boot
[platform/kernel/u-boot.git] / drivers / mtd / nand / nand_util.c
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
33 #include <common.h>
34
35 #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
36
37 #include <command.h>
38 #include <watchdog.h>
39 #include <malloc.h>
40 #include <div64.h>
41
42
43 #include <asm/errno.h>
44 #include <linux/mtd/mtd.h>
45 #include <nand.h>
46 #include <jffs2/jffs2.h>
47
48 typedef struct erase_info erase_info_t;
49 typedef struct mtd_info   mtd_info_t;
50
51 /* support only for native endian JFFS2 */
52 #define cpu_to_je16(x) (x)
53 #define cpu_to_je32(x) (x)
54
55 /*****************************************************************************/
56 static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)
57 {
58         return 0;
59 }
60
61 /**
62  * nand_erase_opts: - erase NAND flash with support for various options
63  *                    (jffs2 formating)
64  *
65  * @param meminfo       NAND device to erase
66  * @param opts          options,  @see struct nand_erase_options
67  * @return              0 in case of success
68  *
69  * This code is ported from flash_eraseall.c from Linux mtd utils by
70  * Arcom Control System Ltd.
71  */
72 int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
73 {
74         struct jffs2_unknown_node cleanmarker;
75         erase_info_t erase;
76         ulong erase_length;
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         struct mtd_oob_ops oob_opts;
83         struct nand_chip *chip = meminfo->priv;
84         uint8_t buf[64];
85
86         memset(buf, 0, sizeof(buf));
87         memset(&erase, 0, sizeof(erase));
88         memset(&oob_opts, 0, sizeof(oob_opts));
89
90         erase.mtd = meminfo;
91         erase.len  = meminfo->erasesize;
92         erase.addr = opts->offset;
93         erase_length = opts->length;
94
95
96         cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
97         cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
98         cleanmarker.totlen = cpu_to_je32(8);
99         cleanmarker.hdr_crc = cpu_to_je32(
100         crc32_no_comp(0, (unsigned char *) &cleanmarker,
101         sizeof(struct jffs2_unknown_node) - 4));
102
103         /* scrub option allows to erase badblock. To prevent internal
104          * check from erase() method, set block check method to dummy
105          * and disable bad block table while erasing.
106          */
107         if (opts->scrub) {
108                 struct nand_chip *priv_nand = meminfo->priv;
109
110                 nand_block_bad_old = priv_nand->block_bad;
111                 priv_nand->block_bad = nand_block_bad_scrub;
112                 /* we don't need the bad block table anymore...
113                  * after scrub, there are no bad blocks left!
114                  */
115                 if (priv_nand->bbt) {
116                         kfree(priv_nand->bbt);
117                 }
118                 priv_nand->bbt = NULL;
119         }
120
121         if (erase_length < meminfo->erasesize) {
122                 printf("Warning: Erase size 0x%08lx smaller than one "  \
123                        "erase block 0x%08x\n",erase_length, meminfo->erasesize);
124                 printf("         Erasing 0x%08x instead\n", meminfo->erasesize);
125                 erase_length = meminfo->erasesize;
126         }
127
128         for (;
129              erase.addr < opts->offset + erase_length;
130              erase.addr += meminfo->erasesize) {
131
132                 WATCHDOG_RESET ();
133
134                 if (!opts->scrub && bbtest) {
135                         int ret = meminfo->block_isbad(meminfo, erase.addr);
136                         if (ret > 0) {
137                                 if (!opts->quiet)
138                                         printf("\rSkipping bad block at  "
139                                                "0x%08x                   "
140                                                "                         \n",
141                                                erase.addr);
142                                 continue;
143
144                         } else if (ret < 0) {
145                                 printf("\n%s: MTD get bad block failed: %d\n",
146                                        mtd_device,
147                                        ret);
148                                 return -1;
149                         }
150                 }
151
152                 result = meminfo->erase(meminfo, &erase);
153                 if (result != 0) {
154                         printf("\n%s: MTD Erase failure: %d\n",
155                                mtd_device, result);
156                         continue;
157                 }
158
159                 /* format for JFFS2 ? */
160                 if (opts->jffs2) {
161
162                         chip->ops.len = chip->ops.ooblen = 64;
163                         chip->ops.datbuf = NULL;
164                         chip->ops.oobbuf = buf;
165                         chip->ops.ooboffs = chip->badblockpos & ~0x01;
166
167                         result = meminfo->write_oob(meminfo,
168                                                         erase.addr + meminfo->oobsize,
169                                                         &chip->ops);
170                         if (result != 0) {
171                                 printf("\n%s: MTD writeoob failure: %d\n",
172                                 mtd_device, result);
173                                 continue;
174                         }
175                         else
176                                 printf("%s: MTD writeoob at 0x%08x\n",mtd_device, erase.addr + meminfo->oobsize );
177                 }
178
179                 if (!opts->quiet) {
180                         unsigned long long n =(unsigned long long)
181                                 (erase.addr + meminfo->erasesize - opts->offset)
182                                 * 100;
183                         int percent;
184
185                         do_div(n, erase_length);
186                         percent = (int)n;
187
188                         /* output progress message only at whole percent
189                          * steps to reduce the number of messages printed
190                          * on (slow) serial consoles
191                          */
192                         if (percent != percent_complete) {
193                                 percent_complete = percent;
194
195                                 printf("\rErasing at 0x%x -- %3d%% complete.",
196                                 erase.addr, percent);
197
198                                 if (opts->jffs2 && result == 0)
199                                 printf(" Cleanmarker written at 0x%x.",
200                                 erase.addr);
201                         }
202                 }
203         }
204         if (!opts->quiet)
205                 printf("\n");
206
207         if (nand_block_bad_old) {
208                 struct nand_chip *priv_nand = meminfo->priv;
209
210                 priv_nand->block_bad = nand_block_bad_old;
211                 priv_nand->scan_bbt(meminfo);
212         }
213
214         return 0;
215 }
216
217 /* XXX U-BOOT XXX */
218 #if 0
219
220 #define MAX_PAGE_SIZE   2048
221 #define MAX_OOB_SIZE    64
222
223 /*
224  * buffer array used for writing data
225  */
226 static unsigned char data_buf[MAX_PAGE_SIZE];
227 static unsigned char oob_buf[MAX_OOB_SIZE];
228
229 /* OOB layouts to pass into the kernel as default */
230 static struct nand_ecclayout none_ecclayout = {
231         .useecc = MTD_NANDECC_OFF,
232 };
233
234 static struct nand_ecclayout jffs2_ecclayout = {
235         .useecc = MTD_NANDECC_PLACE,
236         .eccbytes = 6,
237         .eccpos = { 0, 1, 2, 3, 6, 7 }
238 };
239
240 static struct nand_ecclayout yaffs_ecclayout = {
241         .useecc = MTD_NANDECC_PLACE,
242         .eccbytes = 6,
243         .eccpos = { 8, 9, 10, 13, 14, 15}
244 };
245
246 static struct nand_ecclayout autoplace_ecclayout = {
247         .useecc = MTD_NANDECC_AUTOPLACE
248 };
249 #endif
250
251 /* XXX U-BOOT XXX */
252 #if 0
253 /******************************************************************************
254  * Support for locking / unlocking operations of some NAND devices
255  *****************************************************************************/
256
257 #define NAND_CMD_LOCK           0x2a
258 #define NAND_CMD_LOCK_TIGHT     0x2c
259 #define NAND_CMD_UNLOCK1        0x23
260 #define NAND_CMD_UNLOCK2        0x24
261 #define NAND_CMD_LOCK_STATUS    0x7a
262
263 /**
264  * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
265  *            state
266  *
267  * @param meminfo       nand mtd instance
268  * @param tight         bring device in lock tight mode
269  *
270  * @return              0 on success, -1 in case of error
271  *
272  * The lock / lock-tight command only applies to the whole chip. To get some
273  * parts of the chip lock and others unlocked use the following sequence:
274  *
275  * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
276  * - Call nand_unlock() once for each consecutive area to be unlocked
277  * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
278  *
279  *   If the device is in lock-tight state software can't change the
280  *   current active lock/unlock state of all pages. nand_lock() / nand_unlock()
281  *   calls will fail. It is only posible to leave lock-tight state by
282  *   an hardware signal (low pulse on _WP pin) or by power down.
283  */
284 int nand_lock(nand_info_t *meminfo, int tight)
285 {
286         int ret = 0;
287         int status;
288         struct nand_chip *this = meminfo->priv;
289
290         /* select the NAND device */
291         this->select_chip(meminfo, 0);
292
293         this->cmdfunc(meminfo,
294                       (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
295                       -1, -1);
296
297         /* call wait ready function */
298         status = this->waitfunc(meminfo, this, FL_WRITING);
299
300         /* see if device thinks it succeeded */
301         if (status & 0x01) {
302                 ret = -1;
303         }
304
305         /* de-select the NAND device */
306         this->select_chip(meminfo, -1);
307         return ret;
308 }
309
310 /**
311  * nand_get_lock_status: - query current lock state from one page of NAND
312  *                         flash
313  *
314  * @param meminfo       nand mtd instance
315  * @param offset        page address to query (muss be page aligned!)
316  *
317  * @return              -1 in case of error
318  *                      >0 lock status:
319  *                        bitfield with the following combinations:
320  *                        NAND_LOCK_STATUS_TIGHT: page in tight state
321  *                        NAND_LOCK_STATUS_LOCK:  page locked
322  *                        NAND_LOCK_STATUS_UNLOCK: page unlocked
323  *
324  */
325 int nand_get_lock_status(nand_info_t *meminfo, ulong offset)
326 {
327         int ret = 0;
328         int chipnr;
329         int page;
330         struct nand_chip *this = meminfo->priv;
331
332         /* select the NAND device */
333         chipnr = (int)(offset >> this->chip_shift);
334         this->select_chip(meminfo, chipnr);
335
336
337         if ((offset & (meminfo->writesize - 1)) != 0) {
338                 printf ("nand_get_lock_status: "
339                         "Start address must be beginning of "
340                         "nand page!\n");
341                 ret = -1;
342                 goto out;
343         }
344
345         /* check the Lock Status */
346         page = (int)(offset >> this->page_shift);
347         this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask);
348
349         ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT
350                                           | NAND_LOCK_STATUS_LOCK
351                                           | NAND_LOCK_STATUS_UNLOCK);
352
353  out:
354         /* de-select the NAND device */
355         this->select_chip(meminfo, -1);
356         return ret;
357 }
358
359 /**
360  * nand_unlock: - Unlock area of NAND pages
361  *                only one consecutive area can be unlocked at one time!
362  *
363  * @param meminfo       nand mtd instance
364  * @param start         start byte address
365  * @param length        number of bytes to unlock (must be a multiple of
366  *                      page size nand->writesize)
367  *
368  * @return              0 on success, -1 in case of error
369  */
370 int nand_unlock(nand_info_t *meminfo, ulong start, ulong length)
371 {
372         int ret = 0;
373         int chipnr;
374         int status;
375         int page;
376         struct nand_chip *this = meminfo->priv;
377         printf ("nand_unlock: start: %08x, length: %d!\n",
378                 (int)start, (int)length);
379
380         /* select the NAND device */
381         chipnr = (int)(start >> this->chip_shift);
382         this->select_chip(meminfo, chipnr);
383
384         /* check the WP bit */
385         this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1);
386         if ((this->read_byte(meminfo) & 0x80) == 0) {
387                 printf ("nand_unlock: Device is write protected!\n");
388                 ret = -1;
389                 goto out;
390         }
391
392         if ((start & (meminfo->writesize - 1)) != 0) {
393                 printf ("nand_unlock: Start address must be beginning of "
394                         "nand page!\n");
395                 ret = -1;
396                 goto out;
397         }
398
399         if (length == 0 || (length & (meminfo->writesize - 1)) != 0) {
400                 printf ("nand_unlock: Length must be a multiple of nand page "
401                         "size!\n");
402                 ret = -1;
403                 goto out;
404         }
405
406         /* submit address of first page to unlock */
407         page = (int)(start >> this->page_shift);
408         this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask);
409
410         /* submit ADDRESS of LAST page to unlock */
411         page += (int)(length >> this->page_shift) - 1;
412         this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask);
413
414         /* call wait ready function */
415         status = this->waitfunc(meminfo, this, FL_WRITING);
416         /* see if device thinks it succeeded */
417         if (status & 0x01) {
418                 /* there was an error */
419                 ret = -1;
420                 goto out;
421         }
422
423  out:
424         /* de-select the NAND device */
425         this->select_chip(meminfo, -1);
426         return ret;
427 }
428 #endif
429
430 /**
431  * get_len_incl_bad
432  *
433  * Check if length including bad blocks fits into device.
434  *
435  * @param nand NAND device
436  * @param offset offset in flash
437  * @param length image length
438  * @return image length including bad blocks
439  */
440 static size_t get_len_incl_bad (nand_info_t *nand, size_t offset,
441                                 const size_t length)
442 {
443         size_t len_incl_bad = 0;
444         size_t len_excl_bad = 0;
445         size_t block_len;
446
447         while (len_excl_bad < length) {
448                 block_len = nand->erasesize - (offset & (nand->erasesize - 1));
449
450                 if (!nand_block_isbad (nand, offset & ~(nand->erasesize - 1)))
451                         len_excl_bad += block_len;
452
453                 len_incl_bad += block_len;
454                 offset       += block_len;
455
456                 if ((offset + len_incl_bad) >= nand->size)
457                         break;
458         }
459
460         return len_incl_bad;
461 }
462
463 /**
464  * nand_write_skip_bad:
465  *
466  * Write image to NAND flash.
467  * Blocks that are marked bad are skipped and the is written to the next
468  * block instead as long as the image is short enough to fit even after
469  * skipping the bad blocks.
470  *
471  * @param nand          NAND device
472  * @param offset        offset in flash
473  * @param length        buffer length
474  * @param buf           buffer to read from
475  * @return              0 in case of success
476  */
477 int nand_write_skip_bad(nand_info_t *nand, size_t offset, size_t *length,
478                         u_char *buffer)
479 {
480         int rval;
481         size_t left_to_write = *length;
482         size_t len_incl_bad;
483         u_char *p_buffer = buffer;
484
485         /* Reject writes, which are not page aligned */
486         if ((offset & (nand->writesize - 1)) != 0 ||
487             (*length & (nand->writesize - 1)) != 0) {
488                 printf ("Attempt to write non page aligned data\n");
489                 return -EINVAL;
490         }
491
492         len_incl_bad = get_len_incl_bad (nand, offset, *length);
493
494         if ((offset + len_incl_bad) >= nand->size) {
495                 printf ("Attempt to write outside the flash area\n");
496                 return -EINVAL;
497         }
498
499         if (len_incl_bad == *length) {
500                 rval = nand_write (nand, offset, length, buffer);
501                 if (rval != 0) {
502                         printf ("NAND write to offset %x failed %d\n",
503                                 offset, rval);
504                         return rval;
505                 }
506         }
507
508         while (left_to_write > 0) {
509                 size_t block_offset = offset & (nand->erasesize - 1);
510                 size_t write_size;
511
512                 if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
513                         printf ("Skip bad block 0x%08x\n",
514                                 offset & ~(nand->erasesize - 1));
515                         offset += nand->erasesize - block_offset;
516                         continue;
517                 }
518
519                 if (left_to_write < (nand->erasesize - block_offset))
520                         write_size = left_to_write;
521                 else
522                         write_size = nand->erasesize - block_offset;
523
524                 rval = nand_write (nand, offset, &write_size, p_buffer);
525                 if (rval != 0) {
526                         printf ("NAND write to offset %x failed %d\n",
527                                 offset, rval);
528                         *length -= left_to_write;
529                         return rval;
530                 }
531
532                 left_to_write -= write_size;
533                 offset        += write_size;
534                 p_buffer      += write_size;
535         }
536
537         return 0;
538 }
539
540 /**
541  * nand_read_skip_bad:
542  *
543  * Read image from NAND flash.
544  * Blocks that are marked bad are skipped and the next block is readen
545  * instead as long as the image is short enough to fit even after skipping the
546  * bad blocks.
547  *
548  * @param nand NAND device
549  * @param offset offset in flash
550  * @param length buffer length, on return holds remaining bytes to read
551  * @param buffer buffer to write to
552  * @return 0 in case of success
553  */
554 int nand_read_skip_bad(nand_info_t *nand, size_t offset, size_t *length,
555                        u_char *buffer)
556 {
557         int rval;
558         size_t left_to_read = *length;
559         size_t len_incl_bad;
560         u_char *p_buffer = buffer;
561
562         len_incl_bad = get_len_incl_bad (nand, offset, *length);
563
564         if ((offset + len_incl_bad) >= nand->size) {
565                 printf ("Attempt to read outside the flash area\n");
566                 return -EINVAL;
567         }
568
569         if (len_incl_bad == *length) {
570                 rval = nand_read (nand, offset, length, buffer);
571                 if (rval != 0) {
572                         printf ("NAND read from offset %x failed %d\n",
573                                 offset, rval);
574                         return rval;
575                 }
576         }
577
578         while (left_to_read > 0) {
579                 size_t block_offset = offset & (nand->erasesize - 1);
580                 size_t read_length;
581
582                 if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
583                         printf ("Skipping bad block 0x%08x\n",
584                                 offset & ~(nand->erasesize - 1));
585                         offset += nand->erasesize - block_offset;
586                         continue;
587                 }
588
589                 if (left_to_read < (nand->erasesize - block_offset))
590                         read_length = left_to_read;
591                 else
592                         read_length = nand->erasesize - block_offset;
593
594                 rval = nand_read (nand, offset, &read_length, p_buffer);
595                 if (rval != 0) {
596                         printf ("NAND read from offset %x failed %d\n",
597                                 offset, rval);
598                         *length -= left_to_read;
599                         return rval;
600                 }
601
602                 left_to_read -= read_length;
603                 offset       += read_length;
604                 p_buffer     += read_length;
605         }
606
607         return 0;
608 }
609
610 #endif /* defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY) */