blob: 71285b6669416b1402ebd8b03f7b9957b47a37a4 [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 *
Ben Gardiner169d54d2011-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 *
Tom Rini9d33fb42013-10-31 09:24:00 -040017 * Copyright 2010 Freescale Semiconductor
18 *
19 * SPDX-License-Identifier: GPL-2.0
Stefan Roese2255b2d2006-10-10 12:36:02 +020020 */
21
22#include <common.h>
Stefan Roese2255b2d2006-10-10 12:36:02 +020023#include <command.h>
24#include <watchdog.h>
25#include <malloc.h>
Simon Glass6e295182015-09-02 17:24:57 -060026#include <memalign.h>
Dirk Behme3a6d56c2007-08-02 17:42:08 +020027#include <div64.h>
Stefan Roese2255b2d2006-10-10 12:36:02 +020028
William Juulcfa460a2007-10-31 13:53:06 +010029#include <asm/errno.h>
30#include <linux/mtd/mtd.h>
Stefan Roese2255b2d2006-10-10 12:36:02 +020031#include <nand.h>
32#include <jffs2/jffs2.h>
33
Benoît Thébaudeaubd742802012-11-05 10:15:46 +000034typedef struct erase_info erase_info_t;
35typedef struct mtd_info mtd_info_t;
Stefan Roese2255b2d2006-10-10 12:36:02 +020036
37/* support only for native endian JFFS2 */
38#define cpu_to_je16(x) (x)
39#define cpu_to_je32(x) (x)
40
Stefan Roese2255b2d2006-10-10 12:36:02 +020041/**
42 * nand_erase_opts: - erase NAND flash with support for various options
Benoît Thébaudeaubd742802012-11-05 10:15:46 +000043 * (jffs2 formatting)
Stefan Roese2255b2d2006-10-10 12:36:02 +020044 *
45 * @param meminfo NAND device to erase
46 * @param opts options, @see struct nand_erase_options
47 * @return 0 in case of success
48 *
49 * This code is ported from flash_eraseall.c from Linux mtd utils by
50 * Arcom Control System Ltd.
51 */
52int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
53{
54 struct jffs2_unknown_node cleanmarker;
Stefan Roese2255b2d2006-10-10 12:36:02 +020055 erase_info_t erase;
Scott Wood30486322010-08-25 14:43:29 -050056 unsigned long erase_length, erased_length; /* in blocks */
Stefan Roese2255b2d2006-10-10 12:36:02 +020057 int result;
58 int percent_complete = -1;
Stefan Roese2255b2d2006-10-10 12:36:02 +020059 const char *mtd_device = meminfo->name;
William Juulcfa460a2007-10-31 13:53:06 +010060 struct mtd_oob_ops oob_opts;
61 struct nand_chip *chip = meminfo->priv;
Stefan Roese2255b2d2006-10-10 12:36:02 +020062
Benoît Thébaudeau8156f7322012-11-05 10:16:15 +000063 if ((opts->offset & (meminfo->erasesize - 1)) != 0) {
64 printf("Attempt to erase non block-aligned data\n");
Scott Wood30486322010-08-25 14:43:29 -050065 return -1;
66 }
67
Stefan Roese2255b2d2006-10-10 12:36:02 +020068 memset(&erase, 0, sizeof(erase));
William Juulcfa460a2007-10-31 13:53:06 +010069 memset(&oob_opts, 0, sizeof(oob_opts));
Stefan Roese2255b2d2006-10-10 12:36:02 +020070
71 erase.mtd = meminfo;
72 erase.len = meminfo->erasesize;
Stefan Roese856f0542006-10-28 15:55:52 +020073 erase.addr = opts->offset;
Scott Wood30486322010-08-25 14:43:29 -050074 erase_length = lldiv(opts->length + meminfo->erasesize - 1,
75 meminfo->erasesize);
Stefan Roese2255b2d2006-10-10 12:36:02 +020076
Benoît Thébaudeaubd742802012-11-05 10:15:46 +000077 cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
78 cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
William Juulcfa460a2007-10-31 13:53:06 +010079 cleanmarker.totlen = cpu_to_je32(8);
Stefan Roese2255b2d2006-10-10 12:36:02 +020080
81 /* scrub option allows to erase badblock. To prevent internal
82 * check from erase() method, set block check method to dummy
83 * and disable bad block table while erasing.
84 */
85 if (opts->scrub) {
Marek Vasut6d414192011-09-12 06:04:06 +020086 erase.scrub = opts->scrub;
87 /*
88 * We don't need the bad block table anymore...
Stefan Roese2255b2d2006-10-10 12:36:02 +020089 * after scrub, there are no bad blocks left!
90 */
Marek Vasut6d414192011-09-12 06:04:06 +020091 if (chip->bbt) {
92 kfree(chip->bbt);
Stefan Roese2255b2d2006-10-10 12:36:02 +020093 }
Marek Vasut6d414192011-09-12 06:04:06 +020094 chip->bbt = NULL;
Masahiro Yamadaab37b762014-12-26 22:20:58 +090095 chip->options &= ~NAND_BBT_SCANNED;
Stefan Roese2255b2d2006-10-10 12:36:02 +020096 }
97
Scott Wood30486322010-08-25 14:43:29 -050098 for (erased_length = 0;
99 erased_length < erase_length;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200100 erase.addr += meminfo->erasesize) {
William Juul4cbb6512007-11-08 10:39:53 +0100101
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000102 WATCHDOG_RESET();
Stefan Roese2255b2d2006-10-10 12:36:02 +0200103
Heiko Schochera67cc372013-06-24 18:50:40 +0200104 if (opts->lim && (erase.addr >= (opts->offset + opts->lim))) {
105 puts("Size of erase exceeds limit\n");
106 return -EFBIG;
107 }
Masahiro Yamada47b6dad2013-07-12 10:53:37 +0900108 if (!opts->scrub) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000109 int ret = mtd_block_isbad(meminfo, erase.addr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200110 if (ret > 0) {
111 if (!opts->quiet)
112 printf("\rSkipping bad block at "
Stefan Roese8d2effe2009-05-11 16:03:55 +0200113 "0x%08llx "
Wolfgang Denk87621bc2006-10-12 11:43:47 +0200114 " \n",
115 erase.addr);
Scott Wood30486322010-08-25 14:43:29 -0500116
117 if (!opts->spread)
118 erased_length++;
119
Stefan Roese2255b2d2006-10-10 12:36:02 +0200120 continue;
121
122 } else if (ret < 0) {
123 printf("\n%s: MTD get bad block failed: %d\n",
124 mtd_device,
125 ret);
126 return -1;
127 }
128 }
129
Scott Wood30486322010-08-25 14:43:29 -0500130 erased_length++;
131
Sergey Lapindfe64e22013-01-14 03:46:50 +0000132 result = mtd_erase(meminfo, &erase);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200133 if (result != 0) {
134 printf("\n%s: MTD Erase failure: %d\n",
135 mtd_device, result);
136 continue;
137 }
138
139 /* format for JFFS2 ? */
Scott Woodbd78bc62008-10-29 14:20:26 -0500140 if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000141 struct mtd_oob_ops ops;
142 ops.ooblen = 8;
143 ops.datbuf = NULL;
144 ops.oobbuf = (uint8_t *)&cleanmarker;
145 ops.ooboffs = 0;
146 ops.mode = MTD_OPS_AUTO_OOB;
William Juul4cbb6512007-11-08 10:39:53 +0100147
Sergey Lapindfe64e22013-01-14 03:46:50 +0000148 result = mtd_write_oob(meminfo,
Wolfgang Denk93e14592013-10-04 17:43:24 +0200149 erase.addr,
150 &ops);
William Juulcfa460a2007-10-31 13:53:06 +0100151 if (result != 0) {
152 printf("\n%s: MTD writeoob failure: %d\n",
Scott Woodbd78bc62008-10-29 14:20:26 -0500153 mtd_device, result);
William Juulcfa460a2007-10-31 13:53:06 +0100154 continue;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200155 }
156 }
157
158 if (!opts->quiet) {
Scott Wood30486322010-08-25 14:43:29 -0500159 unsigned long long n = erased_length * 100ULL;
Matthias Fuchs5bd7fe92007-09-11 17:04:00 +0200160 int percent;
161
162 do_div(n, erase_length);
163 percent = (int)n;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200164
165 /* output progress message only at whole percent
166 * steps to reduce the number of messages printed
167 * on (slow) serial consoles
168 */
169 if (percent != percent_complete) {
170 percent_complete = percent;
171
Stefan Roese8d2effe2009-05-11 16:03:55 +0200172 printf("\rErasing at 0x%llx -- %3d%% complete.",
Scott Woodbd78bc62008-10-29 14:20:26 -0500173 erase.addr, percent);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200174
175 if (opts->jffs2 && result == 0)
Stefan Roese8d2effe2009-05-11 16:03:55 +0200176 printf(" Cleanmarker written at 0x%llx.",
Scott Woodbd78bc62008-10-29 14:20:26 -0500177 erase.addr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200178 }
179 }
180 }
181 if (!opts->quiet)
182 printf("\n");
183
Stefan Roese2255b2d2006-10-10 12:36:02 +0200184 return 0;
185}
186
Nishanth Menon50657c22008-12-13 09:43:06 -0600187#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
188
Heiko Schocherff94bc42014-06-24 10:10:04 +0200189#define NAND_CMD_LOCK_TIGHT 0x2c
190#define NAND_CMD_LOCK_STATUS 0x7a
191
Stefan Roese2255b2d2006-10-10 12:36:02 +0200192/******************************************************************************
193 * Support for locking / unlocking operations of some NAND devices
194 *****************************************************************************/
195
Stefan Roese2255b2d2006-10-10 12:36:02 +0200196/**
197 * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
198 * state
199 *
Nishanth Menon50657c22008-12-13 09:43:06 -0600200 * @param mtd nand mtd instance
Stefan Roese2255b2d2006-10-10 12:36:02 +0200201 * @param tight bring device in lock tight mode
202 *
203 * @return 0 on success, -1 in case of error
204 *
205 * The lock / lock-tight command only applies to the whole chip. To get some
206 * parts of the chip lock and others unlocked use the following sequence:
207 *
208 * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
209 * - Call nand_unlock() once for each consecutive area to be unlocked
210 * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
211 *
212 * If the device is in lock-tight state software can't change the
213 * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
214 * calls will fail. It is only posible to leave lock-tight state by
215 * an hardware signal (low pulse on _WP pin) or by power down.
216 */
Nishanth Menon50657c22008-12-13 09:43:06 -0600217int nand_lock(struct mtd_info *mtd, int tight)
Stefan Roese2255b2d2006-10-10 12:36:02 +0200218{
219 int ret = 0;
220 int status;
Nishanth Menon50657c22008-12-13 09:43:06 -0600221 struct nand_chip *chip = mtd->priv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200222
223 /* select the NAND device */
Nishanth Menon50657c22008-12-13 09:43:06 -0600224 chip->select_chip(mtd, 0);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200225
Joe Hershbergerfcecb4a2013-02-08 09:27:19 +0000226 /* check the Lock Tight Status */
227 chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, 0);
228 if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
229 printf("nand_lock: Device is locked tight!\n");
230 ret = -1;
231 goto out;
232 }
233
Nishanth Menon50657c22008-12-13 09:43:06 -0600234 chip->cmdfunc(mtd,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200235 (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
236 -1, -1);
237
238 /* call wait ready function */
Nishanth Menon50657c22008-12-13 09:43:06 -0600239 status = chip->waitfunc(mtd, chip);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200240
241 /* see if device thinks it succeeded */
242 if (status & 0x01) {
243 ret = -1;
244 }
245
Joe Hershbergerfcecb4a2013-02-08 09:27:19 +0000246 out:
Stefan Roese2255b2d2006-10-10 12:36:02 +0200247 /* de-select the NAND device */
Nishanth Menon50657c22008-12-13 09:43:06 -0600248 chip->select_chip(mtd, -1);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200249 return ret;
250}
251
252/**
253 * nand_get_lock_status: - query current lock state from one page of NAND
254 * flash
255 *
Nishanth Menon50657c22008-12-13 09:43:06 -0600256 * @param mtd nand mtd instance
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000257 * @param offset page address to query (must be page-aligned!)
Stefan Roese2255b2d2006-10-10 12:36:02 +0200258 *
259 * @return -1 in case of error
260 * >0 lock status:
261 * bitfield with the following combinations:
262 * NAND_LOCK_STATUS_TIGHT: page in tight state
Stefan Roese2255b2d2006-10-10 12:36:02 +0200263 * NAND_LOCK_STATUS_UNLOCK: page unlocked
264 *
265 */
Jean-Christophe PLAGNIOL-VILLARD378adfc2009-05-16 14:27:40 +0200266int nand_get_lock_status(struct mtd_info *mtd, loff_t offset)
Stefan Roese2255b2d2006-10-10 12:36:02 +0200267{
268 int ret = 0;
269 int chipnr;
270 int page;
Nishanth Menon50657c22008-12-13 09:43:06 -0600271 struct nand_chip *chip = mtd->priv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200272
273 /* select the NAND device */
Nishanth Menon50657c22008-12-13 09:43:06 -0600274 chipnr = (int)(offset >> chip->chip_shift);
275 chip->select_chip(mtd, chipnr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200276
277
Nishanth Menon50657c22008-12-13 09:43:06 -0600278 if ((offset & (mtd->writesize - 1)) != 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000279 printf("nand_get_lock_status: "
Stefan Roese2255b2d2006-10-10 12:36:02 +0200280 "Start address must be beginning of "
281 "nand page!\n");
282 ret = -1;
283 goto out;
284 }
285
286 /* check the Lock Status */
Nishanth Menon50657c22008-12-13 09:43:06 -0600287 page = (int)(offset >> chip->page_shift);
288 chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200289
Nishanth Menon50657c22008-12-13 09:43:06 -0600290 ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT
Stefan Roese2255b2d2006-10-10 12:36:02 +0200291 | NAND_LOCK_STATUS_UNLOCK);
292
293 out:
294 /* de-select the NAND device */
Nishanth Menon50657c22008-12-13 09:43:06 -0600295 chip->select_chip(mtd, -1);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200296 return ret;
297}
298
299/**
300 * nand_unlock: - Unlock area of NAND pages
301 * only one consecutive area can be unlocked at one time!
302 *
Nishanth Menon50657c22008-12-13 09:43:06 -0600303 * @param mtd nand mtd instance
Stefan Roese2255b2d2006-10-10 12:36:02 +0200304 * @param start start byte address
305 * @param length number of bytes to unlock (must be a multiple of
William Juulcfa460a2007-10-31 13:53:06 +0100306 * page size nand->writesize)
Joe Hershbergereee623a2012-08-22 16:49:42 -0500307 * @param allexcept if set, unlock everything not selected
Stefan Roese2255b2d2006-10-10 12:36:02 +0200308 *
309 * @return 0 on success, -1 in case of error
310 */
Joe Hershbergere331ab22012-08-22 16:49:43 -0500311int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
312 int allexcept)
Stefan Roese2255b2d2006-10-10 12:36:02 +0200313{
314 int ret = 0;
315 int chipnr;
316 int status;
317 int page;
Nishanth Menon50657c22008-12-13 09:43:06 -0600318 struct nand_chip *chip = mtd->priv;
Joe Hershbergereee623a2012-08-22 16:49:42 -0500319
Tom Rini3ef1ead2013-12-16 09:59:34 -0500320 debug("nand_unlock%s: start: %08llx, length: %zd!\n",
Joe Hershbergereee623a2012-08-22 16:49:42 -0500321 allexcept ? " (allexcept)" : "", start, length);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200322
323 /* select the NAND device */
Nishanth Menon50657c22008-12-13 09:43:06 -0600324 chipnr = (int)(start >> chip->chip_shift);
325 chip->select_chip(mtd, chipnr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200326
327 /* check the WP bit */
Nishanth Menon50657c22008-12-13 09:43:06 -0600328 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
329 if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000330 printf("nand_unlock: Device is write protected!\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200331 ret = -1;
332 goto out;
333 }
334
Joe Hershbergerfcecb4a2013-02-08 09:27:19 +0000335 /* check the Lock Tight Status */
336 page = (int)(start >> chip->page_shift);
337 chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
338 if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
339 printf("nand_unlock: Device is locked tight!\n");
340 ret = -1;
341 goto out;
342 }
343
Nishanth Menon50657c22008-12-13 09:43:06 -0600344 if ((start & (mtd->erasesize - 1)) != 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000345 printf("nand_unlock: Start address must be beginning of "
Nishanth Menon50657c22008-12-13 09:43:06 -0600346 "nand block!\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200347 ret = -1;
348 goto out;
349 }
350
Nishanth Menon50657c22008-12-13 09:43:06 -0600351 if (length == 0 || (length & (mtd->erasesize - 1)) != 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000352 printf("nand_unlock: Length must be a multiple of nand block "
Nishanth Menon50657c22008-12-13 09:43:06 -0600353 "size %08x!\n", mtd->erasesize);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200354 ret = -1;
355 goto out;
356 }
357
Nishanth Menon50657c22008-12-13 09:43:06 -0600358 /*
359 * Set length so that the last address is set to the
360 * starting address of the last block
361 */
362 length -= mtd->erasesize;
363
Stefan Roese2255b2d2006-10-10 12:36:02 +0200364 /* submit address of first page to unlock */
Nishanth Menon50657c22008-12-13 09:43:06 -0600365 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200366
367 /* submit ADDRESS of LAST page to unlock */
Nishanth Menon50657c22008-12-13 09:43:06 -0600368 page += (int)(length >> chip->page_shift);
Joe Hershbergereee623a2012-08-22 16:49:42 -0500369
370 /*
371 * Page addresses for unlocking are supposed to be block-aligned.
372 * At least some NAND chips use the low bit to indicate that the
373 * page range should be inverted.
374 */
375 if (allexcept)
376 page |= 1;
377
Nishanth Menon50657c22008-12-13 09:43:06 -0600378 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200379
380 /* call wait ready function */
Nishanth Menon50657c22008-12-13 09:43:06 -0600381 status = chip->waitfunc(mtd, chip);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200382 /* see if device thinks it succeeded */
383 if (status & 0x01) {
384 /* there was an error */
385 ret = -1;
386 goto out;
387 }
388
389 out:
390 /* de-select the NAND device */
Nishanth Menon50657c22008-12-13 09:43:06 -0600391 chip->select_chip(mtd, -1);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200392 return ret;
393}
William Juulcfa460a2007-10-31 13:53:06 +0100394#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200395
Scott Wooddfbf6172008-06-12 13:20:16 -0500396/**
Scott Woodf9a52542010-07-30 16:11:41 -0500397 * check_skip_len
Scott Wooddfbf6172008-06-12 13:20:16 -0500398 *
Scott Woodf9a52542010-07-30 16:11:41 -0500399 * Check if there are any bad blocks, and whether length including bad
400 * blocks fits into device
Scott Wooddfbf6172008-06-12 13:20:16 -0500401 *
402 * @param nand NAND device
403 * @param offset offset in flash
404 * @param length image length
Tom Rinic39d6a02013-03-14 05:32:50 +0000405 * @param used length of flash needed for the requested length
Scott Woodf9a52542010-07-30 16:11:41 -0500406 * @return 0 if the image fits and there are no bad blocks
407 * 1 if the image fits, but there are bad blocks
408 * -1 if the image does not fit
Scott Wooddfbf6172008-06-12 13:20:16 -0500409 */
Tom Rinic39d6a02013-03-14 05:32:50 +0000410static int check_skip_len(nand_info_t *nand, loff_t offset, size_t length,
411 size_t *used)
Scott Wooddfbf6172008-06-12 13:20:16 -0500412{
Scott Wooddfbf6172008-06-12 13:20:16 -0500413 size_t len_excl_bad = 0;
Scott Woodf9a52542010-07-30 16:11:41 -0500414 int ret = 0;
Scott Wooddfbf6172008-06-12 13:20:16 -0500415
416 while (len_excl_bad < length) {
Scott Woodf9a52542010-07-30 16:11:41 -0500417 size_t block_len, block_off;
418 loff_t block_start;
Scott Wooddfbf6172008-06-12 13:20:16 -0500419
Daniel Hobi0ec81db2009-12-01 14:05:55 +0100420 if (offset >= nand->size)
Scott Woodf9a52542010-07-30 16:11:41 -0500421 return -1;
422
423 block_start = offset & ~(loff_t)(nand->erasesize - 1);
424 block_off = offset & (nand->erasesize - 1);
425 block_len = nand->erasesize - block_off;
426
427 if (!nand_block_isbad(nand, block_start))
428 len_excl_bad += block_len;
429 else
430 ret = 1;
431
432 offset += block_len;
Tom Rinic39d6a02013-03-14 05:32:50 +0000433 *used += block_len;
Scott Wooddfbf6172008-06-12 13:20:16 -0500434 }
435
Tom Rinic39d6a02013-03-14 05:32:50 +0000436 /* If the length is not a multiple of block_len, adjust. */
437 if (len_excl_bad > length)
438 *used -= (len_excl_bad - length);
439
Scott Woodf9a52542010-07-30 16:11:41 -0500440 return ret;
Scott Wooddfbf6172008-06-12 13:20:16 -0500441}
442
Ben Gardiner169d54d2011-06-14 16:35:06 -0400443#ifdef CONFIG_CMD_NAND_TRIMFFS
444static size_t drop_ffs(const nand_info_t *nand, const u_char *buf,
445 const size_t *len)
446{
htbegin453db362013-03-01 23:00:34 +0000447 size_t l = *len;
448 ssize_t i;
Ben Gardiner169d54d2011-06-14 16:35:06 -0400449
450 for (i = l - 1; i >= 0; i--)
451 if (buf[i] != 0xFF)
452 break;
453
454 /* The resulting length must be aligned to the minimum flash I/O size */
455 l = i + 1;
456 l = (l + nand->writesize - 1) / nand->writesize;
457 l *= nand->writesize;
458
459 /*
460 * since the input length may be unaligned, prevent access past the end
461 * of the buffer
462 */
463 return min(l, *len);
464}
465#endif
466
Scott Wooddfbf6172008-06-12 13:20:16 -0500467/**
Peter Tyser59b5a2a2015-02-03 11:58:12 -0600468 * nand_verify_page_oob:
469 *
470 * Verify a page of NAND flash, including the OOB.
471 * Reads page of NAND and verifies the contents and OOB against the
472 * values in ops.
473 *
474 * @param nand NAND device
475 * @param ops MTD operations, including data to verify
476 * @param ofs offset in flash
477 * @return 0 in case of success
478 */
479int nand_verify_page_oob(nand_info_t *nand, struct mtd_oob_ops *ops, loff_t ofs)
480{
481 int rval;
482 struct mtd_oob_ops vops;
483 size_t verlen = nand->writesize + nand->oobsize;
484
485 memcpy(&vops, ops, sizeof(vops));
486
Stephen Warren5e856fa2015-04-14 08:59:00 -0600487 vops.datbuf = memalign(ARCH_DMA_MINALIGN, verlen);
Peter Tyser59b5a2a2015-02-03 11:58:12 -0600488
489 if (!vops.datbuf)
490 return -ENOMEM;
491
492 vops.oobbuf = vops.datbuf + nand->writesize;
493
494 rval = mtd_read_oob(nand, ofs, &vops);
495 if (!rval)
496 rval = memcmp(ops->datbuf, vops.datbuf, vops.len);
497 if (!rval)
498 rval = memcmp(ops->oobbuf, vops.oobbuf, vops.ooblen);
499
500 free(vops.datbuf);
501
502 return rval ? -EIO : 0;
503}
504
505/**
506 * nand_verify:
507 *
508 * Verify a region of NAND flash.
509 * Reads NAND in page-sized chunks and verifies the contents against
510 * the contents of a buffer. The offset into the NAND must be
511 * page-aligned, and the function doesn't handle skipping bad blocks.
512 *
513 * @param nand NAND device
514 * @param ofs offset in flash
515 * @param len buffer length
516 * @param buf buffer to read from
517 * @return 0 in case of success
518 */
519int nand_verify(nand_info_t *nand, loff_t ofs, size_t len, u_char *buf)
520{
521 int rval = 0;
522 size_t verofs;
523 size_t verlen = nand->writesize;
Stephen Warren5e856fa2015-04-14 08:59:00 -0600524 uint8_t *verbuf = memalign(ARCH_DMA_MINALIGN, verlen);
Peter Tyser59b5a2a2015-02-03 11:58:12 -0600525
526 if (!verbuf)
527 return -ENOMEM;
528
529 /* Read the NAND back in page-size groups to limit malloc size */
530 for (verofs = ofs; verofs < ofs + len;
531 verofs += verlen, buf += verlen) {
532 verlen = min(nand->writesize, (uint32_t)(ofs + len - verofs));
533 rval = nand_read(nand, verofs, &verlen, verbuf);
534 if (!rval || (rval == -EUCLEAN))
535 rval = memcmp(buf, verbuf, verlen);
536
537 if (rval)
538 break;
539 }
540
541 free(verbuf);
542
543 return rval ? -EIO : 0;
544}
545
546
547
548/**
Scott Wooddfbf6172008-06-12 13:20:16 -0500549 * nand_write_skip_bad:
550 *
551 * Write image to NAND flash.
552 * Blocks that are marked bad are skipped and the is written to the next
553 * block instead as long as the image is short enough to fit even after
Tom Rinic39d6a02013-03-14 05:32:50 +0000554 * skipping the bad blocks. Due to bad blocks we may not be able to
555 * perform the requested write. In the case where the write would
556 * extend beyond the end of the NAND device, both length and actual (if
557 * not NULL) are set to 0. In the case where the write would extend
558 * beyond the limit we are passed, length is set to 0 and actual is set
559 * to the required length.
Scott Wooddfbf6172008-06-12 13:20:16 -0500560 *
561 * @param nand NAND device
562 * @param offset offset in flash
563 * @param length buffer length
Tom Rinic39d6a02013-03-14 05:32:50 +0000564 * @param actual set to size required to write length worth of
565 * buffer or 0 on error, if not NULL
566 * @param lim maximum size that actual may be in order to not
567 * exceed the buffer
Lei Wen47fc18f2011-01-06 11:11:58 +0800568 * @param buffer buffer to read from
Ben Gardinera6c9aa12011-05-24 10:18:35 -0400569 * @param flags flags modifying the behaviour of the write to NAND
Scott Wooddfbf6172008-06-12 13:20:16 -0500570 * @return 0 in case of success
571 */
Jean-Christophe PLAGNIOL-VILLARD378adfc2009-05-16 14:27:40 +0200572int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
Tom Rinic39d6a02013-03-14 05:32:50 +0000573 size_t *actual, loff_t lim, u_char *buffer, int flags)
Scott Wooddfbf6172008-06-12 13:20:16 -0500574{
Lei Wen47fc18f2011-01-06 11:11:58 +0800575 int rval = 0, blocksize;
Scott Wooddfbf6172008-06-12 13:20:16 -0500576 size_t left_to_write = *length;
Tom Rinic39d6a02013-03-14 05:32:50 +0000577 size_t used_for_write = 0;
Scott Wooddfbf6172008-06-12 13:20:16 -0500578 u_char *p_buffer = buffer;
Scott Woodf9a52542010-07-30 16:11:41 -0500579 int need_skip;
Scott Wooddfbf6172008-06-12 13:20:16 -0500580
Tom Rinic39d6a02013-03-14 05:32:50 +0000581 if (actual)
582 *actual = 0;
583
Peter Tyser004a1fd2015-02-03 11:58:16 -0600584 blocksize = nand->erasesize;
Lei Wen47fc18f2011-01-06 11:11:58 +0800585
Scott Woodf9a52542010-07-30 16:11:41 -0500586 /*
587 * nand_write() handles unaligned, partial page writes.
588 *
589 * We allow length to be unaligned, for convenience in
590 * using the $filesize variable.
591 *
592 * However, starting at an unaligned offset makes the
593 * semantics of bad block skipping ambiguous (really,
594 * you should only start a block skipping access at a
595 * partition boundary). So don't try to handle that.
596 */
597 if ((offset & (nand->writesize - 1)) != 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000598 printf("Attempt to write non page-aligned data\n");
Scott Woodf9a52542010-07-30 16:11:41 -0500599 *length = 0;
Scott Wooddfbf6172008-06-12 13:20:16 -0500600 return -EINVAL;
601 }
602
Tom Rinic39d6a02013-03-14 05:32:50 +0000603 need_skip = check_skip_len(nand, offset, *length, &used_for_write);
604
605 if (actual)
606 *actual = used_for_write;
607
Scott Woodf9a52542010-07-30 16:11:41 -0500608 if (need_skip < 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000609 printf("Attempt to write outside the flash area\n");
Scott Woodf9a52542010-07-30 16:11:41 -0500610 *length = 0;
Scott Wooddfbf6172008-06-12 13:20:16 -0500611 return -EINVAL;
612 }
613
Tom Rinic39d6a02013-03-14 05:32:50 +0000614 if (used_for_write > lim) {
615 puts("Size of write exceeds partition or device limit\n");
616 *length = 0;
617 return -EFBIG;
618 }
619
Ben Gardiner169d54d2011-06-14 16:35:06 -0400620 if (!need_skip && !(flags & WITH_DROP_FFS)) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000621 rval = nand_write(nand, offset, length, buffer);
Peter Tyser59b5a2a2015-02-03 11:58:12 -0600622
623 if ((flags & WITH_WR_VERIFY) && !rval)
624 rval = nand_verify(nand, offset, *length, buffer);
625
Scott Woodf9a52542010-07-30 16:11:41 -0500626 if (rval == 0)
627 return 0;
Scott Wood2077e342008-11-25 10:47:02 -0600628
Scott Woodf9a52542010-07-30 16:11:41 -0500629 *length = 0;
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000630 printf("NAND write to offset %llx failed %d\n",
Scott Woodf9a52542010-07-30 16:11:41 -0500631 offset, rval);
Scott Wood2077e342008-11-25 10:47:02 -0600632 return rval;
Scott Wooddfbf6172008-06-12 13:20:16 -0500633 }
634
635 while (left_to_write > 0) {
636 size_t block_offset = offset & (nand->erasesize - 1);
Ben Gardiner169d54d2011-06-14 16:35:06 -0400637 size_t write_size, truncated_write_size;
Scott Wooddfbf6172008-06-12 13:20:16 -0500638
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000639 WATCHDOG_RESET();
Giulio Benetti1fc1d9a2009-07-31 17:30:34 -0500640
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000641 if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
642 printf("Skip bad block 0x%08llx\n",
Scott Wooddfbf6172008-06-12 13:20:16 -0500643 offset & ~(nand->erasesize - 1));
644 offset += nand->erasesize - block_offset;
645 continue;
646 }
647
Lei Wen47fc18f2011-01-06 11:11:58 +0800648 if (left_to_write < (blocksize - block_offset))
Scott Wooddfbf6172008-06-12 13:20:16 -0500649 write_size = left_to_write;
650 else
Lei Wen47fc18f2011-01-06 11:11:58 +0800651 write_size = blocksize - block_offset;
Scott Wooddfbf6172008-06-12 13:20:16 -0500652
Peter Tyser004a1fd2015-02-03 11:58:16 -0600653 truncated_write_size = write_size;
Ben Gardiner169d54d2011-06-14 16:35:06 -0400654#ifdef CONFIG_CMD_NAND_TRIMFFS
Peter Tyser004a1fd2015-02-03 11:58:16 -0600655 if (flags & WITH_DROP_FFS)
656 truncated_write_size = drop_ffs(nand, p_buffer,
657 &write_size);
Ben Gardiner169d54d2011-06-14 16:35:06 -0400658#endif
659
Peter Tyser004a1fd2015-02-03 11:58:16 -0600660 rval = nand_write(nand, offset, &truncated_write_size,
661 p_buffer);
Peter Tyser59b5a2a2015-02-03 11:58:12 -0600662
Peter Tyser004a1fd2015-02-03 11:58:16 -0600663 if ((flags & WITH_WR_VERIFY) && !rval)
664 rval = nand_verify(nand, offset,
665 truncated_write_size, p_buffer);
Peter Tyser59b5a2a2015-02-03 11:58:12 -0600666
Peter Tyser004a1fd2015-02-03 11:58:16 -0600667 offset += write_size;
668 p_buffer += write_size;
Lei Wen47fc18f2011-01-06 11:11:58 +0800669
Scott Wooddfbf6172008-06-12 13:20:16 -0500670 if (rval != 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000671 printf("NAND write to offset %llx failed %d\n",
Wolfgang Denk4b070802008-08-14 14:41:06 +0200672 offset, rval);
Scott Wooddfbf6172008-06-12 13:20:16 -0500673 *length -= left_to_write;
674 return rval;
675 }
676
677 left_to_write -= write_size;
Scott Wooddfbf6172008-06-12 13:20:16 -0500678 }
679
680 return 0;
681}
682
683/**
684 * nand_read_skip_bad:
685 *
686 * Read image from NAND flash.
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000687 * Blocks that are marked bad are skipped and the next block is read
Tom Rinic39d6a02013-03-14 05:32:50 +0000688 * instead as long as the image is short enough to fit even after
689 * skipping the bad blocks. Due to bad blocks we may not be able to
690 * perform the requested read. In the case where the read would extend
691 * beyond the end of the NAND device, both length and actual (if not
692 * NULL) are set to 0. In the case where the read would extend beyond
693 * the limit we are passed, length is set to 0 and actual is set to the
694 * required length.
Scott Wooddfbf6172008-06-12 13:20:16 -0500695 *
696 * @param nand NAND device
697 * @param offset offset in flash
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000698 * @param length buffer length, on return holds number of read bytes
Tom Rinic39d6a02013-03-14 05:32:50 +0000699 * @param actual set to size required to read length worth of buffer or 0
700 * on error, if not NULL
701 * @param lim maximum size that actual may be in order to not exceed the
702 * buffer
Scott Wooddfbf6172008-06-12 13:20:16 -0500703 * @param buffer buffer to write to
704 * @return 0 in case of success
705 */
Jean-Christophe PLAGNIOL-VILLARD378adfc2009-05-16 14:27:40 +0200706int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
Tom Rinic39d6a02013-03-14 05:32:50 +0000707 size_t *actual, loff_t lim, u_char *buffer)
Scott Wooddfbf6172008-06-12 13:20:16 -0500708{
709 int rval;
710 size_t left_to_read = *length;
Tom Rinic39d6a02013-03-14 05:32:50 +0000711 size_t used_for_read = 0;
Scott Wooddfbf6172008-06-12 13:20:16 -0500712 u_char *p_buffer = buffer;
Scott Woodf9a52542010-07-30 16:11:41 -0500713 int need_skip;
Scott Wooddfbf6172008-06-12 13:20:16 -0500714
Scott Woodf9a52542010-07-30 16:11:41 -0500715 if ((offset & (nand->writesize - 1)) != 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000716 printf("Attempt to read non page-aligned data\n");
Scott Woodf9a52542010-07-30 16:11:41 -0500717 *length = 0;
Tom Rinic39d6a02013-03-14 05:32:50 +0000718 if (actual)
719 *actual = 0;
Scott Wooddfbf6172008-06-12 13:20:16 -0500720 return -EINVAL;
721 }
722
Tom Rinic39d6a02013-03-14 05:32:50 +0000723 need_skip = check_skip_len(nand, offset, *length, &used_for_read);
724
725 if (actual)
726 *actual = used_for_read;
727
Scott Woodf9a52542010-07-30 16:11:41 -0500728 if (need_skip < 0) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000729 printf("Attempt to read outside the flash area\n");
Scott Woodf9a52542010-07-30 16:11:41 -0500730 *length = 0;
731 return -EINVAL;
732 }
733
Tom Rinic39d6a02013-03-14 05:32:50 +0000734 if (used_for_read > lim) {
735 puts("Size of read exceeds partition or device limit\n");
736 *length = 0;
737 return -EFBIG;
738 }
739
Scott Woodf9a52542010-07-30 16:11:41 -0500740 if (!need_skip) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000741 rval = nand_read(nand, offset, length, buffer);
Valeriy Glushkov3ebf70d2009-07-14 13:51:10 +0300742 if (!rval || rval == -EUCLEAN)
743 return 0;
Scott Woodf9a52542010-07-30 16:11:41 -0500744
745 *length = 0;
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000746 printf("NAND read from offset %llx failed %d\n",
Valeriy Glushkov3ebf70d2009-07-14 13:51:10 +0300747 offset, rval);
Scott Wood2077e342008-11-25 10:47:02 -0600748 return rval;
Scott Wooddfbf6172008-06-12 13:20:16 -0500749 }
750
751 while (left_to_read > 0) {
752 size_t block_offset = offset & (nand->erasesize - 1);
753 size_t read_length;
754
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000755 WATCHDOG_RESET();
Giulio Benetti1fc1d9a2009-07-31 17:30:34 -0500756
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000757 if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
758 printf("Skipping bad block 0x%08llx\n",
Scott Wooddfbf6172008-06-12 13:20:16 -0500759 offset & ~(nand->erasesize - 1));
760 offset += nand->erasesize - block_offset;
761 continue;
762 }
763
764 if (left_to_read < (nand->erasesize - block_offset))
765 read_length = left_to_read;
766 else
767 read_length = nand->erasesize - block_offset;
768
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000769 rval = nand_read(nand, offset, &read_length, p_buffer);
Valeriy Glushkov3ebf70d2009-07-14 13:51:10 +0300770 if (rval && rval != -EUCLEAN) {
Benoît Thébaudeaubd742802012-11-05 10:15:46 +0000771 printf("NAND read from offset %llx failed %d\n",
Wolfgang Denk4b070802008-08-14 14:41:06 +0200772 offset, rval);
Scott Wooddfbf6172008-06-12 13:20:16 -0500773 *length -= left_to_read;
774 return rval;
775 }
776
777 left_to_read -= read_length;
778 offset += read_length;
779 p_buffer += read_length;
780 }
781
782 return 0;
783}
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100784
785#ifdef CONFIG_CMD_NAND_TORTURE
786
787/**
788 * check_pattern:
789 *
790 * Check if buffer contains only a certain byte pattern.
791 *
792 * @param buf buffer to check
793 * @param patt the pattern to check
794 * @param size buffer size in bytes
795 * @return 1 if there are only patt bytes in buf
796 * 0 if something else was found
797 */
798static int check_pattern(const u_char *buf, u_char patt, int size)
799{
800 int i;
801
802 for (i = 0; i < size; i++)
803 if (buf[i] != patt)
804 return 0;
805 return 1;
806}
807
808/**
809 * nand_torture:
810 *
811 * Torture a block of NAND flash.
812 * This is useful to determine if a block that caused a write error is still
813 * good or should be marked as bad.
814 *
815 * @param nand NAND device
816 * @param offset offset in flash
817 * @return 0 if the block is still good
818 */
819int nand_torture(nand_info_t *nand, loff_t offset)
820{
821 u_char patterns[] = {0xa5, 0x5a, 0x00};
822 struct erase_info instr = {
823 .mtd = nand,
824 .addr = offset,
825 .len = nand->erasesize,
826 };
827 size_t retlen;
828 int err, ret = -1, i, patt_count;
829 u_char *buf;
830
831 if ((offset & (nand->erasesize - 1)) != 0) {
832 puts("Attempt to torture a block at a non block-aligned offset\n");
833 return -EINVAL;
834 }
835
836 if (offset + nand->erasesize > nand->size) {
837 puts("Attempt to torture a block outside the flash area\n");
838 return -EINVAL;
839 }
840
841 patt_count = ARRAY_SIZE(patterns);
842
Marcel Ziswiler45196682015-08-18 13:06:37 +0200843 buf = malloc_cache_aligned(nand->erasesize);
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100844 if (buf == NULL) {
845 puts("Out of memory for erase block buffer\n");
846 return -ENOMEM;
847 }
848
849 for (i = 0; i < patt_count; i++) {
850 err = nand->erase(nand, &instr);
851 if (err) {
852 printf("%s: erase() failed for block at 0x%llx: %d\n",
853 nand->name, instr.addr, err);
854 goto out;
855 }
856
857 /* Make sure the block contains only 0xff bytes */
858 err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
859 if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
860 printf("%s: read() failed for block at 0x%llx: %d\n",
861 nand->name, instr.addr, err);
862 goto out;
863 }
864
865 err = check_pattern(buf, 0xff, nand->erasesize);
866 if (!err) {
867 printf("Erased block at 0x%llx, but a non-0xff byte was found\n",
868 offset);
869 ret = -EIO;
870 goto out;
871 }
872
873 /* Write a pattern and check it */
874 memset(buf, patterns[i], nand->erasesize);
875 err = nand->write(nand, offset, nand->erasesize, &retlen, buf);
876 if (err || retlen != nand->erasesize) {
877 printf("%s: write() failed for block at 0x%llx: %d\n",
878 nand->name, instr.addr, err);
879 goto out;
880 }
881
882 err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
883 if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
884 printf("%s: read() failed for block at 0x%llx: %d\n",
885 nand->name, instr.addr, err);
886 goto out;
887 }
888
889 err = check_pattern(buf, patterns[i], nand->erasesize);
890 if (!err) {
891 printf("Pattern 0x%.2x checking failed for block at "
892 "0x%llx\n", patterns[i], offset);
893 ret = -EIO;
894 goto out;
895 }
896 }
897
898 ret = 0;
899
900out:
901 free(buf);
902 return ret;
903}
904
905#endif