blob: 5246bbf1ac28ab1b87e06551c3af32cfe01ea1bd [file] [log] [blame]
Stefan Roesed351b2b2006-10-10 12:36:02 +02001/*
Marcel Ziswileraea68562007-12-30 03:30:46 +01002 * drivers/mtd/nand/nand_util.c
Stefan Roesed351b2b2006-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 *
Ben Gardiner34dd5672011-06-14 16:35:06 -040014 * Copyright (C) 2008 Nokia Corporation: drop_ffs() function by
15 * Artem Bityutskiy <dedekind1@gmail.com> from mtd-utils
16 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020017 * SPDX-License-Identifier: GPL-2.0+
Stefan Roesed351b2b2006-10-10 12:36:02 +020018 */
19
20#include <common.h>
Stefan Roesed351b2b2006-10-10 12:36:02 +020021#include <command.h>
22#include <watchdog.h>
23#include <malloc.h>
Dirk Behme32d1f762007-08-02 17:42:08 +020024#include <div64.h>
Stefan Roesed351b2b2006-10-10 12:36:02 +020025
William Juul52c07962007-10-31 13:53:06 +010026#include <asm/errno.h>
27#include <linux/mtd/mtd.h>
Stefan Roesed351b2b2006-10-10 12:36:02 +020028#include <nand.h>
29#include <jffs2/jffs2.h>
30
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +000031typedef struct erase_info erase_info_t;
32typedef struct mtd_info mtd_info_t;
Stefan Roesed351b2b2006-10-10 12:36:02 +020033
34/* support only for native endian JFFS2 */
35#define cpu_to_je16(x) (x)
36#define cpu_to_je32(x) (x)
37
Stefan Roesed351b2b2006-10-10 12:36:02 +020038/**
39 * nand_erase_opts: - erase NAND flash with support for various options
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +000040 * (jffs2 formatting)
Stefan Roesed351b2b2006-10-10 12:36:02 +020041 *
42 * @param meminfo NAND device to erase
43 * @param opts options, @see struct nand_erase_options
44 * @return 0 in case of success
45 *
46 * This code is ported from flash_eraseall.c from Linux mtd utils by
47 * Arcom Control System Ltd.
48 */
49int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
50{
51 struct jffs2_unknown_node cleanmarker;
Stefan Roesed351b2b2006-10-10 12:36:02 +020052 erase_info_t erase;
Scott Wood1b5cd512010-08-25 14:43:29 -050053 unsigned long erase_length, erased_length; /* in blocks */
Stefan Roesed351b2b2006-10-10 12:36:02 +020054 int result;
55 int percent_complete = -1;
Stefan Roesed351b2b2006-10-10 12:36:02 +020056 const char *mtd_device = meminfo->name;
William Juul52c07962007-10-31 13:53:06 +010057 struct mtd_oob_ops oob_opts;
58 struct nand_chip *chip = meminfo->priv;
Stefan Roesed351b2b2006-10-10 12:36:02 +020059
Benoît Thébaudeauad5d6ee2012-11-05 10:16:15 +000060 if ((opts->offset & (meminfo->erasesize - 1)) != 0) {
61 printf("Attempt to erase non block-aligned data\n");
Scott Wood1b5cd512010-08-25 14:43:29 -050062 return -1;
63 }
64
Stefan Roesed351b2b2006-10-10 12:36:02 +020065 memset(&erase, 0, sizeof(erase));
William Juul52c07962007-10-31 13:53:06 +010066 memset(&oob_opts, 0, sizeof(oob_opts));
Stefan Roesed351b2b2006-10-10 12:36:02 +020067
68 erase.mtd = meminfo;
69 erase.len = meminfo->erasesize;
Stefan Roese198b23e2006-10-28 15:55:52 +020070 erase.addr = opts->offset;
Scott Wood1b5cd512010-08-25 14:43:29 -050071 erase_length = lldiv(opts->length + meminfo->erasesize - 1,
72 meminfo->erasesize);
Stefan Roesed351b2b2006-10-10 12:36:02 +020073
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +000074 cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
75 cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
William Juul52c07962007-10-31 13:53:06 +010076 cleanmarker.totlen = cpu_to_je32(8);
Stefan Roesed351b2b2006-10-10 12:36:02 +020077
78 /* scrub option allows to erase badblock. To prevent internal
79 * check from erase() method, set block check method to dummy
80 * and disable bad block table while erasing.
81 */
82 if (opts->scrub) {
Marek Vasut971d9a12011-09-12 06:04:06 +020083 erase.scrub = opts->scrub;
84 /*
85 * We don't need the bad block table anymore...
Stefan Roesed351b2b2006-10-10 12:36:02 +020086 * after scrub, there are no bad blocks left!
87 */
Marek Vasut971d9a12011-09-12 06:04:06 +020088 if (chip->bbt) {
89 kfree(chip->bbt);
Stefan Roesed351b2b2006-10-10 12:36:02 +020090 }
Marek Vasut971d9a12011-09-12 06:04:06 +020091 chip->bbt = NULL;
Stefan Roesed351b2b2006-10-10 12:36:02 +020092 }
93
Scott Wood1b5cd512010-08-25 14:43:29 -050094 for (erased_length = 0;
95 erased_length < erase_length;
Stefan Roesed351b2b2006-10-10 12:36:02 +020096 erase.addr += meminfo->erasesize) {
William Juulb76ec382007-11-08 10:39:53 +010097
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +000098 WATCHDOG_RESET();
Stefan Roesed351b2b2006-10-10 12:36:02 +020099
Heiko Schocher0ae98b42013-06-24 18:50:40 +0200100 if (opts->lim && (erase.addr >= (opts->offset + opts->lim))) {
101 puts("Size of erase exceeds limit\n");
102 return -EFBIG;
103 }
Masahiro Yamada7580d582013-07-12 10:53:37 +0900104 if (!opts->scrub) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000105 int ret = mtd_block_isbad(meminfo, erase.addr);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200106 if (ret > 0) {
107 if (!opts->quiet)
108 printf("\rSkipping bad block at "
Stefan Roese586b3a62009-05-11 16:03:55 +0200109 "0x%08llx "
Wolfgang Denkd5cf1a42006-10-12 11:43:47 +0200110 " \n",
111 erase.addr);
Scott Wood1b5cd512010-08-25 14:43:29 -0500112
113 if (!opts->spread)
114 erased_length++;
115
Stefan Roesed351b2b2006-10-10 12:36:02 +0200116 continue;
117
118 } else if (ret < 0) {
119 printf("\n%s: MTD get bad block failed: %d\n",
120 mtd_device,
121 ret);
122 return -1;
123 }
124 }
125
Scott Wood1b5cd512010-08-25 14:43:29 -0500126 erased_length++;
127
Sergey Lapin3a38a552013-01-14 03:46:50 +0000128 result = mtd_erase(meminfo, &erase);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200129 if (result != 0) {
130 printf("\n%s: MTD Erase failure: %d\n",
131 mtd_device, result);
132 continue;
133 }
134
135 /* format for JFFS2 ? */
Scott Woodd50ad352008-10-29 14:20:26 -0500136 if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000137 struct mtd_oob_ops ops;
138 ops.ooblen = 8;
139 ops.datbuf = NULL;
140 ops.oobbuf = (uint8_t *)&cleanmarker;
141 ops.ooboffs = 0;
142 ops.mode = MTD_OPS_AUTO_OOB;
William Juulb76ec382007-11-08 10:39:53 +0100143
Sergey Lapin3a38a552013-01-14 03:46:50 +0000144 result = mtd_write_oob(meminfo,
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200145 erase.addr,
146 &ops);
William Juul52c07962007-10-31 13:53:06 +0100147 if (result != 0) {
148 printf("\n%s: MTD writeoob failure: %d\n",
Scott Woodd50ad352008-10-29 14:20:26 -0500149 mtd_device, result);
William Juul52c07962007-10-31 13:53:06 +0100150 continue;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200151 }
152 }
153
154 if (!opts->quiet) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500155 unsigned long long n = erased_length * 100ULL;
Matthias Fuchs82714b92007-09-11 17:04:00 +0200156 int percent;
157
158 do_div(n, erase_length);
159 percent = (int)n;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200160
161 /* output progress message only at whole percent
162 * steps to reduce the number of messages printed
163 * on (slow) serial consoles
164 */
165 if (percent != percent_complete) {
166 percent_complete = percent;
167
Stefan Roese586b3a62009-05-11 16:03:55 +0200168 printf("\rErasing at 0x%llx -- %3d%% complete.",
Scott Woodd50ad352008-10-29 14:20:26 -0500169 erase.addr, percent);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200170
171 if (opts->jffs2 && result == 0)
Stefan Roese586b3a62009-05-11 16:03:55 +0200172 printf(" Cleanmarker written at 0x%llx.",
Scott Woodd50ad352008-10-29 14:20:26 -0500173 erase.addr);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200174 }
175 }
176 }
177 if (!opts->quiet)
178 printf("\n");
179
Marek Vasut971d9a12011-09-12 06:04:06 +0200180 if (opts->scrub)
181 chip->scan_bbt(meminfo);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200182
183 return 0;
184}
185
Nishanth Menonb20f8402008-12-13 09:43:06 -0600186#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
187
Stefan Roesed351b2b2006-10-10 12:36:02 +0200188/******************************************************************************
189 * Support for locking / unlocking operations of some NAND devices
190 *****************************************************************************/
191
Stefan Roesed351b2b2006-10-10 12:36:02 +0200192/**
193 * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
194 * state
195 *
Nishanth Menonb20f8402008-12-13 09:43:06 -0600196 * @param mtd nand mtd instance
Stefan Roesed351b2b2006-10-10 12:36:02 +0200197 * @param tight bring device in lock tight mode
198 *
199 * @return 0 on success, -1 in case of error
200 *
201 * The lock / lock-tight command only applies to the whole chip. To get some
202 * parts of the chip lock and others unlocked use the following sequence:
203 *
204 * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
205 * - Call nand_unlock() once for each consecutive area to be unlocked
206 * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
207 *
208 * If the device is in lock-tight state software can't change the
209 * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
210 * calls will fail. It is only posible to leave lock-tight state by
211 * an hardware signal (low pulse on _WP pin) or by power down.
212 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600213int nand_lock(struct mtd_info *mtd, int tight)
Stefan Roesed351b2b2006-10-10 12:36:02 +0200214{
215 int ret = 0;
216 int status;
Nishanth Menonb20f8402008-12-13 09:43:06 -0600217 struct nand_chip *chip = mtd->priv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200218
219 /* select the NAND device */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600220 chip->select_chip(mtd, 0);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200221
Joe Hershberger8b177d42013-02-08 09:27:19 +0000222 /* check the Lock Tight Status */
223 chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, 0);
224 if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
225 printf("nand_lock: Device is locked tight!\n");
226 ret = -1;
227 goto out;
228 }
229
Nishanth Menonb20f8402008-12-13 09:43:06 -0600230 chip->cmdfunc(mtd,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200231 (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
232 -1, -1);
233
234 /* call wait ready function */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600235 status = chip->waitfunc(mtd, chip);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200236
237 /* see if device thinks it succeeded */
238 if (status & 0x01) {
239 ret = -1;
240 }
241
Joe Hershberger8b177d42013-02-08 09:27:19 +0000242 out:
Stefan Roesed351b2b2006-10-10 12:36:02 +0200243 /* de-select the NAND device */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600244 chip->select_chip(mtd, -1);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200245 return ret;
246}
247
248/**
249 * nand_get_lock_status: - query current lock state from one page of NAND
250 * flash
251 *
Nishanth Menonb20f8402008-12-13 09:43:06 -0600252 * @param mtd nand mtd instance
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000253 * @param offset page address to query (must be page-aligned!)
Stefan Roesed351b2b2006-10-10 12:36:02 +0200254 *
255 * @return -1 in case of error
256 * >0 lock status:
257 * bitfield with the following combinations:
258 * NAND_LOCK_STATUS_TIGHT: page in tight state
Stefan Roesed351b2b2006-10-10 12:36:02 +0200259 * NAND_LOCK_STATUS_UNLOCK: page unlocked
260 *
261 */
Jean-Christophe PLAGNIOL-VILLARD2511ba02009-05-16 14:27:40 +0200262int nand_get_lock_status(struct mtd_info *mtd, loff_t offset)
Stefan Roesed351b2b2006-10-10 12:36:02 +0200263{
264 int ret = 0;
265 int chipnr;
266 int page;
Nishanth Menonb20f8402008-12-13 09:43:06 -0600267 struct nand_chip *chip = mtd->priv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200268
269 /* select the NAND device */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600270 chipnr = (int)(offset >> chip->chip_shift);
271 chip->select_chip(mtd, chipnr);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200272
273
Nishanth Menonb20f8402008-12-13 09:43:06 -0600274 if ((offset & (mtd->writesize - 1)) != 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000275 printf("nand_get_lock_status: "
Stefan Roesed351b2b2006-10-10 12:36:02 +0200276 "Start address must be beginning of "
277 "nand page!\n");
278 ret = -1;
279 goto out;
280 }
281
282 /* check the Lock Status */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600283 page = (int)(offset >> chip->page_shift);
284 chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200285
Nishanth Menonb20f8402008-12-13 09:43:06 -0600286 ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT
Stefan Roesed351b2b2006-10-10 12:36:02 +0200287 | NAND_LOCK_STATUS_UNLOCK);
288
289 out:
290 /* de-select the NAND device */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600291 chip->select_chip(mtd, -1);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200292 return ret;
293}
294
295/**
296 * nand_unlock: - Unlock area of NAND pages
297 * only one consecutive area can be unlocked at one time!
298 *
Nishanth Menonb20f8402008-12-13 09:43:06 -0600299 * @param mtd nand mtd instance
Stefan Roesed351b2b2006-10-10 12:36:02 +0200300 * @param start start byte address
301 * @param length number of bytes to unlock (must be a multiple of
William Juul52c07962007-10-31 13:53:06 +0100302 * page size nand->writesize)
Joe Hershbergercccf5952012-08-22 16:49:42 -0500303 * @param allexcept if set, unlock everything not selected
Stefan Roesed351b2b2006-10-10 12:36:02 +0200304 *
305 * @return 0 on success, -1 in case of error
306 */
Joe Hershbergerdfa8ba42012-08-22 16:49:43 -0500307int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
308 int allexcept)
Stefan Roesed351b2b2006-10-10 12:36:02 +0200309{
310 int ret = 0;
311 int chipnr;
312 int status;
313 int page;
Nishanth Menonb20f8402008-12-13 09:43:06 -0600314 struct nand_chip *chip = mtd->priv;
Joe Hershbergercccf5952012-08-22 16:49:42 -0500315
Joe Hershbergerdfa8ba42012-08-22 16:49:43 -0500316 debug("nand_unlock%s: start: %08llx, length: %d!\n",
Joe Hershbergercccf5952012-08-22 16:49:42 -0500317 allexcept ? " (allexcept)" : "", start, length);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200318
319 /* select the NAND device */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600320 chipnr = (int)(start >> chip->chip_shift);
321 chip->select_chip(mtd, chipnr);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200322
323 /* check the WP bit */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600324 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
325 if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000326 printf("nand_unlock: Device is write protected!\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200327 ret = -1;
328 goto out;
329 }
330
Joe Hershberger8b177d42013-02-08 09:27:19 +0000331 /* check the Lock Tight Status */
332 page = (int)(start >> chip->page_shift);
333 chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
334 if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
335 printf("nand_unlock: Device is locked tight!\n");
336 ret = -1;
337 goto out;
338 }
339
Nishanth Menonb20f8402008-12-13 09:43:06 -0600340 if ((start & (mtd->erasesize - 1)) != 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000341 printf("nand_unlock: Start address must be beginning of "
Nishanth Menonb20f8402008-12-13 09:43:06 -0600342 "nand block!\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200343 ret = -1;
344 goto out;
345 }
346
Nishanth Menonb20f8402008-12-13 09:43:06 -0600347 if (length == 0 || (length & (mtd->erasesize - 1)) != 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000348 printf("nand_unlock: Length must be a multiple of nand block "
Nishanth Menonb20f8402008-12-13 09:43:06 -0600349 "size %08x!\n", mtd->erasesize);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200350 ret = -1;
351 goto out;
352 }
353
Nishanth Menonb20f8402008-12-13 09:43:06 -0600354 /*
355 * Set length so that the last address is set to the
356 * starting address of the last block
357 */
358 length -= mtd->erasesize;
359
Stefan Roesed351b2b2006-10-10 12:36:02 +0200360 /* submit address of first page to unlock */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600361 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200362
363 /* submit ADDRESS of LAST page to unlock */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600364 page += (int)(length >> chip->page_shift);
Joe Hershbergercccf5952012-08-22 16:49:42 -0500365
366 /*
367 * Page addresses for unlocking are supposed to be block-aligned.
368 * At least some NAND chips use the low bit to indicate that the
369 * page range should be inverted.
370 */
371 if (allexcept)
372 page |= 1;
373
Nishanth Menonb20f8402008-12-13 09:43:06 -0600374 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200375
376 /* call wait ready function */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600377 status = chip->waitfunc(mtd, chip);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200378 /* see if device thinks it succeeded */
379 if (status & 0x01) {
380 /* there was an error */
381 ret = -1;
382 goto out;
383 }
384
385 out:
386 /* de-select the NAND device */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600387 chip->select_chip(mtd, -1);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200388 return ret;
389}
William Juul52c07962007-10-31 13:53:06 +0100390#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200391
Scott Woodcc5f3392008-06-12 13:20:16 -0500392/**
Scott Woodfe3b5e12010-07-30 16:11:41 -0500393 * check_skip_len
Scott Woodcc5f3392008-06-12 13:20:16 -0500394 *
Scott Woodfe3b5e12010-07-30 16:11:41 -0500395 * Check if there are any bad blocks, and whether length including bad
396 * blocks fits into device
Scott Woodcc5f3392008-06-12 13:20:16 -0500397 *
398 * @param nand NAND device
399 * @param offset offset in flash
400 * @param length image length
Tom Rini32d96182013-03-14 05:32:50 +0000401 * @param used length of flash needed for the requested length
Scott Woodfe3b5e12010-07-30 16:11:41 -0500402 * @return 0 if the image fits and there are no bad blocks
403 * 1 if the image fits, but there are bad blocks
404 * -1 if the image does not fit
Scott Woodcc5f3392008-06-12 13:20:16 -0500405 */
Tom Rini32d96182013-03-14 05:32:50 +0000406static int check_skip_len(nand_info_t *nand, loff_t offset, size_t length,
407 size_t *used)
Scott Woodcc5f3392008-06-12 13:20:16 -0500408{
Scott Woodcc5f3392008-06-12 13:20:16 -0500409 size_t len_excl_bad = 0;
Scott Woodfe3b5e12010-07-30 16:11:41 -0500410 int ret = 0;
Scott Woodcc5f3392008-06-12 13:20:16 -0500411
412 while (len_excl_bad < length) {
Scott Woodfe3b5e12010-07-30 16:11:41 -0500413 size_t block_len, block_off;
414 loff_t block_start;
Scott Woodcc5f3392008-06-12 13:20:16 -0500415
Scott Woodfe3b5e12010-07-30 16:11:41 -0500416 if (offset >= nand->size)
417 return -1;
Scott Woodcc5f3392008-06-12 13:20:16 -0500418
Scott Woodfe3b5e12010-07-30 16:11:41 -0500419 block_start = offset & ~(loff_t)(nand->erasesize - 1);
420 block_off = offset & (nand->erasesize - 1);
421 block_len = nand->erasesize - block_off;
Scott Woodcc5f3392008-06-12 13:20:16 -0500422
Scott Woodfe3b5e12010-07-30 16:11:41 -0500423 if (!nand_block_isbad(nand, block_start))
424 len_excl_bad += block_len;
425 else
426 ret = 1;
427
428 offset += block_len;
Tom Rini32d96182013-03-14 05:32:50 +0000429 *used += block_len;
Scott Woodcc5f3392008-06-12 13:20:16 -0500430 }
431
Tom Rini32d96182013-03-14 05:32:50 +0000432 /* If the length is not a multiple of block_len, adjust. */
433 if (len_excl_bad > length)
434 *used -= (len_excl_bad - length);
435
Scott Woodfe3b5e12010-07-30 16:11:41 -0500436 return ret;
Scott Woodcc5f3392008-06-12 13:20:16 -0500437}
Ben Gardiner34dd5672011-06-14 16:35:06 -0400438
439#ifdef CONFIG_CMD_NAND_TRIMFFS
440static size_t drop_ffs(const nand_info_t *nand, const u_char *buf,
441 const size_t *len)
442{
htbegin610e8552013-03-01 23:00:34 +0000443 size_t l = *len;
444 ssize_t i;
Ben Gardiner34dd5672011-06-14 16:35:06 -0400445
446 for (i = l - 1; i >= 0; i--)
447 if (buf[i] != 0xFF)
448 break;
449
450 /* The resulting length must be aligned to the minimum flash I/O size */
451 l = i + 1;
452 l = (l + nand->writesize - 1) / nand->writesize;
453 l *= nand->writesize;
454
455 /*
456 * since the input length may be unaligned, prevent access past the end
457 * of the buffer
458 */
459 return min(l, *len);
460}
461#endif
Scott Woodcc5f3392008-06-12 13:20:16 -0500462
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
Tom Rini32d96182013-03-14 05:32:50 +0000469 * skipping the bad blocks. Due to bad blocks we may not be able to
470 * perform the requested write. In the case where the write would
471 * extend beyond the end of the NAND device, both length and actual (if
472 * not NULL) are set to 0. In the case where the write would extend
473 * beyond the limit we are passed, length is set to 0 and actual is set
474 * to the required length.
Scott Woodcc5f3392008-06-12 13:20:16 -0500475 *
476 * @param nand NAND device
477 * @param offset offset in flash
478 * @param length buffer length
Tom Rini32d96182013-03-14 05:32:50 +0000479 * @param actual set to size required to write length worth of
480 * buffer or 0 on error, if not NULL
481 * @param lim maximum size that actual may be in order to not
482 * exceed the buffer
Lei Wen4b5deaa2011-01-06 11:11:58 +0800483 * @param buffer buffer to read from
Ben Gardiner1caafbb2011-05-24 10:18:35 -0400484 * @param flags flags modifying the behaviour of the write to NAND
Scott Woodcc5f3392008-06-12 13:20:16 -0500485 * @return 0 in case of success
486 */
Jean-Christophe PLAGNIOL-VILLARD2511ba02009-05-16 14:27:40 +0200487int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
Tom Rini32d96182013-03-14 05:32:50 +0000488 size_t *actual, loff_t lim, u_char *buffer, int flags)
Scott Woodcc5f3392008-06-12 13:20:16 -0500489{
Lei Wen4b5deaa2011-01-06 11:11:58 +0800490 int rval = 0, blocksize;
Scott Woodcc5f3392008-06-12 13:20:16 -0500491 size_t left_to_write = *length;
Tom Rini32d96182013-03-14 05:32:50 +0000492 size_t used_for_write = 0;
Scott Woodcc5f3392008-06-12 13:20:16 -0500493 u_char *p_buffer = buffer;
Scott Woodfe3b5e12010-07-30 16:11:41 -0500494 int need_skip;
Scott Woodcc5f3392008-06-12 13:20:16 -0500495
Tom Rini32d96182013-03-14 05:32:50 +0000496 if (actual)
497 *actual = 0;
498
Lei Wen4b5deaa2011-01-06 11:11:58 +0800499#ifdef CONFIG_CMD_NAND_YAFFS
Ben Gardiner1caafbb2011-05-24 10:18:35 -0400500 if (flags & WITH_YAFFS_OOB) {
Ben Gardiner871dea42011-06-14 16:35:05 -0400501 if (flags & ~WITH_YAFFS_OOB)
502 return -EINVAL;
503
Lei Wen4b5deaa2011-01-06 11:11:58 +0800504 int pages;
505 pages = nand->erasesize / nand->writesize;
506 blocksize = (pages * nand->oobsize) + nand->erasesize;
507 if (*length % (nand->writesize + nand->oobsize)) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000508 printf("Attempt to write incomplete page"
Lei Wen4b5deaa2011-01-06 11:11:58 +0800509 " in yaffs mode\n");
510 return -EINVAL;
511 }
512 } else
513#endif
514 {
515 blocksize = nand->erasesize;
516 }
517
Scott Woodfe3b5e12010-07-30 16:11:41 -0500518 /*
519 * nand_write() handles unaligned, partial page writes.
520 *
521 * We allow length to be unaligned, for convenience in
522 * using the $filesize variable.
523 *
524 * However, starting at an unaligned offset makes the
525 * semantics of bad block skipping ambiguous (really,
526 * you should only start a block skipping access at a
527 * partition boundary). So don't try to handle that.
528 */
529 if ((offset & (nand->writesize - 1)) != 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000530 printf("Attempt to write non page-aligned data\n");
Scott Woodfe3b5e12010-07-30 16:11:41 -0500531 *length = 0;
Scott Woodcc5f3392008-06-12 13:20:16 -0500532 return -EINVAL;
533 }
534
Tom Rini32d96182013-03-14 05:32:50 +0000535 need_skip = check_skip_len(nand, offset, *length, &used_for_write);
536
537 if (actual)
538 *actual = used_for_write;
539
Scott Woodfe3b5e12010-07-30 16:11:41 -0500540 if (need_skip < 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000541 printf("Attempt to write outside the flash area\n");
Scott Woodfe3b5e12010-07-30 16:11:41 -0500542 *length = 0;
Scott Woodcc5f3392008-06-12 13:20:16 -0500543 return -EINVAL;
544 }
545
Tom Rini32d96182013-03-14 05:32:50 +0000546 if (used_for_write > lim) {
547 puts("Size of write exceeds partition or device limit\n");
548 *length = 0;
549 return -EFBIG;
550 }
551
Ben Gardiner34dd5672011-06-14 16:35:06 -0400552 if (!need_skip && !(flags & WITH_DROP_FFS)) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000553 rval = nand_write(nand, offset, length, buffer);
Scott Woodfe3b5e12010-07-30 16:11:41 -0500554 if (rval == 0)
555 return 0;
Scott Wood90e0a6b2008-11-25 10:47:02 -0600556
Scott Woodfe3b5e12010-07-30 16:11:41 -0500557 *length = 0;
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000558 printf("NAND write to offset %llx failed %d\n",
Scott Woodfe3b5e12010-07-30 16:11:41 -0500559 offset, rval);
Scott Wood90e0a6b2008-11-25 10:47:02 -0600560 return rval;
Scott Woodcc5f3392008-06-12 13:20:16 -0500561 }
562
563 while (left_to_write > 0) {
564 size_t block_offset = offset & (nand->erasesize - 1);
Ben Gardiner34dd5672011-06-14 16:35:06 -0400565 size_t write_size, truncated_write_size;
Scott Woodcc5f3392008-06-12 13:20:16 -0500566
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000567 WATCHDOG_RESET();
Giulio Benetti749bd662009-07-31 17:30:34 -0500568
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000569 if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
570 printf("Skip bad block 0x%08llx\n",
Scott Woodcc5f3392008-06-12 13:20:16 -0500571 offset & ~(nand->erasesize - 1));
572 offset += nand->erasesize - block_offset;
573 continue;
574 }
575
Lei Wen4b5deaa2011-01-06 11:11:58 +0800576 if (left_to_write < (blocksize - block_offset))
Scott Woodcc5f3392008-06-12 13:20:16 -0500577 write_size = left_to_write;
578 else
Lei Wen4b5deaa2011-01-06 11:11:58 +0800579 write_size = blocksize - block_offset;
580
581#ifdef CONFIG_CMD_NAND_YAFFS
Ben Gardiner1caafbb2011-05-24 10:18:35 -0400582 if (flags & WITH_YAFFS_OOB) {
Lei Wen4b5deaa2011-01-06 11:11:58 +0800583 int page, pages;
584 size_t pagesize = nand->writesize;
585 size_t pagesize_oob = pagesize + nand->oobsize;
586 struct mtd_oob_ops ops;
587
588 ops.len = pagesize;
589 ops.ooblen = nand->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000590 ops.mode = MTD_OPS_AUTO_OOB;
Lei Wen4b5deaa2011-01-06 11:11:58 +0800591 ops.ooboffs = 0;
592
593 pages = write_size / pagesize_oob;
594 for (page = 0; page < pages; page++) {
Scott Woodea95b642011-02-02 18:15:57 -0600595 WATCHDOG_RESET();
596
Lei Wen4b5deaa2011-01-06 11:11:58 +0800597 ops.datbuf = p_buffer;
598 ops.oobbuf = ops.datbuf + pagesize;
Scott Woodcc5f3392008-06-12 13:20:16 -0500599
Sergey Lapin3a38a552013-01-14 03:46:50 +0000600 rval = mtd_write_oob(nand, offset, &ops);
Liu, Wentaode95b322012-01-17 19:55:02 +0000601 if (rval != 0)
Lei Wen4b5deaa2011-01-06 11:11:58 +0800602 break;
603
604 offset += pagesize;
605 p_buffer += pagesize_oob;
606 }
607 }
608 else
609#endif
610 {
Ben Gardiner34dd5672011-06-14 16:35:06 -0400611 truncated_write_size = write_size;
612#ifdef CONFIG_CMD_NAND_TRIMFFS
613 if (flags & WITH_DROP_FFS)
614 truncated_write_size = drop_ffs(nand, p_buffer,
615 &write_size);
616#endif
617
618 rval = nand_write(nand, offset, &truncated_write_size,
619 p_buffer);
Lei Wen4b5deaa2011-01-06 11:11:58 +0800620 offset += write_size;
621 p_buffer += write_size;
622 }
623
Scott Woodcc5f3392008-06-12 13:20:16 -0500624 if (rval != 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000625 printf("NAND write to offset %llx failed %d\n",
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200626 offset, rval);
Scott Woodcc5f3392008-06-12 13:20:16 -0500627 *length -= left_to_write;
628 return rval;
629 }
630
631 left_to_write -= write_size;
Scott Woodcc5f3392008-06-12 13:20:16 -0500632 }
633
634 return 0;
635}
636
637/**
638 * nand_read_skip_bad:
639 *
640 * Read image from NAND flash.
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000641 * Blocks that are marked bad are skipped and the next block is read
Tom Rini32d96182013-03-14 05:32:50 +0000642 * instead as long as the image is short enough to fit even after
643 * skipping the bad blocks. Due to bad blocks we may not be able to
644 * perform the requested read. In the case where the read would extend
645 * beyond the end of the NAND device, both length and actual (if not
646 * NULL) are set to 0. In the case where the read would extend beyond
647 * the limit we are passed, length is set to 0 and actual is set to the
648 * required length.
Scott Woodcc5f3392008-06-12 13:20:16 -0500649 *
650 * @param nand NAND device
651 * @param offset offset in flash
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000652 * @param length buffer length, on return holds number of read bytes
Tom Rini32d96182013-03-14 05:32:50 +0000653 * @param actual set to size required to read length worth of buffer or 0
654 * on error, if not NULL
655 * @param lim maximum size that actual may be in order to not exceed the
656 * buffer
Scott Woodcc5f3392008-06-12 13:20:16 -0500657 * @param buffer buffer to write to
658 * @return 0 in case of success
659 */
Jean-Christophe PLAGNIOL-VILLARD2511ba02009-05-16 14:27:40 +0200660int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
Tom Rini32d96182013-03-14 05:32:50 +0000661 size_t *actual, loff_t lim, u_char *buffer)
Scott Woodcc5f3392008-06-12 13:20:16 -0500662{
663 int rval;
664 size_t left_to_read = *length;
Tom Rini32d96182013-03-14 05:32:50 +0000665 size_t used_for_read = 0;
Scott Woodcc5f3392008-06-12 13:20:16 -0500666 u_char *p_buffer = buffer;
Scott Woodfe3b5e12010-07-30 16:11:41 -0500667 int need_skip;
Scott Woodcc5f3392008-06-12 13:20:16 -0500668
Scott Woodfe3b5e12010-07-30 16:11:41 -0500669 if ((offset & (nand->writesize - 1)) != 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000670 printf("Attempt to read non page-aligned data\n");
Scott Woodfe3b5e12010-07-30 16:11:41 -0500671 *length = 0;
Tom Rini32d96182013-03-14 05:32:50 +0000672 if (actual)
673 *actual = 0;
Scott Woodfe3b5e12010-07-30 16:11:41 -0500674 return -EINVAL;
675 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500676
Tom Rini32d96182013-03-14 05:32:50 +0000677 need_skip = check_skip_len(nand, offset, *length, &used_for_read);
678
679 if (actual)
680 *actual = used_for_read;
681
Scott Woodfe3b5e12010-07-30 16:11:41 -0500682 if (need_skip < 0) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000683 printf("Attempt to read outside the flash area\n");
Scott Woodfe3b5e12010-07-30 16:11:41 -0500684 *length = 0;
Scott Woodcc5f3392008-06-12 13:20:16 -0500685 return -EINVAL;
686 }
687
Tom Rini32d96182013-03-14 05:32:50 +0000688 if (used_for_read > lim) {
689 puts("Size of read exceeds partition or device limit\n");
690 *length = 0;
691 return -EFBIG;
692 }
693
Scott Woodfe3b5e12010-07-30 16:11:41 -0500694 if (!need_skip) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000695 rval = nand_read(nand, offset, length, buffer);
Valeriy Glushkovf2aead82009-07-14 13:51:10 +0300696 if (!rval || rval == -EUCLEAN)
697 return 0;
Scott Woodfe3b5e12010-07-30 16:11:41 -0500698
699 *length = 0;
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000700 printf("NAND read from offset %llx failed %d\n",
Valeriy Glushkovf2aead82009-07-14 13:51:10 +0300701 offset, rval);
Scott Wood90e0a6b2008-11-25 10:47:02 -0600702 return rval;
Scott Woodcc5f3392008-06-12 13:20:16 -0500703 }
704
705 while (left_to_read > 0) {
706 size_t block_offset = offset & (nand->erasesize - 1);
707 size_t read_length;
708
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000709 WATCHDOG_RESET();
Giulio Benetti749bd662009-07-31 17:30:34 -0500710
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000711 if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
712 printf("Skipping bad block 0x%08llx\n",
Scott Woodcc5f3392008-06-12 13:20:16 -0500713 offset & ~(nand->erasesize - 1));
714 offset += nand->erasesize - block_offset;
715 continue;
716 }
717
718 if (left_to_read < (nand->erasesize - block_offset))
719 read_length = left_to_read;
720 else
721 read_length = nand->erasesize - block_offset;
722
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000723 rval = nand_read(nand, offset, &read_length, p_buffer);
Valeriy Glushkovf2aead82009-07-14 13:51:10 +0300724 if (rval && rval != -EUCLEAN) {
Benoît Thébaudeaue26708e2012-11-05 10:15:46 +0000725 printf("NAND read from offset %llx failed %d\n",
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200726 offset, rval);
Scott Woodcc5f3392008-06-12 13:20:16 -0500727 *length -= left_to_read;
728 return rval;
729 }
730
731 left_to_read -= read_length;
732 offset += read_length;
733 p_buffer += read_length;
734 }
735
736 return 0;
737}
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100738
739#ifdef CONFIG_CMD_NAND_TORTURE
740
741/**
742 * check_pattern:
743 *
744 * Check if buffer contains only a certain byte pattern.
745 *
746 * @param buf buffer to check
747 * @param patt the pattern to check
748 * @param size buffer size in bytes
749 * @return 1 if there are only patt bytes in buf
750 * 0 if something else was found
751 */
752static int check_pattern(const u_char *buf, u_char patt, int size)
753{
754 int i;
755
756 for (i = 0; i < size; i++)
757 if (buf[i] != patt)
758 return 0;
759 return 1;
760}
761
762/**
763 * nand_torture:
764 *
765 * Torture a block of NAND flash.
766 * This is useful to determine if a block that caused a write error is still
767 * good or should be marked as bad.
768 *
769 * @param nand NAND device
770 * @param offset offset in flash
771 * @return 0 if the block is still good
772 */
773int nand_torture(nand_info_t *nand, loff_t offset)
774{
775 u_char patterns[] = {0xa5, 0x5a, 0x00};
776 struct erase_info instr = {
777 .mtd = nand,
778 .addr = offset,
779 .len = nand->erasesize,
780 };
781 size_t retlen;
782 int err, ret = -1, i, patt_count;
783 u_char *buf;
784
785 if ((offset & (nand->erasesize - 1)) != 0) {
786 puts("Attempt to torture a block at a non block-aligned offset\n");
787 return -EINVAL;
788 }
789
790 if (offset + nand->erasesize > nand->size) {
791 puts("Attempt to torture a block outside the flash area\n");
792 return -EINVAL;
793 }
794
795 patt_count = ARRAY_SIZE(patterns);
796
797 buf = malloc(nand->erasesize);
798 if (buf == NULL) {
799 puts("Out of memory for erase block buffer\n");
800 return -ENOMEM;
801 }
802
803 for (i = 0; i < patt_count; i++) {
804 err = nand->erase(nand, &instr);
805 if (err) {
806 printf("%s: erase() failed for block at 0x%llx: %d\n",
807 nand->name, instr.addr, err);
808 goto out;
809 }
810
811 /* Make sure the block contains only 0xff bytes */
812 err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
813 if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
814 printf("%s: read() failed for block at 0x%llx: %d\n",
815 nand->name, instr.addr, err);
816 goto out;
817 }
818
819 err = check_pattern(buf, 0xff, nand->erasesize);
820 if (!err) {
821 printf("Erased block at 0x%llx, but a non-0xff byte was found\n",
822 offset);
823 ret = -EIO;
824 goto out;
825 }
826
827 /* Write a pattern and check it */
828 memset(buf, patterns[i], nand->erasesize);
829 err = nand->write(nand, offset, nand->erasesize, &retlen, buf);
830 if (err || retlen != nand->erasesize) {
831 printf("%s: write() failed for block at 0x%llx: %d\n",
832 nand->name, instr.addr, err);
833 goto out;
834 }
835
836 err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
837 if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
838 printf("%s: read() failed for block at 0x%llx: %d\n",
839 nand->name, instr.addr, err);
840 goto out;
841 }
842
843 err = check_pattern(buf, patterns[i], nand->erasesize);
844 if (!err) {
845 printf("Pattern 0x%.2x checking failed for block at "
846 "0x%llx\n", patterns[i], offset);
847 ret = -EIO;
848 goto out;
849 }
850 }
851
852 ret = 0;
853
854out:
855 free(buf);
856 return ret;
857}
858
859#endif