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